blob: 03f278880dba6675890d77c5490c600c69c2ab9c [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
35#define IEEE80211_AUTH_MAX_TRIES 3
36#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
39#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static int max_nullfunc_tries = 2;
42module_param(max_nullfunc_tries, int, 0644);
43MODULE_PARM_DESC(max_nullfunc_tries,
44 "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46static int max_probe_tries = 5;
47module_param(max_probe_tries, int, 0644);
48MODULE_PARM_DESC(max_probe_tries,
49 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * Beacon loss timeout is calculated as N frames times the
53 * advertised beacon interval. This may need to be somewhat
54 * higher than what hardware might detect to account for
55 * delays in the host processing frames. But since we also
56 * probe on beacon miss before declaring the connection lost
57 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010059#define IEEE80211_BEACON_LOSS_COUNT 7
60
Johannes Bergb291ba12009-07-10 15:29:03 +020061/*
62 * Time the connection can be idle before we probe
63 * it to see if we can still talk to the AP.
64 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030065#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time we wait for a probe response after sending
68 * a probe request because of beacon loss or for
69 * checking the connection still works.
70 */
Ben Greear180205b2011-02-04 15:30:24 -080071static int probe_wait_ms = 500;
72module_param(probe_wait_ms, int, 0644);
73MODULE_PARM_DESC(probe_wait_ms,
74 "Maximum time(ms) to wait for probe response"
75 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jouni Malinen17e4ec12010-03-29 23:28:30 -070077/*
78 * Weight given to the latest Beacon frame when calculating average signal
79 * strength for Beacon frames received in the current BSS. This must be
80 * between 1 and 15.
81 */
82#define IEEE80211_SIGNAL_AVE_WEIGHT 3
83
Jouni Malinen391a2002010-08-27 22:22:00 +030084/*
85 * How many Beacon frames need to have been used in average signal strength
86 * before starting to indicate signal change events.
87 */
88#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
89
Johannes Berg5bb644a2009-05-17 11:40:42 +020090#define TMR_RUNNING_TIMER 0
91#define TMR_RUNNING_CHANSW 1
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200117};
118
Johannes Berg5484e232008-09-08 17:44:27 +0200119/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
121{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200122 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123}
124
Johannes Bergca386f32009-07-10 02:39:48 +0200125/*
126 * We can have multiple work items (and connection probing)
127 * scheduling this timer, but we need to take care to only
128 * reschedule it when it should fire _earlier_ than it was
129 * asked for before, or if it's not pending right now. This
130 * function ensures that. Note that it then is required to
131 * run this function for all timeouts after the first one
132 * has happened -- the work that runs from this timer will
133 * do that.
134 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100135static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200136{
137 ASSERT_MGD_MTX(ifmgd);
138
139 if (!timer_pending(&ifmgd->timer) ||
140 time_before(timeout, ifmgd->timer.expires))
141 mod_timer(&ifmgd->timer, timeout);
142}
143
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400144void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145{
Johannes Bergc1288b12012-01-19 09:29:57 +0100146 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200147 return;
148
Johannes Berg7eeff742012-07-18 10:27:27 +0200149 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
150 return;
151
Johannes Bergb291ba12009-07-10 15:29:03 +0200152 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100153 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200154}
155
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400156void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
157{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400158 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
159
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100160 if (unlikely(!sdata->u.mgd.associated))
161 return;
162
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400163 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
164 return;
165
166 mod_timer(&sdata->u.mgd.conn_mon_timer,
167 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400168
169 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400170}
171
Johannes Berg5484e232008-09-08 17:44:27 +0200172static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200173{
Johannes Berg5484e232008-09-08 17:44:27 +0200174 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200175}
176
Johannes Berg24398e32012-03-28 10:58:36 +0200177static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
Johannes Berg11289582013-02-07 17:36:12 +0100178 struct sta_info *sta,
Johannes Berg24398e32012-03-28 10:58:36 +0200179 struct ieee80211_ht_operation *ht_oper,
180 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200181{
182 struct ieee80211_local *local = sdata->local;
183 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200184 struct ieee80211_chanctx_conf *chanctx_conf;
185 struct ieee80211_channel *chan;
Johannes Bergd5522e02009-03-30 13:23:35 +0200186 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200187 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200188 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200189
Johannes Berg11289582013-02-07 17:36:12 +0100190 if (WARN_ON_ONCE(!sta))
191 return 0;
192
Johannes Berg55de9082012-07-26 17:24:39 +0200193 rcu_read_lock();
194 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
195 if (WARN_ON(!chanctx_conf)) {
196 rcu_read_unlock();
197 return 0;
198 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100199 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200200 rcu_read_unlock();
201 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200202
Johannes Berg4bf88532012-11-09 11:39:59 +0100203 switch (sdata->vif.bss_conf.chandef.width) {
204 case NL80211_CHAN_WIDTH_40:
205 if (sdata->vif.bss_conf.chandef.chan->center_freq >
206 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100207 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200208 disable_40 = true;
Johannes Berg4bf88532012-11-09 11:39:59 +0100209 if (sdata->vif.bss_conf.chandef.chan->center_freq <
210 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100211 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200212 disable_40 = true;
213 break;
214 default:
215 break;
216 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200217
Johannes Berg24398e32012-03-28 10:58:36 +0200218 /* This can change during the lifetime of the BSS */
219 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200220 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200221
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100222 if (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Johannes Berg64f68e52012-03-28 10:58:37 +0200223 disable_40 = true;
224
Johannes Berg11289582013-02-07 17:36:12 +0100225 if (disable_40 != (sta->sta.bandwidth < IEEE80211_STA_RX_BW_40)) {
Johannes Berg64f68e52012-03-28 10:58:37 +0200226 if (disable_40)
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100227 sta->sta.bandwidth = IEEE80211_STA_RX_BW_20;
Johannes Berg64f68e52012-03-28 10:58:37 +0200228 else
Johannes Berge1a0c6b2013-02-07 11:47:44 +0100229 sta->sta.bandwidth = ieee80211_sta_cur_vht_bw(sta);
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530230
Johannes Berg11289582013-02-07 17:36:12 +0100231 if (reconfig)
232 rate_control_rate_update(local, sband, sta,
233 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200234 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200235
Johannes Berg074d46d2012-03-15 19:45:16 +0100236 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200237
238 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200239 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200240 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200241 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200242 }
243
244 return changed;
245}
246
Johannes Berg9c6bd792008-09-11 00:01:52 +0200247/* frame sending functions */
248
Johannes Berg66e67e42012-01-20 13:55:27 +0100249static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
250 struct ieee80211_supported_band *sband,
251 u32 *rates)
252{
253 int i, j, count;
254 *rates = 0;
255 count = 0;
256 for (i = 0; i < supp_rates_len; i++) {
257 int rate = (supp_rates[i] & 0x7F) * 5;
258
259 for (j = 0; j < sband->n_bitrates; j++)
260 if (sband->bitrates[j].bitrate == rate) {
261 *rates |= BIT(j);
262 count++;
263 break;
264 }
265 }
266
267 return count;
268}
269
270static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200271 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100272 struct ieee80211_supported_band *sband,
273 struct ieee80211_channel *channel,
274 enum ieee80211_smps_mode smps)
275{
Johannes Berg66e67e42012-01-20 13:55:27 +0100276 u8 *pos;
277 u32 flags = channel->flags;
278 u16 cap;
279 struct ieee80211_sta_ht_cap ht_cap;
280
281 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
282
Johannes Berg66e67e42012-01-20 13:55:27 +0100283 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
284 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
285
Johannes Berg66e67e42012-01-20 13:55:27 +0100286 /* determine capability flags */
287 cap = ht_cap.cap;
288
Johannes Berg9dde6422012-05-16 23:43:19 +0200289 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100290 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
291 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
292 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
293 cap &= ~IEEE80211_HT_CAP_SGI_40;
294 }
295 break;
296 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
297 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
298 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
299 cap &= ~IEEE80211_HT_CAP_SGI_40;
300 }
301 break;
302 }
303
Johannes Berg24398e32012-03-28 10:58:36 +0200304 /*
305 * If 40 MHz was disabled associate as though we weren't
306 * capable of 40 MHz -- some broken APs will never fall
307 * back to trying to transmit in 20 MHz.
308 */
309 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
310 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
311 cap &= ~IEEE80211_HT_CAP_SGI_40;
312 }
313
Johannes Berg66e67e42012-01-20 13:55:27 +0100314 /* set SM PS mode properly */
315 cap &= ~IEEE80211_HT_CAP_SM_PS;
316 switch (smps) {
317 case IEEE80211_SMPS_AUTOMATIC:
318 case IEEE80211_SMPS_NUM_MODES:
319 WARN_ON(1);
320 case IEEE80211_SMPS_OFF:
321 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
322 IEEE80211_HT_CAP_SM_PS_SHIFT;
323 break;
324 case IEEE80211_SMPS_STATIC:
325 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
326 IEEE80211_HT_CAP_SM_PS_SHIFT;
327 break;
328 case IEEE80211_SMPS_DYNAMIC:
329 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
330 IEEE80211_HT_CAP_SM_PS_SHIFT;
331 break;
332 }
333
334 /* reserve and fill IE */
335 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
336 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
337}
338
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000339static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
340 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100341 struct ieee80211_supported_band *sband,
342 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000343{
344 u8 *pos;
345 u32 cap;
346 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100347 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000348
349 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
350
351 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
352
353 /* determine capability flags */
354 cap = vht_cap.cap;
355
Johannes Bergf2d9d272012-11-22 14:11:39 +0100356 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
357 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
358 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
359 }
360
361 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
362 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
363 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
364 }
365
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100366 /*
367 * Some APs apparently get confused if our capabilities are better
368 * than theirs, so restrict what we advertise in the assoc request.
369 */
370 if (!(ap_vht_cap->vht_cap_info &
371 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
372 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
373
374 if (!(ap_vht_cap->vht_cap_info &
375 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
376 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
377 IEEE80211_VHT_CAP_RXSTBC_3 |
378 IEEE80211_VHT_CAP_RXSTBC_4);
379
380 for (i = 0; i < 8; i++) {
381 int shift = i * 2;
382 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
383 u16 ap_mcs, our_mcs;
384
385 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
386 mask) >> shift;
387 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
388 mask) >> shift;
389
390 switch (ap_mcs) {
391 default:
392 if (our_mcs <= ap_mcs)
393 break;
394 /* fall through */
395 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
396 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
397 vht_cap.vht_mcs.rx_mcs_map |=
398 cpu_to_le16(ap_mcs << shift);
399 }
400 }
401
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000402 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000403 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000404 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
405}
406
Johannes Berg66e67e42012-01-20 13:55:27 +0100407static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
408{
409 struct ieee80211_local *local = sdata->local;
410 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
411 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
412 struct sk_buff *skb;
413 struct ieee80211_mgmt *mgmt;
414 u8 *pos, qos_info;
415 size_t offset = 0, noffset;
416 int i, count, rates_len, supp_rates_len;
417 u16 capab;
418 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200419 struct ieee80211_chanctx_conf *chanctx_conf;
420 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100421 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100422
423 lockdep_assert_held(&ifmgd->mtx);
424
Johannes Berg55de9082012-07-26 17:24:39 +0200425 rcu_read_lock();
426 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
427 if (WARN_ON(!chanctx_conf)) {
428 rcu_read_unlock();
429 return;
430 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100431 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200432 rcu_read_unlock();
433 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100434
435 if (assoc_data->supp_rates_len) {
436 /*
437 * Get all rates supported by the device and the AP as
438 * some APs don't like getting a superset of their rates
439 * in the association request (e.g. D-Link DAP 1353 in
440 * b-only mode)...
441 */
442 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
443 assoc_data->supp_rates_len,
444 sband, &rates);
445 } else {
446 /*
447 * In case AP not provide any supported rates information
448 * before association, we send information element(s) with
449 * all rates that we support.
450 */
451 rates = ~0;
452 rates_len = sband->n_bitrates;
453 }
454
455 skb = alloc_skb(local->hw.extra_tx_headroom +
456 sizeof(*mgmt) + /* bit too much but doesn't matter */
457 2 + assoc_data->ssid_len + /* SSID */
458 4 + rates_len + /* (extended) rates */
459 4 + /* power capability */
460 2 + 2 * sband->n_channels + /* supported channels */
461 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000462 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100463 assoc_data->ie_len + /* extra IEs */
464 9, /* WMM */
465 GFP_KERNEL);
466 if (!skb)
467 return;
468
469 skb_reserve(skb, local->hw.extra_tx_headroom);
470
471 capab = WLAN_CAPABILITY_ESS;
472
473 if (sband->band == IEEE80211_BAND_2GHZ) {
474 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
475 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
476 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
477 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
478 }
479
480 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
481 capab |= WLAN_CAPABILITY_PRIVACY;
482
483 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
484 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
485 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
486
487 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
488 memset(mgmt, 0, 24);
489 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
490 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
491 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
492
493 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
494 skb_put(skb, 10);
495 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
496 IEEE80211_STYPE_REASSOC_REQ);
497 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
498 mgmt->u.reassoc_req.listen_interval =
499 cpu_to_le16(local->hw.conf.listen_interval);
500 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
501 ETH_ALEN);
502 } else {
503 skb_put(skb, 4);
504 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
505 IEEE80211_STYPE_ASSOC_REQ);
506 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
507 mgmt->u.assoc_req.listen_interval =
508 cpu_to_le16(local->hw.conf.listen_interval);
509 }
510
511 /* SSID */
512 pos = skb_put(skb, 2 + assoc_data->ssid_len);
513 *pos++ = WLAN_EID_SSID;
514 *pos++ = assoc_data->ssid_len;
515 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
516
517 /* add all rates which were marked to be used above */
518 supp_rates_len = rates_len;
519 if (supp_rates_len > 8)
520 supp_rates_len = 8;
521
522 pos = skb_put(skb, supp_rates_len + 2);
523 *pos++ = WLAN_EID_SUPP_RATES;
524 *pos++ = supp_rates_len;
525
526 count = 0;
527 for (i = 0; i < sband->n_bitrates; i++) {
528 if (BIT(i) & rates) {
529 int rate = sband->bitrates[i].bitrate;
530 *pos++ = (u8) (rate / 5);
531 if (++count == 8)
532 break;
533 }
534 }
535
536 if (rates_len > count) {
537 pos = skb_put(skb, rates_len - count + 2);
538 *pos++ = WLAN_EID_EXT_SUPP_RATES;
539 *pos++ = rates_len - count;
540
541 for (i++; i < sband->n_bitrates; i++) {
542 if (BIT(i) & rates) {
543 int rate = sband->bitrates[i].bitrate;
544 *pos++ = (u8) (rate / 5);
545 }
546 }
547 }
548
549 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
550 /* 1. power capabilities */
551 pos = skb_put(skb, 4);
552 *pos++ = WLAN_EID_PWR_CAPABILITY;
553 *pos++ = 2;
554 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200555 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100556
557 /* 2. supported channels */
558 /* TODO: get this in reg domain format */
559 pos = skb_put(skb, 2 * sband->n_channels + 2);
560 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
561 *pos++ = 2 * sband->n_channels;
562 for (i = 0; i < sband->n_channels; i++) {
563 *pos++ = ieee80211_frequency_to_channel(
564 sband->channels[i].center_freq);
565 *pos++ = 1; /* one channel in the subband*/
566 }
567 }
568
569 /* if present, add any custom IEs that go before HT */
570 if (assoc_data->ie_len && assoc_data->ie) {
571 static const u8 before_ht[] = {
572 WLAN_EID_SSID,
573 WLAN_EID_SUPP_RATES,
574 WLAN_EID_EXT_SUPP_RATES,
575 WLAN_EID_PWR_CAPABILITY,
576 WLAN_EID_SUPPORTED_CHANNELS,
577 WLAN_EID_RSN,
578 WLAN_EID_QOS_CAPA,
579 WLAN_EID_RRM_ENABLED_CAPABILITIES,
580 WLAN_EID_MOBILITY_DOMAIN,
581 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
582 };
583 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
584 before_ht, ARRAY_SIZE(before_ht),
585 offset);
586 pos = skb_put(skb, noffset - offset);
587 memcpy(pos, assoc_data->ie + offset, noffset - offset);
588 offset = noffset;
589 }
590
Johannes Bergf2d9d272012-11-22 14:11:39 +0100591 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
592 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
593 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
594
Johannes Berga8243b72012-11-22 14:32:09 +0100595 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200596 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200597 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100598
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100600 ieee80211_add_vht_ie(sdata, skb, sband,
601 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000602
Johannes Berg66e67e42012-01-20 13:55:27 +0100603 /* if present, add any custom non-vendor IEs that go after HT */
604 if (assoc_data->ie_len && assoc_data->ie) {
605 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
606 assoc_data->ie_len,
607 offset);
608 pos = skb_put(skb, noffset - offset);
609 memcpy(pos, assoc_data->ie + offset, noffset - offset);
610 offset = noffset;
611 }
612
Johannes Berg76f03032012-03-08 15:02:05 +0100613 if (assoc_data->wmm) {
614 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200615 qos_info = ifmgd->uapsd_queues;
616 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100617 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
618 } else {
619 qos_info = 0;
620 }
621
622 pos = skb_put(skb, 9);
623 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
624 *pos++ = 7; /* len */
625 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
626 *pos++ = 0x50;
627 *pos++ = 0xf2;
628 *pos++ = 2; /* WME */
629 *pos++ = 0; /* WME info */
630 *pos++ = 1; /* WME ver */
631 *pos++ = qos_info;
632 }
633
634 /* add any remaining custom (i.e. vendor specific here) IEs */
635 if (assoc_data->ie_len && assoc_data->ie) {
636 noffset = assoc_data->ie_len;
637 pos = skb_put(skb, noffset - offset);
638 memcpy(pos, assoc_data->ie + offset, noffset - offset);
639 }
640
Johannes Berga1845fc2012-06-27 13:18:36 +0200641 drv_mgd_prepare_tx(local, sdata);
642
Johannes Berg66e67e42012-01-20 13:55:27 +0100643 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100644 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
645 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
646 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100647 ieee80211_tx_skb(sdata, skb);
648}
649
Kalle Valo572e0012009-02-10 17:09:31 +0200650void ieee80211_send_pspoll(struct ieee80211_local *local,
651 struct ieee80211_sub_if_data *sdata)
652{
Kalle Valo572e0012009-02-10 17:09:31 +0200653 struct ieee80211_pspoll *pspoll;
654 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200655
Kalle Valod8cd1892010-01-05 20:16:26 +0200656 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
657 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200658 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200659
Kalle Valod8cd1892010-01-05 20:16:26 +0200660 pspoll = (struct ieee80211_pspoll *) skb->data;
661 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200662
Johannes Berg62ae67b2009-11-18 18:42:05 +0100663 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
664 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200665}
666
Johannes Berg965beda2009-04-16 13:17:24 +0200667void ieee80211_send_nullfunc(struct ieee80211_local *local,
668 struct ieee80211_sub_if_data *sdata,
669 int powersave)
670{
671 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200672 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530673 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200674
Kalle Valod8cd1892010-01-05 20:16:26 +0200675 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
676 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200677 return;
678
Kalle Valod8cd1892010-01-05 20:16:26 +0200679 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200680 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200681 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200682
Seth Forshee6c17b772013-02-11 11:21:07 -0600683 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
684 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530685 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
686 IEEE80211_STA_CONNECTION_POLL))
687 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
688
Johannes Berg62ae67b2009-11-18 18:42:05 +0100689 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200690}
691
Felix Fietkaud5242152010-01-08 18:06:26 +0100692static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
693 struct ieee80211_sub_if_data *sdata)
694{
695 struct sk_buff *skb;
696 struct ieee80211_hdr *nullfunc;
697 __le16 fc;
698
699 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
700 return;
701
702 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700703 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100704 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700705
Felix Fietkaud5242152010-01-08 18:06:26 +0100706 skb_reserve(skb, local->hw.extra_tx_headroom);
707
708 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
709 memset(nullfunc, 0, 30);
710 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
711 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
712 nullfunc->frame_control = fc;
713 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
714 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
715 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
716 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
717
718 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
719 ieee80211_tx_skb(sdata, skb);
720}
721
Johannes Bergcc32abd2009-05-15 11:52:31 +0200722/* spectrum management related things */
723static void ieee80211_chswitch_work(struct work_struct *work)
724{
725 struct ieee80211_sub_if_data *sdata =
726 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200727 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
728
Johannes Berg9607e6b2009-12-23 13:15:31 +0100729 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200730 return;
731
Johannes Berg77fdaa12009-07-07 03:45:17 +0200732 mutex_lock(&ifmgd->mtx);
733 if (!ifmgd->associated)
734 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200735
Johannes Berg55de9082012-07-26 17:24:39 +0200736 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200737 if (!sdata->local->ops->channel_switch) {
738 /* call "hw_config" only if doing sw channel switch */
739 ieee80211_hw_config(sdata->local,
740 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300741 } else {
742 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200743 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200744 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200745
Johannes Berg77fdaa12009-07-07 03:45:17 +0200746 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200747 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200748
Johannes Berg57eebdf2012-08-01 15:50:46 +0200749 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200750 ieee80211_wake_queues_by_reason(&sdata->local->hw,
751 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200752 out:
753 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
754 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200755}
756
Johannes Berg5ce6e432010-05-11 16:20:57 +0200757void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
758{
Johannes Berg55de9082012-07-26 17:24:39 +0200759 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
760 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200761
762 trace_api_chswitch_done(sdata, success);
763 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200764 sdata_info(sdata,
765 "driver channel switch failed, disconnecting\n");
766 ieee80211_queue_work(&sdata->local->hw,
767 &ifmgd->csa_connection_drop_work);
768 } else {
769 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200770 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200771}
772EXPORT_SYMBOL(ieee80211_chswitch_done);
773
Johannes Bergcc32abd2009-05-15 11:52:31 +0200774static void ieee80211_chswitch_timer(unsigned long data)
775{
776 struct ieee80211_sub_if_data *sdata =
777 (struct ieee80211_sub_if_data *) data;
778 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
779
Johannes Berg5bb644a2009-05-17 11:40:42 +0200780 if (sdata->local->quiescing) {
781 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
782 return;
783 }
784
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400785 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200786}
787
788void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
789 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200790 struct ieee80211_bss *bss,
791 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200792{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100793 struct cfg80211_bss *cbss =
794 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200795 struct ieee80211_channel *new_ch;
796 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900797 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
798 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200799 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200800
Johannes Berg77fdaa12009-07-07 03:45:17 +0200801 ASSERT_MGD_MTX(ifmgd);
802
803 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200804 return;
805
Helmut Schaafbe9c422009-07-23 12:14:04 +0200806 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200807 return;
808
809 /* Disregard subsequent beacons if we are already running a timer
810 processing a CSA */
811
812 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
813 return;
814
815 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +0200816 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
817 sdata_info(sdata,
818 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
819 ifmgd->associated->bssid, new_freq);
820 ieee80211_queue_work(&sdata->local->hw,
821 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200822 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200823 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200824
Johannes Berg57eebdf2012-08-01 15:50:46 +0200825 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
826
Johannes Berg55de9082012-07-26 17:24:39 +0200827 if (sdata->local->use_chanctx) {
828 sdata_info(sdata,
829 "not handling channel switch with channel contexts\n");
830 ieee80211_queue_work(&sdata->local->hw,
831 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +0100832 return;
Johannes Berg55de9082012-07-26 17:24:39 +0200833 }
834
835 mutex_lock(&sdata->local->chanctx_mtx);
836 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
837 mutex_unlock(&sdata->local->chanctx_mtx);
838 return;
839 }
840 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
841 struct ieee80211_chanctx, conf);
842 if (chanctx->refcount > 1) {
843 sdata_info(sdata,
844 "channel switch with multiple interfaces on the same channel, disconnecting\n");
845 ieee80211_queue_work(&sdata->local->hw,
846 &ifmgd->csa_connection_drop_work);
847 mutex_unlock(&sdata->local->chanctx_mtx);
848 return;
849 }
850 mutex_unlock(&sdata->local->chanctx_mtx);
851
852 sdata->local->csa_channel = new_ch;
853
Johannes Berg57eebdf2012-08-01 15:50:46 +0200854 if (sw_elem->mode)
855 ieee80211_stop_queues_by_reason(&sdata->local->hw,
856 IEEE80211_QUEUE_STOP_REASON_CSA);
857
Johannes Berg5ce6e432010-05-11 16:20:57 +0200858 if (sdata->local->ops->channel_switch) {
859 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200860 struct ieee80211_channel_switch ch_switch = {
861 .timestamp = timestamp,
862 .block_tx = sw_elem->mode,
863 .channel = new_ch,
864 .count = sw_elem->count,
865 };
866
Johannes Berg5ce6e432010-05-11 16:20:57 +0200867 drv_channel_switch(sdata->local, &ch_switch);
868 return;
869 }
870
871 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200872 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400873 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200874 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200875 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200876 TU_TO_EXP_TIME(sw_elem->count *
877 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200878}
879
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200880static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
881 struct ieee80211_channel *channel,
882 const u8 *country_ie, u8 country_ie_len,
883 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200884{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200885 struct ieee80211_country_ie_triplet *triplet;
886 int chan = ieee80211_frequency_to_channel(channel->center_freq);
887 int i, chan_pwr, chan_increment, new_ap_level;
888 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200889
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200890 /* Invalid IE */
891 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200892 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200893
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200894 triplet = (void *)(country_ie + 3);
895 country_ie_len -= 3;
896
897 switch (channel->band) {
898 default:
899 WARN_ON_ONCE(1);
900 /* fall through */
901 case IEEE80211_BAND_2GHZ:
902 case IEEE80211_BAND_60GHZ:
903 chan_increment = 1;
904 break;
905 case IEEE80211_BAND_5GHZ:
906 chan_increment = 4;
907 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200908 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200909
910 /* find channel */
911 while (country_ie_len >= 3) {
912 u8 first_channel = triplet->chans.first_channel;
913
914 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
915 goto next;
916
917 for (i = 0; i < triplet->chans.num_channels; i++) {
918 if (first_channel + i * chan_increment == chan) {
919 have_chan_pwr = true;
920 chan_pwr = triplet->chans.max_power;
921 break;
922 }
923 }
924 if (have_chan_pwr)
925 break;
926
927 next:
928 triplet++;
929 country_ie_len -= 3;
930 }
931
932 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200933 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200934
935 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
936
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200937 if (sdata->ap_power_level == new_ap_level)
938 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200939
940 sdata_info(sdata,
941 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
942 new_ap_level, chan_pwr, *pwr_constr_elem,
943 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200944 sdata->ap_power_level = new_ap_level;
945 if (__ieee80211_recalc_txpower(sdata))
946 return BSS_CHANGED_TXPOWER;
947 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200948}
949
Johannes Berg965beda2009-04-16 13:17:24 +0200950/* powersave */
951static void ieee80211_enable_ps(struct ieee80211_local *local,
952 struct ieee80211_sub_if_data *sdata)
953{
954 struct ieee80211_conf *conf = &local->hw.conf;
955
Johannes Bergd5edaed2009-04-22 23:02:51 +0200956 /*
957 * If we are scanning right now then the parameters will
958 * take effect when scan finishes.
959 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200960 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200961 return;
962
Johannes Berg965beda2009-04-16 13:17:24 +0200963 if (conf->dynamic_ps_timeout > 0 &&
964 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
965 mod_timer(&local->dynamic_ps_timer, jiffies +
966 msecs_to_jiffies(conf->dynamic_ps_timeout));
967 } else {
968 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
969 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +0530970
Juuso Oikarinen2a130522010-03-09 14:25:02 +0200971 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
972 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
973 return;
974
975 conf->flags |= IEEE80211_CONF_PS;
976 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +0200977 }
978}
979
980static void ieee80211_change_ps(struct ieee80211_local *local)
981{
982 struct ieee80211_conf *conf = &local->hw.conf;
983
984 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +0200985 ieee80211_enable_ps(local, local->ps_sdata);
986 } else if (conf->flags & IEEE80211_CONF_PS) {
987 conf->flags &= ~IEEE80211_CONF_PS;
988 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
989 del_timer_sync(&local->dynamic_ps_timer);
990 cancel_work_sync(&local->dynamic_ps_enable_work);
991 }
992}
993
Jason Young808118c2011-03-10 16:43:19 -0800994static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
995{
996 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
997 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +0200998 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -0800999
1000 if (!mgd->powersave)
1001 return false;
1002
Johannes Berg05cb9102011-10-28 11:59:47 +02001003 if (mgd->broken_ap)
1004 return false;
1005
Jason Young808118c2011-03-10 16:43:19 -08001006 if (!mgd->associated)
1007 return false;
1008
Jason Young808118c2011-03-10 16:43:19 -08001009 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1010 IEEE80211_STA_CONNECTION_POLL))
1011 return false;
1012
1013 rcu_read_lock();
1014 sta = sta_info_get(sdata, mgd->bssid);
1015 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001016 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001017 rcu_read_unlock();
1018
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001019 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001020}
1021
Johannes Berg965beda2009-04-16 13:17:24 +02001022/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001023void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001024{
1025 struct ieee80211_sub_if_data *sdata, *found = NULL;
1026 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001027 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001028
1029 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1030 local->ps_sdata = NULL;
1031 return;
1032 }
1033
1034 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001035 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001036 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301037 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1038 /* If an AP vif is found, then disable PS
1039 * by setting the count to zero thereby setting
1040 * ps_sdata to NULL.
1041 */
1042 count = 0;
1043 break;
1044 }
Johannes Berg965beda2009-04-16 13:17:24 +02001045 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1046 continue;
1047 found = sdata;
1048 count++;
1049 }
1050
Jason Young808118c2011-03-10 16:43:19 -08001051 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001052 s32 beaconint_us;
1053
1054 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001055 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001056
1057 beaconint_us = ieee80211_tu_to_usec(
1058 found->vif.bss_conf.beacon_int);
1059
Juuso Oikarinenff616382010-06-09 09:51:52 +03001060 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001061 if (timeout < 0) {
1062 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001063 * Go to full PSM if the user configures a very low
1064 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001065 * The 2000 second value is there for compatibility
1066 * until the PM_QOS_NETWORK_LATENCY is configured
1067 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001068 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001069 if (latency > (1900 * USEC_PER_MSEC) &&
1070 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001071 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001072 else
1073 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001074 }
Johannes Berg09b85562013-02-06 23:07:41 +01001075 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001076
Johannes Berg04fe2032009-04-22 18:44:37 +02001077 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001078 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001079 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001080 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001081 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001082
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
Johannes Berg09b85562013-02-06 23:07:41 +01001145 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001146 /* don't enter PS if TX frames are pending */
1147 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301148 mod_timer(&local->dynamic_ps_timer, jiffies +
1149 msecs_to_jiffies(
1150 local->hw.conf.dynamic_ps_timeout));
1151 return;
1152 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001153
1154 /*
1155 * transmission can be stopped by others which leads to
1156 * dynamic_ps_timer expiry. Postpone the ps timer if it
1157 * is not the actual idle state.
1158 */
1159 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1160 for (q = 0; q < local->hw.queues; q++) {
1161 if (local->queue_stop_reasons[q]) {
1162 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1163 flags);
1164 mod_timer(&local->dynamic_ps_timer, jiffies +
1165 msecs_to_jiffies(
1166 local->hw.conf.dynamic_ps_timeout));
1167 return;
1168 }
1169 }
1170 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301171 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301172
Vivek Natarajan375177b2010-02-09 14:50:28 +05301173 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301174 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301175 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001176
Vivek Natarajane8306f92011-04-06 11:41:10 +05301177 if (drv_tx_frames_pending(local))
1178 mod_timer(&local->dynamic_ps_timer, jiffies +
1179 msecs_to_jiffies(
1180 local->hw.conf.dynamic_ps_timeout));
1181 else {
1182 ieee80211_send_nullfunc(local, sdata, 1);
1183 /* Flush to get the tx status of nullfunc frame */
1184 drv_flush(local, false);
1185 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301186 }
1187
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001188 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1189 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301190 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1191 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1192 local->hw.conf.flags |= IEEE80211_CONF_PS;
1193 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1194 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301195
Arik Nemtsov77b70232011-06-26 12:06:54 +03001196 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1197 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001198}
1199
1200void ieee80211_dynamic_ps_timer(unsigned long data)
1201{
1202 struct ieee80211_local *local = (void *) data;
1203
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001204 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001205 return;
1206
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001207 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001208}
1209
Simon Wunderlich164eb022013-02-08 18:16:20 +01001210void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1211{
1212 struct delayed_work *delayed_work =
1213 container_of(work, struct delayed_work, work);
1214 struct ieee80211_sub_if_data *sdata =
1215 container_of(delayed_work, struct ieee80211_sub_if_data,
1216 dfs_cac_timer_work);
1217
1218 ieee80211_vif_release_channel(sdata);
1219
1220 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1221}
1222
Johannes Berg60f8b392008-09-08 17:44:22 +02001223/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001224static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001225 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001226 u8 *wmm_param, size_t wmm_param_len)
1227{
Jiri Bencf0706e82007-05-05 11:45:53 -07001228 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001229 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001230 size_t left;
1231 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001232 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001233
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001234 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001235 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001236
Johannes Berg32c50572012-03-28 11:04:29 +02001237 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001238 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001239
1240 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001241 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001242
Jiri Bencf0706e82007-05-05 11:45:53 -07001243 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001244 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001245
1246 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001247 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001248
Jiri Bencf0706e82007-05-05 11:45:53 -07001249 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001250 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001251 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001252 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001253
1254 pos = wmm_param + 8;
1255 left = wmm_param_len - 8;
1256
1257 memset(&params, 0, sizeof(params));
1258
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001259 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001260 for (; left >= 4; left -= 4, pos += 4) {
1261 int aci = (pos[0] >> 5) & 0x03;
1262 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001263 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001264 int queue;
1265
1266 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001267 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001268 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001269 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001270 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001271 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1272 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001273 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001274 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001275 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001276 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001277 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001278 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1279 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001280 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001281 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001282 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001283 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001284 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001285 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1286 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001287 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001288 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001289 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001290 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001291 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001292 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001293 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1294 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001295 break;
1296 }
1297
1298 params.aifs = pos[0] & 0x0f;
1299 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1300 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001301 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001302 params.uapsd = uapsd;
1303
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001304 mlme_dbg(sdata,
1305 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1306 queue, aci, acm,
1307 params.aifs, params.cw_min, params.cw_max,
1308 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001309 sdata->tx_conf[queue] = params;
1310 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001311 sdata_err(sdata,
1312 "failed to set TX queue parameters for queue %d\n",
1313 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001314 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001315
1316 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001317 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001318 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001319}
1320
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001321static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1322{
1323 lockdep_assert_held(&sdata->local->mtx);
1324
1325 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1326 IEEE80211_STA_BEACON_POLL);
1327 ieee80211_run_deferred_scan(sdata->local);
1328}
1329
1330static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1331{
1332 mutex_lock(&sdata->local->mtx);
1333 __ieee80211_stop_poll(sdata);
1334 mutex_unlock(&sdata->local->mtx);
1335}
1336
Johannes Berg7a5158e2008-10-08 10:59:33 +02001337static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1338 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001339{
Johannes Bergbda39332008-10-11 01:51:51 +02001340 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001341 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001342 bool use_protection;
1343 bool use_short_preamble;
1344 bool use_short_slot;
1345
1346 if (erp_valid) {
1347 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1348 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1349 } else {
1350 use_protection = false;
1351 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1352 }
1353
1354 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001355 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001356 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001357
Johannes Berg471b3ef2007-12-28 14:32:58 +01001358 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001359 bss_conf->use_cts_prot = use_protection;
1360 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001361 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001362
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001363 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001364 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001365 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001366 }
Daniel Draked9430a32007-07-27 15:43:24 +02001367
Johannes Berg7a5158e2008-10-08 10:59:33 +02001368 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001369 bss_conf->use_short_slot = use_short_slot;
1370 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001371 }
1372
1373 return changed;
1374}
1375
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001376static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001377 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001378 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001379{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001380 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001381 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001382 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001383
Johannes Bergae5eb022008-10-14 16:58:37 +02001384 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001385 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001386 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001387
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001388 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1389 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1390
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001391 sdata->u.mgd.associated = cbss;
1392 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001393
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001394 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1395
Johannes Berg488dd7b2012-10-29 20:08:01 +01001396 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001397 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001398
Johannes Berg9caf0362012-11-29 01:25:20 +01001399 rcu_read_lock();
1400 ies = rcu_dereference(cbss->ies);
1401 if (ies) {
1402 u8 noa[2];
1403 int ret;
1404
1405 ret = cfg80211_get_p2p_attr(
1406 ies->data, ies->len,
1407 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1408 noa, sizeof(noa));
1409 if (ret >= 2) {
1410 bss_conf->p2p_oppps = noa[1] & 0x80;
1411 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1412 bss_info_changed |= BSS_CHANGED_P2P_PS;
1413 sdata->u.mgd.p2p_noa_index = noa[0];
1414 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001415 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001416 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001417 }
1418
Johannes Bergb291ba12009-07-10 15:29:03 +02001419 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001420 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001421
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001422 ieee80211_led_assoc(local, 1);
1423
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001424 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001425 /*
1426 * If the AP is buggy we may get here with no DTIM period
1427 * known, so assume it's 1 which is the only safe assumption
1428 * in that case, although if the TIM IE is broken powersave
1429 * probably just won't work at all.
1430 */
1431 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001432 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001433 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001434 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001435 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001436
Juuso Oikarinen68542962010-06-09 13:43:26 +03001437 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001438
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001439 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001440 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001441 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001442 bss_info_changed |= BSS_CHANGED_CQM;
1443
Juuso Oikarinen68542962010-06-09 13:43:26 +03001444 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001445 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001446 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001447
Johannes Bergae5eb022008-10-14 16:58:37 +02001448 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001449
Johannes Berg056508d2009-07-30 21:43:55 +02001450 mutex_lock(&local->iflist_mtx);
1451 ieee80211_recalc_ps(local, -1);
1452 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001453
Johannes Berg04ecd252012-09-11 14:34:12 +02001454 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001455 ieee80211_recalc_ps_vif(sdata);
1456
John W. Linville8a5b33f2010-01-06 15:39:39 -05001457 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001458 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001459}
1460
Jouni Malinene69e95d2010-03-29 23:29:31 -07001461static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001462 u16 stype, u16 reason, bool tx,
1463 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001464{
Johannes Berg46900292009-02-15 12:44:28 +01001465 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001466 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001467 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001468
Johannes Berg77fdaa12009-07-07 03:45:17 +02001469 ASSERT_MGD_MTX(ifmgd);
1470
Johannes Berg37ad3882012-02-24 13:50:54 +01001471 if (WARN_ON_ONCE(tx && !frame_buf))
1472 return;
1473
Johannes Bergb291ba12009-07-10 15:29:03 +02001474 if (WARN_ON(!ifmgd->associated))
1475 return;
1476
David Spinadel79543d82012-06-12 09:59:45 +03001477 ieee80211_stop_poll(sdata);
1478
Johannes Berg77fdaa12009-07-07 03:45:17 +02001479 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001480
1481 /*
1482 * we need to commit the associated = NULL change because the
1483 * scan code uses that to determine whether this iface should
1484 * go to/wake up from powersave or not -- and could otherwise
1485 * wake the queues erroneously.
1486 */
1487 smp_mb();
1488
1489 /*
1490 * Thus, we can only afterwards stop the queues -- to account
1491 * for the case where another CPU is finishing a scan at this
1492 * time -- we don't want the scan code to enable queues.
1493 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001494
John W. Linville8a5b33f2010-01-06 15:39:39 -05001495 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001496 netif_carrier_off(sdata->dev);
1497
Eliad Peller88bc40e2012-07-12 17:35:33 +03001498 /*
1499 * if we want to get out of ps before disassoc (why?) we have
1500 * to do it before sending disassoc, as otherwise the null-packet
1501 * won't be valid.
1502 */
1503 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1504 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1505 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1506 }
1507 local->ps_sdata = NULL;
1508
Eliad Pellerab095872012-07-27 12:33:22 +03001509 /* disable per-vif ps */
1510 ieee80211_recalc_ps_vif(sdata);
1511
Eliad Pellerf8239812012-06-27 14:18:22 +03001512 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1513 if (tx)
1514 drv_flush(local, false);
1515
Johannes Berg37ad3882012-02-24 13:50:54 +01001516 /* deauthenticate/disassociate now */
1517 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001518 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1519 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001520
1521 /* flush out frame */
1522 if (tx)
1523 drv_flush(local, false);
1524
Eliad Peller88a9e312012-06-01 11:14:03 +03001525 /* clear bssid only after building the needed mgmt frames */
1526 memset(ifmgd->bssid, 0, ETH_ALEN);
1527
Johannes Berg37ad3882012-02-24 13:50:54 +01001528 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001529 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001530
1531 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001532 changed |= ieee80211_reset_erp_info(sdata);
1533
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001534 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001535 changed |= BSS_CHANGED_ASSOC;
1536 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001537
Johannes Berg488dd7b2012-10-29 20:08:01 +01001538 sdata->vif.bss_conf.p2p_ctwindow = 0;
1539 sdata->vif.bss_conf.p2p_oppps = false;
1540
Johannes Berg413ad502009-05-08 21:21:06 +02001541 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001542 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1543 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001544
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001545 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301546
Kalle Valo520eb822008-12-18 23:35:27 +02001547 del_timer_sync(&local->dynamic_ps_timer);
1548 cancel_work_sync(&local->dynamic_ps_enable_work);
1549
Juuso Oikarinen68542962010-06-09 13:43:26 +03001550 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001551 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001552 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001553
Johannes Berg3abead52012-03-02 15:56:59 +01001554 sdata->vif.bss_conf.qos = false;
1555 changed |= BSS_CHANGED_QOS;
1556
Johannes Berg0aaffa92010-05-05 15:28:27 +02001557 /* The BSSID (not really interesting) and HT changed */
1558 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001559 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001560
Johannes Berg3abead52012-03-02 15:56:59 +01001561 /* disassociated - set to defaults now */
1562 ieee80211_set_wmm_default(sdata, false);
1563
Johannes Bergb9dcf712010-08-27 12:35:54 +02001564 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1565 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1566 del_timer_sync(&sdata->u.mgd.timer);
1567 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001568
1569 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001570
Johannes Berg826262c2012-12-10 16:38:14 +02001571 sdata->vif.bss_conf.dtim_period = 0;
1572
Johannes Berg028e8da02012-11-26 11:57:41 +01001573 ifmgd->flags = 0;
1574 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001575}
Jiri Bencf0706e82007-05-05 11:45:53 -07001576
Kalle Valo3cf335d2009-03-22 21:57:06 +02001577void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1578 struct ieee80211_hdr *hdr)
1579{
1580 /*
1581 * We can postpone the mgd.timer whenever receiving unicast frames
1582 * from AP because we know that the connection is working both ways
1583 * at that time. But multicast frames (and hence also beacons) must
1584 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001585 * data idle periods for sending the periodic probe request to the
1586 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001587 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001588 if (is_multicast_ether_addr(hdr->addr1))
1589 return;
1590
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001591 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001592}
Jiri Bencf0706e82007-05-05 11:45:53 -07001593
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001594static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1595{
1596 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001597 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001598
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001599 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001600 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001601 IEEE80211_STA_CONNECTION_POLL))) {
1602 mutex_unlock(&local->mtx);
1603 return;
1604 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001605
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001606 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001607
1608 mutex_lock(&local->iflist_mtx);
1609 ieee80211_recalc_ps(local, -1);
1610 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001611
1612 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001613 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001614
1615 /*
1616 * We've received a probe response, but are not sure whether
1617 * we have or will be receiving any beacons or data, so let's
1618 * schedule the timers again, just in case.
1619 */
1620 ieee80211_sta_reset_beacon_monitor(sdata);
1621
1622 mod_timer(&ifmgd->conn_mon_timer,
1623 round_jiffies_up(jiffies +
1624 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001625out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001626 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001627}
1628
1629void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001630 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001631{
Felix Fietkau75706d02010-12-02 21:01:07 +01001632 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001633 return;
1634
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001635 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1636 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001637 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001638 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001639 else
1640 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001641 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001642 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001643 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001644
1645 if (ack)
1646 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001647}
1648
Maxim Levitskya43abf22009-07-31 18:54:12 +03001649static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1650{
1651 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1652 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001653 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001654 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001655
1656 /*
1657 * Try sending broadcast probe requests for the last three
1658 * probe requests after the first ones failed since some
1659 * buggy APs only support broadcast probe requests.
1660 */
1661 if (ifmgd->probe_send_count >= unicast_limit)
1662 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001663
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001664 /*
1665 * When the hardware reports an accurate Tx ACK status, it's
1666 * better to send a nullfunc frame instead of a probe request,
1667 * as it will kick us off the AP quickly if we aren't associated
1668 * anymore. The timeout will be reset if the frame is ACKed by
1669 * the AP.
1670 */
Soumik Das992e68b2012-05-20 15:31:13 +05301671 ifmgd->probe_send_count++;
1672
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001673 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1674 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001675 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001676 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001677 int ssid_len;
1678
Johannes Berg9caf0362012-11-29 01:25:20 +01001679 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001680 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001681 if (WARN_ON_ONCE(ssid == NULL))
1682 ssid_len = 0;
1683 else
1684 ssid_len = ssid[1];
1685
1686 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001687 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001688 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001689 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001690 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001691
Ben Greear180205b2011-02-04 15:30:24 -08001692 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001693 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301694 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1695 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001696}
1697
Johannes Bergb291ba12009-07-10 15:29:03 +02001698static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1699 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001700{
Kalle Valo04de8382009-03-22 21:57:35 +02001701 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001702 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001703
Johannes Berg9607e6b2009-12-23 13:15:31 +01001704 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001705 return;
1706
Johannes Berg77fdaa12009-07-07 03:45:17 +02001707 mutex_lock(&ifmgd->mtx);
1708
1709 if (!ifmgd->associated)
1710 goto out;
1711
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001712 mutex_lock(&sdata->local->mtx);
1713
1714 if (sdata->local->tmp_channel || sdata->local->scanning) {
1715 mutex_unlock(&sdata->local->mtx);
1716 goto out;
1717 }
1718
Joe Perchese87cc472012-05-13 21:56:26 +00001719 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001720 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01001721 "detected beacon loss from AP - probing\n");
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001722
Holger Schurig6efb71b2012-05-15 15:38:59 +02001723 ieee80211_cqm_rssi_notify(&sdata->vif,
1724 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001725
Johannes Bergb291ba12009-07-10 15:29:03 +02001726 /*
1727 * The driver/our work has already reported this event or the
1728 * connection monitoring has kicked in and we have already sent
1729 * a probe request. Or maybe the AP died and the driver keeps
1730 * reporting until we disassociate...
1731 *
1732 * In either case we have to ignore the current call to this
1733 * function (except for setting the correct probe reason bit)
1734 * because otherwise we would reset the timer every time and
1735 * never check whether we received a probe response!
1736 */
1737 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1738 IEEE80211_STA_CONNECTION_POLL))
1739 already = true;
1740
1741 if (beacon)
1742 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1743 else
1744 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1745
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001746 mutex_unlock(&sdata->local->mtx);
1747
Johannes Bergb291ba12009-07-10 15:29:03 +02001748 if (already)
1749 goto out;
1750
Johannes Berg4e751842009-06-10 15:16:52 +02001751 mutex_lock(&sdata->local->iflist_mtx);
1752 ieee80211_recalc_ps(sdata->local, -1);
1753 mutex_unlock(&sdata->local->iflist_mtx);
1754
Maxim Levitskya43abf22009-07-31 18:54:12 +03001755 ifmgd->probe_send_count = 0;
1756 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001757 out:
1758 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001759}
1760
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001761struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1762 struct ieee80211_vif *vif)
1763{
1764 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1765 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001766 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001767 struct sk_buff *skb;
1768 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001769 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001770
1771 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1772 return NULL;
1773
1774 ASSERT_MGD_MTX(ifmgd);
1775
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001776 if (ifmgd->associated)
1777 cbss = ifmgd->associated;
1778 else if (ifmgd->auth_data)
1779 cbss = ifmgd->auth_data->bss;
1780 else if (ifmgd->assoc_data)
1781 cbss = ifmgd->assoc_data->bss;
1782 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001783 return NULL;
1784
Johannes Berg9caf0362012-11-29 01:25:20 +01001785 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001786 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001787 if (WARN_ON_ONCE(ssid == NULL))
1788 ssid_len = 0;
1789 else
1790 ssid_len = ssid[1];
1791
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001792 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001793 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001794 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001795 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001796 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001797
1798 return skb;
1799}
1800EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1801
Johannes Bergeef9e542013-01-29 13:09:34 +01001802static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001803{
1804 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001805 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001806
1807 mutex_lock(&ifmgd->mtx);
1808 if (!ifmgd->associated) {
1809 mutex_unlock(&ifmgd->mtx);
1810 return;
1811 }
1812
Johannes Berg37ad3882012-02-24 13:50:54 +01001813 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1814 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01001815 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02001816 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01001817 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1818 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001819 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001820
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001821 /*
1822 * must be outside lock due to cfg80211,
1823 * but that's not a problem.
1824 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001825 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001826}
1827
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001828static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001829{
1830 struct ieee80211_sub_if_data *sdata =
1831 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001832 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1834 struct sta_info *sta;
1835
1836 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301837 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001838 sta = sta_info_get(sdata, ifmgd->bssid);
1839 if (sta)
1840 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301841 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001842 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001843
Johannes Berg682bd382013-01-29 13:13:50 +01001844 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001845 sdata_info(sdata, "Connection to AP %pM lost\n",
1846 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01001847 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02001848 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001849 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001850 }
1851}
1852
1853static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1854{
1855 struct ieee80211_sub_if_data *sdata =
1856 container_of(work, struct ieee80211_sub_if_data,
1857 u.mgd.csa_connection_drop_work);
1858
Johannes Bergeef9e542013-01-29 13:09:34 +01001859 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001860}
1861
Kalle Valo04de8382009-03-22 21:57:35 +02001862void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1863{
1864 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001865 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001866
Johannes Bergb5878a22010-04-07 16:48:40 +02001867 trace_api_beacon_loss(sdata);
1868
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001869 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
Johannes Berg682bd382013-01-29 13:13:50 +01001870 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001871 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001872}
1873EXPORT_SYMBOL(ieee80211_beacon_loss);
1874
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001875void ieee80211_connection_loss(struct ieee80211_vif *vif)
1876{
1877 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1878 struct ieee80211_hw *hw = &sdata->local->hw;
1879
Johannes Bergb5878a22010-04-07 16:48:40 +02001880 trace_api_connection_loss(sdata);
1881
Johannes Berg682bd382013-01-29 13:13:50 +01001882 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001883 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1884}
1885EXPORT_SYMBOL(ieee80211_connection_loss);
1886
1887
Johannes Berg66e67e42012-01-20 13:55:27 +01001888static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1889 bool assoc)
1890{
1891 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1892
1893 lockdep_assert_held(&sdata->u.mgd.mtx);
1894
Johannes Berg66e67e42012-01-20 13:55:27 +01001895 if (!assoc) {
1896 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1897
1898 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1899 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01001900 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001901 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001902 }
1903
Johannes Berg5b112d32013-02-01 01:49:58 +01001904 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01001905 kfree(auth_data);
1906 sdata->u.mgd.auth_data = NULL;
1907}
1908
1909static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1910 struct ieee80211_mgmt *mgmt, size_t len)
1911{
Johannes Berg1672c0e32013-01-29 15:02:27 +01001912 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01001913 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1914 u8 *pos;
1915 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01001916 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01001917
1918 pos = mgmt->u.auth.variable;
1919 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1920 if (!elems.challenge)
1921 return;
1922 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001923 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01001924 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1925 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
1926 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001927 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001928 elems.challenge - 2, elems.challenge_len + 2,
1929 auth_data->bss->bssid, auth_data->bss->bssid,
1930 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001931 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01001932}
1933
1934static enum rx_mgmt_action __must_check
1935ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1936 struct ieee80211_mgmt *mgmt, size_t len)
1937{
1938 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1939 u8 bssid[ETH_ALEN];
1940 u16 auth_alg, auth_transaction, status_code;
1941 struct sta_info *sta;
1942
1943 lockdep_assert_held(&ifmgd->mtx);
1944
1945 if (len < 24 + 6)
1946 return RX_MGMT_NONE;
1947
1948 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1949 return RX_MGMT_NONE;
1950
1951 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1952
Joe Perchesb203ca32012-05-08 18:56:52 +00001953 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001954 return RX_MGMT_NONE;
1955
1956 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1957 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1958 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1959
1960 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001961 auth_transaction != ifmgd->auth_data->expected_transaction) {
1962 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1963 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1964 auth_transaction,
1965 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001966 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001967 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001968
1969 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001970 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1971 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001972 ieee80211_destroy_auth_data(sdata, false);
1973 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001974 }
1975
1976 switch (ifmgd->auth_data->algorithm) {
1977 case WLAN_AUTH_OPEN:
1978 case WLAN_AUTH_LEAP:
1979 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03001980 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01001981 break;
1982 case WLAN_AUTH_SHARED_KEY:
1983 if (ifmgd->auth_data->expected_transaction != 4) {
1984 ieee80211_auth_challenge(sdata, mgmt, len);
1985 /* need another frame */
1986 return RX_MGMT_NONE;
1987 }
1988 break;
1989 default:
1990 WARN_ONCE(1, "invalid auth alg %d",
1991 ifmgd->auth_data->algorithm);
1992 return RX_MGMT_NONE;
1993 }
1994
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001995 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01001996 ifmgd->auth_data->done = true;
1997 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01001998 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01001999 run_again(ifmgd, ifmgd->auth_data->timeout);
2000
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002001 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2002 ifmgd->auth_data->expected_transaction != 2) {
2003 /*
2004 * Report auth frame to user space for processing since another
2005 * round of Authentication frames is still needed.
2006 */
2007 return RX_MGMT_CFG80211_RX_AUTH;
2008 }
2009
Johannes Berg66e67e42012-01-20 13:55:27 +01002010 /* move station state to auth */
2011 mutex_lock(&sdata->local->sta_mtx);
2012 sta = sta_info_get(sdata, bssid);
2013 if (!sta) {
2014 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2015 goto out_err;
2016 }
2017 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002018 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002019 goto out_err;
2020 }
2021 mutex_unlock(&sdata->local->sta_mtx);
2022
2023 return RX_MGMT_CFG80211_RX_AUTH;
2024 out_err:
2025 mutex_unlock(&sdata->local->sta_mtx);
2026 /* ignore frame -- wait for timeout */
2027 return RX_MGMT_NONE;
2028}
2029
2030
Johannes Berg77fdaa12009-07-07 03:45:17 +02002031static enum rx_mgmt_action __must_check
2032ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002033 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002034{
Johannes Berg46900292009-02-15 12:44:28 +01002035 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002036 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002037 u16 reason_code;
2038
Johannes Berg66e67e42012-01-20 13:55:27 +01002039 lockdep_assert_held(&ifmgd->mtx);
2040
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002041 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002042 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002043
Johannes Berg66e67e42012-01-20 13:55:27 +01002044 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002045 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002046 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002047
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002048 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002049
2050 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2051
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002052 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2053 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002054
Johannes Berg37ad3882012-02-24 13:50:54 +01002055 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2056
Johannes Berg77fdaa12009-07-07 03:45:17 +02002057 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002058}
2059
2060
Johannes Berg77fdaa12009-07-07 03:45:17 +02002061static enum rx_mgmt_action __must_check
2062ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2063 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002064{
Johannes Berg46900292009-02-15 12:44:28 +01002065 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002066 u16 reason_code;
2067
Johannes Berg66e67e42012-01-20 13:55:27 +01002068 lockdep_assert_held(&ifmgd->mtx);
2069
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002070 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002071 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002072
Johannes Berg66e67e42012-01-20 13:55:27 +01002073 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002074 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002075 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002076
2077 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2078
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002079 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2080 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002081
Johannes Berg37ad3882012-02-24 13:50:54 +01002082 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2083
Johannes Berg77fdaa12009-07-07 03:45:17 +02002084 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002085}
2086
Christian Lamparterc74d0842011-10-15 00:14:49 +02002087static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2088 u8 *supp_rates, unsigned int supp_rates_len,
2089 u32 *rates, u32 *basic_rates,
2090 bool *have_higher_than_11mbit,
2091 int *min_rate, int *min_rate_index)
2092{
2093 int i, j;
2094
2095 for (i = 0; i < supp_rates_len; i++) {
2096 int rate = (supp_rates[i] & 0x7f) * 5;
2097 bool is_basic = !!(supp_rates[i] & 0x80);
2098
2099 if (rate > 110)
2100 *have_higher_than_11mbit = true;
2101
2102 /*
2103 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2104 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2105 *
2106 * Note: Even through the membership selector and the basic
2107 * rate flag share the same bit, they are not exactly
2108 * the same.
2109 */
2110 if (!!(supp_rates[i] & 0x80) &&
2111 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2112 continue;
2113
2114 for (j = 0; j < sband->n_bitrates; j++) {
2115 if (sband->bitrates[j].bitrate == rate) {
2116 *rates |= BIT(j);
2117 if (is_basic)
2118 *basic_rates |= BIT(j);
2119 if (rate < *min_rate) {
2120 *min_rate = rate;
2121 *min_rate_index = j;
2122 }
2123 break;
2124 }
2125 }
2126 }
2127}
Jiri Bencf0706e82007-05-05 11:45:53 -07002128
Johannes Berg66e67e42012-01-20 13:55:27 +01002129static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2130 bool assoc)
2131{
2132 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2133
2134 lockdep_assert_held(&sdata->u.mgd.mtx);
2135
Johannes Berg66e67e42012-01-20 13:55:27 +01002136 if (!assoc) {
2137 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2138
2139 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2140 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002141 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002142 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002143 }
2144
2145 kfree(assoc_data);
2146 sdata->u.mgd.assoc_data = NULL;
2147}
2148
2149static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2150 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002151 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002152{
Johannes Berg46900292009-02-15 12:44:28 +01002153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002154 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002155 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002156 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002157 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002158 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002159 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002160 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002161 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002162 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002163
Johannes Bergaf6b6372009-12-23 13:15:35 +01002164 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002165
Jiri Bencf0706e82007-05-05 11:45:53 -07002166 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002167 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002168
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002169 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002170 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2171 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002172 aid &= ~(BIT(15) | BIT(14));
2173
Johannes Berg05cb9102011-10-28 11:59:47 +02002174 ifmgd->broken_ap = false;
2175
2176 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002177 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2178 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002179 aid = 0;
2180 ifmgd->broken_ap = true;
2181 }
2182
Johannes Bergaf6b6372009-12-23 13:15:35 +01002183 pos = mgmt->u.assoc_resp.variable;
2184 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2185
Jiri Bencf0706e82007-05-05 11:45:53 -07002186 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002187 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002188 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002189 }
2190
Johannes Berg46900292009-02-15 12:44:28 +01002191 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002192
Guy Eilam2a33bee2011-08-17 15:18:15 +03002193 mutex_lock(&sdata->local->sta_mtx);
2194 /*
2195 * station info was already allocated and inserted before
2196 * the association and should be available to us
2197 */
Johannes Berg7852e362012-01-20 13:55:24 +01002198 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002199 if (WARN_ON(!sta)) {
2200 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002201 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002202 }
2203
Johannes Berg55de9082012-07-26 17:24:39 +02002204 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002205
Johannes Berga8243b72012-11-22 14:32:09 +01002206 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002207 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002208 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002209
Mahesh Palivela818255e2012-10-10 11:33:04 +00002210 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2211 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002212 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002213
Johannes Berg11289582013-02-07 17:36:12 +01002214 if (elems.ht_operation && elems.wmm_param &&
2215 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
2216 changed |= ieee80211_config_ht_tx(sdata, sta,
2217 elems.ht_operation,
2218 cbss->bssid, false);
2219
Johannes Bergbee7f582013-02-07 22:24:55 +01002220 /*
2221 * If an operating mode notification IE is present, override the
2222 * NSS calculation (that would be done in rate_control_rate_init())
2223 * and use the # of streams from that element.
2224 */
2225 if (elems.opmode_notif &&
2226 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2227 u8 nss;
2228
2229 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2230 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2231 nss += 1;
2232 sta->sta.rx_nss = nss;
2233 }
2234
Johannes Berg4b7679a2008-09-18 18:14:18 +02002235 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002236
Johannes Berg46900292009-02-15 12:44:28 +01002237 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002238 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002239
Johannes Bergddf4ac52008-10-22 11:41:38 +02002240 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002241 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002242
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002243 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002244 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2245 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2246 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002247 sdata_info(sdata,
2248 "failed to move station %pM to desired state\n",
2249 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002250 WARN_ON(__sta_info_destroy(sta));
2251 mutex_unlock(&sdata->local->sta_mtx);
2252 return false;
2253 }
2254
Johannes Berg7852e362012-01-20 13:55:24 +01002255 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002256
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002257 /*
2258 * Always handle WMM once after association regardless
2259 * of the first value the AP uses. Setting -1 here has
2260 * that effect because the AP values is an unsigned
2261 * 4-bit value.
2262 */
2263 ifmgd->wmm_last_param_set = -1;
2264
Johannes Bergddf4ac52008-10-22 11:41:38 +02002265 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002266 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002267 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002268 else
Johannes Berg3abead52012-03-02 15:56:59 +01002269 ieee80211_set_wmm_default(sdata, false);
2270 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002271
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002272 /* set AID and assoc capability,
2273 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002274 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002275 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002276 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002277
Kalle Valo15b7b062009-03-22 21:57:14 +02002278 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002279 * If we're using 4-addr mode, let the AP know that we're
2280 * doing so, so that it can create the STA VLAN on its side
2281 */
2282 if (ifmgd->use_4addr)
2283 ieee80211_send_4addr_nullfunc(local, sdata);
2284
2285 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002286 * Start timer to probe the connection to the AP now.
2287 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002288 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002289 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002290 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002291
Johannes Bergaf6b6372009-12-23 13:15:35 +01002292 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002293}
2294
Johannes Berg66e67e42012-01-20 13:55:27 +01002295static enum rx_mgmt_action __must_check
2296ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2297 struct ieee80211_mgmt *mgmt, size_t len,
2298 struct cfg80211_bss **bss)
2299{
2300 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2301 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2302 u16 capab_info, status_code, aid;
2303 struct ieee802_11_elems elems;
2304 u8 *pos;
2305 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002306
Johannes Berg66e67e42012-01-20 13:55:27 +01002307 lockdep_assert_held(&ifmgd->mtx);
2308
2309 if (!assoc_data)
2310 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002311 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002312 return RX_MGMT_NONE;
2313
2314 /*
2315 * AssocResp and ReassocResp have identical structure, so process both
2316 * of them in this function.
2317 */
2318
2319 if (len < 24 + 6)
2320 return RX_MGMT_NONE;
2321
2322 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2323 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2324 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2325 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2326
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002327 sdata_info(sdata,
2328 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2329 reassoc ? "Rea" : "A", mgmt->sa,
2330 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002331
2332 pos = mgmt->u.assoc_resp.variable;
2333 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2334
2335 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2336 elems.timeout_int && elems.timeout_int_len == 5 &&
2337 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2338 u32 tu, ms;
2339 tu = get_unaligned_le32(elems.timeout_int + 1);
2340 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002341 sdata_info(sdata,
2342 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2343 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002344 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002345 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002346 if (ms > IEEE80211_ASSOC_TIMEOUT)
2347 run_again(ifmgd, assoc_data->timeout);
2348 return RX_MGMT_NONE;
2349 }
2350
2351 *bss = assoc_data->bss;
2352
2353 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002354 sdata_info(sdata, "%pM denied association (code=%d)\n",
2355 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002356 ieee80211_destroy_assoc_data(sdata, false);
2357 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002358 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2359 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002360 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002361 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002362 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2363 }
John W. Linville635d9992012-07-09 16:34:34 -04002364 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002365
2366 /*
2367 * destroy assoc_data afterwards, as otherwise an idle
2368 * recalc after assoc_data is NULL but before associated
2369 * is set can cause the interface to go idle
2370 */
2371 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002372 }
2373
2374 return RX_MGMT_CFG80211_RX_ASSOC;
2375}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002376
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002377static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002378 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002379 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002380 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002381{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002382 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002383 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002384 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002385 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002386 bool need_ps = false;
2387
Johannes Berg826262c2012-12-10 16:38:14 +02002388 if ((sdata->u.mgd.associated &&
2389 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2390 (sdata->u.mgd.assoc_data &&
2391 ether_addr_equal(mgmt->bssid,
2392 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002393 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002394 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2395
2396 if (elems->tim && !elems->parse_error) {
2397 struct ieee80211_tim_ie *tim_ie = elems->tim;
2398 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2399 }
Johannes Berg56007a02010-01-26 14:19:52 +01002400 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002401
Johannes Berg5bda6172008-09-08 11:05:10 +02002402 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002403 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2404 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002405 else
2406 freq = rx_status->freq;
2407
2408 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2409
2410 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2411 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002412
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002413 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002414 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002415 if (bss)
2416 ieee80211_rx_bss_put(local, bss);
2417
2418 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002419 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002420
Johannes Berg56007a02010-01-26 14:19:52 +01002421 if (need_ps) {
2422 mutex_lock(&local->iflist_mtx);
2423 ieee80211_recalc_ps(local, -1);
2424 mutex_unlock(&local->iflist_mtx);
2425 }
2426
Johannes Berg5bc14202012-08-01 16:13:02 +02002427 if (elems->ch_switch_ie &&
2428 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2429 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002430 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002431}
2432
2433
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002434static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002435 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002436{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002437 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002438 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002439 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2440 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002441 struct ieee802_11_elems elems;
2442
Kalle Valo15b7b062009-03-22 21:57:14 +02002443 ifmgd = &sdata->u.mgd;
2444
Johannes Berg77fdaa12009-07-07 03:45:17 +02002445 ASSERT_MGD_MTX(ifmgd);
2446
Joe Perchesb203ca32012-05-08 18:56:52 +00002447 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002448 return; /* ignore ProbeResp to foreign address */
2449
Ester Kummerae6a44e2008-06-27 18:54:48 +03002450 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2451 if (baselen > len)
2452 return;
2453
2454 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2455 &elems);
2456
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002457 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002458
Johannes Berg77fdaa12009-07-07 03:45:17 +02002459 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002460 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002461 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002462
2463 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002464 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002465 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002466 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002467 ifmgd->auth_data->tries = 0;
2468 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002469 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002470 run_again(ifmgd, ifmgd->auth_data->timeout);
2471 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002472}
2473
Johannes Bergd91f36d2009-04-16 13:17:26 +02002474/*
2475 * This is the canonical list of information elements we care about,
2476 * the filter code also gives us all changes to the Microsoft OUI
2477 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2478 *
2479 * We implement beacon filtering in software since that means we can
2480 * avoid processing the frame here and in cfg80211, and userspace
2481 * will not be able to tell whether the hardware supports it or not.
2482 *
2483 * XXX: This list needs to be dynamic -- userspace needs to be able to
2484 * add items it requires. It also needs to be able to tell us to
2485 * look out for other vendor IEs.
2486 */
2487static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002488 (1ULL << WLAN_EID_COUNTRY) |
2489 (1ULL << WLAN_EID_ERP_INFO) |
2490 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2491 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2492 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002493 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002494
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002495static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002496 struct ieee80211_mgmt *mgmt,
2497 size_t len,
2498 struct ieee80211_rx_status *rx_status)
2499{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002500 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002501 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002502 size_t baselen;
2503 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002504 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002505 struct ieee80211_chanctx_conf *chanctx_conf;
2506 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002507 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002508 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002509 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002510 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002511 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002512 u8 *bssid;
2513
Johannes Berg66e67e42012-01-20 13:55:27 +01002514 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002515
Ester Kummerae6a44e2008-06-27 18:54:48 +03002516 /* Process beacon from the current BSS */
2517 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2518 if (baselen > len)
2519 return;
2520
Johannes Berg55de9082012-07-26 17:24:39 +02002521 rcu_read_lock();
2522 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2523 if (!chanctx_conf) {
2524 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002525 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002526 }
2527
Johannes Berg4bf88532012-11-09 11:39:59 +01002528 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002529 rcu_read_unlock();
2530 return;
2531 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002532 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002533 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002534
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002535 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002536 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002537 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2538 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002539
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002540 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002541 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002542 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002543 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2544 sdata->vif.bss_conf.sync_tsf =
2545 le64_to_cpu(mgmt->u.beacon.timestamp);
2546 sdata->vif.bss_conf.sync_device_ts =
2547 rx_status->device_timestamp;
2548 if (elems.tim)
2549 sdata->vif.bss_conf.sync_dtim_count =
2550 elems.tim->dtim_count;
2551 else
2552 sdata->vif.bss_conf.sync_dtim_count = 0;
2553 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002554 /* continue assoc process */
2555 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002556 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002557 run_again(ifmgd, ifmgd->assoc_data->timeout);
2558 return;
2559 }
2560
2561 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002562 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002563 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002564 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002565
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002566 /* Track average RSSI from the Beacon frames of the current AP */
2567 ifmgd->last_beacon_signal = rx_status->signal;
2568 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2569 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002570 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002571 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002572 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002573 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002574 } else {
2575 ifmgd->ave_beacon_signal =
2576 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2577 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2578 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002579 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002580 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002581
2582 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2583 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2584 int sig = ifmgd->ave_beacon_signal;
2585 int last_sig = ifmgd->last_ave_beacon_signal;
2586
2587 /*
2588 * if signal crosses either of the boundaries, invoke callback
2589 * with appropriate parameters
2590 */
2591 if (sig > ifmgd->rssi_max_thold &&
2592 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2593 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002594 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002595 } else if (sig < ifmgd->rssi_min_thold &&
2596 (last_sig >= ifmgd->rssi_max_thold ||
2597 last_sig == 0)) {
2598 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002599 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002600 }
2601 }
2602
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002603 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002604 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002605 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002606 int sig = ifmgd->ave_beacon_signal / 16;
2607 int last_event = ifmgd->last_cqm_event_signal;
2608 int thold = bss_conf->cqm_rssi_thold;
2609 int hyst = bss_conf->cqm_rssi_hyst;
2610 if (sig < thold &&
2611 (last_event == 0 || sig < last_event - hyst)) {
2612 ifmgd->last_cqm_event_signal = sig;
2613 ieee80211_cqm_rssi_notify(
2614 &sdata->vif,
2615 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2616 GFP_KERNEL);
2617 } else if (sig > thold &&
2618 (last_event == 0 || sig > last_event + hyst)) {
2619 ifmgd->last_cqm_event_signal = sig;
2620 ieee80211_cqm_rssi_notify(
2621 &sdata->vif,
2622 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2623 GFP_KERNEL);
2624 }
2625 }
2626
Johannes Bergb291ba12009-07-10 15:29:03 +02002627 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002628 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002629 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002630 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002631 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002632 ieee80211_run_deferred_scan(local);
2633 mutex_unlock(&local->mtx);
2634
Johannes Berg4e751842009-06-10 15:16:52 +02002635 mutex_lock(&local->iflist_mtx);
2636 ieee80211_recalc_ps(local, -1);
2637 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002638 }
2639
Johannes Bergb291ba12009-07-10 15:29:03 +02002640 /*
2641 * Push the beacon loss detection into the future since
2642 * we are processing a beacon from the AP just now.
2643 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002644 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002645
Johannes Bergd91f36d2009-04-16 13:17:26 +02002646 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2647 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2648 len - baselen, &elems,
2649 care_about_ies, ncrc);
2650
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302651 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002652 bool directed_tim = ieee80211_check_tim(elems.tim,
2653 elems.tim_len,
2654 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002655 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002656 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002657 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2658 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2659 ieee80211_hw_config(local,
2660 IEEE80211_CONF_CHANGE_PS);
2661 }
Kalle Valo572e0012009-02-10 17:09:31 +02002662 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302663 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002664 local->pspolling = true;
2665
2666 /*
2667 * Here is assumed that the driver will be
2668 * able to send ps-poll frame and receive a
2669 * response even though power save mode is
2670 * enabled, but some drivers might require
2671 * to disable power save here. This needs
2672 * to be investigated.
2673 */
2674 ieee80211_send_pspoll(local, sdata);
2675 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002676 }
2677 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002678
Johannes Berg488dd7b2012-10-29 20:08:01 +01002679 if (sdata->vif.p2p) {
2680 u8 noa[2];
2681 int ret;
2682
2683 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2684 len - baselen,
2685 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2686 noa, sizeof(noa));
2687 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2688 bss_conf->p2p_oppps = noa[1] & 0x80;
2689 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2690 changed |= BSS_CHANGED_P2P_PS;
2691 sdata->u.mgd.p2p_noa_index = noa[0];
2692 /*
2693 * make sure we update all information, the CRC
2694 * mechanism doesn't look at P2P attributes.
2695 */
2696 ifmgd->beacon_crc_valid = false;
2697 }
2698 }
2699
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002700 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002701 return;
2702 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002703 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002704
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002705 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002706
2707 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2708 elems.wmm_param_len))
2709 changed |= BSS_CHANGED_QOS;
2710
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002711 /*
2712 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002713 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002714 */
2715 if (!bss_conf->dtim_period) {
2716 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2717 if (elems.tim)
2718 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2719 else
2720 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01002721
2722 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2723 sdata->vif.bss_conf.sync_tsf =
2724 le64_to_cpu(mgmt->u.beacon.timestamp);
2725 sdata->vif.bss_conf.sync_device_ts =
2726 rx_status->device_timestamp;
2727 if (elems.tim)
2728 sdata->vif.bss_conf.sync_dtim_count =
2729 elems.tim->dtim_count;
2730 else
2731 sdata->vif.bss_conf.sync_dtim_count = 0;
2732 }
2733
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002734 changed |= BSS_CHANGED_DTIM_PERIOD;
2735 }
2736
Johannes Berg7a5158e2008-10-08 10:59:33 +02002737 if (elems.erp_info && elems.erp_info_len >= 1) {
2738 erp_valid = true;
2739 erp_value = elems.erp_info[0];
2740 } else {
2741 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002742 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002743 changed |= ieee80211_handle_bss_capability(sdata,
2744 le16_to_cpu(mgmt->u.beacon.capab_info),
2745 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002746
Johannes Berg11289582013-02-07 17:36:12 +01002747 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01002748 sta = sta_info_get(sdata, bssid);
2749
Johannes Berg074d46d2012-03-15 19:45:16 +01002750 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002751 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Bergbee7f582013-02-07 22:24:55 +01002752 changed |= ieee80211_config_ht_tx(sdata, sta,
Johannes Berg11289582013-02-07 17:36:12 +01002753 elems.ht_operation,
Johannes Berg24398e32012-03-28 10:58:36 +02002754 bssid, true);
Johannes Bergbee7f582013-02-07 22:24:55 +01002755
2756 if (sta && elems.opmode_notif)
2757 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
2758 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01002759 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002760
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002761 if (elems.country_elem && elems.pwr_constr_elem &&
2762 mgmt->u.probe_resp.capab_info &
2763 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002764 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2765 elems.country_elem,
2766 elems.country_elem_len,
2767 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002768
Johannes Berg471b3ef2007-12-28 14:32:58 +01002769 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002770}
2771
Johannes Berg1fa57d02010-06-10 10:21:32 +02002772void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2773 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002774{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002775 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002776 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002777 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002778 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002779 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002780 u16 fc;
2781
Jiri Bencf0706e82007-05-05 11:45:53 -07002782 rx_status = (struct ieee80211_rx_status *) skb->cb;
2783 mgmt = (struct ieee80211_mgmt *) skb->data;
2784 fc = le16_to_cpu(mgmt->frame_control);
2785
Johannes Berg77fdaa12009-07-07 03:45:17 +02002786 mutex_lock(&ifmgd->mtx);
2787
Johannes Berg66e67e42012-01-20 13:55:27 +01002788 switch (fc & IEEE80211_FCTL_STYPE) {
2789 case IEEE80211_STYPE_BEACON:
2790 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2791 break;
2792 case IEEE80211_STYPE_PROBE_RESP:
2793 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2794 break;
2795 case IEEE80211_STYPE_AUTH:
2796 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2797 break;
2798 case IEEE80211_STYPE_DEAUTH:
2799 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2800 break;
2801 case IEEE80211_STYPE_DISASSOC:
2802 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2803 break;
2804 case IEEE80211_STYPE_ASSOC_RESP:
2805 case IEEE80211_STYPE_REASSOC_RESP:
2806 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2807 break;
2808 case IEEE80211_STYPE_ACTION:
2809 switch (mgmt->u.action.category) {
2810 case WLAN_CATEGORY_SPECTRUM_MGMT:
2811 ieee80211_sta_process_chanswitch(sdata,
2812 &mgmt->u.action.u.chan_switch.sw_elem,
2813 (void *)ifmgd->associated->priv,
2814 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002815 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002816 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002817 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002818 mutex_unlock(&ifmgd->mtx);
2819
Johannes Berg66e67e42012-01-20 13:55:27 +01002820 switch (rma) {
2821 case RX_MGMT_NONE:
2822 /* no action */
2823 break;
2824 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002825 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002826 break;
2827 case RX_MGMT_CFG80211_DISASSOC:
2828 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2829 break;
2830 case RX_MGMT_CFG80211_RX_AUTH:
2831 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2832 break;
2833 case RX_MGMT_CFG80211_RX_ASSOC:
2834 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2835 break;
2836 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2837 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2838 break;
2839 default:
2840 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002841 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002842}
2843
Johannes Berg9c6bd792008-09-11 00:01:52 +02002844static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002845{
2846 struct ieee80211_sub_if_data *sdata =
2847 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002848 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002849 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002850
Johannes Berg5bb644a2009-05-17 11:40:42 +02002851 if (local->quiescing) {
2852 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2853 return;
2854 }
2855
Johannes Berg64592c82010-06-10 10:21:31 +02002856 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002857}
2858
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002859static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01002860 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002861{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002862 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002863 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002864
Johannes Berg37ad3882012-02-24 13:50:54 +01002865 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01002866 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002867 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002868
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002869 /*
2870 * must be outside lock due to cfg80211,
2871 * but that's not a problem.
2872 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002873 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002874
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002875 mutex_lock(&ifmgd->mtx);
2876}
2877
Johannes Berg66e67e42012-01-20 13:55:27 +01002878static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2879{
2880 struct ieee80211_local *local = sdata->local;
2881 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2882 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002883 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002884
2885 lockdep_assert_held(&ifmgd->mtx);
2886
2887 if (WARN_ON_ONCE(!auth_data))
2888 return -EINVAL;
2889
Johannes Berg1672c0e32013-01-29 15:02:27 +01002890 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2891 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2892 IEEE80211_TX_INTFL_MLME_CONN_TX;
2893
Johannes Berg66e67e42012-01-20 13:55:27 +01002894 auth_data->tries++;
2895
2896 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002897 sdata_info(sdata, "authentication with %pM timed out\n",
2898 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002899
2900 /*
2901 * Most likely AP is not in the range so remove the
2902 * bss struct for that AP.
2903 */
2904 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2905
2906 return -ETIMEDOUT;
2907 }
2908
Johannes Berga1845fc2012-06-27 13:18:36 +02002909 drv_mgd_prepare_tx(local, sdata);
2910
Johannes Berg66e67e42012-01-20 13:55:27 +01002911 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002912 u16 trans = 1;
2913 u16 status = 0;
2914
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002915 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2916 auth_data->bss->bssid, auth_data->tries,
2917 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002918
2919 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002920
2921 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2922 trans = auth_data->sae_trans;
2923 status = auth_data->sae_status;
2924 auth_data->expected_transaction = trans;
2925 }
2926
2927 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2928 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002929 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002930 auth_data->bss->bssid, NULL, 0, 0,
2931 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002932 } else {
2933 const u8 *ssidie;
2934
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002935 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2936 auth_data->bss->bssid, auth_data->tries,
2937 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002938
Johannes Berg9caf0362012-11-29 01:25:20 +01002939 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002940 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01002941 if (!ssidie) {
2942 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002943 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01002944 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002945 /*
2946 * Direct probe is sent to broadcast address as some APs
2947 * will not answer to direct packet in unassociated state.
2948 */
2949 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01002950 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02002951 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002952 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002953 }
2954
Johannes Berg1672c0e32013-01-29 15:02:27 +01002955 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
2956 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01002957 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002958 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01002959 } else {
2960 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002961 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002962
2963 return 0;
2964}
2965
2966static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2967{
2968 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2969 struct ieee80211_local *local = sdata->local;
2970
2971 lockdep_assert_held(&sdata->u.mgd.mtx);
2972
Johannes Berg66e67e42012-01-20 13:55:27 +01002973 assoc_data->tries++;
2974 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002975 sdata_info(sdata, "association with %pM timed out\n",
2976 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002977
2978 /*
2979 * Most likely AP is not in the range so remove the
2980 * bss struct for that AP.
2981 */
2982 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2983
2984 return -ETIMEDOUT;
2985 }
2986
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002987 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2988 assoc_data->bss->bssid, assoc_data->tries,
2989 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002990 ieee80211_send_assoc(sdata);
2991
Johannes Berg1672c0e32013-01-29 15:02:27 +01002992 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
2993 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01002994 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002995 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01002996 } else {
2997 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002998 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002999
3000 return 0;
3001}
3002
Johannes Berg1672c0e32013-01-29 15:02:27 +01003003void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3004 __le16 fc, bool acked)
3005{
3006 struct ieee80211_local *local = sdata->local;
3007
3008 sdata->u.mgd.status_fc = fc;
3009 sdata->u.mgd.status_acked = acked;
3010 sdata->u.mgd.status_received = true;
3011
3012 ieee80211_queue_work(&local->hw, &sdata->work);
3013}
3014
Johannes Berg1fa57d02010-06-10 10:21:32 +02003015void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003016{
Johannes Berg60f8b392008-09-08 17:44:22 +02003017 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003018 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003019
Johannes Berg77fdaa12009-07-07 03:45:17 +02003020 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003021
Johannes Berg1672c0e32013-01-29 15:02:27 +01003022 if (ifmgd->status_received) {
3023 __le16 fc = ifmgd->status_fc;
3024 bool status_acked = ifmgd->status_acked;
3025
3026 ifmgd->status_received = false;
3027 if (ifmgd->auth_data &&
3028 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3029 if (status_acked) {
3030 ifmgd->auth_data->timeout =
3031 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3032 run_again(ifmgd, ifmgd->auth_data->timeout);
3033 } else {
3034 ifmgd->auth_data->timeout = jiffies - 1;
3035 }
Johannes Berg89afe612013-02-13 15:39:57 +01003036 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003037 } else if (ifmgd->assoc_data &&
3038 (ieee80211_is_assoc_req(fc) ||
3039 ieee80211_is_reassoc_req(fc))) {
3040 if (status_acked) {
3041 ifmgd->assoc_data->timeout =
3042 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3043 run_again(ifmgd, ifmgd->assoc_data->timeout);
3044 } else {
3045 ifmgd->assoc_data->timeout = jiffies - 1;
3046 }
Johannes Berg89afe612013-02-13 15:39:57 +01003047 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003048 }
3049 }
3050
Johannes Berg89afe612013-02-13 15:39:57 +01003051 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003052 time_after(jiffies, ifmgd->auth_data->timeout)) {
3053 if (ifmgd->auth_data->done) {
3054 /*
3055 * ok ... we waited for assoc but userspace didn't,
3056 * so let's just kill the auth data
3057 */
3058 ieee80211_destroy_auth_data(sdata, false);
3059 } else if (ieee80211_probe_auth(sdata)) {
3060 u8 bssid[ETH_ALEN];
3061
3062 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3063
3064 ieee80211_destroy_auth_data(sdata, false);
3065
3066 mutex_unlock(&ifmgd->mtx);
3067 cfg80211_send_auth_timeout(sdata->dev, bssid);
3068 mutex_lock(&ifmgd->mtx);
3069 }
Johannes Berg89afe612013-02-13 15:39:57 +01003070 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003071 run_again(ifmgd, ifmgd->auth_data->timeout);
3072
Johannes Berg89afe612013-02-13 15:39:57 +01003073 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003074 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003075 if ((ifmgd->assoc_data->need_beacon &&
3076 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003077 ieee80211_do_assoc(sdata)) {
3078 u8 bssid[ETH_ALEN];
3079
3080 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3081
3082 ieee80211_destroy_assoc_data(sdata, false);
3083
3084 mutex_unlock(&ifmgd->mtx);
3085 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3086 mutex_lock(&ifmgd->mtx);
3087 }
Johannes Berg89afe612013-02-13 15:39:57 +01003088 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003089 run_again(ifmgd, ifmgd->assoc_data->timeout);
3090
Johannes Bergb291ba12009-07-10 15:29:03 +02003091 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3092 IEEE80211_STA_CONNECTION_POLL) &&
3093 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003094 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003095 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003096
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003097 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003098
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003099 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003100 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003101 else
Ben Greear180205b2011-02-04 15:30:24 -08003102 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003103
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003104 /* ACK received for nullfunc probing frame */
3105 if (!ifmgd->probe_send_count)
3106 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003107 else if (ifmgd->nullfunc_failed) {
3108 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003109 mlme_dbg(sdata,
3110 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3111 bssid, ifmgd->probe_send_count,
3112 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003113 ieee80211_mgd_probe_ap_send(sdata);
3114 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003115 mlme_dbg(sdata,
3116 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3117 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003118 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003119 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3120 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003121 }
3122 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003123 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003124 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003125 mlme_dbg(sdata,
3126 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3127 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003128 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003129 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003130 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003131 mlme_dbg(sdata,
3132 "No probe response from AP %pM after %dms, try %d/%i\n",
3133 bssid, probe_wait_ms,
3134 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003135 ieee80211_mgd_probe_ap_send(sdata);
3136 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003137 /*
3138 * We actually lost the connection ... or did we?
3139 * Let's make sure!
3140 */
Ben Greearb38afa82010-10-07 16:12:06 -07003141 wiphy_debug(local->hw.wiphy,
3142 "%s: No probe response from AP %pM"
3143 " after %dms, disconnecting.\n",
3144 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003145 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003146
Johannes Berg95acac62011-07-12 12:30:59 +02003147 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003148 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003149 }
3150 }
3151
Johannes Berg77fdaa12009-07-07 03:45:17 +02003152 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003153}
Johannes Berg0a51b272008-09-08 17:44:25 +02003154
Johannes Bergb291ba12009-07-10 15:29:03 +02003155static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3156{
3157 struct ieee80211_sub_if_data *sdata =
3158 (struct ieee80211_sub_if_data *) data;
3159 struct ieee80211_local *local = sdata->local;
3160
3161 if (local->quiescing)
3162 return;
3163
Johannes Berg682bd382013-01-29 13:13:50 +01003164 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003165 ieee80211_queue_work(&sdata->local->hw,
3166 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003167}
3168
3169static void ieee80211_sta_conn_mon_timer(unsigned long data)
3170{
3171 struct ieee80211_sub_if_data *sdata =
3172 (struct ieee80211_sub_if_data *) data;
3173 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3174 struct ieee80211_local *local = sdata->local;
3175
3176 if (local->quiescing)
3177 return;
3178
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003179 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003180}
3181
3182static void ieee80211_sta_monitor_work(struct work_struct *work)
3183{
3184 struct ieee80211_sub_if_data *sdata =
3185 container_of(work, struct ieee80211_sub_if_data,
3186 u.mgd.monitor_work);
3187
3188 ieee80211_mgd_probe_ap(sdata, false);
3189}
3190
Johannes Berga1678f82008-09-11 00:01:47 +02003191static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3192{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003193 u32 flags;
3194
Kalle Vaload935682009-04-19 08:47:19 +03003195 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003196 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003197
Johannes Bergb291ba12009-07-10 15:29:03 +02003198 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003199 flags = sdata->local->hw.flags;
3200 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3201 ieee80211_queue_work(&sdata->local->hw,
3202 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003203 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003204 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003205 }
Johannes Berga1678f82008-09-11 00:01:47 +02003206}
3207
Johannes Berg5bb644a2009-05-17 11:40:42 +02003208#ifdef CONFIG_PM
3209void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3210{
3211 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3212
3213 /*
3214 * we need to use atomic bitops for the running bits
3215 * only because both timers might fire at the same
3216 * time -- the code here is properly synchronised.
3217 */
3218
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003219 cancel_work_sync(&ifmgd->request_smps_work);
3220
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003221 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003222 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003223 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003224 if (del_timer_sync(&ifmgd->timer))
3225 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3226
3227 cancel_work_sync(&ifmgd->chswitch_work);
3228 if (del_timer_sync(&ifmgd->chswitch_timer))
3229 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003230
Johannes Bergb291ba12009-07-10 15:29:03 +02003231 /* these will just be re-established on connection */
3232 del_timer_sync(&ifmgd->conn_mon_timer);
3233 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003234}
3235
3236void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3237{
3238 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3239
Johannes Berg782d2672013-01-18 21:21:31 +01003240 mutex_lock(&ifmgd->mtx);
3241 if (!ifmgd->associated) {
3242 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303243 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003244 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303245
Johannes Berg95acac62011-07-12 12:30:59 +02003246 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3247 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003248 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3249 ieee80211_sta_connection_lost(sdata,
3250 ifmgd->associated->bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003251 WLAN_REASON_UNSPECIFIED,
3252 true);
Johannes Berg95acac62011-07-12 12:30:59 +02003253 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003254 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003255 }
Johannes Berg782d2672013-01-18 21:21:31 +01003256 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003257
Johannes Berg5bb644a2009-05-17 11:40:42 +02003258 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3259 add_timer(&ifmgd->timer);
3260 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3261 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003262 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003263
3264 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003265 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003266 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003267}
3268#endif
3269
Johannes Berg9c6bd792008-09-11 00:01:52 +02003270/* interface setup */
3271void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3272{
Johannes Berg46900292009-02-15 12:44:28 +01003273 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003274
Johannes Berg46900292009-02-15 12:44:28 +01003275 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003276 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003277 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003278 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3279 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003280 INIT_WORK(&ifmgd->csa_connection_drop_work,
3281 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003282 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003283 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003284 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003285 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3286 (unsigned long) sdata);
3287 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3288 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003289 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303290 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003291
Johannes Bergab1faea2009-07-01 21:41:17 +02003292 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303293 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003294 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3295 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003296
Johannes Berg77fdaa12009-07-07 03:45:17 +02003297 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003298
3299 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3300 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3301 else
3302 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003303}
3304
3305/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003306void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3307{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003308 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003309
3310 /* Restart STA timers */
3311 rcu_read_lock();
3312 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3313 ieee80211_restart_sta_timer(sdata);
3314 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003315}
Johannes Berg10f644a2009-04-16 13:17:25 +02003316
3317int ieee80211_max_network_latency(struct notifier_block *nb,
3318 unsigned long data, void *dummy)
3319{
3320 s32 latency_usec = (s32) data;
3321 struct ieee80211_local *local =
3322 container_of(nb, struct ieee80211_local,
3323 network_latency_notifier);
3324
3325 mutex_lock(&local->iflist_mtx);
3326 ieee80211_recalc_ps(local, latency_usec);
3327 mutex_unlock(&local->iflist_mtx);
3328
3329 return 0;
3330}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003331
Johannes Bergf2d9d272012-11-22 14:11:39 +01003332static u32 chandef_downgrade(struct cfg80211_chan_def *c)
3333{
3334 u32 ret;
3335 int tmp;
3336
3337 switch (c->width) {
3338 case NL80211_CHAN_WIDTH_20:
3339 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3340 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3341 break;
3342 case NL80211_CHAN_WIDTH_40:
3343 c->width = NL80211_CHAN_WIDTH_20;
3344 c->center_freq1 = c->chan->center_freq;
3345 ret = IEEE80211_STA_DISABLE_40MHZ |
3346 IEEE80211_STA_DISABLE_VHT;
3347 break;
3348 case NL80211_CHAN_WIDTH_80:
3349 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3350 /* n_P40 */
3351 tmp /= 2;
3352 /* freq_P40 */
3353 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3354 c->width = NL80211_CHAN_WIDTH_40;
3355 ret = IEEE80211_STA_DISABLE_VHT;
3356 break;
3357 case NL80211_CHAN_WIDTH_80P80:
3358 c->center_freq2 = 0;
3359 c->width = NL80211_CHAN_WIDTH_80;
3360 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3361 IEEE80211_STA_DISABLE_160MHZ;
3362 break;
3363 case NL80211_CHAN_WIDTH_160:
3364 /* n_P20 */
3365 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3366 /* n_P80 */
3367 tmp /= 4;
3368 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3369 c->width = NL80211_CHAN_WIDTH_80;
3370 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3371 IEEE80211_STA_DISABLE_160MHZ;
3372 break;
3373 default:
3374 case NL80211_CHAN_WIDTH_20_NOHT:
3375 WARN_ON_ONCE(1);
3376 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3377 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3378 break;
3379 }
3380
3381 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3382
3383 return ret;
3384}
3385
3386static u32
3387ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
3388 struct ieee80211_supported_band *sband,
3389 struct ieee80211_channel *channel,
3390 const struct ieee80211_ht_operation *ht_oper,
3391 const struct ieee80211_vht_operation *vht_oper,
3392 struct cfg80211_chan_def *chandef)
3393{
3394 struct cfg80211_chan_def vht_chandef;
3395 u32 ht_cfreq, ret;
3396
3397 chandef->chan = channel;
3398 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3399 chandef->center_freq1 = channel->center_freq;
3400 chandef->center_freq2 = 0;
3401
3402 if (!ht_oper || !sband->ht_cap.ht_supported) {
3403 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3404 goto out;
3405 }
3406
3407 chandef->width = NL80211_CHAN_WIDTH_20;
3408
3409 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3410 channel->band);
3411 /* check that channel matches the right operating channel */
3412 if (channel->center_freq != ht_cfreq) {
3413 /*
3414 * It's possible that some APs are confused here;
3415 * Netgear WNDR3700 sometimes reports 4 higher than
3416 * the actual channel in association responses, but
3417 * since we look at probe response/beacon data here
3418 * it should be OK.
3419 */
3420 sdata_info(sdata,
3421 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3422 channel->center_freq, ht_cfreq,
3423 ht_oper->primary_chan, channel->band);
3424 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3425 goto out;
3426 }
3427
3428 /* check 40 MHz support, if we have it */
3429 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3430 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3431 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3432 chandef->width = NL80211_CHAN_WIDTH_40;
3433 chandef->center_freq1 += 10;
3434 break;
3435 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3436 chandef->width = NL80211_CHAN_WIDTH_40;
3437 chandef->center_freq1 -= 10;
3438 break;
3439 }
3440 } else {
3441 /* 40 MHz (and 80 MHz) must be supported for VHT */
3442 ret = IEEE80211_STA_DISABLE_VHT;
3443 goto out;
3444 }
3445
3446 if (!vht_oper || !sband->vht_cap.vht_supported) {
3447 ret = IEEE80211_STA_DISABLE_VHT;
3448 goto out;
3449 }
3450
3451 vht_chandef.chan = channel;
3452 vht_chandef.center_freq1 =
3453 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
3454 channel->band);
3455 vht_chandef.center_freq2 = 0;
3456
3457 if (vht_oper->center_freq_seg2_idx)
3458 vht_chandef.center_freq2 =
3459 ieee80211_channel_to_frequency(
3460 vht_oper->center_freq_seg2_idx,
3461 channel->band);
3462
3463 switch (vht_oper->chan_width) {
3464 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3465 vht_chandef.width = chandef->width;
3466 break;
3467 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3468 vht_chandef.width = NL80211_CHAN_WIDTH_80;
3469 break;
3470 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3471 vht_chandef.width = NL80211_CHAN_WIDTH_160;
3472 break;
3473 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3474 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
3475 break;
3476 default:
3477 sdata_info(sdata,
3478 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
3479 vht_oper->chan_width);
3480 ret = IEEE80211_STA_DISABLE_VHT;
3481 goto out;
3482 }
3483
3484 if (!cfg80211_chandef_valid(&vht_chandef)) {
3485 sdata_info(sdata,
3486 "AP VHT information is invalid, disable VHT\n");
3487 ret = IEEE80211_STA_DISABLE_VHT;
3488 goto out;
3489 }
3490
3491 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
3492 ret = 0;
3493 goto out;
3494 }
3495
3496 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
3497 sdata_info(sdata,
3498 "AP VHT information doesn't match HT, disable VHT\n");
3499 ret = IEEE80211_STA_DISABLE_VHT;
3500 goto out;
3501 }
3502
3503 *chandef = vht_chandef;
3504
3505 ret = 0;
3506
Johannes Berg3d9646d2013-02-09 21:46:34 +01003507out:
Johannes Bergf2d9d272012-11-22 14:11:39 +01003508 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
3509 IEEE80211_CHAN_DISABLED)) {
3510 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
3511 ret = IEEE80211_STA_DISABLE_HT |
3512 IEEE80211_STA_DISABLE_VHT;
3513 goto out;
3514 }
3515
Johannes Berg3d9646d2013-02-09 21:46:34 +01003516 ret |= chandef_downgrade(chandef);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003517 }
3518
3519 if (chandef->width != vht_chandef.width)
3520 sdata_info(sdata,
Johannes Berg3d9646d2013-02-09 21:46:34 +01003521 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
Johannes Bergf2d9d272012-11-22 14:11:39 +01003522
Johannes Bergf2d9d272012-11-22 14:11:39 +01003523 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
3524 return ret;
3525}
3526
3527static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3528 struct cfg80211_bss *cbss)
3529{
3530 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3531 const u8 *ht_cap_ie, *vht_cap_ie;
3532 const struct ieee80211_ht_cap *ht_cap;
3533 const struct ieee80211_vht_cap *vht_cap;
3534 u8 chains = 1;
3535
3536 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3537 return chains;
3538
Johannes Berg9caf0362012-11-29 01:25:20 +01003539 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003540 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3541 ht_cap = (void *)(ht_cap_ie + 2);
3542 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3543 /*
3544 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3545 * "Tx Unequal Modulation Supported" fields.
3546 */
3547 }
3548
3549 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3550 return chains;
3551
Johannes Berg9caf0362012-11-29 01:25:20 +01003552 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003553 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3554 u8 nss;
3555 u16 tx_mcs_map;
3556
3557 vht_cap = (void *)(vht_cap_ie + 2);
3558 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3559 for (nss = 8; nss > 0; nss--) {
3560 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3561 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3562 break;
3563 }
3564 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3565 chains = max(chains, nss);
3566 }
3567
3568 return chains;
3569}
3570
Johannes Bergb17166a2012-07-27 11:41:27 +02003571static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3572 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003573{
3574 struct ieee80211_local *local = sdata->local;
3575 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003576 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003577 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003578 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003579 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003580 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003581
Johannes Berg24398e32012-03-28 10:58:36 +02003582 sband = local->hw.wiphy->bands[cbss->channel->band];
3583
Johannes Bergf2d9d272012-11-22 14:11:39 +01003584 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3585 IEEE80211_STA_DISABLE_80P80MHZ |
3586 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003587
Johannes Berg9caf0362012-11-29 01:25:20 +01003588 rcu_read_lock();
3589
Johannes Bergf2d9d272012-11-22 14:11:39 +01003590 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3591 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003592 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003593
Johannes Berg9caf0362012-11-29 01:25:20 +01003594 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003595 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3596 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003597
3598 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3599 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3600 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3601 ht_oper = NULL;
3602 }
Johannes Berg24398e32012-03-28 10:58:36 +02003603 }
3604
Johannes Bergf2d9d272012-11-22 14:11:39 +01003605 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3606 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003607 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003608
Johannes Berg9caf0362012-11-29 01:25:20 +01003609 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3610 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003611 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3612 vht_oper = (void *)(vht_oper_ie + 2);
3613 if (vht_oper && !ht_oper) {
3614 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003615 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003616 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003617 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3618 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003619 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003620
3621 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3622 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3623 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3624 vht_oper = NULL;
3625 }
Johannes Berg24398e32012-03-28 10:58:36 +02003626 }
3627
Johannes Bergf2d9d272012-11-22 14:11:39 +01003628 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3629 cbss->channel,
3630 ht_oper, vht_oper,
3631 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003632
Johannes Bergf2d9d272012-11-22 14:11:39 +01003633 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3634 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003635
Johannes Berg9caf0362012-11-29 01:25:20 +01003636 rcu_read_unlock();
3637
Johannes Berg04ecd252012-09-11 14:34:12 +02003638 /* will change later if needed */
3639 sdata->smps_mode = IEEE80211_SMPS_OFF;
3640
Johannes Bergf2d9d272012-11-22 14:11:39 +01003641 /*
3642 * If this fails (possibly due to channel context sharing
3643 * on incompatible channels, e.g. 80+80 and 160 sharing the
3644 * same control channel) try to use a smaller bandwidth.
3645 */
3646 ret = ieee80211_vif_use_channel(sdata, &chandef,
3647 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003648 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003649 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003650 ret = ieee80211_vif_use_channel(sdata, &chandef,
3651 IEEE80211_CHANCTX_SHARED);
3652 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003653 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003654}
3655
3656static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3657 struct cfg80211_bss *cbss, bool assoc)
3658{
3659 struct ieee80211_local *local = sdata->local;
3660 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3661 struct ieee80211_bss *bss = (void *)cbss->priv;
3662 struct sta_info *new_sta = NULL;
3663 bool have_sta = false;
3664 int err;
3665
3666 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3667 return -EINVAL;
3668
3669 if (assoc) {
3670 rcu_read_lock();
3671 have_sta = sta_info_get(sdata, cbss->bssid);
3672 rcu_read_unlock();
3673 }
3674
3675 if (!have_sta) {
3676 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3677 if (!new_sta)
3678 return -ENOMEM;
3679 }
3680
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003681 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003682 u32 rates = 0, basic_rates = 0;
3683 bool have_higher_than_11mbit;
3684 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003685 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003686 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003687
3688 sband = local->hw.wiphy->bands[cbss->channel->band];
3689
3690 err = ieee80211_prep_channel(sdata, cbss);
3691 if (err) {
3692 sta_info_free(local, new_sta);
3693 return err;
3694 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003695
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003696 ieee80211_get_rates(sband, bss->supp_rates,
3697 bss->supp_rates_len,
3698 &rates, &basic_rates,
3699 &have_higher_than_11mbit,
3700 &min_rate, &min_rate_index);
3701
3702 /*
3703 * This used to be a workaround for basic rates missing
3704 * in the association response frame. Now that we no
3705 * longer use the basic rates from there, it probably
3706 * doesn't happen any more, but keep the workaround so
3707 * in case some *other* APs are buggy in different ways
3708 * we can connect -- with a warning.
3709 */
3710 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003711 sdata_info(sdata,
3712 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003713 basic_rates = BIT(min_rate_index);
3714 }
3715
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003716 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003717 sdata->vif.bss_conf.basic_rates = basic_rates;
3718
3719 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003720 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003721 have_higher_than_11mbit)
3722 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3723 else
3724 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3725
3726 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3727
Johannes Berg8c358bc2012-05-22 22:13:05 +02003728 /* set timing information */
3729 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003730 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003731 ies = rcu_dereference(cbss->beacon_ies);
3732 if (ies) {
3733 const u8 *tim_ie;
3734
3735 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3736 sdata->vif.bss_conf.sync_device_ts =
3737 bss->device_ts_beacon;
3738 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3739 ies->data, ies->len);
3740 if (tim_ie && tim_ie[1] >= 2)
3741 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3742 else
3743 sdata->vif.bss_conf.sync_dtim_count = 0;
3744 } else if (!(local->hw.flags &
3745 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3746 ies = rcu_dereference(cbss->proberesp_ies);
3747 /* must be non-NULL since beacon IEs were NULL */
3748 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3749 sdata->vif.bss_conf.sync_device_ts =
3750 bss->device_ts_presp;
3751 sdata->vif.bss_conf.sync_dtim_count = 0;
3752 } else {
3753 sdata->vif.bss_conf.sync_tsf = 0;
3754 sdata->vif.bss_conf.sync_device_ts = 0;
3755 sdata->vif.bss_conf.sync_dtim_count = 0;
3756 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003757 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003758
3759 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003760 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003761 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3762 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003763
3764 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003765 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003766
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003767 err = sta_info_insert(new_sta);
3768 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003769 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003770 sdata_info(sdata,
3771 "failed to insert STA entry for the AP (error %d)\n",
3772 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003773 return err;
3774 }
3775 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003776 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003777
3778 return 0;
3779}
3780
Johannes Berg77fdaa12009-07-07 03:45:17 +02003781/* config hooks */
3782int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3783 struct cfg80211_auth_request *req)
3784{
Johannes Berg66e67e42012-01-20 13:55:27 +01003785 struct ieee80211_local *local = sdata->local;
3786 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3787 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003788 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003789 int err;
3790
3791 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003792
3793 switch (req->auth_type) {
3794 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3795 auth_alg = WLAN_AUTH_OPEN;
3796 break;
3797 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003798 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003799 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003800 auth_alg = WLAN_AUTH_SHARED_KEY;
3801 break;
3802 case NL80211_AUTHTYPE_FT:
3803 auth_alg = WLAN_AUTH_FT;
3804 break;
3805 case NL80211_AUTHTYPE_NETWORK_EAP:
3806 auth_alg = WLAN_AUTH_LEAP;
3807 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003808 case NL80211_AUTHTYPE_SAE:
3809 auth_alg = WLAN_AUTH_SAE;
3810 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003811 default:
3812 return -EOPNOTSUPP;
3813 }
3814
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003815 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3816 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003817 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003818 return -ENOMEM;
3819
Johannes Berg66e67e42012-01-20 13:55:27 +01003820 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003821
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003822 if (req->sae_data_len >= 4) {
3823 __le16 *pos = (__le16 *) req->sae_data;
3824 auth_data->sae_trans = le16_to_cpu(pos[0]);
3825 auth_data->sae_status = le16_to_cpu(pos[1]);
3826 memcpy(auth_data->data, req->sae_data + 4,
3827 req->sae_data_len - 4);
3828 auth_data->data_len += req->sae_data_len - 4;
3829 }
3830
Johannes Berg77fdaa12009-07-07 03:45:17 +02003831 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003832 memcpy(&auth_data->data[auth_data->data_len],
3833 req->ie, req->ie_len);
3834 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003835 }
3836
Johannes Bergfffd0932009-07-08 14:22:54 +02003837 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003838 auth_data->key_len = req->key_len;
3839 auth_data->key_idx = req->key_idx;
3840 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003841 }
3842
Johannes Berg66e67e42012-01-20 13:55:27 +01003843 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003844
Johannes Berg66e67e42012-01-20 13:55:27 +01003845 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003846
Johannes Berg66e67e42012-01-20 13:55:27 +01003847 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003848
Johannes Berg66e67e42012-01-20 13:55:27 +01003849 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3850 ifmgd->assoc_data) {
3851 err = -EBUSY;
3852 goto err_free;
3853 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003854
Johannes Berg66e67e42012-01-20 13:55:27 +01003855 if (ifmgd->auth_data)
3856 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003857
Johannes Berg66e67e42012-01-20 13:55:27 +01003858 /* prep auth_data so we don't go into idle on disassoc */
3859 ifmgd->auth_data = auth_data;
3860
3861 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003862 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003863
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003864 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003865
Johannes Berga1cf7752012-03-08 15:02:07 +01003866 err = ieee80211_prep_connection(sdata, req->bss, false);
3867 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003868 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003869
Johannes Berg66e67e42012-01-20 13:55:27 +01003870 err = ieee80211_probe_auth(sdata);
3871 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003872 sta_info_destroy_addr(sdata, req->bss->bssid);
3873 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003874 }
3875
Johannes Berg66e67e42012-01-20 13:55:27 +01003876 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003877 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003878 err = 0;
3879 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003880
Johannes Berg66e67e42012-01-20 13:55:27 +01003881 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003882 memset(ifmgd->bssid, 0, ETH_ALEN);
3883 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003884 ifmgd->auth_data = NULL;
3885 err_free:
3886 kfree(auth_data);
3887 out_unlock:
3888 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003889
Johannes Berg66e67e42012-01-20 13:55:27 +01003890 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003891}
3892
Johannes Berg77fdaa12009-07-07 03:45:17 +02003893int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3894 struct cfg80211_assoc_request *req)
3895{
Johannes Berg66e67e42012-01-20 13:55:27 +01003896 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003897 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003898 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003899 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003900 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003901 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003902 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003903 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003904
Johannes Berg66e67e42012-01-20 13:55:27 +01003905 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3906 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003907 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003908
Johannes Berg9caf0362012-11-29 01:25:20 +01003909 rcu_read_lock();
3910 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3911 if (!ssidie) {
3912 rcu_read_unlock();
3913 kfree(assoc_data);
3914 return -EINVAL;
3915 }
3916 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3917 assoc_data->ssid_len = ssidie[1];
3918 rcu_read_unlock();
3919
Johannes Berg66e67e42012-01-20 13:55:27 +01003920 mutex_lock(&ifmgd->mtx);
3921
3922 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003923 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003924
3925 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3926 err = -EBUSY;
3927 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003928 }
3929
Johannes Berg66e67e42012-01-20 13:55:27 +01003930 if (ifmgd->assoc_data) {
3931 err = -EBUSY;
3932 goto err_free;
3933 }
3934
3935 if (ifmgd->auth_data) {
3936 bool match;
3937
3938 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003939 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003940 ieee80211_destroy_auth_data(sdata, match);
3941 }
3942
3943 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003944
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003945 ifmgd->beacon_crc_valid = false;
3946
Johannes Bergde5036a2012-03-08 15:02:03 +01003947 /*
3948 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3949 * We still associate in non-HT mode (11a/b/g) if any one of these
3950 * ciphers is configured as pairwise.
3951 * We can set this to true for non-11n hardware, that'll be checked
3952 * separately along with the peer capabilities.
3953 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003954 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003955 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3956 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003957 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003958 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003959 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003960 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003961 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003962 }
3963 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003964
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003965 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003966 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003967 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3968 }
Ben Greearef96a8422011-11-18 11:32:00 -08003969
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003970 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3971 sband = local->hw.wiphy->bands[req->bss->channel->band];
3972 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003973 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003974 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003975 if (!bss->wmm_used)
3976 netdev_info(sdata->dev,
3977 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003978 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003979
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003980 /* disable VHT if we don't support it or the AP doesn't use WMM */
3981 if (!sband->vht_cap.vht_supported ||
3982 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3983 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003984 if (!bss->wmm_used)
3985 netdev_info(sdata->dev,
3986 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003987 }
3988
Ben Greearef96a8422011-11-18 11:32:00 -08003989 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3990 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3991 sizeof(ifmgd->ht_capa_mask));
3992
Johannes Berg77fdaa12009-07-07 03:45:17 +02003993 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003994 memcpy(assoc_data->ie, req->ie, req->ie_len);
3995 assoc_data->ie_len = req->ie_len;
3996 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003997
Johannes Berg66e67e42012-01-20 13:55:27 +01003998 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003999
Johannes Bergaf6b6372009-12-23 13:15:35 +01004000 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4001 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004002 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004003 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004004 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004005 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004006 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004007
Johannes Berg66e67e42012-01-20 13:55:27 +01004008 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004009 assoc_data->wmm = bss->wmm_used &&
4010 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004011 assoc_data->supp_rates = bss->supp_rates;
4012 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004013
Johannes Berg9caf0362012-11-29 01:25:20 +01004014 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004015 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4016 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4017 assoc_data->ap_ht_param =
4018 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4019 else
Johannes Berga8243b72012-11-22 14:32:09 +01004020 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004021 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4022 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4023 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4024 sizeof(struct ieee80211_vht_cap));
4025 else
4026 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004027 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004028
Kalle Valoab133152010-01-12 10:42:31 +02004029 if (bss->wmm_used && bss->uapsd_supported &&
4030 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01004031 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004032 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4033 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004034 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004035 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4036 }
4037
Johannes Berg77fdaa12009-07-07 03:45:17 +02004038 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004039 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004040
4041 if (req->use_mfp) {
4042 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4043 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4044 } else {
4045 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4046 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4047 }
4048
4049 if (req->crypto.control_port)
4050 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4051 else
4052 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4053
Johannes Berga621fa42010-08-27 14:26:54 +03004054 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4055 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4056
Johannes Berg66e67e42012-01-20 13:55:27 +01004057 /* kick off associate process */
4058
4059 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004060 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004061
Johannes Berga1cf7752012-03-08 15:02:07 +01004062 err = ieee80211_prep_connection(sdata, req->bss, true);
4063 if (err)
4064 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004065
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004066 rcu_read_lock();
4067 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004068
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004069 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4070 !beacon_ies) {
4071 /*
4072 * Wait up to one beacon interval ...
4073 * should this be more if we miss one?
4074 */
4075 sdata_info(sdata, "waiting for beacon from %pM\n",
4076 ifmgd->bssid);
4077 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004078 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004079 assoc_data->need_beacon = true;
4080 } else if (beacon_ies) {
4081 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4082 beacon_ies->data,
4083 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004084 u8 dtim_count = 0;
4085
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004086 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4087 const struct ieee80211_tim_ie *tim;
4088 tim = (void *)(tim_ie + 2);
4089 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004090 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004091 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004092 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004093 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004094 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004095
4096 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4097 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4098 sdata->vif.bss_conf.sync_device_ts =
4099 bss->device_ts_beacon;
4100 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4101 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004102 } else {
4103 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004104 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004105 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004106 rcu_read_unlock();
4107
Johannes Berg66e67e42012-01-20 13:55:27 +01004108 run_again(ifmgd, assoc_data->timeout);
4109
Paul Stewartfcff4f12012-02-23 17:59:53 -08004110 if (bss->corrupt_data) {
4111 char *corrupt_type = "data";
4112 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4113 if (bss->corrupt_data &
4114 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4115 corrupt_type = "beacon and probe response";
4116 else
4117 corrupt_type = "beacon";
4118 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4119 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004120 sdata_info(sdata, "associating with AP with corrupt %s\n",
4121 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004122 }
4123
Johannes Berg66e67e42012-01-20 13:55:27 +01004124 err = 0;
4125 goto out;
4126 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004127 memset(ifmgd->bssid, 0, ETH_ALEN);
4128 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004129 ifmgd->assoc_data = NULL;
4130 err_free:
4131 kfree(assoc_data);
4132 out:
4133 mutex_unlock(&ifmgd->mtx);
4134
4135 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004136}
4137
4138int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004139 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004140{
4141 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004142 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004143 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004144 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004145
Johannes Berg77fdaa12009-07-07 03:45:17 +02004146 mutex_lock(&ifmgd->mtx);
4147
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004148 sdata_info(sdata,
4149 "deauthenticating from %pM by local choice (reason=%d)\n",
4150 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004151
Johannes Berg86552012012-10-29 09:46:31 +01004152 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004153 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004154 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4155 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004156 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004157 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004158 ieee80211_destroy_auth_data(sdata, false);
4159 mutex_unlock(&ifmgd->mtx);
4160
4161 sent_frame = tx;
4162 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004163 }
4164
Johannes Berg86552012012-10-29 09:46:31 +01004165 if (ifmgd->associated &&
4166 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4167 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4168 req->reason_code, tx, frame_buf);
4169 sent_frame = tx;
4170 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004171 mutex_unlock(&ifmgd->mtx);
4172
Johannes Berg86552012012-10-29 09:46:31 +01004173 out:
Johannes Berg86552012012-10-29 09:46:31 +01004174 if (sent_frame)
4175 __cfg80211_send_deauth(sdata->dev, frame_buf,
4176 IEEE80211_DEAUTH_FRAME_LEN);
4177
Johannes Berg77fdaa12009-07-07 03:45:17 +02004178 return 0;
4179}
4180
4181int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004182 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004183{
4184 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004185 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004186 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004187
Johannes Berg77fdaa12009-07-07 03:45:17 +02004188 mutex_lock(&ifmgd->mtx);
4189
Johannes Berg8d8b2612009-07-25 11:58:36 +02004190 /*
4191 * cfg80211 should catch this ... but it's racy since
4192 * we can receive a disassoc frame, process it, hand it
4193 * to cfg80211 while that's in a locked section already
4194 * trying to tell us that the user wants to disconnect.
4195 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004196 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004197 mutex_unlock(&ifmgd->mtx);
4198 return -ENOLINK;
4199 }
4200
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004201 sdata_info(sdata,
4202 "disassociating from %pM by local choice (reason=%d)\n",
4203 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004204
Jouni Malinene69e95d2010-03-29 23:29:31 -07004205 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004206 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4207 req->reason_code, !req->local_state_change,
4208 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004209 mutex_unlock(&ifmgd->mtx);
4210
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004211 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4212 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004213
Johannes Berg77fdaa12009-07-07 03:45:17 +02004214 return 0;
4215}
Jouni Malinen026331c2010-02-15 12:53:10 +02004216
Eliad Pellerafa762f2012-04-23 14:45:15 +03004217void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004218{
4219 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4220
4221 mutex_lock(&ifmgd->mtx);
4222 if (ifmgd->assoc_data)
4223 ieee80211_destroy_assoc_data(sdata, false);
4224 if (ifmgd->auth_data)
4225 ieee80211_destroy_auth_data(sdata, false);
4226 del_timer_sync(&ifmgd->timer);
4227 mutex_unlock(&ifmgd->mtx);
4228}
4229
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004230void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4231 enum nl80211_cqm_rssi_threshold_event rssi_event,
4232 gfp_t gfp)
4233{
4234 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4235
Johannes Bergb5878a22010-04-07 16:48:40 +02004236 trace_api_cqm_rssi_notify(sdata, rssi_event);
4237
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004238 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4239}
4240EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);