blob: cc9e02d79b550106ee07b6aca48ffa9e18081a45 [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 chandef_downgrade(struct cfg80211_chan_def *c)
149{
150 u32 ret;
151 int tmp;
152
153 switch (c->width) {
154 case NL80211_CHAN_WIDTH_20:
155 c->width = NL80211_CHAN_WIDTH_20_NOHT;
156 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
157 break;
158 case NL80211_CHAN_WIDTH_40:
159 c->width = NL80211_CHAN_WIDTH_20;
160 c->center_freq1 = c->chan->center_freq;
161 ret = IEEE80211_STA_DISABLE_40MHZ |
162 IEEE80211_STA_DISABLE_VHT;
163 break;
164 case NL80211_CHAN_WIDTH_80:
165 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
166 /* n_P40 */
167 tmp /= 2;
168 /* freq_P40 */
169 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
170 c->width = NL80211_CHAN_WIDTH_40;
171 ret = IEEE80211_STA_DISABLE_VHT;
172 break;
173 case NL80211_CHAN_WIDTH_80P80:
174 c->center_freq2 = 0;
175 c->width = NL80211_CHAN_WIDTH_80;
176 ret = IEEE80211_STA_DISABLE_80P80MHZ |
177 IEEE80211_STA_DISABLE_160MHZ;
178 break;
179 case NL80211_CHAN_WIDTH_160:
180 /* n_P20 */
181 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
182 /* n_P80 */
183 tmp /= 4;
184 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
185 c->width = NL80211_CHAN_WIDTH_80;
186 ret = IEEE80211_STA_DISABLE_80P80MHZ |
187 IEEE80211_STA_DISABLE_160MHZ;
188 break;
189 default:
190 case NL80211_CHAN_WIDTH_20_NOHT:
191 WARN_ON_ONCE(1);
192 c->width = NL80211_CHAN_WIDTH_20_NOHT;
193 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
194 break;
Simon Wunderlich0418a442013-05-16 13:00:31 +0200195 case NL80211_CHAN_WIDTH_5:
196 case NL80211_CHAN_WIDTH_10:
197 WARN_ON_ONCE(1);
198 /* keep c->width */
199 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
200 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100201 }
202
203 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
204
205 return ret;
206}
207
208static u32
209ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
210 struct ieee80211_supported_band *sband,
211 struct ieee80211_channel *channel,
212 const struct ieee80211_ht_operation *ht_oper,
213 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200214 struct cfg80211_chan_def *chandef, bool tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100215{
Johannes Berg5cdaed12013-07-31 11:23:06 +0200216 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg6565ec92013-02-08 14:52:32 +0100217 struct cfg80211_chan_def vht_chandef;
218 u32 ht_cfreq, ret;
219
220 chandef->chan = channel;
221 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
222 chandef->center_freq1 = channel->center_freq;
223 chandef->center_freq2 = 0;
224
225 if (!ht_oper || !sband->ht_cap.ht_supported) {
226 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
227 goto out;
228 }
229
230 chandef->width = NL80211_CHAN_WIDTH_20;
231
232 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
233 channel->band);
234 /* check that channel matches the right operating channel */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200235 if (!tracking && channel->center_freq != ht_cfreq) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100236 /*
237 * It's possible that some APs are confused here;
238 * Netgear WNDR3700 sometimes reports 4 higher than
239 * the actual channel in association responses, but
240 * since we look at probe response/beacon data here
241 * it should be OK.
242 */
Johannes Berg5cdaed12013-07-31 11:23:06 +0200243 sdata_info(sdata,
244 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
245 channel->center_freq, ht_cfreq,
246 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100247 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
248 goto out;
249 }
250
251 /* check 40 MHz support, if we have it */
252 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
253 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
254 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
255 chandef->width = NL80211_CHAN_WIDTH_40;
256 chandef->center_freq1 += 10;
257 break;
258 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
259 chandef->width = NL80211_CHAN_WIDTH_40;
260 chandef->center_freq1 -= 10;
261 break;
262 }
263 } else {
264 /* 40 MHz (and 80 MHz) must be supported for VHT */
265 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100266 /* also mark 40 MHz disabled */
267 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100268 goto out;
269 }
270
271 if (!vht_oper || !sband->vht_cap.vht_supported) {
272 ret = IEEE80211_STA_DISABLE_VHT;
273 goto out;
274 }
275
276 vht_chandef.chan = channel;
277 vht_chandef.center_freq1 =
278 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
279 channel->band);
280 vht_chandef.center_freq2 = 0;
281
Johannes Berg6565ec92013-02-08 14:52:32 +0100282 switch (vht_oper->chan_width) {
283 case IEEE80211_VHT_CHANWIDTH_USE_HT:
284 vht_chandef.width = chandef->width;
285 break;
286 case IEEE80211_VHT_CHANWIDTH_80MHZ:
287 vht_chandef.width = NL80211_CHAN_WIDTH_80;
288 break;
289 case IEEE80211_VHT_CHANWIDTH_160MHZ:
290 vht_chandef.width = NL80211_CHAN_WIDTH_160;
291 break;
292 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
293 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
Johannes Berg6553bf042013-03-28 10:26:17 +0100294 vht_chandef.center_freq2 =
295 ieee80211_channel_to_frequency(
296 vht_oper->center_freq_seg2_idx,
297 channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100298 break;
299 default:
Johannes Berg5cdaed12013-07-31 11:23:06 +0200300 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100301 sdata_info(sdata,
302 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
303 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100304 ret = IEEE80211_STA_DISABLE_VHT;
305 goto out;
306 }
307
308 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200309 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100310 sdata_info(sdata,
311 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100312 ret = IEEE80211_STA_DISABLE_VHT;
313 goto out;
314 }
315
316 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
317 ret = 0;
318 goto out;
319 }
320
321 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg5cdaed12013-07-31 11:23:06 +0200322 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100323 sdata_info(sdata,
324 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100325 ret = IEEE80211_STA_DISABLE_VHT;
326 goto out;
327 }
328
329 *chandef = vht_chandef;
330
331 ret = 0;
332
333out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100334 /* don't print the message below for VHT mismatch if VHT is disabled */
335 if (ret & IEEE80211_STA_DISABLE_VHT)
336 vht_chandef = *chandef;
337
Johannes Bergddfe49b2013-07-31 20:52:03 +0200338 /*
339 * Ignore the DISABLED flag when we're already connected and only
340 * tracking the APs beacon for bandwidth changes - otherwise we
341 * might get disconnected here if we connect to an AP, update our
342 * regulatory information based on the AP's country IE and the
343 * information we have is wrong/outdated and disables the channel
344 * that we're actually using for the connection to the AP.
345 */
Johannes Berg6565ec92013-02-08 14:52:32 +0100346 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
Johannes Bergddfe49b2013-07-31 20:52:03 +0200347 tracking ? 0 :
348 IEEE80211_CHAN_DISABLED)) {
Johannes Berg6565ec92013-02-08 14:52:32 +0100349 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
350 ret = IEEE80211_STA_DISABLE_HT |
351 IEEE80211_STA_DISABLE_VHT;
Chris Wrightb56e4b82013-07-31 12:12:24 -0700352 break;
Johannes Berg6565ec92013-02-08 14:52:32 +0100353 }
354
355 ret |= chandef_downgrade(chandef);
356 }
357
Johannes Berg5cdaed12013-07-31 11:23:06 +0200358 if (chandef->width != vht_chandef.width && !tracking)
Johannes Berg6565ec92013-02-08 14:52:32 +0100359 sdata_info(sdata,
360 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
361
362 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
363 return ret;
364}
365
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100366static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
367 struct sta_info *sta,
368 const struct ieee80211_ht_operation *ht_oper,
369 const struct ieee80211_vht_operation *vht_oper,
370 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200371{
372 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100373 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200374 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200375 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100376 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200377 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100378 u32 flags;
379 enum ieee80211_sta_rx_bandwidth new_sta_bw;
380 int ret;
381
382 /* if HT was/is disabled, don't track any bandwidth changes */
383 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
384 return 0;
385
386 /* don't check VHT if we associated as non-VHT station */
387 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
388 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200389
Johannes Berg11289582013-02-07 17:36:12 +0100390 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100391 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100392
Johannes Bergf2d93302013-02-08 13:15:55 +0100393 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200394 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200395
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100396 /* calculate new channel (type) based on HT/VHT operation IEs */
397 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +0200398 vht_oper, &chandef, true);
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100399
400 /*
401 * Downgrade the new channel if we associated with restricted
402 * capabilities. For example, if we associated as a 20 MHz STA
403 * to a 40 MHz AP (due to regulatory, capabilities or config
404 * reasons) then switching to a 40 MHz channel now won't do us
405 * any good -- we couldn't use it with the AP.
406 */
407 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
408 chandef.width == NL80211_CHAN_WIDTH_80P80)
409 flags |= chandef_downgrade(&chandef);
410 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
411 chandef.width == NL80211_CHAN_WIDTH_160)
412 flags |= chandef_downgrade(&chandef);
413 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
414 chandef.width > NL80211_CHAN_WIDTH_20)
415 flags |= chandef_downgrade(&chandef);
416
417 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
418 return 0;
419
420 sdata_info(sdata,
421 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
422 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
423 chandef.center_freq1, chandef.center_freq2);
424
425 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
426 IEEE80211_STA_DISABLE_VHT |
427 IEEE80211_STA_DISABLE_40MHZ |
428 IEEE80211_STA_DISABLE_80P80MHZ |
429 IEEE80211_STA_DISABLE_160MHZ)) ||
430 !cfg80211_chandef_valid(&chandef)) {
431 sdata_info(sdata,
432 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
433 ifmgd->bssid);
434 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200435 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200436
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100437 switch (chandef.width) {
438 case NL80211_CHAN_WIDTH_20_NOHT:
439 case NL80211_CHAN_WIDTH_20:
440 new_sta_bw = IEEE80211_STA_RX_BW_20;
441 break;
442 case NL80211_CHAN_WIDTH_40:
443 new_sta_bw = IEEE80211_STA_RX_BW_40;
444 break;
445 case NL80211_CHAN_WIDTH_80:
446 new_sta_bw = IEEE80211_STA_RX_BW_80;
447 break;
448 case NL80211_CHAN_WIDTH_80P80:
449 case NL80211_CHAN_WIDTH_160:
450 new_sta_bw = IEEE80211_STA_RX_BW_160;
451 break;
452 default:
453 return -EINVAL;
454 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200455
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100456 if (new_sta_bw > sta->cur_max_bandwidth)
457 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200458
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100459 if (new_sta_bw < sta->sta.bandwidth) {
460 sta->sta.bandwidth = new_sta_bw;
461 rate_control_rate_update(local, sband, sta,
462 IEEE80211_RC_BW_CHANGED);
463 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530464
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100465 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
466 if (ret) {
467 sdata_info(sdata,
468 "AP %pM changed bandwidth to incompatible one - disconnect\n",
469 ifmgd->bssid);
470 return ret;
471 }
472
473 if (new_sta_bw > sta->sta.bandwidth) {
474 sta->sta.bandwidth = new_sta_bw;
475 rate_control_rate_update(local, sband, sta,
476 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200477 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200478
Johannes Berg074d46d2012-03-15 19:45:16 +0100479 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200480
481 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100482 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
483 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200484 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200485 }
486
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100487 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200488}
489
Johannes Berg9c6bd792008-09-11 00:01:52 +0200490/* frame sending functions */
491
Johannes Berg66e67e42012-01-20 13:55:27 +0100492static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
493 struct ieee80211_supported_band *sband,
494 u32 *rates)
495{
496 int i, j, count;
497 *rates = 0;
498 count = 0;
499 for (i = 0; i < supp_rates_len; i++) {
500 int rate = (supp_rates[i] & 0x7F) * 5;
501
502 for (j = 0; j < sband->n_bitrates; j++)
503 if (sband->bitrates[j].bitrate == rate) {
504 *rates |= BIT(j);
505 count++;
506 break;
507 }
508 }
509
510 return count;
511}
512
513static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200514 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100515 struct ieee80211_supported_band *sband,
516 struct ieee80211_channel *channel,
517 enum ieee80211_smps_mode smps)
518{
Johannes Berg66e67e42012-01-20 13:55:27 +0100519 u8 *pos;
520 u32 flags = channel->flags;
521 u16 cap;
522 struct ieee80211_sta_ht_cap ht_cap;
523
524 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
525
Johannes Berg66e67e42012-01-20 13:55:27 +0100526 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
527 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
528
Johannes Berg66e67e42012-01-20 13:55:27 +0100529 /* determine capability flags */
530 cap = ht_cap.cap;
531
Johannes Berg9dde6422012-05-16 23:43:19 +0200532 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100533 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
534 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
535 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
536 cap &= ~IEEE80211_HT_CAP_SGI_40;
537 }
538 break;
539 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
540 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
541 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
542 cap &= ~IEEE80211_HT_CAP_SGI_40;
543 }
544 break;
545 }
546
Johannes Berg24398e32012-03-28 10:58:36 +0200547 /*
548 * If 40 MHz was disabled associate as though we weren't
549 * capable of 40 MHz -- some broken APs will never fall
550 * back to trying to transmit in 20 MHz.
551 */
552 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
553 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
554 cap &= ~IEEE80211_HT_CAP_SGI_40;
555 }
556
Johannes Berg66e67e42012-01-20 13:55:27 +0100557 /* set SM PS mode properly */
558 cap &= ~IEEE80211_HT_CAP_SM_PS;
559 switch (smps) {
560 case IEEE80211_SMPS_AUTOMATIC:
561 case IEEE80211_SMPS_NUM_MODES:
562 WARN_ON(1);
563 case IEEE80211_SMPS_OFF:
564 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
565 IEEE80211_HT_CAP_SM_PS_SHIFT;
566 break;
567 case IEEE80211_SMPS_STATIC:
568 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
569 IEEE80211_HT_CAP_SM_PS_SHIFT;
570 break;
571 case IEEE80211_SMPS_DYNAMIC:
572 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
573 IEEE80211_HT_CAP_SM_PS_SHIFT;
574 break;
575 }
576
577 /* reserve and fill IE */
578 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
579 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
580}
581
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000582static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
583 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100584 struct ieee80211_supported_band *sband,
585 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000586{
587 u8 *pos;
588 u32 cap;
589 struct ieee80211_sta_vht_cap vht_cap;
590
591 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
592
593 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100594 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000595
596 /* determine capability flags */
597 cap = vht_cap.cap;
598
Johannes Bergf2d9d272012-11-22 14:11:39 +0100599 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
600 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
601 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
602 }
603
604 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
605 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
606 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
607 }
608
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100609 /*
610 * Some APs apparently get confused if our capabilities are better
611 * than theirs, so restrict what we advertise in the assoc request.
612 */
613 if (!(ap_vht_cap->vht_cap_info &
614 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
615 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
616
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000617 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000618 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000619 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
620}
621
Johannes Berg66e67e42012-01-20 13:55:27 +0100622static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
623{
624 struct ieee80211_local *local = sdata->local;
625 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
626 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
627 struct sk_buff *skb;
628 struct ieee80211_mgmt *mgmt;
629 u8 *pos, qos_info;
630 size_t offset = 0, noffset;
631 int i, count, rates_len, supp_rates_len;
632 u16 capab;
633 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200634 struct ieee80211_chanctx_conf *chanctx_conf;
635 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100636 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100637
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200638 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +0100639
Johannes Berg55de9082012-07-26 17:24:39 +0200640 rcu_read_lock();
641 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
642 if (WARN_ON(!chanctx_conf)) {
643 rcu_read_unlock();
644 return;
645 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100646 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200647 rcu_read_unlock();
648 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100649
650 if (assoc_data->supp_rates_len) {
651 /*
652 * Get all rates supported by the device and the AP as
653 * some APs don't like getting a superset of their rates
654 * in the association request (e.g. D-Link DAP 1353 in
655 * b-only mode)...
656 */
657 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
658 assoc_data->supp_rates_len,
659 sband, &rates);
660 } else {
661 /*
662 * In case AP not provide any supported rates information
663 * before association, we send information element(s) with
664 * all rates that we support.
665 */
666 rates = ~0;
667 rates_len = sband->n_bitrates;
668 }
669
670 skb = alloc_skb(local->hw.extra_tx_headroom +
671 sizeof(*mgmt) + /* bit too much but doesn't matter */
672 2 + assoc_data->ssid_len + /* SSID */
673 4 + rates_len + /* (extended) rates */
674 4 + /* power capability */
675 2 + 2 * sband->n_channels + /* supported channels */
676 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000677 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100678 assoc_data->ie_len + /* extra IEs */
679 9, /* WMM */
680 GFP_KERNEL);
681 if (!skb)
682 return;
683
684 skb_reserve(skb, local->hw.extra_tx_headroom);
685
686 capab = WLAN_CAPABILITY_ESS;
687
688 if (sband->band == IEEE80211_BAND_2GHZ) {
689 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
690 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
691 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
692 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
693 }
694
695 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
696 capab |= WLAN_CAPABILITY_PRIVACY;
697
698 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
699 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
700 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
701
702 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
703 memset(mgmt, 0, 24);
704 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
705 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
706 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
707
708 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
709 skb_put(skb, 10);
710 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
711 IEEE80211_STYPE_REASSOC_REQ);
712 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
713 mgmt->u.reassoc_req.listen_interval =
714 cpu_to_le16(local->hw.conf.listen_interval);
715 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
716 ETH_ALEN);
717 } else {
718 skb_put(skb, 4);
719 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
720 IEEE80211_STYPE_ASSOC_REQ);
721 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
722 mgmt->u.assoc_req.listen_interval =
723 cpu_to_le16(local->hw.conf.listen_interval);
724 }
725
726 /* SSID */
727 pos = skb_put(skb, 2 + assoc_data->ssid_len);
728 *pos++ = WLAN_EID_SSID;
729 *pos++ = assoc_data->ssid_len;
730 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
731
732 /* add all rates which were marked to be used above */
733 supp_rates_len = rates_len;
734 if (supp_rates_len > 8)
735 supp_rates_len = 8;
736
737 pos = skb_put(skb, supp_rates_len + 2);
738 *pos++ = WLAN_EID_SUPP_RATES;
739 *pos++ = supp_rates_len;
740
741 count = 0;
742 for (i = 0; i < sband->n_bitrates; i++) {
743 if (BIT(i) & rates) {
744 int rate = sband->bitrates[i].bitrate;
745 *pos++ = (u8) (rate / 5);
746 if (++count == 8)
747 break;
748 }
749 }
750
751 if (rates_len > count) {
752 pos = skb_put(skb, rates_len - count + 2);
753 *pos++ = WLAN_EID_EXT_SUPP_RATES;
754 *pos++ = rates_len - count;
755
756 for (i++; i < sband->n_bitrates; i++) {
757 if (BIT(i) & rates) {
758 int rate = sband->bitrates[i].bitrate;
759 *pos++ = (u8) (rate / 5);
760 }
761 }
762 }
763
764 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
765 /* 1. power capabilities */
766 pos = skb_put(skb, 4);
767 *pos++ = WLAN_EID_PWR_CAPABILITY;
768 *pos++ = 2;
769 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200770 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100771
772 /* 2. supported channels */
773 /* TODO: get this in reg domain format */
774 pos = skb_put(skb, 2 * sband->n_channels + 2);
775 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
776 *pos++ = 2 * sband->n_channels;
777 for (i = 0; i < sband->n_channels; i++) {
778 *pos++ = ieee80211_frequency_to_channel(
779 sband->channels[i].center_freq);
780 *pos++ = 1; /* one channel in the subband*/
781 }
782 }
783
784 /* if present, add any custom IEs that go before HT */
785 if (assoc_data->ie_len && assoc_data->ie) {
786 static const u8 before_ht[] = {
787 WLAN_EID_SSID,
788 WLAN_EID_SUPP_RATES,
789 WLAN_EID_EXT_SUPP_RATES,
790 WLAN_EID_PWR_CAPABILITY,
791 WLAN_EID_SUPPORTED_CHANNELS,
792 WLAN_EID_RSN,
793 WLAN_EID_QOS_CAPA,
794 WLAN_EID_RRM_ENABLED_CAPABILITIES,
795 WLAN_EID_MOBILITY_DOMAIN,
796 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
797 };
798 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
799 before_ht, ARRAY_SIZE(before_ht),
800 offset);
801 pos = skb_put(skb, noffset - offset);
802 memcpy(pos, assoc_data->ie + offset, noffset - offset);
803 offset = noffset;
804 }
805
Johannes Bergf2d9d272012-11-22 14:11:39 +0100806 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
807 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
808 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
809
Johannes Berga8243b72012-11-22 14:32:09 +0100810 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200811 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200812 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100813
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000814 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100815 ieee80211_add_vht_ie(sdata, skb, sband,
816 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000817
Johannes Berg66e67e42012-01-20 13:55:27 +0100818 /* if present, add any custom non-vendor IEs that go after HT */
819 if (assoc_data->ie_len && assoc_data->ie) {
820 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
821 assoc_data->ie_len,
822 offset);
823 pos = skb_put(skb, noffset - offset);
824 memcpy(pos, assoc_data->ie + offset, noffset - offset);
825 offset = noffset;
826 }
827
Johannes Berg76f03032012-03-08 15:02:05 +0100828 if (assoc_data->wmm) {
829 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200830 qos_info = ifmgd->uapsd_queues;
831 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100832 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
833 } else {
834 qos_info = 0;
835 }
836
837 pos = skb_put(skb, 9);
838 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
839 *pos++ = 7; /* len */
840 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
841 *pos++ = 0x50;
842 *pos++ = 0xf2;
843 *pos++ = 2; /* WME */
844 *pos++ = 0; /* WME info */
845 *pos++ = 1; /* WME ver */
846 *pos++ = qos_info;
847 }
848
849 /* add any remaining custom (i.e. vendor specific here) IEs */
850 if (assoc_data->ie_len && assoc_data->ie) {
851 noffset = assoc_data->ie_len;
852 pos = skb_put(skb, noffset - offset);
853 memcpy(pos, assoc_data->ie + offset, noffset - offset);
854 }
855
Johannes Berga1845fc2012-06-27 13:18:36 +0200856 drv_mgd_prepare_tx(local, sdata);
857
Johannes Berg66e67e42012-01-20 13:55:27 +0100858 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100859 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
860 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
861 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100862 ieee80211_tx_skb(sdata, skb);
863}
864
Kalle Valo572e0012009-02-10 17:09:31 +0200865void ieee80211_send_pspoll(struct ieee80211_local *local,
866 struct ieee80211_sub_if_data *sdata)
867{
Kalle Valo572e0012009-02-10 17:09:31 +0200868 struct ieee80211_pspoll *pspoll;
869 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200870
Kalle Valod8cd1892010-01-05 20:16:26 +0200871 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
872 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200873 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200874
Kalle Valod8cd1892010-01-05 20:16:26 +0200875 pspoll = (struct ieee80211_pspoll *) skb->data;
876 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200877
Johannes Berg62ae67b2009-11-18 18:42:05 +0100878 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
879 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200880}
881
Johannes Berg965beda2009-04-16 13:17:24 +0200882void ieee80211_send_nullfunc(struct ieee80211_local *local,
883 struct ieee80211_sub_if_data *sdata,
884 int powersave)
885{
886 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200887 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530888 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200889
Kalle Valod8cd1892010-01-05 20:16:26 +0200890 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
891 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200892 return;
893
Kalle Valod8cd1892010-01-05 20:16:26 +0200894 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200895 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200896 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200897
Seth Forshee6c17b772013-02-11 11:21:07 -0600898 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
899 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Pontus Fuchsff40b422013-06-04 12:44:52 +0200900
901 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
902 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
903
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530904 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
905 IEEE80211_STA_CONNECTION_POLL))
906 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
907
Johannes Berg62ae67b2009-11-18 18:42:05 +0100908 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200909}
910
Felix Fietkaud5242152010-01-08 18:06:26 +0100911static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
912 struct ieee80211_sub_if_data *sdata)
913{
914 struct sk_buff *skb;
915 struct ieee80211_hdr *nullfunc;
916 __le16 fc;
917
918 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
919 return;
920
921 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700922 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100923 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700924
Felix Fietkaud5242152010-01-08 18:06:26 +0100925 skb_reserve(skb, local->hw.extra_tx_headroom);
926
927 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
928 memset(nullfunc, 0, 30);
929 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
930 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
931 nullfunc->frame_control = fc;
932 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
933 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
934 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
935 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
936
937 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
938 ieee80211_tx_skb(sdata, skb);
939}
940
Johannes Bergcc32abd2009-05-15 11:52:31 +0200941/* spectrum management related things */
942static void ieee80211_chswitch_work(struct work_struct *work)
943{
944 struct ieee80211_sub_if_data *sdata =
945 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100946 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200947 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
948
Johannes Berg9607e6b2009-12-23 13:15:31 +0100949 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200950 return;
951
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200952 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200953 if (!ifmgd->associated)
954 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200955
Johannes Berg85220d72013-03-25 18:29:27 +0100956 local->_oper_chandef = local->csa_chandef;
Karl Beldan675a0b02013-03-25 16:26:57 +0100957
958 if (!local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +0200959 /* call "hw_config" only if doing sw channel switch */
Karl Beldan675a0b02013-03-25 16:26:57 +0100960 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300961 } else {
962 /* update the device channel directly */
Karl Beldan675a0b02013-03-25 16:26:57 +0100963 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200964 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200965
Johannes Berg77fdaa12009-07-07 03:45:17 +0200966 /* XXX: shouldn't really modify cfg80211-owned data! */
Karl Beldan675a0b02013-03-25 16:26:57 +0100967 ifmgd->associated->channel = local->_oper_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200968
Johannes Berg57eebdf2012-08-01 15:50:46 +0200969 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +0100970 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100971 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200972 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200973 out:
974 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg8d61ffa2013-05-10 12:32:47 +0200975 sdata_unlock(sdata);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200976}
977
Johannes Berg5ce6e432010-05-11 16:20:57 +0200978void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
979{
Johannes Berg55de9082012-07-26 17:24:39 +0200980 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
981 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200982
983 trace_api_chswitch_done(sdata, success);
984 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200985 sdata_info(sdata,
986 "driver channel switch failed, disconnecting\n");
987 ieee80211_queue_work(&sdata->local->hw,
988 &ifmgd->csa_connection_drop_work);
989 } else {
990 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +0200991 }
Johannes Berg5ce6e432010-05-11 16:20:57 +0200992}
993EXPORT_SYMBOL(ieee80211_chswitch_done);
994
Johannes Bergcc32abd2009-05-15 11:52:31 +0200995static void ieee80211_chswitch_timer(unsigned long data)
996{
997 struct ieee80211_sub_if_data *sdata =
998 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200999
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001000 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001001}
1002
Johannes Berg37799e52013-03-26 14:02:26 +01001003static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001004ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Johannes Berg04a161f2013-05-03 09:35:35 +02001005 u64 timestamp, struct ieee802_11_elems *elems,
1006 bool beacon)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001007{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001008 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001009 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001010 struct cfg80211_bss *cbss = ifmgd->associated;
1011 struct ieee80211_bss *bss;
Johannes Berg55de9082012-07-26 17:24:39 +02001012 struct ieee80211_chanctx *chanctx;
Johannes Bergb4f286a12013-03-26 14:13:58 +01001013 enum ieee80211_band new_band;
1014 int new_freq;
1015 u8 new_chan_no;
1016 u8 count;
1017 u8 mode;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001018 struct ieee80211_channel *new_chan;
Johannes Berg85220d72013-03-25 18:29:27 +01001019 struct cfg80211_chan_def new_chandef = {};
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001020 struct cfg80211_chan_def new_vht_chandef = {};
1021 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1022 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
Johannes Berg04a161f2013-05-03 09:35:35 +02001023 const struct ieee80211_ht_operation *ht_oper;
Johannes Berg85220d72013-03-25 18:29:27 +01001024 int secondary_channel_offset = -1;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001025
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001026 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001027
Johannes Berg37799e52013-03-26 14:02:26 +01001028 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001029 return;
1030
Johannes Bergb4f286a12013-03-26 14:13:58 +01001031 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001032 return;
1033
Johannes Berg37799e52013-03-26 14:02:26 +01001034 /* disregard subsequent announcements if we are already processing */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001035 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1036 return;
1037
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001038 sec_chan_offs = elems->sec_chan_offs;
1039 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
Johannes Berg04a161f2013-05-03 09:35:35 +02001040 ht_oper = elems->ht_operation;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001041
1042 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1043 IEEE80211_STA_DISABLE_40MHZ)) {
1044 sec_chan_offs = NULL;
1045 wide_bw_chansw_ie = NULL;
Johannes Berg04a161f2013-05-03 09:35:35 +02001046 /* only used for bandwidth here */
1047 ht_oper = NULL;
Johannes Berg85220d72013-03-25 18:29:27 +01001048 }
1049
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001050 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1051 wide_bw_chansw_ie = NULL;
Johannes Berg85220d72013-03-25 18:29:27 +01001052
Johannes Bergb4f286a12013-03-26 14:13:58 +01001053 if (elems->ext_chansw_ie) {
1054 if (!ieee80211_operating_class_to_band(
1055 elems->ext_chansw_ie->new_operating_class,
1056 &new_band)) {
1057 sdata_info(sdata,
1058 "cannot understand ECSA IE operating class %d, disconnecting\n",
1059 elems->ext_chansw_ie->new_operating_class);
1060 ieee80211_queue_work(&local->hw,
1061 &ifmgd->csa_connection_drop_work);
1062 }
1063 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1064 count = elems->ext_chansw_ie->count;
1065 mode = elems->ext_chansw_ie->mode;
1066 } else if (elems->ch_switch_ie) {
1067 new_band = cbss->channel->band;
1068 new_chan_no = elems->ch_switch_ie->new_ch_num;
1069 count = elems->ch_switch_ie->count;
1070 mode = elems->ch_switch_ie->mode;
1071 } else {
1072 /* nothing here we understand */
Johannes Berg37799e52013-03-26 14:02:26 +01001073 return;
Johannes Bergb4f286a12013-03-26 14:13:58 +01001074 }
Johannes Berg37799e52013-03-26 14:02:26 +01001075
1076 bss = (void *)cbss->priv;
1077
Johannes Bergb4f286a12013-03-26 14:13:58 +01001078 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001079 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1080 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001081 sdata_info(sdata,
1082 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1083 ifmgd->associated->bssid, new_freq);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001084 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001085 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001086 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001087 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001088
Johannes Berg04a161f2013-05-03 09:35:35 +02001089 if (!beacon && sec_chan_offs) {
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001090 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
Johannes Berg04a161f2013-05-03 09:35:35 +02001091 } else if (beacon && ht_oper) {
1092 secondary_channel_offset =
1093 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001094 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
Johannes Berg04a161f2013-05-03 09:35:35 +02001095 /*
1096 * If it's not a beacon, HT is enabled and the IE not present,
1097 * it's 20 MHz, 802.11-2012 8.5.2.6:
1098 * This element [the Secondary Channel Offset Element] is
1099 * present when switching to a 40 MHz channel. It may be
1100 * present when switching to a 20 MHz channel (in which
1101 * case the secondary channel offset is set to SCN).
1102 */
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001103 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1104 }
1105
Johannes Berg85220d72013-03-25 18:29:27 +01001106 switch (secondary_channel_offset) {
1107 default:
1108 /* secondary_channel_offset was present but is invalid */
1109 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001110 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001111 NL80211_CHAN_HT20);
1112 break;
1113 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001114 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001115 NL80211_CHAN_HT40PLUS);
1116 break;
1117 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001118 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001119 NL80211_CHAN_HT40MINUS);
1120 break;
1121 case -1:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001122 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001123 NL80211_CHAN_NO_HT);
1124 break;
1125 }
1126
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001127 if (wide_bw_chansw_ie) {
1128 new_vht_chandef.chan = new_chan;
1129 new_vht_chandef.center_freq1 =
1130 ieee80211_channel_to_frequency(
1131 wide_bw_chansw_ie->new_center_freq_seg0,
1132 new_band);
1133
1134 switch (wide_bw_chansw_ie->new_channel_width) {
1135 default:
1136 /* hmmm, ignore VHT and use HT if present */
1137 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1138 new_vht_chandef.chan = NULL;
1139 break;
1140 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1141 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1142 break;
1143 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1144 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1145 break;
1146 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1147 /* field is otherwise reserved */
1148 new_vht_chandef.center_freq2 =
1149 ieee80211_channel_to_frequency(
1150 wide_bw_chansw_ie->new_center_freq_seg1,
1151 new_band);
1152 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1153 break;
1154 }
1155 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1156 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1157 chandef_downgrade(&new_vht_chandef);
1158 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1159 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1160 chandef_downgrade(&new_vht_chandef);
1161 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1162 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1163 chandef_downgrade(&new_vht_chandef);
1164 }
1165
1166 /* if VHT data is there validate & use it */
1167 if (new_vht_chandef.chan) {
1168 if (!cfg80211_chandef_compatible(&new_vht_chandef,
1169 &new_chandef)) {
1170 sdata_info(sdata,
1171 "AP %pM CSA has inconsistent channel data, disconnecting\n",
1172 ifmgd->associated->bssid);
1173 ieee80211_queue_work(&local->hw,
1174 &ifmgd->csa_connection_drop_work);
1175 return;
1176 }
1177 new_chandef = new_vht_chandef;
1178 }
1179
Johannes Berg85220d72013-03-25 18:29:27 +01001180 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1181 IEEE80211_CHAN_DISABLED)) {
1182 sdata_info(sdata,
1183 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1184 ifmgd->associated->bssid, new_freq,
1185 new_chandef.width, new_chandef.center_freq1,
1186 new_chandef.center_freq2);
1187 ieee80211_queue_work(&local->hw,
1188 &ifmgd->csa_connection_drop_work);
1189 return;
1190 }
1191
Johannes Berg57eebdf2012-08-01 15:50:46 +02001192 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1193
Johannes Bergb4f286a12013-03-26 14:13:58 +01001194 if (local->use_chanctx) {
Johannes Berg55de9082012-07-26 17:24:39 +02001195 sdata_info(sdata,
1196 "not handling channel switch with channel contexts\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001197 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001198 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001199 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001200 }
1201
Johannes Bergb4f286a12013-03-26 14:13:58 +01001202 mutex_lock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001203 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
Johannes Bergb4f286a12013-03-26 14:13:58 +01001204 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001205 return;
1206 }
1207 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1208 struct ieee80211_chanctx, conf);
1209 if (chanctx->refcount > 1) {
1210 sdata_info(sdata,
1211 "channel switch with multiple interfaces on the same channel, disconnecting\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001212 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001213 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001214 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001215 return;
1216 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001217 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001218
Johannes Berg85220d72013-03-25 18:29:27 +01001219 local->csa_chandef = new_chandef;
Johannes Berg55de9082012-07-26 17:24:39 +02001220
Johannes Bergb4f286a12013-03-26 14:13:58 +01001221 if (mode)
1222 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001223 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001224 IEEE80211_QUEUE_STOP_REASON_CSA);
1225
Johannes Bergb4f286a12013-03-26 14:13:58 +01001226 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001227 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001228 struct ieee80211_channel_switch ch_switch = {
1229 .timestamp = timestamp,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001230 .block_tx = mode,
Johannes Berg85220d72013-03-25 18:29:27 +01001231 .chandef = new_chandef,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001232 .count = count,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001233 };
1234
Johannes Bergb4f286a12013-03-26 14:13:58 +01001235 drv_channel_switch(local, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001236 return;
1237 }
1238
1239 /* channel switch handled in software */
Johannes Bergb4f286a12013-03-26 14:13:58 +01001240 if (count <= 1)
1241 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001242 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001243 mod_timer(&ifmgd->chswitch_timer,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001244 TU_TO_EXP_TIME(count * cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001245}
1246
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001247static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1248 struct ieee80211_channel *channel,
1249 const u8 *country_ie, u8 country_ie_len,
1250 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001251{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001252 struct ieee80211_country_ie_triplet *triplet;
1253 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1254 int i, chan_pwr, chan_increment, new_ap_level;
1255 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001256
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001257 /* Invalid IE */
1258 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001259 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001260
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001261 triplet = (void *)(country_ie + 3);
1262 country_ie_len -= 3;
1263
1264 switch (channel->band) {
1265 default:
1266 WARN_ON_ONCE(1);
1267 /* fall through */
1268 case IEEE80211_BAND_2GHZ:
1269 case IEEE80211_BAND_60GHZ:
1270 chan_increment = 1;
1271 break;
1272 case IEEE80211_BAND_5GHZ:
1273 chan_increment = 4;
1274 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001275 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001276
1277 /* find channel */
1278 while (country_ie_len >= 3) {
1279 u8 first_channel = triplet->chans.first_channel;
1280
1281 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1282 goto next;
1283
1284 for (i = 0; i < triplet->chans.num_channels; i++) {
1285 if (first_channel + i * chan_increment == chan) {
1286 have_chan_pwr = true;
1287 chan_pwr = triplet->chans.max_power;
1288 break;
1289 }
1290 }
1291 if (have_chan_pwr)
1292 break;
1293
1294 next:
1295 triplet++;
1296 country_ie_len -= 3;
1297 }
1298
1299 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001300 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001301
1302 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1303
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001304 if (sdata->ap_power_level == new_ap_level)
1305 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001306
1307 sdata_info(sdata,
1308 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1309 new_ap_level, chan_pwr, *pwr_constr_elem,
1310 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001311 sdata->ap_power_level = new_ap_level;
1312 if (__ieee80211_recalc_txpower(sdata))
1313 return BSS_CHANGED_TXPOWER;
1314 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001315}
1316
Johannes Berg965beda2009-04-16 13:17:24 +02001317/* powersave */
1318static void ieee80211_enable_ps(struct ieee80211_local *local,
1319 struct ieee80211_sub_if_data *sdata)
1320{
1321 struct ieee80211_conf *conf = &local->hw.conf;
1322
Johannes Bergd5edaed2009-04-22 23:02:51 +02001323 /*
1324 * If we are scanning right now then the parameters will
1325 * take effect when scan finishes.
1326 */
Helmut Schaafbe9c422009-07-23 12:14:04 +02001327 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001328 return;
1329
Johannes Berg965beda2009-04-16 13:17:24 +02001330 if (conf->dynamic_ps_timeout > 0 &&
1331 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1332 mod_timer(&local->dynamic_ps_timer, jiffies +
1333 msecs_to_jiffies(conf->dynamic_ps_timeout));
1334 } else {
1335 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1336 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301337
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001338 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1339 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1340 return;
1341
1342 conf->flags |= IEEE80211_CONF_PS;
1343 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001344 }
1345}
1346
1347static void ieee80211_change_ps(struct ieee80211_local *local)
1348{
1349 struct ieee80211_conf *conf = &local->hw.conf;
1350
1351 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001352 ieee80211_enable_ps(local, local->ps_sdata);
1353 } else if (conf->flags & IEEE80211_CONF_PS) {
1354 conf->flags &= ~IEEE80211_CONF_PS;
1355 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1356 del_timer_sync(&local->dynamic_ps_timer);
1357 cancel_work_sync(&local->dynamic_ps_enable_work);
1358 }
1359}
1360
Jason Young808118c2011-03-10 16:43:19 -08001361static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1362{
1363 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1364 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001365 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001366
1367 if (!mgd->powersave)
1368 return false;
1369
Johannes Berg05cb9102011-10-28 11:59:47 +02001370 if (mgd->broken_ap)
1371 return false;
1372
Jason Young808118c2011-03-10 16:43:19 -08001373 if (!mgd->associated)
1374 return false;
1375
Jason Young808118c2011-03-10 16:43:19 -08001376 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1377 IEEE80211_STA_CONNECTION_POLL))
1378 return false;
1379
Alexander Bondar989c6502013-05-16 17:34:17 +03001380 if (!mgd->have_beacon)
Alexander Bondarce857882013-05-06 17:17:04 +03001381 return false;
1382
Jason Young808118c2011-03-10 16:43:19 -08001383 rcu_read_lock();
1384 sta = sta_info_get(sdata, mgd->bssid);
1385 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001386 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001387 rcu_read_unlock();
1388
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001389 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001390}
1391
Johannes Berg965beda2009-04-16 13:17:24 +02001392/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001393void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001394{
1395 struct ieee80211_sub_if_data *sdata, *found = NULL;
1396 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001397 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001398
1399 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1400 local->ps_sdata = NULL;
1401 return;
1402 }
1403
1404 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001405 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001406 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301407 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1408 /* If an AP vif is found, then disable PS
1409 * by setting the count to zero thereby setting
1410 * ps_sdata to NULL.
1411 */
1412 count = 0;
1413 break;
1414 }
Johannes Berg965beda2009-04-16 13:17:24 +02001415 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1416 continue;
1417 found = sdata;
1418 count++;
1419 }
1420
Jason Young808118c2011-03-10 16:43:19 -08001421 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001422 s32 beaconint_us;
1423
1424 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001425 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001426
1427 beaconint_us = ieee80211_tu_to_usec(
1428 found->vif.bss_conf.beacon_int);
1429
Juuso Oikarinenff616382010-06-09 09:51:52 +03001430 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001431 if (timeout < 0) {
1432 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001433 * Go to full PSM if the user configures a very low
1434 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001435 * The 2000 second value is there for compatibility
1436 * until the PM_QOS_NETWORK_LATENCY is configured
1437 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001438 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001439 if (latency > (1900 * USEC_PER_MSEC) &&
1440 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001441 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001442 else
1443 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001444 }
Johannes Berg09b85562013-02-06 23:07:41 +01001445 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001446
Johannes Berg04fe2032009-04-22 18:44:37 +02001447 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001448 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001449 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001450 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001451 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001452
1453 /* If the TIM IE is invalid, pretend the value is 1 */
1454 if (!dtimper)
1455 dtimper = 1;
1456 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001457 maxslp = min_t(int, dtimper,
1458 latency / beaconint_us);
1459
Johannes Berg9ccebe62009-04-23 10:32:36 +02001460 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001461 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001462 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001463 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001464 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001465 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001466 }
Johannes Berg965beda2009-04-16 13:17:24 +02001467
1468 ieee80211_change_ps(local);
1469}
1470
Eliad Pellerab095872012-07-27 12:33:22 +03001471void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1472{
1473 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1474
1475 if (sdata->vif.bss_conf.ps != ps_allowed) {
1476 sdata->vif.bss_conf.ps = ps_allowed;
1477 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1478 }
1479}
1480
Johannes Berg965beda2009-04-16 13:17:24 +02001481void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1482{
1483 struct ieee80211_local *local =
1484 container_of(work, struct ieee80211_local,
1485 dynamic_ps_disable_work);
1486
1487 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1488 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1489 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1490 }
1491
1492 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001493 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001494 IEEE80211_QUEUE_STOP_REASON_PS);
1495}
1496
1497void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1498{
1499 struct ieee80211_local *local =
1500 container_of(work, struct ieee80211_local,
1501 dynamic_ps_enable_work);
1502 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001503 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301504 unsigned long flags;
1505 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001506
1507 /* can only happen when PS was just disabled anyway */
1508 if (!sdata)
1509 return;
1510
Christian Lamparter5e340692011-06-30 21:08:43 +02001511 ifmgd = &sdata->u.mgd;
1512
Johannes Berg965beda2009-04-16 13:17:24 +02001513 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1514 return;
1515
Johannes Berg09b85562013-02-06 23:07:41 +01001516 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001517 /* don't enter PS if TX frames are pending */
1518 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301519 mod_timer(&local->dynamic_ps_timer, jiffies +
1520 msecs_to_jiffies(
1521 local->hw.conf.dynamic_ps_timeout));
1522 return;
1523 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001524
1525 /*
1526 * transmission can be stopped by others which leads to
1527 * dynamic_ps_timer expiry. Postpone the ps timer if it
1528 * is not the actual idle state.
1529 */
1530 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1531 for (q = 0; q < local->hw.queues; q++) {
1532 if (local->queue_stop_reasons[q]) {
1533 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1534 flags);
1535 mod_timer(&local->dynamic_ps_timer, jiffies +
1536 msecs_to_jiffies(
1537 local->hw.conf.dynamic_ps_timeout));
1538 return;
1539 }
1540 }
1541 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301542 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301543
Vivek Natarajan375177b2010-02-09 14:50:28 +05301544 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301545 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga1598382013-03-26 22:02:42 +01001546 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301547 mod_timer(&local->dynamic_ps_timer, jiffies +
1548 msecs_to_jiffies(
1549 local->hw.conf.dynamic_ps_timeout));
Johannes Berga1598382013-03-26 22:02:42 +01001550 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301551 ieee80211_send_nullfunc(local, sdata, 1);
1552 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001553 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301554 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301555 }
1556
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001557 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1558 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301559 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1560 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1561 local->hw.conf.flags |= IEEE80211_CONF_PS;
1562 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1563 }
Johannes Berg965beda2009-04-16 13:17:24 +02001564}
1565
1566void ieee80211_dynamic_ps_timer(unsigned long data)
1567{
1568 struct ieee80211_local *local = (void *) data;
1569
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001570 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001571 return;
1572
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001573 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001574}
1575
Simon Wunderlich164eb022013-02-08 18:16:20 +01001576void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1577{
1578 struct delayed_work *delayed_work =
1579 container_of(work, struct delayed_work, work);
1580 struct ieee80211_sub_if_data *sdata =
1581 container_of(delayed_work, struct ieee80211_sub_if_data,
1582 dfs_cac_timer_work);
1583
1584 ieee80211_vif_release_channel(sdata);
1585
1586 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1587}
1588
Johannes Berg60f8b392008-09-08 17:44:22 +02001589/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001590static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001591 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001592 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001593{
Jiri Bencf0706e82007-05-05 11:45:53 -07001594 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001595 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001596 size_t left;
1597 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001598 const u8 *pos;
1599 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001600
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001601 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001602 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001603
Johannes Berg32c50572012-03-28 11:04:29 +02001604 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001605 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001606
1607 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001608 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001609
Jiri Bencf0706e82007-05-05 11:45:53 -07001610 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001611 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001612
1613 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001614 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001615
Jiri Bencf0706e82007-05-05 11:45:53 -07001616 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001617 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001618 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001619 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001620
1621 pos = wmm_param + 8;
1622 left = wmm_param_len - 8;
1623
1624 memset(&params, 0, sizeof(params));
1625
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001626 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001627 for (; left >= 4; left -= 4, pos += 4) {
1628 int aci = (pos[0] >> 5) & 0x03;
1629 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001630 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001631 int queue;
1632
1633 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001634 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001635 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001636 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001637 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001638 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1639 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001640 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001641 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001642 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001643 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001644 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001645 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1646 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001647 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001648 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001649 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001650 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001651 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001652 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1653 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001654 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001655 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001656 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001657 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001658 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001659 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001660 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1661 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001662 break;
1663 }
1664
1665 params.aifs = pos[0] & 0x0f;
1666 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1667 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001668 params.txop = get_unaligned_le16(pos + 2);
Alexander Bondar908f8d02013-04-07 09:53:30 +03001669 params.acm = acm;
Kalle Valoab133152010-01-12 10:42:31 +02001670 params.uapsd = uapsd;
1671
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001672 mlme_dbg(sdata,
1673 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1674 queue, aci, acm,
1675 params.aifs, params.cw_min, params.cw_max,
1676 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001677 sdata->tx_conf[queue] = params;
1678 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001679 sdata_err(sdata,
1680 "failed to set TX queue parameters for queue %d\n",
1681 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001682 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001683
1684 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001685 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001686 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001687}
1688
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001689static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1690{
1691 lockdep_assert_held(&sdata->local->mtx);
1692
1693 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1694 IEEE80211_STA_BEACON_POLL);
1695 ieee80211_run_deferred_scan(sdata->local);
1696}
1697
1698static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1699{
1700 mutex_lock(&sdata->local->mtx);
1701 __ieee80211_stop_poll(sdata);
1702 mutex_unlock(&sdata->local->mtx);
1703}
1704
Johannes Berg7a5158e2008-10-08 10:59:33 +02001705static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1706 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001707{
Johannes Bergbda39332008-10-11 01:51:51 +02001708 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001709 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001710 bool use_protection;
1711 bool use_short_preamble;
1712 bool use_short_slot;
1713
1714 if (erp_valid) {
1715 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1716 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1717 } else {
1718 use_protection = false;
1719 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1720 }
1721
1722 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001723 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001724 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001725
Johannes Berg471b3ef2007-12-28 14:32:58 +01001726 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001727 bss_conf->use_cts_prot = use_protection;
1728 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001729 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001730
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001731 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001732 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001733 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001734 }
Daniel Draked9430a32007-07-27 15:43:24 +02001735
Johannes Berg7a5158e2008-10-08 10:59:33 +02001736 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001737 bss_conf->use_short_slot = use_short_slot;
1738 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001739 }
1740
1741 return changed;
1742}
1743
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001744static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001745 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001746 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001747{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001748 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001749 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001750 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001751
Johannes Bergae5eb022008-10-14 16:58:37 +02001752 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001753 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001754 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001755
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001756 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001757 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001758
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001759 sdata->u.mgd.associated = cbss;
1760 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001761
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001762 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1763
Johannes Berg488dd7b2012-10-29 20:08:01 +01001764 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001765 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001766
Johannes Berg9caf0362012-11-29 01:25:20 +01001767 rcu_read_lock();
1768 ies = rcu_dereference(cbss->ies);
1769 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001770 int ret;
1771
1772 ret = cfg80211_get_p2p_attr(
1773 ies->data, ies->len,
1774 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001775 (u8 *) &bss_conf->p2p_noa_attr,
1776 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001777 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001778 sdata->u.mgd.p2p_noa_index =
1779 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001780 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001781 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001782 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001783 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001784 }
1785
Johannes Bergb291ba12009-07-10 15:29:03 +02001786 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001787 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001788
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001789 ieee80211_led_assoc(local, 1);
1790
Alexander Bondar989c6502013-05-16 17:34:17 +03001791 if (sdata->u.mgd.have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001792 /*
1793 * If the AP is buggy we may get here with no DTIM period
1794 * known, so assume it's 1 which is the only safe assumption
1795 * in that case, although if the TIM IE is broken powersave
1796 * probably just won't work at all.
1797 */
1798 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Alexander Bondar817cee72013-05-19 14:23:57 +03001799 bss_conf->beacon_rate = bss->beacon_rate;
Alexander Bondar989c6502013-05-16 17:34:17 +03001800 bss_info_changed |= BSS_CHANGED_BEACON_INFO;
Johannes Berg826262c2012-12-10 16:38:14 +02001801 } else {
Alexander Bondar817cee72013-05-19 14:23:57 +03001802 bss_conf->beacon_rate = NULL;
Johannes Berge5b900d2010-07-29 16:08:55 +02001803 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001804 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001805
Juuso Oikarinen68542962010-06-09 13:43:26 +03001806 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001807
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001808 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001809 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001810 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001811 bss_info_changed |= BSS_CHANGED_CQM;
1812
Juuso Oikarinen68542962010-06-09 13:43:26 +03001813 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001814 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001815 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001816
Johannes Bergae5eb022008-10-14 16:58:37 +02001817 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001818
Johannes Berg056508d2009-07-30 21:43:55 +02001819 mutex_lock(&local->iflist_mtx);
1820 ieee80211_recalc_ps(local, -1);
1821 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001822
Johannes Berg04ecd252012-09-11 14:34:12 +02001823 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001824 ieee80211_recalc_ps_vif(sdata);
1825
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001826 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001827}
1828
Jouni Malinene69e95d2010-03-29 23:29:31 -07001829static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001830 u16 stype, u16 reason, bool tx,
1831 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001832{
Johannes Berg46900292009-02-15 12:44:28 +01001833 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001834 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001835 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001836
Johannes Berg8d61ffa2013-05-10 12:32:47 +02001837 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02001838
Johannes Berg37ad3882012-02-24 13:50:54 +01001839 if (WARN_ON_ONCE(tx && !frame_buf))
1840 return;
1841
Johannes Bergb291ba12009-07-10 15:29:03 +02001842 if (WARN_ON(!ifmgd->associated))
1843 return;
1844
David Spinadel79543d82012-06-12 09:59:45 +03001845 ieee80211_stop_poll(sdata);
1846
Johannes Berg77fdaa12009-07-07 03:45:17 +02001847 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001848 netif_carrier_off(sdata->dev);
1849
Eliad Peller88bc40e2012-07-12 17:35:33 +03001850 /*
1851 * if we want to get out of ps before disassoc (why?) we have
1852 * to do it before sending disassoc, as otherwise the null-packet
1853 * won't be valid.
1854 */
1855 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1856 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1857 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1858 }
1859 local->ps_sdata = NULL;
1860
Eliad Pellerab095872012-07-27 12:33:22 +03001861 /* disable per-vif ps */
1862 ieee80211_recalc_ps_vif(sdata);
1863
Eliad Pellerf8239812012-06-27 14:18:22 +03001864 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1865 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001866 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001867
Johannes Berg37ad3882012-02-24 13:50:54 +01001868 /* deauthenticate/disassociate now */
1869 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001870 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1871 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001872
1873 /* flush out frame */
1874 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001875 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001876
Eliad Peller88a9e312012-06-01 11:14:03 +03001877 /* clear bssid only after building the needed mgmt frames */
1878 memset(ifmgd->bssid, 0, ETH_ALEN);
1879
Johannes Berg37ad3882012-02-24 13:50:54 +01001880 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001881 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001882
1883 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001884 changed |= ieee80211_reset_erp_info(sdata);
1885
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001886 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001887 changed |= BSS_CHANGED_ASSOC;
1888 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001889
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001890 ifmgd->p2p_noa_index = -1;
1891 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1892 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001893
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001894 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001895 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1896 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001897 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1898 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001899
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001900 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301901
Kalle Valo520eb822008-12-18 23:35:27 +02001902 del_timer_sync(&local->dynamic_ps_timer);
1903 cancel_work_sync(&local->dynamic_ps_enable_work);
1904
Juuso Oikarinen68542962010-06-09 13:43:26 +03001905 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001906 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001907 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001908
Johannes Berg3abead52012-03-02 15:56:59 +01001909 sdata->vif.bss_conf.qos = false;
1910 changed |= BSS_CHANGED_QOS;
1911
Johannes Berg0aaffa92010-05-05 15:28:27 +02001912 /* The BSSID (not really interesting) and HT changed */
1913 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001914 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001915
Johannes Berg3abead52012-03-02 15:56:59 +01001916 /* disassociated - set to defaults now */
1917 ieee80211_set_wmm_default(sdata, false);
1918
Johannes Bergb9dcf712010-08-27 12:35:54 +02001919 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1920 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1921 del_timer_sync(&sdata->u.mgd.timer);
1922 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001923
Johannes Berg826262c2012-12-10 16:38:14 +02001924 sdata->vif.bss_conf.dtim_period = 0;
Alexander Bondar817cee72013-05-19 14:23:57 +03001925 sdata->vif.bss_conf.beacon_rate = NULL;
1926
Alexander Bondar989c6502013-05-16 17:34:17 +03001927 ifmgd->have_beacon = false;
Johannes Berg826262c2012-12-10 16:38:14 +02001928
Johannes Berg028e8da02012-11-26 11:57:41 +01001929 ifmgd->flags = 0;
1930 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001931}
Jiri Bencf0706e82007-05-05 11:45:53 -07001932
Kalle Valo3cf335d2009-03-22 21:57:06 +02001933void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1934 struct ieee80211_hdr *hdr)
1935{
1936 /*
1937 * We can postpone the mgd.timer whenever receiving unicast frames
1938 * from AP because we know that the connection is working both ways
1939 * at that time. But multicast frames (and hence also beacons) must
1940 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001941 * data idle periods for sending the periodic probe request to the
1942 * AP we're connected to.
Kalle Valo3cf335d2009-03-22 21:57:06 +02001943 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001944 if (is_multicast_ether_addr(hdr->addr1))
1945 return;
1946
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001947 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d2009-03-22 21:57:06 +02001948}
Jiri Bencf0706e82007-05-05 11:45:53 -07001949
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001950static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1951{
1952 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001953 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001954
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001955 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001956 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001957 IEEE80211_STA_CONNECTION_POLL))) {
1958 mutex_unlock(&local->mtx);
1959 return;
1960 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001961
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001962 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001963
1964 mutex_lock(&local->iflist_mtx);
1965 ieee80211_recalc_ps(local, -1);
1966 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001967
1968 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001969 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001970
1971 /*
1972 * We've received a probe response, but are not sure whether
1973 * we have or will be receiving any beacons or data, so let's
1974 * schedule the timers again, just in case.
1975 */
1976 ieee80211_sta_reset_beacon_monitor(sdata);
1977
1978 mod_timer(&ifmgd->conn_mon_timer,
1979 round_jiffies_up(jiffies +
1980 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001981out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001982 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001983}
1984
1985void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001986 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001987{
Felix Fietkau75706d02010-12-02 21:01:07 +01001988 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001989 return;
1990
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001991 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1992 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001993 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001994 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001995 else
1996 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001997 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001998 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001999 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01002000
2001 if (ack)
2002 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002003}
2004
Maxim Levitskya43abf22009-07-31 18:54:12 +03002005static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2006{
2007 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2008 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002009 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002010 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002011
2012 /*
2013 * Try sending broadcast probe requests for the last three
2014 * probe requests after the first ones failed since some
2015 * buggy APs only support broadcast probe requests.
2016 */
2017 if (ifmgd->probe_send_count >= unicast_limit)
2018 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002019
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002020 /*
2021 * When the hardware reports an accurate Tx ACK status, it's
2022 * better to send a nullfunc frame instead of a probe request,
2023 * as it will kick us off the AP quickly if we aren't associated
2024 * anymore. The timeout will be reset if the frame is ACKed by
2025 * the AP.
2026 */
Soumik Das992e68b2012-05-20 15:31:13 +05302027 ifmgd->probe_send_count++;
2028
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002029 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2030 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002031 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002032 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002033 int ssid_len;
2034
Johannes Berg9caf0362012-11-29 01:25:20 +01002035 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002036 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002037 if (WARN_ON_ONCE(ssid == NULL))
2038 ssid_len = 0;
2039 else
2040 ssid_len = ssid[1];
2041
2042 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002043 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02002044 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002045 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002046 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002047
Ben Greear180205b2011-02-04 15:30:24 -08002048 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002049 run_again(sdata, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05302050 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01002051 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002052}
2053
Johannes Bergb291ba12009-07-10 15:29:03 +02002054static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2055 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002056{
Kalle Valo04de8382009-03-22 21:57:35 +02002057 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002058 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002059
Johannes Berg9607e6b2009-12-23 13:15:31 +01002060 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002061 return;
2062
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002063 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002064
2065 if (!ifmgd->associated)
2066 goto out;
2067
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002068 mutex_lock(&sdata->local->mtx);
2069
2070 if (sdata->local->tmp_channel || sdata->local->scanning) {
2071 mutex_unlock(&sdata->local->mtx);
2072 goto out;
2073 }
2074
Ben Greeara13fbe52013-03-25 11:19:35 -07002075 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002076 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002077 "detected beacon loss from AP (missed %d beacons) - probing\n",
2078 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002079
Ben Greeara13fbe52013-03-25 11:19:35 -07002080 ieee80211_cqm_rssi_notify(&sdata->vif,
2081 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
2082 GFP_KERNEL);
2083 }
Kalle Valo04de8382009-03-22 21:57:35 +02002084
Johannes Bergb291ba12009-07-10 15:29:03 +02002085 /*
2086 * The driver/our work has already reported this event or the
2087 * connection monitoring has kicked in and we have already sent
2088 * a probe request. Or maybe the AP died and the driver keeps
2089 * reporting until we disassociate...
2090 *
2091 * In either case we have to ignore the current call to this
2092 * function (except for setting the correct probe reason bit)
2093 * because otherwise we would reset the timer every time and
2094 * never check whether we received a probe response!
2095 */
2096 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2097 IEEE80211_STA_CONNECTION_POLL))
2098 already = true;
2099
2100 if (beacon)
2101 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2102 else
2103 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2104
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002105 mutex_unlock(&sdata->local->mtx);
2106
Johannes Bergb291ba12009-07-10 15:29:03 +02002107 if (already)
2108 goto out;
2109
Johannes Berg4e751842009-06-10 15:16:52 +02002110 mutex_lock(&sdata->local->iflist_mtx);
2111 ieee80211_recalc_ps(sdata->local, -1);
2112 mutex_unlock(&sdata->local->iflist_mtx);
2113
Maxim Levitskya43abf22009-07-31 18:54:12 +03002114 ifmgd->probe_send_count = 0;
2115 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002116 out:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002117 sdata_unlock(sdata);
Kalle Valo04de8382009-03-22 21:57:35 +02002118}
2119
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002120struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2121 struct ieee80211_vif *vif)
2122{
2123 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002125 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002126 struct sk_buff *skb;
2127 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002128 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002129
2130 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2131 return NULL;
2132
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002133 sdata_assert_lock(sdata);
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002134
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002135 if (ifmgd->associated)
2136 cbss = ifmgd->associated;
2137 else if (ifmgd->auth_data)
2138 cbss = ifmgd->auth_data->bss;
2139 else if (ifmgd->assoc_data)
2140 cbss = ifmgd->assoc_data->bss;
2141 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002142 return NULL;
2143
Johannes Berg9caf0362012-11-29 01:25:20 +01002144 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002145 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002146 if (WARN_ON_ONCE(ssid == NULL))
2147 ssid_len = 0;
2148 else
2149 ssid_len = ssid[1];
2150
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002151 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002152 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002153 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002154 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002155 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002156
2157 return skb;
2158}
2159EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2160
Johannes Bergeef9e542013-01-29 13:09:34 +01002161static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002162{
2163 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002164 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002165
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002166 sdata_lock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002167 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002168 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002169 return;
2170 }
2171
Johannes Berg37ad3882012-02-24 13:50:54 +01002172 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2173 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002174 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002175 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002176 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002177 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002178 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002179
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002180 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
2181 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002182 sdata_unlock(sdata);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002183}
2184
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002185static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002186{
2187 struct ieee80211_sub_if_data *sdata =
2188 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002189 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002190 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2191 struct sta_info *sta;
2192
2193 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302194 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002195 sta = sta_info_get(sdata, ifmgd->bssid);
2196 if (sta)
2197 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302198 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002199 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002200
Johannes Berg682bd382013-01-29 13:13:50 +01002201 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002202 sdata_info(sdata, "Connection to AP %pM lost\n",
2203 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002204 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002205 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002206 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002207 }
2208}
2209
2210static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2211{
2212 struct ieee80211_sub_if_data *sdata =
2213 container_of(work, struct ieee80211_sub_if_data,
2214 u.mgd.csa_connection_drop_work);
2215
Johannes Bergeef9e542013-01-29 13:09:34 +01002216 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002217}
2218
Kalle Valo04de8382009-03-22 21:57:35 +02002219void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2220{
2221 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002222 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002223
Johannes Bergb5878a22010-04-07 16:48:40 +02002224 trace_api_beacon_loss(sdata);
2225
Johannes Berg682bd382013-01-29 13:13:50 +01002226 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002227 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002228}
2229EXPORT_SYMBOL(ieee80211_beacon_loss);
2230
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002231void ieee80211_connection_loss(struct ieee80211_vif *vif)
2232{
2233 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2234 struct ieee80211_hw *hw = &sdata->local->hw;
2235
Johannes Bergb5878a22010-04-07 16:48:40 +02002236 trace_api_connection_loss(sdata);
2237
Johannes Berg682bd382013-01-29 13:13:50 +01002238 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002239 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2240}
2241EXPORT_SYMBOL(ieee80211_connection_loss);
2242
2243
Johannes Berg66e67e42012-01-20 13:55:27 +01002244static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2245 bool assoc)
2246{
2247 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2248
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002249 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002250
Johannes Berg66e67e42012-01-20 13:55:27 +01002251 if (!assoc) {
2252 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2253
2254 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2255 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002256 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002257 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002258 }
2259
Johannes Berg5b112d32013-02-01 01:49:58 +01002260 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002261 kfree(auth_data);
2262 sdata->u.mgd.auth_data = NULL;
2263}
2264
2265static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2266 struct ieee80211_mgmt *mgmt, size_t len)
2267{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002268 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002269 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2270 u8 *pos;
2271 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002272 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002273
2274 pos = mgmt->u.auth.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002275 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002276 if (!elems.challenge)
2277 return;
2278 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002279 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002280 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2281 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2282 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002283 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002284 elems.challenge - 2, elems.challenge_len + 2,
2285 auth_data->bss->bssid, auth_data->bss->bssid,
2286 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002287 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002288}
2289
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002290static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2291 struct ieee80211_mgmt *mgmt, size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002292{
2293 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2294 u8 bssid[ETH_ALEN];
2295 u16 auth_alg, auth_transaction, status_code;
2296 struct sta_info *sta;
2297
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002298 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002299
2300 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002301 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002302
2303 if (!ifmgd->auth_data || ifmgd->auth_data->done)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002304 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002305
2306 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2307
Joe Perchesb203ca32012-05-08 18:56:52 +00002308 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002309 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002310
2311 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2312 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2313 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2314
2315 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002316 auth_transaction != ifmgd->auth_data->expected_transaction) {
2317 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2318 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2319 auth_transaction,
2320 ifmgd->auth_data->expected_transaction);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002321 return;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002322 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002323
2324 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002325 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2326 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002327 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002328 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002329 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002330 }
2331
2332 switch (ifmgd->auth_data->algorithm) {
2333 case WLAN_AUTH_OPEN:
2334 case WLAN_AUTH_LEAP:
2335 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002336 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002337 break;
2338 case WLAN_AUTH_SHARED_KEY:
2339 if (ifmgd->auth_data->expected_transaction != 4) {
2340 ieee80211_auth_challenge(sdata, mgmt, len);
2341 /* need another frame */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002342 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002343 }
2344 break;
2345 default:
2346 WARN_ONCE(1, "invalid auth alg %d",
2347 ifmgd->auth_data->algorithm);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002348 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002349 }
2350
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002351 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002352 ifmgd->auth_data->done = true;
2353 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002354 ifmgd->auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002355 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01002356
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002357 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2358 ifmgd->auth_data->expected_transaction != 2) {
2359 /*
2360 * Report auth frame to user space for processing since another
2361 * round of Authentication frames is still needed.
2362 */
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002363 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002364 return;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002365 }
2366
Johannes Berg66e67e42012-01-20 13:55:27 +01002367 /* move station state to auth */
2368 mutex_lock(&sdata->local->sta_mtx);
2369 sta = sta_info_get(sdata, bssid);
2370 if (!sta) {
2371 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2372 goto out_err;
2373 }
2374 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002375 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002376 goto out_err;
2377 }
2378 mutex_unlock(&sdata->local->sta_mtx);
2379
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002380 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002381 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002382 out_err:
2383 mutex_unlock(&sdata->local->sta_mtx);
2384 /* ignore frame -- wait for timeout */
Johannes Berg66e67e42012-01-20 13:55:27 +01002385}
2386
2387
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002388static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2389 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002390{
Johannes Berg46900292009-02-15 12:44:28 +01002391 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002392 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002393 u16 reason_code;
2394
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002395 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002396
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002397 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002398 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002399
Johannes Berg66e67e42012-01-20 13:55:27 +01002400 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002401 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002402 return;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002403
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002404 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002405
2406 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2407
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002408 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2409 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002410
Johannes Berg37ad3882012-02-24 13:50:54 +01002411 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2412
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002413 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002414}
2415
2416
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002417static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2418 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002419{
Johannes Berg46900292009-02-15 12:44:28 +01002420 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002421 u16 reason_code;
2422
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002423 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002424
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002425 if (len < 24 + 2)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002426 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002427
Johannes Berg66e67e42012-01-20 13:55:27 +01002428 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002429 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002430 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002431
2432 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2433
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002434 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2435 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002436
Johannes Berg37ad3882012-02-24 13:50:54 +01002437 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2438
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002439 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002440}
2441
Christian Lamparterc74d0842011-10-15 00:14:49 +02002442static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2443 u8 *supp_rates, unsigned int supp_rates_len,
2444 u32 *rates, u32 *basic_rates,
2445 bool *have_higher_than_11mbit,
2446 int *min_rate, int *min_rate_index)
2447{
2448 int i, j;
2449
2450 for (i = 0; i < supp_rates_len; i++) {
2451 int rate = (supp_rates[i] & 0x7f) * 5;
2452 bool is_basic = !!(supp_rates[i] & 0x80);
2453
2454 if (rate > 110)
2455 *have_higher_than_11mbit = true;
2456
2457 /*
2458 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2459 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2460 *
2461 * Note: Even through the membership selector and the basic
2462 * rate flag share the same bit, they are not exactly
2463 * the same.
2464 */
2465 if (!!(supp_rates[i] & 0x80) &&
2466 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2467 continue;
2468
2469 for (j = 0; j < sband->n_bitrates; j++) {
2470 if (sband->bitrates[j].bitrate == rate) {
2471 *rates |= BIT(j);
2472 if (is_basic)
2473 *basic_rates |= BIT(j);
2474 if (rate < *min_rate) {
2475 *min_rate = rate;
2476 *min_rate_index = j;
2477 }
2478 break;
2479 }
2480 }
2481 }
2482}
Jiri Bencf0706e82007-05-05 11:45:53 -07002483
Johannes Berg66e67e42012-01-20 13:55:27 +01002484static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2485 bool assoc)
2486{
2487 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2488
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002489 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002490
Johannes Berg66e67e42012-01-20 13:55:27 +01002491 if (!assoc) {
2492 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2493
2494 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2495 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002496 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002497 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002498 }
2499
2500 kfree(assoc_data);
2501 sdata->u.mgd.assoc_data = NULL;
2502}
2503
2504static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2505 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002506 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002507{
Johannes Berg46900292009-02-15 12:44:28 +01002508 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002509 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002510 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002511 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002512 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002513 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002514 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002515 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg35d865a2013-05-28 10:54:03 +02002516 const struct cfg80211_bss_ies *bss_ies = NULL;
2517 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
Johannes Bergae5eb022008-10-14 16:58:37 +02002518 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002519 int err;
Johannes Berg35d865a2013-05-28 10:54:03 +02002520 bool ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002521
Johannes Bergaf6b6372009-12-23 13:15:35 +01002522 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002523
Jiri Bencf0706e82007-05-05 11:45:53 -07002524 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002525 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002526
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002527 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002528 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2529 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002530 aid &= ~(BIT(15) | BIT(14));
2531
Johannes Berg05cb9102011-10-28 11:59:47 +02002532 ifmgd->broken_ap = false;
2533
2534 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002535 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2536 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002537 aid = 0;
2538 ifmgd->broken_ap = true;
2539 }
2540
Johannes Bergaf6b6372009-12-23 13:15:35 +01002541 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002542 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002543
Jiri Bencf0706e82007-05-05 11:45:53 -07002544 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002545 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002546 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002547 }
2548
Johannes Berg46900292009-02-15 12:44:28 +01002549 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002550
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002551 /*
Johannes Berg35d865a2013-05-28 10:54:03 +02002552 * Some APs are erroneously not including some information in their
2553 * (re)association response frames. Try to recover by using the data
2554 * from the beacon or probe response. This seems to afflict mobile
2555 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
2556 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
2557 */
2558 if ((assoc_data->wmm && !elems.wmm_param) ||
2559 (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2560 (!elems.ht_cap_elem || !elems.ht_operation)) ||
2561 (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2562 (!elems.vht_cap_elem || !elems.vht_operation))) {
2563 const struct cfg80211_bss_ies *ies;
2564 struct ieee802_11_elems bss_elems;
2565
2566 rcu_read_lock();
2567 ies = rcu_dereference(cbss->ies);
2568 if (ies)
2569 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
2570 GFP_ATOMIC);
2571 rcu_read_unlock();
2572 if (!bss_ies)
2573 return false;
2574
2575 ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
2576 false, &bss_elems);
2577 if (assoc_data->wmm &&
2578 !elems.wmm_param && bss_elems.wmm_param) {
2579 elems.wmm_param = bss_elems.wmm_param;
2580 sdata_info(sdata,
2581 "AP bug: WMM param missing from AssocResp\n");
2582 }
2583
2584 /*
2585 * Also check if we requested HT/VHT, otherwise the AP doesn't
2586 * have to include the IEs in the (re)association response.
2587 */
2588 if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
2589 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2590 elems.ht_cap_elem = bss_elems.ht_cap_elem;
2591 sdata_info(sdata,
2592 "AP bug: HT capability missing from AssocResp\n");
2593 }
2594 if (!elems.ht_operation && bss_elems.ht_operation &&
2595 !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
2596 elems.ht_operation = bss_elems.ht_operation;
2597 sdata_info(sdata,
2598 "AP bug: HT operation missing from AssocResp\n");
2599 }
2600 if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
2601 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2602 elems.vht_cap_elem = bss_elems.vht_cap_elem;
2603 sdata_info(sdata,
2604 "AP bug: VHT capa missing from AssocResp\n");
2605 }
2606 if (!elems.vht_operation && bss_elems.vht_operation &&
2607 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
2608 elems.vht_operation = bss_elems.vht_operation;
2609 sdata_info(sdata,
2610 "AP bug: VHT operation missing from AssocResp\n");
2611 }
2612 }
2613
2614 /*
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002615 * We previously checked these in the beacon/probe response, so
2616 * they should be present here. This is just a safety net.
2617 */
2618 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2619 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2620 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002621 "HT AP is missing WMM params or HT capability/operation\n");
2622 ret = false;
2623 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002624 }
2625
2626 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2627 (!elems.vht_cap_elem || !elems.vht_operation)) {
2628 sdata_info(sdata,
Johannes Berg35d865a2013-05-28 10:54:03 +02002629 "VHT AP is missing VHT capability/operation\n");
2630 ret = false;
2631 goto out;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002632 }
2633
Guy Eilam2a33bee2011-08-17 15:18:15 +03002634 mutex_lock(&sdata->local->sta_mtx);
2635 /*
2636 * station info was already allocated and inserted before
2637 * the association and should be available to us
2638 */
Johannes Berg7852e362012-01-20 13:55:24 +01002639 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002640 if (WARN_ON(!sta)) {
2641 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002642 ret = false;
2643 goto out;
Jiri Bencf0706e82007-05-05 11:45:53 -07002644 }
2645
Johannes Berg55de9082012-07-26 17:24:39 +02002646 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002647
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002648 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002649 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002650 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002651 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002652
Mahesh Palivela818255e2012-10-10 11:33:04 +00002653 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2654 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002655 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002656
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002657 /*
2658 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2659 * in their association response, so ignore that data for our own
2660 * configuration. If it changed since the last beacon, we'll get the
2661 * next beacon and update then.
2662 */
Johannes Berg11289582013-02-07 17:36:12 +01002663
Johannes Bergbee7f582013-02-07 22:24:55 +01002664 /*
2665 * If an operating mode notification IE is present, override the
2666 * NSS calculation (that would be done in rate_control_rate_init())
2667 * and use the # of streams from that element.
2668 */
2669 if (elems.opmode_notif &&
2670 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2671 u8 nss;
2672
2673 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2674 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2675 nss += 1;
2676 sta->sta.rx_nss = nss;
2677 }
2678
Johannes Berg4b7679a2008-09-18 18:14:18 +02002679 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002680
Johannes Berg46900292009-02-15 12:44:28 +01002681 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002682 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002683
Johannes Bergddf4ac52008-10-22 11:41:38 +02002684 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002685 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002686
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002687 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002688 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2689 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2690 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002691 sdata_info(sdata,
2692 "failed to move station %pM to desired state\n",
2693 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002694 WARN_ON(__sta_info_destroy(sta));
2695 mutex_unlock(&sdata->local->sta_mtx);
Johannes Berg35d865a2013-05-28 10:54:03 +02002696 ret = false;
2697 goto out;
Johannes Bergc8987872012-01-20 13:55:17 +01002698 }
2699
Johannes Berg7852e362012-01-20 13:55:24 +01002700 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002701
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002702 /*
2703 * Always handle WMM once after association regardless
2704 * of the first value the AP uses. Setting -1 here has
2705 * that effect because the AP values is an unsigned
2706 * 4-bit value.
2707 */
2708 ifmgd->wmm_last_param_set = -1;
2709
Johannes Bergddf4ac52008-10-22 11:41:38 +02002710 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002711 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002712 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002713 else
Johannes Berg3abead52012-03-02 15:56:59 +01002714 ieee80211_set_wmm_default(sdata, false);
2715 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002716
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002717 /* set AID and assoc capability,
2718 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002719 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002720 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002721 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002722
Kalle Valo15b7b062009-03-22 21:57:14 +02002723 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002724 * If we're using 4-addr mode, let the AP know that we're
2725 * doing so, so that it can create the STA VLAN on its side
2726 */
2727 if (ifmgd->use_4addr)
2728 ieee80211_send_4addr_nullfunc(local, sdata);
2729
2730 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002731 * Start timer to probe the connection to the AP now.
2732 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002733 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002734 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002735 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002736
Johannes Berg35d865a2013-05-28 10:54:03 +02002737 ret = true;
2738 out:
2739 kfree(bss_ies);
2740 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07002741}
2742
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002743static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2744 struct ieee80211_mgmt *mgmt,
2745 size_t len)
Johannes Berg66e67e42012-01-20 13:55:27 +01002746{
2747 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2748 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2749 u16 capab_info, status_code, aid;
2750 struct ieee802_11_elems elems;
2751 u8 *pos;
2752 bool reassoc;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002753 struct cfg80211_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002754
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002755 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002756
2757 if (!assoc_data)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002758 return;
Joe Perchesb203ca32012-05-08 18:56:52 +00002759 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002760 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002761
2762 /*
2763 * AssocResp and ReassocResp have identical structure, so process both
2764 * of them in this function.
2765 */
2766
2767 if (len < 24 + 6)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002768 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002769
2770 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2771 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2772 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2773 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2774
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002775 sdata_info(sdata,
2776 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2777 reassoc ? "Rea" : "A", mgmt->sa,
2778 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002779
2780 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002781 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002782
2783 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002784 elems.timeout_int &&
2785 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002786 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002787 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002788 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002789 sdata_info(sdata,
2790 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2791 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002792 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002793 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002794 if (ms > IEEE80211_ASSOC_TIMEOUT)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002795 run_again(sdata, assoc_data->timeout);
2796 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002797 }
2798
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002799 bss = assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01002800
2801 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002802 sdata_info(sdata, "%pM denied association (code=%d)\n",
2803 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002804 ieee80211_destroy_assoc_data(sdata, false);
2805 } else {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002806 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002807 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002808 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02002809 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002810 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002811 }
John W. Linville635d9992012-07-09 16:34:34 -04002812 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002813
2814 /*
2815 * destroy assoc_data afterwards, as otherwise an idle
2816 * recalc after assoc_data is NULL but before associated
2817 * is set can cause the interface to go idle
2818 */
2819 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002820 }
2821
Johannes Berg6ff57cf2013-05-16 00:55:00 +02002822 cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
Johannes Berg66e67e42012-01-20 13:55:27 +01002823}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002824
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002825static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002826 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002827 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002828 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002829{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002830 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002831 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002832 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002833 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002834
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002835 sdata_assert_lock(sdata);
Johannes Bergb4f286a12013-03-26 14:13:58 +01002836
Johannes Berg1cd8e882013-03-27 14:30:12 +01002837 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002838 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2839 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002840 else
2841 freq = rx_status->freq;
2842
2843 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2844
2845 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2846 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002847
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002848 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002849 channel);
Alexander Bondar817cee72013-05-19 14:23:57 +03002850 if (bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02002851 ieee80211_rx_bss_put(local, bss);
Alexander Bondar817cee72013-05-19 14:23:57 +03002852 sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
2853 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02002854
Johannes Berg37799e52013-03-26 14:02:26 +01002855 if (!sdata->u.mgd.associated ||
2856 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002857 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002858
Johannes Berg04a161f2013-05-03 09:35:35 +02002859 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
2860 elems, true);
Johannes Bergb4f286a12013-03-26 14:13:58 +01002861
Jiri Bencf0706e82007-05-05 11:45:53 -07002862}
2863
2864
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002865static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002866 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002867{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002868 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002869 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002870 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2871 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002872 struct ieee802_11_elems elems;
2873
Kalle Valo15b7b062009-03-22 21:57:14 +02002874 ifmgd = &sdata->u.mgd;
2875
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002876 sdata_assert_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002877
Joe Perchesb203ca32012-05-08 18:56:52 +00002878 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002879 return; /* ignore ProbeResp to foreign address */
2880
Ester Kummerae6a44e2008-06-27 18:54:48 +03002881 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2882 if (baselen > len)
2883 return;
2884
2885 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002886 false, &elems);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002887
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002888 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002889
Johannes Berg77fdaa12009-07-07 03:45:17 +02002890 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002891 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002892 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002893
2894 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002895 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002896 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002897 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002898 ifmgd->auth_data->tries = 0;
2899 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002900 ifmgd->auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002901 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01002902 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002903}
2904
Johannes Bergd91f36d2009-04-16 13:17:26 +02002905/*
2906 * This is the canonical list of information elements we care about,
2907 * the filter code also gives us all changes to the Microsoft OUI
2908 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2909 *
2910 * We implement beacon filtering in software since that means we can
2911 * avoid processing the frame here and in cfg80211, and userspace
2912 * will not be able to tell whether the hardware supports it or not.
2913 *
2914 * XXX: This list needs to be dynamic -- userspace needs to be able to
2915 * add items it requires. It also needs to be able to tell us to
2916 * look out for other vendor IEs.
2917 */
2918static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002919 (1ULL << WLAN_EID_COUNTRY) |
2920 (1ULL << WLAN_EID_ERP_INFO) |
2921 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2922 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2923 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002924 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002925
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002926static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2927 struct ieee80211_mgmt *mgmt, size_t len,
2928 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002929{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002930 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002931 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002932 size_t baselen;
2933 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002934 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002935 struct ieee80211_chanctx_conf *chanctx_conf;
2936 struct ieee80211_channel *chan;
Johannes Bergbee7f582013-02-07 22:24:55 +01002937 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002938 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002939 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002940 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002941 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002942 u8 *bssid;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002943 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02002944
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002945 sdata_assert_lock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07002946
Ester Kummerae6a44e2008-06-27 18:54:48 +03002947 /* Process beacon from the current BSS */
2948 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2949 if (baselen > len)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002950 return;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002951
Johannes Berg55de9082012-07-26 17:24:39 +02002952 rcu_read_lock();
2953 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2954 if (!chanctx_conf) {
2955 rcu_read_unlock();
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002956 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002957 }
2958
Johannes Berg4bf88532012-11-09 11:39:59 +01002959 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002960 rcu_read_unlock();
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002961 return;
Johannes Berg55de9082012-07-26 17:24:39 +02002962 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002963 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002964 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002965
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002966 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002967 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002968 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002969 len - baselen, false, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002970
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002971 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Alexander Bondar482a9c72013-06-03 17:29:33 +03002972 if (elems.tim && !elems.parse_error) {
2973 const struct ieee80211_tim_ie *tim_ie = elems.tim;
2974 ifmgd->dtim_period = tim_ie->dtim_period;
2975 }
Alexander Bondar989c6502013-05-16 17:34:17 +03002976 ifmgd->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002977 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002978 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2979 sdata->vif.bss_conf.sync_tsf =
2980 le64_to_cpu(mgmt->u.beacon.timestamp);
2981 sdata->vif.bss_conf.sync_device_ts =
2982 rx_status->device_timestamp;
2983 if (elems.tim)
2984 sdata->vif.bss_conf.sync_dtim_count =
2985 elems.tim->dtim_count;
2986 else
2987 sdata->vif.bss_conf.sync_dtim_count = 0;
2988 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002989 /* continue assoc process */
2990 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002991 ifmgd->assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002992 run_again(sdata, ifmgd->assoc_data->timeout);
2993 return;
Johannes Berg66e67e42012-01-20 13:55:27 +01002994 }
2995
2996 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002997 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02002998 return;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002999 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003000
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003001 /* Track average RSSI from the Beacon frames of the current AP */
3002 ifmgd->last_beacon_signal = rx_status->signal;
3003 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
3004 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03003005 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003006 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03003007 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003008 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003009 } else {
3010 ifmgd->ave_beacon_signal =
3011 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
3012 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
3013 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03003014 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003015 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003016
3017 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
3018 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3019 int sig = ifmgd->ave_beacon_signal;
3020 int last_sig = ifmgd->last_ave_beacon_signal;
3021
3022 /*
3023 * if signal crosses either of the boundaries, invoke callback
3024 * with appropriate parameters
3025 */
3026 if (sig > ifmgd->rssi_max_thold &&
3027 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3028 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02003029 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003030 } else if (sig < ifmgd->rssi_min_thold &&
3031 (last_sig >= ifmgd->rssi_max_thold ||
3032 last_sig == 0)) {
3033 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02003034 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07003035 }
3036 }
3037
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003038 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03003039 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01003040 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07003041 int sig = ifmgd->ave_beacon_signal / 16;
3042 int last_event = ifmgd->last_cqm_event_signal;
3043 int thold = bss_conf->cqm_rssi_thold;
3044 int hyst = bss_conf->cqm_rssi_hyst;
3045 if (sig < thold &&
3046 (last_event == 0 || sig < last_event - hyst)) {
3047 ifmgd->last_cqm_event_signal = sig;
3048 ieee80211_cqm_rssi_notify(
3049 &sdata->vif,
3050 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
3051 GFP_KERNEL);
3052 } else if (sig > thold &&
3053 (last_event == 0 || sig > last_event + hyst)) {
3054 ifmgd->last_cqm_event_signal = sig;
3055 ieee80211_cqm_rssi_notify(
3056 &sdata->vif,
3057 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
3058 GFP_KERNEL);
3059 }
3060 }
3061
Johannes Bergb291ba12009-07-10 15:29:03 +02003062 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003063 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01003064 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003065 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02003066 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003067 ieee80211_run_deferred_scan(local);
3068 mutex_unlock(&local->mtx);
3069
Johannes Berg4e751842009-06-10 15:16:52 +02003070 mutex_lock(&local->iflist_mtx);
3071 ieee80211_recalc_ps(local, -1);
3072 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03003073 }
3074
Johannes Bergb291ba12009-07-10 15:29:03 +02003075 /*
3076 * Push the beacon loss detection into the future since
3077 * we are processing a beacon from the AP just now.
3078 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003079 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003080
Johannes Bergd91f36d2009-04-16 13:17:26 +02003081 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3082 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003083 len - baselen, false, &elems,
Johannes Bergd91f36d2009-04-16 13:17:26 +02003084 care_about_ies, ncrc);
3085
Vivek Natarajan25c9c872009-03-02 20:20:30 +05303086 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02003087 bool directed_tim = ieee80211_check_tim(elems.tim,
3088 elems.tim_len,
3089 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02003090 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02003091 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01003092 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3093 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3094 ieee80211_hw_config(local,
3095 IEEE80211_CONF_CHANGE_PS);
3096 }
Kalle Valo572e0012009-02-10 17:09:31 +02003097 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05303098 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02003099 local->pspolling = true;
3100
3101 /*
3102 * Here is assumed that the driver will be
3103 * able to send ps-poll frame and receive a
3104 * response even though power save mode is
3105 * enabled, but some drivers might require
3106 * to disable power save here. This needs
3107 * to be investigated.
3108 */
3109 ieee80211_send_pspoll(local, sdata);
3110 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08003111 }
3112 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003113
Johannes Berg488dd7b2012-10-29 20:08:01 +01003114 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003115 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01003116 int ret;
3117
3118 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3119 len - baselen,
3120 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01003121 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003122 if (ret >= 2) {
3123 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3124 /* valid noa_attr and index changed */
3125 sdata->u.mgd.p2p_noa_index = noa.index;
3126 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3127 changed |= BSS_CHANGED_P2P_PS;
3128 /*
3129 * make sure we update all information, the CRC
3130 * mechanism doesn't look at P2P attributes.
3131 */
3132 ifmgd->beacon_crc_valid = false;
3133 }
3134 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3135 /* noa_attr not found and we had valid noa_attr before */
3136 sdata->u.mgd.p2p_noa_index = -1;
3137 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01003138 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01003139 ifmgd->beacon_crc_valid = false;
3140 }
3141 }
3142
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003143 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003144 return;
Jouni Malinen30196672009-05-19 17:01:43 +03003145 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003146 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03003147
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02003148 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02003149
3150 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3151 elems.wmm_param_len))
3152 changed |= BSS_CHANGED_QOS;
3153
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003154 /*
3155 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01003156 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003157 */
Alexander Bondar989c6502013-05-16 17:34:17 +03003158 if (!ifmgd->have_beacon) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003159 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3160 if (elems.tim)
3161 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3162 else
3163 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003164
3165 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3166 sdata->vif.bss_conf.sync_tsf =
3167 le64_to_cpu(mgmt->u.beacon.timestamp);
3168 sdata->vif.bss_conf.sync_device_ts =
3169 rx_status->device_timestamp;
3170 if (elems.tim)
3171 sdata->vif.bss_conf.sync_dtim_count =
3172 elems.tim->dtim_count;
3173 else
3174 sdata->vif.bss_conf.sync_dtim_count = 0;
3175 }
3176
Alexander Bondar989c6502013-05-16 17:34:17 +03003177 changed |= BSS_CHANGED_BEACON_INFO;
3178 ifmgd->have_beacon = true;
Alexander Bondar482a9c72013-06-03 17:29:33 +03003179
3180 mutex_lock(&local->iflist_mtx);
3181 ieee80211_recalc_ps(local, -1);
3182 mutex_unlock(&local->iflist_mtx);
3183
Alexander Bondarce857882013-05-06 17:17:04 +03003184 ieee80211_recalc_ps_vif(sdata);
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003185 }
3186
Johannes Berg1946bed2013-03-27 14:31:53 +01003187 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003188 erp_valid = true;
3189 erp_value = elems.erp_info[0];
3190 } else {
3191 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003192 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003193 changed |= ieee80211_handle_bss_capability(sdata,
3194 le16_to_cpu(mgmt->u.beacon.capab_info),
3195 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003196
Johannes Berg11289582013-02-07 17:36:12 +01003197 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f582013-02-07 22:24:55 +01003198 sta = sta_info_get(sdata, bssid);
3199
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003200 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3201 elems.vht_operation, bssid, &changed)) {
3202 mutex_unlock(&local->sta_mtx);
3203 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3204 WLAN_REASON_DEAUTH_LEAVING,
3205 true, deauth_buf);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003206 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
3207 sizeof(deauth_buf));
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003208 return;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003209 }
Johannes Bergbee7f582013-02-07 22:24:55 +01003210
3211 if (sta && elems.opmode_notif)
3212 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3213 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003214 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003215
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003216 if (elems.country_elem && elems.pwr_constr_elem &&
3217 mgmt->u.probe_resp.capab_info &
3218 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003219 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3220 elems.country_elem,
3221 elems.country_elem_len,
3222 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003223
Johannes Berg471b3ef2007-12-28 14:32:58 +01003224 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003225}
3226
Johannes Berg1fa57d02010-06-10 10:21:32 +02003227void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3228 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003229{
3230 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003231 struct ieee80211_mgmt *mgmt;
3232 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003233 struct ieee802_11_elems elems;
3234 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07003235
Jiri Bencf0706e82007-05-05 11:45:53 -07003236 rx_status = (struct ieee80211_rx_status *) skb->cb;
3237 mgmt = (struct ieee80211_mgmt *) skb->data;
3238 fc = le16_to_cpu(mgmt->frame_control);
3239
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003240 sdata_lock(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003241
Johannes Berg66e67e42012-01-20 13:55:27 +01003242 switch (fc & IEEE80211_FCTL_STYPE) {
3243 case IEEE80211_STYPE_BEACON:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003244 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003245 break;
3246 case IEEE80211_STYPE_PROBE_RESP:
3247 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3248 break;
3249 case IEEE80211_STYPE_AUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003250 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003251 break;
3252 case IEEE80211_STYPE_DEAUTH:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003253 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003254 break;
3255 case IEEE80211_STYPE_DISASSOC:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003256 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003257 break;
3258 case IEEE80211_STYPE_ASSOC_RESP:
3259 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003260 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003261 break;
3262 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01003263 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003264 ies_len = skb->len -
3265 offsetof(struct ieee80211_mgmt,
3266 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01003267
3268 if (ies_len < 0)
3269 break;
3270
3271 ieee802_11_parse_elems(
3272 mgmt->u.action.u.chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003273 ies_len, true, &elems);
Johannes Berg37799e52013-03-26 14:02:26 +01003274
3275 if (elems.parse_error)
3276 break;
3277
Johannes Berg66e67e42012-01-20 13:55:27 +01003278 ieee80211_sta_process_chanswitch(sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01003279 rx_status->mactime,
Johannes Berg04a161f2013-05-03 09:35:35 +02003280 &elems, false);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003281 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3282 ies_len = skb->len -
3283 offsetof(struct ieee80211_mgmt,
3284 u.action.u.ext_chan_switch.variable);
3285
3286 if (ies_len < 0)
3287 break;
3288
3289 ieee802_11_parse_elems(
3290 mgmt->u.action.u.ext_chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003291 ies_len, true, &elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003292
3293 if (elems.parse_error)
3294 break;
3295
3296 /* for the handling code pretend this was also an IE */
3297 elems.ext_chansw_ie =
3298 &mgmt->u.action.u.ext_chan_switch.data;
3299
3300 ieee80211_sta_process_chanswitch(sdata,
3301 rx_status->mactime,
Johannes Berg04a161f2013-05-03 09:35:35 +02003302 &elems, false);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003303 }
Johannes Berg37799e52013-03-26 14:02:26 +01003304 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003305 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003306 sdata_unlock(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003307}
3308
Johannes Berg9c6bd792008-09-11 00:01:52 +02003309static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003310{
3311 struct ieee80211_sub_if_data *sdata =
3312 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003313
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003314 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003315}
3316
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003317static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003318 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003319{
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003320 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003321
Johannes Berg37ad3882012-02-24 13:50:54 +01003322 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003323 tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01003324
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003325 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
3326 IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003327}
3328
Johannes Berg66e67e42012-01-20 13:55:27 +01003329static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3330{
3331 struct ieee80211_local *local = sdata->local;
3332 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3333 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003334 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003335
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003336 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003337
3338 if (WARN_ON_ONCE(!auth_data))
3339 return -EINVAL;
3340
Johannes Berg66e67e42012-01-20 13:55:27 +01003341 auth_data->tries++;
3342
3343 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003344 sdata_info(sdata, "authentication with %pM timed out\n",
3345 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003346
3347 /*
3348 * Most likely AP is not in the range so remove the
3349 * bss struct for that AP.
3350 */
3351 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3352
3353 return -ETIMEDOUT;
3354 }
3355
Johannes Berga1845fc2012-06-27 13:18:36 +02003356 drv_mgd_prepare_tx(local, sdata);
3357
Johannes Berg66e67e42012-01-20 13:55:27 +01003358 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003359 u16 trans = 1;
3360 u16 status = 0;
3361
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003362 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3363 auth_data->bss->bssid, auth_data->tries,
3364 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003365
3366 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003367
3368 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3369 trans = auth_data->sae_trans;
3370 status = auth_data->sae_status;
3371 auth_data->expected_transaction = trans;
3372 }
3373
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003374 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3375 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3376 IEEE80211_TX_INTFL_MLME_CONN_TX;
3377
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003378 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3379 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003380 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003381 auth_data->bss->bssid, NULL, 0, 0,
3382 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003383 } else {
3384 const u8 *ssidie;
3385
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003386 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3387 auth_data->bss->bssid, auth_data->tries,
3388 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003389
Johannes Berg9caf0362012-11-29 01:25:20 +01003390 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003391 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003392 if (!ssidie) {
3393 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003394 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003395 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003396 /*
3397 * Direct probe is sent to broadcast address as some APs
3398 * will not answer to direct packet in unassociated state.
3399 */
3400 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003401 NULL, 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02003402 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003403 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003404 }
3405
Stanislaw Gruszka6211dd12013-05-17 13:43:04 +02003406 if (tx_flags == 0) {
Johannes Berg1672c0e32013-01-29 15:02:27 +01003407 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Bergcb236d22013-07-29 23:07:43 +02003408 auth_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003409 run_again(sdata, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003410 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02003411 auth_data->timeout =
3412 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
3413 auth_data->timeout_started = true;
3414 run_again(sdata, auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003415 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003416
3417 return 0;
3418}
3419
3420static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3421{
3422 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3423 struct ieee80211_local *local = sdata->local;
3424
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003425 sdata_assert_lock(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01003426
Johannes Berg66e67e42012-01-20 13:55:27 +01003427 assoc_data->tries++;
3428 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003429 sdata_info(sdata, "association with %pM timed out\n",
3430 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003431
3432 /*
3433 * Most likely AP is not in the range so remove the
3434 * bss struct for that AP.
3435 */
3436 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3437
3438 return -ETIMEDOUT;
3439 }
3440
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003441 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3442 assoc_data->bss->bssid, assoc_data->tries,
3443 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003444 ieee80211_send_assoc(sdata);
3445
Johannes Berg1672c0e32013-01-29 15:02:27 +01003446 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3447 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003448 assoc_data->timeout_started = true;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003449 run_again(sdata, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003450 } else {
Johannes Bergcb236d22013-07-29 23:07:43 +02003451 assoc_data->timeout =
3452 round_jiffies_up(jiffies +
3453 IEEE80211_ASSOC_TIMEOUT_LONG);
3454 assoc_data->timeout_started = true;
3455 run_again(sdata, assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003456 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003457
3458 return 0;
3459}
3460
Johannes Berg1672c0e32013-01-29 15:02:27 +01003461void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3462 __le16 fc, bool acked)
3463{
3464 struct ieee80211_local *local = sdata->local;
3465
3466 sdata->u.mgd.status_fc = fc;
3467 sdata->u.mgd.status_acked = acked;
3468 sdata->u.mgd.status_received = true;
3469
3470 ieee80211_queue_work(&local->hw, &sdata->work);
3471}
3472
Johannes Berg1fa57d02010-06-10 10:21:32 +02003473void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b392008-09-08 17:44:22 +02003474{
Johannes Berg60f8b392008-09-08 17:44:22 +02003475 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003476 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b392008-09-08 17:44:22 +02003477
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003478 sdata_lock(sdata);
Johannes Berg60f8b392008-09-08 17:44:22 +02003479
Johannes Berg1672c0e32013-01-29 15:02:27 +01003480 if (ifmgd->status_received) {
3481 __le16 fc = ifmgd->status_fc;
3482 bool status_acked = ifmgd->status_acked;
3483
3484 ifmgd->status_received = false;
3485 if (ifmgd->auth_data &&
3486 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3487 if (status_acked) {
3488 ifmgd->auth_data->timeout =
3489 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003490 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003491 } else {
3492 ifmgd->auth_data->timeout = jiffies - 1;
3493 }
Johannes Berg89afe612013-02-13 15:39:57 +01003494 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003495 } else if (ifmgd->assoc_data &&
3496 (ieee80211_is_assoc_req(fc) ||
3497 ieee80211_is_reassoc_req(fc))) {
3498 if (status_acked) {
3499 ifmgd->assoc_data->timeout =
3500 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003501 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg1672c0e32013-01-29 15:02:27 +01003502 } else {
3503 ifmgd->assoc_data->timeout = jiffies - 1;
3504 }
Johannes Berg89afe612013-02-13 15:39:57 +01003505 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003506 }
3507 }
3508
Johannes Berg89afe612013-02-13 15:39:57 +01003509 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003510 time_after(jiffies, ifmgd->auth_data->timeout)) {
3511 if (ifmgd->auth_data->done) {
3512 /*
3513 * ok ... we waited for assoc but userspace didn't,
3514 * so let's just kill the auth data
3515 */
3516 ieee80211_destroy_auth_data(sdata, false);
3517 } else if (ieee80211_probe_auth(sdata)) {
3518 u8 bssid[ETH_ALEN];
3519
3520 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3521
3522 ieee80211_destroy_auth_data(sdata, false);
3523
Johannes Berg6ff57cf2013-05-16 00:55:00 +02003524 cfg80211_auth_timeout(sdata->dev, bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003525 }
Johannes Berg89afe612013-02-13 15:39:57 +01003526 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003527 run_again(sdata, ifmgd->auth_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01003528
Johannes Berg89afe612013-02-13 15:39:57 +01003529 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003530 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Alexander Bondar989c6502013-05-16 17:34:17 +03003531 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003532 ieee80211_do_assoc(sdata)) {
Johannes Berg959867f2013-06-19 13:05:42 +02003533 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berg66e67e42012-01-20 13:55:27 +01003534
3535 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02003536 cfg80211_assoc_timeout(sdata->dev, bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01003537 }
Johannes Berg89afe612013-02-13 15:39:57 +01003538 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003539 run_again(sdata, ifmgd->assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01003540
Johannes Bergb291ba12009-07-10 15:29:03 +02003541 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3542 IEEE80211_STA_CONNECTION_POLL) &&
3543 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003544 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003545 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003546
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003547 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003548
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003549 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003550 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003551 else
Ben Greear180205b2011-02-04 15:30:24 -08003552 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003553
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003554 /* ACK received for nullfunc probing frame */
3555 if (!ifmgd->probe_send_count)
3556 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003557 else if (ifmgd->nullfunc_failed) {
3558 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003559 mlme_dbg(sdata,
3560 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3561 bssid, ifmgd->probe_send_count,
3562 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003563 ieee80211_mgd_probe_ap_send(sdata);
3564 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003565 mlme_dbg(sdata,
3566 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3567 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003568 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003569 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3570 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003571 }
3572 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003573 run_again(sdata, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003574 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003575 mlme_dbg(sdata,
3576 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3577 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003578 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003579 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003580 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003581 mlme_dbg(sdata,
3582 "No probe response from AP %pM after %dms, try %d/%i\n",
3583 bssid, probe_wait_ms,
3584 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003585 ieee80211_mgd_probe_ap_send(sdata);
3586 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003587 /*
3588 * We actually lost the connection ... or did we?
3589 * Let's make sure!
3590 */
Ben Greearb38afa82010-10-07 16:12:06 -07003591 wiphy_debug(local->hw.wiphy,
3592 "%s: No probe response from AP %pM"
3593 " after %dms, disconnecting.\n",
3594 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003595 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003596
Johannes Berg95acac62011-07-12 12:30:59 +02003597 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003598 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003599 }
3600 }
3601
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003602 sdata_unlock(sdata);
Johannes Berg60f8b392008-09-08 17:44:22 +02003603}
Johannes Berg0a51b272008-09-08 17:44:25 +02003604
Johannes Bergb291ba12009-07-10 15:29:03 +02003605static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3606{
3607 struct ieee80211_sub_if_data *sdata =
3608 (struct ieee80211_sub_if_data *) data;
3609 struct ieee80211_local *local = sdata->local;
3610
3611 if (local->quiescing)
3612 return;
3613
Johannes Berg682bd382013-01-29 13:13:50 +01003614 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003615 ieee80211_queue_work(&sdata->local->hw,
3616 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003617}
3618
3619static void ieee80211_sta_conn_mon_timer(unsigned long data)
3620{
3621 struct ieee80211_sub_if_data *sdata =
3622 (struct ieee80211_sub_if_data *) data;
3623 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3624 struct ieee80211_local *local = sdata->local;
3625
3626 if (local->quiescing)
3627 return;
3628
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003629 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003630}
3631
3632static void ieee80211_sta_monitor_work(struct work_struct *work)
3633{
3634 struct ieee80211_sub_if_data *sdata =
3635 container_of(work, struct ieee80211_sub_if_data,
3636 u.mgd.monitor_work);
3637
3638 ieee80211_mgd_probe_ap(sdata, false);
3639}
3640
Johannes Berga1678f82008-09-11 00:01:47 +02003641static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3642{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003643 u32 flags;
3644
Kalle Vaload935682009-04-19 08:47:19 +03003645 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003646 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003647
Johannes Bergb291ba12009-07-10 15:29:03 +02003648 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003649 flags = sdata->local->hw.flags;
3650 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3651 ieee80211_queue_work(&sdata->local->hw,
3652 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003653 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003654 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003655 }
Johannes Berga1678f82008-09-11 00:01:47 +02003656}
3657
Johannes Bergb8360ab2013-04-29 14:57:44 +02003658#ifdef CONFIG_PM
3659void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
3660{
3661 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3662
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003663 sdata_lock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003664 if (!ifmgd->associated) {
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003665 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003666 return;
3667 }
3668
3669 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
3670 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
3671 mlme_dbg(sdata, "driver requested disconnect after resume\n");
3672 ieee80211_sta_connection_lost(sdata,
3673 ifmgd->associated->bssid,
3674 WLAN_REASON_UNSPECIFIED,
3675 true);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003676 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003677 return;
3678 }
Johannes Berg8d61ffa2013-05-10 12:32:47 +02003679 sdata_unlock(sdata);
Johannes Bergb8360ab2013-04-29 14:57:44 +02003680}
3681#endif
3682
Johannes Berg9c6bd792008-09-11 00:01:52 +02003683/* interface setup */
3684void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3685{
Johannes Berg46900292009-02-15 12:44:28 +01003686 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003687
Johannes Berg46900292009-02-15 12:44:28 +01003688 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003689 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003690 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003691 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3692 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003693 INIT_WORK(&ifmgd->csa_connection_drop_work,
3694 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003695 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003696 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003697 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003698 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3699 (unsigned long) sdata);
3700 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3701 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003702 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303703 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003704
Johannes Bergab1faea2009-07-01 21:41:17 +02003705 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303706 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003707 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3708 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003709 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003710
Johannes Berg0f782312009-12-01 13:37:02 +01003711 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3712 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3713 else
3714 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003715}
3716
3717/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003718void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3719{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003720 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003721
3722 /* Restart STA timers */
3723 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07003724 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3725 if (ieee80211_sdata_running(sdata))
3726 ieee80211_restart_sta_timer(sdata);
3727 }
Johannes Berga1678f82008-09-11 00:01:47 +02003728 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003729}
Johannes Berg10f644a2009-04-16 13:17:25 +02003730
3731int ieee80211_max_network_latency(struct notifier_block *nb,
3732 unsigned long data, void *dummy)
3733{
3734 s32 latency_usec = (s32) data;
3735 struct ieee80211_local *local =
3736 container_of(nb, struct ieee80211_local,
3737 network_latency_notifier);
3738
3739 mutex_lock(&local->iflist_mtx);
3740 ieee80211_recalc_ps(local, latency_usec);
3741 mutex_unlock(&local->iflist_mtx);
3742
3743 return 0;
3744}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003745
Johannes Bergf2d9d272012-11-22 14:11:39 +01003746static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3747 struct cfg80211_bss *cbss)
3748{
3749 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3750 const u8 *ht_cap_ie, *vht_cap_ie;
3751 const struct ieee80211_ht_cap *ht_cap;
3752 const struct ieee80211_vht_cap *vht_cap;
3753 u8 chains = 1;
3754
3755 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3756 return chains;
3757
Johannes Berg9caf0362012-11-29 01:25:20 +01003758 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003759 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3760 ht_cap = (void *)(ht_cap_ie + 2);
3761 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3762 /*
3763 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3764 * "Tx Unequal Modulation Supported" fields.
3765 */
3766 }
3767
3768 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3769 return chains;
3770
Johannes Berg9caf0362012-11-29 01:25:20 +01003771 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003772 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3773 u8 nss;
3774 u16 tx_mcs_map;
3775
3776 vht_cap = (void *)(vht_cap_ie + 2);
3777 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3778 for (nss = 8; nss > 0; nss--) {
3779 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3780 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3781 break;
3782 }
3783 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3784 chains = max(chains, nss);
3785 }
3786
3787 return chains;
3788}
3789
Johannes Bergb17166a2012-07-27 11:41:27 +02003790static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3791 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003792{
3793 struct ieee80211_local *local = sdata->local;
3794 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003795 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003796 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003797 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003798 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003799 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003800
Johannes Berg24398e32012-03-28 10:58:36 +02003801 sband = local->hw.wiphy->bands[cbss->channel->band];
3802
Johannes Bergf2d9d272012-11-22 14:11:39 +01003803 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3804 IEEE80211_STA_DISABLE_80P80MHZ |
3805 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003806
Johannes Berg9caf0362012-11-29 01:25:20 +01003807 rcu_read_lock();
3808
Johannes Bergf2d9d272012-11-22 14:11:39 +01003809 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3810 sband->ht_cap.ht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003811 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003812
Johannes Berg9caf0362012-11-29 01:25:20 +01003813 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003814 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3815 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6eff2013-02-07 23:33:32 +01003816
3817 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3818 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3819 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3820 ht_oper = NULL;
3821 }
Johannes Berg24398e32012-03-28 10:58:36 +02003822 }
3823
Johannes Bergf2d9d272012-11-22 14:11:39 +01003824 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3825 sband->vht_cap.vht_supported) {
Johannes Berg08e6eff2013-02-07 23:33:32 +01003826 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003827
Johannes Berg9caf0362012-11-29 01:25:20 +01003828 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3829 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003830 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3831 vht_oper = (void *)(vht_oper_ie + 2);
3832 if (vht_oper && !ht_oper) {
3833 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003834 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003835 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003836 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3837 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003838 }
Johannes Berg08e6eff2013-02-07 23:33:32 +01003839
3840 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3841 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3842 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3843 vht_oper = NULL;
3844 }
Johannes Berg24398e32012-03-28 10:58:36 +02003845 }
3846
Johannes Bergf2d9d272012-11-22 14:11:39 +01003847 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3848 cbss->channel,
3849 ht_oper, vht_oper,
Johannes Berg5cdaed12013-07-31 11:23:06 +02003850 &chandef, false);
Johannes Berg04ecd252012-09-11 14:34:12 +02003851
Johannes Bergf2d9d272012-11-22 14:11:39 +01003852 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3853 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003854
Johannes Berg9caf0362012-11-29 01:25:20 +01003855 rcu_read_unlock();
3856
Johannes Berg04ecd252012-09-11 14:34:12 +02003857 /* will change later if needed */
3858 sdata->smps_mode = IEEE80211_SMPS_OFF;
3859
Johannes Bergf2d9d272012-11-22 14:11:39 +01003860 /*
3861 * If this fails (possibly due to channel context sharing
3862 * on incompatible channels, e.g. 80+80 and 160 sharing the
3863 * same control channel) try to use a smaller bandwidth.
3864 */
3865 ret = ieee80211_vif_use_channel(sdata, &chandef,
3866 IEEE80211_CHANCTX_SHARED);
Simon Wunderlich0418a442013-05-16 13:00:31 +02003867
3868 /* don't downgrade for 5 and 10 MHz channels, though. */
3869 if (chandef.width == NL80211_CHAN_WIDTH_5 ||
3870 chandef.width == NL80211_CHAN_WIDTH_10)
3871 return ret;
3872
Johannes Bergd601cd82013-02-07 20:54:51 +01003873 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003874 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003875 ret = ieee80211_vif_use_channel(sdata, &chandef,
3876 IEEE80211_CHANCTX_SHARED);
3877 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003878 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003879}
3880
3881static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3882 struct cfg80211_bss *cbss, bool assoc)
3883{
3884 struct ieee80211_local *local = sdata->local;
3885 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3886 struct ieee80211_bss *bss = (void *)cbss->priv;
3887 struct sta_info *new_sta = NULL;
3888 bool have_sta = false;
3889 int err;
3890
3891 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3892 return -EINVAL;
3893
3894 if (assoc) {
3895 rcu_read_lock();
3896 have_sta = sta_info_get(sdata, cbss->bssid);
3897 rcu_read_unlock();
3898 }
3899
3900 if (!have_sta) {
3901 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3902 if (!new_sta)
3903 return -ENOMEM;
3904 }
3905
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003906 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003907 u32 rates = 0, basic_rates = 0;
3908 bool have_higher_than_11mbit;
3909 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003910 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003911 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003912
3913 sband = local->hw.wiphy->bands[cbss->channel->band];
3914
3915 err = ieee80211_prep_channel(sdata, cbss);
3916 if (err) {
3917 sta_info_free(local, new_sta);
3918 return err;
3919 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003920
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003921 ieee80211_get_rates(sband, bss->supp_rates,
3922 bss->supp_rates_len,
3923 &rates, &basic_rates,
3924 &have_higher_than_11mbit,
3925 &min_rate, &min_rate_index);
3926
3927 /*
3928 * This used to be a workaround for basic rates missing
3929 * in the association response frame. Now that we no
3930 * longer use the basic rates from there, it probably
3931 * doesn't happen any more, but keep the workaround so
3932 * in case some *other* APs are buggy in different ways
3933 * we can connect -- with a warning.
3934 */
3935 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003936 sdata_info(sdata,
3937 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003938 basic_rates = BIT(min_rate_index);
3939 }
3940
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003941 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003942 sdata->vif.bss_conf.basic_rates = basic_rates;
3943
3944 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003945 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003946 have_higher_than_11mbit)
3947 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3948 else
3949 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3950
3951 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3952
Johannes Berg8c358bc2012-05-22 22:13:05 +02003953 /* set timing information */
3954 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003955 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003956 ies = rcu_dereference(cbss->beacon_ies);
3957 if (ies) {
3958 const u8 *tim_ie;
3959
3960 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3961 sdata->vif.bss_conf.sync_device_ts =
3962 bss->device_ts_beacon;
3963 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3964 ies->data, ies->len);
3965 if (tim_ie && tim_ie[1] >= 2)
3966 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3967 else
3968 sdata->vif.bss_conf.sync_dtim_count = 0;
3969 } else if (!(local->hw.flags &
3970 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3971 ies = rcu_dereference(cbss->proberesp_ies);
3972 /* must be non-NULL since beacon IEs were NULL */
3973 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3974 sdata->vif.bss_conf.sync_device_ts =
3975 bss->device_ts_presp;
3976 sdata->vif.bss_conf.sync_dtim_count = 0;
3977 } else {
3978 sdata->vif.bss_conf.sync_tsf = 0;
3979 sdata->vif.bss_conf.sync_device_ts = 0;
3980 sdata->vif.bss_conf.sync_dtim_count = 0;
3981 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003982 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003983
3984 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003985 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003986 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3987 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003988
3989 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003990 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003991
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003992 err = sta_info_insert(new_sta);
3993 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003994 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003995 sdata_info(sdata,
3996 "failed to insert STA entry for the AP (error %d)\n",
3997 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003998 return err;
3999 }
4000 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00004001 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01004002
4003 return 0;
4004}
4005
Johannes Berg77fdaa12009-07-07 03:45:17 +02004006/* config hooks */
4007int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4008 struct cfg80211_auth_request *req)
4009{
Johannes Berg66e67e42012-01-20 13:55:27 +01004010 struct ieee80211_local *local = sdata->local;
4011 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4012 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004013 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01004014 int err;
4015
4016 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02004017
4018 switch (req->auth_type) {
4019 case NL80211_AUTHTYPE_OPEN_SYSTEM:
4020 auth_alg = WLAN_AUTH_OPEN;
4021 break;
4022 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01004023 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02004024 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004025 auth_alg = WLAN_AUTH_SHARED_KEY;
4026 break;
4027 case NL80211_AUTHTYPE_FT:
4028 auth_alg = WLAN_AUTH_FT;
4029 break;
4030 case NL80211_AUTHTYPE_NETWORK_EAP:
4031 auth_alg = WLAN_AUTH_LEAP;
4032 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004033 case NL80211_AUTHTYPE_SAE:
4034 auth_alg = WLAN_AUTH_SAE;
4035 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004036 default:
4037 return -EOPNOTSUPP;
4038 }
4039
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004040 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
4041 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01004042 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004043 return -ENOMEM;
4044
Johannes Berg66e67e42012-01-20 13:55:27 +01004045 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004046
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004047 if (req->sae_data_len >= 4) {
4048 __le16 *pos = (__le16 *) req->sae_data;
4049 auth_data->sae_trans = le16_to_cpu(pos[0]);
4050 auth_data->sae_status = le16_to_cpu(pos[1]);
4051 memcpy(auth_data->data, req->sae_data + 4,
4052 req->sae_data_len - 4);
4053 auth_data->data_len += req->sae_data_len - 4;
4054 }
4055
Johannes Berg77fdaa12009-07-07 03:45:17 +02004056 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03004057 memcpy(&auth_data->data[auth_data->data_len],
4058 req->ie, req->ie_len);
4059 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004060 }
4061
Johannes Bergfffd0932009-07-08 14:22:54 +02004062 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004063 auth_data->key_len = req->key_len;
4064 auth_data->key_idx = req->key_idx;
4065 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02004066 }
4067
Johannes Berg66e67e42012-01-20 13:55:27 +01004068 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004069
Johannes Berg66e67e42012-01-20 13:55:27 +01004070 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01004071
Johannes Berg66e67e42012-01-20 13:55:27 +01004072 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4073 ifmgd->assoc_data) {
4074 err = -EBUSY;
4075 goto err_free;
4076 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004077
Johannes Berg66e67e42012-01-20 13:55:27 +01004078 if (ifmgd->auth_data)
4079 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03004080
Johannes Berg66e67e42012-01-20 13:55:27 +01004081 /* prep auth_data so we don't go into idle on disassoc */
4082 ifmgd->auth_data = auth_data;
4083
Johannes Berg7b119dc2013-04-10 21:38:36 +02004084 if (ifmgd->associated) {
4085 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4086
4087 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4088 WLAN_REASON_UNSPECIFIED,
4089 false, frame_buf);
4090
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004091 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4092 sizeof(frame_buf));
Johannes Berg7b119dc2013-04-10 21:38:36 +02004093 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004094
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004095 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004096
Johannes Berga1cf7752012-03-08 15:02:07 +01004097 err = ieee80211_prep_connection(sdata, req->bss, false);
4098 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01004099 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004100
Johannes Berg66e67e42012-01-20 13:55:27 +01004101 err = ieee80211_probe_auth(sdata);
4102 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004103 sta_info_destroy_addr(sdata, req->bss->bssid);
4104 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004105 }
4106
Johannes Berg66e67e42012-01-20 13:55:27 +01004107 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01004108 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004109 return 0;
Johannes Berge5b900d2010-07-29 16:08:55 +02004110
Johannes Berg66e67e42012-01-20 13:55:27 +01004111 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004112 memset(ifmgd->bssid, 0, ETH_ALEN);
4113 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004114 ifmgd->auth_data = NULL;
4115 err_free:
4116 kfree(auth_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01004117 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004118}
4119
Johannes Berg77fdaa12009-07-07 03:45:17 +02004120int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4121 struct cfg80211_assoc_request *req)
4122{
Johannes Berg66e67e42012-01-20 13:55:27 +01004123 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004124 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004125 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004126 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004127 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004128 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004129 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004130 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004131
Johannes Berg66e67e42012-01-20 13:55:27 +01004132 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4133 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004134 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004135
Johannes Berg9caf0362012-11-29 01:25:20 +01004136 rcu_read_lock();
4137 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4138 if (!ssidie) {
4139 rcu_read_unlock();
4140 kfree(assoc_data);
4141 return -EINVAL;
4142 }
4143 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4144 assoc_data->ssid_len = ssidie[1];
4145 rcu_read_unlock();
4146
Johannes Berg7b119dc2013-04-10 21:38:36 +02004147 if (ifmgd->associated) {
4148 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4149
4150 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4151 WLAN_REASON_UNSPECIFIED,
4152 false, frame_buf);
4153
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004154 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4155 sizeof(frame_buf));
Johannes Berg7b119dc2013-04-10 21:38:36 +02004156 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004157
4158 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4159 err = -EBUSY;
4160 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004161 }
4162
Johannes Berg66e67e42012-01-20 13:55:27 +01004163 if (ifmgd->assoc_data) {
4164 err = -EBUSY;
4165 goto err_free;
4166 }
4167
4168 if (ifmgd->auth_data) {
4169 bool match;
4170
4171 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004172 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004173 ieee80211_destroy_auth_data(sdata, match);
4174 }
4175
4176 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02004177
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004178 ifmgd->beacon_crc_valid = false;
4179
Johannes Bergde5036a2012-03-08 15:02:03 +01004180 /*
4181 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4182 * We still associate in non-HT mode (11a/b/g) if any one of these
4183 * ciphers is configured as pairwise.
4184 * We can set this to true for non-11n hardware, that'll be checked
4185 * separately along with the peer capabilities.
4186 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004187 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004188 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4189 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004190 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004191 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004192 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004193 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004194 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004195 }
4196 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004197
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004198 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004199 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004200 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4201 }
Ben Greearef96a8422011-11-18 11:32:00 -08004202
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004203 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4204 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4205
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004206 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4207 sband = local->hw.wiphy->bands[req->bss->channel->band];
4208 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004209 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004210 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004211 if (!bss->wmm_used)
4212 netdev_info(sdata->dev,
4213 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004214 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004215
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004216 /* disable VHT if we don't support it or the AP doesn't use WMM */
4217 if (!sband->vht_cap.vht_supported ||
4218 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4219 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004220 if (!bss->wmm_used)
4221 netdev_info(sdata->dev,
4222 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004223 }
4224
Ben Greearef96a8422011-11-18 11:32:00 -08004225 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4226 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4227 sizeof(ifmgd->ht_capa_mask));
4228
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004229 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4230 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4231 sizeof(ifmgd->vht_capa_mask));
4232
Johannes Berg77fdaa12009-07-07 03:45:17 +02004233 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004234 memcpy(assoc_data->ie, req->ie, req->ie_len);
4235 assoc_data->ie_len = req->ie_len;
4236 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004237
Johannes Berg66e67e42012-01-20 13:55:27 +01004238 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004239
Johannes Bergaf6b6372009-12-23 13:15:35 +01004240 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4241 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004242 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004243 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004244 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004245 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004246 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004247
Johannes Berg66e67e42012-01-20 13:55:27 +01004248 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004249 assoc_data->wmm = bss->wmm_used &&
4250 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004251 assoc_data->supp_rates = bss->supp_rates;
4252 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004253
Johannes Berg9caf0362012-11-29 01:25:20 +01004254 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004255 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4256 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4257 assoc_data->ap_ht_param =
4258 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4259 else
Johannes Berga8243b72012-11-22 14:32:09 +01004260 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004261 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4262 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4263 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4264 sizeof(struct ieee80211_vht_cap));
4265 else
4266 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004267 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004268
Kalle Valoab133152010-01-12 10:42:31 +02004269 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004270 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4271 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004272 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004273 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4274 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004275 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004276 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4277 }
4278
Johannes Berg77fdaa12009-07-07 03:45:17 +02004279 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004280 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004281
4282 if (req->use_mfp) {
4283 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4284 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4285 } else {
4286 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4287 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4288 }
4289
4290 if (req->crypto.control_port)
4291 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4292 else
4293 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4294
Johannes Berga621fa42010-08-27 14:26:54 +03004295 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4296 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4297
Johannes Berg66e67e42012-01-20 13:55:27 +01004298 /* kick off associate process */
4299
4300 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004301 ifmgd->dtim_period = 0;
Alexander Bondar989c6502013-05-16 17:34:17 +03004302 ifmgd->have_beacon = false;
Johannes Berg66e67e42012-01-20 13:55:27 +01004303
Johannes Berga1cf7752012-03-08 15:02:07 +01004304 err = ieee80211_prep_connection(sdata, req->bss, true);
4305 if (err)
4306 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004307
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004308 rcu_read_lock();
4309 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004310
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004311 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4312 !beacon_ies) {
4313 /*
4314 * Wait up to one beacon interval ...
4315 * should this be more if we miss one?
4316 */
4317 sdata_info(sdata, "waiting for beacon from %pM\n",
4318 ifmgd->bssid);
4319 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004320 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004321 assoc_data->need_beacon = true;
4322 } else if (beacon_ies) {
4323 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4324 beacon_ies->data,
4325 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004326 u8 dtim_count = 0;
4327
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004328 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4329 const struct ieee80211_tim_ie *tim;
4330 tim = (void *)(tim_ie + 2);
4331 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004332 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004333 }
Alexander Bondar989c6502013-05-16 17:34:17 +03004334 ifmgd->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004335 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004336 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004337
4338 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4339 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4340 sdata->vif.bss_conf.sync_device_ts =
4341 bss->device_ts_beacon;
4342 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4343 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004344 } else {
4345 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004346 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004347 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004348 rcu_read_unlock();
4349
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004350 run_again(sdata, assoc_data->timeout);
Johannes Berg66e67e42012-01-20 13:55:27 +01004351
Paul Stewartfcff4f12012-02-23 17:59:53 -08004352 if (bss->corrupt_data) {
4353 char *corrupt_type = "data";
4354 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4355 if (bss->corrupt_data &
4356 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4357 corrupt_type = "beacon and probe response";
4358 else
4359 corrupt_type = "beacon";
4360 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4361 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004362 sdata_info(sdata, "associating with AP with corrupt %s\n",
4363 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004364 }
4365
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004366 return 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004367 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004368 memset(ifmgd->bssid, 0, ETH_ALEN);
4369 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004370 ifmgd->assoc_data = NULL;
4371 err_free:
4372 kfree(assoc_data);
Johannes Berg66e67e42012-01-20 13:55:27 +01004373 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004374}
4375
4376int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004377 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004378{
4379 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004380 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004381 bool tx = !req->local_state_change;
Johannes Bergde3d43a2013-05-14 09:39:02 +02004382 bool report_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004383
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004384 sdata_info(sdata,
4385 "deauthenticating from %pM by local choice (reason=%d)\n",
4386 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004387
Johannes Berg86552012012-10-29 09:46:31 +01004388 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004389 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004390 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4391 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004392 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004393 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004394 ieee80211_destroy_auth_data(sdata, false);
Johannes Berg86552012012-10-29 09:46:31 +01004395
Johannes Bergde3d43a2013-05-14 09:39:02 +02004396 report_frame = true;
Johannes Berg86552012012-10-29 09:46:31 +01004397 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004398 }
4399
Johannes Berg86552012012-10-29 09:46:31 +01004400 if (ifmgd->associated &&
4401 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4402 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4403 req->reason_code, tx, frame_buf);
Johannes Bergde3d43a2013-05-14 09:39:02 +02004404 report_frame = true;
Johannes Berg86552012012-10-29 09:46:31 +01004405 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004406
Johannes Berg86552012012-10-29 09:46:31 +01004407 out:
Johannes Bergde3d43a2013-05-14 09:39:02 +02004408 if (report_frame)
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004409 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4410 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Berg86552012012-10-29 09:46:31 +01004411
Johannes Berg77fdaa12009-07-07 03:45:17 +02004412 return 0;
4413}
4414
4415int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004416 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004417{
4418 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004419 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004420 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004421
Johannes Berg8d8b2612009-07-25 11:58:36 +02004422 /*
4423 * cfg80211 should catch this ... but it's racy since
4424 * we can receive a disassoc frame, process it, hand it
4425 * to cfg80211 while that's in a locked section already
4426 * trying to tell us that the user wants to disconnect.
4427 */
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004428 if (ifmgd->associated != req->bss)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004429 return -ENOLINK;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004430
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004431 sdata_info(sdata,
4432 "disassociating from %pM by local choice (reason=%d)\n",
4433 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004434
Jouni Malinene69e95d2010-03-29 23:29:31 -07004435 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004436 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4437 req->reason_code, !req->local_state_change,
4438 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004439
Johannes Berg6ff57cf2013-05-16 00:55:00 +02004440 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
4441 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004442
Johannes Berg77fdaa12009-07-07 03:45:17 +02004443 return 0;
4444}
Jouni Malinen026331c2010-02-15 12:53:10 +02004445
Eliad Pellerafa762f2012-04-23 14:45:15 +03004446void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004447{
4448 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4449
Ben Greear49921852013-02-20 09:41:09 -08004450 /*
4451 * Make sure some work items will not run after this,
4452 * they will not do anything but might not have been
4453 * cancelled when disconnecting.
4454 */
4455 cancel_work_sync(&ifmgd->monitor_work);
4456 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4457 cancel_work_sync(&ifmgd->request_smps_work);
4458 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4459 cancel_work_sync(&ifmgd->chswitch_work);
4460
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004461 sdata_lock(sdata);
Johannes Berg959867f2013-06-19 13:05:42 +02004462 if (ifmgd->assoc_data) {
4463 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004464 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg959867f2013-06-19 13:05:42 +02004465 cfg80211_assoc_timeout(sdata->dev, bss);
4466 }
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004467 if (ifmgd->auth_data)
4468 ieee80211_destroy_auth_data(sdata, false);
4469 del_timer_sync(&ifmgd->timer);
Johannes Berg8d61ffa2013-05-10 12:32:47 +02004470 sdata_unlock(sdata);
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004471}
4472
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004473void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4474 enum nl80211_cqm_rssi_threshold_event rssi_event,
4475 gfp_t gfp)
4476{
4477 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4478
Johannes Bergb5878a22010-04-07 16:48:40 +02004479 trace_api_cqm_rssi_notify(sdata, rssi_event);
4480
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004481 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4482}
4483EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);