blob: 4ff52d0aaf9c1d93dd0c2a7d0b370c92d71da584 [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 Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
35#define IEEE80211_AUTH_MAX_TRIES 3
36#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
39#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static int max_nullfunc_tries = 2;
42module_param(max_nullfunc_tries, int, 0644);
43MODULE_PARM_DESC(max_nullfunc_tries,
44 "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46static int max_probe_tries = 5;
47module_param(max_probe_tries, int, 0644);
48MODULE_PARM_DESC(max_probe_tries,
49 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * Beacon loss timeout is calculated as N frames times the
53 * advertised beacon interval. This may need to be somewhat
54 * higher than what hardware might detect to account for
55 * delays in the host processing frames. But since we also
56 * probe on beacon miss before declaring the connection lost
57 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010059#define IEEE80211_BEACON_LOSS_COUNT 7
60
Johannes Bergb291ba12009-07-10 15:29:03 +020061/*
62 * Time the connection can be idle before we probe
63 * it to see if we can still talk to the AP.
64 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030065#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time we wait for a probe response after sending
68 * a probe request because of beacon loss or for
69 * checking the connection still works.
70 */
Ben Greear180205b2011-02-04 15:30:24 -080071static int probe_wait_ms = 500;
72module_param(probe_wait_ms, int, 0644);
73MODULE_PARM_DESC(probe_wait_ms,
74 "Maximum time(ms) to wait for probe response"
75 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jouni Malinen17e4ec12010-03-29 23:28:30 -070077/*
78 * Weight given to the latest Beacon frame when calculating average signal
79 * strength for Beacon frames received in the current BSS. This must be
80 * between 1 and 15.
81 */
82#define IEEE80211_SIGNAL_AVE_WEIGHT 3
83
Jouni Malinen391a2002010-08-27 22:22:00 +030084/*
85 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg5bb644a2009-05-17 11:40:42 +020090#define TMR_RUNNING_TIMER 0
91#define TMR_RUNNING_CHANSW 1
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
Johannes Berg7eeff742012-07-18 10:27:27 +0200149 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
150 return;
151
Johannes Bergb291ba12009-07-10 15:29:03 +0200152 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100153 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200154}
155
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400156void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
157{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400158 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
159
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100160 if (unlikely(!sdata->u.mgd.associated))
161 return;
162
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400163 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
164 return;
165
166 mod_timer(&sdata->u.mgd.conn_mon_timer,
167 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400168
169 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400170}
171
Johannes Berg5484e232008-09-08 17:44:27 +0200172static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200173{
Johannes Berg5484e232008-09-08 17:44:27 +0200174 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200175}
176
Johannes Berg24398e32012-03-28 10:58:36 +0200177static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
178 struct ieee80211_ht_operation *ht_oper,
179 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200180{
181 struct ieee80211_local *local = sdata->local;
182 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200183 struct ieee80211_chanctx_conf *chanctx_conf;
184 struct ieee80211_channel *chan;
Johannes Bergd5522e02009-03-30 13:23:35 +0200185 struct sta_info *sta;
186 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200187 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200188 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200189
Johannes Berg55de9082012-07-26 17:24:39 +0200190 rcu_read_lock();
191 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
192 if (WARN_ON(!chanctx_conf)) {
193 rcu_read_unlock();
194 return 0;
195 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100196 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200197 rcu_read_unlock();
198 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200199
Johannes Berg4bf88532012-11-09 11:39:59 +0100200 switch (sdata->vif.bss_conf.chandef.width) {
201 case NL80211_CHAN_WIDTH_40:
202 if (sdata->vif.bss_conf.chandef.chan->center_freq >
203 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100204 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200205 disable_40 = true;
Johannes Berg4bf88532012-11-09 11:39:59 +0100206 if (sdata->vif.bss_conf.chandef.chan->center_freq <
207 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100208 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200209 disable_40 = true;
210 break;
211 default:
212 break;
213 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200214
Johannes Berg24398e32012-03-28 10:58:36 +0200215 /* This can change during the lifetime of the BSS */
216 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200217 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200218
Johannes Berg64f68e52012-03-28 10:58:37 +0200219 mutex_lock(&local->sta_mtx);
220 sta = sta_info_get(sdata, bssid);
221
222 WARN_ON_ONCE(!sta);
223
224 if (sta && !sta->supports_40mhz)
225 disable_40 = true;
226
227 if (sta && (!reconfig ||
Rajkumar Manoharan91a00992012-04-25 20:24:24 +0530228 (disable_40 != !(sta->sta.ht_cap.cap &
Johannes Berg64f68e52012-03-28 10:58:37 +0200229 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530230
Johannes Berg64f68e52012-03-28 10:58:37 +0200231 if (disable_40)
232 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
233 else
234 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530235
Johannes Berg64f68e52012-03-28 10:58:37 +0200236 rate_control_rate_update(local, sband, sta,
Johannes Berg8f727ef2012-03-30 08:43:32 +0200237 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200238 }
Johannes Berg64f68e52012-03-28 10:58:37 +0200239 mutex_unlock(&local->sta_mtx);
Johannes Bergd5522e02009-03-30 13:23:35 +0200240
Johannes Berg074d46d2012-03-15 19:45:16 +0100241 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200242
243 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200244 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200245 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200246 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200247 }
248
249 return changed;
250}
251
Johannes Berg9c6bd792008-09-11 00:01:52 +0200252/* frame sending functions */
253
Johannes Berg66e67e42012-01-20 13:55:27 +0100254static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
255 struct ieee80211_supported_band *sband,
256 u32 *rates)
257{
258 int i, j, count;
259 *rates = 0;
260 count = 0;
261 for (i = 0; i < supp_rates_len; i++) {
262 int rate = (supp_rates[i] & 0x7F) * 5;
263
264 for (j = 0; j < sband->n_bitrates; j++)
265 if (sband->bitrates[j].bitrate == rate) {
266 *rates |= BIT(j);
267 count++;
268 break;
269 }
270 }
271
272 return count;
273}
274
275static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200276 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100277 struct ieee80211_supported_band *sband,
278 struct ieee80211_channel *channel,
279 enum ieee80211_smps_mode smps)
280{
Johannes Berg66e67e42012-01-20 13:55:27 +0100281 u8 *pos;
282 u32 flags = channel->flags;
283 u16 cap;
284 struct ieee80211_sta_ht_cap ht_cap;
285
286 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
287
Johannes Berg66e67e42012-01-20 13:55:27 +0100288 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
289 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
290
Johannes Berg66e67e42012-01-20 13:55:27 +0100291 /* determine capability flags */
292 cap = ht_cap.cap;
293
Johannes Berg9dde6422012-05-16 23:43:19 +0200294 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100295 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
296 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
297 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
298 cap &= ~IEEE80211_HT_CAP_SGI_40;
299 }
300 break;
301 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
302 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
303 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
304 cap &= ~IEEE80211_HT_CAP_SGI_40;
305 }
306 break;
307 }
308
Johannes Berg24398e32012-03-28 10:58:36 +0200309 /*
310 * If 40 MHz was disabled associate as though we weren't
311 * capable of 40 MHz -- some broken APs will never fall
312 * back to trying to transmit in 20 MHz.
313 */
314 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
315 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
316 cap &= ~IEEE80211_HT_CAP_SGI_40;
317 }
318
Johannes Berg66e67e42012-01-20 13:55:27 +0100319 /* set SM PS mode properly */
320 cap &= ~IEEE80211_HT_CAP_SM_PS;
321 switch (smps) {
322 case IEEE80211_SMPS_AUTOMATIC:
323 case IEEE80211_SMPS_NUM_MODES:
324 WARN_ON(1);
325 case IEEE80211_SMPS_OFF:
326 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
327 IEEE80211_HT_CAP_SM_PS_SHIFT;
328 break;
329 case IEEE80211_SMPS_STATIC:
330 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
331 IEEE80211_HT_CAP_SM_PS_SHIFT;
332 break;
333 case IEEE80211_SMPS_DYNAMIC:
334 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
335 IEEE80211_HT_CAP_SM_PS_SHIFT;
336 break;
337 }
338
339 /* reserve and fill IE */
340 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
341 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
342}
343
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000344static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
345 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100346 struct ieee80211_supported_band *sband,
347 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000348{
349 u8 *pos;
350 u32 cap;
351 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100352 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000353
354 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
355
356 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
357
358 /* determine capability flags */
359 cap = vht_cap.cap;
360
Johannes Bergf2d9d272012-11-22 14:11:39 +0100361 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
362 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
363 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
364 }
365
366 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
367 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
368 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
369 }
370
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100371 /*
372 * Some APs apparently get confused if our capabilities are better
373 * than theirs, so restrict what we advertise in the assoc request.
374 */
375 if (!(ap_vht_cap->vht_cap_info &
376 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
377 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
378
379 if (!(ap_vht_cap->vht_cap_info &
380 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
381 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
382 IEEE80211_VHT_CAP_RXSTBC_3 |
383 IEEE80211_VHT_CAP_RXSTBC_4);
384
385 for (i = 0; i < 8; i++) {
386 int shift = i * 2;
387 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
388 u16 ap_mcs, our_mcs;
389
390 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
391 mask) >> shift;
392 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
393 mask) >> shift;
394
395 switch (ap_mcs) {
396 default:
397 if (our_mcs <= ap_mcs)
398 break;
399 /* fall through */
400 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
401 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
402 vht_cap.vht_mcs.rx_mcs_map |=
403 cpu_to_le16(ap_mcs << shift);
404 }
405 }
406
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000407 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000408 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000409 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
410}
411
Johannes Berg66e67e42012-01-20 13:55:27 +0100412static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
413{
414 struct ieee80211_local *local = sdata->local;
415 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
416 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
417 struct sk_buff *skb;
418 struct ieee80211_mgmt *mgmt;
419 u8 *pos, qos_info;
420 size_t offset = 0, noffset;
421 int i, count, rates_len, supp_rates_len;
422 u16 capab;
423 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200424 struct ieee80211_chanctx_conf *chanctx_conf;
425 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100426 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100427
428 lockdep_assert_held(&ifmgd->mtx);
429
Johannes Berg55de9082012-07-26 17:24:39 +0200430 rcu_read_lock();
431 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
432 if (WARN_ON(!chanctx_conf)) {
433 rcu_read_unlock();
434 return;
435 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100436 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200437 rcu_read_unlock();
438 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100439
440 if (assoc_data->supp_rates_len) {
441 /*
442 * Get all rates supported by the device and the AP as
443 * some APs don't like getting a superset of their rates
444 * in the association request (e.g. D-Link DAP 1353 in
445 * b-only mode)...
446 */
447 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
448 assoc_data->supp_rates_len,
449 sband, &rates);
450 } else {
451 /*
452 * In case AP not provide any supported rates information
453 * before association, we send information element(s) with
454 * all rates that we support.
455 */
456 rates = ~0;
457 rates_len = sband->n_bitrates;
458 }
459
460 skb = alloc_skb(local->hw.extra_tx_headroom +
461 sizeof(*mgmt) + /* bit too much but doesn't matter */
462 2 + assoc_data->ssid_len + /* SSID */
463 4 + rates_len + /* (extended) rates */
464 4 + /* power capability */
465 2 + 2 * sband->n_channels + /* supported channels */
466 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000467 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100468 assoc_data->ie_len + /* extra IEs */
469 9, /* WMM */
470 GFP_KERNEL);
471 if (!skb)
472 return;
473
474 skb_reserve(skb, local->hw.extra_tx_headroom);
475
476 capab = WLAN_CAPABILITY_ESS;
477
478 if (sband->band == IEEE80211_BAND_2GHZ) {
479 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
480 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
481 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
482 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
483 }
484
485 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
486 capab |= WLAN_CAPABILITY_PRIVACY;
487
488 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
489 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
490 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
491
492 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
493 memset(mgmt, 0, 24);
494 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
495 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
496 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
497
498 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
499 skb_put(skb, 10);
500 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
501 IEEE80211_STYPE_REASSOC_REQ);
502 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
503 mgmt->u.reassoc_req.listen_interval =
504 cpu_to_le16(local->hw.conf.listen_interval);
505 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
506 ETH_ALEN);
507 } else {
508 skb_put(skb, 4);
509 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
510 IEEE80211_STYPE_ASSOC_REQ);
511 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
512 mgmt->u.assoc_req.listen_interval =
513 cpu_to_le16(local->hw.conf.listen_interval);
514 }
515
516 /* SSID */
517 pos = skb_put(skb, 2 + assoc_data->ssid_len);
518 *pos++ = WLAN_EID_SSID;
519 *pos++ = assoc_data->ssid_len;
520 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
521
522 /* add all rates which were marked to be used above */
523 supp_rates_len = rates_len;
524 if (supp_rates_len > 8)
525 supp_rates_len = 8;
526
527 pos = skb_put(skb, supp_rates_len + 2);
528 *pos++ = WLAN_EID_SUPP_RATES;
529 *pos++ = supp_rates_len;
530
531 count = 0;
532 for (i = 0; i < sband->n_bitrates; i++) {
533 if (BIT(i) & rates) {
534 int rate = sband->bitrates[i].bitrate;
535 *pos++ = (u8) (rate / 5);
536 if (++count == 8)
537 break;
538 }
539 }
540
541 if (rates_len > count) {
542 pos = skb_put(skb, rates_len - count + 2);
543 *pos++ = WLAN_EID_EXT_SUPP_RATES;
544 *pos++ = rates_len - count;
545
546 for (i++; i < sband->n_bitrates; i++) {
547 if (BIT(i) & rates) {
548 int rate = sband->bitrates[i].bitrate;
549 *pos++ = (u8) (rate / 5);
550 }
551 }
552 }
553
554 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
555 /* 1. power capabilities */
556 pos = skb_put(skb, 4);
557 *pos++ = WLAN_EID_PWR_CAPABILITY;
558 *pos++ = 2;
559 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200560 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100561
562 /* 2. supported channels */
563 /* TODO: get this in reg domain format */
564 pos = skb_put(skb, 2 * sband->n_channels + 2);
565 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
566 *pos++ = 2 * sband->n_channels;
567 for (i = 0; i < sband->n_channels; i++) {
568 *pos++ = ieee80211_frequency_to_channel(
569 sband->channels[i].center_freq);
570 *pos++ = 1; /* one channel in the subband*/
571 }
572 }
573
574 /* if present, add any custom IEs that go before HT */
575 if (assoc_data->ie_len && assoc_data->ie) {
576 static const u8 before_ht[] = {
577 WLAN_EID_SSID,
578 WLAN_EID_SUPP_RATES,
579 WLAN_EID_EXT_SUPP_RATES,
580 WLAN_EID_PWR_CAPABILITY,
581 WLAN_EID_SUPPORTED_CHANNELS,
582 WLAN_EID_RSN,
583 WLAN_EID_QOS_CAPA,
584 WLAN_EID_RRM_ENABLED_CAPABILITIES,
585 WLAN_EID_MOBILITY_DOMAIN,
586 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
587 };
588 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
589 before_ht, ARRAY_SIZE(before_ht),
590 offset);
591 pos = skb_put(skb, noffset - offset);
592 memcpy(pos, assoc_data->ie + offset, noffset - offset);
593 offset = noffset;
594 }
595
Johannes Bergf2d9d272012-11-22 14:11:39 +0100596 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
597 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
598 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
599
Johannes Berga8243b72012-11-22 14:32:09 +0100600 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200601 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200602 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100603
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000604 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100605 ieee80211_add_vht_ie(sdata, skb, sband,
606 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000607
Johannes Berg66e67e42012-01-20 13:55:27 +0100608 /* if present, add any custom non-vendor IEs that go after HT */
609 if (assoc_data->ie_len && assoc_data->ie) {
610 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
611 assoc_data->ie_len,
612 offset);
613 pos = skb_put(skb, noffset - offset);
614 memcpy(pos, assoc_data->ie + offset, noffset - offset);
615 offset = noffset;
616 }
617
Johannes Berg76f03032012-03-08 15:02:05 +0100618 if (assoc_data->wmm) {
619 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200620 qos_info = ifmgd->uapsd_queues;
621 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100622 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
623 } else {
624 qos_info = 0;
625 }
626
627 pos = skb_put(skb, 9);
628 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
629 *pos++ = 7; /* len */
630 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
631 *pos++ = 0x50;
632 *pos++ = 0xf2;
633 *pos++ = 2; /* WME */
634 *pos++ = 0; /* WME info */
635 *pos++ = 1; /* WME ver */
636 *pos++ = qos_info;
637 }
638
639 /* add any remaining custom (i.e. vendor specific here) IEs */
640 if (assoc_data->ie_len && assoc_data->ie) {
641 noffset = assoc_data->ie_len;
642 pos = skb_put(skb, noffset - offset);
643 memcpy(pos, assoc_data->ie + offset, noffset - offset);
644 }
645
Johannes Berga1845fc2012-06-27 13:18:36 +0200646 drv_mgd_prepare_tx(local, sdata);
647
Johannes Berg66e67e42012-01-20 13:55:27 +0100648 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100649 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
650 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
651 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100652 ieee80211_tx_skb(sdata, skb);
653}
654
Kalle Valo572e0012009-02-10 17:09:31 +0200655void ieee80211_send_pspoll(struct ieee80211_local *local,
656 struct ieee80211_sub_if_data *sdata)
657{
Kalle Valo572e0012009-02-10 17:09:31 +0200658 struct ieee80211_pspoll *pspoll;
659 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200660
Kalle Valod8cd1892010-01-05 20:16:26 +0200661 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
662 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200663 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200664
Kalle Valod8cd1892010-01-05 20:16:26 +0200665 pspoll = (struct ieee80211_pspoll *) skb->data;
666 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200667
Johannes Berg62ae67b2009-11-18 18:42:05 +0100668 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
669 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200670}
671
Johannes Berg965beda2009-04-16 13:17:24 +0200672void ieee80211_send_nullfunc(struct ieee80211_local *local,
673 struct ieee80211_sub_if_data *sdata,
674 int powersave)
675{
676 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200677 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530678 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200679
Kalle Valod8cd1892010-01-05 20:16:26 +0200680 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
681 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200682 return;
683
Kalle Valod8cd1892010-01-05 20:16:26 +0200684 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200685 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200686 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200687
Johannes Berg62ae67b2009-11-18 18:42:05 +0100688 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530689 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
690 IEEE80211_STA_CONNECTION_POLL))
691 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
692
Johannes Berg62ae67b2009-11-18 18:42:05 +0100693 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200694}
695
Felix Fietkaud5242152010-01-08 18:06:26 +0100696static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
697 struct ieee80211_sub_if_data *sdata)
698{
699 struct sk_buff *skb;
700 struct ieee80211_hdr *nullfunc;
701 __le16 fc;
702
703 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
704 return;
705
706 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700707 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100708 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700709
Felix Fietkaud5242152010-01-08 18:06:26 +0100710 skb_reserve(skb, local->hw.extra_tx_headroom);
711
712 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
713 memset(nullfunc, 0, 30);
714 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
715 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
716 nullfunc->frame_control = fc;
717 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
718 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
719 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
720 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
721
722 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
723 ieee80211_tx_skb(sdata, skb);
724}
725
Johannes Bergcc32abd2009-05-15 11:52:31 +0200726/* spectrum management related things */
727static void ieee80211_chswitch_work(struct work_struct *work)
728{
729 struct ieee80211_sub_if_data *sdata =
730 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200731 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
732
Johannes Berg9607e6b2009-12-23 13:15:31 +0100733 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200734 return;
735
Johannes Berg77fdaa12009-07-07 03:45:17 +0200736 mutex_lock(&ifmgd->mtx);
737 if (!ifmgd->associated)
738 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200739
Johannes Berg55de9082012-07-26 17:24:39 +0200740 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200741 if (!sdata->local->ops->channel_switch) {
742 /* call "hw_config" only if doing sw channel switch */
743 ieee80211_hw_config(sdata->local,
744 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300745 } else {
746 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200747 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200748 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200749
Johannes Berg77fdaa12009-07-07 03:45:17 +0200750 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200751 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200752
Johannes Berg57eebdf2012-08-01 15:50:46 +0200753 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200754 ieee80211_wake_queues_by_reason(&sdata->local->hw,
755 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200756 out:
757 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
758 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200759}
760
Johannes Berg5ce6e432010-05-11 16:20:57 +0200761void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
762{
Johannes Berg55de9082012-07-26 17:24:39 +0200763 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
764 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200765
766 trace_api_chswitch_done(sdata, success);
767 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200768 sdata_info(sdata,
769 "driver channel switch failed, disconnecting\n");
770 ieee80211_queue_work(&sdata->local->hw,
771 &ifmgd->csa_connection_drop_work);
772 } else {
773 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200774 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200775}
776EXPORT_SYMBOL(ieee80211_chswitch_done);
777
Johannes Bergcc32abd2009-05-15 11:52:31 +0200778static void ieee80211_chswitch_timer(unsigned long data)
779{
780 struct ieee80211_sub_if_data *sdata =
781 (struct ieee80211_sub_if_data *) data;
782 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
783
Johannes Berg5bb644a2009-05-17 11:40:42 +0200784 if (sdata->local->quiescing) {
785 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
786 return;
787 }
788
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400789 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200790}
791
792void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
793 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200794 struct ieee80211_bss *bss,
795 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200796{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100797 struct cfg80211_bss *cbss =
798 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200799 struct ieee80211_channel *new_ch;
800 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900801 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
802 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200803 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200804
Johannes Berg77fdaa12009-07-07 03:45:17 +0200805 ASSERT_MGD_MTX(ifmgd);
806
807 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200808 return;
809
Helmut Schaafbe9c422009-07-23 12:14:04 +0200810 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200811 return;
812
813 /* Disregard subsequent beacons if we are already running a timer
814 processing a CSA */
815
816 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
817 return;
818
819 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +0200820 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
821 sdata_info(sdata,
822 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
823 ifmgd->associated->bssid, new_freq);
824 ieee80211_queue_work(&sdata->local->hw,
825 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200826 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200827 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200828
Johannes Berg57eebdf2012-08-01 15:50:46 +0200829 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
830
Johannes Berg55de9082012-07-26 17:24:39 +0200831 if (sdata->local->use_chanctx) {
832 sdata_info(sdata,
833 "not handling channel switch with channel contexts\n");
834 ieee80211_queue_work(&sdata->local->hw,
835 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +0100836 return;
Johannes Berg55de9082012-07-26 17:24:39 +0200837 }
838
839 mutex_lock(&sdata->local->chanctx_mtx);
840 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
841 mutex_unlock(&sdata->local->chanctx_mtx);
842 return;
843 }
844 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
845 struct ieee80211_chanctx, conf);
846 if (chanctx->refcount > 1) {
847 sdata_info(sdata,
848 "channel switch with multiple interfaces on the same channel, disconnecting\n");
849 ieee80211_queue_work(&sdata->local->hw,
850 &ifmgd->csa_connection_drop_work);
851 mutex_unlock(&sdata->local->chanctx_mtx);
852 return;
853 }
854 mutex_unlock(&sdata->local->chanctx_mtx);
855
856 sdata->local->csa_channel = new_ch;
857
Johannes Berg57eebdf2012-08-01 15:50:46 +0200858 if (sw_elem->mode)
859 ieee80211_stop_queues_by_reason(&sdata->local->hw,
860 IEEE80211_QUEUE_STOP_REASON_CSA);
861
Johannes Berg5ce6e432010-05-11 16:20:57 +0200862 if (sdata->local->ops->channel_switch) {
863 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200864 struct ieee80211_channel_switch ch_switch = {
865 .timestamp = timestamp,
866 .block_tx = sw_elem->mode,
867 .channel = new_ch,
868 .count = sw_elem->count,
869 };
870
Johannes Berg5ce6e432010-05-11 16:20:57 +0200871 drv_channel_switch(sdata->local, &ch_switch);
872 return;
873 }
874
875 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200876 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400877 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200878 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200879 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200880 TU_TO_EXP_TIME(sw_elem->count *
881 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200882}
883
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200884static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
885 struct ieee80211_channel *channel,
886 const u8 *country_ie, u8 country_ie_len,
887 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200888{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200889 struct ieee80211_country_ie_triplet *triplet;
890 int chan = ieee80211_frequency_to_channel(channel->center_freq);
891 int i, chan_pwr, chan_increment, new_ap_level;
892 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200893
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200894 /* Invalid IE */
895 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200896 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200897
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200898 triplet = (void *)(country_ie + 3);
899 country_ie_len -= 3;
900
901 switch (channel->band) {
902 default:
903 WARN_ON_ONCE(1);
904 /* fall through */
905 case IEEE80211_BAND_2GHZ:
906 case IEEE80211_BAND_60GHZ:
907 chan_increment = 1;
908 break;
909 case IEEE80211_BAND_5GHZ:
910 chan_increment = 4;
911 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200912 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200913
914 /* find channel */
915 while (country_ie_len >= 3) {
916 u8 first_channel = triplet->chans.first_channel;
917
918 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
919 goto next;
920
921 for (i = 0; i < triplet->chans.num_channels; i++) {
922 if (first_channel + i * chan_increment == chan) {
923 have_chan_pwr = true;
924 chan_pwr = triplet->chans.max_power;
925 break;
926 }
927 }
928 if (have_chan_pwr)
929 break;
930
931 next:
932 triplet++;
933 country_ie_len -= 3;
934 }
935
936 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200937 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200938
939 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
940
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200941 if (sdata->ap_power_level == new_ap_level)
942 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200943
944 sdata_info(sdata,
945 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
946 new_ap_level, chan_pwr, *pwr_constr_elem,
947 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200948 sdata->ap_power_level = new_ap_level;
949 if (__ieee80211_recalc_txpower(sdata))
950 return BSS_CHANGED_TXPOWER;
951 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200952}
953
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300954void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
955{
956 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
957 struct ieee80211_local *local = sdata->local;
958 struct ieee80211_conf *conf = &local->hw.conf;
959
960 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
961 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
962 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
963
964 local->disable_dynamic_ps = false;
965 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
966}
967EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
968
969void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
970{
971 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
972 struct ieee80211_local *local = sdata->local;
973 struct ieee80211_conf *conf = &local->hw.conf;
974
975 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
976 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
977 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
978
979 local->disable_dynamic_ps = true;
980 conf->dynamic_ps_timeout = 0;
981 del_timer_sync(&local->dynamic_ps_timer);
982 ieee80211_queue_work(&local->hw,
983 &local->dynamic_ps_enable_work);
984}
985EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
986
Johannes Berg965beda2009-04-16 13:17:24 +0200987/* powersave */
988static void ieee80211_enable_ps(struct ieee80211_local *local,
989 struct ieee80211_sub_if_data *sdata)
990{
991 struct ieee80211_conf *conf = &local->hw.conf;
992
Johannes Bergd5edaed2009-04-22 23:02:51 +0200993 /*
994 * If we are scanning right now then the parameters will
995 * take effect when scan finishes.
996 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200997 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200998 return;
999
Johannes Berg965beda2009-04-16 13:17:24 +02001000 if (conf->dynamic_ps_timeout > 0 &&
1001 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1002 mod_timer(&local->dynamic_ps_timer, jiffies +
1003 msecs_to_jiffies(conf->dynamic_ps_timeout));
1004 } else {
1005 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1006 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301007
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001008 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1009 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1010 return;
1011
1012 conf->flags |= IEEE80211_CONF_PS;
1013 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001014 }
1015}
1016
1017static void ieee80211_change_ps(struct ieee80211_local *local)
1018{
1019 struct ieee80211_conf *conf = &local->hw.conf;
1020
1021 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001022 ieee80211_enable_ps(local, local->ps_sdata);
1023 } else if (conf->flags & IEEE80211_CONF_PS) {
1024 conf->flags &= ~IEEE80211_CONF_PS;
1025 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1026 del_timer_sync(&local->dynamic_ps_timer);
1027 cancel_work_sync(&local->dynamic_ps_enable_work);
1028 }
1029}
1030
Jason Young808118c2011-03-10 16:43:19 -08001031static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1032{
1033 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1034 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001035 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001036
1037 if (!mgd->powersave)
1038 return false;
1039
Johannes Berg05cb9102011-10-28 11:59:47 +02001040 if (mgd->broken_ap)
1041 return false;
1042
Jason Young808118c2011-03-10 16:43:19 -08001043 if (!mgd->associated)
1044 return false;
1045
Jason Young808118c2011-03-10 16:43:19 -08001046 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1047 IEEE80211_STA_CONNECTION_POLL))
1048 return false;
1049
1050 rcu_read_lock();
1051 sta = sta_info_get(sdata, mgd->bssid);
1052 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001053 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001054 rcu_read_unlock();
1055
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001056 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001057}
1058
Johannes Berg965beda2009-04-16 13:17:24 +02001059/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001060void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001061{
1062 struct ieee80211_sub_if_data *sdata, *found = NULL;
1063 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001064 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001065
1066 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1067 local->ps_sdata = NULL;
1068 return;
1069 }
1070
1071 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001072 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001073 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301074 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1075 /* If an AP vif is found, then disable PS
1076 * by setting the count to zero thereby setting
1077 * ps_sdata to NULL.
1078 */
1079 count = 0;
1080 break;
1081 }
Johannes Berg965beda2009-04-16 13:17:24 +02001082 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1083 continue;
1084 found = sdata;
1085 count++;
1086 }
1087
Jason Young808118c2011-03-10 16:43:19 -08001088 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001089 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001090 s32 beaconint_us;
1091
1092 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001093 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001094
1095 beaconint_us = ieee80211_tu_to_usec(
1096 found->vif.bss_conf.beacon_int);
1097
Juuso Oikarinenff616382010-06-09 09:51:52 +03001098 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001099 if (timeout < 0) {
1100 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001101 * Go to full PSM if the user configures a very low
1102 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001103 * The 2000 second value is there for compatibility
1104 * until the PM_QOS_NETWORK_LATENCY is configured
1105 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001106 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001107 if (latency > (1900 * USEC_PER_MSEC) &&
1108 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001109 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001110 else
1111 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001112 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001113 local->dynamic_ps_user_timeout = timeout;
1114 if (!local->disable_dynamic_ps)
1115 conf->dynamic_ps_timeout =
1116 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001117
Johannes Berg04fe2032009-04-22 18:44:37 +02001118 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001119 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001120 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001121 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001122 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001123
1124 /* If the TIM IE is invalid, pretend the value is 1 */
1125 if (!dtimper)
1126 dtimper = 1;
1127 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001128 maxslp = min_t(int, dtimper,
1129 latency / beaconint_us);
1130
Johannes Berg9ccebe62009-04-23 10:32:36 +02001131 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001132 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001133 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001134 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001135 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001136 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001137 }
Johannes Berg965beda2009-04-16 13:17:24 +02001138
1139 ieee80211_change_ps(local);
1140}
1141
Eliad Pellerab095872012-07-27 12:33:22 +03001142void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1143{
1144 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1145
1146 if (sdata->vif.bss_conf.ps != ps_allowed) {
1147 sdata->vif.bss_conf.ps = ps_allowed;
1148 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1149 }
1150}
1151
Johannes Berg965beda2009-04-16 13:17:24 +02001152void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1153{
1154 struct ieee80211_local *local =
1155 container_of(work, struct ieee80211_local,
1156 dynamic_ps_disable_work);
1157
1158 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1159 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1160 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1161 }
1162
1163 ieee80211_wake_queues_by_reason(&local->hw,
1164 IEEE80211_QUEUE_STOP_REASON_PS);
1165}
1166
1167void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1168{
1169 struct ieee80211_local *local =
1170 container_of(work, struct ieee80211_local,
1171 dynamic_ps_enable_work);
1172 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001173 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301174 unsigned long flags;
1175 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001176
1177 /* can only happen when PS was just disabled anyway */
1178 if (!sdata)
1179 return;
1180
Christian Lamparter5e340692011-06-30 21:08:43 +02001181 ifmgd = &sdata->u.mgd;
1182
Johannes Berg965beda2009-04-16 13:17:24 +02001183 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1184 return;
1185
Arik Nemtsov77b70232011-06-26 12:06:54 +03001186 if (!local->disable_dynamic_ps &&
1187 local->hw.conf.dynamic_ps_timeout > 0) {
1188 /* don't enter PS if TX frames are pending */
1189 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301190 mod_timer(&local->dynamic_ps_timer, jiffies +
1191 msecs_to_jiffies(
1192 local->hw.conf.dynamic_ps_timeout));
1193 return;
1194 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001195
1196 /*
1197 * transmission can be stopped by others which leads to
1198 * dynamic_ps_timer expiry. Postpone the ps timer if it
1199 * is not the actual idle state.
1200 */
1201 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1202 for (q = 0; q < local->hw.queues; q++) {
1203 if (local->queue_stop_reasons[q]) {
1204 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1205 flags);
1206 mod_timer(&local->dynamic_ps_timer, jiffies +
1207 msecs_to_jiffies(
1208 local->hw.conf.dynamic_ps_timeout));
1209 return;
1210 }
1211 }
1212 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301213 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301214
Vivek Natarajan375177b2010-02-09 14:50:28 +05301215 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301216 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301217 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001218
Vivek Natarajane8306f92011-04-06 11:41:10 +05301219 if (drv_tx_frames_pending(local))
1220 mod_timer(&local->dynamic_ps_timer, jiffies +
1221 msecs_to_jiffies(
1222 local->hw.conf.dynamic_ps_timeout));
1223 else {
1224 ieee80211_send_nullfunc(local, sdata, 1);
1225 /* Flush to get the tx status of nullfunc frame */
1226 drv_flush(local, false);
1227 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301228 }
1229
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001230 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1231 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301232 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1233 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1234 local->hw.conf.flags |= IEEE80211_CONF_PS;
1235 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1236 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301237
Arik Nemtsov77b70232011-06-26 12:06:54 +03001238 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1239 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001240}
1241
1242void ieee80211_dynamic_ps_timer(unsigned long data)
1243{
1244 struct ieee80211_local *local = (void *) data;
1245
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001246 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001247 return;
1248
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001249 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001250}
1251
Johannes Berg60f8b392008-09-08 17:44:22 +02001252/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001253static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001254 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001255 u8 *wmm_param, size_t wmm_param_len)
1256{
Jiri Bencf0706e82007-05-05 11:45:53 -07001257 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001258 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001259 size_t left;
1260 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001261 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001262
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001263 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001264 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001265
Johannes Berg32c50572012-03-28 11:04:29 +02001266 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001267 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001268
1269 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001270 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001271
Jiri Bencf0706e82007-05-05 11:45:53 -07001272 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001273 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001274
1275 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001276 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001277
Jiri Bencf0706e82007-05-05 11:45:53 -07001278 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001279 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001280 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001281 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001282
1283 pos = wmm_param + 8;
1284 left = wmm_param_len - 8;
1285
1286 memset(&params, 0, sizeof(params));
1287
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001288 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001289 for (; left >= 4; left -= 4, pos += 4) {
1290 int aci = (pos[0] >> 5) & 0x03;
1291 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001292 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001293 int queue;
1294
1295 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001296 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001297 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001298 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001299 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001300 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1301 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001302 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001303 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001304 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001305 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001306 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001307 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1308 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001309 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001310 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001311 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001312 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001313 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001314 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1315 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001316 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001317 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001318 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001319 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001320 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001321 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001322 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1323 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001324 break;
1325 }
1326
1327 params.aifs = pos[0] & 0x0f;
1328 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1329 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001330 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001331 params.uapsd = uapsd;
1332
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001333 mlme_dbg(sdata,
1334 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1335 queue, aci, acm,
1336 params.aifs, params.cw_min, params.cw_max,
1337 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001338 sdata->tx_conf[queue] = params;
1339 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001340 sdata_err(sdata,
1341 "failed to set TX queue parameters for queue %d\n",
1342 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001343 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001344
1345 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001346 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001347 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001348}
1349
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001350static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1351{
1352 lockdep_assert_held(&sdata->local->mtx);
1353
1354 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1355 IEEE80211_STA_BEACON_POLL);
1356 ieee80211_run_deferred_scan(sdata->local);
1357}
1358
1359static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1360{
1361 mutex_lock(&sdata->local->mtx);
1362 __ieee80211_stop_poll(sdata);
1363 mutex_unlock(&sdata->local->mtx);
1364}
1365
Johannes Berg7a5158e2008-10-08 10:59:33 +02001366static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1367 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001368{
Johannes Bergbda39332008-10-11 01:51:51 +02001369 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001370 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001371 bool use_protection;
1372 bool use_short_preamble;
1373 bool use_short_slot;
1374
1375 if (erp_valid) {
1376 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1377 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1378 } else {
1379 use_protection = false;
1380 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1381 }
1382
1383 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001384 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001385 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001386
Johannes Berg471b3ef2007-12-28 14:32:58 +01001387 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001388 bss_conf->use_cts_prot = use_protection;
1389 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001390 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001391
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001392 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001393 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001394 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001395 }
Daniel Draked9430a32007-07-27 15:43:24 +02001396
Johannes Berg7a5158e2008-10-08 10:59:33 +02001397 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001398 bss_conf->use_short_slot = use_short_slot;
1399 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001400 }
1401
1402 return changed;
1403}
1404
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001405static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001406 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001407 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001408{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001409 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001410 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001411 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001412
Johannes Bergae5eb022008-10-14 16:58:37 +02001413 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001414 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001415 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001416
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001417 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1418 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1419
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001420 sdata->u.mgd.associated = cbss;
1421 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001422
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001423 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1424
Johannes Berg488dd7b2012-10-29 20:08:01 +01001425 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001426 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001427
Johannes Berg9caf0362012-11-29 01:25:20 +01001428 rcu_read_lock();
1429 ies = rcu_dereference(cbss->ies);
1430 if (ies) {
1431 u8 noa[2];
1432 int ret;
1433
1434 ret = cfg80211_get_p2p_attr(
1435 ies->data, ies->len,
1436 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1437 noa, sizeof(noa));
1438 if (ret >= 2) {
1439 bss_conf->p2p_oppps = noa[1] & 0x80;
1440 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1441 bss_info_changed |= BSS_CHANGED_P2P_PS;
1442 sdata->u.mgd.p2p_noa_index = noa[0];
1443 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001444 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001445 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001446 }
1447
Johannes Bergb291ba12009-07-10 15:29:03 +02001448 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001449 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001450
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001451 ieee80211_led_assoc(local, 1);
1452
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001453 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001454 /*
1455 * If the AP is buggy we may get here with no DTIM period
1456 * known, so assume it's 1 which is the only safe assumption
1457 * in that case, although if the TIM IE is broken powersave
1458 * probably just won't work at all.
1459 */
1460 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001461 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001462 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001463 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001464 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001465
Juuso Oikarinen68542962010-06-09 13:43:26 +03001466 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001467
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001468 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001469 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001470 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001471 bss_info_changed |= BSS_CHANGED_CQM;
1472
Juuso Oikarinen68542962010-06-09 13:43:26 +03001473 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001474 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001475 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001476
Johannes Bergae5eb022008-10-14 16:58:37 +02001477 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001478
Johannes Berg056508d2009-07-30 21:43:55 +02001479 mutex_lock(&local->iflist_mtx);
1480 ieee80211_recalc_ps(local, -1);
1481 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001482
Johannes Berg04ecd252012-09-11 14:34:12 +02001483 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001484 ieee80211_recalc_ps_vif(sdata);
1485
John W. Linville8a5b33f2010-01-06 15:39:39 -05001486 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001487 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001488}
1489
Jouni Malinene69e95d2010-03-29 23:29:31 -07001490static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001491 u16 stype, u16 reason, bool tx,
1492 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001493{
Johannes Berg46900292009-02-15 12:44:28 +01001494 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001495 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001496 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001497
Johannes Berg77fdaa12009-07-07 03:45:17 +02001498 ASSERT_MGD_MTX(ifmgd);
1499
Johannes Berg37ad3882012-02-24 13:50:54 +01001500 if (WARN_ON_ONCE(tx && !frame_buf))
1501 return;
1502
Johannes Bergb291ba12009-07-10 15:29:03 +02001503 if (WARN_ON(!ifmgd->associated))
1504 return;
1505
David Spinadel79543d82012-06-12 09:59:45 +03001506 ieee80211_stop_poll(sdata);
1507
Johannes Berg77fdaa12009-07-07 03:45:17 +02001508 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001509
1510 /*
1511 * we need to commit the associated = NULL change because the
1512 * scan code uses that to determine whether this iface should
1513 * go to/wake up from powersave or not -- and could otherwise
1514 * wake the queues erroneously.
1515 */
1516 smp_mb();
1517
1518 /*
1519 * Thus, we can only afterwards stop the queues -- to account
1520 * for the case where another CPU is finishing a scan at this
1521 * time -- we don't want the scan code to enable queues.
1522 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001523
John W. Linville8a5b33f2010-01-06 15:39:39 -05001524 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001525 netif_carrier_off(sdata->dev);
1526
Eliad Peller88bc40e2012-07-12 17:35:33 +03001527 /*
1528 * if we want to get out of ps before disassoc (why?) we have
1529 * to do it before sending disassoc, as otherwise the null-packet
1530 * won't be valid.
1531 */
1532 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1533 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1534 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1535 }
1536 local->ps_sdata = NULL;
1537
Eliad Pellerab095872012-07-27 12:33:22 +03001538 /* disable per-vif ps */
1539 ieee80211_recalc_ps_vif(sdata);
1540
Eliad Pellerf8239812012-06-27 14:18:22 +03001541 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1542 if (tx)
1543 drv_flush(local, false);
1544
Johannes Berg37ad3882012-02-24 13:50:54 +01001545 /* deauthenticate/disassociate now */
1546 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001547 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1548 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001549
1550 /* flush out frame */
1551 if (tx)
1552 drv_flush(local, false);
1553
Eliad Peller88a9e312012-06-01 11:14:03 +03001554 /* clear bssid only after building the needed mgmt frames */
1555 memset(ifmgd->bssid, 0, ETH_ALEN);
1556
Johannes Berg37ad3882012-02-24 13:50:54 +01001557 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001558 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001559
1560 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001561 changed |= ieee80211_reset_erp_info(sdata);
1562
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001563 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001564 changed |= BSS_CHANGED_ASSOC;
1565 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001566
Johannes Berg488dd7b2012-10-29 20:08:01 +01001567 sdata->vif.bss_conf.p2p_ctwindow = 0;
1568 sdata->vif.bss_conf.p2p_oppps = false;
1569
Johannes Berg413ad502009-05-08 21:21:06 +02001570 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001571 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1572 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001573
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001574 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301575
Kalle Valo520eb822008-12-18 23:35:27 +02001576 del_timer_sync(&local->dynamic_ps_timer);
1577 cancel_work_sync(&local->dynamic_ps_enable_work);
1578
Juuso Oikarinen68542962010-06-09 13:43:26 +03001579 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001580 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001581 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001582
Johannes Berg3abead52012-03-02 15:56:59 +01001583 sdata->vif.bss_conf.qos = false;
1584 changed |= BSS_CHANGED_QOS;
1585
Johannes Berg0aaffa92010-05-05 15:28:27 +02001586 /* The BSSID (not really interesting) and HT changed */
1587 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001588 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001589
Johannes Berg3abead52012-03-02 15:56:59 +01001590 /* disassociated - set to defaults now */
1591 ieee80211_set_wmm_default(sdata, false);
1592
Johannes Bergb9dcf712010-08-27 12:35:54 +02001593 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1594 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1595 del_timer_sync(&sdata->u.mgd.timer);
1596 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001597
1598 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001599
Johannes Berg826262c2012-12-10 16:38:14 +02001600 sdata->vif.bss_conf.dtim_period = 0;
1601
Johannes Berg028e8da02012-11-26 11:57:41 +01001602 ifmgd->flags = 0;
1603 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001604}
Jiri Bencf0706e82007-05-05 11:45:53 -07001605
Kalle Valo3cf335d2009-03-22 21:57:06 +02001606void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1607 struct ieee80211_hdr *hdr)
1608{
1609 /*
1610 * We can postpone the mgd.timer whenever receiving unicast frames
1611 * from AP because we know that the connection is working both ways
1612 * at that time. But multicast frames (and hence also beacons) must
1613 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001614 * data idle periods for sending the periodic probe request to the
1615 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001616 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001617 if (is_multicast_ether_addr(hdr->addr1))
1618 return;
1619
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001620 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001621}
Jiri Bencf0706e82007-05-05 11:45:53 -07001622
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001623static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1624{
1625 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001626 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001627
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001628 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001629 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001630 IEEE80211_STA_CONNECTION_POLL))) {
1631 mutex_unlock(&local->mtx);
1632 return;
1633 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001634
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001635 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001636
1637 mutex_lock(&local->iflist_mtx);
1638 ieee80211_recalc_ps(local, -1);
1639 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001640
1641 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001642 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001643
1644 /*
1645 * We've received a probe response, but are not sure whether
1646 * we have or will be receiving any beacons or data, so let's
1647 * schedule the timers again, just in case.
1648 */
1649 ieee80211_sta_reset_beacon_monitor(sdata);
1650
1651 mod_timer(&ifmgd->conn_mon_timer,
1652 round_jiffies_up(jiffies +
1653 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001654out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001655 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001656}
1657
1658void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001659 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001660{
Felix Fietkau75706d02010-12-02 21:01:07 +01001661 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001662 return;
1663
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001664 if (ack)
1665 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001666
1667 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1668 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001669 if (ack)
1670 sdata->u.mgd.probe_send_count = 0;
1671 else
1672 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001673 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1674 }
1675}
1676
Maxim Levitskya43abf22009-07-31 18:54:12 +03001677static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1678{
1679 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1680 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001681 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001682 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001683
1684 /*
1685 * Try sending broadcast probe requests for the last three
1686 * probe requests after the first ones failed since some
1687 * buggy APs only support broadcast probe requests.
1688 */
1689 if (ifmgd->probe_send_count >= unicast_limit)
1690 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001691
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001692 /*
1693 * When the hardware reports an accurate Tx ACK status, it's
1694 * better to send a nullfunc frame instead of a probe request,
1695 * as it will kick us off the AP quickly if we aren't associated
1696 * anymore. The timeout will be reset if the frame is ACKed by
1697 * the AP.
1698 */
Soumik Das992e68b2012-05-20 15:31:13 +05301699 ifmgd->probe_send_count++;
1700
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001701 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1702 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001703 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001704 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001705 int ssid_len;
1706
Johannes Berg9caf0362012-11-29 01:25:20 +01001707 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001708 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001709 if (WARN_ON_ONCE(ssid == NULL))
1710 ssid_len = 0;
1711 else
1712 ssid_len = ssid[1];
1713
1714 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001715 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001716 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001717 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001718 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001719
Ben Greear180205b2011-02-04 15:30:24 -08001720 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001721 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301722 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1723 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001724}
1725
Johannes Bergb291ba12009-07-10 15:29:03 +02001726static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1727 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001728{
Kalle Valo04de8382009-03-22 21:57:35 +02001729 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001730 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001731
Johannes Berg9607e6b2009-12-23 13:15:31 +01001732 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001733 return;
1734
Johannes Berg77fdaa12009-07-07 03:45:17 +02001735 mutex_lock(&ifmgd->mtx);
1736
1737 if (!ifmgd->associated)
1738 goto out;
1739
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001740 mutex_lock(&sdata->local->mtx);
1741
1742 if (sdata->local->tmp_channel || sdata->local->scanning) {
1743 mutex_unlock(&sdata->local->mtx);
1744 goto out;
1745 }
1746
Joe Perchese87cc472012-05-13 21:56:26 +00001747 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001748 mlme_dbg_ratelimited(sdata,
1749 "detected beacon loss from AP - sending probe request\n");
1750
Holger Schurig6efb71b2012-05-15 15:38:59 +02001751 ieee80211_cqm_rssi_notify(&sdata->vif,
1752 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001753
Johannes Bergb291ba12009-07-10 15:29:03 +02001754 /*
1755 * The driver/our work has already reported this event or the
1756 * connection monitoring has kicked in and we have already sent
1757 * a probe request. Or maybe the AP died and the driver keeps
1758 * reporting until we disassociate...
1759 *
1760 * In either case we have to ignore the current call to this
1761 * function (except for setting the correct probe reason bit)
1762 * because otherwise we would reset the timer every time and
1763 * never check whether we received a probe response!
1764 */
1765 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1766 IEEE80211_STA_CONNECTION_POLL))
1767 already = true;
1768
1769 if (beacon)
1770 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1771 else
1772 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1773
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001774 mutex_unlock(&sdata->local->mtx);
1775
Johannes Bergb291ba12009-07-10 15:29:03 +02001776 if (already)
1777 goto out;
1778
Johannes Berg4e751842009-06-10 15:16:52 +02001779 mutex_lock(&sdata->local->iflist_mtx);
1780 ieee80211_recalc_ps(sdata->local, -1);
1781 mutex_unlock(&sdata->local->iflist_mtx);
1782
Maxim Levitskya43abf22009-07-31 18:54:12 +03001783 ifmgd->probe_send_count = 0;
1784 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001785 out:
1786 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001787}
1788
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001789struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1790 struct ieee80211_vif *vif)
1791{
1792 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1793 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001794 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001795 struct sk_buff *skb;
1796 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001797 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001798
1799 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1800 return NULL;
1801
1802 ASSERT_MGD_MTX(ifmgd);
1803
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001804 if (ifmgd->associated)
1805 cbss = ifmgd->associated;
1806 else if (ifmgd->auth_data)
1807 cbss = ifmgd->auth_data->bss;
1808 else if (ifmgd->assoc_data)
1809 cbss = ifmgd->assoc_data->bss;
1810 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001811 return NULL;
1812
Johannes Berg9caf0362012-11-29 01:25:20 +01001813 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001814 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001815 if (WARN_ON_ONCE(ssid == NULL))
1816 ssid_len = 0;
1817 else
1818 ssid_len = ssid[1];
1819
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001820 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001821 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001822 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001823 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001824 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001825
1826 return skb;
1827}
1828EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1829
Johannes Berg882a7c62012-08-01 22:32:45 +02001830static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1831 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001832{
1833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1834 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001835 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001836
1837 mutex_lock(&ifmgd->mtx);
1838 if (!ifmgd->associated) {
1839 mutex_unlock(&ifmgd->mtx);
1840 return;
1841 }
1842
Johannes Berg37ad3882012-02-24 13:50:54 +01001843 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1844 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001845 transmit_frame, frame_buf);
1846 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001847 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001848
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001849 /*
1850 * must be outside lock due to cfg80211,
1851 * but that's not a problem.
1852 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001853 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001854
1855 mutex_lock(&local->mtx);
1856 ieee80211_recalc_idle(local);
1857 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001858}
1859
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001860static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001861{
1862 struct ieee80211_sub_if_data *sdata =
1863 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001864 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001865 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1866 struct sta_info *sta;
1867
1868 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301869 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001870 sta = sta_info_get(sdata, ifmgd->bssid);
1871 if (sta)
1872 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301873 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001874 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001875
Johannes Berg882a7c62012-08-01 22:32:45 +02001876 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1877 sdata_info(sdata, "Connection to AP %pM lost\n",
1878 ifmgd->bssid);
1879 __ieee80211_disconnect(sdata, false);
1880 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001881 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001882 }
1883}
1884
1885static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1886{
1887 struct ieee80211_sub_if_data *sdata =
1888 container_of(work, struct ieee80211_sub_if_data,
1889 u.mgd.csa_connection_drop_work);
1890
1891 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1892 IEEE80211_QUEUE_STOP_REASON_CSA);
1893 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001894}
1895
Kalle Valo04de8382009-03-22 21:57:35 +02001896void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1897{
1898 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001899 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001900
Johannes Bergb5878a22010-04-07 16:48:40 +02001901 trace_api_beacon_loss(sdata);
1902
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001903 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1904 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001905}
1906EXPORT_SYMBOL(ieee80211_beacon_loss);
1907
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001908void ieee80211_connection_loss(struct ieee80211_vif *vif)
1909{
1910 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1911 struct ieee80211_hw *hw = &sdata->local->hw;
1912
Johannes Bergb5878a22010-04-07 16:48:40 +02001913 trace_api_connection_loss(sdata);
1914
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001915 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1916 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1917}
1918EXPORT_SYMBOL(ieee80211_connection_loss);
1919
1920
Johannes Berg66e67e42012-01-20 13:55:27 +01001921static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1922 bool assoc)
1923{
1924 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1925
1926 lockdep_assert_held(&sdata->u.mgd.mtx);
1927
Johannes Berg66e67e42012-01-20 13:55:27 +01001928 if (!assoc) {
1929 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1930
1931 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1932 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01001933 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001934 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001935 }
1936
1937 cfg80211_put_bss(auth_data->bss);
1938 kfree(auth_data);
1939 sdata->u.mgd.auth_data = NULL;
1940}
1941
1942static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1943 struct ieee80211_mgmt *mgmt, size_t len)
1944{
Johannes Berg1672c0e32013-01-29 15:02:27 +01001945 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01001946 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1947 u8 *pos;
1948 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01001949 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01001950
1951 pos = mgmt->u.auth.variable;
1952 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1953 if (!elems.challenge)
1954 return;
1955 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001956 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01001957 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1958 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
1959 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001960 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001961 elems.challenge - 2, elems.challenge_len + 2,
1962 auth_data->bss->bssid, auth_data->bss->bssid,
1963 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001964 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01001965}
1966
1967static enum rx_mgmt_action __must_check
1968ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1969 struct ieee80211_mgmt *mgmt, size_t len)
1970{
1971 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1972 u8 bssid[ETH_ALEN];
1973 u16 auth_alg, auth_transaction, status_code;
1974 struct sta_info *sta;
1975
1976 lockdep_assert_held(&ifmgd->mtx);
1977
1978 if (len < 24 + 6)
1979 return RX_MGMT_NONE;
1980
1981 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1982 return RX_MGMT_NONE;
1983
1984 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1985
Joe Perchesb203ca32012-05-08 18:56:52 +00001986 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001987 return RX_MGMT_NONE;
1988
1989 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1990 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1991 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1992
1993 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001994 auth_transaction != ifmgd->auth_data->expected_transaction) {
1995 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1996 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1997 auth_transaction,
1998 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001999 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002000 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002001
2002 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002003 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2004 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002005 ieee80211_destroy_auth_data(sdata, false);
2006 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002007 }
2008
2009 switch (ifmgd->auth_data->algorithm) {
2010 case WLAN_AUTH_OPEN:
2011 case WLAN_AUTH_LEAP:
2012 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002013 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002014 break;
2015 case WLAN_AUTH_SHARED_KEY:
2016 if (ifmgd->auth_data->expected_transaction != 4) {
2017 ieee80211_auth_challenge(sdata, mgmt, len);
2018 /* need another frame */
2019 return RX_MGMT_NONE;
2020 }
2021 break;
2022 default:
2023 WARN_ONCE(1, "invalid auth alg %d",
2024 ifmgd->auth_data->algorithm);
2025 return RX_MGMT_NONE;
2026 }
2027
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002028 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002029 ifmgd->auth_data->done = true;
2030 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2031 run_again(ifmgd, ifmgd->auth_data->timeout);
2032
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002033 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2034 ifmgd->auth_data->expected_transaction != 2) {
2035 /*
2036 * Report auth frame to user space for processing since another
2037 * round of Authentication frames is still needed.
2038 */
2039 return RX_MGMT_CFG80211_RX_AUTH;
2040 }
2041
Johannes Berg66e67e42012-01-20 13:55:27 +01002042 /* move station state to auth */
2043 mutex_lock(&sdata->local->sta_mtx);
2044 sta = sta_info_get(sdata, bssid);
2045 if (!sta) {
2046 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2047 goto out_err;
2048 }
2049 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002050 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002051 goto out_err;
2052 }
2053 mutex_unlock(&sdata->local->sta_mtx);
2054
2055 return RX_MGMT_CFG80211_RX_AUTH;
2056 out_err:
2057 mutex_unlock(&sdata->local->sta_mtx);
2058 /* ignore frame -- wait for timeout */
2059 return RX_MGMT_NONE;
2060}
2061
2062
Johannes Berg77fdaa12009-07-07 03:45:17 +02002063static enum rx_mgmt_action __must_check
2064ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002065 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002066{
Johannes Berg46900292009-02-15 12:44:28 +01002067 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002068 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002069 u16 reason_code;
2070
Johannes Berg66e67e42012-01-20 13:55:27 +01002071 lockdep_assert_held(&ifmgd->mtx);
2072
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002073 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002074 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002075
Johannes Berg66e67e42012-01-20 13:55:27 +01002076 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002077 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002078 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002079
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002080 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002081
2082 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2083
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002084 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2085 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002086
Johannes Berg37ad3882012-02-24 13:50:54 +01002087 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2088
Johannes Berg7da7cc12010-08-05 17:02:38 +02002089 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01002090 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002091 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002092
2093 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002094}
2095
2096
Johannes Berg77fdaa12009-07-07 03:45:17 +02002097static enum rx_mgmt_action __must_check
2098ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2099 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002100{
Johannes Berg46900292009-02-15 12:44:28 +01002101 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002102 u16 reason_code;
2103
Johannes Berg66e67e42012-01-20 13:55:27 +01002104 lockdep_assert_held(&ifmgd->mtx);
2105
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002106 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002107 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002108
Johannes Berg66e67e42012-01-20 13:55:27 +01002109 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002110 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002111 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002112
2113 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2114
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002115 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2116 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002117
Johannes Berg37ad3882012-02-24 13:50:54 +01002118 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2119
Johannes Berg7da7cc12010-08-05 17:02:38 +02002120 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002121 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002122 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002123
Johannes Berg77fdaa12009-07-07 03:45:17 +02002124 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002125}
2126
Christian Lamparterc74d0842011-10-15 00:14:49 +02002127static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2128 u8 *supp_rates, unsigned int supp_rates_len,
2129 u32 *rates, u32 *basic_rates,
2130 bool *have_higher_than_11mbit,
2131 int *min_rate, int *min_rate_index)
2132{
2133 int i, j;
2134
2135 for (i = 0; i < supp_rates_len; i++) {
2136 int rate = (supp_rates[i] & 0x7f) * 5;
2137 bool is_basic = !!(supp_rates[i] & 0x80);
2138
2139 if (rate > 110)
2140 *have_higher_than_11mbit = true;
2141
2142 /*
2143 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2144 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2145 *
2146 * Note: Even through the membership selector and the basic
2147 * rate flag share the same bit, they are not exactly
2148 * the same.
2149 */
2150 if (!!(supp_rates[i] & 0x80) &&
2151 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2152 continue;
2153
2154 for (j = 0; j < sband->n_bitrates; j++) {
2155 if (sband->bitrates[j].bitrate == rate) {
2156 *rates |= BIT(j);
2157 if (is_basic)
2158 *basic_rates |= BIT(j);
2159 if (rate < *min_rate) {
2160 *min_rate = rate;
2161 *min_rate_index = j;
2162 }
2163 break;
2164 }
2165 }
2166 }
2167}
Jiri Bencf0706e82007-05-05 11:45:53 -07002168
Johannes Berg66e67e42012-01-20 13:55:27 +01002169static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2170 bool assoc)
2171{
2172 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2173
2174 lockdep_assert_held(&sdata->u.mgd.mtx);
2175
Johannes Berg66e67e42012-01-20 13:55:27 +01002176 if (!assoc) {
2177 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2178
2179 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2180 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002181 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002182 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002183 }
2184
2185 kfree(assoc_data);
2186 sdata->u.mgd.assoc_data = NULL;
2187}
2188
2189static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2190 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002191 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002192{
Johannes Berg46900292009-02-15 12:44:28 +01002193 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002194 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002195 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002196 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002197 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002198 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002199 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002200 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002201 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002202 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002203
Johannes Bergaf6b6372009-12-23 13:15:35 +01002204 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002205
Jiri Bencf0706e82007-05-05 11:45:53 -07002206 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002207 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002208
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002209 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002210 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2211 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002212 aid &= ~(BIT(15) | BIT(14));
2213
Johannes Berg05cb9102011-10-28 11:59:47 +02002214 ifmgd->broken_ap = false;
2215
2216 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002217 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2218 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002219 aid = 0;
2220 ifmgd->broken_ap = true;
2221 }
2222
Johannes Bergaf6b6372009-12-23 13:15:35 +01002223 pos = mgmt->u.assoc_resp.variable;
2224 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2225
Jiri Bencf0706e82007-05-05 11:45:53 -07002226 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002227 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002228 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002229 }
2230
Johannes Berg46900292009-02-15 12:44:28 +01002231 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002232
Guy Eilam2a33bee2011-08-17 15:18:15 +03002233 mutex_lock(&sdata->local->sta_mtx);
2234 /*
2235 * station info was already allocated and inserted before
2236 * the association and should be available to us
2237 */
Johannes Berg7852e362012-01-20 13:55:24 +01002238 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002239 if (WARN_ON(!sta)) {
2240 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002241 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002242 }
2243
Johannes Berg55de9082012-07-26 17:24:39 +02002244 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002245
Johannes Berga8243b72012-11-22 14:32:09 +01002246 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002247 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002248 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002249
Johannes Berg64f68e52012-03-28 10:58:37 +02002250 sta->supports_40mhz =
2251 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2252
Mahesh Palivela818255e2012-10-10 11:33:04 +00002253 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2254 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2255 elems.vht_cap_elem,
2256 &sta->sta.vht_cap);
2257
Johannes Berg4b7679a2008-09-18 18:14:18 +02002258 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002259
Johannes Berg46900292009-02-15 12:44:28 +01002260 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002261 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002262
Johannes Bergddf4ac52008-10-22 11:41:38 +02002263 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002264 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002265
Johannes Bergc8987872012-01-20 13:55:17 +01002266 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2267 if (!err)
2268 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2269 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2270 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2271 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002272 sdata_info(sdata,
2273 "failed to move station %pM to desired state\n",
2274 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002275 WARN_ON(__sta_info_destroy(sta));
2276 mutex_unlock(&sdata->local->sta_mtx);
2277 return false;
2278 }
2279
Johannes Berg7852e362012-01-20 13:55:24 +01002280 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002281
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002282 /*
2283 * Always handle WMM once after association regardless
2284 * of the first value the AP uses. Setting -1 here has
2285 * that effect because the AP values is an unsigned
2286 * 4-bit value.
2287 */
2288 ifmgd->wmm_last_param_set = -1;
2289
Johannes Bergddf4ac52008-10-22 11:41:38 +02002290 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002291 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002292 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002293 else
Johannes Berg3abead52012-03-02 15:56:59 +01002294 ieee80211_set_wmm_default(sdata, false);
2295 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002296
Johannes Berg074d46d2012-03-15 19:45:16 +01002297 if (elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002298 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002299 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2300 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002301
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002302 /* set AID and assoc capability,
2303 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002304 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002305 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002306 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002307
Kalle Valo15b7b062009-03-22 21:57:14 +02002308 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002309 * If we're using 4-addr mode, let the AP know that we're
2310 * doing so, so that it can create the STA VLAN on its side
2311 */
2312 if (ifmgd->use_4addr)
2313 ieee80211_send_4addr_nullfunc(local, sdata);
2314
2315 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002316 * Start timer to probe the connection to the AP now.
2317 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002318 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002319 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002320 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002321
Johannes Bergaf6b6372009-12-23 13:15:35 +01002322 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002323}
2324
Johannes Berg66e67e42012-01-20 13:55:27 +01002325static enum rx_mgmt_action __must_check
2326ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2327 struct ieee80211_mgmt *mgmt, size_t len,
2328 struct cfg80211_bss **bss)
2329{
2330 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2331 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2332 u16 capab_info, status_code, aid;
2333 struct ieee802_11_elems elems;
2334 u8 *pos;
2335 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002336
Johannes Berg66e67e42012-01-20 13:55:27 +01002337 lockdep_assert_held(&ifmgd->mtx);
2338
2339 if (!assoc_data)
2340 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002341 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002342 return RX_MGMT_NONE;
2343
2344 /*
2345 * AssocResp and ReassocResp have identical structure, so process both
2346 * of them in this function.
2347 */
2348
2349 if (len < 24 + 6)
2350 return RX_MGMT_NONE;
2351
2352 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2353 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2354 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2355 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2356
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002357 sdata_info(sdata,
2358 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2359 reassoc ? "Rea" : "A", mgmt->sa,
2360 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002361
2362 pos = mgmt->u.assoc_resp.variable;
2363 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2364
2365 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2366 elems.timeout_int && elems.timeout_int_len == 5 &&
2367 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2368 u32 tu, ms;
2369 tu = get_unaligned_le32(elems.timeout_int + 1);
2370 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002371 sdata_info(sdata,
2372 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2373 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002374 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2375 if (ms > IEEE80211_ASSOC_TIMEOUT)
2376 run_again(ifmgd, assoc_data->timeout);
2377 return RX_MGMT_NONE;
2378 }
2379
2380 *bss = assoc_data->bss;
2381
2382 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002383 sdata_info(sdata, "%pM denied association (code=%d)\n",
2384 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002385 ieee80211_destroy_assoc_data(sdata, false);
2386 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002387 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2388 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002389 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002390 cfg80211_put_bss(*bss);
2391 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2392 }
John W. Linville635d9992012-07-09 16:34:34 -04002393 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002394
2395 /*
2396 * destroy assoc_data afterwards, as otherwise an idle
2397 * recalc after assoc_data is NULL but before associated
2398 * is set can cause the interface to go idle
2399 */
2400 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002401 }
2402
2403 return RX_MGMT_CFG80211_RX_ASSOC;
2404}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002405
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002406static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002407 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002408 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002409 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002410{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002411 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002412 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002413 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002414 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002415 bool need_ps = false;
2416
Johannes Berg826262c2012-12-10 16:38:14 +02002417 if ((sdata->u.mgd.associated &&
2418 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2419 (sdata->u.mgd.assoc_data &&
2420 ether_addr_equal(mgmt->bssid,
2421 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002422 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002423 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2424
2425 if (elems->tim && !elems->parse_error) {
2426 struct ieee80211_tim_ie *tim_ie = elems->tim;
2427 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2428 }
Johannes Berg56007a02010-01-26 14:19:52 +01002429 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002430
Johannes Berg5bda6172008-09-08 11:05:10 +02002431 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002432 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2433 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002434 else
2435 freq = rx_status->freq;
2436
2437 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2438
2439 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2440 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002441
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002442 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002443 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002444 if (bss)
2445 ieee80211_rx_bss_put(local, bss);
2446
2447 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002448 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002449
Johannes Berg56007a02010-01-26 14:19:52 +01002450 if (need_ps) {
2451 mutex_lock(&local->iflist_mtx);
2452 ieee80211_recalc_ps(local, -1);
2453 mutex_unlock(&local->iflist_mtx);
2454 }
2455
Johannes Berg5bc14202012-08-01 16:13:02 +02002456 if (elems->ch_switch_ie &&
2457 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2458 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002459 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002460}
2461
2462
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002463static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002464 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002465{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002466 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002467 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002468 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2469 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002470 struct ieee802_11_elems elems;
2471
Kalle Valo15b7b062009-03-22 21:57:14 +02002472 ifmgd = &sdata->u.mgd;
2473
Johannes Berg77fdaa12009-07-07 03:45:17 +02002474 ASSERT_MGD_MTX(ifmgd);
2475
Joe Perchesb203ca32012-05-08 18:56:52 +00002476 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002477 return; /* ignore ProbeResp to foreign address */
2478
Ester Kummerae6a44e2008-06-27 18:54:48 +03002479 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2480 if (baselen > len)
2481 return;
2482
2483 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2484 &elems);
2485
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002486 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002487
Johannes Berg77fdaa12009-07-07 03:45:17 +02002488 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002489 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002490 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002491
2492 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002493 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002494 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002495 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002496 ifmgd->auth_data->tries = 0;
2497 ifmgd->auth_data->timeout = jiffies;
2498 run_again(ifmgd, ifmgd->auth_data->timeout);
2499 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002500}
2501
Johannes Bergd91f36d2009-04-16 13:17:26 +02002502/*
2503 * This is the canonical list of information elements we care about,
2504 * the filter code also gives us all changes to the Microsoft OUI
2505 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2506 *
2507 * We implement beacon filtering in software since that means we can
2508 * avoid processing the frame here and in cfg80211, and userspace
2509 * will not be able to tell whether the hardware supports it or not.
2510 *
2511 * XXX: This list needs to be dynamic -- userspace needs to be able to
2512 * add items it requires. It also needs to be able to tell us to
2513 * look out for other vendor IEs.
2514 */
2515static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002516 (1ULL << WLAN_EID_COUNTRY) |
2517 (1ULL << WLAN_EID_ERP_INFO) |
2518 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2519 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2520 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002521 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002522
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002523static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002524 struct ieee80211_mgmt *mgmt,
2525 size_t len,
2526 struct ieee80211_rx_status *rx_status)
2527{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002528 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002529 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002530 size_t baselen;
2531 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002532 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002533 struct ieee80211_chanctx_conf *chanctx_conf;
2534 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002535 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002536 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002537 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002538 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002539 u8 *bssid;
2540
Johannes Berg66e67e42012-01-20 13:55:27 +01002541 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002542
Ester Kummerae6a44e2008-06-27 18:54:48 +03002543 /* Process beacon from the current BSS */
2544 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2545 if (baselen > len)
2546 return;
2547
Johannes Berg55de9082012-07-26 17:24:39 +02002548 rcu_read_lock();
2549 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2550 if (!chanctx_conf) {
2551 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002552 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002553 }
2554
Johannes Berg4bf88532012-11-09 11:39:59 +01002555 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002556 rcu_read_unlock();
2557 return;
2558 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002559 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002560 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002561
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002562 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002563 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002564 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2565 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002566
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002567 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002568 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002569 ifmgd->assoc_data->need_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01002570 /* continue assoc process */
2571 ifmgd->assoc_data->timeout = jiffies;
2572 run_again(ifmgd, ifmgd->assoc_data->timeout);
2573 return;
2574 }
2575
2576 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002577 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002578 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002579 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002580
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002581 /* Track average RSSI from the Beacon frames of the current AP */
2582 ifmgd->last_beacon_signal = rx_status->signal;
2583 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2584 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002585 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002586 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002587 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002588 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002589 } else {
2590 ifmgd->ave_beacon_signal =
2591 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2592 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2593 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002594 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002595 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002596
2597 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2598 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2599 int sig = ifmgd->ave_beacon_signal;
2600 int last_sig = ifmgd->last_ave_beacon_signal;
2601
2602 /*
2603 * if signal crosses either of the boundaries, invoke callback
2604 * with appropriate parameters
2605 */
2606 if (sig > ifmgd->rssi_max_thold &&
2607 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2608 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002609 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002610 } else if (sig < ifmgd->rssi_min_thold &&
2611 (last_sig >= ifmgd->rssi_max_thold ||
2612 last_sig == 0)) {
2613 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002614 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002615 }
2616 }
2617
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002618 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002619 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002620 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002621 int sig = ifmgd->ave_beacon_signal / 16;
2622 int last_event = ifmgd->last_cqm_event_signal;
2623 int thold = bss_conf->cqm_rssi_thold;
2624 int hyst = bss_conf->cqm_rssi_hyst;
2625 if (sig < thold &&
2626 (last_event == 0 || sig < last_event - hyst)) {
2627 ifmgd->last_cqm_event_signal = sig;
2628 ieee80211_cqm_rssi_notify(
2629 &sdata->vif,
2630 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2631 GFP_KERNEL);
2632 } else if (sig > thold &&
2633 (last_event == 0 || sig > last_event + hyst)) {
2634 ifmgd->last_cqm_event_signal = sig;
2635 ieee80211_cqm_rssi_notify(
2636 &sdata->vif,
2637 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2638 GFP_KERNEL);
2639 }
2640 }
2641
Johannes Bergb291ba12009-07-10 15:29:03 +02002642 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002643 mlme_dbg_ratelimited(sdata,
2644 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002645 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002646 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002647 ieee80211_run_deferred_scan(local);
2648 mutex_unlock(&local->mtx);
2649
Johannes Berg4e751842009-06-10 15:16:52 +02002650 mutex_lock(&local->iflist_mtx);
2651 ieee80211_recalc_ps(local, -1);
2652 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002653 }
2654
Johannes Bergb291ba12009-07-10 15:29:03 +02002655 /*
2656 * Push the beacon loss detection into the future since
2657 * we are processing a beacon from the AP just now.
2658 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002659 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002660
Johannes Bergd91f36d2009-04-16 13:17:26 +02002661 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2662 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2663 len - baselen, &elems,
2664 care_about_ies, ncrc);
2665
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302666 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002667 bool directed_tim = ieee80211_check_tim(elems.tim,
2668 elems.tim_len,
2669 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002670 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002671 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002672 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2673 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2674 ieee80211_hw_config(local,
2675 IEEE80211_CONF_CHANGE_PS);
2676 }
Kalle Valo572e0012009-02-10 17:09:31 +02002677 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302678 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002679 local->pspolling = true;
2680
2681 /*
2682 * Here is assumed that the driver will be
2683 * able to send ps-poll frame and receive a
2684 * response even though power save mode is
2685 * enabled, but some drivers might require
2686 * to disable power save here. This needs
2687 * to be investigated.
2688 */
2689 ieee80211_send_pspoll(local, sdata);
2690 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002691 }
2692 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002693
Johannes Berg488dd7b2012-10-29 20:08:01 +01002694 if (sdata->vif.p2p) {
2695 u8 noa[2];
2696 int ret;
2697
2698 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2699 len - baselen,
2700 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2701 noa, sizeof(noa));
2702 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2703 bss_conf->p2p_oppps = noa[1] & 0x80;
2704 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2705 changed |= BSS_CHANGED_P2P_PS;
2706 sdata->u.mgd.p2p_noa_index = noa[0];
2707 /*
2708 * make sure we update all information, the CRC
2709 * mechanism doesn't look at P2P attributes.
2710 */
2711 ifmgd->beacon_crc_valid = false;
2712 }
2713 }
2714
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002715 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002716 return;
2717 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002718 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002719
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002720 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002721
2722 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2723 elems.wmm_param_len))
2724 changed |= BSS_CHANGED_QOS;
2725
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002726 /*
2727 * If we haven't had a beacon before, tell the driver about the
2728 * DTIM period now.
2729 */
2730 if (!bss_conf->dtim_period) {
2731 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2732 if (elems.tim)
2733 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2734 else
2735 bss_conf->dtim_period = 1;
2736 changed |= BSS_CHANGED_DTIM_PERIOD;
2737 }
2738
Johannes Berg7a5158e2008-10-08 10:59:33 +02002739 if (elems.erp_info && elems.erp_info_len >= 1) {
2740 erp_valid = true;
2741 erp_value = elems.erp_info[0];
2742 } else {
2743 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002744 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002745 changed |= ieee80211_handle_bss_capability(sdata,
2746 le16_to_cpu(mgmt->u.beacon.capab_info),
2747 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002748
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002749
Johannes Berg074d46d2012-03-15 19:45:16 +01002750 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002751 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002752 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2753 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002754
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002755 if (elems.country_elem && elems.pwr_constr_elem &&
2756 mgmt->u.probe_resp.capab_info &
2757 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002758 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2759 elems.country_elem,
2760 elems.country_elem_len,
2761 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002762
Johannes Berg471b3ef2007-12-28 14:32:58 +01002763 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002764}
2765
Johannes Berg1fa57d02010-06-10 10:21:32 +02002766void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2767 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002768{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002769 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002770 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002771 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002772 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002773 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002774 u16 fc;
2775
Jiri Bencf0706e82007-05-05 11:45:53 -07002776 rx_status = (struct ieee80211_rx_status *) skb->cb;
2777 mgmt = (struct ieee80211_mgmt *) skb->data;
2778 fc = le16_to_cpu(mgmt->frame_control);
2779
Johannes Berg77fdaa12009-07-07 03:45:17 +02002780 mutex_lock(&ifmgd->mtx);
2781
Johannes Berg66e67e42012-01-20 13:55:27 +01002782 switch (fc & IEEE80211_FCTL_STYPE) {
2783 case IEEE80211_STYPE_BEACON:
2784 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2785 break;
2786 case IEEE80211_STYPE_PROBE_RESP:
2787 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2788 break;
2789 case IEEE80211_STYPE_AUTH:
2790 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2791 break;
2792 case IEEE80211_STYPE_DEAUTH:
2793 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2794 break;
2795 case IEEE80211_STYPE_DISASSOC:
2796 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2797 break;
2798 case IEEE80211_STYPE_ASSOC_RESP:
2799 case IEEE80211_STYPE_REASSOC_RESP:
2800 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2801 break;
2802 case IEEE80211_STYPE_ACTION:
2803 switch (mgmt->u.action.category) {
2804 case WLAN_CATEGORY_SPECTRUM_MGMT:
2805 ieee80211_sta_process_chanswitch(sdata,
2806 &mgmt->u.action.u.chan_switch.sw_elem,
2807 (void *)ifmgd->associated->priv,
2808 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002809 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002810 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002811 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002812 mutex_unlock(&ifmgd->mtx);
2813
Johannes Berg66e67e42012-01-20 13:55:27 +01002814 switch (rma) {
2815 case RX_MGMT_NONE:
2816 /* no action */
2817 break;
2818 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002819 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002820 break;
2821 case RX_MGMT_CFG80211_DISASSOC:
2822 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2823 break;
2824 case RX_MGMT_CFG80211_RX_AUTH:
2825 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2826 break;
2827 case RX_MGMT_CFG80211_RX_ASSOC:
2828 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2829 break;
2830 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2831 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2832 break;
2833 default:
2834 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002835 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002836}
2837
Johannes Berg9c6bd792008-09-11 00:01:52 +02002838static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002839{
2840 struct ieee80211_sub_if_data *sdata =
2841 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002842 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002843 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002844
Johannes Berg5bb644a2009-05-17 11:40:42 +02002845 if (local->quiescing) {
2846 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2847 return;
2848 }
2849
Johannes Berg64592c82010-06-10 10:21:31 +02002850 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002851}
2852
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002853static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002854 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002855{
2856 struct ieee80211_local *local = sdata->local;
2857 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002858 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002859
Johannes Berg37ad3882012-02-24 13:50:54 +01002860 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2861 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002862 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002863
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002864 /*
2865 * must be outside lock due to cfg80211,
2866 * but that's not a problem.
2867 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002868 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002869
2870 mutex_lock(&local->mtx);
2871 ieee80211_recalc_idle(local);
2872 mutex_unlock(&local->mtx);
2873
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002874 mutex_lock(&ifmgd->mtx);
2875}
2876
Johannes Berg66e67e42012-01-20 13:55:27 +01002877static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2878{
2879 struct ieee80211_local *local = sdata->local;
2880 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2881 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002882 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002883
2884 lockdep_assert_held(&ifmgd->mtx);
2885
2886 if (WARN_ON_ONCE(!auth_data))
2887 return -EINVAL;
2888
Johannes Berg1672c0e32013-01-29 15:02:27 +01002889 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2890 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2891 IEEE80211_TX_INTFL_MLME_CONN_TX;
2892
Johannes Berg66e67e42012-01-20 13:55:27 +01002893 auth_data->tries++;
2894
2895 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002896 sdata_info(sdata, "authentication with %pM timed out\n",
2897 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002898
2899 /*
2900 * Most likely AP is not in the range so remove the
2901 * bss struct for that AP.
2902 */
2903 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2904
2905 return -ETIMEDOUT;
2906 }
2907
Johannes Berga1845fc2012-06-27 13:18:36 +02002908 drv_mgd_prepare_tx(local, sdata);
2909
Johannes Berg66e67e42012-01-20 13:55:27 +01002910 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002911 u16 trans = 1;
2912 u16 status = 0;
2913
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002914 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2915 auth_data->bss->bssid, auth_data->tries,
2916 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002917
2918 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002919
2920 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2921 trans = auth_data->sae_trans;
2922 status = auth_data->sae_status;
2923 auth_data->expected_transaction = trans;
2924 }
2925
2926 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2927 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002928 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002929 auth_data->bss->bssid, NULL, 0, 0,
2930 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002931 } else {
2932 const u8 *ssidie;
2933
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002934 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2935 auth_data->bss->bssid, auth_data->tries,
2936 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002937
Johannes Berg9caf0362012-11-29 01:25:20 +01002938 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002939 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01002940 if (!ssidie) {
2941 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002942 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01002943 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002944 /*
2945 * Direct probe is sent to broadcast address as some APs
2946 * will not answer to direct packet in unassociated state.
2947 */
2948 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01002949 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02002950 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002951 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002952 }
2953
Johannes Berg1672c0e32013-01-29 15:02:27 +01002954 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
2955 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2956 run_again(ifmgd, auth_data->timeout);
2957 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002958
2959 return 0;
2960}
2961
2962static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2963{
2964 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2965 struct ieee80211_local *local = sdata->local;
2966
2967 lockdep_assert_held(&sdata->u.mgd.mtx);
2968
Johannes Berg66e67e42012-01-20 13:55:27 +01002969 assoc_data->tries++;
2970 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002971 sdata_info(sdata, "association with %pM timed out\n",
2972 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002973
2974 /*
2975 * Most likely AP is not in the range so remove the
2976 * bss struct for that AP.
2977 */
2978 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2979
2980 return -ETIMEDOUT;
2981 }
2982
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002983 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2984 assoc_data->bss->bssid, assoc_data->tries,
2985 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002986 ieee80211_send_assoc(sdata);
2987
Johannes Berg1672c0e32013-01-29 15:02:27 +01002988 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
2989 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2990 run_again(&sdata->u.mgd, assoc_data->timeout);
2991 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002992
2993 return 0;
2994}
2995
Johannes Berg1672c0e32013-01-29 15:02:27 +01002996void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
2997 __le16 fc, bool acked)
2998{
2999 struct ieee80211_local *local = sdata->local;
3000
3001 sdata->u.mgd.status_fc = fc;
3002 sdata->u.mgd.status_acked = acked;
3003 sdata->u.mgd.status_received = true;
3004
3005 ieee80211_queue_work(&local->hw, &sdata->work);
3006}
3007
Johannes Berg1fa57d02010-06-10 10:21:32 +02003008void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003009{
Johannes Berg60f8b392008-09-08 17:44:22 +02003010 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003011 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003012
Johannes Berg77fdaa12009-07-07 03:45:17 +02003013 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003014
Johannes Berg1672c0e32013-01-29 15:02:27 +01003015 if (ifmgd->status_received) {
3016 __le16 fc = ifmgd->status_fc;
3017 bool status_acked = ifmgd->status_acked;
3018
3019 ifmgd->status_received = false;
3020 if (ifmgd->auth_data &&
3021 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3022 if (status_acked) {
3023 ifmgd->auth_data->timeout =
3024 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3025 run_again(ifmgd, ifmgd->auth_data->timeout);
3026 } else {
3027 ifmgd->auth_data->timeout = jiffies - 1;
3028 }
3029 } else if (ifmgd->assoc_data &&
3030 (ieee80211_is_assoc_req(fc) ||
3031 ieee80211_is_reassoc_req(fc))) {
3032 if (status_acked) {
3033 ifmgd->assoc_data->timeout =
3034 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3035 run_again(ifmgd, ifmgd->assoc_data->timeout);
3036 } else {
3037 ifmgd->assoc_data->timeout = jiffies - 1;
3038 }
3039 }
3040 }
3041
Johannes Berg66e67e42012-01-20 13:55:27 +01003042 if (ifmgd->auth_data &&
3043 time_after(jiffies, ifmgd->auth_data->timeout)) {
3044 if (ifmgd->auth_data->done) {
3045 /*
3046 * ok ... we waited for assoc but userspace didn't,
3047 * so let's just kill the auth data
3048 */
3049 ieee80211_destroy_auth_data(sdata, false);
3050 } else if (ieee80211_probe_auth(sdata)) {
3051 u8 bssid[ETH_ALEN];
3052
3053 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3054
3055 ieee80211_destroy_auth_data(sdata, false);
3056
3057 mutex_unlock(&ifmgd->mtx);
3058 cfg80211_send_auth_timeout(sdata->dev, bssid);
3059 mutex_lock(&ifmgd->mtx);
3060 }
3061 } else if (ifmgd->auth_data)
3062 run_again(ifmgd, ifmgd->auth_data->timeout);
3063
3064 if (ifmgd->assoc_data &&
3065 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003066 if ((ifmgd->assoc_data->need_beacon &&
3067 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003068 ieee80211_do_assoc(sdata)) {
3069 u8 bssid[ETH_ALEN];
3070
3071 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3072
3073 ieee80211_destroy_assoc_data(sdata, false);
3074
3075 mutex_unlock(&ifmgd->mtx);
3076 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3077 mutex_lock(&ifmgd->mtx);
3078 }
3079 } else if (ifmgd->assoc_data)
3080 run_again(ifmgd, ifmgd->assoc_data->timeout);
3081
Johannes Bergb291ba12009-07-10 15:29:03 +02003082 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3083 IEEE80211_STA_CONNECTION_POLL) &&
3084 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003085 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003086 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003087
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003088 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003089
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003090 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003091 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003092 else
Ben Greear180205b2011-02-04 15:30:24 -08003093 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003094
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003095 /* ACK received for nullfunc probing frame */
3096 if (!ifmgd->probe_send_count)
3097 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003098 else if (ifmgd->nullfunc_failed) {
3099 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003100 mlme_dbg(sdata,
3101 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3102 bssid, ifmgd->probe_send_count,
3103 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003104 ieee80211_mgd_probe_ap_send(sdata);
3105 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003106 mlme_dbg(sdata,
3107 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3108 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003109 ieee80211_sta_connection_lost(sdata, bssid,
3110 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003111 }
3112 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003113 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003114 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003115 mlme_dbg(sdata,
3116 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3117 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003118 ieee80211_sta_connection_lost(sdata, bssid,
3119 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003120 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003121 mlme_dbg(sdata,
3122 "No probe response from AP %pM after %dms, try %d/%i\n",
3123 bssid, probe_wait_ms,
3124 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003125 ieee80211_mgd_probe_ap_send(sdata);
3126 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003127 /*
3128 * We actually lost the connection ... or did we?
3129 * Let's make sure!
3130 */
Ben Greearb38afa82010-10-07 16:12:06 -07003131 wiphy_debug(local->hw.wiphy,
3132 "%s: No probe response from AP %pM"
3133 " after %dms, disconnecting.\n",
3134 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003135 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003136
Johannes Berg95acac62011-07-12 12:30:59 +02003137 ieee80211_sta_connection_lost(sdata, bssid,
3138 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02003139 }
3140 }
3141
Johannes Berg77fdaa12009-07-07 03:45:17 +02003142 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003143
3144 mutex_lock(&local->mtx);
3145 ieee80211_recalc_idle(local);
3146 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003147}
Johannes Berg0a51b272008-09-08 17:44:25 +02003148
Johannes Bergb291ba12009-07-10 15:29:03 +02003149static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3150{
3151 struct ieee80211_sub_if_data *sdata =
3152 (struct ieee80211_sub_if_data *) data;
3153 struct ieee80211_local *local = sdata->local;
3154
3155 if (local->quiescing)
3156 return;
3157
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003158 ieee80211_queue_work(&sdata->local->hw,
3159 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003160}
3161
3162static void ieee80211_sta_conn_mon_timer(unsigned long data)
3163{
3164 struct ieee80211_sub_if_data *sdata =
3165 (struct ieee80211_sub_if_data *) data;
3166 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3167 struct ieee80211_local *local = sdata->local;
3168
3169 if (local->quiescing)
3170 return;
3171
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003172 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003173}
3174
3175static void ieee80211_sta_monitor_work(struct work_struct *work)
3176{
3177 struct ieee80211_sub_if_data *sdata =
3178 container_of(work, struct ieee80211_sub_if_data,
3179 u.mgd.monitor_work);
3180
3181 ieee80211_mgd_probe_ap(sdata, false);
3182}
3183
Johannes Berga1678f82008-09-11 00:01:47 +02003184static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3185{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003186 u32 flags;
3187
Kalle Vaload935682009-04-19 08:47:19 +03003188 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003189 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003190
Johannes Bergb291ba12009-07-10 15:29:03 +02003191 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003192 flags = sdata->local->hw.flags;
3193 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3194 ieee80211_queue_work(&sdata->local->hw,
3195 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003196 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003197 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003198 }
Johannes Berga1678f82008-09-11 00:01:47 +02003199}
3200
Johannes Berg5bb644a2009-05-17 11:40:42 +02003201#ifdef CONFIG_PM
3202void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3203{
3204 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3205
3206 /*
3207 * we need to use atomic bitops for the running bits
3208 * only because both timers might fire at the same
3209 * time -- the code here is properly synchronised.
3210 */
3211
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003212 cancel_work_sync(&ifmgd->request_smps_work);
3213
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003214 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003215 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003216 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003217 if (del_timer_sync(&ifmgd->timer))
3218 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3219
3220 cancel_work_sync(&ifmgd->chswitch_work);
3221 if (del_timer_sync(&ifmgd->chswitch_timer))
3222 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003223
Johannes Bergb291ba12009-07-10 15:29:03 +02003224 /* these will just be re-established on connection */
3225 del_timer_sync(&ifmgd->conn_mon_timer);
3226 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003227}
3228
3229void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3230{
3231 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3232
Johannes Berg782d2672013-01-18 21:21:31 +01003233 mutex_lock(&ifmgd->mtx);
3234 if (!ifmgd->associated) {
3235 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303236 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003237 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303238
Johannes Berg95acac62011-07-12 12:30:59 +02003239 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3240 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003241 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3242 ieee80211_sta_connection_lost(sdata,
3243 ifmgd->associated->bssid,
3244 WLAN_REASON_UNSPECIFIED);
Johannes Berg95acac62011-07-12 12:30:59 +02003245 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003246 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003247 }
Johannes Berg782d2672013-01-18 21:21:31 +01003248 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003249
Johannes Berg5bb644a2009-05-17 11:40:42 +02003250 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3251 add_timer(&ifmgd->timer);
3252 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3253 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003254 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003255
3256 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003257 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003258 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003259}
3260#endif
3261
Johannes Berg9c6bd792008-09-11 00:01:52 +02003262/* interface setup */
3263void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3264{
Johannes Berg46900292009-02-15 12:44:28 +01003265 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003266
Johannes Berg46900292009-02-15 12:44:28 +01003267 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003268 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003269 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003270 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3271 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003272 INIT_WORK(&ifmgd->csa_connection_drop_work,
3273 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003274 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003275 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003276 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003277 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3278 (unsigned long) sdata);
3279 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3280 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003281 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303282 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003283
Johannes Bergab1faea2009-07-01 21:41:17 +02003284 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303285 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003286 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3287 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003288
Johannes Berg77fdaa12009-07-07 03:45:17 +02003289 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003290
3291 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3292 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3293 else
3294 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003295}
3296
3297/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003298void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3299{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003300 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003301
3302 /* Restart STA timers */
3303 rcu_read_lock();
3304 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3305 ieee80211_restart_sta_timer(sdata);
3306 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003307}
Johannes Berg10f644a2009-04-16 13:17:25 +02003308
3309int ieee80211_max_network_latency(struct notifier_block *nb,
3310 unsigned long data, void *dummy)
3311{
3312 s32 latency_usec = (s32) data;
3313 struct ieee80211_local *local =
3314 container_of(nb, struct ieee80211_local,
3315 network_latency_notifier);
3316
3317 mutex_lock(&local->iflist_mtx);
3318 ieee80211_recalc_ps(local, latency_usec);
3319 mutex_unlock(&local->iflist_mtx);
3320
3321 return 0;
3322}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003323
Johannes Bergf2d9d272012-11-22 14:11:39 +01003324static u32 chandef_downgrade(struct cfg80211_chan_def *c)
3325{
3326 u32 ret;
3327 int tmp;
3328
3329 switch (c->width) {
3330 case NL80211_CHAN_WIDTH_20:
3331 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3332 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3333 break;
3334 case NL80211_CHAN_WIDTH_40:
3335 c->width = NL80211_CHAN_WIDTH_20;
3336 c->center_freq1 = c->chan->center_freq;
3337 ret = IEEE80211_STA_DISABLE_40MHZ |
3338 IEEE80211_STA_DISABLE_VHT;
3339 break;
3340 case NL80211_CHAN_WIDTH_80:
3341 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3342 /* n_P40 */
3343 tmp /= 2;
3344 /* freq_P40 */
3345 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3346 c->width = NL80211_CHAN_WIDTH_40;
3347 ret = IEEE80211_STA_DISABLE_VHT;
3348 break;
3349 case NL80211_CHAN_WIDTH_80P80:
3350 c->center_freq2 = 0;
3351 c->width = NL80211_CHAN_WIDTH_80;
3352 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3353 IEEE80211_STA_DISABLE_160MHZ;
3354 break;
3355 case NL80211_CHAN_WIDTH_160:
3356 /* n_P20 */
3357 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3358 /* n_P80 */
3359 tmp /= 4;
3360 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3361 c->width = NL80211_CHAN_WIDTH_80;
3362 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3363 IEEE80211_STA_DISABLE_160MHZ;
3364 break;
3365 default:
3366 case NL80211_CHAN_WIDTH_20_NOHT:
3367 WARN_ON_ONCE(1);
3368 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3369 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3370 break;
3371 }
3372
3373 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3374
3375 return ret;
3376}
3377
3378static u32
3379ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
3380 struct ieee80211_supported_band *sband,
3381 struct ieee80211_channel *channel,
3382 const struct ieee80211_ht_operation *ht_oper,
3383 const struct ieee80211_vht_operation *vht_oper,
3384 struct cfg80211_chan_def *chandef)
3385{
3386 struct cfg80211_chan_def vht_chandef;
3387 u32 ht_cfreq, ret;
3388
3389 chandef->chan = channel;
3390 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3391 chandef->center_freq1 = channel->center_freq;
3392 chandef->center_freq2 = 0;
3393
3394 if (!ht_oper || !sband->ht_cap.ht_supported) {
3395 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3396 goto out;
3397 }
3398
3399 chandef->width = NL80211_CHAN_WIDTH_20;
3400
3401 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3402 channel->band);
3403 /* check that channel matches the right operating channel */
3404 if (channel->center_freq != ht_cfreq) {
3405 /*
3406 * It's possible that some APs are confused here;
3407 * Netgear WNDR3700 sometimes reports 4 higher than
3408 * the actual channel in association responses, but
3409 * since we look at probe response/beacon data here
3410 * it should be OK.
3411 */
3412 sdata_info(sdata,
3413 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3414 channel->center_freq, ht_cfreq,
3415 ht_oper->primary_chan, channel->band);
3416 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3417 goto out;
3418 }
3419
3420 /* check 40 MHz support, if we have it */
3421 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3422 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3423 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3424 chandef->width = NL80211_CHAN_WIDTH_40;
3425 chandef->center_freq1 += 10;
3426 break;
3427 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3428 chandef->width = NL80211_CHAN_WIDTH_40;
3429 chandef->center_freq1 -= 10;
3430 break;
3431 }
3432 } else {
3433 /* 40 MHz (and 80 MHz) must be supported for VHT */
3434 ret = IEEE80211_STA_DISABLE_VHT;
3435 goto out;
3436 }
3437
3438 if (!vht_oper || !sband->vht_cap.vht_supported) {
3439 ret = IEEE80211_STA_DISABLE_VHT;
3440 goto out;
3441 }
3442
3443 vht_chandef.chan = channel;
3444 vht_chandef.center_freq1 =
3445 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
3446 channel->band);
3447 vht_chandef.center_freq2 = 0;
3448
3449 if (vht_oper->center_freq_seg2_idx)
3450 vht_chandef.center_freq2 =
3451 ieee80211_channel_to_frequency(
3452 vht_oper->center_freq_seg2_idx,
3453 channel->band);
3454
3455 switch (vht_oper->chan_width) {
3456 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3457 vht_chandef.width = chandef->width;
3458 break;
3459 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3460 vht_chandef.width = NL80211_CHAN_WIDTH_80;
3461 break;
3462 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3463 vht_chandef.width = NL80211_CHAN_WIDTH_160;
3464 break;
3465 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3466 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
3467 break;
3468 default:
3469 sdata_info(sdata,
3470 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
3471 vht_oper->chan_width);
3472 ret = IEEE80211_STA_DISABLE_VHT;
3473 goto out;
3474 }
3475
3476 if (!cfg80211_chandef_valid(&vht_chandef)) {
3477 sdata_info(sdata,
3478 "AP VHT information is invalid, disable VHT\n");
3479 ret = IEEE80211_STA_DISABLE_VHT;
3480 goto out;
3481 }
3482
3483 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
3484 ret = 0;
3485 goto out;
3486 }
3487
3488 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
3489 sdata_info(sdata,
3490 "AP VHT information doesn't match HT, disable VHT\n");
3491 ret = IEEE80211_STA_DISABLE_VHT;
3492 goto out;
3493 }
3494
3495 *chandef = vht_chandef;
3496
3497 ret = 0;
3498
3499 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
3500 IEEE80211_CHAN_DISABLED)) {
3501 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
3502 ret = IEEE80211_STA_DISABLE_HT |
3503 IEEE80211_STA_DISABLE_VHT;
3504 goto out;
3505 }
3506
3507 ret = chandef_downgrade(chandef);
3508 }
3509
3510 if (chandef->width != vht_chandef.width)
3511 sdata_info(sdata,
3512 "local regulatory prevented using AP HT/VHT configuration, downgraded\n");
3513
3514out:
3515 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
3516 return ret;
3517}
3518
3519static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3520 struct cfg80211_bss *cbss)
3521{
3522 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3523 const u8 *ht_cap_ie, *vht_cap_ie;
3524 const struct ieee80211_ht_cap *ht_cap;
3525 const struct ieee80211_vht_cap *vht_cap;
3526 u8 chains = 1;
3527
3528 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3529 return chains;
3530
Johannes Berg9caf0362012-11-29 01:25:20 +01003531 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003532 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3533 ht_cap = (void *)(ht_cap_ie + 2);
3534 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3535 /*
3536 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3537 * "Tx Unequal Modulation Supported" fields.
3538 */
3539 }
3540
3541 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3542 return chains;
3543
Johannes Berg9caf0362012-11-29 01:25:20 +01003544 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003545 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3546 u8 nss;
3547 u16 tx_mcs_map;
3548
3549 vht_cap = (void *)(vht_cap_ie + 2);
3550 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3551 for (nss = 8; nss > 0; nss--) {
3552 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3553 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3554 break;
3555 }
3556 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3557 chains = max(chains, nss);
3558 }
3559
3560 return chains;
3561}
3562
Johannes Bergb17166a2012-07-27 11:41:27 +02003563static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3564 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003565{
3566 struct ieee80211_local *local = sdata->local;
3567 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003568 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003569 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003570 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003571 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003572 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003573
Johannes Berg24398e32012-03-28 10:58:36 +02003574 sband = local->hw.wiphy->bands[cbss->channel->band];
3575
Johannes Bergf2d9d272012-11-22 14:11:39 +01003576 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3577 IEEE80211_STA_DISABLE_80P80MHZ |
3578 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003579
Johannes Berg9caf0362012-11-29 01:25:20 +01003580 rcu_read_lock();
3581
Johannes Bergf2d9d272012-11-22 14:11:39 +01003582 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3583 sband->ht_cap.ht_supported) {
3584 const u8 *ht_oper_ie;
3585
Johannes Berg9caf0362012-11-29 01:25:20 +01003586 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003587 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3588 ht_oper = (void *)(ht_oper_ie + 2);
3589 }
3590
Johannes Bergf2d9d272012-11-22 14:11:39 +01003591 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3592 sband->vht_cap.vht_supported) {
3593 const u8 *vht_oper_ie;
3594
Johannes Berg9caf0362012-11-29 01:25:20 +01003595 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3596 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003597 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3598 vht_oper = (void *)(vht_oper_ie + 2);
3599 if (vht_oper && !ht_oper) {
3600 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003601 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003602 "AP advertised VHT without HT, disabling both\n");
3603 sdata->flags |= IEEE80211_STA_DISABLE_HT;
3604 sdata->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003605 }
3606 }
3607
Johannes Bergf2d9d272012-11-22 14:11:39 +01003608 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3609 cbss->channel,
3610 ht_oper, vht_oper,
3611 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003612
Johannes Bergf2d9d272012-11-22 14:11:39 +01003613 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3614 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003615
Johannes Berg9caf0362012-11-29 01:25:20 +01003616 rcu_read_unlock();
3617
Johannes Berg04ecd252012-09-11 14:34:12 +02003618 /* will change later if needed */
3619 sdata->smps_mode = IEEE80211_SMPS_OFF;
3620
Johannes Bergf2d9d272012-11-22 14:11:39 +01003621 /*
3622 * If this fails (possibly due to channel context sharing
3623 * on incompatible channels, e.g. 80+80 and 160 sharing the
3624 * same control channel) try to use a smaller bandwidth.
3625 */
3626 ret = ieee80211_vif_use_channel(sdata, &chandef,
3627 IEEE80211_CHANCTX_SHARED);
3628 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
3629 ifmgd->flags |= chandef_downgrade(&chandef);
3630 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003631}
3632
3633static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3634 struct cfg80211_bss *cbss, bool assoc)
3635{
3636 struct ieee80211_local *local = sdata->local;
3637 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3638 struct ieee80211_bss *bss = (void *)cbss->priv;
3639 struct sta_info *new_sta = NULL;
3640 bool have_sta = false;
3641 int err;
3642
3643 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3644 return -EINVAL;
3645
3646 if (assoc) {
3647 rcu_read_lock();
3648 have_sta = sta_info_get(sdata, cbss->bssid);
3649 rcu_read_unlock();
3650 }
3651
3652 if (!have_sta) {
3653 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3654 if (!new_sta)
3655 return -ENOMEM;
3656 }
3657
3658 mutex_lock(&local->mtx);
3659 ieee80211_recalc_idle(sdata->local);
3660 mutex_unlock(&local->mtx);
3661
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003662 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003663 u32 rates = 0, basic_rates = 0;
3664 bool have_higher_than_11mbit;
3665 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003666 struct ieee80211_supported_band *sband;
3667
3668 sband = local->hw.wiphy->bands[cbss->channel->band];
3669
3670 err = ieee80211_prep_channel(sdata, cbss);
3671 if (err) {
3672 sta_info_free(local, new_sta);
3673 return err;
3674 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003675
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003676 ieee80211_get_rates(sband, bss->supp_rates,
3677 bss->supp_rates_len,
3678 &rates, &basic_rates,
3679 &have_higher_than_11mbit,
3680 &min_rate, &min_rate_index);
3681
3682 /*
3683 * This used to be a workaround for basic rates missing
3684 * in the association response frame. Now that we no
3685 * longer use the basic rates from there, it probably
3686 * doesn't happen any more, but keep the workaround so
3687 * in case some *other* APs are buggy in different ways
3688 * we can connect -- with a warning.
3689 */
3690 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003691 sdata_info(sdata,
3692 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003693 basic_rates = BIT(min_rate_index);
3694 }
3695
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003696 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003697 sdata->vif.bss_conf.basic_rates = basic_rates;
3698
3699 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003700 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003701 have_higher_than_11mbit)
3702 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3703 else
3704 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3705
3706 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3707
Johannes Berg8c358bc2012-05-22 22:13:05 +02003708 /* set timing information */
3709 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3710 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3711 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3712
3713 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003714 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003715 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3716 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003717
3718 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003719 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003720
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003721 err = sta_info_insert(new_sta);
3722 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003723 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003724 sdata_info(sdata,
3725 "failed to insert STA entry for the AP (error %d)\n",
3726 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003727 return err;
3728 }
3729 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003730 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003731
3732 return 0;
3733}
3734
Johannes Berg77fdaa12009-07-07 03:45:17 +02003735/* config hooks */
3736int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3737 struct cfg80211_auth_request *req)
3738{
Johannes Berg66e67e42012-01-20 13:55:27 +01003739 struct ieee80211_local *local = sdata->local;
3740 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3741 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003742 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003743 int err;
3744
3745 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003746
3747 switch (req->auth_type) {
3748 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3749 auth_alg = WLAN_AUTH_OPEN;
3750 break;
3751 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003752 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003753 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003754 auth_alg = WLAN_AUTH_SHARED_KEY;
3755 break;
3756 case NL80211_AUTHTYPE_FT:
3757 auth_alg = WLAN_AUTH_FT;
3758 break;
3759 case NL80211_AUTHTYPE_NETWORK_EAP:
3760 auth_alg = WLAN_AUTH_LEAP;
3761 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003762 case NL80211_AUTHTYPE_SAE:
3763 auth_alg = WLAN_AUTH_SAE;
3764 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003765 default:
3766 return -EOPNOTSUPP;
3767 }
3768
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003769 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3770 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003771 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003772 return -ENOMEM;
3773
Johannes Berg66e67e42012-01-20 13:55:27 +01003774 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003775
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003776 if (req->sae_data_len >= 4) {
3777 __le16 *pos = (__le16 *) req->sae_data;
3778 auth_data->sae_trans = le16_to_cpu(pos[0]);
3779 auth_data->sae_status = le16_to_cpu(pos[1]);
3780 memcpy(auth_data->data, req->sae_data + 4,
3781 req->sae_data_len - 4);
3782 auth_data->data_len += req->sae_data_len - 4;
3783 }
3784
Johannes Berg77fdaa12009-07-07 03:45:17 +02003785 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003786 memcpy(&auth_data->data[auth_data->data_len],
3787 req->ie, req->ie_len);
3788 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003789 }
3790
Johannes Bergfffd0932009-07-08 14:22:54 +02003791 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003792 auth_data->key_len = req->key_len;
3793 auth_data->key_idx = req->key_idx;
3794 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003795 }
3796
Johannes Berg66e67e42012-01-20 13:55:27 +01003797 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003798
Johannes Berg66e67e42012-01-20 13:55:27 +01003799 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003800
Johannes Berg66e67e42012-01-20 13:55:27 +01003801 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003802
Johannes Berg66e67e42012-01-20 13:55:27 +01003803 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3804 ifmgd->assoc_data) {
3805 err = -EBUSY;
3806 goto err_free;
3807 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003808
Johannes Berg66e67e42012-01-20 13:55:27 +01003809 if (ifmgd->auth_data)
3810 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003811
Johannes Berg66e67e42012-01-20 13:55:27 +01003812 /* prep auth_data so we don't go into idle on disassoc */
3813 ifmgd->auth_data = auth_data;
3814
3815 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003816 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003817
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003818 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003819
Johannes Berga1cf7752012-03-08 15:02:07 +01003820 err = ieee80211_prep_connection(sdata, req->bss, false);
3821 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003822 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003823
Johannes Berg66e67e42012-01-20 13:55:27 +01003824 err = ieee80211_probe_auth(sdata);
3825 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003826 sta_info_destroy_addr(sdata, req->bss->bssid);
3827 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003828 }
3829
Johannes Berg66e67e42012-01-20 13:55:27 +01003830 /* hold our own reference */
3831 cfg80211_ref_bss(auth_data->bss);
3832 err = 0;
3833 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003834
Johannes Berg66e67e42012-01-20 13:55:27 +01003835 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003836 memset(ifmgd->bssid, 0, ETH_ALEN);
3837 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003838 ifmgd->auth_data = NULL;
3839 err_free:
3840 kfree(auth_data);
3841 out_unlock:
3842 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003843
Johannes Berg66e67e42012-01-20 13:55:27 +01003844 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003845}
3846
Johannes Berg77fdaa12009-07-07 03:45:17 +02003847int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3848 struct cfg80211_assoc_request *req)
3849{
Johannes Berg66e67e42012-01-20 13:55:27 +01003850 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003851 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003852 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003853 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003854 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003855 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003856 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003857 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003858
Johannes Berg66e67e42012-01-20 13:55:27 +01003859 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3860 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003861 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003862
Johannes Berg9caf0362012-11-29 01:25:20 +01003863 rcu_read_lock();
3864 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3865 if (!ssidie) {
3866 rcu_read_unlock();
3867 kfree(assoc_data);
3868 return -EINVAL;
3869 }
3870 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3871 assoc_data->ssid_len = ssidie[1];
3872 rcu_read_unlock();
3873
Johannes Berg66e67e42012-01-20 13:55:27 +01003874 mutex_lock(&ifmgd->mtx);
3875
3876 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003877 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003878
3879 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3880 err = -EBUSY;
3881 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003882 }
3883
Johannes Berg66e67e42012-01-20 13:55:27 +01003884 if (ifmgd->assoc_data) {
3885 err = -EBUSY;
3886 goto err_free;
3887 }
3888
3889 if (ifmgd->auth_data) {
3890 bool match;
3891
3892 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003893 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003894 ieee80211_destroy_auth_data(sdata, match);
3895 }
3896
3897 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003898
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003899 ifmgd->beacon_crc_valid = false;
3900
Johannes Bergde5036a2012-03-08 15:02:03 +01003901 /*
3902 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3903 * We still associate in non-HT mode (11a/b/g) if any one of these
3904 * ciphers is configured as pairwise.
3905 * We can set this to true for non-11n hardware, that'll be checked
3906 * separately along with the peer capabilities.
3907 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003908 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003909 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3910 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003911 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003912 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003913 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003914 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003915 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003916 }
3917 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003918
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003919 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003920 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003921 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3922 }
Ben Greearef96a8422011-11-18 11:32:00 -08003923
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003924 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3925 sband = local->hw.wiphy->bands[req->bss->channel->band];
3926 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003927 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003928 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003929 if (!bss->wmm_used)
3930 netdev_info(sdata->dev,
3931 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003932 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003933
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003934 /* disable VHT if we don't support it or the AP doesn't use WMM */
3935 if (!sband->vht_cap.vht_supported ||
3936 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3937 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003938 if (!bss->wmm_used)
3939 netdev_info(sdata->dev,
3940 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003941 }
3942
Ben Greearef96a8422011-11-18 11:32:00 -08003943 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3944 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3945 sizeof(ifmgd->ht_capa_mask));
3946
Johannes Berg77fdaa12009-07-07 03:45:17 +02003947 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003948 memcpy(assoc_data->ie, req->ie, req->ie_len);
3949 assoc_data->ie_len = req->ie_len;
3950 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003951
Johannes Berg66e67e42012-01-20 13:55:27 +01003952 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003953
Johannes Bergaf6b6372009-12-23 13:15:35 +01003954 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3955 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003956 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003957 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003958 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003959 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003960 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003961
Johannes Berg66e67e42012-01-20 13:55:27 +01003962 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003963 assoc_data->wmm = bss->wmm_used &&
3964 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003965 assoc_data->supp_rates = bss->supp_rates;
3966 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003967
Johannes Berg9caf0362012-11-29 01:25:20 +01003968 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02003969 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3970 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3971 assoc_data->ap_ht_param =
3972 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3973 else
Johannes Berga8243b72012-11-22 14:32:09 +01003974 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003975 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
3976 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
3977 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
3978 sizeof(struct ieee80211_vht_cap));
3979 else
3980 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01003981 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01003982
Kalle Valoab133152010-01-12 10:42:31 +02003983 if (bss->wmm_used && bss->uapsd_supported &&
3984 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003985 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003986 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3987 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003988 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003989 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3990 }
3991
Johannes Berg77fdaa12009-07-07 03:45:17 +02003992 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003993 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003994
3995 if (req->use_mfp) {
3996 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3997 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3998 } else {
3999 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4000 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4001 }
4002
4003 if (req->crypto.control_port)
4004 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4005 else
4006 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4007
Johannes Berga621fa42010-08-27 14:26:54 +03004008 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4009 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4010
Johannes Berg66e67e42012-01-20 13:55:27 +01004011 /* kick off associate process */
4012
4013 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004014 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004015
Johannes Berga1cf7752012-03-08 15:02:07 +01004016 err = ieee80211_prep_connection(sdata, req->bss, true);
4017 if (err)
4018 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004019
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004020 rcu_read_lock();
4021 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004022
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004023 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4024 !beacon_ies) {
4025 /*
4026 * Wait up to one beacon interval ...
4027 * should this be more if we miss one?
4028 */
4029 sdata_info(sdata, "waiting for beacon from %pM\n",
4030 ifmgd->bssid);
4031 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
4032 assoc_data->need_beacon = true;
4033 } else if (beacon_ies) {
4034 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4035 beacon_ies->data,
4036 beacon_ies->len);
4037 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4038 const struct ieee80211_tim_ie *tim;
4039 tim = (void *)(tim_ie + 2);
4040 ifmgd->dtim_period = tim->dtim_period;
Johannes Berg826262c2012-12-10 16:38:14 +02004041 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004042 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004043 assoc_data->timeout = jiffies;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004044 } else {
4045 assoc_data->timeout = jiffies;
Johannes Berg66e67e42012-01-20 13:55:27 +01004046 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004047 rcu_read_unlock();
4048
Johannes Berg66e67e42012-01-20 13:55:27 +01004049 run_again(ifmgd, assoc_data->timeout);
4050
Paul Stewartfcff4f12012-02-23 17:59:53 -08004051 if (bss->corrupt_data) {
4052 char *corrupt_type = "data";
4053 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4054 if (bss->corrupt_data &
4055 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4056 corrupt_type = "beacon and probe response";
4057 else
4058 corrupt_type = "beacon";
4059 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4060 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004061 sdata_info(sdata, "associating with AP with corrupt %s\n",
4062 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004063 }
4064
Johannes Berg66e67e42012-01-20 13:55:27 +01004065 err = 0;
4066 goto out;
4067 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004068 memset(ifmgd->bssid, 0, ETH_ALEN);
4069 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004070 ifmgd->assoc_data = NULL;
4071 err_free:
4072 kfree(assoc_data);
4073 out:
4074 mutex_unlock(&ifmgd->mtx);
4075
4076 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004077}
4078
4079int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004080 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004081{
4082 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004083 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004084 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004085 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004086
Johannes Berg77fdaa12009-07-07 03:45:17 +02004087 mutex_lock(&ifmgd->mtx);
4088
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004089 sdata_info(sdata,
4090 "deauthenticating from %pM by local choice (reason=%d)\n",
4091 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004092
Johannes Berg86552012012-10-29 09:46:31 +01004093 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004094 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004095 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4096 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004097 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004098 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004099 ieee80211_destroy_auth_data(sdata, false);
4100 mutex_unlock(&ifmgd->mtx);
4101
4102 sent_frame = tx;
4103 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004104 }
4105
Johannes Berg86552012012-10-29 09:46:31 +01004106 if (ifmgd->associated &&
4107 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4108 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4109 req->reason_code, tx, frame_buf);
4110 sent_frame = tx;
4111 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004112 mutex_unlock(&ifmgd->mtx);
4113
Johannes Berg86552012012-10-29 09:46:31 +01004114 out:
Johannes Berg7da7cc12010-08-05 17:02:38 +02004115 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004116 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004117 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004118
Johannes Berg86552012012-10-29 09:46:31 +01004119 if (sent_frame)
4120 __cfg80211_send_deauth(sdata->dev, frame_buf,
4121 IEEE80211_DEAUTH_FRAME_LEN);
4122
Johannes Berg77fdaa12009-07-07 03:45:17 +02004123 return 0;
4124}
4125
4126int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004127 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004128{
4129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004130 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004131 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004132
Johannes Berg77fdaa12009-07-07 03:45:17 +02004133 mutex_lock(&ifmgd->mtx);
4134
Johannes Berg8d8b2612009-07-25 11:58:36 +02004135 /*
4136 * cfg80211 should catch this ... but it's racy since
4137 * we can receive a disassoc frame, process it, hand it
4138 * to cfg80211 while that's in a locked section already
4139 * trying to tell us that the user wants to disconnect.
4140 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004141 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004142 mutex_unlock(&ifmgd->mtx);
4143 return -ENOLINK;
4144 }
4145
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004146 sdata_info(sdata,
4147 "disassociating from %pM by local choice (reason=%d)\n",
4148 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004149
Jouni Malinene69e95d2010-03-29 23:29:31 -07004150 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004151 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4152 req->reason_code, !req->local_state_change,
4153 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004154 mutex_unlock(&ifmgd->mtx);
4155
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004156 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4157 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004158
Johannes Berg7da7cc12010-08-05 17:02:38 +02004159 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004160 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004161 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004162
Johannes Berg77fdaa12009-07-07 03:45:17 +02004163 return 0;
4164}
Jouni Malinen026331c2010-02-15 12:53:10 +02004165
Eliad Pellerafa762f2012-04-23 14:45:15 +03004166void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004167{
4168 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4169
4170 mutex_lock(&ifmgd->mtx);
4171 if (ifmgd->assoc_data)
4172 ieee80211_destroy_assoc_data(sdata, false);
4173 if (ifmgd->auth_data)
4174 ieee80211_destroy_auth_data(sdata, false);
4175 del_timer_sync(&ifmgd->timer);
4176 mutex_unlock(&ifmgd->mtx);
4177}
4178
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004179void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4180 enum nl80211_cqm_rssi_threshold_event rssi_event,
4181 gfp_t gfp)
4182{
4183 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4184
Johannes Bergb5878a22010-04-07 16:48:40 +02004185 trace_api_cqm_rssi_notify(sdata, rssi_event);
4186
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004187 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4188}
4189EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);