blob: 7d253a02217333d1c8d29e5ca35e524ae9124d18 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg66e67e42012-01-20 13:55:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_MAX_TRIES 3
35#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37#define IEEE80211_ASSOC_MAX_TRIES 3
38
Ben Greear180205b2011-02-04 15:30:24 -080039static int max_nullfunc_tries = 2;
40module_param(max_nullfunc_tries, int, 0644);
41MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44static int max_probe_tries = 5;
45module_param(max_probe_tries, int, 0644);
46MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020048
49/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010050 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020056 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010057#define IEEE80211_BEACON_LOSS_COUNT 7
58
Johannes Bergb291ba12009-07-10 15:29:03 +020059/*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030063#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
Ben Greear180205b2011-02-04 15:30:24 -080069static int probe_wait_ms = 500;
70module_param(probe_wait_ms, int, 0644);
71MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070074
Jouni Malinen17e4ec12010-03-29 23:28:30 -070075/*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80#define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
Jouni Malinen391a2002010-08-27 22:22:00 +030082/*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
Johannes Berg5bb644a2009-05-17 11:40:42 +020088#define TMR_RUNNING_TIMER 0
89#define TMR_RUNNING_CHANSW 1
90
Johannes Berg77fdaa12009-07-07 03:45:17 +020091/*
92 * All cfg80211 functions have to be called outside a locked
93 * section so that they can acquire a lock themselves... This
94 * is much simpler than queuing up things in cfg80211, but we
95 * do need some indirection for that here.
96 */
97enum rx_mgmt_action {
98 /* no action required */
99 RX_MGMT_NONE,
100
Johannes Berg77fdaa12009-07-07 03:45:17 +0200101 /* caller must call cfg80211_send_deauth() */
102 RX_MGMT_CFG80211_DEAUTH,
103
104 /* caller must call cfg80211_send_disassoc() */
105 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100106
107 /* caller must call cfg80211_send_rx_auth() */
108 RX_MGMT_CFG80211_RX_AUTH,
109
110 /* caller must call cfg80211_send_rx_assoc() */
111 RX_MGMT_CFG80211_RX_ASSOC,
112
113 /* caller must call cfg80211_send_assoc_timeout() */
114 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200115};
116
Johannes Berg5484e232008-09-08 17:44:27 +0200117/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200118static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
119{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200120 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200121}
122
Johannes Bergca386f32009-07-10 02:39:48 +0200123/*
124 * We can have multiple work items (and connection probing)
125 * scheduling this timer, but we need to take care to only
126 * reschedule it when it should fire _earlier_ than it was
127 * asked for before, or if it's not pending right now. This
128 * function ensures that. Note that it then is required to
129 * run this function for all timeouts after the first one
130 * has happened -- the work that runs from this timer will
131 * do that.
132 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100133static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200134{
135 ASSERT_MGD_MTX(ifmgd);
136
137 if (!timer_pending(&ifmgd->timer) ||
138 time_before(timeout, ifmgd->timer.expires))
139 mod_timer(&ifmgd->timer, timeout);
140}
141
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400142void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200143{
Johannes Bergc1288b12012-01-19 09:29:57 +0100144 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145 return;
146
Johannes Berg7eeff742012-07-18 10:27:27 +0200147 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
148 return;
149
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100151 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200152}
153
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400154void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
155{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100158 if (unlikely(!sdata->u.mgd.associated))
159 return;
160
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400161 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
162 return;
163
164 mod_timer(&sdata->u.mgd.conn_mon_timer,
165 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400166
167 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400168}
169
Johannes Berg5484e232008-09-08 17:44:27 +0200170static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200171{
Johannes Berg5484e232008-09-08 17:44:27 +0200172 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200173}
174
Johannes Berg24398e32012-03-28 10:58:36 +0200175static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
176 struct ieee80211_ht_operation *ht_oper,
177 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200178{
179 struct ieee80211_local *local = sdata->local;
180 struct ieee80211_supported_band *sband;
Johannes 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 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100194 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200195 rcu_read_unlock();
196 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200197
Johannes Berg4bf88532012-11-09 11:39:59 +0100198 switch (sdata->vif.bss_conf.chandef.width) {
199 case NL80211_CHAN_WIDTH_40:
200 if (sdata->vif.bss_conf.chandef.chan->center_freq >
201 sdata->vif.bss_conf.chandef.center_freq1 &&
202 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200203 disable_40 = true;
Johannes Berg4bf88532012-11-09 11:39:59 +0100204 if (sdata->vif.bss_conf.chandef.chan->center_freq <
205 sdata->vif.bss_conf.chandef.center_freq1 &&
206 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200207 disable_40 = true;
208 break;
209 default:
210 break;
211 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200212
Johannes Berg24398e32012-03-28 10:58:36 +0200213 /* This can change during the lifetime of the BSS */
214 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200215 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200216
Johannes Berg64f68e52012-03-28 10:58:37 +0200217 mutex_lock(&local->sta_mtx);
218 sta = sta_info_get(sdata, bssid);
219
220 WARN_ON_ONCE(!sta);
221
222 if (sta && !sta->supports_40mhz)
223 disable_40 = true;
224
225 if (sta && (!reconfig ||
Rajkumar Manoharan91a00992012-04-25 20:24:24 +0530226 (disable_40 != !(sta->sta.ht_cap.cap &
Johannes Berg64f68e52012-03-28 10:58:37 +0200227 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530228
Johannes Berg64f68e52012-03-28 10:58:37 +0200229 if (disable_40)
230 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
231 else
232 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530233
Johannes Berg64f68e52012-03-28 10:58:37 +0200234 rate_control_rate_update(local, sband, sta,
Johannes Berg8f727ef2012-03-30 08:43:32 +0200235 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200236 }
Johannes Berg64f68e52012-03-28 10:58:37 +0200237 mutex_unlock(&local->sta_mtx);
Johannes Bergd5522e02009-03-30 13:23:35 +0200238
Johannes Berg074d46d2012-03-15 19:45:16 +0100239 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200240
241 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200242 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200243 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200244 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200245 }
246
247 return changed;
248}
249
Johannes Berg9c6bd792008-09-11 00:01:52 +0200250/* frame sending functions */
251
Johannes Berg66e67e42012-01-20 13:55:27 +0100252static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
253 struct ieee80211_supported_band *sband,
254 u32 *rates)
255{
256 int i, j, count;
257 *rates = 0;
258 count = 0;
259 for (i = 0; i < supp_rates_len; i++) {
260 int rate = (supp_rates[i] & 0x7F) * 5;
261
262 for (j = 0; j < sband->n_bitrates; j++)
263 if (sband->bitrates[j].bitrate == rate) {
264 *rates |= BIT(j);
265 count++;
266 break;
267 }
268 }
269
270 return count;
271}
272
273static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200274 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100275 struct ieee80211_supported_band *sband,
276 struct ieee80211_channel *channel,
277 enum ieee80211_smps_mode smps)
278{
Johannes Berg66e67e42012-01-20 13:55:27 +0100279 u8 *pos;
280 u32 flags = channel->flags;
281 u16 cap;
282 struct ieee80211_sta_ht_cap ht_cap;
283
284 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
285
Johannes Berg66e67e42012-01-20 13:55:27 +0100286 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
287 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
288
Johannes Berg66e67e42012-01-20 13:55:27 +0100289 /* determine capability flags */
290 cap = ht_cap.cap;
291
Johannes Berg9dde6422012-05-16 23:43:19 +0200292 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100293 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
294 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
295 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
296 cap &= ~IEEE80211_HT_CAP_SGI_40;
297 }
298 break;
299 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
300 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
301 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
302 cap &= ~IEEE80211_HT_CAP_SGI_40;
303 }
304 break;
305 }
306
Johannes Berg24398e32012-03-28 10:58:36 +0200307 /*
308 * If 40 MHz was disabled associate as though we weren't
309 * capable of 40 MHz -- some broken APs will never fall
310 * back to trying to transmit in 20 MHz.
311 */
312 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
313 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
314 cap &= ~IEEE80211_HT_CAP_SGI_40;
315 }
316
Johannes Berg66e67e42012-01-20 13:55:27 +0100317 /* set SM PS mode properly */
318 cap &= ~IEEE80211_HT_CAP_SM_PS;
319 switch (smps) {
320 case IEEE80211_SMPS_AUTOMATIC:
321 case IEEE80211_SMPS_NUM_MODES:
322 WARN_ON(1);
323 case IEEE80211_SMPS_OFF:
324 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
325 IEEE80211_HT_CAP_SM_PS_SHIFT;
326 break;
327 case IEEE80211_SMPS_STATIC:
328 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
329 IEEE80211_HT_CAP_SM_PS_SHIFT;
330 break;
331 case IEEE80211_SMPS_DYNAMIC:
332 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
333 IEEE80211_HT_CAP_SM_PS_SHIFT;
334 break;
335 }
336
337 /* reserve and fill IE */
338 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
339 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
340}
341
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000342static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
343 struct sk_buff *skb,
344 struct ieee80211_supported_band *sband)
345{
346 u8 *pos;
347 u32 cap;
348 struct ieee80211_sta_vht_cap vht_cap;
349
350 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
351
352 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
353
354 /* determine capability flags */
355 cap = vht_cap.cap;
356
Johannes Bergf2d9d272012-11-22 14:11:39 +0100357 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
358 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
359 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
360 }
361
362 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
363 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
364 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
365 }
366
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000367 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000368 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000369 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
370}
371
Johannes Berg66e67e42012-01-20 13:55:27 +0100372static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
373{
374 struct ieee80211_local *local = sdata->local;
375 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
376 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
377 struct sk_buff *skb;
378 struct ieee80211_mgmt *mgmt;
379 u8 *pos, qos_info;
380 size_t offset = 0, noffset;
381 int i, count, rates_len, supp_rates_len;
382 u16 capab;
383 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200384 struct ieee80211_chanctx_conf *chanctx_conf;
385 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100386 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100387
388 lockdep_assert_held(&ifmgd->mtx);
389
Johannes Berg55de9082012-07-26 17:24:39 +0200390 rcu_read_lock();
391 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
392 if (WARN_ON(!chanctx_conf)) {
393 rcu_read_unlock();
394 return;
395 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100396 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200397 rcu_read_unlock();
398 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100399
400 if (assoc_data->supp_rates_len) {
401 /*
402 * Get all rates supported by the device and the AP as
403 * some APs don't like getting a superset of their rates
404 * in the association request (e.g. D-Link DAP 1353 in
405 * b-only mode)...
406 */
407 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
408 assoc_data->supp_rates_len,
409 sband, &rates);
410 } else {
411 /*
412 * In case AP not provide any supported rates information
413 * before association, we send information element(s) with
414 * all rates that we support.
415 */
416 rates = ~0;
417 rates_len = sband->n_bitrates;
418 }
419
420 skb = alloc_skb(local->hw.extra_tx_headroom +
421 sizeof(*mgmt) + /* bit too much but doesn't matter */
422 2 + assoc_data->ssid_len + /* SSID */
423 4 + rates_len + /* (extended) rates */
424 4 + /* power capability */
425 2 + 2 * sband->n_channels + /* supported channels */
426 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000427 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100428 assoc_data->ie_len + /* extra IEs */
429 9, /* WMM */
430 GFP_KERNEL);
431 if (!skb)
432 return;
433
434 skb_reserve(skb, local->hw.extra_tx_headroom);
435
436 capab = WLAN_CAPABILITY_ESS;
437
438 if (sband->band == IEEE80211_BAND_2GHZ) {
439 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
440 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
441 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
442 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
443 }
444
445 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
446 capab |= WLAN_CAPABILITY_PRIVACY;
447
448 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
449 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
450 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
451
452 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
453 memset(mgmt, 0, 24);
454 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
455 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
456 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
457
458 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
459 skb_put(skb, 10);
460 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
461 IEEE80211_STYPE_REASSOC_REQ);
462 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
463 mgmt->u.reassoc_req.listen_interval =
464 cpu_to_le16(local->hw.conf.listen_interval);
465 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
466 ETH_ALEN);
467 } else {
468 skb_put(skb, 4);
469 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
470 IEEE80211_STYPE_ASSOC_REQ);
471 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
472 mgmt->u.assoc_req.listen_interval =
473 cpu_to_le16(local->hw.conf.listen_interval);
474 }
475
476 /* SSID */
477 pos = skb_put(skb, 2 + assoc_data->ssid_len);
478 *pos++ = WLAN_EID_SSID;
479 *pos++ = assoc_data->ssid_len;
480 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
481
482 /* add all rates which were marked to be used above */
483 supp_rates_len = rates_len;
484 if (supp_rates_len > 8)
485 supp_rates_len = 8;
486
487 pos = skb_put(skb, supp_rates_len + 2);
488 *pos++ = WLAN_EID_SUPP_RATES;
489 *pos++ = supp_rates_len;
490
491 count = 0;
492 for (i = 0; i < sband->n_bitrates; i++) {
493 if (BIT(i) & rates) {
494 int rate = sband->bitrates[i].bitrate;
495 *pos++ = (u8) (rate / 5);
496 if (++count == 8)
497 break;
498 }
499 }
500
501 if (rates_len > count) {
502 pos = skb_put(skb, rates_len - count + 2);
503 *pos++ = WLAN_EID_EXT_SUPP_RATES;
504 *pos++ = rates_len - count;
505
506 for (i++; i < sband->n_bitrates; i++) {
507 if (BIT(i) & rates) {
508 int rate = sband->bitrates[i].bitrate;
509 *pos++ = (u8) (rate / 5);
510 }
511 }
512 }
513
514 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
515 /* 1. power capabilities */
516 pos = skb_put(skb, 4);
517 *pos++ = WLAN_EID_PWR_CAPABILITY;
518 *pos++ = 2;
519 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200520 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100521
522 /* 2. supported channels */
523 /* TODO: get this in reg domain format */
524 pos = skb_put(skb, 2 * sband->n_channels + 2);
525 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
526 *pos++ = 2 * sband->n_channels;
527 for (i = 0; i < sband->n_channels; i++) {
528 *pos++ = ieee80211_frequency_to_channel(
529 sband->channels[i].center_freq);
530 *pos++ = 1; /* one channel in the subband*/
531 }
532 }
533
534 /* if present, add any custom IEs that go before HT */
535 if (assoc_data->ie_len && assoc_data->ie) {
536 static const u8 before_ht[] = {
537 WLAN_EID_SSID,
538 WLAN_EID_SUPP_RATES,
539 WLAN_EID_EXT_SUPP_RATES,
540 WLAN_EID_PWR_CAPABILITY,
541 WLAN_EID_SUPPORTED_CHANNELS,
542 WLAN_EID_RSN,
543 WLAN_EID_QOS_CAPA,
544 WLAN_EID_RRM_ENABLED_CAPABILITIES,
545 WLAN_EID_MOBILITY_DOMAIN,
546 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
547 };
548 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
549 before_ht, ARRAY_SIZE(before_ht),
550 offset);
551 pos = skb_put(skb, noffset - offset);
552 memcpy(pos, assoc_data->ie + offset, noffset - offset);
553 offset = noffset;
554 }
555
Johannes Bergf2d9d272012-11-22 14:11:39 +0100556 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
557 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
558 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
559
Johannes Berga8243b72012-11-22 14:32:09 +0100560 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200561 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200562 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100563
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000564 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
565 ieee80211_add_vht_ie(sdata, skb, sband);
566
Johannes Berg66e67e42012-01-20 13:55:27 +0100567 /* if present, add any custom non-vendor IEs that go after HT */
568 if (assoc_data->ie_len && assoc_data->ie) {
569 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
570 assoc_data->ie_len,
571 offset);
572 pos = skb_put(skb, noffset - offset);
573 memcpy(pos, assoc_data->ie + offset, noffset - offset);
574 offset = noffset;
575 }
576
Johannes Berg76f03032012-03-08 15:02:05 +0100577 if (assoc_data->wmm) {
578 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200579 qos_info = ifmgd->uapsd_queues;
580 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100581 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
582 } else {
583 qos_info = 0;
584 }
585
586 pos = skb_put(skb, 9);
587 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
588 *pos++ = 7; /* len */
589 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
590 *pos++ = 0x50;
591 *pos++ = 0xf2;
592 *pos++ = 2; /* WME */
593 *pos++ = 0; /* WME info */
594 *pos++ = 1; /* WME ver */
595 *pos++ = qos_info;
596 }
597
598 /* add any remaining custom (i.e. vendor specific here) IEs */
599 if (assoc_data->ie_len && assoc_data->ie) {
600 noffset = assoc_data->ie_len;
601 pos = skb_put(skb, noffset - offset);
602 memcpy(pos, assoc_data->ie + offset, noffset - offset);
603 }
604
Johannes Berga1845fc2012-06-27 13:18:36 +0200605 drv_mgd_prepare_tx(local, sdata);
606
Johannes Berg66e67e42012-01-20 13:55:27 +0100607 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
608 ieee80211_tx_skb(sdata, skb);
609}
610
Kalle Valo572e0012009-02-10 17:09:31 +0200611void ieee80211_send_pspoll(struct ieee80211_local *local,
612 struct ieee80211_sub_if_data *sdata)
613{
Kalle Valo572e0012009-02-10 17:09:31 +0200614 struct ieee80211_pspoll *pspoll;
615 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200616
Kalle Valod8cd1892010-01-05 20:16:26 +0200617 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
618 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200619 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200620
Kalle Valod8cd1892010-01-05 20:16:26 +0200621 pspoll = (struct ieee80211_pspoll *) skb->data;
622 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200623
Johannes Berg62ae67b2009-11-18 18:42:05 +0100624 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
625 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200626}
627
Johannes Berg965beda2009-04-16 13:17:24 +0200628void ieee80211_send_nullfunc(struct ieee80211_local *local,
629 struct ieee80211_sub_if_data *sdata,
630 int powersave)
631{
632 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200633 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530634 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200635
Kalle Valod8cd1892010-01-05 20:16:26 +0200636 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
637 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200638 return;
639
Kalle Valod8cd1892010-01-05 20:16:26 +0200640 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200641 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200642 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200643
Johannes Berg62ae67b2009-11-18 18:42:05 +0100644 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530645 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
646 IEEE80211_STA_CONNECTION_POLL))
647 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
648
Johannes Berg62ae67b2009-11-18 18:42:05 +0100649 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200650}
651
Felix Fietkaud5242152010-01-08 18:06:26 +0100652static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
653 struct ieee80211_sub_if_data *sdata)
654{
655 struct sk_buff *skb;
656 struct ieee80211_hdr *nullfunc;
657 __le16 fc;
658
659 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
660 return;
661
662 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700663 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100664 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700665
Felix Fietkaud5242152010-01-08 18:06:26 +0100666 skb_reserve(skb, local->hw.extra_tx_headroom);
667
668 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
669 memset(nullfunc, 0, 30);
670 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
671 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
672 nullfunc->frame_control = fc;
673 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
674 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
675 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
676 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
677
678 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
679 ieee80211_tx_skb(sdata, skb);
680}
681
Johannes Bergcc32abd2009-05-15 11:52:31 +0200682/* spectrum management related things */
683static void ieee80211_chswitch_work(struct work_struct *work)
684{
685 struct ieee80211_sub_if_data *sdata =
686 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200687 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
688
Johannes Berg9607e6b2009-12-23 13:15:31 +0100689 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200690 return;
691
Johannes Berg77fdaa12009-07-07 03:45:17 +0200692 mutex_lock(&ifmgd->mtx);
693 if (!ifmgd->associated)
694 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200695
Johannes Berg55de9082012-07-26 17:24:39 +0200696 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200697 if (!sdata->local->ops->channel_switch) {
698 /* call "hw_config" only if doing sw channel switch */
699 ieee80211_hw_config(sdata->local,
700 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300701 } else {
702 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200703 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200704 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200705
Johannes Berg77fdaa12009-07-07 03:45:17 +0200706 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200707 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200708
Johannes Berg57eebdf2012-08-01 15:50:46 +0200709 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200710 ieee80211_wake_queues_by_reason(&sdata->local->hw,
711 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200712 out:
713 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
714 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200715}
716
Johannes Berg5ce6e432010-05-11 16:20:57 +0200717void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
718{
Johannes Berg55de9082012-07-26 17:24:39 +0200719 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
720 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200721
722 trace_api_chswitch_done(sdata, success);
723 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200724 sdata_info(sdata,
725 "driver channel switch failed, disconnecting\n");
726 ieee80211_queue_work(&sdata->local->hw,
727 &ifmgd->csa_connection_drop_work);
728 } else {
729 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200730 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200731}
732EXPORT_SYMBOL(ieee80211_chswitch_done);
733
Johannes Bergcc32abd2009-05-15 11:52:31 +0200734static void ieee80211_chswitch_timer(unsigned long data)
735{
736 struct ieee80211_sub_if_data *sdata =
737 (struct ieee80211_sub_if_data *) data;
738 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
739
Johannes Berg5bb644a2009-05-17 11:40:42 +0200740 if (sdata->local->quiescing) {
741 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
742 return;
743 }
744
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400745 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200746}
747
748void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
749 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200750 struct ieee80211_bss *bss,
751 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200752{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100753 struct cfg80211_bss *cbss =
754 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200755 struct ieee80211_channel *new_ch;
756 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900757 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
758 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200759 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200760
Johannes Berg77fdaa12009-07-07 03:45:17 +0200761 ASSERT_MGD_MTX(ifmgd);
762
763 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200764 return;
765
Helmut Schaafbe9c422009-07-23 12:14:04 +0200766 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200767 return;
768
769 /* Disregard subsequent beacons if we are already running a timer
770 processing a CSA */
771
772 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
773 return;
774
775 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +0200776 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
777 sdata_info(sdata,
778 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
779 ifmgd->associated->bssid, new_freq);
780 ieee80211_queue_work(&sdata->local->hw,
781 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200782 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200783 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200784
Johannes Berg57eebdf2012-08-01 15:50:46 +0200785 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
786
Johannes Berg55de9082012-07-26 17:24:39 +0200787 if (sdata->local->use_chanctx) {
788 sdata_info(sdata,
789 "not handling channel switch with channel contexts\n");
790 ieee80211_queue_work(&sdata->local->hw,
791 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +0100792 return;
Johannes Berg55de9082012-07-26 17:24:39 +0200793 }
794
795 mutex_lock(&sdata->local->chanctx_mtx);
796 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
797 mutex_unlock(&sdata->local->chanctx_mtx);
798 return;
799 }
800 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
801 struct ieee80211_chanctx, conf);
802 if (chanctx->refcount > 1) {
803 sdata_info(sdata,
804 "channel switch with multiple interfaces on the same channel, disconnecting\n");
805 ieee80211_queue_work(&sdata->local->hw,
806 &ifmgd->csa_connection_drop_work);
807 mutex_unlock(&sdata->local->chanctx_mtx);
808 return;
809 }
810 mutex_unlock(&sdata->local->chanctx_mtx);
811
812 sdata->local->csa_channel = new_ch;
813
Johannes Berg57eebdf2012-08-01 15:50:46 +0200814 if (sw_elem->mode)
815 ieee80211_stop_queues_by_reason(&sdata->local->hw,
816 IEEE80211_QUEUE_STOP_REASON_CSA);
817
Johannes Berg5ce6e432010-05-11 16:20:57 +0200818 if (sdata->local->ops->channel_switch) {
819 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200820 struct ieee80211_channel_switch ch_switch = {
821 .timestamp = timestamp,
822 .block_tx = sw_elem->mode,
823 .channel = new_ch,
824 .count = sw_elem->count,
825 };
826
Johannes Berg5ce6e432010-05-11 16:20:57 +0200827 drv_channel_switch(sdata->local, &ch_switch);
828 return;
829 }
830
831 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200832 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400833 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200834 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200835 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200836 TU_TO_EXP_TIME(sw_elem->count *
837 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200838}
839
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200840static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
841 struct ieee80211_channel *channel,
842 const u8 *country_ie, u8 country_ie_len,
843 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200844{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200845 struct ieee80211_country_ie_triplet *triplet;
846 int chan = ieee80211_frequency_to_channel(channel->center_freq);
847 int i, chan_pwr, chan_increment, new_ap_level;
848 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200849
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200850 /* Invalid IE */
851 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200852 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200853
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200854 triplet = (void *)(country_ie + 3);
855 country_ie_len -= 3;
856
857 switch (channel->band) {
858 default:
859 WARN_ON_ONCE(1);
860 /* fall through */
861 case IEEE80211_BAND_2GHZ:
862 case IEEE80211_BAND_60GHZ:
863 chan_increment = 1;
864 break;
865 case IEEE80211_BAND_5GHZ:
866 chan_increment = 4;
867 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200868 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200869
870 /* find channel */
871 while (country_ie_len >= 3) {
872 u8 first_channel = triplet->chans.first_channel;
873
874 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
875 goto next;
876
877 for (i = 0; i < triplet->chans.num_channels; i++) {
878 if (first_channel + i * chan_increment == chan) {
879 have_chan_pwr = true;
880 chan_pwr = triplet->chans.max_power;
881 break;
882 }
883 }
884 if (have_chan_pwr)
885 break;
886
887 next:
888 triplet++;
889 country_ie_len -= 3;
890 }
891
892 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200893 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200894
895 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
896
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200897 if (sdata->ap_power_level == new_ap_level)
898 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200899
900 sdata_info(sdata,
901 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
902 new_ap_level, chan_pwr, *pwr_constr_elem,
903 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200904 sdata->ap_power_level = new_ap_level;
905 if (__ieee80211_recalc_txpower(sdata))
906 return BSS_CHANGED_TXPOWER;
907 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200908}
909
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300910void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
911{
912 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
913 struct ieee80211_local *local = sdata->local;
914 struct ieee80211_conf *conf = &local->hw.conf;
915
916 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
917 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
918 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
919
920 local->disable_dynamic_ps = false;
921 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
922}
923EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
924
925void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
926{
927 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
928 struct ieee80211_local *local = sdata->local;
929 struct ieee80211_conf *conf = &local->hw.conf;
930
931 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
932 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
933 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
934
935 local->disable_dynamic_ps = true;
936 conf->dynamic_ps_timeout = 0;
937 del_timer_sync(&local->dynamic_ps_timer);
938 ieee80211_queue_work(&local->hw,
939 &local->dynamic_ps_enable_work);
940}
941EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
942
Johannes Berg965beda2009-04-16 13:17:24 +0200943/* powersave */
944static void ieee80211_enable_ps(struct ieee80211_local *local,
945 struct ieee80211_sub_if_data *sdata)
946{
947 struct ieee80211_conf *conf = &local->hw.conf;
948
Johannes Bergd5edaed2009-04-22 23:02:51 +0200949 /*
950 * If we are scanning right now then the parameters will
951 * take effect when scan finishes.
952 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200953 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200954 return;
955
Johannes Berg965beda2009-04-16 13:17:24 +0200956 if (conf->dynamic_ps_timeout > 0 &&
957 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
958 mod_timer(&local->dynamic_ps_timer, jiffies +
959 msecs_to_jiffies(conf->dynamic_ps_timeout));
960 } else {
961 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
962 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530963
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200964 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
965 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
966 return;
967
968 conf->flags |= IEEE80211_CONF_PS;
969 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200970 }
971}
972
973static void ieee80211_change_ps(struct ieee80211_local *local)
974{
975 struct ieee80211_conf *conf = &local->hw.conf;
976
977 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200978 ieee80211_enable_ps(local, local->ps_sdata);
979 } else if (conf->flags & IEEE80211_CONF_PS) {
980 conf->flags &= ~IEEE80211_CONF_PS;
981 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
982 del_timer_sync(&local->dynamic_ps_timer);
983 cancel_work_sync(&local->dynamic_ps_enable_work);
984 }
985}
986
Jason Young808118c2011-03-10 16:43:19 -0800987static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
988{
989 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
990 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200991 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800992
993 if (!mgd->powersave)
994 return false;
995
Johannes Berg05cb9102011-10-28 11:59:47 +0200996 if (mgd->broken_ap)
997 return false;
998
Jason Young808118c2011-03-10 16:43:19 -0800999 if (!mgd->associated)
1000 return false;
1001
Jason Young808118c2011-03-10 16:43:19 -08001002 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1003 IEEE80211_STA_CONNECTION_POLL))
1004 return false;
1005
1006 rcu_read_lock();
1007 sta = sta_info_get(sdata, mgd->bssid);
1008 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001009 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001010 rcu_read_unlock();
1011
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001012 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001013}
1014
Johannes Berg965beda2009-04-16 13:17:24 +02001015/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001016void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001017{
1018 struct ieee80211_sub_if_data *sdata, *found = NULL;
1019 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001020 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001021
1022 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1023 local->ps_sdata = NULL;
1024 return;
1025 }
1026
1027 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001028 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001029 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301030 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1031 /* If an AP vif is found, then disable PS
1032 * by setting the count to zero thereby setting
1033 * ps_sdata to NULL.
1034 */
1035 count = 0;
1036 break;
1037 }
Johannes Berg965beda2009-04-16 13:17:24 +02001038 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1039 continue;
1040 found = sdata;
1041 count++;
1042 }
1043
Jason Young808118c2011-03-10 16:43:19 -08001044 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001045 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001046 s32 beaconint_us;
1047
1048 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001049 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001050
1051 beaconint_us = ieee80211_tu_to_usec(
1052 found->vif.bss_conf.beacon_int);
1053
Juuso Oikarinenff616382010-06-09 09:51:52 +03001054 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001055 if (timeout < 0) {
1056 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001057 * Go to full PSM if the user configures a very low
1058 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001059 * The 2000 second value is there for compatibility
1060 * until the PM_QOS_NETWORK_LATENCY is configured
1061 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001062 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001063 if (latency > (1900 * USEC_PER_MSEC) &&
1064 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001065 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001066 else
1067 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001068 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001069 local->dynamic_ps_user_timeout = timeout;
1070 if (!local->disable_dynamic_ps)
1071 conf->dynamic_ps_timeout =
1072 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001073
Johannes Berg04fe2032009-04-22 18:44:37 +02001074 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001075 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001076 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001077 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001078 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001079
1080 /* If the TIM IE is invalid, pretend the value is 1 */
1081 if (!dtimper)
1082 dtimper = 1;
1083 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001084 maxslp = min_t(int, dtimper,
1085 latency / beaconint_us);
1086
Johannes Berg9ccebe62009-04-23 10:32:36 +02001087 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001088 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001089 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001090 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001091 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001092 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001093 }
Johannes Berg965beda2009-04-16 13:17:24 +02001094
1095 ieee80211_change_ps(local);
1096}
1097
Eliad Pellerab095872012-07-27 12:33:22 +03001098void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1099{
1100 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1101
1102 if (sdata->vif.bss_conf.ps != ps_allowed) {
1103 sdata->vif.bss_conf.ps = ps_allowed;
1104 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1105 }
1106}
1107
Johannes Berg965beda2009-04-16 13:17:24 +02001108void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1109{
1110 struct ieee80211_local *local =
1111 container_of(work, struct ieee80211_local,
1112 dynamic_ps_disable_work);
1113
1114 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1115 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1116 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1117 }
1118
1119 ieee80211_wake_queues_by_reason(&local->hw,
1120 IEEE80211_QUEUE_STOP_REASON_PS);
1121}
1122
1123void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1124{
1125 struct ieee80211_local *local =
1126 container_of(work, struct ieee80211_local,
1127 dynamic_ps_enable_work);
1128 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001129 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301130 unsigned long flags;
1131 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001132
1133 /* can only happen when PS was just disabled anyway */
1134 if (!sdata)
1135 return;
1136
Christian Lamparter5e340692011-06-30 21:08:43 +02001137 ifmgd = &sdata->u.mgd;
1138
Johannes Berg965beda2009-04-16 13:17:24 +02001139 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1140 return;
1141
Arik Nemtsov77b70232011-06-26 12:06:54 +03001142 if (!local->disable_dynamic_ps &&
1143 local->hw.conf.dynamic_ps_timeout > 0) {
1144 /* don't enter PS if TX frames are pending */
1145 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301146 mod_timer(&local->dynamic_ps_timer, jiffies +
1147 msecs_to_jiffies(
1148 local->hw.conf.dynamic_ps_timeout));
1149 return;
1150 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001151
1152 /*
1153 * transmission can be stopped by others which leads to
1154 * dynamic_ps_timer expiry. Postpone the ps timer if it
1155 * is not the actual idle state.
1156 */
1157 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1158 for (q = 0; q < local->hw.queues; q++) {
1159 if (local->queue_stop_reasons[q]) {
1160 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1161 flags);
1162 mod_timer(&local->dynamic_ps_timer, jiffies +
1163 msecs_to_jiffies(
1164 local->hw.conf.dynamic_ps_timeout));
1165 return;
1166 }
1167 }
1168 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301169 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301170
Vivek Natarajan375177b2010-02-09 14:50:28 +05301171 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301172 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301173 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001174
Vivek Natarajane8306f92011-04-06 11:41:10 +05301175 if (drv_tx_frames_pending(local))
1176 mod_timer(&local->dynamic_ps_timer, jiffies +
1177 msecs_to_jiffies(
1178 local->hw.conf.dynamic_ps_timeout));
1179 else {
1180 ieee80211_send_nullfunc(local, sdata, 1);
1181 /* Flush to get the tx status of nullfunc frame */
1182 drv_flush(local, false);
1183 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301184 }
1185
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001186 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1187 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301188 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1189 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1190 local->hw.conf.flags |= IEEE80211_CONF_PS;
1191 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1192 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301193
Arik Nemtsov77b70232011-06-26 12:06:54 +03001194 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1195 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001196}
1197
1198void ieee80211_dynamic_ps_timer(unsigned long data)
1199{
1200 struct ieee80211_local *local = (void *) data;
1201
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001202 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001203 return;
1204
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001205 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001206}
1207
Johannes Berg60f8b392008-09-08 17:44:22 +02001208/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001209static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001210 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001211 u8 *wmm_param, size_t wmm_param_len)
1212{
Jiri Bencf0706e82007-05-05 11:45:53 -07001213 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001214 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001215 size_t left;
1216 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001217 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001218
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001219 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001220 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001221
Johannes Berg32c5057b2012-03-28 11:04:29 +02001222 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001223 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001224
1225 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001226 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001227
Jiri Bencf0706e82007-05-05 11:45:53 -07001228 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001229 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001230
1231 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001232 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001233
Jiri Bencf0706e82007-05-05 11:45:53 -07001234 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001235 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001236 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001237 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001238
1239 pos = wmm_param + 8;
1240 left = wmm_param_len - 8;
1241
1242 memset(&params, 0, sizeof(params));
1243
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001244 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001245 for (; left >= 4; left -= 4, pos += 4) {
1246 int aci = (pos[0] >> 5) & 0x03;
1247 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001248 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001249 int queue;
1250
1251 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001252 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001253 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001254 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001255 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001256 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1257 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001258 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001259 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001260 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001261 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001262 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001263 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1264 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001265 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001266 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001267 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001268 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001269 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001270 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1271 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001272 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001273 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001274 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001275 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001276 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001277 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001278 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1279 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001280 break;
1281 }
1282
1283 params.aifs = pos[0] & 0x0f;
1284 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1285 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001286 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001287 params.uapsd = uapsd;
1288
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001289 mlme_dbg(sdata,
1290 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1291 queue, aci, acm,
1292 params.aifs, params.cw_min, params.cw_max,
1293 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001294 sdata->tx_conf[queue] = params;
1295 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001296 sdata_err(sdata,
1297 "failed to set TX queue parameters for queue %d\n",
1298 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001299 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001300
1301 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001302 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001303 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001304}
1305
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001306static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1307{
1308 lockdep_assert_held(&sdata->local->mtx);
1309
1310 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1311 IEEE80211_STA_BEACON_POLL);
1312 ieee80211_run_deferred_scan(sdata->local);
1313}
1314
1315static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1316{
1317 mutex_lock(&sdata->local->mtx);
1318 __ieee80211_stop_poll(sdata);
1319 mutex_unlock(&sdata->local->mtx);
1320}
1321
Johannes Berg7a5158e2008-10-08 10:59:33 +02001322static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1323 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001324{
Johannes Bergbda39332008-10-11 01:51:51 +02001325 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001326 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001327 bool use_protection;
1328 bool use_short_preamble;
1329 bool use_short_slot;
1330
1331 if (erp_valid) {
1332 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1333 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1334 } else {
1335 use_protection = false;
1336 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1337 }
1338
1339 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001340 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001341 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001342
Johannes Berg471b3ef2007-12-28 14:32:58 +01001343 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001344 bss_conf->use_cts_prot = use_protection;
1345 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001346 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001347
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001348 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001349 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001350 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001351 }
Daniel Draked9430a32007-07-27 15:43:24 +02001352
Johannes Berg7a5158e2008-10-08 10:59:33 +02001353 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001354 bss_conf->use_short_slot = use_short_slot;
1355 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001356 }
1357
1358 return changed;
1359}
1360
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001361static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001362 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001363 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001364{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001365 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001366 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001367 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001368
Johannes Bergae5eb022008-10-14 16:58:37 +02001369 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001370 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001371 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001372
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001373 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1374 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1375
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001376 sdata->u.mgd.associated = cbss;
1377 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001378
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001379 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1380
Johannes Berg488dd7b2012-10-29 20:08:01 +01001381 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001382 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001383
Johannes Berg9caf0362012-11-29 01:25:20 +01001384 rcu_read_lock();
1385 ies = rcu_dereference(cbss->ies);
1386 if (ies) {
1387 u8 noa[2];
1388 int ret;
1389
1390 ret = cfg80211_get_p2p_attr(
1391 ies->data, ies->len,
1392 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1393 noa, sizeof(noa));
1394 if (ret >= 2) {
1395 bss_conf->p2p_oppps = noa[1] & 0x80;
1396 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1397 bss_info_changed |= BSS_CHANGED_P2P_PS;
1398 sdata->u.mgd.p2p_noa_index = noa[0];
1399 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001400 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001401 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001402 }
1403
Johannes Bergb291ba12009-07-10 15:29:03 +02001404 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001405 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001406
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001407 ieee80211_led_assoc(local, 1);
1408
Johannes Berg826262c2012-12-10 16:38:14 +02001409 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
1410 /*
1411 * If the AP is buggy we may get here with no DTIM period
1412 * known, so assume it's 1 which is the only safe assumption
1413 * in that case, although if the TIM IE is broken powersave
1414 * probably just won't work at all.
1415 */
1416 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
1417 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001418 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001419 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001420
Juuso Oikarinen68542962010-06-09 13:43:26 +03001421 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001422
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001423 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001424 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001425 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001426 bss_info_changed |= BSS_CHANGED_CQM;
1427
Juuso Oikarinen68542962010-06-09 13:43:26 +03001428 /* Enable ARP filtering */
1429 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1430 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1431 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1432 }
1433
Johannes Bergae5eb022008-10-14 16:58:37 +02001434 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001435
Johannes Berg056508d2009-07-30 21:43:55 +02001436 mutex_lock(&local->iflist_mtx);
1437 ieee80211_recalc_ps(local, -1);
1438 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001439
Johannes Berg04ecd252012-09-11 14:34:12 +02001440 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001441 ieee80211_recalc_ps_vif(sdata);
1442
John W. Linville8a5b33f2010-01-06 15:39:39 -05001443 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001444 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001445}
1446
Jouni Malinene69e95d2010-03-29 23:29:31 -07001447static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001448 u16 stype, u16 reason, bool tx,
1449 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001450{
Johannes Berg46900292009-02-15 12:44:28 +01001451 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001452 struct ieee80211_local *local = sdata->local;
1453 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001454 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001455
Johannes Berg77fdaa12009-07-07 03:45:17 +02001456 ASSERT_MGD_MTX(ifmgd);
1457
Johannes Berg37ad3882012-02-24 13:50:54 +01001458 if (WARN_ON_ONCE(tx && !frame_buf))
1459 return;
1460
Johannes Bergb291ba12009-07-10 15:29:03 +02001461 if (WARN_ON(!ifmgd->associated))
1462 return;
1463
David Spinadel79543d82012-06-12 09:59:45 +03001464 ieee80211_stop_poll(sdata);
1465
Johannes Berg77fdaa12009-07-07 03:45:17 +02001466 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001467
1468 /*
1469 * we need to commit the associated = NULL change because the
1470 * scan code uses that to determine whether this iface should
1471 * go to/wake up from powersave or not -- and could otherwise
1472 * wake the queues erroneously.
1473 */
1474 smp_mb();
1475
1476 /*
1477 * Thus, we can only afterwards stop the queues -- to account
1478 * for the case where another CPU is finishing a scan at this
1479 * time -- we don't want the scan code to enable queues.
1480 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001481
John W. Linville8a5b33f2010-01-06 15:39:39 -05001482 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001483 netif_carrier_off(sdata->dev);
1484
Johannes Berg2a419052010-06-10 10:21:29 +02001485 mutex_lock(&local->sta_mtx);
Eliad Peller88a9e312012-06-01 11:14:03 +03001486 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301487 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001488 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg7f161142012-07-18 12:53:34 +02001489 ieee80211_sta_tear_down_BA_sessions(sta, false);
Sujith4cad6c72010-02-10 14:52:21 +05301490 }
Johannes Berg2a419052010-06-10 10:21:29 +02001491 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001492
Eliad Peller88bc40e2012-07-12 17:35:33 +03001493 /*
1494 * if we want to get out of ps before disassoc (why?) we have
1495 * to do it before sending disassoc, as otherwise the null-packet
1496 * won't be valid.
1497 */
1498 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1499 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1500 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1501 }
1502 local->ps_sdata = NULL;
1503
Eliad Pellerab095872012-07-27 12:33:22 +03001504 /* disable per-vif ps */
1505 ieee80211_recalc_ps_vif(sdata);
1506
Eliad Pellerf8239812012-06-27 14:18:22 +03001507 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1508 if (tx)
1509 drv_flush(local, false);
1510
Johannes Berg37ad3882012-02-24 13:50:54 +01001511 /* deauthenticate/disassociate now */
1512 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001513 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1514 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001515
1516 /* flush out frame */
1517 if (tx)
1518 drv_flush(local, false);
1519
Eliad Peller88a9e312012-06-01 11:14:03 +03001520 /* clear bssid only after building the needed mgmt frames */
1521 memset(ifmgd->bssid, 0, ETH_ALEN);
1522
Johannes Berg37ad3882012-02-24 13:50:54 +01001523 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001524 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001525
1526 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001527 changed |= ieee80211_reset_erp_info(sdata);
1528
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001529 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001530 changed |= BSS_CHANGED_ASSOC;
1531 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001532
Johannes Berg488dd7b2012-10-29 20:08:01 +01001533 sdata->vif.bss_conf.p2p_ctwindow = 0;
1534 sdata->vif.bss_conf.p2p_oppps = false;
1535
Johannes Berg413ad502009-05-08 21:21:06 +02001536 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001537 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1538 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001539
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001540 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301541
Kalle Valo520eb822008-12-18 23:35:27 +02001542 del_timer_sync(&local->dynamic_ps_timer);
1543 cancel_work_sync(&local->dynamic_ps_enable_work);
1544
Juuso Oikarinen68542962010-06-09 13:43:26 +03001545 /* Disable ARP filtering */
1546 if (sdata->vif.bss_conf.arp_filter_enabled) {
1547 sdata->vif.bss_conf.arp_filter_enabled = false;
1548 changed |= BSS_CHANGED_ARP_FILTER;
1549 }
1550
Johannes Berg3abead52012-03-02 15:56:59 +01001551 sdata->vif.bss_conf.qos = false;
1552 changed |= BSS_CHANGED_QOS;
1553
Johannes Berg0aaffa92010-05-05 15:28:27 +02001554 /* The BSSID (not really interesting) and HT changed */
1555 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001556 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001557
Johannes Berg3abead52012-03-02 15:56:59 +01001558 /* disassociated - set to defaults now */
1559 ieee80211_set_wmm_default(sdata, false);
1560
Johannes Bergb9dcf712010-08-27 12:35:54 +02001561 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1562 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1563 del_timer_sync(&sdata->u.mgd.timer);
1564 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001565
1566 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001567
Johannes Berg826262c2012-12-10 16:38:14 +02001568 sdata->vif.bss_conf.dtim_period = 0;
1569
Johannes Berg028e8da02012-11-26 11:57:41 +01001570 ifmgd->flags = 0;
1571 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001572}
Jiri Bencf0706e82007-05-05 11:45:53 -07001573
Kalle Valo3cf335d2009-03-22 21:57:06 +02001574void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1575 struct ieee80211_hdr *hdr)
1576{
1577 /*
1578 * We can postpone the mgd.timer whenever receiving unicast frames
1579 * from AP because we know that the connection is working both ways
1580 * at that time. But multicast frames (and hence also beacons) must
1581 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001582 * data idle periods for sending the periodic probe request to the
1583 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001584 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001585 if (is_multicast_ether_addr(hdr->addr1))
1586 return;
1587
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001588 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001589}
Jiri Bencf0706e82007-05-05 11:45:53 -07001590
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001591static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1592{
1593 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001594 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001595
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001596 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001597 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001598 IEEE80211_STA_CONNECTION_POLL))) {
1599 mutex_unlock(&local->mtx);
1600 return;
1601 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001602
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001603 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001604
1605 mutex_lock(&local->iflist_mtx);
1606 ieee80211_recalc_ps(local, -1);
1607 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001608
1609 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001610 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001611
1612 /*
1613 * We've received a probe response, but are not sure whether
1614 * we have or will be receiving any beacons or data, so let's
1615 * schedule the timers again, just in case.
1616 */
1617 ieee80211_sta_reset_beacon_monitor(sdata);
1618
1619 mod_timer(&ifmgd->conn_mon_timer,
1620 round_jiffies_up(jiffies +
1621 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001622out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001623 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001624}
1625
1626void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001627 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001628{
Felix Fietkau75706d02010-12-02 21:01:07 +01001629 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001630 return;
1631
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001632 if (ack)
1633 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001634
1635 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1636 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001637 if (ack)
1638 sdata->u.mgd.probe_send_count = 0;
1639 else
1640 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001641 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1642 }
1643}
1644
Maxim Levitskya43abf22009-07-31 18:54:12 +03001645static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1646{
1647 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1648 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001649 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001650 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001651
1652 /*
1653 * Try sending broadcast probe requests for the last three
1654 * probe requests after the first ones failed since some
1655 * buggy APs only support broadcast probe requests.
1656 */
1657 if (ifmgd->probe_send_count >= unicast_limit)
1658 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001659
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001660 /*
1661 * When the hardware reports an accurate Tx ACK status, it's
1662 * better to send a nullfunc frame instead of a probe request,
1663 * as it will kick us off the AP quickly if we aren't associated
1664 * anymore. The timeout will be reset if the frame is ACKed by
1665 * the AP.
1666 */
Soumik Das992e68b2012-05-20 15:31:13 +05301667 ifmgd->probe_send_count++;
1668
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001669 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1670 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001671 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001672 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001673 int ssid_len;
1674
Johannes Berg9caf0362012-11-29 01:25:20 +01001675 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001676 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001677 if (WARN_ON_ONCE(ssid == NULL))
1678 ssid_len = 0;
1679 else
1680 ssid_len = ssid[1];
1681
1682 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Bergfe94fe02012-07-30 12:26:34 +02001683 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02001684 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001685 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001686 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001687
Ben Greear180205b2011-02-04 15:30:24 -08001688 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001689 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301690 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1691 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001692}
1693
Johannes Bergb291ba12009-07-10 15:29:03 +02001694static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1695 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001696{
Kalle Valo04de8382009-03-22 21:57:35 +02001697 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001698 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001699
Johannes Berg9607e6b2009-12-23 13:15:31 +01001700 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001701 return;
1702
Johannes Berg77fdaa12009-07-07 03:45:17 +02001703 mutex_lock(&ifmgd->mtx);
1704
1705 if (!ifmgd->associated)
1706 goto out;
1707
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001708 mutex_lock(&sdata->local->mtx);
1709
1710 if (sdata->local->tmp_channel || sdata->local->scanning) {
1711 mutex_unlock(&sdata->local->mtx);
1712 goto out;
1713 }
1714
Joe Perchese87cc472012-05-13 21:56:26 +00001715 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001716 mlme_dbg_ratelimited(sdata,
1717 "detected beacon loss from AP - sending probe request\n");
1718
Holger Schurig6efb71b2012-05-15 15:38:59 +02001719 ieee80211_cqm_rssi_notify(&sdata->vif,
1720 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001721
Johannes Bergb291ba12009-07-10 15:29:03 +02001722 /*
1723 * The driver/our work has already reported this event or the
1724 * connection monitoring has kicked in and we have already sent
1725 * a probe request. Or maybe the AP died and the driver keeps
1726 * reporting until we disassociate...
1727 *
1728 * In either case we have to ignore the current call to this
1729 * function (except for setting the correct probe reason bit)
1730 * because otherwise we would reset the timer every time and
1731 * never check whether we received a probe response!
1732 */
1733 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1734 IEEE80211_STA_CONNECTION_POLL))
1735 already = true;
1736
1737 if (beacon)
1738 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1739 else
1740 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1741
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001742 mutex_unlock(&sdata->local->mtx);
1743
Johannes Bergb291ba12009-07-10 15:29:03 +02001744 if (already)
1745 goto out;
1746
Johannes Berg4e751842009-06-10 15:16:52 +02001747 mutex_lock(&sdata->local->iflist_mtx);
1748 ieee80211_recalc_ps(sdata->local, -1);
1749 mutex_unlock(&sdata->local->iflist_mtx);
1750
Maxim Levitskya43abf22009-07-31 18:54:12 +03001751 ifmgd->probe_send_count = 0;
1752 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001753 out:
1754 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001755}
1756
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001757struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1758 struct ieee80211_vif *vif)
1759{
1760 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1761 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001762 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001763 struct sk_buff *skb;
1764 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001765 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001766
1767 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1768 return NULL;
1769
1770 ASSERT_MGD_MTX(ifmgd);
1771
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001772 if (ifmgd->associated)
1773 cbss = ifmgd->associated;
1774 else if (ifmgd->auth_data)
1775 cbss = ifmgd->auth_data->bss;
1776 else if (ifmgd->assoc_data)
1777 cbss = ifmgd->assoc_data->bss;
1778 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001779 return NULL;
1780
Johannes Berg9caf0362012-11-29 01:25:20 +01001781 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001782 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001783 if (WARN_ON_ONCE(ssid == NULL))
1784 ssid_len = 0;
1785 else
1786 ssid_len = ssid[1];
1787
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001788 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001789 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001790 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001791 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001792 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001793
1794 return skb;
1795}
1796EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1797
Johannes Berg882a7c62012-08-01 22:32:45 +02001798static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1799 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001800{
1801 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1802 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001803 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001804
1805 mutex_lock(&ifmgd->mtx);
1806 if (!ifmgd->associated) {
1807 mutex_unlock(&ifmgd->mtx);
1808 return;
1809 }
1810
Johannes Berg37ad3882012-02-24 13:50:54 +01001811 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1812 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001813 transmit_frame, frame_buf);
1814 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001815 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001816
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001817 /*
1818 * must be outside lock due to cfg80211,
1819 * but that's not a problem.
1820 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001821 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001822
1823 mutex_lock(&local->mtx);
1824 ieee80211_recalc_idle(local);
1825 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001826}
1827
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001828static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001829{
1830 struct ieee80211_sub_if_data *sdata =
1831 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001832 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1834 struct sta_info *sta;
1835
1836 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301837 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001838 sta = sta_info_get(sdata, ifmgd->bssid);
1839 if (sta)
1840 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301841 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001842 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001843
Johannes Berg882a7c62012-08-01 22:32:45 +02001844 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1845 sdata_info(sdata, "Connection to AP %pM lost\n",
1846 ifmgd->bssid);
1847 __ieee80211_disconnect(sdata, false);
1848 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001849 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001850 }
1851}
1852
1853static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1854{
1855 struct ieee80211_sub_if_data *sdata =
1856 container_of(work, struct ieee80211_sub_if_data,
1857 u.mgd.csa_connection_drop_work);
1858
1859 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1860 IEEE80211_QUEUE_STOP_REASON_CSA);
1861 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001862}
1863
Kalle Valo04de8382009-03-22 21:57:35 +02001864void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1865{
1866 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001867 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001868
Johannes Bergb5878a22010-04-07 16:48:40 +02001869 trace_api_beacon_loss(sdata);
1870
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001871 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1872 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001873}
1874EXPORT_SYMBOL(ieee80211_beacon_loss);
1875
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001876void ieee80211_connection_loss(struct ieee80211_vif *vif)
1877{
1878 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1879 struct ieee80211_hw *hw = &sdata->local->hw;
1880
Johannes Bergb5878a22010-04-07 16:48:40 +02001881 trace_api_connection_loss(sdata);
1882
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001883 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1884 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1885}
1886EXPORT_SYMBOL(ieee80211_connection_loss);
1887
1888
Johannes Berg66e67e42012-01-20 13:55:27 +01001889static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1890 bool assoc)
1891{
1892 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1893
1894 lockdep_assert_held(&sdata->u.mgd.mtx);
1895
Johannes Berg66e67e42012-01-20 13:55:27 +01001896 if (!assoc) {
1897 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1898
1899 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1900 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01001901 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001902 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001903 }
1904
1905 cfg80211_put_bss(auth_data->bss);
1906 kfree(auth_data);
1907 sdata->u.mgd.auth_data = NULL;
1908}
1909
1910static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1911 struct ieee80211_mgmt *mgmt, size_t len)
1912{
1913 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1914 u8 *pos;
1915 struct ieee802_11_elems elems;
1916
1917 pos = mgmt->u.auth.variable;
1918 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1919 if (!elems.challenge)
1920 return;
1921 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001922 drv_mgd_prepare_tx(sdata->local, sdata);
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001923 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001924 elems.challenge - 2, elems.challenge_len + 2,
1925 auth_data->bss->bssid, auth_data->bss->bssid,
1926 auth_data->key, auth_data->key_len,
1927 auth_data->key_idx);
1928}
1929
1930static enum rx_mgmt_action __must_check
1931ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1932 struct ieee80211_mgmt *mgmt, size_t len)
1933{
1934 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1935 u8 bssid[ETH_ALEN];
1936 u16 auth_alg, auth_transaction, status_code;
1937 struct sta_info *sta;
1938
1939 lockdep_assert_held(&ifmgd->mtx);
1940
1941 if (len < 24 + 6)
1942 return RX_MGMT_NONE;
1943
1944 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1945 return RX_MGMT_NONE;
1946
1947 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1948
Joe Perchesb203ca32012-05-08 18:56:52 +00001949 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001950 return RX_MGMT_NONE;
1951
1952 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1953 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1954 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1955
1956 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001957 auth_transaction != ifmgd->auth_data->expected_transaction) {
1958 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1959 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1960 auth_transaction,
1961 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001962 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001963 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001964
1965 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001966 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1967 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001968 ieee80211_destroy_auth_data(sdata, false);
1969 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001970 }
1971
1972 switch (ifmgd->auth_data->algorithm) {
1973 case WLAN_AUTH_OPEN:
1974 case WLAN_AUTH_LEAP:
1975 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001976 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01001977 break;
1978 case WLAN_AUTH_SHARED_KEY:
1979 if (ifmgd->auth_data->expected_transaction != 4) {
1980 ieee80211_auth_challenge(sdata, mgmt, len);
1981 /* need another frame */
1982 return RX_MGMT_NONE;
1983 }
1984 break;
1985 default:
1986 WARN_ONCE(1, "invalid auth alg %d",
1987 ifmgd->auth_data->algorithm);
1988 return RX_MGMT_NONE;
1989 }
1990
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001991 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001992 ifmgd->auth_data->done = true;
1993 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1994 run_again(ifmgd, ifmgd->auth_data->timeout);
1995
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001996 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
1997 ifmgd->auth_data->expected_transaction != 2) {
1998 /*
1999 * Report auth frame to user space for processing since another
2000 * round of Authentication frames is still needed.
2001 */
2002 return RX_MGMT_CFG80211_RX_AUTH;
2003 }
2004
Johannes Berg66e67e42012-01-20 13:55:27 +01002005 /* move station state to auth */
2006 mutex_lock(&sdata->local->sta_mtx);
2007 sta = sta_info_get(sdata, bssid);
2008 if (!sta) {
2009 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2010 goto out_err;
2011 }
2012 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002013 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002014 goto out_err;
2015 }
2016 mutex_unlock(&sdata->local->sta_mtx);
2017
2018 return RX_MGMT_CFG80211_RX_AUTH;
2019 out_err:
2020 mutex_unlock(&sdata->local->sta_mtx);
2021 /* ignore frame -- wait for timeout */
2022 return RX_MGMT_NONE;
2023}
2024
2025
Johannes Berg77fdaa12009-07-07 03:45:17 +02002026static enum rx_mgmt_action __must_check
2027ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002028 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002029{
Johannes Berg46900292009-02-15 12:44:28 +01002030 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002031 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002032 u16 reason_code;
2033
Johannes Berg66e67e42012-01-20 13:55:27 +01002034 lockdep_assert_held(&ifmgd->mtx);
2035
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002036 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002037 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002038
Johannes Berg66e67e42012-01-20 13:55:27 +01002039 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002040 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002041 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002042
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002043 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002044
2045 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2046
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002047 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2048 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002049
Johannes Berg37ad3882012-02-24 13:50:54 +01002050 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2051
Johannes Berg7da7cc12010-08-05 17:02:38 +02002052 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01002053 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002054 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002055
2056 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002057}
2058
2059
Johannes Berg77fdaa12009-07-07 03:45:17 +02002060static enum rx_mgmt_action __must_check
2061ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2062 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002063{
Johannes Berg46900292009-02-15 12:44:28 +01002064 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002065 u16 reason_code;
2066
Johannes Berg66e67e42012-01-20 13:55:27 +01002067 lockdep_assert_held(&ifmgd->mtx);
2068
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002069 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002070 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002071
Johannes Berg66e67e42012-01-20 13:55:27 +01002072 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002073 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002074 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002075
2076 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2077
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002078 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2079 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002080
Johannes Berg37ad3882012-02-24 13:50:54 +01002081 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2082
Johannes Berg7da7cc12010-08-05 17:02:38 +02002083 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002084 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002085 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002086
Johannes Berg77fdaa12009-07-07 03:45:17 +02002087 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002088}
2089
Christian Lamparterc74d0842011-10-15 00:14:49 +02002090static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2091 u8 *supp_rates, unsigned int supp_rates_len,
2092 u32 *rates, u32 *basic_rates,
2093 bool *have_higher_than_11mbit,
2094 int *min_rate, int *min_rate_index)
2095{
2096 int i, j;
2097
2098 for (i = 0; i < supp_rates_len; i++) {
2099 int rate = (supp_rates[i] & 0x7f) * 5;
2100 bool is_basic = !!(supp_rates[i] & 0x80);
2101
2102 if (rate > 110)
2103 *have_higher_than_11mbit = true;
2104
2105 /*
2106 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2107 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2108 *
2109 * Note: Even through the membership selector and the basic
2110 * rate flag share the same bit, they are not exactly
2111 * the same.
2112 */
2113 if (!!(supp_rates[i] & 0x80) &&
2114 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2115 continue;
2116
2117 for (j = 0; j < sband->n_bitrates; j++) {
2118 if (sband->bitrates[j].bitrate == rate) {
2119 *rates |= BIT(j);
2120 if (is_basic)
2121 *basic_rates |= BIT(j);
2122 if (rate < *min_rate) {
2123 *min_rate = rate;
2124 *min_rate_index = j;
2125 }
2126 break;
2127 }
2128 }
2129 }
2130}
Jiri Bencf0706e82007-05-05 11:45:53 -07002131
Johannes Berg66e67e42012-01-20 13:55:27 +01002132static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2133 bool assoc)
2134{
2135 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2136
2137 lockdep_assert_held(&sdata->u.mgd.mtx);
2138
Johannes Berg66e67e42012-01-20 13:55:27 +01002139 if (!assoc) {
2140 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2141
2142 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2143 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002144 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002145 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002146 }
2147
2148 kfree(assoc_data);
2149 sdata->u.mgd.assoc_data = NULL;
2150}
2151
2152static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2153 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002154 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002155{
Johannes Berg46900292009-02-15 12:44:28 +01002156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002157 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002158 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002159 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002160 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002161 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002162 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002163 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002164 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002165 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002166
Johannes Bergaf6b6372009-12-23 13:15:35 +01002167 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002168
Jiri Bencf0706e82007-05-05 11:45:53 -07002169 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002170 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002171
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002172 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002173 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2174 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002175 aid &= ~(BIT(15) | BIT(14));
2176
Johannes Berg05cb9102011-10-28 11:59:47 +02002177 ifmgd->broken_ap = false;
2178
2179 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002180 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2181 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002182 aid = 0;
2183 ifmgd->broken_ap = true;
2184 }
2185
Johannes Bergaf6b6372009-12-23 13:15:35 +01002186 pos = mgmt->u.assoc_resp.variable;
2187 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2188
Jiri Bencf0706e82007-05-05 11:45:53 -07002189 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002190 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002191 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002192 }
2193
Johannes Berg46900292009-02-15 12:44:28 +01002194 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002195
Guy Eilam2a33bee2011-08-17 15:18:15 +03002196 mutex_lock(&sdata->local->sta_mtx);
2197 /*
2198 * station info was already allocated and inserted before
2199 * the association and should be available to us
2200 */
Johannes Berg7852e362012-01-20 13:55:24 +01002201 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002202 if (WARN_ON(!sta)) {
2203 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002204 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002205 }
2206
Johannes Berg55de9082012-07-26 17:24:39 +02002207 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002208
Johannes Berga8243b72012-11-22 14:32:09 +01002209 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002210 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002211 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002212
Johannes Berg64f68e52012-03-28 10:58:37 +02002213 sta->supports_40mhz =
2214 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2215
Mahesh Palivela818255e2012-10-10 11:33:04 +00002216 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2217 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2218 elems.vht_cap_elem,
2219 &sta->sta.vht_cap);
2220
Johannes Berg4b7679a2008-09-18 18:14:18 +02002221 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002222
Johannes Berg46900292009-02-15 12:44:28 +01002223 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002224 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002225
Johannes Bergddf4ac52008-10-22 11:41:38 +02002226 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002227 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002228
Johannes Bergc8987872012-01-20 13:55:17 +01002229 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2230 if (!err)
2231 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2232 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2233 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2234 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002235 sdata_info(sdata,
2236 "failed to move station %pM to desired state\n",
2237 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002238 WARN_ON(__sta_info_destroy(sta));
2239 mutex_unlock(&sdata->local->sta_mtx);
2240 return false;
2241 }
2242
Johannes Berg7852e362012-01-20 13:55:24 +01002243 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002244
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002245 /*
2246 * Always handle WMM once after association regardless
2247 * of the first value the AP uses. Setting -1 here has
2248 * that effect because the AP values is an unsigned
2249 * 4-bit value.
2250 */
2251 ifmgd->wmm_last_param_set = -1;
2252
Johannes Bergddf4ac52008-10-22 11:41:38 +02002253 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002254 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002255 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002256 else
Johannes Berg3abead52012-03-02 15:56:59 +01002257 ieee80211_set_wmm_default(sdata, false);
2258 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002259
Johannes Berg074d46d2012-03-15 19:45:16 +01002260 if (elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002261 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002262 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2263 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002264
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002265 /* set AID and assoc capability,
2266 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002267 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002268 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002269 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002270
Kalle Valo15b7b062009-03-22 21:57:14 +02002271 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002272 * If we're using 4-addr mode, let the AP know that we're
2273 * doing so, so that it can create the STA VLAN on its side
2274 */
2275 if (ifmgd->use_4addr)
2276 ieee80211_send_4addr_nullfunc(local, sdata);
2277
2278 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002279 * Start timer to probe the connection to the AP now.
2280 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002281 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002282 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002283 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002284
Johannes Bergaf6b6372009-12-23 13:15:35 +01002285 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002286}
2287
Johannes Berg66e67e42012-01-20 13:55:27 +01002288static enum rx_mgmt_action __must_check
2289ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2290 struct ieee80211_mgmt *mgmt, size_t len,
2291 struct cfg80211_bss **bss)
2292{
2293 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2294 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2295 u16 capab_info, status_code, aid;
2296 struct ieee802_11_elems elems;
2297 u8 *pos;
2298 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002299
Johannes Berg66e67e42012-01-20 13:55:27 +01002300 lockdep_assert_held(&ifmgd->mtx);
2301
2302 if (!assoc_data)
2303 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002304 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002305 return RX_MGMT_NONE;
2306
2307 /*
2308 * AssocResp and ReassocResp have identical structure, so process both
2309 * of them in this function.
2310 */
2311
2312 if (len < 24 + 6)
2313 return RX_MGMT_NONE;
2314
2315 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2316 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2317 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2318 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2319
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002320 sdata_info(sdata,
2321 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2322 reassoc ? "Rea" : "A", mgmt->sa,
2323 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002324
2325 pos = mgmt->u.assoc_resp.variable;
2326 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2327
2328 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2329 elems.timeout_int && elems.timeout_int_len == 5 &&
2330 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2331 u32 tu, ms;
2332 tu = get_unaligned_le32(elems.timeout_int + 1);
2333 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002334 sdata_info(sdata,
2335 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2336 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002337 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2338 if (ms > IEEE80211_ASSOC_TIMEOUT)
2339 run_again(ifmgd, assoc_data->timeout);
2340 return RX_MGMT_NONE;
2341 }
2342
2343 *bss = assoc_data->bss;
2344
2345 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002346 sdata_info(sdata, "%pM denied association (code=%d)\n",
2347 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002348 ieee80211_destroy_assoc_data(sdata, false);
2349 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002350 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2351 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002352 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002353 cfg80211_put_bss(*bss);
2354 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2355 }
John W. Linville635d9992012-07-09 16:34:34 -04002356 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002357
2358 /*
2359 * destroy assoc_data afterwards, as otherwise an idle
2360 * recalc after assoc_data is NULL but before associated
2361 * is set can cause the interface to go idle
2362 */
2363 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002364 }
2365
2366 return RX_MGMT_CFG80211_RX_ASSOC;
2367}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002368
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002369static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002370 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002371 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002372 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002373{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002374 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002375 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002376 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002377 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002378 bool need_ps = false;
2379
Johannes Berg826262c2012-12-10 16:38:14 +02002380 if ((sdata->u.mgd.associated &&
2381 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2382 (sdata->u.mgd.assoc_data &&
2383 ether_addr_equal(mgmt->bssid,
2384 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002385 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002386 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2387
2388 if (elems->tim && !elems->parse_error) {
2389 struct ieee80211_tim_ie *tim_ie = elems->tim;
2390 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2391 }
Johannes Berg56007a02010-01-26 14:19:52 +01002392 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002393
Johannes Berg5bda6172008-09-08 11:05:10 +02002394 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002395 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2396 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002397 else
2398 freq = rx_status->freq;
2399
2400 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2401
2402 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2403 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002404
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002405 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002406 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002407 if (bss)
2408 ieee80211_rx_bss_put(local, bss);
2409
2410 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002411 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002412
Johannes Berg56007a02010-01-26 14:19:52 +01002413 if (need_ps) {
2414 mutex_lock(&local->iflist_mtx);
2415 ieee80211_recalc_ps(local, -1);
2416 mutex_unlock(&local->iflist_mtx);
2417 }
2418
Johannes Berg5bc14202012-08-01 16:13:02 +02002419 if (elems->ch_switch_ie &&
2420 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2421 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002422 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002423}
2424
2425
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002426static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002427 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002428{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002429 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002430 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002431 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2432 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002433 struct ieee802_11_elems elems;
2434
Kalle Valo15b7b062009-03-22 21:57:14 +02002435 ifmgd = &sdata->u.mgd;
2436
Johannes Berg77fdaa12009-07-07 03:45:17 +02002437 ASSERT_MGD_MTX(ifmgd);
2438
Joe Perchesb203ca32012-05-08 18:56:52 +00002439 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002440 return; /* ignore ProbeResp to foreign address */
2441
Ester Kummerae6a44e2008-06-27 18:54:48 +03002442 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2443 if (baselen > len)
2444 return;
2445
2446 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2447 &elems);
2448
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002449 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002450
Johannes Berg77fdaa12009-07-07 03:45:17 +02002451 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002452 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002453 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002454
2455 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002456 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002457 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002458 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002459 ifmgd->auth_data->tries = 0;
2460 ifmgd->auth_data->timeout = jiffies;
2461 run_again(ifmgd, ifmgd->auth_data->timeout);
2462 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002463}
2464
Johannes Bergd91f36d2009-04-16 13:17:26 +02002465/*
2466 * This is the canonical list of information elements we care about,
2467 * the filter code also gives us all changes to the Microsoft OUI
2468 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2469 *
2470 * We implement beacon filtering in software since that means we can
2471 * avoid processing the frame here and in cfg80211, and userspace
2472 * will not be able to tell whether the hardware supports it or not.
2473 *
2474 * XXX: This list needs to be dynamic -- userspace needs to be able to
2475 * add items it requires. It also needs to be able to tell us to
2476 * look out for other vendor IEs.
2477 */
2478static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002479 (1ULL << WLAN_EID_COUNTRY) |
2480 (1ULL << WLAN_EID_ERP_INFO) |
2481 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2482 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2483 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002484 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002485
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002486static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002487 struct ieee80211_mgmt *mgmt,
2488 size_t len,
2489 struct ieee80211_rx_status *rx_status)
2490{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002491 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002492 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002493 size_t baselen;
2494 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002495 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002496 struct ieee80211_chanctx_conf *chanctx_conf;
2497 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002498 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002499 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002500 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002501 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002502 u8 *bssid;
2503
Johannes Berg66e67e42012-01-20 13:55:27 +01002504 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002505
Ester Kummerae6a44e2008-06-27 18:54:48 +03002506 /* Process beacon from the current BSS */
2507 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2508 if (baselen > len)
2509 return;
2510
Johannes Berg55de9082012-07-26 17:24:39 +02002511 rcu_read_lock();
2512 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2513 if (!chanctx_conf) {
2514 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002515 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002516 }
2517
Johannes Berg4bf88532012-11-09 11:39:59 +01002518 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002519 rcu_read_unlock();
2520 return;
2521 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002522 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002523 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002524
Johannes Berg66e67e42012-01-20 13:55:27 +01002525 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002526 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002527 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2528 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002529
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002530 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002531 ifmgd->assoc_data->have_beacon = true;
2532 ifmgd->assoc_data->sent_assoc = false;
2533 /* continue assoc process */
2534 ifmgd->assoc_data->timeout = jiffies;
2535 run_again(ifmgd, ifmgd->assoc_data->timeout);
2536 return;
2537 }
2538
2539 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002540 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002541 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002542 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002543
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002544 /* Track average RSSI from the Beacon frames of the current AP */
2545 ifmgd->last_beacon_signal = rx_status->signal;
2546 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2547 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002548 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002549 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002550 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002551 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002552 } else {
2553 ifmgd->ave_beacon_signal =
2554 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2555 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2556 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002557 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002558 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002559
2560 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2561 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2562 int sig = ifmgd->ave_beacon_signal;
2563 int last_sig = ifmgd->last_ave_beacon_signal;
2564
2565 /*
2566 * if signal crosses either of the boundaries, invoke callback
2567 * with appropriate parameters
2568 */
2569 if (sig > ifmgd->rssi_max_thold &&
2570 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2571 ifmgd->last_ave_beacon_signal = sig;
2572 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2573 } else if (sig < ifmgd->rssi_min_thold &&
2574 (last_sig >= ifmgd->rssi_max_thold ||
2575 last_sig == 0)) {
2576 ifmgd->last_ave_beacon_signal = sig;
2577 drv_rssi_callback(local, RSSI_EVENT_LOW);
2578 }
2579 }
2580
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002581 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002582 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002583 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002584 int sig = ifmgd->ave_beacon_signal / 16;
2585 int last_event = ifmgd->last_cqm_event_signal;
2586 int thold = bss_conf->cqm_rssi_thold;
2587 int hyst = bss_conf->cqm_rssi_hyst;
2588 if (sig < thold &&
2589 (last_event == 0 || sig < last_event - hyst)) {
2590 ifmgd->last_cqm_event_signal = sig;
2591 ieee80211_cqm_rssi_notify(
2592 &sdata->vif,
2593 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2594 GFP_KERNEL);
2595 } else if (sig > thold &&
2596 (last_event == 0 || sig > last_event + hyst)) {
2597 ifmgd->last_cqm_event_signal = sig;
2598 ieee80211_cqm_rssi_notify(
2599 &sdata->vif,
2600 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2601 GFP_KERNEL);
2602 }
2603 }
2604
Johannes Bergb291ba12009-07-10 15:29:03 +02002605 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002606 mlme_dbg_ratelimited(sdata,
2607 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002608 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002609 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002610 ieee80211_run_deferred_scan(local);
2611 mutex_unlock(&local->mtx);
2612
Johannes Berg4e751842009-06-10 15:16:52 +02002613 mutex_lock(&local->iflist_mtx);
2614 ieee80211_recalc_ps(local, -1);
2615 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002616 }
2617
Johannes Bergb291ba12009-07-10 15:29:03 +02002618 /*
2619 * Push the beacon loss detection into the future since
2620 * we are processing a beacon from the AP just now.
2621 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002622 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002623
Johannes Bergd91f36d2009-04-16 13:17:26 +02002624 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2625 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2626 len - baselen, &elems,
2627 care_about_ies, ncrc);
2628
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302629 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002630 bool directed_tim = ieee80211_check_tim(elems.tim,
2631 elems.tim_len,
2632 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002633 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002634 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002635 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2636 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2637 ieee80211_hw_config(local,
2638 IEEE80211_CONF_CHANGE_PS);
2639 }
Kalle Valo572e0012009-02-10 17:09:31 +02002640 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302641 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002642 local->pspolling = true;
2643
2644 /*
2645 * Here is assumed that the driver will be
2646 * able to send ps-poll frame and receive a
2647 * response even though power save mode is
2648 * enabled, but some drivers might require
2649 * to disable power save here. This needs
2650 * to be investigated.
2651 */
2652 ieee80211_send_pspoll(local, sdata);
2653 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002654 }
2655 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002656
Johannes Berg488dd7b2012-10-29 20:08:01 +01002657 if (sdata->vif.p2p) {
2658 u8 noa[2];
2659 int ret;
2660
2661 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2662 len - baselen,
2663 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2664 noa, sizeof(noa));
2665 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2666 bss_conf->p2p_oppps = noa[1] & 0x80;
2667 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2668 changed |= BSS_CHANGED_P2P_PS;
2669 sdata->u.mgd.p2p_noa_index = noa[0];
2670 /*
2671 * make sure we update all information, the CRC
2672 * mechanism doesn't look at P2P attributes.
2673 */
2674 ifmgd->beacon_crc_valid = false;
2675 }
2676 }
2677
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002678 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002679 return;
2680 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002681 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002682
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002683 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002684
2685 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2686 elems.wmm_param_len))
2687 changed |= BSS_CHANGED_QOS;
2688
Johannes Berg7a5158e2008-10-08 10:59:33 +02002689 if (elems.erp_info && elems.erp_info_len >= 1) {
2690 erp_valid = true;
2691 erp_value = elems.erp_info[0];
2692 } else {
2693 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002694 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002695 changed |= ieee80211_handle_bss_capability(sdata,
2696 le16_to_cpu(mgmt->u.beacon.capab_info),
2697 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002698
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002699
Johannes Berg074d46d2012-03-15 19:45:16 +01002700 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002701 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002702 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2703 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002704
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002705 if (elems.country_elem && elems.pwr_constr_elem &&
2706 mgmt->u.probe_resp.capab_info &
2707 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002708 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2709 elems.country_elem,
2710 elems.country_elem_len,
2711 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002712
Johannes Berg471b3ef2007-12-28 14:32:58 +01002713 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002714}
2715
Johannes Berg1fa57d02010-06-10 10:21:32 +02002716void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2717 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002718{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002719 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002720 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002721 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002722 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002723 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002724 u16 fc;
2725
Jiri Bencf0706e82007-05-05 11:45:53 -07002726 rx_status = (struct ieee80211_rx_status *) skb->cb;
2727 mgmt = (struct ieee80211_mgmt *) skb->data;
2728 fc = le16_to_cpu(mgmt->frame_control);
2729
Johannes Berg77fdaa12009-07-07 03:45:17 +02002730 mutex_lock(&ifmgd->mtx);
2731
Johannes Berg66e67e42012-01-20 13:55:27 +01002732 switch (fc & IEEE80211_FCTL_STYPE) {
2733 case IEEE80211_STYPE_BEACON:
2734 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2735 break;
2736 case IEEE80211_STYPE_PROBE_RESP:
2737 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2738 break;
2739 case IEEE80211_STYPE_AUTH:
2740 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2741 break;
2742 case IEEE80211_STYPE_DEAUTH:
2743 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2744 break;
2745 case IEEE80211_STYPE_DISASSOC:
2746 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2747 break;
2748 case IEEE80211_STYPE_ASSOC_RESP:
2749 case IEEE80211_STYPE_REASSOC_RESP:
2750 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2751 break;
2752 case IEEE80211_STYPE_ACTION:
2753 switch (mgmt->u.action.category) {
2754 case WLAN_CATEGORY_SPECTRUM_MGMT:
2755 ieee80211_sta_process_chanswitch(sdata,
2756 &mgmt->u.action.u.chan_switch.sw_elem,
2757 (void *)ifmgd->associated->priv,
2758 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002759 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002760 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002761 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002762 mutex_unlock(&ifmgd->mtx);
2763
Johannes Berg66e67e42012-01-20 13:55:27 +01002764 switch (rma) {
2765 case RX_MGMT_NONE:
2766 /* no action */
2767 break;
2768 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002769 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002770 break;
2771 case RX_MGMT_CFG80211_DISASSOC:
2772 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2773 break;
2774 case RX_MGMT_CFG80211_RX_AUTH:
2775 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2776 break;
2777 case RX_MGMT_CFG80211_RX_ASSOC:
2778 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2779 break;
2780 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2781 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2782 break;
2783 default:
2784 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002785 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002786}
2787
Johannes Berg9c6bd792008-09-11 00:01:52 +02002788static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002789{
2790 struct ieee80211_sub_if_data *sdata =
2791 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002792 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002793 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002794
Johannes Berg5bb644a2009-05-17 11:40:42 +02002795 if (local->quiescing) {
2796 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2797 return;
2798 }
2799
Johannes Berg64592c82010-06-10 10:21:31 +02002800 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002801}
2802
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002803static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002804 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002805{
2806 struct ieee80211_local *local = sdata->local;
2807 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002808 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002809
Johannes Berg37ad3882012-02-24 13:50:54 +01002810 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2811 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002812 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002813
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002814 /*
2815 * must be outside lock due to cfg80211,
2816 * but that's not a problem.
2817 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002818 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002819
2820 mutex_lock(&local->mtx);
2821 ieee80211_recalc_idle(local);
2822 mutex_unlock(&local->mtx);
2823
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002824 mutex_lock(&ifmgd->mtx);
2825}
2826
Johannes Berg66e67e42012-01-20 13:55:27 +01002827static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2828{
2829 struct ieee80211_local *local = sdata->local;
2830 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2831 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2832
2833 lockdep_assert_held(&ifmgd->mtx);
2834
2835 if (WARN_ON_ONCE(!auth_data))
2836 return -EINVAL;
2837
Johannes Berg66e67e42012-01-20 13:55:27 +01002838 auth_data->tries++;
2839
2840 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002841 sdata_info(sdata, "authentication with %pM timed out\n",
2842 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002843
2844 /*
2845 * Most likely AP is not in the range so remove the
2846 * bss struct for that AP.
2847 */
2848 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2849
2850 return -ETIMEDOUT;
2851 }
2852
Johannes Berga1845fc2012-06-27 13:18:36 +02002853 drv_mgd_prepare_tx(local, sdata);
2854
Johannes Berg66e67e42012-01-20 13:55:27 +01002855 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002856 u16 trans = 1;
2857 u16 status = 0;
2858
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002859 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2860 auth_data->bss->bssid, auth_data->tries,
2861 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002862
2863 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002864
2865 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2866 trans = auth_data->sae_trans;
2867 status = auth_data->sae_status;
2868 auth_data->expected_transaction = trans;
2869 }
2870
2871 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2872 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002873 auth_data->bss->bssid,
2874 auth_data->bss->bssid, NULL, 0, 0);
2875 } else {
2876 const u8 *ssidie;
2877
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002878 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2879 auth_data->bss->bssid, auth_data->tries,
2880 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002881
Johannes Berg9caf0362012-11-29 01:25:20 +01002882 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002883 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01002884 if (!ssidie) {
2885 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002886 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01002887 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002888 /*
2889 * Direct probe is sent to broadcast address as some APs
2890 * will not answer to direct packet in unassociated state.
2891 */
2892 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Bergfe94fe02012-07-30 12:26:34 +02002893 NULL, 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02002894 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002895 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002896 }
2897
2898 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2899 run_again(ifmgd, auth_data->timeout);
2900
2901 return 0;
2902}
2903
2904static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2905{
2906 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2907 struct ieee80211_local *local = sdata->local;
2908
2909 lockdep_assert_held(&sdata->u.mgd.mtx);
2910
Johannes Berg66e67e42012-01-20 13:55:27 +01002911 assoc_data->tries++;
2912 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002913 sdata_info(sdata, "association with %pM timed out\n",
2914 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002915
2916 /*
2917 * Most likely AP is not in the range so remove the
2918 * bss struct for that AP.
2919 */
2920 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2921
2922 return -ETIMEDOUT;
2923 }
2924
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002925 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2926 assoc_data->bss->bssid, assoc_data->tries,
2927 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002928 ieee80211_send_assoc(sdata);
2929
2930 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2931 run_again(&sdata->u.mgd, assoc_data->timeout);
2932
2933 return 0;
2934}
2935
Johannes Berg1fa57d02010-06-10 10:21:32 +02002936void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002937{
Johannes Berg60f8b392008-09-08 17:44:22 +02002938 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002939 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002940
Johannes Berg77fdaa12009-07-07 03:45:17 +02002941 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002942
Johannes Berg66e67e42012-01-20 13:55:27 +01002943 if (ifmgd->auth_data &&
2944 time_after(jiffies, ifmgd->auth_data->timeout)) {
2945 if (ifmgd->auth_data->done) {
2946 /*
2947 * ok ... we waited for assoc but userspace didn't,
2948 * so let's just kill the auth data
2949 */
2950 ieee80211_destroy_auth_data(sdata, false);
2951 } else if (ieee80211_probe_auth(sdata)) {
2952 u8 bssid[ETH_ALEN];
2953
2954 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2955
2956 ieee80211_destroy_auth_data(sdata, false);
2957
2958 mutex_unlock(&ifmgd->mtx);
2959 cfg80211_send_auth_timeout(sdata->dev, bssid);
2960 mutex_lock(&ifmgd->mtx);
2961 }
2962 } else if (ifmgd->auth_data)
2963 run_again(ifmgd, ifmgd->auth_data->timeout);
2964
2965 if (ifmgd->assoc_data &&
2966 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2967 if (!ifmgd->assoc_data->have_beacon ||
2968 ieee80211_do_assoc(sdata)) {
2969 u8 bssid[ETH_ALEN];
2970
2971 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2972
2973 ieee80211_destroy_assoc_data(sdata, false);
2974
2975 mutex_unlock(&ifmgd->mtx);
2976 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2977 mutex_lock(&ifmgd->mtx);
2978 }
2979 } else if (ifmgd->assoc_data)
2980 run_again(ifmgd, ifmgd->assoc_data->timeout);
2981
Johannes Bergb291ba12009-07-10 15:29:03 +02002982 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2983 IEEE80211_STA_CONNECTION_POLL) &&
2984 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002985 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002986 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002987
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002988 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002989
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002990 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002991 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002992 else
Ben Greear180205b2011-02-04 15:30:24 -08002993 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002994
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002995 /* ACK received for nullfunc probing frame */
2996 if (!ifmgd->probe_send_count)
2997 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002998 else if (ifmgd->nullfunc_failed) {
2999 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003000 mlme_dbg(sdata,
3001 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3002 bssid, ifmgd->probe_send_count,
3003 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003004 ieee80211_mgd_probe_ap_send(sdata);
3005 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003006 mlme_dbg(sdata,
3007 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3008 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003009 ieee80211_sta_connection_lost(sdata, bssid,
3010 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003011 }
3012 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003013 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003014 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003015 mlme_dbg(sdata,
3016 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3017 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003018 ieee80211_sta_connection_lost(sdata, bssid,
3019 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003020 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003021 mlme_dbg(sdata,
3022 "No probe response from AP %pM after %dms, try %d/%i\n",
3023 bssid, probe_wait_ms,
3024 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003025 ieee80211_mgd_probe_ap_send(sdata);
3026 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003027 /*
3028 * We actually lost the connection ... or did we?
3029 * Let's make sure!
3030 */
Ben Greearb38afa82010-10-07 16:12:06 -07003031 wiphy_debug(local->hw.wiphy,
3032 "%s: No probe response from AP %pM"
3033 " after %dms, disconnecting.\n",
3034 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003035 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003036
Johannes Berg95acac62011-07-12 12:30:59 +02003037 ieee80211_sta_connection_lost(sdata, bssid,
3038 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02003039 }
3040 }
3041
Johannes Berg77fdaa12009-07-07 03:45:17 +02003042 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003043
3044 mutex_lock(&local->mtx);
3045 ieee80211_recalc_idle(local);
3046 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003047}
Johannes Berg0a51b272008-09-08 17:44:25 +02003048
Johannes Bergb291ba12009-07-10 15:29:03 +02003049static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3050{
3051 struct ieee80211_sub_if_data *sdata =
3052 (struct ieee80211_sub_if_data *) data;
3053 struct ieee80211_local *local = sdata->local;
3054
3055 if (local->quiescing)
3056 return;
3057
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003058 ieee80211_queue_work(&sdata->local->hw,
3059 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003060}
3061
3062static void ieee80211_sta_conn_mon_timer(unsigned long data)
3063{
3064 struct ieee80211_sub_if_data *sdata =
3065 (struct ieee80211_sub_if_data *) data;
3066 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3067 struct ieee80211_local *local = sdata->local;
3068
3069 if (local->quiescing)
3070 return;
3071
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003072 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003073}
3074
3075static void ieee80211_sta_monitor_work(struct work_struct *work)
3076{
3077 struct ieee80211_sub_if_data *sdata =
3078 container_of(work, struct ieee80211_sub_if_data,
3079 u.mgd.monitor_work);
3080
3081 ieee80211_mgd_probe_ap(sdata, false);
3082}
3083
Johannes Berga1678f82008-09-11 00:01:47 +02003084static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3085{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003086 u32 flags;
3087
Kalle Vaload935682009-04-19 08:47:19 +03003088 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003089 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003090
Johannes Bergb291ba12009-07-10 15:29:03 +02003091 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003092 flags = sdata->local->hw.flags;
3093 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3094 ieee80211_queue_work(&sdata->local->hw,
3095 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003096 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003097 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003098 }
Johannes Berga1678f82008-09-11 00:01:47 +02003099}
3100
Johannes Berg5bb644a2009-05-17 11:40:42 +02003101#ifdef CONFIG_PM
3102void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3103{
3104 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3105
3106 /*
3107 * we need to use atomic bitops for the running bits
3108 * only because both timers might fire at the same
3109 * time -- the code here is properly synchronised.
3110 */
3111
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003112 cancel_work_sync(&ifmgd->request_smps_work);
3113
Johannes Berg0ecfe802011-11-09 12:14:16 +01003114 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003115 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003116 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003117 if (del_timer_sync(&ifmgd->timer))
3118 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3119
3120 cancel_work_sync(&ifmgd->chswitch_work);
3121 if (del_timer_sync(&ifmgd->chswitch_timer))
3122 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003123
Johannes Bergb291ba12009-07-10 15:29:03 +02003124 /* these will just be re-established on connection */
3125 del_timer_sync(&ifmgd->conn_mon_timer);
3126 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003127}
3128
3129void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3130{
3131 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3132
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303133 if (!ifmgd->associated)
3134 return;
3135
Johannes Berg95acac62011-07-12 12:30:59 +02003136 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3137 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3138 mutex_lock(&ifmgd->mtx);
3139 if (ifmgd->associated) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003140 mlme_dbg(sdata,
3141 "driver requested disconnect after resume\n");
Johannes Berg95acac62011-07-12 12:30:59 +02003142 ieee80211_sta_connection_lost(sdata,
3143 ifmgd->associated->bssid,
3144 WLAN_REASON_UNSPECIFIED);
3145 mutex_unlock(&ifmgd->mtx);
3146 return;
3147 }
3148 mutex_unlock(&ifmgd->mtx);
3149 }
3150
Johannes Berg5bb644a2009-05-17 11:40:42 +02003151 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3152 add_timer(&ifmgd->timer);
3153 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3154 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003155 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003156
3157 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003158 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003159 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003160}
3161#endif
3162
Johannes Berg9c6bd792008-09-11 00:01:52 +02003163/* interface setup */
3164void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3165{
Johannes Berg46900292009-02-15 12:44:28 +01003166 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003167
Johannes Berg46900292009-02-15 12:44:28 +01003168 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003169 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003170 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003171 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3172 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003173 INIT_WORK(&ifmgd->csa_connection_drop_work,
3174 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003175 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003176 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003177 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003178 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3179 (unsigned long) sdata);
3180 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3181 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003182 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303183 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003184
Johannes Bergab1faea2009-07-01 21:41:17 +02003185 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303186 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003187 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3188 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003189
Johannes Berg77fdaa12009-07-07 03:45:17 +02003190 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003191
3192 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3193 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3194 else
3195 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003196}
3197
3198/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003199void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3200{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003201 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003202
3203 /* Restart STA timers */
3204 rcu_read_lock();
3205 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3206 ieee80211_restart_sta_timer(sdata);
3207 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003208}
Johannes Berg10f644a2009-04-16 13:17:25 +02003209
3210int ieee80211_max_network_latency(struct notifier_block *nb,
3211 unsigned long data, void *dummy)
3212{
3213 s32 latency_usec = (s32) data;
3214 struct ieee80211_local *local =
3215 container_of(nb, struct ieee80211_local,
3216 network_latency_notifier);
3217
3218 mutex_lock(&local->iflist_mtx);
3219 ieee80211_recalc_ps(local, latency_usec);
3220 mutex_unlock(&local->iflist_mtx);
3221
3222 return 0;
3223}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003224
Johannes Bergf2d9d272012-11-22 14:11:39 +01003225static u32 chandef_downgrade(struct cfg80211_chan_def *c)
3226{
3227 u32 ret;
3228 int tmp;
3229
3230 switch (c->width) {
3231 case NL80211_CHAN_WIDTH_20:
3232 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3233 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3234 break;
3235 case NL80211_CHAN_WIDTH_40:
3236 c->width = NL80211_CHAN_WIDTH_20;
3237 c->center_freq1 = c->chan->center_freq;
3238 ret = IEEE80211_STA_DISABLE_40MHZ |
3239 IEEE80211_STA_DISABLE_VHT;
3240 break;
3241 case NL80211_CHAN_WIDTH_80:
3242 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3243 /* n_P40 */
3244 tmp /= 2;
3245 /* freq_P40 */
3246 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3247 c->width = NL80211_CHAN_WIDTH_40;
3248 ret = IEEE80211_STA_DISABLE_VHT;
3249 break;
3250 case NL80211_CHAN_WIDTH_80P80:
3251 c->center_freq2 = 0;
3252 c->width = NL80211_CHAN_WIDTH_80;
3253 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3254 IEEE80211_STA_DISABLE_160MHZ;
3255 break;
3256 case NL80211_CHAN_WIDTH_160:
3257 /* n_P20 */
3258 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3259 /* n_P80 */
3260 tmp /= 4;
3261 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3262 c->width = NL80211_CHAN_WIDTH_80;
3263 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3264 IEEE80211_STA_DISABLE_160MHZ;
3265 break;
3266 default:
3267 case NL80211_CHAN_WIDTH_20_NOHT:
3268 WARN_ON_ONCE(1);
3269 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3270 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3271 break;
3272 }
3273
3274 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3275
3276 return ret;
3277}
3278
3279static u32
3280ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
3281 struct ieee80211_supported_band *sband,
3282 struct ieee80211_channel *channel,
3283 const struct ieee80211_ht_operation *ht_oper,
3284 const struct ieee80211_vht_operation *vht_oper,
3285 struct cfg80211_chan_def *chandef)
3286{
3287 struct cfg80211_chan_def vht_chandef;
3288 u32 ht_cfreq, ret;
3289
3290 chandef->chan = channel;
3291 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3292 chandef->center_freq1 = channel->center_freq;
3293 chandef->center_freq2 = 0;
3294
3295 if (!ht_oper || !sband->ht_cap.ht_supported) {
3296 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3297 goto out;
3298 }
3299
3300 chandef->width = NL80211_CHAN_WIDTH_20;
3301
3302 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3303 channel->band);
3304 /* check that channel matches the right operating channel */
3305 if (channel->center_freq != ht_cfreq) {
3306 /*
3307 * It's possible that some APs are confused here;
3308 * Netgear WNDR3700 sometimes reports 4 higher than
3309 * the actual channel in association responses, but
3310 * since we look at probe response/beacon data here
3311 * it should be OK.
3312 */
3313 sdata_info(sdata,
3314 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3315 channel->center_freq, ht_cfreq,
3316 ht_oper->primary_chan, channel->band);
3317 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3318 goto out;
3319 }
3320
3321 /* check 40 MHz support, if we have it */
3322 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3323 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3324 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3325 chandef->width = NL80211_CHAN_WIDTH_40;
3326 chandef->center_freq1 += 10;
3327 break;
3328 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3329 chandef->width = NL80211_CHAN_WIDTH_40;
3330 chandef->center_freq1 -= 10;
3331 break;
3332 }
3333 } else {
3334 /* 40 MHz (and 80 MHz) must be supported for VHT */
3335 ret = IEEE80211_STA_DISABLE_VHT;
3336 goto out;
3337 }
3338
3339 if (!vht_oper || !sband->vht_cap.vht_supported) {
3340 ret = IEEE80211_STA_DISABLE_VHT;
3341 goto out;
3342 }
3343
3344 vht_chandef.chan = channel;
3345 vht_chandef.center_freq1 =
3346 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
3347 channel->band);
3348 vht_chandef.center_freq2 = 0;
3349
3350 if (vht_oper->center_freq_seg2_idx)
3351 vht_chandef.center_freq2 =
3352 ieee80211_channel_to_frequency(
3353 vht_oper->center_freq_seg2_idx,
3354 channel->band);
3355
3356 switch (vht_oper->chan_width) {
3357 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3358 vht_chandef.width = chandef->width;
3359 break;
3360 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3361 vht_chandef.width = NL80211_CHAN_WIDTH_80;
3362 break;
3363 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3364 vht_chandef.width = NL80211_CHAN_WIDTH_160;
3365 break;
3366 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3367 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
3368 break;
3369 default:
3370 sdata_info(sdata,
3371 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
3372 vht_oper->chan_width);
3373 ret = IEEE80211_STA_DISABLE_VHT;
3374 goto out;
3375 }
3376
3377 if (!cfg80211_chandef_valid(&vht_chandef)) {
3378 sdata_info(sdata,
3379 "AP VHT information is invalid, disable VHT\n");
3380 ret = IEEE80211_STA_DISABLE_VHT;
3381 goto out;
3382 }
3383
3384 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
3385 ret = 0;
3386 goto out;
3387 }
3388
3389 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
3390 sdata_info(sdata,
3391 "AP VHT information doesn't match HT, disable VHT\n");
3392 ret = IEEE80211_STA_DISABLE_VHT;
3393 goto out;
3394 }
3395
3396 *chandef = vht_chandef;
3397
3398 ret = 0;
3399
3400 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
3401 IEEE80211_CHAN_DISABLED)) {
3402 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
3403 ret = IEEE80211_STA_DISABLE_HT |
3404 IEEE80211_STA_DISABLE_VHT;
3405 goto out;
3406 }
3407
3408 ret = chandef_downgrade(chandef);
3409 }
3410
3411 if (chandef->width != vht_chandef.width)
3412 sdata_info(sdata,
3413 "local regulatory prevented using AP HT/VHT configuration, downgraded\n");
3414
3415out:
3416 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
3417 return ret;
3418}
3419
3420static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3421 struct cfg80211_bss *cbss)
3422{
3423 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3424 const u8 *ht_cap_ie, *vht_cap_ie;
3425 const struct ieee80211_ht_cap *ht_cap;
3426 const struct ieee80211_vht_cap *vht_cap;
3427 u8 chains = 1;
3428
3429 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3430 return chains;
3431
Johannes Berg9caf0362012-11-29 01:25:20 +01003432 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003433 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3434 ht_cap = (void *)(ht_cap_ie + 2);
3435 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3436 /*
3437 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3438 * "Tx Unequal Modulation Supported" fields.
3439 */
3440 }
3441
3442 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3443 return chains;
3444
Johannes Berg9caf0362012-11-29 01:25:20 +01003445 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003446 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3447 u8 nss;
3448 u16 tx_mcs_map;
3449
3450 vht_cap = (void *)(vht_cap_ie + 2);
3451 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3452 for (nss = 8; nss > 0; nss--) {
3453 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3454 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3455 break;
3456 }
3457 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3458 chains = max(chains, nss);
3459 }
3460
3461 return chains;
3462}
3463
Johannes Bergb17166a2012-07-27 11:41:27 +02003464static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3465 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003466{
3467 struct ieee80211_local *local = sdata->local;
3468 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003469 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003470 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003471 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003472 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003473 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003474
Johannes Berg24398e32012-03-28 10:58:36 +02003475 sband = local->hw.wiphy->bands[cbss->channel->band];
3476
Johannes Bergf2d9d272012-11-22 14:11:39 +01003477 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3478 IEEE80211_STA_DISABLE_80P80MHZ |
3479 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003480
Johannes Berg9caf0362012-11-29 01:25:20 +01003481 rcu_read_lock();
3482
Johannes Bergf2d9d272012-11-22 14:11:39 +01003483 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3484 sband->ht_cap.ht_supported) {
3485 const u8 *ht_oper_ie;
3486
Johannes Berg9caf0362012-11-29 01:25:20 +01003487 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003488 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3489 ht_oper = (void *)(ht_oper_ie + 2);
3490 }
3491
Johannes Bergf2d9d272012-11-22 14:11:39 +01003492 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3493 sband->vht_cap.vht_supported) {
3494 const u8 *vht_oper_ie;
3495
Johannes Berg9caf0362012-11-29 01:25:20 +01003496 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3497 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003498 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3499 vht_oper = (void *)(vht_oper_ie + 2);
3500 if (vht_oper && !ht_oper) {
3501 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003502 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003503 "AP advertised VHT without HT, disabling both\n");
3504 sdata->flags |= IEEE80211_STA_DISABLE_HT;
3505 sdata->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003506 }
3507 }
3508
Johannes Bergf2d9d272012-11-22 14:11:39 +01003509 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3510 cbss->channel,
3511 ht_oper, vht_oper,
3512 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003513
Johannes Bergf2d9d272012-11-22 14:11:39 +01003514 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3515 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003516
Johannes Berg9caf0362012-11-29 01:25:20 +01003517 rcu_read_unlock();
3518
Johannes Berg04ecd252012-09-11 14:34:12 +02003519 /* will change later if needed */
3520 sdata->smps_mode = IEEE80211_SMPS_OFF;
3521
Johannes Bergf2d9d272012-11-22 14:11:39 +01003522 /*
3523 * If this fails (possibly due to channel context sharing
3524 * on incompatible channels, e.g. 80+80 and 160 sharing the
3525 * same control channel) try to use a smaller bandwidth.
3526 */
3527 ret = ieee80211_vif_use_channel(sdata, &chandef,
3528 IEEE80211_CHANCTX_SHARED);
3529 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
3530 ifmgd->flags |= chandef_downgrade(&chandef);
3531 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003532}
3533
3534static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3535 struct cfg80211_bss *cbss, bool assoc)
3536{
3537 struct ieee80211_local *local = sdata->local;
3538 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3539 struct ieee80211_bss *bss = (void *)cbss->priv;
3540 struct sta_info *new_sta = NULL;
3541 bool have_sta = false;
3542 int err;
3543
3544 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3545 return -EINVAL;
3546
3547 if (assoc) {
3548 rcu_read_lock();
3549 have_sta = sta_info_get(sdata, cbss->bssid);
3550 rcu_read_unlock();
3551 }
3552
3553 if (!have_sta) {
3554 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3555 if (!new_sta)
3556 return -ENOMEM;
3557 }
3558
3559 mutex_lock(&local->mtx);
3560 ieee80211_recalc_idle(sdata->local);
3561 mutex_unlock(&local->mtx);
3562
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003563 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003564 u32 rates = 0, basic_rates = 0;
3565 bool have_higher_than_11mbit;
3566 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003567 struct ieee80211_supported_band *sband;
3568
3569 sband = local->hw.wiphy->bands[cbss->channel->band];
3570
3571 err = ieee80211_prep_channel(sdata, cbss);
3572 if (err) {
3573 sta_info_free(local, new_sta);
3574 return err;
3575 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003576
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003577 ieee80211_get_rates(sband, bss->supp_rates,
3578 bss->supp_rates_len,
3579 &rates, &basic_rates,
3580 &have_higher_than_11mbit,
3581 &min_rate, &min_rate_index);
3582
3583 /*
3584 * This used to be a workaround for basic rates missing
3585 * in the association response frame. Now that we no
3586 * longer use the basic rates from there, it probably
3587 * doesn't happen any more, but keep the workaround so
3588 * in case some *other* APs are buggy in different ways
3589 * we can connect -- with a warning.
3590 */
3591 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003592 sdata_info(sdata,
3593 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003594 basic_rates = BIT(min_rate_index);
3595 }
3596
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003597 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003598 sdata->vif.bss_conf.basic_rates = basic_rates;
3599
3600 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003601 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003602 have_higher_than_11mbit)
3603 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3604 else
3605 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3606
3607 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3608
Johannes Berg8c358bc2012-05-22 22:13:05 +02003609 /* set timing information */
3610 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3611 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3612 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3613
3614 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003615 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003616 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3617 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003618
3619 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003620 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003621
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003622 err = sta_info_insert(new_sta);
3623 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003624 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003625 sdata_info(sdata,
3626 "failed to insert STA entry for the AP (error %d)\n",
3627 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003628 return err;
3629 }
3630 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003631 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003632
3633 return 0;
3634}
3635
Johannes Berg77fdaa12009-07-07 03:45:17 +02003636/* config hooks */
3637int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3638 struct cfg80211_auth_request *req)
3639{
Johannes Berg66e67e42012-01-20 13:55:27 +01003640 struct ieee80211_local *local = sdata->local;
3641 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3642 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003643 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003644 int err;
3645
3646 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003647
3648 switch (req->auth_type) {
3649 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3650 auth_alg = WLAN_AUTH_OPEN;
3651 break;
3652 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003653 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003654 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003655 auth_alg = WLAN_AUTH_SHARED_KEY;
3656 break;
3657 case NL80211_AUTHTYPE_FT:
3658 auth_alg = WLAN_AUTH_FT;
3659 break;
3660 case NL80211_AUTHTYPE_NETWORK_EAP:
3661 auth_alg = WLAN_AUTH_LEAP;
3662 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003663 case NL80211_AUTHTYPE_SAE:
3664 auth_alg = WLAN_AUTH_SAE;
3665 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003666 default:
3667 return -EOPNOTSUPP;
3668 }
3669
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003670 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3671 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003672 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003673 return -ENOMEM;
3674
Johannes Berg66e67e42012-01-20 13:55:27 +01003675 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003676
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003677 if (req->sae_data_len >= 4) {
3678 __le16 *pos = (__le16 *) req->sae_data;
3679 auth_data->sae_trans = le16_to_cpu(pos[0]);
3680 auth_data->sae_status = le16_to_cpu(pos[1]);
3681 memcpy(auth_data->data, req->sae_data + 4,
3682 req->sae_data_len - 4);
3683 auth_data->data_len += req->sae_data_len - 4;
3684 }
3685
Johannes Berg77fdaa12009-07-07 03:45:17 +02003686 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003687 memcpy(&auth_data->data[auth_data->data_len],
3688 req->ie, req->ie_len);
3689 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003690 }
3691
Johannes Bergfffd0932009-07-08 14:22:54 +02003692 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003693 auth_data->key_len = req->key_len;
3694 auth_data->key_idx = req->key_idx;
3695 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003696 }
3697
Johannes Berg66e67e42012-01-20 13:55:27 +01003698 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003699
Johannes Berg66e67e42012-01-20 13:55:27 +01003700 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003701
Johannes Berg66e67e42012-01-20 13:55:27 +01003702 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003703
Johannes Berg66e67e42012-01-20 13:55:27 +01003704 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3705 ifmgd->assoc_data) {
3706 err = -EBUSY;
3707 goto err_free;
3708 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003709
Johannes Berg66e67e42012-01-20 13:55:27 +01003710 if (ifmgd->auth_data)
3711 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003712
Johannes Berg66e67e42012-01-20 13:55:27 +01003713 /* prep auth_data so we don't go into idle on disassoc */
3714 ifmgd->auth_data = auth_data;
3715
3716 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003717 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003718
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003719 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003720
Johannes Berga1cf7752012-03-08 15:02:07 +01003721 err = ieee80211_prep_connection(sdata, req->bss, false);
3722 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003723 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003724
Johannes Berg66e67e42012-01-20 13:55:27 +01003725 err = ieee80211_probe_auth(sdata);
3726 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003727 sta_info_destroy_addr(sdata, req->bss->bssid);
3728 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003729 }
3730
Johannes Berg66e67e42012-01-20 13:55:27 +01003731 /* hold our own reference */
3732 cfg80211_ref_bss(auth_data->bss);
3733 err = 0;
3734 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003735
Johannes Berg66e67e42012-01-20 13:55:27 +01003736 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003737 memset(ifmgd->bssid, 0, ETH_ALEN);
3738 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003739 ifmgd->auth_data = NULL;
3740 err_free:
3741 kfree(auth_data);
3742 out_unlock:
3743 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003744
Johannes Berg66e67e42012-01-20 13:55:27 +01003745 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003746}
3747
Johannes Berg77fdaa12009-07-07 03:45:17 +02003748int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3749 struct cfg80211_assoc_request *req)
3750{
Johannes Berg66e67e42012-01-20 13:55:27 +01003751 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003752 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003753 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003754 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003755 struct ieee80211_supported_band *sband;
Johannes Berg9dde6422012-05-16 23:43:19 +02003756 const u8 *ssidie, *ht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003757 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003758
Johannes Berg66e67e42012-01-20 13:55:27 +01003759 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3760 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003761 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003762
Johannes Berg9caf0362012-11-29 01:25:20 +01003763 rcu_read_lock();
3764 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3765 if (!ssidie) {
3766 rcu_read_unlock();
3767 kfree(assoc_data);
3768 return -EINVAL;
3769 }
3770 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3771 assoc_data->ssid_len = ssidie[1];
3772 rcu_read_unlock();
3773
Johannes Berg66e67e42012-01-20 13:55:27 +01003774 mutex_lock(&ifmgd->mtx);
3775
3776 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003777 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003778
3779 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3780 err = -EBUSY;
3781 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003782 }
3783
Johannes Berg66e67e42012-01-20 13:55:27 +01003784 if (ifmgd->assoc_data) {
3785 err = -EBUSY;
3786 goto err_free;
3787 }
3788
3789 if (ifmgd->auth_data) {
3790 bool match;
3791
3792 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003793 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003794 ieee80211_destroy_auth_data(sdata, match);
3795 }
3796
3797 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003798
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003799 ifmgd->beacon_crc_valid = false;
3800
Johannes Bergde5036a2012-03-08 15:02:03 +01003801 /*
3802 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3803 * We still associate in non-HT mode (11a/b/g) if any one of these
3804 * ciphers is configured as pairwise.
3805 * We can set this to true for non-11n hardware, that'll be checked
3806 * separately along with the peer capabilities.
3807 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003808 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003809 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3810 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003811 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003812 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003813 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003814 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003815 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003816 }
3817 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003818
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003819 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003820 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003821 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3822 }
Ben Greearef96a8422011-11-18 11:32:00 -08003823
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003824 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3825 sband = local->hw.wiphy->bands[req->bss->channel->band];
3826 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003827 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003828 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003829 if (!bss->wmm_used)
3830 netdev_info(sdata->dev,
3831 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003832 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003833
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003834 /* disable VHT if we don't support it or the AP doesn't use WMM */
3835 if (!sband->vht_cap.vht_supported ||
3836 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3837 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003838 if (!bss->wmm_used)
3839 netdev_info(sdata->dev,
3840 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003841 }
3842
Ben Greearef96a8422011-11-18 11:32:00 -08003843 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3844 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3845 sizeof(ifmgd->ht_capa_mask));
3846
Johannes Berg77fdaa12009-07-07 03:45:17 +02003847 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003848 memcpy(assoc_data->ie, req->ie, req->ie_len);
3849 assoc_data->ie_len = req->ie_len;
3850 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003851
Johannes Berg66e67e42012-01-20 13:55:27 +01003852 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003853
Johannes Bergaf6b6372009-12-23 13:15:35 +01003854 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3855 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003856 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003857 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003858 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003859 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003860 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003861
Johannes Berg66e67e42012-01-20 13:55:27 +01003862 assoc_data->capability = req->bss->capability;
Johannes Berg32c5057b2012-03-28 11:04:29 +02003863 assoc_data->wmm = bss->wmm_used &&
3864 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003865 assoc_data->supp_rates = bss->supp_rates;
3866 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003867
Johannes Berg9caf0362012-11-29 01:25:20 +01003868 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02003869 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3870 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3871 assoc_data->ap_ht_param =
3872 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3873 else
Johannes Berga8243b72012-11-22 14:32:09 +01003874 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg9caf0362012-11-29 01:25:20 +01003875 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01003876
Kalle Valoab133152010-01-12 10:42:31 +02003877 if (bss->wmm_used && bss->uapsd_supported &&
3878 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003879 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003880 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3881 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003882 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003883 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3884 }
3885
Johannes Berg77fdaa12009-07-07 03:45:17 +02003886 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003887 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003888
3889 if (req->use_mfp) {
3890 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3891 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3892 } else {
3893 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3894 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3895 }
3896
3897 if (req->crypto.control_port)
3898 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3899 else
3900 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3901
Johannes Berga621fa42010-08-27 14:26:54 +03003902 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3903 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3904
Johannes Berg66e67e42012-01-20 13:55:27 +01003905 /* kick off associate process */
3906
3907 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02003908 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003909
Johannes Berga1cf7752012-03-08 15:02:07 +01003910 err = ieee80211_prep_connection(sdata, req->bss, true);
3911 if (err)
3912 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003913
Johannes Berg826262c2012-12-10 16:38:14 +02003914 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3915 const struct cfg80211_bss_ies *beacon_ies;
3916
3917 rcu_read_lock();
3918 beacon_ies = rcu_dereference(req->bss->beacon_ies);
3919 if (!beacon_ies) {
3920 /*
3921 * Wait up to one beacon interval ...
3922 * should this be more if we miss one?
3923 */
3924 sdata_info(sdata, "waiting for beacon from %pM\n",
3925 ifmgd->bssid);
3926 assoc_data->timeout =
3927 TU_TO_EXP_TIME(req->bss->beacon_interval);
3928 } else {
3929 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3930 beacon_ies->data,
3931 beacon_ies->len);
3932 if (tim_ie && tim_ie[1] >=
3933 sizeof(struct ieee80211_tim_ie)) {
3934 const struct ieee80211_tim_ie *tim;
3935 tim = (void *)(tim_ie + 2);
3936 ifmgd->dtim_period = tim->dtim_period;
3937 }
3938 assoc_data->have_beacon = true;
3939 assoc_data->sent_assoc = false;
3940 assoc_data->timeout = jiffies;
3941 }
3942 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003943 } else {
3944 assoc_data->have_beacon = true;
3945 assoc_data->sent_assoc = false;
3946 assoc_data->timeout = jiffies;
3947 }
3948 run_again(ifmgd, assoc_data->timeout);
3949
Paul Stewartfcff4f12012-02-23 17:59:53 -08003950 if (bss->corrupt_data) {
3951 char *corrupt_type = "data";
3952 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3953 if (bss->corrupt_data &
3954 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3955 corrupt_type = "beacon and probe response";
3956 else
3957 corrupt_type = "beacon";
3958 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3959 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003960 sdata_info(sdata, "associating with AP with corrupt %s\n",
3961 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08003962 }
3963
Johannes Berg66e67e42012-01-20 13:55:27 +01003964 err = 0;
3965 goto out;
3966 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003967 memset(ifmgd->bssid, 0, ETH_ALEN);
3968 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003969 ifmgd->assoc_data = NULL;
3970 err_free:
3971 kfree(assoc_data);
3972 out:
3973 mutex_unlock(&ifmgd->mtx);
3974
3975 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003976}
3977
3978int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003979 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003980{
3981 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003982 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02003983 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01003984 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003985
Johannes Berg77fdaa12009-07-07 03:45:17 +02003986 mutex_lock(&ifmgd->mtx);
3987
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003988 sdata_info(sdata,
3989 "deauthenticating from %pM by local choice (reason=%d)\n",
3990 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003991
Johannes Berg86552012012-10-29 09:46:31 +01003992 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003993 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01003994 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3995 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02003996 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01003997 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01003998 ieee80211_destroy_auth_data(sdata, false);
3999 mutex_unlock(&ifmgd->mtx);
4000
4001 sent_frame = tx;
4002 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004003 }
4004
Johannes Berg86552012012-10-29 09:46:31 +01004005 if (ifmgd->associated &&
4006 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4007 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4008 req->reason_code, tx, frame_buf);
4009 sent_frame = tx;
4010 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004011 mutex_unlock(&ifmgd->mtx);
4012
Johannes Berg86552012012-10-29 09:46:31 +01004013 out:
Johannes Berg7da7cc12010-08-05 17:02:38 +02004014 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004015 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004016 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004017
Johannes Berg86552012012-10-29 09:46:31 +01004018 if (sent_frame)
4019 __cfg80211_send_deauth(sdata->dev, frame_buf,
4020 IEEE80211_DEAUTH_FRAME_LEN);
4021
Johannes Berg77fdaa12009-07-07 03:45:17 +02004022 return 0;
4023}
4024
4025int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004026 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004027{
4028 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004029 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004030 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004031
Johannes Berg77fdaa12009-07-07 03:45:17 +02004032 mutex_lock(&ifmgd->mtx);
4033
Johannes Berg8d8b2612009-07-25 11:58:36 +02004034 /*
4035 * cfg80211 should catch this ... but it's racy since
4036 * we can receive a disassoc frame, process it, hand it
4037 * to cfg80211 while that's in a locked section already
4038 * trying to tell us that the user wants to disconnect.
4039 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004040 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004041 mutex_unlock(&ifmgd->mtx);
4042 return -ENOLINK;
4043 }
4044
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004045 sdata_info(sdata,
4046 "disassociating from %pM by local choice (reason=%d)\n",
4047 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004048
Jouni Malinene69e95d2010-03-29 23:29:31 -07004049 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004050 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4051 req->reason_code, !req->local_state_change,
4052 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004053 mutex_unlock(&ifmgd->mtx);
4054
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004055 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4056 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004057
Johannes Berg7da7cc12010-08-05 17:02:38 +02004058 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004059 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004060 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004061
Johannes Berg77fdaa12009-07-07 03:45:17 +02004062 return 0;
4063}
Jouni Malinen026331c2010-02-15 12:53:10 +02004064
Eliad Pellerafa762f2012-04-23 14:45:15 +03004065void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004066{
4067 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4068
4069 mutex_lock(&ifmgd->mtx);
4070 if (ifmgd->assoc_data)
4071 ieee80211_destroy_assoc_data(sdata, false);
4072 if (ifmgd->auth_data)
4073 ieee80211_destroy_auth_data(sdata, false);
4074 del_timer_sync(&ifmgd->timer);
4075 mutex_unlock(&ifmgd->mtx);
4076}
4077
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004078void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4079 enum nl80211_cqm_rssi_threshold_event rssi_event,
4080 gfp_t gfp)
4081{
4082 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4083
Johannes Bergb5878a22010-04-07 16:48:40 +02004084 trace_api_cqm_rssi_notify(sdata, rssi_event);
4085
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004086 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4087}
4088EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);