blob: 725258c20746c02dbbdaaa9d8e589a8f16b9fe5f [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg66e67e42012-01-20 13:55:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_MAX_TRIES 3
35#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37#define IEEE80211_ASSOC_MAX_TRIES 3
38
Ben Greear180205b2011-02-04 15:30:24 -080039static int max_nullfunc_tries = 2;
40module_param(max_nullfunc_tries, int, 0644);
41MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44static int max_probe_tries = 5;
45module_param(max_probe_tries, int, 0644);
46MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020048
49/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010050 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020056 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010057#define IEEE80211_BEACON_LOSS_COUNT 7
58
Johannes Bergb291ba12009-07-10 15:29:03 +020059/*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030063#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
Ben Greear180205b2011-02-04 15:30:24 -080069static int probe_wait_ms = 500;
70module_param(probe_wait_ms, int, 0644);
71MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070074
Jouni Malinen17e4ec12010-03-29 23:28:30 -070075/*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80#define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
Jouni Malinen391a2002010-08-27 22:22:00 +030082/*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
Johannes Berg5bb644a2009-05-17 11:40:42 +020088#define TMR_RUNNING_TIMER 0
89#define TMR_RUNNING_CHANSW 1
90
Johannes Berg5fef7db2012-02-24 13:50:52 +010091#define DEAUTH_DISASSOC_LEN (24 /* hdr */ + 2 /* reason */)
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200117};
118
Johannes Berg5484e232008-09-08 17:44:27 +0200119/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200122 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123}
124
Johannes Bergca386f32009-07-10 02:39:48 +0200125/*
126 * We can have multiple work items (and connection probing)
127 * scheduling this timer, but we need to take care to only
128 * reschedule it when it should fire _earlier_ than it was
129 * asked for before, or if it's not pending right now. This
130 * function ensures that. Note that it then is required to
131 * run this function for all timeouts after the first one
132 * has happened -- the work that runs from this timer will
133 * do that.
134 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100135static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200136{
137 ASSERT_MGD_MTX(ifmgd);
138
139 if (!timer_pending(&ifmgd->timer) ||
140 time_before(timeout, ifmgd->timer.expires))
141 mod_timer(&ifmgd->timer, timeout);
142}
143
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400144void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145{
Johannes Bergc1288b12012-01-19 09:29:57 +0100146 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200147 return;
148
149 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100150 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200151}
152
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400153void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
154{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400155 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
156
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100157 if (unlikely(!sdata->u.mgd.associated))
158 return;
159
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400160 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
161 return;
162
163 mod_timer(&sdata->u.mgd.conn_mon_timer,
164 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400165
166 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400167}
168
Johannes Berg5484e232008-09-08 17:44:27 +0200169static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200170{
Johannes Berg5484e232008-09-08 17:44:27 +0200171 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200172}
173
Johannes Berg24398e32012-03-28 10:58:36 +0200174static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
175 struct ieee80211_ht_operation *ht_oper,
176 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200177{
178 struct ieee80211_local *local = sdata->local;
179 struct ieee80211_supported_band *sband;
Johannes Bergd5522e02009-03-30 13:23:35 +0200180 struct sta_info *sta;
181 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200182 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200183 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200184
185 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200186
Johannes Berg64f68e52012-03-28 10:58:37 +0200187 switch (sdata->vif.bss_conf.channel_type) {
188 case NL80211_CHAN_HT40PLUS:
189 if (local->hw.conf.channel->flags & IEEE80211_CHAN_NO_HT40PLUS)
190 disable_40 = true;
191 break;
192 case NL80211_CHAN_HT40MINUS:
193 if (local->hw.conf.channel->flags & IEEE80211_CHAN_NO_HT40MINUS)
194 disable_40 = true;
195 break;
196 default:
197 break;
198 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200199
Johannes Berg24398e32012-03-28 10:58:36 +0200200 /* This can change during the lifetime of the BSS */
201 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200202 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200203
Johannes Berg64f68e52012-03-28 10:58:37 +0200204 mutex_lock(&local->sta_mtx);
205 sta = sta_info_get(sdata, bssid);
206
207 WARN_ON_ONCE(!sta);
208
209 if (sta && !sta->supports_40mhz)
210 disable_40 = true;
211
212 if (sta && (!reconfig ||
Rajkumar Manoharan91a00992012-04-25 20:24:24 +0530213 (disable_40 != !(sta->sta.ht_cap.cap &
Johannes Berg64f68e52012-03-28 10:58:37 +0200214 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530215
Johannes Berg64f68e52012-03-28 10:58:37 +0200216 if (disable_40)
217 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
218 else
219 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530220
Johannes Berg64f68e52012-03-28 10:58:37 +0200221 rate_control_rate_update(local, sband, sta,
Johannes Berg8f727ef2012-03-30 08:43:32 +0200222 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200223 }
Johannes Berg64f68e52012-03-28 10:58:37 +0200224 mutex_unlock(&local->sta_mtx);
Johannes Bergd5522e02009-03-30 13:23:35 +0200225
Johannes Berg074d46d2012-03-15 19:45:16 +0100226 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200227
228 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200229 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200230 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200231 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200232 }
233
234 return changed;
235}
236
Johannes Berg9c6bd792008-09-11 00:01:52 +0200237/* frame sending functions */
238
Johannes Berg66e67e42012-01-20 13:55:27 +0100239static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
240 struct ieee80211_supported_band *sband,
241 u32 *rates)
242{
243 int i, j, count;
244 *rates = 0;
245 count = 0;
246 for (i = 0; i < supp_rates_len; i++) {
247 int rate = (supp_rates[i] & 0x7F) * 5;
248
249 for (j = 0; j < sband->n_bitrates; j++)
250 if (sband->bitrates[j].bitrate == rate) {
251 *rates |= BIT(j);
252 count++;
253 break;
254 }
255 }
256
257 return count;
258}
259
260static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200261 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100262 struct ieee80211_supported_band *sband,
263 struct ieee80211_channel *channel,
264 enum ieee80211_smps_mode smps)
265{
Johannes Berg66e67e42012-01-20 13:55:27 +0100266 u8 *pos;
267 u32 flags = channel->flags;
268 u16 cap;
269 struct ieee80211_sta_ht_cap ht_cap;
270
271 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
272
Johannes Berg66e67e42012-01-20 13:55:27 +0100273 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
274 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
275
Johannes Berg66e67e42012-01-20 13:55:27 +0100276 /* determine capability flags */
277 cap = ht_cap.cap;
278
Johannes Berg9dde6422012-05-16 23:43:19 +0200279 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100280 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
281 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
282 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
283 cap &= ~IEEE80211_HT_CAP_SGI_40;
284 }
285 break;
286 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
287 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
288 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
289 cap &= ~IEEE80211_HT_CAP_SGI_40;
290 }
291 break;
292 }
293
Johannes Berg24398e32012-03-28 10:58:36 +0200294 /*
295 * If 40 MHz was disabled associate as though we weren't
296 * capable of 40 MHz -- some broken APs will never fall
297 * back to trying to transmit in 20 MHz.
298 */
299 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
300 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
301 cap &= ~IEEE80211_HT_CAP_SGI_40;
302 }
303
Johannes Berg66e67e42012-01-20 13:55:27 +0100304 /* set SM PS mode properly */
305 cap &= ~IEEE80211_HT_CAP_SM_PS;
306 switch (smps) {
307 case IEEE80211_SMPS_AUTOMATIC:
308 case IEEE80211_SMPS_NUM_MODES:
309 WARN_ON(1);
310 case IEEE80211_SMPS_OFF:
311 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
312 IEEE80211_HT_CAP_SM_PS_SHIFT;
313 break;
314 case IEEE80211_SMPS_STATIC:
315 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
316 IEEE80211_HT_CAP_SM_PS_SHIFT;
317 break;
318 case IEEE80211_SMPS_DYNAMIC:
319 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
320 IEEE80211_HT_CAP_SM_PS_SHIFT;
321 break;
322 }
323
324 /* reserve and fill IE */
325 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
326 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
327}
328
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000329static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
330 struct sk_buff *skb,
331 struct ieee80211_supported_band *sband)
332{
333 u8 *pos;
334 u32 cap;
335 struct ieee80211_sta_vht_cap vht_cap;
336
337 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
338
339 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
340
341 /* determine capability flags */
342 cap = vht_cap.cap;
343
344 /* reserve and fill IE */
345 pos = skb_put(skb, sizeof(struct ieee80211_vht_capabilities) + 2);
346 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
347}
348
Johannes Berg66e67e42012-01-20 13:55:27 +0100349static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
350{
351 struct ieee80211_local *local = sdata->local;
352 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
353 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
354 struct sk_buff *skb;
355 struct ieee80211_mgmt *mgmt;
356 u8 *pos, qos_info;
357 size_t offset = 0, noffset;
358 int i, count, rates_len, supp_rates_len;
359 u16 capab;
360 struct ieee80211_supported_band *sband;
361 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100362
363 lockdep_assert_held(&ifmgd->mtx);
364
365 sband = local->hw.wiphy->bands[local->oper_channel->band];
366
367 if (assoc_data->supp_rates_len) {
368 /*
369 * Get all rates supported by the device and the AP as
370 * some APs don't like getting a superset of their rates
371 * in the association request (e.g. D-Link DAP 1353 in
372 * b-only mode)...
373 */
374 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
375 assoc_data->supp_rates_len,
376 sband, &rates);
377 } else {
378 /*
379 * In case AP not provide any supported rates information
380 * before association, we send information element(s) with
381 * all rates that we support.
382 */
383 rates = ~0;
384 rates_len = sband->n_bitrates;
385 }
386
387 skb = alloc_skb(local->hw.extra_tx_headroom +
388 sizeof(*mgmt) + /* bit too much but doesn't matter */
389 2 + assoc_data->ssid_len + /* SSID */
390 4 + rates_len + /* (extended) rates */
391 4 + /* power capability */
392 2 + 2 * sband->n_channels + /* supported channels */
393 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000394 2 + sizeof(struct ieee80211_vht_capabilities) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100395 assoc_data->ie_len + /* extra IEs */
396 9, /* WMM */
397 GFP_KERNEL);
398 if (!skb)
399 return;
400
401 skb_reserve(skb, local->hw.extra_tx_headroom);
402
403 capab = WLAN_CAPABILITY_ESS;
404
405 if (sband->band == IEEE80211_BAND_2GHZ) {
406 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
407 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
408 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
409 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
410 }
411
412 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
413 capab |= WLAN_CAPABILITY_PRIVACY;
414
415 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
416 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
417 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
418
419 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
420 memset(mgmt, 0, 24);
421 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
422 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
423 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
424
425 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
426 skb_put(skb, 10);
427 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
428 IEEE80211_STYPE_REASSOC_REQ);
429 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
430 mgmt->u.reassoc_req.listen_interval =
431 cpu_to_le16(local->hw.conf.listen_interval);
432 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
433 ETH_ALEN);
434 } else {
435 skb_put(skb, 4);
436 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
437 IEEE80211_STYPE_ASSOC_REQ);
438 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
439 mgmt->u.assoc_req.listen_interval =
440 cpu_to_le16(local->hw.conf.listen_interval);
441 }
442
443 /* SSID */
444 pos = skb_put(skb, 2 + assoc_data->ssid_len);
445 *pos++ = WLAN_EID_SSID;
446 *pos++ = assoc_data->ssid_len;
447 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
448
449 /* add all rates which were marked to be used above */
450 supp_rates_len = rates_len;
451 if (supp_rates_len > 8)
452 supp_rates_len = 8;
453
454 pos = skb_put(skb, supp_rates_len + 2);
455 *pos++ = WLAN_EID_SUPP_RATES;
456 *pos++ = supp_rates_len;
457
458 count = 0;
459 for (i = 0; i < sband->n_bitrates; i++) {
460 if (BIT(i) & rates) {
461 int rate = sband->bitrates[i].bitrate;
462 *pos++ = (u8) (rate / 5);
463 if (++count == 8)
464 break;
465 }
466 }
467
468 if (rates_len > count) {
469 pos = skb_put(skb, rates_len - count + 2);
470 *pos++ = WLAN_EID_EXT_SUPP_RATES;
471 *pos++ = rates_len - count;
472
473 for (i++; i < sband->n_bitrates; i++) {
474 if (BIT(i) & rates) {
475 int rate = sband->bitrates[i].bitrate;
476 *pos++ = (u8) (rate / 5);
477 }
478 }
479 }
480
481 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
482 /* 1. power capabilities */
483 pos = skb_put(skb, 4);
484 *pos++ = WLAN_EID_PWR_CAPABILITY;
485 *pos++ = 2;
486 *pos++ = 0; /* min tx power */
487 *pos++ = local->oper_channel->max_power; /* max tx power */
488
489 /* 2. supported channels */
490 /* TODO: get this in reg domain format */
491 pos = skb_put(skb, 2 * sband->n_channels + 2);
492 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
493 *pos++ = 2 * sband->n_channels;
494 for (i = 0; i < sband->n_channels; i++) {
495 *pos++ = ieee80211_frequency_to_channel(
496 sband->channels[i].center_freq);
497 *pos++ = 1; /* one channel in the subband*/
498 }
499 }
500
501 /* if present, add any custom IEs that go before HT */
502 if (assoc_data->ie_len && assoc_data->ie) {
503 static const u8 before_ht[] = {
504 WLAN_EID_SSID,
505 WLAN_EID_SUPP_RATES,
506 WLAN_EID_EXT_SUPP_RATES,
507 WLAN_EID_PWR_CAPABILITY,
508 WLAN_EID_SUPPORTED_CHANNELS,
509 WLAN_EID_RSN,
510 WLAN_EID_QOS_CAPA,
511 WLAN_EID_RRM_ENABLED_CAPABILITIES,
512 WLAN_EID_MOBILITY_DOMAIN,
513 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
514 };
515 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
516 before_ht, ARRAY_SIZE(before_ht),
517 offset);
518 pos = skb_put(skb, noffset - offset);
519 memcpy(pos, assoc_data->ie + offset, noffset - offset);
520 offset = noffset;
521 }
522
Johannes Berg4e74bfd2012-03-08 15:02:04 +0100523 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg9dde6422012-05-16 23:43:19 +0200524 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100525 sband, local->oper_channel, ifmgd->ap_smps);
526
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000527 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
528 ieee80211_add_vht_ie(sdata, skb, sband);
529
Johannes Berg66e67e42012-01-20 13:55:27 +0100530 /* if present, add any custom non-vendor IEs that go after HT */
531 if (assoc_data->ie_len && assoc_data->ie) {
532 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
533 assoc_data->ie_len,
534 offset);
535 pos = skb_put(skb, noffset - offset);
536 memcpy(pos, assoc_data->ie + offset, noffset - offset);
537 offset = noffset;
538 }
539
Johannes Berg76f03032012-03-08 15:02:05 +0100540 if (assoc_data->wmm) {
541 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200542 qos_info = ifmgd->uapsd_queues;
543 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100544 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
545 } else {
546 qos_info = 0;
547 }
548
549 pos = skb_put(skb, 9);
550 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
551 *pos++ = 7; /* len */
552 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
553 *pos++ = 0x50;
554 *pos++ = 0xf2;
555 *pos++ = 2; /* WME */
556 *pos++ = 0; /* WME info */
557 *pos++ = 1; /* WME ver */
558 *pos++ = qos_info;
559 }
560
561 /* add any remaining custom (i.e. vendor specific here) IEs */
562 if (assoc_data->ie_len && assoc_data->ie) {
563 noffset = assoc_data->ie_len;
564 pos = skb_put(skb, noffset - offset);
565 memcpy(pos, assoc_data->ie + offset, noffset - offset);
566 }
567
Johannes Berga1845fc2012-06-27 13:18:36 +0200568 drv_mgd_prepare_tx(local, sdata);
569
Johannes Berg66e67e42012-01-20 13:55:27 +0100570 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
571 ieee80211_tx_skb(sdata, skb);
572}
573
Johannes Bergef422bc2008-09-09 10:58:25 +0200574static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +0100575 const u8 *bssid, u16 stype,
Johannes Berg5fef7db2012-02-24 13:50:52 +0100576 u16 reason, bool send_frame,
577 u8 *frame_buf)
Johannes Berg9ac19a92008-09-09 10:57:09 +0200578{
579 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100580 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200581 struct sk_buff *skb;
Johannes Berg5fef7db2012-02-24 13:50:52 +0100582 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
Johannes Berg9ac19a92008-09-09 10:57:09 +0200583
Johannes Berg5fef7db2012-02-24 13:50:52 +0100584 /* build frame */
585 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
586 mgmt->duration = 0; /* initialize only */
587 mgmt->seq_ctrl = 0; /* initialize only */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200588 memcpy(mgmt->da, bssid, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +0100589 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200590 memcpy(mgmt->bssid, bssid, ETH_ALEN);
Johannes Bergef422bc2008-09-09 10:58:25 +0200591 /* u.deauth.reason_code == u.disassoc.reason_code */
Johannes Berg9ac19a92008-09-09 10:57:09 +0200592 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
593
Johannes Berg5fef7db2012-02-24 13:50:52 +0100594 if (send_frame) {
595 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
596 DEAUTH_DISASSOC_LEN);
597 if (!skb)
598 return;
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300599
Johannes Berg5fef7db2012-02-24 13:50:52 +0100600 skb_reserve(skb, local->hw.extra_tx_headroom);
601
602 /* copy in frame */
603 memcpy(skb_put(skb, DEAUTH_DISASSOC_LEN),
604 mgmt, DEAUTH_DISASSOC_LEN);
605
606 if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
607 IEEE80211_SKB_CB(skb)->flags |=
608 IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berga1845fc2012-06-27 13:18:36 +0200609
610 drv_mgd_prepare_tx(local, sdata);
611
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300612 ieee80211_tx_skb(sdata, skb);
Johannes Berg5fef7db2012-02-24 13:50:52 +0100613 }
Johannes Berg9ac19a92008-09-09 10:57:09 +0200614}
615
Kalle Valo572e0012009-02-10 17:09:31 +0200616void ieee80211_send_pspoll(struct ieee80211_local *local,
617 struct ieee80211_sub_if_data *sdata)
618{
Kalle Valo572e0012009-02-10 17:09:31 +0200619 struct ieee80211_pspoll *pspoll;
620 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200621
Kalle Valod8cd1892010-01-05 20:16:26 +0200622 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
623 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200624 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200625
Kalle Valod8cd1892010-01-05 20:16:26 +0200626 pspoll = (struct ieee80211_pspoll *) skb->data;
627 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200628
Johannes Berg62ae67b2009-11-18 18:42:05 +0100629 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
630 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200631}
632
Johannes Berg965beda2009-04-16 13:17:24 +0200633void ieee80211_send_nullfunc(struct ieee80211_local *local,
634 struct ieee80211_sub_if_data *sdata,
635 int powersave)
636{
637 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200638 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530639 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200640
Kalle Valod8cd1892010-01-05 20:16:26 +0200641 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
642 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200643 return;
644
Kalle Valod8cd1892010-01-05 20:16:26 +0200645 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200646 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200647 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200648
Johannes Berg62ae67b2009-11-18 18:42:05 +0100649 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530650 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
651 IEEE80211_STA_CONNECTION_POLL))
652 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
653
Johannes Berg62ae67b2009-11-18 18:42:05 +0100654 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200655}
656
Felix Fietkaud5242152010-01-08 18:06:26 +0100657static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
658 struct ieee80211_sub_if_data *sdata)
659{
660 struct sk_buff *skb;
661 struct ieee80211_hdr *nullfunc;
662 __le16 fc;
663
664 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
665 return;
666
667 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700668 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100669 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700670
Felix Fietkaud5242152010-01-08 18:06:26 +0100671 skb_reserve(skb, local->hw.extra_tx_headroom);
672
673 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
674 memset(nullfunc, 0, 30);
675 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
676 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
677 nullfunc->frame_control = fc;
678 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
679 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
680 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
681 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
682
683 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
684 ieee80211_tx_skb(sdata, skb);
685}
686
Johannes Bergcc32abd2009-05-15 11:52:31 +0200687/* spectrum management related things */
688static void ieee80211_chswitch_work(struct work_struct *work)
689{
690 struct ieee80211_sub_if_data *sdata =
691 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200692 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
693
Johannes Berg9607e6b2009-12-23 13:15:31 +0100694 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200695 return;
696
Johannes Berg77fdaa12009-07-07 03:45:17 +0200697 mutex_lock(&ifmgd->mtx);
698 if (!ifmgd->associated)
699 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200700
701 sdata->local->oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200702 if (!sdata->local->ops->channel_switch) {
703 /* call "hw_config" only if doing sw channel switch */
704 ieee80211_hw_config(sdata->local,
705 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300706 } else {
707 /* update the device channel directly */
708 sdata->local->hw.conf.channel = sdata->local->oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200709 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200710
Johannes Berg77fdaa12009-07-07 03:45:17 +0200711 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100712 ifmgd->associated->channel = sdata->local->oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200713
Johannes Bergcc32abd2009-05-15 11:52:31 +0200714 ieee80211_wake_queues_by_reason(&sdata->local->hw,
715 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200716 out:
717 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
718 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200719}
720
Johannes Berg5ce6e432010-05-11 16:20:57 +0200721void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
722{
723 struct ieee80211_sub_if_data *sdata;
724 struct ieee80211_if_managed *ifmgd;
725
726 sdata = vif_to_sdata(vif);
727 ifmgd = &sdata->u.mgd;
728
729 trace_api_chswitch_done(sdata, success);
730 if (!success) {
731 /*
732 * If the channel switch was not successful, stay
733 * around on the old channel. We currently lack
734 * good handling of this situation, possibly we
735 * should just drop the association.
736 */
737 sdata->local->csa_channel = sdata->local->oper_channel;
738 }
739
740 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
741}
742EXPORT_SYMBOL(ieee80211_chswitch_done);
743
Johannes Bergcc32abd2009-05-15 11:52:31 +0200744static void ieee80211_chswitch_timer(unsigned long data)
745{
746 struct ieee80211_sub_if_data *sdata =
747 (struct ieee80211_sub_if_data *) data;
748 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
749
Johannes Berg5bb644a2009-05-17 11:40:42 +0200750 if (sdata->local->quiescing) {
751 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
752 return;
753 }
754
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400755 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200756}
757
758void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
759 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200760 struct ieee80211_bss *bss,
761 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200762{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100763 struct cfg80211_bss *cbss =
764 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200765 struct ieee80211_channel *new_ch;
766 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900767 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
768 cbss->channel->band);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200769
Johannes Berg77fdaa12009-07-07 03:45:17 +0200770 ASSERT_MGD_MTX(ifmgd);
771
772 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200773 return;
774
Helmut Schaafbe9c422009-07-23 12:14:04 +0200775 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200776 return;
777
778 /* Disregard subsequent beacons if we are already running a timer
779 processing a CSA */
780
781 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
782 return;
783
784 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
785 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
786 return;
787
788 sdata->local->csa_channel = new_ch;
789
Johannes Berg5ce6e432010-05-11 16:20:57 +0200790 if (sdata->local->ops->channel_switch) {
791 /* use driver's channel switch callback */
792 struct ieee80211_channel_switch ch_switch;
793 memset(&ch_switch, 0, sizeof(ch_switch));
794 ch_switch.timestamp = timestamp;
795 if (sw_elem->mode) {
796 ch_switch.block_tx = true;
797 ieee80211_stop_queues_by_reason(&sdata->local->hw,
798 IEEE80211_QUEUE_STOP_REASON_CSA);
799 }
800 ch_switch.channel = new_ch;
801 ch_switch.count = sw_elem->count;
802 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
803 drv_channel_switch(sdata->local, &ch_switch);
804 return;
805 }
806
807 /* channel switch handled in software */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200808 if (sw_elem->count <= 1) {
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400809 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200810 } else {
Wey-Yi Guy9feaddc2010-05-05 20:34:02 -0700811 if (sw_elem->mode)
812 ieee80211_stop_queues_by_reason(&sdata->local->hw,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200813 IEEE80211_QUEUE_STOP_REASON_CSA);
814 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
815 mod_timer(&ifmgd->chswitch_timer,
816 jiffies +
817 msecs_to_jiffies(sw_elem->count *
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100818 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200819 }
820}
821
822static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
823 u16 capab_info, u8 *pwr_constr_elem,
824 u8 pwr_constr_elem_len)
825{
826 struct ieee80211_conf *conf = &sdata->local->hw.conf;
827
828 if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
829 return;
830
831 /* Power constraint IE length should be 1 octet */
832 if (pwr_constr_elem_len != 1)
833 return;
834
Hong Wua48b13a2012-01-11 20:34:30 +0200835 if ((*pwr_constr_elem <= conf->channel->max_reg_power) &&
Johannes Bergcc32abd2009-05-15 11:52:31 +0200836 (*pwr_constr_elem != sdata->local->power_constr_level)) {
837 sdata->local->power_constr_level = *pwr_constr_elem;
838 ieee80211_hw_config(sdata->local, 0);
839 }
840}
841
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300842void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
843{
844 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
845 struct ieee80211_local *local = sdata->local;
846 struct ieee80211_conf *conf = &local->hw.conf;
847
848 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
849 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
850 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
851
852 local->disable_dynamic_ps = false;
853 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
854}
855EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
856
857void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
858{
859 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
860 struct ieee80211_local *local = sdata->local;
861 struct ieee80211_conf *conf = &local->hw.conf;
862
863 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
864 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
865 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
866
867 local->disable_dynamic_ps = true;
868 conf->dynamic_ps_timeout = 0;
869 del_timer_sync(&local->dynamic_ps_timer);
870 ieee80211_queue_work(&local->hw,
871 &local->dynamic_ps_enable_work);
872}
873EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
874
Johannes Berg965beda2009-04-16 13:17:24 +0200875/* powersave */
876static void ieee80211_enable_ps(struct ieee80211_local *local,
877 struct ieee80211_sub_if_data *sdata)
878{
879 struct ieee80211_conf *conf = &local->hw.conf;
880
Johannes Bergd5edaed2009-04-22 23:02:51 +0200881 /*
882 * If we are scanning right now then the parameters will
883 * take effect when scan finishes.
884 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200885 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200886 return;
887
Johannes Berg965beda2009-04-16 13:17:24 +0200888 if (conf->dynamic_ps_timeout > 0 &&
889 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
890 mod_timer(&local->dynamic_ps_timer, jiffies +
891 msecs_to_jiffies(conf->dynamic_ps_timeout));
892 } else {
893 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
894 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530895
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200896 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
897 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
898 return;
899
900 conf->flags |= IEEE80211_CONF_PS;
901 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200902 }
903}
904
905static void ieee80211_change_ps(struct ieee80211_local *local)
906{
907 struct ieee80211_conf *conf = &local->hw.conf;
908
909 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200910 ieee80211_enable_ps(local, local->ps_sdata);
911 } else if (conf->flags & IEEE80211_CONF_PS) {
912 conf->flags &= ~IEEE80211_CONF_PS;
913 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
914 del_timer_sync(&local->dynamic_ps_timer);
915 cancel_work_sync(&local->dynamic_ps_enable_work);
916 }
917}
918
Jason Young808118c2011-03-10 16:43:19 -0800919static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
920{
921 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
922 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200923 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800924
925 if (!mgd->powersave)
926 return false;
927
Johannes Berg05cb9102011-10-28 11:59:47 +0200928 if (mgd->broken_ap)
929 return false;
930
Jason Young808118c2011-03-10 16:43:19 -0800931 if (!mgd->associated)
932 return false;
933
Jason Young808118c2011-03-10 16:43:19 -0800934 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
935 IEEE80211_STA_CONNECTION_POLL))
936 return false;
937
938 rcu_read_lock();
939 sta = sta_info_get(sdata, mgd->bssid);
940 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200941 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -0800942 rcu_read_unlock();
943
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200944 return authorized;
Jason Young808118c2011-03-10 16:43:19 -0800945}
946
Johannes Berg965beda2009-04-16 13:17:24 +0200947/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200948void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200949{
950 struct ieee80211_sub_if_data *sdata, *found = NULL;
951 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300952 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +0200953
954 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
955 local->ps_sdata = NULL;
956 return;
957 }
958
959 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +0100960 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +0200961 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +0530962 if (sdata->vif.type == NL80211_IFTYPE_AP) {
963 /* If an AP vif is found, then disable PS
964 * by setting the count to zero thereby setting
965 * ps_sdata to NULL.
966 */
967 count = 0;
968 break;
969 }
Johannes Berg965beda2009-04-16 13:17:24 +0200970 if (sdata->vif.type != NL80211_IFTYPE_STATION)
971 continue;
972 found = sdata;
973 count++;
974 }
975
Jason Young808118c2011-03-10 16:43:19 -0800976 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300977 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +0200978 s32 beaconint_us;
979
980 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +0200981 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +0200982
983 beaconint_us = ieee80211_tu_to_usec(
984 found->vif.bss_conf.beacon_int);
985
Juuso Oikarinenff616382010-06-09 09:51:52 +0300986 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300987 if (timeout < 0) {
988 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +0300989 * Go to full PSM if the user configures a very low
990 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +0200991 * The 2000 second value is there for compatibility
992 * until the PM_QOS_NETWORK_LATENCY is configured
993 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300994 */
Eliad Peller0ab82b02010-12-03 02:16:23 +0200995 if (latency > (1900 * USEC_PER_MSEC) &&
996 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +0300997 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +0300998 else
999 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001000 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001001 local->dynamic_ps_user_timeout = timeout;
1002 if (!local->disable_dynamic_ps)
1003 conf->dynamic_ps_timeout =
1004 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001005
Johannes Berg04fe2032009-04-22 18:44:37 +02001006 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001007 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001008 } else {
Johannes Berg56007a02010-01-26 14:19:52 +01001009 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +02001010 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001011 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +02001012
Johannes Berg56007a02010-01-26 14:19:52 +01001013 bss = (void *)found->u.mgd.associated->priv;
1014 dtimper = bss->dtim_period;
1015
1016 /* If the TIM IE is invalid, pretend the value is 1 */
1017 if (!dtimper)
1018 dtimper = 1;
1019 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001020 maxslp = min_t(int, dtimper,
1021 latency / beaconint_us);
1022
Johannes Berg9ccebe62009-04-23 10:32:36 +02001023 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001024 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001025 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001026 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001027 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001028 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001029 }
Johannes Berg965beda2009-04-16 13:17:24 +02001030
1031 ieee80211_change_ps(local);
1032}
1033
1034void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1035{
1036 struct ieee80211_local *local =
1037 container_of(work, struct ieee80211_local,
1038 dynamic_ps_disable_work);
1039
1040 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1041 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1042 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1043 }
1044
1045 ieee80211_wake_queues_by_reason(&local->hw,
1046 IEEE80211_QUEUE_STOP_REASON_PS);
1047}
1048
1049void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1050{
1051 struct ieee80211_local *local =
1052 container_of(work, struct ieee80211_local,
1053 dynamic_ps_enable_work);
1054 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001055 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301056 unsigned long flags;
1057 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001058
1059 /* can only happen when PS was just disabled anyway */
1060 if (!sdata)
1061 return;
1062
Christian Lamparter5e340692011-06-30 21:08:43 +02001063 ifmgd = &sdata->u.mgd;
1064
Johannes Berg965beda2009-04-16 13:17:24 +02001065 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1066 return;
1067
Arik Nemtsov77b70232011-06-26 12:06:54 +03001068 if (!local->disable_dynamic_ps &&
1069 local->hw.conf.dynamic_ps_timeout > 0) {
1070 /* don't enter PS if TX frames are pending */
1071 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301072 mod_timer(&local->dynamic_ps_timer, jiffies +
1073 msecs_to_jiffies(
1074 local->hw.conf.dynamic_ps_timeout));
1075 return;
1076 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001077
1078 /*
1079 * transmission can be stopped by others which leads to
1080 * dynamic_ps_timer expiry. Postpone the ps timer if it
1081 * is not the actual idle state.
1082 */
1083 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1084 for (q = 0; q < local->hw.queues; q++) {
1085 if (local->queue_stop_reasons[q]) {
1086 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1087 flags);
1088 mod_timer(&local->dynamic_ps_timer, jiffies +
1089 msecs_to_jiffies(
1090 local->hw.conf.dynamic_ps_timeout));
1091 return;
1092 }
1093 }
1094 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301095 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301096
Vivek Natarajan375177b2010-02-09 14:50:28 +05301097 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301098 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301099 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001100
Vivek Natarajane8306f92011-04-06 11:41:10 +05301101 if (drv_tx_frames_pending(local))
1102 mod_timer(&local->dynamic_ps_timer, jiffies +
1103 msecs_to_jiffies(
1104 local->hw.conf.dynamic_ps_timeout));
1105 else {
1106 ieee80211_send_nullfunc(local, sdata, 1);
1107 /* Flush to get the tx status of nullfunc frame */
1108 drv_flush(local, false);
1109 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301110 }
1111
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001112 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1113 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301114 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1115 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1116 local->hw.conf.flags |= IEEE80211_CONF_PS;
1117 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1118 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301119
Arik Nemtsov77b70232011-06-26 12:06:54 +03001120 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1121 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001122}
1123
1124void ieee80211_dynamic_ps_timer(unsigned long data)
1125{
1126 struct ieee80211_local *local = (void *) data;
1127
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001128 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001129 return;
1130
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001131 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001132}
1133
Johannes Berg60f8b392008-09-08 17:44:22 +02001134/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001135static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001136 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001137 u8 *wmm_param, size_t wmm_param_len)
1138{
Jiri Bencf0706e82007-05-05 11:45:53 -07001139 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001140 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001141 size_t left;
1142 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001143 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001144
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001145 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001146 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001147
Johannes Berg32c5057b2012-03-28 11:04:29 +02001148 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001149 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001150
1151 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001152 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001153
Jiri Bencf0706e82007-05-05 11:45:53 -07001154 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001155 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001156
1157 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001158 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001159
Jiri Bencf0706e82007-05-05 11:45:53 -07001160 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001161 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001162 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001163 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001164
1165 pos = wmm_param + 8;
1166 left = wmm_param_len - 8;
1167
1168 memset(&params, 0, sizeof(params));
1169
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001170 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001171 for (; left >= 4; left -= 4, pos += 4) {
1172 int aci = (pos[0] >> 5) & 0x03;
1173 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001174 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001175 int queue;
1176
1177 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001178 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001179 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001180 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001181 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001182 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1183 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001184 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001185 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001186 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001187 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001188 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001189 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1190 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001191 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001192 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001193 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001194 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001195 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001196 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1197 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001198 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001199 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001200 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001201 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001202 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001203 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001204 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1205 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001206 break;
1207 }
1208
1209 params.aifs = pos[0] & 0x0f;
1210 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1211 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001212 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001213 params.uapsd = uapsd;
1214
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001215 mlme_dbg(sdata,
1216 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1217 queue, aci, acm,
1218 params.aifs, params.cw_min, params.cw_max,
1219 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001220 sdata->tx_conf[queue] = params;
1221 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001222 sdata_err(sdata,
1223 "failed to set TX queue parameters for queue %d\n",
1224 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001225 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001226
1227 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001228 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001229 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001230}
1231
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001232static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1233{
1234 lockdep_assert_held(&sdata->local->mtx);
1235
1236 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1237 IEEE80211_STA_BEACON_POLL);
1238 ieee80211_run_deferred_scan(sdata->local);
1239}
1240
1241static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1242{
1243 mutex_lock(&sdata->local->mtx);
1244 __ieee80211_stop_poll(sdata);
1245 mutex_unlock(&sdata->local->mtx);
1246}
1247
Johannes Berg7a5158e2008-10-08 10:59:33 +02001248static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1249 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001250{
Johannes Bergbda39332008-10-11 01:51:51 +02001251 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001252 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001253 bool use_protection;
1254 bool use_short_preamble;
1255 bool use_short_slot;
1256
1257 if (erp_valid) {
1258 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1259 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1260 } else {
1261 use_protection = false;
1262 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1263 }
1264
1265 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Felix Fietkau43d35342010-01-15 03:00:48 +01001266 if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
1267 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001268
Johannes Berg471b3ef2007-12-28 14:32:58 +01001269 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001270 bss_conf->use_cts_prot = use_protection;
1271 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001272 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001273
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001274 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001275 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001276 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001277 }
Daniel Draked9430a32007-07-27 15:43:24 +02001278
Johannes Berg7a5158e2008-10-08 10:59:33 +02001279 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001280 bss_conf->use_short_slot = use_short_slot;
1281 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001282 }
1283
1284 return changed;
1285}
1286
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001287static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001288 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001289 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001290{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001291 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001292 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001293 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001294
Johannes Bergae5eb022008-10-14 16:58:37 +02001295 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001296 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001297 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001298
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001299 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1300 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1301
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001302 sdata->u.mgd.associated = cbss;
1303 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001304
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001305 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1306
Johannes Bergb291ba12009-07-10 15:29:03 +02001307 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001308 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001309
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001310 ieee80211_led_assoc(local, 1);
1311
Johannes Berge5b900d2010-07-29 16:08:55 +02001312 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1313 bss_conf->dtim_period = bss->dtim_period;
1314 else
1315 bss_conf->dtim_period = 0;
1316
Juuso Oikarinen68542962010-06-09 13:43:26 +03001317 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001318
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001319 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001320 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001321 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001322 bss_info_changed |= BSS_CHANGED_CQM;
1323
Juuso Oikarinen68542962010-06-09 13:43:26 +03001324 /* Enable ARP filtering */
1325 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1326 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1327 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1328 }
1329
Johannes Bergae5eb022008-10-14 16:58:37 +02001330 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001331
Johannes Berg056508d2009-07-30 21:43:55 +02001332 mutex_lock(&local->iflist_mtx);
1333 ieee80211_recalc_ps(local, -1);
Johannes Berg025e6be2010-10-05 10:41:47 +02001334 ieee80211_recalc_smps(local);
Johannes Berg056508d2009-07-30 21:43:55 +02001335 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001336
John W. Linville8a5b33f2010-01-06 15:39:39 -05001337 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001338 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001339}
1340
Jouni Malinene69e95d2010-03-29 23:29:31 -07001341static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001342 u16 stype, u16 reason, bool tx,
1343 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001344{
Johannes Berg46900292009-02-15 12:44:28 +01001345 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001346 struct ieee80211_local *local = sdata->local;
1347 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001348 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001349
Johannes Berg77fdaa12009-07-07 03:45:17 +02001350 ASSERT_MGD_MTX(ifmgd);
1351
Johannes Berg37ad3882012-02-24 13:50:54 +01001352 if (WARN_ON_ONCE(tx && !frame_buf))
1353 return;
1354
Johannes Bergb291ba12009-07-10 15:29:03 +02001355 if (WARN_ON(!ifmgd->associated))
1356 return;
1357
David Spinadel79543d82012-06-12 09:59:45 +03001358 ieee80211_stop_poll(sdata);
1359
Johannes Berg77fdaa12009-07-07 03:45:17 +02001360 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001361
1362 /*
1363 * we need to commit the associated = NULL change because the
1364 * scan code uses that to determine whether this iface should
1365 * go to/wake up from powersave or not -- and could otherwise
1366 * wake the queues erroneously.
1367 */
1368 smp_mb();
1369
1370 /*
1371 * Thus, we can only afterwards stop the queues -- to account
1372 * for the case where another CPU is finishing a scan at this
1373 * time -- we don't want the scan code to enable queues.
1374 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001375
John W. Linville8a5b33f2010-01-06 15:39:39 -05001376 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001377 netif_carrier_off(sdata->dev);
1378
Johannes Berg2a419052010-06-10 10:21:29 +02001379 mutex_lock(&local->sta_mtx);
Eliad Peller88a9e312012-06-01 11:14:03 +03001380 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301381 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001382 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg53f73c02010-10-05 19:37:40 +02001383 ieee80211_sta_tear_down_BA_sessions(sta, tx);
Sujith4cad6c72010-02-10 14:52:21 +05301384 }
Johannes Berg2a419052010-06-10 10:21:29 +02001385 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001386
Eliad Peller88bc40e2012-07-12 17:35:33 +03001387 /*
1388 * if we want to get out of ps before disassoc (why?) we have
1389 * to do it before sending disassoc, as otherwise the null-packet
1390 * won't be valid.
1391 */
1392 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1393 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1394 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1395 }
1396 local->ps_sdata = NULL;
1397
Eliad Pellerf8239812012-06-27 14:18:22 +03001398 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1399 if (tx)
1400 drv_flush(local, false);
1401
Johannes Berg37ad3882012-02-24 13:50:54 +01001402 /* deauthenticate/disassociate now */
1403 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001404 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1405 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001406
1407 /* flush out frame */
1408 if (tx)
1409 drv_flush(local, false);
1410
Eliad Peller88a9e312012-06-01 11:14:03 +03001411 /* clear bssid only after building the needed mgmt frames */
1412 memset(ifmgd->bssid, 0, ETH_ALEN);
1413
Johannes Berg37ad3882012-02-24 13:50:54 +01001414 /* remove AP and TDLS peers */
1415 sta_info_flush(local, sdata);
1416
1417 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001418 changed |= ieee80211_reset_erp_info(sdata);
1419
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001420 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001421 changed |= BSS_CHANGED_ASSOC;
1422 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001423
Johannes Berg413ad502009-05-08 21:21:06 +02001424 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001425 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1426 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001427
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301428 local->power_constr_level = 0;
1429
Kalle Valo520eb822008-12-18 23:35:27 +02001430 del_timer_sync(&local->dynamic_ps_timer);
1431 cancel_work_sync(&local->dynamic_ps_enable_work);
1432
Juuso Oikarinen68542962010-06-09 13:43:26 +03001433 /* Disable ARP filtering */
1434 if (sdata->vif.bss_conf.arp_filter_enabled) {
1435 sdata->vif.bss_conf.arp_filter_enabled = false;
1436 changed |= BSS_CHANGED_ARP_FILTER;
1437 }
1438
Johannes Berg3abead52012-03-02 15:56:59 +01001439 sdata->vif.bss_conf.qos = false;
1440 changed |= BSS_CHANGED_QOS;
1441
Johannes Berg0aaffa92010-05-05 15:28:27 +02001442 /* The BSSID (not really interesting) and HT changed */
1443 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001444 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001445
Johannes Berg3cc52402012-03-09 13:12:35 +01001446 /* channel(_type) changes are handled by ieee80211_hw_config */
1447 WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
1448 ieee80211_hw_config(local, 0);
1449
Johannes Berg3abead52012-03-02 15:56:59 +01001450 /* disassociated - set to defaults now */
1451 ieee80211_set_wmm_default(sdata, false);
1452
Johannes Bergb9dcf712010-08-27 12:35:54 +02001453 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1454 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1455 del_timer_sync(&sdata->u.mgd.timer);
1456 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001457}
Jiri Bencf0706e82007-05-05 11:45:53 -07001458
Kalle Valo3cf335d2009-03-22 21:57:06 +02001459void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1460 struct ieee80211_hdr *hdr)
1461{
1462 /*
1463 * We can postpone the mgd.timer whenever receiving unicast frames
1464 * from AP because we know that the connection is working both ways
1465 * at that time. But multicast frames (and hence also beacons) must
1466 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001467 * data idle periods for sending the periodic probe request to the
1468 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001469 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001470 if (is_multicast_ether_addr(hdr->addr1))
1471 return;
1472
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001473 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001474}
Jiri Bencf0706e82007-05-05 11:45:53 -07001475
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001476static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1477{
1478 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001479 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001480
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001481 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001482 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001483 IEEE80211_STA_CONNECTION_POLL))) {
1484 mutex_unlock(&local->mtx);
1485 return;
1486 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001487
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001488 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001489
1490 mutex_lock(&local->iflist_mtx);
1491 ieee80211_recalc_ps(local, -1);
1492 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001493
1494 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001495 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001496
1497 /*
1498 * We've received a probe response, but are not sure whether
1499 * we have or will be receiving any beacons or data, so let's
1500 * schedule the timers again, just in case.
1501 */
1502 ieee80211_sta_reset_beacon_monitor(sdata);
1503
1504 mod_timer(&ifmgd->conn_mon_timer,
1505 round_jiffies_up(jiffies +
1506 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001507out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001508 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001509}
1510
1511void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001512 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001513{
Felix Fietkau75706d02010-12-02 21:01:07 +01001514 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001515 return;
1516
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001517 if (ack)
1518 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001519
1520 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1521 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001522 if (ack)
1523 sdata->u.mgd.probe_send_count = 0;
1524 else
1525 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001526 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1527 }
1528}
1529
Maxim Levitskya43abf22009-07-31 18:54:12 +03001530static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1531{
1532 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1533 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001534 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001535 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001536
1537 /*
1538 * Try sending broadcast probe requests for the last three
1539 * probe requests after the first ones failed since some
1540 * buggy APs only support broadcast probe requests.
1541 */
1542 if (ifmgd->probe_send_count >= unicast_limit)
1543 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001544
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001545 /*
1546 * When the hardware reports an accurate Tx ACK status, it's
1547 * better to send a nullfunc frame instead of a probe request,
1548 * as it will kick us off the AP quickly if we aren't associated
1549 * anymore. The timeout will be reset if the frame is ACKed by
1550 * the AP.
1551 */
Soumik Das992e68b2012-05-20 15:31:13 +05301552 ifmgd->probe_send_count++;
1553
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001554 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1555 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001556 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001557 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001558 int ssid_len;
1559
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001560 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001561 if (WARN_ON_ONCE(ssid == NULL))
1562 ssid_len = 0;
1563 else
1564 ssid_len = ssid[1];
1565
1566 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
1567 0, (u32) -1, true, false);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001568 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001569
Ben Greear180205b2011-02-04 15:30:24 -08001570 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001571 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301572 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1573 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001574}
1575
Johannes Bergb291ba12009-07-10 15:29:03 +02001576static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1577 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001578{
Kalle Valo04de8382009-03-22 21:57:35 +02001579 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001580 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001581
Johannes Berg9607e6b2009-12-23 13:15:31 +01001582 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001583 return;
1584
Johannes Berg77fdaa12009-07-07 03:45:17 +02001585 mutex_lock(&ifmgd->mtx);
1586
1587 if (!ifmgd->associated)
1588 goto out;
1589
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001590 mutex_lock(&sdata->local->mtx);
1591
1592 if (sdata->local->tmp_channel || sdata->local->scanning) {
1593 mutex_unlock(&sdata->local->mtx);
1594 goto out;
1595 }
1596
Joe Perchese87cc472012-05-13 21:56:26 +00001597 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001598 mlme_dbg_ratelimited(sdata,
1599 "detected beacon loss from AP - sending probe request\n");
1600
Holger Schurig6efb71b2012-05-15 15:38:59 +02001601 ieee80211_cqm_rssi_notify(&sdata->vif,
1602 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001603
Johannes Bergb291ba12009-07-10 15:29:03 +02001604 /*
1605 * The driver/our work has already reported this event or the
1606 * connection monitoring has kicked in and we have already sent
1607 * a probe request. Or maybe the AP died and the driver keeps
1608 * reporting until we disassociate...
1609 *
1610 * In either case we have to ignore the current call to this
1611 * function (except for setting the correct probe reason bit)
1612 * because otherwise we would reset the timer every time and
1613 * never check whether we received a probe response!
1614 */
1615 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1616 IEEE80211_STA_CONNECTION_POLL))
1617 already = true;
1618
1619 if (beacon)
1620 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1621 else
1622 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1623
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001624 mutex_unlock(&sdata->local->mtx);
1625
Johannes Bergb291ba12009-07-10 15:29:03 +02001626 if (already)
1627 goto out;
1628
Johannes Berg4e751842009-06-10 15:16:52 +02001629 mutex_lock(&sdata->local->iflist_mtx);
1630 ieee80211_recalc_ps(sdata->local, -1);
1631 mutex_unlock(&sdata->local->iflist_mtx);
1632
Maxim Levitskya43abf22009-07-31 18:54:12 +03001633 ifmgd->probe_send_count = 0;
1634 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001635 out:
1636 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001637}
1638
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001639struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1640 struct ieee80211_vif *vif)
1641{
1642 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1643 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001644 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001645 struct sk_buff *skb;
1646 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001647 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001648
1649 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1650 return NULL;
1651
1652 ASSERT_MGD_MTX(ifmgd);
1653
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001654 if (ifmgd->associated)
1655 cbss = ifmgd->associated;
1656 else if (ifmgd->auth_data)
1657 cbss = ifmgd->auth_data->bss;
1658 else if (ifmgd->assoc_data)
1659 cbss = ifmgd->assoc_data->bss;
1660 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001661 return NULL;
1662
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001663 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001664 if (WARN_ON_ONCE(ssid == NULL))
1665 ssid_len = 0;
1666 else
1667 ssid_len = ssid[1];
1668
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001669 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001670 (u32) -1, ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001671 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001672
1673 return skb;
1674}
1675EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1676
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001677static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
1678{
1679 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1680 struct ieee80211_local *local = sdata->local;
1681 u8 bssid[ETH_ALEN];
Johannes Berg5fef7db2012-02-24 13:50:52 +01001682 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001683
1684 mutex_lock(&ifmgd->mtx);
1685 if (!ifmgd->associated) {
1686 mutex_unlock(&ifmgd->mtx);
1687 return;
1688 }
1689
1690 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
1691
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001692 sdata_info(sdata, "Connection to AP %pM lost\n", bssid);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001693
Johannes Berg37ad3882012-02-24 13:50:54 +01001694 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1695 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
1696 false, frame_buf);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001697 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001698
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001699 /*
1700 * must be outside lock due to cfg80211,
1701 * but that's not a problem.
1702 */
Johannes Berg5fef7db2012-02-24 13:50:52 +01001703 cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001704
1705 mutex_lock(&local->mtx);
1706 ieee80211_recalc_idle(local);
1707 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001708}
1709
1710void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001711{
1712 struct ieee80211_sub_if_data *sdata =
1713 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001714 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001715 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1716 struct sta_info *sta;
1717
1718 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301719 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001720 sta = sta_info_get(sdata, ifmgd->bssid);
1721 if (sta)
1722 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301723 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001724 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001725
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001726 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
1727 __ieee80211_connection_loss(sdata);
1728 else
1729 ieee80211_mgd_probe_ap(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001730}
1731
Kalle Valo04de8382009-03-22 21:57:35 +02001732void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1733{
1734 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001735 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001736
Johannes Bergb5878a22010-04-07 16:48:40 +02001737 trace_api_beacon_loss(sdata);
1738
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001739 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1740 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001741}
1742EXPORT_SYMBOL(ieee80211_beacon_loss);
1743
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001744void ieee80211_connection_loss(struct ieee80211_vif *vif)
1745{
1746 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1747 struct ieee80211_hw *hw = &sdata->local->hw;
1748
Johannes Bergb5878a22010-04-07 16:48:40 +02001749 trace_api_connection_loss(sdata);
1750
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001751 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1752 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1753}
1754EXPORT_SYMBOL(ieee80211_connection_loss);
1755
1756
Johannes Berg66e67e42012-01-20 13:55:27 +01001757static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1758 bool assoc)
1759{
1760 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1761
1762 lockdep_assert_held(&sdata->u.mgd.mtx);
1763
Johannes Berg66e67e42012-01-20 13:55:27 +01001764 if (!assoc) {
1765 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1766
1767 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1768 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1769 }
1770
1771 cfg80211_put_bss(auth_data->bss);
1772 kfree(auth_data);
1773 sdata->u.mgd.auth_data = NULL;
1774}
1775
1776static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1777 struct ieee80211_mgmt *mgmt, size_t len)
1778{
1779 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1780 u8 *pos;
1781 struct ieee802_11_elems elems;
1782
1783 pos = mgmt->u.auth.variable;
1784 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1785 if (!elems.challenge)
1786 return;
1787 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001788 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001789 ieee80211_send_auth(sdata, 3, auth_data->algorithm,
1790 elems.challenge - 2, elems.challenge_len + 2,
1791 auth_data->bss->bssid, auth_data->bss->bssid,
1792 auth_data->key, auth_data->key_len,
1793 auth_data->key_idx);
1794}
1795
1796static enum rx_mgmt_action __must_check
1797ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1798 struct ieee80211_mgmt *mgmt, size_t len)
1799{
1800 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1801 u8 bssid[ETH_ALEN];
1802 u16 auth_alg, auth_transaction, status_code;
1803 struct sta_info *sta;
1804
1805 lockdep_assert_held(&ifmgd->mtx);
1806
1807 if (len < 24 + 6)
1808 return RX_MGMT_NONE;
1809
1810 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1811 return RX_MGMT_NONE;
1812
1813 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1814
Joe Perchesb203ca32012-05-08 18:56:52 +00001815 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001816 return RX_MGMT_NONE;
1817
1818 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1819 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1820 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1821
1822 if (auth_alg != ifmgd->auth_data->algorithm ||
1823 auth_transaction != ifmgd->auth_data->expected_transaction)
1824 return RX_MGMT_NONE;
1825
1826 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001827 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1828 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001829 ieee80211_destroy_auth_data(sdata, false);
1830 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001831 }
1832
1833 switch (ifmgd->auth_data->algorithm) {
1834 case WLAN_AUTH_OPEN:
1835 case WLAN_AUTH_LEAP:
1836 case WLAN_AUTH_FT:
1837 break;
1838 case WLAN_AUTH_SHARED_KEY:
1839 if (ifmgd->auth_data->expected_transaction != 4) {
1840 ieee80211_auth_challenge(sdata, mgmt, len);
1841 /* need another frame */
1842 return RX_MGMT_NONE;
1843 }
1844 break;
1845 default:
1846 WARN_ONCE(1, "invalid auth alg %d",
1847 ifmgd->auth_data->algorithm);
1848 return RX_MGMT_NONE;
1849 }
1850
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001851 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001852 ifmgd->auth_data->done = true;
1853 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1854 run_again(ifmgd, ifmgd->auth_data->timeout);
1855
1856 /* move station state to auth */
1857 mutex_lock(&sdata->local->sta_mtx);
1858 sta = sta_info_get(sdata, bssid);
1859 if (!sta) {
1860 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1861 goto out_err;
1862 }
1863 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001864 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01001865 goto out_err;
1866 }
1867 mutex_unlock(&sdata->local->sta_mtx);
1868
1869 return RX_MGMT_CFG80211_RX_AUTH;
1870 out_err:
1871 mutex_unlock(&sdata->local->sta_mtx);
1872 /* ignore frame -- wait for timeout */
1873 return RX_MGMT_NONE;
1874}
1875
1876
Johannes Berg77fdaa12009-07-07 03:45:17 +02001877static enum rx_mgmt_action __must_check
1878ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001879 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001880{
Johannes Berg46900292009-02-15 12:44:28 +01001881 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001882 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001883 u16 reason_code;
1884
Johannes Berg66e67e42012-01-20 13:55:27 +01001885 lockdep_assert_held(&ifmgd->mtx);
1886
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001887 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001888 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001889
Johannes Berg66e67e42012-01-20 13:55:27 +01001890 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001891 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001892 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001893
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001894 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001895
1896 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1897
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001898 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
1899 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001900
Johannes Berg37ad3882012-02-24 13:50:54 +01001901 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1902
Johannes Berg7da7cc12010-08-05 17:02:38 +02001903 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001904 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001905 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001906
1907 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07001908}
1909
1910
Johannes Berg77fdaa12009-07-07 03:45:17 +02001911static enum rx_mgmt_action __must_check
1912ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
1913 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001914{
Johannes Berg46900292009-02-15 12:44:28 +01001915 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001916 u16 reason_code;
1917
Johannes Berg66e67e42012-01-20 13:55:27 +01001918 lockdep_assert_held(&ifmgd->mtx);
1919
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001920 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001921 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001922
Johannes Berg66e67e42012-01-20 13:55:27 +01001923 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001924 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02001925 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001926
1927 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1928
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001929 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
1930 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001931
Johannes Berg37ad3882012-02-24 13:50:54 +01001932 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1933
Johannes Berg7da7cc12010-08-05 17:02:38 +02001934 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01001935 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001936 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01001937
Johannes Berg77fdaa12009-07-07 03:45:17 +02001938 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07001939}
1940
Christian Lamparterc74d0842011-10-15 00:14:49 +02001941static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
1942 u8 *supp_rates, unsigned int supp_rates_len,
1943 u32 *rates, u32 *basic_rates,
1944 bool *have_higher_than_11mbit,
1945 int *min_rate, int *min_rate_index)
1946{
1947 int i, j;
1948
1949 for (i = 0; i < supp_rates_len; i++) {
1950 int rate = (supp_rates[i] & 0x7f) * 5;
1951 bool is_basic = !!(supp_rates[i] & 0x80);
1952
1953 if (rate > 110)
1954 *have_higher_than_11mbit = true;
1955
1956 /*
1957 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
1958 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
1959 *
1960 * Note: Even through the membership selector and the basic
1961 * rate flag share the same bit, they are not exactly
1962 * the same.
1963 */
1964 if (!!(supp_rates[i] & 0x80) &&
1965 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1966 continue;
1967
1968 for (j = 0; j < sband->n_bitrates; j++) {
1969 if (sband->bitrates[j].bitrate == rate) {
1970 *rates |= BIT(j);
1971 if (is_basic)
1972 *basic_rates |= BIT(j);
1973 if (rate < *min_rate) {
1974 *min_rate = rate;
1975 *min_rate_index = j;
1976 }
1977 break;
1978 }
1979 }
1980 }
1981}
Jiri Bencf0706e82007-05-05 11:45:53 -07001982
Johannes Berg66e67e42012-01-20 13:55:27 +01001983static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
1984 bool assoc)
1985{
1986 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
1987
1988 lockdep_assert_held(&sdata->u.mgd.mtx);
1989
Johannes Berg66e67e42012-01-20 13:55:27 +01001990 if (!assoc) {
1991 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
1992
1993 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1994 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
1995 }
1996
1997 kfree(assoc_data);
1998 sdata->u.mgd.assoc_data = NULL;
1999}
2000
2001static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2002 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002003 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002004{
Johannes Berg46900292009-02-15 12:44:28 +01002005 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002006 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002007 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002009 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002010 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002011 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002012 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002013 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002014 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002015
Johannes Bergaf6b6372009-12-23 13:15:35 +01002016 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002017
Jiri Bencf0706e82007-05-05 11:45:53 -07002018 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002019 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002020
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002021 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002022 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2023 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002024 aid &= ~(BIT(15) | BIT(14));
2025
Johannes Berg05cb9102011-10-28 11:59:47 +02002026 ifmgd->broken_ap = false;
2027
2028 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002029 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2030 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002031 aid = 0;
2032 ifmgd->broken_ap = true;
2033 }
2034
Johannes Bergaf6b6372009-12-23 13:15:35 +01002035 pos = mgmt->u.assoc_resp.variable;
2036 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2037
Jiri Bencf0706e82007-05-05 11:45:53 -07002038 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002039 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002040 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002041 }
2042
Johannes Berg46900292009-02-15 12:44:28 +01002043 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002044
Guy Eilam2a33bee2011-08-17 15:18:15 +03002045 mutex_lock(&sdata->local->sta_mtx);
2046 /*
2047 * station info was already allocated and inserted before
2048 * the association and should be available to us
2049 */
Johannes Berg7852e362012-01-20 13:55:24 +01002050 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002051 if (WARN_ON(!sta)) {
2052 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002053 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002054 }
2055
Johannes Berg66e67e42012-01-20 13:55:27 +01002056 sband = local->hw.wiphy->bands[local->oper_channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +01002057
Johannes Bergab1faea2009-07-01 21:41:17 +02002058 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Ben Greearef96a8422011-11-18 11:32:00 -08002059 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002060 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002061
Johannes Berg64f68e52012-03-28 10:58:37 +02002062 sta->supports_40mhz =
2063 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2064
Johannes Berg4b7679a2008-09-18 18:14:18 +02002065 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002066
Johannes Berg46900292009-02-15 12:44:28 +01002067 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002068 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002069
Johannes Bergddf4ac52008-10-22 11:41:38 +02002070 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002071 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002072
Johannes Bergc8987872012-01-20 13:55:17 +01002073 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2074 if (!err)
2075 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2076 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2077 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2078 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002079 sdata_info(sdata,
2080 "failed to move station %pM to desired state\n",
2081 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002082 WARN_ON(__sta_info_destroy(sta));
2083 mutex_unlock(&sdata->local->sta_mtx);
2084 return false;
2085 }
2086
Johannes Berg7852e362012-01-20 13:55:24 +01002087 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002088
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002089 /*
2090 * Always handle WMM once after association regardless
2091 * of the first value the AP uses. Setting -1 here has
2092 * that effect because the AP values is an unsigned
2093 * 4-bit value.
2094 */
2095 ifmgd->wmm_last_param_set = -1;
2096
Johannes Bergddf4ac52008-10-22 11:41:38 +02002097 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002098 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002099 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002100 else
Johannes Berg3abead52012-03-02 15:56:59 +01002101 ieee80211_set_wmm_default(sdata, false);
2102 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002103
Johannes Berg074d46d2012-03-15 19:45:16 +01002104 if (elems.ht_operation && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002105 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg24398e32012-03-28 10:58:36 +02002106 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2107 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002108
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002109 /* set AID and assoc capability,
2110 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002111 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002112 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002113 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002114
Kalle Valo15b7b062009-03-22 21:57:14 +02002115 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002116 * If we're using 4-addr mode, let the AP know that we're
2117 * doing so, so that it can create the STA VLAN on its side
2118 */
2119 if (ifmgd->use_4addr)
2120 ieee80211_send_4addr_nullfunc(local, sdata);
2121
2122 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002123 * Start timer to probe the connection to the AP now.
2124 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002125 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002126 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002127 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002128
Johannes Bergaf6b6372009-12-23 13:15:35 +01002129 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002130}
2131
Johannes Berg66e67e42012-01-20 13:55:27 +01002132static enum rx_mgmt_action __must_check
2133ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2134 struct ieee80211_mgmt *mgmt, size_t len,
2135 struct cfg80211_bss **bss)
2136{
2137 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2138 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2139 u16 capab_info, status_code, aid;
2140 struct ieee802_11_elems elems;
2141 u8 *pos;
2142 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002143
Johannes Berg66e67e42012-01-20 13:55:27 +01002144 lockdep_assert_held(&ifmgd->mtx);
2145
2146 if (!assoc_data)
2147 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002148 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002149 return RX_MGMT_NONE;
2150
2151 /*
2152 * AssocResp and ReassocResp have identical structure, so process both
2153 * of them in this function.
2154 */
2155
2156 if (len < 24 + 6)
2157 return RX_MGMT_NONE;
2158
2159 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2160 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2161 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2162 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2163
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002164 sdata_info(sdata,
2165 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2166 reassoc ? "Rea" : "A", mgmt->sa,
2167 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002168
2169 pos = mgmt->u.assoc_resp.variable;
2170 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2171
2172 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2173 elems.timeout_int && elems.timeout_int_len == 5 &&
2174 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2175 u32 tu, ms;
2176 tu = get_unaligned_le32(elems.timeout_int + 1);
2177 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002178 sdata_info(sdata,
2179 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2180 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002181 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2182 if (ms > IEEE80211_ASSOC_TIMEOUT)
2183 run_again(ifmgd, assoc_data->timeout);
2184 return RX_MGMT_NONE;
2185 }
2186
2187 *bss = assoc_data->bss;
2188
2189 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002190 sdata_info(sdata, "%pM denied association (code=%d)\n",
2191 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002192 ieee80211_destroy_assoc_data(sdata, false);
2193 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002194 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2195 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002196 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002197 cfg80211_put_bss(*bss);
2198 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2199 }
John W. Linville635d9992012-07-09 16:34:34 -04002200 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002201
2202 /*
2203 * destroy assoc_data afterwards, as otherwise an idle
2204 * recalc after assoc_data is NULL but before associated
2205 * is set can cause the interface to go idle
2206 */
2207 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002208 }
2209
2210 return RX_MGMT_CFG80211_RX_ASSOC;
2211}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002212static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002213 struct ieee80211_mgmt *mgmt,
2214 size_t len,
2215 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002216 struct ieee802_11_elems *elems,
2217 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002218{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002219 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002220 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002221 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002222 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002223 bool need_ps = false;
2224
Johannes Berg66e67e42012-01-20 13:55:27 +01002225 if (sdata->u.mgd.associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002226 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
Johannes Berg56007a02010-01-26 14:19:52 +01002227 bss = (void *)sdata->u.mgd.associated->priv;
2228 /* not previously set so we may need to recalc */
2229 need_ps = !bss->dtim_period;
2230 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002231
Johannes Berg5bda6172008-09-08 11:05:10 +02002232 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002233 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2234 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002235 else
2236 freq = rx_status->freq;
2237
2238 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2239
2240 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2241 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002242
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002243 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002244 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002245 if (bss)
2246 ieee80211_rx_bss_put(local, bss);
2247
2248 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002249 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002250
Johannes Berg56007a02010-01-26 14:19:52 +01002251 if (need_ps) {
2252 mutex_lock(&local->iflist_mtx);
2253 ieee80211_recalc_ps(local, -1);
2254 mutex_unlock(&local->iflist_mtx);
2255 }
2256
Sujithc481ec92009-01-06 09:28:37 +05302257 if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002258 (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002259 ETH_ALEN) == 0)) {
Sujithc481ec92009-01-06 09:28:37 +05302260 struct ieee80211_channel_sw_ie *sw_elem =
2261 (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
Johannes Berg5ce6e432010-05-11 16:20:57 +02002262 ieee80211_sta_process_chanswitch(sdata, sw_elem,
2263 bss, rx_status->mactime);
Sujithc481ec92009-01-06 09:28:37 +05302264 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002265}
2266
2267
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002268static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002269 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002270{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002271 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002272 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002273 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2274 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002275 struct ieee802_11_elems elems;
2276
Kalle Valo15b7b062009-03-22 21:57:14 +02002277 ifmgd = &sdata->u.mgd;
2278
Johannes Berg77fdaa12009-07-07 03:45:17 +02002279 ASSERT_MGD_MTX(ifmgd);
2280
Joe Perchesb203ca32012-05-08 18:56:52 +00002281 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002282 return; /* ignore ProbeResp to foreign address */
2283
Ester Kummerae6a44e2008-06-27 18:54:48 +03002284 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2285 if (baselen > len)
2286 return;
2287
2288 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2289 &elems);
2290
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002291 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002292
Johannes Berg77fdaa12009-07-07 03:45:17 +02002293 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002294 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002295 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002296
2297 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002298 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002299 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002300 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002301 ifmgd->auth_data->tries = 0;
2302 ifmgd->auth_data->timeout = jiffies;
2303 run_again(ifmgd, ifmgd->auth_data->timeout);
2304 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002305}
2306
Johannes Bergd91f36d2009-04-16 13:17:26 +02002307/*
2308 * This is the canonical list of information elements we care about,
2309 * the filter code also gives us all changes to the Microsoft OUI
2310 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2311 *
2312 * We implement beacon filtering in software since that means we can
2313 * avoid processing the frame here and in cfg80211, and userspace
2314 * will not be able to tell whether the hardware supports it or not.
2315 *
2316 * XXX: This list needs to be dynamic -- userspace needs to be able to
2317 * add items it requires. It also needs to be able to tell us to
2318 * look out for other vendor IEs.
2319 */
2320static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002321 (1ULL << WLAN_EID_COUNTRY) |
2322 (1ULL << WLAN_EID_ERP_INFO) |
2323 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2324 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2325 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002326 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002327
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002328static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002329 struct ieee80211_mgmt *mgmt,
2330 size_t len,
2331 struct ieee80211_rx_status *rx_status)
2332{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002333 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002334 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002335 size_t baselen;
2336 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002337 struct ieee80211_local *local = sdata->local;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002338 u32 changed = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002339 bool erp_valid, directed_tim = false;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002340 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002341 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002342 u8 *bssid;
2343
Johannes Berg66e67e42012-01-20 13:55:27 +01002344 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002345
Ester Kummerae6a44e2008-06-27 18:54:48 +03002346 /* Process beacon from the current BSS */
2347 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2348 if (baselen > len)
2349 return;
2350
Johannes Bergd91f36d2009-04-16 13:17:26 +02002351 if (rx_status->freq != local->hw.conf.channel->center_freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07002352 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002353
Johannes Berg66e67e42012-01-20 13:55:27 +01002354 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002355 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002356 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2357 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002358
Johannes Berg66e67e42012-01-20 13:55:27 +01002359 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2360 false);
2361 ifmgd->assoc_data->have_beacon = true;
2362 ifmgd->assoc_data->sent_assoc = false;
2363 /* continue assoc process */
2364 ifmgd->assoc_data->timeout = jiffies;
2365 run_again(ifmgd, ifmgd->assoc_data->timeout);
2366 return;
2367 }
2368
2369 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002370 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002371 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002372 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002373
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002374 /* Track average RSSI from the Beacon frames of the current AP */
2375 ifmgd->last_beacon_signal = rx_status->signal;
2376 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2377 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002378 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002379 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002380 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002381 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002382 } else {
2383 ifmgd->ave_beacon_signal =
2384 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2385 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2386 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002387 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002388 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002389
2390 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2391 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2392 int sig = ifmgd->ave_beacon_signal;
2393 int last_sig = ifmgd->last_ave_beacon_signal;
2394
2395 /*
2396 * if signal crosses either of the boundaries, invoke callback
2397 * with appropriate parameters
2398 */
2399 if (sig > ifmgd->rssi_max_thold &&
2400 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2401 ifmgd->last_ave_beacon_signal = sig;
2402 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2403 } else if (sig < ifmgd->rssi_min_thold &&
2404 (last_sig >= ifmgd->rssi_max_thold ||
2405 last_sig == 0)) {
2406 ifmgd->last_ave_beacon_signal = sig;
2407 drv_rssi_callback(local, RSSI_EVENT_LOW);
2408 }
2409 }
2410
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002411 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002412 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002413 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002414 int sig = ifmgd->ave_beacon_signal / 16;
2415 int last_event = ifmgd->last_cqm_event_signal;
2416 int thold = bss_conf->cqm_rssi_thold;
2417 int hyst = bss_conf->cqm_rssi_hyst;
2418 if (sig < thold &&
2419 (last_event == 0 || sig < last_event - hyst)) {
2420 ifmgd->last_cqm_event_signal = sig;
2421 ieee80211_cqm_rssi_notify(
2422 &sdata->vif,
2423 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2424 GFP_KERNEL);
2425 } else if (sig > thold &&
2426 (last_event == 0 || sig > last_event + hyst)) {
2427 ifmgd->last_cqm_event_signal = sig;
2428 ieee80211_cqm_rssi_notify(
2429 &sdata->vif,
2430 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2431 GFP_KERNEL);
2432 }
2433 }
2434
Johannes Bergb291ba12009-07-10 15:29:03 +02002435 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002436 mlme_dbg_ratelimited(sdata,
2437 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002438 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002439 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002440 ieee80211_run_deferred_scan(local);
2441 mutex_unlock(&local->mtx);
2442
Johannes Berg4e751842009-06-10 15:16:52 +02002443 mutex_lock(&local->iflist_mtx);
2444 ieee80211_recalc_ps(local, -1);
2445 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002446 }
2447
Johannes Bergb291ba12009-07-10 15:29:03 +02002448 /*
2449 * Push the beacon loss detection into the future since
2450 * we are processing a beacon from the AP just now.
2451 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002452 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002453
Johannes Bergd91f36d2009-04-16 13:17:26 +02002454 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2455 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2456 len - baselen, &elems,
2457 care_about_ies, ncrc);
2458
2459 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
Johannes Berge7ec86f2009-04-18 17:33:24 +02002460 directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
2461 ifmgd->aid);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002462
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302463 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Kalle Valo1fb36062009-02-10 17:09:24 +02002464 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002465 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002466 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2467 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2468 ieee80211_hw_config(local,
2469 IEEE80211_CONF_CHANGE_PS);
2470 }
Kalle Valo572e0012009-02-10 17:09:31 +02002471 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302472 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002473 local->pspolling = true;
2474
2475 /*
2476 * Here is assumed that the driver will be
2477 * able to send ps-poll frame and receive a
2478 * response even though power save mode is
2479 * enabled, but some drivers might require
2480 * to disable power save here. This needs
2481 * to be investigated.
2482 */
2483 ieee80211_send_pspoll(local, sdata);
2484 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002485 }
2486 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002487
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002488 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002489 return;
2490 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002491 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002492
Johannes Berg7d257452012-07-06 17:37:43 +02002493 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2494 true);
2495
2496 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2497 elems.wmm_param_len))
2498 changed |= BSS_CHANGED_QOS;
2499
Johannes Berg7a5158e2008-10-08 10:59:33 +02002500 if (elems.erp_info && elems.erp_info_len >= 1) {
2501 erp_valid = true;
2502 erp_value = elems.erp_info[0];
2503 } else {
2504 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002505 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002506 changed |= ieee80211_handle_bss_capability(sdata,
2507 le16_to_cpu(mgmt->u.beacon.capab_info),
2508 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002509
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002510
Johannes Berg074d46d2012-03-15 19:45:16 +01002511 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002512 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
Johannes Bergae5eb022008-10-14 16:58:37 +02002513 struct ieee80211_supported_band *sband;
Johannes Bergae5eb022008-10-14 16:58:37 +02002514
2515 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2516
Johannes Berg24398e32012-03-28 10:58:36 +02002517 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2518 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002519 }
2520
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -07002521 /* Note: country IE parsing is done for us by cfg80211 */
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002522 if (elems.country_elem) {
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05302523 /* TODO: IBSS also needs this */
2524 if (elems.pwr_constr_elem)
2525 ieee80211_handle_pwr_constr(sdata,
2526 le16_to_cpu(mgmt->u.probe_resp.capab_info),
2527 elems.pwr_constr_elem,
2528 elems.pwr_constr_elem_len);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002529 }
2530
Johannes Berg471b3ef2007-12-28 14:32:58 +01002531 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002532}
2533
Johannes Berg1fa57d02010-06-10 10:21:32 +02002534void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2535 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002536{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002537 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002538 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002539 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002540 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002541 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002542 u16 fc;
2543
Jiri Bencf0706e82007-05-05 11:45:53 -07002544 rx_status = (struct ieee80211_rx_status *) skb->cb;
2545 mgmt = (struct ieee80211_mgmt *) skb->data;
2546 fc = le16_to_cpu(mgmt->frame_control);
2547
Johannes Berg77fdaa12009-07-07 03:45:17 +02002548 mutex_lock(&ifmgd->mtx);
2549
Johannes Berg66e67e42012-01-20 13:55:27 +01002550 switch (fc & IEEE80211_FCTL_STYPE) {
2551 case IEEE80211_STYPE_BEACON:
2552 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2553 break;
2554 case IEEE80211_STYPE_PROBE_RESP:
2555 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2556 break;
2557 case IEEE80211_STYPE_AUTH:
2558 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2559 break;
2560 case IEEE80211_STYPE_DEAUTH:
2561 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2562 break;
2563 case IEEE80211_STYPE_DISASSOC:
2564 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2565 break;
2566 case IEEE80211_STYPE_ASSOC_RESP:
2567 case IEEE80211_STYPE_REASSOC_RESP:
2568 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2569 break;
2570 case IEEE80211_STYPE_ACTION:
2571 switch (mgmt->u.action.category) {
2572 case WLAN_CATEGORY_SPECTRUM_MGMT:
2573 ieee80211_sta_process_chanswitch(sdata,
2574 &mgmt->u.action.u.chan_switch.sw_elem,
2575 (void *)ifmgd->associated->priv,
2576 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002577 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002578 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002579 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002580 mutex_unlock(&ifmgd->mtx);
2581
Johannes Berg66e67e42012-01-20 13:55:27 +01002582 switch (rma) {
2583 case RX_MGMT_NONE:
2584 /* no action */
2585 break;
2586 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002587 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002588 break;
2589 case RX_MGMT_CFG80211_DISASSOC:
2590 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2591 break;
2592 case RX_MGMT_CFG80211_RX_AUTH:
2593 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2594 break;
2595 case RX_MGMT_CFG80211_RX_ASSOC:
2596 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2597 break;
2598 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2599 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2600 break;
2601 default:
2602 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002603 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002604}
2605
Johannes Berg9c6bd792008-09-11 00:01:52 +02002606static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002607{
2608 struct ieee80211_sub_if_data *sdata =
2609 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002610 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002611 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002612
Johannes Berg5bb644a2009-05-17 11:40:42 +02002613 if (local->quiescing) {
2614 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2615 return;
2616 }
2617
Johannes Berg64592c82010-06-10 10:21:31 +02002618 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002619}
2620
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002621static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002622 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002623{
2624 struct ieee80211_local *local = sdata->local;
2625 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5fef7db2012-02-24 13:50:52 +01002626 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002627
Johannes Berg37ad3882012-02-24 13:50:54 +01002628 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2629 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002630 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002631
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002632 /*
2633 * must be outside lock due to cfg80211,
2634 * but that's not a problem.
2635 */
Johannes Berg5fef7db2012-02-24 13:50:52 +01002636 cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002637
2638 mutex_lock(&local->mtx);
2639 ieee80211_recalc_idle(local);
2640 mutex_unlock(&local->mtx);
2641
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002642 mutex_lock(&ifmgd->mtx);
2643}
2644
Johannes Berg66e67e42012-01-20 13:55:27 +01002645static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2646{
2647 struct ieee80211_local *local = sdata->local;
2648 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2649 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2650
2651 lockdep_assert_held(&ifmgd->mtx);
2652
2653 if (WARN_ON_ONCE(!auth_data))
2654 return -EINVAL;
2655
Johannes Berg66e67e42012-01-20 13:55:27 +01002656 auth_data->tries++;
2657
2658 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002659 sdata_info(sdata, "authentication with %pM timed out\n",
2660 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002661
2662 /*
2663 * Most likely AP is not in the range so remove the
2664 * bss struct for that AP.
2665 */
2666 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2667
2668 return -ETIMEDOUT;
2669 }
2670
Johannes Berga1845fc2012-06-27 13:18:36 +02002671 drv_mgd_prepare_tx(local, sdata);
2672
Johannes Berg66e67e42012-01-20 13:55:27 +01002673 if (auth_data->bss->proberesp_ies) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002674 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2675 auth_data->bss->bssid, auth_data->tries,
2676 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002677
2678 auth_data->expected_transaction = 2;
2679 ieee80211_send_auth(sdata, 1, auth_data->algorithm,
2680 auth_data->ie, auth_data->ie_len,
2681 auth_data->bss->bssid,
2682 auth_data->bss->bssid, NULL, 0, 0);
2683 } else {
2684 const u8 *ssidie;
2685
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002686 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2687 auth_data->bss->bssid, auth_data->tries,
2688 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002689
2690 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2691 if (!ssidie)
2692 return -EINVAL;
2693 /*
2694 * Direct probe is sent to broadcast address as some APs
2695 * will not answer to direct packet in unassociated state.
2696 */
2697 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
2698 NULL, 0, (u32) -1, true, false);
2699 }
2700
2701 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2702 run_again(ifmgd, auth_data->timeout);
2703
2704 return 0;
2705}
2706
2707static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2708{
2709 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2710 struct ieee80211_local *local = sdata->local;
2711
2712 lockdep_assert_held(&sdata->u.mgd.mtx);
2713
Johannes Berg66e67e42012-01-20 13:55:27 +01002714 assoc_data->tries++;
2715 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002716 sdata_info(sdata, "association with %pM timed out\n",
2717 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002718
2719 /*
2720 * Most likely AP is not in the range so remove the
2721 * bss struct for that AP.
2722 */
2723 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2724
2725 return -ETIMEDOUT;
2726 }
2727
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002728 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2729 assoc_data->bss->bssid, assoc_data->tries,
2730 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002731 ieee80211_send_assoc(sdata);
2732
2733 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2734 run_again(&sdata->u.mgd, assoc_data->timeout);
2735
2736 return 0;
2737}
2738
Johannes Berg1fa57d02010-06-10 10:21:32 +02002739void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002740{
Johannes Berg60f8b392008-09-08 17:44:22 +02002741 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002742 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002743
Johannes Berg77fdaa12009-07-07 03:45:17 +02002744 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002745
Johannes Berg66e67e42012-01-20 13:55:27 +01002746 if (ifmgd->auth_data &&
2747 time_after(jiffies, ifmgd->auth_data->timeout)) {
2748 if (ifmgd->auth_data->done) {
2749 /*
2750 * ok ... we waited for assoc but userspace didn't,
2751 * so let's just kill the auth data
2752 */
2753 ieee80211_destroy_auth_data(sdata, false);
2754 } else if (ieee80211_probe_auth(sdata)) {
2755 u8 bssid[ETH_ALEN];
2756
2757 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2758
2759 ieee80211_destroy_auth_data(sdata, false);
2760
2761 mutex_unlock(&ifmgd->mtx);
2762 cfg80211_send_auth_timeout(sdata->dev, bssid);
2763 mutex_lock(&ifmgd->mtx);
2764 }
2765 } else if (ifmgd->auth_data)
2766 run_again(ifmgd, ifmgd->auth_data->timeout);
2767
2768 if (ifmgd->assoc_data &&
2769 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2770 if (!ifmgd->assoc_data->have_beacon ||
2771 ieee80211_do_assoc(sdata)) {
2772 u8 bssid[ETH_ALEN];
2773
2774 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2775
2776 ieee80211_destroy_assoc_data(sdata, false);
2777
2778 mutex_unlock(&ifmgd->mtx);
2779 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2780 mutex_lock(&ifmgd->mtx);
2781 }
2782 } else if (ifmgd->assoc_data)
2783 run_again(ifmgd, ifmgd->assoc_data->timeout);
2784
Johannes Bergb291ba12009-07-10 15:29:03 +02002785 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2786 IEEE80211_STA_CONNECTION_POLL) &&
2787 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002788 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002789 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002790
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002791 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002792
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002793 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002794 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002795 else
Ben Greear180205b2011-02-04 15:30:24 -08002796 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002797
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002798 /* ACK received for nullfunc probing frame */
2799 if (!ifmgd->probe_send_count)
2800 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002801 else if (ifmgd->nullfunc_failed) {
2802 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002803 mlme_dbg(sdata,
2804 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
2805 bssid, ifmgd->probe_send_count,
2806 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002807 ieee80211_mgd_probe_ap_send(sdata);
2808 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002809 mlme_dbg(sdata,
2810 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
2811 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02002812 ieee80211_sta_connection_lost(sdata, bssid,
2813 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002814 }
2815 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002816 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002817 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002818 mlme_dbg(sdata,
2819 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
2820 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02002821 ieee80211_sta_connection_lost(sdata, bssid,
2822 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002823 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002824 mlme_dbg(sdata,
2825 "No probe response from AP %pM after %dms, try %d/%i\n",
2826 bssid, probe_wait_ms,
2827 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002828 ieee80211_mgd_probe_ap_send(sdata);
2829 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002830 /*
2831 * We actually lost the connection ... or did we?
2832 * Let's make sure!
2833 */
Ben Greearb38afa82010-10-07 16:12:06 -07002834 wiphy_debug(local->hw.wiphy,
2835 "%s: No probe response from AP %pM"
2836 " after %dms, disconnecting.\n",
2837 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002838 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002839
Johannes Berg95acac62011-07-12 12:30:59 +02002840 ieee80211_sta_connection_lost(sdata, bssid,
2841 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02002842 }
2843 }
2844
Johannes Berg77fdaa12009-07-07 03:45:17 +02002845 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002846
2847 mutex_lock(&local->mtx);
2848 ieee80211_recalc_idle(local);
2849 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002850}
Johannes Berg0a51b272008-09-08 17:44:25 +02002851
Johannes Bergb291ba12009-07-10 15:29:03 +02002852static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2853{
2854 struct ieee80211_sub_if_data *sdata =
2855 (struct ieee80211_sub_if_data *) data;
2856 struct ieee80211_local *local = sdata->local;
2857
2858 if (local->quiescing)
2859 return;
2860
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002861 ieee80211_queue_work(&sdata->local->hw,
2862 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002863}
2864
2865static void ieee80211_sta_conn_mon_timer(unsigned long data)
2866{
2867 struct ieee80211_sub_if_data *sdata =
2868 (struct ieee80211_sub_if_data *) data;
2869 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2870 struct ieee80211_local *local = sdata->local;
2871
2872 if (local->quiescing)
2873 return;
2874
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002875 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002876}
2877
2878static void ieee80211_sta_monitor_work(struct work_struct *work)
2879{
2880 struct ieee80211_sub_if_data *sdata =
2881 container_of(work, struct ieee80211_sub_if_data,
2882 u.mgd.monitor_work);
2883
2884 ieee80211_mgd_probe_ap(sdata, false);
2885}
2886
Johannes Berga1678f82008-09-11 00:01:47 +02002887static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2888{
Eliad Peller494f1fe2012-02-19 15:26:09 +02002889 u32 flags;
2890
Kalle Vaload935682009-04-19 08:47:19 +03002891 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002892 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03002893
Johannes Bergb291ba12009-07-10 15:29:03 +02002894 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02002895 flags = sdata->local->hw.flags;
2896 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
2897 ieee80211_queue_work(&sdata->local->hw,
2898 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002899 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02002900 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03002901 }
Johannes Berga1678f82008-09-11 00:01:47 +02002902}
2903
Johannes Berg5bb644a2009-05-17 11:40:42 +02002904#ifdef CONFIG_PM
2905void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
2906{
2907 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2908
2909 /*
2910 * we need to use atomic bitops for the running bits
2911 * only because both timers might fire at the same
2912 * time -- the code here is properly synchronised.
2913 */
2914
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002915 cancel_work_sync(&ifmgd->request_smps_work);
2916
Johannes Berg0ecfe802011-11-09 12:14:16 +01002917 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002918 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002919 if (del_timer_sync(&ifmgd->timer))
2920 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2921
2922 cancel_work_sync(&ifmgd->chswitch_work);
2923 if (del_timer_sync(&ifmgd->chswitch_timer))
2924 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02002925
Johannes Bergb291ba12009-07-10 15:29:03 +02002926 /* these will just be re-established on connection */
2927 del_timer_sync(&ifmgd->conn_mon_timer);
2928 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002929}
2930
2931void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
2932{
2933 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2934
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05302935 if (!ifmgd->associated)
2936 return;
2937
Johannes Berg95acac62011-07-12 12:30:59 +02002938 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
2939 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
2940 mutex_lock(&ifmgd->mtx);
2941 if (ifmgd->associated) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002942 mlme_dbg(sdata,
2943 "driver requested disconnect after resume\n");
Johannes Berg95acac62011-07-12 12:30:59 +02002944 ieee80211_sta_connection_lost(sdata,
2945 ifmgd->associated->bssid,
2946 WLAN_REASON_UNSPECIFIED);
2947 mutex_unlock(&ifmgd->mtx);
2948 return;
2949 }
2950 mutex_unlock(&ifmgd->mtx);
2951 }
2952
Johannes Berg5bb644a2009-05-17 11:40:42 +02002953 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
2954 add_timer(&ifmgd->timer);
2955 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
2956 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01002957 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002958
2959 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01002960 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002961 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02002962}
2963#endif
2964
Johannes Berg9c6bd792008-09-11 00:01:52 +02002965/* interface setup */
2966void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
2967{
Johannes Berg46900292009-02-15 12:44:28 +01002968 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002969
Johannes Berg46900292009-02-15 12:44:28 +01002970 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002971 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01002972 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002973 INIT_WORK(&ifmgd->beacon_connection_loss_work,
2974 ieee80211_beacon_connection_loss_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02002975 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01002976 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02002977 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002978 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
2979 (unsigned long) sdata);
2980 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
2981 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01002982 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05302983 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02002984
Johannes Bergab1faea2009-07-01 21:41:17 +02002985 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05302986 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02002987 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
2988 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02002989
Johannes Berg77fdaa12009-07-07 03:45:17 +02002990 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01002991
2992 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
2993 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
2994 else
2995 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02002996}
2997
2998/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02002999void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3000{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003001 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003002
3003 /* Restart STA timers */
3004 rcu_read_lock();
3005 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3006 ieee80211_restart_sta_timer(sdata);
3007 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003008}
Johannes Berg10f644a2009-04-16 13:17:25 +02003009
3010int ieee80211_max_network_latency(struct notifier_block *nb,
3011 unsigned long data, void *dummy)
3012{
3013 s32 latency_usec = (s32) data;
3014 struct ieee80211_local *local =
3015 container_of(nb, struct ieee80211_local,
3016 network_latency_notifier);
3017
3018 mutex_lock(&local->iflist_mtx);
3019 ieee80211_recalc_ps(local, latency_usec);
3020 mutex_unlock(&local->iflist_mtx);
3021
3022 return 0;
3023}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003024
Johannes Berga1cf7752012-03-08 15:02:07 +01003025static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003026 struct cfg80211_bss *cbss, bool assoc)
Johannes Berga1cf7752012-03-08 15:02:07 +01003027{
3028 struct ieee80211_local *local = sdata->local;
3029 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003030 struct ieee80211_bss *bss = (void *)cbss->priv;
Luciano Coelho6df653c2012-06-18 15:52:51 +03003031 struct sta_info *sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003032 bool have_sta = false;
3033 int err;
Johannes Berg24398e32012-03-28 10:58:36 +02003034 int ht_cfreq;
3035 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3036 const u8 *ht_oper_ie;
3037 const struct ieee80211_ht_operation *ht_oper = NULL;
3038 struct ieee80211_supported_band *sband;
Johannes Berga1cf7752012-03-08 15:02:07 +01003039
3040 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3041 return -EINVAL;
3042
3043 if (assoc) {
3044 rcu_read_lock();
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003045 have_sta = sta_info_get(sdata, cbss->bssid);
Johannes Berga1cf7752012-03-08 15:02:07 +01003046 rcu_read_unlock();
3047 }
3048
3049 if (!have_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003050 sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
Johannes Berga1cf7752012-03-08 15:02:07 +01003051 if (!sta)
3052 return -ENOMEM;
3053 }
3054
3055 mutex_lock(&local->mtx);
3056 ieee80211_recalc_idle(sdata->local);
3057 mutex_unlock(&local->mtx);
3058
3059 /* switch to the right channel */
Johannes Berg24398e32012-03-28 10:58:36 +02003060 sband = local->hw.wiphy->bands[cbss->channel->band];
3061
3062 ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
3063
3064 if (sband->ht_cap.ht_supported) {
3065 ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
3066 cbss->information_elements,
3067 cbss->len_information_elements);
3068 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3069 ht_oper = (void *)(ht_oper_ie + 2);
3070 }
3071
3072 if (ht_oper) {
3073 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3074 cbss->channel->band);
3075 /* check that channel matches the right operating channel */
3076 if (cbss->channel->center_freq != ht_cfreq) {
3077 /*
3078 * It's possible that some APs are confused here;
3079 * Netgear WNDR3700 sometimes reports 4 higher than
3080 * the actual channel in association responses, but
3081 * since we look at probe response/beacon data here
3082 * it should be OK.
3083 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003084 sdata_info(sdata,
3085 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3086 cbss->channel->center_freq,
3087 ht_cfreq, ht_oper->primary_chan,
3088 cbss->channel->band);
Johannes Berg24398e32012-03-28 10:58:36 +02003089 ht_oper = NULL;
3090 }
3091 }
3092
3093 if (ht_oper) {
3094 channel_type = NL80211_CHAN_HT20;
3095
3096 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3097 switch (ht_oper->ht_param &
3098 IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3099 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3100 channel_type = NL80211_CHAN_HT40PLUS;
3101 break;
3102 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3103 channel_type = NL80211_CHAN_HT40MINUS;
3104 break;
3105 }
3106 }
3107 }
3108
3109 if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
3110 /* can only fail due to HT40+/- mismatch */
3111 channel_type = NL80211_CHAN_HT20;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003112 sdata_info(sdata,
3113 "disabling 40 MHz due to multi-vif mismatch\n");
Johannes Berg24398e32012-03-28 10:58:36 +02003114 ifmgd->flags |= IEEE80211_STA_DISABLE_40MHZ;
3115 WARN_ON(!ieee80211_set_channel_type(local, sdata,
3116 channel_type));
3117 }
3118
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003119 local->oper_channel = cbss->channel;
Stanislaw Gruszka6aee4ca2012-06-07 14:47:21 +02003120 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berga1cf7752012-03-08 15:02:07 +01003121
Luciano Coelho6df653c2012-06-18 15:52:51 +03003122 if (sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003123 u32 rates = 0, basic_rates = 0;
3124 bool have_higher_than_11mbit;
3125 int min_rate = INT_MAX, min_rate_index = -1;
3126
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003127 ieee80211_get_rates(sband, bss->supp_rates,
3128 bss->supp_rates_len,
3129 &rates, &basic_rates,
3130 &have_higher_than_11mbit,
3131 &min_rate, &min_rate_index);
3132
3133 /*
3134 * This used to be a workaround for basic rates missing
3135 * in the association response frame. Now that we no
3136 * longer use the basic rates from there, it probably
3137 * doesn't happen any more, but keep the workaround so
3138 * in case some *other* APs are buggy in different ways
3139 * we can connect -- with a warning.
3140 */
3141 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003142 sdata_info(sdata,
3143 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003144 basic_rates = BIT(min_rate_index);
3145 }
3146
3147 sta->sta.supp_rates[cbss->channel->band] = rates;
3148 sdata->vif.bss_conf.basic_rates = basic_rates;
3149
3150 /* cf. IEEE 802.11 9.2.12 */
3151 if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
3152 have_higher_than_11mbit)
3153 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3154 else
3155 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3156
3157 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3158
Johannes Berg8c358bc2012-05-22 22:13:05 +02003159 /* set timing information */
3160 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3161 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3162 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3163
3164 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003165 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003166 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3167 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003168
3169 if (assoc)
3170 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
3171
3172 err = sta_info_insert(sta);
3173 sta = NULL;
3174 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003175 sdata_info(sdata,
3176 "failed to insert STA entry for the AP (error %d)\n",
3177 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003178 return err;
3179 }
3180 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003181 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003182
3183 return 0;
3184}
3185
Johannes Berg77fdaa12009-07-07 03:45:17 +02003186/* config hooks */
3187int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3188 struct cfg80211_auth_request *req)
3189{
Johannes Berg66e67e42012-01-20 13:55:27 +01003190 struct ieee80211_local *local = sdata->local;
3191 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3192 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003193 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003194 int err;
3195
3196 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003197
3198 switch (req->auth_type) {
3199 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3200 auth_alg = WLAN_AUTH_OPEN;
3201 break;
3202 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003203 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003204 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003205 auth_alg = WLAN_AUTH_SHARED_KEY;
3206 break;
3207 case NL80211_AUTHTYPE_FT:
3208 auth_alg = WLAN_AUTH_FT;
3209 break;
3210 case NL80211_AUTHTYPE_NETWORK_EAP:
3211 auth_alg = WLAN_AUTH_LEAP;
3212 break;
3213 default:
3214 return -EOPNOTSUPP;
3215 }
3216
Johannes Berg66e67e42012-01-20 13:55:27 +01003217 auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
3218 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003219 return -ENOMEM;
3220
Johannes Berg66e67e42012-01-20 13:55:27 +01003221 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003222
3223 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003224 memcpy(auth_data->ie, req->ie, req->ie_len);
3225 auth_data->ie_len = req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003226 }
3227
Johannes Bergfffd0932009-07-08 14:22:54 +02003228 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003229 auth_data->key_len = req->key_len;
3230 auth_data->key_idx = req->key_idx;
3231 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003232 }
3233
Johannes Berg66e67e42012-01-20 13:55:27 +01003234 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003235
Johannes Berg66e67e42012-01-20 13:55:27 +01003236 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003237
Johannes Berg66e67e42012-01-20 13:55:27 +01003238 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003239
Johannes Berg66e67e42012-01-20 13:55:27 +01003240 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3241 ifmgd->assoc_data) {
3242 err = -EBUSY;
3243 goto err_free;
3244 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003245
Johannes Berg66e67e42012-01-20 13:55:27 +01003246 if (ifmgd->auth_data)
3247 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003248
Johannes Berg66e67e42012-01-20 13:55:27 +01003249 /* prep auth_data so we don't go into idle on disassoc */
3250 ifmgd->auth_data = auth_data;
3251
3252 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003253 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003254
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003255 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003256
Johannes Berga1cf7752012-03-08 15:02:07 +01003257 err = ieee80211_prep_connection(sdata, req->bss, false);
3258 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003259 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003260
Johannes Berg66e67e42012-01-20 13:55:27 +01003261 err = ieee80211_probe_auth(sdata);
3262 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003263 sta_info_destroy_addr(sdata, req->bss->bssid);
3264 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003265 }
3266
Johannes Berg66e67e42012-01-20 13:55:27 +01003267 /* hold our own reference */
3268 cfg80211_ref_bss(auth_data->bss);
3269 err = 0;
3270 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003271
Johannes Berg66e67e42012-01-20 13:55:27 +01003272 err_clear:
3273 ifmgd->auth_data = NULL;
3274 err_free:
3275 kfree(auth_data);
3276 out_unlock:
3277 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003278
Johannes Berg66e67e42012-01-20 13:55:27 +01003279 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003280}
3281
Johannes Berg77fdaa12009-07-07 03:45:17 +02003282int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3283 struct cfg80211_assoc_request *req)
3284{
Johannes Berg66e67e42012-01-20 13:55:27 +01003285 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003286 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003287 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003288 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003289 struct ieee80211_supported_band *sband;
Johannes Berg9dde6422012-05-16 23:43:19 +02003290 const u8 *ssidie, *ht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003291 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003292
Johannes Berg66e67e42012-01-20 13:55:27 +01003293 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3294 if (!ssidie)
3295 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003296
Johannes Berg66e67e42012-01-20 13:55:27 +01003297 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3298 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003299 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003300
Johannes Berg66e67e42012-01-20 13:55:27 +01003301 mutex_lock(&ifmgd->mtx);
3302
3303 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003304 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003305
3306 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3307 err = -EBUSY;
3308 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003309 }
3310
Johannes Berg66e67e42012-01-20 13:55:27 +01003311 if (ifmgd->assoc_data) {
3312 err = -EBUSY;
3313 goto err_free;
3314 }
3315
3316 if (ifmgd->auth_data) {
3317 bool match;
3318
3319 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003320 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003321 ieee80211_destroy_auth_data(sdata, match);
3322 }
3323
3324 /* prepare assoc data */
3325
Johannes Berg77fdaa12009-07-07 03:45:17 +02003326 ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
Vivek Natarajan375177b2010-02-09 14:50:28 +05303327 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003328 ifmgd->flags &= ~IEEE80211_STA_DISABLE_VHT;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003329
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003330 ifmgd->beacon_crc_valid = false;
3331
Johannes Bergde5036a2012-03-08 15:02:03 +01003332 /*
3333 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3334 * We still associate in non-HT mode (11a/b/g) if any one of these
3335 * ciphers is configured as pairwise.
3336 * We can set this to true for non-11n hardware, that'll be checked
3337 * separately along with the peer capabilities.
3338 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003339 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003340 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3341 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003342 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003343 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003344 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003345 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003346 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003347 }
3348 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003349
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003350 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Ben Greearef96a8422011-11-18 11:32:00 -08003351 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003352 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3353 }
Ben Greearef96a8422011-11-18 11:32:00 -08003354
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003355 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3356 sband = local->hw.wiphy->bands[req->bss->channel->band];
3357 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003358 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003359 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003360 netdev_info(sdata->dev,
3361 "disabling HT as WMM/QoS is not supported\n");
3362 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003363
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003364 /* disable VHT if we don't support it or the AP doesn't use WMM */
3365 if (!sband->vht_cap.vht_supported ||
3366 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3367 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3368 netdev_info(sdata->dev,
3369 "disabling VHT as WMM/QoS is not supported\n");
3370 }
3371
Ben Greearef96a8422011-11-18 11:32:00 -08003372 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3373 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3374 sizeof(ifmgd->ht_capa_mask));
3375
Johannes Berg77fdaa12009-07-07 03:45:17 +02003376 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003377 memcpy(assoc_data->ie, req->ie, req->ie_len);
3378 assoc_data->ie_len = req->ie_len;
3379 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003380
Johannes Berg66e67e42012-01-20 13:55:27 +01003381 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003382
Johannes Bergaf6b6372009-12-23 13:15:35 +01003383 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3384 if (ifmgd->powersave)
3385 ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
3386 else
3387 ifmgd->ap_smps = IEEE80211_SMPS_OFF;
3388 } else
3389 ifmgd->ap_smps = ifmgd->req_smps;
3390
Johannes Berg66e67e42012-01-20 13:55:27 +01003391 assoc_data->capability = req->bss->capability;
Johannes Berg32c5057b2012-03-28 11:04:29 +02003392 assoc_data->wmm = bss->wmm_used &&
3393 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003394 assoc_data->supp_rates = bss->supp_rates;
3395 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003396
3397 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3398 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3399 assoc_data->ap_ht_param =
3400 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3401 else
3402 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg63f170e2009-12-23 13:15:33 +01003403
Kalle Valoab133152010-01-12 10:42:31 +02003404 if (bss->wmm_used && bss->uapsd_supported &&
3405 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003406 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003407 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3408 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003409 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003410 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3411 }
3412
Johannes Berg66e67e42012-01-20 13:55:27 +01003413 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3414 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003415
Johannes Berg77fdaa12009-07-07 03:45:17 +02003416 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003417 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003418
3419 if (req->use_mfp) {
3420 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3421 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3422 } else {
3423 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3424 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3425 }
3426
3427 if (req->crypto.control_port)
3428 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3429 else
3430 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3431
Johannes Berga621fa42010-08-27 14:26:54 +03003432 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3433 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3434
Johannes Berg66e67e42012-01-20 13:55:27 +01003435 /* kick off associate process */
3436
3437 ifmgd->assoc_data = assoc_data;
3438
Johannes Berga1cf7752012-03-08 15:02:07 +01003439 err = ieee80211_prep_connection(sdata, req->bss, true);
3440 if (err)
3441 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003442
3443 if (!bss->dtim_period &&
3444 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3445 /*
3446 * Wait up to one beacon interval ...
3447 * should this be more if we miss one?
3448 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003449 sdata_info(sdata, "waiting for beacon from %pM\n",
3450 ifmgd->bssid);
Johannes Berg3f9768a2012-03-28 21:02:46 +02003451 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +01003452 } else {
3453 assoc_data->have_beacon = true;
3454 assoc_data->sent_assoc = false;
3455 assoc_data->timeout = jiffies;
3456 }
3457 run_again(ifmgd, assoc_data->timeout);
3458
Paul Stewartfcff4f12012-02-23 17:59:53 -08003459 if (bss->corrupt_data) {
3460 char *corrupt_type = "data";
3461 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3462 if (bss->corrupt_data &
3463 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3464 corrupt_type = "beacon and probe response";
3465 else
3466 corrupt_type = "beacon";
3467 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3468 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003469 sdata_info(sdata, "associating with AP with corrupt %s\n",
3470 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08003471 }
3472
Johannes Berg66e67e42012-01-20 13:55:27 +01003473 err = 0;
3474 goto out;
3475 err_clear:
3476 ifmgd->assoc_data = NULL;
3477 err_free:
3478 kfree(assoc_data);
3479 out:
3480 mutex_unlock(&ifmgd->mtx);
3481
3482 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003483}
3484
3485int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003486 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003487{
3488 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5fef7db2012-02-24 13:50:52 +01003489 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003490
Johannes Berg77fdaa12009-07-07 03:45:17 +02003491 mutex_lock(&ifmgd->mtx);
3492
Johannes Berg37ad3882012-02-24 13:50:54 +01003493 if (ifmgd->auth_data) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003494 ieee80211_destroy_auth_data(sdata, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003495 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003496 return 0;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003497 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003498
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003499 sdata_info(sdata,
3500 "deauthenticating from %pM by local choice (reason=%d)\n",
3501 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003502
Johannes Berg37ad3882012-02-24 13:50:54 +01003503 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00003504 ether_addr_equal(ifmgd->associated->bssid, req->bssid))
Johannes Berg37ad3882012-02-24 13:50:54 +01003505 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
Johannes Berg5fef7db2012-02-24 13:50:52 +01003506 req->reason_code, true, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01003507 else
3508 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3509 IEEE80211_STYPE_DEAUTH,
3510 req->reason_code, true,
3511 frame_buf);
3512 mutex_unlock(&ifmgd->mtx);
3513
Johannes Berg5fef7db2012-02-24 13:50:52 +01003514 __cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003515
Johannes Berg7da7cc12010-08-05 17:02:38 +02003516 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003517 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003518 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003519
Johannes Berg77fdaa12009-07-07 03:45:17 +02003520 return 0;
3521}
3522
3523int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003524 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003525{
3526 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003527 u8 bssid[ETH_ALEN];
Johannes Berg5fef7db2012-02-24 13:50:52 +01003528 u8 frame_buf[DEAUTH_DISASSOC_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003529
Johannes Berg77fdaa12009-07-07 03:45:17 +02003530 mutex_lock(&ifmgd->mtx);
3531
Johannes Berg8d8b2612009-07-25 11:58:36 +02003532 /*
3533 * cfg80211 should catch this ... but it's racy since
3534 * we can receive a disassoc frame, process it, hand it
3535 * to cfg80211 while that's in a locked section already
3536 * trying to tell us that the user wants to disconnect.
3537 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003538 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003539 mutex_unlock(&ifmgd->mtx);
3540 return -ENOLINK;
3541 }
3542
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003543 sdata_info(sdata,
3544 "disassociating from %pM by local choice (reason=%d)\n",
3545 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003546
Jouni Malinene69e95d2010-03-29 23:29:31 -07003547 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01003548 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
3549 req->reason_code, !req->local_state_change,
3550 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003551 mutex_unlock(&ifmgd->mtx);
3552
Johannes Berg5fef7db2012-02-24 13:50:52 +01003553 __cfg80211_send_disassoc(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01003554
Johannes Berg7da7cc12010-08-05 17:02:38 +02003555 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003556 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003557 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003558
Johannes Berg77fdaa12009-07-07 03:45:17 +02003559 return 0;
3560}
Jouni Malinen026331c2010-02-15 12:53:10 +02003561
Eliad Pellerafa762f2012-04-23 14:45:15 +03003562void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01003563{
3564 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3565
3566 mutex_lock(&ifmgd->mtx);
3567 if (ifmgd->assoc_data)
3568 ieee80211_destroy_assoc_data(sdata, false);
3569 if (ifmgd->auth_data)
3570 ieee80211_destroy_auth_data(sdata, false);
3571 del_timer_sync(&ifmgd->timer);
3572 mutex_unlock(&ifmgd->mtx);
3573}
3574
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003575void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3576 enum nl80211_cqm_rssi_threshold_event rssi_event,
3577 gfp_t gfp)
3578{
3579 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3580
Johannes Bergb5878a22010-04-07 16:48:40 +02003581 trace_api_cqm_rssi_notify(sdata, rssi_event);
3582
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003583 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3584}
3585EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);