blob: 2bdf7769506fb166a9b231e0b1b441ec066b5f2b [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 Berg60f8b39c2008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg66e67e42012-01-20 13:55:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_MAX_TRIES 3
35#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37#define IEEE80211_ASSOC_MAX_TRIES 3
38
Ben Greear180205b2011-02-04 15:30:24 -080039static int max_nullfunc_tries = 2;
40module_param(max_nullfunc_tries, int, 0644);
41MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44static int max_probe_tries = 5;
45module_param(max_probe_tries, int, 0644);
46MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020048
49/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010050 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020056 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010057#define IEEE80211_BEACON_LOSS_COUNT 7
58
Johannes Bergb291ba12009-07-10 15:29:03 +020059/*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030063#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
Ben Greear180205b2011-02-04 15:30:24 -080069static int probe_wait_ms = 500;
70module_param(probe_wait_ms, int, 0644);
71MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070074
Jouni Malinen17e4ec12010-03-29 23:28:30 -070075/*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80#define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
Jouni Malinen391a2002010-08-27 22:22:00 +030082/*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
Johannes Berg5bb644a2009-05-17 11:40:42 +020088#define TMR_RUNNING_TIMER 0
89#define TMR_RUNNING_CHANSW 1
90
Johannes Berg77fdaa12009-07-07 03:45:17 +020091/*
92 * All cfg80211 functions have to be called outside a locked
93 * section so that they can acquire a lock themselves... This
94 * is much simpler than queuing up things in cfg80211, but we
95 * do need some indirection for that here.
96 */
97enum rx_mgmt_action {
98 /* no action required */
99 RX_MGMT_NONE,
100
Johannes Berg77fdaa12009-07-07 03:45:17 +0200101 /* caller must call cfg80211_send_deauth() */
102 RX_MGMT_CFG80211_DEAUTH,
103
104 /* caller must call cfg80211_send_disassoc() */
105 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100106
107 /* caller must call cfg80211_send_rx_auth() */
108 RX_MGMT_CFG80211_RX_AUTH,
109
110 /* caller must call cfg80211_send_rx_assoc() */
111 RX_MGMT_CFG80211_RX_ASSOC,
112
113 /* caller must call cfg80211_send_assoc_timeout() */
114 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200115};
116
Johannes Berg5484e232008-09-08 17:44:27 +0200117/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200118static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
119{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200120 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200121}
122
Johannes Bergca386f32009-07-10 02:39:48 +0200123/*
124 * We can have multiple work items (and connection probing)
125 * scheduling this timer, but we need to take care to only
126 * reschedule it when it should fire _earlier_ than it was
127 * asked for before, or if it's not pending right now. This
128 * function ensures that. Note that it then is required to
129 * run this function for all timeouts after the first one
130 * has happened -- the work that runs from this timer will
131 * do that.
132 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100133static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200134{
135 ASSERT_MGD_MTX(ifmgd);
136
137 if (!timer_pending(&ifmgd->timer) ||
138 time_before(timeout, ifmgd->timer.expires))
139 mod_timer(&ifmgd->timer, timeout);
140}
141
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400142void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200143{
Johannes Bergc1288b12012-01-19 09:29:57 +0100144 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145 return;
146
Johannes Berg7eeff742012-07-18 10:27:27 +0200147 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
148 return;
149
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100151 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200152}
153
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400154void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
155{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100158 if (unlikely(!sdata->u.mgd.associated))
159 return;
160
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400161 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
162 return;
163
164 mod_timer(&sdata->u.mgd.conn_mon_timer,
165 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400166
167 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400168}
169
Johannes Berg5484e232008-09-08 17:44:27 +0200170static int ecw2cw(int ecw)
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200171{
Johannes Berg5484e232008-09-08 17:44:27 +0200172 return (1 << ecw) - 1;
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200173}
174
Johannes Berg24398e32012-03-28 10:58:36 +0200175static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
176 struct ieee80211_ht_operation *ht_oper,
177 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200178{
179 struct ieee80211_local *local = sdata->local;
180 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200181 struct ieee80211_chanctx_conf *chanctx_conf;
182 struct ieee80211_channel *chan;
Johannes Bergd5522e02009-03-30 13:23:35 +0200183 struct sta_info *sta;
184 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200185 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200186 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200187
Johannes Berg55de9082012-07-26 17:24:39 +0200188 rcu_read_lock();
189 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
190 if (WARN_ON(!chanctx_conf)) {
191 rcu_read_unlock();
192 return 0;
193 }
194 chan = chanctx_conf->channel;
195 rcu_read_unlock();
196 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200197
Johannes Berg64f68e52012-03-28 10:58:37 +0200198 switch (sdata->vif.bss_conf.channel_type) {
199 case NL80211_CHAN_HT40PLUS:
Johannes Berg55de9082012-07-26 17:24:39 +0200200 if (chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200201 disable_40 = true;
202 break;
203 case NL80211_CHAN_HT40MINUS:
Johannes Berg55de9082012-07-26 17:24:39 +0200204 if (chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200205 disable_40 = true;
206 break;
207 default:
208 break;
209 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200210
Johannes Berg24398e32012-03-28 10:58:36 +0200211 /* This can change during the lifetime of the BSS */
212 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200213 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200214
Johannes Berg64f68e52012-03-28 10:58:37 +0200215 mutex_lock(&local->sta_mtx);
216 sta = sta_info_get(sdata, bssid);
217
218 WARN_ON_ONCE(!sta);
219
220 if (sta && !sta->supports_40mhz)
221 disable_40 = true;
222
223 if (sta && (!reconfig ||
Rajkumar Manoharan91a00992012-04-25 20:24:24 +0530224 (disable_40 != !(sta->sta.ht_cap.cap &
Johannes Berg64f68e52012-03-28 10:58:37 +0200225 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530226
Johannes Berg64f68e52012-03-28 10:58:37 +0200227 if (disable_40)
228 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
229 else
230 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530231
Johannes Berg64f68e52012-03-28 10:58:37 +0200232 rate_control_rate_update(local, sband, sta,
Johannes Berg8f727ef2012-03-30 08:43:32 +0200233 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200234 }
Johannes Berg64f68e52012-03-28 10:58:37 +0200235 mutex_unlock(&local->sta_mtx);
Johannes Bergd5522e02009-03-30 13:23:35 +0200236
Johannes Berg074d46d2012-03-15 19:45:16 +0100237 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200238
239 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200240 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200241 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200242 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200243 }
244
245 return changed;
246}
247
Johannes Berg9c6bd792008-09-11 00:01:52 +0200248/* frame sending functions */
249
Johannes Berg66e67e42012-01-20 13:55:27 +0100250static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
251 struct ieee80211_supported_band *sband,
252 u32 *rates)
253{
254 int i, j, count;
255 *rates = 0;
256 count = 0;
257 for (i = 0; i < supp_rates_len; i++) {
258 int rate = (supp_rates[i] & 0x7F) * 5;
259
260 for (j = 0; j < sband->n_bitrates; j++)
261 if (sband->bitrates[j].bitrate == rate) {
262 *rates |= BIT(j);
263 count++;
264 break;
265 }
266 }
267
268 return count;
269}
270
271static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200272 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100273 struct ieee80211_supported_band *sband,
274 struct ieee80211_channel *channel,
275 enum ieee80211_smps_mode smps)
276{
Johannes Berg66e67e42012-01-20 13:55:27 +0100277 u8 *pos;
278 u32 flags = channel->flags;
279 u16 cap;
280 struct ieee80211_sta_ht_cap ht_cap;
281
282 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
283
Johannes Berg66e67e42012-01-20 13:55:27 +0100284 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
285 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
286
Johannes Berg66e67e42012-01-20 13:55:27 +0100287 /* determine capability flags */
288 cap = ht_cap.cap;
289
Johannes Berg9dde6422012-05-16 23:43:19 +0200290 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100291 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
292 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
293 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
294 cap &= ~IEEE80211_HT_CAP_SGI_40;
295 }
296 break;
297 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
298 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
299 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
300 cap &= ~IEEE80211_HT_CAP_SGI_40;
301 }
302 break;
303 }
304
Johannes Berg24398e32012-03-28 10:58:36 +0200305 /*
306 * If 40 MHz was disabled associate as though we weren't
307 * capable of 40 MHz -- some broken APs will never fall
308 * back to trying to transmit in 20 MHz.
309 */
310 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
311 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
312 cap &= ~IEEE80211_HT_CAP_SGI_40;
313 }
314
Johannes Berg66e67e42012-01-20 13:55:27 +0100315 /* set SM PS mode properly */
316 cap &= ~IEEE80211_HT_CAP_SM_PS;
317 switch (smps) {
318 case IEEE80211_SMPS_AUTOMATIC:
319 case IEEE80211_SMPS_NUM_MODES:
320 WARN_ON(1);
321 case IEEE80211_SMPS_OFF:
322 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
323 IEEE80211_HT_CAP_SM_PS_SHIFT;
324 break;
325 case IEEE80211_SMPS_STATIC:
326 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
327 IEEE80211_HT_CAP_SM_PS_SHIFT;
328 break;
329 case IEEE80211_SMPS_DYNAMIC:
330 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
331 IEEE80211_HT_CAP_SM_PS_SHIFT;
332 break;
333 }
334
335 /* reserve and fill IE */
336 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
337 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
338}
339
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000340static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
341 struct sk_buff *skb,
342 struct ieee80211_supported_band *sband)
343{
344 u8 *pos;
345 u32 cap;
346 struct ieee80211_sta_vht_cap vht_cap;
347
348 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
349
350 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
351
352 /* determine capability flags */
353 cap = vht_cap.cap;
354
355 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000356 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000357 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
358}
359
Johannes Berg66e67e42012-01-20 13:55:27 +0100360static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
361{
362 struct ieee80211_local *local = sdata->local;
363 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
364 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
365 struct sk_buff *skb;
366 struct ieee80211_mgmt *mgmt;
367 u8 *pos, qos_info;
368 size_t offset = 0, noffset;
369 int i, count, rates_len, supp_rates_len;
370 u16 capab;
371 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200372 struct ieee80211_chanctx_conf *chanctx_conf;
373 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100374 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100375
376 lockdep_assert_held(&ifmgd->mtx);
377
Johannes Berg55de9082012-07-26 17:24:39 +0200378 rcu_read_lock();
379 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
380 if (WARN_ON(!chanctx_conf)) {
381 rcu_read_unlock();
382 return;
383 }
384 chan = chanctx_conf->channel;
385 rcu_read_unlock();
386 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100387
388 if (assoc_data->supp_rates_len) {
389 /*
390 * Get all rates supported by the device and the AP as
391 * some APs don't like getting a superset of their rates
392 * in the association request (e.g. D-Link DAP 1353 in
393 * b-only mode)...
394 */
395 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
396 assoc_data->supp_rates_len,
397 sband, &rates);
398 } else {
399 /*
400 * In case AP not provide any supported rates information
401 * before association, we send information element(s) with
402 * all rates that we support.
403 */
404 rates = ~0;
405 rates_len = sband->n_bitrates;
406 }
407
408 skb = alloc_skb(local->hw.extra_tx_headroom +
409 sizeof(*mgmt) + /* bit too much but doesn't matter */
410 2 + assoc_data->ssid_len + /* SSID */
411 4 + rates_len + /* (extended) rates */
412 4 + /* power capability */
413 2 + 2 * sband->n_channels + /* supported channels */
414 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000415 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100416 assoc_data->ie_len + /* extra IEs */
417 9, /* WMM */
418 GFP_KERNEL);
419 if (!skb)
420 return;
421
422 skb_reserve(skb, local->hw.extra_tx_headroom);
423
424 capab = WLAN_CAPABILITY_ESS;
425
426 if (sband->band == IEEE80211_BAND_2GHZ) {
427 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
428 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
429 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
430 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
431 }
432
433 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
434 capab |= WLAN_CAPABILITY_PRIVACY;
435
436 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
437 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
438 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
439
440 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
441 memset(mgmt, 0, 24);
442 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
443 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
444 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
445
446 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
447 skb_put(skb, 10);
448 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
449 IEEE80211_STYPE_REASSOC_REQ);
450 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
451 mgmt->u.reassoc_req.listen_interval =
452 cpu_to_le16(local->hw.conf.listen_interval);
453 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
454 ETH_ALEN);
455 } else {
456 skb_put(skb, 4);
457 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
458 IEEE80211_STYPE_ASSOC_REQ);
459 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
460 mgmt->u.assoc_req.listen_interval =
461 cpu_to_le16(local->hw.conf.listen_interval);
462 }
463
464 /* SSID */
465 pos = skb_put(skb, 2 + assoc_data->ssid_len);
466 *pos++ = WLAN_EID_SSID;
467 *pos++ = assoc_data->ssid_len;
468 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
469
470 /* add all rates which were marked to be used above */
471 supp_rates_len = rates_len;
472 if (supp_rates_len > 8)
473 supp_rates_len = 8;
474
475 pos = skb_put(skb, supp_rates_len + 2);
476 *pos++ = WLAN_EID_SUPP_RATES;
477 *pos++ = supp_rates_len;
478
479 count = 0;
480 for (i = 0; i < sband->n_bitrates; i++) {
481 if (BIT(i) & rates) {
482 int rate = sband->bitrates[i].bitrate;
483 *pos++ = (u8) (rate / 5);
484 if (++count == 8)
485 break;
486 }
487 }
488
489 if (rates_len > count) {
490 pos = skb_put(skb, rates_len - count + 2);
491 *pos++ = WLAN_EID_EXT_SUPP_RATES;
492 *pos++ = rates_len - count;
493
494 for (i++; i < sband->n_bitrates; i++) {
495 if (BIT(i) & rates) {
496 int rate = sband->bitrates[i].bitrate;
497 *pos++ = (u8) (rate / 5);
498 }
499 }
500 }
501
502 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
503 /* 1. power capabilities */
504 pos = skb_put(skb, 4);
505 *pos++ = WLAN_EID_PWR_CAPABILITY;
506 *pos++ = 2;
507 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200508 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100509
510 /* 2. supported channels */
511 /* TODO: get this in reg domain format */
512 pos = skb_put(skb, 2 * sband->n_channels + 2);
513 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
514 *pos++ = 2 * sband->n_channels;
515 for (i = 0; i < sband->n_channels; i++) {
516 *pos++ = ieee80211_frequency_to_channel(
517 sband->channels[i].center_freq);
518 *pos++ = 1; /* one channel in the subband*/
519 }
520 }
521
522 /* if present, add any custom IEs that go before HT */
523 if (assoc_data->ie_len && assoc_data->ie) {
524 static const u8 before_ht[] = {
525 WLAN_EID_SSID,
526 WLAN_EID_SUPP_RATES,
527 WLAN_EID_EXT_SUPP_RATES,
528 WLAN_EID_PWR_CAPABILITY,
529 WLAN_EID_SUPPORTED_CHANNELS,
530 WLAN_EID_RSN,
531 WLAN_EID_QOS_CAPA,
532 WLAN_EID_RRM_ENABLED_CAPABILITIES,
533 WLAN_EID_MOBILITY_DOMAIN,
534 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
535 };
536 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
537 before_ht, ARRAY_SIZE(before_ht),
538 offset);
539 pos = skb_put(skb, noffset - offset);
540 memcpy(pos, assoc_data->ie + offset, noffset - offset);
541 offset = noffset;
542 }
543
Johannes Berg4e74bfd2012-03-08 15:02:04 +0100544 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg9dde6422012-05-16 23:43:19 +0200545 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200546 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100547
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000548 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
549 ieee80211_add_vht_ie(sdata, skb, sband);
550
Johannes Berg66e67e42012-01-20 13:55:27 +0100551 /* if present, add any custom non-vendor IEs that go after HT */
552 if (assoc_data->ie_len && assoc_data->ie) {
553 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
554 assoc_data->ie_len,
555 offset);
556 pos = skb_put(skb, noffset - offset);
557 memcpy(pos, assoc_data->ie + offset, noffset - offset);
558 offset = noffset;
559 }
560
Johannes Berg76f03032012-03-08 15:02:05 +0100561 if (assoc_data->wmm) {
562 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200563 qos_info = ifmgd->uapsd_queues;
564 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100565 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
566 } else {
567 qos_info = 0;
568 }
569
570 pos = skb_put(skb, 9);
571 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
572 *pos++ = 7; /* len */
573 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
574 *pos++ = 0x50;
575 *pos++ = 0xf2;
576 *pos++ = 2; /* WME */
577 *pos++ = 0; /* WME info */
578 *pos++ = 1; /* WME ver */
579 *pos++ = qos_info;
580 }
581
582 /* add any remaining custom (i.e. vendor specific here) IEs */
583 if (assoc_data->ie_len && assoc_data->ie) {
584 noffset = assoc_data->ie_len;
585 pos = skb_put(skb, noffset - offset);
586 memcpy(pos, assoc_data->ie + offset, noffset - offset);
587 }
588
Johannes Berga1845fc2012-06-27 13:18:36 +0200589 drv_mgd_prepare_tx(local, sdata);
590
Johannes Berg66e67e42012-01-20 13:55:27 +0100591 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
592 ieee80211_tx_skb(sdata, skb);
593}
594
Kalle Valo572e0012009-02-10 17:09:31 +0200595void ieee80211_send_pspoll(struct ieee80211_local *local,
596 struct ieee80211_sub_if_data *sdata)
597{
Kalle Valo572e0012009-02-10 17:09:31 +0200598 struct ieee80211_pspoll *pspoll;
599 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200600
Kalle Valod8cd1892010-01-05 20:16:26 +0200601 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
602 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200603 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200604
Kalle Valod8cd1892010-01-05 20:16:26 +0200605 pspoll = (struct ieee80211_pspoll *) skb->data;
606 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200607
Johannes Berg62ae67b2009-11-18 18:42:05 +0100608 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
609 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200610}
611
Johannes Berg965beda2009-04-16 13:17:24 +0200612void ieee80211_send_nullfunc(struct ieee80211_local *local,
613 struct ieee80211_sub_if_data *sdata,
614 int powersave)
615{
616 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200617 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530618 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200619
Kalle Valod8cd1892010-01-05 20:16:26 +0200620 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
621 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200622 return;
623
Kalle Valod8cd1892010-01-05 20:16:26 +0200624 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200625 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200626 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200627
Johannes Berg62ae67b2009-11-18 18:42:05 +0100628 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530629 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
630 IEEE80211_STA_CONNECTION_POLL))
631 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
632
Johannes Berg62ae67b2009-11-18 18:42:05 +0100633 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200634}
635
Felix Fietkaud5242152010-01-08 18:06:26 +0100636static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
637 struct ieee80211_sub_if_data *sdata)
638{
639 struct sk_buff *skb;
640 struct ieee80211_hdr *nullfunc;
641 __le16 fc;
642
643 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
644 return;
645
646 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700647 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100648 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700649
Felix Fietkaud5242152010-01-08 18:06:26 +0100650 skb_reserve(skb, local->hw.extra_tx_headroom);
651
652 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
653 memset(nullfunc, 0, 30);
654 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
655 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
656 nullfunc->frame_control = fc;
657 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
658 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
659 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
660 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
661
662 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
663 ieee80211_tx_skb(sdata, skb);
664}
665
Johannes Bergcc32abd2009-05-15 11:52:31 +0200666/* spectrum management related things */
667static void ieee80211_chswitch_work(struct work_struct *work)
668{
669 struct ieee80211_sub_if_data *sdata =
670 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200671 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
672
Johannes Berg9607e6b662009-12-23 13:15:31 +0100673 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200674 return;
675
Johannes Berg77fdaa12009-07-07 03:45:17 +0200676 mutex_lock(&ifmgd->mtx);
677 if (!ifmgd->associated)
678 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200679
Johannes Berg55de9082012-07-26 17:24:39 +0200680 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200681 if (!sdata->local->ops->channel_switch) {
682 /* call "hw_config" only if doing sw channel switch */
683 ieee80211_hw_config(sdata->local,
684 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300685 } else {
686 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200687 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200688 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200689
Johannes Berg77fdaa12009-07-07 03:45:17 +0200690 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200691 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200692
Johannes Berg57eebdf2012-08-01 15:50:46 +0200693 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200694 ieee80211_wake_queues_by_reason(&sdata->local->hw,
695 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200696 out:
697 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
698 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200699}
700
Johannes Berg5ce6e432010-05-11 16:20:57 +0200701void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
702{
Johannes Berg55de9082012-07-26 17:24:39 +0200703 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
704 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200705
706 trace_api_chswitch_done(sdata, success);
707 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200708 sdata_info(sdata,
709 "driver channel switch failed, disconnecting\n");
710 ieee80211_queue_work(&sdata->local->hw,
711 &ifmgd->csa_connection_drop_work);
712 } else {
713 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200714 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200715}
716EXPORT_SYMBOL(ieee80211_chswitch_done);
717
Johannes Bergcc32abd2009-05-15 11:52:31 +0200718static void ieee80211_chswitch_timer(unsigned long data)
719{
720 struct ieee80211_sub_if_data *sdata =
721 (struct ieee80211_sub_if_data *) data;
722 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
723
Johannes Berg5bb644a2009-05-17 11:40:42 +0200724 if (sdata->local->quiescing) {
725 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
726 return;
727 }
728
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400729 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200730}
731
732void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
733 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200734 struct ieee80211_bss *bss,
735 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200736{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100737 struct cfg80211_bss *cbss =
738 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200739 struct ieee80211_channel *new_ch;
740 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900741 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
742 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200743 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200744
Johannes Berg77fdaa12009-07-07 03:45:17 +0200745 ASSERT_MGD_MTX(ifmgd);
746
747 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200748 return;
749
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200750 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200751 return;
752
753 /* Disregard subsequent beacons if we are already running a timer
754 processing a CSA */
755
756 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
757 return;
758
759 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +0200760 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
761 sdata_info(sdata,
762 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
763 ifmgd->associated->bssid, new_freq);
764 ieee80211_queue_work(&sdata->local->hw,
765 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200766 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200767 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200768
Johannes Berg57eebdf2012-08-01 15:50:46 +0200769 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
770
Johannes Berg55de9082012-07-26 17:24:39 +0200771 if (sdata->local->use_chanctx) {
772 sdata_info(sdata,
773 "not handling channel switch with channel contexts\n");
774 ieee80211_queue_work(&sdata->local->hw,
775 &ifmgd->csa_connection_drop_work);
776 }
777
778 mutex_lock(&sdata->local->chanctx_mtx);
779 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
780 mutex_unlock(&sdata->local->chanctx_mtx);
781 return;
782 }
783 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
784 struct ieee80211_chanctx, conf);
785 if (chanctx->refcount > 1) {
786 sdata_info(sdata,
787 "channel switch with multiple interfaces on the same channel, disconnecting\n");
788 ieee80211_queue_work(&sdata->local->hw,
789 &ifmgd->csa_connection_drop_work);
790 mutex_unlock(&sdata->local->chanctx_mtx);
791 return;
792 }
793 mutex_unlock(&sdata->local->chanctx_mtx);
794
795 sdata->local->csa_channel = new_ch;
796
Johannes Berg57eebdf2012-08-01 15:50:46 +0200797 if (sw_elem->mode)
798 ieee80211_stop_queues_by_reason(&sdata->local->hw,
799 IEEE80211_QUEUE_STOP_REASON_CSA);
800
Johannes Berg5ce6e432010-05-11 16:20:57 +0200801 if (sdata->local->ops->channel_switch) {
802 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200803 struct ieee80211_channel_switch ch_switch = {
804 .timestamp = timestamp,
805 .block_tx = sw_elem->mode,
806 .channel = new_ch,
807 .count = sw_elem->count,
808 };
809
Johannes Berg5ce6e432010-05-11 16:20:57 +0200810 drv_channel_switch(sdata->local, &ch_switch);
811 return;
812 }
813
814 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200815 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400816 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200817 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200818 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200819 TU_TO_EXP_TIME(sw_elem->count *
820 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200821}
822
823static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200824 struct ieee80211_channel *channel,
825 const u8 *country_ie, u8 country_ie_len,
826 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200827{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200828 struct ieee80211_country_ie_triplet *triplet;
829 int chan = ieee80211_frequency_to_channel(channel->center_freq);
830 int i, chan_pwr, chan_increment, new_ap_level;
831 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200832
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200833 /* Invalid IE */
834 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200835 return;
836
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200837 triplet = (void *)(country_ie + 3);
838 country_ie_len -= 3;
839
840 switch (channel->band) {
841 default:
842 WARN_ON_ONCE(1);
843 /* fall through */
844 case IEEE80211_BAND_2GHZ:
845 case IEEE80211_BAND_60GHZ:
846 chan_increment = 1;
847 break;
848 case IEEE80211_BAND_5GHZ:
849 chan_increment = 4;
850 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200851 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200852
853 /* find channel */
854 while (country_ie_len >= 3) {
855 u8 first_channel = triplet->chans.first_channel;
856
857 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
858 goto next;
859
860 for (i = 0; i < triplet->chans.num_channels; i++) {
861 if (first_channel + i * chan_increment == chan) {
862 have_chan_pwr = true;
863 chan_pwr = triplet->chans.max_power;
864 break;
865 }
866 }
867 if (have_chan_pwr)
868 break;
869
870 next:
871 triplet++;
872 country_ie_len -= 3;
873 }
874
875 if (!have_chan_pwr)
876 return;
877
878 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
879
880 if (sdata->local->ap_power_level == new_ap_level)
881 return;
882
883 sdata_info(sdata,
884 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
885 new_ap_level, chan_pwr, *pwr_constr_elem,
886 sdata->u.mgd.bssid);
887 sdata->local->ap_power_level = new_ap_level;
888 ieee80211_hw_config(sdata->local, 0);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200889}
890
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300891void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
892{
893 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
894 struct ieee80211_local *local = sdata->local;
895 struct ieee80211_conf *conf = &local->hw.conf;
896
897 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
898 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
899 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
900
901 local->disable_dynamic_ps = false;
902 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
903}
904EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
905
906void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
907{
908 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
909 struct ieee80211_local *local = sdata->local;
910 struct ieee80211_conf *conf = &local->hw.conf;
911
912 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
913 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
914 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
915
916 local->disable_dynamic_ps = true;
917 conf->dynamic_ps_timeout = 0;
918 del_timer_sync(&local->dynamic_ps_timer);
919 ieee80211_queue_work(&local->hw,
920 &local->dynamic_ps_enable_work);
921}
922EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
923
Johannes Berg965beda2009-04-16 13:17:24 +0200924/* powersave */
925static void ieee80211_enable_ps(struct ieee80211_local *local,
926 struct ieee80211_sub_if_data *sdata)
927{
928 struct ieee80211_conf *conf = &local->hw.conf;
929
Johannes Bergd5edaed2009-04-22 23:02:51 +0200930 /*
931 * If we are scanning right now then the parameters will
932 * take effect when scan finishes.
933 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200934 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200935 return;
936
Johannes Berg965beda2009-04-16 13:17:24 +0200937 if (conf->dynamic_ps_timeout > 0 &&
938 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
939 mod_timer(&local->dynamic_ps_timer, jiffies +
940 msecs_to_jiffies(conf->dynamic_ps_timeout));
941 } else {
942 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
943 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530944
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200945 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
946 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
947 return;
948
949 conf->flags |= IEEE80211_CONF_PS;
950 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200951 }
952}
953
954static void ieee80211_change_ps(struct ieee80211_local *local)
955{
956 struct ieee80211_conf *conf = &local->hw.conf;
957
958 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200959 ieee80211_enable_ps(local, local->ps_sdata);
960 } else if (conf->flags & IEEE80211_CONF_PS) {
961 conf->flags &= ~IEEE80211_CONF_PS;
962 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
963 del_timer_sync(&local->dynamic_ps_timer);
964 cancel_work_sync(&local->dynamic_ps_enable_work);
965 }
966}
967
Jason Young808118c2011-03-10 16:43:19 -0800968static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
969{
970 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
971 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200972 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800973
974 if (!mgd->powersave)
975 return false;
976
Johannes Berg05cb9102011-10-28 11:59:47 +0200977 if (mgd->broken_ap)
978 return false;
979
Jason Young808118c2011-03-10 16:43:19 -0800980 if (!mgd->associated)
981 return false;
982
Jason Young808118c2011-03-10 16:43:19 -0800983 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
984 IEEE80211_STA_CONNECTION_POLL))
985 return false;
986
987 rcu_read_lock();
988 sta = sta_info_get(sdata, mgd->bssid);
989 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200990 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -0800991 rcu_read_unlock();
992
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200993 return authorized;
Jason Young808118c2011-03-10 16:43:19 -0800994}
995
Johannes Berg965beda2009-04-16 13:17:24 +0200996/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200997void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +0200998{
999 struct ieee80211_sub_if_data *sdata, *found = NULL;
1000 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001001 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001002
1003 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1004 local->ps_sdata = NULL;
1005 return;
1006 }
1007
1008 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001009 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001010 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301011 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1012 /* If an AP vif is found, then disable PS
1013 * by setting the count to zero thereby setting
1014 * ps_sdata to NULL.
1015 */
1016 count = 0;
1017 break;
1018 }
Johannes Berg965beda2009-04-16 13:17:24 +02001019 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1020 continue;
1021 found = sdata;
1022 count++;
1023 }
1024
Jason Young808118c2011-03-10 16:43:19 -08001025 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001026 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001027 s32 beaconint_us;
1028
1029 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001030 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001031
1032 beaconint_us = ieee80211_tu_to_usec(
1033 found->vif.bss_conf.beacon_int);
1034
Juuso Oikarinenff616382010-06-09 09:51:52 +03001035 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001036 if (timeout < 0) {
1037 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001038 * Go to full PSM if the user configures a very low
1039 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001040 * The 2000 second value is there for compatibility
1041 * until the PM_QOS_NETWORK_LATENCY is configured
1042 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001043 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001044 if (latency > (1900 * USEC_PER_MSEC) &&
1045 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001046 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001047 else
1048 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001049 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001050 local->dynamic_ps_user_timeout = timeout;
1051 if (!local->disable_dynamic_ps)
1052 conf->dynamic_ps_timeout =
1053 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001054
Johannes Berg04fe2032009-04-22 18:44:37 +02001055 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001056 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001057 } else {
Johannes Berg56007a02010-01-26 14:19:52 +01001058 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +02001059 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001060 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +02001061
Johannes Berg56007a02010-01-26 14:19:52 +01001062 bss = (void *)found->u.mgd.associated->priv;
1063 dtimper = bss->dtim_period;
1064
1065 /* If the TIM IE is invalid, pretend the value is 1 */
1066 if (!dtimper)
1067 dtimper = 1;
1068 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001069 maxslp = min_t(int, dtimper,
1070 latency / beaconint_us);
1071
Johannes Berg9ccebe62009-04-23 10:32:36 +02001072 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001073 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001074 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001075 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001076 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001077 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001078 }
Johannes Berg965beda2009-04-16 13:17:24 +02001079
1080 ieee80211_change_ps(local);
1081}
1082
Eliad Pellerab095872012-07-27 12:33:22 +03001083void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1084{
1085 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1086
1087 if (sdata->vif.bss_conf.ps != ps_allowed) {
1088 sdata->vif.bss_conf.ps = ps_allowed;
1089 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1090 }
1091}
1092
Johannes Berg965beda2009-04-16 13:17:24 +02001093void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1094{
1095 struct ieee80211_local *local =
1096 container_of(work, struct ieee80211_local,
1097 dynamic_ps_disable_work);
1098
1099 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1100 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1101 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1102 }
1103
1104 ieee80211_wake_queues_by_reason(&local->hw,
1105 IEEE80211_QUEUE_STOP_REASON_PS);
1106}
1107
1108void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1109{
1110 struct ieee80211_local *local =
1111 container_of(work, struct ieee80211_local,
1112 dynamic_ps_enable_work);
1113 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001114 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301115 unsigned long flags;
1116 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001117
1118 /* can only happen when PS was just disabled anyway */
1119 if (!sdata)
1120 return;
1121
Christian Lamparter5e340692011-06-30 21:08:43 +02001122 ifmgd = &sdata->u.mgd;
1123
Johannes Berg965beda2009-04-16 13:17:24 +02001124 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1125 return;
1126
Arik Nemtsov77b70232011-06-26 12:06:54 +03001127 if (!local->disable_dynamic_ps &&
1128 local->hw.conf.dynamic_ps_timeout > 0) {
1129 /* don't enter PS if TX frames are pending */
1130 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301131 mod_timer(&local->dynamic_ps_timer, jiffies +
1132 msecs_to_jiffies(
1133 local->hw.conf.dynamic_ps_timeout));
1134 return;
1135 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001136
1137 /*
1138 * transmission can be stopped by others which leads to
1139 * dynamic_ps_timer expiry. Postpone the ps timer if it
1140 * is not the actual idle state.
1141 */
1142 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1143 for (q = 0; q < local->hw.queues; q++) {
1144 if (local->queue_stop_reasons[q]) {
1145 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1146 flags);
1147 mod_timer(&local->dynamic_ps_timer, jiffies +
1148 msecs_to_jiffies(
1149 local->hw.conf.dynamic_ps_timeout));
1150 return;
1151 }
1152 }
1153 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301154 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301155
Vivek Natarajan375177b2010-02-09 14:50:28 +05301156 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301157 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301158 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001159
Vivek Natarajane8306f92011-04-06 11:41:10 +05301160 if (drv_tx_frames_pending(local))
1161 mod_timer(&local->dynamic_ps_timer, jiffies +
1162 msecs_to_jiffies(
1163 local->hw.conf.dynamic_ps_timeout));
1164 else {
1165 ieee80211_send_nullfunc(local, sdata, 1);
1166 /* Flush to get the tx status of nullfunc frame */
1167 drv_flush(local, false);
1168 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301169 }
1170
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001171 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1172 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301173 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1174 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1175 local->hw.conf.flags |= IEEE80211_CONF_PS;
1176 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1177 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301178
Arik Nemtsov77b70232011-06-26 12:06:54 +03001179 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1180 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001181}
1182
1183void ieee80211_dynamic_ps_timer(unsigned long data)
1184{
1185 struct ieee80211_local *local = (void *) data;
1186
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001187 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001188 return;
1189
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001190 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001191}
1192
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001193/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001194static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001195 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001196 u8 *wmm_param, size_t wmm_param_len)
1197{
Jiri Bencf0706e82007-05-05 11:45:53 -07001198 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001199 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001200 size_t left;
1201 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001202 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001203
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001204 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001205 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001206
Johannes Berg32c50572012-03-28 11:04:29 +02001207 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001208 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001209
1210 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001211 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001212
Jiri Bencf0706e82007-05-05 11:45:53 -07001213 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001214 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001215
1216 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001217 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001218
Jiri Bencf0706e82007-05-05 11:45:53 -07001219 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001220 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001221 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001222 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001223
1224 pos = wmm_param + 8;
1225 left = wmm_param_len - 8;
1226
1227 memset(&params, 0, sizeof(params));
1228
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001229 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001230 for (; left >= 4; left -= 4, pos += 4) {
1231 int aci = (pos[0] >> 5) & 0x03;
1232 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001233 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001234 int queue;
1235
1236 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001237 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001238 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001239 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001240 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001241 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1242 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001243 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001244 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001245 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001246 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001247 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001248 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1249 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001250 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001251 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001252 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001253 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001254 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001255 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1256 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001257 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001258 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001259 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001260 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001261 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001262 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001263 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1264 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001265 break;
1266 }
1267
1268 params.aifs = pos[0] & 0x0f;
1269 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1270 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001271 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001272 params.uapsd = uapsd;
1273
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001274 mlme_dbg(sdata,
1275 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1276 queue, aci, acm,
1277 params.aifs, params.cw_min, params.cw_max,
1278 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001279 sdata->tx_conf[queue] = params;
1280 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001281 sdata_err(sdata,
1282 "failed to set TX queue parameters for queue %d\n",
1283 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001284 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001285
1286 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001287 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001288 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001289}
1290
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001291static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1292{
1293 lockdep_assert_held(&sdata->local->mtx);
1294
1295 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1296 IEEE80211_STA_BEACON_POLL);
1297 ieee80211_run_deferred_scan(sdata->local);
1298}
1299
1300static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1301{
1302 mutex_lock(&sdata->local->mtx);
1303 __ieee80211_stop_poll(sdata);
1304 mutex_unlock(&sdata->local->mtx);
1305}
1306
Johannes Berg7a5158e2008-10-08 10:59:33 +02001307static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1308 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001309{
Johannes Bergbda39332008-10-11 01:51:51 +02001310 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001311 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001312 bool use_protection;
1313 bool use_short_preamble;
1314 bool use_short_slot;
1315
1316 if (erp_valid) {
1317 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1318 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1319 } else {
1320 use_protection = false;
1321 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1322 }
1323
1324 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001325 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001326 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001327
Johannes Berg471b3ef2007-12-28 14:32:58 +01001328 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001329 bss_conf->use_cts_prot = use_protection;
1330 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001331 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001332
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001333 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001334 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001335 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001336 }
Daniel Draked9430a32007-07-27 15:43:24 +02001337
Johannes Berg7a5158e2008-10-08 10:59:33 +02001338 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001339 bss_conf->use_short_slot = use_short_slot;
1340 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001341 }
1342
1343 return changed;
1344}
1345
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001346static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001347 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001348 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001349{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001350 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001351 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001352 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001353
Johannes Bergae5eb022008-10-14 16:58:37 +02001354 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001355 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001356 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001357
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001358 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1359 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1360
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001361 sdata->u.mgd.associated = cbss;
1362 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001363
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001364 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1365
Johannes Bergb291ba12009-07-10 15:29:03 +02001366 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001367 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001368
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001369 ieee80211_led_assoc(local, 1);
1370
Johannes Berge5b900d2010-07-29 16:08:55 +02001371 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1372 bss_conf->dtim_period = bss->dtim_period;
1373 else
1374 bss_conf->dtim_period = 0;
1375
Juuso Oikarinen68542962010-06-09 13:43:26 +03001376 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001377
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001378 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001379 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001380 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001381 bss_info_changed |= BSS_CHANGED_CQM;
1382
Juuso Oikarinen68542962010-06-09 13:43:26 +03001383 /* Enable ARP filtering */
1384 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1385 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1386 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1387 }
1388
Johannes Bergae5eb022008-10-14 16:58:37 +02001389 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001390
Johannes Berg056508d2009-07-30 21:43:55 +02001391 mutex_lock(&local->iflist_mtx);
1392 ieee80211_recalc_ps(local, -1);
1393 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001394
Johannes Berg04ecd252012-09-11 14:34:12 +02001395 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001396 ieee80211_recalc_ps_vif(sdata);
1397
John W. Linville8a5b33f2010-01-06 15:39:39 -05001398 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001399 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001400}
1401
Jouni Malinene69e95d2010-03-29 23:29:31 -07001402static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001403 u16 stype, u16 reason, bool tx,
1404 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001405{
Johannes Berg46900292009-02-15 12:44:28 +01001406 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001407 struct ieee80211_local *local = sdata->local;
1408 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001409 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001410
Johannes Berg77fdaa12009-07-07 03:45:17 +02001411 ASSERT_MGD_MTX(ifmgd);
1412
Johannes Berg37ad3882012-02-24 13:50:54 +01001413 if (WARN_ON_ONCE(tx && !frame_buf))
1414 return;
1415
Johannes Bergb291ba12009-07-10 15:29:03 +02001416 if (WARN_ON(!ifmgd->associated))
1417 return;
1418
David Spinadel79543d82012-06-12 09:59:45 +03001419 ieee80211_stop_poll(sdata);
1420
Johannes Berg77fdaa12009-07-07 03:45:17 +02001421 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001422
1423 /*
1424 * we need to commit the associated = NULL change because the
1425 * scan code uses that to determine whether this iface should
1426 * go to/wake up from powersave or not -- and could otherwise
1427 * wake the queues erroneously.
1428 */
1429 smp_mb();
1430
1431 /*
1432 * Thus, we can only afterwards stop the queues -- to account
1433 * for the case where another CPU is finishing a scan at this
1434 * time -- we don't want the scan code to enable queues.
1435 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001436
John W. Linville8a5b33f2010-01-06 15:39:39 -05001437 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001438 netif_carrier_off(sdata->dev);
1439
Johannes Berg2a419052010-06-10 10:21:29 +02001440 mutex_lock(&local->sta_mtx);
Eliad Peller88a9e312012-06-01 11:14:03 +03001441 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301442 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001443 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg7f161142012-07-18 12:53:34 +02001444 ieee80211_sta_tear_down_BA_sessions(sta, false);
Sujith4cad6c72010-02-10 14:52:21 +05301445 }
Johannes Berg2a419052010-06-10 10:21:29 +02001446 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001447
Eliad Peller88bc40e2012-07-12 17:35:33 +03001448 /*
1449 * if we want to get out of ps before disassoc (why?) we have
1450 * to do it before sending disassoc, as otherwise the null-packet
1451 * won't be valid.
1452 */
1453 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1454 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1455 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1456 }
1457 local->ps_sdata = NULL;
1458
Eliad Pellerab095872012-07-27 12:33:22 +03001459 /* disable per-vif ps */
1460 ieee80211_recalc_ps_vif(sdata);
1461
Eliad Pellerf8239812012-06-27 14:18:22 +03001462 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1463 if (tx)
1464 drv_flush(local, false);
1465
Johannes Berg37ad3882012-02-24 13:50:54 +01001466 /* deauthenticate/disassociate now */
1467 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001468 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1469 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001470
1471 /* flush out frame */
1472 if (tx)
1473 drv_flush(local, false);
1474
Eliad Peller88a9e312012-06-01 11:14:03 +03001475 /* clear bssid only after building the needed mgmt frames */
1476 memset(ifmgd->bssid, 0, ETH_ALEN);
1477
Johannes Berg37ad3882012-02-24 13:50:54 +01001478 /* remove AP and TDLS peers */
1479 sta_info_flush(local, sdata);
1480
1481 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001482 changed |= ieee80211_reset_erp_info(sdata);
1483
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001484 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001485 changed |= BSS_CHANGED_ASSOC;
1486 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001487
Johannes Berg413ad502009-05-08 21:21:06 +02001488 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001489 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1490 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001491
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001492 local->ap_power_level = 0;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301493
Kalle Valo520eb822008-12-18 23:35:27 +02001494 del_timer_sync(&local->dynamic_ps_timer);
1495 cancel_work_sync(&local->dynamic_ps_enable_work);
1496
Juuso Oikarinen68542962010-06-09 13:43:26 +03001497 /* Disable ARP filtering */
1498 if (sdata->vif.bss_conf.arp_filter_enabled) {
1499 sdata->vif.bss_conf.arp_filter_enabled = false;
1500 changed |= BSS_CHANGED_ARP_FILTER;
1501 }
1502
Johannes Berg3abead52012-03-02 15:56:59 +01001503 sdata->vif.bss_conf.qos = false;
1504 changed |= BSS_CHANGED_QOS;
1505
Johannes Berg0aaffa92010-05-05 15:28:27 +02001506 /* The BSSID (not really interesting) and HT changed */
1507 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001508 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001509
Johannes Berg55de9082012-07-26 17:24:39 +02001510 ieee80211_vif_release_channel(sdata);
Johannes Berg3cc52402012-03-09 13:12:35 +01001511
Johannes Berg3abead52012-03-02 15:56:59 +01001512 /* disassociated - set to defaults now */
1513 ieee80211_set_wmm_default(sdata, false);
1514
Johannes Bergb9dcf712010-08-27 12:35:54 +02001515 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1516 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1517 del_timer_sync(&sdata->u.mgd.timer);
1518 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001519
1520 sdata->u.mgd.timers_running = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001521}
Jiri Bencf0706e82007-05-05 11:45:53 -07001522
Kalle Valo3cf335d52009-03-22 21:57:06 +02001523void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1524 struct ieee80211_hdr *hdr)
1525{
1526 /*
1527 * We can postpone the mgd.timer whenever receiving unicast frames
1528 * from AP because we know that the connection is working both ways
1529 * at that time. But multicast frames (and hence also beacons) must
1530 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001531 * data idle periods for sending the periodic probe request to the
1532 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02001533 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001534 if (is_multicast_ether_addr(hdr->addr1))
1535 return;
1536
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001537 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02001538}
Jiri Bencf0706e82007-05-05 11:45:53 -07001539
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001540static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1541{
1542 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001543 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001544
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001545 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001546 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001547 IEEE80211_STA_CONNECTION_POLL))) {
1548 mutex_unlock(&local->mtx);
1549 return;
1550 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001551
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001552 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001553
1554 mutex_lock(&local->iflist_mtx);
1555 ieee80211_recalc_ps(local, -1);
1556 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001557
1558 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001559 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001560
1561 /*
1562 * We've received a probe response, but are not sure whether
1563 * we have or will be receiving any beacons or data, so let's
1564 * schedule the timers again, just in case.
1565 */
1566 ieee80211_sta_reset_beacon_monitor(sdata);
1567
1568 mod_timer(&ifmgd->conn_mon_timer,
1569 round_jiffies_up(jiffies +
1570 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001571out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001572 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001573}
1574
1575void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001576 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001577{
Felix Fietkau75706d02010-12-02 21:01:07 +01001578 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001579 return;
1580
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001581 if (ack)
1582 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001583
1584 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1585 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001586 if (ack)
1587 sdata->u.mgd.probe_send_count = 0;
1588 else
1589 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001590 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1591 }
1592}
1593
Maxim Levitskya43abf22009-07-31 18:54:12 +03001594static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1595{
1596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1597 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001598 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001599 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001600
1601 /*
1602 * Try sending broadcast probe requests for the last three
1603 * probe requests after the first ones failed since some
1604 * buggy APs only support broadcast probe requests.
1605 */
1606 if (ifmgd->probe_send_count >= unicast_limit)
1607 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001608
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001609 /*
1610 * When the hardware reports an accurate Tx ACK status, it's
1611 * better to send a nullfunc frame instead of a probe request,
1612 * as it will kick us off the AP quickly if we aren't associated
1613 * anymore. The timeout will be reset if the frame is ACKed by
1614 * the AP.
1615 */
Soumik Das992e68b2012-05-20 15:31:13 +05301616 ifmgd->probe_send_count++;
1617
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001618 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1619 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001620 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001621 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001622 int ssid_len;
1623
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001624 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001625 if (WARN_ON_ONCE(ssid == NULL))
1626 ssid_len = 0;
1627 else
1628 ssid_len = ssid[1];
1629
1630 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Bergfe94fe02012-07-30 12:26:34 +02001631 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02001632 ifmgd->associated->channel, false);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001633 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001634
Ben Greear180205b2011-02-04 15:30:24 -08001635 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001636 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301637 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1638 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001639}
1640
Johannes Bergb291ba12009-07-10 15:29:03 +02001641static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1642 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001643{
Kalle Valo04de8382009-03-22 21:57:35 +02001644 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001645 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001646
Johannes Berg9607e6b662009-12-23 13:15:31 +01001647 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001648 return;
1649
Johannes Berg77fdaa12009-07-07 03:45:17 +02001650 mutex_lock(&ifmgd->mtx);
1651
1652 if (!ifmgd->associated)
1653 goto out;
1654
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001655 mutex_lock(&sdata->local->mtx);
1656
1657 if (sdata->local->tmp_channel || sdata->local->scanning) {
1658 mutex_unlock(&sdata->local->mtx);
1659 goto out;
1660 }
1661
Joe Perchese87cc472012-05-13 21:56:26 +00001662 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001663 mlme_dbg_ratelimited(sdata,
1664 "detected beacon loss from AP - sending probe request\n");
1665
Holger Schurig6efb71b2012-05-15 15:38:59 +02001666 ieee80211_cqm_rssi_notify(&sdata->vif,
1667 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001668
Johannes Bergb291ba12009-07-10 15:29:03 +02001669 /*
1670 * The driver/our work has already reported this event or the
1671 * connection monitoring has kicked in and we have already sent
1672 * a probe request. Or maybe the AP died and the driver keeps
1673 * reporting until we disassociate...
1674 *
1675 * In either case we have to ignore the current call to this
1676 * function (except for setting the correct probe reason bit)
1677 * because otherwise we would reset the timer every time and
1678 * never check whether we received a probe response!
1679 */
1680 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1681 IEEE80211_STA_CONNECTION_POLL))
1682 already = true;
1683
1684 if (beacon)
1685 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1686 else
1687 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1688
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001689 mutex_unlock(&sdata->local->mtx);
1690
Johannes Bergb291ba12009-07-10 15:29:03 +02001691 if (already)
1692 goto out;
1693
Johannes Berg4e751842009-06-10 15:16:52 +02001694 mutex_lock(&sdata->local->iflist_mtx);
1695 ieee80211_recalc_ps(sdata->local, -1);
1696 mutex_unlock(&sdata->local->iflist_mtx);
1697
Maxim Levitskya43abf22009-07-31 18:54:12 +03001698 ifmgd->probe_send_count = 0;
1699 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001700 out:
1701 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001702}
1703
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001704struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1705 struct ieee80211_vif *vif)
1706{
1707 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1708 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001709 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001710 struct sk_buff *skb;
1711 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001712 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001713
1714 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1715 return NULL;
1716
1717 ASSERT_MGD_MTX(ifmgd);
1718
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001719 if (ifmgd->associated)
1720 cbss = ifmgd->associated;
1721 else if (ifmgd->auth_data)
1722 cbss = ifmgd->auth_data->bss;
1723 else if (ifmgd->assoc_data)
1724 cbss = ifmgd->assoc_data->bss;
1725 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001726 return NULL;
1727
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001728 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001729 if (WARN_ON_ONCE(ssid == NULL))
1730 ssid_len = 0;
1731 else
1732 ssid_len = ssid[1];
1733
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001734 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001735 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001736 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001737 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001738
1739 return skb;
1740}
1741EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1742
Johannes Berg882a7c62012-08-01 22:32:45 +02001743static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1744 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001745{
1746 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1747 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001748 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001749
1750 mutex_lock(&ifmgd->mtx);
1751 if (!ifmgd->associated) {
1752 mutex_unlock(&ifmgd->mtx);
1753 return;
1754 }
1755
Johannes Berg37ad3882012-02-24 13:50:54 +01001756 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1757 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001758 transmit_frame, frame_buf);
1759 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001760 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001761
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001762 /*
1763 * must be outside lock due to cfg80211,
1764 * but that's not a problem.
1765 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001766 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001767
1768 mutex_lock(&local->mtx);
1769 ieee80211_recalc_idle(local);
1770 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001771}
1772
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001773static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001774{
1775 struct ieee80211_sub_if_data *sdata =
1776 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001777 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001778 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1779 struct sta_info *sta;
1780
1781 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301782 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001783 sta = sta_info_get(sdata, ifmgd->bssid);
1784 if (sta)
1785 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301786 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001787 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001788
Johannes Berg882a7c62012-08-01 22:32:45 +02001789 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1790 sdata_info(sdata, "Connection to AP %pM lost\n",
1791 ifmgd->bssid);
1792 __ieee80211_disconnect(sdata, false);
1793 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001794 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001795 }
1796}
1797
1798static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1799{
1800 struct ieee80211_sub_if_data *sdata =
1801 container_of(work, struct ieee80211_sub_if_data,
1802 u.mgd.csa_connection_drop_work);
1803
1804 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1805 IEEE80211_QUEUE_STOP_REASON_CSA);
1806 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001807}
1808
Kalle Valo04de8382009-03-22 21:57:35 +02001809void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1810{
1811 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001812 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001813
Johannes Bergb5878a22010-04-07 16:48:40 +02001814 trace_api_beacon_loss(sdata);
1815
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001816 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1817 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001818}
1819EXPORT_SYMBOL(ieee80211_beacon_loss);
1820
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001821void ieee80211_connection_loss(struct ieee80211_vif *vif)
1822{
1823 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1824 struct ieee80211_hw *hw = &sdata->local->hw;
1825
Johannes Bergb5878a22010-04-07 16:48:40 +02001826 trace_api_connection_loss(sdata);
1827
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001828 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1829 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1830}
1831EXPORT_SYMBOL(ieee80211_connection_loss);
1832
1833
Johannes Berg66e67e42012-01-20 13:55:27 +01001834static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1835 bool assoc)
1836{
1837 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1838
1839 lockdep_assert_held(&sdata->u.mgd.mtx);
1840
Johannes Berg66e67e42012-01-20 13:55:27 +01001841 if (!assoc) {
1842 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1843
1844 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1845 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg55de9082012-07-26 17:24:39 +02001846 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001847 }
1848
1849 cfg80211_put_bss(auth_data->bss);
1850 kfree(auth_data);
1851 sdata->u.mgd.auth_data = NULL;
1852}
1853
1854static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1855 struct ieee80211_mgmt *mgmt, size_t len)
1856{
1857 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1858 u8 *pos;
1859 struct ieee802_11_elems elems;
1860
1861 pos = mgmt->u.auth.variable;
1862 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1863 if (!elems.challenge)
1864 return;
1865 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001866 drv_mgd_prepare_tx(sdata->local, sdata);
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001867 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001868 elems.challenge - 2, elems.challenge_len + 2,
1869 auth_data->bss->bssid, auth_data->bss->bssid,
1870 auth_data->key, auth_data->key_len,
1871 auth_data->key_idx);
1872}
1873
1874static enum rx_mgmt_action __must_check
1875ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1876 struct ieee80211_mgmt *mgmt, size_t len)
1877{
1878 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1879 u8 bssid[ETH_ALEN];
1880 u16 auth_alg, auth_transaction, status_code;
1881 struct sta_info *sta;
1882
1883 lockdep_assert_held(&ifmgd->mtx);
1884
1885 if (len < 24 + 6)
1886 return RX_MGMT_NONE;
1887
1888 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1889 return RX_MGMT_NONE;
1890
1891 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1892
Joe Perchesb203ca32012-05-08 18:56:52 +00001893 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001894 return RX_MGMT_NONE;
1895
1896 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1897 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1898 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1899
1900 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001901 auth_transaction != ifmgd->auth_data->expected_transaction) {
1902 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1903 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1904 auth_transaction,
1905 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001906 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001907 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001908
1909 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001910 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1911 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001912 ieee80211_destroy_auth_data(sdata, false);
1913 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001914 }
1915
1916 switch (ifmgd->auth_data->algorithm) {
1917 case WLAN_AUTH_OPEN:
1918 case WLAN_AUTH_LEAP:
1919 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001920 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01001921 break;
1922 case WLAN_AUTH_SHARED_KEY:
1923 if (ifmgd->auth_data->expected_transaction != 4) {
1924 ieee80211_auth_challenge(sdata, mgmt, len);
1925 /* need another frame */
1926 return RX_MGMT_NONE;
1927 }
1928 break;
1929 default:
1930 WARN_ONCE(1, "invalid auth alg %d",
1931 ifmgd->auth_data->algorithm);
1932 return RX_MGMT_NONE;
1933 }
1934
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001935 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001936 ifmgd->auth_data->done = true;
1937 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1938 run_again(ifmgd, ifmgd->auth_data->timeout);
1939
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001940 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
1941 ifmgd->auth_data->expected_transaction != 2) {
1942 /*
1943 * Report auth frame to user space for processing since another
1944 * round of Authentication frames is still needed.
1945 */
1946 return RX_MGMT_CFG80211_RX_AUTH;
1947 }
1948
Johannes Berg66e67e42012-01-20 13:55:27 +01001949 /* move station state to auth */
1950 mutex_lock(&sdata->local->sta_mtx);
1951 sta = sta_info_get(sdata, bssid);
1952 if (!sta) {
1953 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1954 goto out_err;
1955 }
1956 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001957 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01001958 goto out_err;
1959 }
1960 mutex_unlock(&sdata->local->sta_mtx);
1961
1962 return RX_MGMT_CFG80211_RX_AUTH;
1963 out_err:
1964 mutex_unlock(&sdata->local->sta_mtx);
1965 /* ignore frame -- wait for timeout */
1966 return RX_MGMT_NONE;
1967}
1968
1969
Johannes Berg77fdaa12009-07-07 03:45:17 +02001970static enum rx_mgmt_action __must_check
1971ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001972 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001973{
Johannes Berg46900292009-02-15 12:44:28 +01001974 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001975 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001976 u16 reason_code;
1977
Johannes Berg66e67e42012-01-20 13:55:27 +01001978 lockdep_assert_held(&ifmgd->mtx);
1979
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001980 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02001981 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001982
Johannes Berg66e67e42012-01-20 13:55:27 +01001983 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00001984 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001985 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001986
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001987 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001988
1989 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1990
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001991 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
1992 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001993
Johannes Berg37ad3882012-02-24 13:50:54 +01001994 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1995
Johannes Berg7da7cc12010-08-05 17:02:38 +02001996 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01001997 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001998 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001999
2000 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002001}
2002
2003
Johannes Berg77fdaa12009-07-07 03:45:17 +02002004static enum rx_mgmt_action __must_check
2005ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2006 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002007{
Johannes Berg46900292009-02-15 12:44:28 +01002008 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002009 u16 reason_code;
2010
Johannes Berg66e67e42012-01-20 13:55:27 +01002011 lockdep_assert_held(&ifmgd->mtx);
2012
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002013 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002014 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002015
Johannes Berg66e67e42012-01-20 13:55:27 +01002016 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002017 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002018 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002019
2020 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2021
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002022 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2023 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002024
Johannes Berg37ad3882012-02-24 13:50:54 +01002025 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2026
Johannes Berg7da7cc12010-08-05 17:02:38 +02002027 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002028 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002029 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002030
Johannes Berg77fdaa12009-07-07 03:45:17 +02002031 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002032}
2033
Christian Lamparterc74d0842011-10-15 00:14:49 +02002034static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2035 u8 *supp_rates, unsigned int supp_rates_len,
2036 u32 *rates, u32 *basic_rates,
2037 bool *have_higher_than_11mbit,
2038 int *min_rate, int *min_rate_index)
2039{
2040 int i, j;
2041
2042 for (i = 0; i < supp_rates_len; i++) {
2043 int rate = (supp_rates[i] & 0x7f) * 5;
2044 bool is_basic = !!(supp_rates[i] & 0x80);
2045
2046 if (rate > 110)
2047 *have_higher_than_11mbit = true;
2048
2049 /*
2050 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2051 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2052 *
2053 * Note: Even through the membership selector and the basic
2054 * rate flag share the same bit, they are not exactly
2055 * the same.
2056 */
2057 if (!!(supp_rates[i] & 0x80) &&
2058 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2059 continue;
2060
2061 for (j = 0; j < sband->n_bitrates; j++) {
2062 if (sband->bitrates[j].bitrate == rate) {
2063 *rates |= BIT(j);
2064 if (is_basic)
2065 *basic_rates |= BIT(j);
2066 if (rate < *min_rate) {
2067 *min_rate = rate;
2068 *min_rate_index = j;
2069 }
2070 break;
2071 }
2072 }
2073 }
2074}
Jiri Bencf0706e82007-05-05 11:45:53 -07002075
Johannes Berg66e67e42012-01-20 13:55:27 +01002076static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2077 bool assoc)
2078{
2079 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2080
2081 lockdep_assert_held(&sdata->u.mgd.mtx);
2082
Johannes Berg66e67e42012-01-20 13:55:27 +01002083 if (!assoc) {
2084 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2085
2086 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2087 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg55de9082012-07-26 17:24:39 +02002088 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002089 }
2090
2091 kfree(assoc_data);
2092 sdata->u.mgd.assoc_data = NULL;
2093}
2094
2095static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2096 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002097 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002098{
Johannes Berg46900292009-02-15 12:44:28 +01002099 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002100 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002101 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002102 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002103 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002104 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002105 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002106 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002107 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002108 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002109
Johannes Bergaf6b6372009-12-23 13:15:35 +01002110 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002111
Jiri Bencf0706e82007-05-05 11:45:53 -07002112 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002113 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002114
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002115 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002116 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2117 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002118 aid &= ~(BIT(15) | BIT(14));
2119
Johannes Berg05cb9102011-10-28 11:59:47 +02002120 ifmgd->broken_ap = false;
2121
2122 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002123 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2124 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002125 aid = 0;
2126 ifmgd->broken_ap = true;
2127 }
2128
Johannes Bergaf6b6372009-12-23 13:15:35 +01002129 pos = mgmt->u.assoc_resp.variable;
2130 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2131
Jiri Bencf0706e82007-05-05 11:45:53 -07002132 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002133 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002134 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002135 }
2136
Johannes Berg46900292009-02-15 12:44:28 +01002137 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002138
Guy Eilam2a33bee2011-08-17 15:18:15 +03002139 mutex_lock(&sdata->local->sta_mtx);
2140 /*
2141 * station info was already allocated and inserted before
2142 * the association and should be available to us
2143 */
Johannes Berg7852e362012-01-20 13:55:24 +01002144 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002145 if (WARN_ON(!sta)) {
2146 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002147 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002148 }
2149
Johannes Berg55de9082012-07-26 17:24:39 +02002150 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002151
Johannes Bergab1faea2009-07-01 21:41:17 +02002152 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Ben Greearef96a8422011-11-18 11:32:00 -08002153 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002154 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002155
Johannes Berg64f68e52012-03-28 10:58:37 +02002156 sta->supports_40mhz =
2157 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2158
Mahesh Palivela818255e2012-10-10 11:33:04 +00002159 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2160 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2161 elems.vht_cap_elem,
2162 &sta->sta.vht_cap);
2163
Johannes Berg4b7679a2008-09-18 18:14:18 +02002164 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002165
Johannes Berg46900292009-02-15 12:44:28 +01002166 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002167 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002168
Johannes Bergddf4ac52008-10-22 11:41:38 +02002169 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002170 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002171
Johannes Bergc8987872012-01-20 13:55:17 +01002172 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2173 if (!err)
2174 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2175 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2176 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2177 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002178 sdata_info(sdata,
2179 "failed to move station %pM to desired state\n",
2180 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002181 WARN_ON(__sta_info_destroy(sta));
2182 mutex_unlock(&sdata->local->sta_mtx);
2183 return false;
2184 }
2185
Johannes Berg7852e362012-01-20 13:55:24 +01002186 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002187
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002188 /*
2189 * Always handle WMM once after association regardless
2190 * of the first value the AP uses. Setting -1 here has
2191 * that effect because the AP values is an unsigned
2192 * 4-bit value.
2193 */
2194 ifmgd->wmm_last_param_set = -1;
2195
Johannes Bergddf4ac52008-10-22 11:41:38 +02002196 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002197 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002198 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002199 else
Johannes Berg3abead52012-03-02 15:56:59 +01002200 ieee80211_set_wmm_default(sdata, false);
2201 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002202
Johannes Berg074d46d2012-03-15 19:45:16 +01002203 if (elems.ht_operation && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002204 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg24398e32012-03-28 10:58:36 +02002205 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2206 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002207
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002208 /* set AID and assoc capability,
2209 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002210 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002211 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002212 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002213
Kalle Valo15b7b062009-03-22 21:57:14 +02002214 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002215 * If we're using 4-addr mode, let the AP know that we're
2216 * doing so, so that it can create the STA VLAN on its side
2217 */
2218 if (ifmgd->use_4addr)
2219 ieee80211_send_4addr_nullfunc(local, sdata);
2220
2221 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002222 * Start timer to probe the connection to the AP now.
2223 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002224 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002225 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002226 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002227
Johannes Bergaf6b6372009-12-23 13:15:35 +01002228 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002229}
2230
Johannes Berg66e67e42012-01-20 13:55:27 +01002231static enum rx_mgmt_action __must_check
2232ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2233 struct ieee80211_mgmt *mgmt, size_t len,
2234 struct cfg80211_bss **bss)
2235{
2236 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2237 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2238 u16 capab_info, status_code, aid;
2239 struct ieee802_11_elems elems;
2240 u8 *pos;
2241 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002242
Johannes Berg66e67e42012-01-20 13:55:27 +01002243 lockdep_assert_held(&ifmgd->mtx);
2244
2245 if (!assoc_data)
2246 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002247 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002248 return RX_MGMT_NONE;
2249
2250 /*
2251 * AssocResp and ReassocResp have identical structure, so process both
2252 * of them in this function.
2253 */
2254
2255 if (len < 24 + 6)
2256 return RX_MGMT_NONE;
2257
2258 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2259 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2260 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2261 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2262
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002263 sdata_info(sdata,
2264 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2265 reassoc ? "Rea" : "A", mgmt->sa,
2266 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002267
2268 pos = mgmt->u.assoc_resp.variable;
2269 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2270
2271 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2272 elems.timeout_int && elems.timeout_int_len == 5 &&
2273 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2274 u32 tu, ms;
2275 tu = get_unaligned_le32(elems.timeout_int + 1);
2276 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002277 sdata_info(sdata,
2278 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2279 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002280 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2281 if (ms > IEEE80211_ASSOC_TIMEOUT)
2282 run_again(ifmgd, assoc_data->timeout);
2283 return RX_MGMT_NONE;
2284 }
2285
2286 *bss = assoc_data->bss;
2287
2288 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002289 sdata_info(sdata, "%pM denied association (code=%d)\n",
2290 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002291 ieee80211_destroy_assoc_data(sdata, false);
2292 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002293 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2294 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002295 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002296 cfg80211_put_bss(*bss);
2297 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2298 }
John W. Linville635d9992012-07-09 16:34:34 -04002299 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002300
2301 /*
2302 * destroy assoc_data afterwards, as otherwise an idle
2303 * recalc after assoc_data is NULL but before associated
2304 * is set can cause the interface to go idle
2305 */
2306 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002307 }
2308
2309 return RX_MGMT_CFG80211_RX_ASSOC;
2310}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002311static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002312 struct ieee80211_mgmt *mgmt,
2313 size_t len,
2314 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002315 struct ieee802_11_elems *elems,
2316 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002317{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002318 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002319 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002320 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002321 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002322 bool need_ps = false;
2323
Johannes Berg66e67e42012-01-20 13:55:27 +01002324 if (sdata->u.mgd.associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002325 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
Johannes Berg56007a02010-01-26 14:19:52 +01002326 bss = (void *)sdata->u.mgd.associated->priv;
2327 /* not previously set so we may need to recalc */
2328 need_ps = !bss->dtim_period;
2329 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002330
Johannes Berg5bda6172008-09-08 11:05:10 +02002331 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002332 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2333 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002334 else
2335 freq = rx_status->freq;
2336
2337 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2338
2339 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2340 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002341
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002342 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002343 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002344 if (bss)
2345 ieee80211_rx_bss_put(local, bss);
2346
2347 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002348 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002349
Johannes Berg56007a02010-01-26 14:19:52 +01002350 if (need_ps) {
2351 mutex_lock(&local->iflist_mtx);
2352 ieee80211_recalc_ps(local, -1);
2353 mutex_unlock(&local->iflist_mtx);
2354 }
2355
Johannes Berg5bc14202012-08-01 16:13:02 +02002356 if (elems->ch_switch_ie &&
2357 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2358 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002359 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002360}
2361
2362
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002363static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002364 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002365{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002366 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002367 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002368 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2369 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002370 struct ieee802_11_elems elems;
2371
Kalle Valo15b7b062009-03-22 21:57:14 +02002372 ifmgd = &sdata->u.mgd;
2373
Johannes Berg77fdaa12009-07-07 03:45:17 +02002374 ASSERT_MGD_MTX(ifmgd);
2375
Joe Perchesb203ca32012-05-08 18:56:52 +00002376 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002377 return; /* ignore ProbeResp to foreign address */
2378
Ester Kummerae6a44e2008-06-27 18:54:48 +03002379 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2380 if (baselen > len)
2381 return;
2382
2383 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2384 &elems);
2385
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002386 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002387
Johannes Berg77fdaa12009-07-07 03:45:17 +02002388 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002389 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002390 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002391
2392 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002393 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002394 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002395 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002396 ifmgd->auth_data->tries = 0;
2397 ifmgd->auth_data->timeout = jiffies;
2398 run_again(ifmgd, ifmgd->auth_data->timeout);
2399 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002400}
2401
Johannes Bergd91f36d2009-04-16 13:17:26 +02002402/*
2403 * This is the canonical list of information elements we care about,
2404 * the filter code also gives us all changes to the Microsoft OUI
2405 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2406 *
2407 * We implement beacon filtering in software since that means we can
2408 * avoid processing the frame here and in cfg80211, and userspace
2409 * will not be able to tell whether the hardware supports it or not.
2410 *
2411 * XXX: This list needs to be dynamic -- userspace needs to be able to
2412 * add items it requires. It also needs to be able to tell us to
2413 * look out for other vendor IEs.
2414 */
2415static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002416 (1ULL << WLAN_EID_COUNTRY) |
2417 (1ULL << WLAN_EID_ERP_INFO) |
2418 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2419 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2420 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002421 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002422
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002423static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002424 struct ieee80211_mgmt *mgmt,
2425 size_t len,
2426 struct ieee80211_rx_status *rx_status)
2427{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002428 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002429 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002430 size_t baselen;
2431 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002432 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002433 struct ieee80211_chanctx_conf *chanctx_conf;
2434 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002435 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002436 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002437 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002438 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002439 u8 *bssid;
2440
Johannes Berg66e67e42012-01-20 13:55:27 +01002441 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002442
Ester Kummerae6a44e2008-06-27 18:54:48 +03002443 /* Process beacon from the current BSS */
2444 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2445 if (baselen > len)
2446 return;
2447
Johannes Berg55de9082012-07-26 17:24:39 +02002448 rcu_read_lock();
2449 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2450 if (!chanctx_conf) {
2451 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002452 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002453 }
2454
2455 if (rx_status->freq != chanctx_conf->channel->center_freq) {
2456 rcu_read_unlock();
2457 return;
2458 }
2459 chan = chanctx_conf->channel;
2460 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002461
Johannes Berg66e67e42012-01-20 13:55:27 +01002462 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002463 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002464 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2465 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002466
Johannes Berg66e67e42012-01-20 13:55:27 +01002467 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2468 false);
2469 ifmgd->assoc_data->have_beacon = true;
2470 ifmgd->assoc_data->sent_assoc = false;
2471 /* continue assoc process */
2472 ifmgd->assoc_data->timeout = jiffies;
2473 run_again(ifmgd, ifmgd->assoc_data->timeout);
2474 return;
2475 }
2476
2477 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002478 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002479 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002480 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002481
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002482 /* Track average RSSI from the Beacon frames of the current AP */
2483 ifmgd->last_beacon_signal = rx_status->signal;
2484 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2485 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002486 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002487 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002488 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002489 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002490 } else {
2491 ifmgd->ave_beacon_signal =
2492 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2493 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2494 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002495 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002496 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002497
2498 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2499 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2500 int sig = ifmgd->ave_beacon_signal;
2501 int last_sig = ifmgd->last_ave_beacon_signal;
2502
2503 /*
2504 * if signal crosses either of the boundaries, invoke callback
2505 * with appropriate parameters
2506 */
2507 if (sig > ifmgd->rssi_max_thold &&
2508 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2509 ifmgd->last_ave_beacon_signal = sig;
2510 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2511 } else if (sig < ifmgd->rssi_min_thold &&
2512 (last_sig >= ifmgd->rssi_max_thold ||
2513 last_sig == 0)) {
2514 ifmgd->last_ave_beacon_signal = sig;
2515 drv_rssi_callback(local, RSSI_EVENT_LOW);
2516 }
2517 }
2518
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002519 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002520 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002521 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002522 int sig = ifmgd->ave_beacon_signal / 16;
2523 int last_event = ifmgd->last_cqm_event_signal;
2524 int thold = bss_conf->cqm_rssi_thold;
2525 int hyst = bss_conf->cqm_rssi_hyst;
2526 if (sig < thold &&
2527 (last_event == 0 || sig < last_event - hyst)) {
2528 ifmgd->last_cqm_event_signal = sig;
2529 ieee80211_cqm_rssi_notify(
2530 &sdata->vif,
2531 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2532 GFP_KERNEL);
2533 } else if (sig > thold &&
2534 (last_event == 0 || sig > last_event + hyst)) {
2535 ifmgd->last_cqm_event_signal = sig;
2536 ieee80211_cqm_rssi_notify(
2537 &sdata->vif,
2538 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2539 GFP_KERNEL);
2540 }
2541 }
2542
Johannes Bergb291ba12009-07-10 15:29:03 +02002543 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002544 mlme_dbg_ratelimited(sdata,
2545 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002546 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002547 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002548 ieee80211_run_deferred_scan(local);
2549 mutex_unlock(&local->mtx);
2550
Johannes Berg4e751842009-06-10 15:16:52 +02002551 mutex_lock(&local->iflist_mtx);
2552 ieee80211_recalc_ps(local, -1);
2553 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002554 }
2555
Johannes Bergb291ba12009-07-10 15:29:03 +02002556 /*
2557 * Push the beacon loss detection into the future since
2558 * we are processing a beacon from the AP just now.
2559 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002560 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002561
Johannes Bergd91f36d2009-04-16 13:17:26 +02002562 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2563 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2564 len - baselen, &elems,
2565 care_about_ies, ncrc);
2566
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302567 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002568 bool directed_tim = ieee80211_check_tim(elems.tim,
2569 elems.tim_len,
2570 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002571 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002572 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002573 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2574 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2575 ieee80211_hw_config(local,
2576 IEEE80211_CONF_CHANGE_PS);
2577 }
Kalle Valo572e0012009-02-10 17:09:31 +02002578 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302579 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002580 local->pspolling = true;
2581
2582 /*
2583 * Here is assumed that the driver will be
2584 * able to send ps-poll frame and receive a
2585 * response even though power save mode is
2586 * enabled, but some drivers might require
2587 * to disable power save here. This needs
2588 * to be investigated.
2589 */
2590 ieee80211_send_pspoll(local, sdata);
2591 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002592 }
2593 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002594
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002595 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002596 return;
2597 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002598 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002599
Johannes Berg7d257452012-07-06 17:37:43 +02002600 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2601 true);
2602
2603 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2604 elems.wmm_param_len))
2605 changed |= BSS_CHANGED_QOS;
2606
Johannes Berg7a5158e2008-10-08 10:59:33 +02002607 if (elems.erp_info && elems.erp_info_len >= 1) {
2608 erp_valid = true;
2609 erp_value = elems.erp_info[0];
2610 } else {
2611 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002612 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002613 changed |= ieee80211_handle_bss_capability(sdata,
2614 le16_to_cpu(mgmt->u.beacon.capab_info),
2615 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002616
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002617
Johannes Berg074d46d2012-03-15 19:45:16 +01002618 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berg444e3802012-09-30 17:08:35 +02002619 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg24398e32012-03-28 10:58:36 +02002620 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2621 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002622
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002623 if (elems.country_elem && elems.pwr_constr_elem &&
2624 mgmt->u.probe_resp.capab_info &
2625 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg55de9082012-07-26 17:24:39 +02002626 ieee80211_handle_pwr_constr(sdata, chan,
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002627 elems.country_elem,
2628 elems.country_elem_len,
2629 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002630
Johannes Berg471b3ef2007-12-28 14:32:58 +01002631 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002632}
2633
Johannes Berg1fa57d02010-06-10 10:21:32 +02002634void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2635 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002636{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002637 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002638 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002639 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002640 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002641 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002642 u16 fc;
2643
Jiri Bencf0706e82007-05-05 11:45:53 -07002644 rx_status = (struct ieee80211_rx_status *) skb->cb;
2645 mgmt = (struct ieee80211_mgmt *) skb->data;
2646 fc = le16_to_cpu(mgmt->frame_control);
2647
Johannes Berg77fdaa12009-07-07 03:45:17 +02002648 mutex_lock(&ifmgd->mtx);
2649
Johannes Berg66e67e42012-01-20 13:55:27 +01002650 switch (fc & IEEE80211_FCTL_STYPE) {
2651 case IEEE80211_STYPE_BEACON:
2652 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2653 break;
2654 case IEEE80211_STYPE_PROBE_RESP:
2655 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2656 break;
2657 case IEEE80211_STYPE_AUTH:
2658 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2659 break;
2660 case IEEE80211_STYPE_DEAUTH:
2661 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2662 break;
2663 case IEEE80211_STYPE_DISASSOC:
2664 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2665 break;
2666 case IEEE80211_STYPE_ASSOC_RESP:
2667 case IEEE80211_STYPE_REASSOC_RESP:
2668 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2669 break;
2670 case IEEE80211_STYPE_ACTION:
2671 switch (mgmt->u.action.category) {
2672 case WLAN_CATEGORY_SPECTRUM_MGMT:
2673 ieee80211_sta_process_chanswitch(sdata,
2674 &mgmt->u.action.u.chan_switch.sw_elem,
2675 (void *)ifmgd->associated->priv,
2676 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002677 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002678 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002679 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002680 mutex_unlock(&ifmgd->mtx);
2681
Johannes Berg66e67e42012-01-20 13:55:27 +01002682 switch (rma) {
2683 case RX_MGMT_NONE:
2684 /* no action */
2685 break;
2686 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002687 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002688 break;
2689 case RX_MGMT_CFG80211_DISASSOC:
2690 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2691 break;
2692 case RX_MGMT_CFG80211_RX_AUTH:
2693 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2694 break;
2695 case RX_MGMT_CFG80211_RX_ASSOC:
2696 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2697 break;
2698 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2699 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2700 break;
2701 default:
2702 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002703 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002704}
2705
Johannes Berg9c6bd792008-09-11 00:01:52 +02002706static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002707{
2708 struct ieee80211_sub_if_data *sdata =
2709 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002710 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002711 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002712
Johannes Berg5bb644a2009-05-17 11:40:42 +02002713 if (local->quiescing) {
2714 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2715 return;
2716 }
2717
Johannes Berg64592c82010-06-10 10:21:31 +02002718 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002719}
2720
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002721static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002722 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002723{
2724 struct ieee80211_local *local = sdata->local;
2725 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002726 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002727
Johannes Berg37ad3882012-02-24 13:50:54 +01002728 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2729 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002730 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002731
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002732 /*
2733 * must be outside lock due to cfg80211,
2734 * but that's not a problem.
2735 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002736 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002737
2738 mutex_lock(&local->mtx);
2739 ieee80211_recalc_idle(local);
2740 mutex_unlock(&local->mtx);
2741
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002742 mutex_lock(&ifmgd->mtx);
2743}
2744
Johannes Berg66e67e42012-01-20 13:55:27 +01002745static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2746{
2747 struct ieee80211_local *local = sdata->local;
2748 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2749 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2750
2751 lockdep_assert_held(&ifmgd->mtx);
2752
2753 if (WARN_ON_ONCE(!auth_data))
2754 return -EINVAL;
2755
Johannes Berg66e67e42012-01-20 13:55:27 +01002756 auth_data->tries++;
2757
2758 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002759 sdata_info(sdata, "authentication with %pM timed out\n",
2760 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002761
2762 /*
2763 * Most likely AP is not in the range so remove the
2764 * bss struct for that AP.
2765 */
2766 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2767
2768 return -ETIMEDOUT;
2769 }
2770
Johannes Berga1845fc2012-06-27 13:18:36 +02002771 drv_mgd_prepare_tx(local, sdata);
2772
Johannes Berg66e67e42012-01-20 13:55:27 +01002773 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002774 u16 trans = 1;
2775 u16 status = 0;
2776
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002777 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2778 auth_data->bss->bssid, auth_data->tries,
2779 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002780
2781 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002782
2783 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2784 trans = auth_data->sae_trans;
2785 status = auth_data->sae_status;
2786 auth_data->expected_transaction = trans;
2787 }
2788
2789 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2790 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002791 auth_data->bss->bssid,
2792 auth_data->bss->bssid, NULL, 0, 0);
2793 } else {
2794 const u8 *ssidie;
2795
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002796 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2797 auth_data->bss->bssid, auth_data->tries,
2798 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002799
2800 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2801 if (!ssidie)
2802 return -EINVAL;
2803 /*
2804 * Direct probe is sent to broadcast address as some APs
2805 * will not answer to direct packet in unassociated state.
2806 */
2807 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Bergfe94fe02012-07-30 12:26:34 +02002808 NULL, 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02002809 auth_data->bss->channel, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002810 }
2811
2812 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2813 run_again(ifmgd, auth_data->timeout);
2814
2815 return 0;
2816}
2817
2818static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2819{
2820 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2821 struct ieee80211_local *local = sdata->local;
2822
2823 lockdep_assert_held(&sdata->u.mgd.mtx);
2824
Johannes Berg66e67e42012-01-20 13:55:27 +01002825 assoc_data->tries++;
2826 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002827 sdata_info(sdata, "association with %pM timed out\n",
2828 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002829
2830 /*
2831 * Most likely AP is not in the range so remove the
2832 * bss struct for that AP.
2833 */
2834 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2835
2836 return -ETIMEDOUT;
2837 }
2838
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002839 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2840 assoc_data->bss->bssid, assoc_data->tries,
2841 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002842 ieee80211_send_assoc(sdata);
2843
2844 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2845 run_again(&sdata->u.mgd, assoc_data->timeout);
2846
2847 return 0;
2848}
2849
Johannes Berg1fa57d02010-06-10 10:21:32 +02002850void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002851{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002852 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002853 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002854
Johannes Berg77fdaa12009-07-07 03:45:17 +02002855 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002856
Johannes Berg66e67e42012-01-20 13:55:27 +01002857 if (ifmgd->auth_data &&
2858 time_after(jiffies, ifmgd->auth_data->timeout)) {
2859 if (ifmgd->auth_data->done) {
2860 /*
2861 * ok ... we waited for assoc but userspace didn't,
2862 * so let's just kill the auth data
2863 */
2864 ieee80211_destroy_auth_data(sdata, false);
2865 } else if (ieee80211_probe_auth(sdata)) {
2866 u8 bssid[ETH_ALEN];
2867
2868 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2869
2870 ieee80211_destroy_auth_data(sdata, false);
2871
2872 mutex_unlock(&ifmgd->mtx);
2873 cfg80211_send_auth_timeout(sdata->dev, bssid);
2874 mutex_lock(&ifmgd->mtx);
2875 }
2876 } else if (ifmgd->auth_data)
2877 run_again(ifmgd, ifmgd->auth_data->timeout);
2878
2879 if (ifmgd->assoc_data &&
2880 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2881 if (!ifmgd->assoc_data->have_beacon ||
2882 ieee80211_do_assoc(sdata)) {
2883 u8 bssid[ETH_ALEN];
2884
2885 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2886
2887 ieee80211_destroy_assoc_data(sdata, false);
2888
2889 mutex_unlock(&ifmgd->mtx);
2890 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2891 mutex_lock(&ifmgd->mtx);
2892 }
2893 } else if (ifmgd->assoc_data)
2894 run_again(ifmgd, ifmgd->assoc_data->timeout);
2895
Johannes Bergb291ba12009-07-10 15:29:03 +02002896 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2897 IEEE80211_STA_CONNECTION_POLL) &&
2898 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002899 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002900 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002901
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002902 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002903
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002904 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002905 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002906 else
Ben Greear180205b2011-02-04 15:30:24 -08002907 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002908
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002909 /* ACK received for nullfunc probing frame */
2910 if (!ifmgd->probe_send_count)
2911 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002912 else if (ifmgd->nullfunc_failed) {
2913 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002914 mlme_dbg(sdata,
2915 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
2916 bssid, ifmgd->probe_send_count,
2917 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002918 ieee80211_mgd_probe_ap_send(sdata);
2919 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002920 mlme_dbg(sdata,
2921 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
2922 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02002923 ieee80211_sta_connection_lost(sdata, bssid,
2924 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002925 }
2926 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002927 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002928 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002929 mlme_dbg(sdata,
2930 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
2931 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02002932 ieee80211_sta_connection_lost(sdata, bssid,
2933 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002934 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002935 mlme_dbg(sdata,
2936 "No probe response from AP %pM after %dms, try %d/%i\n",
2937 bssid, probe_wait_ms,
2938 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002939 ieee80211_mgd_probe_ap_send(sdata);
2940 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002941 /*
2942 * We actually lost the connection ... or did we?
2943 * Let's make sure!
2944 */
Ben Greearb38afa82010-10-07 16:12:06 -07002945 wiphy_debug(local->hw.wiphy,
2946 "%s: No probe response from AP %pM"
2947 " after %dms, disconnecting.\n",
2948 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002949 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002950
Johannes Berg95acac62011-07-12 12:30:59 +02002951 ieee80211_sta_connection_lost(sdata, bssid,
2952 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02002953 }
2954 }
2955
Johannes Berg77fdaa12009-07-07 03:45:17 +02002956 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002957
2958 mutex_lock(&local->mtx);
2959 ieee80211_recalc_idle(local);
2960 mutex_unlock(&local->mtx);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002961}
Johannes Berg0a51b272008-09-08 17:44:25 +02002962
Johannes Bergb291ba12009-07-10 15:29:03 +02002963static void ieee80211_sta_bcn_mon_timer(unsigned long data)
2964{
2965 struct ieee80211_sub_if_data *sdata =
2966 (struct ieee80211_sub_if_data *) data;
2967 struct ieee80211_local *local = sdata->local;
2968
2969 if (local->quiescing)
2970 return;
2971
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002972 ieee80211_queue_work(&sdata->local->hw,
2973 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002974}
2975
2976static void ieee80211_sta_conn_mon_timer(unsigned long data)
2977{
2978 struct ieee80211_sub_if_data *sdata =
2979 (struct ieee80211_sub_if_data *) data;
2980 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2981 struct ieee80211_local *local = sdata->local;
2982
2983 if (local->quiescing)
2984 return;
2985
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002986 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02002987}
2988
2989static void ieee80211_sta_monitor_work(struct work_struct *work)
2990{
2991 struct ieee80211_sub_if_data *sdata =
2992 container_of(work, struct ieee80211_sub_if_data,
2993 u.mgd.monitor_work);
2994
2995 ieee80211_mgd_probe_ap(sdata, false);
2996}
2997
Johannes Berga1678f82008-09-11 00:01:47 +02002998static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
2999{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003000 u32 flags;
3001
Kalle Vaload935682009-04-19 08:47:19 +03003002 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003003 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003004
Johannes Bergb291ba12009-07-10 15:29:03 +02003005 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003006 flags = sdata->local->hw.flags;
3007 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3008 ieee80211_queue_work(&sdata->local->hw,
3009 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003010 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003011 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003012 }
Johannes Berga1678f82008-09-11 00:01:47 +02003013}
3014
Johannes Berg5bb644a2009-05-17 11:40:42 +02003015#ifdef CONFIG_PM
3016void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3017{
3018 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3019
3020 /*
3021 * we need to use atomic bitops for the running bits
3022 * only because both timers might fire at the same
3023 * time -- the code here is properly synchronised.
3024 */
3025
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003026 cancel_work_sync(&ifmgd->request_smps_work);
3027
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003028 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003029 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003030 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003031 if (del_timer_sync(&ifmgd->timer))
3032 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3033
3034 cancel_work_sync(&ifmgd->chswitch_work);
3035 if (del_timer_sync(&ifmgd->chswitch_timer))
3036 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003037
Johannes Bergb291ba12009-07-10 15:29:03 +02003038 /* these will just be re-established on connection */
3039 del_timer_sync(&ifmgd->conn_mon_timer);
3040 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003041}
3042
3043void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3044{
3045 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3046
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303047 if (!ifmgd->associated)
3048 return;
3049
Johannes Berg95acac62011-07-12 12:30:59 +02003050 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3051 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3052 mutex_lock(&ifmgd->mtx);
3053 if (ifmgd->associated) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003054 mlme_dbg(sdata,
3055 "driver requested disconnect after resume\n");
Johannes Berg95acac62011-07-12 12:30:59 +02003056 ieee80211_sta_connection_lost(sdata,
3057 ifmgd->associated->bssid,
3058 WLAN_REASON_UNSPECIFIED);
3059 mutex_unlock(&ifmgd->mtx);
3060 return;
3061 }
3062 mutex_unlock(&ifmgd->mtx);
3063 }
3064
Johannes Berg5bb644a2009-05-17 11:40:42 +02003065 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3066 add_timer(&ifmgd->timer);
3067 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3068 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003069 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003070
3071 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003072 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003073 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003074}
3075#endif
3076
Johannes Berg9c6bd792008-09-11 00:01:52 +02003077/* interface setup */
3078void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3079{
Johannes Berg46900292009-02-15 12:44:28 +01003080 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003081
Johannes Berg46900292009-02-15 12:44:28 +01003082 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003083 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003084 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003085 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3086 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003087 INIT_WORK(&ifmgd->csa_connection_drop_work,
3088 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003089 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003090 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003091 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003092 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3093 (unsigned long) sdata);
3094 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3095 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003096 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303097 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003098
Johannes Bergab1faea2009-07-01 21:41:17 +02003099 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303100 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003101 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3102 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003103
Johannes Berg77fdaa12009-07-07 03:45:17 +02003104 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003105
3106 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3107 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3108 else
3109 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003110}
3111
3112/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003113void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3114{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003115 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003116
3117 /* Restart STA timers */
3118 rcu_read_lock();
3119 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3120 ieee80211_restart_sta_timer(sdata);
3121 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003122}
Johannes Berg10f644a2009-04-16 13:17:25 +02003123
3124int ieee80211_max_network_latency(struct notifier_block *nb,
3125 unsigned long data, void *dummy)
3126{
3127 s32 latency_usec = (s32) data;
3128 struct ieee80211_local *local =
3129 container_of(nb, struct ieee80211_local,
3130 network_latency_notifier);
3131
3132 mutex_lock(&local->iflist_mtx);
3133 ieee80211_recalc_ps(local, latency_usec);
3134 mutex_unlock(&local->iflist_mtx);
3135
3136 return 0;
3137}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003138
Johannes Bergb17166a2012-07-27 11:41:27 +02003139static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3140 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003141{
3142 struct ieee80211_local *local = sdata->local;
3143 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003144 int ht_cfreq;
3145 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3146 const u8 *ht_oper_ie;
3147 const struct ieee80211_ht_operation *ht_oper = NULL;
3148 struct ieee80211_supported_band *sband;
Johannes Berga1cf7752012-03-08 15:02:07 +01003149
Johannes Berg24398e32012-03-28 10:58:36 +02003150 sband = local->hw.wiphy->bands[cbss->channel->band];
3151
3152 ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
3153
3154 if (sband->ht_cap.ht_supported) {
3155 ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
3156 cbss->information_elements,
3157 cbss->len_information_elements);
3158 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3159 ht_oper = (void *)(ht_oper_ie + 2);
3160 }
3161
3162 if (ht_oper) {
3163 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3164 cbss->channel->band);
3165 /* check that channel matches the right operating channel */
3166 if (cbss->channel->center_freq != ht_cfreq) {
3167 /*
3168 * It's possible that some APs are confused here;
3169 * Netgear WNDR3700 sometimes reports 4 higher than
3170 * the actual channel in association responses, but
3171 * since we look at probe response/beacon data here
3172 * it should be OK.
3173 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003174 sdata_info(sdata,
3175 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3176 cbss->channel->center_freq,
3177 ht_cfreq, ht_oper->primary_chan,
3178 cbss->channel->band);
Johannes Berg24398e32012-03-28 10:58:36 +02003179 ht_oper = NULL;
3180 }
3181 }
3182
3183 if (ht_oper) {
Johannes Berg04ecd252012-09-11 14:34:12 +02003184 const u8 *ht_cap_ie;
3185 const struct ieee80211_ht_cap *ht_cap;
3186 u8 chains = 1;
3187
Johannes Berg24398e32012-03-28 10:58:36 +02003188 channel_type = NL80211_CHAN_HT20;
3189
3190 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3191 switch (ht_oper->ht_param &
3192 IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3193 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3194 channel_type = NL80211_CHAN_HT40PLUS;
3195 break;
3196 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3197 channel_type = NL80211_CHAN_HT40MINUS;
3198 break;
3199 }
3200 }
Johannes Berg04ecd252012-09-11 14:34:12 +02003201
3202 ht_cap_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY,
3203 cbss->information_elements,
3204 cbss->len_information_elements);
3205 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3206 ht_cap = (void *)(ht_cap_ie + 2);
3207 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3208 }
3209 sdata->needed_rx_chains = min(chains, local->rx_chains);
3210 } else {
3211 sdata->needed_rx_chains = 1;
Johannes Berg24398e32012-03-28 10:58:36 +02003212 }
3213
Johannes Berg04ecd252012-09-11 14:34:12 +02003214 /* will change later if needed */
3215 sdata->smps_mode = IEEE80211_SMPS_OFF;
3216
Johannes Berg55de9082012-07-26 17:24:39 +02003217 ieee80211_vif_release_channel(sdata);
3218 return ieee80211_vif_use_channel(sdata, cbss->channel, channel_type,
3219 IEEE80211_CHANCTX_SHARED);
Johannes Bergb17166a2012-07-27 11:41:27 +02003220}
3221
3222static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3223 struct cfg80211_bss *cbss, bool assoc)
3224{
3225 struct ieee80211_local *local = sdata->local;
3226 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3227 struct ieee80211_bss *bss = (void *)cbss->priv;
3228 struct sta_info *new_sta = NULL;
3229 bool have_sta = false;
3230 int err;
3231
3232 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3233 return -EINVAL;
3234
3235 if (assoc) {
3236 rcu_read_lock();
3237 have_sta = sta_info_get(sdata, cbss->bssid);
3238 rcu_read_unlock();
3239 }
3240
3241 if (!have_sta) {
3242 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3243 if (!new_sta)
3244 return -ENOMEM;
3245 }
3246
3247 mutex_lock(&local->mtx);
3248 ieee80211_recalc_idle(sdata->local);
3249 mutex_unlock(&local->mtx);
3250
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003251 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003252 u32 rates = 0, basic_rates = 0;
3253 bool have_higher_than_11mbit;
3254 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003255 struct ieee80211_supported_band *sband;
3256
3257 sband = local->hw.wiphy->bands[cbss->channel->band];
3258
3259 err = ieee80211_prep_channel(sdata, cbss);
3260 if (err) {
3261 sta_info_free(local, new_sta);
3262 return err;
3263 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003264
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003265 ieee80211_get_rates(sband, bss->supp_rates,
3266 bss->supp_rates_len,
3267 &rates, &basic_rates,
3268 &have_higher_than_11mbit,
3269 &min_rate, &min_rate_index);
3270
3271 /*
3272 * This used to be a workaround for basic rates missing
3273 * in the association response frame. Now that we no
3274 * longer use the basic rates from there, it probably
3275 * doesn't happen any more, but keep the workaround so
3276 * in case some *other* APs are buggy in different ways
3277 * we can connect -- with a warning.
3278 */
3279 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003280 sdata_info(sdata,
3281 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003282 basic_rates = BIT(min_rate_index);
3283 }
3284
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003285 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003286 sdata->vif.bss_conf.basic_rates = basic_rates;
3287
3288 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003289 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003290 have_higher_than_11mbit)
3291 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3292 else
3293 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3294
3295 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3296
Johannes Berg8c358bc2012-05-22 22:13:05 +02003297 /* set timing information */
3298 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3299 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3300 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3301
3302 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003303 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003304 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3305 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003306
3307 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003308 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003309
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003310 err = sta_info_insert(new_sta);
3311 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003312 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003313 sdata_info(sdata,
3314 "failed to insert STA entry for the AP (error %d)\n",
3315 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003316 return err;
3317 }
3318 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003319 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003320
3321 return 0;
3322}
3323
Johannes Berg77fdaa12009-07-07 03:45:17 +02003324/* config hooks */
3325int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3326 struct cfg80211_auth_request *req)
3327{
Johannes Berg66e67e42012-01-20 13:55:27 +01003328 struct ieee80211_local *local = sdata->local;
3329 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3330 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003331 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003332 int err;
3333
3334 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003335
3336 switch (req->auth_type) {
3337 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3338 auth_alg = WLAN_AUTH_OPEN;
3339 break;
3340 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003341 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003342 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003343 auth_alg = WLAN_AUTH_SHARED_KEY;
3344 break;
3345 case NL80211_AUTHTYPE_FT:
3346 auth_alg = WLAN_AUTH_FT;
3347 break;
3348 case NL80211_AUTHTYPE_NETWORK_EAP:
3349 auth_alg = WLAN_AUTH_LEAP;
3350 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003351 case NL80211_AUTHTYPE_SAE:
3352 auth_alg = WLAN_AUTH_SAE;
3353 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003354 default:
3355 return -EOPNOTSUPP;
3356 }
3357
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003358 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3359 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003360 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003361 return -ENOMEM;
3362
Johannes Berg66e67e42012-01-20 13:55:27 +01003363 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003364
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003365 if (req->sae_data_len >= 4) {
3366 __le16 *pos = (__le16 *) req->sae_data;
3367 auth_data->sae_trans = le16_to_cpu(pos[0]);
3368 auth_data->sae_status = le16_to_cpu(pos[1]);
3369 memcpy(auth_data->data, req->sae_data + 4,
3370 req->sae_data_len - 4);
3371 auth_data->data_len += req->sae_data_len - 4;
3372 }
3373
Johannes Berg77fdaa12009-07-07 03:45:17 +02003374 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003375 memcpy(&auth_data->data[auth_data->data_len],
3376 req->ie, req->ie_len);
3377 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003378 }
3379
Johannes Bergfffd0932009-07-08 14:22:54 +02003380 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003381 auth_data->key_len = req->key_len;
3382 auth_data->key_idx = req->key_idx;
3383 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003384 }
3385
Johannes Berg66e67e42012-01-20 13:55:27 +01003386 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003387
Johannes Berg66e67e42012-01-20 13:55:27 +01003388 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003389
Johannes Berg66e67e42012-01-20 13:55:27 +01003390 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003391
Johannes Berg66e67e42012-01-20 13:55:27 +01003392 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3393 ifmgd->assoc_data) {
3394 err = -EBUSY;
3395 goto err_free;
3396 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003397
Johannes Berg66e67e42012-01-20 13:55:27 +01003398 if (ifmgd->auth_data)
3399 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003400
Johannes Berg66e67e42012-01-20 13:55:27 +01003401 /* prep auth_data so we don't go into idle on disassoc */
3402 ifmgd->auth_data = auth_data;
3403
3404 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003405 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003406
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003407 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003408
Johannes Berga1cf7752012-03-08 15:02:07 +01003409 err = ieee80211_prep_connection(sdata, req->bss, false);
3410 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003411 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003412
Johannes Berg66e67e42012-01-20 13:55:27 +01003413 err = ieee80211_probe_auth(sdata);
3414 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003415 sta_info_destroy_addr(sdata, req->bss->bssid);
3416 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003417 }
3418
Johannes Berg66e67e42012-01-20 13:55:27 +01003419 /* hold our own reference */
3420 cfg80211_ref_bss(auth_data->bss);
3421 err = 0;
3422 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003423
Johannes Berg66e67e42012-01-20 13:55:27 +01003424 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003425 memset(ifmgd->bssid, 0, ETH_ALEN);
3426 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003427 ifmgd->auth_data = NULL;
3428 err_free:
3429 kfree(auth_data);
3430 out_unlock:
3431 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003432
Johannes Berg66e67e42012-01-20 13:55:27 +01003433 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003434}
3435
Johannes Berg77fdaa12009-07-07 03:45:17 +02003436int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3437 struct cfg80211_assoc_request *req)
3438{
Johannes Berg66e67e42012-01-20 13:55:27 +01003439 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003440 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003441 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003442 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003443 struct ieee80211_supported_band *sband;
Johannes Berg9dde6422012-05-16 23:43:19 +02003444 const u8 *ssidie, *ht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003445 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003446
Johannes Berg66e67e42012-01-20 13:55:27 +01003447 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3448 if (!ssidie)
3449 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003450
Johannes Berg66e67e42012-01-20 13:55:27 +01003451 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3452 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003453 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003454
Johannes Berg66e67e42012-01-20 13:55:27 +01003455 mutex_lock(&ifmgd->mtx);
3456
3457 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003458 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003459
3460 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3461 err = -EBUSY;
3462 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003463 }
3464
Johannes Berg66e67e42012-01-20 13:55:27 +01003465 if (ifmgd->assoc_data) {
3466 err = -EBUSY;
3467 goto err_free;
3468 }
3469
3470 if (ifmgd->auth_data) {
3471 bool match;
3472
3473 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003474 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003475 ieee80211_destroy_auth_data(sdata, match);
3476 }
3477
3478 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003479
3480 /*
3481 * keep only the 40 MHz disable bit set as it might have
3482 * been set during authentication already, all other bits
3483 * should be reset for a new connection
3484 */
3485 ifmgd->flags &= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003486
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003487 ifmgd->beacon_crc_valid = false;
3488
Johannes Bergde5036a2012-03-08 15:02:03 +01003489 /*
3490 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3491 * We still associate in non-HT mode (11a/b/g) if any one of these
3492 * ciphers is configured as pairwise.
3493 * We can set this to true for non-11n hardware, that'll be checked
3494 * separately along with the peer capabilities.
3495 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003496 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003497 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3498 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003499 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003500 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003501 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003502 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003503 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003504 }
3505 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003506
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003507 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Ben Greearef96a8422011-11-18 11:32:00 -08003508 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003509 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3510 }
Ben Greearef96a8422011-11-18 11:32:00 -08003511
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003512 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3513 sband = local->hw.wiphy->bands[req->bss->channel->band];
3514 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003515 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003516 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg1b49de22012-07-27 10:29:14 +02003517 if (!bss->wmm_used)
3518 netdev_info(sdata->dev,
3519 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003520 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003521
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003522 /* disable VHT if we don't support it or the AP doesn't use WMM */
3523 if (!sband->vht_cap.vht_supported ||
3524 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3525 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003526 if (!bss->wmm_used)
3527 netdev_info(sdata->dev,
3528 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003529 }
3530
Ben Greearef96a8422011-11-18 11:32:00 -08003531 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3532 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3533 sizeof(ifmgd->ht_capa_mask));
3534
Johannes Berg77fdaa12009-07-07 03:45:17 +02003535 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003536 memcpy(assoc_data->ie, req->ie, req->ie_len);
3537 assoc_data->ie_len = req->ie_len;
3538 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003539
Johannes Berg66e67e42012-01-20 13:55:27 +01003540 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003541
Johannes Bergaf6b6372009-12-23 13:15:35 +01003542 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3543 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003544 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003545 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003546 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003547 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003548 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003549
Johannes Berg66e67e42012-01-20 13:55:27 +01003550 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003551 assoc_data->wmm = bss->wmm_used &&
3552 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003553 assoc_data->supp_rates = bss->supp_rates;
3554 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003555
3556 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3557 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3558 assoc_data->ap_ht_param =
3559 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3560 else
3561 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg63f170e2009-12-23 13:15:33 +01003562
Kalle Valoab133152010-01-12 10:42:31 +02003563 if (bss->wmm_used && bss->uapsd_supported &&
3564 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003565 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003566 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3567 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003568 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003569 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3570 }
3571
Johannes Berg66e67e42012-01-20 13:55:27 +01003572 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3573 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003574
Johannes Berg77fdaa12009-07-07 03:45:17 +02003575 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003576 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003577
3578 if (req->use_mfp) {
3579 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3580 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3581 } else {
3582 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3583 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3584 }
3585
3586 if (req->crypto.control_port)
3587 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3588 else
3589 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3590
Johannes Berga621fa42010-08-27 14:26:54 +03003591 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3592 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3593
Johannes Berg66e67e42012-01-20 13:55:27 +01003594 /* kick off associate process */
3595
3596 ifmgd->assoc_data = assoc_data;
3597
Johannes Berga1cf7752012-03-08 15:02:07 +01003598 err = ieee80211_prep_connection(sdata, req->bss, true);
3599 if (err)
3600 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003601
3602 if (!bss->dtim_period &&
3603 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3604 /*
3605 * Wait up to one beacon interval ...
3606 * should this be more if we miss one?
3607 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003608 sdata_info(sdata, "waiting for beacon from %pM\n",
3609 ifmgd->bssid);
Johannes Berg3f9768a2012-03-28 21:02:46 +02003610 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +01003611 } else {
3612 assoc_data->have_beacon = true;
3613 assoc_data->sent_assoc = false;
3614 assoc_data->timeout = jiffies;
3615 }
3616 run_again(ifmgd, assoc_data->timeout);
3617
Paul Stewartfcff4f12012-02-23 17:59:53 -08003618 if (bss->corrupt_data) {
3619 char *corrupt_type = "data";
3620 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3621 if (bss->corrupt_data &
3622 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3623 corrupt_type = "beacon and probe response";
3624 else
3625 corrupt_type = "beacon";
3626 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3627 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003628 sdata_info(sdata, "associating with AP with corrupt %s\n",
3629 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08003630 }
3631
Johannes Berg66e67e42012-01-20 13:55:27 +01003632 err = 0;
3633 goto out;
3634 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003635 memset(ifmgd->bssid, 0, ETH_ALEN);
3636 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003637 ifmgd->assoc_data = NULL;
3638 err_free:
3639 kfree(assoc_data);
3640 out:
3641 mutex_unlock(&ifmgd->mtx);
3642
3643 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003644}
3645
3646int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003647 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003648{
3649 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003650 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003651
Johannes Berg77fdaa12009-07-07 03:45:17 +02003652 mutex_lock(&ifmgd->mtx);
3653
Johannes Berg37ad3882012-02-24 13:50:54 +01003654 if (ifmgd->auth_data) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003655 ieee80211_destroy_auth_data(sdata, false);
Johannes Bergaf6b6372009-12-23 13:15:35 +01003656 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003657 return 0;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003658 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003659
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003660 sdata_info(sdata,
3661 "deauthenticating from %pM by local choice (reason=%d)\n",
3662 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003663
Johannes Berg37ad3882012-02-24 13:50:54 +01003664 if (ifmgd->associated &&
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003665 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
Johannes Berg37ad3882012-02-24 13:50:54 +01003666 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
Johannes Berg5fef7db2012-02-24 13:50:52 +01003667 req->reason_code, true, frame_buf);
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003668 } else {
3669 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01003670 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3671 IEEE80211_STYPE_DEAUTH,
3672 req->reason_code, true,
3673 frame_buf);
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003674 }
3675
Johannes Berg37ad3882012-02-24 13:50:54 +01003676 mutex_unlock(&ifmgd->mtx);
3677
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003678 __cfg80211_send_deauth(sdata->dev, frame_buf,
3679 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003680
Johannes Berg7da7cc12010-08-05 17:02:38 +02003681 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003682 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003683 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003684
Johannes Berg77fdaa12009-07-07 03:45:17 +02003685 return 0;
3686}
3687
3688int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003689 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003690{
3691 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003692 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003693 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003694
Johannes Berg77fdaa12009-07-07 03:45:17 +02003695 mutex_lock(&ifmgd->mtx);
3696
Johannes Berg8d8b2612009-07-25 11:58:36 +02003697 /*
3698 * cfg80211 should catch this ... but it's racy since
3699 * we can receive a disassoc frame, process it, hand it
3700 * to cfg80211 while that's in a locked section already
3701 * trying to tell us that the user wants to disconnect.
3702 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003703 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003704 mutex_unlock(&ifmgd->mtx);
3705 return -ENOLINK;
3706 }
3707
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003708 sdata_info(sdata,
3709 "disassociating from %pM by local choice (reason=%d)\n",
3710 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003711
Jouni Malinene69e95d2010-03-29 23:29:31 -07003712 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01003713 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
3714 req->reason_code, !req->local_state_change,
3715 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003716 mutex_unlock(&ifmgd->mtx);
3717
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003718 __cfg80211_send_disassoc(sdata->dev, frame_buf,
3719 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01003720
Johannes Berg7da7cc12010-08-05 17:02:38 +02003721 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003722 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003723 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003724
Johannes Berg77fdaa12009-07-07 03:45:17 +02003725 return 0;
3726}
Jouni Malinen026331c2010-02-15 12:53:10 +02003727
Eliad Pellerafa762f2012-04-23 14:45:15 +03003728void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01003729{
3730 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3731
3732 mutex_lock(&ifmgd->mtx);
3733 if (ifmgd->assoc_data)
3734 ieee80211_destroy_assoc_data(sdata, false);
3735 if (ifmgd->auth_data)
3736 ieee80211_destroy_auth_data(sdata, false);
3737 del_timer_sync(&ifmgd->timer);
3738 mutex_unlock(&ifmgd->mtx);
3739}
3740
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003741void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3742 enum nl80211_cqm_rssi_threshold_event rssi_event,
3743 gfp_t gfp)
3744{
3745 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3746
Johannes Bergb5878a22010-04-07 16:48:40 +02003747 trace_api_cqm_rssi_notify(sdata, rssi_event);
3748
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003749 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3750}
3751EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);