blob: 61614461e089a9690e07009aaff4727041853fcb [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
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200823static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
824 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 Berg1ea6f9c2012-10-24 10:59:25 +0200835 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200836
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)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200876 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200877
878 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
879
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200880 if (sdata->ap_power_level == new_ap_level)
881 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200882
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);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200887 sdata->ap_power_level = new_ap_level;
888 if (__ieee80211_recalc_txpower(sdata))
889 return BSS_CHANGED_TXPOWER;
890 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200891}
892
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300893void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
894{
895 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
896 struct ieee80211_local *local = sdata->local;
897 struct ieee80211_conf *conf = &local->hw.conf;
898
899 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
900 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
901 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
902
903 local->disable_dynamic_ps = false;
904 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
905}
906EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
907
908void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
909{
910 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
911 struct ieee80211_local *local = sdata->local;
912 struct ieee80211_conf *conf = &local->hw.conf;
913
914 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
915 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
916 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
917
918 local->disable_dynamic_ps = true;
919 conf->dynamic_ps_timeout = 0;
920 del_timer_sync(&local->dynamic_ps_timer);
921 ieee80211_queue_work(&local->hw,
922 &local->dynamic_ps_enable_work);
923}
924EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
925
Johannes Berg965beda2009-04-16 13:17:24 +0200926/* powersave */
927static void ieee80211_enable_ps(struct ieee80211_local *local,
928 struct ieee80211_sub_if_data *sdata)
929{
930 struct ieee80211_conf *conf = &local->hw.conf;
931
Johannes Bergd5edaed2009-04-22 23:02:51 +0200932 /*
933 * If we are scanning right now then the parameters will
934 * take effect when scan finishes.
935 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200936 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200937 return;
938
Johannes Berg965beda2009-04-16 13:17:24 +0200939 if (conf->dynamic_ps_timeout > 0 &&
940 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
941 mod_timer(&local->dynamic_ps_timer, jiffies +
942 msecs_to_jiffies(conf->dynamic_ps_timeout));
943 } else {
944 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
945 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530946
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200947 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
948 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
949 return;
950
951 conf->flags |= IEEE80211_CONF_PS;
952 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200953 }
954}
955
956static void ieee80211_change_ps(struct ieee80211_local *local)
957{
958 struct ieee80211_conf *conf = &local->hw.conf;
959
960 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200961 ieee80211_enable_ps(local, local->ps_sdata);
962 } else if (conf->flags & IEEE80211_CONF_PS) {
963 conf->flags &= ~IEEE80211_CONF_PS;
964 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
965 del_timer_sync(&local->dynamic_ps_timer);
966 cancel_work_sync(&local->dynamic_ps_enable_work);
967 }
968}
969
Jason Young808118c2011-03-10 16:43:19 -0800970static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
971{
972 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
973 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200974 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800975
976 if (!mgd->powersave)
977 return false;
978
Johannes Berg05cb9102011-10-28 11:59:47 +0200979 if (mgd->broken_ap)
980 return false;
981
Jason Young808118c2011-03-10 16:43:19 -0800982 if (!mgd->associated)
983 return false;
984
Jason Young808118c2011-03-10 16:43:19 -0800985 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
986 IEEE80211_STA_CONNECTION_POLL))
987 return false;
988
989 rcu_read_lock();
990 sta = sta_info_get(sdata, mgd->bssid);
991 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200992 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -0800993 rcu_read_unlock();
994
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200995 return authorized;
Jason Young808118c2011-03-10 16:43:19 -0800996}
997
Johannes Berg965beda2009-04-16 13:17:24 +0200998/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +0200999void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001000{
1001 struct ieee80211_sub_if_data *sdata, *found = NULL;
1002 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001003 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001004
1005 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1006 local->ps_sdata = NULL;
1007 return;
1008 }
1009
1010 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001011 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001012 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301013 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1014 /* If an AP vif is found, then disable PS
1015 * by setting the count to zero thereby setting
1016 * ps_sdata to NULL.
1017 */
1018 count = 0;
1019 break;
1020 }
Johannes Berg965beda2009-04-16 13:17:24 +02001021 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1022 continue;
1023 found = sdata;
1024 count++;
1025 }
1026
Jason Young808118c2011-03-10 16:43:19 -08001027 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001028 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001029 s32 beaconint_us;
1030
1031 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001032 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001033
1034 beaconint_us = ieee80211_tu_to_usec(
1035 found->vif.bss_conf.beacon_int);
1036
Juuso Oikarinenff616382010-06-09 09:51:52 +03001037 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001038 if (timeout < 0) {
1039 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001040 * Go to full PSM if the user configures a very low
1041 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001042 * The 2000 second value is there for compatibility
1043 * until the PM_QOS_NETWORK_LATENCY is configured
1044 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001045 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001046 if (latency > (1900 * USEC_PER_MSEC) &&
1047 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001048 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001049 else
1050 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001051 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001052 local->dynamic_ps_user_timeout = timeout;
1053 if (!local->disable_dynamic_ps)
1054 conf->dynamic_ps_timeout =
1055 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001056
Johannes Berg04fe2032009-04-22 18:44:37 +02001057 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001058 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001059 } else {
Johannes Berg56007a02010-01-26 14:19:52 +01001060 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +02001061 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001062 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +02001063
Johannes Berg56007a02010-01-26 14:19:52 +01001064 bss = (void *)found->u.mgd.associated->priv;
1065 dtimper = bss->dtim_period;
1066
1067 /* If the TIM IE is invalid, pretend the value is 1 */
1068 if (!dtimper)
1069 dtimper = 1;
1070 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001071 maxslp = min_t(int, dtimper,
1072 latency / beaconint_us);
1073
Johannes Berg9ccebe62009-04-23 10:32:36 +02001074 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001075 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001076 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001077 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001078 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001079 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001080 }
Johannes Berg965beda2009-04-16 13:17:24 +02001081
1082 ieee80211_change_ps(local);
1083}
1084
Eliad Pellerab095872012-07-27 12:33:22 +03001085void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1086{
1087 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1088
1089 if (sdata->vif.bss_conf.ps != ps_allowed) {
1090 sdata->vif.bss_conf.ps = ps_allowed;
1091 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1092 }
1093}
1094
Johannes Berg965beda2009-04-16 13:17:24 +02001095void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1096{
1097 struct ieee80211_local *local =
1098 container_of(work, struct ieee80211_local,
1099 dynamic_ps_disable_work);
1100
1101 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1102 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1103 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1104 }
1105
1106 ieee80211_wake_queues_by_reason(&local->hw,
1107 IEEE80211_QUEUE_STOP_REASON_PS);
1108}
1109
1110void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1111{
1112 struct ieee80211_local *local =
1113 container_of(work, struct ieee80211_local,
1114 dynamic_ps_enable_work);
1115 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001116 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301117 unsigned long flags;
1118 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001119
1120 /* can only happen when PS was just disabled anyway */
1121 if (!sdata)
1122 return;
1123
Christian Lamparter5e340692011-06-30 21:08:43 +02001124 ifmgd = &sdata->u.mgd;
1125
Johannes Berg965beda2009-04-16 13:17:24 +02001126 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1127 return;
1128
Arik Nemtsov77b70232011-06-26 12:06:54 +03001129 if (!local->disable_dynamic_ps &&
1130 local->hw.conf.dynamic_ps_timeout > 0) {
1131 /* don't enter PS if TX frames are pending */
1132 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301133 mod_timer(&local->dynamic_ps_timer, jiffies +
1134 msecs_to_jiffies(
1135 local->hw.conf.dynamic_ps_timeout));
1136 return;
1137 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001138
1139 /*
1140 * transmission can be stopped by others which leads to
1141 * dynamic_ps_timer expiry. Postpone the ps timer if it
1142 * is not the actual idle state.
1143 */
1144 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1145 for (q = 0; q < local->hw.queues; q++) {
1146 if (local->queue_stop_reasons[q]) {
1147 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1148 flags);
1149 mod_timer(&local->dynamic_ps_timer, jiffies +
1150 msecs_to_jiffies(
1151 local->hw.conf.dynamic_ps_timeout));
1152 return;
1153 }
1154 }
1155 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301156 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301157
Vivek Natarajan375177b2010-02-09 14:50:28 +05301158 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301159 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301160 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001161
Vivek Natarajane8306f92011-04-06 11:41:10 +05301162 if (drv_tx_frames_pending(local))
1163 mod_timer(&local->dynamic_ps_timer, jiffies +
1164 msecs_to_jiffies(
1165 local->hw.conf.dynamic_ps_timeout));
1166 else {
1167 ieee80211_send_nullfunc(local, sdata, 1);
1168 /* Flush to get the tx status of nullfunc frame */
1169 drv_flush(local, false);
1170 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301171 }
1172
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001173 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1174 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301175 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1176 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1177 local->hw.conf.flags |= IEEE80211_CONF_PS;
1178 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1179 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301180
Arik Nemtsov77b70232011-06-26 12:06:54 +03001181 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1182 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001183}
1184
1185void ieee80211_dynamic_ps_timer(unsigned long data)
1186{
1187 struct ieee80211_local *local = (void *) data;
1188
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001189 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001190 return;
1191
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001192 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001193}
1194
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001195/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001196static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001197 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001198 u8 *wmm_param, size_t wmm_param_len)
1199{
Jiri Bencf0706e82007-05-05 11:45:53 -07001200 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001201 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001202 size_t left;
1203 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001204 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001205
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001206 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001207 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001208
Johannes Berg32c50572012-03-28 11:04:29 +02001209 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001210 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001211
1212 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001213 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001214
Jiri Bencf0706e82007-05-05 11:45:53 -07001215 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001216 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001217
1218 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001219 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001220
Jiri Bencf0706e82007-05-05 11:45:53 -07001221 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001222 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001223 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001224 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001225
1226 pos = wmm_param + 8;
1227 left = wmm_param_len - 8;
1228
1229 memset(&params, 0, sizeof(params));
1230
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001231 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001232 for (; left >= 4; left -= 4, pos += 4) {
1233 int aci = (pos[0] >> 5) & 0x03;
1234 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001235 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001236 int queue;
1237
1238 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001239 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001240 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001241 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001242 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001243 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1244 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001245 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001246 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001247 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001248 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001249 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001250 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1251 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001252 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001253 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001254 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001255 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001256 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001257 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1258 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001259 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001260 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001261 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001262 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001263 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001264 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001265 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1266 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001267 break;
1268 }
1269
1270 params.aifs = pos[0] & 0x0f;
1271 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1272 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001273 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001274 params.uapsd = uapsd;
1275
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001276 mlme_dbg(sdata,
1277 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1278 queue, aci, acm,
1279 params.aifs, params.cw_min, params.cw_max,
1280 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001281 sdata->tx_conf[queue] = params;
1282 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001283 sdata_err(sdata,
1284 "failed to set TX queue parameters for queue %d\n",
1285 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001286 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001287
1288 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001289 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001290 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001291}
1292
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001293static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1294{
1295 lockdep_assert_held(&sdata->local->mtx);
1296
1297 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1298 IEEE80211_STA_BEACON_POLL);
1299 ieee80211_run_deferred_scan(sdata->local);
1300}
1301
1302static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1303{
1304 mutex_lock(&sdata->local->mtx);
1305 __ieee80211_stop_poll(sdata);
1306 mutex_unlock(&sdata->local->mtx);
1307}
1308
Johannes Berg7a5158e2008-10-08 10:59:33 +02001309static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1310 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001311{
Johannes Bergbda39332008-10-11 01:51:51 +02001312 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001313 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001314 bool use_protection;
1315 bool use_short_preamble;
1316 bool use_short_slot;
1317
1318 if (erp_valid) {
1319 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1320 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1321 } else {
1322 use_protection = false;
1323 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1324 }
1325
1326 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001327 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001328 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001329
Johannes Berg471b3ef2007-12-28 14:32:58 +01001330 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001331 bss_conf->use_cts_prot = use_protection;
1332 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001333 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001334
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001335 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001336 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001337 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001338 }
Daniel Draked9430a32007-07-27 15:43:24 +02001339
Johannes Berg7a5158e2008-10-08 10:59:33 +02001340 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001341 bss_conf->use_short_slot = use_short_slot;
1342 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001343 }
1344
1345 return changed;
1346}
1347
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001348static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001349 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001350 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001351{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001352 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001353 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001354 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001355
Johannes Bergae5eb022008-10-14 16:58:37 +02001356 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001357 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001358 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001359
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001360 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1361 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1362
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001363 sdata->u.mgd.associated = cbss;
1364 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001365
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001366 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1367
Johannes Berg488dd7b2012-10-29 20:08:01 +01001368 if (sdata->vif.p2p) {
1369 u8 noa[2];
1370 int ret;
1371
1372 ret = cfg80211_get_p2p_attr(cbss->information_elements,
1373 cbss->len_information_elements,
1374 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1375 noa, sizeof(noa));
1376 if (ret >= 2) {
1377 bss_conf->p2p_oppps = noa[1] & 0x80;
1378 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1379 bss_info_changed |= BSS_CHANGED_P2P_PS;
1380 sdata->u.mgd.p2p_noa_index = noa[0];
1381 }
1382 }
1383
Johannes Bergb291ba12009-07-10 15:29:03 +02001384 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001385 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001386
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001387 ieee80211_led_assoc(local, 1);
1388
Johannes Berge5b900d2010-07-29 16:08:55 +02001389 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1390 bss_conf->dtim_period = bss->dtim_period;
1391 else
1392 bss_conf->dtim_period = 0;
1393
Juuso Oikarinen68542962010-06-09 13:43:26 +03001394 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001395
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001396 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001397 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001398 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001399 bss_info_changed |= BSS_CHANGED_CQM;
1400
Juuso Oikarinen68542962010-06-09 13:43:26 +03001401 /* Enable ARP filtering */
1402 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1403 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1404 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1405 }
1406
Johannes Bergae5eb022008-10-14 16:58:37 +02001407 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001408
Johannes Berg056508d2009-07-30 21:43:55 +02001409 mutex_lock(&local->iflist_mtx);
1410 ieee80211_recalc_ps(local, -1);
1411 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001412
Johannes Berg04ecd252012-09-11 14:34:12 +02001413 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001414 ieee80211_recalc_ps_vif(sdata);
1415
John W. Linville8a5b33f2010-01-06 15:39:39 -05001416 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001417 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001418}
1419
Jouni Malinene69e95d2010-03-29 23:29:31 -07001420static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001421 u16 stype, u16 reason, bool tx,
1422 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001423{
Johannes Berg46900292009-02-15 12:44:28 +01001424 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001425 struct ieee80211_local *local = sdata->local;
1426 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001427 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001428
Johannes Berg77fdaa12009-07-07 03:45:17 +02001429 ASSERT_MGD_MTX(ifmgd);
1430
Johannes Berg37ad3882012-02-24 13:50:54 +01001431 if (WARN_ON_ONCE(tx && !frame_buf))
1432 return;
1433
Johannes Bergb291ba12009-07-10 15:29:03 +02001434 if (WARN_ON(!ifmgd->associated))
1435 return;
1436
David Spinadel79543d82012-06-12 09:59:45 +03001437 ieee80211_stop_poll(sdata);
1438
Johannes Berg77fdaa12009-07-07 03:45:17 +02001439 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001440
1441 /*
1442 * we need to commit the associated = NULL change because the
1443 * scan code uses that to determine whether this iface should
1444 * go to/wake up from powersave or not -- and could otherwise
1445 * wake the queues erroneously.
1446 */
1447 smp_mb();
1448
1449 /*
1450 * Thus, we can only afterwards stop the queues -- to account
1451 * for the case where another CPU is finishing a scan at this
1452 * time -- we don't want the scan code to enable queues.
1453 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001454
John W. Linville8a5b33f2010-01-06 15:39:39 -05001455 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001456 netif_carrier_off(sdata->dev);
1457
Johannes Berg2a419052010-06-10 10:21:29 +02001458 mutex_lock(&local->sta_mtx);
Eliad Peller88a9e312012-06-01 11:14:03 +03001459 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301460 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001461 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg7f161142012-07-18 12:53:34 +02001462 ieee80211_sta_tear_down_BA_sessions(sta, false);
Sujith4cad6c72010-02-10 14:52:21 +05301463 }
Johannes Berg2a419052010-06-10 10:21:29 +02001464 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001465
Eliad Peller88bc40e2012-07-12 17:35:33 +03001466 /*
1467 * if we want to get out of ps before disassoc (why?) we have
1468 * to do it before sending disassoc, as otherwise the null-packet
1469 * won't be valid.
1470 */
1471 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1472 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1473 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1474 }
1475 local->ps_sdata = NULL;
1476
Eliad Pellerab095872012-07-27 12:33:22 +03001477 /* disable per-vif ps */
1478 ieee80211_recalc_ps_vif(sdata);
1479
Eliad Pellerf8239812012-06-27 14:18:22 +03001480 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1481 if (tx)
1482 drv_flush(local, false);
1483
Johannes Berg37ad3882012-02-24 13:50:54 +01001484 /* deauthenticate/disassociate now */
1485 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001486 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1487 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001488
1489 /* flush out frame */
1490 if (tx)
1491 drv_flush(local, false);
1492
Eliad Peller88a9e312012-06-01 11:14:03 +03001493 /* clear bssid only after building the needed mgmt frames */
1494 memset(ifmgd->bssid, 0, ETH_ALEN);
1495
Johannes Berg37ad3882012-02-24 13:50:54 +01001496 /* remove AP and TDLS peers */
1497 sta_info_flush(local, sdata);
1498
1499 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001500 changed |= ieee80211_reset_erp_info(sdata);
1501
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001502 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001503 changed |= BSS_CHANGED_ASSOC;
1504 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001505
Johannes Berg488dd7b2012-10-29 20:08:01 +01001506 sdata->vif.bss_conf.p2p_ctwindow = 0;
1507 sdata->vif.bss_conf.p2p_oppps = false;
1508
Johannes Berg413ad502009-05-08 21:21:06 +02001509 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001510 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1511 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001512
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001513 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301514
Kalle Valo520eb822008-12-18 23:35:27 +02001515 del_timer_sync(&local->dynamic_ps_timer);
1516 cancel_work_sync(&local->dynamic_ps_enable_work);
1517
Juuso Oikarinen68542962010-06-09 13:43:26 +03001518 /* Disable ARP filtering */
1519 if (sdata->vif.bss_conf.arp_filter_enabled) {
1520 sdata->vif.bss_conf.arp_filter_enabled = false;
1521 changed |= BSS_CHANGED_ARP_FILTER;
1522 }
1523
Johannes Berg3abead52012-03-02 15:56:59 +01001524 sdata->vif.bss_conf.qos = false;
1525 changed |= BSS_CHANGED_QOS;
1526
Johannes Berg0aaffa92010-05-05 15:28:27 +02001527 /* The BSSID (not really interesting) and HT changed */
1528 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001529 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001530
Johannes Berg55de9082012-07-26 17:24:39 +02001531 ieee80211_vif_release_channel(sdata);
Johannes Berg3cc52402012-03-09 13:12:35 +01001532
Johannes Berg3abead52012-03-02 15:56:59 +01001533 /* disassociated - set to defaults now */
1534 ieee80211_set_wmm_default(sdata, false);
1535
Johannes Bergb9dcf712010-08-27 12:35:54 +02001536 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1537 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1538 del_timer_sync(&sdata->u.mgd.timer);
1539 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001540
1541 sdata->u.mgd.timers_running = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001542}
Jiri Bencf0706e82007-05-05 11:45:53 -07001543
Kalle Valo3cf335d52009-03-22 21:57:06 +02001544void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1545 struct ieee80211_hdr *hdr)
1546{
1547 /*
1548 * We can postpone the mgd.timer whenever receiving unicast frames
1549 * from AP because we know that the connection is working both ways
1550 * at that time. But multicast frames (and hence also beacons) must
1551 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001552 * data idle periods for sending the periodic probe request to the
1553 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02001554 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001555 if (is_multicast_ether_addr(hdr->addr1))
1556 return;
1557
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001558 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02001559}
Jiri Bencf0706e82007-05-05 11:45:53 -07001560
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001561static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1562{
1563 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001564 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001565
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001566 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001567 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001568 IEEE80211_STA_CONNECTION_POLL))) {
1569 mutex_unlock(&local->mtx);
1570 return;
1571 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001572
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001573 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001574
1575 mutex_lock(&local->iflist_mtx);
1576 ieee80211_recalc_ps(local, -1);
1577 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001578
1579 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001580 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001581
1582 /*
1583 * We've received a probe response, but are not sure whether
1584 * we have or will be receiving any beacons or data, so let's
1585 * schedule the timers again, just in case.
1586 */
1587 ieee80211_sta_reset_beacon_monitor(sdata);
1588
1589 mod_timer(&ifmgd->conn_mon_timer,
1590 round_jiffies_up(jiffies +
1591 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001592out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001593 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001594}
1595
1596void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001597 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001598{
Felix Fietkau75706d02010-12-02 21:01:07 +01001599 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001600 return;
1601
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001602 if (ack)
1603 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001604
1605 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1606 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001607 if (ack)
1608 sdata->u.mgd.probe_send_count = 0;
1609 else
1610 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001611 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1612 }
1613}
1614
Maxim Levitskya43abf22009-07-31 18:54:12 +03001615static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1616{
1617 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1618 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001619 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001620 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001621
1622 /*
1623 * Try sending broadcast probe requests for the last three
1624 * probe requests after the first ones failed since some
1625 * buggy APs only support broadcast probe requests.
1626 */
1627 if (ifmgd->probe_send_count >= unicast_limit)
1628 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001629
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001630 /*
1631 * When the hardware reports an accurate Tx ACK status, it's
1632 * better to send a nullfunc frame instead of a probe request,
1633 * as it will kick us off the AP quickly if we aren't associated
1634 * anymore. The timeout will be reset if the frame is ACKed by
1635 * the AP.
1636 */
Soumik Das992e68b2012-05-20 15:31:13 +05301637 ifmgd->probe_send_count++;
1638
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001639 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1640 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001641 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001642 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001643 int ssid_len;
1644
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001645 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001646 if (WARN_ON_ONCE(ssid == NULL))
1647 ssid_len = 0;
1648 else
1649 ssid_len = ssid[1];
1650
1651 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Bergfe94fe02012-07-30 12:26:34 +02001652 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02001653 ifmgd->associated->channel, false);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001654 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001655
Ben Greear180205b2011-02-04 15:30:24 -08001656 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001657 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301658 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1659 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001660}
1661
Johannes Bergb291ba12009-07-10 15:29:03 +02001662static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1663 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001664{
Kalle Valo04de8382009-03-22 21:57:35 +02001665 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001666 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001667
Johannes Berg9607e6b662009-12-23 13:15:31 +01001668 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001669 return;
1670
Johannes Berg77fdaa12009-07-07 03:45:17 +02001671 mutex_lock(&ifmgd->mtx);
1672
1673 if (!ifmgd->associated)
1674 goto out;
1675
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001676 mutex_lock(&sdata->local->mtx);
1677
1678 if (sdata->local->tmp_channel || sdata->local->scanning) {
1679 mutex_unlock(&sdata->local->mtx);
1680 goto out;
1681 }
1682
Joe Perchese87cc472012-05-13 21:56:26 +00001683 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001684 mlme_dbg_ratelimited(sdata,
1685 "detected beacon loss from AP - sending probe request\n");
1686
Holger Schurig6efb71b2012-05-15 15:38:59 +02001687 ieee80211_cqm_rssi_notify(&sdata->vif,
1688 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001689
Johannes Bergb291ba12009-07-10 15:29:03 +02001690 /*
1691 * The driver/our work has already reported this event or the
1692 * connection monitoring has kicked in and we have already sent
1693 * a probe request. Or maybe the AP died and the driver keeps
1694 * reporting until we disassociate...
1695 *
1696 * In either case we have to ignore the current call to this
1697 * function (except for setting the correct probe reason bit)
1698 * because otherwise we would reset the timer every time and
1699 * never check whether we received a probe response!
1700 */
1701 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1702 IEEE80211_STA_CONNECTION_POLL))
1703 already = true;
1704
1705 if (beacon)
1706 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1707 else
1708 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1709
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001710 mutex_unlock(&sdata->local->mtx);
1711
Johannes Bergb291ba12009-07-10 15:29:03 +02001712 if (already)
1713 goto out;
1714
Johannes Berg4e751842009-06-10 15:16:52 +02001715 mutex_lock(&sdata->local->iflist_mtx);
1716 ieee80211_recalc_ps(sdata->local, -1);
1717 mutex_unlock(&sdata->local->iflist_mtx);
1718
Maxim Levitskya43abf22009-07-31 18:54:12 +03001719 ifmgd->probe_send_count = 0;
1720 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001721 out:
1722 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001723}
1724
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001725struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1726 struct ieee80211_vif *vif)
1727{
1728 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1729 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001730 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001731 struct sk_buff *skb;
1732 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001733 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001734
1735 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1736 return NULL;
1737
1738 ASSERT_MGD_MTX(ifmgd);
1739
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001740 if (ifmgd->associated)
1741 cbss = ifmgd->associated;
1742 else if (ifmgd->auth_data)
1743 cbss = ifmgd->auth_data->bss;
1744 else if (ifmgd->assoc_data)
1745 cbss = ifmgd->assoc_data->bss;
1746 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001747 return NULL;
1748
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001749 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001750 if (WARN_ON_ONCE(ssid == NULL))
1751 ssid_len = 0;
1752 else
1753 ssid_len = ssid[1];
1754
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001755 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001756 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001757 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001758 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001759
1760 return skb;
1761}
1762EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1763
Johannes Berg882a7c62012-08-01 22:32:45 +02001764static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1765 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001766{
1767 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1768 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001769 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001770
1771 mutex_lock(&ifmgd->mtx);
1772 if (!ifmgd->associated) {
1773 mutex_unlock(&ifmgd->mtx);
1774 return;
1775 }
1776
Johannes Berg37ad3882012-02-24 13:50:54 +01001777 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1778 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001779 transmit_frame, frame_buf);
1780 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001781 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001782
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001783 /*
1784 * must be outside lock due to cfg80211,
1785 * but that's not a problem.
1786 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001787 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001788
1789 mutex_lock(&local->mtx);
1790 ieee80211_recalc_idle(local);
1791 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001792}
1793
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001794static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001795{
1796 struct ieee80211_sub_if_data *sdata =
1797 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001798 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001799 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1800 struct sta_info *sta;
1801
1802 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301803 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001804 sta = sta_info_get(sdata, ifmgd->bssid);
1805 if (sta)
1806 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301807 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001808 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001809
Johannes Berg882a7c62012-08-01 22:32:45 +02001810 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1811 sdata_info(sdata, "Connection to AP %pM lost\n",
1812 ifmgd->bssid);
1813 __ieee80211_disconnect(sdata, false);
1814 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001815 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001816 }
1817}
1818
1819static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1820{
1821 struct ieee80211_sub_if_data *sdata =
1822 container_of(work, struct ieee80211_sub_if_data,
1823 u.mgd.csa_connection_drop_work);
1824
1825 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1826 IEEE80211_QUEUE_STOP_REASON_CSA);
1827 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001828}
1829
Kalle Valo04de8382009-03-22 21:57:35 +02001830void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1831{
1832 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001833 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001834
Johannes Bergb5878a22010-04-07 16:48:40 +02001835 trace_api_beacon_loss(sdata);
1836
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001837 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1838 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001839}
1840EXPORT_SYMBOL(ieee80211_beacon_loss);
1841
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001842void ieee80211_connection_loss(struct ieee80211_vif *vif)
1843{
1844 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1845 struct ieee80211_hw *hw = &sdata->local->hw;
1846
Johannes Bergb5878a22010-04-07 16:48:40 +02001847 trace_api_connection_loss(sdata);
1848
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001849 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1850 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1851}
1852EXPORT_SYMBOL(ieee80211_connection_loss);
1853
1854
Johannes Berg66e67e42012-01-20 13:55:27 +01001855static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1856 bool assoc)
1857{
1858 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1859
1860 lockdep_assert_held(&sdata->u.mgd.mtx);
1861
Johannes Berg66e67e42012-01-20 13:55:27 +01001862 if (!assoc) {
1863 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1864
1865 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1866 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg55de9082012-07-26 17:24:39 +02001867 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001868 }
1869
1870 cfg80211_put_bss(auth_data->bss);
1871 kfree(auth_data);
1872 sdata->u.mgd.auth_data = NULL;
1873}
1874
1875static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1876 struct ieee80211_mgmt *mgmt, size_t len)
1877{
1878 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1879 u8 *pos;
1880 struct ieee802_11_elems elems;
1881
1882 pos = mgmt->u.auth.variable;
1883 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1884 if (!elems.challenge)
1885 return;
1886 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001887 drv_mgd_prepare_tx(sdata->local, sdata);
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001888 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001889 elems.challenge - 2, elems.challenge_len + 2,
1890 auth_data->bss->bssid, auth_data->bss->bssid,
1891 auth_data->key, auth_data->key_len,
1892 auth_data->key_idx);
1893}
1894
1895static enum rx_mgmt_action __must_check
1896ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1897 struct ieee80211_mgmt *mgmt, size_t len)
1898{
1899 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1900 u8 bssid[ETH_ALEN];
1901 u16 auth_alg, auth_transaction, status_code;
1902 struct sta_info *sta;
1903
1904 lockdep_assert_held(&ifmgd->mtx);
1905
1906 if (len < 24 + 6)
1907 return RX_MGMT_NONE;
1908
1909 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1910 return RX_MGMT_NONE;
1911
1912 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1913
Joe Perchesb203ca32012-05-08 18:56:52 +00001914 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001915 return RX_MGMT_NONE;
1916
1917 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1918 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1919 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1920
1921 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001922 auth_transaction != ifmgd->auth_data->expected_transaction) {
1923 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1924 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1925 auth_transaction,
1926 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001927 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001928 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001929
1930 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001931 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1932 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001933 ieee80211_destroy_auth_data(sdata, false);
1934 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001935 }
1936
1937 switch (ifmgd->auth_data->algorithm) {
1938 case WLAN_AUTH_OPEN:
1939 case WLAN_AUTH_LEAP:
1940 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001941 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01001942 break;
1943 case WLAN_AUTH_SHARED_KEY:
1944 if (ifmgd->auth_data->expected_transaction != 4) {
1945 ieee80211_auth_challenge(sdata, mgmt, len);
1946 /* need another frame */
1947 return RX_MGMT_NONE;
1948 }
1949 break;
1950 default:
1951 WARN_ONCE(1, "invalid auth alg %d",
1952 ifmgd->auth_data->algorithm);
1953 return RX_MGMT_NONE;
1954 }
1955
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001956 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001957 ifmgd->auth_data->done = true;
1958 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1959 run_again(ifmgd, ifmgd->auth_data->timeout);
1960
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001961 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
1962 ifmgd->auth_data->expected_transaction != 2) {
1963 /*
1964 * Report auth frame to user space for processing since another
1965 * round of Authentication frames is still needed.
1966 */
1967 return RX_MGMT_CFG80211_RX_AUTH;
1968 }
1969
Johannes Berg66e67e42012-01-20 13:55:27 +01001970 /* move station state to auth */
1971 mutex_lock(&sdata->local->sta_mtx);
1972 sta = sta_info_get(sdata, bssid);
1973 if (!sta) {
1974 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1975 goto out_err;
1976 }
1977 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001978 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01001979 goto out_err;
1980 }
1981 mutex_unlock(&sdata->local->sta_mtx);
1982
1983 return RX_MGMT_CFG80211_RX_AUTH;
1984 out_err:
1985 mutex_unlock(&sdata->local->sta_mtx);
1986 /* ignore frame -- wait for timeout */
1987 return RX_MGMT_NONE;
1988}
1989
1990
Johannes Berg77fdaa12009-07-07 03:45:17 +02001991static enum rx_mgmt_action __must_check
1992ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02001993 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001994{
Johannes Berg46900292009-02-15 12:44:28 +01001995 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001996 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001997 u16 reason_code;
1998
Johannes Berg66e67e42012-01-20 13:55:27 +01001999 lockdep_assert_held(&ifmgd->mtx);
2000
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002001 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002002 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002003
Johannes Berg66e67e42012-01-20 13:55:27 +01002004 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002005 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002006 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002007
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002008 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002009
2010 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2011
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002012 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2013 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002014
Johannes Berg37ad3882012-02-24 13:50:54 +01002015 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2016
Johannes Berg7da7cc12010-08-05 17:02:38 +02002017 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01002018 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002019 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002020
2021 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002022}
2023
2024
Johannes Berg77fdaa12009-07-07 03:45:17 +02002025static enum rx_mgmt_action __must_check
2026ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2027 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002028{
Johannes Berg46900292009-02-15 12:44:28 +01002029 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002030 u16 reason_code;
2031
Johannes Berg66e67e42012-01-20 13:55:27 +01002032 lockdep_assert_held(&ifmgd->mtx);
2033
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002034 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002035 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002036
Johannes Berg66e67e42012-01-20 13:55:27 +01002037 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002038 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002039 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002040
2041 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2042
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002043 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2044 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002045
Johannes Berg37ad3882012-02-24 13:50:54 +01002046 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2047
Johannes Berg7da7cc12010-08-05 17:02:38 +02002048 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002049 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002050 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002051
Johannes Berg77fdaa12009-07-07 03:45:17 +02002052 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002053}
2054
Christian Lamparterc74d0842011-10-15 00:14:49 +02002055static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2056 u8 *supp_rates, unsigned int supp_rates_len,
2057 u32 *rates, u32 *basic_rates,
2058 bool *have_higher_than_11mbit,
2059 int *min_rate, int *min_rate_index)
2060{
2061 int i, j;
2062
2063 for (i = 0; i < supp_rates_len; i++) {
2064 int rate = (supp_rates[i] & 0x7f) * 5;
2065 bool is_basic = !!(supp_rates[i] & 0x80);
2066
2067 if (rate > 110)
2068 *have_higher_than_11mbit = true;
2069
2070 /*
2071 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2072 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2073 *
2074 * Note: Even through the membership selector and the basic
2075 * rate flag share the same bit, they are not exactly
2076 * the same.
2077 */
2078 if (!!(supp_rates[i] & 0x80) &&
2079 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2080 continue;
2081
2082 for (j = 0; j < sband->n_bitrates; j++) {
2083 if (sband->bitrates[j].bitrate == rate) {
2084 *rates |= BIT(j);
2085 if (is_basic)
2086 *basic_rates |= BIT(j);
2087 if (rate < *min_rate) {
2088 *min_rate = rate;
2089 *min_rate_index = j;
2090 }
2091 break;
2092 }
2093 }
2094 }
2095}
Jiri Bencf0706e82007-05-05 11:45:53 -07002096
Johannes Berg66e67e42012-01-20 13:55:27 +01002097static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2098 bool assoc)
2099{
2100 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2101
2102 lockdep_assert_held(&sdata->u.mgd.mtx);
2103
Johannes Berg66e67e42012-01-20 13:55:27 +01002104 if (!assoc) {
2105 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2106
2107 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2108 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg55de9082012-07-26 17:24:39 +02002109 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002110 }
2111
2112 kfree(assoc_data);
2113 sdata->u.mgd.assoc_data = NULL;
2114}
2115
2116static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2117 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002118 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002119{
Johannes Berg46900292009-02-15 12:44:28 +01002120 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002121 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002122 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002123 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002124 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002125 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002126 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002127 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002128 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002129 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002130
Johannes Bergaf6b6372009-12-23 13:15:35 +01002131 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002132
Jiri Bencf0706e82007-05-05 11:45:53 -07002133 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002134 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002135
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002136 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002137 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2138 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002139 aid &= ~(BIT(15) | BIT(14));
2140
Johannes Berg05cb9102011-10-28 11:59:47 +02002141 ifmgd->broken_ap = false;
2142
2143 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002144 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2145 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002146 aid = 0;
2147 ifmgd->broken_ap = true;
2148 }
2149
Johannes Bergaf6b6372009-12-23 13:15:35 +01002150 pos = mgmt->u.assoc_resp.variable;
2151 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2152
Jiri Bencf0706e82007-05-05 11:45:53 -07002153 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002154 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002155 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002156 }
2157
Johannes Berg46900292009-02-15 12:44:28 +01002158 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002159
Guy Eilam2a33bee2011-08-17 15:18:15 +03002160 mutex_lock(&sdata->local->sta_mtx);
2161 /*
2162 * station info was already allocated and inserted before
2163 * the association and should be available to us
2164 */
Johannes Berg7852e362012-01-20 13:55:24 +01002165 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002166 if (WARN_ON(!sta)) {
2167 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002168 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002169 }
2170
Johannes Berg55de9082012-07-26 17:24:39 +02002171 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002172
Johannes Bergab1faea2009-07-01 21:41:17 +02002173 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Ben Greearef96a8422011-11-18 11:32:00 -08002174 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002175 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002176
Johannes Berg64f68e52012-03-28 10:58:37 +02002177 sta->supports_40mhz =
2178 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2179
Mahesh Palivela818255e2012-10-10 11:33:04 +00002180 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2181 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2182 elems.vht_cap_elem,
2183 &sta->sta.vht_cap);
2184
Johannes Berg4b7679a2008-09-18 18:14:18 +02002185 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002186
Johannes Berg46900292009-02-15 12:44:28 +01002187 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002188 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002189
Johannes Bergddf4ac52008-10-22 11:41:38 +02002190 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002191 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002192
Johannes Bergc8987872012-01-20 13:55:17 +01002193 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2194 if (!err)
2195 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2196 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2197 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2198 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002199 sdata_info(sdata,
2200 "failed to move station %pM to desired state\n",
2201 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002202 WARN_ON(__sta_info_destroy(sta));
2203 mutex_unlock(&sdata->local->sta_mtx);
2204 return false;
2205 }
2206
Johannes Berg7852e362012-01-20 13:55:24 +01002207 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002208
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002209 /*
2210 * Always handle WMM once after association regardless
2211 * of the first value the AP uses. Setting -1 here has
2212 * that effect because the AP values is an unsigned
2213 * 4-bit value.
2214 */
2215 ifmgd->wmm_last_param_set = -1;
2216
Johannes Bergddf4ac52008-10-22 11:41:38 +02002217 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002218 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002219 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002220 else
Johannes Berg3abead52012-03-02 15:56:59 +01002221 ieee80211_set_wmm_default(sdata, false);
2222 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002223
Johannes Berg074d46d2012-03-15 19:45:16 +01002224 if (elems.ht_operation && elems.wmm_param &&
Johannes Bergab1faea2009-07-01 21:41:17 +02002225 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg24398e32012-03-28 10:58:36 +02002226 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2227 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002228
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002229 /* set AID and assoc capability,
2230 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002231 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002232 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002233 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002234
Kalle Valo15b7b062009-03-22 21:57:14 +02002235 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002236 * If we're using 4-addr mode, let the AP know that we're
2237 * doing so, so that it can create the STA VLAN on its side
2238 */
2239 if (ifmgd->use_4addr)
2240 ieee80211_send_4addr_nullfunc(local, sdata);
2241
2242 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002243 * Start timer to probe the connection to the AP now.
2244 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002245 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002246 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002247 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002248
Johannes Bergaf6b6372009-12-23 13:15:35 +01002249 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002250}
2251
Johannes Berg66e67e42012-01-20 13:55:27 +01002252static enum rx_mgmt_action __must_check
2253ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2254 struct ieee80211_mgmt *mgmt, size_t len,
2255 struct cfg80211_bss **bss)
2256{
2257 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2258 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2259 u16 capab_info, status_code, aid;
2260 struct ieee802_11_elems elems;
2261 u8 *pos;
2262 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002263
Johannes Berg66e67e42012-01-20 13:55:27 +01002264 lockdep_assert_held(&ifmgd->mtx);
2265
2266 if (!assoc_data)
2267 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002268 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002269 return RX_MGMT_NONE;
2270
2271 /*
2272 * AssocResp and ReassocResp have identical structure, so process both
2273 * of them in this function.
2274 */
2275
2276 if (len < 24 + 6)
2277 return RX_MGMT_NONE;
2278
2279 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2280 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2281 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2282 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2283
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002284 sdata_info(sdata,
2285 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2286 reassoc ? "Rea" : "A", mgmt->sa,
2287 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002288
2289 pos = mgmt->u.assoc_resp.variable;
2290 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2291
2292 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2293 elems.timeout_int && elems.timeout_int_len == 5 &&
2294 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2295 u32 tu, ms;
2296 tu = get_unaligned_le32(elems.timeout_int + 1);
2297 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002298 sdata_info(sdata,
2299 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2300 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002301 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2302 if (ms > IEEE80211_ASSOC_TIMEOUT)
2303 run_again(ifmgd, assoc_data->timeout);
2304 return RX_MGMT_NONE;
2305 }
2306
2307 *bss = assoc_data->bss;
2308
2309 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002310 sdata_info(sdata, "%pM denied association (code=%d)\n",
2311 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002312 ieee80211_destroy_assoc_data(sdata, false);
2313 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002314 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2315 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002316 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002317 cfg80211_put_bss(*bss);
2318 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2319 }
John W. Linville635d9992012-07-09 16:34:34 -04002320 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002321
2322 /*
2323 * destroy assoc_data afterwards, as otherwise an idle
2324 * recalc after assoc_data is NULL but before associated
2325 * is set can cause the interface to go idle
2326 */
2327 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002328 }
2329
2330 return RX_MGMT_CFG80211_RX_ASSOC;
2331}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002332static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002333 struct ieee80211_mgmt *mgmt,
2334 size_t len,
2335 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002336 struct ieee802_11_elems *elems,
2337 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002338{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002339 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002340 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002341 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002342 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002343 bool need_ps = false;
2344
Johannes Berg66e67e42012-01-20 13:55:27 +01002345 if (sdata->u.mgd.associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002346 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
Johannes Berg56007a02010-01-26 14:19:52 +01002347 bss = (void *)sdata->u.mgd.associated->priv;
2348 /* not previously set so we may need to recalc */
2349 need_ps = !bss->dtim_period;
2350 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002351
Johannes Berg5bda6172008-09-08 11:05:10 +02002352 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002353 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2354 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002355 else
2356 freq = rx_status->freq;
2357
2358 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2359
2360 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2361 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002362
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002363 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002364 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002365 if (bss)
2366 ieee80211_rx_bss_put(local, bss);
2367
2368 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002369 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002370
Johannes Berg56007a02010-01-26 14:19:52 +01002371 if (need_ps) {
2372 mutex_lock(&local->iflist_mtx);
2373 ieee80211_recalc_ps(local, -1);
2374 mutex_unlock(&local->iflist_mtx);
2375 }
2376
Johannes Berg5bc14202012-08-01 16:13:02 +02002377 if (elems->ch_switch_ie &&
2378 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2379 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002380 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002381}
2382
2383
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002384static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002385 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002386{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002387 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002388 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002389 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2390 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002391 struct ieee802_11_elems elems;
2392
Kalle Valo15b7b062009-03-22 21:57:14 +02002393 ifmgd = &sdata->u.mgd;
2394
Johannes Berg77fdaa12009-07-07 03:45:17 +02002395 ASSERT_MGD_MTX(ifmgd);
2396
Joe Perchesb203ca32012-05-08 18:56:52 +00002397 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002398 return; /* ignore ProbeResp to foreign address */
2399
Ester Kummerae6a44e2008-06-27 18:54:48 +03002400 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2401 if (baselen > len)
2402 return;
2403
2404 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2405 &elems);
2406
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002407 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002408
Johannes Berg77fdaa12009-07-07 03:45:17 +02002409 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002410 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002411 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002412
2413 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002414 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002415 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002416 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002417 ifmgd->auth_data->tries = 0;
2418 ifmgd->auth_data->timeout = jiffies;
2419 run_again(ifmgd, ifmgd->auth_data->timeout);
2420 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002421}
2422
Johannes Bergd91f36d2009-04-16 13:17:26 +02002423/*
2424 * This is the canonical list of information elements we care about,
2425 * the filter code also gives us all changes to the Microsoft OUI
2426 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2427 *
2428 * We implement beacon filtering in software since that means we can
2429 * avoid processing the frame here and in cfg80211, and userspace
2430 * will not be able to tell whether the hardware supports it or not.
2431 *
2432 * XXX: This list needs to be dynamic -- userspace needs to be able to
2433 * add items it requires. It also needs to be able to tell us to
2434 * look out for other vendor IEs.
2435 */
2436static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002437 (1ULL << WLAN_EID_COUNTRY) |
2438 (1ULL << WLAN_EID_ERP_INFO) |
2439 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2440 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2441 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002442 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002443
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002444static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002445 struct ieee80211_mgmt *mgmt,
2446 size_t len,
2447 struct ieee80211_rx_status *rx_status)
2448{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002449 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002450 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002451 size_t baselen;
2452 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002453 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002454 struct ieee80211_chanctx_conf *chanctx_conf;
2455 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002456 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002457 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002458 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002459 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002460 u8 *bssid;
2461
Johannes Berg66e67e42012-01-20 13:55:27 +01002462 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002463
Ester Kummerae6a44e2008-06-27 18:54:48 +03002464 /* Process beacon from the current BSS */
2465 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2466 if (baselen > len)
2467 return;
2468
Johannes Berg55de9082012-07-26 17:24:39 +02002469 rcu_read_lock();
2470 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2471 if (!chanctx_conf) {
2472 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002473 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002474 }
2475
2476 if (rx_status->freq != chanctx_conf->channel->center_freq) {
2477 rcu_read_unlock();
2478 return;
2479 }
2480 chan = chanctx_conf->channel;
2481 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002482
Johannes Berg66e67e42012-01-20 13:55:27 +01002483 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002484 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002485 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2486 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002487
Johannes Berg66e67e42012-01-20 13:55:27 +01002488 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2489 false);
2490 ifmgd->assoc_data->have_beacon = true;
2491 ifmgd->assoc_data->sent_assoc = false;
2492 /* continue assoc process */
2493 ifmgd->assoc_data->timeout = jiffies;
2494 run_again(ifmgd, ifmgd->assoc_data->timeout);
2495 return;
2496 }
2497
2498 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002499 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002500 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002501 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002502
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002503 /* Track average RSSI from the Beacon frames of the current AP */
2504 ifmgd->last_beacon_signal = rx_status->signal;
2505 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2506 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002507 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002508 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002509 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002510 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002511 } else {
2512 ifmgd->ave_beacon_signal =
2513 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2514 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2515 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002516 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002517 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002518
2519 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2520 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2521 int sig = ifmgd->ave_beacon_signal;
2522 int last_sig = ifmgd->last_ave_beacon_signal;
2523
2524 /*
2525 * if signal crosses either of the boundaries, invoke callback
2526 * with appropriate parameters
2527 */
2528 if (sig > ifmgd->rssi_max_thold &&
2529 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2530 ifmgd->last_ave_beacon_signal = sig;
2531 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2532 } else if (sig < ifmgd->rssi_min_thold &&
2533 (last_sig >= ifmgd->rssi_max_thold ||
2534 last_sig == 0)) {
2535 ifmgd->last_ave_beacon_signal = sig;
2536 drv_rssi_callback(local, RSSI_EVENT_LOW);
2537 }
2538 }
2539
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002540 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002541 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002542 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002543 int sig = ifmgd->ave_beacon_signal / 16;
2544 int last_event = ifmgd->last_cqm_event_signal;
2545 int thold = bss_conf->cqm_rssi_thold;
2546 int hyst = bss_conf->cqm_rssi_hyst;
2547 if (sig < thold &&
2548 (last_event == 0 || sig < last_event - hyst)) {
2549 ifmgd->last_cqm_event_signal = sig;
2550 ieee80211_cqm_rssi_notify(
2551 &sdata->vif,
2552 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2553 GFP_KERNEL);
2554 } else if (sig > thold &&
2555 (last_event == 0 || sig > last_event + hyst)) {
2556 ifmgd->last_cqm_event_signal = sig;
2557 ieee80211_cqm_rssi_notify(
2558 &sdata->vif,
2559 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2560 GFP_KERNEL);
2561 }
2562 }
2563
Johannes Bergb291ba12009-07-10 15:29:03 +02002564 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002565 mlme_dbg_ratelimited(sdata,
2566 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002567 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002568 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002569 ieee80211_run_deferred_scan(local);
2570 mutex_unlock(&local->mtx);
2571
Johannes Berg4e751842009-06-10 15:16:52 +02002572 mutex_lock(&local->iflist_mtx);
2573 ieee80211_recalc_ps(local, -1);
2574 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002575 }
2576
Johannes Bergb291ba12009-07-10 15:29:03 +02002577 /*
2578 * Push the beacon loss detection into the future since
2579 * we are processing a beacon from the AP just now.
2580 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002581 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002582
Johannes Bergd91f36d2009-04-16 13:17:26 +02002583 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2584 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2585 len - baselen, &elems,
2586 care_about_ies, ncrc);
2587
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302588 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002589 bool directed_tim = ieee80211_check_tim(elems.tim,
2590 elems.tim_len,
2591 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002592 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002593 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002594 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2595 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2596 ieee80211_hw_config(local,
2597 IEEE80211_CONF_CHANGE_PS);
2598 }
Kalle Valo572e0012009-02-10 17:09:31 +02002599 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302600 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002601 local->pspolling = true;
2602
2603 /*
2604 * Here is assumed that the driver will be
2605 * able to send ps-poll frame and receive a
2606 * response even though power save mode is
2607 * enabled, but some drivers might require
2608 * to disable power save here. This needs
2609 * to be investigated.
2610 */
2611 ieee80211_send_pspoll(local, sdata);
2612 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002613 }
2614 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002615
Johannes Berg488dd7b2012-10-29 20:08:01 +01002616 if (sdata->vif.p2p) {
2617 u8 noa[2];
2618 int ret;
2619
2620 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2621 len - baselen,
2622 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2623 noa, sizeof(noa));
2624 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2625 bss_conf->p2p_oppps = noa[1] & 0x80;
2626 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2627 changed |= BSS_CHANGED_P2P_PS;
2628 sdata->u.mgd.p2p_noa_index = noa[0];
2629 /*
2630 * make sure we update all information, the CRC
2631 * mechanism doesn't look at P2P attributes.
2632 */
2633 ifmgd->beacon_crc_valid = false;
2634 }
2635 }
2636
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002637 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002638 return;
2639 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002640 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002641
Johannes Berg7d257452012-07-06 17:37:43 +02002642 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2643 true);
2644
2645 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2646 elems.wmm_param_len))
2647 changed |= BSS_CHANGED_QOS;
2648
Johannes Berg7a5158e2008-10-08 10:59:33 +02002649 if (elems.erp_info && elems.erp_info_len >= 1) {
2650 erp_valid = true;
2651 erp_value = elems.erp_info[0];
2652 } else {
2653 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002654 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002655 changed |= ieee80211_handle_bss_capability(sdata,
2656 le16_to_cpu(mgmt->u.beacon.capab_info),
2657 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002658
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002659
Johannes Berg074d46d2012-03-15 19:45:16 +01002660 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berg444e3802012-09-30 17:08:35 +02002661 !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
Johannes Berg24398e32012-03-28 10:58:36 +02002662 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2663 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002664
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002665 if (elems.country_elem && elems.pwr_constr_elem &&
2666 mgmt->u.probe_resp.capab_info &
2667 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002668 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2669 elems.country_elem,
2670 elems.country_elem_len,
2671 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002672
Johannes Berg471b3ef2007-12-28 14:32:58 +01002673 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002674}
2675
Johannes Berg1fa57d02010-06-10 10:21:32 +02002676void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2677 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002678{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002679 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002680 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002681 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002682 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002683 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002684 u16 fc;
2685
Jiri Bencf0706e82007-05-05 11:45:53 -07002686 rx_status = (struct ieee80211_rx_status *) skb->cb;
2687 mgmt = (struct ieee80211_mgmt *) skb->data;
2688 fc = le16_to_cpu(mgmt->frame_control);
2689
Johannes Berg77fdaa12009-07-07 03:45:17 +02002690 mutex_lock(&ifmgd->mtx);
2691
Johannes Berg66e67e42012-01-20 13:55:27 +01002692 switch (fc & IEEE80211_FCTL_STYPE) {
2693 case IEEE80211_STYPE_BEACON:
2694 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2695 break;
2696 case IEEE80211_STYPE_PROBE_RESP:
2697 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2698 break;
2699 case IEEE80211_STYPE_AUTH:
2700 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2701 break;
2702 case IEEE80211_STYPE_DEAUTH:
2703 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2704 break;
2705 case IEEE80211_STYPE_DISASSOC:
2706 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2707 break;
2708 case IEEE80211_STYPE_ASSOC_RESP:
2709 case IEEE80211_STYPE_REASSOC_RESP:
2710 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2711 break;
2712 case IEEE80211_STYPE_ACTION:
2713 switch (mgmt->u.action.category) {
2714 case WLAN_CATEGORY_SPECTRUM_MGMT:
2715 ieee80211_sta_process_chanswitch(sdata,
2716 &mgmt->u.action.u.chan_switch.sw_elem,
2717 (void *)ifmgd->associated->priv,
2718 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002719 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002720 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002721 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002722 mutex_unlock(&ifmgd->mtx);
2723
Johannes Berg66e67e42012-01-20 13:55:27 +01002724 switch (rma) {
2725 case RX_MGMT_NONE:
2726 /* no action */
2727 break;
2728 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002729 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002730 break;
2731 case RX_MGMT_CFG80211_DISASSOC:
2732 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2733 break;
2734 case RX_MGMT_CFG80211_RX_AUTH:
2735 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2736 break;
2737 case RX_MGMT_CFG80211_RX_ASSOC:
2738 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2739 break;
2740 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2741 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2742 break;
2743 default:
2744 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002745 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002746}
2747
Johannes Berg9c6bd792008-09-11 00:01:52 +02002748static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002749{
2750 struct ieee80211_sub_if_data *sdata =
2751 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002752 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002753 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002754
Johannes Berg5bb644a2009-05-17 11:40:42 +02002755 if (local->quiescing) {
2756 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2757 return;
2758 }
2759
Johannes Berg64592c82010-06-10 10:21:31 +02002760 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002761}
2762
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002763static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002764 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002765{
2766 struct ieee80211_local *local = sdata->local;
2767 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002768 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002769
Johannes Berg37ad3882012-02-24 13:50:54 +01002770 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2771 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002772 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002773
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002774 /*
2775 * must be outside lock due to cfg80211,
2776 * but that's not a problem.
2777 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002778 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002779
2780 mutex_lock(&local->mtx);
2781 ieee80211_recalc_idle(local);
2782 mutex_unlock(&local->mtx);
2783
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002784 mutex_lock(&ifmgd->mtx);
2785}
2786
Johannes Berg66e67e42012-01-20 13:55:27 +01002787static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2788{
2789 struct ieee80211_local *local = sdata->local;
2790 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2791 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2792
2793 lockdep_assert_held(&ifmgd->mtx);
2794
2795 if (WARN_ON_ONCE(!auth_data))
2796 return -EINVAL;
2797
Johannes Berg66e67e42012-01-20 13:55:27 +01002798 auth_data->tries++;
2799
2800 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002801 sdata_info(sdata, "authentication with %pM timed out\n",
2802 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002803
2804 /*
2805 * Most likely AP is not in the range so remove the
2806 * bss struct for that AP.
2807 */
2808 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2809
2810 return -ETIMEDOUT;
2811 }
2812
Johannes Berga1845fc2012-06-27 13:18:36 +02002813 drv_mgd_prepare_tx(local, sdata);
2814
Johannes Berg66e67e42012-01-20 13:55:27 +01002815 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002816 u16 trans = 1;
2817 u16 status = 0;
2818
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002819 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2820 auth_data->bss->bssid, auth_data->tries,
2821 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002822
2823 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002824
2825 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2826 trans = auth_data->sae_trans;
2827 status = auth_data->sae_status;
2828 auth_data->expected_transaction = trans;
2829 }
2830
2831 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2832 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002833 auth_data->bss->bssid,
2834 auth_data->bss->bssid, NULL, 0, 0);
2835 } else {
2836 const u8 *ssidie;
2837
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002838 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2839 auth_data->bss->bssid, auth_data->tries,
2840 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002841
2842 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2843 if (!ssidie)
2844 return -EINVAL;
2845 /*
2846 * Direct probe is sent to broadcast address as some APs
2847 * will not answer to direct packet in unassociated state.
2848 */
2849 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Bergfe94fe02012-07-30 12:26:34 +02002850 NULL, 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02002851 auth_data->bss->channel, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002852 }
2853
2854 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2855 run_again(ifmgd, auth_data->timeout);
2856
2857 return 0;
2858}
2859
2860static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2861{
2862 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2863 struct ieee80211_local *local = sdata->local;
2864
2865 lockdep_assert_held(&sdata->u.mgd.mtx);
2866
Johannes Berg66e67e42012-01-20 13:55:27 +01002867 assoc_data->tries++;
2868 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002869 sdata_info(sdata, "association with %pM timed out\n",
2870 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002871
2872 /*
2873 * Most likely AP is not in the range so remove the
2874 * bss struct for that AP.
2875 */
2876 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2877
2878 return -ETIMEDOUT;
2879 }
2880
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002881 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2882 assoc_data->bss->bssid, assoc_data->tries,
2883 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002884 ieee80211_send_assoc(sdata);
2885
2886 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2887 run_again(&sdata->u.mgd, assoc_data->timeout);
2888
2889 return 0;
2890}
2891
Johannes Berg1fa57d02010-06-10 10:21:32 +02002892void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002893{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002894 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002895 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002896
Johannes Berg77fdaa12009-07-07 03:45:17 +02002897 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02002898
Johannes Berg66e67e42012-01-20 13:55:27 +01002899 if (ifmgd->auth_data &&
2900 time_after(jiffies, ifmgd->auth_data->timeout)) {
2901 if (ifmgd->auth_data->done) {
2902 /*
2903 * ok ... we waited for assoc but userspace didn't,
2904 * so let's just kill the auth data
2905 */
2906 ieee80211_destroy_auth_data(sdata, false);
2907 } else if (ieee80211_probe_auth(sdata)) {
2908 u8 bssid[ETH_ALEN];
2909
2910 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2911
2912 ieee80211_destroy_auth_data(sdata, false);
2913
2914 mutex_unlock(&ifmgd->mtx);
2915 cfg80211_send_auth_timeout(sdata->dev, bssid);
2916 mutex_lock(&ifmgd->mtx);
2917 }
2918 } else if (ifmgd->auth_data)
2919 run_again(ifmgd, ifmgd->auth_data->timeout);
2920
2921 if (ifmgd->assoc_data &&
2922 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2923 if (!ifmgd->assoc_data->have_beacon ||
2924 ieee80211_do_assoc(sdata)) {
2925 u8 bssid[ETH_ALEN];
2926
2927 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2928
2929 ieee80211_destroy_assoc_data(sdata, false);
2930
2931 mutex_unlock(&ifmgd->mtx);
2932 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2933 mutex_lock(&ifmgd->mtx);
2934 }
2935 } else if (ifmgd->assoc_data)
2936 run_again(ifmgd, ifmgd->assoc_data->timeout);
2937
Johannes Bergb291ba12009-07-10 15:29:03 +02002938 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2939 IEEE80211_STA_CONNECTION_POLL) &&
2940 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002941 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002942 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002943
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002944 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002945
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002946 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002947 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002948 else
Ben Greear180205b2011-02-04 15:30:24 -08002949 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002950
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002951 /* ACK received for nullfunc probing frame */
2952 if (!ifmgd->probe_send_count)
2953 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002954 else if (ifmgd->nullfunc_failed) {
2955 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002956 mlme_dbg(sdata,
2957 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
2958 bssid, ifmgd->probe_send_count,
2959 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002960 ieee80211_mgd_probe_ap_send(sdata);
2961 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002962 mlme_dbg(sdata,
2963 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
2964 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02002965 ieee80211_sta_connection_lost(sdata, bssid,
2966 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002967 }
2968 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002969 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002970 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002971 mlme_dbg(sdata,
2972 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
2973 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02002974 ieee80211_sta_connection_lost(sdata, bssid,
2975 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002976 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002977 mlme_dbg(sdata,
2978 "No probe response from AP %pM after %dms, try %d/%i\n",
2979 bssid, probe_wait_ms,
2980 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002981 ieee80211_mgd_probe_ap_send(sdata);
2982 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02002983 /*
2984 * We actually lost the connection ... or did we?
2985 * Let's make sure!
2986 */
Ben Greearb38afa82010-10-07 16:12:06 -07002987 wiphy_debug(local->hw.wiphy,
2988 "%s: No probe response from AP %pM"
2989 " after %dms, disconnecting.\n",
2990 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08002991 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002992
Johannes Berg95acac62011-07-12 12:30:59 +02002993 ieee80211_sta_connection_lost(sdata, bssid,
2994 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02002995 }
2996 }
2997
Johannes Berg77fdaa12009-07-07 03:45:17 +02002998 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002999
3000 mutex_lock(&local->mtx);
3001 ieee80211_recalc_idle(local);
3002 mutex_unlock(&local->mtx);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02003003}
Johannes Berg0a51b272008-09-08 17:44:25 +02003004
Johannes Bergb291ba12009-07-10 15:29:03 +02003005static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3006{
3007 struct ieee80211_sub_if_data *sdata =
3008 (struct ieee80211_sub_if_data *) data;
3009 struct ieee80211_local *local = sdata->local;
3010
3011 if (local->quiescing)
3012 return;
3013
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003014 ieee80211_queue_work(&sdata->local->hw,
3015 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003016}
3017
3018static void ieee80211_sta_conn_mon_timer(unsigned long data)
3019{
3020 struct ieee80211_sub_if_data *sdata =
3021 (struct ieee80211_sub_if_data *) data;
3022 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3023 struct ieee80211_local *local = sdata->local;
3024
3025 if (local->quiescing)
3026 return;
3027
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003028 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003029}
3030
3031static void ieee80211_sta_monitor_work(struct work_struct *work)
3032{
3033 struct ieee80211_sub_if_data *sdata =
3034 container_of(work, struct ieee80211_sub_if_data,
3035 u.mgd.monitor_work);
3036
3037 ieee80211_mgd_probe_ap(sdata, false);
3038}
3039
Johannes Berga1678f82008-09-11 00:01:47 +02003040static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3041{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003042 u32 flags;
3043
Kalle Vaload935682009-04-19 08:47:19 +03003044 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003045 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003046
Johannes Bergb291ba12009-07-10 15:29:03 +02003047 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003048 flags = sdata->local->hw.flags;
3049 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3050 ieee80211_queue_work(&sdata->local->hw,
3051 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003052 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003053 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003054 }
Johannes Berga1678f82008-09-11 00:01:47 +02003055}
3056
Johannes Berg5bb644a2009-05-17 11:40:42 +02003057#ifdef CONFIG_PM
3058void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3059{
3060 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3061
3062 /*
3063 * we need to use atomic bitops for the running bits
3064 * only because both timers might fire at the same
3065 * time -- the code here is properly synchronised.
3066 */
3067
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003068 cancel_work_sync(&ifmgd->request_smps_work);
3069
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003070 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003071 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003072 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003073 if (del_timer_sync(&ifmgd->timer))
3074 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3075
3076 cancel_work_sync(&ifmgd->chswitch_work);
3077 if (del_timer_sync(&ifmgd->chswitch_timer))
3078 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003079
Johannes Bergb291ba12009-07-10 15:29:03 +02003080 /* these will just be re-established on connection */
3081 del_timer_sync(&ifmgd->conn_mon_timer);
3082 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003083}
3084
3085void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3086{
3087 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3088
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303089 if (!ifmgd->associated)
3090 return;
3091
Johannes Berg95acac62011-07-12 12:30:59 +02003092 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3093 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3094 mutex_lock(&ifmgd->mtx);
3095 if (ifmgd->associated) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003096 mlme_dbg(sdata,
3097 "driver requested disconnect after resume\n");
Johannes Berg95acac62011-07-12 12:30:59 +02003098 ieee80211_sta_connection_lost(sdata,
3099 ifmgd->associated->bssid,
3100 WLAN_REASON_UNSPECIFIED);
3101 mutex_unlock(&ifmgd->mtx);
3102 return;
3103 }
3104 mutex_unlock(&ifmgd->mtx);
3105 }
3106
Johannes Berg5bb644a2009-05-17 11:40:42 +02003107 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3108 add_timer(&ifmgd->timer);
3109 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3110 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003111 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003112
3113 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003114 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003115 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003116}
3117#endif
3118
Johannes Berg9c6bd792008-09-11 00:01:52 +02003119/* interface setup */
3120void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3121{
Johannes Berg46900292009-02-15 12:44:28 +01003122 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003123
Johannes Berg46900292009-02-15 12:44:28 +01003124 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003125 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003126 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003127 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3128 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003129 INIT_WORK(&ifmgd->csa_connection_drop_work,
3130 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003131 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003132 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003133 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003134 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3135 (unsigned long) sdata);
3136 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3137 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003138 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303139 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003140
Johannes Bergab1faea2009-07-01 21:41:17 +02003141 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303142 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003143 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3144 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003145
Johannes Berg77fdaa12009-07-07 03:45:17 +02003146 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003147
3148 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3149 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3150 else
3151 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003152}
3153
3154/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003155void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3156{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003157 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003158
3159 /* Restart STA timers */
3160 rcu_read_lock();
3161 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3162 ieee80211_restart_sta_timer(sdata);
3163 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003164}
Johannes Berg10f644a2009-04-16 13:17:25 +02003165
3166int ieee80211_max_network_latency(struct notifier_block *nb,
3167 unsigned long data, void *dummy)
3168{
3169 s32 latency_usec = (s32) data;
3170 struct ieee80211_local *local =
3171 container_of(nb, struct ieee80211_local,
3172 network_latency_notifier);
3173
3174 mutex_lock(&local->iflist_mtx);
3175 ieee80211_recalc_ps(local, latency_usec);
3176 mutex_unlock(&local->iflist_mtx);
3177
3178 return 0;
3179}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003180
Johannes Bergb17166a2012-07-27 11:41:27 +02003181static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3182 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003183{
3184 struct ieee80211_local *local = sdata->local;
3185 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003186 int ht_cfreq;
3187 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3188 const u8 *ht_oper_ie;
3189 const struct ieee80211_ht_operation *ht_oper = NULL;
3190 struct ieee80211_supported_band *sband;
Johannes Berga1cf7752012-03-08 15:02:07 +01003191
Johannes Berg24398e32012-03-28 10:58:36 +02003192 sband = local->hw.wiphy->bands[cbss->channel->band];
3193
3194 ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
3195
3196 if (sband->ht_cap.ht_supported) {
3197 ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
3198 cbss->information_elements,
3199 cbss->len_information_elements);
3200 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3201 ht_oper = (void *)(ht_oper_ie + 2);
3202 }
3203
3204 if (ht_oper) {
3205 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3206 cbss->channel->band);
3207 /* check that channel matches the right operating channel */
3208 if (cbss->channel->center_freq != ht_cfreq) {
3209 /*
3210 * It's possible that some APs are confused here;
3211 * Netgear WNDR3700 sometimes reports 4 higher than
3212 * the actual channel in association responses, but
3213 * since we look at probe response/beacon data here
3214 * it should be OK.
3215 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003216 sdata_info(sdata,
3217 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3218 cbss->channel->center_freq,
3219 ht_cfreq, ht_oper->primary_chan,
3220 cbss->channel->band);
Johannes Berg24398e32012-03-28 10:58:36 +02003221 ht_oper = NULL;
Johannes Berg3a404142012-10-01 15:52:00 +02003222 } else {
3223 channel_type = NL80211_CHAN_HT20;
Johannes Berg24398e32012-03-28 10:58:36 +02003224 }
3225 }
3226
Johannes Berg3a404142012-10-01 15:52:00 +02003227 if (ht_oper && sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3228 /*
3229 * cfg80211 already verified that the channel itself can
3230 * be used, but it didn't check that we can do the right
3231 * HT type, so do that here as well. If HT40 isn't allowed
3232 * on this channel, disable 40 MHz operation.
3233 */
Johannes Berg04ecd252012-09-11 14:34:12 +02003234 const u8 *ht_cap_ie;
3235 const struct ieee80211_ht_cap *ht_cap;
3236 u8 chains = 1;
3237
Johannes Berg24398e32012-03-28 10:58:36 +02003238 channel_type = NL80211_CHAN_HT20;
3239
Johannes Berg3a404142012-10-01 15:52:00 +02003240 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3241 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3242 if (cbss->channel->flags & IEEE80211_CHAN_NO_HT40PLUS)
3243 ifmgd->flags |= IEEE80211_STA_DISABLE_40MHZ;
3244 else
Johannes Berg24398e32012-03-28 10:58:36 +02003245 channel_type = NL80211_CHAN_HT40PLUS;
Johannes Berg3a404142012-10-01 15:52:00 +02003246 break;
3247 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3248 if (cbss->channel->flags & IEEE80211_CHAN_NO_HT40MINUS)
3249 ifmgd->flags |= IEEE80211_STA_DISABLE_40MHZ;
3250 else
Johannes Berg24398e32012-03-28 10:58:36 +02003251 channel_type = NL80211_CHAN_HT40MINUS;
Johannes Berg3a404142012-10-01 15:52:00 +02003252 break;
Johannes Berg24398e32012-03-28 10:58:36 +02003253 }
Johannes Berg04ecd252012-09-11 14:34:12 +02003254
3255 ht_cap_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY,
3256 cbss->information_elements,
3257 cbss->len_information_elements);
3258 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3259 ht_cap = (void *)(ht_cap_ie + 2);
3260 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3261 }
3262 sdata->needed_rx_chains = min(chains, local->rx_chains);
3263 } else {
3264 sdata->needed_rx_chains = 1;
Johannes Berg24398e32012-03-28 10:58:36 +02003265 }
3266
Johannes Berg04ecd252012-09-11 14:34:12 +02003267 /* will change later if needed */
3268 sdata->smps_mode = IEEE80211_SMPS_OFF;
3269
Johannes Berg55de9082012-07-26 17:24:39 +02003270 ieee80211_vif_release_channel(sdata);
3271 return ieee80211_vif_use_channel(sdata, cbss->channel, channel_type,
3272 IEEE80211_CHANCTX_SHARED);
Johannes Bergb17166a2012-07-27 11:41:27 +02003273}
3274
3275static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3276 struct cfg80211_bss *cbss, bool assoc)
3277{
3278 struct ieee80211_local *local = sdata->local;
3279 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3280 struct ieee80211_bss *bss = (void *)cbss->priv;
3281 struct sta_info *new_sta = NULL;
3282 bool have_sta = false;
3283 int err;
3284
3285 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3286 return -EINVAL;
3287
3288 if (assoc) {
3289 rcu_read_lock();
3290 have_sta = sta_info_get(sdata, cbss->bssid);
3291 rcu_read_unlock();
3292 }
3293
3294 if (!have_sta) {
3295 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3296 if (!new_sta)
3297 return -ENOMEM;
3298 }
3299
3300 mutex_lock(&local->mtx);
3301 ieee80211_recalc_idle(sdata->local);
3302 mutex_unlock(&local->mtx);
3303
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003304 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003305 u32 rates = 0, basic_rates = 0;
3306 bool have_higher_than_11mbit;
3307 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003308 struct ieee80211_supported_band *sband;
3309
3310 sband = local->hw.wiphy->bands[cbss->channel->band];
3311
3312 err = ieee80211_prep_channel(sdata, cbss);
3313 if (err) {
3314 sta_info_free(local, new_sta);
3315 return err;
3316 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003317
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003318 ieee80211_get_rates(sband, bss->supp_rates,
3319 bss->supp_rates_len,
3320 &rates, &basic_rates,
3321 &have_higher_than_11mbit,
3322 &min_rate, &min_rate_index);
3323
3324 /*
3325 * This used to be a workaround for basic rates missing
3326 * in the association response frame. Now that we no
3327 * longer use the basic rates from there, it probably
3328 * doesn't happen any more, but keep the workaround so
3329 * in case some *other* APs are buggy in different ways
3330 * we can connect -- with a warning.
3331 */
3332 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003333 sdata_info(sdata,
3334 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003335 basic_rates = BIT(min_rate_index);
3336 }
3337
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003338 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003339 sdata->vif.bss_conf.basic_rates = basic_rates;
3340
3341 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003342 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003343 have_higher_than_11mbit)
3344 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3345 else
3346 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3347
3348 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3349
Johannes Berg8c358bc2012-05-22 22:13:05 +02003350 /* set timing information */
3351 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3352 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3353 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3354
3355 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003356 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003357 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3358 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003359
3360 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003361 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003362
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003363 err = sta_info_insert(new_sta);
3364 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003365 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003366 sdata_info(sdata,
3367 "failed to insert STA entry for the AP (error %d)\n",
3368 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003369 return err;
3370 }
3371 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003372 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003373
3374 return 0;
3375}
3376
Johannes Berg77fdaa12009-07-07 03:45:17 +02003377/* config hooks */
3378int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3379 struct cfg80211_auth_request *req)
3380{
Johannes Berg66e67e42012-01-20 13:55:27 +01003381 struct ieee80211_local *local = sdata->local;
3382 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3383 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003384 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003385 int err;
3386
3387 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003388
3389 switch (req->auth_type) {
3390 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3391 auth_alg = WLAN_AUTH_OPEN;
3392 break;
3393 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003394 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003395 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003396 auth_alg = WLAN_AUTH_SHARED_KEY;
3397 break;
3398 case NL80211_AUTHTYPE_FT:
3399 auth_alg = WLAN_AUTH_FT;
3400 break;
3401 case NL80211_AUTHTYPE_NETWORK_EAP:
3402 auth_alg = WLAN_AUTH_LEAP;
3403 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003404 case NL80211_AUTHTYPE_SAE:
3405 auth_alg = WLAN_AUTH_SAE;
3406 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003407 default:
3408 return -EOPNOTSUPP;
3409 }
3410
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003411 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3412 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003413 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003414 return -ENOMEM;
3415
Johannes Berg66e67e42012-01-20 13:55:27 +01003416 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003417
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003418 if (req->sae_data_len >= 4) {
3419 __le16 *pos = (__le16 *) req->sae_data;
3420 auth_data->sae_trans = le16_to_cpu(pos[0]);
3421 auth_data->sae_status = le16_to_cpu(pos[1]);
3422 memcpy(auth_data->data, req->sae_data + 4,
3423 req->sae_data_len - 4);
3424 auth_data->data_len += req->sae_data_len - 4;
3425 }
3426
Johannes Berg77fdaa12009-07-07 03:45:17 +02003427 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003428 memcpy(&auth_data->data[auth_data->data_len],
3429 req->ie, req->ie_len);
3430 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003431 }
3432
Johannes Bergfffd0932009-07-08 14:22:54 +02003433 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003434 auth_data->key_len = req->key_len;
3435 auth_data->key_idx = req->key_idx;
3436 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003437 }
3438
Johannes Berg66e67e42012-01-20 13:55:27 +01003439 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003440
Johannes Berg66e67e42012-01-20 13:55:27 +01003441 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003442
Johannes Berg66e67e42012-01-20 13:55:27 +01003443 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003444
Johannes Berg66e67e42012-01-20 13:55:27 +01003445 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3446 ifmgd->assoc_data) {
3447 err = -EBUSY;
3448 goto err_free;
3449 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003450
Johannes Berg66e67e42012-01-20 13:55:27 +01003451 if (ifmgd->auth_data)
3452 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003453
Johannes Berg66e67e42012-01-20 13:55:27 +01003454 /* prep auth_data so we don't go into idle on disassoc */
3455 ifmgd->auth_data = auth_data;
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
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003460 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003461
Johannes Berga1cf7752012-03-08 15:02:07 +01003462 err = ieee80211_prep_connection(sdata, req->bss, false);
3463 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003464 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003465
Johannes Berg66e67e42012-01-20 13:55:27 +01003466 err = ieee80211_probe_auth(sdata);
3467 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003468 sta_info_destroy_addr(sdata, req->bss->bssid);
3469 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003470 }
3471
Johannes Berg66e67e42012-01-20 13:55:27 +01003472 /* hold our own reference */
3473 cfg80211_ref_bss(auth_data->bss);
3474 err = 0;
3475 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003476
Johannes Berg66e67e42012-01-20 13:55:27 +01003477 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003478 memset(ifmgd->bssid, 0, ETH_ALEN);
3479 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003480 ifmgd->auth_data = NULL;
3481 err_free:
3482 kfree(auth_data);
3483 out_unlock:
3484 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003485
Johannes Berg66e67e42012-01-20 13:55:27 +01003486 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003487}
3488
Johannes Berg77fdaa12009-07-07 03:45:17 +02003489int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3490 struct cfg80211_assoc_request *req)
3491{
Johannes Berg66e67e42012-01-20 13:55:27 +01003492 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003493 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003494 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003495 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003496 struct ieee80211_supported_band *sband;
Johannes Berg9dde6422012-05-16 23:43:19 +02003497 const u8 *ssidie, *ht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003498 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003499
Johannes Berg66e67e42012-01-20 13:55:27 +01003500 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3501 if (!ssidie)
3502 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003503
Johannes Berg66e67e42012-01-20 13:55:27 +01003504 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3505 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003506 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003507
Johannes Berg66e67e42012-01-20 13:55:27 +01003508 mutex_lock(&ifmgd->mtx);
3509
3510 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003511 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003512
3513 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3514 err = -EBUSY;
3515 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003516 }
3517
Johannes Berg66e67e42012-01-20 13:55:27 +01003518 if (ifmgd->assoc_data) {
3519 err = -EBUSY;
3520 goto err_free;
3521 }
3522
3523 if (ifmgd->auth_data) {
3524 bool match;
3525
3526 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003527 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003528 ieee80211_destroy_auth_data(sdata, match);
3529 }
3530
3531 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003532
3533 /*
3534 * keep only the 40 MHz disable bit set as it might have
3535 * been set during authentication already, all other bits
3536 * should be reset for a new connection
3537 */
3538 ifmgd->flags &= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003539
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003540 ifmgd->beacon_crc_valid = false;
3541
Johannes Bergde5036a2012-03-08 15:02:03 +01003542 /*
3543 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3544 * We still associate in non-HT mode (11a/b/g) if any one of these
3545 * ciphers is configured as pairwise.
3546 * We can set this to true for non-11n hardware, that'll be checked
3547 * separately along with the peer capabilities.
3548 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003549 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003550 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3551 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003552 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003553 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003554 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003555 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003556 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003557 }
3558 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003559
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003560 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Ben Greearef96a8422011-11-18 11:32:00 -08003561 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003562 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3563 }
Ben Greearef96a8422011-11-18 11:32:00 -08003564
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003565 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3566 sband = local->hw.wiphy->bands[req->bss->channel->band];
3567 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003568 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003569 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg1b49de22012-07-27 10:29:14 +02003570 if (!bss->wmm_used)
3571 netdev_info(sdata->dev,
3572 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003573 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003574
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003575 /* disable VHT if we don't support it or the AP doesn't use WMM */
3576 if (!sband->vht_cap.vht_supported ||
3577 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3578 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003579 if (!bss->wmm_used)
3580 netdev_info(sdata->dev,
3581 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003582 }
3583
Ben Greearef96a8422011-11-18 11:32:00 -08003584 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3585 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3586 sizeof(ifmgd->ht_capa_mask));
3587
Johannes Berg77fdaa12009-07-07 03:45:17 +02003588 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003589 memcpy(assoc_data->ie, req->ie, req->ie_len);
3590 assoc_data->ie_len = req->ie_len;
3591 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003592
Johannes Berg66e67e42012-01-20 13:55:27 +01003593 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003594
Johannes Bergaf6b6372009-12-23 13:15:35 +01003595 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3596 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003597 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003598 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003599 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003600 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003601 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003602
Johannes Berg66e67e42012-01-20 13:55:27 +01003603 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003604 assoc_data->wmm = bss->wmm_used &&
3605 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003606 assoc_data->supp_rates = bss->supp_rates;
3607 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003608
3609 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3610 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3611 assoc_data->ap_ht_param =
3612 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3613 else
3614 ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
Johannes Berg63f170e2009-12-23 13:15:33 +01003615
Kalle Valoab133152010-01-12 10:42:31 +02003616 if (bss->wmm_used && bss->uapsd_supported &&
3617 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003618 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003619 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3620 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003621 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003622 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3623 }
3624
Johannes Berg66e67e42012-01-20 13:55:27 +01003625 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3626 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003627
Johannes Berg77fdaa12009-07-07 03:45:17 +02003628 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003629 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003630
3631 if (req->use_mfp) {
3632 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3633 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3634 } else {
3635 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3636 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3637 }
3638
3639 if (req->crypto.control_port)
3640 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3641 else
3642 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3643
Johannes Berga621fa42010-08-27 14:26:54 +03003644 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3645 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3646
Johannes Berg66e67e42012-01-20 13:55:27 +01003647 /* kick off associate process */
3648
3649 ifmgd->assoc_data = assoc_data;
3650
Johannes Berga1cf7752012-03-08 15:02:07 +01003651 err = ieee80211_prep_connection(sdata, req->bss, true);
3652 if (err)
3653 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003654
3655 if (!bss->dtim_period &&
3656 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3657 /*
3658 * Wait up to one beacon interval ...
3659 * should this be more if we miss one?
3660 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003661 sdata_info(sdata, "waiting for beacon from %pM\n",
3662 ifmgd->bssid);
Johannes Berg3f9768a2012-03-28 21:02:46 +02003663 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +01003664 } else {
3665 assoc_data->have_beacon = true;
3666 assoc_data->sent_assoc = false;
3667 assoc_data->timeout = jiffies;
3668 }
3669 run_again(ifmgd, assoc_data->timeout);
3670
Paul Stewartfcff4f12012-02-23 17:59:53 -08003671 if (bss->corrupt_data) {
3672 char *corrupt_type = "data";
3673 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3674 if (bss->corrupt_data &
3675 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3676 corrupt_type = "beacon and probe response";
3677 else
3678 corrupt_type = "beacon";
3679 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3680 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003681 sdata_info(sdata, "associating with AP with corrupt %s\n",
3682 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08003683 }
3684
Johannes Berg66e67e42012-01-20 13:55:27 +01003685 err = 0;
3686 goto out;
3687 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003688 memset(ifmgd->bssid, 0, ETH_ALEN);
3689 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003690 ifmgd->assoc_data = NULL;
3691 err_free:
3692 kfree(assoc_data);
3693 out:
3694 mutex_unlock(&ifmgd->mtx);
3695
3696 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003697}
3698
3699int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003700 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003701{
3702 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003703 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02003704 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01003705 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003706
Johannes Berg77fdaa12009-07-07 03:45:17 +02003707 mutex_lock(&ifmgd->mtx);
3708
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003709 sdata_info(sdata,
3710 "deauthenticating from %pM by local choice (reason=%d)\n",
3711 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003712
Johannes Berg86552012012-10-29 09:46:31 +01003713 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003714 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01003715 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3716 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02003717 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01003718 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01003719 ieee80211_destroy_auth_data(sdata, false);
3720 mutex_unlock(&ifmgd->mtx);
3721
3722 sent_frame = tx;
3723 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003724 }
3725
Johannes Berg86552012012-10-29 09:46:31 +01003726 if (ifmgd->associated &&
3727 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
3728 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3729 req->reason_code, tx, frame_buf);
3730 sent_frame = tx;
3731 }
Johannes Berg37ad3882012-02-24 13:50:54 +01003732 mutex_unlock(&ifmgd->mtx);
3733
Johannes Berg86552012012-10-29 09:46:31 +01003734 out:
Johannes Berg7da7cc12010-08-05 17:02:38 +02003735 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003736 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003737 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003738
Johannes Berg86552012012-10-29 09:46:31 +01003739 if (sent_frame)
3740 __cfg80211_send_deauth(sdata->dev, frame_buf,
3741 IEEE80211_DEAUTH_FRAME_LEN);
3742
Johannes Berg77fdaa12009-07-07 03:45:17 +02003743 return 0;
3744}
3745
3746int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003747 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003748{
3749 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003750 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003751 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003752
Johannes Berg77fdaa12009-07-07 03:45:17 +02003753 mutex_lock(&ifmgd->mtx);
3754
Johannes Berg8d8b2612009-07-25 11:58:36 +02003755 /*
3756 * cfg80211 should catch this ... but it's racy since
3757 * we can receive a disassoc frame, process it, hand it
3758 * to cfg80211 while that's in a locked section already
3759 * trying to tell us that the user wants to disconnect.
3760 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003761 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003762 mutex_unlock(&ifmgd->mtx);
3763 return -ENOLINK;
3764 }
3765
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003766 sdata_info(sdata,
3767 "disassociating from %pM by local choice (reason=%d)\n",
3768 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003769
Jouni Malinene69e95d2010-03-29 23:29:31 -07003770 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01003771 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
3772 req->reason_code, !req->local_state_change,
3773 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003774 mutex_unlock(&ifmgd->mtx);
3775
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003776 __cfg80211_send_disassoc(sdata->dev, frame_buf,
3777 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01003778
Johannes Berg7da7cc12010-08-05 17:02:38 +02003779 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003780 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003781 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003782
Johannes Berg77fdaa12009-07-07 03:45:17 +02003783 return 0;
3784}
Jouni Malinen026331c2010-02-15 12:53:10 +02003785
Eliad Pellerafa762f2012-04-23 14:45:15 +03003786void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01003787{
3788 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3789
3790 mutex_lock(&ifmgd->mtx);
3791 if (ifmgd->assoc_data)
3792 ieee80211_destroy_assoc_data(sdata, false);
3793 if (ifmgd->auth_data)
3794 ieee80211_destroy_auth_data(sdata, false);
3795 del_timer_sync(&ifmgd->timer);
3796 mutex_unlock(&ifmgd->mtx);
3797}
3798
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003799void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
3800 enum nl80211_cqm_rssi_threshold_event rssi_event,
3801 gfp_t gfp)
3802{
3803 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3804
Johannes Bergb5878a22010-04-07 16:48:40 +02003805 trace_api_cqm_rssi_notify(sdata, rssi_event);
3806
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02003807 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
3808}
3809EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);