blob: cadf05905e5aec83bbc8fc20855320f39f457c9e [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
Jouni Malinen5394af42009-01-08 13:31:59 +02003 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070014#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070015#include <linux/if_ether.h>
16#include <linux/skbuff.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017#include <linux/if_arp.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070018#include <linux/etherdevice.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040019#include <linux/moduleparam.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010020#include <linux/rtnetlink.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020021#include <linux/pm_qos.h>
Johannes Bergd91f36d2009-04-16 13:17:26 +020022#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/mac80211.h>
Johannes Berg472dbc42008-09-11 00:01:49 +020026#include <asm/unaligned.h>
Johannes Berg60f8b392008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020034#define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010035#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
36#define IEEE80211_AUTH_MAX_TRIES 3
37#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
38#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
Johannes Bergcb236d22013-07-29 23:07:43 +020039#define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
Johannes Berg1672c0e32013-01-29 15:02:27 +010040#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
41#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010042
Ben Greear180205b2011-02-04 15:30:24 -080043static int max_nullfunc_tries = 2;
44module_param(max_nullfunc_tries, int, 0644);
45MODULE_PARM_DESC(max_nullfunc_tries,
46 "Maximum nullfunc tx tries before disconnecting (reason 4).");
47
48static int max_probe_tries = 5;
49module_param(max_probe_tries, int, 0644);
50MODULE_PARM_DESC(max_probe_tries,
51 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020052
53/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010054 * Beacon loss timeout is calculated as N frames times the
55 * advertised beacon interval. This may need to be somewhat
56 * higher than what hardware might detect to account for
57 * delays in the host processing frames. But since we also
58 * probe on beacon miss before declaring the connection lost
59 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020060 */
Ben Greear59c1ec22013-03-19 14:19:56 -070061static int beacon_loss_count = 7;
62module_param(beacon_loss_count, int, 0644);
63MODULE_PARM_DESC(beacon_loss_count,
64 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010065
Johannes Bergb291ba12009-07-10 15:29:03 +020066/*
67 * Time the connection can be idle before we probe
68 * it to see if we can still talk to the AP.
69 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030070#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020071/*
72 * Time we wait for a probe response after sending
73 * a probe request because of beacon loss or for
74 * checking the connection still works.
75 */
Ben Greear180205b2011-02-04 15:30:24 -080076static int probe_wait_ms = 500;
77module_param(probe_wait_ms, int, 0644);
78MODULE_PARM_DESC(probe_wait_ms,
79 "Maximum time(ms) to wait for probe response"
80 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070081
Jouni Malinen17e4ec12010-03-29 23:28:30 -070082/*
83 * Weight given to the latest Beacon frame when calculating average signal
84 * strength for Beacon frames received in the current BSS. This must be
85 * between 1 and 15.
86 */
87#define IEEE80211_SIGNAL_AVE_WEIGHT 3
88
Jouni Malinen391a2002010-08-27 22:22:00 +030089/*
90 * How many Beacon frames need to have been used in average signal strength
91 * before starting to indicate signal change events.
92 */
93#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
94
Johannes Berg77fdaa12009-07-07 03:45:17 +020095/*
Johannes Bergca386f32009-07-10 02:39:48 +020096 * We can have multiple work items (and connection probing)
97 * scheduling this timer, but we need to take care to only
98 * reschedule it when it should fire _earlier_ than it was
99 * asked for before, or if it's not pending right now. This
100 * function ensures that. Note that it then is required to
101 * run this function for all timeouts after the first one
102 * has happened -- the work that runs from this timer will
103 * do that.
104 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200105static void run_again(struct ieee80211_sub_if_data *sdata,
106 unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200107{
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200108 sdata_assert_lock(sdata);
Johannes Bergca386f32009-07-10 02:39:48 +0200109
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200110 if (!timer_pending(&sdata->u.mgd.timer) ||
111 time_before(timeout, sdata->u.mgd.timer.expires))
112 mod_timer(&sdata->u.mgd.timer, timeout);
Johannes Bergca386f32009-07-10 02:39:48 +0200113}
114
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400115void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200116{
Johannes Bergc1288b12012-01-19 09:29:57 +0100117 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200118 return;
119
Johannes Berg7eeff742012-07-18 10:27:27 +0200120 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
121 return;
122
Johannes Bergb291ba12009-07-10 15:29:03 +0200123 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100124 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200125}
126
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400127void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
128{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400129 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
130
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100131 if (unlikely(!sdata->u.mgd.associated))
132 return;
133
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400134 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
135 return;
136
137 mod_timer(&sdata->u.mgd.conn_mon_timer,
138 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400139
140 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400141}
142
Johannes Berg5484e232008-09-08 17:44:27 +0200143static int ecw2cw(int ecw)
Johannes Berg60f8b392008-09-08 17:44:22 +0200144{
Johannes Berg5484e232008-09-08 17:44:27 +0200145 return (1 << ecw) - 1;
Johannes Berg60f8b392008-09-08 17:44:22 +0200146}
147
Johannes Berg6565ec92013-02-08 14:52:32 +0100148static u32
149ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
150 struct ieee80211_supported_band *sband,
151 struct ieee80211_channel *channel,
152 const struct ieee80211_ht_operation *ht_oper,
153 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200154 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100155{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200156 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100157 struct cfg80211_chan_def vht_chandef;
158 u32 ht_cfreq, ret;
159
160 chandef->chan = channel;
161 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
162 chandef->center_freq1 = channel->center_freq;
163 chandef->center_freq2 = 0;
164
165 if (!ht_oper || !sband->ht_cap.ht_supported) {
166 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
167 goto out;
168 }
169
170 chandef->width = NL80211_CHAN_WIDTH_20;
171
172 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
173 channel->band);
174 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200175 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100176 /*
177 * It's possible that some APs are confused here;
178 * Netgear WNDR3700 sometimes reports 4 higher than
179 * the actual channel in association responses, but
180 * since we look at probe response/beacon data here
181 * it should be OK.
182 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200183 sdata_info(sdata,
184 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
185 channel->center_freq, ht_cfreq,
186 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100187 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
188 goto out;
189 }
190
191 /* check 40 MHz support, if we have it */
192 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
193 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
194 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
195 chandef->width = NL80211_CHAN_WIDTH_40;
196 chandef->center_freq1 += 10;
197 break;
198 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
199 chandef->width = NL80211_CHAN_WIDTH_40;
200 chandef->center_freq1 -= 10;
201 break;
202 }
203 } else {
204 /* 40 MHz (and 80 MHz) must be supported for VHT */
205 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100206 /* also mark 40 MHz disabled */
207 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100208 goto out;
209 }
210
211 if (!vht_oper || !sband->vht_cap.vht_supported) {
212 ret = IEEE80211_STA_DISABLE_VHT;
213 goto out;
214 }
215
216 vht_chandef.chan = channel;
217 vht_chandef.center_freq1 =
218 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
219 channel->band);
220 vht_chandef.center_freq2 = 0;
221
Johannes Berg6565ec92013-02-08 14:52:32 +0100222 switch (vht_oper->chan_width) {
223 case IEEE80211_VHT_CHANWIDTH_USE_HT:
224 vht_chandef.width = chandef->width;
225 break;
226 case IEEE80211_VHT_CHANWIDTH_80MHZ:
227 vht_chandef.width = NL80211_CHAN_WIDTH_80;
228 break;
229 case IEEE80211_VHT_CHANWIDTH_160MHZ:
230 vht_chandef.width = NL80211_CHAN_WIDTH_160;
231 break;
232 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
233 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
Johannes Berg6553bf042013-03-28 10:26:17 +0100234 vht_chandef.center_freq2 =
235 ieee80211_channel_to_frequency(
236 vht_oper->center_freq_seg2_idx,
237 channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100238 break;
239 default:
Johannes Berg5cdaed12013-07-31 11:23:06 +0200240 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100241 sdata_info(sdata,
242 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
243 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100244 ret = IEEE80211_STA_DISABLE_VHT;
245 goto out;
246 }
247
248 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200249 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100250 sdata_info(sdata,
251 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100252 ret = IEEE80211_STA_DISABLE_VHT;
253 goto out;
254 }
255
256 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
257 ret = 0;
258 goto out;
259 }
260
261 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200262 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100263 sdata_info(sdata,
264 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100265 ret = IEEE80211_STA_DISABLE_VHT;
266 goto out;
267 }
268
269 *chandef = vht_chandef;
270
271 ret = 0;
272
273out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100274 /* don't print the message below for VHT mismatch if VHT is disabled */
275 if (ret & IEEE80211_STA_DISABLE_VHT)
276 vht_chandef = *chandef;
277
Johannes Bergddfe49b2013-07-31 20:52:03 +0200278 /*
279 * Ignore the DISABLED flag when we're already connected and only
280 * tracking the APs beacon for bandwidth changes - otherwise we
281 * might get disconnected here if we connect to an AP, update our
282 * regulatory information based on the AP's country IE and the
283 * information we have is wrong/outdated and disables the channel
284 * that we're actually using for the connection to the AP.
285 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100286 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200287 tracking ? 0 :
288 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100289 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
290 ret = IEEE80211_STA_DISABLE_HT |
291 IEEE80211_STA_DISABLE_VHT;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700292 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100293 }
294
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200295 ret |= ieee80211_chandef_downgrade(chandef);
Johannes Berg6565ec92013-02-08 14:52:32 +0100296 }
297
Johannes Berg5cdaed12013-07-31 11:23:06 +0200298 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100299 sdata_info(sdata,
300 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
301
302 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
303 return ret;
304}
305
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100306static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
307 struct sta_info *sta,
308 const struct ieee80211_ht_operation *ht_oper,
309 const struct ieee80211_vht_operation *vht_oper,
310 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200311{
312 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200314 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200315 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100316 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200317 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100318 u32 flags;
319 enum ieee80211_sta_rx_bandwidth new_sta_bw;
320 int ret;
321
322 /* if HT was/is disabled, don't track any bandwidth changes */
323 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
324 return 0;
325
326 /* don't check VHT if we associated as non-VHT station */
327 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
328 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200329
Johannes Berg11289582013-02-07 17:36:12 +0100330 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100331 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100332
Avri Altman017b45b2013-11-18 19:06:48 +0200333 /*
334 * if bss configuration changed store the new one -
335 * this may be applicable even if channel is identical
336 */
337 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
338 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
339 *changed |= BSS_CHANGED_HT;
340 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
341 }
342
Johannes Bergf2d93302013-02-08 13:15:55 +0100343 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200344 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200345
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100346 /* calculate new channel (type) based on HT/VHT operation IEs */
347 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200348 vht_oper, &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100349
350 /*
351 * Downgrade the new channel if we associated with restricted
352 * capabilities. For example, if we associated as a 20 MHz STA
353 * to a 40 MHz AP (due to regulatory, capabilities or config
354 * reasons) then switching to a 40 MHz channel now won't do us
355 * any good -- we couldn't use it with the AP.
356 */
357 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
358 chandef.width == NL80211_CHAN_WIDTH_80P80)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200359 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100360 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
361 chandef.width == NL80211_CHAN_WIDTH_160)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200362 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100363 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
364 chandef.width > NL80211_CHAN_WIDTH_20)
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200365 flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100366
367 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
368 return 0;
369
370 sdata_info(sdata,
371 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
372 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
373 chandef.center_freq1, chandef.center_freq2);
374
375 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
376 IEEE80211_STA_DISABLE_VHT |
377 IEEE80211_STA_DISABLE_40MHZ |
378 IEEE80211_STA_DISABLE_80P80MHZ |
379 IEEE80211_STA_DISABLE_160MHZ)) ||
380 !cfg80211_chandef_valid(&chandef)) {
381 sdata_info(sdata,
382 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
383 ifmgd->bssid);
384 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200385 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200386
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100387 switch (chandef.width) {
388 case NL80211_CHAN_WIDTH_20_NOHT:
389 case NL80211_CHAN_WIDTH_20:
390 new_sta_bw = IEEE80211_STA_RX_BW_20;
391 break;
392 case NL80211_CHAN_WIDTH_40:
393 new_sta_bw = IEEE80211_STA_RX_BW_40;
394 break;
395 case NL80211_CHAN_WIDTH_80:
396 new_sta_bw = IEEE80211_STA_RX_BW_80;
397 break;
398 case NL80211_CHAN_WIDTH_80P80:
399 case NL80211_CHAN_WIDTH_160:
400 new_sta_bw = IEEE80211_STA_RX_BW_160;
401 break;
402 default:
403 return -EINVAL;
404 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200405
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100406 if (new_sta_bw > sta->cur_max_bandwidth)
407 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200408
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100409 if (new_sta_bw < sta->sta.bandwidth) {
410 sta->sta.bandwidth = new_sta_bw;
411 rate_control_rate_update(local, sband, sta,
412 IEEE80211_RC_BW_CHANGED);
413 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530414
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100415 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
416 if (ret) {
417 sdata_info(sdata,
418 "AP %pM changed bandwidth to incompatible one - disconnect\n",
419 ifmgd->bssid);
420 return ret;
421 }
422
423 if (new_sta_bw > sta->sta.bandwidth) {
424 sta->sta.bandwidth = new_sta_bw;
425 rate_control_rate_update(local, sband, sta,
426 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200427 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200428
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100429 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200430}
431
Johannes Berg9c6bd792008-09-11 00:01:52 +0200432/* frame sending functions */
433
Johannes Berg66e67e42012-01-20 13:55:27 +0100434static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200435 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100436 struct ieee80211_supported_band *sband,
437 struct ieee80211_channel *channel,
438 enum ieee80211_smps_mode smps)
439{
Johannes Berg66e67e42012-01-20 13:55:27 +0100440 u8 *pos;
441 u32 flags = channel->flags;
442 u16 cap;
443 struct ieee80211_sta_ht_cap ht_cap;
444
445 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
446
Johannes Berg66e67e42012-01-20 13:55:27 +0100447 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
448 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
449
Johannes Berg66e67e42012-01-20 13:55:27 +0100450 /* determine capability flags */
451 cap = ht_cap.cap;
452
Johannes Berg9dde6422012-05-16 23:43:19 +0200453 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100454 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
455 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
456 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
457 cap &= ~IEEE80211_HT_CAP_SGI_40;
458 }
459 break;
460 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
461 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
462 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
463 cap &= ~IEEE80211_HT_CAP_SGI_40;
464 }
465 break;
466 }
467
Johannes Berg24398e32012-03-28 10:58:36 +0200468 /*
469 * If 40 MHz was disabled associate as though we weren't
470 * capable of 40 MHz -- some broken APs will never fall
471 * back to trying to transmit in 20 MHz.
472 */
473 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
474 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
475 cap &= ~IEEE80211_HT_CAP_SGI_40;
476 }
477
Johannes Berg66e67e42012-01-20 13:55:27 +0100478 /* set SM PS mode properly */
479 cap &= ~IEEE80211_HT_CAP_SM_PS;
480 switch (smps) {
481 case IEEE80211_SMPS_AUTOMATIC:
482 case IEEE80211_SMPS_NUM_MODES:
483 WARN_ON(1);
484 case IEEE80211_SMPS_OFF:
485 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
486 IEEE80211_HT_CAP_SM_PS_SHIFT;
487 break;
488 case IEEE80211_SMPS_STATIC:
489 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
490 IEEE80211_HT_CAP_SM_PS_SHIFT;
491 break;
492 case IEEE80211_SMPS_DYNAMIC:
493 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
494 IEEE80211_HT_CAP_SM_PS_SHIFT;
495 break;
496 }
497
498 /* reserve and fill IE */
499 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
500 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
501}
502
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000503static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
504 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100505 struct ieee80211_supported_band *sband,
506 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000507{
508 u8 *pos;
509 u32 cap;
510 struct ieee80211_sta_vht_cap vht_cap;
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200511 u32 mask, ap_bf_sts, our_bf_sts;
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000512
513 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
514
515 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100516 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000517
518 /* determine capability flags */
519 cap = vht_cap.cap;
520
Johannes Bergf2d9d272012-11-22 14:11:39 +0100521 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
522 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
523 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
524 }
525
526 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
527 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
528 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
529 }
530
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100531 /*
532 * Some APs apparently get confused if our capabilities are better
533 * than theirs, so restrict what we advertise in the assoc request.
534 */
535 if (!(ap_vht_cap->vht_cap_info &
536 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
537 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
538
Eyal Shapirac1cf6d42014-01-08 15:49:08 +0200539 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
540
541 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask;
542 our_bf_sts = cap & mask;
543
544 if (ap_bf_sts < our_bf_sts) {
545 cap &= ~mask;
546 cap |= ap_bf_sts;
547 }
548
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000549 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000550 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000551 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
552}
553
Johannes Berg66e67e42012-01-20 13:55:27 +0100554static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
555{
556 struct ieee80211_local *local = sdata->local;
557 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
558 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
559 struct sk_buff *skb;
560 struct ieee80211_mgmt *mgmt;
561 u8 *pos, qos_info;
562 size_t offset = 0, noffset;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200563 int i, count, rates_len, supp_rates_len, shift;
Johannes Berg66e67e42012-01-20 13:55:27 +0100564 u16 capab;
565 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200566 struct ieee80211_chanctx_conf *chanctx_conf;
567 struct ieee80211_channel *chan;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200568 u32 rate_flags, rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100569
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200570 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100571
Johannes Berg55de9082012-07-26 17:24:39 +0200572 rcu_read_lock();
573 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
574 if (WARN_ON(!chanctx_conf)) {
575 rcu_read_unlock();
576 return;
577 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100578 chan = chanctx_conf->def.chan;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200579 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
Johannes Berg55de9082012-07-26 17:24:39 +0200580 rcu_read_unlock();
581 sband = local->hw.wiphy->bands[chan->band];
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200582 shift = ieee80211_vif_get_shift(&sdata->vif);
Johannes Berg66e67e42012-01-20 13:55:27 +0100583
584 if (assoc_data->supp_rates_len) {
585 /*
586 * Get all rates supported by the device and the AP as
587 * some APs don't like getting a superset of their rates
588 * in the association request (e.g. D-Link DAP 1353 in
589 * b-only mode)...
590 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200591 rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
592 assoc_data->supp_rates,
593 assoc_data->supp_rates_len,
594 &rates);
Johannes Berg66e67e42012-01-20 13:55:27 +0100595 } else {
596 /*
597 * In case AP not provide any supported rates information
598 * before association, we send information element(s) with
599 * all rates that we support.
600 */
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200601 rates_len = 0;
602 for (i = 0; i < sband->n_bitrates; i++) {
603 if ((rate_flags & sband->bitrates[i].flags)
604 != rate_flags)
605 continue;
606 rates |= BIT(i);
607 rates_len++;
608 }
Johannes Berg66e67e42012-01-20 13:55:27 +0100609 }
610
611 skb = alloc_skb(local->hw.extra_tx_headroom +
612 sizeof(*mgmt) + /* bit too much but doesn't matter */
613 2 + assoc_data->ssid_len + /* SSID */
614 4 + rates_len + /* (extended) rates */
615 4 + /* power capability */
616 2 + 2 * sband->n_channels + /* supported channels */
617 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000618 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100619 assoc_data->ie_len + /* extra IEs */
620 9, /* WMM */
621 GFP_KERNEL);
622 if (!skb)
623 return;
624
625 skb_reserve(skb, local->hw.extra_tx_headroom);
626
627 capab = WLAN_CAPABILITY_ESS;
628
629 if (sband->band == IEEE80211_BAND_2GHZ) {
630 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
631 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
632 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
633 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
634 }
635
636 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
637 capab |= WLAN_CAPABILITY_PRIVACY;
638
639 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
640 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
641 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
642
643 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
644 memset(mgmt, 0, 24);
645 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
646 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
647 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
648
649 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
650 skb_put(skb, 10);
651 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
652 IEEE80211_STYPE_REASSOC_REQ);
653 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
654 mgmt->u.reassoc_req.listen_interval =
655 cpu_to_le16(local->hw.conf.listen_interval);
656 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
657 ETH_ALEN);
658 } else {
659 skb_put(skb, 4);
660 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
661 IEEE80211_STYPE_ASSOC_REQ);
662 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
663 mgmt->u.assoc_req.listen_interval =
664 cpu_to_le16(local->hw.conf.listen_interval);
665 }
666
667 /* SSID */
668 pos = skb_put(skb, 2 + assoc_data->ssid_len);
669 *pos++ = WLAN_EID_SSID;
670 *pos++ = assoc_data->ssid_len;
671 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
672
673 /* add all rates which were marked to be used above */
674 supp_rates_len = rates_len;
675 if (supp_rates_len > 8)
676 supp_rates_len = 8;
677
678 pos = skb_put(skb, supp_rates_len + 2);
679 *pos++ = WLAN_EID_SUPP_RATES;
680 *pos++ = supp_rates_len;
681
682 count = 0;
683 for (i = 0; i < sband->n_bitrates; i++) {
684 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200685 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
686 5 * (1 << shift));
687 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100688 if (++count == 8)
689 break;
690 }
691 }
692
693 if (rates_len > count) {
694 pos = skb_put(skb, rates_len - count + 2);
695 *pos++ = WLAN_EID_EXT_SUPP_RATES;
696 *pos++ = rates_len - count;
697
698 for (i++; i < sband->n_bitrates; i++) {
699 if (BIT(i) & rates) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200700 int rate;
701 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
702 5 * (1 << shift));
703 *pos++ = (u8) rate;
Johannes Berg66e67e42012-01-20 13:55:27 +0100704 }
705 }
706 }
707
708 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
709 /* 1. power capabilities */
710 pos = skb_put(skb, 4);
711 *pos++ = WLAN_EID_PWR_CAPABILITY;
712 *pos++ = 2;
713 *pos++ = 0; /* min tx power */
Simon Wunderlich0430c882013-07-08 16:55:55 +0200714 /* max tx power */
715 *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
Johannes Berg66e67e42012-01-20 13:55:27 +0100716
717 /* 2. supported channels */
718 /* TODO: get this in reg domain format */
719 pos = skb_put(skb, 2 * sband->n_channels + 2);
720 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
721 *pos++ = 2 * sband->n_channels;
722 for (i = 0; i < sband->n_channels; i++) {
723 *pos++ = ieee80211_frequency_to_channel(
724 sband->channels[i].center_freq);
725 *pos++ = 1; /* one channel in the subband*/
726 }
727 }
728
729 /* if present, add any custom IEs that go before HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200730 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100731 static const u8 before_ht[] = {
732 WLAN_EID_SSID,
733 WLAN_EID_SUPP_RATES,
734 WLAN_EID_EXT_SUPP_RATES,
735 WLAN_EID_PWR_CAPABILITY,
736 WLAN_EID_SUPPORTED_CHANNELS,
737 WLAN_EID_RSN,
738 WLAN_EID_QOS_CAPA,
739 WLAN_EID_RRM_ENABLED_CAPABILITIES,
740 WLAN_EID_MOBILITY_DOMAIN,
741 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
742 };
743 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
744 before_ht, ARRAY_SIZE(before_ht),
745 offset);
746 pos = skb_put(skb, noffset - offset);
747 memcpy(pos, assoc_data->ie + offset, noffset - offset);
748 offset = noffset;
749 }
750
Johannes Bergf2d9d272012-11-22 14:11:39 +0100751 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
752 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
753 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
754
Johannes Berga8243b72012-11-22 14:32:09 +0100755 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200756 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200757 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100758
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000759 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100760 ieee80211_add_vht_ie(sdata, skb, sband,
761 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000762
Johannes Berg66e67e42012-01-20 13:55:27 +0100763 /* if present, add any custom non-vendor IEs that go after HT */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200764 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100765 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
766 assoc_data->ie_len,
767 offset);
768 pos = skb_put(skb, noffset - offset);
769 memcpy(pos, assoc_data->ie + offset, noffset - offset);
770 offset = noffset;
771 }
772
Johannes Berg76f03032012-03-08 15:02:05 +0100773 if (assoc_data->wmm) {
774 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200775 qos_info = ifmgd->uapsd_queues;
776 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100777 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
778 } else {
779 qos_info = 0;
780 }
781
782 pos = skb_put(skb, 9);
783 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
784 *pos++ = 7; /* len */
785 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
786 *pos++ = 0x50;
787 *pos++ = 0xf2;
788 *pos++ = 2; /* WME */
789 *pos++ = 0; /* WME info */
790 *pos++ = 1; /* WME ver */
791 *pos++ = qos_info;
792 }
793
794 /* add any remaining custom (i.e. vendor specific here) IEs */
Johannes Bergb3f51e92013-10-25 11:31:42 +0200795 if (assoc_data->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100796 noffset = assoc_data->ie_len;
797 pos = skb_put(skb, noffset - offset);
798 memcpy(pos, assoc_data->ie + offset, noffset - offset);
799 }
800
Johannes Berga1845fc2012-06-27 13:18:36 +0200801 drv_mgd_prepare_tx(local, sdata);
802
Johannes Berg66e67e42012-01-20 13:55:27 +0100803 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100804 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
805 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
806 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100807 ieee80211_tx_skb(sdata, skb);
808}
809
Kalle Valo572e0012009-02-10 17:09:31 +0200810void ieee80211_send_pspoll(struct ieee80211_local *local,
811 struct ieee80211_sub_if_data *sdata)
812{
Kalle Valo572e0012009-02-10 17:09:31 +0200813 struct ieee80211_pspoll *pspoll;
814 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200815
Kalle Valod8cd1892010-01-05 20:16:26 +0200816 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
817 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200818 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200819
Kalle Valod8cd1892010-01-05 20:16:26 +0200820 pspoll = (struct ieee80211_pspoll *) skb->data;
821 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200822
Johannes Berg62ae67b2009-11-18 18:42:05 +0100823 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
824 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200825}
826
Johannes Berg965beda2009-04-16 13:17:24 +0200827void ieee80211_send_nullfunc(struct ieee80211_local *local,
828 struct ieee80211_sub_if_data *sdata,
829 int powersave)
830{
831 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200832 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200834
Kalle Valod8cd1892010-01-05 20:16:26 +0200835 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
836 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200837 return;
838
Kalle Valod8cd1892010-01-05 20:16:26 +0200839 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200840 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200841 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200842
Seth Forshee6c17b772013-02-11 11:21:07 -0600843 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
844 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +0200845
846 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
847 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
848
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +0200849 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530850 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
851
Johannes Berg62ae67b2009-11-18 18:42:05 +0100852 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200853}
854
Felix Fietkaud5242152010-01-08 18:06:26 +0100855static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
856 struct ieee80211_sub_if_data *sdata)
857{
858 struct sk_buff *skb;
859 struct ieee80211_hdr *nullfunc;
860 __le16 fc;
861
862 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
863 return;
864
865 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700866 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100867 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700868
Felix Fietkaud5242152010-01-08 18:06:26 +0100869 skb_reserve(skb, local->hw.extra_tx_headroom);
870
871 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
872 memset(nullfunc, 0, 30);
873 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
874 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
875 nullfunc->frame_control = fc;
876 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
877 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
878 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
879 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
880
881 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
882 ieee80211_tx_skb(sdata, skb);
883}
884
Johannes Bergcc32abd2009-05-15 11:52:31 +0200885/* spectrum management related things */
886static void ieee80211_chswitch_work(struct work_struct *work)
887{
888 struct ieee80211_sub_if_data *sdata =
889 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100890 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200891 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Arik Nemtsov7578d572013-09-01 17:15:51 +0300892 u32 changed = 0;
893 int ret;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200894
Johannes Berg9607e6b2009-12-23 13:15:31 +0100895 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200896 return;
897
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200898 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200899 if (!ifmgd->associated)
900 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200901
Johannes Berg34a37402013-12-18 09:43:33 +0100902 mutex_lock(&local->mtx);
Luciano Coelho33787fc2013-11-11 20:34:54 +0200903 ret = ieee80211_vif_change_channel(sdata, &changed);
Johannes Berg34a37402013-12-18 09:43:33 +0100904 mutex_unlock(&local->mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +0300905 if (ret) {
906 sdata_info(sdata,
907 "vif channel switch failed, disconnecting\n");
908 ieee80211_queue_work(&sdata->local->hw,
909 &ifmgd->csa_connection_drop_work);
910 goto out;
911 }
Karl Beldan675a0b02013-03-25 16:26:57 +0100912
Arik Nemtsov7578d572013-09-01 17:15:51 +0300913 if (!local->use_chanctx) {
Luciano Coelho33787fc2013-11-11 20:34:54 +0200914 local->_oper_chandef = sdata->csa_chandef;
Arik Nemtsov7578d572013-09-01 17:15:51 +0300915 /* Call "hw_config" only if doing sw channel switch.
916 * Otherwise update the channel directly
917 */
918 if (!local->ops->channel_switch)
919 ieee80211_hw_config(local, 0);
920 else
921 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200922 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200923
Johannes Berg77fdaa12009-07-07 03:45:17 +0200924 /* XXX: shouldn't really modify cfg80211-owned data! */
Luciano Coelho33787fc2013-11-11 20:34:54 +0200925 ifmgd->associated->channel = sdata->csa_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200926
Johannes Berg57eebdf2012-08-01 15:50:46 +0200927 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +0100928 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100929 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200930 IEEE80211_QUEUE_STOP_REASON_CSA);
Arik Nemtsov7578d572013-09-01 17:15:51 +0300931
932 ieee80211_bss_info_change_notify(sdata, changed);
933
Johannes Berg77fdaa12009-07-07 03:45:17 +0200934 out:
Arik Nemtsov7578d572013-09-01 17:15:51 +0300935 sdata->vif.csa_active = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200936 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200937 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200938}
939
Johannes Berg5ce6e432010-05-11 16:20:57 +0200940void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
941{
Johannes Berg55de9082012-07-26 17:24:39 +0200942 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
943 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200944
945 trace_api_chswitch_done(sdata, success);
946 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200947 sdata_info(sdata,
948 "driver channel switch failed, disconnecting\n");
949 ieee80211_queue_work(&sdata->local->hw,
950 &ifmgd->csa_connection_drop_work);
951 } else {
952 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200953 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200954}
955EXPORT_SYMBOL(ieee80211_chswitch_done);
956
Johannes Bergcc32abd2009-05-15 11:52:31 +0200957static void ieee80211_chswitch_timer(unsigned long data)
958{
959 struct ieee80211_sub_if_data *sdata =
960 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200961
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +0100962 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200963}
964
Johannes Berg37799e52013-03-26 14:02:26 +0100965static void
Johannes Berg4a3cb702013-02-12 16:43:19 +0100966ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Johannes Berg04a161f2013-05-03 09:35:35 +0200967 u64 timestamp, struct ieee802_11_elems *elems,
968 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200969{
Johannes Bergb4f286a12013-03-26 14:13:58 +0100970 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200971 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +0100972 struct cfg80211_bss *cbss = ifmgd->associated;
Johannes Berg55de9082012-07-26 17:24:39 +0200973 struct ieee80211_chanctx *chanctx;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200974 enum ieee80211_band current_band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700975 struct ieee80211_csa_ie csa_ie;
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200976 int res;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200977
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200978 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200979
Johannes Berg37799e52013-03-26 14:02:26 +0100980 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200981 return;
982
Johannes Bergb4f286a12013-03-26 14:13:58 +0100983 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200984 return;
985
Johannes Berg37799e52013-03-26 14:02:26 +0100986 /* disregard subsequent announcements if we are already processing */
Johannes Bergcc32abd2009-05-15 11:52:31 +0200987 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
988 return;
989
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200990 current_band = cbss->channel->band;
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700991 memset(&csa_ie, 0, sizeof(csa_ie));
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200992 res = ieee80211_parse_ch_switch_ie(sdata, elems, beacon, current_band,
993 ifmgd->flags,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -0700994 ifmgd->associated->bssid, &csa_ie);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200995 if (res < 0)
Johannes Bergb4f286a12013-03-26 14:13:58 +0100996 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +0200997 &ifmgd->csa_connection_drop_work);
Simon Wunderliche6b7cde2013-08-28 13:41:29 +0200998 if (res)
Johannes Bergcc32abd2009-05-15 11:52:31 +0200999 return;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001000
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001001 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
Johannes Berg85220d72013-03-25 18:29:27 +01001002 IEEE80211_CHAN_DISABLED)) {
1003 sdata_info(sdata,
1004 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02001005 ifmgd->associated->bssid,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001006 csa_ie.chandef.chan->center_freq,
1007 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
1008 csa_ie.chandef.center_freq2);
Johannes Berg85220d72013-03-25 18:29:27 +01001009 ieee80211_queue_work(&local->hw,
1010 &ifmgd->csa_connection_drop_work);
1011 return;
1012 }
1013
Johannes Berg57eebdf2012-08-01 15:50:46 +02001014 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
Arik Nemtsov7578d572013-09-01 17:15:51 +03001015 sdata->vif.csa_active = true;
Johannes Berg55de9082012-07-26 17:24:39 +02001016
Johannes Bergb4f286a12013-03-26 14:13:58 +01001017 mutex_lock(&local->chanctx_mtx);
Arik Nemtsov7578d572013-09-01 17:15:51 +03001018 if (local->use_chanctx) {
1019 u32 num_chanctx = 0;
1020 list_for_each_entry(chanctx, &local->chanctx_list, list)
1021 num_chanctx++;
1022
1023 if (num_chanctx > 1 ||
1024 !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) {
1025 sdata_info(sdata,
1026 "not handling chan-switch with channel contexts\n");
1027 ieee80211_queue_work(&local->hw,
1028 &ifmgd->csa_connection_drop_work);
1029 mutex_unlock(&local->chanctx_mtx);
1030 return;
1031 }
1032 }
1033
Johannes Berg55de9082012-07-26 17:24:39 +02001034 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
Arik Nemtsov7578d572013-09-01 17:15:51 +03001035 ieee80211_queue_work(&local->hw,
1036 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001037 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001038 return;
1039 }
1040 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1041 struct ieee80211_chanctx, conf);
1042 if (chanctx->refcount > 1) {
1043 sdata_info(sdata,
1044 "channel switch with multiple interfaces on the same channel, disconnecting\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001045 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001046 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001047 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001048 return;
1049 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001050 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001051
Luciano Coelho33787fc2013-11-11 20:34:54 +02001052 sdata->csa_chandef = csa_ie.chandef;
Johannes Berg55de9082012-07-26 17:24:39 +02001053
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001054 if (csa_ie.mode)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001055 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001056 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001057 IEEE80211_QUEUE_STOP_REASON_CSA);
1058
Johannes Bergb4f286a12013-03-26 14:13:58 +01001059 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001060 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001061 struct ieee80211_channel_switch ch_switch = {
1062 .timestamp = timestamp,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001063 .block_tx = csa_ie.mode,
1064 .chandef = csa_ie.chandef,
1065 .count = csa_ie.count,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001066 };
1067
Johannes Bergb4f286a12013-03-26 14:13:58 +01001068 drv_channel_switch(local, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001069 return;
1070 }
1071
1072 /* channel switch handled in software */
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001073 if (csa_ie.count <= 1)
Johannes Bergb4f286a12013-03-26 14:13:58 +01001074 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001075 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001076 mod_timer(&ifmgd->chswitch_timer,
Chun-Yeow Yeohc0f17eb2013-10-14 19:08:29 -07001077 TU_TO_EXP_TIME(csa_ie.count * cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001078}
1079
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001080static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1081 struct ieee80211_channel *channel,
1082 const u8 *country_ie, u8 country_ie_len,
1083 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001084{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001085 struct ieee80211_country_ie_triplet *triplet;
1086 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1087 int i, chan_pwr, chan_increment, new_ap_level;
1088 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001089
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001090 /* Invalid IE */
1091 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001092 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001093
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001094 triplet = (void *)(country_ie + 3);
1095 country_ie_len -= 3;
1096
1097 switch (channel->band) {
1098 default:
1099 WARN_ON_ONCE(1);
1100 /* fall through */
1101 case IEEE80211_BAND_2GHZ:
1102 case IEEE80211_BAND_60GHZ:
1103 chan_increment = 1;
1104 break;
1105 case IEEE80211_BAND_5GHZ:
1106 chan_increment = 4;
1107 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001108 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001109
1110 /* find channel */
1111 while (country_ie_len >= 3) {
1112 u8 first_channel = triplet->chans.first_channel;
1113
1114 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1115 goto next;
1116
1117 for (i = 0; i < triplet->chans.num_channels; i++) {
1118 if (first_channel + i * chan_increment == chan) {
1119 have_chan_pwr = true;
1120 chan_pwr = triplet->chans.max_power;
1121 break;
1122 }
1123 }
1124 if (have_chan_pwr)
1125 break;
1126
1127 next:
1128 triplet++;
1129 country_ie_len -= 3;
1130 }
1131
1132 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001133 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001134
1135 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1136
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001137 if (sdata->ap_power_level == new_ap_level)
1138 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001139
1140 sdata_info(sdata,
1141 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1142 new_ap_level, chan_pwr, *pwr_constr_elem,
1143 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001144 sdata->ap_power_level = new_ap_level;
1145 if (__ieee80211_recalc_txpower(sdata))
1146 return BSS_CHANGED_TXPOWER;
1147 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001148}
1149
Johannes Berg965beda2009-04-16 13:17:24 +02001150/* powersave */
1151static void ieee80211_enable_ps(struct ieee80211_local *local,
1152 struct ieee80211_sub_if_data *sdata)
1153{
1154 struct ieee80211_conf *conf = &local->hw.conf;
1155
Johannes Bergd5edaed2009-04-22 23:02:51 +02001156 /*
1157 * If we are scanning right now then the parameters will
1158 * take effect when scan finishes.
1159 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001160 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001161 return;
1162
Johannes Berg965beda2009-04-16 13:17:24 +02001163 if (conf->dynamic_ps_timeout > 0 &&
1164 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1165 mod_timer(&local->dynamic_ps_timer, jiffies +
1166 msecs_to_jiffies(conf->dynamic_ps_timeout));
1167 } else {
1168 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1169 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301170
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001171 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1172 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1173 return;
1174
1175 conf->flags |= IEEE80211_CONF_PS;
1176 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001177 }
1178}
1179
1180static void ieee80211_change_ps(struct ieee80211_local *local)
1181{
1182 struct ieee80211_conf *conf = &local->hw.conf;
1183
1184 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001185 ieee80211_enable_ps(local, local->ps_sdata);
1186 } else if (conf->flags & IEEE80211_CONF_PS) {
1187 conf->flags &= ~IEEE80211_CONF_PS;
1188 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1189 del_timer_sync(&local->dynamic_ps_timer);
1190 cancel_work_sync(&local->dynamic_ps_enable_work);
1191 }
1192}
1193
Jason Young808118c2011-03-10 16:43:19 -08001194static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1195{
1196 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1197 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001198 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001199
1200 if (!mgd->powersave)
1201 return false;
1202
Johannes Berg05cb9102011-10-28 11:59:47 +02001203 if (mgd->broken_ap)
1204 return false;
1205
Jason Young808118c2011-03-10 16:43:19 -08001206 if (!mgd->associated)
1207 return false;
1208
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001209 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
Jason Young808118c2011-03-10 16:43:19 -08001210 return false;
1211
Alexander Bondar989c6502013-05-16 17:34:17 +03001212 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001213 return false;
1214
Jason Young808118c2011-03-10 16:43:19 -08001215 rcu_read_lock();
1216 sta = sta_info_get(sdata, mgd->bssid);
1217 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001218 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001219 rcu_read_unlock();
1220
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001221 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001222}
1223
Johannes Berg965beda2009-04-16 13:17:24 +02001224/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001225void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001226{
1227 struct ieee80211_sub_if_data *sdata, *found = NULL;
1228 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001229 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001230
1231 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1232 local->ps_sdata = NULL;
1233 return;
1234 }
1235
1236 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001237 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001238 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301239 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1240 /* If an AP vif is found, then disable PS
1241 * by setting the count to zero thereby setting
1242 * ps_sdata to NULL.
1243 */
1244 count = 0;
1245 break;
1246 }
Johannes Berg965beda2009-04-16 13:17:24 +02001247 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1248 continue;
1249 found = sdata;
1250 count++;
1251 }
1252
Jason Young808118c2011-03-10 16:43:19 -08001253 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001254 s32 beaconint_us;
1255
1256 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001257 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001258
1259 beaconint_us = ieee80211_tu_to_usec(
1260 found->vif.bss_conf.beacon_int);
1261
Juuso Oikarinenff616382010-06-09 09:51:52 +03001262 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001263 if (timeout < 0) {
1264 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001265 * Go to full PSM if the user configures a very low
1266 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001267 * The 2000 second value is there for compatibility
1268 * until the PM_QOS_NETWORK_LATENCY is configured
1269 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001270 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001271 if (latency > (1900 * USEC_PER_MSEC) &&
1272 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001273 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001274 else
1275 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001276 }
Johannes Berg09b85562013-02-06 23:07:41 +01001277 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001278
Johannes Berg04fe2032009-04-22 18:44:37 +02001279 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001280 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001281 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001282 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001283 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001284
1285 /* If the TIM IE is invalid, pretend the value is 1 */
1286 if (!dtimper)
1287 dtimper = 1;
1288 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001289 maxslp = min_t(int, dtimper,
1290 latency / beaconint_us);
1291
Johannes Berg9ccebe62009-04-23 10:32:36 +02001292 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001293 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001294 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001295 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001296 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001297 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001298 }
Johannes Berg965beda2009-04-16 13:17:24 +02001299
1300 ieee80211_change_ps(local);
1301}
1302
Eliad Pellerab095872012-07-27 12:33:22 +03001303void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1304{
1305 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1306
1307 if (sdata->vif.bss_conf.ps != ps_allowed) {
1308 sdata->vif.bss_conf.ps = ps_allowed;
1309 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1310 }
1311}
1312
Johannes Berg965beda2009-04-16 13:17:24 +02001313void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1314{
1315 struct ieee80211_local *local =
1316 container_of(work, struct ieee80211_local,
1317 dynamic_ps_disable_work);
1318
1319 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1320 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1321 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1322 }
1323
1324 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001325 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001326 IEEE80211_QUEUE_STOP_REASON_PS);
1327}
1328
1329void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1330{
1331 struct ieee80211_local *local =
1332 container_of(work, struct ieee80211_local,
1333 dynamic_ps_enable_work);
1334 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001335 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301336 unsigned long flags;
1337 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001338
1339 /* can only happen when PS was just disabled anyway */
1340 if (!sdata)
1341 return;
1342
Christian Lamparter5e340692011-06-30 21:08:43 +02001343 ifmgd = &sdata->u.mgd;
1344
Johannes Berg965beda2009-04-16 13:17:24 +02001345 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1346 return;
1347
Johannes Berg09b85562013-02-06 23:07:41 +01001348 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001349 /* don't enter PS if TX frames are pending */
1350 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301351 mod_timer(&local->dynamic_ps_timer, jiffies +
1352 msecs_to_jiffies(
1353 local->hw.conf.dynamic_ps_timeout));
1354 return;
1355 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001356
1357 /*
1358 * transmission can be stopped by others which leads to
1359 * dynamic_ps_timer expiry. Postpone the ps timer if it
1360 * is not the actual idle state.
1361 */
1362 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1363 for (q = 0; q < local->hw.queues; q++) {
1364 if (local->queue_stop_reasons[q]) {
1365 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1366 flags);
1367 mod_timer(&local->dynamic_ps_timer, jiffies +
1368 msecs_to_jiffies(
1369 local->hw.conf.dynamic_ps_timeout));
1370 return;
1371 }
1372 }
1373 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301374 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301375
Vivek Natarajan375177b2010-02-09 14:50:28 +05301376 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301377 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001378 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301379 mod_timer(&local->dynamic_ps_timer, jiffies +
1380 msecs_to_jiffies(
1381 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001382 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301383 ieee80211_send_nullfunc(local, sdata, 1);
1384 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001385 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301386 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301387 }
1388
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001389 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1390 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301391 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1392 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1393 local->hw.conf.flags |= IEEE80211_CONF_PS;
1394 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1395 }
Johannes Berg965beda2009-04-16 13:17:24 +02001396}
1397
1398void ieee80211_dynamic_ps_timer(unsigned long data)
1399{
1400 struct ieee80211_local *local = (void *) data;
1401
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001402 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001403 return;
1404
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001405 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001406}
1407
Simon Wunderlich164eb022013-02-08 18:16:20 +01001408void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1409{
1410 struct delayed_work *delayed_work =
1411 container_of(work, struct delayed_work, work);
1412 struct ieee80211_sub_if_data *sdata =
1413 container_of(delayed_work, struct ieee80211_sub_if_data,
1414 dfs_cac_timer_work);
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01001415 struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
Simon Wunderlich164eb022013-02-08 18:16:20 +01001416
Johannes Berg34a37402013-12-18 09:43:33 +01001417 mutex_lock(&sdata->local->mtx);
1418 if (sdata->wdev.cac_started) {
1419 ieee80211_vif_release_channel(sdata);
1420 cfg80211_cac_event(sdata->dev, &chandef,
1421 NL80211_RADAR_CAC_FINISHED,
1422 GFP_KERNEL);
1423 }
1424 mutex_unlock(&sdata->local->mtx);
Simon Wunderlich164eb022013-02-08 18:16:20 +01001425}
1426
Johannes Berg60f8b392008-09-08 17:44:22 +02001427/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001428static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001429 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001430 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001431{
Jiri Bencf0706e82007-05-05 11:45:53 -07001432 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001433 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001434 size_t left;
1435 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001436 const u8 *pos;
1437 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001438
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001439 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001440 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001441
Johannes Berg32c50572012-03-28 11:04:29 +02001442 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001443 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001444
1445 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001446 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001447
Jiri Bencf0706e82007-05-05 11:45:53 -07001448 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001449 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001450
1451 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001452 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001453
Jiri Bencf0706e82007-05-05 11:45:53 -07001454 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001455 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001456 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001457 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001458
1459 pos = wmm_param + 8;
1460 left = wmm_param_len - 8;
1461
1462 memset(&params, 0, sizeof(params));
1463
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001464 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001465 for (; left >= 4; left -= 4, pos += 4) {
1466 int aci = (pos[0] >> 5) & 0x03;
1467 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001468 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001469 int queue;
1470
1471 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001472 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001473 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001474 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001475 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001476 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1477 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001478 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001479 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001480 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001481 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001482 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001483 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1484 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001485 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001486 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001487 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001488 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001489 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001490 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1491 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001492 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001493 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001494 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001495 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001496 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001497 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001498 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1499 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001500 break;
1501 }
1502
1503 params.aifs = pos[0] & 0x0f;
1504 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1505 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001506 params.txop = get_unaligned_le16(pos + 2);
Alexander Bondar908f8d02013-04-07 09:53:30 +03001507 params.acm = acm;
Kalle Valoab133152010-01-12 10:42:31 +02001508 params.uapsd = uapsd;
1509
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001510 mlme_dbg(sdata,
1511 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1512 queue, aci, acm,
1513 params.aifs, params.cw_min, params.cw_max,
1514 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001515 sdata->tx_conf[queue] = params;
1516 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001517 sdata_err(sdata,
1518 "failed to set TX queue parameters for queue %d\n",
1519 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001520 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001521
1522 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001523 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001524 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001525}
1526
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001527static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1528{
1529 lockdep_assert_held(&sdata->local->mtx);
1530
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001531 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001532 ieee80211_run_deferred_scan(sdata->local);
1533}
1534
1535static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1536{
1537 mutex_lock(&sdata->local->mtx);
1538 __ieee80211_stop_poll(sdata);
1539 mutex_unlock(&sdata->local->mtx);
1540}
1541
Johannes Berg7a5158e2008-10-08 10:59:33 +02001542static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1543 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001544{
Johannes Bergbda39332008-10-11 01:51:51 +02001545 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001546 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001547 bool use_protection;
1548 bool use_short_preamble;
1549 bool use_short_slot;
1550
1551 if (erp_valid) {
1552 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1553 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1554 } else {
1555 use_protection = false;
1556 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1557 }
1558
1559 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001560 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001561 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001562
Johannes Berg471b3ef2007-12-28 14:32:58 +01001563 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001564 bss_conf->use_cts_prot = use_protection;
1565 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001566 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001567
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001568 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001569 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001570 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001571 }
Daniel Draked9430a32007-07-27 15:43:24 +02001572
Johannes Berg7a5158e2008-10-08 10:59:33 +02001573 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001574 bss_conf->use_short_slot = use_short_slot;
1575 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001576 }
1577
1578 return changed;
1579}
1580
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001581static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001582 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001583 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001584{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001585 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001586 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001587 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001588
Johannes Bergae5eb022008-10-14 16:58:37 +02001589 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001590 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001591 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001592
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001593 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001594 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001595
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001596 sdata->u.mgd.associated = cbss;
1597 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001598
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001599 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1600
Johannes Berg488dd7b2012-10-29 20:08:01 +01001601 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001602 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001603
Johannes Berg9caf0362012-11-29 01:25:20 +01001604 rcu_read_lock();
1605 ies = rcu_dereference(cbss->ies);
1606 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001607 int ret;
1608
1609 ret = cfg80211_get_p2p_attr(
1610 ies->data, ies->len,
1611 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001612 (u8 *) &bss_conf->p2p_noa_attr,
1613 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001614 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001615 sdata->u.mgd.p2p_noa_index =
1616 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001617 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001618 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001619 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001620 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001621 }
1622
Johannes Bergb291ba12009-07-10 15:29:03 +02001623 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001624 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001625
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001626 ieee80211_led_assoc(local, 1);
1627
Alexander Bondar989c6502013-05-16 17:34:17 +03001628 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001629 /*
1630 * If the AP is buggy we may get here with no DTIM period
1631 * known, so assume it's 1 which is the only safe assumption
1632 * in that case, although if the TIM IE is broken powersave
1633 * probably just won't work at all.
1634 */
1635 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03001636 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03001637 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02001638 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03001639 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02001640 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001641 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001642
Juuso Oikarinen68542962010-06-09 13:43:26 +03001643 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001644
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001645 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001646 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001647 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001648 bss_info_changed |= BSS_CHANGED_CQM;
1649
Juuso Oikarinen68542962010-06-09 13:43:26 +03001650 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001651 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001652 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001653
Johannes Bergae5eb022008-10-14 16:58:37 +02001654 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001655
Johannes Berg056508d2009-07-30 21:43:55 +02001656 mutex_lock(&local->iflist_mtx);
1657 ieee80211_recalc_ps(local, -1);
1658 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001659
Johannes Berg04ecd252012-09-11 14:34:12 +02001660 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001661 ieee80211_recalc_ps_vif(sdata);
1662
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001663 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001664}
1665
Jouni Malinene69e95d2010-03-29 23:29:31 -07001666static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001667 u16 stype, u16 reason, bool tx,
1668 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001669{
Johannes Berg46900292009-02-15 12:44:28 +01001670 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001671 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001672 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001673
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001674 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001675
Johannes Berg37ad3882012-02-24 13:50:54 +01001676 if (WARN_ON_ONCE(tx && !frame_buf))
1677 return;
1678
Johannes Bergb291ba12009-07-10 15:29:03 +02001679 if (WARN_ON(!ifmgd->associated))
1680 return;
1681
David Spinadel79543d82012-06-12 09:59:45 +03001682 ieee80211_stop_poll(sdata);
1683
Johannes Berg77fdaa12009-07-07 03:45:17 +02001684 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001685 netif_carrier_off(sdata->dev);
1686
Eliad Peller88bc40e2012-07-12 17:35:33 +03001687 /*
1688 * if we want to get out of ps before disassoc (why?) we have
1689 * to do it before sending disassoc, as otherwise the null-packet
1690 * won't be valid.
1691 */
1692 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1693 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1694 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1695 }
1696 local->ps_sdata = NULL;
1697
Eliad Pellerab095872012-07-27 12:33:22 +03001698 /* disable per-vif ps */
1699 ieee80211_recalc_ps_vif(sdata);
1700
Eliad Pellerf8239812012-06-27 14:18:22 +03001701 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1702 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001703 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001704
Johannes Berg37ad3882012-02-24 13:50:54 +01001705 /* deauthenticate/disassociate now */
1706 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001707 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1708 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001709
1710 /* flush out frame */
1711 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001712 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001713
Eliad Peller88a9e312012-06-01 11:14:03 +03001714 /* clear bssid only after building the needed mgmt frames */
1715 memset(ifmgd->bssid, 0, ETH_ALEN);
1716
Johannes Berg37ad3882012-02-24 13:50:54 +01001717 /* remove AP and TDLS peers */
Johannes Bergd34ba212013-12-04 22:46:11 +01001718 sta_info_flush(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001719
1720 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001721 changed |= ieee80211_reset_erp_info(sdata);
1722
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001723 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001724 changed |= BSS_CHANGED_ASSOC;
1725 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001726
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001727 ifmgd->p2p_noa_index = -1;
1728 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1729 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001730
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001731 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001732 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1733 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001734 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1735 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001736
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001737 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301738
Kalle Valo520eb822008-12-18 23:35:27 +02001739 del_timer_sync(&local->dynamic_ps_timer);
1740 cancel_work_sync(&local->dynamic_ps_enable_work);
1741
Juuso Oikarinen68542962010-06-09 13:43:26 +03001742 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001743 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001744 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001745
Johannes Berg3abead52012-03-02 15:56:59 +01001746 sdata->vif.bss_conf.qos = false;
1747 changed |= BSS_CHANGED_QOS;
1748
Johannes Berg0aaffa92010-05-05 15:28:27 +02001749 /* The BSSID (not really interesting) and HT changed */
1750 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001751 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001752
Johannes Berg3abead52012-03-02 15:56:59 +01001753 /* disassociated - set to defaults now */
1754 ieee80211_set_wmm_default(sdata, false);
1755
Johannes Bergb9dcf712010-08-27 12:35:54 +02001756 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1757 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1758 del_timer_sync(&sdata->u.mgd.timer);
1759 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001760
Johannes Berg826262c2012-12-10 16:38:14 +02001761 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03001762 sdata->vif.bss_conf.beacon_rate = NULL;
1763
Alexander Bondar989c6502013-05-16 17:34:17 +03001764 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02001765
Johannes Berg028e8da02012-11-26 11:57:41 +01001766 ifmgd->flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01001767 mutex_lock(&local->mtx);
Johannes Berg028e8da02012-11-26 11:57:41 +01001768 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01001769 mutex_unlock(&local->mtx);
Max Stepanov2475b1cc2013-03-24 14:23:27 +02001770
1771 sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001772}
Jiri Bencf0706e82007-05-05 11:45:53 -07001773
Kalle Valo3cf335d2009-03-22 21:57:06 +02001774void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1775 struct ieee80211_hdr *hdr)
1776{
1777 /*
1778 * We can postpone the mgd.timer whenever receiving unicast frames
1779 * from AP because we know that the connection is working both ways
1780 * at that time. But multicast frames (and hence also beacons) must
1781 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001782 * data idle periods for sending the periodic probe request to the
1783 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001784 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001785 if (is_multicast_ether_addr(hdr->addr1))
1786 return;
1787
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001788 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001789}
Jiri Bencf0706e82007-05-05 11:45:53 -07001790
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001791static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1792{
1793 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001794 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001795
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001796 mutex_lock(&local->mtx);
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001797 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
1798 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001799
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001800 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001801
1802 mutex_lock(&local->iflist_mtx);
1803 ieee80211_recalc_ps(local, -1);
1804 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001805
1806 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001807 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001808
1809 /*
1810 * We've received a probe response, but are not sure whether
1811 * we have or will be receiving any beacons or data, so let's
1812 * schedule the timers again, just in case.
1813 */
1814 ieee80211_sta_reset_beacon_monitor(sdata);
1815
1816 mod_timer(&ifmgd->conn_mon_timer,
1817 round_jiffies_up(jiffies +
1818 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001819out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001820 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001821}
1822
1823void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001824 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001825{
Felix Fietkau75706d02010-12-02 21:01:07 +01001826 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001827 return;
1828
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001829 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1830 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001831 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001832 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001833 else
1834 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001835 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001836 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001837 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001838
1839 if (ack)
1840 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001841}
1842
Maxim Levitskya43abf22009-07-31 18:54:12 +03001843static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1844{
1845 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1846 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001847 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08001848 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001849
1850 /*
1851 * Try sending broadcast probe requests for the last three
1852 * probe requests after the first ones failed since some
1853 * buggy APs only support broadcast probe requests.
1854 */
1855 if (ifmgd->probe_send_count >= unicast_limit)
1856 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03001857
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001858 /*
1859 * When the hardware reports an accurate Tx ACK status, it's
1860 * better to send a nullfunc frame instead of a probe request,
1861 * as it will kick us off the AP quickly if we aren't associated
1862 * anymore. The timeout will be reset if the frame is ACKed by
1863 * the AP.
1864 */
Soumik Das992e68b2012-05-20 15:31:13 +05301865 ifmgd->probe_send_count++;
1866
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001867 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
1868 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001869 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001870 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001871 int ssid_len;
1872
Johannes Berg9caf0362012-11-29 01:25:20 +01001873 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001874 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001875 if (WARN_ON_ONCE(ssid == NULL))
1876 ssid_len = 0;
1877 else
1878 ssid_len = ssid[1];
1879
1880 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01001881 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02001882 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01001883 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001884 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03001885
Ben Greear180205b2011-02-04 15:30:24 -08001886 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001887 run_again(sdata, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05301888 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01001889 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03001890}
1891
Johannes Bergb291ba12009-07-10 15:29:03 +02001892static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
1893 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02001894{
Kalle Valo04de8382009-03-22 21:57:35 +02001895 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02001896 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02001897
Johannes Berg9607e6b2009-12-23 13:15:31 +01001898 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02001899 return;
1900
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001901 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001902
1903 if (!ifmgd->associated)
1904 goto out;
1905
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001906 mutex_lock(&sdata->local->mtx);
1907
1908 if (sdata->local->tmp_channel || sdata->local->scanning) {
1909 mutex_unlock(&sdata->local->mtx);
1910 goto out;
1911 }
1912
Ben Greeara13fbe52013-03-25 11:19:35 -07001913 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001914 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07001915 "detected beacon loss from AP (missed %d beacons) - probing\n",
1916 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001917
Ben Greeara13fbe52013-03-25 11:19:35 -07001918 ieee80211_cqm_rssi_notify(&sdata->vif,
1919 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
1920 GFP_KERNEL);
1921 }
Kalle Valo04de8382009-03-22 21:57:35 +02001922
Johannes Bergb291ba12009-07-10 15:29:03 +02001923 /*
1924 * The driver/our work has already reported this event or the
1925 * connection monitoring has kicked in and we have already sent
1926 * a probe request. Or maybe the AP died and the driver keeps
1927 * reporting until we disassociate...
1928 *
1929 * In either case we have to ignore the current call to this
1930 * function (except for setting the correct probe reason bit)
1931 * because otherwise we would reset the timer every time and
1932 * never check whether we received a probe response!
1933 */
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02001934 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
Johannes Bergb291ba12009-07-10 15:29:03 +02001935 already = true;
1936
Eliad Peller12b5f342013-11-18 19:06:46 +02001937 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
1938
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001939 mutex_unlock(&sdata->local->mtx);
1940
Johannes Bergb291ba12009-07-10 15:29:03 +02001941 if (already)
1942 goto out;
1943
Johannes Berg4e751842009-06-10 15:16:52 +02001944 mutex_lock(&sdata->local->iflist_mtx);
1945 ieee80211_recalc_ps(sdata->local, -1);
1946 mutex_unlock(&sdata->local->iflist_mtx);
1947
Maxim Levitskya43abf22009-07-31 18:54:12 +03001948 ifmgd->probe_send_count = 0;
1949 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001950 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001951 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02001952}
1953
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001954struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
1955 struct ieee80211_vif *vif)
1956{
1957 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001959 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001960 struct sk_buff *skb;
1961 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001962 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001963
1964 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1965 return NULL;
1966
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001967 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001968
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001969 if (ifmgd->associated)
1970 cbss = ifmgd->associated;
1971 else if (ifmgd->auth_data)
1972 cbss = ifmgd->auth_data->bss;
1973 else if (ifmgd->assoc_data)
1974 cbss = ifmgd->assoc_data->bss;
1975 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001976 return NULL;
1977
Johannes Berg9caf0362012-11-29 01:25:20 +01001978 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001979 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02001980 if (WARN_ON_ONCE(ssid == NULL))
1981 ssid_len = 0;
1982 else
1983 ssid_len = ssid[1];
1984
Eliad Pellerd9b3b282012-06-28 15:03:13 +03001985 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02001986 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02001987 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02001988 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01001989 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02001990
1991 return skb;
1992}
1993EXPORT_SYMBOL(ieee80211_ap_probereq_get);
1994
Johannes Bergeef9e542013-01-29 13:09:34 +01001995static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001996{
1997 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02001998 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02001999
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002000 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002001 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002002 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002003 return;
2004 }
2005
Johannes Berg37ad3882012-02-24 13:50:54 +01002006 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2007 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002008 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002009 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Arik Nemtsov7578d572013-09-01 17:15:51 +03002010 sdata->vif.csa_active = false;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002011 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002012 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002013 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002014
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002015 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
2016 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002017 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002018}
2019
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002020static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002021{
2022 struct ieee80211_sub_if_data *sdata =
2023 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002024 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002025 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2026 struct sta_info *sta;
2027
2028 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302029 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002030 sta = sta_info_get(sdata, ifmgd->bssid);
2031 if (sta)
2032 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302033 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002034 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002035
Johannes Berg682bd382013-01-29 13:13:50 +01002036 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002037 sdata_info(sdata, "Connection to AP %pM lost\n",
2038 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002039 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002040 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002041 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002042 }
2043}
2044
2045static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2046{
2047 struct ieee80211_sub_if_data *sdata =
2048 container_of(work, struct ieee80211_sub_if_data,
2049 u.mgd.csa_connection_drop_work);
2050
Johannes Bergeef9e542013-01-29 13:09:34 +01002051 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002052}
2053
Kalle Valo04de8382009-03-22 21:57:35 +02002054void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2055{
2056 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002057 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002058
Johannes Bergb5878a22010-04-07 16:48:40 +02002059 trace_api_beacon_loss(sdata);
2060
Johannes Berg682bd382013-01-29 13:13:50 +01002061 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002062 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002063}
2064EXPORT_SYMBOL(ieee80211_beacon_loss);
2065
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002066void ieee80211_connection_loss(struct ieee80211_vif *vif)
2067{
2068 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2069 struct ieee80211_hw *hw = &sdata->local->hw;
2070
Johannes Bergb5878a22010-04-07 16:48:40 +02002071 trace_api_connection_loss(sdata);
2072
Johannes Berg682bd382013-01-29 13:13:50 +01002073 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002074 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2075}
2076EXPORT_SYMBOL(ieee80211_connection_loss);
2077
2078
Johannes Berg66e67e42012-01-20 13:55:27 +01002079static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2080 bool assoc)
2081{
2082 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2083
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002084 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002085
Johannes Berg66e67e42012-01-20 13:55:27 +01002086 if (!assoc) {
2087 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2088
2089 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2090 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002091 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002092 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002093 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002094 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002095 }
2096
Johannes Berg5b112d32013-02-01 01:49:58 +01002097 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002098 kfree(auth_data);
2099 sdata->u.mgd.auth_data = NULL;
2100}
2101
2102static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2103 struct ieee80211_mgmt *mgmt, size_t len)
2104{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002105 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002106 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2107 u8 *pos;
2108 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002109 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002110
2111 pos = mgmt->u.auth.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002112 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002113 if (!elems.challenge)
2114 return;
2115 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002116 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002117 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2118 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2119 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002120 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002121 elems.challenge - 2, elems.challenge_len + 2,
2122 auth_data->bss->bssid, auth_data->bss->bssid,
2123 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002124 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002125}
2126
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002127static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2128 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002129{
2130 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2131 u8 bssid[ETH_ALEN];
2132 u16 auth_alg, auth_transaction, status_code;
2133 struct sta_info *sta;
2134
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002135 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002136
2137 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002138 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002139
2140 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002141 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002142
2143 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2144
Joe Perchesb203ca32012-05-08 18:56:52 +00002145 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002146 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002147
2148 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2149 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2150 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2151
2152 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002153 auth_transaction != ifmgd->auth_data->expected_transaction) {
2154 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2155 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2156 auth_transaction,
2157 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002158 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002159 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002160
2161 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002162 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2163 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002164 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002165 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002166 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002167 }
2168
2169 switch (ifmgd->auth_data->algorithm) {
2170 case WLAN_AUTH_OPEN:
2171 case WLAN_AUTH_LEAP:
2172 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002173 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002174 break;
2175 case WLAN_AUTH_SHARED_KEY:
2176 if (ifmgd->auth_data->expected_transaction != 4) {
2177 ieee80211_auth_challenge(sdata, mgmt, len);
2178 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002179 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002180 }
2181 break;
2182 default:
2183 WARN_ONCE(1, "invalid auth alg %d",
2184 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002185 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002186 }
2187
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002188 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002189 ifmgd->auth_data->done = true;
2190 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002191 ifmgd->auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002192 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01002193
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002194 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2195 ifmgd->auth_data->expected_transaction != 2) {
2196 /*
2197 * Report auth frame to user space for processing since another
2198 * round of Authentication frames is still needed.
2199 */
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002200 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002201 return;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002202 }
2203
Johannes Berg66e67e42012-01-20 13:55:27 +01002204 /* move station state to auth */
2205 mutex_lock(&sdata->local->sta_mtx);
2206 sta = sta_info_get(sdata, bssid);
2207 if (!sta) {
2208 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2209 goto out_err;
2210 }
2211 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002212 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002213 goto out_err;
2214 }
2215 mutex_unlock(&sdata->local->sta_mtx);
2216
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002217 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002218 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002219 out_err:
2220 mutex_unlock(&sdata->local->sta_mtx);
2221 /* ignore frame -- wait for timeout */
Johannes Berg66e67e42012-01-20 13:55:27 +01002222}
2223
2224
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002225static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2226 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002227{
Johannes Berg46900292009-02-15 12:44:28 +01002228 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002229 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002230 u16 reason_code;
2231
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002232 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002233
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002234 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002235 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002236
Johannes Berg66e67e42012-01-20 13:55:27 +01002237 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002238 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002239 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002240
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002241 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002242
2243 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2244
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002245 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2246 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002247
Johannes Berg37ad3882012-02-24 13:50:54 +01002248 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2249
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002250 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002251}
2252
2253
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002254static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2255 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002256{
Johannes Berg46900292009-02-15 12:44:28 +01002257 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002258 u16 reason_code;
2259
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002260 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002261
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002262 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002263 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002264
Johannes Berg66e67e42012-01-20 13:55:27 +01002265 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002266 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002267 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002268
2269 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2270
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002271 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2272 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002273
Johannes Berg37ad3882012-02-24 13:50:54 +01002274 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2275
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002276 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002277}
2278
Christian Lamparterc74d0842011-10-15 00:14:49 +02002279static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2280 u8 *supp_rates, unsigned int supp_rates_len,
2281 u32 *rates, u32 *basic_rates,
2282 bool *have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002283 int *min_rate, int *min_rate_index,
2284 int shift, u32 rate_flags)
Christian Lamparterc74d0842011-10-15 00:14:49 +02002285{
2286 int i, j;
2287
2288 for (i = 0; i < supp_rates_len; i++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002289 int rate = supp_rates[i] & 0x7f;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002290 bool is_basic = !!(supp_rates[i] & 0x80);
2291
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002292 if ((rate * 5 * (1 << shift)) > 110)
Christian Lamparterc74d0842011-10-15 00:14:49 +02002293 *have_higher_than_11mbit = true;
2294
2295 /*
2296 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2297 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2298 *
2299 * Note: Even through the membership selector and the basic
2300 * rate flag share the same bit, they are not exactly
2301 * the same.
2302 */
2303 if (!!(supp_rates[i] & 0x80) &&
2304 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2305 continue;
2306
2307 for (j = 0; j < sband->n_bitrates; j++) {
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002308 struct ieee80211_rate *br;
2309 int brate;
2310
2311 br = &sband->bitrates[j];
2312 if ((rate_flags & br->flags) != rate_flags)
2313 continue;
2314
2315 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2316 if (brate == rate) {
Christian Lamparterc74d0842011-10-15 00:14:49 +02002317 *rates |= BIT(j);
2318 if (is_basic)
2319 *basic_rates |= BIT(j);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02002320 if ((rate * 5) < *min_rate) {
2321 *min_rate = rate * 5;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002322 *min_rate_index = j;
2323 }
2324 break;
2325 }
2326 }
2327 }
2328}
Jiri Bencf0706e82007-05-05 11:45:53 -07002329
Johannes Berg66e67e42012-01-20 13:55:27 +01002330static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2331 bool assoc)
2332{
2333 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2334
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002335 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002336
Johannes Berg66e67e42012-01-20 13:55:27 +01002337 if (!assoc) {
2338 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2339
2340 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2341 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002342 sdata->u.mgd.flags = 0;
Johannes Berg34a37402013-12-18 09:43:33 +01002343 mutex_lock(&sdata->local->mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02002344 ieee80211_vif_release_channel(sdata);
Johannes Berg34a37402013-12-18 09:43:33 +01002345 mutex_unlock(&sdata->local->mtx);
Johannes Berg66e67e42012-01-20 13:55:27 +01002346 }
2347
2348 kfree(assoc_data);
2349 sdata->u.mgd.assoc_data = NULL;
2350}
2351
2352static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2353 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002354 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002355{
Johannes Berg46900292009-02-15 12:44:28 +01002356 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002357 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002358 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002359 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002360 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002361 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002362 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002363 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02002364 const struct cfg80211_bss_ies *bss_ies = NULL;
2365 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Bergae5eb022008-10-14 16:58:37 +02002366 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002367 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02002368 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002369
Johannes Bergaf6b6372009-12-23 13:15:35 +01002370 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002371
Jiri Bencf0706e82007-05-05 11:45:53 -07002372 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002373 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002374
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002375 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002376 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2377 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002378 aid &= ~(BIT(15) | BIT(14));
2379
Johannes Berg05cb9102011-10-28 11:59:47 +02002380 ifmgd->broken_ap = false;
2381
2382 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002383 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2384 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002385 aid = 0;
2386 ifmgd->broken_ap = true;
2387 }
2388
Johannes Bergaf6b6372009-12-23 13:15:35 +01002389 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002390 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002391
Jiri Bencf0706e82007-05-05 11:45:53 -07002392 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002393 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002394 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002395 }
2396
Johannes Berg46900292009-02-15 12:44:28 +01002397 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002398
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002399 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02002400 * Some APs are erroneously not including some information in their
2401 * (re)association response frames. Try to recover by using the data
2402 * from the beacon or probe response. This seems to afflict mobile
2403 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2404 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2405 */
2406 if ((assoc_data->wmm && !elems.wmm_param) ||
2407 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2408 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2409 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2410 (!elems.vht_cap_elem || !elems.vht_operation))) {
2411 const struct cfg80211_bss_ies *ies;
2412 struct ieee802_11_elems bss_elems;
2413
2414 rcu_read_lock();
2415 ies = rcu_dereference(cbss->ies);
2416 if (ies)
2417 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2418 GFP_ATOMIC);
2419 rcu_read_unlock();
2420 if (!bss_ies)
2421 return false;
2422
2423 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2424 false, &bss_elems);
2425 if (assoc_data->wmm &&
2426 !elems.wmm_param && bss_elems.wmm_param) {
2427 elems.wmm_param = bss_elems.wmm_param;
2428 sdata_info(sdata,
2429 "AP bug: WMM param missing from AssocResp\n");
2430 }
2431
2432 /*
2433 * Also check if we requested HT/VHT, otherwise the AP doesn't
2434 * have to include the IEs in the (re)association response.
2435 */
2436 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2437 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2438 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2439 sdata_info(sdata,
2440 "AP bug: HT capability missing from AssocResp\n");
2441 }
2442 if (!elems.ht_operation && bss_elems.ht_operation &&
2443 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2444 elems.ht_operation = bss_elems.ht_operation;
2445 sdata_info(sdata,
2446 "AP bug: HT operation missing from AssocResp\n");
2447 }
2448 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2449 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2450 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2451 sdata_info(sdata,
2452 "AP bug: VHT capa missing from AssocResp\n");
2453 }
2454 if (!elems.vht_operation && bss_elems.vht_operation &&
2455 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2456 elems.vht_operation = bss_elems.vht_operation;
2457 sdata_info(sdata,
2458 "AP bug: VHT operation missing from AssocResp\n");
2459 }
2460 }
2461
2462 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002463 * We previously checked these in the beacon/probe response, so
2464 * they should be present here. This is just a safety net.
2465 */
2466 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2467 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2468 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002469 "HT AP is missing WMM params or HT capability/operation\n");
2470 ret = false;
2471 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002472 }
2473
2474 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2475 (!elems.vht_cap_elem || !elems.vht_operation)) {
2476 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002477 "VHT AP is missing VHT capability/operation\n");
2478 ret = false;
2479 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002480 }
2481
Guy Eilam2a33bee2011-08-17 15:18:15 +03002482 mutex_lock(&sdata->local->sta_mtx);
2483 /*
2484 * station info was already allocated and inserted before
2485 * the association and should be available to us
2486 */
Johannes Berg7852e362012-01-20 13:55:24 +01002487 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002488 if (WARN_ON(!sta)) {
2489 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002490 ret = false;
2491 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07002492 }
2493
Johannes Berg55de9082012-07-26 17:24:39 +02002494 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002495
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002496 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002497 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002498 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002499 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002500
Mahesh Palivela818255e2012-10-10 11:33:04 +00002501 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2502 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002503 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002504
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002505 /*
2506 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2507 * in their association response, so ignore that data for our own
2508 * configuration. If it changed since the last beacon, we'll get the
2509 * next beacon and update then.
2510 */
Johannes Berg11289582013-02-07 17:36:12 +01002511
Johannes Bergbee7f582013-02-07 22:24:55 +01002512 /*
2513 * If an operating mode notification IE is present, override the
2514 * NSS calculation (that would be done in rate_control_rate_init())
2515 * and use the # of streams from that element.
2516 */
2517 if (elems.opmode_notif &&
2518 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2519 u8 nss;
2520
2521 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2522 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2523 nss += 1;
2524 sta->sta.rx_nss = nss;
2525 }
2526
Johannes Berg4b7679a2008-09-18 18:14:18 +02002527 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002528
Johannes Berg46900292009-02-15 12:44:28 +01002529 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002530 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002531
Johannes Bergddf4ac52008-10-22 11:41:38 +02002532 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002533 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002534
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002535 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002536 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2537 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2538 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002539 sdata_info(sdata,
2540 "failed to move station %pM to desired state\n",
2541 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002542 WARN_ON(__sta_info_destroy(sta));
2543 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002544 ret = false;
2545 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01002546 }
2547
Johannes Berg7852e362012-01-20 13:55:24 +01002548 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002549
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002550 /*
2551 * Always handle WMM once after association regardless
2552 * of the first value the AP uses. Setting -1 here has
2553 * that effect because the AP values is an unsigned
2554 * 4-bit value.
2555 */
2556 ifmgd->wmm_last_param_set = -1;
2557
Johannes Berg095d81c2013-10-15 12:25:07 +02002558 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002559 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002560 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002561 else
Johannes Berg3abead52012-03-02 15:56:59 +01002562 ieee80211_set_wmm_default(sdata, false);
2563 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002564
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002565 /* set AID and assoc capability,
2566 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002567 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002568 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002569 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002570
Kalle Valo15b7b062009-03-22 21:57:14 +02002571 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002572 * If we're using 4-addr mode, let the AP know that we're
2573 * doing so, so that it can create the STA VLAN on its side
2574 */
2575 if (ifmgd->use_4addr)
2576 ieee80211_send_4addr_nullfunc(local, sdata);
2577
2578 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002579 * Start timer to probe the connection to the AP now.
2580 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002581 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002582 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002583 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002584
Johannes Berg35d865a2013-05-28 10:54:03 +02002585 ret = true;
2586 out:
2587 kfree(bss_ies);
2588 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002589}
2590
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002591static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2592 struct ieee80211_mgmt *mgmt,
2593 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002594{
2595 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2596 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2597 u16 capab_info, status_code, aid;
2598 struct ieee802_11_elems elems;
2599 u8 *pos;
2600 bool reassoc;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002601 struct cfg80211_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002602
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002603 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002604
2605 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002606 return;
Joe Perchesb203ca32012-05-08 18:56:52 +00002607 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002608 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002609
2610 /*
2611 * AssocResp and ReassocResp have identical structure, so process both
2612 * of them in this function.
2613 */
2614
2615 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002616 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002617
2618 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2619 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2620 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2621 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2622
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002623 sdata_info(sdata,
2624 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2625 reassoc ? "Rea" : "A", mgmt->sa,
2626 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002627
2628 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002629 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002630
2631 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002632 elems.timeout_int &&
2633 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002634 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002635 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002636 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002637 sdata_info(sdata,
2638 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2639 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002640 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002641 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002642 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002643 run_again(sdata, assoc_data->timeout);
2644 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002645 }
2646
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002647 bss = assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01002648
2649 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002650 sdata_info(sdata, "%pM denied association (code=%d)\n",
2651 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002652 ieee80211_destroy_assoc_data(sdata, false);
2653 } else {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002654 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002655 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002656 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02002657 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002658 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002659 }
John W. Linville635d9992012-07-09 16:34:34 -04002660 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002661
2662 /*
2663 * destroy assoc_data afterwards, as otherwise an idle
2664 * recalc after assoc_data is NULL but before associated
2665 * is set can cause the interface to go idle
2666 */
2667 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002668 }
2669
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002670 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002671}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002672
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002673static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002674 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002675 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002676 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002677{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002678 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002679 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002680 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002681 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002682
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002683 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01002684
Johannes Berg1cd8e882013-03-27 14:30:12 +01002685 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002686 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2687 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002688 else
2689 freq = rx_status->freq;
2690
2691 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2692
2693 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2694 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002695
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002696 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002697 channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03002698 if (bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002699 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03002700 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
2701 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002702}
2703
2704
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002705static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002706 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002707{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002708 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002709 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002710 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2711 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002712 struct ieee802_11_elems elems;
2713
Kalle Valo15b7b062009-03-22 21:57:14 +02002714 ifmgd = &sdata->u.mgd;
2715
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002716 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002717
Joe Perchesb203ca32012-05-08 18:56:52 +00002718 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002719 return; /* ignore ProbeResp to foreign address */
2720
Ester Kummerae6a44e2008-06-27 18:54:48 +03002721 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2722 if (baselen > len)
2723 return;
2724
2725 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002726 false, &elems);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002727
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002728 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002729
Johannes Berg77fdaa12009-07-07 03:45:17 +02002730 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002731 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002732 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002733
2734 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002735 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002736 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002737 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002738 ifmgd->auth_data->tries = 0;
2739 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002740 ifmgd->auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002741 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01002742 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002743}
2744
Johannes Bergd91f36d2009-04-16 13:17:26 +02002745/*
2746 * This is the canonical list of information elements we care about,
2747 * the filter code also gives us all changes to the Microsoft OUI
2748 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2749 *
2750 * We implement beacon filtering in software since that means we can
2751 * avoid processing the frame here and in cfg80211, and userspace
2752 * will not be able to tell whether the hardware supports it or not.
2753 *
2754 * XXX: This list needs to be dynamic -- userspace needs to be able to
2755 * add items it requires. It also needs to be able to tell us to
2756 * look out for other vendor IEs.
2757 */
2758static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002759 (1ULL << WLAN_EID_COUNTRY) |
2760 (1ULL << WLAN_EID_ERP_INFO) |
2761 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2762 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2763 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002764 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002765
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002766static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2767 struct ieee80211_mgmt *mgmt, size_t len,
2768 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002769{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002770 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002771 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002772 size_t baselen;
2773 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002774 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002775 struct ieee80211_chanctx_conf *chanctx_conf;
2776 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002777 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002778 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002779 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002780 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002781 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002782 u8 *bssid;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002783 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002784
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002785 sdata_assert_lock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07002786
Ester Kummerae6a44e2008-06-27 18:54:48 +03002787 /* Process beacon from the current BSS */
2788 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2789 if (baselen > len)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002790 return;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002791
Johannes Berg55de9082012-07-26 17:24:39 +02002792 rcu_read_lock();
2793 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2794 if (!chanctx_conf) {
2795 rcu_read_unlock();
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002796 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002797 }
2798
Johannes Berg4bf88532012-11-09 11:39:59 +01002799 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002800 rcu_read_unlock();
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002801 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002802 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002803 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002804 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002805
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002806 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002807 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002808 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002809 len - baselen, false, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002810
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002811 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Alexander Bondar482a9c72013-06-03 17:29:33 +03002812 if (elems.tim && !elems.parse_error) {
2813 const struct ieee80211_tim_ie *tim_ie = elems.tim;
2814 ifmgd->dtim_period = tim_ie->dtim_period;
2815 }
Alexander Bondar989c6502013-05-16 17:34:17 +03002816 ifmgd->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002817 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002818 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2819 sdata->vif.bss_conf.sync_tsf =
2820 le64_to_cpu(mgmt->u.beacon.timestamp);
2821 sdata->vif.bss_conf.sync_device_ts =
2822 rx_status->device_timestamp;
2823 if (elems.tim)
2824 sdata->vif.bss_conf.sync_dtim_count =
2825 elems.tim->dtim_count;
2826 else
2827 sdata->vif.bss_conf.sync_dtim_count = 0;
2828 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002829 /* continue assoc process */
2830 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002831 ifmgd->assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002832 run_again(sdata, ifmgd->assoc_data->timeout);
2833 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002834 }
2835
2836 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002837 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002838 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002839 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002840
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002841 /* Track average RSSI from the Beacon frames of the current AP */
2842 ifmgd->last_beacon_signal = rx_status->signal;
2843 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2844 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002845 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002846 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002847 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002848 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002849 } else {
2850 ifmgd->ave_beacon_signal =
2851 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2852 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2853 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002854 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002855 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002856
2857 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2858 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2859 int sig = ifmgd->ave_beacon_signal;
2860 int last_sig = ifmgd->last_ave_beacon_signal;
2861
2862 /*
2863 * if signal crosses either of the boundaries, invoke callback
2864 * with appropriate parameters
2865 */
2866 if (sig > ifmgd->rssi_max_thold &&
2867 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2868 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002869 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002870 } else if (sig < ifmgd->rssi_min_thold &&
2871 (last_sig >= ifmgd->rssi_max_thold ||
2872 last_sig == 0)) {
2873 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002874 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002875 }
2876 }
2877
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002878 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002879 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002880 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002881 int sig = ifmgd->ave_beacon_signal / 16;
2882 int last_event = ifmgd->last_cqm_event_signal;
2883 int thold = bss_conf->cqm_rssi_thold;
2884 int hyst = bss_conf->cqm_rssi_hyst;
2885 if (sig < thold &&
2886 (last_event == 0 || sig < last_event - hyst)) {
2887 ifmgd->last_cqm_event_signal = sig;
2888 ieee80211_cqm_rssi_notify(
2889 &sdata->vif,
2890 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2891 GFP_KERNEL);
2892 } else if (sig > thold &&
2893 (last_event == 0 || sig > last_event + hyst)) {
2894 ifmgd->last_cqm_event_signal = sig;
2895 ieee80211_cqm_rssi_notify(
2896 &sdata->vif,
2897 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2898 GFP_KERNEL);
2899 }
2900 }
2901
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002902 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002903 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002904 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02002905 ieee80211_reset_ap_probe(sdata);
Jouni Malinen92778182009-05-14 21:15:36 +03002906 }
2907
Johannes Bergb291ba12009-07-10 15:29:03 +02002908 /*
2909 * Push the beacon loss detection into the future since
2910 * we are processing a beacon from the AP just now.
2911 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002912 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002913
Johannes Bergd91f36d2009-04-16 13:17:26 +02002914 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
2915 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002916 len - baselen, false, &elems,
Johannes Bergd91f36d2009-04-16 13:17:26 +02002917 care_about_ies, ncrc);
2918
Vivek Natarajan25c9c872009-03-02 20:20:30 +05302919 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02002920 bool directed_tim = ieee80211_check_tim(elems.tim,
2921 elems.tim_len,
2922 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02002923 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02002924 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01002925 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
2926 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
2927 ieee80211_hw_config(local,
2928 IEEE80211_CONF_CHANGE_PS);
2929 }
Kalle Valo572e0012009-02-10 17:09:31 +02002930 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05302931 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02002932 local->pspolling = true;
2933
2934 /*
2935 * Here is assumed that the driver will be
2936 * able to send ps-poll frame and receive a
2937 * response even though power save mode is
2938 * enabled, but some drivers might require
2939 * to disable power save here. This needs
2940 * to be investigated.
2941 */
2942 ieee80211_send_pspoll(local, sdata);
2943 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08002944 }
2945 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02002946
Johannes Berg488dd7b2012-10-29 20:08:01 +01002947 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002948 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01002949 int ret;
2950
2951 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
2952 len - baselen,
2953 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01002954 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01002955 if (ret >= 2) {
2956 if (sdata->u.mgd.p2p_noa_index != noa.index) {
2957 /* valid noa_attr and index changed */
2958 sdata->u.mgd.p2p_noa_index = noa.index;
2959 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
2960 changed |= BSS_CHANGED_P2P_PS;
2961 /*
2962 * make sure we update all information, the CRC
2963 * mechanism doesn't look at P2P attributes.
2964 */
2965 ifmgd->beacon_crc_valid = false;
2966 }
2967 } else if (sdata->u.mgd.p2p_noa_index != -1) {
2968 /* noa_attr not found and we had valid noa_attr before */
2969 sdata->u.mgd.p2p_noa_index = -1;
2970 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01002971 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01002972 ifmgd->beacon_crc_valid = false;
2973 }
2974 }
2975
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002976 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002977 return;
Jouni Malinen30196672009-05-19 17:01:43 +03002978 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03002979 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03002980
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002981 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02002982
Johannes Bergd70b7612013-08-23 15:48:48 +02002983 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2984 &elems, true);
2985
Johannes Berg095d81c2013-10-15 12:25:07 +02002986 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
2987 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Johannes Berg7d257452012-07-06 17:37:43 +02002988 elems.wmm_param_len))
2989 changed |= BSS_CHANGED_QOS;
2990
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002991 /*
2992 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01002993 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002994 */
Alexander Bondar989c6502013-05-16 17:34:17 +03002995 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002996 /* a few bogus AP send dtim_period = 0 or no TIM IE */
2997 if (elems.tim)
2998 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
2999 else
3000 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003001
3002 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3003 sdata->vif.bss_conf.sync_tsf =
3004 le64_to_cpu(mgmt->u.beacon.timestamp);
3005 sdata->vif.bss_conf.sync_device_ts =
3006 rx_status->device_timestamp;
3007 if (elems.tim)
3008 sdata->vif.bss_conf.sync_dtim_count =
3009 elems.tim->dtim_count;
3010 else
3011 sdata->vif.bss_conf.sync_dtim_count = 0;
3012 }
3013
Alexander Bondar989c6502013-05-16 17:34:17 +03003014 changed |= BSS_CHANGED_BEACON_INFO;
3015 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03003016
3017 mutex_lock(&local->iflist_mtx);
3018 ieee80211_recalc_ps(local, -1);
3019 mutex_unlock(&local->iflist_mtx);
3020
Alexander Bondarce857882013-05-06 17:17:04 +03003021 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003022 }
3023
Johannes Berg1946bed2013-03-27 14:31:53 +01003024 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003025 erp_valid = true;
3026 erp_value = elems.erp_info[0];
3027 } else {
3028 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003029 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003030 changed |= ieee80211_handle_bss_capability(sdata,
3031 le16_to_cpu(mgmt->u.beacon.capab_info),
3032 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003033
Johannes Berg11289582013-02-07 17:36:12 +01003034 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003035 sta = sta_info_get(sdata, bssid);
3036
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003037 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3038 elems.vht_operation, bssid, &changed)) {
3039 mutex_unlock(&local->sta_mtx);
3040 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3041 WLAN_REASON_DEAUTH_LEAVING,
3042 true, deauth_buf);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003043 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
3044 sizeof(deauth_buf));
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003045 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003046 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003047
3048 if (sta && elems.opmode_notif)
3049 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3050 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003051 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003052
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003053 if (elems.country_elem && elems.pwr_constr_elem &&
3054 mgmt->u.probe_resp.capab_info &
3055 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003056 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3057 elems.country_elem,
3058 elems.country_elem_len,
3059 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003060
Johannes Berg471b3ef2007-12-28 14:32:58 +01003061 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003062}
3063
Johannes Berg1fa57d02010-06-10 10:21:32 +02003064void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3065 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003066{
3067 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003068 struct ieee80211_mgmt *mgmt;
3069 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003070 struct ieee802_11_elems elems;
3071 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07003072
Jiri Bencf0706e82007-05-05 11:45:53 -07003073 rx_status = (struct ieee80211_rx_status *) skb->cb;
3074 mgmt = (struct ieee80211_mgmt *) skb->data;
3075 fc = le16_to_cpu(mgmt->frame_control);
3076
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003077 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003078
Johannes Berg66e67e42012-01-20 13:55:27 +01003079 switch (fc & IEEE80211_FCTL_STYPE) {
3080 case IEEE80211_STYPE_BEACON:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003081 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003082 break;
3083 case IEEE80211_STYPE_PROBE_RESP:
3084 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3085 break;
3086 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003087 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003088 break;
3089 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003090 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003091 break;
3092 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003093 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003094 break;
3095 case IEEE80211_STYPE_ASSOC_RESP:
3096 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003097 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003098 break;
3099 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01003100 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003101 ies_len = skb->len -
3102 offsetof(struct ieee80211_mgmt,
3103 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01003104
3105 if (ies_len < 0)
3106 break;
3107
3108 ieee802_11_parse_elems(
3109 mgmt->u.action.u.chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003110 ies_len, true, &elems);
Johannes Berg37799e52013-03-26 14:02:26 +01003111
3112 if (elems.parse_error)
3113 break;
3114
Johannes Berg66e67e42012-01-20 13:55:27 +01003115 ieee80211_sta_process_chanswitch(sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01003116 rx_status->mactime,
Johannes Berg04a161f2013-05-03 09:35:35 +02003117 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003118 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3119 ies_len = skb->len -
3120 offsetof(struct ieee80211_mgmt,
3121 u.action.u.ext_chan_switch.variable);
3122
3123 if (ies_len < 0)
3124 break;
3125
3126 ieee802_11_parse_elems(
3127 mgmt->u.action.u.ext_chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003128 ies_len, true, &elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003129
3130 if (elems.parse_error)
3131 break;
3132
3133 /* for the handling code pretend this was also an IE */
3134 elems.ext_chansw_ie =
3135 &mgmt->u.action.u.ext_chan_switch.data;
3136
3137 ieee80211_sta_process_chanswitch(sdata,
3138 rx_status->mactime,
Johannes Berg04a161f2013-05-03 09:35:35 +02003139 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003140 }
Johannes Berg37799e52013-03-26 14:02:26 +01003141 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003142 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003143 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003144}
3145
Johannes Berg9c6bd792008-09-11 00:01:52 +02003146static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003147{
3148 struct ieee80211_sub_if_data *sdata =
3149 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003150
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003151 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003152}
3153
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003154static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003155 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003156{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003157 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003158
Johannes Berg37ad3882012-02-24 13:50:54 +01003159 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003160 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01003161
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003162 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3163 IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003164}
3165
Johannes Berg66e67e42012-01-20 13:55:27 +01003166static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3167{
3168 struct ieee80211_local *local = sdata->local;
3169 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3170 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003171 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003172
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003173 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003174
3175 if (WARN_ON_ONCE(!auth_data))
3176 return -EINVAL;
3177
Johannes Berg66e67e42012-01-20 13:55:27 +01003178 auth_data->tries++;
3179
3180 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003181 sdata_info(sdata, "authentication with %pM timed out\n",
3182 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003183
3184 /*
3185 * Most likely AP is not in the range so remove the
3186 * bss struct for that AP.
3187 */
3188 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3189
3190 return -ETIMEDOUT;
3191 }
3192
Johannes Berga1845fc2012-06-27 13:18:36 +02003193 drv_mgd_prepare_tx(local, sdata);
3194
Johannes Berg66e67e42012-01-20 13:55:27 +01003195 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003196 u16 trans = 1;
3197 u16 status = 0;
3198
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003199 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3200 auth_data->bss->bssid, auth_data->tries,
3201 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003202
3203 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003204
3205 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3206 trans = auth_data->sae_trans;
3207 status = auth_data->sae_status;
3208 auth_data->expected_transaction = trans;
3209 }
3210
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003211 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3212 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3213 IEEE80211_TX_INTFL_MLME_CONN_TX;
3214
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003215 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3216 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003217 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003218 auth_data->bss->bssid, NULL, 0, 0,
3219 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003220 } else {
3221 const u8 *ssidie;
3222
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003223 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3224 auth_data->bss->bssid, auth_data->tries,
3225 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003226
Johannes Berg9caf0362012-11-29 01:25:20 +01003227 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003228 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003229 if (!ssidie) {
3230 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003231 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003232 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003233 /*
3234 * Direct probe is sent to broadcast address as some APs
3235 * will not answer to direct packet in unassociated state.
3236 */
3237 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003238 NULL, 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02003239 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003240 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003241 }
3242
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003243 if (tx_flags == 0) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01003244 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Bergcb236d22013-07-29 23:07:43 +02003245 auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003246 run_again(sdata, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003247 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02003248 auth_data->timeout =
3249 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3250 auth_data->timeout_started = true;
3251 run_again(sdata, auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003252 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003253
3254 return 0;
3255}
3256
3257static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3258{
3259 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3260 struct ieee80211_local *local = sdata->local;
3261
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003262 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003263
Johannes Berg66e67e42012-01-20 13:55:27 +01003264 assoc_data->tries++;
3265 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003266 sdata_info(sdata, "association with %pM timed out\n",
3267 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003268
3269 /*
3270 * Most likely AP is not in the range so remove the
3271 * bss struct for that AP.
3272 */
3273 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3274
3275 return -ETIMEDOUT;
3276 }
3277
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003278 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3279 assoc_data->bss->bssid, assoc_data->tries,
3280 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003281 ieee80211_send_assoc(sdata);
3282
Johannes Berg1672c0e32013-01-29 15:02:27 +01003283 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3284 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003285 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003286 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003287 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02003288 assoc_data->timeout =
3289 round_jiffies_up(jiffies +
3290 IEEE80211_ASSOC_TIMEOUT_LONG);
3291 assoc_data->timeout_started = true;
3292 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003293 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003294
3295 return 0;
3296}
3297
Johannes Berg1672c0e32013-01-29 15:02:27 +01003298void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3299 __le16 fc, bool acked)
3300{
3301 struct ieee80211_local *local = sdata->local;
3302
3303 sdata->u.mgd.status_fc = fc;
3304 sdata->u.mgd.status_acked = acked;
3305 sdata->u.mgd.status_received = true;
3306
3307 ieee80211_queue_work(&local->hw, &sdata->work);
3308}
3309
Johannes Berg1fa57d02010-06-10 10:21:32 +02003310void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003311{
Johannes Berg60f8b392008-09-08 17:44:22 +02003312 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003313 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003314
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003315 sdata_lock(sdata);
Johannes Berg60f8b392008-09-08 17:44:22 +02003316
Johannes Berg1672c0e32013-01-29 15:02:27 +01003317 if (ifmgd->status_received) {
3318 __le16 fc = ifmgd->status_fc;
3319 bool status_acked = ifmgd->status_acked;
3320
3321 ifmgd->status_received = false;
3322 if (ifmgd->auth_data &&
3323 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3324 if (status_acked) {
3325 ifmgd->auth_data->timeout =
3326 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003327 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003328 } else {
3329 ifmgd->auth_data->timeout = jiffies - 1;
3330 }
Johannes Berg89afe612013-02-13 15:39:57 +01003331 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003332 } else if (ifmgd->assoc_data &&
3333 (ieee80211_is_assoc_req(fc) ||
3334 ieee80211_is_reassoc_req(fc))) {
3335 if (status_acked) {
3336 ifmgd->assoc_data->timeout =
3337 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003338 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003339 } else {
3340 ifmgd->assoc_data->timeout = jiffies - 1;
3341 }
Johannes Berg89afe612013-02-13 15:39:57 +01003342 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003343 }
3344 }
3345
Johannes Berg89afe612013-02-13 15:39:57 +01003346 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003347 time_after(jiffies, ifmgd->auth_data->timeout)) {
3348 if (ifmgd->auth_data->done) {
3349 /*
3350 * ok ... we waited for assoc but userspace didn't,
3351 * so let's just kill the auth data
3352 */
3353 ieee80211_destroy_auth_data(sdata, false);
3354 } else if (ieee80211_probe_auth(sdata)) {
3355 u8 bssid[ETH_ALEN];
3356
3357 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3358
3359 ieee80211_destroy_auth_data(sdata, false);
3360
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003361 cfg80211_auth_timeout(sdata->dev, bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003362 }
Johannes Berg89afe612013-02-13 15:39:57 +01003363 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003364 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01003365
Johannes Berg89afe612013-02-13 15:39:57 +01003366 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003367 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03003368 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003369 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02003370 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01003371
3372 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02003373 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003374 }
Johannes Berg89afe612013-02-13 15:39:57 +01003375 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003376 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01003377
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +02003378 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
Johannes Bergb291ba12009-07-10 15:29:03 +02003379 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003380 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003381 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003382
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003383 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003384
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003385 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003386 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003387 else
Ben Greear180205b2011-02-04 15:30:24 -08003388 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003389
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003390 /* ACK received for nullfunc probing frame */
3391 if (!ifmgd->probe_send_count)
3392 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003393 else if (ifmgd->nullfunc_failed) {
3394 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003395 mlme_dbg(sdata,
3396 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3397 bssid, ifmgd->probe_send_count,
3398 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003399 ieee80211_mgd_probe_ap_send(sdata);
3400 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003401 mlme_dbg(sdata,
3402 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3403 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003404 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003405 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3406 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003407 }
3408 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003409 run_again(sdata, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003410 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003411 mlme_dbg(sdata,
3412 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3413 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003414 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003415 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003416 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003417 mlme_dbg(sdata,
3418 "No probe response from AP %pM after %dms, try %d/%i\n",
3419 bssid, probe_wait_ms,
3420 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003421 ieee80211_mgd_probe_ap_send(sdata);
3422 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003423 /*
3424 * We actually lost the connection ... or did we?
3425 * Let's make sure!
3426 */
Ben Greearb38afa82010-10-07 16:12:06 -07003427 wiphy_debug(local->hw.wiphy,
3428 "%s: No probe response from AP %pM"
3429 " after %dms, disconnecting.\n",
3430 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003431 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003432
Johannes Berg95acac62011-07-12 12:30:59 +02003433 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003434 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003435 }
3436 }
3437
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003438 sdata_unlock(sdata);
Johannes Berg60f8b392008-09-08 17:44:22 +02003439}
Johannes Berg0a51b272008-09-08 17:44:25 +02003440
Johannes Bergb291ba12009-07-10 15:29:03 +02003441static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3442{
3443 struct ieee80211_sub_if_data *sdata =
3444 (struct ieee80211_sub_if_data *) data;
3445 struct ieee80211_local *local = sdata->local;
3446
3447 if (local->quiescing)
3448 return;
3449
Johannes Berg682bd382013-01-29 13:13:50 +01003450 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003451 ieee80211_queue_work(&sdata->local->hw,
3452 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003453}
3454
3455static void ieee80211_sta_conn_mon_timer(unsigned long data)
3456{
3457 struct ieee80211_sub_if_data *sdata =
3458 (struct ieee80211_sub_if_data *) data;
3459 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3460 struct ieee80211_local *local = sdata->local;
3461
3462 if (local->quiescing)
3463 return;
3464
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003465 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003466}
3467
3468static void ieee80211_sta_monitor_work(struct work_struct *work)
3469{
3470 struct ieee80211_sub_if_data *sdata =
3471 container_of(work, struct ieee80211_sub_if_data,
3472 u.mgd.monitor_work);
3473
3474 ieee80211_mgd_probe_ap(sdata, false);
3475}
3476
Johannes Berga1678f82008-09-11 00:01:47 +02003477static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3478{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003479 u32 flags;
3480
Kalle Vaload935682009-04-19 08:47:19 +03003481 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003482 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003483
Johannes Bergb291ba12009-07-10 15:29:03 +02003484 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003485 flags = sdata->local->hw.flags;
3486 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3487 ieee80211_queue_work(&sdata->local->hw,
3488 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003489 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003490 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003491 }
Johannes Berga1678f82008-09-11 00:01:47 +02003492}
3493
Johannes Bergb8360ab2013-04-29 14:57:44 +02003494#ifdef CONFIG_PM
3495void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3496{
3497 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3498
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003499 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003500 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003501 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003502 return;
3503 }
3504
3505 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3506 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3507 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3508 ieee80211_sta_connection_lost(sdata,
3509 ifmgd->associated->bssid,
3510 WLAN_REASON_UNSPECIFIED,
3511 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003512 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003513 return;
3514 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003515 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003516}
3517#endif
3518
Johannes Berg9c6bd792008-09-11 00:01:52 +02003519/* interface setup */
3520void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3521{
Johannes Berg46900292009-02-15 12:44:28 +01003522 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003523
Johannes Berg46900292009-02-15 12:44:28 +01003524 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003525 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003526 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003527 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3528 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003529 INIT_WORK(&ifmgd->csa_connection_drop_work,
3530 ieee80211_csa_connection_drop_work);
Emmanuel Grumbach687da132013-10-01 16:45:43 +03003531 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
Johannes Berg46900292009-02-15 12:44:28 +01003532 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003533 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003534 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3535 (unsigned long) sdata);
3536 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3537 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003538 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303539 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003540
Johannes Bergab1faea2009-07-01 21:41:17 +02003541 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303542 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003543 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3544 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003545 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003546
Johannes Berg0f782312009-12-01 13:37:02 +01003547 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3548 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3549 else
3550 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003551}
3552
3553/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003554void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3555{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003556 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003557
3558 /* Restart STA timers */
3559 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07003560 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3561 if (ieee80211_sdata_running(sdata))
3562 ieee80211_restart_sta_timer(sdata);
3563 }
Johannes Berga1678f82008-09-11 00:01:47 +02003564 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003565}
Johannes Berg10f644a2009-04-16 13:17:25 +02003566
3567int ieee80211_max_network_latency(struct notifier_block *nb,
3568 unsigned long data, void *dummy)
3569{
3570 s32 latency_usec = (s32) data;
3571 struct ieee80211_local *local =
3572 container_of(nb, struct ieee80211_local,
3573 network_latency_notifier);
3574
3575 mutex_lock(&local->iflist_mtx);
3576 ieee80211_recalc_ps(local, latency_usec);
3577 mutex_unlock(&local->iflist_mtx);
3578
3579 return 0;
3580}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003581
Johannes Bergf2d9d272012-11-22 14:11:39 +01003582static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3583 struct cfg80211_bss *cbss)
3584{
3585 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3586 const u8 *ht_cap_ie, *vht_cap_ie;
3587 const struct ieee80211_ht_cap *ht_cap;
3588 const struct ieee80211_vht_cap *vht_cap;
3589 u8 chains = 1;
3590
3591 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3592 return chains;
3593
Johannes Berg9caf0362012-11-29 01:25:20 +01003594 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003595 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3596 ht_cap = (void *)(ht_cap_ie + 2);
3597 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3598 /*
3599 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3600 * "Tx Unequal Modulation Supported" fields.
3601 */
3602 }
3603
3604 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3605 return chains;
3606
Johannes Berg9caf0362012-11-29 01:25:20 +01003607 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003608 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3609 u8 nss;
3610 u16 tx_mcs_map;
3611
3612 vht_cap = (void *)(vht_cap_ie + 2);
3613 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3614 for (nss = 8; nss > 0; nss--) {
3615 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3616 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3617 break;
3618 }
3619 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3620 chains = max(chains, nss);
3621 }
3622
3623 return chains;
3624}
3625
Johannes Bergb17166a2012-07-27 11:41:27 +02003626static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3627 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003628{
3629 struct ieee80211_local *local = sdata->local;
3630 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003631 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003632 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003633 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003634 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003635 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003636
Johannes Berg24398e32012-03-28 10:58:36 +02003637 sband = local->hw.wiphy->bands[cbss->channel->band];
3638
Johannes Bergf2d9d272012-11-22 14:11:39 +01003639 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3640 IEEE80211_STA_DISABLE_80P80MHZ |
3641 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003642
Johannes Berg9caf0362012-11-29 01:25:20 +01003643 rcu_read_lock();
3644
Johannes Bergf2d9d272012-11-22 14:11:39 +01003645 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3646 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003647 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003648
Johannes Berg9caf0362012-11-29 01:25:20 +01003649 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003650 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3651 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003652
3653 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3654 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3655 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3656 ht_oper = NULL;
3657 }
Johannes Berg24398e32012-03-28 10:58:36 +02003658 }
3659
Johannes Bergf2d9d272012-11-22 14:11:39 +01003660 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3661 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003662 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003663
Johannes Berg9caf0362012-11-29 01:25:20 +01003664 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3665 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003666 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3667 vht_oper = (void *)(vht_oper_ie + 2);
3668 if (vht_oper && !ht_oper) {
3669 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003670 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003671 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003672 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3673 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003674 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003675
3676 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3677 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3678 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3679 vht_oper = NULL;
3680 }
Johannes Berg24398e32012-03-28 10:58:36 +02003681 }
3682
Johannes Bergf2d9d272012-11-22 14:11:39 +01003683 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3684 cbss->channel,
3685 ht_oper, vht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02003686 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02003687
Johannes Bergf2d9d272012-11-22 14:11:39 +01003688 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3689 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003690
Johannes Berg9caf0362012-11-29 01:25:20 +01003691 rcu_read_unlock();
3692
Johannes Berg04ecd252012-09-11 14:34:12 +02003693 /* will change later if needed */
3694 sdata->smps_mode = IEEE80211_SMPS_OFF;
3695
Johannes Berg34a37402013-12-18 09:43:33 +01003696 mutex_lock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003697 /*
3698 * If this fails (possibly due to channel context sharing
3699 * on incompatible channels, e.g. 80+80 and 160 sharing the
3700 * same control channel) try to use a smaller bandwidth.
3701 */
3702 ret = ieee80211_vif_use_channel(sdata, &chandef,
3703 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02003704
3705 /* don't downgrade for 5 and 10 MHz channels, though. */
3706 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
3707 chandef.width == NL80211_CHAN_WIDTH_10)
Johannes Berg34a37402013-12-18 09:43:33 +01003708 goto out;
Simon Wunderlich0418a442013-05-16 13:00:31 +02003709
Johannes Bergd601cd82013-02-07 20:54:51 +01003710 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Simon Wunderliche6b7cde2013-08-28 13:41:29 +02003711 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003712 ret = ieee80211_vif_use_channel(sdata, &chandef,
3713 IEEE80211_CHANCTX_SHARED);
3714 }
Johannes Berg34a37402013-12-18 09:43:33 +01003715 out:
3716 mutex_unlock(&local->mtx);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003717 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003718}
3719
3720static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3721 struct cfg80211_bss *cbss, bool assoc)
3722{
3723 struct ieee80211_local *local = sdata->local;
3724 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3725 struct ieee80211_bss *bss = (void *)cbss->priv;
3726 struct sta_info *new_sta = NULL;
3727 bool have_sta = false;
3728 int err;
3729
3730 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3731 return -EINVAL;
3732
3733 if (assoc) {
3734 rcu_read_lock();
3735 have_sta = sta_info_get(sdata, cbss->bssid);
3736 rcu_read_unlock();
3737 }
3738
3739 if (!have_sta) {
3740 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3741 if (!new_sta)
3742 return -ENOMEM;
3743 }
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003744 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003745 u32 rates = 0, basic_rates = 0;
3746 bool have_higher_than_11mbit;
3747 int min_rate = INT_MAX, min_rate_index = -1;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003748 struct ieee80211_chanctx_conf *chanctx_conf;
Johannes Bergb17166a2012-07-27 11:41:27 +02003749 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003750 const struct cfg80211_bss_ies *ies;
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003751 int shift;
3752 u32 rate_flags;
Johannes Bergb17166a2012-07-27 11:41:27 +02003753
3754 sband = local->hw.wiphy->bands[cbss->channel->band];
3755
3756 err = ieee80211_prep_channel(sdata, cbss);
3757 if (err) {
3758 sta_info_free(local, new_sta);
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003759 return -EINVAL;
Johannes Bergb17166a2012-07-27 11:41:27 +02003760 }
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003761 shift = ieee80211_vif_get_shift(&sdata->vif);
3762
3763 rcu_read_lock();
3764 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3765 if (WARN_ON(!chanctx_conf)) {
3766 rcu_read_unlock();
3767 return -EINVAL;
3768 }
3769 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
3770 rcu_read_unlock();
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003771
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003772 ieee80211_get_rates(sband, bss->supp_rates,
3773 bss->supp_rates_len,
3774 &rates, &basic_rates,
3775 &have_higher_than_11mbit,
Simon Wunderlich2103dec2013-07-08 16:55:53 +02003776 &min_rate, &min_rate_index,
3777 shift, rate_flags);
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003778
3779 /*
3780 * This used to be a workaround for basic rates missing
3781 * in the association response frame. Now that we no
3782 * longer use the basic rates from there, it probably
3783 * doesn't happen any more, but keep the workaround so
3784 * in case some *other* APs are buggy in different ways
3785 * we can connect -- with a warning.
3786 */
3787 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003788 sdata_info(sdata,
3789 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003790 basic_rates = BIT(min_rate_index);
3791 }
3792
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003793 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003794 sdata->vif.bss_conf.basic_rates = basic_rates;
3795
3796 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003797 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003798 have_higher_than_11mbit)
3799 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3800 else
3801 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3802
3803 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3804
Johannes Berg8c358bc2012-05-22 22:13:05 +02003805 /* set timing information */
3806 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003807 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003808 ies = rcu_dereference(cbss->beacon_ies);
3809 if (ies) {
3810 const u8 *tim_ie;
3811
3812 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3813 sdata->vif.bss_conf.sync_device_ts =
3814 bss->device_ts_beacon;
3815 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3816 ies->data, ies->len);
3817 if (tim_ie && tim_ie[1] >= 2)
3818 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3819 else
3820 sdata->vif.bss_conf.sync_dtim_count = 0;
3821 } else if (!(local->hw.flags &
3822 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3823 ies = rcu_dereference(cbss->proberesp_ies);
3824 /* must be non-NULL since beacon IEs were NULL */
3825 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3826 sdata->vif.bss_conf.sync_device_ts =
3827 bss->device_ts_presp;
3828 sdata->vif.bss_conf.sync_dtim_count = 0;
3829 } else {
3830 sdata->vif.bss_conf.sync_tsf = 0;
3831 sdata->vif.bss_conf.sync_device_ts = 0;
3832 sdata->vif.bss_conf.sync_dtim_count = 0;
3833 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003834 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003835
3836 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003837 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003838 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3839 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003840
3841 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003842 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003843
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003844 err = sta_info_insert(new_sta);
3845 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003846 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003847 sdata_info(sdata,
3848 "failed to insert STA entry for the AP (error %d)\n",
3849 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003850 return err;
3851 }
3852 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003853 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003854
3855 return 0;
3856}
3857
Johannes Berg77fdaa12009-07-07 03:45:17 +02003858/* config hooks */
3859int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3860 struct cfg80211_auth_request *req)
3861{
Johannes Berg66e67e42012-01-20 13:55:27 +01003862 struct ieee80211_local *local = sdata->local;
3863 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3864 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003865 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003866 int err;
3867
3868 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003869
3870 switch (req->auth_type) {
3871 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3872 auth_alg = WLAN_AUTH_OPEN;
3873 break;
3874 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003875 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003876 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003877 auth_alg = WLAN_AUTH_SHARED_KEY;
3878 break;
3879 case NL80211_AUTHTYPE_FT:
3880 auth_alg = WLAN_AUTH_FT;
3881 break;
3882 case NL80211_AUTHTYPE_NETWORK_EAP:
3883 auth_alg = WLAN_AUTH_LEAP;
3884 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003885 case NL80211_AUTHTYPE_SAE:
3886 auth_alg = WLAN_AUTH_SAE;
3887 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003888 default:
3889 return -EOPNOTSUPP;
3890 }
3891
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003892 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3893 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003894 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003895 return -ENOMEM;
3896
Johannes Berg66e67e42012-01-20 13:55:27 +01003897 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003898
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003899 if (req->sae_data_len >= 4) {
3900 __le16 *pos = (__le16 *) req->sae_data;
3901 auth_data->sae_trans = le16_to_cpu(pos[0]);
3902 auth_data->sae_status = le16_to_cpu(pos[1]);
3903 memcpy(auth_data->data, req->sae_data + 4,
3904 req->sae_data_len - 4);
3905 auth_data->data_len += req->sae_data_len - 4;
3906 }
3907
Johannes Berg77fdaa12009-07-07 03:45:17 +02003908 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003909 memcpy(&auth_data->data[auth_data->data_len],
3910 req->ie, req->ie_len);
3911 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003912 }
3913
Johannes Bergfffd0932009-07-08 14:22:54 +02003914 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003915 auth_data->key_len = req->key_len;
3916 auth_data->key_idx = req->key_idx;
3917 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02003918 }
3919
Johannes Berg66e67e42012-01-20 13:55:27 +01003920 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003921
Johannes Berg66e67e42012-01-20 13:55:27 +01003922 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01003923
Johannes Berg66e67e42012-01-20 13:55:27 +01003924 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
3925 ifmgd->assoc_data) {
3926 err = -EBUSY;
3927 goto err_free;
3928 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003929
Johannes Berg66e67e42012-01-20 13:55:27 +01003930 if (ifmgd->auth_data)
3931 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03003932
Johannes Berg66e67e42012-01-20 13:55:27 +01003933 /* prep auth_data so we don't go into idle on disassoc */
3934 ifmgd->auth_data = auth_data;
3935
Johannes Berg7b119dc2013-04-10 21:38:36 +02003936 if (ifmgd->associated) {
3937 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
3938
3939 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3940 WLAN_REASON_UNSPECIFIED,
3941 false, frame_buf);
3942
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003943 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3944 sizeof(frame_buf));
Johannes Berg7b119dc2013-04-10 21:38:36 +02003945 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003946
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003947 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003948
Johannes Berga1cf7752012-03-08 15:02:07 +01003949 err = ieee80211_prep_connection(sdata, req->bss, false);
3950 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01003951 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03003952
Johannes Berg66e67e42012-01-20 13:55:27 +01003953 err = ieee80211_probe_auth(sdata);
3954 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01003955 sta_info_destroy_addr(sdata, req->bss->bssid);
3956 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003957 }
3958
Johannes Berg66e67e42012-01-20 13:55:27 +01003959 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01003960 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003961 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02003962
Johannes Berg66e67e42012-01-20 13:55:27 +01003963 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03003964 memset(ifmgd->bssid, 0, ETH_ALEN);
3965 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01003966 ifmgd->auth_data = NULL;
3967 err_free:
3968 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01003969 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01003970}
3971
Johannes Berg095d81c2013-10-15 12:25:07 +02003972static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
3973 const u8 *wmm_param, int len)
3974{
3975 const u8 *pos;
3976 size_t left;
3977
3978 if (len < 8)
3979 return false;
3980
3981 if (wmm_param[5] != 1 /* version */)
3982 return false;
3983
3984 pos = wmm_param + 8;
3985 left = len - 8;
3986
3987 for (; left >= 4; left -= 4, pos += 4) {
3988 u8 aifsn = pos[0] & 0x0f;
3989 u8 ecwmin = pos[1] & 0x0f;
3990 u8 ecwmax = (pos[1] & 0xf0) >> 4;
3991 int aci = (pos[0] >> 5) & 0x03;
3992
3993 if (aifsn < 2) {
3994 sdata_info(sdata,
3995 "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
3996 aifsn, aci);
3997 return false;
3998 }
3999 if (ecwmin > ecwmax) {
4000 sdata_info(sdata,
4001 "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
4002 ecwmin, ecwmax, aci);
4003 return false;
4004 }
4005 }
4006
4007 return true;
4008}
4009
Johannes Berg77fdaa12009-07-07 03:45:17 +02004010int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4011 struct cfg80211_assoc_request *req)
4012{
Johannes Berg66e67e42012-01-20 13:55:27 +01004013 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004014 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004015 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004016 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004017 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004018 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004019 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004020 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004021
Johannes Berg66e67e42012-01-20 13:55:27 +01004022 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4023 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004024 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004025
Johannes Berg9caf0362012-11-29 01:25:20 +01004026 rcu_read_lock();
4027 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4028 if (!ssidie) {
4029 rcu_read_unlock();
4030 kfree(assoc_data);
4031 return -EINVAL;
4032 }
4033 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4034 assoc_data->ssid_len = ssidie[1];
4035 rcu_read_unlock();
4036
Johannes Berg7b119dc2013-04-10 21:38:36 +02004037 if (ifmgd->associated) {
4038 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4039
4040 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4041 WLAN_REASON_UNSPECIFIED,
4042 false, frame_buf);
4043
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004044 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4045 sizeof(frame_buf));
Johannes Berg7b119dc2013-04-10 21:38:36 +02004046 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004047
4048 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4049 err = -EBUSY;
4050 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004051 }
4052
Johannes Berg66e67e42012-01-20 13:55:27 +01004053 if (ifmgd->assoc_data) {
4054 err = -EBUSY;
4055 goto err_free;
4056 }
4057
4058 if (ifmgd->auth_data) {
4059 bool match;
4060
4061 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004062 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004063 ieee80211_destroy_auth_data(sdata, match);
4064 }
4065
4066 /* prepare assoc data */
Johannes Berg095d81c2013-10-15 12:25:07 +02004067
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004068 ifmgd->beacon_crc_valid = false;
4069
Johannes Berg095d81c2013-10-15 12:25:07 +02004070 assoc_data->wmm = bss->wmm_used &&
4071 (local->hw.queues >= IEEE80211_NUM_ACS);
4072 if (assoc_data->wmm) {
4073 /* try to check validity of WMM params IE */
4074 const struct cfg80211_bss_ies *ies;
4075 const u8 *wp, *start, *end;
4076
4077 rcu_read_lock();
4078 ies = rcu_dereference(req->bss->ies);
4079 start = ies->data;
4080 end = start + ies->len;
4081
4082 while (true) {
4083 wp = cfg80211_find_vendor_ie(
4084 WLAN_OUI_MICROSOFT,
4085 WLAN_OUI_TYPE_MICROSOFT_WMM,
4086 start, end - start);
4087 if (!wp)
4088 break;
4089 start = wp + wp[1] + 2;
4090 /* if this IE is too short, try the next */
4091 if (wp[1] <= 4)
4092 continue;
4093 /* if this IE is WMM params, we found what we wanted */
4094 if (wp[6] == 1)
4095 break;
4096 }
4097
4098 if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
4099 wp[1] - 2)) {
4100 assoc_data->wmm = false;
4101 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
4102 }
4103 rcu_read_unlock();
4104 }
4105
Johannes Bergde5036a2012-03-08 15:02:03 +01004106 /*
4107 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4108 * We still associate in non-HT mode (11a/b/g) if any one of these
4109 * ciphers is configured as pairwise.
4110 * We can set this to true for non-11n hardware, that'll be checked
4111 * separately along with the peer capabilities.
4112 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004113 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004114 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4115 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004116 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004117 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004118 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004119 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004120 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004121 }
4122 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004123
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004124 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004125 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004126 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4127 }
Ben Greearef96a8422011-11-18 11:32:00 -08004128
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004129 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4130 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4131
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004132 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4133 sband = local->hw.wiphy->bands[req->bss->channel->band];
4134 if (!sband->ht_cap.ht_supported ||
Johannes Berg095d81c2013-10-15 12:25:07 +02004135 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4136 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Johannes Berga8243b72012-11-22 14:32:09 +01004137 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg095d81c2013-10-15 12:25:07 +02004138 if (!bss->wmm_used &&
4139 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
Johannes Berg1b49de22012-07-27 10:29:14 +02004140 netdev_info(sdata->dev,
4141 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004142 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004143
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004144 /* disable VHT if we don't support it or the AP doesn't use WMM */
4145 if (!sband->vht_cap.vht_supported ||
Johannes Berg095d81c2013-10-15 12:25:07 +02004146 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4147 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004148 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg095d81c2013-10-15 12:25:07 +02004149 if (!bss->wmm_used &&
4150 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
Johannes Berg1b49de22012-07-27 10:29:14 +02004151 netdev_info(sdata->dev,
4152 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004153 }
4154
Ben Greearef96a8422011-11-18 11:32:00 -08004155 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4156 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4157 sizeof(ifmgd->ht_capa_mask));
4158
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004159 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4160 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4161 sizeof(ifmgd->vht_capa_mask));
4162
Johannes Berg77fdaa12009-07-07 03:45:17 +02004163 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004164 memcpy(assoc_data->ie, req->ie, req->ie_len);
4165 assoc_data->ie_len = req->ie_len;
4166 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004167
Johannes Berg66e67e42012-01-20 13:55:27 +01004168 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004169
Johannes Bergaf6b6372009-12-23 13:15:35 +01004170 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4171 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004172 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004173 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004174 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004175 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004176 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004177
Johannes Berg66e67e42012-01-20 13:55:27 +01004178 assoc_data->capability = req->bss->capability;
Johannes Berg66e67e42012-01-20 13:55:27 +01004179 assoc_data->supp_rates = bss->supp_rates;
4180 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004181
Johannes Berg9caf0362012-11-29 01:25:20 +01004182 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004183 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4184 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4185 assoc_data->ap_ht_param =
4186 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4187 else
Johannes Berga8243b72012-11-22 14:32:09 +01004188 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004189 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4190 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4191 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4192 sizeof(struct ieee80211_vht_cap));
4193 else
4194 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004195 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004196
Kalle Valoab133152010-01-12 10:42:31 +02004197 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004198 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4199 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004200 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004201 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4202 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004203 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004204 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4205 }
4206
Johannes Berg77fdaa12009-07-07 03:45:17 +02004207 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004208 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004209
4210 if (req->use_mfp) {
4211 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4212 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4213 } else {
4214 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4215 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4216 }
4217
4218 if (req->crypto.control_port)
4219 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4220 else
4221 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4222
Johannes Berga621fa42010-08-27 14:26:54 +03004223 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4224 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
Max Stepanov2475b1cc2013-03-24 14:23:27 +02004225 sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
4226 sdata->vif.type);
Johannes Berga621fa42010-08-27 14:26:54 +03004227
Johannes Berg66e67e42012-01-20 13:55:27 +01004228 /* kick off associate process */
4229
4230 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004231 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03004232 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01004233
Johannes Berga1cf7752012-03-08 15:02:07 +01004234 err = ieee80211_prep_connection(sdata, req->bss, true);
4235 if (err)
4236 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004237
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004238 rcu_read_lock();
4239 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004240
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004241 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4242 !beacon_ies) {
4243 /*
4244 * Wait up to one beacon interval ...
4245 * should this be more if we miss one?
4246 */
4247 sdata_info(sdata, "waiting for beacon from %pM\n",
4248 ifmgd->bssid);
4249 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004250 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004251 assoc_data->need_beacon = true;
4252 } else if (beacon_ies) {
4253 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4254 beacon_ies->data,
4255 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004256 u8 dtim_count = 0;
4257
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004258 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4259 const struct ieee80211_tim_ie *tim;
4260 tim = (void *)(tim_ie + 2);
4261 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004262 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004263 }
Alexander Bondar989c6502013-05-16 17:34:17 +03004264 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004265 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004266 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004267
4268 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4269 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4270 sdata->vif.bss_conf.sync_device_ts =
4271 bss->device_ts_beacon;
4272 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4273 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004274 } else {
4275 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004276 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004277 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004278 rcu_read_unlock();
4279
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004280 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004281
Paul Stewartfcff4f12012-02-23 17:59:53 -08004282 if (bss->corrupt_data) {
4283 char *corrupt_type = "data";
4284 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4285 if (bss->corrupt_data &
4286 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4287 corrupt_type = "beacon and probe response";
4288 else
4289 corrupt_type = "beacon";
4290 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4291 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004292 sdata_info(sdata, "associating with AP with corrupt %s\n",
4293 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004294 }
4295
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004296 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004297 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004298 memset(ifmgd->bssid, 0, ETH_ALEN);
4299 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004300 ifmgd->assoc_data = NULL;
4301 err_free:
4302 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01004303 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004304}
4305
4306int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004307 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004308{
4309 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004310 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004311 bool tx = !req->local_state_change;
Johannes Bergde3d43a2013-05-14 09:39:02 +02004312 bool report_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004313
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004314 sdata_info(sdata,
4315 "deauthenticating from %pM by local choice (reason=%d)\n",
4316 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004317
Johannes Berg86552012012-10-29 09:46:31 +01004318 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004319 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004320 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4321 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004322 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004323 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004324 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg86552012012-10-29 09:46:31 +01004325
Johannes Bergde3d43a2013-05-14 09:39:02 +02004326 report_frame = true;
Johannes Berg86552012012-10-29 09:46:31 +01004327 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004328 }
4329
Johannes Berg86552012012-10-29 09:46:31 +01004330 if (ifmgd->associated &&
4331 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4332 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4333 req->reason_code, tx, frame_buf);
Johannes Bergde3d43a2013-05-14 09:39:02 +02004334 report_frame = true;
Johannes Berg86552012012-10-29 09:46:31 +01004335 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004336
Johannes Berg86552012012-10-29 09:46:31 +01004337 out:
Johannes Bergde3d43a2013-05-14 09:39:02 +02004338 if (report_frame)
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004339 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4340 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg86552012012-10-29 09:46:31 +01004341
Johannes Berg77fdaa12009-07-07 03:45:17 +02004342 return 0;
4343}
4344
4345int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004346 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004347{
4348 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004349 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004350 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004351
Johannes Berg8d8b2612009-07-25 11:58:36 +02004352 /*
4353 * cfg80211 should catch this ... but it's racy since
4354 * we can receive a disassoc frame, process it, hand it
4355 * to cfg80211 while that's in a locked section already
4356 * trying to tell us that the user wants to disconnect.
4357 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004358 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004359 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004360
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004361 sdata_info(sdata,
4362 "disassociating from %pM by local choice (reason=%d)\n",
4363 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004364
Jouni Malinene69e95d2010-03-29 23:29:31 -07004365 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004366 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4367 req->reason_code, !req->local_state_change,
4368 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004369
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004370 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4371 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004372
Johannes Berg77fdaa12009-07-07 03:45:17 +02004373 return 0;
4374}
Jouni Malinen026331c2010-02-15 12:53:10 +02004375
Eliad Pellerafa762f2012-04-23 14:45:15 +03004376void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004377{
4378 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4379
Ben Greear49921852013-02-20 09:41:09 -08004380 /*
4381 * Make sure some work items will not run after this,
4382 * they will not do anything but might not have been
4383 * cancelled when disconnecting.
4384 */
4385 cancel_work_sync(&ifmgd->monitor_work);
4386 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4387 cancel_work_sync(&ifmgd->request_smps_work);
4388 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4389 cancel_work_sync(&ifmgd->chswitch_work);
4390
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004391 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02004392 if (ifmgd->assoc_data) {
4393 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004394 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004395 cfg80211_assoc_timeout(sdata->dev, bss);
4396 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004397 if (ifmgd->auth_data)
4398 ieee80211_destroy_auth_data(sdata, false);
4399 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004400 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004401}
4402
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004403void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4404 enum nl80211_cqm_rssi_threshold_event rssi_event,
4405 gfp_t gfp)
4406{
4407 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4408
Johannes Bergb5878a22010-04-07 16:48:40 +02004409 trace_api_cqm_rssi_notify(sdata, rssi_event);
4410
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004411 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4412}
4413EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);