blob: a5dba67bbe0b431eb0efce617bd4004b4d45b3cc [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg66e67e42012-01-20 13:55:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_MAX_TRIES 3
35#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
36#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
37#define IEEE80211_ASSOC_MAX_TRIES 3
38
Ben Greear180205b2011-02-04 15:30:24 -080039static int max_nullfunc_tries = 2;
40module_param(max_nullfunc_tries, int, 0644);
41MODULE_PARM_DESC(max_nullfunc_tries,
42 "Maximum nullfunc tx tries before disconnecting (reason 4).");
43
44static int max_probe_tries = 5;
45module_param(max_probe_tries, int, 0644);
46MODULE_PARM_DESC(max_probe_tries,
47 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020048
49/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010050 * Beacon loss timeout is calculated as N frames times the
51 * advertised beacon interval. This may need to be somewhat
52 * higher than what hardware might detect to account for
53 * delays in the host processing frames. But since we also
54 * probe on beacon miss before declaring the connection lost
55 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020056 */
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010057#define IEEE80211_BEACON_LOSS_COUNT 7
58
Johannes Bergb291ba12009-07-10 15:29:03 +020059/*
60 * Time the connection can be idle before we probe
61 * it to see if we can still talk to the AP.
62 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030063#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time we wait for a probe response after sending
66 * a probe request because of beacon loss or for
67 * checking the connection still works.
68 */
Ben Greear180205b2011-02-04 15:30:24 -080069static int probe_wait_ms = 500;
70module_param(probe_wait_ms, int, 0644);
71MODULE_PARM_DESC(probe_wait_ms,
72 "Maximum time(ms) to wait for probe response"
73 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070074
Jouni Malinen17e4ec12010-03-29 23:28:30 -070075/*
76 * Weight given to the latest Beacon frame when calculating average signal
77 * strength for Beacon frames received in the current BSS. This must be
78 * between 1 and 15.
79 */
80#define IEEE80211_SIGNAL_AVE_WEIGHT 3
81
Jouni Malinen391a2002010-08-27 22:22:00 +030082/*
83 * How many Beacon frames need to have been used in average signal strength
84 * before starting to indicate signal change events.
85 */
86#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
87
Johannes Berg5bb644a2009-05-17 11:40:42 +020088#define TMR_RUNNING_TIMER 0
89#define TMR_RUNNING_CHANSW 1
90
Johannes Berg77fdaa12009-07-07 03:45:17 +020091/*
92 * All cfg80211 functions have to be called outside a locked
93 * section so that they can acquire a lock themselves... This
94 * is much simpler than queuing up things in cfg80211, but we
95 * do need some indirection for that here.
96 */
97enum rx_mgmt_action {
98 /* no action required */
99 RX_MGMT_NONE,
100
Johannes Berg77fdaa12009-07-07 03:45:17 +0200101 /* caller must call cfg80211_send_deauth() */
102 RX_MGMT_CFG80211_DEAUTH,
103
104 /* caller must call cfg80211_send_disassoc() */
105 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100106
107 /* caller must call cfg80211_send_rx_auth() */
108 RX_MGMT_CFG80211_RX_AUTH,
109
110 /* caller must call cfg80211_send_rx_assoc() */
111 RX_MGMT_CFG80211_RX_ASSOC,
112
113 /* caller must call cfg80211_send_assoc_timeout() */
114 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200115};
116
Johannes Berg5484e232008-09-08 17:44:27 +0200117/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200118static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
119{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200120 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200121}
122
Johannes Bergca386f32009-07-10 02:39:48 +0200123/*
124 * We can have multiple work items (and connection probing)
125 * scheduling this timer, but we need to take care to only
126 * reschedule it when it should fire _earlier_ than it was
127 * asked for before, or if it's not pending right now. This
128 * function ensures that. Note that it then is required to
129 * run this function for all timeouts after the first one
130 * has happened -- the work that runs from this timer will
131 * do that.
132 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100133static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200134{
135 ASSERT_MGD_MTX(ifmgd);
136
137 if (!timer_pending(&ifmgd->timer) ||
138 time_before(timeout, ifmgd->timer.expires))
139 mod_timer(&ifmgd->timer, timeout);
140}
141
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400142void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200143{
Johannes Bergc1288b12012-01-19 09:29:57 +0100144 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200145 return;
146
Johannes Berg7eeff742012-07-18 10:27:27 +0200147 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
148 return;
149
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100151 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200152}
153
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400154void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
155{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
157
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100158 if (unlikely(!sdata->u.mgd.associated))
159 return;
160
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400161 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
162 return;
163
164 mod_timer(&sdata->u.mgd.conn_mon_timer,
165 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400166
167 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400168}
169
Johannes Berg5484e232008-09-08 17:44:27 +0200170static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200171{
Johannes Berg5484e232008-09-08 17:44:27 +0200172 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200173}
174
Johannes Berg24398e32012-03-28 10:58:36 +0200175static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
176 struct ieee80211_ht_operation *ht_oper,
177 const u8 *bssid, bool reconfig)
Johannes Bergd5522e02009-03-30 13:23:35 +0200178{
179 struct ieee80211_local *local = sdata->local;
180 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200181 struct ieee80211_chanctx_conf *chanctx_conf;
182 struct ieee80211_channel *chan;
Johannes Bergd5522e02009-03-30 13:23:35 +0200183 struct sta_info *sta;
184 u32 changed = 0;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200185 u16 ht_opmode;
Johannes Berg64f68e52012-03-28 10:58:37 +0200186 bool disable_40 = false;
Johannes Bergd5522e02009-03-30 13:23:35 +0200187
Johannes Berg55de9082012-07-26 17:24:39 +0200188 rcu_read_lock();
189 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
190 if (WARN_ON(!chanctx_conf)) {
191 rcu_read_unlock();
192 return 0;
193 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100194 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200195 rcu_read_unlock();
196 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200197
Johannes Berg4bf88532012-11-09 11:39:59 +0100198 switch (sdata->vif.bss_conf.chandef.width) {
199 case NL80211_CHAN_WIDTH_40:
200 if (sdata->vif.bss_conf.chandef.chan->center_freq >
201 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100202 chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200203 disable_40 = true;
Johannes Berg4bf88532012-11-09 11:39:59 +0100204 if (sdata->vif.bss_conf.chandef.chan->center_freq <
205 sdata->vif.bss_conf.chandef.center_freq1 &&
Johannes Berg75e69342013-01-11 12:32:37 +0100206 chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
Johannes Berg64f68e52012-03-28 10:58:37 +0200207 disable_40 = true;
208 break;
209 default:
210 break;
211 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200212
Johannes Berg24398e32012-03-28 10:58:36 +0200213 /* This can change during the lifetime of the BSS */
214 if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
Johannes Berg64f68e52012-03-28 10:58:37 +0200215 disable_40 = true;
Johannes Bergd5522e02009-03-30 13:23:35 +0200216
Johannes Berg64f68e52012-03-28 10:58:37 +0200217 mutex_lock(&local->sta_mtx);
218 sta = sta_info_get(sdata, bssid);
219
220 WARN_ON_ONCE(!sta);
221
222 if (sta && !sta->supports_40mhz)
223 disable_40 = true;
224
225 if (sta && (!reconfig ||
Rajkumar Manoharan91a00992012-04-25 20:24:24 +0530226 (disable_40 != !(sta->sta.ht_cap.cap &
Johannes Berg64f68e52012-03-28 10:58:37 +0200227 IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530228
Johannes Berg64f68e52012-03-28 10:58:37 +0200229 if (disable_40)
230 sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
231 else
232 sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530233
Johannes Berg64f68e52012-03-28 10:58:37 +0200234 rate_control_rate_update(local, sband, sta,
Johannes Berg8f727ef2012-03-30 08:43:32 +0200235 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200236 }
Johannes Berg64f68e52012-03-28 10:58:37 +0200237 mutex_unlock(&local->sta_mtx);
Johannes Bergd5522e02009-03-30 13:23:35 +0200238
Johannes Berg074d46d2012-03-15 19:45:16 +0100239 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200240
241 /* if bss configuration changed store the new one */
Johannes Berg24398e32012-03-28 10:58:36 +0200242 if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
Johannes Bergd5522e02009-03-30 13:23:35 +0200243 changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200244 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200245 }
246
247 return changed;
248}
249
Johannes Berg9c6bd792008-09-11 00:01:52 +0200250/* frame sending functions */
251
Johannes Berg66e67e42012-01-20 13:55:27 +0100252static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
253 struct ieee80211_supported_band *sband,
254 u32 *rates)
255{
256 int i, j, count;
257 *rates = 0;
258 count = 0;
259 for (i = 0; i < supp_rates_len; i++) {
260 int rate = (supp_rates[i] & 0x7F) * 5;
261
262 for (j = 0; j < sband->n_bitrates; j++)
263 if (sband->bitrates[j].bitrate == rate) {
264 *rates |= BIT(j);
265 count++;
266 break;
267 }
268 }
269
270 return count;
271}
272
273static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200274 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100275 struct ieee80211_supported_band *sband,
276 struct ieee80211_channel *channel,
277 enum ieee80211_smps_mode smps)
278{
Johannes Berg66e67e42012-01-20 13:55:27 +0100279 u8 *pos;
280 u32 flags = channel->flags;
281 u16 cap;
282 struct ieee80211_sta_ht_cap ht_cap;
283
284 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
285
Johannes Berg66e67e42012-01-20 13:55:27 +0100286 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
287 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
288
Johannes Berg66e67e42012-01-20 13:55:27 +0100289 /* determine capability flags */
290 cap = ht_cap.cap;
291
Johannes Berg9dde6422012-05-16 23:43:19 +0200292 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100293 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
294 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
295 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
296 cap &= ~IEEE80211_HT_CAP_SGI_40;
297 }
298 break;
299 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
300 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
301 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
302 cap &= ~IEEE80211_HT_CAP_SGI_40;
303 }
304 break;
305 }
306
Johannes Berg24398e32012-03-28 10:58:36 +0200307 /*
308 * If 40 MHz was disabled associate as though we weren't
309 * capable of 40 MHz -- some broken APs will never fall
310 * back to trying to transmit in 20 MHz.
311 */
312 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
313 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
314 cap &= ~IEEE80211_HT_CAP_SGI_40;
315 }
316
Johannes Berg66e67e42012-01-20 13:55:27 +0100317 /* set SM PS mode properly */
318 cap &= ~IEEE80211_HT_CAP_SM_PS;
319 switch (smps) {
320 case IEEE80211_SMPS_AUTOMATIC:
321 case IEEE80211_SMPS_NUM_MODES:
322 WARN_ON(1);
323 case IEEE80211_SMPS_OFF:
324 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
325 IEEE80211_HT_CAP_SM_PS_SHIFT;
326 break;
327 case IEEE80211_SMPS_STATIC:
328 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
329 IEEE80211_HT_CAP_SM_PS_SHIFT;
330 break;
331 case IEEE80211_SMPS_DYNAMIC:
332 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
333 IEEE80211_HT_CAP_SM_PS_SHIFT;
334 break;
335 }
336
337 /* reserve and fill IE */
338 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
339 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
340}
341
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000342static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
343 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100344 struct ieee80211_supported_band *sband,
345 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000346{
347 u8 *pos;
348 u32 cap;
349 struct ieee80211_sta_vht_cap vht_cap;
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100350 int i;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000351
352 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
353
354 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
355
356 /* determine capability flags */
357 cap = vht_cap.cap;
358
Johannes Bergf2d9d272012-11-22 14:11:39 +0100359 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
360 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
361 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
362 }
363
364 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
365 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
366 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
367 }
368
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100369 /*
370 * Some APs apparently get confused if our capabilities are better
371 * than theirs, so restrict what we advertise in the assoc request.
372 */
373 if (!(ap_vht_cap->vht_cap_info &
374 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
375 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
376
377 if (!(ap_vht_cap->vht_cap_info &
378 cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
379 cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
380 IEEE80211_VHT_CAP_RXSTBC_3 |
381 IEEE80211_VHT_CAP_RXSTBC_4);
382
383 for (i = 0; i < 8; i++) {
384 int shift = i * 2;
385 u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
386 u16 ap_mcs, our_mcs;
387
388 ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
389 mask) >> shift;
390 our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
391 mask) >> shift;
392
393 switch (ap_mcs) {
394 default:
395 if (our_mcs <= ap_mcs)
396 break;
397 /* fall through */
398 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
399 vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
400 vht_cap.vht_mcs.rx_mcs_map |=
401 cpu_to_le16(ap_mcs << shift);
402 }
403 }
404
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000405 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000406 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000407 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
408}
409
Johannes Berg66e67e42012-01-20 13:55:27 +0100410static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
411{
412 struct ieee80211_local *local = sdata->local;
413 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
414 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
415 struct sk_buff *skb;
416 struct ieee80211_mgmt *mgmt;
417 u8 *pos, qos_info;
418 size_t offset = 0, noffset;
419 int i, count, rates_len, supp_rates_len;
420 u16 capab;
421 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200422 struct ieee80211_chanctx_conf *chanctx_conf;
423 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100424 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100425
426 lockdep_assert_held(&ifmgd->mtx);
427
Johannes Berg55de9082012-07-26 17:24:39 +0200428 rcu_read_lock();
429 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
430 if (WARN_ON(!chanctx_conf)) {
431 rcu_read_unlock();
432 return;
433 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100434 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200435 rcu_read_unlock();
436 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100437
438 if (assoc_data->supp_rates_len) {
439 /*
440 * Get all rates supported by the device and the AP as
441 * some APs don't like getting a superset of their rates
442 * in the association request (e.g. D-Link DAP 1353 in
443 * b-only mode)...
444 */
445 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
446 assoc_data->supp_rates_len,
447 sband, &rates);
448 } else {
449 /*
450 * In case AP not provide any supported rates information
451 * before association, we send information element(s) with
452 * all rates that we support.
453 */
454 rates = ~0;
455 rates_len = sband->n_bitrates;
456 }
457
458 skb = alloc_skb(local->hw.extra_tx_headroom +
459 sizeof(*mgmt) + /* bit too much but doesn't matter */
460 2 + assoc_data->ssid_len + /* SSID */
461 4 + rates_len + /* (extended) rates */
462 4 + /* power capability */
463 2 + 2 * sband->n_channels + /* supported channels */
464 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000465 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100466 assoc_data->ie_len + /* extra IEs */
467 9, /* WMM */
468 GFP_KERNEL);
469 if (!skb)
470 return;
471
472 skb_reserve(skb, local->hw.extra_tx_headroom);
473
474 capab = WLAN_CAPABILITY_ESS;
475
476 if (sband->band == IEEE80211_BAND_2GHZ) {
477 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
478 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
479 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
480 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
481 }
482
483 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
484 capab |= WLAN_CAPABILITY_PRIVACY;
485
486 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
487 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
488 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
489
490 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
491 memset(mgmt, 0, 24);
492 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
493 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
494 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
495
496 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
497 skb_put(skb, 10);
498 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
499 IEEE80211_STYPE_REASSOC_REQ);
500 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
501 mgmt->u.reassoc_req.listen_interval =
502 cpu_to_le16(local->hw.conf.listen_interval);
503 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
504 ETH_ALEN);
505 } else {
506 skb_put(skb, 4);
507 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
508 IEEE80211_STYPE_ASSOC_REQ);
509 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
510 mgmt->u.assoc_req.listen_interval =
511 cpu_to_le16(local->hw.conf.listen_interval);
512 }
513
514 /* SSID */
515 pos = skb_put(skb, 2 + assoc_data->ssid_len);
516 *pos++ = WLAN_EID_SSID;
517 *pos++ = assoc_data->ssid_len;
518 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
519
520 /* add all rates which were marked to be used above */
521 supp_rates_len = rates_len;
522 if (supp_rates_len > 8)
523 supp_rates_len = 8;
524
525 pos = skb_put(skb, supp_rates_len + 2);
526 *pos++ = WLAN_EID_SUPP_RATES;
527 *pos++ = supp_rates_len;
528
529 count = 0;
530 for (i = 0; i < sband->n_bitrates; i++) {
531 if (BIT(i) & rates) {
532 int rate = sband->bitrates[i].bitrate;
533 *pos++ = (u8) (rate / 5);
534 if (++count == 8)
535 break;
536 }
537 }
538
539 if (rates_len > count) {
540 pos = skb_put(skb, rates_len - count + 2);
541 *pos++ = WLAN_EID_EXT_SUPP_RATES;
542 *pos++ = rates_len - count;
543
544 for (i++; i < sband->n_bitrates; i++) {
545 if (BIT(i) & rates) {
546 int rate = sband->bitrates[i].bitrate;
547 *pos++ = (u8) (rate / 5);
548 }
549 }
550 }
551
552 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
553 /* 1. power capabilities */
554 pos = skb_put(skb, 4);
555 *pos++ = WLAN_EID_PWR_CAPABILITY;
556 *pos++ = 2;
557 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200558 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100559
560 /* 2. supported channels */
561 /* TODO: get this in reg domain format */
562 pos = skb_put(skb, 2 * sband->n_channels + 2);
563 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
564 *pos++ = 2 * sband->n_channels;
565 for (i = 0; i < sband->n_channels; i++) {
566 *pos++ = ieee80211_frequency_to_channel(
567 sband->channels[i].center_freq);
568 *pos++ = 1; /* one channel in the subband*/
569 }
570 }
571
572 /* if present, add any custom IEs that go before HT */
573 if (assoc_data->ie_len && assoc_data->ie) {
574 static const u8 before_ht[] = {
575 WLAN_EID_SSID,
576 WLAN_EID_SUPP_RATES,
577 WLAN_EID_EXT_SUPP_RATES,
578 WLAN_EID_PWR_CAPABILITY,
579 WLAN_EID_SUPPORTED_CHANNELS,
580 WLAN_EID_RSN,
581 WLAN_EID_QOS_CAPA,
582 WLAN_EID_RRM_ENABLED_CAPABILITIES,
583 WLAN_EID_MOBILITY_DOMAIN,
584 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
585 };
586 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
587 before_ht, ARRAY_SIZE(before_ht),
588 offset);
589 pos = skb_put(skb, noffset - offset);
590 memcpy(pos, assoc_data->ie + offset, noffset - offset);
591 offset = noffset;
592 }
593
Johannes Bergf2d9d272012-11-22 14:11:39 +0100594 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
595 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
596 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
597
Johannes Berga8243b72012-11-22 14:32:09 +0100598 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200599 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200600 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100601
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000602 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100603 ieee80211_add_vht_ie(sdata, skb, sband,
604 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000605
Johannes Berg66e67e42012-01-20 13:55:27 +0100606 /* if present, add any custom non-vendor IEs that go after HT */
607 if (assoc_data->ie_len && assoc_data->ie) {
608 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
609 assoc_data->ie_len,
610 offset);
611 pos = skb_put(skb, noffset - offset);
612 memcpy(pos, assoc_data->ie + offset, noffset - offset);
613 offset = noffset;
614 }
615
Johannes Berg76f03032012-03-08 15:02:05 +0100616 if (assoc_data->wmm) {
617 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200618 qos_info = ifmgd->uapsd_queues;
619 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100620 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
621 } else {
622 qos_info = 0;
623 }
624
625 pos = skb_put(skb, 9);
626 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
627 *pos++ = 7; /* len */
628 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
629 *pos++ = 0x50;
630 *pos++ = 0xf2;
631 *pos++ = 2; /* WME */
632 *pos++ = 0; /* WME info */
633 *pos++ = 1; /* WME ver */
634 *pos++ = qos_info;
635 }
636
637 /* add any remaining custom (i.e. vendor specific here) IEs */
638 if (assoc_data->ie_len && assoc_data->ie) {
639 noffset = assoc_data->ie_len;
640 pos = skb_put(skb, noffset - offset);
641 memcpy(pos, assoc_data->ie + offset, noffset - offset);
642 }
643
Johannes Berga1845fc2012-06-27 13:18:36 +0200644 drv_mgd_prepare_tx(local, sdata);
645
Johannes Berg66e67e42012-01-20 13:55:27 +0100646 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
647 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
Johannes Berg62ae67b2009-11-18 18:42:05 +0100683 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530684 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
685 IEEE80211_STA_CONNECTION_POLL))
686 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
687
Johannes Berg62ae67b2009-11-18 18:42:05 +0100688 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200689}
690
Felix Fietkaud5242152010-01-08 18:06:26 +0100691static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
692 struct ieee80211_sub_if_data *sdata)
693{
694 struct sk_buff *skb;
695 struct ieee80211_hdr *nullfunc;
696 __le16 fc;
697
698 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
699 return;
700
701 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700702 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100703 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700704
Felix Fietkaud5242152010-01-08 18:06:26 +0100705 skb_reserve(skb, local->hw.extra_tx_headroom);
706
707 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
708 memset(nullfunc, 0, 30);
709 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
710 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
711 nullfunc->frame_control = fc;
712 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
713 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
714 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
715 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
716
717 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
718 ieee80211_tx_skb(sdata, skb);
719}
720
Johannes Bergcc32abd2009-05-15 11:52:31 +0200721/* spectrum management related things */
722static void ieee80211_chswitch_work(struct work_struct *work)
723{
724 struct ieee80211_sub_if_data *sdata =
725 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200726 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
727
Johannes Berg9607e6b2009-12-23 13:15:31 +0100728 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200729 return;
730
Johannes Berg77fdaa12009-07-07 03:45:17 +0200731 mutex_lock(&ifmgd->mtx);
732 if (!ifmgd->associated)
733 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200734
Johannes Berg55de9082012-07-26 17:24:39 +0200735 sdata->local->_oper_channel = sdata->local->csa_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200736 if (!sdata->local->ops->channel_switch) {
737 /* call "hw_config" only if doing sw channel switch */
738 ieee80211_hw_config(sdata->local,
739 IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300740 } else {
741 /* update the device channel directly */
Johannes Berg55de9082012-07-26 17:24:39 +0200742 sdata->local->hw.conf.channel = sdata->local->_oper_channel;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200743 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200744
Johannes Berg77fdaa12009-07-07 03:45:17 +0200745 /* XXX: shouldn't really modify cfg80211-owned data! */
Johannes Berg55de9082012-07-26 17:24:39 +0200746 ifmgd->associated->channel = sdata->local->_oper_channel;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200747
Johannes Berg57eebdf2012-08-01 15:50:46 +0200748 /* XXX: wait for a beacon first? */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200749 ieee80211_wake_queues_by_reason(&sdata->local->hw,
750 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200751 out:
752 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
753 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200754}
755
Johannes Berg5ce6e432010-05-11 16:20:57 +0200756void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
757{
Johannes Berg55de9082012-07-26 17:24:39 +0200758 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
759 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200760
761 trace_api_chswitch_done(sdata, success);
762 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200763 sdata_info(sdata,
764 "driver channel switch failed, disconnecting\n");
765 ieee80211_queue_work(&sdata->local->hw,
766 &ifmgd->csa_connection_drop_work);
767 } else {
768 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200769 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200770}
771EXPORT_SYMBOL(ieee80211_chswitch_done);
772
Johannes Bergcc32abd2009-05-15 11:52:31 +0200773static void ieee80211_chswitch_timer(unsigned long data)
774{
775 struct ieee80211_sub_if_data *sdata =
776 (struct ieee80211_sub_if_data *) data;
777 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
778
Johannes Berg5bb644a2009-05-17 11:40:42 +0200779 if (sdata->local->quiescing) {
780 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
781 return;
782 }
783
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400784 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200785}
786
787void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
788 struct ieee80211_channel_sw_ie *sw_elem,
Johannes Berg5ce6e432010-05-11 16:20:57 +0200789 struct ieee80211_bss *bss,
790 u64 timestamp)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200791{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100792 struct cfg80211_bss *cbss =
793 container_of((void *)bss, struct cfg80211_bss, priv);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200794 struct ieee80211_channel *new_ch;
795 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900796 int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
797 cbss->channel->band);
Johannes Berg55de9082012-07-26 17:24:39 +0200798 struct ieee80211_chanctx *chanctx;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200799
Johannes Berg77fdaa12009-07-07 03:45:17 +0200800 ASSERT_MGD_MTX(ifmgd);
801
802 if (!ifmgd->associated)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200803 return;
804
Helmut Schaafbe9c422009-07-23 12:14:04 +0200805 if (sdata->local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200806 return;
807
808 /* Disregard subsequent beacons if we are already running a timer
809 processing a CSA */
810
811 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
812 return;
813
814 new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
Johannes Berg882a7c62012-08-01 22:32:45 +0200815 if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
816 sdata_info(sdata,
817 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
818 ifmgd->associated->bssid, new_freq);
819 ieee80211_queue_work(&sdata->local->hw,
820 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200821 return;
Johannes Berg882a7c62012-08-01 22:32:45 +0200822 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200823
Johannes Berg57eebdf2012-08-01 15:50:46 +0200824 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
825
Johannes Berg55de9082012-07-26 17:24:39 +0200826 if (sdata->local->use_chanctx) {
827 sdata_info(sdata,
828 "not handling channel switch with channel contexts\n");
829 ieee80211_queue_work(&sdata->local->hw,
830 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +0100831 return;
Johannes Berg55de9082012-07-26 17:24:39 +0200832 }
833
834 mutex_lock(&sdata->local->chanctx_mtx);
835 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
836 mutex_unlock(&sdata->local->chanctx_mtx);
837 return;
838 }
839 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
840 struct ieee80211_chanctx, conf);
841 if (chanctx->refcount > 1) {
842 sdata_info(sdata,
843 "channel switch with multiple interfaces on the same channel, disconnecting\n");
844 ieee80211_queue_work(&sdata->local->hw,
845 &ifmgd->csa_connection_drop_work);
846 mutex_unlock(&sdata->local->chanctx_mtx);
847 return;
848 }
849 mutex_unlock(&sdata->local->chanctx_mtx);
850
851 sdata->local->csa_channel = new_ch;
852
Johannes Berg57eebdf2012-08-01 15:50:46 +0200853 if (sw_elem->mode)
854 ieee80211_stop_queues_by_reason(&sdata->local->hw,
855 IEEE80211_QUEUE_STOP_REASON_CSA);
856
Johannes Berg5ce6e432010-05-11 16:20:57 +0200857 if (sdata->local->ops->channel_switch) {
858 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200859 struct ieee80211_channel_switch ch_switch = {
860 .timestamp = timestamp,
861 .block_tx = sw_elem->mode,
862 .channel = new_ch,
863 .count = sw_elem->count,
864 };
865
Johannes Berg5ce6e432010-05-11 16:20:57 +0200866 drv_channel_switch(sdata->local, &ch_switch);
867 return;
868 }
869
870 /* channel switch handled in software */
Johannes Berg57eebdf2012-08-01 15:50:46 +0200871 if (sw_elem->count <= 1)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400872 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +0200873 else
Johannes Bergcc32abd2009-05-15 11:52:31 +0200874 mod_timer(&ifmgd->chswitch_timer,
Johannes Berg30490002012-08-01 15:53:45 +0200875 TU_TO_EXP_TIME(sw_elem->count *
876 cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +0200877}
878
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200879static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
880 struct ieee80211_channel *channel,
881 const u8 *country_ie, u8 country_ie_len,
882 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200883{
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200884 struct ieee80211_country_ie_triplet *triplet;
885 int chan = ieee80211_frequency_to_channel(channel->center_freq);
886 int i, chan_pwr, chan_increment, new_ap_level;
887 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200888
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200889 /* Invalid IE */
890 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200891 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200892
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200893 triplet = (void *)(country_ie + 3);
894 country_ie_len -= 3;
895
896 switch (channel->band) {
897 default:
898 WARN_ON_ONCE(1);
899 /* fall through */
900 case IEEE80211_BAND_2GHZ:
901 case IEEE80211_BAND_60GHZ:
902 chan_increment = 1;
903 break;
904 case IEEE80211_BAND_5GHZ:
905 chan_increment = 4;
906 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200907 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200908
909 /* find channel */
910 while (country_ie_len >= 3) {
911 u8 first_channel = triplet->chans.first_channel;
912
913 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
914 goto next;
915
916 for (i = 0; i < triplet->chans.num_channels; i++) {
917 if (first_channel + i * chan_increment == chan) {
918 have_chan_pwr = true;
919 chan_pwr = triplet->chans.max_power;
920 break;
921 }
922 }
923 if (have_chan_pwr)
924 break;
925
926 next:
927 triplet++;
928 country_ie_len -= 3;
929 }
930
931 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200932 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200933
934 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
935
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200936 if (sdata->ap_power_level == new_ap_level)
937 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +0200938
939 sdata_info(sdata,
940 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
941 new_ap_level, chan_pwr, *pwr_constr_elem,
942 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200943 sdata->ap_power_level = new_ap_level;
944 if (__ieee80211_recalc_txpower(sdata))
945 return BSS_CHANGED_TXPOWER;
946 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200947}
948
Juuso Oikarinenf90754c2010-06-21 08:59:39 +0300949void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
950{
951 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
952 struct ieee80211_local *local = sdata->local;
953 struct ieee80211_conf *conf = &local->hw.conf;
954
955 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
956 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
957 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
958
959 local->disable_dynamic_ps = false;
960 conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
961}
962EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
963
964void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
965{
966 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
967 struct ieee80211_local *local = sdata->local;
968 struct ieee80211_conf *conf = &local->hw.conf;
969
970 WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
971 !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
972 (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
973
974 local->disable_dynamic_ps = true;
975 conf->dynamic_ps_timeout = 0;
976 del_timer_sync(&local->dynamic_ps_timer);
977 ieee80211_queue_work(&local->hw,
978 &local->dynamic_ps_enable_work);
979}
980EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
981
Johannes Berg965beda2009-04-16 13:17:24 +0200982/* powersave */
983static void ieee80211_enable_ps(struct ieee80211_local *local,
984 struct ieee80211_sub_if_data *sdata)
985{
986 struct ieee80211_conf *conf = &local->hw.conf;
987
Johannes Bergd5edaed2009-04-22 23:02:51 +0200988 /*
989 * If we are scanning right now then the parameters will
990 * take effect when scan finishes.
991 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200992 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +0200993 return;
994
Johannes Berg965beda2009-04-16 13:17:24 +0200995 if (conf->dynamic_ps_timeout > 0 &&
996 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
997 mod_timer(&local->dynamic_ps_timer, jiffies +
998 msecs_to_jiffies(conf->dynamic_ps_timeout));
999 } else {
1000 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1001 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301002
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001003 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1004 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1005 return;
1006
1007 conf->flags |= IEEE80211_CONF_PS;
1008 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001009 }
1010}
1011
1012static void ieee80211_change_ps(struct ieee80211_local *local)
1013{
1014 struct ieee80211_conf *conf = &local->hw.conf;
1015
1016 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001017 ieee80211_enable_ps(local, local->ps_sdata);
1018 } else if (conf->flags & IEEE80211_CONF_PS) {
1019 conf->flags &= ~IEEE80211_CONF_PS;
1020 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1021 del_timer_sync(&local->dynamic_ps_timer);
1022 cancel_work_sync(&local->dynamic_ps_enable_work);
1023 }
1024}
1025
Jason Young808118c2011-03-10 16:43:19 -08001026static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1027{
1028 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1029 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001030 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001031
1032 if (!mgd->powersave)
1033 return false;
1034
Johannes Berg05cb9102011-10-28 11:59:47 +02001035 if (mgd->broken_ap)
1036 return false;
1037
Jason Young808118c2011-03-10 16:43:19 -08001038 if (!mgd->associated)
1039 return false;
1040
Jason Young808118c2011-03-10 16:43:19 -08001041 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1042 IEEE80211_STA_CONNECTION_POLL))
1043 return false;
1044
1045 rcu_read_lock();
1046 sta = sta_info_get(sdata, mgd->bssid);
1047 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001048 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001049 rcu_read_unlock();
1050
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001051 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001052}
1053
Johannes Berg965beda2009-04-16 13:17:24 +02001054/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001055void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001056{
1057 struct ieee80211_sub_if_data *sdata, *found = NULL;
1058 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001059 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001060
1061 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1062 local->ps_sdata = NULL;
1063 return;
1064 }
1065
1066 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001067 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001068 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301069 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1070 /* If an AP vif is found, then disable PS
1071 * by setting the count to zero thereby setting
1072 * ps_sdata to NULL.
1073 */
1074 count = 0;
1075 break;
1076 }
Johannes Berg965beda2009-04-16 13:17:24 +02001077 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1078 continue;
1079 found = sdata;
1080 count++;
1081 }
1082
Jason Young808118c2011-03-10 16:43:19 -08001083 if (count == 1 && ieee80211_powersave_allowed(found)) {
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001084 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg10f644a2009-04-16 13:17:25 +02001085 s32 beaconint_us;
1086
1087 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001088 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001089
1090 beaconint_us = ieee80211_tu_to_usec(
1091 found->vif.bss_conf.beacon_int);
1092
Juuso Oikarinenff616382010-06-09 09:51:52 +03001093 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001094 if (timeout < 0) {
1095 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001096 * Go to full PSM if the user configures a very low
1097 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001098 * The 2000 second value is there for compatibility
1099 * until the PM_QOS_NETWORK_LATENCY is configured
1100 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001101 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001102 if (latency > (1900 * USEC_PER_MSEC) &&
1103 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001104 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001105 else
1106 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001107 }
Juuso Oikarinenf90754c2010-06-21 08:59:39 +03001108 local->dynamic_ps_user_timeout = timeout;
1109 if (!local->disable_dynamic_ps)
1110 conf->dynamic_ps_timeout =
1111 local->dynamic_ps_user_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001112
Johannes Berg04fe2032009-04-22 18:44:37 +02001113 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001114 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001115 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001116 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001117 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001118
1119 /* If the TIM IE is invalid, pretend the value is 1 */
1120 if (!dtimper)
1121 dtimper = 1;
1122 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001123 maxslp = min_t(int, dtimper,
1124 latency / beaconint_us);
1125
Johannes Berg9ccebe62009-04-23 10:32:36 +02001126 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001127 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001128 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001129 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001130 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001131 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001132 }
Johannes Berg965beda2009-04-16 13:17:24 +02001133
1134 ieee80211_change_ps(local);
1135}
1136
Eliad Pellerab095872012-07-27 12:33:22 +03001137void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1138{
1139 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1140
1141 if (sdata->vif.bss_conf.ps != ps_allowed) {
1142 sdata->vif.bss_conf.ps = ps_allowed;
1143 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1144 }
1145}
1146
Johannes Berg965beda2009-04-16 13:17:24 +02001147void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1148{
1149 struct ieee80211_local *local =
1150 container_of(work, struct ieee80211_local,
1151 dynamic_ps_disable_work);
1152
1153 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1154 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1155 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1156 }
1157
1158 ieee80211_wake_queues_by_reason(&local->hw,
1159 IEEE80211_QUEUE_STOP_REASON_PS);
1160}
1161
1162void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1163{
1164 struct ieee80211_local *local =
1165 container_of(work, struct ieee80211_local,
1166 dynamic_ps_enable_work);
1167 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001168 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301169 unsigned long flags;
1170 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001171
1172 /* can only happen when PS was just disabled anyway */
1173 if (!sdata)
1174 return;
1175
Christian Lamparter5e340692011-06-30 21:08:43 +02001176 ifmgd = &sdata->u.mgd;
1177
Johannes Berg965beda2009-04-16 13:17:24 +02001178 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1179 return;
1180
Arik Nemtsov77b70232011-06-26 12:06:54 +03001181 if (!local->disable_dynamic_ps &&
1182 local->hw.conf.dynamic_ps_timeout > 0) {
1183 /* don't enter PS if TX frames are pending */
1184 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301185 mod_timer(&local->dynamic_ps_timer, jiffies +
1186 msecs_to_jiffies(
1187 local->hw.conf.dynamic_ps_timeout));
1188 return;
1189 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001190
1191 /*
1192 * transmission can be stopped by others which leads to
1193 * dynamic_ps_timer expiry. Postpone the ps timer if it
1194 * is not the actual idle state.
1195 */
1196 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1197 for (q = 0; q < local->hw.queues; q++) {
1198 if (local->queue_stop_reasons[q]) {
1199 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1200 flags);
1201 mod_timer(&local->dynamic_ps_timer, jiffies +
1202 msecs_to_jiffies(
1203 local->hw.conf.dynamic_ps_timeout));
1204 return;
1205 }
1206 }
1207 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301208 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301209
Vivek Natarajan375177b2010-02-09 14:50:28 +05301210 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301211 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301212 netif_tx_stop_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001213
Vivek Natarajane8306f92011-04-06 11:41:10 +05301214 if (drv_tx_frames_pending(local))
1215 mod_timer(&local->dynamic_ps_timer, jiffies +
1216 msecs_to_jiffies(
1217 local->hw.conf.dynamic_ps_timeout));
1218 else {
1219 ieee80211_send_nullfunc(local, sdata, 1);
1220 /* Flush to get the tx status of nullfunc frame */
1221 drv_flush(local, false);
1222 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301223 }
1224
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001225 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1226 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301227 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1228 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1229 local->hw.conf.flags |= IEEE80211_CONF_PS;
1230 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1231 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301232
Arik Nemtsov77b70232011-06-26 12:06:54 +03001233 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1234 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg965beda2009-04-16 13:17:24 +02001235}
1236
1237void ieee80211_dynamic_ps_timer(unsigned long data)
1238{
1239 struct ieee80211_local *local = (void *) data;
1240
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001241 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001242 return;
1243
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001244 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001245}
1246
Johannes Berg60f8b392008-09-08 17:44:22 +02001247/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001248static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001249 struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001250 u8 *wmm_param, size_t wmm_param_len)
1251{
Jiri Bencf0706e82007-05-05 11:45:53 -07001252 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001253 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001254 size_t left;
1255 int count;
Kalle Valoab133152010-01-12 10:42:31 +02001256 u8 *pos, uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001257
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001258 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001259 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001260
Johannes Berg32c50572012-03-28 11:04:29 +02001261 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001262 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001263
1264 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001265 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001266
Jiri Bencf0706e82007-05-05 11:45:53 -07001267 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001268 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001269
1270 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001271 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001272
Jiri Bencf0706e82007-05-05 11:45:53 -07001273 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001274 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001275 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001276 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001277
1278 pos = wmm_param + 8;
1279 left = wmm_param_len - 8;
1280
1281 memset(&params, 0, sizeof(params));
1282
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001283 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001284 for (; left >= 4; left -= 4, pos += 4) {
1285 int aci = (pos[0] >> 5) & 0x03;
1286 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001287 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001288 int queue;
1289
1290 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001291 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001292 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001293 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001294 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001295 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1296 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001297 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001298 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001299 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001300 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001301 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001302 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1303 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001304 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001305 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001306 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001307 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001308 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001309 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1310 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001311 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001312 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001313 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001314 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001315 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001316 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001317 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1318 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001319 break;
1320 }
1321
1322 params.aifs = pos[0] & 0x0f;
1323 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1324 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001325 params.txop = get_unaligned_le16(pos + 2);
Kalle Valoab133152010-01-12 10:42:31 +02001326 params.uapsd = uapsd;
1327
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001328 mlme_dbg(sdata,
1329 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1330 queue, aci, acm,
1331 params.aifs, params.cw_min, params.cw_max,
1332 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001333 sdata->tx_conf[queue] = params;
1334 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001335 sdata_err(sdata,
1336 "failed to set TX queue parameters for queue %d\n",
1337 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001338 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001339
1340 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001341 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001342 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001343}
1344
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001345static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1346{
1347 lockdep_assert_held(&sdata->local->mtx);
1348
1349 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1350 IEEE80211_STA_BEACON_POLL);
1351 ieee80211_run_deferred_scan(sdata->local);
1352}
1353
1354static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1355{
1356 mutex_lock(&sdata->local->mtx);
1357 __ieee80211_stop_poll(sdata);
1358 mutex_unlock(&sdata->local->mtx);
1359}
1360
Johannes Berg7a5158e2008-10-08 10:59:33 +02001361static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1362 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001363{
Johannes Bergbda39332008-10-11 01:51:51 +02001364 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001365 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001366 bool use_protection;
1367 bool use_short_preamble;
1368 bool use_short_slot;
1369
1370 if (erp_valid) {
1371 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1372 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1373 } else {
1374 use_protection = false;
1375 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1376 }
1377
1378 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001379 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001380 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001381
Johannes Berg471b3ef2007-12-28 14:32:58 +01001382 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001383 bss_conf->use_cts_prot = use_protection;
1384 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001385 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001386
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001387 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001388 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001389 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001390 }
Daniel Draked9430a32007-07-27 15:43:24 +02001391
Johannes Berg7a5158e2008-10-08 10:59:33 +02001392 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001393 bss_conf->use_short_slot = use_short_slot;
1394 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001395 }
1396
1397 return changed;
1398}
1399
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001400static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001401 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001402 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001403{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001404 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001405 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001406 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001407
Johannes Bergae5eb022008-10-14 16:58:37 +02001408 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001409 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001410 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001411
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001412 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
1413 IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
1414
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001415 sdata->u.mgd.associated = cbss;
1416 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001417
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001418 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1419
Johannes Berg488dd7b2012-10-29 20:08:01 +01001420 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001421 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001422
Johannes Berg9caf0362012-11-29 01:25:20 +01001423 rcu_read_lock();
1424 ies = rcu_dereference(cbss->ies);
1425 if (ies) {
1426 u8 noa[2];
1427 int ret;
1428
1429 ret = cfg80211_get_p2p_attr(
1430 ies->data, ies->len,
1431 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
1432 noa, sizeof(noa));
1433 if (ret >= 2) {
1434 bss_conf->p2p_oppps = noa[1] & 0x80;
1435 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
1436 bss_info_changed |= BSS_CHANGED_P2P_PS;
1437 sdata->u.mgd.p2p_noa_index = noa[0];
1438 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001439 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001440 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001441 }
1442
Johannes Bergb291ba12009-07-10 15:29:03 +02001443 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001444 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001445
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001446 ieee80211_led_assoc(local, 1);
1447
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001448 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001449 /*
1450 * If the AP is buggy we may get here with no DTIM period
1451 * known, so assume it's 1 which is the only safe assumption
1452 * in that case, although if the TIM IE is broken powersave
1453 * probably just won't work at all.
1454 */
1455 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001456 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001457 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001458 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001459 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001460
Juuso Oikarinen68542962010-06-09 13:43:26 +03001461 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001462
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001463 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001464 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001465 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001466 bss_info_changed |= BSS_CHANGED_CQM;
1467
Juuso Oikarinen68542962010-06-09 13:43:26 +03001468 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001469 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001470 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001471
Johannes Bergae5eb022008-10-14 16:58:37 +02001472 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001473
Johannes Berg056508d2009-07-30 21:43:55 +02001474 mutex_lock(&local->iflist_mtx);
1475 ieee80211_recalc_ps(local, -1);
1476 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001477
Johannes Berg04ecd252012-09-11 14:34:12 +02001478 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001479 ieee80211_recalc_ps_vif(sdata);
1480
John W. Linville8a5b33f2010-01-06 15:39:39 -05001481 netif_tx_start_all_queues(sdata->dev);
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001482 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001483}
1484
Jouni Malinene69e95d2010-03-29 23:29:31 -07001485static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001486 u16 stype, u16 reason, bool tx,
1487 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001488{
Johannes Berg46900292009-02-15 12:44:28 +01001489 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001490 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001491 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001492
Johannes Berg77fdaa12009-07-07 03:45:17 +02001493 ASSERT_MGD_MTX(ifmgd);
1494
Johannes Berg37ad3882012-02-24 13:50:54 +01001495 if (WARN_ON_ONCE(tx && !frame_buf))
1496 return;
1497
Johannes Bergb291ba12009-07-10 15:29:03 +02001498 if (WARN_ON(!ifmgd->associated))
1499 return;
1500
David Spinadel79543d82012-06-12 09:59:45 +03001501 ieee80211_stop_poll(sdata);
1502
Johannes Berg77fdaa12009-07-07 03:45:17 +02001503 ifmgd->associated = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001504
1505 /*
1506 * we need to commit the associated = NULL change because the
1507 * scan code uses that to determine whether this iface should
1508 * go to/wake up from powersave or not -- and could otherwise
1509 * wake the queues erroneously.
1510 */
1511 smp_mb();
1512
1513 /*
1514 * Thus, we can only afterwards stop the queues -- to account
1515 * for the case where another CPU is finishing a scan at this
1516 * time -- we don't want the scan code to enable queues.
1517 */
Tomas Winkleraa458d12008-09-09 00:32:12 +03001518
John W. Linville8a5b33f2010-01-06 15:39:39 -05001519 netif_tx_stop_all_queues(sdata->dev);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001520 netif_carrier_off(sdata->dev);
1521
Eliad Peller88bc40e2012-07-12 17:35:33 +03001522 /*
1523 * if we want to get out of ps before disassoc (why?) we have
1524 * to do it before sending disassoc, as otherwise the null-packet
1525 * won't be valid.
1526 */
1527 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1528 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1529 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1530 }
1531 local->ps_sdata = NULL;
1532
Eliad Pellerab095872012-07-27 12:33:22 +03001533 /* disable per-vif ps */
1534 ieee80211_recalc_ps_vif(sdata);
1535
Eliad Pellerf8239812012-06-27 14:18:22 +03001536 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1537 if (tx)
1538 drv_flush(local, false);
1539
Johannes Berg37ad3882012-02-24 13:50:54 +01001540 /* deauthenticate/disassociate now */
1541 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001542 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1543 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001544
1545 /* flush out frame */
1546 if (tx)
1547 drv_flush(local, false);
1548
Eliad Peller88a9e312012-06-01 11:14:03 +03001549 /* clear bssid only after building the needed mgmt frames */
1550 memset(ifmgd->bssid, 0, ETH_ALEN);
1551
Johannes Berg37ad3882012-02-24 13:50:54 +01001552 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001553 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001554
1555 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001556 changed |= ieee80211_reset_erp_info(sdata);
1557
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001558 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001559 changed |= BSS_CHANGED_ASSOC;
1560 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001561
Johannes Berg488dd7b2012-10-29 20:08:01 +01001562 sdata->vif.bss_conf.p2p_ctwindow = 0;
1563 sdata->vif.bss_conf.p2p_oppps = false;
1564
Johannes Berg413ad502009-05-08 21:21:06 +02001565 /* on the next assoc, re-program HT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001566 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1567 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001568
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001569 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301570
Kalle Valo520eb822008-12-18 23:35:27 +02001571 del_timer_sync(&local->dynamic_ps_timer);
1572 cancel_work_sync(&local->dynamic_ps_enable_work);
1573
Juuso Oikarinen68542962010-06-09 13:43:26 +03001574 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001575 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001576 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001577
Johannes Berg3abead52012-03-02 15:56:59 +01001578 sdata->vif.bss_conf.qos = false;
1579 changed |= BSS_CHANGED_QOS;
1580
Johannes Berg0aaffa92010-05-05 15:28:27 +02001581 /* The BSSID (not really interesting) and HT changed */
1582 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001583 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001584
Johannes Berg3abead52012-03-02 15:56:59 +01001585 /* disassociated - set to defaults now */
1586 ieee80211_set_wmm_default(sdata, false);
1587
Johannes Bergb9dcf712010-08-27 12:35:54 +02001588 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1589 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1590 del_timer_sync(&sdata->u.mgd.timer);
1591 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001592
1593 sdata->u.mgd.timers_running = 0;
Johannes Berg028e8da02012-11-26 11:57:41 +01001594
Johannes Berg826262c2012-12-10 16:38:14 +02001595 sdata->vif.bss_conf.dtim_period = 0;
1596
Johannes Berg028e8da02012-11-26 11:57:41 +01001597 ifmgd->flags = 0;
1598 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001599}
Jiri Bencf0706e82007-05-05 11:45:53 -07001600
Kalle Valo3cf335d2009-03-22 21:57:06 +02001601void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1602 struct ieee80211_hdr *hdr)
1603{
1604 /*
1605 * We can postpone the mgd.timer whenever receiving unicast frames
1606 * from AP because we know that the connection is working both ways
1607 * at that time. But multicast frames (and hence also beacons) must
1608 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001609 * data idle periods for sending the periodic probe request to the
1610 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001611 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001612 if (is_multicast_ether_addr(hdr->addr1))
1613 return;
1614
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001615 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001616}
Jiri Bencf0706e82007-05-05 11:45:53 -07001617
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001618static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1619{
1620 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001621 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001622
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001623 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001624 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001625 IEEE80211_STA_CONNECTION_POLL))) {
1626 mutex_unlock(&local->mtx);
1627 return;
1628 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001629
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001630 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001631
1632 mutex_lock(&local->iflist_mtx);
1633 ieee80211_recalc_ps(local, -1);
1634 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001635
1636 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001637 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001638
1639 /*
1640 * We've received a probe response, but are not sure whether
1641 * we have or will be receiving any beacons or data, so let's
1642 * schedule the timers again, just in case.
1643 */
1644 ieee80211_sta_reset_beacon_monitor(sdata);
1645
1646 mod_timer(&ifmgd->conn_mon_timer,
1647 round_jiffies_up(jiffies +
1648 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001649out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001650 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001651}
1652
1653void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001654 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001655{
Felix Fietkau75706d02010-12-02 21:01:07 +01001656 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001657 return;
1658
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001659 if (ack)
1660 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001661
1662 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1663 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001664 if (ack)
1665 sdata->u.mgd.probe_send_count = 0;
1666 else
1667 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001668 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
1669 }
1670}
1671
Maxim Levitskya43abf22009-07-31 18:54:12 +03001672static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1673{
1674 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1675 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001676 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001677 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001678
1679 /*
1680 * Try sending broadcast probe requests for the last three
1681 * probe requests after the first ones failed since some
1682 * buggy APs only support broadcast probe requests.
1683 */
1684 if (ifmgd->probe_send_count >= unicast_limit)
1685 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001686
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001687 /*
1688 * When the hardware reports an accurate Tx ACK status, it's
1689 * better to send a nullfunc frame instead of a probe request,
1690 * as it will kick us off the AP quickly if we aren't associated
1691 * anymore. The timeout will be reset if the frame is ACKed by
1692 * the AP.
1693 */
Soumik Das992e68b2012-05-20 15:31:13 +05301694 ifmgd->probe_send_count++;
1695
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001696 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1697 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001698 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001699 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001700 int ssid_len;
1701
Johannes Berg9caf0362012-11-29 01:25:20 +01001702 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001703 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001704 if (WARN_ON_ONCE(ssid == NULL))
1705 ssid_len = 0;
1706 else
1707 ssid_len = ssid[1];
1708
1709 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Bergfe94fe02012-07-30 12:26:34 +02001710 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02001711 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001712 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001713 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001714
Ben Greear180205b2011-02-04 15:30:24 -08001715 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001716 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301717 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
1718 drv_flush(sdata->local, false);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001719}
1720
Johannes Bergb291ba12009-07-10 15:29:03 +02001721static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1722 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001723{
Kalle Valo04de8382009-03-22 21:57:35 +02001724 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001725 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001726
Johannes Berg9607e6b2009-12-23 13:15:31 +01001727 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001728 return;
1729
Johannes Berg77fdaa12009-07-07 03:45:17 +02001730 mutex_lock(&ifmgd->mtx);
1731
1732 if (!ifmgd->associated)
1733 goto out;
1734
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001735 mutex_lock(&sdata->local->mtx);
1736
1737 if (sdata->local->tmp_channel || sdata->local->scanning) {
1738 mutex_unlock(&sdata->local->mtx);
1739 goto out;
1740 }
1741
Joe Perchese87cc472012-05-13 21:56:26 +00001742 if (beacon)
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001743 mlme_dbg_ratelimited(sdata,
1744 "detected beacon loss from AP - sending probe request\n");
1745
Holger Schurig6efb71b2012-05-15 15:38:59 +02001746 ieee80211_cqm_rssi_notify(&sdata->vif,
1747 NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
Kalle Valo04de8382009-03-22 21:57:35 +02001748
Johannes Bergb291ba12009-07-10 15:29:03 +02001749 /*
1750 * The driver/our work has already reported this event or the
1751 * connection monitoring has kicked in and we have already sent
1752 * a probe request. Or maybe the AP died and the driver keeps
1753 * reporting until we disassociate...
1754 *
1755 * In either case we have to ignore the current call to this
1756 * function (except for setting the correct probe reason bit)
1757 * because otherwise we would reset the timer every time and
1758 * never check whether we received a probe response!
1759 */
1760 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
1761 IEEE80211_STA_CONNECTION_POLL))
1762 already = true;
1763
1764 if (beacon)
1765 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
1766 else
1767 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1768
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001769 mutex_unlock(&sdata->local->mtx);
1770
Johannes Bergb291ba12009-07-10 15:29:03 +02001771 if (already)
1772 goto out;
1773
Johannes Berg4e751842009-06-10 15:16:52 +02001774 mutex_lock(&sdata->local->iflist_mtx);
1775 ieee80211_recalc_ps(sdata->local, -1);
1776 mutex_unlock(&sdata->local->iflist_mtx);
1777
Maxim Levitskya43abf22009-07-31 18:54:12 +03001778 ifmgd->probe_send_count = 0;
1779 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001780 out:
1781 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02001782}
1783
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001784struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1785 struct ieee80211_vif *vif)
1786{
1787 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1788 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001789 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001790 struct sk_buff *skb;
1791 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001792 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001793
1794 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1795 return NULL;
1796
1797 ASSERT_MGD_MTX(ifmgd);
1798
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001799 if (ifmgd->associated)
1800 cbss = ifmgd->associated;
1801 else if (ifmgd->auth_data)
1802 cbss = ifmgd->auth_data->bss;
1803 else if (ifmgd->assoc_data)
1804 cbss = ifmgd->assoc_data->bss;
1805 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001806 return NULL;
1807
Johannes Berg9caf0362012-11-29 01:25:20 +01001808 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001809 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001810 if (WARN_ON_ONCE(ssid == NULL))
1811 ssid_len = 0;
1812 else
1813 ssid_len = ssid[1];
1814
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001815 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001816 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001817 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001818 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001819 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001820
1821 return skb;
1822}
1823EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1824
Johannes Berg882a7c62012-08-01 22:32:45 +02001825static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
1826 bool transmit_frame)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001827{
1828 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1829 struct ieee80211_local *local = sdata->local;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001830 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001831
1832 mutex_lock(&ifmgd->mtx);
1833 if (!ifmgd->associated) {
1834 mutex_unlock(&ifmgd->mtx);
1835 return;
1836 }
1837
Johannes Berg37ad3882012-02-24 13:50:54 +01001838 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1839 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Berg882a7c62012-08-01 22:32:45 +02001840 transmit_frame, frame_buf);
1841 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001842 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02001843
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001844 /*
1845 * must be outside lock due to cfg80211,
1846 * but that's not a problem.
1847 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001848 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01001849
1850 mutex_lock(&local->mtx);
1851 ieee80211_recalc_idle(local);
1852 mutex_unlock(&local->mtx);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001853}
1854
Johannes Bergcc74c0c2012-08-01 16:49:34 +02001855static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02001856{
1857 struct ieee80211_sub_if_data *sdata =
1858 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001859 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08001860 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1861 struct sta_info *sta;
1862
1863 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301864 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001865 sta = sta_info_get(sdata, ifmgd->bssid);
1866 if (sta)
1867 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05301868 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08001869 }
Johannes Bergb291ba12009-07-10 15:29:03 +02001870
Johannes Berg882a7c62012-08-01 22:32:45 +02001871 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
1872 sdata_info(sdata, "Connection to AP %pM lost\n",
1873 ifmgd->bssid);
1874 __ieee80211_disconnect(sdata, false);
1875 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001876 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02001877 }
1878}
1879
1880static void ieee80211_csa_connection_drop_work(struct work_struct *work)
1881{
1882 struct ieee80211_sub_if_data *sdata =
1883 container_of(work, struct ieee80211_sub_if_data,
1884 u.mgd.csa_connection_drop_work);
1885
1886 ieee80211_wake_queues_by_reason(&sdata->local->hw,
1887 IEEE80211_QUEUE_STOP_REASON_CSA);
1888 __ieee80211_disconnect(sdata, true);
Johannes Bergb291ba12009-07-10 15:29:03 +02001889}
1890
Kalle Valo04de8382009-03-22 21:57:35 +02001891void ieee80211_beacon_loss(struct ieee80211_vif *vif)
1892{
1893 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001894 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02001895
Johannes Bergb5878a22010-04-07 16:48:40 +02001896 trace_api_beacon_loss(sdata);
1897
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001898 WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1899 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02001900}
1901EXPORT_SYMBOL(ieee80211_beacon_loss);
1902
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001903void ieee80211_connection_loss(struct ieee80211_vif *vif)
1904{
1905 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1906 struct ieee80211_hw *hw = &sdata->local->hw;
1907
Johannes Bergb5878a22010-04-07 16:48:40 +02001908 trace_api_connection_loss(sdata);
1909
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001910 WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
1911 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1912}
1913EXPORT_SYMBOL(ieee80211_connection_loss);
1914
1915
Johannes Berg66e67e42012-01-20 13:55:27 +01001916static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
1917 bool assoc)
1918{
1919 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1920
1921 lockdep_assert_held(&sdata->u.mgd.mtx);
1922
Johannes Berg66e67e42012-01-20 13:55:27 +01001923 if (!assoc) {
1924 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
1925
1926 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
1927 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01001928 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02001929 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01001930 }
1931
1932 cfg80211_put_bss(auth_data->bss);
1933 kfree(auth_data);
1934 sdata->u.mgd.auth_data = NULL;
1935}
1936
1937static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
1938 struct ieee80211_mgmt *mgmt, size_t len)
1939{
1940 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
1941 u8 *pos;
1942 struct ieee802_11_elems elems;
1943
1944 pos = mgmt->u.auth.variable;
1945 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1946 if (!elems.challenge)
1947 return;
1948 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02001949 drv_mgd_prepare_tx(sdata->local, sdata);
Jouni Malinen700e8ea2012-09-30 19:29:37 +03001950 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01001951 elems.challenge - 2, elems.challenge_len + 2,
1952 auth_data->bss->bssid, auth_data->bss->bssid,
1953 auth_data->key, auth_data->key_len,
1954 auth_data->key_idx);
1955}
1956
1957static enum rx_mgmt_action __must_check
1958ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
1959 struct ieee80211_mgmt *mgmt, size_t len)
1960{
1961 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1962 u8 bssid[ETH_ALEN];
1963 u16 auth_alg, auth_transaction, status_code;
1964 struct sta_info *sta;
1965
1966 lockdep_assert_held(&ifmgd->mtx);
1967
1968 if (len < 24 + 6)
1969 return RX_MGMT_NONE;
1970
1971 if (!ifmgd->auth_data || ifmgd->auth_data->done)
1972 return RX_MGMT_NONE;
1973
1974 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
1975
Joe Perchesb203ca32012-05-08 18:56:52 +00001976 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01001977 return RX_MGMT_NONE;
1978
1979 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1980 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1981 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1982
1983 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001984 auth_transaction != ifmgd->auth_data->expected_transaction) {
1985 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
1986 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
1987 auth_transaction,
1988 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01001989 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03001990 }
Johannes Berg66e67e42012-01-20 13:55:27 +01001991
1992 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001993 sdata_info(sdata, "%pM denied authentication (status %d)\n",
1994 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03001995 ieee80211_destroy_auth_data(sdata, false);
1996 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01001997 }
1998
1999 switch (ifmgd->auth_data->algorithm) {
2000 case WLAN_AUTH_OPEN:
2001 case WLAN_AUTH_LEAP:
2002 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002003 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002004 break;
2005 case WLAN_AUTH_SHARED_KEY:
2006 if (ifmgd->auth_data->expected_transaction != 4) {
2007 ieee80211_auth_challenge(sdata, mgmt, len);
2008 /* need another frame */
2009 return RX_MGMT_NONE;
2010 }
2011 break;
2012 default:
2013 WARN_ONCE(1, "invalid auth alg %d",
2014 ifmgd->auth_data->algorithm);
2015 return RX_MGMT_NONE;
2016 }
2017
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002018 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002019 ifmgd->auth_data->done = true;
2020 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
2021 run_again(ifmgd, ifmgd->auth_data->timeout);
2022
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002023 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2024 ifmgd->auth_data->expected_transaction != 2) {
2025 /*
2026 * Report auth frame to user space for processing since another
2027 * round of Authentication frames is still needed.
2028 */
2029 return RX_MGMT_CFG80211_RX_AUTH;
2030 }
2031
Johannes Berg66e67e42012-01-20 13:55:27 +01002032 /* move station state to auth */
2033 mutex_lock(&sdata->local->sta_mtx);
2034 sta = sta_info_get(sdata, bssid);
2035 if (!sta) {
2036 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2037 goto out_err;
2038 }
2039 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002040 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002041 goto out_err;
2042 }
2043 mutex_unlock(&sdata->local->sta_mtx);
2044
2045 return RX_MGMT_CFG80211_RX_AUTH;
2046 out_err:
2047 mutex_unlock(&sdata->local->sta_mtx);
2048 /* ignore frame -- wait for timeout */
2049 return RX_MGMT_NONE;
2050}
2051
2052
Johannes Berg77fdaa12009-07-07 03:45:17 +02002053static enum rx_mgmt_action __must_check
2054ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002055 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002056{
Johannes Berg46900292009-02-15 12:44:28 +01002057 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002058 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002059 u16 reason_code;
2060
Johannes Berg66e67e42012-01-20 13:55:27 +01002061 lockdep_assert_held(&ifmgd->mtx);
2062
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002063 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002064 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002065
Johannes Berg66e67e42012-01-20 13:55:27 +01002066 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002067 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002068 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002069
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002070 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002071
2072 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2073
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002074 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2075 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002076
Johannes Berg37ad3882012-02-24 13:50:54 +01002077 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2078
Johannes Berg7da7cc12010-08-05 17:02:38 +02002079 mutex_lock(&sdata->local->mtx);
Johannes Berg63f170e2009-12-23 13:15:33 +01002080 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002081 mutex_unlock(&sdata->local->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002082
2083 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002084}
2085
2086
Johannes Berg77fdaa12009-07-07 03:45:17 +02002087static enum rx_mgmt_action __must_check
2088ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2089 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002090{
Johannes Berg46900292009-02-15 12:44:28 +01002091 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002092 u16 reason_code;
2093
Johannes Berg66e67e42012-01-20 13:55:27 +01002094 lockdep_assert_held(&ifmgd->mtx);
2095
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002096 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002097 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002098
Johannes Berg66e67e42012-01-20 13:55:27 +01002099 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002100 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002101 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002102
2103 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2104
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002105 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2106 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002107
Johannes Berg37ad3882012-02-24 13:50:54 +01002108 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2109
Johannes Berg7da7cc12010-08-05 17:02:38 +02002110 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01002111 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002112 mutex_unlock(&sdata->local->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002113
Johannes Berg77fdaa12009-07-07 03:45:17 +02002114 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002115}
2116
Christian Lamparterc74d0842011-10-15 00:14:49 +02002117static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2118 u8 *supp_rates, unsigned int supp_rates_len,
2119 u32 *rates, u32 *basic_rates,
2120 bool *have_higher_than_11mbit,
2121 int *min_rate, int *min_rate_index)
2122{
2123 int i, j;
2124
2125 for (i = 0; i < supp_rates_len; i++) {
2126 int rate = (supp_rates[i] & 0x7f) * 5;
2127 bool is_basic = !!(supp_rates[i] & 0x80);
2128
2129 if (rate > 110)
2130 *have_higher_than_11mbit = true;
2131
2132 /*
2133 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2134 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2135 *
2136 * Note: Even through the membership selector and the basic
2137 * rate flag share the same bit, they are not exactly
2138 * the same.
2139 */
2140 if (!!(supp_rates[i] & 0x80) &&
2141 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2142 continue;
2143
2144 for (j = 0; j < sband->n_bitrates; j++) {
2145 if (sband->bitrates[j].bitrate == rate) {
2146 *rates |= BIT(j);
2147 if (is_basic)
2148 *basic_rates |= BIT(j);
2149 if (rate < *min_rate) {
2150 *min_rate = rate;
2151 *min_rate_index = j;
2152 }
2153 break;
2154 }
2155 }
2156 }
2157}
Jiri Bencf0706e82007-05-05 11:45:53 -07002158
Johannes Berg66e67e42012-01-20 13:55:27 +01002159static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2160 bool assoc)
2161{
2162 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2163
2164 lockdep_assert_held(&sdata->u.mgd.mtx);
2165
Johannes Berg66e67e42012-01-20 13:55:27 +01002166 if (!assoc) {
2167 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2168
2169 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2170 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002171 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002172 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002173 }
2174
2175 kfree(assoc_data);
2176 sdata->u.mgd.assoc_data = NULL;
2177}
2178
2179static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2180 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002181 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002182{
Johannes Berg46900292009-02-15 12:44:28 +01002183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002184 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002185 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002186 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002187 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002188 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002189 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002190 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002191 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002192 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002193
Johannes Bergaf6b6372009-12-23 13:15:35 +01002194 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002195
Jiri Bencf0706e82007-05-05 11:45:53 -07002196 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002197 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002198
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002199 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002200 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2201 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002202 aid &= ~(BIT(15) | BIT(14));
2203
Johannes Berg05cb9102011-10-28 11:59:47 +02002204 ifmgd->broken_ap = false;
2205
2206 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002207 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2208 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002209 aid = 0;
2210 ifmgd->broken_ap = true;
2211 }
2212
Johannes Bergaf6b6372009-12-23 13:15:35 +01002213 pos = mgmt->u.assoc_resp.variable;
2214 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2215
Jiri Bencf0706e82007-05-05 11:45:53 -07002216 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002217 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002218 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002219 }
2220
Johannes Berg46900292009-02-15 12:44:28 +01002221 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002222
Guy Eilam2a33bee2011-08-17 15:18:15 +03002223 mutex_lock(&sdata->local->sta_mtx);
2224 /*
2225 * station info was already allocated and inserted before
2226 * the association and should be available to us
2227 */
Johannes Berg7852e362012-01-20 13:55:24 +01002228 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002229 if (WARN_ON(!sta)) {
2230 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002231 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002232 }
2233
Johannes Berg55de9082012-07-26 17:24:39 +02002234 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002235
Johannes Berga8243b72012-11-22 14:32:09 +01002236 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002237 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Bergd9fe60d2008-10-09 12:13:49 +02002238 elems.ht_cap_elem, &sta->sta.ht_cap);
Johannes Bergae5eb022008-10-14 16:58:37 +02002239
Johannes Berg64f68e52012-03-28 10:58:37 +02002240 sta->supports_40mhz =
2241 sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2242
Mahesh Palivela818255e2012-10-10 11:33:04 +00002243 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2244 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
2245 elems.vht_cap_elem,
2246 &sta->sta.vht_cap);
2247
Johannes Berg4b7679a2008-09-18 18:14:18 +02002248 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002249
Johannes Berg46900292009-02-15 12:44:28 +01002250 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002251 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002252
Johannes Bergddf4ac52008-10-22 11:41:38 +02002253 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002254 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002255
Johannes Bergc8987872012-01-20 13:55:17 +01002256 err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
2257 if (!err)
2258 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2259 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2260 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2261 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002262 sdata_info(sdata,
2263 "failed to move station %pM to desired state\n",
2264 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002265 WARN_ON(__sta_info_destroy(sta));
2266 mutex_unlock(&sdata->local->sta_mtx);
2267 return false;
2268 }
2269
Johannes Berg7852e362012-01-20 13:55:24 +01002270 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002271
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002272 /*
2273 * Always handle WMM once after association regardless
2274 * of the first value the AP uses. Setting -1 here has
2275 * that effect because the AP values is an unsigned
2276 * 4-bit value.
2277 */
2278 ifmgd->wmm_last_param_set = -1;
2279
Johannes Bergddf4ac52008-10-22 11:41:38 +02002280 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002281 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002282 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002283 else
Johannes Berg3abead52012-03-02 15:56:59 +01002284 ieee80211_set_wmm_default(sdata, false);
2285 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002286
Johannes Berg074d46d2012-03-15 19:45:16 +01002287 if (elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002288 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002289 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2290 cbss->bssid, false);
Johannes Bergae5eb022008-10-14 16:58:37 +02002291
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002292 /* set AID and assoc capability,
2293 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002294 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002295 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002296 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002297
Kalle Valo15b7b062009-03-22 21:57:14 +02002298 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002299 * If we're using 4-addr mode, let the AP know that we're
2300 * doing so, so that it can create the STA VLAN on its side
2301 */
2302 if (ifmgd->use_4addr)
2303 ieee80211_send_4addr_nullfunc(local, sdata);
2304
2305 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002306 * Start timer to probe the connection to the AP now.
2307 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002308 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002309 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002310 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002311
Johannes Bergaf6b6372009-12-23 13:15:35 +01002312 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002313}
2314
Johannes Berg66e67e42012-01-20 13:55:27 +01002315static enum rx_mgmt_action __must_check
2316ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2317 struct ieee80211_mgmt *mgmt, size_t len,
2318 struct cfg80211_bss **bss)
2319{
2320 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2321 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2322 u16 capab_info, status_code, aid;
2323 struct ieee802_11_elems elems;
2324 u8 *pos;
2325 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002326
Johannes Berg66e67e42012-01-20 13:55:27 +01002327 lockdep_assert_held(&ifmgd->mtx);
2328
2329 if (!assoc_data)
2330 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002331 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002332 return RX_MGMT_NONE;
2333
2334 /*
2335 * AssocResp and ReassocResp have identical structure, so process both
2336 * of them in this function.
2337 */
2338
2339 if (len < 24 + 6)
2340 return RX_MGMT_NONE;
2341
2342 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2343 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2344 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2345 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2346
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002347 sdata_info(sdata,
2348 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2349 reassoc ? "Rea" : "A", mgmt->sa,
2350 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002351
2352 pos = mgmt->u.assoc_resp.variable;
2353 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
2354
2355 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
2356 elems.timeout_int && elems.timeout_int_len == 5 &&
2357 elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
2358 u32 tu, ms;
2359 tu = get_unaligned_le32(elems.timeout_int + 1);
2360 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002361 sdata_info(sdata,
2362 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2363 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002364 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
2365 if (ms > IEEE80211_ASSOC_TIMEOUT)
2366 run_again(ifmgd, assoc_data->timeout);
2367 return RX_MGMT_NONE;
2368 }
2369
2370 *bss = assoc_data->bss;
2371
2372 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002373 sdata_info(sdata, "%pM denied association (code=%d)\n",
2374 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002375 ieee80211_destroy_assoc_data(sdata, false);
2376 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002377 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2378 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002379 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg66e67e42012-01-20 13:55:27 +01002380 cfg80211_put_bss(*bss);
2381 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2382 }
John W. Linville635d9992012-07-09 16:34:34 -04002383 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002384
2385 /*
2386 * destroy assoc_data afterwards, as otherwise an idle
2387 * recalc after assoc_data is NULL but before associated
2388 * is set can cause the interface to go idle
2389 */
2390 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002391 }
2392
2393 return RX_MGMT_CFG80211_RX_ASSOC;
2394}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002395
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002396static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002397 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002398 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002399 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002400{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002401 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002402 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002403 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002404 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002405 bool need_ps = false;
2406
Johannes Berg826262c2012-12-10 16:38:14 +02002407 if ((sdata->u.mgd.associated &&
2408 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2409 (sdata->u.mgd.assoc_data &&
2410 ether_addr_equal(mgmt->bssid,
2411 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002412 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002413 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2414
2415 if (elems->tim && !elems->parse_error) {
2416 struct ieee80211_tim_ie *tim_ie = elems->tim;
2417 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2418 }
Johannes Berg56007a02010-01-26 14:19:52 +01002419 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002420
Johannes Berg5bda6172008-09-08 11:05:10 +02002421 if (elems->ds_params && elems->ds_params_len == 1)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002422 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2423 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002424 else
2425 freq = rx_status->freq;
2426
2427 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2428
2429 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2430 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002431
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002432 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002433 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002434 if (bss)
2435 ieee80211_rx_bss_put(local, bss);
2436
2437 if (!sdata->u.mgd.associated)
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002438 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002439
Johannes Berg56007a02010-01-26 14:19:52 +01002440 if (need_ps) {
2441 mutex_lock(&local->iflist_mtx);
2442 ieee80211_recalc_ps(local, -1);
2443 mutex_unlock(&local->iflist_mtx);
2444 }
2445
Johannes Berg5bc14202012-08-01 16:13:02 +02002446 if (elems->ch_switch_ie &&
2447 memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
2448 ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
Johannes Berg5ce6e432010-05-11 16:20:57 +02002449 bss, rx_status->mactime);
Jiri Bencf0706e82007-05-05 11:45:53 -07002450}
2451
2452
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002453static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002454 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002455{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002456 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002457 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002458 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2459 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002460 struct ieee802_11_elems elems;
2461
Kalle Valo15b7b062009-03-22 21:57:14 +02002462 ifmgd = &sdata->u.mgd;
2463
Johannes Berg77fdaa12009-07-07 03:45:17 +02002464 ASSERT_MGD_MTX(ifmgd);
2465
Joe Perchesb203ca32012-05-08 18:56:52 +00002466 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002467 return; /* ignore ProbeResp to foreign address */
2468
Ester Kummerae6a44e2008-06-27 18:54:48 +03002469 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2470 if (baselen > len)
2471 return;
2472
2473 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2474 &elems);
2475
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002476 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002477
Johannes Berg77fdaa12009-07-07 03:45:17 +02002478 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002479 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002480 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002481
2482 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002483 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002484 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002485 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002486 ifmgd->auth_data->tries = 0;
2487 ifmgd->auth_data->timeout = jiffies;
2488 run_again(ifmgd, ifmgd->auth_data->timeout);
2489 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002490}
2491
Johannes Bergd91f36d2009-04-16 13:17:26 +02002492/*
2493 * This is the canonical list of information elements we care about,
2494 * the filter code also gives us all changes to the Microsoft OUI
2495 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2496 *
2497 * We implement beacon filtering in software since that means we can
2498 * avoid processing the frame here and in cfg80211, and userspace
2499 * will not be able to tell whether the hardware supports it or not.
2500 *
2501 * XXX: This list needs to be dynamic -- userspace needs to be able to
2502 * add items it requires. It also needs to be able to tell us to
2503 * look out for other vendor IEs.
2504 */
2505static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002506 (1ULL << WLAN_EID_COUNTRY) |
2507 (1ULL << WLAN_EID_ERP_INFO) |
2508 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2509 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2510 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002511 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002512
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002513static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002514 struct ieee80211_mgmt *mgmt,
2515 size_t len,
2516 struct ieee80211_rx_status *rx_status)
2517{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002518 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002519 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002520 size_t baselen;
2521 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002522 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002523 struct ieee80211_chanctx_conf *chanctx_conf;
2524 struct ieee80211_channel *chan;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002525 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002526 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002527 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002528 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002529 u8 *bssid;
2530
Johannes Berg66e67e42012-01-20 13:55:27 +01002531 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002532
Ester Kummerae6a44e2008-06-27 18:54:48 +03002533 /* Process beacon from the current BSS */
2534 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2535 if (baselen > len)
2536 return;
2537
Johannes Berg55de9082012-07-26 17:24:39 +02002538 rcu_read_lock();
2539 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2540 if (!chanctx_conf) {
2541 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002542 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002543 }
2544
Johannes Berg4bf88532012-11-09 11:39:59 +01002545 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002546 rcu_read_unlock();
2547 return;
2548 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002549 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002550 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002551
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002552 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002553 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002554 ieee802_11_parse_elems(mgmt->u.beacon.variable,
2555 len - baselen, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002556
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002557 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002558 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002559 ifmgd->assoc_data->need_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01002560 /* continue assoc process */
2561 ifmgd->assoc_data->timeout = jiffies;
2562 run_again(ifmgd, ifmgd->assoc_data->timeout);
2563 return;
2564 }
2565
2566 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002567 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002568 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002569 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002570
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002571 /* Track average RSSI from the Beacon frames of the current AP */
2572 ifmgd->last_beacon_signal = rx_status->signal;
2573 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2574 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002575 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002576 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002577 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002578 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002579 } else {
2580 ifmgd->ave_beacon_signal =
2581 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2582 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2583 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002584 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002585 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002586
2587 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2588 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2589 int sig = ifmgd->ave_beacon_signal;
2590 int last_sig = ifmgd->last_ave_beacon_signal;
2591
2592 /*
2593 * if signal crosses either of the boundaries, invoke callback
2594 * with appropriate parameters
2595 */
2596 if (sig > ifmgd->rssi_max_thold &&
2597 (last_sig <= ifmgd->rssi_min_thold || 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_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002600 } else if (sig < ifmgd->rssi_min_thold &&
2601 (last_sig >= ifmgd->rssi_max_thold ||
2602 last_sig == 0)) {
2603 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002604 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002605 }
2606 }
2607
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002608 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002609 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002610 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002611 int sig = ifmgd->ave_beacon_signal / 16;
2612 int last_event = ifmgd->last_cqm_event_signal;
2613 int thold = bss_conf->cqm_rssi_thold;
2614 int hyst = bss_conf->cqm_rssi_hyst;
2615 if (sig < thold &&
2616 (last_event == 0 || sig < last_event - hyst)) {
2617 ifmgd->last_cqm_event_signal = sig;
2618 ieee80211_cqm_rssi_notify(
2619 &sdata->vif,
2620 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2621 GFP_KERNEL);
2622 } else if (sig > thold &&
2623 (last_event == 0 || sig > last_event + hyst)) {
2624 ifmgd->last_cqm_event_signal = sig;
2625 ieee80211_cqm_rssi_notify(
2626 &sdata->vif,
2627 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2628 GFP_KERNEL);
2629 }
2630 }
2631
Johannes Bergb291ba12009-07-10 15:29:03 +02002632 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002633 mlme_dbg_ratelimited(sdata,
2634 "cancelling probereq poll due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002635 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02002636 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02002637 ieee80211_run_deferred_scan(local);
2638 mutex_unlock(&local->mtx);
2639
Johannes Berg4e751842009-06-10 15:16:52 +02002640 mutex_lock(&local->iflist_mtx);
2641 ieee80211_recalc_ps(local, -1);
2642 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03002643 }
2644
Johannes Bergb291ba12009-07-10 15:29:03 +02002645 /*
2646 * Push the beacon loss detection into the future since
2647 * we are processing a beacon from the AP just now.
2648 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002649 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002650
Johannes Bergd91f36d2009-04-16 13:17:26 +02002651 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2652 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
2653 len - baselen, &elems,
2654 care_about_ies, ncrc);
2655
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302656 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002657 bool directed_tim = ieee80211_check_tim(elems.tim,
2658 elems.tim_len,
2659 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002660 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002661 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002662 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2663 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2664 ieee80211_hw_config(local,
2665 IEEE80211_CONF_CHANGE_PS);
2666 }
Kalle Valo572e0012009-02-10 17:09:31 +02002667 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302668 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002669 local->pspolling = true;
2670
2671 /*
2672 * Here is assumed that the driver will be
2673 * able to send ps-poll frame and receive a
2674 * response even though power save mode is
2675 * enabled, but some drivers might require
2676 * to disable power save here. This needs
2677 * to be investigated.
2678 */
2679 ieee80211_send_pspoll(local, sdata);
2680 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002681 }
2682 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002683
Johannes Berg488dd7b2012-10-29 20:08:01 +01002684 if (sdata->vif.p2p) {
2685 u8 noa[2];
2686 int ret;
2687
2688 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2689 len - baselen,
2690 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
2691 noa, sizeof(noa));
2692 if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
2693 bss_conf->p2p_oppps = noa[1] & 0x80;
2694 bss_conf->p2p_ctwindow = noa[1] & 0x7f;
2695 changed |= BSS_CHANGED_P2P_PS;
2696 sdata->u.mgd.p2p_noa_index = noa[0];
2697 /*
2698 * make sure we update all information, the CRC
2699 * mechanism doesn't look at P2P attributes.
2700 */
2701 ifmgd->beacon_crc_valid = false;
2702 }
2703 }
2704
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002705 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Jouni Malinen30196672009-05-19 17:01:43 +03002706 return;
2707 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002708 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002709
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002710 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002711
2712 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2713 elems.wmm_param_len))
2714 changed |= BSS_CHANGED_QOS;
2715
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002716 /*
2717 * If we haven't had a beacon before, tell the driver about the
2718 * DTIM period now.
2719 */
2720 if (!bss_conf->dtim_period) {
2721 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2722 if (elems.tim)
2723 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2724 else
2725 bss_conf->dtim_period = 1;
2726 changed |= BSS_CHANGED_DTIM_PERIOD;
2727 }
2728
Johannes Berg7a5158e2008-10-08 10:59:33 +02002729 if (elems.erp_info && elems.erp_info_len >= 1) {
2730 erp_valid = true;
2731 erp_value = elems.erp_info[0];
2732 } else {
2733 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04002734 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002735 changed |= ieee80211_handle_bss_capability(sdata,
2736 le16_to_cpu(mgmt->u.beacon.capab_info),
2737 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07002738
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002739
Johannes Berg074d46d2012-03-15 19:45:16 +01002740 if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
Johannes Berga8243b72012-11-22 14:32:09 +01002741 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg24398e32012-03-28 10:58:36 +02002742 changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
2743 bssid, true);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002744
Johannes Berg04b7b2f2012-09-05 13:41:37 +02002745 if (elems.country_elem && elems.pwr_constr_elem &&
2746 mgmt->u.probe_resp.capab_info &
2747 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02002748 changed |= ieee80211_handle_pwr_constr(sdata, chan,
2749 elems.country_elem,
2750 elems.country_elem_len,
2751 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08002752
Johannes Berg471b3ef2007-12-28 14:32:58 +01002753 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002754}
2755
Johannes Berg1fa57d02010-06-10 10:21:32 +02002756void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
2757 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002758{
Johannes Berg77fdaa12009-07-07 03:45:17 +02002759 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002760 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002761 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01002762 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002763 enum rx_mgmt_action rma = RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002764 u16 fc;
2765
Jiri Bencf0706e82007-05-05 11:45:53 -07002766 rx_status = (struct ieee80211_rx_status *) skb->cb;
2767 mgmt = (struct ieee80211_mgmt *) skb->data;
2768 fc = le16_to_cpu(mgmt->frame_control);
2769
Johannes Berg77fdaa12009-07-07 03:45:17 +02002770 mutex_lock(&ifmgd->mtx);
2771
Johannes Berg66e67e42012-01-20 13:55:27 +01002772 switch (fc & IEEE80211_FCTL_STYPE) {
2773 case IEEE80211_STYPE_BEACON:
2774 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
2775 break;
2776 case IEEE80211_STYPE_PROBE_RESP:
2777 ieee80211_rx_mgmt_probe_resp(sdata, skb);
2778 break;
2779 case IEEE80211_STYPE_AUTH:
2780 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
2781 break;
2782 case IEEE80211_STYPE_DEAUTH:
2783 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
2784 break;
2785 case IEEE80211_STYPE_DISASSOC:
2786 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
2787 break;
2788 case IEEE80211_STYPE_ASSOC_RESP:
2789 case IEEE80211_STYPE_REASSOC_RESP:
2790 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
2791 break;
2792 case IEEE80211_STYPE_ACTION:
2793 switch (mgmt->u.action.category) {
2794 case WLAN_CATEGORY_SPECTRUM_MGMT:
2795 ieee80211_sta_process_chanswitch(sdata,
2796 &mgmt->u.action.u.chan_switch.sw_elem,
2797 (void *)ifmgd->associated->priv,
2798 rx_status->mactime);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002799 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002800 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002801 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002802 mutex_unlock(&ifmgd->mtx);
2803
Johannes Berg66e67e42012-01-20 13:55:27 +01002804 switch (rma) {
2805 case RX_MGMT_NONE:
2806 /* no action */
2807 break;
2808 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02002809 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002810 break;
2811 case RX_MGMT_CFG80211_DISASSOC:
2812 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
2813 break;
2814 case RX_MGMT_CFG80211_RX_AUTH:
2815 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
2816 break;
2817 case RX_MGMT_CFG80211_RX_ASSOC:
2818 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
2819 break;
2820 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
2821 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
2822 break;
2823 default:
2824 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02002825 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002826}
2827
Johannes Berg9c6bd792008-09-11 00:01:52 +02002828static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07002829{
2830 struct ieee80211_sub_if_data *sdata =
2831 (struct ieee80211_sub_if_data *) data;
Johannes Berg46900292009-02-15 12:44:28 +01002832 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002833 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002834
Johannes Berg5bb644a2009-05-17 11:40:42 +02002835 if (local->quiescing) {
2836 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
2837 return;
2838 }
2839
Johannes Berg64592c82010-06-10 10:21:31 +02002840 ieee80211_queue_work(&local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07002841}
2842
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002843static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg95acac62011-07-12 12:30:59 +02002844 u8 *bssid, u8 reason)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002845{
2846 struct ieee80211_local *local = sdata->local;
2847 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002848 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002849
Johannes Berg37ad3882012-02-24 13:50:54 +01002850 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
2851 false, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002852 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01002853
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002854 /*
2855 * must be outside lock due to cfg80211,
2856 * but that's not a problem.
2857 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002858 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01002859
2860 mutex_lock(&local->mtx);
2861 ieee80211_recalc_idle(local);
2862 mutex_unlock(&local->mtx);
2863
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002864 mutex_lock(&ifmgd->mtx);
2865}
2866
Johannes Berg66e67e42012-01-20 13:55:27 +01002867static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
2868{
2869 struct ieee80211_local *local = sdata->local;
2870 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2871 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
2872
2873 lockdep_assert_held(&ifmgd->mtx);
2874
2875 if (WARN_ON_ONCE(!auth_data))
2876 return -EINVAL;
2877
Johannes Berg66e67e42012-01-20 13:55:27 +01002878 auth_data->tries++;
2879
2880 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002881 sdata_info(sdata, "authentication with %pM timed out\n",
2882 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002883
2884 /*
2885 * Most likely AP is not in the range so remove the
2886 * bss struct for that AP.
2887 */
2888 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
2889
2890 return -ETIMEDOUT;
2891 }
2892
Johannes Berga1845fc2012-06-27 13:18:36 +02002893 drv_mgd_prepare_tx(local, sdata);
2894
Johannes Berg66e67e42012-01-20 13:55:27 +01002895 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002896 u16 trans = 1;
2897 u16 status = 0;
2898
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002899 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
2900 auth_data->bss->bssid, auth_data->tries,
2901 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002902
2903 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002904
2905 if (auth_data->algorithm == WLAN_AUTH_SAE) {
2906 trans = auth_data->sae_trans;
2907 status = auth_data->sae_status;
2908 auth_data->expected_transaction = trans;
2909 }
2910
2911 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
2912 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01002913 auth_data->bss->bssid,
2914 auth_data->bss->bssid, NULL, 0, 0);
2915 } else {
2916 const u8 *ssidie;
2917
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002918 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
2919 auth_data->bss->bssid, auth_data->tries,
2920 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002921
Johannes Berg9caf0362012-11-29 01:25:20 +01002922 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002923 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01002924 if (!ssidie) {
2925 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002926 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01002927 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002928 /*
2929 * Direct probe is sent to broadcast address as some APs
2930 * will not answer to direct packet in unassociated state.
2931 */
2932 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Bergfe94fe02012-07-30 12:26:34 +02002933 NULL, 0, (u32) -1, true, false,
Johannes Berg55de9082012-07-26 17:24:39 +02002934 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002935 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01002936 }
2937
2938 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2939 run_again(ifmgd, auth_data->timeout);
2940
2941 return 0;
2942}
2943
2944static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
2945{
2946 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2947 struct ieee80211_local *local = sdata->local;
2948
2949 lockdep_assert_held(&sdata->u.mgd.mtx);
2950
Johannes Berg66e67e42012-01-20 13:55:27 +01002951 assoc_data->tries++;
2952 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002953 sdata_info(sdata, "association with %pM timed out\n",
2954 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002955
2956 /*
2957 * Most likely AP is not in the range so remove the
2958 * bss struct for that AP.
2959 */
2960 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
2961
2962 return -ETIMEDOUT;
2963 }
2964
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002965 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
2966 assoc_data->bss->bssid, assoc_data->tries,
2967 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01002968 ieee80211_send_assoc(sdata);
2969
2970 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
2971 run_again(&sdata->u.mgd, assoc_data->timeout);
2972
2973 return 0;
2974}
2975
Johannes Berg1fa57d02010-06-10 10:21:32 +02002976void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02002977{
Johannes Berg60f8b392008-09-08 17:44:22 +02002978 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02002979 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02002980
Johannes Berg77fdaa12009-07-07 03:45:17 +02002981 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02002982
Johannes Berg66e67e42012-01-20 13:55:27 +01002983 if (ifmgd->auth_data &&
2984 time_after(jiffies, ifmgd->auth_data->timeout)) {
2985 if (ifmgd->auth_data->done) {
2986 /*
2987 * ok ... we waited for assoc but userspace didn't,
2988 * so let's just kill the auth data
2989 */
2990 ieee80211_destroy_auth_data(sdata, false);
2991 } else if (ieee80211_probe_auth(sdata)) {
2992 u8 bssid[ETH_ALEN];
2993
2994 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2995
2996 ieee80211_destroy_auth_data(sdata, false);
2997
2998 mutex_unlock(&ifmgd->mtx);
2999 cfg80211_send_auth_timeout(sdata->dev, bssid);
3000 mutex_lock(&ifmgd->mtx);
3001 }
3002 } else if (ifmgd->auth_data)
3003 run_again(ifmgd, ifmgd->auth_data->timeout);
3004
3005 if (ifmgd->assoc_data &&
3006 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003007 if ((ifmgd->assoc_data->need_beacon &&
3008 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003009 ieee80211_do_assoc(sdata)) {
3010 u8 bssid[ETH_ALEN];
3011
3012 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3013
3014 ieee80211_destroy_assoc_data(sdata, false);
3015
3016 mutex_unlock(&ifmgd->mtx);
3017 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3018 mutex_lock(&ifmgd->mtx);
3019 }
3020 } else if (ifmgd->assoc_data)
3021 run_again(ifmgd, ifmgd->assoc_data->timeout);
3022
Johannes Bergb291ba12009-07-10 15:29:03 +02003023 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3024 IEEE80211_STA_CONNECTION_POLL) &&
3025 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003026 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003027 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003028
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003029 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003030
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003031 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003032 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003033 else
Ben Greear180205b2011-02-04 15:30:24 -08003034 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003035
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003036 /* ACK received for nullfunc probing frame */
3037 if (!ifmgd->probe_send_count)
3038 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003039 else if (ifmgd->nullfunc_failed) {
3040 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003041 mlme_dbg(sdata,
3042 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3043 bssid, ifmgd->probe_send_count,
3044 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003045 ieee80211_mgd_probe_ap_send(sdata);
3046 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003047 mlme_dbg(sdata,
3048 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3049 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003050 ieee80211_sta_connection_lost(sdata, bssid,
3051 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003052 }
3053 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003054 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003055 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003056 mlme_dbg(sdata,
3057 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3058 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003059 ieee80211_sta_connection_lost(sdata, bssid,
3060 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003061 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003062 mlme_dbg(sdata,
3063 "No probe response from AP %pM after %dms, try %d/%i\n",
3064 bssid, probe_wait_ms,
3065 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003066 ieee80211_mgd_probe_ap_send(sdata);
3067 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003068 /*
3069 * We actually lost the connection ... or did we?
3070 * Let's make sure!
3071 */
Ben Greearb38afa82010-10-07 16:12:06 -07003072 wiphy_debug(local->hw.wiphy,
3073 "%s: No probe response from AP %pM"
3074 " after %dms, disconnecting.\n",
3075 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003076 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003077
Johannes Berg95acac62011-07-12 12:30:59 +02003078 ieee80211_sta_connection_lost(sdata, bssid,
3079 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
Johannes Bergb291ba12009-07-10 15:29:03 +02003080 }
3081 }
3082
Johannes Berg77fdaa12009-07-07 03:45:17 +02003083 mutex_unlock(&ifmgd->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01003084
3085 mutex_lock(&local->mtx);
3086 ieee80211_recalc_idle(local);
3087 mutex_unlock(&local->mtx);
Johannes Berg60f8b392008-09-08 17:44:22 +02003088}
Johannes Berg0a51b272008-09-08 17:44:25 +02003089
Johannes Bergb291ba12009-07-10 15:29:03 +02003090static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3091{
3092 struct ieee80211_sub_if_data *sdata =
3093 (struct ieee80211_sub_if_data *) data;
3094 struct ieee80211_local *local = sdata->local;
3095
3096 if (local->quiescing)
3097 return;
3098
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003099 ieee80211_queue_work(&sdata->local->hw,
3100 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003101}
3102
3103static void ieee80211_sta_conn_mon_timer(unsigned long data)
3104{
3105 struct ieee80211_sub_if_data *sdata =
3106 (struct ieee80211_sub_if_data *) data;
3107 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3108 struct ieee80211_local *local = sdata->local;
3109
3110 if (local->quiescing)
3111 return;
3112
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003113 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003114}
3115
3116static void ieee80211_sta_monitor_work(struct work_struct *work)
3117{
3118 struct ieee80211_sub_if_data *sdata =
3119 container_of(work, struct ieee80211_sub_if_data,
3120 u.mgd.monitor_work);
3121
3122 ieee80211_mgd_probe_ap(sdata, false);
3123}
3124
Johannes Berga1678f82008-09-11 00:01:47 +02003125static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3126{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003127 u32 flags;
3128
Kalle Vaload935682009-04-19 08:47:19 +03003129 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003130 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003131
Johannes Bergb291ba12009-07-10 15:29:03 +02003132 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003133 flags = sdata->local->hw.flags;
3134 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3135 ieee80211_queue_work(&sdata->local->hw,
3136 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003137 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003138 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003139 }
Johannes Berga1678f82008-09-11 00:01:47 +02003140}
3141
Johannes Berg5bb644a2009-05-17 11:40:42 +02003142#ifdef CONFIG_PM
3143void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
3144{
3145 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3146
3147 /*
3148 * we need to use atomic bitops for the running bits
3149 * only because both timers might fire at the same
3150 * time -- the code here is properly synchronised.
3151 */
3152
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003153 cancel_work_sync(&ifmgd->request_smps_work);
3154
Johannes Berg0ecfe8062011-11-09 12:14:16 +01003155 cancel_work_sync(&ifmgd->monitor_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003156 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003157 cancel_work_sync(&ifmgd->csa_connection_drop_work);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003158 if (del_timer_sync(&ifmgd->timer))
3159 set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
3160
3161 cancel_work_sync(&ifmgd->chswitch_work);
3162 if (del_timer_sync(&ifmgd->chswitch_timer))
3163 set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
Johannes Bergb291ba12009-07-10 15:29:03 +02003164
Johannes Bergb291ba12009-07-10 15:29:03 +02003165 /* these will just be re-established on connection */
3166 del_timer_sync(&ifmgd->conn_mon_timer);
3167 del_timer_sync(&ifmgd->bcn_mon_timer);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003168}
3169
3170void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3171{
3172 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3173
Johannes Berg782d2672013-01-18 21:21:31 +01003174 mutex_lock(&ifmgd->mtx);
3175 if (!ifmgd->associated) {
3176 mutex_unlock(&ifmgd->mtx);
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303177 return;
Johannes Berg782d2672013-01-18 21:21:31 +01003178 }
Rajkumar Manoharan676b58c2011-07-07 23:33:39 +05303179
Johannes Berg95acac62011-07-12 12:30:59 +02003180 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3181 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
Johannes Berg782d2672013-01-18 21:21:31 +01003182 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3183 ieee80211_sta_connection_lost(sdata,
3184 ifmgd->associated->bssid,
3185 WLAN_REASON_UNSPECIFIED);
Johannes Berg95acac62011-07-12 12:30:59 +02003186 mutex_unlock(&ifmgd->mtx);
Johannes Berg782d2672013-01-18 21:21:31 +01003187 return;
Johannes Berg95acac62011-07-12 12:30:59 +02003188 }
Johannes Berg782d2672013-01-18 21:21:31 +01003189 mutex_unlock(&ifmgd->mtx);
Johannes Berg95acac62011-07-12 12:30:59 +02003190
Johannes Berg5bb644a2009-05-17 11:40:42 +02003191 if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
3192 add_timer(&ifmgd->timer);
3193 if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
3194 add_timer(&ifmgd->chswitch_timer);
Felix Fietkauc8a79722010-11-19 22:55:37 +01003195 ieee80211_sta_reset_beacon_monitor(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003196
3197 mutex_lock(&sdata->local->mtx);
Felix Fietkau46090972010-11-23 20:28:03 +01003198 ieee80211_restart_sta_timer(sdata);
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003199 mutex_unlock(&sdata->local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02003200}
3201#endif
3202
Johannes Berg9c6bd792008-09-11 00:01:52 +02003203/* interface setup */
3204void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3205{
Johannes Berg46900292009-02-15 12:44:28 +01003206 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003207
Johannes Berg46900292009-02-15 12:44:28 +01003208 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003209 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003210 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003211 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3212 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003213 INIT_WORK(&ifmgd->csa_connection_drop_work,
3214 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003215 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003216 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003217 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003218 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3219 (unsigned long) sdata);
3220 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3221 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003222 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303223 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003224
Johannes Bergab1faea2009-07-01 21:41:17 +02003225 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303226 ifmgd->powersave = sdata->wdev.ps;
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02003227 ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
3228 ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003229
Johannes Berg77fdaa12009-07-07 03:45:17 +02003230 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003231
3232 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3233 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3234 else
3235 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003236}
3237
3238/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003239void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3240{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003241 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003242
3243 /* Restart STA timers */
3244 rcu_read_lock();
3245 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3246 ieee80211_restart_sta_timer(sdata);
3247 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003248}
Johannes Berg10f644a2009-04-16 13:17:25 +02003249
3250int ieee80211_max_network_latency(struct notifier_block *nb,
3251 unsigned long data, void *dummy)
3252{
3253 s32 latency_usec = (s32) data;
3254 struct ieee80211_local *local =
3255 container_of(nb, struct ieee80211_local,
3256 network_latency_notifier);
3257
3258 mutex_lock(&local->iflist_mtx);
3259 ieee80211_recalc_ps(local, latency_usec);
3260 mutex_unlock(&local->iflist_mtx);
3261
3262 return 0;
3263}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003264
Johannes Bergf2d9d272012-11-22 14:11:39 +01003265static u32 chandef_downgrade(struct cfg80211_chan_def *c)
3266{
3267 u32 ret;
3268 int tmp;
3269
3270 switch (c->width) {
3271 case NL80211_CHAN_WIDTH_20:
3272 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3273 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3274 break;
3275 case NL80211_CHAN_WIDTH_40:
3276 c->width = NL80211_CHAN_WIDTH_20;
3277 c->center_freq1 = c->chan->center_freq;
3278 ret = IEEE80211_STA_DISABLE_40MHZ |
3279 IEEE80211_STA_DISABLE_VHT;
3280 break;
3281 case NL80211_CHAN_WIDTH_80:
3282 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
3283 /* n_P40 */
3284 tmp /= 2;
3285 /* freq_P40 */
3286 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
3287 c->width = NL80211_CHAN_WIDTH_40;
3288 ret = IEEE80211_STA_DISABLE_VHT;
3289 break;
3290 case NL80211_CHAN_WIDTH_80P80:
3291 c->center_freq2 = 0;
3292 c->width = NL80211_CHAN_WIDTH_80;
3293 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3294 IEEE80211_STA_DISABLE_160MHZ;
3295 break;
3296 case NL80211_CHAN_WIDTH_160:
3297 /* n_P20 */
3298 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
3299 /* n_P80 */
3300 tmp /= 4;
3301 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
3302 c->width = NL80211_CHAN_WIDTH_80;
3303 ret = IEEE80211_STA_DISABLE_80P80MHZ |
3304 IEEE80211_STA_DISABLE_160MHZ;
3305 break;
3306 default:
3307 case NL80211_CHAN_WIDTH_20_NOHT:
3308 WARN_ON_ONCE(1);
3309 c->width = NL80211_CHAN_WIDTH_20_NOHT;
3310 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3311 break;
3312 }
3313
3314 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
3315
3316 return ret;
3317}
3318
3319static u32
3320ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
3321 struct ieee80211_supported_band *sband,
3322 struct ieee80211_channel *channel,
3323 const struct ieee80211_ht_operation *ht_oper,
3324 const struct ieee80211_vht_operation *vht_oper,
3325 struct cfg80211_chan_def *chandef)
3326{
3327 struct cfg80211_chan_def vht_chandef;
3328 u32 ht_cfreq, ret;
3329
3330 chandef->chan = channel;
3331 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3332 chandef->center_freq1 = channel->center_freq;
3333 chandef->center_freq2 = 0;
3334
3335 if (!ht_oper || !sband->ht_cap.ht_supported) {
3336 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3337 goto out;
3338 }
3339
3340 chandef->width = NL80211_CHAN_WIDTH_20;
3341
3342 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
3343 channel->band);
3344 /* check that channel matches the right operating channel */
3345 if (channel->center_freq != ht_cfreq) {
3346 /*
3347 * It's possible that some APs are confused here;
3348 * Netgear WNDR3700 sometimes reports 4 higher than
3349 * the actual channel in association responses, but
3350 * since we look at probe response/beacon data here
3351 * it should be OK.
3352 */
3353 sdata_info(sdata,
3354 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
3355 channel->center_freq, ht_cfreq,
3356 ht_oper->primary_chan, channel->band);
3357 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
3358 goto out;
3359 }
3360
3361 /* check 40 MHz support, if we have it */
3362 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
3363 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
3364 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3365 chandef->width = NL80211_CHAN_WIDTH_40;
3366 chandef->center_freq1 += 10;
3367 break;
3368 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3369 chandef->width = NL80211_CHAN_WIDTH_40;
3370 chandef->center_freq1 -= 10;
3371 break;
3372 }
3373 } else {
3374 /* 40 MHz (and 80 MHz) must be supported for VHT */
3375 ret = IEEE80211_STA_DISABLE_VHT;
3376 goto out;
3377 }
3378
3379 if (!vht_oper || !sband->vht_cap.vht_supported) {
3380 ret = IEEE80211_STA_DISABLE_VHT;
3381 goto out;
3382 }
3383
3384 vht_chandef.chan = channel;
3385 vht_chandef.center_freq1 =
3386 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
3387 channel->band);
3388 vht_chandef.center_freq2 = 0;
3389
3390 if (vht_oper->center_freq_seg2_idx)
3391 vht_chandef.center_freq2 =
3392 ieee80211_channel_to_frequency(
3393 vht_oper->center_freq_seg2_idx,
3394 channel->band);
3395
3396 switch (vht_oper->chan_width) {
3397 case IEEE80211_VHT_CHANWIDTH_USE_HT:
3398 vht_chandef.width = chandef->width;
3399 break;
3400 case IEEE80211_VHT_CHANWIDTH_80MHZ:
3401 vht_chandef.width = NL80211_CHAN_WIDTH_80;
3402 break;
3403 case IEEE80211_VHT_CHANWIDTH_160MHZ:
3404 vht_chandef.width = NL80211_CHAN_WIDTH_160;
3405 break;
3406 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
3407 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
3408 break;
3409 default:
3410 sdata_info(sdata,
3411 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
3412 vht_oper->chan_width);
3413 ret = IEEE80211_STA_DISABLE_VHT;
3414 goto out;
3415 }
3416
3417 if (!cfg80211_chandef_valid(&vht_chandef)) {
3418 sdata_info(sdata,
3419 "AP VHT information is invalid, disable VHT\n");
3420 ret = IEEE80211_STA_DISABLE_VHT;
3421 goto out;
3422 }
3423
3424 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
3425 ret = 0;
3426 goto out;
3427 }
3428
3429 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
3430 sdata_info(sdata,
3431 "AP VHT information doesn't match HT, disable VHT\n");
3432 ret = IEEE80211_STA_DISABLE_VHT;
3433 goto out;
3434 }
3435
3436 *chandef = vht_chandef;
3437
3438 ret = 0;
3439
3440 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
3441 IEEE80211_CHAN_DISABLED)) {
3442 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
3443 ret = IEEE80211_STA_DISABLE_HT |
3444 IEEE80211_STA_DISABLE_VHT;
3445 goto out;
3446 }
3447
3448 ret = chandef_downgrade(chandef);
3449 }
3450
3451 if (chandef->width != vht_chandef.width)
3452 sdata_info(sdata,
3453 "local regulatory prevented using AP HT/VHT configuration, downgraded\n");
3454
3455out:
3456 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
3457 return ret;
3458}
3459
3460static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3461 struct cfg80211_bss *cbss)
3462{
3463 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3464 const u8 *ht_cap_ie, *vht_cap_ie;
3465 const struct ieee80211_ht_cap *ht_cap;
3466 const struct ieee80211_vht_cap *vht_cap;
3467 u8 chains = 1;
3468
3469 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3470 return chains;
3471
Johannes Berg9caf0362012-11-29 01:25:20 +01003472 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003473 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3474 ht_cap = (void *)(ht_cap_ie + 2);
3475 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3476 /*
3477 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3478 * "Tx Unequal Modulation Supported" fields.
3479 */
3480 }
3481
3482 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3483 return chains;
3484
Johannes Berg9caf0362012-11-29 01:25:20 +01003485 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003486 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3487 u8 nss;
3488 u16 tx_mcs_map;
3489
3490 vht_cap = (void *)(vht_cap_ie + 2);
3491 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3492 for (nss = 8; nss > 0; nss--) {
3493 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3494 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3495 break;
3496 }
3497 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3498 chains = max(chains, nss);
3499 }
3500
3501 return chains;
3502}
3503
Johannes Bergb17166a2012-07-27 11:41:27 +02003504static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3505 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003506{
3507 struct ieee80211_local *local = sdata->local;
3508 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003509 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003510 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003511 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003512 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003513 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003514
Johannes Berg24398e32012-03-28 10:58:36 +02003515 sband = local->hw.wiphy->bands[cbss->channel->band];
3516
Johannes Bergf2d9d272012-11-22 14:11:39 +01003517 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3518 IEEE80211_STA_DISABLE_80P80MHZ |
3519 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003520
Johannes Berg9caf0362012-11-29 01:25:20 +01003521 rcu_read_lock();
3522
Johannes Bergf2d9d272012-11-22 14:11:39 +01003523 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3524 sband->ht_cap.ht_supported) {
3525 const u8 *ht_oper_ie;
3526
Johannes Berg9caf0362012-11-29 01:25:20 +01003527 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003528 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3529 ht_oper = (void *)(ht_oper_ie + 2);
3530 }
3531
Johannes Bergf2d9d272012-11-22 14:11:39 +01003532 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3533 sband->vht_cap.vht_supported) {
3534 const u8 *vht_oper_ie;
3535
Johannes Berg9caf0362012-11-29 01:25:20 +01003536 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3537 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003538 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3539 vht_oper = (void *)(vht_oper_ie + 2);
3540 if (vht_oper && !ht_oper) {
3541 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003542 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003543 "AP advertised VHT without HT, disabling both\n");
3544 sdata->flags |= IEEE80211_STA_DISABLE_HT;
3545 sdata->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003546 }
3547 }
3548
Johannes Bergf2d9d272012-11-22 14:11:39 +01003549 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3550 cbss->channel,
3551 ht_oper, vht_oper,
3552 &chandef);
Johannes Berg04ecd252012-09-11 14:34:12 +02003553
Johannes Bergf2d9d272012-11-22 14:11:39 +01003554 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3555 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003556
Johannes Berg9caf0362012-11-29 01:25:20 +01003557 rcu_read_unlock();
3558
Johannes Berg04ecd252012-09-11 14:34:12 +02003559 /* will change later if needed */
3560 sdata->smps_mode = IEEE80211_SMPS_OFF;
3561
Johannes Bergf2d9d272012-11-22 14:11:39 +01003562 /*
3563 * If this fails (possibly due to channel context sharing
3564 * on incompatible channels, e.g. 80+80 and 160 sharing the
3565 * same control channel) try to use a smaller bandwidth.
3566 */
3567 ret = ieee80211_vif_use_channel(sdata, &chandef,
3568 IEEE80211_CHANCTX_SHARED);
3569 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
3570 ifmgd->flags |= chandef_downgrade(&chandef);
3571 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003572}
3573
3574static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3575 struct cfg80211_bss *cbss, bool assoc)
3576{
3577 struct ieee80211_local *local = sdata->local;
3578 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3579 struct ieee80211_bss *bss = (void *)cbss->priv;
3580 struct sta_info *new_sta = NULL;
3581 bool have_sta = false;
3582 int err;
3583
3584 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3585 return -EINVAL;
3586
3587 if (assoc) {
3588 rcu_read_lock();
3589 have_sta = sta_info_get(sdata, cbss->bssid);
3590 rcu_read_unlock();
3591 }
3592
3593 if (!have_sta) {
3594 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3595 if (!new_sta)
3596 return -ENOMEM;
3597 }
3598
3599 mutex_lock(&local->mtx);
3600 ieee80211_recalc_idle(sdata->local);
3601 mutex_unlock(&local->mtx);
3602
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003603 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003604 u32 rates = 0, basic_rates = 0;
3605 bool have_higher_than_11mbit;
3606 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003607 struct ieee80211_supported_band *sband;
3608
3609 sband = local->hw.wiphy->bands[cbss->channel->band];
3610
3611 err = ieee80211_prep_channel(sdata, cbss);
3612 if (err) {
3613 sta_info_free(local, new_sta);
3614 return err;
3615 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003616
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003617 ieee80211_get_rates(sband, bss->supp_rates,
3618 bss->supp_rates_len,
3619 &rates, &basic_rates,
3620 &have_higher_than_11mbit,
3621 &min_rate, &min_rate_index);
3622
3623 /*
3624 * This used to be a workaround for basic rates missing
3625 * in the association response frame. Now that we no
3626 * longer use the basic rates from there, it probably
3627 * doesn't happen any more, but keep the workaround so
3628 * in case some *other* APs are buggy in different ways
3629 * we can connect -- with a warning.
3630 */
3631 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003632 sdata_info(sdata,
3633 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003634 basic_rates = BIT(min_rate_index);
3635 }
3636
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003637 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003638 sdata->vif.bss_conf.basic_rates = basic_rates;
3639
3640 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003641 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003642 have_higher_than_11mbit)
3643 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3644 else
3645 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3646
3647 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3648
Johannes Berg8c358bc2012-05-22 22:13:05 +02003649 /* set timing information */
3650 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
3651 sdata->vif.bss_conf.sync_tsf = cbss->tsf;
3652 sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
3653
3654 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003655 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003656 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3657 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003658
3659 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003660 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003661
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003662 err = sta_info_insert(new_sta);
3663 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003664 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003665 sdata_info(sdata,
3666 "failed to insert STA entry for the AP (error %d)\n",
3667 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003668 return err;
3669 }
3670 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003671 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003672
3673 return 0;
3674}
3675
Johannes Berg77fdaa12009-07-07 03:45:17 +02003676/* config hooks */
3677int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3678 struct cfg80211_auth_request *req)
3679{
Johannes Berg66e67e42012-01-20 13:55:27 +01003680 struct ieee80211_local *local = sdata->local;
3681 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3682 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003683 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003684 int err;
3685
3686 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003687
3688 switch (req->auth_type) {
3689 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3690 auth_alg = WLAN_AUTH_OPEN;
3691 break;
3692 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003693 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003694 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003695 auth_alg = WLAN_AUTH_SHARED_KEY;
3696 break;
3697 case NL80211_AUTHTYPE_FT:
3698 auth_alg = WLAN_AUTH_FT;
3699 break;
3700 case NL80211_AUTHTYPE_NETWORK_EAP:
3701 auth_alg = WLAN_AUTH_LEAP;
3702 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003703 case NL80211_AUTHTYPE_SAE:
3704 auth_alg = WLAN_AUTH_SAE;
3705 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003706 default:
3707 return -EOPNOTSUPP;
3708 }
3709
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003710 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3711 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003712 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003713 return -ENOMEM;
3714
Johannes Berg66e67e42012-01-20 13:55:27 +01003715 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003716
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003717 if (req->sae_data_len >= 4) {
3718 __le16 *pos = (__le16 *) req->sae_data;
3719 auth_data->sae_trans = le16_to_cpu(pos[0]);
3720 auth_data->sae_status = le16_to_cpu(pos[1]);
3721 memcpy(auth_data->data, req->sae_data + 4,
3722 req->sae_data_len - 4);
3723 auth_data->data_len += req->sae_data_len - 4;
3724 }
3725
Johannes Berg77fdaa12009-07-07 03:45:17 +02003726 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003727 memcpy(&auth_data->data[auth_data->data_len],
3728 req->ie, req->ie_len);
3729 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003730 }
3731
Johannes Bergfffd0932009-07-08 14:22:54 +02003732 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003733 auth_data->key_len = req->key_len;
3734 auth_data->key_idx = req->key_idx;
3735 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003736 }
3737
Johannes Berg66e67e42012-01-20 13:55:27 +01003738 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003739
Johannes Berg66e67e42012-01-20 13:55:27 +01003740 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003741
Johannes Berg66e67e42012-01-20 13:55:27 +01003742 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003743
Johannes Berg66e67e42012-01-20 13:55:27 +01003744 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3745 ifmgd->assoc_data) {
3746 err = -EBUSY;
3747 goto err_free;
3748 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003749
Johannes Berg66e67e42012-01-20 13:55:27 +01003750 if (ifmgd->auth_data)
3751 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003752
Johannes Berg66e67e42012-01-20 13:55:27 +01003753 /* prep auth_data so we don't go into idle on disassoc */
3754 ifmgd->auth_data = auth_data;
3755
3756 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003757 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003758
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003759 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003760
Johannes Berga1cf7752012-03-08 15:02:07 +01003761 err = ieee80211_prep_connection(sdata, req->bss, false);
3762 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003763 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003764
Johannes Berg66e67e42012-01-20 13:55:27 +01003765 err = ieee80211_probe_auth(sdata);
3766 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003767 sta_info_destroy_addr(sdata, req->bss->bssid);
3768 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003769 }
3770
Johannes Berg66e67e42012-01-20 13:55:27 +01003771 /* hold our own reference */
3772 cfg80211_ref_bss(auth_data->bss);
3773 err = 0;
3774 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02003775
Johannes Berg66e67e42012-01-20 13:55:27 +01003776 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003777 memset(ifmgd->bssid, 0, ETH_ALEN);
3778 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003779 ifmgd->auth_data = NULL;
3780 err_free:
3781 kfree(auth_data);
3782 out_unlock:
3783 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02003784
Johannes Berg66e67e42012-01-20 13:55:27 +01003785 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003786}
3787
Johannes Berg77fdaa12009-07-07 03:45:17 +02003788int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
3789 struct cfg80211_assoc_request *req)
3790{
Johannes Berg66e67e42012-01-20 13:55:27 +01003791 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003792 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003793 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01003794 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003795 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003796 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003797 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003798 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003799
Johannes Berg66e67e42012-01-20 13:55:27 +01003800 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
3801 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01003802 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003803
Johannes Berg9caf0362012-11-29 01:25:20 +01003804 rcu_read_lock();
3805 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
3806 if (!ssidie) {
3807 rcu_read_unlock();
3808 kfree(assoc_data);
3809 return -EINVAL;
3810 }
3811 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
3812 assoc_data->ssid_len = ssidie[1];
3813 rcu_read_unlock();
3814
Johannes Berg66e67e42012-01-20 13:55:27 +01003815 mutex_lock(&ifmgd->mtx);
3816
3817 if (ifmgd->associated)
Johannes Berg37ad3882012-02-24 13:50:54 +01003818 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003819
3820 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
3821 err = -EBUSY;
3822 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003823 }
3824
Johannes Berg66e67e42012-01-20 13:55:27 +01003825 if (ifmgd->assoc_data) {
3826 err = -EBUSY;
3827 goto err_free;
3828 }
3829
3830 if (ifmgd->auth_data) {
3831 bool match;
3832
3833 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00003834 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003835 ieee80211_destroy_auth_data(sdata, match);
3836 }
3837
3838 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02003839
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003840 ifmgd->beacon_crc_valid = false;
3841
Johannes Bergde5036a2012-03-08 15:02:03 +01003842 /*
3843 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
3844 * We still associate in non-HT mode (11a/b/g) if any one of these
3845 * ciphers is configured as pairwise.
3846 * We can set this to true for non-11n hardware, that'll be checked
3847 * separately along with the peer capabilities.
3848 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02003849 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02003850 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
3851 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003852 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01003853 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003854 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02003855 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003856 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003857 }
3858 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003859
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003860 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01003861 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003862 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3863 }
Ben Greearef96a8422011-11-18 11:32:00 -08003864
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003865 /* Also disable HT if we don't support it or the AP doesn't use WMM */
3866 sband = local->hw.wiphy->bands[req->bss->channel->band];
3867 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02003868 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01003869 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003870 if (!bss->wmm_used)
3871 netdev_info(sdata->dev,
3872 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02003873 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01003874
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003875 /* disable VHT if we don't support it or the AP doesn't use WMM */
3876 if (!sband->vht_cap.vht_supported ||
3877 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
3878 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02003879 if (!bss->wmm_used)
3880 netdev_info(sdata->dev,
3881 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00003882 }
3883
Ben Greearef96a8422011-11-18 11:32:00 -08003884 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
3885 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
3886 sizeof(ifmgd->ht_capa_mask));
3887
Johannes Berg77fdaa12009-07-07 03:45:17 +02003888 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003889 memcpy(assoc_data->ie, req->ie, req->ie_len);
3890 assoc_data->ie_len = req->ie_len;
3891 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003892
Johannes Berg66e67e42012-01-20 13:55:27 +01003893 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01003894
Johannes Bergaf6b6372009-12-23 13:15:35 +01003895 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
3896 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02003897 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003898 else
Johannes Berg04ecd252012-09-11 14:34:12 +02003899 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003900 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02003901 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003902
Johannes Berg66e67e42012-01-20 13:55:27 +01003903 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02003904 assoc_data->wmm = bss->wmm_used &&
3905 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01003906 assoc_data->supp_rates = bss->supp_rates;
3907 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02003908
Johannes Berg9caf0362012-11-29 01:25:20 +01003909 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02003910 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
3911 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
3912 assoc_data->ap_ht_param =
3913 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
3914 else
Johannes Berga8243b72012-11-22 14:32:09 +01003915 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01003916 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
3917 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
3918 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
3919 sizeof(struct ieee80211_vht_cap));
3920 else
3921 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01003922 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01003923
Kalle Valoab133152010-01-12 10:42:31 +02003924 if (bss->wmm_used && bss->uapsd_supported &&
3925 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
Johannes Berg76f03032012-03-08 15:02:05 +01003926 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02003927 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
3928 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01003929 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02003930 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
3931 }
3932
Johannes Berg77fdaa12009-07-07 03:45:17 +02003933 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01003934 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003935
3936 if (req->use_mfp) {
3937 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
3938 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
3939 } else {
3940 ifmgd->mfp = IEEE80211_MFP_DISABLED;
3941 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
3942 }
3943
3944 if (req->crypto.control_port)
3945 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
3946 else
3947 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
3948
Johannes Berga621fa42010-08-27 14:26:54 +03003949 sdata->control_port_protocol = req->crypto.control_port_ethertype;
3950 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
3951
Johannes Berg66e67e42012-01-20 13:55:27 +01003952 /* kick off associate process */
3953
3954 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02003955 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003956
Johannes Berga1cf7752012-03-08 15:02:07 +01003957 err = ieee80211_prep_connection(sdata, req->bss, true);
3958 if (err)
3959 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01003960
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003961 rcu_read_lock();
3962 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02003963
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003964 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
3965 !beacon_ies) {
3966 /*
3967 * Wait up to one beacon interval ...
3968 * should this be more if we miss one?
3969 */
3970 sdata_info(sdata, "waiting for beacon from %pM\n",
3971 ifmgd->bssid);
3972 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
3973 assoc_data->need_beacon = true;
3974 } else if (beacon_ies) {
3975 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3976 beacon_ies->data,
3977 beacon_ies->len);
3978 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
3979 const struct ieee80211_tim_ie *tim;
3980 tim = (void *)(tim_ie + 2);
3981 ifmgd->dtim_period = tim->dtim_period;
Johannes Berg826262c2012-12-10 16:38:14 +02003982 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003983 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01003984 assoc_data->timeout = jiffies;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003985 } else {
3986 assoc_data->timeout = jiffies;
Johannes Berg66e67e42012-01-20 13:55:27 +01003987 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003988 rcu_read_unlock();
3989
Johannes Berg66e67e42012-01-20 13:55:27 +01003990 run_again(ifmgd, assoc_data->timeout);
3991
Paul Stewartfcff4f12012-02-23 17:59:53 -08003992 if (bss->corrupt_data) {
3993 char *corrupt_type = "data";
3994 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
3995 if (bss->corrupt_data &
3996 IEEE80211_BSS_CORRUPT_PROBE_RESP)
3997 corrupt_type = "beacon and probe response";
3998 else
3999 corrupt_type = "beacon";
4000 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4001 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004002 sdata_info(sdata, "associating with AP with corrupt %s\n",
4003 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004004 }
4005
Johannes Berg66e67e42012-01-20 13:55:27 +01004006 err = 0;
4007 goto out;
4008 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004009 memset(ifmgd->bssid, 0, ETH_ALEN);
4010 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004011 ifmgd->assoc_data = NULL;
4012 err_free:
4013 kfree(assoc_data);
4014 out:
4015 mutex_unlock(&ifmgd->mtx);
4016
4017 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004018}
4019
4020int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004021 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004022{
4023 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004024 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004025 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004026 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004027
Johannes Berg77fdaa12009-07-07 03:45:17 +02004028 mutex_lock(&ifmgd->mtx);
4029
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004030 sdata_info(sdata,
4031 "deauthenticating from %pM by local choice (reason=%d)\n",
4032 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004033
Johannes Berg86552012012-10-29 09:46:31 +01004034 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004035 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004036 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4037 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004038 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004039 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004040 ieee80211_destroy_auth_data(sdata, false);
4041 mutex_unlock(&ifmgd->mtx);
4042
4043 sent_frame = tx;
4044 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004045 }
4046
Johannes Berg86552012012-10-29 09:46:31 +01004047 if (ifmgd->associated &&
4048 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4049 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4050 req->reason_code, tx, frame_buf);
4051 sent_frame = tx;
4052 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004053 mutex_unlock(&ifmgd->mtx);
4054
Johannes Berg86552012012-10-29 09:46:31 +01004055 out:
Johannes Berg7da7cc12010-08-05 17:02:38 +02004056 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004057 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004058 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004059
Johannes Berg86552012012-10-29 09:46:31 +01004060 if (sent_frame)
4061 __cfg80211_send_deauth(sdata->dev, frame_buf,
4062 IEEE80211_DEAUTH_FRAME_LEN);
4063
Johannes Berg77fdaa12009-07-07 03:45:17 +02004064 return 0;
4065}
4066
4067int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004068 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004069{
4070 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004071 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004072 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004073
Johannes Berg77fdaa12009-07-07 03:45:17 +02004074 mutex_lock(&ifmgd->mtx);
4075
Johannes Berg8d8b2612009-07-25 11:58:36 +02004076 /*
4077 * cfg80211 should catch this ... but it's racy since
4078 * we can receive a disassoc frame, process it, hand it
4079 * to cfg80211 while that's in a locked section already
4080 * trying to tell us that the user wants to disconnect.
4081 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004082 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004083 mutex_unlock(&ifmgd->mtx);
4084 return -ENOLINK;
4085 }
4086
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004087 sdata_info(sdata,
4088 "disassociating from %pM by local choice (reason=%d)\n",
4089 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004090
Jouni Malinene69e95d2010-03-29 23:29:31 -07004091 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004092 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4093 req->reason_code, !req->local_state_change,
4094 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004095 mutex_unlock(&ifmgd->mtx);
4096
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004097 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4098 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004099
Johannes Berg7da7cc12010-08-05 17:02:38 +02004100 mutex_lock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004101 ieee80211_recalc_idle(sdata->local);
Johannes Berg7da7cc12010-08-05 17:02:38 +02004102 mutex_unlock(&sdata->local->mtx);
Johannes Bergbc83b682009-11-29 12:19:06 +01004103
Johannes Berg77fdaa12009-07-07 03:45:17 +02004104 return 0;
4105}
Jouni Malinen026331c2010-02-15 12:53:10 +02004106
Eliad Pellerafa762f2012-04-23 14:45:15 +03004107void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004108{
4109 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4110
4111 mutex_lock(&ifmgd->mtx);
4112 if (ifmgd->assoc_data)
4113 ieee80211_destroy_assoc_data(sdata, false);
4114 if (ifmgd->auth_data)
4115 ieee80211_destroy_auth_data(sdata, false);
4116 del_timer_sync(&ifmgd->timer);
4117 mutex_unlock(&ifmgd->mtx);
4118}
4119
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004120void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4121 enum nl80211_cqm_rssi_threshold_event rssi_event,
4122 gfp_t gfp)
4123{
4124 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4125
Johannes Bergb5878a22010-04-07 16:48:40 +02004126 trace_api_cqm_rssi_notify(sdata, rssi_event);
4127
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004128 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4129}
4130EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);