blob: 29620bfc7a69663c34ab89b5bd03875eaaa4c25c [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 Berg60f8b39c2008-09-08 17:44:22 +020027
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020029#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040030#include "rate.h"
31#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070032
Johannes Berg1672c0e32013-01-29 15:02:27 +010033#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
34#define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
35#define IEEE80211_AUTH_MAX_TRIES 3
36#define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
39#define IEEE80211_ASSOC_MAX_TRIES 3
Johannes Berg66e67e42012-01-20 13:55:27 +010040
Ben Greear180205b2011-02-04 15:30:24 -080041static int max_nullfunc_tries = 2;
42module_param(max_nullfunc_tries, int, 0644);
43MODULE_PARM_DESC(max_nullfunc_tries,
44 "Maximum nullfunc tx tries before disconnecting (reason 4).");
45
46static int max_probe_tries = 5;
47module_param(max_probe_tries, int, 0644);
48MODULE_PARM_DESC(max_probe_tries,
49 "Maximum probe tries before disconnecting (reason 4).");
Johannes Bergb291ba12009-07-10 15:29:03 +020050
51/*
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010052 * Beacon loss timeout is calculated as N frames times the
53 * advertised beacon interval. This may need to be somewhat
54 * higher than what hardware might detect to account for
55 * delays in the host processing frames. But since we also
56 * probe on beacon miss before declaring the connection lost
57 * default to what we want.
Johannes Bergb291ba12009-07-10 15:29:03 +020058 */
Ben Greear59c1ec22013-03-19 14:19:56 -070059static int beacon_loss_count = 7;
60module_param(beacon_loss_count, int, 0644);
61MODULE_PARM_DESC(beacon_loss_count,
62 "Number of beacon intervals before we decide beacon was lost.");
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +010063
Johannes Bergb291ba12009-07-10 15:29:03 +020064/*
65 * Time the connection can be idle before we probe
66 * it to see if we can still talk to the AP.
67 */
Maxim Levitskyd1c50912009-07-31 18:54:23 +030068#define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
Johannes Bergb291ba12009-07-10 15:29:03 +020069/*
70 * Time we wait for a probe response after sending
71 * a probe request because of beacon loss or for
72 * checking the connection still works.
73 */
Ben Greear180205b2011-02-04 15:30:24 -080074static int probe_wait_ms = 500;
75module_param(probe_wait_ms, int, 0644);
76MODULE_PARM_DESC(probe_wait_ms,
77 "Maximum time(ms) to wait for probe response"
78 " before disconnecting (reason 4).");
Jiri Bencf0706e82007-05-05 11:45:53 -070079
Jouni Malinen17e4ec12010-03-29 23:28:30 -070080/*
81 * Weight given to the latest Beacon frame when calculating average signal
82 * strength for Beacon frames received in the current BSS. This must be
83 * between 1 and 15.
84 */
85#define IEEE80211_SIGNAL_AVE_WEIGHT 3
86
Jouni Malinen391a2002010-08-27 22:22:00 +030087/*
88 * How many Beacon frames need to have been used in average signal strength
89 * before starting to indicate signal change events.
90 */
91#define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
92
Johannes Berg77fdaa12009-07-07 03:45:17 +020093/*
94 * All cfg80211 functions have to be called outside a locked
95 * section so that they can acquire a lock themselves... This
96 * is much simpler than queuing up things in cfg80211, but we
97 * do need some indirection for that here.
98 */
99enum rx_mgmt_action {
100 /* no action required */
101 RX_MGMT_NONE,
102
Johannes Berg77fdaa12009-07-07 03:45:17 +0200103 /* caller must call cfg80211_send_deauth() */
104 RX_MGMT_CFG80211_DEAUTH,
105
106 /* caller must call cfg80211_send_disassoc() */
107 RX_MGMT_CFG80211_DISASSOC,
Johannes Berg66e67e42012-01-20 13:55:27 +0100108
109 /* caller must call cfg80211_send_rx_auth() */
110 RX_MGMT_CFG80211_RX_AUTH,
111
112 /* caller must call cfg80211_send_rx_assoc() */
113 RX_MGMT_CFG80211_RX_ASSOC,
114
115 /* caller must call cfg80211_send_assoc_timeout() */
116 RX_MGMT_CFG80211_ASSOC_TIMEOUT,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100117
118 /* used when a processed beacon causes a deauth */
119 RX_MGMT_CFG80211_TX_DEAUTH,
Johannes Berg77fdaa12009-07-07 03:45:17 +0200120};
121
Johannes Berg5484e232008-09-08 17:44:27 +0200122/* utils */
Johannes Berg77fdaa12009-07-07 03:45:17 +0200123static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
124{
Johannes Berg46a5eba2010-09-15 13:28:15 +0200125 lockdep_assert_held(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200126}
127
Johannes Bergca386f32009-07-10 02:39:48 +0200128/*
129 * We can have multiple work items (and connection probing)
130 * scheduling this timer, but we need to take care to only
131 * reschedule it when it should fire _earlier_ than it was
132 * asked for before, or if it's not pending right now. This
133 * function ensures that. Note that it then is required to
134 * run this function for all timeouts after the first one
135 * has happened -- the work that runs from this timer will
136 * do that.
137 */
Johannes Berg66e67e42012-01-20 13:55:27 +0100138static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
Johannes Bergca386f32009-07-10 02:39:48 +0200139{
140 ASSERT_MGD_MTX(ifmgd);
141
142 if (!timer_pending(&ifmgd->timer) ||
143 time_before(timeout, ifmgd->timer.expires))
144 mod_timer(&ifmgd->timer, timeout);
145}
146
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -0400147void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
Johannes Bergb291ba12009-07-10 15:29:03 +0200148{
Johannes Bergc1288b12012-01-19 09:29:57 +0100149 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
Johannes Bergb291ba12009-07-10 15:29:03 +0200150 return;
151
Johannes Berg7eeff742012-07-18 10:27:27 +0200152 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
153 return;
154
Johannes Bergb291ba12009-07-10 15:29:03 +0200155 mod_timer(&sdata->u.mgd.bcn_mon_timer,
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +0100156 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
Johannes Bergb291ba12009-07-10 15:29:03 +0200157}
158
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400159void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
160{
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400161 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
162
Stanislaw Gruszka86281722011-02-25 14:46:02 +0100163 if (unlikely(!sdata->u.mgd.associated))
164 return;
165
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400166 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
167 return;
168
169 mod_timer(&sdata->u.mgd.conn_mon_timer,
170 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
Luis R. Rodriguez0c699c32010-09-16 15:12:30 -0400171
172 ifmgd->probe_send_count = 0;
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -0400173}
174
Johannes Berg5484e232008-09-08 17:44:27 +0200175static int ecw2cw(int ecw)
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200176{
Johannes Berg5484e232008-09-08 17:44:27 +0200177 return (1 << ecw) - 1;
Johannes Berg60f8b39c2008-09-08 17:44:22 +0200178}
179
Johannes Berg6565ec92013-02-08 14:52:32 +0100180static u32 chandef_downgrade(struct cfg80211_chan_def *c)
181{
182 u32 ret;
183 int tmp;
184
185 switch (c->width) {
186 case NL80211_CHAN_WIDTH_20:
187 c->width = NL80211_CHAN_WIDTH_20_NOHT;
188 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
189 break;
190 case NL80211_CHAN_WIDTH_40:
191 c->width = NL80211_CHAN_WIDTH_20;
192 c->center_freq1 = c->chan->center_freq;
193 ret = IEEE80211_STA_DISABLE_40MHZ |
194 IEEE80211_STA_DISABLE_VHT;
195 break;
196 case NL80211_CHAN_WIDTH_80:
197 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
198 /* n_P40 */
199 tmp /= 2;
200 /* freq_P40 */
201 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
202 c->width = NL80211_CHAN_WIDTH_40;
203 ret = IEEE80211_STA_DISABLE_VHT;
204 break;
205 case NL80211_CHAN_WIDTH_80P80:
206 c->center_freq2 = 0;
207 c->width = NL80211_CHAN_WIDTH_80;
208 ret = IEEE80211_STA_DISABLE_80P80MHZ |
209 IEEE80211_STA_DISABLE_160MHZ;
210 break;
211 case NL80211_CHAN_WIDTH_160:
212 /* n_P20 */
213 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
214 /* n_P80 */
215 tmp /= 4;
216 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
217 c->width = NL80211_CHAN_WIDTH_80;
218 ret = IEEE80211_STA_DISABLE_80P80MHZ |
219 IEEE80211_STA_DISABLE_160MHZ;
220 break;
221 default:
222 case NL80211_CHAN_WIDTH_20_NOHT:
223 WARN_ON_ONCE(1);
224 c->width = NL80211_CHAN_WIDTH_20_NOHT;
225 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
226 break;
227 }
228
229 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
230
231 return ret;
232}
233
234static u32
235ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
236 struct ieee80211_supported_band *sband,
237 struct ieee80211_channel *channel,
238 const struct ieee80211_ht_operation *ht_oper,
239 const struct ieee80211_vht_operation *vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100240 struct cfg80211_chan_def *chandef, bool verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100241{
242 struct cfg80211_chan_def vht_chandef;
243 u32 ht_cfreq, ret;
244
245 chandef->chan = channel;
246 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
247 chandef->center_freq1 = channel->center_freq;
248 chandef->center_freq2 = 0;
249
250 if (!ht_oper || !sband->ht_cap.ht_supported) {
251 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
252 goto out;
253 }
254
255 chandef->width = NL80211_CHAN_WIDTH_20;
256
257 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
258 channel->band);
259 /* check that channel matches the right operating channel */
260 if (channel->center_freq != ht_cfreq) {
261 /*
262 * It's possible that some APs are confused here;
263 * Netgear WNDR3700 sometimes reports 4 higher than
264 * the actual channel in association responses, but
265 * since we look at probe response/beacon data here
266 * it should be OK.
267 */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100268 if (verbose)
269 sdata_info(sdata,
270 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
271 channel->center_freq, ht_cfreq,
272 ht_oper->primary_chan, channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100273 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
274 goto out;
275 }
276
277 /* check 40 MHz support, if we have it */
278 if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
279 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
280 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
281 chandef->width = NL80211_CHAN_WIDTH_40;
282 chandef->center_freq1 += 10;
283 break;
284 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
285 chandef->width = NL80211_CHAN_WIDTH_40;
286 chandef->center_freq1 -= 10;
287 break;
288 }
289 } else {
290 /* 40 MHz (and 80 MHz) must be supported for VHT */
291 ret = IEEE80211_STA_DISABLE_VHT;
Johannes Berg85220d72013-03-25 18:29:27 +0100292 /* also mark 40 MHz disabled */
293 ret |= IEEE80211_STA_DISABLE_40MHZ;
Johannes Berg6565ec92013-02-08 14:52:32 +0100294 goto out;
295 }
296
297 if (!vht_oper || !sband->vht_cap.vht_supported) {
298 ret = IEEE80211_STA_DISABLE_VHT;
299 goto out;
300 }
301
302 vht_chandef.chan = channel;
303 vht_chandef.center_freq1 =
304 ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
305 channel->band);
306 vht_chandef.center_freq2 = 0;
307
Johannes Berg6565ec92013-02-08 14:52:32 +0100308 switch (vht_oper->chan_width) {
309 case IEEE80211_VHT_CHANWIDTH_USE_HT:
310 vht_chandef.width = chandef->width;
311 break;
312 case IEEE80211_VHT_CHANWIDTH_80MHZ:
313 vht_chandef.width = NL80211_CHAN_WIDTH_80;
314 break;
315 case IEEE80211_VHT_CHANWIDTH_160MHZ:
316 vht_chandef.width = NL80211_CHAN_WIDTH_160;
317 break;
318 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
319 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
Johannes Berg6553bf042013-03-28 10:26:17 +0100320 vht_chandef.center_freq2 =
321 ieee80211_channel_to_frequency(
322 vht_oper->center_freq_seg2_idx,
323 channel->band);
Johannes Berg6565ec92013-02-08 14:52:32 +0100324 break;
325 default:
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100326 if (verbose)
327 sdata_info(sdata,
328 "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
329 vht_oper->chan_width);
Johannes Berg6565ec92013-02-08 14:52:32 +0100330 ret = IEEE80211_STA_DISABLE_VHT;
331 goto out;
332 }
333
334 if (!cfg80211_chandef_valid(&vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100335 if (verbose)
336 sdata_info(sdata,
337 "AP VHT information is invalid, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100338 ret = IEEE80211_STA_DISABLE_VHT;
339 goto out;
340 }
341
342 if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
343 ret = 0;
344 goto out;
345 }
346
347 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100348 if (verbose)
349 sdata_info(sdata,
350 "AP VHT information doesn't match HT, disable VHT\n");
Johannes Berg6565ec92013-02-08 14:52:32 +0100351 ret = IEEE80211_STA_DISABLE_VHT;
352 goto out;
353 }
354
355 *chandef = vht_chandef;
356
357 ret = 0;
358
359out:
Johannes Berg586e01e2013-02-14 12:13:53 +0100360 /* don't print the message below for VHT mismatch if VHT is disabled */
361 if (ret & IEEE80211_STA_DISABLE_VHT)
362 vht_chandef = *chandef;
363
Johannes Berg6565ec92013-02-08 14:52:32 +0100364 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
365 IEEE80211_CHAN_DISABLED)) {
366 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
367 ret = IEEE80211_STA_DISABLE_HT |
368 IEEE80211_STA_DISABLE_VHT;
369 goto out;
370 }
371
372 ret |= chandef_downgrade(chandef);
373 }
374
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100375 if (chandef->width != vht_chandef.width && verbose)
Johannes Berg6565ec92013-02-08 14:52:32 +0100376 sdata_info(sdata,
377 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
378
379 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
380 return ret;
381}
382
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100383static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
384 struct sta_info *sta,
385 const struct ieee80211_ht_operation *ht_oper,
386 const struct ieee80211_vht_operation *vht_oper,
387 const u8 *bssid, u32 *changed)
Johannes Bergd5522e02009-03-30 13:23:35 +0200388{
389 struct ieee80211_local *local = sdata->local;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100390 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergd5522e02009-03-30 13:23:35 +0200391 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200392 struct ieee80211_channel *chan;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100393 struct cfg80211_chan_def chandef;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200394 u16 ht_opmode;
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100395 u32 flags;
396 enum ieee80211_sta_rx_bandwidth new_sta_bw;
397 int ret;
398
399 /* if HT was/is disabled, don't track any bandwidth changes */
400 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
401 return 0;
402
403 /* don't check VHT if we associated as non-VHT station */
404 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
405 vht_oper = NULL;
Johannes Bergd5522e02009-03-30 13:23:35 +0200406
Johannes Berg11289582013-02-07 17:36:12 +0100407 if (WARN_ON_ONCE(!sta))
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100408 return -EINVAL;
Johannes Berg11289582013-02-07 17:36:12 +0100409
Johannes Bergf2d93302013-02-08 13:15:55 +0100410 chan = sdata->vif.bss_conf.chandef.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200411 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg0aaffa92010-05-05 15:28:27 +0200412
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100413 /* calculate new channel (type) based on HT/VHT operation IEs */
414 flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
415 vht_oper, &chandef, false);
416
417 /*
418 * Downgrade the new channel if we associated with restricted
419 * capabilities. For example, if we associated as a 20 MHz STA
420 * to a 40 MHz AP (due to regulatory, capabilities or config
421 * reasons) then switching to a 40 MHz channel now won't do us
422 * any good -- we couldn't use it with the AP.
423 */
424 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
425 chandef.width == NL80211_CHAN_WIDTH_80P80)
426 flags |= chandef_downgrade(&chandef);
427 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
428 chandef.width == NL80211_CHAN_WIDTH_160)
429 flags |= chandef_downgrade(&chandef);
430 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
431 chandef.width > NL80211_CHAN_WIDTH_20)
432 flags |= chandef_downgrade(&chandef);
433
434 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
435 return 0;
436
437 sdata_info(sdata,
438 "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
439 ifmgd->bssid, chandef.chan->center_freq, chandef.width,
440 chandef.center_freq1, chandef.center_freq2);
441
442 if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
443 IEEE80211_STA_DISABLE_VHT |
444 IEEE80211_STA_DISABLE_40MHZ |
445 IEEE80211_STA_DISABLE_80P80MHZ |
446 IEEE80211_STA_DISABLE_160MHZ)) ||
447 !cfg80211_chandef_valid(&chandef)) {
448 sdata_info(sdata,
449 "AP %pM changed bandwidth in a way we can't support - disconnect\n",
450 ifmgd->bssid);
451 return -EINVAL;
Johannes Berg64f68e52012-03-28 10:58:37 +0200452 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200453
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100454 switch (chandef.width) {
455 case NL80211_CHAN_WIDTH_20_NOHT:
456 case NL80211_CHAN_WIDTH_20:
457 new_sta_bw = IEEE80211_STA_RX_BW_20;
458 break;
459 case NL80211_CHAN_WIDTH_40:
460 new_sta_bw = IEEE80211_STA_RX_BW_40;
461 break;
462 case NL80211_CHAN_WIDTH_80:
463 new_sta_bw = IEEE80211_STA_RX_BW_80;
464 break;
465 case NL80211_CHAN_WIDTH_80P80:
466 case NL80211_CHAN_WIDTH_160:
467 new_sta_bw = IEEE80211_STA_RX_BW_160;
468 break;
469 default:
470 return -EINVAL;
471 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200472
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100473 if (new_sta_bw > sta->cur_max_bandwidth)
474 new_sta_bw = sta->cur_max_bandwidth;
Johannes Berg64f68e52012-03-28 10:58:37 +0200475
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100476 if (new_sta_bw < sta->sta.bandwidth) {
477 sta->sta.bandwidth = new_sta_bw;
478 rate_control_rate_update(local, sband, sta,
479 IEEE80211_RC_BW_CHANGED);
480 }
Rajkumar Manoharan7cc44ed2011-09-16 15:32:34 +0530481
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100482 ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
483 if (ret) {
484 sdata_info(sdata,
485 "AP %pM changed bandwidth to incompatible one - disconnect\n",
486 ifmgd->bssid);
487 return ret;
488 }
489
490 if (new_sta_bw > sta->sta.bandwidth) {
491 sta->sta.bandwidth = new_sta_bw;
492 rate_control_rate_update(local, sband, sta,
493 IEEE80211_RC_BW_CHANGED);
Johannes Berg0aaffa92010-05-05 15:28:27 +0200494 }
Johannes Bergd5522e02009-03-30 13:23:35 +0200495
Johannes Berg074d46d2012-03-15 19:45:16 +0100496 ht_opmode = le16_to_cpu(ht_oper->operation_mode);
Johannes Bergd5522e02009-03-30 13:23:35 +0200497
498 /* if bss configuration changed store the new one */
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100499 if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
500 *changed |= BSS_CHANGED_HT;
Johannes Berg9ed6bcc2009-05-08 20:47:39 +0200501 sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
Johannes Bergd5522e02009-03-30 13:23:35 +0200502 }
503
Johannes Berg30eb1dc2013-02-08 15:12:14 +0100504 return 0;
Johannes Bergd5522e02009-03-30 13:23:35 +0200505}
506
Johannes Berg9c6bd792008-09-11 00:01:52 +0200507/* frame sending functions */
508
Johannes Berg66e67e42012-01-20 13:55:27 +0100509static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
510 struct ieee80211_supported_band *sband,
511 u32 *rates)
512{
513 int i, j, count;
514 *rates = 0;
515 count = 0;
516 for (i = 0; i < supp_rates_len; i++) {
517 int rate = (supp_rates[i] & 0x7F) * 5;
518
519 for (j = 0; j < sband->n_bitrates; j++)
520 if (sband->bitrates[j].bitrate == rate) {
521 *rates |= BIT(j);
522 count++;
523 break;
524 }
525 }
526
527 return count;
528}
529
530static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
Johannes Berg9dde6422012-05-16 23:43:19 +0200531 struct sk_buff *skb, u8 ap_ht_param,
Johannes Berg66e67e42012-01-20 13:55:27 +0100532 struct ieee80211_supported_band *sband,
533 struct ieee80211_channel *channel,
534 enum ieee80211_smps_mode smps)
535{
Johannes Berg66e67e42012-01-20 13:55:27 +0100536 u8 *pos;
537 u32 flags = channel->flags;
538 u16 cap;
539 struct ieee80211_sta_ht_cap ht_cap;
540
541 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
542
Johannes Berg66e67e42012-01-20 13:55:27 +0100543 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
544 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
545
Johannes Berg66e67e42012-01-20 13:55:27 +0100546 /* determine capability flags */
547 cap = ht_cap.cap;
548
Johannes Berg9dde6422012-05-16 23:43:19 +0200549 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
Johannes Berg66e67e42012-01-20 13:55:27 +0100550 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
551 if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
552 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
553 cap &= ~IEEE80211_HT_CAP_SGI_40;
554 }
555 break;
556 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
557 if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
558 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
559 cap &= ~IEEE80211_HT_CAP_SGI_40;
560 }
561 break;
562 }
563
Johannes Berg24398e32012-03-28 10:58:36 +0200564 /*
565 * If 40 MHz was disabled associate as though we weren't
566 * capable of 40 MHz -- some broken APs will never fall
567 * back to trying to transmit in 20 MHz.
568 */
569 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
570 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
571 cap &= ~IEEE80211_HT_CAP_SGI_40;
572 }
573
Johannes Berg66e67e42012-01-20 13:55:27 +0100574 /* set SM PS mode properly */
575 cap &= ~IEEE80211_HT_CAP_SM_PS;
576 switch (smps) {
577 case IEEE80211_SMPS_AUTOMATIC:
578 case IEEE80211_SMPS_NUM_MODES:
579 WARN_ON(1);
580 case IEEE80211_SMPS_OFF:
581 cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
582 IEEE80211_HT_CAP_SM_PS_SHIFT;
583 break;
584 case IEEE80211_SMPS_STATIC:
585 cap |= WLAN_HT_CAP_SM_PS_STATIC <<
586 IEEE80211_HT_CAP_SM_PS_SHIFT;
587 break;
588 case IEEE80211_SMPS_DYNAMIC:
589 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
590 IEEE80211_HT_CAP_SM_PS_SHIFT;
591 break;
592 }
593
594 /* reserve and fill IE */
595 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
596 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
597}
598
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000599static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
600 struct sk_buff *skb,
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100601 struct ieee80211_supported_band *sband,
602 struct ieee80211_vht_cap *ap_vht_cap)
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000603{
604 u8 *pos;
605 u32 cap;
606 struct ieee80211_sta_vht_cap vht_cap;
607
608 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
609
610 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +0100611 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000612
613 /* determine capability flags */
614 cap = vht_cap.cap;
615
Johannes Bergf2d9d272012-11-22 14:11:39 +0100616 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
617 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
618 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
619 }
620
621 if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
622 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
623 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
624 }
625
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100626 /*
627 * Some APs apparently get confused if our capabilities are better
628 * than theirs, so restrict what we advertise in the assoc request.
629 */
630 if (!(ap_vht_cap->vht_cap_info &
631 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
632 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
633
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000634 /* reserve and fill IE */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000635 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000636 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
637}
638
Johannes Berg66e67e42012-01-20 13:55:27 +0100639static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
640{
641 struct ieee80211_local *local = sdata->local;
642 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
643 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
644 struct sk_buff *skb;
645 struct ieee80211_mgmt *mgmt;
646 u8 *pos, qos_info;
647 size_t offset = 0, noffset;
648 int i, count, rates_len, supp_rates_len;
649 u16 capab;
650 struct ieee80211_supported_band *sband;
Johannes Berg55de9082012-07-26 17:24:39 +0200651 struct ieee80211_chanctx_conf *chanctx_conf;
652 struct ieee80211_channel *chan;
Johannes Berg66e67e42012-01-20 13:55:27 +0100653 u32 rates = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +0100654
655 lockdep_assert_held(&ifmgd->mtx);
656
Johannes Berg55de9082012-07-26 17:24:39 +0200657 rcu_read_lock();
658 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
659 if (WARN_ON(!chanctx_conf)) {
660 rcu_read_unlock();
661 return;
662 }
Johannes Berg4bf88532012-11-09 11:39:59 +0100663 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +0200664 rcu_read_unlock();
665 sband = local->hw.wiphy->bands[chan->band];
Johannes Berg66e67e42012-01-20 13:55:27 +0100666
667 if (assoc_data->supp_rates_len) {
668 /*
669 * Get all rates supported by the device and the AP as
670 * some APs don't like getting a superset of their rates
671 * in the association request (e.g. D-Link DAP 1353 in
672 * b-only mode)...
673 */
674 rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
675 assoc_data->supp_rates_len,
676 sband, &rates);
677 } else {
678 /*
679 * In case AP not provide any supported rates information
680 * before association, we send information element(s) with
681 * all rates that we support.
682 */
683 rates = ~0;
684 rates_len = sband->n_bitrates;
685 }
686
687 skb = alloc_skb(local->hw.extra_tx_headroom +
688 sizeof(*mgmt) + /* bit too much but doesn't matter */
689 2 + assoc_data->ssid_len + /* SSID */
690 4 + rates_len + /* (extended) rates */
691 4 + /* power capability */
692 2 + 2 * sband->n_channels + /* supported channels */
693 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
Mahesh Palivelad4950282012-10-10 11:25:40 +0000694 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
Johannes Berg66e67e42012-01-20 13:55:27 +0100695 assoc_data->ie_len + /* extra IEs */
696 9, /* WMM */
697 GFP_KERNEL);
698 if (!skb)
699 return;
700
701 skb_reserve(skb, local->hw.extra_tx_headroom);
702
703 capab = WLAN_CAPABILITY_ESS;
704
705 if (sband->band == IEEE80211_BAND_2GHZ) {
706 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
707 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
708 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
709 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
710 }
711
712 if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
713 capab |= WLAN_CAPABILITY_PRIVACY;
714
715 if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
716 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
717 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
718
719 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
720 memset(mgmt, 0, 24);
721 memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
722 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
723 memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
724
725 if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
726 skb_put(skb, 10);
727 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
728 IEEE80211_STYPE_REASSOC_REQ);
729 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
730 mgmt->u.reassoc_req.listen_interval =
731 cpu_to_le16(local->hw.conf.listen_interval);
732 memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
733 ETH_ALEN);
734 } else {
735 skb_put(skb, 4);
736 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
737 IEEE80211_STYPE_ASSOC_REQ);
738 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
739 mgmt->u.assoc_req.listen_interval =
740 cpu_to_le16(local->hw.conf.listen_interval);
741 }
742
743 /* SSID */
744 pos = skb_put(skb, 2 + assoc_data->ssid_len);
745 *pos++ = WLAN_EID_SSID;
746 *pos++ = assoc_data->ssid_len;
747 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
748
749 /* add all rates which were marked to be used above */
750 supp_rates_len = rates_len;
751 if (supp_rates_len > 8)
752 supp_rates_len = 8;
753
754 pos = skb_put(skb, supp_rates_len + 2);
755 *pos++ = WLAN_EID_SUPP_RATES;
756 *pos++ = supp_rates_len;
757
758 count = 0;
759 for (i = 0; i < sband->n_bitrates; i++) {
760 if (BIT(i) & rates) {
761 int rate = sband->bitrates[i].bitrate;
762 *pos++ = (u8) (rate / 5);
763 if (++count == 8)
764 break;
765 }
766 }
767
768 if (rates_len > count) {
769 pos = skb_put(skb, rates_len - count + 2);
770 *pos++ = WLAN_EID_EXT_SUPP_RATES;
771 *pos++ = rates_len - count;
772
773 for (i++; i < sband->n_bitrates; i++) {
774 if (BIT(i) & rates) {
775 int rate = sband->bitrates[i].bitrate;
776 *pos++ = (u8) (rate / 5);
777 }
778 }
779 }
780
781 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
782 /* 1. power capabilities */
783 pos = skb_put(skb, 4);
784 *pos++ = WLAN_EID_PWR_CAPABILITY;
785 *pos++ = 2;
786 *pos++ = 0; /* min tx power */
Johannes Berg55de9082012-07-26 17:24:39 +0200787 *pos++ = chan->max_power; /* max tx power */
Johannes Berg66e67e42012-01-20 13:55:27 +0100788
789 /* 2. supported channels */
790 /* TODO: get this in reg domain format */
791 pos = skb_put(skb, 2 * sband->n_channels + 2);
792 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
793 *pos++ = 2 * sband->n_channels;
794 for (i = 0; i < sband->n_channels; i++) {
795 *pos++ = ieee80211_frequency_to_channel(
796 sband->channels[i].center_freq);
797 *pos++ = 1; /* one channel in the subband*/
798 }
799 }
800
801 /* if present, add any custom IEs that go before HT */
802 if (assoc_data->ie_len && assoc_data->ie) {
803 static const u8 before_ht[] = {
804 WLAN_EID_SSID,
805 WLAN_EID_SUPP_RATES,
806 WLAN_EID_EXT_SUPP_RATES,
807 WLAN_EID_PWR_CAPABILITY,
808 WLAN_EID_SUPPORTED_CHANNELS,
809 WLAN_EID_RSN,
810 WLAN_EID_QOS_CAPA,
811 WLAN_EID_RRM_ENABLED_CAPABILITIES,
812 WLAN_EID_MOBILITY_DOMAIN,
813 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
814 };
815 noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
816 before_ht, ARRAY_SIZE(before_ht),
817 offset);
818 pos = skb_put(skb, noffset - offset);
819 memcpy(pos, assoc_data->ie + offset, noffset - offset);
820 offset = noffset;
821 }
822
Johannes Bergf2d9d272012-11-22 14:11:39 +0100823 if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
824 !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
825 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
826
Johannes Berga8243b72012-11-22 14:32:09 +0100827 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Johannes Berg9dde6422012-05-16 23:43:19 +0200828 ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
Johannes Berg04ecd252012-09-11 14:34:12 +0200829 sband, chan, sdata->smps_mode);
Johannes Berg66e67e42012-01-20 13:55:27 +0100830
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000831 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
Johannes Bergb08fbbd2012-12-07 13:06:48 +0100832 ieee80211_add_vht_ie(sdata, skb, sband,
833 &assoc_data->ap_vht_cap);
Mahesh Palivelad545dab2012-07-24 03:33:10 +0000834
Johannes Berg66e67e42012-01-20 13:55:27 +0100835 /* if present, add any custom non-vendor IEs that go after HT */
836 if (assoc_data->ie_len && assoc_data->ie) {
837 noffset = ieee80211_ie_split_vendor(assoc_data->ie,
838 assoc_data->ie_len,
839 offset);
840 pos = skb_put(skb, noffset - offset);
841 memcpy(pos, assoc_data->ie + offset, noffset - offset);
842 offset = noffset;
843 }
844
Johannes Berg76f03032012-03-08 15:02:05 +0100845 if (assoc_data->wmm) {
846 if (assoc_data->uapsd) {
Eliad Pellerdc41e4d2012-03-14 16:15:03 +0200847 qos_info = ifmgd->uapsd_queues;
848 qos_info |= (ifmgd->uapsd_max_sp_len <<
Johannes Berg66e67e42012-01-20 13:55:27 +0100849 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
850 } else {
851 qos_info = 0;
852 }
853
854 pos = skb_put(skb, 9);
855 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
856 *pos++ = 7; /* len */
857 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
858 *pos++ = 0x50;
859 *pos++ = 0xf2;
860 *pos++ = 2; /* WME */
861 *pos++ = 0; /* WME info */
862 *pos++ = 1; /* WME ver */
863 *pos++ = qos_info;
864 }
865
866 /* add any remaining custom (i.e. vendor specific here) IEs */
867 if (assoc_data->ie_len && assoc_data->ie) {
868 noffset = assoc_data->ie_len;
869 pos = skb_put(skb, noffset - offset);
870 memcpy(pos, assoc_data->ie + offset, noffset - offset);
871 }
872
Johannes Berga1845fc2012-06-27 13:18:36 +0200873 drv_mgd_prepare_tx(local, sdata);
874
Johannes Berg66e67e42012-01-20 13:55:27 +0100875 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg1672c0e32013-01-29 15:02:27 +0100876 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
877 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
878 IEEE80211_TX_INTFL_MLME_CONN_TX;
Johannes Berg66e67e42012-01-20 13:55:27 +0100879 ieee80211_tx_skb(sdata, skb);
880}
881
Kalle Valo572e0012009-02-10 17:09:31 +0200882void ieee80211_send_pspoll(struct ieee80211_local *local,
883 struct ieee80211_sub_if_data *sdata)
884{
Kalle Valo572e0012009-02-10 17:09:31 +0200885 struct ieee80211_pspoll *pspoll;
886 struct sk_buff *skb;
Kalle Valo572e0012009-02-10 17:09:31 +0200887
Kalle Valod8cd1892010-01-05 20:16:26 +0200888 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
889 if (!skb)
Kalle Valo572e0012009-02-10 17:09:31 +0200890 return;
Kalle Valo572e0012009-02-10 17:09:31 +0200891
Kalle Valod8cd1892010-01-05 20:16:26 +0200892 pspoll = (struct ieee80211_pspoll *) skb->data;
893 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Kalle Valo572e0012009-02-10 17:09:31 +0200894
Johannes Berg62ae67b2009-11-18 18:42:05 +0100895 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
896 ieee80211_tx_skb(sdata, skb);
Kalle Valo572e0012009-02-10 17:09:31 +0200897}
898
Johannes Berg965beda2009-04-16 13:17:24 +0200899void ieee80211_send_nullfunc(struct ieee80211_local *local,
900 struct ieee80211_sub_if_data *sdata,
901 int powersave)
902{
903 struct sk_buff *skb;
Kalle Valod8cd1892010-01-05 20:16:26 +0200904 struct ieee80211_hdr_3addr *nullfunc;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530905 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg965beda2009-04-16 13:17:24 +0200906
Kalle Valod8cd1892010-01-05 20:16:26 +0200907 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
908 if (!skb)
Johannes Berg965beda2009-04-16 13:17:24 +0200909 return;
910
Kalle Valod8cd1892010-01-05 20:16:26 +0200911 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
Johannes Berg965beda2009-04-16 13:17:24 +0200912 if (powersave)
Kalle Valod8cd1892010-01-05 20:16:26 +0200913 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
Johannes Berg965beda2009-04-16 13:17:24 +0200914
Seth Forshee6c17b772013-02-11 11:21:07 -0600915 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
916 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Rajkumar Manoharanb6f35302011-09-29 20:34:04 +0530917 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
918 IEEE80211_STA_CONNECTION_POLL))
919 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
920
Johannes Berg62ae67b2009-11-18 18:42:05 +0100921 ieee80211_tx_skb(sdata, skb);
Johannes Berg965beda2009-04-16 13:17:24 +0200922}
923
Felix Fietkaud5242152010-01-08 18:06:26 +0100924static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
925 struct ieee80211_sub_if_data *sdata)
926{
927 struct sk_buff *skb;
928 struct ieee80211_hdr *nullfunc;
929 __le16 fc;
930
931 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
932 return;
933
934 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
Joe Perchesd15b8452011-08-29 14:17:31 -0700935 if (!skb)
Felix Fietkaud5242152010-01-08 18:06:26 +0100936 return;
Joe Perchesd15b8452011-08-29 14:17:31 -0700937
Felix Fietkaud5242152010-01-08 18:06:26 +0100938 skb_reserve(skb, local->hw.extra_tx_headroom);
939
940 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
941 memset(nullfunc, 0, 30);
942 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
943 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
944 nullfunc->frame_control = fc;
945 memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
946 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
947 memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
948 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
949
950 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
951 ieee80211_tx_skb(sdata, skb);
952}
953
Johannes Bergcc32abd2009-05-15 11:52:31 +0200954/* spectrum management related things */
955static void ieee80211_chswitch_work(struct work_struct *work)
956{
957 struct ieee80211_sub_if_data *sdata =
958 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
Karl Beldan675a0b02013-03-25 16:26:57 +0100959 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200960 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
961
Johannes Berg9607e6b662009-12-23 13:15:31 +0100962 if (!ieee80211_sdata_running(sdata))
Johannes Bergcc32abd2009-05-15 11:52:31 +0200963 return;
964
Johannes Berg77fdaa12009-07-07 03:45:17 +0200965 mutex_lock(&ifmgd->mtx);
966 if (!ifmgd->associated)
967 goto out;
Johannes Bergcc32abd2009-05-15 11:52:31 +0200968
Johannes Berg85220d72013-03-25 18:29:27 +0100969 local->_oper_chandef = local->csa_chandef;
Karl Beldan675a0b02013-03-25 16:26:57 +0100970
971 if (!local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +0200972 /* call "hw_config" only if doing sw channel switch */
Karl Beldan675a0b02013-03-25 16:26:57 +0100973 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Shahar Levi1ea57b12011-09-08 08:44:05 +0300974 } else {
975 /* update the device channel directly */
Karl Beldan675a0b02013-03-25 16:26:57 +0100976 local->hw.conf.chandef = local->_oper_chandef;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200977 }
Johannes Bergcc32abd2009-05-15 11:52:31 +0200978
Johannes Berg77fdaa12009-07-07 03:45:17 +0200979 /* XXX: shouldn't really modify cfg80211-owned data! */
Karl Beldan675a0b02013-03-25 16:26:57 +0100980 ifmgd->associated->channel = local->_oper_chandef.chan;
Johannes Berg77fdaa12009-07-07 03:45:17 +0200981
Johannes Berg57eebdf2012-08-01 15:50:46 +0200982 /* XXX: wait for a beacon first? */
Karl Beldan675a0b02013-03-25 16:26:57 +0100983 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +0100984 IEEE80211_MAX_QUEUE_MAP,
Johannes Bergcc32abd2009-05-15 11:52:31 +0200985 IEEE80211_QUEUE_STOP_REASON_CSA);
Johannes Berg77fdaa12009-07-07 03:45:17 +0200986 out:
987 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
988 mutex_unlock(&ifmgd->mtx);
Johannes Bergcc32abd2009-05-15 11:52:31 +0200989}
990
Johannes Berg5ce6e432010-05-11 16:20:57 +0200991void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
992{
Johannes Berg55de9082012-07-26 17:24:39 +0200993 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
994 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg5ce6e432010-05-11 16:20:57 +0200995
996 trace_api_chswitch_done(sdata, success);
997 if (!success) {
Johannes Berg882a7c62012-08-01 22:32:45 +0200998 sdata_info(sdata,
999 "driver channel switch failed, disconnecting\n");
1000 ieee80211_queue_work(&sdata->local->hw,
1001 &ifmgd->csa_connection_drop_work);
1002 } else {
1003 ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001004 }
Johannes Berg5ce6e432010-05-11 16:20:57 +02001005}
1006EXPORT_SYMBOL(ieee80211_chswitch_done);
1007
Johannes Bergcc32abd2009-05-15 11:52:31 +02001008static void ieee80211_chswitch_timer(unsigned long data)
1009{
1010 struct ieee80211_sub_if_data *sdata =
1011 (struct ieee80211_sub_if_data *) data;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001012
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01001013 ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001014}
1015
Johannes Berg37799e52013-03-26 14:02:26 +01001016static void
Johannes Berg4a3cb702013-02-12 16:43:19 +01001017ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01001018 u64 timestamp, struct ieee802_11_elems *elems)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001019{
Johannes Bergb4f286a12013-03-26 14:13:58 +01001020 struct ieee80211_local *local = sdata->local;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001021 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg37799e52013-03-26 14:02:26 +01001022 struct cfg80211_bss *cbss = ifmgd->associated;
1023 struct ieee80211_bss *bss;
Johannes Berg55de9082012-07-26 17:24:39 +02001024 struct ieee80211_chanctx *chanctx;
Johannes Bergb4f286a12013-03-26 14:13:58 +01001025 enum ieee80211_band new_band;
1026 int new_freq;
1027 u8 new_chan_no;
1028 u8 count;
1029 u8 mode;
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001030 struct ieee80211_channel *new_chan;
Johannes Berg85220d72013-03-25 18:29:27 +01001031 struct cfg80211_chan_def new_chandef = {};
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001032 struct cfg80211_chan_def new_vht_chandef = {};
1033 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1034 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
Johannes Berg85220d72013-03-25 18:29:27 +01001035 int secondary_channel_offset = -1;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001036
Johannes Berg77fdaa12009-07-07 03:45:17 +02001037 ASSERT_MGD_MTX(ifmgd);
1038
Johannes Berg37799e52013-03-26 14:02:26 +01001039 if (!cbss)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001040 return;
1041
Johannes Bergb4f286a12013-03-26 14:13:58 +01001042 if (local->scanning)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001043 return;
1044
Johannes Berg37799e52013-03-26 14:02:26 +01001045 /* disregard subsequent announcements if we are already processing */
Johannes Bergcc32abd2009-05-15 11:52:31 +02001046 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1047 return;
1048
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001049 sec_chan_offs = elems->sec_chan_offs;
1050 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
1051
1052 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1053 IEEE80211_STA_DISABLE_40MHZ)) {
1054 sec_chan_offs = NULL;
1055 wide_bw_chansw_ie = NULL;
Johannes Berg85220d72013-03-25 18:29:27 +01001056 }
1057
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001058 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1059 wide_bw_chansw_ie = NULL;
Johannes Berg85220d72013-03-25 18:29:27 +01001060
Johannes Bergb4f286a12013-03-26 14:13:58 +01001061 if (elems->ext_chansw_ie) {
1062 if (!ieee80211_operating_class_to_band(
1063 elems->ext_chansw_ie->new_operating_class,
1064 &new_band)) {
1065 sdata_info(sdata,
1066 "cannot understand ECSA IE operating class %d, disconnecting\n",
1067 elems->ext_chansw_ie->new_operating_class);
1068 ieee80211_queue_work(&local->hw,
1069 &ifmgd->csa_connection_drop_work);
1070 }
1071 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1072 count = elems->ext_chansw_ie->count;
1073 mode = elems->ext_chansw_ie->mode;
1074 } else if (elems->ch_switch_ie) {
1075 new_band = cbss->channel->band;
1076 new_chan_no = elems->ch_switch_ie->new_ch_num;
1077 count = elems->ch_switch_ie->count;
1078 mode = elems->ch_switch_ie->mode;
1079 } else {
1080 /* nothing here we understand */
Johannes Berg37799e52013-03-26 14:02:26 +01001081 return;
Johannes Bergb4f286a12013-03-26 14:13:58 +01001082 }
Johannes Berg37799e52013-03-26 14:02:26 +01001083
1084 bss = (void *)cbss->priv;
1085
Johannes Bergb4f286a12013-03-26 14:13:58 +01001086 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001087 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1088 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
Johannes Berg882a7c62012-08-01 22:32:45 +02001089 sdata_info(sdata,
1090 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1091 ifmgd->associated->bssid, new_freq);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001092 ieee80211_queue_work(&local->hw,
Johannes Berg882a7c62012-08-01 22:32:45 +02001093 &ifmgd->csa_connection_drop_work);
Johannes Bergcc32abd2009-05-15 11:52:31 +02001094 return;
Johannes Berg882a7c62012-08-01 22:32:45 +02001095 }
Johannes Bergcc32abd2009-05-15 11:52:31 +02001096
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001097 if (sec_chan_offs) {
1098 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
1099 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
1100 /* if HT is enabled and the IE not present, it's still HT */
1101 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1102 }
1103
Johannes Berg85220d72013-03-25 18:29:27 +01001104 switch (secondary_channel_offset) {
1105 default:
1106 /* secondary_channel_offset was present but is invalid */
1107 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001108 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001109 NL80211_CHAN_HT20);
1110 break;
1111 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001112 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001113 NL80211_CHAN_HT40PLUS);
1114 break;
1115 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001116 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001117 NL80211_CHAN_HT40MINUS);
1118 break;
1119 case -1:
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001120 cfg80211_chandef_create(&new_chandef, new_chan,
Johannes Berg85220d72013-03-25 18:29:27 +01001121 NL80211_CHAN_NO_HT);
1122 break;
1123 }
1124
Johannes Bergcd64f2a2013-03-28 10:44:18 +01001125 if (wide_bw_chansw_ie) {
1126 new_vht_chandef.chan = new_chan;
1127 new_vht_chandef.center_freq1 =
1128 ieee80211_channel_to_frequency(
1129 wide_bw_chansw_ie->new_center_freq_seg0,
1130 new_band);
1131
1132 switch (wide_bw_chansw_ie->new_channel_width) {
1133 default:
1134 /* hmmm, ignore VHT and use HT if present */
1135 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1136 new_vht_chandef.chan = NULL;
1137 break;
1138 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1139 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1140 break;
1141 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1142 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1143 break;
1144 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1145 /* field is otherwise reserved */
1146 new_vht_chandef.center_freq2 =
1147 ieee80211_channel_to_frequency(
1148 wide_bw_chansw_ie->new_center_freq_seg1,
1149 new_band);
1150 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1151 break;
1152 }
1153 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1154 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1155 chandef_downgrade(&new_vht_chandef);
1156 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1157 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1158 chandef_downgrade(&new_vht_chandef);
1159 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1160 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1161 chandef_downgrade(&new_vht_chandef);
1162 }
1163
1164 /* if VHT data is there validate & use it */
1165 if (new_vht_chandef.chan) {
1166 if (!cfg80211_chandef_compatible(&new_vht_chandef,
1167 &new_chandef)) {
1168 sdata_info(sdata,
1169 "AP %pM CSA has inconsistent channel data, disconnecting\n",
1170 ifmgd->associated->bssid);
1171 ieee80211_queue_work(&local->hw,
1172 &ifmgd->csa_connection_drop_work);
1173 return;
1174 }
1175 new_chandef = new_vht_chandef;
1176 }
1177
Johannes Berg85220d72013-03-25 18:29:27 +01001178 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1179 IEEE80211_CHAN_DISABLED)) {
1180 sdata_info(sdata,
1181 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1182 ifmgd->associated->bssid, new_freq,
1183 new_chandef.width, new_chandef.center_freq1,
1184 new_chandef.center_freq2);
1185 ieee80211_queue_work(&local->hw,
1186 &ifmgd->csa_connection_drop_work);
1187 return;
1188 }
1189
Johannes Berg57eebdf2012-08-01 15:50:46 +02001190 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1191
Johannes Bergb4f286a12013-03-26 14:13:58 +01001192 if (local->use_chanctx) {
Johannes Berg55de9082012-07-26 17:24:39 +02001193 sdata_info(sdata,
1194 "not handling channel switch with channel contexts\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001195 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001196 &ifmgd->csa_connection_drop_work);
Simon Wunderlich246dc3f2012-11-30 19:17:27 +01001197 return;
Johannes Berg55de9082012-07-26 17:24:39 +02001198 }
1199
Johannes Bergb4f286a12013-03-26 14:13:58 +01001200 mutex_lock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001201 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
Johannes Bergb4f286a12013-03-26 14:13:58 +01001202 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001203 return;
1204 }
1205 chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1206 struct ieee80211_chanctx, conf);
1207 if (chanctx->refcount > 1) {
1208 sdata_info(sdata,
1209 "channel switch with multiple interfaces on the same channel, disconnecting\n");
Johannes Bergb4f286a12013-03-26 14:13:58 +01001210 ieee80211_queue_work(&local->hw,
Johannes Berg55de9082012-07-26 17:24:39 +02001211 &ifmgd->csa_connection_drop_work);
Johannes Bergb4f286a12013-03-26 14:13:58 +01001212 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001213 return;
1214 }
Johannes Bergb4f286a12013-03-26 14:13:58 +01001215 mutex_unlock(&local->chanctx_mtx);
Johannes Berg55de9082012-07-26 17:24:39 +02001216
Johannes Berg85220d72013-03-25 18:29:27 +01001217 local->csa_chandef = new_chandef;
Johannes Berg55de9082012-07-26 17:24:39 +02001218
Johannes Bergb4f286a12013-03-26 14:13:58 +01001219 if (mode)
1220 ieee80211_stop_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001221 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001222 IEEE80211_QUEUE_STOP_REASON_CSA);
1223
Johannes Bergb4f286a12013-03-26 14:13:58 +01001224 if (local->ops->channel_switch) {
Johannes Berg5ce6e432010-05-11 16:20:57 +02001225 /* use driver's channel switch callback */
Johannes Berg57eebdf2012-08-01 15:50:46 +02001226 struct ieee80211_channel_switch ch_switch = {
1227 .timestamp = timestamp,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001228 .block_tx = mode,
Johannes Berg85220d72013-03-25 18:29:27 +01001229 .chandef = new_chandef,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001230 .count = count,
Johannes Berg57eebdf2012-08-01 15:50:46 +02001231 };
1232
Johannes Bergb4f286a12013-03-26 14:13:58 +01001233 drv_channel_switch(local, &ch_switch);
Johannes Berg5ce6e432010-05-11 16:20:57 +02001234 return;
1235 }
1236
1237 /* channel switch handled in software */
Johannes Bergb4f286a12013-03-26 14:13:58 +01001238 if (count <= 1)
1239 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
Johannes Berg57eebdf2012-08-01 15:50:46 +02001240 else
Johannes Bergcc32abd2009-05-15 11:52:31 +02001241 mod_timer(&ifmgd->chswitch_timer,
Johannes Bergb4f286a12013-03-26 14:13:58 +01001242 TU_TO_EXP_TIME(count * cbss->beacon_interval));
Johannes Bergcc32abd2009-05-15 11:52:31 +02001243}
1244
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001245static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1246 struct ieee80211_channel *channel,
1247 const u8 *country_ie, u8 country_ie_len,
1248 const u8 *pwr_constr_elem)
Johannes Bergcc32abd2009-05-15 11:52:31 +02001249{
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001250 struct ieee80211_country_ie_triplet *triplet;
1251 int chan = ieee80211_frequency_to_channel(channel->center_freq);
1252 int i, chan_pwr, chan_increment, new_ap_level;
1253 bool have_chan_pwr = false;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001254
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001255 /* Invalid IE */
1256 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001257 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001258
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001259 triplet = (void *)(country_ie + 3);
1260 country_ie_len -= 3;
1261
1262 switch (channel->band) {
1263 default:
1264 WARN_ON_ONCE(1);
1265 /* fall through */
1266 case IEEE80211_BAND_2GHZ:
1267 case IEEE80211_BAND_60GHZ:
1268 chan_increment = 1;
1269 break;
1270 case IEEE80211_BAND_5GHZ:
1271 chan_increment = 4;
1272 break;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001273 }
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001274
1275 /* find channel */
1276 while (country_ie_len >= 3) {
1277 u8 first_channel = triplet->chans.first_channel;
1278
1279 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
1280 goto next;
1281
1282 for (i = 0; i < triplet->chans.num_channels; i++) {
1283 if (first_channel + i * chan_increment == chan) {
1284 have_chan_pwr = true;
1285 chan_pwr = triplet->chans.max_power;
1286 break;
1287 }
1288 }
1289 if (have_chan_pwr)
1290 break;
1291
1292 next:
1293 triplet++;
1294 country_ie_len -= 3;
1295 }
1296
1297 if (!have_chan_pwr)
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001298 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001299
1300 new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
1301
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001302 if (sdata->ap_power_level == new_ap_level)
1303 return 0;
Johannes Berg04b7b2f2012-09-05 13:41:37 +02001304
1305 sdata_info(sdata,
1306 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1307 new_ap_level, chan_pwr, *pwr_constr_elem,
1308 sdata->u.mgd.bssid);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001309 sdata->ap_power_level = new_ap_level;
1310 if (__ieee80211_recalc_txpower(sdata))
1311 return BSS_CHANGED_TXPOWER;
1312 return 0;
Johannes Bergcc32abd2009-05-15 11:52:31 +02001313}
1314
Johannes Berg965beda2009-04-16 13:17:24 +02001315/* powersave */
1316static void ieee80211_enable_ps(struct ieee80211_local *local,
1317 struct ieee80211_sub_if_data *sdata)
1318{
1319 struct ieee80211_conf *conf = &local->hw.conf;
1320
Johannes Bergd5edaed2009-04-22 23:02:51 +02001321 /*
1322 * If we are scanning right now then the parameters will
1323 * take effect when scan finishes.
1324 */
Helmut Schaafbe9c4292009-07-23 12:14:04 +02001325 if (local->scanning)
Johannes Bergd5edaed2009-04-22 23:02:51 +02001326 return;
1327
Johannes Berg965beda2009-04-16 13:17:24 +02001328 if (conf->dynamic_ps_timeout > 0 &&
1329 !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
1330 mod_timer(&local->dynamic_ps_timer, jiffies +
1331 msecs_to_jiffies(conf->dynamic_ps_timeout));
1332 } else {
1333 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
1334 ieee80211_send_nullfunc(local, sdata, 1);
Vivek Natarajan375177b2010-02-09 14:50:28 +05301335
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001336 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
1337 (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
1338 return;
1339
1340 conf->flags |= IEEE80211_CONF_PS;
1341 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
Johannes Berg965beda2009-04-16 13:17:24 +02001342 }
1343}
1344
1345static void ieee80211_change_ps(struct ieee80211_local *local)
1346{
1347 struct ieee80211_conf *conf = &local->hw.conf;
1348
1349 if (local->ps_sdata) {
Johannes Berg965beda2009-04-16 13:17:24 +02001350 ieee80211_enable_ps(local, local->ps_sdata);
1351 } else if (conf->flags & IEEE80211_CONF_PS) {
1352 conf->flags &= ~IEEE80211_CONF_PS;
1353 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1354 del_timer_sync(&local->dynamic_ps_timer);
1355 cancel_work_sync(&local->dynamic_ps_enable_work);
1356 }
1357}
1358
Jason Young808118c2011-03-10 16:43:19 -08001359static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1360{
1361 struct ieee80211_if_managed *mgd = &sdata->u.mgd;
1362 struct sta_info *sta = NULL;
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001363 bool authorized = false;
Jason Young808118c2011-03-10 16:43:19 -08001364
1365 if (!mgd->powersave)
1366 return false;
1367
Johannes Berg05cb9102011-10-28 11:59:47 +02001368 if (mgd->broken_ap)
1369 return false;
1370
Jason Young808118c2011-03-10 16:43:19 -08001371 if (!mgd->associated)
1372 return false;
1373
Jason Young808118c2011-03-10 16:43:19 -08001374 if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
1375 IEEE80211_STA_CONNECTION_POLL))
1376 return false;
1377
1378 rcu_read_lock();
1379 sta = sta_info_get(sdata, mgd->bssid);
1380 if (sta)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001381 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
Jason Young808118c2011-03-10 16:43:19 -08001382 rcu_read_unlock();
1383
Johannes Bergc2c98fd2011-09-29 16:04:36 +02001384 return authorized;
Jason Young808118c2011-03-10 16:43:19 -08001385}
1386
Johannes Berg965beda2009-04-16 13:17:24 +02001387/* need to hold RTNL or interface lock */
Johannes Berg10f644a2009-04-16 13:17:25 +02001388void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
Johannes Berg965beda2009-04-16 13:17:24 +02001389{
1390 struct ieee80211_sub_if_data *sdata, *found = NULL;
1391 int count = 0;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001392 int timeout;
Johannes Berg965beda2009-04-16 13:17:24 +02001393
1394 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
1395 local->ps_sdata = NULL;
1396 return;
1397 }
1398
1399 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +01001400 if (!ieee80211_sdata_running(sdata))
Johannes Berg965beda2009-04-16 13:17:24 +02001401 continue;
Rajkumar Manoharan8c7914d2011-02-01 00:28:59 +05301402 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1403 /* If an AP vif is found, then disable PS
1404 * by setting the count to zero thereby setting
1405 * ps_sdata to NULL.
1406 */
1407 count = 0;
1408 break;
1409 }
Johannes Berg965beda2009-04-16 13:17:24 +02001410 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1411 continue;
1412 found = sdata;
1413 count++;
1414 }
1415
Jason Young808118c2011-03-10 16:43:19 -08001416 if (count == 1 && ieee80211_powersave_allowed(found)) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001417 s32 beaconint_us;
1418
1419 if (latency < 0)
Mark Grossed771342010-05-06 01:59:26 +02001420 latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
Johannes Berg10f644a2009-04-16 13:17:25 +02001421
1422 beaconint_us = ieee80211_tu_to_usec(
1423 found->vif.bss_conf.beacon_int);
1424
Juuso Oikarinenff616382010-06-09 09:51:52 +03001425 timeout = local->dynamic_ps_forced_timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001426 if (timeout < 0) {
1427 /*
Juuso Oikarinenff616382010-06-09 09:51:52 +03001428 * Go to full PSM if the user configures a very low
1429 * latency requirement.
Eliad Peller0ab82b02010-12-03 02:16:23 +02001430 * The 2000 second value is there for compatibility
1431 * until the PM_QOS_NETWORK_LATENCY is configured
1432 * with real values.
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001433 */
Eliad Peller0ab82b02010-12-03 02:16:23 +02001434 if (latency > (1900 * USEC_PER_MSEC) &&
1435 latency != (2000 * USEC_PER_SEC))
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001436 timeout = 0;
Juuso Oikarinenff616382010-06-09 09:51:52 +03001437 else
1438 timeout = 100;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001439 }
Johannes Berg09b85562013-02-06 23:07:41 +01001440 local->hw.conf.dynamic_ps_timeout = timeout;
Juuso Oikarinen195e2942010-04-27 12:47:40 +03001441
Johannes Berg04fe2032009-04-22 18:44:37 +02001442 if (beaconint_us > latency) {
Johannes Berg10f644a2009-04-16 13:17:25 +02001443 local->ps_sdata = NULL;
Johannes Berg04fe2032009-04-22 18:44:37 +02001444 } else {
Johannes Berg04fe2032009-04-22 18:44:37 +02001445 int maxslp = 1;
Johannes Berg826262c2012-12-10 16:38:14 +02001446 u8 dtimper = found->u.mgd.dtim_period;
Johannes Berg56007a02010-01-26 14:19:52 +01001447
1448 /* If the TIM IE is invalid, pretend the value is 1 */
1449 if (!dtimper)
1450 dtimper = 1;
1451 else if (dtimper > 1)
Johannes Berg04fe2032009-04-22 18:44:37 +02001452 maxslp = min_t(int, dtimper,
1453 latency / beaconint_us);
1454
Johannes Berg9ccebe62009-04-23 10:32:36 +02001455 local->hw.conf.max_sleep_period = maxslp;
Johannes Berg56007a02010-01-26 14:19:52 +01001456 local->hw.conf.ps_dtim_period = dtimper;
Johannes Berg10f644a2009-04-16 13:17:25 +02001457 local->ps_sdata = found;
Johannes Berg04fe2032009-04-22 18:44:37 +02001458 }
Johannes Berg10f644a2009-04-16 13:17:25 +02001459 } else {
Johannes Berg965beda2009-04-16 13:17:24 +02001460 local->ps_sdata = NULL;
Johannes Berg10f644a2009-04-16 13:17:25 +02001461 }
Johannes Berg965beda2009-04-16 13:17:24 +02001462
1463 ieee80211_change_ps(local);
1464}
1465
Eliad Pellerab095872012-07-27 12:33:22 +03001466void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
1467{
1468 bool ps_allowed = ieee80211_powersave_allowed(sdata);
1469
1470 if (sdata->vif.bss_conf.ps != ps_allowed) {
1471 sdata->vif.bss_conf.ps = ps_allowed;
1472 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
1473 }
1474}
1475
Johannes Berg965beda2009-04-16 13:17:24 +02001476void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
1477{
1478 struct ieee80211_local *local =
1479 container_of(work, struct ieee80211_local,
1480 dynamic_ps_disable_work);
1481
1482 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1483 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1484 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1485 }
1486
1487 ieee80211_wake_queues_by_reason(&local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01001488 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg965beda2009-04-16 13:17:24 +02001489 IEEE80211_QUEUE_STOP_REASON_PS);
1490}
1491
1492void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
1493{
1494 struct ieee80211_local *local =
1495 container_of(work, struct ieee80211_local,
1496 dynamic_ps_enable_work);
1497 struct ieee80211_sub_if_data *sdata = local->ps_sdata;
Christian Lamparter5e340692011-06-30 21:08:43 +02001498 struct ieee80211_if_managed *ifmgd;
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301499 unsigned long flags;
1500 int q;
Johannes Berg965beda2009-04-16 13:17:24 +02001501
1502 /* can only happen when PS was just disabled anyway */
1503 if (!sdata)
1504 return;
1505
Christian Lamparter5e340692011-06-30 21:08:43 +02001506 ifmgd = &sdata->u.mgd;
1507
Johannes Berg965beda2009-04-16 13:17:24 +02001508 if (local->hw.conf.flags & IEEE80211_CONF_PS)
1509 return;
1510
Johannes Berg09b85562013-02-06 23:07:41 +01001511 if (local->hw.conf.dynamic_ps_timeout > 0) {
Arik Nemtsov77b70232011-06-26 12:06:54 +03001512 /* don't enter PS if TX frames are pending */
1513 if (drv_tx_frames_pending(local)) {
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301514 mod_timer(&local->dynamic_ps_timer, jiffies +
1515 msecs_to_jiffies(
1516 local->hw.conf.dynamic_ps_timeout));
1517 return;
1518 }
Arik Nemtsov77b70232011-06-26 12:06:54 +03001519
1520 /*
1521 * transmission can be stopped by others which leads to
1522 * dynamic_ps_timer expiry. Postpone the ps timer if it
1523 * is not the actual idle state.
1524 */
1525 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1526 for (q = 0; q < local->hw.queues; q++) {
1527 if (local->queue_stop_reasons[q]) {
1528 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1529 flags);
1530 mod_timer(&local->dynamic_ps_timer, jiffies +
1531 msecs_to_jiffies(
1532 local->hw.conf.dynamic_ps_timeout));
1533 return;
1534 }
1535 }
1536 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301537 }
Rajkumar Manoharan1ddc2862011-05-03 17:03:59 +05301538
Vivek Natarajan375177b2010-02-09 14:50:28 +05301539 if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
Mohammed Shafi Shajakhan9c38a8b2011-12-14 19:46:07 +05301540 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
Johannes Berga15983832013-03-26 22:02:42 +01001541 if (drv_tx_frames_pending(local)) {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301542 mod_timer(&local->dynamic_ps_timer, jiffies +
1543 msecs_to_jiffies(
1544 local->hw.conf.dynamic_ps_timeout));
Johannes Berga15983832013-03-26 22:02:42 +01001545 } else {
Vivek Natarajane8306f92011-04-06 11:41:10 +05301546 ieee80211_send_nullfunc(local, sdata, 1);
1547 /* Flush to get the tx status of nullfunc frame */
Johannes Berg39ecc012013-02-13 12:11:00 +01001548 ieee80211_flush_queues(local, sdata);
Vivek Natarajane8306f92011-04-06 11:41:10 +05301549 }
Vivek Natarajanf3e85b92011-02-23 13:04:32 +05301550 }
1551
Juuso Oikarinen2a130522010-03-09 14:25:02 +02001552 if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
1553 (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
Vivek Natarajan375177b2010-02-09 14:50:28 +05301554 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
1555 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
1556 local->hw.conf.flags |= IEEE80211_CONF_PS;
1557 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1558 }
Johannes Berg965beda2009-04-16 13:17:24 +02001559}
1560
1561void ieee80211_dynamic_ps_timer(unsigned long data)
1562{
1563 struct ieee80211_local *local = (void *) data;
1564
Luis R. Rodriguez78f1a8b2009-07-27 08:38:25 -07001565 if (local->quiescing || local->suspended)
Johannes Berg5bb644a2009-05-17 11:40:42 +02001566 return;
1567
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001568 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
Johannes Berg965beda2009-04-16 13:17:24 +02001569}
1570
Simon Wunderlich164eb022013-02-08 18:16:20 +01001571void ieee80211_dfs_cac_timer_work(struct work_struct *work)
1572{
1573 struct delayed_work *delayed_work =
1574 container_of(work, struct delayed_work, work);
1575 struct ieee80211_sub_if_data *sdata =
1576 container_of(delayed_work, struct ieee80211_sub_if_data,
1577 dfs_cac_timer_work);
1578
1579 ieee80211_vif_release_channel(sdata);
1580
1581 cfg80211_cac_event(sdata->dev, NL80211_RADAR_CAC_FINISHED, GFP_KERNEL);
1582}
1583
Johannes Berg60f8b39c2008-09-08 17:44:22 +02001584/* MLME */
Johannes Berg7d257452012-07-06 17:37:43 +02001585static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
Johannes Berg4ced3f72010-07-19 16:39:04 +02001586 struct ieee80211_sub_if_data *sdata,
Johannes Berg4a3cb702013-02-12 16:43:19 +01001587 const u8 *wmm_param, size_t wmm_param_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001588{
Jiri Bencf0706e82007-05-05 11:45:53 -07001589 struct ieee80211_tx_queue_params params;
Johannes Berg4ced3f72010-07-19 16:39:04 +02001590 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07001591 size_t left;
1592 int count;
Johannes Berg4a3cb702013-02-12 16:43:19 +01001593 const u8 *pos;
1594 u8 uapsd_queues = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001595
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001596 if (!local->ops->conf_tx)
Johannes Berg7d257452012-07-06 17:37:43 +02001597 return false;
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001598
Johannes Berg32c50572012-03-28 11:04:29 +02001599 if (local->hw.queues < IEEE80211_NUM_ACS)
Johannes Berg7d257452012-07-06 17:37:43 +02001600 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001601
1602 if (!wmm_param)
Johannes Berg7d257452012-07-06 17:37:43 +02001603 return false;
Johannes Berg3434fbd2008-05-03 00:59:37 +02001604
Jiri Bencf0706e82007-05-05 11:45:53 -07001605 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
Johannes Berg7d257452012-07-06 17:37:43 +02001606 return false;
Kalle Valoab133152010-01-12 10:42:31 +02001607
1608 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
Eliad Pellerdc41e4d2012-03-14 16:15:03 +02001609 uapsd_queues = ifmgd->uapsd_queues;
Kalle Valoab133152010-01-12 10:42:31 +02001610
Jiri Bencf0706e82007-05-05 11:45:53 -07001611 count = wmm_param[6] & 0x0f;
Johannes Berg46900292009-02-15 12:44:28 +01001612 if (count == ifmgd->wmm_last_param_set)
Johannes Berg7d257452012-07-06 17:37:43 +02001613 return false;
Johannes Berg46900292009-02-15 12:44:28 +01001614 ifmgd->wmm_last_param_set = count;
Jiri Bencf0706e82007-05-05 11:45:53 -07001615
1616 pos = wmm_param + 8;
1617 left = wmm_param_len - 8;
1618
1619 memset(&params, 0, sizeof(params));
1620
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001621 sdata->wmm_acm = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001622 for (; left >= 4; left -= 4, pos += 4) {
1623 int aci = (pos[0] >> 5) & 0x03;
1624 int acm = (pos[0] >> 4) & 0x01;
Kalle Valoab133152010-01-12 10:42:31 +02001625 bool uapsd = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001626 int queue;
1627
1628 switch (aci) {
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001629 case 1: /* AC_BK */
Johannes Berge100bb62008-04-30 18:51:21 +02001630 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +02001631 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001632 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
Kalle Valoab133152010-01-12 10:42:31 +02001633 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1634 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001635 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001636 case 2: /* AC_VI */
Johannes Berge100bb62008-04-30 18:51:21 +02001637 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +02001638 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001639 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
Kalle Valoab133152010-01-12 10:42:31 +02001640 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1641 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001642 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001643 case 3: /* AC_VO */
Johannes Berge100bb62008-04-30 18:51:21 +02001644 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02001645 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001646 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
Kalle Valoab133152010-01-12 10:42:31 +02001647 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1648 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001649 break;
Jouni Malinen0eeb59f2009-03-05 17:23:46 +02001650 case 0: /* AC_BE */
Jiri Bencf0706e82007-05-05 11:45:53 -07001651 default:
Johannes Berge100bb62008-04-30 18:51:21 +02001652 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +02001653 if (acm)
Yoni Divinsky00e96de2012-06-20 15:39:13 +03001654 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
Kalle Valoab133152010-01-12 10:42:31 +02001655 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1656 uapsd = true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001657 break;
1658 }
1659
1660 params.aifs = pos[0] & 0x0f;
1661 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
1662 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +02001663 params.txop = get_unaligned_le16(pos + 2);
Alexander Bondar908f8d02013-04-07 09:53:30 +03001664 params.acm = acm;
Kalle Valoab133152010-01-12 10:42:31 +02001665 params.uapsd = uapsd;
1666
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001667 mlme_dbg(sdata,
1668 "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
1669 queue, aci, acm,
1670 params.aifs, params.cw_min, params.cw_max,
1671 params.txop, params.uapsd);
Eliad Pellerf6f3def2011-09-25 20:06:54 +03001672 sdata->tx_conf[queue] = params;
1673 if (drv_conf_tx(local, sdata, queue, &params))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02001674 sdata_err(sdata,
1675 "failed to set TX queue parameters for queue %d\n",
1676 queue);
Jiri Bencf0706e82007-05-05 11:45:53 -07001677 }
Stanislaw Gruszkae1b3ec12010-03-29 12:18:34 +02001678
1679 /* enable WMM or activate new settings */
Johannes Berg4ced3f72010-07-19 16:39:04 +02001680 sdata->vif.bss_conf.qos = true;
Johannes Berg7d257452012-07-06 17:37:43 +02001681 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07001682}
1683
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001684static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1685{
1686 lockdep_assert_held(&sdata->local->mtx);
1687
1688 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
1689 IEEE80211_STA_BEACON_POLL);
1690 ieee80211_run_deferred_scan(sdata->local);
1691}
1692
1693static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1694{
1695 mutex_lock(&sdata->local->mtx);
1696 __ieee80211_stop_poll(sdata);
1697 mutex_unlock(&sdata->local->mtx);
1698}
1699
Johannes Berg7a5158e2008-10-08 10:59:33 +02001700static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
1701 u16 capab, bool erp_valid, u8 erp)
Daniel Drake56282212007-07-10 19:32:10 +02001702{
Johannes Bergbda39332008-10-11 01:51:51 +02001703 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001704 u32 changed = 0;
Johannes Berg7a5158e2008-10-08 10:59:33 +02001705 bool use_protection;
1706 bool use_short_preamble;
1707 bool use_short_slot;
1708
1709 if (erp_valid) {
1710 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
1711 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
1712 } else {
1713 use_protection = false;
1714 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
1715 }
1716
1717 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
Johannes Berg55de9082012-07-26 17:24:39 +02001718 if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
Felix Fietkau43d35342010-01-15 03:00:48 +01001719 use_short_slot = true;
Daniel Drake56282212007-07-10 19:32:10 +02001720
Johannes Berg471b3ef2007-12-28 14:32:58 +01001721 if (use_protection != bss_conf->use_cts_prot) {
Johannes Berg471b3ef2007-12-28 14:32:58 +01001722 bss_conf->use_cts_prot = use_protection;
1723 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +02001724 }
Daniel Drake7e9ed182007-07-27 15:43:24 +02001725
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001726 if (use_short_preamble != bss_conf->use_short_preamble) {
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +02001727 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001728 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +02001729 }
Daniel Draked9430a32007-07-27 15:43:24 +02001730
Johannes Berg7a5158e2008-10-08 10:59:33 +02001731 if (use_short_slot != bss_conf->use_short_slot) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02001732 bss_conf->use_short_slot = use_short_slot;
1733 changed |= BSS_CHANGED_ERP_SLOT;
John W. Linville50c4afb2008-04-15 14:09:27 -04001734 }
1735
1736 return changed;
1737}
1738
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001739static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001740 struct cfg80211_bss *cbss,
Johannes Bergae5eb022008-10-14 16:58:37 +02001741 u32 bss_info_changed)
Jiri Bencf0706e82007-05-05 11:45:53 -07001742{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001743 struct ieee80211_bss *bss = (void *)cbss->priv;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001744 struct ieee80211_local *local = sdata->local;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001745 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001746
Johannes Bergae5eb022008-10-14 16:58:37 +02001747 bss_info_changed |= BSS_CHANGED_ASSOC;
Johannes Berg77fdaa12009-07-07 03:45:17 +02001748 bss_info_changed |= ieee80211_handle_bss_capability(sdata,
Luciano Coelho50ae34a2012-06-20 17:23:24 +03001749 bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07001750
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001751 sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
Ben Greear59c1ec22013-03-19 14:19:56 -07001752 beacon_loss_count * bss_conf->beacon_int));
Felix Fietkau7ccc8bd2010-11-19 22:55:38 +01001753
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01001754 sdata->u.mgd.associated = cbss;
1755 memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
Johannes Berg471b3ef2007-12-28 14:32:58 +01001756
Jouni Malinen17e4ec12010-03-29 23:28:30 -07001757 sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
1758
Johannes Berg488dd7b2012-10-29 20:08:01 +01001759 if (sdata->vif.p2p) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001760 const struct cfg80211_bss_ies *ies;
Johannes Berg488dd7b2012-10-29 20:08:01 +01001761
Johannes Berg9caf0362012-11-29 01:25:20 +01001762 rcu_read_lock();
1763 ies = rcu_dereference(cbss->ies);
1764 if (ies) {
Johannes Berg9caf0362012-11-29 01:25:20 +01001765 int ret;
1766
1767 ret = cfg80211_get_p2p_attr(
1768 ies->data, ies->len,
1769 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001770 (u8 *) &bss_conf->p2p_noa_attr,
1771 sizeof(bss_conf->p2p_noa_attr));
Johannes Berg9caf0362012-11-29 01:25:20 +01001772 if (ret >= 2) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001773 sdata->u.mgd.p2p_noa_index =
1774 bss_conf->p2p_noa_attr.index;
Johannes Berg9caf0362012-11-29 01:25:20 +01001775 bss_info_changed |= BSS_CHANGED_P2P_PS;
Johannes Berg9caf0362012-11-29 01:25:20 +01001776 }
Johannes Berg488dd7b2012-10-29 20:08:01 +01001777 }
Johannes Berg9caf0362012-11-29 01:25:20 +01001778 rcu_read_unlock();
Johannes Berg488dd7b2012-10-29 20:08:01 +01001779 }
1780
Johannes Bergb291ba12009-07-10 15:29:03 +02001781 /* just to be sure */
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001782 ieee80211_stop_poll(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02001783
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001784 ieee80211_led_assoc(local, 1);
1785
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001786 if (sdata->u.mgd.assoc_data->have_beacon) {
Johannes Berg826262c2012-12-10 16:38:14 +02001787 /*
1788 * If the AP is buggy we may get here with no DTIM period
1789 * known, so assume it's 1 which is the only safe assumption
1790 * in that case, although if the TIM IE is broken powersave
1791 * probably just won't work at all.
1792 */
1793 bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02001794 bss_info_changed |= BSS_CHANGED_DTIM_PERIOD;
Johannes Berg826262c2012-12-10 16:38:14 +02001795 } else {
Johannes Berge5b900d2010-07-29 16:08:55 +02001796 bss_conf->dtim_period = 0;
Johannes Berg826262c2012-12-10 16:38:14 +02001797 }
Johannes Berge5b900d2010-07-29 16:08:55 +02001798
Juuso Oikarinen68542962010-06-09 13:43:26 +03001799 bss_conf->assoc = 1;
Johannes Berg9cef8732009-05-14 13:10:14 +02001800
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001801 /* Tell the driver to monitor connection quality (if supported) */
Johannes Bergea086352012-01-19 09:29:58 +01001802 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
Juuso Oikarinen68542962010-06-09 13:43:26 +03001803 bss_conf->cqm_rssi_thold)
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02001804 bss_info_changed |= BSS_CHANGED_CQM;
1805
Juuso Oikarinen68542962010-06-09 13:43:26 +03001806 /* Enable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001807 if (bss_conf->arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001808 bss_info_changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001809
Johannes Bergae5eb022008-10-14 16:58:37 +02001810 ieee80211_bss_info_change_notify(sdata, bss_info_changed);
Guy Cohen8db93692008-07-03 19:56:13 +03001811
Johannes Berg056508d2009-07-30 21:43:55 +02001812 mutex_lock(&local->iflist_mtx);
1813 ieee80211_recalc_ps(local, -1);
1814 mutex_unlock(&local->iflist_mtx);
Kalle Valoe0cb6862008-12-18 23:35:13 +02001815
Johannes Berg04ecd252012-09-11 14:34:12 +02001816 ieee80211_recalc_smps(sdata);
Eliad Pellerab095872012-07-27 12:33:22 +03001817 ieee80211_recalc_ps_vif(sdata);
1818
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001819 netif_carrier_on(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001820}
1821
Jouni Malinene69e95d2010-03-29 23:29:31 -07001822static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg37ad3882012-02-24 13:50:54 +01001823 u16 stype, u16 reason, bool tx,
1824 u8 *frame_buf)
Tomas Winkleraa458d12008-09-09 00:32:12 +03001825{
Johannes Berg46900292009-02-15 12:44:28 +01001826 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001827 struct ieee80211_local *local = sdata->local;
Johannes Berg3cc52402012-03-09 13:12:35 +01001828 u32 changed = 0;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001829
Johannes Berg77fdaa12009-07-07 03:45:17 +02001830 ASSERT_MGD_MTX(ifmgd);
1831
Johannes Berg37ad3882012-02-24 13:50:54 +01001832 if (WARN_ON_ONCE(tx && !frame_buf))
1833 return;
1834
Johannes Bergb291ba12009-07-10 15:29:03 +02001835 if (WARN_ON(!ifmgd->associated))
1836 return;
1837
David Spinadel79543d82012-06-12 09:59:45 +03001838 ieee80211_stop_poll(sdata);
1839
Johannes Berg77fdaa12009-07-07 03:45:17 +02001840 ifmgd->associated = NULL;
Tomas Winkleraa458d12008-09-09 00:32:12 +03001841 netif_carrier_off(sdata->dev);
1842
Eliad Peller88bc40e2012-07-12 17:35:33 +03001843 /*
1844 * if we want to get out of ps before disassoc (why?) we have
1845 * to do it before sending disassoc, as otherwise the null-packet
1846 * won't be valid.
1847 */
1848 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
1849 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
1850 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1851 }
1852 local->ps_sdata = NULL;
1853
Eliad Pellerab095872012-07-27 12:33:22 +03001854 /* disable per-vif ps */
1855 ieee80211_recalc_ps_vif(sdata);
1856
Eliad Pellerf8239812012-06-27 14:18:22 +03001857 /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
1858 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001859 ieee80211_flush_queues(local, sdata);
Eliad Pellerf8239812012-06-27 14:18:22 +03001860
Johannes Berg37ad3882012-02-24 13:50:54 +01001861 /* deauthenticate/disassociate now */
1862 if (tx || frame_buf)
Eliad Peller88a9e312012-06-01 11:14:03 +03001863 ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
1864 reason, tx, frame_buf);
Johannes Berg37ad3882012-02-24 13:50:54 +01001865
1866 /* flush out frame */
1867 if (tx)
Johannes Berg39ecc012013-02-13 12:11:00 +01001868 ieee80211_flush_queues(local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001869
Eliad Peller88a9e312012-06-01 11:14:03 +03001870 /* clear bssid only after building the needed mgmt frames */
1871 memset(ifmgd->bssid, 0, ETH_ALEN);
1872
Johannes Berg37ad3882012-02-24 13:50:54 +01001873 /* remove AP and TDLS peers */
Johannes Berg051007d2012-12-13 23:49:02 +01001874 sta_info_flush_defer(sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01001875
1876 /* finally reset all BSS / config parameters */
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001877 changed |= ieee80211_reset_erp_info(sdata);
1878
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001879 ieee80211_led_assoc(local, 0);
Johannes Bergae5eb022008-10-14 16:58:37 +02001880 changed |= BSS_CHANGED_ASSOC;
1881 sdata->vif.bss_conf.assoc = false;
Tomas Winklerf5e5bf22008-09-08 17:33:39 +02001882
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001883 ifmgd->p2p_noa_index = -1;
1884 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
1885 sizeof(sdata->vif.bss_conf.p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01001886
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001887 /* on the next assoc, re-program HT/VHT parameters */
Ben Greearef96a8422011-11-18 11:32:00 -08001888 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
1889 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01001890 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
1891 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
Johannes Berg413ad502009-05-08 21:21:06 +02001892
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02001893 sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
Vasanthakumar Thiagarajana8302de2009-01-09 18:14:15 +05301894
Kalle Valo520eb822008-12-18 23:35:27 +02001895 del_timer_sync(&local->dynamic_ps_timer);
1896 cancel_work_sync(&local->dynamic_ps_enable_work);
1897
Juuso Oikarinen68542962010-06-09 13:43:26 +03001898 /* Disable ARP filtering */
Johannes Berg0f19b412013-01-14 16:39:07 +01001899 if (sdata->vif.bss_conf.arp_addr_cnt)
Juuso Oikarinen68542962010-06-09 13:43:26 +03001900 changed |= BSS_CHANGED_ARP_FILTER;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001901
Johannes Berg3abead52012-03-02 15:56:59 +01001902 sdata->vif.bss_conf.qos = false;
1903 changed |= BSS_CHANGED_QOS;
1904
Johannes Berg0aaffa92010-05-05 15:28:27 +02001905 /* The BSSID (not really interesting) and HT changed */
1906 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
Johannes Bergae5eb022008-10-14 16:58:37 +02001907 ieee80211_bss_info_change_notify(sdata, changed);
Tomas Winkler8e268e42008-11-25 13:05:44 +02001908
Johannes Berg3abead52012-03-02 15:56:59 +01001909 /* disassociated - set to defaults now */
1910 ieee80211_set_wmm_default(sdata, false);
1911
Johannes Bergb9dcf712010-08-27 12:35:54 +02001912 del_timer_sync(&sdata->u.mgd.conn_mon_timer);
1913 del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
1914 del_timer_sync(&sdata->u.mgd.timer);
1915 del_timer_sync(&sdata->u.mgd.chswitch_timer);
Johannes Berg2d9957c2012-08-01 20:54:52 +02001916
Johannes Berg826262c2012-12-10 16:38:14 +02001917 sdata->vif.bss_conf.dtim_period = 0;
1918
Johannes Berg028e8da02012-11-26 11:57:41 +01001919 ifmgd->flags = 0;
1920 ieee80211_vif_release_channel(sdata);
Tomas Winkleraa458d12008-09-09 00:32:12 +03001921}
Jiri Bencf0706e82007-05-05 11:45:53 -07001922
Kalle Valo3cf335d52009-03-22 21:57:06 +02001923void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
1924 struct ieee80211_hdr *hdr)
1925{
1926 /*
1927 * We can postpone the mgd.timer whenever receiving unicast frames
1928 * from AP because we know that the connection is working both ways
1929 * at that time. But multicast frames (and hence also beacons) must
1930 * be ignored here, because we need to trigger the timer during
Johannes Bergb291ba12009-07-10 15:29:03 +02001931 * data idle periods for sending the periodic probe request to the
1932 * AP we're connected to.
Kalle Valo3cf335d52009-03-22 21:57:06 +02001933 */
Johannes Bergb291ba12009-07-10 15:29:03 +02001934 if (is_multicast_ether_addr(hdr->addr1))
1935 return;
1936
Luis R. Rodriguezbe099e82010-09-16 15:12:29 -04001937 ieee80211_sta_reset_conn_monitor(sdata);
Kalle Valo3cf335d52009-03-22 21:57:06 +02001938}
Jiri Bencf0706e82007-05-05 11:45:53 -07001939
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001940static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1941{
1942 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001943 struct ieee80211_local *local = sdata->local;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001944
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001945 mutex_lock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001946 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001947 IEEE80211_STA_CONNECTION_POLL))) {
1948 mutex_unlock(&local->mtx);
1949 return;
1950 }
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001951
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02001952 __ieee80211_stop_poll(sdata);
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001953
1954 mutex_lock(&local->iflist_mtx);
1955 ieee80211_recalc_ps(local, -1);
1956 mutex_unlock(&local->iflist_mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001957
1958 if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001959 goto out;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001960
1961 /*
1962 * We've received a probe response, but are not sure whether
1963 * we have or will be receiving any beacons or data, so let's
1964 * schedule the timers again, just in case.
1965 */
1966 ieee80211_sta_reset_beacon_monitor(sdata);
1967
1968 mod_timer(&ifmgd->conn_mon_timer,
1969 round_jiffies_up(jiffies +
1970 IEEE80211_CONNECTION_IDLE_TIME));
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001971out:
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02001972 mutex_unlock(&local->mtx);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001973}
1974
1975void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001976 struct ieee80211_hdr *hdr, bool ack)
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001977{
Felix Fietkau75706d02010-12-02 21:01:07 +01001978 if (!ieee80211_is_data(hdr->frame_control))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001979 return;
1980
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001981 if (ieee80211_is_nullfunc(hdr->frame_control) &&
1982 sdata->u.mgd.probe_send_count > 0) {
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001983 if (ack)
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001984 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01001985 else
1986 sdata->u.mgd.nullfunc_failed = true;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001987 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001988 return;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001989 }
Wojciech Dubowikcab1c7f2013-02-14 14:08:37 +01001990
1991 if (ack)
1992 ieee80211_sta_reset_conn_monitor(sdata);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01001993}
1994
Maxim Levitskya43abf22009-07-31 18:54:12 +03001995static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
1996{
1997 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1998 const u8 *ssid;
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04001999 u8 *dst = ifmgd->associated->bssid;
Ben Greear180205b2011-02-04 15:30:24 -08002000 u8 unicast_limit = max(1, max_probe_tries - 3);
Luis R. Rodriguezf01a0672010-09-16 15:12:34 -04002001
2002 /*
2003 * Try sending broadcast probe requests for the last three
2004 * probe requests after the first ones failed since some
2005 * buggy APs only support broadcast probe requests.
2006 */
2007 if (ifmgd->probe_send_count >= unicast_limit)
2008 dst = NULL;
Maxim Levitskya43abf22009-07-31 18:54:12 +03002009
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002010 /*
2011 * When the hardware reports an accurate Tx ACK status, it's
2012 * better to send a nullfunc frame instead of a probe request,
2013 * as it will kick us off the AP quickly if we aren't associated
2014 * anymore. The timeout will be reset if the frame is ACKed by
2015 * the AP.
2016 */
Soumik Das992e68b2012-05-20 15:31:13 +05302017 ifmgd->probe_send_count++;
2018
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002019 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
2020 ifmgd->nullfunc_failed = false;
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002021 ieee80211_send_nullfunc(sdata->local, sdata, 0);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01002022 } else {
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002023 int ssid_len;
2024
Johannes Berg9caf0362012-11-29 01:25:20 +01002025 rcu_read_lock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002026 ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002027 if (WARN_ON_ONCE(ssid == NULL))
2028 ssid_len = 0;
2029 else
2030 ssid_len = ssid[1];
2031
2032 ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002033 0, (u32) -1, true, 0,
Johannes Berg55de9082012-07-26 17:24:39 +02002034 ifmgd->associated->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01002035 rcu_read_unlock();
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002036 }
Maxim Levitskya43abf22009-07-31 18:54:12 +03002037
Ben Greear180205b2011-02-04 15:30:24 -08002038 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002039 run_again(ifmgd, ifmgd->probe_timeout);
Rajkumar Manoharanf69b9c72012-03-15 06:15:26 +05302040 if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Johannes Berg39ecc012013-02-13 12:11:00 +01002041 ieee80211_flush_queues(sdata->local, sdata);
Maxim Levitskya43abf22009-07-31 18:54:12 +03002042}
2043
Johannes Bergb291ba12009-07-10 15:29:03 +02002044static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2045 bool beacon)
Kalle Valo04de8382009-03-22 21:57:35 +02002046{
Kalle Valo04de8382009-03-22 21:57:35 +02002047 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02002048 bool already = false;
Johannes Berg34bfc412009-05-12 19:58:12 +02002049
Johannes Berg9607e6b662009-12-23 13:15:31 +01002050 if (!ieee80211_sdata_running(sdata))
Johannes Berg0e2b6282009-07-13 13:23:39 +02002051 return;
2052
Johannes Berg77fdaa12009-07-07 03:45:17 +02002053 mutex_lock(&ifmgd->mtx);
2054
2055 if (!ifmgd->associated)
2056 goto out;
2057
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002058 mutex_lock(&sdata->local->mtx);
2059
2060 if (sdata->local->tmp_channel || sdata->local->scanning) {
2061 mutex_unlock(&sdata->local->mtx);
2062 goto out;
2063 }
2064
Ben Greeara13fbe52013-03-25 11:19:35 -07002065 if (beacon) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002066 mlme_dbg_ratelimited(sdata,
Ben Greear59c1ec22013-03-19 14:19:56 -07002067 "detected beacon loss from AP (missed %d beacons) - probing\n",
2068 beacon_loss_count);
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002069
Ben Greeara13fbe52013-03-25 11:19:35 -07002070 ieee80211_cqm_rssi_notify(&sdata->vif,
2071 NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
2072 GFP_KERNEL);
2073 }
Kalle Valo04de8382009-03-22 21:57:35 +02002074
Johannes Bergb291ba12009-07-10 15:29:03 +02002075 /*
2076 * The driver/our work has already reported this event or the
2077 * connection monitoring has kicked in and we have already sent
2078 * a probe request. Or maybe the AP died and the driver keeps
2079 * reporting until we disassociate...
2080 *
2081 * In either case we have to ignore the current call to this
2082 * function (except for setting the correct probe reason bit)
2083 * because otherwise we would reset the timer every time and
2084 * never check whether we received a probe response!
2085 */
2086 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
2087 IEEE80211_STA_CONNECTION_POLL))
2088 already = true;
2089
2090 if (beacon)
2091 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2092 else
2093 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2094
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +02002095 mutex_unlock(&sdata->local->mtx);
2096
Johannes Bergb291ba12009-07-10 15:29:03 +02002097 if (already)
2098 goto out;
2099
Johannes Berg4e751842009-06-10 15:16:52 +02002100 mutex_lock(&sdata->local->iflist_mtx);
2101 ieee80211_recalc_ps(sdata->local, -1);
2102 mutex_unlock(&sdata->local->iflist_mtx);
2103
Maxim Levitskya43abf22009-07-31 18:54:12 +03002104 ifmgd->probe_send_count = 0;
2105 ieee80211_mgd_probe_ap_send(sdata);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002106 out:
2107 mutex_unlock(&ifmgd->mtx);
Kalle Valo04de8382009-03-22 21:57:35 +02002108}
2109
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002110struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2111 struct ieee80211_vif *vif)
2112{
2113 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2114 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002115 struct cfg80211_bss *cbss;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002116 struct sk_buff *skb;
2117 const u8 *ssid;
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002118 int ssid_len;
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002119
2120 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2121 return NULL;
2122
2123 ASSERT_MGD_MTX(ifmgd);
2124
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002125 if (ifmgd->associated)
2126 cbss = ifmgd->associated;
2127 else if (ifmgd->auth_data)
2128 cbss = ifmgd->auth_data->bss;
2129 else if (ifmgd->assoc_data)
2130 cbss = ifmgd->assoc_data->bss;
2131 else
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002132 return NULL;
2133
Johannes Berg9caf0362012-11-29 01:25:20 +01002134 rcu_read_lock();
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002135 ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
Stanislaw Gruszka88c868c2012-03-29 16:30:41 +02002136 if (WARN_ON_ONCE(ssid == NULL))
2137 ssid_len = 0;
2138 else
2139 ssid_len = ssid[1];
2140
Eliad Pellerd9b3b282012-06-28 15:03:13 +03002141 skb = ieee80211_build_probe_req(sdata, cbss->bssid,
Johannes Berg55de9082012-07-26 17:24:39 +02002142 (u32) -1, cbss->channel,
Johannes Berg6b778632012-07-23 14:53:27 +02002143 ssid + 2, ssid_len,
Johannes Berg85a237f2011-07-18 18:08:36 +02002144 NULL, 0, true);
Johannes Berg9caf0362012-11-29 01:25:20 +01002145 rcu_read_unlock();
Juuso Oikarinena619a4c2010-11-11 08:50:18 +02002146
2147 return skb;
2148}
2149EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2150
Johannes Bergeef9e542013-01-29 13:09:34 +01002151static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002152{
2153 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002154 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002155
2156 mutex_lock(&ifmgd->mtx);
2157 if (!ifmgd->associated) {
2158 mutex_unlock(&ifmgd->mtx);
2159 return;
2160 }
2161
Johannes Berg37ad3882012-02-24 13:50:54 +01002162 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
2163 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
Johannes Bergeef9e542013-01-29 13:09:34 +01002164 true, frame_buf);
Johannes Berg882a7c62012-08-01 22:32:45 +02002165 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002166 ieee80211_wake_queues_by_reason(&sdata->local->hw,
Johannes Berg445ea4e2013-02-13 12:25:28 +01002167 IEEE80211_MAX_QUEUE_MAP,
Johannes Berg5b36ebd2013-02-13 14:21:45 +01002168 IEEE80211_QUEUE_STOP_REASON_CSA);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002169 mutex_unlock(&ifmgd->mtx);
Johannes Berg7da7cc12010-08-05 17:02:38 +02002170
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002171 /*
2172 * must be outside lock due to cfg80211,
2173 * but that's not a problem.
2174 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02002175 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002176}
2177
Johannes Bergcc74c0c2012-08-01 16:49:34 +02002178static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
Johannes Bergb291ba12009-07-10 15:29:03 +02002179{
2180 struct ieee80211_sub_if_data *sdata =
2181 container_of(work, struct ieee80211_sub_if_data,
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002182 u.mgd.beacon_connection_loss_work);
Paul Stewarta85e1d52011-12-09 11:01:49 -08002183 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2184 struct sta_info *sta;
2185
2186 if (ifmgd->associated) {
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302187 rcu_read_lock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002188 sta = sta_info_get(sdata, ifmgd->bssid);
2189 if (sta)
2190 sta->beacon_loss_count++;
Mohammed Shafi Shajakhan30fa9042011-12-27 18:54:07 +05302191 rcu_read_unlock();
Paul Stewarta85e1d52011-12-09 11:01:49 -08002192 }
Johannes Bergb291ba12009-07-10 15:29:03 +02002193
Johannes Berg682bd382013-01-29 13:13:50 +01002194 if (ifmgd->connection_loss) {
Johannes Berg882a7c62012-08-01 22:32:45 +02002195 sdata_info(sdata, "Connection to AP %pM lost\n",
2196 ifmgd->bssid);
Johannes Bergeef9e542013-01-29 13:09:34 +01002197 __ieee80211_disconnect(sdata);
Johannes Berg882a7c62012-08-01 22:32:45 +02002198 } else {
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002199 ieee80211_mgd_probe_ap(sdata, true);
Johannes Berg882a7c62012-08-01 22:32:45 +02002200 }
2201}
2202
2203static void ieee80211_csa_connection_drop_work(struct work_struct *work)
2204{
2205 struct ieee80211_sub_if_data *sdata =
2206 container_of(work, struct ieee80211_sub_if_data,
2207 u.mgd.csa_connection_drop_work);
2208
Johannes Bergeef9e542013-01-29 13:09:34 +01002209 __ieee80211_disconnect(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02002210}
2211
Kalle Valo04de8382009-03-22 21:57:35 +02002212void ieee80211_beacon_loss(struct ieee80211_vif *vif)
2213{
2214 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002215 struct ieee80211_hw *hw = &sdata->local->hw;
Kalle Valo04de8382009-03-22 21:57:35 +02002216
Johannes Bergb5878a22010-04-07 16:48:40 +02002217 trace_api_beacon_loss(sdata);
2218
Johannes Berg682bd382013-01-29 13:13:50 +01002219 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002220 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
Kalle Valo04de8382009-03-22 21:57:35 +02002221}
2222EXPORT_SYMBOL(ieee80211_beacon_loss);
2223
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002224void ieee80211_connection_loss(struct ieee80211_vif *vif)
2225{
2226 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2227 struct ieee80211_hw *hw = &sdata->local->hw;
2228
Johannes Bergb5878a22010-04-07 16:48:40 +02002229 trace_api_connection_loss(sdata);
2230
Johannes Berg682bd382013-01-29 13:13:50 +01002231 sdata->u.mgd.connection_loss = true;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02002232 ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
2233}
2234EXPORT_SYMBOL(ieee80211_connection_loss);
2235
2236
Johannes Berg66e67e42012-01-20 13:55:27 +01002237static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2238 bool assoc)
2239{
2240 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2241
2242 lockdep_assert_held(&sdata->u.mgd.mtx);
2243
Johannes Berg66e67e42012-01-20 13:55:27 +01002244 if (!assoc) {
2245 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2246
2247 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2248 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002249 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002250 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002251 }
2252
Johannes Berg5b112d32013-02-01 01:49:58 +01002253 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002254 kfree(auth_data);
2255 sdata->u.mgd.auth_data = NULL;
2256}
2257
2258static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
2259 struct ieee80211_mgmt *mgmt, size_t len)
2260{
Johannes Berg1672c0e32013-01-29 15:02:27 +01002261 struct ieee80211_local *local = sdata->local;
Johannes Berg66e67e42012-01-20 13:55:27 +01002262 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
2263 u8 *pos;
2264 struct ieee802_11_elems elems;
Johannes Berg1672c0e32013-01-29 15:02:27 +01002265 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01002266
2267 pos = mgmt->u.auth.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002268 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002269 if (!elems.challenge)
2270 return;
2271 auth_data->expected_transaction = 4;
Johannes Berga1845fc2012-06-27 13:18:36 +02002272 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg1672c0e32013-01-29 15:02:27 +01002273 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
2274 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2275 IEEE80211_TX_INTFL_MLME_CONN_TX;
Jouni Malinen700e8ea2012-09-30 19:29:37 +03002276 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
Johannes Berg66e67e42012-01-20 13:55:27 +01002277 elems.challenge - 2, elems.challenge_len + 2,
2278 auth_data->bss->bssid, auth_data->bss->bssid,
2279 auth_data->key, auth_data->key_len,
Johannes Berg1672c0e32013-01-29 15:02:27 +01002280 auth_data->key_idx, tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01002281}
2282
2283static enum rx_mgmt_action __must_check
2284ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2285 struct ieee80211_mgmt *mgmt, size_t len)
2286{
2287 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2288 u8 bssid[ETH_ALEN];
2289 u16 auth_alg, auth_transaction, status_code;
2290 struct sta_info *sta;
2291
2292 lockdep_assert_held(&ifmgd->mtx);
2293
2294 if (len < 24 + 6)
2295 return RX_MGMT_NONE;
2296
2297 if (!ifmgd->auth_data || ifmgd->auth_data->done)
2298 return RX_MGMT_NONE;
2299
2300 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
2301
Joe Perchesb203ca32012-05-08 18:56:52 +00002302 if (!ether_addr_equal(bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002303 return RX_MGMT_NONE;
2304
2305 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
2306 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
2307 status_code = le16_to_cpu(mgmt->u.auth.status_code);
2308
2309 if (auth_alg != ifmgd->auth_data->algorithm ||
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002310 auth_transaction != ifmgd->auth_data->expected_transaction) {
2311 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
2312 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
2313 auth_transaction,
2314 ifmgd->auth_data->expected_transaction);
Johannes Berg66e67e42012-01-20 13:55:27 +01002315 return RX_MGMT_NONE;
Jouni Malinen0f4126e2012-09-30 19:29:38 +03002316 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002317
2318 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002319 sdata_info(sdata, "%pM denied authentication (status %d)\n",
2320 mgmt->sa, status_code);
Eliad Pellerdac211e2012-05-13 18:07:04 +03002321 ieee80211_destroy_auth_data(sdata, false);
2322 return RX_MGMT_CFG80211_RX_AUTH;
Johannes Berg66e67e42012-01-20 13:55:27 +01002323 }
2324
2325 switch (ifmgd->auth_data->algorithm) {
2326 case WLAN_AUTH_OPEN:
2327 case WLAN_AUTH_LEAP:
2328 case WLAN_AUTH_FT:
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002329 case WLAN_AUTH_SAE:
Johannes Berg66e67e42012-01-20 13:55:27 +01002330 break;
2331 case WLAN_AUTH_SHARED_KEY:
2332 if (ifmgd->auth_data->expected_transaction != 4) {
2333 ieee80211_auth_challenge(sdata, mgmt, len);
2334 /* need another frame */
2335 return RX_MGMT_NONE;
2336 }
2337 break;
2338 default:
2339 WARN_ONCE(1, "invalid auth alg %d",
2340 ifmgd->auth_data->algorithm);
2341 return RX_MGMT_NONE;
2342 }
2343
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002344 sdata_info(sdata, "authenticated\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002345 ifmgd->auth_data->done = true;
2346 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
Johannes Berg89afe612013-02-13 15:39:57 +01002347 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002348 run_again(ifmgd, ifmgd->auth_data->timeout);
2349
Jouni Malinen6b8ece32012-09-30 19:29:40 +03002350 if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
2351 ifmgd->auth_data->expected_transaction != 2) {
2352 /*
2353 * Report auth frame to user space for processing since another
2354 * round of Authentication frames is still needed.
2355 */
2356 return RX_MGMT_CFG80211_RX_AUTH;
2357 }
2358
Johannes Berg66e67e42012-01-20 13:55:27 +01002359 /* move station state to auth */
2360 mutex_lock(&sdata->local->sta_mtx);
2361 sta = sta_info_get(sdata, bssid);
2362 if (!sta) {
2363 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
2364 goto out_err;
2365 }
2366 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002367 sdata_info(sdata, "failed moving %pM to auth\n", bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01002368 goto out_err;
2369 }
2370 mutex_unlock(&sdata->local->sta_mtx);
2371
2372 return RX_MGMT_CFG80211_RX_AUTH;
2373 out_err:
2374 mutex_unlock(&sdata->local->sta_mtx);
2375 /* ignore frame -- wait for timeout */
2376 return RX_MGMT_NONE;
2377}
2378
2379
Johannes Berg77fdaa12009-07-07 03:45:17 +02002380static enum rx_mgmt_action __must_check
2381ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg77fdaa12009-07-07 03:45:17 +02002382 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002383{
Johannes Berg46900292009-02-15 12:44:28 +01002384 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002385 const u8 *bssid = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002386 u16 reason_code;
2387
Johannes Berg66e67e42012-01-20 13:55:27 +01002388 lockdep_assert_held(&ifmgd->mtx);
2389
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002390 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002391 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002392
Johannes Berg66e67e42012-01-20 13:55:27 +01002393 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002394 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002395 return RX_MGMT_NONE;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002396
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002397 bssid = ifmgd->associated->bssid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002398
2399 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
2400
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002401 sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
2402 bssid, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002403
Johannes Berg37ad3882012-02-24 13:50:54 +01002404 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2405
Johannes Berg77fdaa12009-07-07 03:45:17 +02002406 return RX_MGMT_CFG80211_DEAUTH;
Jiri Bencf0706e82007-05-05 11:45:53 -07002407}
2408
2409
Johannes Berg77fdaa12009-07-07 03:45:17 +02002410static enum rx_mgmt_action __must_check
2411ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2412 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002413{
Johannes Berg46900292009-02-15 12:44:28 +01002414 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07002415 u16 reason_code;
2416
Johannes Berg66e67e42012-01-20 13:55:27 +01002417 lockdep_assert_held(&ifmgd->mtx);
2418
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002419 if (len < 24 + 2)
Johannes Berg77fdaa12009-07-07 03:45:17 +02002420 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002421
Johannes Berg66e67e42012-01-20 13:55:27 +01002422 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002423 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg77fdaa12009-07-07 03:45:17 +02002424 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002425
2426 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2427
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002428 sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
2429 mgmt->sa, reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002430
Johannes Berg37ad3882012-02-24 13:50:54 +01002431 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2432
Johannes Berg77fdaa12009-07-07 03:45:17 +02002433 return RX_MGMT_CFG80211_DISASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -07002434}
2435
Christian Lamparterc74d0842011-10-15 00:14:49 +02002436static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
2437 u8 *supp_rates, unsigned int supp_rates_len,
2438 u32 *rates, u32 *basic_rates,
2439 bool *have_higher_than_11mbit,
2440 int *min_rate, int *min_rate_index)
2441{
2442 int i, j;
2443
2444 for (i = 0; i < supp_rates_len; i++) {
2445 int rate = (supp_rates[i] & 0x7f) * 5;
2446 bool is_basic = !!(supp_rates[i] & 0x80);
2447
2448 if (rate > 110)
2449 *have_higher_than_11mbit = true;
2450
2451 /*
2452 * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
2453 * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
2454 *
2455 * Note: Even through the membership selector and the basic
2456 * rate flag share the same bit, they are not exactly
2457 * the same.
2458 */
2459 if (!!(supp_rates[i] & 0x80) &&
2460 (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
2461 continue;
2462
2463 for (j = 0; j < sband->n_bitrates; j++) {
2464 if (sband->bitrates[j].bitrate == rate) {
2465 *rates |= BIT(j);
2466 if (is_basic)
2467 *basic_rates |= BIT(j);
2468 if (rate < *min_rate) {
2469 *min_rate = rate;
2470 *min_rate_index = j;
2471 }
2472 break;
2473 }
2474 }
2475 }
2476}
Jiri Bencf0706e82007-05-05 11:45:53 -07002477
Johannes Berg66e67e42012-01-20 13:55:27 +01002478static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2479 bool assoc)
2480{
2481 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
2482
2483 lockdep_assert_held(&sdata->u.mgd.mtx);
2484
Johannes Berg66e67e42012-01-20 13:55:27 +01002485 if (!assoc) {
2486 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2487
2488 memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
2489 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg028e8da02012-11-26 11:57:41 +01002490 sdata->u.mgd.flags = 0;
Johannes Berg55de9082012-07-26 17:24:39 +02002491 ieee80211_vif_release_channel(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002492 }
2493
2494 kfree(assoc_data);
2495 sdata->u.mgd.assoc_data = NULL;
2496}
2497
2498static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2499 struct cfg80211_bss *cbss,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002500 struct ieee80211_mgmt *mgmt, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002501{
Johannes Berg46900292009-02-15 12:44:28 +01002502 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002503 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002504 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002505 struct sta_info *sta;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002506 u8 *pos;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002507 u16 capab_info, aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002508 struct ieee802_11_elems elems;
Johannes Bergbda39332008-10-11 01:51:51 +02002509 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Johannes Bergae5eb022008-10-14 16:58:37 +02002510 u32 changed = 0;
Christian Lamparterc74d0842011-10-15 00:14:49 +02002511 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -07002512
Johannes Bergaf6b6372009-12-23 13:15:35 +01002513 /* AssocResp and ReassocResp have identical structure */
Jiri Bencf0706e82007-05-05 11:45:53 -07002514
Jiri Bencf0706e82007-05-05 11:45:53 -07002515 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002516 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002517
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002518 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002519 sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
2520 aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002521 aid &= ~(BIT(15) | BIT(14));
2522
Johannes Berg05cb9102011-10-28 11:59:47 +02002523 ifmgd->broken_ap = false;
2524
2525 if (aid == 0 || aid > IEEE80211_MAX_AID) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002526 sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
2527 aid);
Johannes Berg05cb9102011-10-28 11:59:47 +02002528 aid = 0;
2529 ifmgd->broken_ap = true;
2530 }
2531
Johannes Bergaf6b6372009-12-23 13:15:35 +01002532 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002533 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002534
Jiri Bencf0706e82007-05-05 11:45:53 -07002535 if (!elems.supp_rates) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002536 sdata_info(sdata, "no SuppRates element in AssocResp\n");
Johannes Bergaf6b6372009-12-23 13:15:35 +01002537 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002538 }
2539
Johannes Berg46900292009-02-15 12:44:28 +01002540 ifmgd->aid = aid;
Jiri Bencf0706e82007-05-05 11:45:53 -07002541
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002542 /*
2543 * We previously checked these in the beacon/probe response, so
2544 * they should be present here. This is just a safety net.
2545 */
2546 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
2547 (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
2548 sdata_info(sdata,
2549 "HT AP is missing WMM params or HT capability/operation in AssocResp\n");
2550 return false;
2551 }
2552
2553 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
2554 (!elems.vht_cap_elem || !elems.vht_operation)) {
2555 sdata_info(sdata,
2556 "VHT AP is missing VHT capability/operation in AssocResp\n");
2557 return false;
2558 }
2559
Guy Eilam2a33bee2011-08-17 15:18:15 +03002560 mutex_lock(&sdata->local->sta_mtx);
2561 /*
2562 * station info was already allocated and inserted before
2563 * the association and should be available to us
2564 */
Johannes Berg7852e362012-01-20 13:55:24 +01002565 sta = sta_info_get(sdata, cbss->bssid);
Guy Eilam2a33bee2011-08-17 15:18:15 +03002566 if (WARN_ON(!sta)) {
2567 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergaf6b6372009-12-23 13:15:35 +01002568 return false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002569 }
2570
Johannes Berg55de9082012-07-26 17:24:39 +02002571 sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
Johannes Berg8318d782008-01-24 19:38:38 +01002572
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002573 /* Set up internal HT/VHT capabilities */
Johannes Berga8243b72012-11-22 14:32:09 +01002574 if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
Ben Greearef96a8422011-11-18 11:32:00 -08002575 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
Johannes Berge1a0c6b2013-02-07 11:47:44 +01002576 elems.ht_cap_elem, sta);
Johannes Berg64f68e52012-03-28 10:58:37 +02002577
Mahesh Palivela818255e2012-10-10 11:33:04 +00002578 if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2579 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
Johannes Berg4a342152013-02-07 11:58:58 +01002580 elems.vht_cap_elem, sta);
Mahesh Palivela818255e2012-10-10 11:33:04 +00002581
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002582 /*
2583 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
2584 * in their association response, so ignore that data for our own
2585 * configuration. If it changed since the last beacon, we'll get the
2586 * next beacon and update then.
2587 */
Johannes Berg11289582013-02-07 17:36:12 +01002588
Johannes Bergbee7f5862013-02-07 22:24:55 +01002589 /*
2590 * If an operating mode notification IE is present, override the
2591 * NSS calculation (that would be done in rate_control_rate_init())
2592 * and use the # of streams from that element.
2593 */
2594 if (elems.opmode_notif &&
2595 !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
2596 u8 nss;
2597
2598 nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
2599 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
2600 nss += 1;
2601 sta->sta.rx_nss = nss;
2602 }
2603
Johannes Berg4b7679a2008-09-18 18:14:18 +02002604 rate_control_rate_init(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002605
Johannes Berg46900292009-02-15 12:44:28 +01002606 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002607 set_sta_flag(sta, WLAN_STA_MFP);
Jouni Malinen5394af42009-01-08 13:31:59 +02002608
Johannes Bergddf4ac52008-10-22 11:41:38 +02002609 if (elems.wmm_param)
Johannes Bergc2c98fd2011-09-29 16:04:36 +02002610 set_sta_flag(sta, WLAN_STA_WME);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002611
Johannes Berg3e4d40f2013-02-07 17:19:08 +01002612 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
Johannes Bergc8987872012-01-20 13:55:17 +01002613 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
2614 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
2615 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002616 sdata_info(sdata,
2617 "failed to move station %pM to desired state\n",
2618 sta->sta.addr);
Johannes Bergc8987872012-01-20 13:55:17 +01002619 WARN_ON(__sta_info_destroy(sta));
2620 mutex_unlock(&sdata->local->sta_mtx);
2621 return false;
2622 }
2623
Johannes Berg7852e362012-01-20 13:55:24 +01002624 mutex_unlock(&sdata->local->sta_mtx);
Johannes Bergddf4ac52008-10-22 11:41:38 +02002625
Juuso Oikarinenf2176d72010-09-28 14:39:32 +03002626 /*
2627 * Always handle WMM once after association regardless
2628 * of the first value the AP uses. Setting -1 here has
2629 * that effect because the AP values is an unsigned
2630 * 4-bit value.
2631 */
2632 ifmgd->wmm_last_param_set = -1;
2633
Johannes Bergddf4ac52008-10-22 11:41:38 +02002634 if (elems.wmm_param)
Johannes Berg4ced3f72010-07-19 16:39:04 +02002635 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002636 elems.wmm_param_len);
Johannes Bergaa837e12009-05-07 16:16:24 +02002637 else
Johannes Berg3abead52012-03-02 15:56:59 +01002638 ieee80211_set_wmm_default(sdata, false);
2639 changed |= BSS_CHANGED_QOS;
Jiri Bencf0706e82007-05-05 11:45:53 -07002640
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002641 /* set AID and assoc capability,
2642 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002643 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002644 bss_conf->assoc_capability = capab_info;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002645 ieee80211_set_associated(sdata, cbss, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002646
Kalle Valo15b7b062009-03-22 21:57:14 +02002647 /*
Felix Fietkaud5242152010-01-08 18:06:26 +01002648 * If we're using 4-addr mode, let the AP know that we're
2649 * doing so, so that it can create the STA VLAN on its side
2650 */
2651 if (ifmgd->use_4addr)
2652 ieee80211_send_4addr_nullfunc(local, sdata);
2653
2654 /*
Johannes Bergb291ba12009-07-10 15:29:03 +02002655 * Start timer to probe the connection to the AP now.
2656 * Also start the timer that will detect beacon loss.
Kalle Valo15b7b062009-03-22 21:57:14 +02002657 */
Johannes Bergb291ba12009-07-10 15:29:03 +02002658 ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04002659 ieee80211_sta_reset_beacon_monitor(sdata);
Kalle Valo15b7b062009-03-22 21:57:14 +02002660
Johannes Bergaf6b6372009-12-23 13:15:35 +01002661 return true;
Jiri Bencf0706e82007-05-05 11:45:53 -07002662}
2663
Johannes Berg66e67e42012-01-20 13:55:27 +01002664static enum rx_mgmt_action __must_check
2665ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
2666 struct ieee80211_mgmt *mgmt, size_t len,
2667 struct cfg80211_bss **bss)
2668{
2669 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2670 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
2671 u16 capab_info, status_code, aid;
2672 struct ieee802_11_elems elems;
2673 u8 *pos;
2674 bool reassoc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002675
Johannes Berg66e67e42012-01-20 13:55:27 +01002676 lockdep_assert_held(&ifmgd->mtx);
2677
2678 if (!assoc_data)
2679 return RX_MGMT_NONE;
Joe Perchesb203ca32012-05-08 18:56:52 +00002680 if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
Johannes Berg66e67e42012-01-20 13:55:27 +01002681 return RX_MGMT_NONE;
2682
2683 /*
2684 * AssocResp and ReassocResp have identical structure, so process both
2685 * of them in this function.
2686 */
2687
2688 if (len < 24 + 6)
2689 return RX_MGMT_NONE;
2690
2691 reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
2692 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2693 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2694 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
2695
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002696 sdata_info(sdata,
2697 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
2698 reassoc ? "Rea" : "A", mgmt->sa,
2699 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Johannes Berg66e67e42012-01-20 13:55:27 +01002700
2701 pos = mgmt->u.assoc_resp.variable;
Johannes Bergb2e506b2013-03-26 14:54:16 +01002702 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002703
2704 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
Johannes Berg79ba1d82013-03-27 14:38:07 +01002705 elems.timeout_int &&
2706 elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002707 u32 tu, ms;
Johannes Berg79ba1d82013-03-27 14:38:07 +01002708 tu = le32_to_cpu(elems.timeout_int->value);
Johannes Berg66e67e42012-01-20 13:55:27 +01002709 ms = tu * 1024 / 1000;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002710 sdata_info(sdata,
2711 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
2712 mgmt->sa, tu, ms);
Johannes Berg66e67e42012-01-20 13:55:27 +01002713 assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
Johannes Berg89afe612013-02-13 15:39:57 +01002714 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002715 if (ms > IEEE80211_ASSOC_TIMEOUT)
2716 run_again(ifmgd, assoc_data->timeout);
2717 return RX_MGMT_NONE;
2718 }
2719
2720 *bss = assoc_data->bss;
2721
2722 if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002723 sdata_info(sdata, "%pM denied association (code=%d)\n",
2724 mgmt->sa, status_code);
Johannes Berg66e67e42012-01-20 13:55:27 +01002725 ieee80211_destroy_assoc_data(sdata, false);
2726 } else {
Johannes Berg66e67e42012-01-20 13:55:27 +01002727 if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
2728 /* oops -- internal error -- send timeout for now */
Eliad Peller10a91092012-07-02 14:42:03 +03002729 ieee80211_destroy_assoc_data(sdata, false);
Johannes Berg5b112d32013-02-01 01:49:58 +01002730 cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01002731 return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
2732 }
John W. Linville635d9992012-07-09 16:34:34 -04002733 sdata_info(sdata, "associated\n");
Johannes Berg79ebfb82012-02-20 14:19:58 +01002734
2735 /*
2736 * destroy assoc_data afterwards, as otherwise an idle
2737 * recalc after assoc_data is NULL but before associated
2738 * is set can cause the interface to go idle
2739 */
2740 ieee80211_destroy_assoc_data(sdata, true);
Johannes Berg66e67e42012-01-20 13:55:27 +01002741 }
2742
2743 return RX_MGMT_CFG80211_RX_ASSOC;
2744}
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002745
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002746static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Johannes Berg8e3c1b72012-12-10 13:44:19 +01002747 struct ieee80211_mgmt *mgmt, size_t len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002748 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002749 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002750{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002751 struct ieee80211_local *local = sdata->local;
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002752 int freq;
Johannes Bergc2b13452008-09-11 00:01:55 +02002753 struct ieee80211_bss *bss;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002754 struct ieee80211_channel *channel;
Johannes Berg56007a02010-01-26 14:19:52 +01002755 bool need_ps = false;
2756
Johannes Bergb4f286a12013-03-26 14:13:58 +01002757 lockdep_assert_held(&sdata->u.mgd.mtx);
2758
Johannes Berg826262c2012-12-10 16:38:14 +02002759 if ((sdata->u.mgd.associated &&
2760 ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
2761 (sdata->u.mgd.assoc_data &&
2762 ether_addr_equal(mgmt->bssid,
2763 sdata->u.mgd.assoc_data->bss->bssid))) {
Johannes Berg56007a02010-01-26 14:19:52 +01002764 /* not previously set so we may need to recalc */
Johannes Berg826262c2012-12-10 16:38:14 +02002765 need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
2766
2767 if (elems->tim && !elems->parse_error) {
Johannes Berg4a3cb702013-02-12 16:43:19 +01002768 const struct ieee80211_tim_ie *tim_ie = elems->tim;
Johannes Berg826262c2012-12-10 16:38:14 +02002769 sdata->u.mgd.dtim_period = tim_ie->dtim_period;
2770 }
Johannes Berg56007a02010-01-26 14:19:52 +01002771 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002772
Johannes Berg1cd8e882013-03-27 14:30:12 +01002773 if (elems->ds_params)
Bruno Randolf59eb21a2011-01-17 13:37:28 +09002774 freq = ieee80211_channel_to_frequency(elems->ds_params[0],
2775 rx_status->band);
Johannes Berg5bda6172008-09-08 11:05:10 +02002776 else
2777 freq = rx_status->freq;
2778
2779 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2780
2781 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2782 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002783
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002784 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002785 channel);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002786 if (bss)
2787 ieee80211_rx_bss_put(local, bss);
2788
Johannes Berg37799e52013-03-26 14:02:26 +01002789 if (!sdata->u.mgd.associated ||
2790 !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
Johannes Berg98c8fcc2008-09-08 17:44:26 +02002791 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002792
Johannes Berg56007a02010-01-26 14:19:52 +01002793 if (need_ps) {
2794 mutex_lock(&local->iflist_mtx);
2795 ieee80211_recalc_ps(local, -1);
2796 mutex_unlock(&local->iflist_mtx);
2797 }
2798
Johannes Berg37799e52013-03-26 14:02:26 +01002799 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime, elems);
Johannes Bergb4f286a12013-03-26 14:13:58 +01002800
Jiri Bencf0706e82007-05-05 11:45:53 -07002801}
2802
2803
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002804static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Johannes Bergaf6b6372009-12-23 13:15:35 +01002805 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07002806{
Johannes Bergaf6b6372009-12-23 13:15:35 +01002807 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Kalle Valo15b7b062009-03-22 21:57:14 +02002808 struct ieee80211_if_managed *ifmgd;
Johannes Bergaf6b6372009-12-23 13:15:35 +01002809 struct ieee80211_rx_status *rx_status = (void *) skb->cb;
2810 size_t baselen, len = skb->len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002811 struct ieee802_11_elems elems;
2812
Kalle Valo15b7b062009-03-22 21:57:14 +02002813 ifmgd = &sdata->u.mgd;
2814
Johannes Berg77fdaa12009-07-07 03:45:17 +02002815 ASSERT_MGD_MTX(ifmgd);
2816
Joe Perchesb203ca32012-05-08 18:56:52 +00002817 if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002818 return; /* ignore ProbeResp to foreign address */
2819
Ester Kummerae6a44e2008-06-27 18:54:48 +03002820 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2821 if (baselen > len)
2822 return;
2823
2824 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002825 false, &elems);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002826
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002827 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002828
Johannes Berg77fdaa12009-07-07 03:45:17 +02002829 if (ifmgd->associated &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002830 ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Felix Fietkau4e5ff372010-11-23 03:10:31 +01002831 ieee80211_reset_ap_probe(sdata);
Johannes Berg66e67e42012-01-20 13:55:27 +01002832
2833 if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002834 ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002835 /* got probe response, continue with auth */
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002836 sdata_info(sdata, "direct probe responded\n");
Johannes Berg66e67e42012-01-20 13:55:27 +01002837 ifmgd->auth_data->tries = 0;
2838 ifmgd->auth_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002839 ifmgd->auth_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002840 run_again(ifmgd, ifmgd->auth_data->timeout);
2841 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002842}
2843
Johannes Bergd91f36d2009-04-16 13:17:26 +02002844/*
2845 * This is the canonical list of information elements we care about,
2846 * the filter code also gives us all changes to the Microsoft OUI
2847 * (00:50:F2) vendor IE which is used for WMM which we need to track.
2848 *
2849 * We implement beacon filtering in software since that means we can
2850 * avoid processing the frame here and in cfg80211, and userspace
2851 * will not be able to tell whether the hardware supports it or not.
2852 *
2853 * XXX: This list needs to be dynamic -- userspace needs to be able to
2854 * add items it requires. It also needs to be able to tell us to
2855 * look out for other vendor IEs.
2856 */
2857static const u64 care_about_ies =
Johannes Berg1d4df3a2009-04-22 11:25:43 +02002858 (1ULL << WLAN_EID_COUNTRY) |
2859 (1ULL << WLAN_EID_ERP_INFO) |
2860 (1ULL << WLAN_EID_CHANNEL_SWITCH) |
2861 (1ULL << WLAN_EID_PWR_CONSTRAINT) |
2862 (1ULL << WLAN_EID_HT_CAPABILITY) |
Johannes Berg074d46d2012-03-15 19:45:16 +01002863 (1ULL << WLAN_EID_HT_OPERATION);
Johannes Bergd91f36d2009-04-16 13:17:26 +02002864
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002865static enum rx_mgmt_action
2866ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
2867 struct ieee80211_mgmt *mgmt, size_t len,
2868 u8 *deauth_buf, struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002869{
Johannes Bergd91f36d2009-04-16 13:17:26 +02002870 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002871 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002872 size_t baselen;
2873 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002874 struct ieee80211_local *local = sdata->local;
Johannes Berg55de9082012-07-26 17:24:39 +02002875 struct ieee80211_chanctx_conf *chanctx_conf;
2876 struct ieee80211_channel *chan;
Johannes Bergbee7f5862013-02-07 22:24:55 +01002877 struct sta_info *sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002878 u32 changed = 0;
Rami Rosenf87ad632012-10-25 10:16:23 +02002879 bool erp_valid;
Johannes Berg7a5158e2008-10-08 10:59:33 +02002880 u8 erp_value = 0;
Johannes Bergd91f36d2009-04-16 13:17:26 +02002881 u32 ncrc;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002882 u8 *bssid;
2883
Johannes Berg66e67e42012-01-20 13:55:27 +01002884 lockdep_assert_held(&ifmgd->mtx);
Jiri Bencf0706e82007-05-05 11:45:53 -07002885
Ester Kummerae6a44e2008-06-27 18:54:48 +03002886 /* Process beacon from the current BSS */
2887 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2888 if (baselen > len)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002889 return RX_MGMT_NONE;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002890
Johannes Berg55de9082012-07-26 17:24:39 +02002891 rcu_read_lock();
2892 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2893 if (!chanctx_conf) {
2894 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002895 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002896 }
2897
Johannes Berg4bf88532012-11-09 11:39:59 +01002898 if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
Johannes Berg55de9082012-07-26 17:24:39 +02002899 rcu_read_unlock();
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002900 return RX_MGMT_NONE;
Johannes Berg55de9082012-07-26 17:24:39 +02002901 }
Johannes Berg4bf88532012-11-09 11:39:59 +01002902 chan = chanctx_conf->def.chan;
Johannes Berg55de9082012-07-26 17:24:39 +02002903 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002904
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002905 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
Joe Perchesb203ca32012-05-08 18:56:52 +00002906 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
Johannes Berg66e67e42012-01-20 13:55:27 +01002907 ieee802_11_parse_elems(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01002908 len - baselen, false, &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02002909
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02002910 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg66e67e42012-01-20 13:55:27 +01002911 ifmgd->assoc_data->have_beacon = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02002912 ifmgd->assoc_data->need_beacon = false;
Johannes Bergef429da2013-02-05 17:48:40 +01002913 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
2914 sdata->vif.bss_conf.sync_tsf =
2915 le64_to_cpu(mgmt->u.beacon.timestamp);
2916 sdata->vif.bss_conf.sync_device_ts =
2917 rx_status->device_timestamp;
2918 if (elems.tim)
2919 sdata->vif.bss_conf.sync_dtim_count =
2920 elems.tim->dtim_count;
2921 else
2922 sdata->vif.bss_conf.sync_dtim_count = 0;
2923 }
Johannes Berg66e67e42012-01-20 13:55:27 +01002924 /* continue assoc process */
2925 ifmgd->assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01002926 ifmgd->assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01002927 run_again(ifmgd, ifmgd->assoc_data->timeout);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002928 return RX_MGMT_NONE;
Johannes Berg66e67e42012-01-20 13:55:27 +01002929 }
2930
2931 if (!ifmgd->associated ||
Joe Perchesb203ca32012-05-08 18:56:52 +00002932 !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
Johannes Berg30eb1dc2013-02-08 15:12:14 +01002933 return RX_MGMT_NONE;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01002934 bssid = ifmgd->associated->bssid;
Johannes Berg77fdaa12009-07-07 03:45:17 +02002935
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002936 /* Track average RSSI from the Beacon frames of the current AP */
2937 ifmgd->last_beacon_signal = rx_status->signal;
2938 if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
2939 ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
Jouni Malinen3ba06c62010-08-27 22:21:13 +03002940 ifmgd->ave_beacon_signal = rx_status->signal * 16;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002941 ifmgd->last_cqm_event_signal = 0;
Jouni Malinen391a2002010-08-27 22:22:00 +03002942 ifmgd->count_beacon_signal = 1;
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002943 ifmgd->last_ave_beacon_signal = 0;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002944 } else {
2945 ifmgd->ave_beacon_signal =
2946 (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
2947 (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
2948 ifmgd->ave_beacon_signal) / 16;
Jouni Malinen391a2002010-08-27 22:22:00 +03002949 ifmgd->count_beacon_signal++;
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002950 }
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002951
2952 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
2953 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
2954 int sig = ifmgd->ave_beacon_signal;
2955 int last_sig = ifmgd->last_ave_beacon_signal;
2956
2957 /*
2958 * if signal crosses either of the boundaries, invoke callback
2959 * with appropriate parameters
2960 */
2961 if (sig > ifmgd->rssi_max_thold &&
2962 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
2963 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002964 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002965 } else if (sig < ifmgd->rssi_min_thold &&
2966 (last_sig >= ifmgd->rssi_max_thold ||
2967 last_sig == 0)) {
2968 ifmgd->last_ave_beacon_signal = sig;
Emmanuel Grumbach887da912013-01-20 17:32:41 +02002969 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
Meenakshi Venkataraman615f7b92011-07-08 08:46:22 -07002970 }
2971 }
2972
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002973 if (bss_conf->cqm_rssi_thold &&
Jouni Malinen391a2002010-08-27 22:22:00 +03002974 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
Johannes Bergea086352012-01-19 09:29:58 +01002975 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
Jouni Malinen17e4ec12010-03-29 23:28:30 -07002976 int sig = ifmgd->ave_beacon_signal / 16;
2977 int last_event = ifmgd->last_cqm_event_signal;
2978 int thold = bss_conf->cqm_rssi_thold;
2979 int hyst = bss_conf->cqm_rssi_hyst;
2980 if (sig < thold &&
2981 (last_event == 0 || sig < last_event - hyst)) {
2982 ifmgd->last_cqm_event_signal = sig;
2983 ieee80211_cqm_rssi_notify(
2984 &sdata->vif,
2985 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
2986 GFP_KERNEL);
2987 } else if (sig > thold &&
2988 (last_event == 0 || sig > last_event + hyst)) {
2989 ifmgd->last_cqm_event_signal = sig;
2990 ieee80211_cqm_rssi_notify(
2991 &sdata->vif,
2992 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
2993 GFP_KERNEL);
2994 }
2995 }
2996
Johannes Bergb291ba12009-07-10 15:29:03 +02002997 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02002998 mlme_dbg_ratelimited(sdata,
Johannes Berg112c31f2013-02-08 22:59:00 +01002999 "cancelling AP probe due to a received beacon\n");
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003000 mutex_lock(&local->mtx);
Johannes Bergb291ba12009-07-10 15:29:03 +02003001 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003002 ieee80211_run_deferred_scan(local);
3003 mutex_unlock(&local->mtx);
3004
Johannes Berg4e751842009-06-10 15:16:52 +02003005 mutex_lock(&local->iflist_mtx);
3006 ieee80211_recalc_ps(local, -1);
3007 mutex_unlock(&local->iflist_mtx);
Jouni Malinen92778182009-05-14 21:15:36 +03003008 }
3009
Johannes Bergb291ba12009-07-10 15:29:03 +02003010 /*
3011 * Push the beacon loss detection into the future since
3012 * we are processing a beacon from the AP just now.
3013 */
Luis R. Rodriguezd3a910a2010-09-16 15:12:32 -04003014 ieee80211_sta_reset_beacon_monitor(sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003015
Johannes Bergd91f36d2009-04-16 13:17:26 +02003016 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
3017 ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003018 len - baselen, false, &elems,
Johannes Bergd91f36d2009-04-16 13:17:26 +02003019 care_about_ies, ncrc);
3020
Vivek Natarajan25c9c872009-03-02 20:20:30 +05303021 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
Rami Rosenf87ad632012-10-25 10:16:23 +02003022 bool directed_tim = ieee80211_check_tim(elems.tim,
3023 elems.tim_len,
3024 ifmgd->aid);
Kalle Valo1fb36062009-02-10 17:09:24 +02003025 if (directed_tim) {
Kalle Valo572e0012009-02-10 17:09:31 +02003026 if (local->hw.conf.dynamic_ps_timeout > 0) {
Ronald Wahl70b12f22012-03-19 14:37:20 +01003027 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
3028 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
3029 ieee80211_hw_config(local,
3030 IEEE80211_CONF_CHANGE_PS);
3031 }
Kalle Valo572e0012009-02-10 17:09:31 +02003032 ieee80211_send_nullfunc(local, sdata, 0);
Rajkumar Manoharan6f0756a2012-03-15 05:50:36 +05303033 } else if (!local->pspolling && sdata->u.mgd.powersave) {
Kalle Valo572e0012009-02-10 17:09:31 +02003034 local->pspolling = true;
3035
3036 /*
3037 * Here is assumed that the driver will be
3038 * able to send ps-poll frame and receive a
3039 * response even though power save mode is
3040 * enabled, but some drivers might require
3041 * to disable power save here. This needs
3042 * to be investigated.
3043 */
3044 ieee80211_send_pspoll(local, sdata);
3045 }
Vivek Natarajana97b77b2008-12-23 18:39:02 -08003046 }
3047 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003048
Johannes Berg488dd7b2012-10-29 20:08:01 +01003049 if (sdata->vif.p2p) {
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003050 struct ieee80211_p2p_noa_attr noa = {};
Johannes Berg488dd7b2012-10-29 20:08:01 +01003051 int ret;
3052
3053 ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
3054 len - baselen,
3055 IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
Janusz Dziedzic934457e2013-03-21 15:47:55 +01003056 (u8 *) &noa, sizeof(noa));
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003057 if (ret >= 2) {
3058 if (sdata->u.mgd.p2p_noa_index != noa.index) {
3059 /* valid noa_attr and index changed */
3060 sdata->u.mgd.p2p_noa_index = noa.index;
3061 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
3062 changed |= BSS_CHANGED_P2P_PS;
3063 /*
3064 * make sure we update all information, the CRC
3065 * mechanism doesn't look at P2P attributes.
3066 */
3067 ifmgd->beacon_crc_valid = false;
3068 }
3069 } else if (sdata->u.mgd.p2p_noa_index != -1) {
3070 /* noa_attr not found and we had valid noa_attr before */
3071 sdata->u.mgd.p2p_noa_index = -1;
3072 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
Johannes Berg488dd7b2012-10-29 20:08:01 +01003073 changed |= BSS_CHANGED_P2P_PS;
Johannes Berg488dd7b2012-10-29 20:08:01 +01003074 ifmgd->beacon_crc_valid = false;
3075 }
3076 }
3077
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003078 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003079 return RX_MGMT_NONE;
Jouni Malinen30196672009-05-19 17:01:43 +03003080 ifmgd->beacon_crc = ncrc;
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03003081 ifmgd->beacon_crc_valid = true;
Jouni Malinen30196672009-05-19 17:01:43 +03003082
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +02003083 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Johannes Berg7d257452012-07-06 17:37:43 +02003084
3085 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3086 elems.wmm_param_len))
3087 changed |= BSS_CHANGED_QOS;
3088
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003089 /*
3090 * If we haven't had a beacon before, tell the driver about the
Johannes Bergef429da2013-02-05 17:48:40 +01003091 * DTIM period (and beacon timing if desired) now.
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003092 */
3093 if (!bss_conf->dtim_period) {
3094 /* a few bogus AP send dtim_period = 0 or no TIM IE */
3095 if (elems.tim)
3096 bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
3097 else
3098 bss_conf->dtim_period = 1;
Johannes Bergef429da2013-02-05 17:48:40 +01003099
3100 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3101 sdata->vif.bss_conf.sync_tsf =
3102 le64_to_cpu(mgmt->u.beacon.timestamp);
3103 sdata->vif.bss_conf.sync_device_ts =
3104 rx_status->device_timestamp;
3105 if (elems.tim)
3106 sdata->vif.bss_conf.sync_dtim_count =
3107 elems.tim->dtim_count;
3108 else
3109 sdata->vif.bss_conf.sync_dtim_count = 0;
3110 }
3111
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003112 changed |= BSS_CHANGED_DTIM_PERIOD;
3113 }
3114
Johannes Berg1946bed2013-03-27 14:31:53 +01003115 if (elems.erp_info) {
Johannes Berg7a5158e2008-10-08 10:59:33 +02003116 erp_valid = true;
3117 erp_value = elems.erp_info[0];
3118 } else {
3119 erp_valid = false;
John W. Linville50c4afb2008-04-15 14:09:27 -04003120 }
Johannes Berg7a5158e2008-10-08 10:59:33 +02003121 changed |= ieee80211_handle_bss_capability(sdata,
3122 le16_to_cpu(mgmt->u.beacon.capab_info),
3123 erp_valid, erp_value);
Jiri Bencf0706e82007-05-05 11:45:53 -07003124
Johannes Berg11289582013-02-07 17:36:12 +01003125 mutex_lock(&local->sta_mtx);
Johannes Bergbee7f5862013-02-07 22:24:55 +01003126 sta = sta_info_get(sdata, bssid);
3127
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003128 if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
3129 elems.vht_operation, bssid, &changed)) {
3130 mutex_unlock(&local->sta_mtx);
3131 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3132 WLAN_REASON_DEAUTH_LEAVING,
3133 true, deauth_buf);
3134 return RX_MGMT_CFG80211_TX_DEAUTH;
3135 }
Johannes Bergbee7f5862013-02-07 22:24:55 +01003136
3137 if (sta && elems.opmode_notif)
3138 ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
3139 rx_status->band, true);
Johannes Berg11289582013-02-07 17:36:12 +01003140 mutex_unlock(&local->sta_mtx);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003141
Johannes Berg04b7b2f2012-09-05 13:41:37 +02003142 if (elems.country_elem && elems.pwr_constr_elem &&
3143 mgmt->u.probe_resp.capab_info &
3144 cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
Johannes Berg1ea6f9c2012-10-24 10:59:25 +02003145 changed |= ieee80211_handle_pwr_constr(sdata, chan,
3146 elems.country_elem,
3147 elems.country_elem_len,
3148 elems.pwr_constr_elem);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08003149
Johannes Berg471b3ef2007-12-28 14:32:58 +01003150 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003151
3152 return RX_MGMT_NONE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003153}
3154
Johannes Berg1fa57d02010-06-10 10:21:32 +02003155void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
3156 struct sk_buff *skb)
Jiri Bencf0706e82007-05-05 11:45:53 -07003157{
Johannes Berg77fdaa12009-07-07 03:45:17 +02003158 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jiri Bencf0706e82007-05-05 11:45:53 -07003159 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07003160 struct ieee80211_mgmt *mgmt;
Johannes Berg66e67e42012-01-20 13:55:27 +01003161 struct cfg80211_bss *bss = NULL;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003162 enum rx_mgmt_action rma = RX_MGMT_NONE;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003163 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
Jiri Bencf0706e82007-05-05 11:45:53 -07003164 u16 fc;
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003165 struct ieee802_11_elems elems;
3166 int ies_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07003167
Jiri Bencf0706e82007-05-05 11:45:53 -07003168 rx_status = (struct ieee80211_rx_status *) skb->cb;
3169 mgmt = (struct ieee80211_mgmt *) skb->data;
3170 fc = le16_to_cpu(mgmt->frame_control);
3171
Johannes Berg77fdaa12009-07-07 03:45:17 +02003172 mutex_lock(&ifmgd->mtx);
3173
Johannes Berg66e67e42012-01-20 13:55:27 +01003174 switch (fc & IEEE80211_FCTL_STYPE) {
3175 case IEEE80211_STYPE_BEACON:
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003176 rma = ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
3177 deauth_buf, rx_status);
Johannes Berg66e67e42012-01-20 13:55:27 +01003178 break;
3179 case IEEE80211_STYPE_PROBE_RESP:
3180 ieee80211_rx_mgmt_probe_resp(sdata, skb);
3181 break;
3182 case IEEE80211_STYPE_AUTH:
3183 rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
3184 break;
3185 case IEEE80211_STYPE_DEAUTH:
3186 rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
3187 break;
3188 case IEEE80211_STYPE_DISASSOC:
3189 rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
3190 break;
3191 case IEEE80211_STYPE_ASSOC_RESP:
3192 case IEEE80211_STYPE_REASSOC_RESP:
3193 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
3194 break;
3195 case IEEE80211_STYPE_ACTION:
Johannes Berg37799e52013-03-26 14:02:26 +01003196 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003197 ies_len = skb->len -
3198 offsetof(struct ieee80211_mgmt,
3199 u.action.u.chan_switch.variable);
Johannes Berg37799e52013-03-26 14:02:26 +01003200
3201 if (ies_len < 0)
3202 break;
3203
3204 ieee802_11_parse_elems(
3205 mgmt->u.action.u.chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003206 ies_len, true, &elems);
Johannes Berg37799e52013-03-26 14:02:26 +01003207
3208 if (elems.parse_error)
3209 break;
3210
Johannes Berg66e67e42012-01-20 13:55:27 +01003211 ieee80211_sta_process_chanswitch(sdata,
Johannes Berg37799e52013-03-26 14:02:26 +01003212 rx_status->mactime,
3213 &elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003214 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
3215 ies_len = skb->len -
3216 offsetof(struct ieee80211_mgmt,
3217 u.action.u.ext_chan_switch.variable);
3218
3219 if (ies_len < 0)
3220 break;
3221
3222 ieee802_11_parse_elems(
3223 mgmt->u.action.u.ext_chan_switch.variable,
Johannes Bergb2e506b2013-03-26 14:54:16 +01003224 ies_len, true, &elems);
Johannes Berg1b3a2e42013-03-26 15:17:18 +01003225
3226 if (elems.parse_error)
3227 break;
3228
3229 /* for the handling code pretend this was also an IE */
3230 elems.ext_chansw_ie =
3231 &mgmt->u.action.u.ext_chan_switch.data;
3232
3233 ieee80211_sta_process_chanswitch(sdata,
3234 rx_status->mactime,
3235 &elems);
Johannes Berg77fdaa12009-07-07 03:45:17 +02003236 }
Johannes Berg37799e52013-03-26 14:02:26 +01003237 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003238 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02003239 mutex_unlock(&ifmgd->mtx);
3240
Johannes Berg66e67e42012-01-20 13:55:27 +01003241 switch (rma) {
3242 case RX_MGMT_NONE:
3243 /* no action */
3244 break;
3245 case RX_MGMT_CFG80211_DEAUTH:
Holger Schurigce470612009-10-13 13:28:13 +02003246 cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
Johannes Berg66e67e42012-01-20 13:55:27 +01003247 break;
3248 case RX_MGMT_CFG80211_DISASSOC:
3249 cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
3250 break;
3251 case RX_MGMT_CFG80211_RX_AUTH:
3252 cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
3253 break;
3254 case RX_MGMT_CFG80211_RX_ASSOC:
3255 cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
3256 break;
3257 case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
3258 cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
3259 break;
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003260 case RX_MGMT_CFG80211_TX_DEAUTH:
3261 cfg80211_send_deauth(sdata->dev, deauth_buf,
3262 sizeof(deauth_buf));
3263 break;
Johannes Berg66e67e42012-01-20 13:55:27 +01003264 default:
3265 WARN(1, "unexpected: %d", rma);
Johannes Bergb054b742010-06-07 21:50:07 +02003266 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003267}
3268
Johannes Berg9c6bd792008-09-11 00:01:52 +02003269static void ieee80211_sta_timer(unsigned long data)
Jiri Bencf0706e82007-05-05 11:45:53 -07003270{
3271 struct ieee80211_sub_if_data *sdata =
3272 (struct ieee80211_sub_if_data *) data;
Jiri Bencf0706e82007-05-05 11:45:53 -07003273
Stanislaw Gruszka9b7d72c2013-02-28 10:55:27 +01003274 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Jiri Bencf0706e82007-05-05 11:45:53 -07003275}
3276
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003277static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
Johannes Berg6b684db2013-01-29 11:35:29 +01003278 u8 *bssid, u8 reason, bool tx)
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003279{
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003280 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003281 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003282
Johannes Berg37ad3882012-02-24 13:50:54 +01003283 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
Johannes Berg6b684db2013-01-29 11:35:29 +01003284 tx, frame_buf);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003285 mutex_unlock(&ifmgd->mtx);
Johannes Berg37ad3882012-02-24 13:50:54 +01003286
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003287 /*
3288 * must be outside lock due to cfg80211,
3289 * but that's not a problem.
3290 */
Antonio Quartulli6ae16772012-09-07 13:28:52 +02003291 cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
Felix Fietkaufcac4fb2011-11-16 13:34:55 +01003292
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003293 mutex_lock(&ifmgd->mtx);
3294}
3295
Johannes Berg66e67e42012-01-20 13:55:27 +01003296static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
3297{
3298 struct ieee80211_local *local = sdata->local;
3299 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3300 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003301 u32 tx_flags = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01003302
3303 lockdep_assert_held(&ifmgd->mtx);
3304
3305 if (WARN_ON_ONCE(!auth_data))
3306 return -EINVAL;
3307
Johannes Berg1672c0e32013-01-29 15:02:27 +01003308 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
3309 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
3310 IEEE80211_TX_INTFL_MLME_CONN_TX;
3311
Johannes Berg66e67e42012-01-20 13:55:27 +01003312 auth_data->tries++;
3313
3314 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003315 sdata_info(sdata, "authentication with %pM timed out\n",
3316 auth_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003317
3318 /*
3319 * Most likely AP is not in the range so remove the
3320 * bss struct for that AP.
3321 */
3322 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
3323
3324 return -ETIMEDOUT;
3325 }
3326
Johannes Berga1845fc2012-06-27 13:18:36 +02003327 drv_mgd_prepare_tx(local, sdata);
3328
Johannes Berg66e67e42012-01-20 13:55:27 +01003329 if (auth_data->bss->proberesp_ies) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003330 u16 trans = 1;
3331 u16 status = 0;
3332
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003333 sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
3334 auth_data->bss->bssid, auth_data->tries,
3335 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003336
3337 auth_data->expected_transaction = 2;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003338
3339 if (auth_data->algorithm == WLAN_AUTH_SAE) {
3340 trans = auth_data->sae_trans;
3341 status = auth_data->sae_status;
3342 auth_data->expected_transaction = trans;
3343 }
3344
3345 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
3346 auth_data->data, auth_data->data_len,
Johannes Berg66e67e42012-01-20 13:55:27 +01003347 auth_data->bss->bssid,
Johannes Berg1672c0e32013-01-29 15:02:27 +01003348 auth_data->bss->bssid, NULL, 0, 0,
3349 tx_flags);
Johannes Berg66e67e42012-01-20 13:55:27 +01003350 } else {
3351 const u8 *ssidie;
3352
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003353 sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
3354 auth_data->bss->bssid, auth_data->tries,
3355 IEEE80211_AUTH_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003356
Johannes Berg9caf0362012-11-29 01:25:20 +01003357 rcu_read_lock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003358 ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
Johannes Berg9caf0362012-11-29 01:25:20 +01003359 if (!ssidie) {
3360 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003361 return -EINVAL;
Johannes Berg9caf0362012-11-29 01:25:20 +01003362 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003363 /*
3364 * Direct probe is sent to broadcast address as some APs
3365 * will not answer to direct packet in unassociated state.
3366 */
3367 ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
Johannes Berg1672c0e32013-01-29 15:02:27 +01003368 NULL, 0, (u32) -1, true, tx_flags,
Johannes Berg55de9082012-07-26 17:24:39 +02003369 auth_data->bss->channel, false);
Johannes Berg9caf0362012-11-29 01:25:20 +01003370 rcu_read_unlock();
Johannes Berg66e67e42012-01-20 13:55:27 +01003371 }
3372
Johannes Berg1672c0e32013-01-29 15:02:27 +01003373 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3374 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003375 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003376 run_again(ifmgd, auth_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003377 } else {
3378 auth_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003379 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003380
3381 return 0;
3382}
3383
3384static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
3385{
3386 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
3387 struct ieee80211_local *local = sdata->local;
3388
3389 lockdep_assert_held(&sdata->u.mgd.mtx);
3390
Johannes Berg66e67e42012-01-20 13:55:27 +01003391 assoc_data->tries++;
3392 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003393 sdata_info(sdata, "association with %pM timed out\n",
3394 assoc_data->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01003395
3396 /*
3397 * Most likely AP is not in the range so remove the
3398 * bss struct for that AP.
3399 */
3400 cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
3401
3402 return -ETIMEDOUT;
3403 }
3404
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003405 sdata_info(sdata, "associate with %pM (try %d/%d)\n",
3406 assoc_data->bss->bssid, assoc_data->tries,
3407 IEEE80211_ASSOC_MAX_TRIES);
Johannes Berg66e67e42012-01-20 13:55:27 +01003408 ieee80211_send_assoc(sdata);
3409
Johannes Berg1672c0e32013-01-29 15:02:27 +01003410 if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
3411 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
Johannes Berg89afe612013-02-13 15:39:57 +01003412 assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003413 run_again(&sdata->u.mgd, assoc_data->timeout);
Johannes Berg89afe612013-02-13 15:39:57 +01003414 } else {
3415 assoc_data->timeout_started = false;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003416 }
Johannes Berg66e67e42012-01-20 13:55:27 +01003417
3418 return 0;
3419}
3420
Johannes Berg1672c0e32013-01-29 15:02:27 +01003421void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
3422 __le16 fc, bool acked)
3423{
3424 struct ieee80211_local *local = sdata->local;
3425
3426 sdata->u.mgd.status_fc = fc;
3427 sdata->u.mgd.status_acked = acked;
3428 sdata->u.mgd.status_received = true;
3429
3430 ieee80211_queue_work(&local->hw, &sdata->work);
3431}
3432
Johannes Berg1fa57d02010-06-10 10:21:32 +02003433void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
Johannes Berg60f8b39c2008-09-08 17:44:22 +02003434{
Johannes Berg60f8b39c2008-09-08 17:44:22 +02003435 struct ieee80211_local *local = sdata->local;
Johannes Berg1fa57d02010-06-10 10:21:32 +02003436 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg60f8b39c2008-09-08 17:44:22 +02003437
Johannes Berg77fdaa12009-07-07 03:45:17 +02003438 mutex_lock(&ifmgd->mtx);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02003439
Johannes Berg1672c0e32013-01-29 15:02:27 +01003440 if (ifmgd->status_received) {
3441 __le16 fc = ifmgd->status_fc;
3442 bool status_acked = ifmgd->status_acked;
3443
3444 ifmgd->status_received = false;
3445 if (ifmgd->auth_data &&
3446 (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
3447 if (status_acked) {
3448 ifmgd->auth_data->timeout =
3449 jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
3450 run_again(ifmgd, ifmgd->auth_data->timeout);
3451 } else {
3452 ifmgd->auth_data->timeout = jiffies - 1;
3453 }
Johannes Berg89afe612013-02-13 15:39:57 +01003454 ifmgd->auth_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003455 } else if (ifmgd->assoc_data &&
3456 (ieee80211_is_assoc_req(fc) ||
3457 ieee80211_is_reassoc_req(fc))) {
3458 if (status_acked) {
3459 ifmgd->assoc_data->timeout =
3460 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
3461 run_again(ifmgd, ifmgd->assoc_data->timeout);
3462 } else {
3463 ifmgd->assoc_data->timeout = jiffies - 1;
3464 }
Johannes Berg89afe612013-02-13 15:39:57 +01003465 ifmgd->assoc_data->timeout_started = true;
Johannes Berg1672c0e32013-01-29 15:02:27 +01003466 }
3467 }
3468
Johannes Berg89afe612013-02-13 15:39:57 +01003469 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003470 time_after(jiffies, ifmgd->auth_data->timeout)) {
3471 if (ifmgd->auth_data->done) {
3472 /*
3473 * ok ... we waited for assoc but userspace didn't,
3474 * so let's just kill the auth data
3475 */
3476 ieee80211_destroy_auth_data(sdata, false);
3477 } else if (ieee80211_probe_auth(sdata)) {
3478 u8 bssid[ETH_ALEN];
3479
3480 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3481
3482 ieee80211_destroy_auth_data(sdata, false);
3483
3484 mutex_unlock(&ifmgd->mtx);
3485 cfg80211_send_auth_timeout(sdata->dev, bssid);
3486 mutex_lock(&ifmgd->mtx);
3487 }
Johannes Berg89afe612013-02-13 15:39:57 +01003488 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003489 run_again(ifmgd, ifmgd->auth_data->timeout);
3490
Johannes Berg89afe612013-02-13 15:39:57 +01003491 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
Johannes Berg66e67e42012-01-20 13:55:27 +01003492 time_after(jiffies, ifmgd->assoc_data->timeout)) {
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02003493 if ((ifmgd->assoc_data->need_beacon &&
3494 !ifmgd->assoc_data->have_beacon) ||
Johannes Berg66e67e42012-01-20 13:55:27 +01003495 ieee80211_do_assoc(sdata)) {
3496 u8 bssid[ETH_ALEN];
3497
3498 memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
3499
3500 ieee80211_destroy_assoc_data(sdata, false);
3501
3502 mutex_unlock(&ifmgd->mtx);
3503 cfg80211_send_assoc_timeout(sdata->dev, bssid);
3504 mutex_lock(&ifmgd->mtx);
3505 }
Johannes Berg89afe612013-02-13 15:39:57 +01003506 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
Johannes Berg66e67e42012-01-20 13:55:27 +01003507 run_again(ifmgd, ifmgd->assoc_data->timeout);
3508
Johannes Bergb291ba12009-07-10 15:29:03 +02003509 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
3510 IEEE80211_STA_CONNECTION_POLL) &&
3511 ifmgd->associated) {
Maxim Levitskya43abf22009-07-31 18:54:12 +03003512 u8 bssid[ETH_ALEN];
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003513 int max_tries;
Maxim Levitskya43abf22009-07-31 18:54:12 +03003514
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01003515 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003516
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003517 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
Ben Greear180205b2011-02-04 15:30:24 -08003518 max_tries = max_nullfunc_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003519 else
Ben Greear180205b2011-02-04 15:30:24 -08003520 max_tries = max_probe_tries;
Felix Fietkau72a8a3e2010-11-23 03:10:32 +01003521
Felix Fietkau4e5ff372010-11-23 03:10:31 +01003522 /* ACK received for nullfunc probing frame */
3523 if (!ifmgd->probe_send_count)
3524 ieee80211_reset_ap_probe(sdata);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003525 else if (ifmgd->nullfunc_failed) {
3526 if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003527 mlme_dbg(sdata,
3528 "No ack for nullfunc frame to AP %pM, try %d/%i\n",
3529 bssid, ifmgd->probe_send_count,
3530 max_tries);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003531 ieee80211_mgd_probe_ap_send(sdata);
3532 } else {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003533 mlme_dbg(sdata,
3534 "No ack for nullfunc frame to AP %pM, disconnecting.\n",
3535 bssid);
Johannes Berg95acac62011-07-12 12:30:59 +02003536 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003537 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
3538 false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003539 }
3540 } else if (time_is_after_jiffies(ifmgd->probe_timeout))
Johannes Bergb291ba12009-07-10 15:29:03 +02003541 run_again(ifmgd, ifmgd->probe_timeout);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003542 else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003543 mlme_dbg(sdata,
3544 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
3545 bssid, probe_wait_ms);
Johannes Berg95acac62011-07-12 12:30:59 +02003546 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003547 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003548 } else if (ifmgd->probe_send_count < max_tries) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003549 mlme_dbg(sdata,
3550 "No probe response from AP %pM after %dms, try %d/%i\n",
3551 bssid, probe_wait_ms,
3552 ifmgd->probe_send_count, max_tries);
Maxim Levitskya43abf22009-07-31 18:54:12 +03003553 ieee80211_mgd_probe_ap_send(sdata);
3554 } else {
Johannes Bergb291ba12009-07-10 15:29:03 +02003555 /*
3556 * We actually lost the connection ... or did we?
3557 * Let's make sure!
3558 */
Ben Greearb38afa82010-10-07 16:12:06 -07003559 wiphy_debug(local->hw.wiphy,
3560 "%s: No probe response from AP %pM"
3561 " after %dms, disconnecting.\n",
3562 sdata->name,
Ben Greear180205b2011-02-04 15:30:24 -08003563 bssid, probe_wait_ms);
Felix Fietkau04ac3c02010-12-02 21:01:08 +01003564
Johannes Berg95acac62011-07-12 12:30:59 +02003565 ieee80211_sta_connection_lost(sdata, bssid,
Johannes Berg6b684db2013-01-29 11:35:29 +01003566 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
Johannes Bergb291ba12009-07-10 15:29:03 +02003567 }
3568 }
3569
Johannes Berg77fdaa12009-07-07 03:45:17 +02003570 mutex_unlock(&ifmgd->mtx);
Johannes Berg60f8b39c2008-09-08 17:44:22 +02003571}
Johannes Berg0a51b272008-09-08 17:44:25 +02003572
Johannes Bergb291ba12009-07-10 15:29:03 +02003573static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3574{
3575 struct ieee80211_sub_if_data *sdata =
3576 (struct ieee80211_sub_if_data *) data;
3577 struct ieee80211_local *local = sdata->local;
3578
3579 if (local->quiescing)
3580 return;
3581
Johannes Berg682bd382013-01-29 13:13:50 +01003582 sdata->u.mgd.connection_loss = false;
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003583 ieee80211_queue_work(&sdata->local->hw,
3584 &sdata->u.mgd.beacon_connection_loss_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003585}
3586
3587static void ieee80211_sta_conn_mon_timer(unsigned long data)
3588{
3589 struct ieee80211_sub_if_data *sdata =
3590 (struct ieee80211_sub_if_data *) data;
3591 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3592 struct ieee80211_local *local = sdata->local;
3593
3594 if (local->quiescing)
3595 return;
3596
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04003597 ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003598}
3599
3600static void ieee80211_sta_monitor_work(struct work_struct *work)
3601{
3602 struct ieee80211_sub_if_data *sdata =
3603 container_of(work, struct ieee80211_sub_if_data,
3604 u.mgd.monitor_work);
3605
3606 ieee80211_mgd_probe_ap(sdata, false);
3607}
3608
Johannes Berga1678f82008-09-11 00:01:47 +02003609static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3610{
Eliad Peller494f1fe2012-02-19 15:26:09 +02003611 u32 flags;
3612
Kalle Vaload935682009-04-19 08:47:19 +03003613 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Stanislaw Gruszka925e64c2012-05-16 15:27:20 +02003614 __ieee80211_stop_poll(sdata);
Kalle Vaload935682009-04-19 08:47:19 +03003615
Johannes Bergb291ba12009-07-10 15:29:03 +02003616 /* let's probe the connection once */
Eliad Peller494f1fe2012-02-19 15:26:09 +02003617 flags = sdata->local->hw.flags;
3618 if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
3619 ieee80211_queue_work(&sdata->local->hw,
3620 &sdata->u.mgd.monitor_work);
Johannes Bergb291ba12009-07-10 15:29:03 +02003621 /* and do all the other regular work too */
Johannes Berg64592c82010-06-10 10:21:31 +02003622 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
Kalle Vaload935682009-04-19 08:47:19 +03003623 }
Johannes Berga1678f82008-09-11 00:01:47 +02003624}
3625
Johannes Berg9c6bd792008-09-11 00:01:52 +02003626/* interface setup */
3627void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3628{
Johannes Berg46900292009-02-15 12:44:28 +01003629 struct ieee80211_if_managed *ifmgd;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003630
Johannes Berg46900292009-02-15 12:44:28 +01003631 ifmgd = &sdata->u.mgd;
Johannes Bergb291ba12009-07-10 15:29:03 +02003632 INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
Johannes Berg46900292009-02-15 12:44:28 +01003633 INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
Juuso Oikarinen1e4dcd02010-03-19 07:14:53 +02003634 INIT_WORK(&ifmgd->beacon_connection_loss_work,
3635 ieee80211_beacon_connection_loss_work);
Johannes Berg882a7c62012-08-01 22:32:45 +02003636 INIT_WORK(&ifmgd->csa_connection_drop_work,
3637 ieee80211_csa_connection_drop_work);
Johannes Bergd1f5b7a2010-08-05 17:05:55 +02003638 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
Johannes Berg46900292009-02-15 12:44:28 +01003639 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
Johannes Berg9c6bd792008-09-11 00:01:52 +02003640 (unsigned long) sdata);
Johannes Bergb291ba12009-07-10 15:29:03 +02003641 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
3642 (unsigned long) sdata);
3643 setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
3644 (unsigned long) sdata);
Johannes Berg46900292009-02-15 12:44:28 +01003645 setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
Sujithc481ec92009-01-06 09:28:37 +05303646 (unsigned long) sdata);
Johannes Berg9c6bd792008-09-11 00:01:52 +02003647
Johannes Bergab1faea2009-07-01 21:41:17 +02003648 ifmgd->flags = 0;
Mohammed Shafi Shajakhan1478acb2011-12-14 19:46:08 +05303649 ifmgd->powersave = sdata->wdev.ps;
Alexander Bondar219c3862013-01-22 16:52:23 +02003650 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
3651 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
Janusz Dziedzic67baf662013-03-21 15:47:56 +01003652 ifmgd->p2p_noa_index = -1;
Johannes Bergbbbdff92009-04-16 13:27:42 +02003653
Johannes Berg77fdaa12009-07-07 03:45:17 +02003654 mutex_init(&ifmgd->mtx);
Johannes Berg0f782312009-12-01 13:37:02 +01003655
3656 if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
3657 ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
3658 else
3659 ifmgd->req_smps = IEEE80211_SMPS_OFF;
Johannes Berg9c6bd792008-09-11 00:01:52 +02003660}
3661
3662/* scan finished notification */
Johannes Berg0a51b272008-09-08 17:44:25 +02003663void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
3664{
Johannes Berg31ee67a2012-07-06 21:18:24 +02003665 struct ieee80211_sub_if_data *sdata;
Johannes Berga1678f82008-09-11 00:01:47 +02003666
3667 /* Restart STA timers */
3668 rcu_read_lock();
Ben Greear370bd002013-03-19 17:50:50 -07003669 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
3670 if (ieee80211_sdata_running(sdata))
3671 ieee80211_restart_sta_timer(sdata);
3672 }
Johannes Berga1678f82008-09-11 00:01:47 +02003673 rcu_read_unlock();
Johannes Berg0a51b272008-09-08 17:44:25 +02003674}
Johannes Berg10f644a2009-04-16 13:17:25 +02003675
3676int ieee80211_max_network_latency(struct notifier_block *nb,
3677 unsigned long data, void *dummy)
3678{
3679 s32 latency_usec = (s32) data;
3680 struct ieee80211_local *local =
3681 container_of(nb, struct ieee80211_local,
3682 network_latency_notifier);
3683
3684 mutex_lock(&local->iflist_mtx);
3685 ieee80211_recalc_ps(local, latency_usec);
3686 mutex_unlock(&local->iflist_mtx);
3687
3688 return 0;
3689}
Johannes Berg77fdaa12009-07-07 03:45:17 +02003690
Johannes Bergf2d9d272012-11-22 14:11:39 +01003691static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
3692 struct cfg80211_bss *cbss)
3693{
3694 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3695 const u8 *ht_cap_ie, *vht_cap_ie;
3696 const struct ieee80211_ht_cap *ht_cap;
3697 const struct ieee80211_vht_cap *vht_cap;
3698 u8 chains = 1;
3699
3700 if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
3701 return chains;
3702
Johannes Berg9caf0362012-11-29 01:25:20 +01003703 ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003704 if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
3705 ht_cap = (void *)(ht_cap_ie + 2);
3706 chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
3707 /*
3708 * TODO: use "Tx Maximum Number Spatial Streams Supported" and
3709 * "Tx Unequal Modulation Supported" fields.
3710 */
3711 }
3712
3713 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
3714 return chains;
3715
Johannes Berg9caf0362012-11-29 01:25:20 +01003716 vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003717 if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
3718 u8 nss;
3719 u16 tx_mcs_map;
3720
3721 vht_cap = (void *)(vht_cap_ie + 2);
3722 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
3723 for (nss = 8; nss > 0; nss--) {
3724 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
3725 IEEE80211_VHT_MCS_NOT_SUPPORTED)
3726 break;
3727 }
3728 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
3729 chains = max(chains, nss);
3730 }
3731
3732 return chains;
3733}
3734
Johannes Bergb17166a2012-07-27 11:41:27 +02003735static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3736 struct cfg80211_bss *cbss)
Johannes Berga1cf7752012-03-08 15:02:07 +01003737{
3738 struct ieee80211_local *local = sdata->local;
3739 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg24398e32012-03-28 10:58:36 +02003740 const struct ieee80211_ht_operation *ht_oper = NULL;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003741 const struct ieee80211_vht_operation *vht_oper = NULL;
Johannes Berg24398e32012-03-28 10:58:36 +02003742 struct ieee80211_supported_band *sband;
Johannes Berg4bf88532012-11-09 11:39:59 +01003743 struct cfg80211_chan_def chandef;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003744 int ret;
Johannes Berga1cf7752012-03-08 15:02:07 +01003745
Johannes Berg24398e32012-03-28 10:58:36 +02003746 sband = local->hw.wiphy->bands[cbss->channel->band];
3747
Johannes Bergf2d9d272012-11-22 14:11:39 +01003748 ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
3749 IEEE80211_STA_DISABLE_80P80MHZ |
3750 IEEE80211_STA_DISABLE_160MHZ);
Johannes Berg24398e32012-03-28 10:58:36 +02003751
Johannes Berg9caf0362012-11-29 01:25:20 +01003752 rcu_read_lock();
3753
Johannes Bergf2d9d272012-11-22 14:11:39 +01003754 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
3755 sband->ht_cap.ht_supported) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01003756 const u8 *ht_oper_ie, *ht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003757
Johannes Berg9caf0362012-11-29 01:25:20 +01003758 ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
Johannes Berg24398e32012-03-28 10:58:36 +02003759 if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
3760 ht_oper = (void *)(ht_oper_ie + 2);
Johannes Berg08e6effa2013-02-07 23:33:32 +01003761
3762 ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
3763 if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
3764 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3765 ht_oper = NULL;
3766 }
Johannes Berg24398e32012-03-28 10:58:36 +02003767 }
3768
Johannes Bergf2d9d272012-11-22 14:11:39 +01003769 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
3770 sband->vht_cap.vht_supported) {
Johannes Berg08e6effa2013-02-07 23:33:32 +01003771 const u8 *vht_oper_ie, *vht_cap;
Johannes Bergf2d9d272012-11-22 14:11:39 +01003772
Johannes Berg9caf0362012-11-29 01:25:20 +01003773 vht_oper_ie = ieee80211_bss_get_ie(cbss,
3774 WLAN_EID_VHT_OPERATION);
Johannes Bergf2d9d272012-11-22 14:11:39 +01003775 if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
3776 vht_oper = (void *)(vht_oper_ie + 2);
3777 if (vht_oper && !ht_oper) {
3778 vht_oper = NULL;
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003779 sdata_info(sdata,
Johannes Bergf2d9d272012-11-22 14:11:39 +01003780 "AP advertised VHT without HT, disabling both\n");
Johannes Bergcb145022013-02-07 20:41:50 +01003781 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
3782 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg24398e32012-03-28 10:58:36 +02003783 }
Johannes Berg08e6effa2013-02-07 23:33:32 +01003784
3785 vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
3786 if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
3787 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
3788 vht_oper = NULL;
3789 }
Johannes Berg24398e32012-03-28 10:58:36 +02003790 }
3791
Johannes Bergf2d9d272012-11-22 14:11:39 +01003792 ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
3793 cbss->channel,
3794 ht_oper, vht_oper,
Johannes Berg30eb1dc2013-02-08 15:12:14 +01003795 &chandef, true);
Johannes Berg04ecd252012-09-11 14:34:12 +02003796
Johannes Bergf2d9d272012-11-22 14:11:39 +01003797 sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
3798 local->rx_chains);
Johannes Berg24398e32012-03-28 10:58:36 +02003799
Johannes Berg9caf0362012-11-29 01:25:20 +01003800 rcu_read_unlock();
3801
Johannes Berg04ecd252012-09-11 14:34:12 +02003802 /* will change later if needed */
3803 sdata->smps_mode = IEEE80211_SMPS_OFF;
3804
Johannes Bergf2d9d272012-11-22 14:11:39 +01003805 /*
3806 * If this fails (possibly due to channel context sharing
3807 * on incompatible channels, e.g. 80+80 and 160 sharing the
3808 * same control channel) try to use a smaller bandwidth.
3809 */
3810 ret = ieee80211_vif_use_channel(sdata, &chandef,
3811 IEEE80211_CHANCTX_SHARED);
Johannes Bergd601cd82013-02-07 20:54:51 +01003812 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
Johannes Bergf2d9d272012-11-22 14:11:39 +01003813 ifmgd->flags |= chandef_downgrade(&chandef);
Johannes Bergd601cd82013-02-07 20:54:51 +01003814 ret = ieee80211_vif_use_channel(sdata, &chandef,
3815 IEEE80211_CHANCTX_SHARED);
3816 }
Johannes Bergf2d9d272012-11-22 14:11:39 +01003817 return ret;
Johannes Bergb17166a2012-07-27 11:41:27 +02003818}
3819
3820static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
3821 struct cfg80211_bss *cbss, bool assoc)
3822{
3823 struct ieee80211_local *local = sdata->local;
3824 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3825 struct ieee80211_bss *bss = (void *)cbss->priv;
3826 struct sta_info *new_sta = NULL;
3827 bool have_sta = false;
3828 int err;
3829
3830 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
3831 return -EINVAL;
3832
3833 if (assoc) {
3834 rcu_read_lock();
3835 have_sta = sta_info_get(sdata, cbss->bssid);
3836 rcu_read_unlock();
3837 }
3838
3839 if (!have_sta) {
3840 new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
3841 if (!new_sta)
3842 return -ENOMEM;
3843 }
3844
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003845 if (new_sta) {
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003846 u32 rates = 0, basic_rates = 0;
3847 bool have_higher_than_11mbit;
3848 int min_rate = INT_MAX, min_rate_index = -1;
Johannes Bergb17166a2012-07-27 11:41:27 +02003849 struct ieee80211_supported_band *sband;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003850 const struct cfg80211_bss_ies *ies;
Johannes Bergb17166a2012-07-27 11:41:27 +02003851
3852 sband = local->hw.wiphy->bands[cbss->channel->band];
3853
3854 err = ieee80211_prep_channel(sdata, cbss);
3855 if (err) {
3856 sta_info_free(local, new_sta);
3857 return err;
3858 }
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003859
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003860 ieee80211_get_rates(sband, bss->supp_rates,
3861 bss->supp_rates_len,
3862 &rates, &basic_rates,
3863 &have_higher_than_11mbit,
3864 &min_rate, &min_rate_index);
3865
3866 /*
3867 * This used to be a workaround for basic rates missing
3868 * in the association response frame. Now that we no
3869 * longer use the basic rates from there, it probably
3870 * doesn't happen any more, but keep the workaround so
3871 * in case some *other* APs are buggy in different ways
3872 * we can connect -- with a warning.
3873 */
3874 if (!basic_rates && min_rate_index >= 0) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003875 sdata_info(sdata,
3876 "No basic rates, using min rate instead\n");
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003877 basic_rates = BIT(min_rate_index);
3878 }
3879
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003880 new_sta->sta.supp_rates[cbss->channel->band] = rates;
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003881 sdata->vif.bss_conf.basic_rates = basic_rates;
3882
3883 /* cf. IEEE 802.11 9.2.12 */
Johannes Berg55de9082012-07-26 17:24:39 +02003884 if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003885 have_higher_than_11mbit)
3886 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
3887 else
3888 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
3889
3890 memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
3891
Johannes Berg8c358bc2012-05-22 22:13:05 +02003892 /* set timing information */
3893 sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
Johannes Berg8cef2c92013-02-05 16:54:31 +01003894 rcu_read_lock();
Johannes Bergef429da2013-02-05 17:48:40 +01003895 ies = rcu_dereference(cbss->beacon_ies);
3896 if (ies) {
3897 const u8 *tim_ie;
3898
3899 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3900 sdata->vif.bss_conf.sync_device_ts =
3901 bss->device_ts_beacon;
3902 tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
3903 ies->data, ies->len);
3904 if (tim_ie && tim_ie[1] >= 2)
3905 sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
3906 else
3907 sdata->vif.bss_conf.sync_dtim_count = 0;
3908 } else if (!(local->hw.flags &
3909 IEEE80211_HW_TIMING_BEACON_ONLY)) {
3910 ies = rcu_dereference(cbss->proberesp_ies);
3911 /* must be non-NULL since beacon IEs were NULL */
3912 sdata->vif.bss_conf.sync_tsf = ies->tsf;
3913 sdata->vif.bss_conf.sync_device_ts =
3914 bss->device_ts_presp;
3915 sdata->vif.bss_conf.sync_dtim_count = 0;
3916 } else {
3917 sdata->vif.bss_conf.sync_tsf = 0;
3918 sdata->vif.bss_conf.sync_device_ts = 0;
3919 sdata->vif.bss_conf.sync_dtim_count = 0;
3920 }
Johannes Berg8cef2c92013-02-05 16:54:31 +01003921 rcu_read_unlock();
Johannes Berg8c358bc2012-05-22 22:13:05 +02003922
3923 /* tell driver about BSSID, basic rates and timing */
Johannes Berg5d6a1b02012-03-08 15:02:08 +01003924 ieee80211_bss_info_change_notify(sdata,
Johannes Berg8c358bc2012-05-22 22:13:05 +02003925 BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
3926 BSS_CHANGED_BEACON_INT);
Johannes Berga1cf7752012-03-08 15:02:07 +01003927
3928 if (assoc)
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003929 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
Johannes Berga1cf7752012-03-08 15:02:07 +01003930
Johannes Berg13e0c8e2012-07-27 10:43:16 +02003931 err = sta_info_insert(new_sta);
3932 new_sta = NULL;
Johannes Berga1cf7752012-03-08 15:02:07 +01003933 if (err) {
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02003934 sdata_info(sdata,
3935 "failed to insert STA entry for the AP (error %d)\n",
3936 err);
Johannes Berga1cf7752012-03-08 15:02:07 +01003937 return err;
3938 }
3939 } else
Joe Perchesb203ca32012-05-08 18:56:52 +00003940 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
Johannes Berga1cf7752012-03-08 15:02:07 +01003941
3942 return 0;
3943}
3944
Johannes Berg77fdaa12009-07-07 03:45:17 +02003945/* config hooks */
3946int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
3947 struct cfg80211_auth_request *req)
3948{
Johannes Berg66e67e42012-01-20 13:55:27 +01003949 struct ieee80211_local *local = sdata->local;
3950 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3951 struct ieee80211_mgd_auth_data *auth_data;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003952 u16 auth_alg;
Johannes Berg66e67e42012-01-20 13:55:27 +01003953 int err;
3954
3955 /* prepare auth data structure */
Johannes Berg77fdaa12009-07-07 03:45:17 +02003956
3957 switch (req->auth_type) {
3958 case NL80211_AUTHTYPE_OPEN_SYSTEM:
3959 auth_alg = WLAN_AUTH_OPEN;
3960 break;
3961 case NL80211_AUTHTYPE_SHARED_KEY:
Johannes Berg66e67e42012-01-20 13:55:27 +01003962 if (IS_ERR(local->wep_tx_tfm))
Johannes Berg9dca9c42010-07-21 10:09:25 +02003963 return -EOPNOTSUPP;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003964 auth_alg = WLAN_AUTH_SHARED_KEY;
3965 break;
3966 case NL80211_AUTHTYPE_FT:
3967 auth_alg = WLAN_AUTH_FT;
3968 break;
3969 case NL80211_AUTHTYPE_NETWORK_EAP:
3970 auth_alg = WLAN_AUTH_LEAP;
3971 break;
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003972 case NL80211_AUTHTYPE_SAE:
3973 auth_alg = WLAN_AUTH_SAE;
3974 break;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003975 default:
3976 return -EOPNOTSUPP;
3977 }
3978
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003979 auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
3980 req->ie_len, GFP_KERNEL);
Johannes Berg66e67e42012-01-20 13:55:27 +01003981 if (!auth_data)
Johannes Berg77fdaa12009-07-07 03:45:17 +02003982 return -ENOMEM;
3983
Johannes Berg66e67e42012-01-20 13:55:27 +01003984 auth_data->bss = req->bss;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003985
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003986 if (req->sae_data_len >= 4) {
3987 __le16 *pos = (__le16 *) req->sae_data;
3988 auth_data->sae_trans = le16_to_cpu(pos[0]);
3989 auth_data->sae_status = le16_to_cpu(pos[1]);
3990 memcpy(auth_data->data, req->sae_data + 4,
3991 req->sae_data_len - 4);
3992 auth_data->data_len += req->sae_data_len - 4;
3993 }
3994
Johannes Berg77fdaa12009-07-07 03:45:17 +02003995 if (req->ie && req->ie_len) {
Jouni Malinen6b8ece32012-09-30 19:29:40 +03003996 memcpy(&auth_data->data[auth_data->data_len],
3997 req->ie, req->ie_len);
3998 auth_data->data_len += req->ie_len;
Johannes Berg77fdaa12009-07-07 03:45:17 +02003999 }
4000
Johannes Bergfffd0932009-07-08 14:22:54 +02004001 if (req->key && req->key_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004002 auth_data->key_len = req->key_len;
4003 auth_data->key_idx = req->key_idx;
4004 memcpy(auth_data->key, req->key, req->key_len);
Johannes Bergfffd0932009-07-08 14:22:54 +02004005 }
4006
Johannes Berg66e67e42012-01-20 13:55:27 +01004007 auth_data->algorithm = auth_alg;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004008
Johannes Berg66e67e42012-01-20 13:55:27 +01004009 /* try to authenticate/probe */
Johannes Bergf679f652009-12-23 13:15:34 +01004010
Johannes Berg66e67e42012-01-20 13:55:27 +01004011 mutex_lock(&ifmgd->mtx);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004012
Johannes Berg66e67e42012-01-20 13:55:27 +01004013 if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
4014 ifmgd->assoc_data) {
4015 err = -EBUSY;
4016 goto err_free;
4017 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004018
Johannes Berg66e67e42012-01-20 13:55:27 +01004019 if (ifmgd->auth_data)
4020 ieee80211_destroy_auth_data(sdata, false);
Guy Eilam2a33bee2011-08-17 15:18:15 +03004021
Johannes Berg66e67e42012-01-20 13:55:27 +01004022 /* prep auth_data so we don't go into idle on disassoc */
4023 ifmgd->auth_data = auth_data;
4024
Johannes Berg7b119dc2013-04-10 21:38:36 +02004025 if (ifmgd->associated) {
4026 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4027
4028 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4029 WLAN_REASON_UNSPECIFIED,
4030 false, frame_buf);
4031
4032 __cfg80211_send_deauth(sdata->dev, frame_buf,
4033 sizeof(frame_buf));
4034 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004035
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004036 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004037
Johannes Berga1cf7752012-03-08 15:02:07 +01004038 err = ieee80211_prep_connection(sdata, req->bss, false);
4039 if (err)
Johannes Berg66e67e42012-01-20 13:55:27 +01004040 goto err_clear;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004041
Johannes Berg66e67e42012-01-20 13:55:27 +01004042 err = ieee80211_probe_auth(sdata);
4043 if (err) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004044 sta_info_destroy_addr(sdata, req->bss->bssid);
4045 goto err_clear;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004046 }
4047
Johannes Berg66e67e42012-01-20 13:55:27 +01004048 /* hold our own reference */
Johannes Berg5b112d32013-02-01 01:49:58 +01004049 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
Johannes Berg66e67e42012-01-20 13:55:27 +01004050 err = 0;
4051 goto out_unlock;
Johannes Berge5b900d2010-07-29 16:08:55 +02004052
Johannes Berg66e67e42012-01-20 13:55:27 +01004053 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004054 memset(ifmgd->bssid, 0, ETH_ALEN);
4055 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004056 ifmgd->auth_data = NULL;
4057 err_free:
4058 kfree(auth_data);
4059 out_unlock:
4060 mutex_unlock(&ifmgd->mtx);
Johannes Berge5b900d2010-07-29 16:08:55 +02004061
Johannes Berg66e67e42012-01-20 13:55:27 +01004062 return err;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004063}
4064
Johannes Berg77fdaa12009-07-07 03:45:17 +02004065int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4066 struct cfg80211_assoc_request *req)
4067{
Johannes Berg66e67e42012-01-20 13:55:27 +01004068 struct ieee80211_local *local = sdata->local;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004069 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004070 struct ieee80211_bss *bss = (void *)req->bss->priv;
Johannes Berg66e67e42012-01-20 13:55:27 +01004071 struct ieee80211_mgd_assoc_data *assoc_data;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004072 const struct cfg80211_bss_ies *beacon_ies;
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004073 struct ieee80211_supported_band *sband;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004074 const u8 *ssidie, *ht_ie, *vht_ie;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004075 int i, err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004076
Johannes Berg66e67e42012-01-20 13:55:27 +01004077 assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
4078 if (!assoc_data)
Johannes Bergaf6b6372009-12-23 13:15:35 +01004079 return -ENOMEM;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004080
Johannes Berg9caf0362012-11-29 01:25:20 +01004081 rcu_read_lock();
4082 ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
4083 if (!ssidie) {
4084 rcu_read_unlock();
4085 kfree(assoc_data);
4086 return -EINVAL;
4087 }
4088 memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
4089 assoc_data->ssid_len = ssidie[1];
4090 rcu_read_unlock();
4091
Johannes Berg66e67e42012-01-20 13:55:27 +01004092 mutex_lock(&ifmgd->mtx);
4093
Johannes Berg7b119dc2013-04-10 21:38:36 +02004094 if (ifmgd->associated) {
4095 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
4096
4097 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4098 WLAN_REASON_UNSPECIFIED,
4099 false, frame_buf);
4100
4101 __cfg80211_send_deauth(sdata->dev, frame_buf,
4102 sizeof(frame_buf));
4103 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004104
4105 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
4106 err = -EBUSY;
4107 goto err_free;
Guy Eilam2a33bee2011-08-17 15:18:15 +03004108 }
4109
Johannes Berg66e67e42012-01-20 13:55:27 +01004110 if (ifmgd->assoc_data) {
4111 err = -EBUSY;
4112 goto err_free;
4113 }
4114
4115 if (ifmgd->auth_data) {
4116 bool match;
4117
4118 /* keep sta info, bssid if matching */
Joe Perchesb203ca32012-05-08 18:56:52 +00004119 match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
Johannes Berg66e67e42012-01-20 13:55:27 +01004120 ieee80211_destroy_auth_data(sdata, match);
4121 }
4122
4123 /* prepare assoc data */
Johannes Berg19c3b832012-08-01 20:13:36 +02004124
Juuso Oikarinend8ec4432010-10-01 16:02:31 +03004125 ifmgd->beacon_crc_valid = false;
4126
Johannes Bergde5036a2012-03-08 15:02:03 +01004127 /*
4128 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4129 * We still associate in non-HT mode (11a/b/g) if any one of these
4130 * ciphers is configured as pairwise.
4131 * We can set this to true for non-11n hardware, that'll be checked
4132 * separately along with the peer capabilities.
4133 */
Johannes Berg1c4cb922012-05-30 15:57:00 +02004134 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004135 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
4136 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004137 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
Johannes Berga8243b72012-11-22 14:32:09 +01004138 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004139 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1c4cb922012-05-30 15:57:00 +02004140 netdev_info(sdata->dev,
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004141 "disabling HT/VHT due to WEP/TKIP use\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004142 }
4143 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004144
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004145 if (req->flags & ASSOC_REQ_DISABLE_HT) {
Johannes Berga8243b72012-11-22 14:32:09 +01004146 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004147 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4148 }
Ben Greearef96a8422011-11-18 11:32:00 -08004149
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004150 if (req->flags & ASSOC_REQ_DISABLE_VHT)
4151 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4152
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004153 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4154 sband = local->hw.wiphy->bands[req->bss->channel->band];
4155 if (!sband->ht_cap.ht_supported ||
Johannes Berg1c4cb922012-05-30 15:57:00 +02004156 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
Johannes Berga8243b72012-11-22 14:32:09 +01004157 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004158 if (!bss->wmm_used)
4159 netdev_info(sdata->dev,
4160 "disabling HT as WMM/QoS is not supported by the AP\n");
Johannes Berg1c4cb922012-05-30 15:57:00 +02004161 }
Johannes Berg4e74bfd2012-03-08 15:02:04 +01004162
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004163 /* disable VHT if we don't support it or the AP doesn't use WMM */
4164 if (!sband->vht_cap.vht_supported ||
4165 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
4166 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg1b49de22012-07-27 10:29:14 +02004167 if (!bss->wmm_used)
4168 netdev_info(sdata->dev,
4169 "disabling VHT as WMM/QoS is not supported by the AP\n");
Mahesh Palivelad545dab2012-07-24 03:33:10 +00004170 }
4171
Ben Greearef96a8422011-11-18 11:32:00 -08004172 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
4173 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
4174 sizeof(ifmgd->ht_capa_mask));
4175
Johannes Bergdd5ecfe2013-02-21 17:40:19 +01004176 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
4177 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
4178 sizeof(ifmgd->vht_capa_mask));
4179
Johannes Berg77fdaa12009-07-07 03:45:17 +02004180 if (req->ie && req->ie_len) {
Johannes Berg66e67e42012-01-20 13:55:27 +01004181 memcpy(assoc_data->ie, req->ie, req->ie_len);
4182 assoc_data->ie_len = req->ie_len;
4183 }
Johannes Berg77fdaa12009-07-07 03:45:17 +02004184
Johannes Berg66e67e42012-01-20 13:55:27 +01004185 assoc_data->bss = req->bss;
Johannes Bergf679f652009-12-23 13:15:34 +01004186
Johannes Bergaf6b6372009-12-23 13:15:35 +01004187 if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
4188 if (ifmgd->powersave)
Johannes Berg04ecd252012-09-11 14:34:12 +02004189 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004190 else
Johannes Berg04ecd252012-09-11 14:34:12 +02004191 sdata->smps_mode = IEEE80211_SMPS_OFF;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004192 } else
Johannes Berg04ecd252012-09-11 14:34:12 +02004193 sdata->smps_mode = ifmgd->req_smps;
Johannes Bergaf6b6372009-12-23 13:15:35 +01004194
Johannes Berg66e67e42012-01-20 13:55:27 +01004195 assoc_data->capability = req->bss->capability;
Johannes Berg32c50572012-03-28 11:04:29 +02004196 assoc_data->wmm = bss->wmm_used &&
4197 (local->hw.queues >= IEEE80211_NUM_ACS);
Johannes Berg66e67e42012-01-20 13:55:27 +01004198 assoc_data->supp_rates = bss->supp_rates;
4199 assoc_data->supp_rates_len = bss->supp_rates_len;
Johannes Berg9dde6422012-05-16 23:43:19 +02004200
Johannes Berg9caf0362012-11-29 01:25:20 +01004201 rcu_read_lock();
Johannes Berg9dde6422012-05-16 23:43:19 +02004202 ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
4203 if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
4204 assoc_data->ap_ht_param =
4205 ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
4206 else
Johannes Berga8243b72012-11-22 14:32:09 +01004207 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
Johannes Bergb08fbbd2012-12-07 13:06:48 +01004208 vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
4209 if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
4210 memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
4211 sizeof(struct ieee80211_vht_cap));
4212 else
4213 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
Johannes Berg9caf0362012-11-29 01:25:20 +01004214 rcu_read_unlock();
Johannes Berg63f170e2009-12-23 13:15:33 +01004215
Kalle Valoab133152010-01-12 10:42:31 +02004216 if (bss->wmm_used && bss->uapsd_supported &&
Alexander Bondar24aa11a2013-04-02 15:30:14 +03004217 (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
4218 sdata->wmm_acm != 0xff) {
Johannes Berg76f03032012-03-08 15:02:05 +01004219 assoc_data->uapsd = true;
Kalle Valoab133152010-01-12 10:42:31 +02004220 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
4221 } else {
Johannes Berg76f03032012-03-08 15:02:05 +01004222 assoc_data->uapsd = false;
Kalle Valoab133152010-01-12 10:42:31 +02004223 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
4224 }
4225
Johannes Berg77fdaa12009-07-07 03:45:17 +02004226 if (req->prev_bssid)
Johannes Berg66e67e42012-01-20 13:55:27 +01004227 memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004228
4229 if (req->use_mfp) {
4230 ifmgd->mfp = IEEE80211_MFP_REQUIRED;
4231 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
4232 } else {
4233 ifmgd->mfp = IEEE80211_MFP_DISABLED;
4234 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
4235 }
4236
4237 if (req->crypto.control_port)
4238 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
4239 else
4240 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
4241
Johannes Berga621fa42010-08-27 14:26:54 +03004242 sdata->control_port_protocol = req->crypto.control_port_ethertype;
4243 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
4244
Johannes Berg66e67e42012-01-20 13:55:27 +01004245 /* kick off associate process */
4246
4247 ifmgd->assoc_data = assoc_data;
Johannes Berg826262c2012-12-10 16:38:14 +02004248 ifmgd->dtim_period = 0;
Johannes Berg66e67e42012-01-20 13:55:27 +01004249
Johannes Berga1cf7752012-03-08 15:02:07 +01004250 err = ieee80211_prep_connection(sdata, req->bss, true);
4251 if (err)
4252 goto err_clear;
Johannes Berg66e67e42012-01-20 13:55:27 +01004253
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004254 rcu_read_lock();
4255 beacon_ies = rcu_dereference(req->bss->beacon_ies);
Johannes Berg826262c2012-12-10 16:38:14 +02004256
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004257 if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
4258 !beacon_ies) {
4259 /*
4260 * Wait up to one beacon interval ...
4261 * should this be more if we miss one?
4262 */
4263 sdata_info(sdata, "waiting for beacon from %pM\n",
4264 ifmgd->bssid);
4265 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
Johannes Berg89afe612013-02-13 15:39:57 +01004266 assoc_data->timeout_started = true;
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004267 assoc_data->need_beacon = true;
4268 } else if (beacon_ies) {
4269 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
4270 beacon_ies->data,
4271 beacon_ies->len);
Johannes Bergef429da2013-02-05 17:48:40 +01004272 u8 dtim_count = 0;
4273
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004274 if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
4275 const struct ieee80211_tim_ie *tim;
4276 tim = (void *)(tim_ie + 2);
4277 ifmgd->dtim_period = tim->dtim_period;
Johannes Bergef429da2013-02-05 17:48:40 +01004278 dtim_count = tim->dtim_count;
Johannes Berg826262c2012-12-10 16:38:14 +02004279 }
Johannes Berg66e67e42012-01-20 13:55:27 +01004280 assoc_data->have_beacon = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004281 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004282 assoc_data->timeout_started = true;
Johannes Bergef429da2013-02-05 17:48:40 +01004283
4284 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
4285 sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
4286 sdata->vif.bss_conf.sync_device_ts =
4287 bss->device_ts_beacon;
4288 sdata->vif.bss_conf.sync_dtim_count = dtim_count;
4289 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004290 } else {
4291 assoc_data->timeout = jiffies;
Johannes Berg89afe612013-02-13 15:39:57 +01004292 assoc_data->timeout_started = true;
Johannes Berg66e67e42012-01-20 13:55:27 +01004293 }
Emmanuel Grumbachc65dd142012-12-12 10:12:24 +02004294 rcu_read_unlock();
4295
Johannes Berg66e67e42012-01-20 13:55:27 +01004296 run_again(ifmgd, assoc_data->timeout);
4297
Paul Stewartfcff4f12012-02-23 17:59:53 -08004298 if (bss->corrupt_data) {
4299 char *corrupt_type = "data";
4300 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
4301 if (bss->corrupt_data &
4302 IEEE80211_BSS_CORRUPT_PROBE_RESP)
4303 corrupt_type = "beacon and probe response";
4304 else
4305 corrupt_type = "beacon";
4306 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
4307 corrupt_type = "probe response";
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004308 sdata_info(sdata, "associating with AP with corrupt %s\n",
4309 corrupt_type);
Paul Stewartfcff4f12012-02-23 17:59:53 -08004310 }
4311
Johannes Berg66e67e42012-01-20 13:55:27 +01004312 err = 0;
4313 goto out;
4314 err_clear:
Eliad Peller3d2abdf2012-09-04 17:44:45 +03004315 memset(ifmgd->bssid, 0, ETH_ALEN);
4316 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
Johannes Berg66e67e42012-01-20 13:55:27 +01004317 ifmgd->assoc_data = NULL;
4318 err_free:
4319 kfree(assoc_data);
4320 out:
4321 mutex_unlock(&ifmgd->mtx);
4322
4323 return err;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004324}
4325
4326int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004327 struct cfg80211_deauth_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004328{
4329 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004330 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004331 bool tx = !req->local_state_change;
Johannes Berg86552012012-10-29 09:46:31 +01004332 bool sent_frame = false;
Johannes Berg77fdaa12009-07-07 03:45:17 +02004333
Johannes Berg77fdaa12009-07-07 03:45:17 +02004334 mutex_lock(&ifmgd->mtx);
4335
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004336 sdata_info(sdata,
4337 "deauthenticating from %pM by local choice (reason=%d)\n",
4338 req->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004339
Johannes Berg86552012012-10-29 09:46:31 +01004340 if (ifmgd->auth_data) {
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004341 drv_mgd_prepare_tx(sdata->local, sdata);
Johannes Berg37ad3882012-02-24 13:50:54 +01004342 ieee80211_send_deauth_disassoc(sdata, req->bssid,
4343 IEEE80211_STYPE_DEAUTH,
Stanislaw Gruszka68632552012-10-15 14:52:41 +02004344 req->reason_code, tx,
Johannes Berg37ad3882012-02-24 13:50:54 +01004345 frame_buf);
Johannes Berg86552012012-10-29 09:46:31 +01004346 ieee80211_destroy_auth_data(sdata, false);
4347 mutex_unlock(&ifmgd->mtx);
4348
4349 sent_frame = tx;
4350 goto out;
Emmanuel Grumbach8c7d8572012-07-25 01:42:36 +03004351 }
4352
Johannes Berg86552012012-10-29 09:46:31 +01004353 if (ifmgd->associated &&
4354 ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
4355 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4356 req->reason_code, tx, frame_buf);
4357 sent_frame = tx;
4358 }
Johannes Berg37ad3882012-02-24 13:50:54 +01004359 mutex_unlock(&ifmgd->mtx);
4360
Johannes Berg86552012012-10-29 09:46:31 +01004361 out:
Johannes Berg86552012012-10-29 09:46:31 +01004362 if (sent_frame)
4363 __cfg80211_send_deauth(sdata->dev, frame_buf,
4364 IEEE80211_DEAUTH_FRAME_LEN);
4365
Johannes Berg77fdaa12009-07-07 03:45:17 +02004366 return 0;
4367}
4368
4369int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
Johannes Berg63c9c5e2012-02-24 13:50:51 +01004370 struct cfg80211_disassoc_request *req)
Johannes Berg77fdaa12009-07-07 03:45:17 +02004371{
4372 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
Jouni Malinene69e95d2010-03-29 23:29:31 -07004373 u8 bssid[ETH_ALEN];
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004374 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
Johannes Berg77fdaa12009-07-07 03:45:17 +02004375
Johannes Berg77fdaa12009-07-07 03:45:17 +02004376 mutex_lock(&ifmgd->mtx);
4377
Johannes Berg8d8b2612009-07-25 11:58:36 +02004378 /*
4379 * cfg80211 should catch this ... but it's racy since
4380 * we can receive a disassoc frame, process it, hand it
4381 * to cfg80211 while that's in a locked section already
4382 * trying to tell us that the user wants to disconnect.
4383 */
Johannes Berg0c1ad2c2009-12-23 13:15:39 +01004384 if (ifmgd->associated != req->bss) {
Johannes Berg77fdaa12009-07-07 03:45:17 +02004385 mutex_unlock(&ifmgd->mtx);
4386 return -ENOLINK;
4387 }
4388
Johannes Bergbdcbd8e2012-06-22 11:29:50 +02004389 sdata_info(sdata,
4390 "disassociating from %pM by local choice (reason=%d)\n",
4391 req->bss->bssid, req->reason_code);
Johannes Berg0ff71612009-09-26 14:45:41 +02004392
Jouni Malinene69e95d2010-03-29 23:29:31 -07004393 memcpy(bssid, req->bss->bssid, ETH_ALEN);
Johannes Berg37ad3882012-02-24 13:50:54 +01004394 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
4395 req->reason_code, !req->local_state_change,
4396 frame_buf);
Johannes Berg77fdaa12009-07-07 03:45:17 +02004397 mutex_unlock(&ifmgd->mtx);
4398
Antonio Quartulli6ae16772012-09-07 13:28:52 +02004399 __cfg80211_send_disassoc(sdata->dev, frame_buf,
4400 IEEE80211_DEAUTH_FRAME_LEN);
Johannes Bergbc83b682009-11-29 12:19:06 +01004401
Johannes Berg77fdaa12009-07-07 03:45:17 +02004402 return 0;
4403}
Jouni Malinen026331c2010-02-15 12:53:10 +02004404
Eliad Pellerafa762f2012-04-23 14:45:15 +03004405void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004406{
4407 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
4408
Ben Greear49921852013-02-20 09:41:09 -08004409 /*
4410 * Make sure some work items will not run after this,
4411 * they will not do anything but might not have been
4412 * cancelled when disconnecting.
4413 */
4414 cancel_work_sync(&ifmgd->monitor_work);
4415 cancel_work_sync(&ifmgd->beacon_connection_loss_work);
4416 cancel_work_sync(&ifmgd->request_smps_work);
4417 cancel_work_sync(&ifmgd->csa_connection_drop_work);
4418 cancel_work_sync(&ifmgd->chswitch_work);
4419
Johannes Berg54e4ffb2012-02-25 21:48:08 +01004420 mutex_lock(&ifmgd->mtx);
4421 if (ifmgd->assoc_data)
4422 ieee80211_destroy_assoc_data(sdata, false);
4423 if (ifmgd->auth_data)
4424 ieee80211_destroy_auth_data(sdata, false);
4425 del_timer_sync(&ifmgd->timer);
4426 mutex_unlock(&ifmgd->mtx);
4427}
4428
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004429void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
4430 enum nl80211_cqm_rssi_threshold_event rssi_event,
4431 gfp_t gfp)
4432{
4433 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4434
Johannes Bergb5878a22010-04-07 16:48:40 +02004435 trace_api_cqm_rssi_notify(sdata, rssi_event);
4436
Juuso Oikarinena97c13c2010-03-23 09:02:34 +02004437 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
4438}
4439EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);