blob: 35d8cffc973ac20b68765093530592e9fe70bb7c [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 Berg9607e6b662009-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 Schaafbe9c4292009-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);
792 }
793
794 mutex_lock(&sdata->local->chanctx_mtx);
795 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
796 mutex_unlock(&sdata->local->chanctx_mtx);
797 return;
798 }
799 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
800 struct ieee80211_chanctx, conf);
801 if (chanctx->refcount > 1) {
802 sdata_info(sdata,
803 "channel switch with multiple interfaces on the same channel, disconnecting\n");
804 ieee80211_queue_work(&sdata->local->hw,
805 &ifmgd->csa_connection_drop_work);
806 mutex_unlock(&sdata->local->chanctx_mtx);
807 return;
808 }
809 mutex_unlock(&sdata->local->chanctx_mtx);
810
811 sdata->local->csa_channel = new_ch;
812
Johannes Berg57eebdf2012-08-01 15:50:46 +0200813 if (sw_elem->mode)
814 ieee80211_stop_queues_by_reason(&sdata->local->hw,
815 IEEE80211_QUEUE_STOP_REASON_CSA);
816
Johannes Berg5ce6e432010-05-11 16:20:57 +0200817 if (sdata->local->ops->channel_switch) {
818 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200819 struct ieee80211_channel_switch ch_switch = {
820 .timestamp = timestamp,
821 .block_tx = sw_elem->mode,
822 .channel = new_ch,
823 .count = sw_elem->count,
824 };
825
Johannes Berg5ce6e432010-05-11 16:20:57 +0200826 drv_channel_switch(sdata->local, &ch_switch);
827 return;
828 }
829
830 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200831 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400832 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200833 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200834 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200835 TU_TO_EXP_TIME(sw_elem->count *
836 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200837}
838
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200839static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
840 struct ieee80211_channel *channel,
841 const u8 *country_ie, u8 country_ie_len,
842 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200843{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200844 struct ieee80211_country_ie_triplet *triplet;
845 int chan = ieee80211_frequency_to_channel(channel->center_freq);
846 int i, chan_pwr, chan_increment, new_ap_level;
847 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200848
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200849 /* Invalid IE */
850 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200851 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200852
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200853 triplet = (void *)(country_ie + 3);
854 country_ie_len -= 3;
855
856 switch (channel->band) {
857 default:
858 WARN_ON_ONCE(1);
859 /* fall through */
860 case IEEE80211_BAND_2GHZ:
861 case IEEE80211_BAND_60GHZ:
862 chan_increment = 1;
863 break;
864 case IEEE80211_BAND_5GHZ:
865 chan_increment = 4;
866 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200867 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200868
869 /* find channel */
870 while (country_ie_len >= 3) {
871 u8 first_channel = triplet->chans.first_channel;
872
873 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
874 goto next;
875
876 for (i = 0; i < triplet->chans.num_channels; i++) {
877 if (first_channel + i * chan_increment == chan) {
878 have_chan_pwr = true;
879 chan_pwr = triplet->chans.max_power;
880 break;
881 }
882 }
883 if (have_chan_pwr)
884 break;
885
886 next:
887 triplet++;
888 country_ie_len -= 3;
889 }
890
891 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200892 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200893
894 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
895
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200896 if (sdata->ap_power_level == new_ap_level)
897 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200898
899 sdata_info(sdata,
900 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
901 new_ap_level, chan_pwr, *pwr_constr_elem,
902 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200903 sdata->ap_power_level = new_ap_level;
904 if (__ieee80211_recalc_txpower(sdata))
905 return BSS_CHANGED_TXPOWER;
906 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200907}
908
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300909void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
910{
911 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
912 struct ieee80211_local *local = sdata->local;
913 struct ieee80211_conf *conf = &local->hw.conf;
914
915 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
916 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
917 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
918
919 local->disable_dynamic_ps = false;
920 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
921}
922EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
923
924void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
925{
926 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
927 struct ieee80211_local *local = sdata->local;
928 struct ieee80211_conf *conf = &local->hw.conf;
929
930 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
931 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
932 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
933
934 local->disable_dynamic_ps = true;
935 conf->dynamic_ps_timeout = 0;
936 del_timer_sync(&local->dynamic_ps_timer);
937 ieee80211_queue_work(&local->hw,
938 &local->dynamic_ps_enable_work);
939}
940EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
941
Johannes Berg965beda2009-04-16 13:17:24 +0200942/* powersave */
943static void ieee80211_enable_ps(struct ieee80211_local *local,
944 struct ieee80211_sub_if_data *sdata)
945{
946 struct ieee80211_conf *conf = &local->hw.conf;
947
Johannes Bergd5edaed2009-04-22 23:02:51 +0200948 /*
949 * If we are scanning right now then the parameters will
950 * take effect when scan finishes.
951 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200952 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200953 return;
954
Johannes Berg965beda2009-04-16 13:17:24 +0200955 if (conf->dynamic_ps_timeout > 0 &&
956 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
957 mod_timer(&local->dynamic_ps_timer, jiffies +
958 msecs_to_jiffies(conf->dynamic_ps_timeout));
959 } else {
960 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
961 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530962
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200963 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
964 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
965 return;
966
967 conf->flags |= IEEE80211_CONF_PS;
968 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200969 }
970}
971
972static void ieee80211_change_ps(struct ieee80211_local *local)
973{
974 struct ieee80211_conf *conf = &local->hw.conf;
975
976 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200977 ieee80211_enable_ps(local, local->ps_sdata);
978 } else if (conf->flags & IEEE80211_CONF_PS) {
979 conf->flags &= ~IEEE80211_CONF_PS;
980 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
981 del_timer_sync(&local->dynamic_ps_timer);
982 cancel_work_sync(&local->dynamic_ps_enable_work);
983 }
984}
985
Jason Young808118c2011-03-10 16:43:19 -0800986static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
987{
988 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
989 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200990 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800991
992 if (!mgd->powersave)
993 return false;
994
Johannes Berg05cb9102011-10-28 11:59:47 +0200995 if (mgd->broken_ap)
996 return false;
997
Jason Young808118c2011-03-10 16:43:19 -0800998 if (!mgd->associated)
999 return false;
1000
Jason Young808118c2011-03-10 16:43:19 -08001001 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1002 IEEE80211_STA_CONNECTION_POLL))
1003 return false;
1004
1005 rcu_read_lock();
1006 sta = sta_info_get(sdata, mgd->bssid);
1007 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001008 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001009 rcu_read_unlock();
1010
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001011 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001012}
1013
Johannes Berg965beda2009-04-16 13:17:24 +02001014/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001015void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001016{
1017 struct ieee80211_sub_if_data *sdata, *found = NULL;
1018 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001019 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001020
1021 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1022 local->ps_sdata = NULL;
1023 return;
1024 }
1025
1026 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001027 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001028 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301029 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1030 /* If an AP vif is found, then disable PS
1031 * by setting the count to zero thereby setting
1032 * ps_sdata to NULL.
1033 */
1034 count = 0;
1035 break;
1036 }
Johannes Berg965beda2009-04-16 13:17:24 +02001037 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1038 continue;
1039 found = sdata;
1040 count++;
1041 }
1042
Jason Young808118c2011-03-10 16:43:19 -08001043 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001044 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001045 s32 beaconint_us;
1046
1047 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001048 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001049
1050 beaconint_us = ieee80211_tu_to_usec(
1051 found->vif.bss_conf.beacon_int);
1052
Juuso Oikarinenff616382010-06-09 09:51:52 +03001053 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001054 if (timeout < 0) {
1055 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001056 * Go to full PSM if the user configures a very low
1057 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001058 * The 2000 second value is there for compatibility
1059 * until the PM_QOS_NETWORK_LATENCY is configured
1060 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001061 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001062 if (latency > (1900 * USEC_PER_MSEC) &&
1063 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001064 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001065 else
1066 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001067 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001068 local->dynamic_ps_user_timeout = timeout;
1069 if (!local->disable_dynamic_ps)
1070 conf->dynamic_ps_timeout =
1071 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001072
Johannes Berg04fe2032009-04-22 18:44:37 +02001073 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001074 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001075 } else {
Johannes Berg56007a02010-01-26 14:19:52 +01001076 struct ieee80211_bss *bss;
Johannes Berg04fe2032009-04-22 18:44:37 +02001077 int maxslp = 1;
Johannes Berg56007a02010-01-26 14:19:52 +01001078 u8 dtimper;
Johannes Berg04fe2032009-04-22 18:44:37 +02001079
Johannes Berg56007a02010-01-26 14:19:52 +01001080 bss = (void *)found->u.mgd.associated->priv;
1081 dtimper = bss->dtim_period;
1082
1083 /* If the TIM IE is invalid, pretend the value is 1 */
1084 if (!dtimper)
1085 dtimper = 1;
1086 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001087 maxslp = min_t(int, dtimper,
1088 latency / beaconint_us);
1089
Johannes Berg9ccebe62009-04-23 10:32:36 +02001090 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001091 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001092 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001093 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001094 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001095 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001096 }
Johannes Berg965beda2009-04-16 13:17:24 +02001097
1098 ieee80211_change_ps(local);
1099}
1100
Eliad Pellerab095872012-07-27 12:33:22 +03001101void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1102{
1103 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1104
1105 if (sdata->vif.bss_conf.ps != ps_allowed) {
1106 sdata->vif.bss_conf.ps = ps_allowed;
1107 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1108 }
1109}
1110
Johannes Berg965beda2009-04-16 13:17:24 +02001111void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1112{
1113 struct ieee80211_local *local =
1114 container_of(work, struct ieee80211_local,
1115 dynamic_ps_disable_work);
1116
1117 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1118 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1119 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1120 }
1121
1122 ieee80211_wake_queues_by_reason(&local->hw,
1123 IEEE80211_QUEUE_STOP_REASON_PS);
1124}
1125
1126void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1127{
1128 struct ieee80211_local *local =
1129 container_of(work, struct ieee80211_local,
1130 dynamic_ps_enable_work);
1131 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001132 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301133 unsigned long flags;
1134 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001135
1136 /* can only happen when PS was just disabled anyway */
1137 if (!sdata)
1138 return;
1139
Christian Lamparter5e340692011-06-30 21:08:43 +02001140 ifmgd = &sdata->u.mgd;
1141
Johannes Berg965beda2009-04-16 13:17:24 +02001142 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1143 return;
1144
Arik Nemtsov77b70232011-06-26 12:06:54 +03001145 if (!local->disable_dynamic_ps &&
1146 local->hw.conf.dynamic_ps_timeout > 0) {
1147 /* don't enter PS if TX frames are pending */
1148 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301149 mod_timer(&local->dynamic_ps_timer, jiffies +
1150 msecs_to_jiffies(
1151 local->hw.conf.dynamic_ps_timeout));
1152 return;
1153 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001154
1155 /*
1156 * transmission can be stopped by others which leads to
1157 * dynamic_ps_timer expiry. Postpone the ps timer if it
1158 * is not the actual idle state.
1159 */
1160 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1161 for (q = 0; q < local->hw.queues; q++) {
1162 if (local->queue_stop_reasons[q]) {
1163 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1164 flags);
1165 mod_timer(&local->dynamic_ps_timer, jiffies +
1166 msecs_to_jiffies(
1167 local->hw.conf.dynamic_ps_timeout));
1168 return;
1169 }
1170 }
1171 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301172 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301173
Vivek Natarajan375177b2010-02-09 14:50:28 +05301174 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301175 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301176 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001177
Vivek Natarajane8306f92011-04-06 11:41:10 +05301178 if (drv_tx_frames_pending(local))
1179 mod_timer(&local->dynamic_ps_timer, jiffies +
1180 msecs_to_jiffies(
1181 local->hw.conf.dynamic_ps_timeout));
1182 else {
1183 ieee80211_send_nullfunc(local, sdata, 1);
1184 /* Flush to get the tx status of nullfunc frame */
1185 drv_flush(local, false);
1186 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301187 }
1188
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001189 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1190 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301191 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1192 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1193 local->hw.conf.flags |= IEEE80211_CONF_PS;
1194 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1195 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301196
Arik Nemtsov77b70232011-06-26 12:06:54 +03001197 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1198 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001199}
1200
1201void ieee80211_dynamic_ps_timer(unsigned long data)
1202{
1203 struct ieee80211_local *local = (void *) data;
1204
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001205 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001206 return;
1207
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001208 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001209}
1210
Johannes Berg60f8b392008-09-08 17:44:22 +02001211/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001212static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001213 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001214 u8 *wmm_param, size_t wmm_param_len)
1215{
Jiri Bencf0706e82007-05-05 11:45:53 -07001216 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001217 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001218 size_t left;
1219 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001220 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001221
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001222 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001223 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001224
Johannes Berg32c50572012-03-28 11:04:29 +02001225 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001226 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001227
1228 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001229 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001230
Jiri Bencf0706e82007-05-05 11:45:53 -07001231 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001232 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001233
1234 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001235 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001236
Jiri Bencf0706e82007-05-05 11:45:53 -07001237 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001238 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001239 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001240 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001241
1242 pos = wmm_param + 8;
1243 left = wmm_param_len - 8;
1244
1245 memset(&params, 0, sizeof(params));
1246
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001247 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001248 for (; left >= 4; left -= 4, pos += 4) {
1249 int aci = (pos[0] >> 5) & 0x03;
1250 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001251 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001252 int queue;
1253
1254 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001255 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001256 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001257 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001258 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001259 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1260 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001261 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001262 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001263 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001264 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001265 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001266 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1267 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001268 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001269 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001270 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001271 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001272 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001273 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1274 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001275 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001276 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001277 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001278 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001279 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001280 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001281 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1282 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001283 break;
1284 }
1285
1286 params.aifs = pos[0] & 0x0f;
1287 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1288 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001289 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001290 params.uapsd = uapsd;
1291
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001292 mlme_dbg(sdata,
1293 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1294 queue, aci, acm,
1295 params.aifs, params.cw_min, params.cw_max,
1296 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001297 sdata->tx_conf[queue] = params;
1298 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001299 sdata_err(sdata,
1300 "failed to set TX queue parameters for queue %d\n",
1301 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001302 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001303
1304 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001305 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001306 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001307}
1308
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001309static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1310{
1311 lockdep_assert_held(&sdata->local->mtx);
1312
1313 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1314 IEEE80211_STA_BEACON_POLL);
1315 ieee80211_run_deferred_scan(sdata->local);
1316}
1317
1318static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1319{
1320 mutex_lock(&sdata->local->mtx);
1321 __ieee80211_stop_poll(sdata);
1322 mutex_unlock(&sdata->local->mtx);
1323}
1324
Johannes Berg7a5158e2008-10-08 10:59:33 +02001325static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1326 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001327{
Johannes Bergbda39332008-10-11 01:51:51 +02001328 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001329 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001330 bool use_protection;
1331 bool use_short_preamble;
1332 bool use_short_slot;
1333
1334 if (erp_valid) {
1335 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1336 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1337 } else {
1338 use_protection = false;
1339 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1340 }
1341
1342 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001343 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001344 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001345
Johannes Berg471b3ef2007-12-28 14:32:58 +01001346 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001347 bss_conf->use_cts_prot = use_protection;
1348 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001349 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001350
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001351 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001352 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001353 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001354 }
Daniel Draked9430a32007-07-27 15:43:24 +02001355
Johannes Berg7a5158e2008-10-08 10:59:33 +02001356 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001357 bss_conf->use_short_slot = use_short_slot;
1358 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001359 }
1360
1361 return changed;
1362}
1363
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001364static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001365 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001366 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001367{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001368 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001369 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001370 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001371
Johannes Bergae5eb022008-10-14 16:58:37 +02001372 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001373 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001374 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001375
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001376 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1377 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1378
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001379 sdata->u.mgd.associated = cbss;
1380 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001381
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001382 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1383
Johannes Berg488dd7b2012-10-29 20:08:01 +01001384 if (sdata->vif.p2p) {
1385 u8 noa[2];
1386 int ret;
1387
1388 ret = cfg80211_get_p2p_attr(cbss->information_elements,
1389 cbss->len_information_elements,
1390 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1391 noa, sizeof(noa));
1392 if (ret >= 2) {
1393 bss_conf->p2p_oppps = noa[1] & 0x80;
1394 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1395 bss_info_changed |= BSS_CHANGED_P2P_PS;
1396 sdata->u.mgd.p2p_noa_index = noa[0];
1397 }
1398 }
1399
Johannes Bergb291ba12009-07-10 15:29:03 +02001400 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001401 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001402
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001403 ieee80211_led_assoc(local, 1);
1404
Johannes Berge5b900d2010-07-29 16:08:55 +02001405 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
1406 bss_conf->dtim_period = bss->dtim_period;
1407 else
1408 bss_conf->dtim_period = 0;
1409
Juuso Oikarinen68542962010-06-09 13:43:26 +03001410 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001411
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001412 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001413 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001414 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001415 bss_info_changed |= BSS_CHANGED_CQM;
1416
Juuso Oikarinen68542962010-06-09 13:43:26 +03001417 /* Enable ARP filtering */
1418 if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
1419 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
1420 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
1421 }
1422
Johannes Bergae5eb022008-10-14 16:58:37 +02001423 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001424
Johannes Berg056508d2009-07-30 21:43:55 +02001425 mutex_lock(&local->iflist_mtx);
1426 ieee80211_recalc_ps(local, -1);
1427 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001428
Johannes Berg04ecd252012-09-11 14:34:12 +02001429 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001430 ieee80211_recalc_ps_vif(sdata);
1431
John W. Linville8a5b33f2010-01-06 15:39:39 -05001432 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001433 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001434}
1435
Jouni Malinene69e95d2010-03-29 23:29:31 -07001436static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001437 u16 stype, u16 reason, bool tx,
1438 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001439{
Johannes Berg46900292009-02-15 12:44:28 +01001440 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001441 struct ieee80211_local *local = sdata->local;
1442 struct sta_info *sta;
Johannes Berg3cc52402012-03-09 13:12:35 +01001443 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001444
Johannes Berg77fdaa12009-07-07 03:45:17 +02001445 ASSERT_MGD_MTX(ifmgd);
1446
Johannes Berg37ad3882012-02-24 13:50:54 +01001447 if (WARN_ON_ONCE(tx && !frame_buf))
1448 return;
1449
Johannes Bergb291ba12009-07-10 15:29:03 +02001450 if (WARN_ON(!ifmgd->associated))
1451 return;
1452
David Spinadel79543d82012-06-12 09:59:45 +03001453 ieee80211_stop_poll(sdata);
1454
Johannes Berg77fdaa12009-07-07 03:45:17 +02001455 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001456
1457 /*
1458 * we need to commit the associated = NULL change because the
1459 * scan code uses that to determine whether this iface should
1460 * go to/wake up from powersave or not -- and could otherwise
1461 * wake the queues erroneously.
1462 */
1463 smp_mb();
1464
1465 /*
1466 * Thus, we can only afterwards stop the queues -- to account
1467 * for the case where another CPU is finishing a scan at this
1468 * time -- we don't want the scan code to enable queues.
1469 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001470
John W. Linville8a5b33f2010-01-06 15:39:39 -05001471 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001472 netif_carrier_off(sdata->dev);
1473
Johannes Berg2a419052010-06-10 10:21:29 +02001474 mutex_lock(&local->sta_mtx);
Eliad Peller88a9e312012-06-01 11:14:03 +03001475 sta = sta_info_get(sdata, ifmgd->bssid);
Sujith4cad6c72010-02-10 14:52:21 +05301476 if (sta) {
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001477 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
Johannes Berg7f161142012-07-18 12:53:34 +02001478 ieee80211_sta_tear_down_BA_sessions(sta, false);
Sujith4cad6c72010-02-10 14:52:21 +05301479 }
Johannes Berg2a419052010-06-10 10:21:29 +02001480 mutex_unlock(&local->sta_mtx);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001481
Eliad Peller88bc40e2012-07-12 17:35:33 +03001482 /*
1483 * if we want to get out of ps before disassoc (why?) we have
1484 * to do it before sending disassoc, as otherwise the null-packet
1485 * won't be valid.
1486 */
1487 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1488 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1489 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1490 }
1491 local->ps_sdata = NULL;
1492
Eliad Pellerab095872012-07-27 12:33:22 +03001493 /* disable per-vif ps */
1494 ieee80211_recalc_ps_vif(sdata);
1495
Eliad Pellerf8239812012-06-27 14:18:22 +03001496 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1497 if (tx)
1498 drv_flush(local, false);
1499
Johannes Berg37ad3882012-02-24 13:50:54 +01001500 /* deauthenticate/disassociate now */
1501 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001502 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1503 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001504
1505 /* flush out frame */
1506 if (tx)
1507 drv_flush(local, false);
1508
Eliad Peller88a9e312012-06-01 11:14:03 +03001509 /* clear bssid only after building the needed mgmt frames */
1510 memset(ifmgd->bssid, 0, ETH_ALEN);
1511
Johannes Berg37ad3882012-02-24 13:50:54 +01001512 /* remove AP and TDLS peers */
1513 sta_info_flush(local, sdata);
1514
1515 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001516 changed |= ieee80211_reset_erp_info(sdata);
1517
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001518 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001519 changed |= BSS_CHANGED_ASSOC;
1520 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001521
Johannes Berg488dd7b2012-10-29 20:08:01 +01001522 sdata->vif.bss_conf.p2p_ctwindow = 0;
1523 sdata->vif.bss_conf.p2p_oppps = false;
1524
Johannes Berg413ad502009-05-08 21:21:06 +02001525 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001526 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1527 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001528
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001529 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301530
Kalle Valo520eb822008-12-18 23:35:27 +02001531 del_timer_sync(&local->dynamic_ps_timer);
1532 cancel_work_sync(&local->dynamic_ps_enable_work);
1533
Juuso Oikarinen68542962010-06-09 13:43:26 +03001534 /* Disable ARP filtering */
1535 if (sdata->vif.bss_conf.arp_filter_enabled) {
1536 sdata->vif.bss_conf.arp_filter_enabled = false;
1537 changed |= BSS_CHANGED_ARP_FILTER;
1538 }
1539
Johannes Berg3abead52012-03-02 15:56:59 +01001540 sdata->vif.bss_conf.qos = false;
1541 changed |= BSS_CHANGED_QOS;
1542
Johannes Berg0aaffa92010-05-05 15:28:27 +02001543 /* The BSSID (not really interesting) and HT changed */
1544 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001545 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001546
Johannes Berg3abead52012-03-02 15:56:59 +01001547 /* disassociated - set to defaults now */
1548 ieee80211_set_wmm_default(sdata, false);
1549
Johannes Bergb9dcf712010-08-27 12:35:54 +02001550 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1551 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1552 del_timer_sync(&sdata->u.mgd.timer);
1553 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001554
1555 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001556
1557 ifmgd->flags = 0;
1558 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001559}
Jiri Bencf0706e82007-05-05 11:45:53 -07001560
Kalle Valo3cf335d52009-03-22 21:57:06 +02001561void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1562 struct ieee80211_hdr *hdr)
1563{
1564 /*
1565 * We can postpone the mgd.timer whenever receiving unicast frames
1566 * from AP because we know that the connection is working both ways
1567 * at that time. But multicast frames (and hence also beacons) must
1568 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001569 * data idle periods for sending the periodic probe request to the
1570 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02001571 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001572 if (is_multicast_ether_addr(hdr->addr1))
1573 return;
1574
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001575 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02001576}
Jiri Bencf0706e82007-05-05 11:45:53 -07001577
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001578static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1579{
1580 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001581 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001582
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001583 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001584 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001585 IEEE80211_STA_CONNECTION_POLL))) {
1586 mutex_unlock(&local->mtx);
1587 return;
1588 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001589
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001590 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001591
1592 mutex_lock(&local->iflist_mtx);
1593 ieee80211_recalc_ps(local, -1);
1594 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001595
1596 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001597 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001598
1599 /*
1600 * We've received a probe response, but are not sure whether
1601 * we have or will be receiving any beacons or data, so let's
1602 * schedule the timers again, just in case.
1603 */
1604 ieee80211_sta_reset_beacon_monitor(sdata);
1605
1606 mod_timer(&ifmgd->conn_mon_timer,
1607 round_jiffies_up(jiffies +
1608 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001609out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001610 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001611}
1612
1613void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001614 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001615{
Felix Fietkau75706d02010-12-02 21:01:07 +01001616 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001617 return;
1618
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001619 if (ack)
1620 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001621
1622 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1623 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001624 if (ack)
1625 sdata->u.mgd.probe_send_count = 0;
1626 else
1627 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001628 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1629 }
1630}
1631
Maxim Levitskya43abf22009-07-31 18:54:12 +03001632static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1633{
1634 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1635 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001636 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001637 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001638
1639 /*
1640 * Try sending broadcast probe requests for the last three
1641 * probe requests after the first ones failed since some
1642 * buggy APs only support broadcast probe requests.
1643 */
1644 if (ifmgd->probe_send_count >= unicast_limit)
1645 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001646
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001647 /*
1648 * When the hardware reports an accurate Tx ACK status, it's
1649 * better to send a nullfunc frame instead of a probe request,
1650 * as it will kick us off the AP quickly if we aren't associated
1651 * anymore. The timeout will be reset if the frame is ACKed by
1652 * the AP.
1653 */
Soumik Das992e68b2012-05-20 15:31:13 +05301654 ifmgd->probe_send_count++;
1655
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001656 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1657 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001658 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001659 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001660 int ssid_len;
1661
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001662 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001663 if (WARN_ON_ONCE(ssid == NULL))
1664 ssid_len = 0;
1665 else
1666 ssid_len = ssid[1];
1667
1668 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Bergfe94fe02012-07-30 12:26:34 +02001669 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02001670 ifmgd->associated->channel, false);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001671 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001672
Ben Greear180205b2011-02-04 15:30:24 -08001673 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001674 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301675 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1676 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001677}
1678
Johannes Bergb291ba12009-07-10 15:29:03 +02001679static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1680 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001681{
Kalle Valo04de8382009-03-22 21:57:35 +02001682 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001683 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001684
Johannes Berg9607e6b662009-12-23 13:15:31 +01001685 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001686 return;
1687
Johannes Berg77fdaa12009-07-07 03:45:17 +02001688 mutex_lock(&ifmgd->mtx);
1689
1690 if (!ifmgd->associated)
1691 goto out;
1692
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001693 mutex_lock(&sdata->local->mtx);
1694
1695 if (sdata->local->tmp_channel || sdata->local->scanning) {
1696 mutex_unlock(&sdata->local->mtx);
1697 goto out;
1698 }
1699
Joe Perchese87cc472012-05-13 21:56:26 +00001700 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001701 mlme_dbg_ratelimited(sdata,
1702 "detected beacon loss from AP - sending probe request\n");
1703
Holger Schurig6efb71b2012-05-15 15:38:59 +02001704 ieee80211_cqm_rssi_notify(&sdata->vif,
1705 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001706
Johannes Bergb291ba12009-07-10 15:29:03 +02001707 /*
1708 * The driver/our work has already reported this event or the
1709 * connection monitoring has kicked in and we have already sent
1710 * a probe request. Or maybe the AP died and the driver keeps
1711 * reporting until we disassociate...
1712 *
1713 * In either case we have to ignore the current call to this
1714 * function (except for setting the correct probe reason bit)
1715 * because otherwise we would reset the timer every time and
1716 * never check whether we received a probe response!
1717 */
1718 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1719 IEEE80211_STA_CONNECTION_POLL))
1720 already = true;
1721
1722 if (beacon)
1723 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1724 else
1725 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1726
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001727 mutex_unlock(&sdata->local->mtx);
1728
Johannes Bergb291ba12009-07-10 15:29:03 +02001729 if (already)
1730 goto out;
1731
Johannes Berg4e751842009-06-10 15:16:52 +02001732 mutex_lock(&sdata->local->iflist_mtx);
1733 ieee80211_recalc_ps(sdata->local, -1);
1734 mutex_unlock(&sdata->local->iflist_mtx);
1735
Maxim Levitskya43abf22009-07-31 18:54:12 +03001736 ifmgd->probe_send_count = 0;
1737 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001738 out:
1739 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001740}
1741
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001742struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1743 struct ieee80211_vif *vif)
1744{
1745 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1746 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001747 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001748 struct sk_buff *skb;
1749 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001750 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001751
1752 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1753 return NULL;
1754
1755 ASSERT_MGD_MTX(ifmgd);
1756
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001757 if (ifmgd->associated)
1758 cbss = ifmgd->associated;
1759 else if (ifmgd->auth_data)
1760 cbss = ifmgd->auth_data->bss;
1761 else if (ifmgd->assoc_data)
1762 cbss = ifmgd->assoc_data->bss;
1763 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001764 return NULL;
1765
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001766 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001767 if (WARN_ON_ONCE(ssid == NULL))
1768 ssid_len = 0;
1769 else
1770 ssid_len = ssid[1];
1771
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001772 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001773 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001774 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001775 NULL, 0, true);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001776
1777 return skb;
1778}
1779EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1780
Johannes Berg882a7c62012-08-01 22:32:45 +02001781static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1782 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001783{
1784 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1785 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001786 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001787
1788 mutex_lock(&ifmgd->mtx);
1789 if (!ifmgd->associated) {
1790 mutex_unlock(&ifmgd->mtx);
1791 return;
1792 }
1793
Johannes Berg37ad3882012-02-24 13:50:54 +01001794 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1795 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001796 transmit_frame, frame_buf);
1797 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001798 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001799
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001800 /*
1801 * must be outside lock due to cfg80211,
1802 * but that's not a problem.
1803 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001804 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001805
1806 mutex_lock(&local->mtx);
1807 ieee80211_recalc_idle(local);
1808 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001809}
1810
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001811static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001812{
1813 struct ieee80211_sub_if_data *sdata =
1814 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001815 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001816 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1817 struct sta_info *sta;
1818
1819 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301820 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001821 sta = sta_info_get(sdata, ifmgd->bssid);
1822 if (sta)
1823 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301824 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001825 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001826
Johannes Berg882a7c62012-08-01 22:32:45 +02001827 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1828 sdata_info(sdata, "Connection to AP %pM lost\n",
1829 ifmgd->bssid);
1830 __ieee80211_disconnect(sdata, false);
1831 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001832 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001833 }
1834}
1835
1836static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1837{
1838 struct ieee80211_sub_if_data *sdata =
1839 container_of(work, struct ieee80211_sub_if_data,
1840 u.mgd.csa_connection_drop_work);
1841
1842 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1843 IEEE80211_QUEUE_STOP_REASON_CSA);
1844 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001845}
1846
Kalle Valo04de8382009-03-22 21:57:35 +02001847void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1848{
1849 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001850 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001851
Johannes Bergb5878a22010-04-07 16:48:40 +02001852 trace_api_beacon_loss(sdata);
1853
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001854 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1855 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001856}
1857EXPORT_SYMBOL(ieee80211_beacon_loss);
1858
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001859void ieee80211_connection_loss(struct ieee80211_vif *vif)
1860{
1861 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1862 struct ieee80211_hw *hw = &sdata->local->hw;
1863
Johannes Bergb5878a22010-04-07 16:48:40 +02001864 trace_api_connection_loss(sdata);
1865
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001866 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1867 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1868}
1869EXPORT_SYMBOL(ieee80211_connection_loss);
1870
1871
Johannes Berg66e67e42012-01-20 13:55:27 +01001872static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1873 bool assoc)
1874{
1875 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1876
1877 lockdep_assert_held(&sdata->u.mgd.mtx);
1878
Johannes Berg66e67e42012-01-20 13:55:27 +01001879 if (!assoc) {
1880 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1881
1882 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1883 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01001884 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001885 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001886 }
1887
1888 cfg80211_put_bss(auth_data->bss);
1889 kfree(auth_data);
1890 sdata->u.mgd.auth_data = NULL;
1891}
1892
1893static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1894 struct ieee80211_mgmt *mgmt, size_t len)
1895{
1896 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1897 u8 *pos;
1898 struct ieee802_11_elems elems;
1899
1900 pos = mgmt->u.auth.variable;
1901 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1902 if (!elems.challenge)
1903 return;
1904 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001905 drv_mgd_prepare_tx(sdata->local, sdata);
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001906 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001907 elems.challenge - 2, elems.challenge_len + 2,
1908 auth_data->bss->bssid, auth_data->bss->bssid,
1909 auth_data->key, auth_data->key_len,
1910 auth_data->key_idx);
1911}
1912
1913static enum rx_mgmt_action __must_check
1914ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1915 struct ieee80211_mgmt *mgmt, size_t len)
1916{
1917 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1918 u8 bssid[ETH_ALEN];
1919 u16 auth_alg, auth_transaction, status_code;
1920 struct sta_info *sta;
1921
1922 lockdep_assert_held(&ifmgd->mtx);
1923
1924 if (len < 24 + 6)
1925 return RX_MGMT_NONE;
1926
1927 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1928 return RX_MGMT_NONE;
1929
1930 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1931
Joe Perchesb203ca32012-05-08 18:56:52 +00001932 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001933 return RX_MGMT_NONE;
1934
1935 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1936 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1937 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1938
1939 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001940 auth_transaction != ifmgd->auth_data->expected_transaction) {
1941 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1942 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1943 auth_transaction,
1944 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001945 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001946 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001947
1948 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001949 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1950 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001951 ieee80211_destroy_auth_data(sdata, false);
1952 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001953 }
1954
1955 switch (ifmgd->auth_data->algorithm) {
1956 case WLAN_AUTH_OPEN:
1957 case WLAN_AUTH_LEAP:
1958 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001959 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01001960 break;
1961 case WLAN_AUTH_SHARED_KEY:
1962 if (ifmgd->auth_data->expected_transaction != 4) {
1963 ieee80211_auth_challenge(sdata, mgmt, len);
1964 /* need another frame */
1965 return RX_MGMT_NONE;
1966 }
1967 break;
1968 default:
1969 WARN_ONCE(1, "invalid auth alg %d",
1970 ifmgd->auth_data->algorithm);
1971 return RX_MGMT_NONE;
1972 }
1973
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001974 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001975 ifmgd->auth_data->done = true;
1976 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
1977 run_again(ifmgd, ifmgd->auth_data->timeout);
1978
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001979 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
1980 ifmgd->auth_data->expected_transaction != 2) {
1981 /*
1982 * Report auth frame to user space for processing since another
1983 * round of Authentication frames is still needed.
1984 */
1985 return RX_MGMT_CFG80211_RX_AUTH;
1986 }
1987
Johannes Berg66e67e42012-01-20 13:55:27 +01001988 /* move station state to auth */
1989 mutex_lock(&sdata->local->sta_mtx);
1990 sta = sta_info_get(sdata, bssid);
1991 if (!sta) {
1992 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
1993 goto out_err;
1994 }
1995 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001996 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01001997 goto out_err;
1998 }
1999 mutex_unlock(&sdata->local->sta_mtx);
2000
2001 return RX_MGMT_CFG80211_RX_AUTH;
2002 out_err:
2003 mutex_unlock(&sdata->local->sta_mtx);
2004 /* ignore frame -- wait for timeout */
2005 return RX_MGMT_NONE;
2006}
2007
2008
Johannes Berg77fdaa12009-07-07 03:45:17 +02002009static enum rx_mgmt_action __must_check
2010ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002011 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002012{
Johannes Berg46900292009-02-15 12:44:28 +01002013 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002014 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002015 u16 reason_code;
2016
Johannes Berg66e67e42012-01-20 13:55:27 +01002017 lockdep_assert_held(&ifmgd->mtx);
2018
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002019 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002020 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002021
Johannes Berg66e67e42012-01-20 13:55:27 +01002022 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002023 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002024 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002025
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002026 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002027
2028 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2029
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002030 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2031 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002032
Johannes Berg37ad3882012-02-24 13:50:54 +01002033 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2034
Johannes Berg7da7cc12010-08-05 17:02:38 +02002035 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01002036 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002037 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002038
2039 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002040}
2041
2042
Johannes Berg77fdaa12009-07-07 03:45:17 +02002043static enum rx_mgmt_action __must_check
2044ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2045 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002046{
Johannes Berg46900292009-02-15 12:44:28 +01002047 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002048 u16 reason_code;
2049
Johannes Berg66e67e42012-01-20 13:55:27 +01002050 lockdep_assert_held(&ifmgd->mtx);
2051
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002052 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002053 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002054
Johannes Berg66e67e42012-01-20 13:55:27 +01002055 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002056 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002057 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002058
2059 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2060
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002061 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2062 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002063
Johannes Berg37ad3882012-02-24 13:50:54 +01002064 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2065
Johannes Berg7da7cc12010-08-05 17:02:38 +02002066 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002067 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002068 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002069
Johannes Berg77fdaa12009-07-07 03:45:17 +02002070 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002071}
2072
Christian Lamparterc74d0842011-10-15 00:14:49 +02002073static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2074 u8 *supp_rates, unsigned int supp_rates_len,
2075 u32 *rates, u32 *basic_rates,
2076 bool *have_higher_than_11mbit,
2077 int *min_rate, int *min_rate_index)
2078{
2079 int i, j;
2080
2081 for (i = 0; i < supp_rates_len; i++) {
2082 int rate = (supp_rates[i] & 0x7f) * 5;
2083 bool is_basic = !!(supp_rates[i] & 0x80);
2084
2085 if (rate > 110)
2086 *have_higher_than_11mbit = true;
2087
2088 /*
2089 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2090 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2091 *
2092 * Note: Even through the membership selector and the basic
2093 * rate flag share the same bit, they are not exactly
2094 * the same.
2095 */
2096 if (!!(supp_rates[i] & 0x80) &&
2097 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2098 continue;
2099
2100 for (j = 0; j < sband->n_bitrates; j++) {
2101 if (sband->bitrates[j].bitrate == rate) {
2102 *rates |= BIT(j);
2103 if (is_basic)
2104 *basic_rates |= BIT(j);
2105 if (rate < *min_rate) {
2106 *min_rate = rate;
2107 *min_rate_index = j;
2108 }
2109 break;
2110 }
2111 }
2112 }
2113}
Jiri Bencf0706e82007-05-05 11:45:53 -07002114
Johannes Berg66e67e42012-01-20 13:55:27 +01002115static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2116 bool assoc)
2117{
2118 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2119
2120 lockdep_assert_held(&sdata->u.mgd.mtx);
2121
Johannes Berg66e67e42012-01-20 13:55:27 +01002122 if (!assoc) {
2123 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2124
2125 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2126 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002127 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002128 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002129 }
2130
2131 kfree(assoc_data);
2132 sdata->u.mgd.assoc_data = NULL;
2133}
2134
2135static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2136 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002137 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002138{
Johannes Berg46900292009-02-15 12:44:28 +01002139 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002140 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002141 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002142 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002143 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002144 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002145 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002146 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002147 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002148 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002149
Johannes Bergaf6b6372009-12-23 13:15:35 +01002150 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002151
Jiri Bencf0706e82007-05-05 11:45:53 -07002152 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002153 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002154
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002155 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002156 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2157 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002158 aid &= ~(BIT(15) | BIT(14));
2159
Johannes Berg05cb9102011-10-28 11:59:47 +02002160 ifmgd->broken_ap = false;
2161
2162 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002163 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2164 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002165 aid = 0;
2166 ifmgd->broken_ap = true;
2167 }
2168
Johannes Bergaf6b6372009-12-23 13:15:35 +01002169 pos = mgmt->u.assoc_resp.variable;
2170 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2171
Jiri Bencf0706e82007-05-05 11:45:53 -07002172 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002173 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002174 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002175 }
2176
Johannes Berg46900292009-02-15 12:44:28 +01002177 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002178
Guy Eilam2a33bee2011-08-17 15:18:15 +03002179 mutex_lock(&sdata->local->sta_mtx);
2180 /*
2181 * station info was already allocated and inserted before
2182 * the association and should be available to us
2183 */
Johannes Berg7852e362012-01-20 13:55:24 +01002184 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002185 if (WARN_ON(!sta)) {
2186 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002187 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002188 }
2189
Johannes Berg55de9082012-07-26 17:24:39 +02002190 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002191
Johannes Berga8243b72012-11-22 14:32:09 +01002192 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002193 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002194 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002195
Johannes Berg64f68e52012-03-28 10:58:37 +02002196 sta->supports_40mhz =
2197 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2198
Mahesh Palivela818255e2012-10-10 11:33:04 +00002199 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2200 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2201 elems.vht_cap_elem,
2202 &sta->sta.vht_cap);
2203
Johannes Berg4b7679a2008-09-18 18:14:18 +02002204 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002205
Johannes Berg46900292009-02-15 12:44:28 +01002206 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002207 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002208
Johannes Bergddf4ac52008-10-22 11:41:38 +02002209 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002210 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002211
Johannes Bergc8987872012-01-20 13:55:17 +01002212 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2213 if (!err)
2214 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2215 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2216 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2217 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002218 sdata_info(sdata,
2219 "failed to move station %pM to desired state\n",
2220 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002221 WARN_ON(__sta_info_destroy(sta));
2222 mutex_unlock(&sdata->local->sta_mtx);
2223 return false;
2224 }
2225
Johannes Berg7852e362012-01-20 13:55:24 +01002226 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002227
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002228 /*
2229 * Always handle WMM once after association regardless
2230 * of the first value the AP uses. Setting -1 here has
2231 * that effect because the AP values is an unsigned
2232 * 4-bit value.
2233 */
2234 ifmgd->wmm_last_param_set = -1;
2235
Johannes Bergddf4ac52008-10-22 11:41:38 +02002236 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002237 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002238 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002239 else
Johannes Berg3abead52012-03-02 15:56:59 +01002240 ieee80211_set_wmm_default(sdata, false);
2241 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002242
Johannes Berg074d46d2012-03-15 19:45:16 +01002243 if (elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002244 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002245 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2246 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002247
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002248 /* set AID and assoc capability,
2249 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002250 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002251 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002252 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002253
Kalle Valo15b7b062009-03-22 21:57:14 +02002254 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002255 * If we're using 4-addr mode, let the AP know that we're
2256 * doing so, so that it can create the STA VLAN on its side
2257 */
2258 if (ifmgd->use_4addr)
2259 ieee80211_send_4addr_nullfunc(local, sdata);
2260
2261 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002262 * Start timer to probe the connection to the AP now.
2263 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002264 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002265 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002266 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002267
Johannes Bergaf6b6372009-12-23 13:15:35 +01002268 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002269}
2270
Johannes Berg66e67e42012-01-20 13:55:27 +01002271static enum rx_mgmt_action __must_check
2272ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2273 struct ieee80211_mgmt *mgmt, size_t len,
2274 struct cfg80211_bss **bss)
2275{
2276 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2277 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2278 u16 capab_info, status_code, aid;
2279 struct ieee802_11_elems elems;
2280 u8 *pos;
2281 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002282
Johannes Berg66e67e42012-01-20 13:55:27 +01002283 lockdep_assert_held(&ifmgd->mtx);
2284
2285 if (!assoc_data)
2286 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002287 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002288 return RX_MGMT_NONE;
2289
2290 /*
2291 * AssocResp and ReassocResp have identical structure, so process both
2292 * of them in this function.
2293 */
2294
2295 if (len < 24 + 6)
2296 return RX_MGMT_NONE;
2297
2298 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2299 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2300 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2301 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2302
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002303 sdata_info(sdata,
2304 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2305 reassoc ? "Rea" : "A", mgmt->sa,
2306 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002307
2308 pos = mgmt->u.assoc_resp.variable;
2309 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2310
2311 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2312 elems.timeout_int && elems.timeout_int_len == 5 &&
2313 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2314 u32 tu, ms;
2315 tu = get_unaligned_le32(elems.timeout_int + 1);
2316 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002317 sdata_info(sdata,
2318 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2319 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002320 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2321 if (ms > IEEE80211_ASSOC_TIMEOUT)
2322 run_again(ifmgd, assoc_data->timeout);
2323 return RX_MGMT_NONE;
2324 }
2325
2326 *bss = assoc_data->bss;
2327
2328 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002329 sdata_info(sdata, "%pM denied association (code=%d)\n",
2330 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002331 ieee80211_destroy_assoc_data(sdata, false);
2332 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002333 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2334 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002335 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002336 cfg80211_put_bss(*bss);
2337 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2338 }
John W. Linville635d9992012-07-09 16:34:34 -04002339 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002340
2341 /*
2342 * destroy assoc_data afterwards, as otherwise an idle
2343 * recalc after assoc_data is NULL but before associated
2344 * is set can cause the interface to go idle
2345 */
2346 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002347 }
2348
2349 return RX_MGMT_CFG80211_RX_ASSOC;
2350}
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002351static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002352 struct ieee80211_mgmt *mgmt,
2353 size_t len,
2354 struct ieee80211_rx_status *rx_status,
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002355 struct ieee802_11_elems *elems,
2356 bool beacon)
Jiri Bencf0706e82007-05-05 11:45:53 -07002357{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002358 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002359 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002360 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002361 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002362 bool need_ps = false;
2363
Johannes Berg66e67e42012-01-20 13:55:27 +01002364 if (sdata->u.mgd.associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002365 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
Johannes Berg56007a02010-01-26 14:19:52 +01002366 bss = (void *)sdata->u.mgd.associated->priv;
2367 /* not previously set so we may need to recalc */
2368 need_ps = !bss->dtim_period;
2369 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002370
Johannes Berg5bda6172008-09-08 11:05:10 +02002371 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002372 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2373 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002374 else
2375 freq = rx_status->freq;
2376
2377 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2378
2379 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2380 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002381
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002382 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Johannes Berg2a519312009-02-10 21:25:55 +01002383 channel, beacon);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002384 if (bss)
2385 ieee80211_rx_bss_put(local, bss);
2386
2387 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002388 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002389
Johannes Berg56007a02010-01-26 14:19:52 +01002390 if (need_ps) {
2391 mutex_lock(&local->iflist_mtx);
2392 ieee80211_recalc_ps(local, -1);
2393 mutex_unlock(&local->iflist_mtx);
2394 }
2395
Johannes Berg5bc14202012-08-01 16:13:02 +02002396 if (elems->ch_switch_ie &&
2397 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2398 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002399 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002400}
2401
2402
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002403static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002404 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002405{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002406 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002407 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002408 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2409 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002410 struct ieee802_11_elems elems;
2411
Kalle Valo15b7b062009-03-22 21:57:14 +02002412 ifmgd = &sdata->u.mgd;
2413
Johannes Berg77fdaa12009-07-07 03:45:17 +02002414 ASSERT_MGD_MTX(ifmgd);
2415
Joe Perchesb203ca32012-05-08 18:56:52 +00002416 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002417 return; /* ignore ProbeResp to foreign address */
2418
Ester Kummerae6a44e2008-06-27 18:54:48 +03002419 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2420 if (baselen > len)
2421 return;
2422
2423 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2424 &elems);
2425
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002426 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002427
Johannes Berg77fdaa12009-07-07 03:45:17 +02002428 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002429 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002430 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002431
2432 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002433 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002434 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002435 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002436 ifmgd->auth_data->tries = 0;
2437 ifmgd->auth_data->timeout = jiffies;
2438 run_again(ifmgd, ifmgd->auth_data->timeout);
2439 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002440}
2441
Johannes Bergd91f36d2009-04-16 13:17:26 +02002442/*
2443 * This is the canonical list of information elements we care about,
2444 * the filter code also gives us all changes to the Microsoft OUI
2445 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2446 *
2447 * We implement beacon filtering in software since that means we can
2448 * avoid processing the frame here and in cfg80211, and userspace
2449 * will not be able to tell whether the hardware supports it or not.
2450 *
2451 * XXX: This list needs to be dynamic -- userspace needs to be able to
2452 * add items it requires. It also needs to be able to tell us to
2453 * look out for other vendor IEs.
2454 */
2455static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002456 (1ULL << WLAN_EID_COUNTRY) |
2457 (1ULL << WLAN_EID_ERP_INFO) |
2458 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2459 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2460 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002461 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002462
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002463static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002464 struct ieee80211_mgmt *mgmt,
2465 size_t len,
2466 struct ieee80211_rx_status *rx_status)
2467{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002468 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002469 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002470 size_t baselen;
2471 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002472 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002473 struct ieee80211_chanctx_conf *chanctx_conf;
2474 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002475 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002476 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002477 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002478 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002479 u8 *bssid;
2480
Johannes Berg66e67e42012-01-20 13:55:27 +01002481 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002482
Ester Kummerae6a44e2008-06-27 18:54:48 +03002483 /* Process beacon from the current BSS */
2484 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2485 if (baselen > len)
2486 return;
2487
Johannes Berg55de9082012-07-26 17:24:39 +02002488 rcu_read_lock();
2489 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2490 if (!chanctx_conf) {
2491 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002492 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002493 }
2494
Johannes Berg4bf88532012-11-09 11:39:59 +01002495 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002496 rcu_read_unlock();
2497 return;
2498 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002499 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002500 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002501
Johannes Berg66e67e42012-01-20 13:55:27 +01002502 if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002503 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002504 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2505 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002506
Johannes Berg66e67e42012-01-20 13:55:27 +01002507 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2508 false);
2509 ifmgd->assoc_data->have_beacon = true;
2510 ifmgd->assoc_data->sent_assoc = false;
2511 /* continue assoc process */
2512 ifmgd->assoc_data->timeout = jiffies;
2513 run_again(ifmgd, ifmgd->assoc_data->timeout);
2514 return;
2515 }
2516
2517 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002518 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002519 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002520 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002521
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002522 /* Track average RSSI from the Beacon frames of the current AP */
2523 ifmgd->last_beacon_signal = rx_status->signal;
2524 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2525 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002526 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002527 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002528 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002529 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002530 } else {
2531 ifmgd->ave_beacon_signal =
2532 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2533 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2534 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002535 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002536 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002537
2538 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2539 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2540 int sig = ifmgd->ave_beacon_signal;
2541 int last_sig = ifmgd->last_ave_beacon_signal;
2542
2543 /*
2544 * if signal crosses either of the boundaries, invoke callback
2545 * with appropriate parameters
2546 */
2547 if (sig > ifmgd->rssi_max_thold &&
2548 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2549 ifmgd->last_ave_beacon_signal = sig;
2550 drv_rssi_callback(local, RSSI_EVENT_HIGH);
2551 } else if (sig < ifmgd->rssi_min_thold &&
2552 (last_sig >= ifmgd->rssi_max_thold ||
2553 last_sig == 0)) {
2554 ifmgd->last_ave_beacon_signal = sig;
2555 drv_rssi_callback(local, RSSI_EVENT_LOW);
2556 }
2557 }
2558
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002559 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002560 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002561 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002562 int sig = ifmgd->ave_beacon_signal / 16;
2563 int last_event = ifmgd->last_cqm_event_signal;
2564 int thold = bss_conf->cqm_rssi_thold;
2565 int hyst = bss_conf->cqm_rssi_hyst;
2566 if (sig < thold &&
2567 (last_event == 0 || sig < last_event - hyst)) {
2568 ifmgd->last_cqm_event_signal = sig;
2569 ieee80211_cqm_rssi_notify(
2570 &sdata->vif,
2571 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2572 GFP_KERNEL);
2573 } else if (sig > thold &&
2574 (last_event == 0 || sig > last_event + hyst)) {
2575 ifmgd->last_cqm_event_signal = sig;
2576 ieee80211_cqm_rssi_notify(
2577 &sdata->vif,
2578 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2579 GFP_KERNEL);
2580 }
2581 }
2582
Johannes Bergb291ba12009-07-10 15:29:03 +02002583 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002584 mlme_dbg_ratelimited(sdata,
2585 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002586 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002587 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002588 ieee80211_run_deferred_scan(local);
2589 mutex_unlock(&local->mtx);
2590
Johannes Berg4e751842009-06-10 15:16:52 +02002591 mutex_lock(&local->iflist_mtx);
2592 ieee80211_recalc_ps(local, -1);
2593 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002594 }
2595
Johannes Bergb291ba12009-07-10 15:29:03 +02002596 /*
2597 * Push the beacon loss detection into the future since
2598 * we are processing a beacon from the AP just now.
2599 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002600 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002601
Johannes Bergd91f36d2009-04-16 13:17:26 +02002602 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2603 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2604 len - baselen, &elems,
2605 care_about_ies, ncrc);
2606
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302607 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002608 bool directed_tim = ieee80211_check_tim(elems.tim,
2609 elems.tim_len,
2610 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002611 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002612 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002613 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2614 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2615 ieee80211_hw_config(local,
2616 IEEE80211_CONF_CHANGE_PS);
2617 }
Kalle Valo572e0012009-02-10 17:09:31 +02002618 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302619 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002620 local->pspolling = true;
2621
2622 /*
2623 * Here is assumed that the driver will be
2624 * able to send ps-poll frame and receive a
2625 * response even though power save mode is
2626 * enabled, but some drivers might require
2627 * to disable power save here. This needs
2628 * to be investigated.
2629 */
2630 ieee80211_send_pspoll(local, sdata);
2631 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002632 }
2633 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002634
Johannes Berg488dd7b2012-10-29 20:08:01 +01002635 if (sdata->vif.p2p) {
2636 u8 noa[2];
2637 int ret;
2638
2639 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2640 len - baselen,
2641 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2642 noa, sizeof(noa));
2643 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2644 bss_conf->p2p_oppps = noa[1] & 0x80;
2645 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2646 changed |= BSS_CHANGED_P2P_PS;
2647 sdata->u.mgd.p2p_noa_index = noa[0];
2648 /*
2649 * make sure we update all information, the CRC
2650 * mechanism doesn't look at P2P attributes.
2651 */
2652 ifmgd->beacon_crc_valid = false;
2653 }
2654 }
2655
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002656 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002657 return;
2658 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002659 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002660
Johannes Berg7d257452012-07-06 17:37:43 +02002661 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
2662 true);
2663
2664 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2665 elems.wmm_param_len))
2666 changed |= BSS_CHANGED_QOS;
2667
Johannes Berg7a5158e2008-10-08 10:59:33 +02002668 if (elems.erp_info && elems.erp_info_len >= 1) {
2669 erp_valid = true;
2670 erp_value = elems.erp_info[0];
2671 } else {
2672 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002673 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002674 changed |= ieee80211_handle_bss_capability(sdata,
2675 le16_to_cpu(mgmt->u.beacon.capab_info),
2676 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002677
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002678
Johannes Berg074d46d2012-03-15 19:45:16 +01002679 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002680 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002681 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2682 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002683
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002684 if (elems.country_elem && elems.pwr_constr_elem &&
2685 mgmt->u.probe_resp.capab_info &
2686 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002687 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2688 elems.country_elem,
2689 elems.country_elem_len,
2690 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002691
Johannes Berg471b3ef2007-12-28 14:32:58 +01002692 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002693}
2694
Johannes Berg1fa57d02010-06-10 10:21:32 +02002695void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2696 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002697{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002698 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002699 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002700 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002701 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002702 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002703 u16 fc;
2704
Jiri Bencf0706e82007-05-05 11:45:53 -07002705 rx_status = (struct ieee80211_rx_status *) skb->cb;
2706 mgmt = (struct ieee80211_mgmt *) skb->data;
2707 fc = le16_to_cpu(mgmt->frame_control);
2708
Johannes Berg77fdaa12009-07-07 03:45:17 +02002709 mutex_lock(&ifmgd->mtx);
2710
Johannes Berg66e67e42012-01-20 13:55:27 +01002711 switch (fc & IEEE80211_FCTL_STYPE) {
2712 case IEEE80211_STYPE_BEACON:
2713 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2714 break;
2715 case IEEE80211_STYPE_PROBE_RESP:
2716 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2717 break;
2718 case IEEE80211_STYPE_AUTH:
2719 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2720 break;
2721 case IEEE80211_STYPE_DEAUTH:
2722 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2723 break;
2724 case IEEE80211_STYPE_DISASSOC:
2725 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2726 break;
2727 case IEEE80211_STYPE_ASSOC_RESP:
2728 case IEEE80211_STYPE_REASSOC_RESP:
2729 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2730 break;
2731 case IEEE80211_STYPE_ACTION:
2732 switch (mgmt->u.action.category) {
2733 case WLAN_CATEGORY_SPECTRUM_MGMT:
2734 ieee80211_sta_process_chanswitch(sdata,
2735 &mgmt->u.action.u.chan_switch.sw_elem,
2736 (void *)ifmgd->associated->priv,
2737 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002738 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002739 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002740 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002741 mutex_unlock(&ifmgd->mtx);
2742
Johannes Berg66e67e42012-01-20 13:55:27 +01002743 switch (rma) {
2744 case RX_MGMT_NONE:
2745 /* no action */
2746 break;
2747 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002748 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002749 break;
2750 case RX_MGMT_CFG80211_DISASSOC:
2751 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2752 break;
2753 case RX_MGMT_CFG80211_RX_AUTH:
2754 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2755 break;
2756 case RX_MGMT_CFG80211_RX_ASSOC:
2757 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2758 break;
2759 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2760 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2761 break;
2762 default:
2763 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002764 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002765}
2766
Johannes Berg9c6bd792008-09-11 00:01:52 +02002767static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002768{
2769 struct ieee80211_sub_if_data *sdata =
2770 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002771 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002772 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002773
Johannes Berg5bb644a2009-05-17 11:40:42 +02002774 if (local->quiescing) {
2775 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2776 return;
2777 }
2778
Johannes Berg64592c82010-06-10 10:21:31 +02002779 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002780}
2781
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002782static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002783 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002784{
2785 struct ieee80211_local *local = sdata->local;
2786 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002787 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002788
Johannes Berg37ad3882012-02-24 13:50:54 +01002789 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2790 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002791 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002792
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002793 /*
2794 * must be outside lock due to cfg80211,
2795 * but that's not a problem.
2796 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002797 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002798
2799 mutex_lock(&local->mtx);
2800 ieee80211_recalc_idle(local);
2801 mutex_unlock(&local->mtx);
2802
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002803 mutex_lock(&ifmgd->mtx);
2804}
2805
Johannes Berg66e67e42012-01-20 13:55:27 +01002806static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2807{
2808 struct ieee80211_local *local = sdata->local;
2809 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2810 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2811
2812 lockdep_assert_held(&ifmgd->mtx);
2813
2814 if (WARN_ON_ONCE(!auth_data))
2815 return -EINVAL;
2816
Johannes Berg66e67e42012-01-20 13:55:27 +01002817 auth_data->tries++;
2818
2819 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002820 sdata_info(sdata, "authentication with %pM timed out\n",
2821 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002822
2823 /*
2824 * Most likely AP is not in the range so remove the
2825 * bss struct for that AP.
2826 */
2827 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2828
2829 return -ETIMEDOUT;
2830 }
2831
Johannes Berga1845fc2012-06-27 13:18:36 +02002832 drv_mgd_prepare_tx(local, sdata);
2833
Johannes Berg66e67e42012-01-20 13:55:27 +01002834 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002835 u16 trans = 1;
2836 u16 status = 0;
2837
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002838 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2839 auth_data->bss->bssid, auth_data->tries,
2840 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002841
2842 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002843
2844 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2845 trans = auth_data->sae_trans;
2846 status = auth_data->sae_status;
2847 auth_data->expected_transaction = trans;
2848 }
2849
2850 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2851 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002852 auth_data->bss->bssid,
2853 auth_data->bss->bssid, NULL, 0, 0);
2854 } else {
2855 const u8 *ssidie;
2856
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002857 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2858 auth_data->bss->bssid, auth_data->tries,
2859 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002860
2861 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
2862 if (!ssidie)
2863 return -EINVAL;
2864 /*
2865 * Direct probe is sent to broadcast address as some APs
2866 * will not answer to direct packet in unassociated state.
2867 */
2868 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Bergfe94fe02012-07-30 12:26:34 +02002869 NULL, 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02002870 auth_data->bss->channel, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002871 }
2872
2873 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2874 run_again(ifmgd, auth_data->timeout);
2875
2876 return 0;
2877}
2878
2879static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2880{
2881 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2882 struct ieee80211_local *local = sdata->local;
2883
2884 lockdep_assert_held(&sdata->u.mgd.mtx);
2885
Johannes Berg66e67e42012-01-20 13:55:27 +01002886 assoc_data->tries++;
2887 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002888 sdata_info(sdata, "association with %pM timed out\n",
2889 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002890
2891 /*
2892 * Most likely AP is not in the range so remove the
2893 * bss struct for that AP.
2894 */
2895 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2896
2897 return -ETIMEDOUT;
2898 }
2899
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002900 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2901 assoc_data->bss->bssid, assoc_data->tries,
2902 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002903 ieee80211_send_assoc(sdata);
2904
2905 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2906 run_again(&sdata->u.mgd, assoc_data->timeout);
2907
2908 return 0;
2909}
2910
Johannes Berg1fa57d02010-06-10 10:21:32 +02002911void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002912{
Johannes Berg60f8b392008-09-08 17:44:22 +02002913 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002914 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002915
Johannes Berg77fdaa12009-07-07 03:45:17 +02002916 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002917
Johannes Berg66e67e42012-01-20 13:55:27 +01002918 if (ifmgd->auth_data &&
2919 time_after(jiffies, ifmgd->auth_data->timeout)) {
2920 if (ifmgd->auth_data->done) {
2921 /*
2922 * ok ... we waited for assoc but userspace didn't,
2923 * so let's just kill the auth data
2924 */
2925 ieee80211_destroy_auth_data(sdata, false);
2926 } else if (ieee80211_probe_auth(sdata)) {
2927 u8 bssid[ETH_ALEN];
2928
2929 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2930
2931 ieee80211_destroy_auth_data(sdata, false);
2932
2933 mutex_unlock(&ifmgd->mtx);
2934 cfg80211_send_auth_timeout(sdata->dev, bssid);
2935 mutex_lock(&ifmgd->mtx);
2936 }
2937 } else if (ifmgd->auth_data)
2938 run_again(ifmgd, ifmgd->auth_data->timeout);
2939
2940 if (ifmgd->assoc_data &&
2941 time_after(jiffies, ifmgd->assoc_data->timeout)) {
2942 if (!ifmgd->assoc_data->have_beacon ||
2943 ieee80211_do_assoc(sdata)) {
2944 u8 bssid[ETH_ALEN];
2945
2946 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
2947
2948 ieee80211_destroy_assoc_data(sdata, false);
2949
2950 mutex_unlock(&ifmgd->mtx);
2951 cfg80211_send_assoc_timeout(sdata->dev, bssid);
2952 mutex_lock(&ifmgd->mtx);
2953 }
2954 } else if (ifmgd->assoc_data)
2955 run_again(ifmgd, ifmgd->assoc_data->timeout);
2956
Johannes Bergb291ba12009-07-10 15:29:03 +02002957 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2958 IEEE80211_STA_CONNECTION_POLL) &&
2959 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03002960 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002961 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002962
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002963 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002964
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002965 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08002966 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002967 else
Ben Greear180205b2011-02-04 15:30:24 -08002968 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01002969
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002970 /* ACK received for nullfunc probing frame */
2971 if (!ifmgd->probe_send_count)
2972 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002973 else if (ifmgd->nullfunc_failed) {
2974 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002975 mlme_dbg(sdata,
2976 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
2977 bssid, ifmgd->probe_send_count,
2978 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002979 ieee80211_mgd_probe_ap_send(sdata);
2980 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002981 mlme_dbg(sdata,
2982 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
2983 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02002984 ieee80211_sta_connection_lost(sdata, bssid,
2985 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002986 }
2987 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02002988 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002989 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002990 mlme_dbg(sdata,
2991 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
2992 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02002993 ieee80211_sta_connection_lost(sdata, bssid,
2994 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002995 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002996 mlme_dbg(sdata,
2997 "No probe response from AP %pM after %dms, try %d/%i\n",
2998 bssid, probe_wait_ms,
2999 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003000 ieee80211_mgd_probe_ap_send(sdata);
3001 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003002 /*
3003 * We actually lost the connection ... or did we?
3004 * Let's make sure!
3005 */
Ben Greearb38afa82010-10-07 16:12:06 -07003006 wiphy_debug(local->hw.wiphy,
3007 "%s: No probe response from AP %pM"
3008 " after %dms, disconnecting.\n",
3009 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003010 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003011
Johannes Berg95acac62011-07-12 12:30:59 +02003012 ieee80211_sta_connection_lost(sdata, bssid,
3013 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02003014 }
3015 }
3016
Johannes Berg77fdaa12009-07-07 03:45:17 +02003017 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003018
3019 mutex_lock(&local->mtx);
3020 ieee80211_recalc_idle(local);
3021 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003022}
Johannes Berg0a51b272008-09-08 17:44:25 +02003023
Johannes Bergb291ba12009-07-10 15:29:03 +02003024static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3025{
3026 struct ieee80211_sub_if_data *sdata =
3027 (struct ieee80211_sub_if_data *) data;
3028 struct ieee80211_local *local = sdata->local;
3029
3030 if (local->quiescing)
3031 return;
3032
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003033 ieee80211_queue_work(&sdata->local->hw,
3034 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003035}
3036
3037static void ieee80211_sta_conn_mon_timer(unsigned long data)
3038{
3039 struct ieee80211_sub_if_data *sdata =
3040 (struct ieee80211_sub_if_data *) data;
3041 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3042 struct ieee80211_local *local = sdata->local;
3043
3044 if (local->quiescing)
3045 return;
3046
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003047 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003048}
3049
3050static void ieee80211_sta_monitor_work(struct work_struct *work)
3051{
3052 struct ieee80211_sub_if_data *sdata =
3053 container_of(work, struct ieee80211_sub_if_data,
3054 u.mgd.monitor_work);
3055
3056 ieee80211_mgd_probe_ap(sdata, false);
3057}
3058
Johannes Berga1678f82008-09-11 00:01:47 +02003059static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3060{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003061 u32 flags;
3062
Kalle Vaload935682009-04-19 08:47:19 +03003063 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003064 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003065
Johannes Bergb291ba12009-07-10 15:29:03 +02003066 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003067 flags = sdata->local->hw.flags;
3068 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3069 ieee80211_queue_work(&sdata->local->hw,
3070 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003071 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003072 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003073 }
Johannes Berga1678f82008-09-11 00:01:47 +02003074}
3075
Johannes Berg5bb644a2009-05-17 11:40:42 +02003076#ifdef CONFIG_PM
3077void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3078{
3079 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3080
3081 /*
3082 * we need to use atomic bitops for the running bits
3083 * only because both timers might fire at the same
3084 * time -- the code here is properly synchronised.
3085 */
3086
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003087 cancel_work_sync(&ifmgd->request_smps_work);
3088
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003089 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003090 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003091 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003092 if (del_timer_sync(&ifmgd->timer))
3093 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3094
3095 cancel_work_sync(&ifmgd->chswitch_work);
3096 if (del_timer_sync(&ifmgd->chswitch_timer))
3097 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003098
Johannes Bergb291ba12009-07-10 15:29:03 +02003099 /* these will just be re-established on connection */
3100 del_timer_sync(&ifmgd->conn_mon_timer);
3101 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003102}
3103
3104void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3105{
3106 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3107
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303108 if (!ifmgd->associated)
3109 return;
3110
Johannes Berg95acac62011-07-12 12:30:59 +02003111 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3112 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3113 mutex_lock(&ifmgd->mtx);
3114 if (ifmgd->associated) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003115 mlme_dbg(sdata,
3116 "driver requested disconnect after resume\n");
Johannes Berg95acac62011-07-12 12:30:59 +02003117 ieee80211_sta_connection_lost(sdata,
3118 ifmgd->associated->bssid,
3119 WLAN_REASON_UNSPECIFIED);
3120 mutex_unlock(&ifmgd->mtx);
3121 return;
3122 }
3123 mutex_unlock(&ifmgd->mtx);
3124 }
3125
Johannes Berg5bb644a2009-05-17 11:40:42 +02003126 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3127 add_timer(&ifmgd->timer);
3128 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3129 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003130 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003131
3132 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003133 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003134 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003135}
3136#endif
3137
Johannes Berg9c6bd792008-09-11 00:01:52 +02003138/* interface setup */
3139void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3140{
Johannes Berg46900292009-02-15 12:44:28 +01003141 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003142
Johannes Berg46900292009-02-15 12:44:28 +01003143 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003144 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003145 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003146 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3147 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003148 INIT_WORK(&ifmgd->csa_connection_drop_work,
3149 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003150 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003151 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003152 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003153 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3154 (unsigned long) sdata);
3155 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3156 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003157 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303158 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003159
Johannes Bergab1faea2009-07-01 21:41:17 +02003160 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303161 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003162 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3163 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003164
Johannes Berg77fdaa12009-07-07 03:45:17 +02003165 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003166
3167 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3168 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3169 else
3170 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003171}
3172
3173/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003174void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3175{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003176 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003177
3178 /* Restart STA timers */
3179 rcu_read_lock();
3180 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3181 ieee80211_restart_sta_timer(sdata);
3182 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003183}
Johannes Berg10f644a2009-04-16 13:17:25 +02003184
3185int ieee80211_max_network_latency(struct notifier_block *nb,
3186 unsigned long data, void *dummy)
3187{
3188 s32 latency_usec = (s32) data;
3189 struct ieee80211_local *local =
3190 container_of(nb, struct ieee80211_local,
3191 network_latency_notifier);
3192
3193 mutex_lock(&local->iflist_mtx);
3194 ieee80211_recalc_ps(local, latency_usec);
3195 mutex_unlock(&local->iflist_mtx);
3196
3197 return 0;
3198}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003199
Johannes Bergf2d9d272012-11-22 14:11:39 +01003200static u32 chandef_downgrade(struct cfg80211_chan_def *c)
3201{
3202 u32 ret;
3203 int tmp;
3204
3205 switch (c->width) {
3206 case NL80211_CHAN_WIDTH_20:
3207 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3208 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3209 break;
3210 case NL80211_CHAN_WIDTH_40:
3211 c->width = NL80211_CHAN_WIDTH_20;
3212 c->center_freq1 = c->chan->center_freq;
3213 ret = IEEE80211_STA_DISABLE_40MHZ |
3214 IEEE80211_STA_DISABLE_VHT;
3215 break;
3216 case NL80211_CHAN_WIDTH_80:
3217 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3218 /* n_P40 */
3219 tmp /= 2;
3220 /* freq_P40 */
3221 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3222 c->width = NL80211_CHAN_WIDTH_40;
3223 ret = IEEE80211_STA_DISABLE_VHT;
3224 break;
3225 case NL80211_CHAN_WIDTH_80P80:
3226 c->center_freq2 = 0;
3227 c->width = NL80211_CHAN_WIDTH_80;
3228 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3229 IEEE80211_STA_DISABLE_160MHZ;
3230 break;
3231 case NL80211_CHAN_WIDTH_160:
3232 /* n_P20 */
3233 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3234 /* n_P80 */
3235 tmp /= 4;
3236 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3237 c->width = NL80211_CHAN_WIDTH_80;
3238 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3239 IEEE80211_STA_DISABLE_160MHZ;
3240 break;
3241 default:
3242 case NL80211_CHAN_WIDTH_20_NOHT:
3243 WARN_ON_ONCE(1);
3244 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3245 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3246 break;
3247 }
3248
3249 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3250
3251 return ret;
3252}
3253
3254static u32
3255ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
3256 struct ieee80211_supported_band *sband,
3257 struct ieee80211_channel *channel,
3258 const struct ieee80211_ht_operation *ht_oper,
3259 const struct ieee80211_vht_operation *vht_oper,
3260 struct cfg80211_chan_def *chandef)
3261{
3262 struct cfg80211_chan_def vht_chandef;
3263 u32 ht_cfreq, ret;
3264
3265 chandef->chan = channel;
3266 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3267 chandef->center_freq1 = channel->center_freq;
3268 chandef->center_freq2 = 0;
3269
3270 if (!ht_oper || !sband->ht_cap.ht_supported) {
3271 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3272 goto out;
3273 }
3274
3275 chandef->width = NL80211_CHAN_WIDTH_20;
3276
3277 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3278 channel->band);
3279 /* check that channel matches the right operating channel */
3280 if (channel->center_freq != ht_cfreq) {
3281 /*
3282 * It's possible that some APs are confused here;
3283 * Netgear WNDR3700 sometimes reports 4 higher than
3284 * the actual channel in association responses, but
3285 * since we look at probe response/beacon data here
3286 * it should be OK.
3287 */
3288 sdata_info(sdata,
3289 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3290 channel->center_freq, ht_cfreq,
3291 ht_oper->primary_chan, channel->band);
3292 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3293 goto out;
3294 }
3295
3296 /* check 40 MHz support, if we have it */
3297 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3298 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3299 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3300 chandef->width = NL80211_CHAN_WIDTH_40;
3301 chandef->center_freq1 += 10;
3302 break;
3303 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3304 chandef->width = NL80211_CHAN_WIDTH_40;
3305 chandef->center_freq1 -= 10;
3306 break;
3307 }
3308 } else {
3309 /* 40 MHz (and 80 MHz) must be supported for VHT */
3310 ret = IEEE80211_STA_DISABLE_VHT;
3311 goto out;
3312 }
3313
3314 if (!vht_oper || !sband->vht_cap.vht_supported) {
3315 ret = IEEE80211_STA_DISABLE_VHT;
3316 goto out;
3317 }
3318
3319 vht_chandef.chan = channel;
3320 vht_chandef.center_freq1 =
3321 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
3322 channel->band);
3323 vht_chandef.center_freq2 = 0;
3324
3325 if (vht_oper->center_freq_seg2_idx)
3326 vht_chandef.center_freq2 =
3327 ieee80211_channel_to_frequency(
3328 vht_oper->center_freq_seg2_idx,
3329 channel->band);
3330
3331 switch (vht_oper->chan_width) {
3332 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3333 vht_chandef.width = chandef->width;
3334 break;
3335 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3336 vht_chandef.width = NL80211_CHAN_WIDTH_80;
3337 break;
3338 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3339 vht_chandef.width = NL80211_CHAN_WIDTH_160;
3340 break;
3341 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3342 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
3343 break;
3344 default:
3345 sdata_info(sdata,
3346 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
3347 vht_oper->chan_width);
3348 ret = IEEE80211_STA_DISABLE_VHT;
3349 goto out;
3350 }
3351
3352 if (!cfg80211_chandef_valid(&vht_chandef)) {
3353 sdata_info(sdata,
3354 "AP VHT information is invalid, disable VHT\n");
3355 ret = IEEE80211_STA_DISABLE_VHT;
3356 goto out;
3357 }
3358
3359 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
3360 ret = 0;
3361 goto out;
3362 }
3363
3364 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
3365 sdata_info(sdata,
3366 "AP VHT information doesn't match HT, disable VHT\n");
3367 ret = IEEE80211_STA_DISABLE_VHT;
3368 goto out;
3369 }
3370
3371 *chandef = vht_chandef;
3372
3373 ret = 0;
3374
3375 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
3376 IEEE80211_CHAN_DISABLED)) {
3377 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
3378 ret = IEEE80211_STA_DISABLE_HT |
3379 IEEE80211_STA_DISABLE_VHT;
3380 goto out;
3381 }
3382
3383 ret = chandef_downgrade(chandef);
3384 }
3385
3386 if (chandef->width != vht_chandef.width)
3387 sdata_info(sdata,
3388 "local regulatory prevented using AP HT/VHT configuration, downgraded\n");
3389
3390out:
3391 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
3392 return ret;
3393}
3394
3395static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3396 struct cfg80211_bss *cbss)
3397{
3398 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3399 const u8 *ht_cap_ie, *vht_cap_ie;
3400 const struct ieee80211_ht_cap *ht_cap;
3401 const struct ieee80211_vht_cap *vht_cap;
3402 u8 chains = 1;
3403
3404 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3405 return chains;
3406
3407 ht_cap_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY,
3408 cbss->information_elements,
3409 cbss->len_information_elements);
3410 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3411 ht_cap = (void *)(ht_cap_ie + 2);
3412 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3413 /*
3414 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3415 * "Tx Unequal Modulation Supported" fields.
3416 */
3417 }
3418
3419 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3420 return chains;
3421
3422 vht_cap_ie = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY,
3423 cbss->information_elements,
3424 cbss->len_information_elements);
3425 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3426 u8 nss;
3427 u16 tx_mcs_map;
3428
3429 vht_cap = (void *)(vht_cap_ie + 2);
3430 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3431 for (nss = 8; nss > 0; nss--) {
3432 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3433 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3434 break;
3435 }
3436 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3437 chains = max(chains, nss);
3438 }
3439
3440 return chains;
3441}
3442
Johannes Bergb17166a2012-07-27 11:41:27 +02003443static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3444 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003445{
3446 struct ieee80211_local *local = sdata->local;
3447 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003448 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003449 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003450 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003451 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003452 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003453
Johannes Berg24398e32012-03-28 10:58:36 +02003454 sband = local->hw.wiphy->bands[cbss->channel->band];
3455
Johannes Bergf2d9d272012-11-22 14:11:39 +01003456 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3457 IEEE80211_STA_DISABLE_80P80MHZ |
3458 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003459
Johannes Bergf2d9d272012-11-22 14:11:39 +01003460 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3461 sband->ht_cap.ht_supported) {
3462 const u8 *ht_oper_ie;
3463
Johannes Berg24398e32012-03-28 10:58:36 +02003464 ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
3465 cbss->information_elements,
3466 cbss->len_information_elements);
3467 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3468 ht_oper = (void *)(ht_oper_ie + 2);
3469 }
3470
Johannes Bergf2d9d272012-11-22 14:11:39 +01003471 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3472 sband->vht_cap.vht_supported) {
3473 const u8 *vht_oper_ie;
3474
3475 vht_oper_ie = cfg80211_find_ie(WLAN_EID_VHT_OPERATION,
3476 cbss->information_elements,
3477 cbss->len_information_elements);
3478 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3479 vht_oper = (void *)(vht_oper_ie + 2);
3480 if (vht_oper && !ht_oper) {
3481 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003482 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003483 "AP advertised VHT without HT, disabling both\n");
3484 sdata->flags |= IEEE80211_STA_DISABLE_HT;
3485 sdata->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003486 }
3487 }
3488
Johannes Bergf2d9d272012-11-22 14:11:39 +01003489 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3490 cbss->channel,
3491 ht_oper, vht_oper,
3492 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003493
Johannes Bergf2d9d272012-11-22 14:11:39 +01003494 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3495 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003496
Johannes Berg04ecd252012-09-11 14:34:12 +02003497 /* will change later if needed */
3498 sdata->smps_mode = IEEE80211_SMPS_OFF;
3499
Johannes Bergf2d9d272012-11-22 14:11:39 +01003500 /*
3501 * If this fails (possibly due to channel context sharing
3502 * on incompatible channels, e.g. 80+80 and 160 sharing the
3503 * same control channel) try to use a smaller bandwidth.
3504 */
3505 ret = ieee80211_vif_use_channel(sdata, &chandef,
3506 IEEE80211_CHANCTX_SHARED);
3507 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
3508 ifmgd->flags |= chandef_downgrade(&chandef);
3509 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003510}
3511
3512static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3513 struct cfg80211_bss *cbss, bool assoc)
3514{
3515 struct ieee80211_local *local = sdata->local;
3516 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3517 struct ieee80211_bss *bss = (void *)cbss->priv;
3518 struct sta_info *new_sta = NULL;
3519 bool have_sta = false;
3520 int err;
3521
3522 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3523 return -EINVAL;
3524
3525 if (assoc) {
3526 rcu_read_lock();
3527 have_sta = sta_info_get(sdata, cbss->bssid);
3528 rcu_read_unlock();
3529 }
3530
3531 if (!have_sta) {
3532 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3533 if (!new_sta)
3534 return -ENOMEM;
3535 }
3536
3537 mutex_lock(&local->mtx);
3538 ieee80211_recalc_idle(sdata->local);
3539 mutex_unlock(&local->mtx);
3540
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003541 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003542 u32 rates = 0, basic_rates = 0;
3543 bool have_higher_than_11mbit;
3544 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003545 struct ieee80211_supported_band *sband;
3546
3547 sband = local->hw.wiphy->bands[cbss->channel->band];
3548
3549 err = ieee80211_prep_channel(sdata, cbss);
3550 if (err) {
3551 sta_info_free(local, new_sta);
3552 return err;
3553 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003554
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003555 ieee80211_get_rates(sband, bss->supp_rates,
3556 bss->supp_rates_len,
3557 &rates, &basic_rates,
3558 &have_higher_than_11mbit,
3559 &min_rate, &min_rate_index);
3560
3561 /*
3562 * This used to be a workaround for basic rates missing
3563 * in the association response frame. Now that we no
3564 * longer use the basic rates from there, it probably
3565 * doesn't happen any more, but keep the workaround so
3566 * in case some *other* APs are buggy in different ways
3567 * we can connect -- with a warning.
3568 */
3569 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003570 sdata_info(sdata,
3571 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003572 basic_rates = BIT(min_rate_index);
3573 }
3574
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003575 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003576 sdata->vif.bss_conf.basic_rates = basic_rates;
3577
3578 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003579 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003580 have_higher_than_11mbit)
3581 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3582 else
3583 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3584
3585 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3586
Johannes Berg8c358bc2012-05-22 22:13:05 +02003587 /* set timing information */
3588 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3589 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3590 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3591
3592 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003593 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003594 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3595 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003596
3597 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003598 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003599
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003600 err = sta_info_insert(new_sta);
3601 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003602 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003603 sdata_info(sdata,
3604 "failed to insert STA entry for the AP (error %d)\n",
3605 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003606 return err;
3607 }
3608 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003609 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003610
3611 return 0;
3612}
3613
Johannes Berg77fdaa12009-07-07 03:45:17 +02003614/* config hooks */
3615int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3616 struct cfg80211_auth_request *req)
3617{
Johannes Berg66e67e42012-01-20 13:55:27 +01003618 struct ieee80211_local *local = sdata->local;
3619 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3620 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003621 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003622 int err;
3623
3624 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003625
3626 switch (req->auth_type) {
3627 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3628 auth_alg = WLAN_AUTH_OPEN;
3629 break;
3630 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003631 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003632 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003633 auth_alg = WLAN_AUTH_SHARED_KEY;
3634 break;
3635 case NL80211_AUTHTYPE_FT:
3636 auth_alg = WLAN_AUTH_FT;
3637 break;
3638 case NL80211_AUTHTYPE_NETWORK_EAP:
3639 auth_alg = WLAN_AUTH_LEAP;
3640 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003641 case NL80211_AUTHTYPE_SAE:
3642 auth_alg = WLAN_AUTH_SAE;
3643 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003644 default:
3645 return -EOPNOTSUPP;
3646 }
3647
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003648 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3649 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003650 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003651 return -ENOMEM;
3652
Johannes Berg66e67e42012-01-20 13:55:27 +01003653 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003654
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003655 if (req->sae_data_len >= 4) {
3656 __le16 *pos = (__le16 *) req->sae_data;
3657 auth_data->sae_trans = le16_to_cpu(pos[0]);
3658 auth_data->sae_status = le16_to_cpu(pos[1]);
3659 memcpy(auth_data->data, req->sae_data + 4,
3660 req->sae_data_len - 4);
3661 auth_data->data_len += req->sae_data_len - 4;
3662 }
3663
Johannes Berg77fdaa12009-07-07 03:45:17 +02003664 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003665 memcpy(&auth_data->data[auth_data->data_len],
3666 req->ie, req->ie_len);
3667 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003668 }
3669
Johannes Bergfffd0932009-07-08 14:22:54 +02003670 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003671 auth_data->key_len = req->key_len;
3672 auth_data->key_idx = req->key_idx;
3673 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003674 }
3675
Johannes Berg66e67e42012-01-20 13:55:27 +01003676 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003677
Johannes Berg66e67e42012-01-20 13:55:27 +01003678 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003679
Johannes Berg66e67e42012-01-20 13:55:27 +01003680 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003681
Johannes Berg66e67e42012-01-20 13:55:27 +01003682 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3683 ifmgd->assoc_data) {
3684 err = -EBUSY;
3685 goto err_free;
3686 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003687
Johannes Berg66e67e42012-01-20 13:55:27 +01003688 if (ifmgd->auth_data)
3689 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003690
Johannes Berg66e67e42012-01-20 13:55:27 +01003691 /* prep auth_data so we don't go into idle on disassoc */
3692 ifmgd->auth_data = auth_data;
3693
3694 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003695 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003696
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003697 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003698
Johannes Berga1cf7752012-03-08 15:02:07 +01003699 err = ieee80211_prep_connection(sdata, req->bss, false);
3700 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003701 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003702
Johannes Berg66e67e42012-01-20 13:55:27 +01003703 err = ieee80211_probe_auth(sdata);
3704 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003705 sta_info_destroy_addr(sdata, req->bss->bssid);
3706 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003707 }
3708
Johannes Berg66e67e42012-01-20 13:55:27 +01003709 /* hold our own reference */
3710 cfg80211_ref_bss(auth_data->bss);
3711 err = 0;
3712 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003713
Johannes Berg66e67e42012-01-20 13:55:27 +01003714 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003715 memset(ifmgd->bssid, 0, ETH_ALEN);
3716 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003717 ifmgd->auth_data = NULL;
3718 err_free:
3719 kfree(auth_data);
3720 out_unlock:
3721 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003722
Johannes Berg66e67e42012-01-20 13:55:27 +01003723 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003724}
3725
Johannes Berg77fdaa12009-07-07 03:45:17 +02003726int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3727 struct cfg80211_assoc_request *req)
3728{
Johannes Berg66e67e42012-01-20 13:55:27 +01003729 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003730 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003731 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003732 struct ieee80211_mgd_assoc_data *assoc_data;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003733 struct ieee80211_supported_band *sband;
Johannes Berg9dde6422012-05-16 23:43:19 +02003734 const u8 *ssidie, *ht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003735 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003736
Johannes Berg66e67e42012-01-20 13:55:27 +01003737 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3738 if (!ssidie)
3739 return -EINVAL;
Jouni Malinen9c87ba62010-02-28 12:13:46 +02003740
Johannes Berg66e67e42012-01-20 13:55:27 +01003741 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3742 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003743 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003744
Johannes Berg66e67e42012-01-20 13:55:27 +01003745 mutex_lock(&ifmgd->mtx);
3746
3747 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003748 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003749
3750 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3751 err = -EBUSY;
3752 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003753 }
3754
Johannes Berg66e67e42012-01-20 13:55:27 +01003755 if (ifmgd->assoc_data) {
3756 err = -EBUSY;
3757 goto err_free;
3758 }
3759
3760 if (ifmgd->auth_data) {
3761 bool match;
3762
3763 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003764 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003765 ieee80211_destroy_auth_data(sdata, match);
3766 }
3767
3768 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003769
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003770 ifmgd->beacon_crc_valid = false;
3771
Johannes Bergde5036a2012-03-08 15:02:03 +01003772 /*
3773 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3774 * We still associate in non-HT mode (11a/b/g) if any one of these
3775 * ciphers is configured as pairwise.
3776 * We can set this to true for non-11n hardware, that'll be checked
3777 * separately along with the peer capabilities.
3778 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003779 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003780 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3781 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003782 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003783 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003784 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003785 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003786 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003787 }
3788 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003789
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003790 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003791 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003792 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3793 }
Ben Greearef96a8422011-11-18 11:32:00 -08003794
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003795 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3796 sband = local->hw.wiphy->bands[req->bss->channel->band];
3797 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003798 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003799 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003800 if (!bss->wmm_used)
3801 netdev_info(sdata->dev,
3802 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003803 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003804
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003805 /* disable VHT if we don't support it or the AP doesn't use WMM */
3806 if (!sband->vht_cap.vht_supported ||
3807 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3808 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003809 if (!bss->wmm_used)
3810 netdev_info(sdata->dev,
3811 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003812 }
3813
Ben Greearef96a8422011-11-18 11:32:00 -08003814 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3815 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3816 sizeof(ifmgd->ht_capa_mask));
3817
Johannes Berg77fdaa12009-07-07 03:45:17 +02003818 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003819 memcpy(assoc_data->ie, req->ie, req->ie_len);
3820 assoc_data->ie_len = req->ie_len;
3821 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003822
Johannes Berg66e67e42012-01-20 13:55:27 +01003823 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003824
Johannes Bergaf6b6372009-12-23 13:15:35 +01003825 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3826 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003827 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003828 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003829 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003830 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003831 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003832
Johannes Berg66e67e42012-01-20 13:55:27 +01003833 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003834 assoc_data->wmm = bss->wmm_used &&
3835 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003836 assoc_data->supp_rates = bss->supp_rates;
3837 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003838
3839 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3840 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3841 assoc_data->ap_ht_param =
3842 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3843 else
Johannes Berga8243b72012-11-22 14:32:09 +01003844 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg63f170e2009-12-23 13:15:33 +01003845
Kalle Valoab133152010-01-12 10:42:31 +02003846 if (bss->wmm_used && bss->uapsd_supported &&
3847 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003848 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003849 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3850 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003851 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003852 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3853 }
3854
Johannes Berg66e67e42012-01-20 13:55:27 +01003855 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3856 assoc_data->ssid_len = ssidie[1];
Johannes Berg63f170e2009-12-23 13:15:33 +01003857
Johannes Berg77fdaa12009-07-07 03:45:17 +02003858 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003859 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003860
3861 if (req->use_mfp) {
3862 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3863 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3864 } else {
3865 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3866 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3867 }
3868
3869 if (req->crypto.control_port)
3870 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3871 else
3872 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3873
Johannes Berga621fa42010-08-27 14:26:54 +03003874 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3875 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3876
Johannes Berg66e67e42012-01-20 13:55:27 +01003877 /* kick off associate process */
3878
3879 ifmgd->assoc_data = assoc_data;
3880
Johannes Berga1cf7752012-03-08 15:02:07 +01003881 err = ieee80211_prep_connection(sdata, req->bss, true);
3882 if (err)
3883 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003884
3885 if (!bss->dtim_period &&
3886 sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
3887 /*
3888 * Wait up to one beacon interval ...
3889 * should this be more if we miss one?
3890 */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003891 sdata_info(sdata, "waiting for beacon from %pM\n",
3892 ifmgd->bssid);
Johannes Berg3f9768a2012-03-28 21:02:46 +02003893 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg66e67e42012-01-20 13:55:27 +01003894 } else {
3895 assoc_data->have_beacon = true;
3896 assoc_data->sent_assoc = false;
3897 assoc_data->timeout = jiffies;
3898 }
3899 run_again(ifmgd, assoc_data->timeout);
3900
Paul Stewartfcff4f12012-02-23 17:59:53 -08003901 if (bss->corrupt_data) {
3902 char *corrupt_type = "data";
3903 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3904 if (bss->corrupt_data &
3905 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3906 corrupt_type = "beacon and probe response";
3907 else
3908 corrupt_type = "beacon";
3909 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
3910 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003911 sdata_info(sdata, "associating with AP with corrupt %s\n",
3912 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08003913 }
3914
Johannes Berg66e67e42012-01-20 13:55:27 +01003915 err = 0;
3916 goto out;
3917 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003918 memset(ifmgd->bssid, 0, ETH_ALEN);
3919 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003920 ifmgd->assoc_data = NULL;
3921 err_free:
3922 kfree(assoc_data);
3923 out:
3924 mutex_unlock(&ifmgd->mtx);
3925
3926 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003927}
3928
3929int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003930 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003931{
3932 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003933 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02003934 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01003935 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003936
Johannes Berg77fdaa12009-07-07 03:45:17 +02003937 mutex_lock(&ifmgd->mtx);
3938
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003939 sdata_info(sdata,
3940 "deauthenticating from %pM by local choice (reason=%d)\n",
3941 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003942
Johannes Berg86552012012-10-29 09:46:31 +01003943 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003944 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01003945 ieee80211_send_deauth_disassoc(sdata, req->bssid,
3946 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02003947 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01003948 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01003949 ieee80211_destroy_auth_data(sdata, false);
3950 mutex_unlock(&ifmgd->mtx);
3951
3952 sent_frame = tx;
3953 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03003954 }
3955
Johannes Berg86552012012-10-29 09:46:31 +01003956 if (ifmgd->associated &&
3957 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
3958 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3959 req->reason_code, tx, frame_buf);
3960 sent_frame = tx;
3961 }
Johannes Berg37ad3882012-02-24 13:50:54 +01003962 mutex_unlock(&ifmgd->mtx);
3963
Johannes Berg86552012012-10-29 09:46:31 +01003964 out:
Johannes Berg7da7cc12010-08-05 17:02:38 +02003965 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003966 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02003967 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01003968
Johannes Berg86552012012-10-29 09:46:31 +01003969 if (sent_frame)
3970 __cfg80211_send_deauth(sdata->dev, frame_buf,
3971 IEEE80211_DEAUTH_FRAME_LEN);
3972
Johannes Berg77fdaa12009-07-07 03:45:17 +02003973 return 0;
3974}
3975
3976int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01003977 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003978{
3979 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07003980 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003981 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02003982
Johannes Berg77fdaa12009-07-07 03:45:17 +02003983 mutex_lock(&ifmgd->mtx);
3984
Johannes Berg8d8b2612009-07-25 11:58:36 +02003985 /*
3986 * cfg80211 should catch this ... but it's racy since
3987 * we can receive a disassoc frame, process it, hand it
3988 * to cfg80211 while that's in a locked section already
3989 * trying to tell us that the user wants to disconnect.
3990 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003991 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003992 mutex_unlock(&ifmgd->mtx);
3993 return -ENOLINK;
3994 }
3995
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003996 sdata_info(sdata,
3997 "disassociating from %pM by local choice (reason=%d)\n",
3998 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02003999
Jouni Malinene69e95d2010-03-29 23:29:31 -07004000 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004001 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4002 req->reason_code, !req->local_state_change,
4003 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004004 mutex_unlock(&ifmgd->mtx);
4005
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004006 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4007 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004008
Johannes Berg7da7cc12010-08-05 17:02:38 +02004009 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004010 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004011 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004012
Johannes Berg77fdaa12009-07-07 03:45:17 +02004013 return 0;
4014}
Jouni Malinen026331c2010-02-15 12:53:10 +02004015
Eliad Pellerafa762f2012-04-23 14:45:15 +03004016void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004017{
4018 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4019
4020 mutex_lock(&ifmgd->mtx);
4021 if (ifmgd->assoc_data)
4022 ieee80211_destroy_assoc_data(sdata, false);
4023 if (ifmgd->auth_data)
4024 ieee80211_destroy_auth_data(sdata, false);
4025 del_timer_sync(&ifmgd->timer);
4026 mutex_unlock(&ifmgd->mtx);
4027}
4028
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004029void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4030 enum nl80211_cqm_rssi_threshold_event rssi_event,
4031 gfp_t gfp)
4032{
4033 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4034
Johannes Bergb5878a22010-04-07 16:48:40 +02004035 trace_api_cqm_rssi_notify(sdata, rssi_event);
4036
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004037 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4038}
4039EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);