blob: fc88e2e2f9235611b9200f068886521fcfb40324 [file] [log] [blame]
Johannes Berg0a51b272008-09-08 17:44:25 +02001/*
Johannes Berg5484e232008-09-08 17:44:27 +02002 * Scanning implementation
3 *
Johannes Berg0a51b272008-09-08 17:44:25 +02004 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Johannes Berg5484e232008-09-08 17:44:27 +020015/* TODO:
Johannes Berg2a519312009-02-10 21:25:55 +010016 * figure out how to avoid that the "current BSS" expires
Johannes Berg99cf5f52009-02-10 21:25:56 +010017 * use cfg80211's BSS handling
Johannes Berg5484e232008-09-08 17:44:27 +020018 */
19
Johannes Berg0a51b272008-09-08 17:44:25 +020020#include <linux/wireless.h>
21#include <linux/if_arp.h>
Johannes Berg078e1e62009-01-22 18:07:31 +010022#include <linux/rtnetlink.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020023#include <net/mac80211.h>
24#include <net/iw_handler.h>
25
26#include "ieee80211_i.h"
Johannes Berg5484e232008-09-08 17:44:27 +020027#include "mesh.h"
Johannes Berg0a51b272008-09-08 17:44:25 +020028
29#define IEEE80211_PROBE_DELAY (HZ / 33)
30#define IEEE80211_CHANNEL_TIME (HZ / 33)
31#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
32
Johannes Berg5484e232008-09-08 17:44:27 +020033void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
34{
Johannes Bergc2b13452008-09-11 00:01:55 +020035 spin_lock_init(&local->bss_lock);
36 INIT_LIST_HEAD(&local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +020037}
38
39void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
40{
Johannes Bergc2b13452008-09-11 00:01:55 +020041 struct ieee80211_bss *bss, *tmp;
Johannes Berg5484e232008-09-08 17:44:27 +020042
Johannes Bergc2b13452008-09-11 00:01:55 +020043 list_for_each_entry_safe(bss, tmp, &local->bss_list, list)
Johannes Berg5484e232008-09-08 17:44:27 +020044 ieee80211_rx_bss_put(local, bss);
45}
46
Johannes Bergc2b13452008-09-11 00:01:55 +020047struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +020048ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
49 u8 *ssid, u8 ssid_len)
50{
Johannes Bergc2b13452008-09-11 00:01:55 +020051 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +020052
Johannes Bergc2b13452008-09-11 00:01:55 +020053 spin_lock_bh(&local->bss_lock);
54 bss = local->bss_hash[STA_HASH(bssid)];
Johannes Berg5484e232008-09-08 17:44:27 +020055 while (bss) {
56 if (!bss_mesh_cfg(bss) &&
57 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
58 bss->freq == freq &&
59 bss->ssid_len == ssid_len &&
60 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
61 atomic_inc(&bss->users);
62 break;
63 }
64 bss = bss->hnext;
65 }
Johannes Bergc2b13452008-09-11 00:01:55 +020066 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +020067 return bss;
68}
69
Johannes Bergc2b13452008-09-11 00:01:55 +020070/* Caller must hold local->bss_lock */
Johannes Berg5484e232008-09-08 17:44:27 +020071static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020072 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020073{
74 u8 hash_idx;
75
76 if (bss_mesh_cfg(bss))
77 hash_idx = mesh_id_hash(bss_mesh_id(bss),
78 bss_mesh_id_len(bss));
79 else
80 hash_idx = STA_HASH(bss->bssid);
81
Johannes Bergc2b13452008-09-11 00:01:55 +020082 bss->hnext = local->bss_hash[hash_idx];
83 local->bss_hash[hash_idx] = bss;
Johannes Berg5484e232008-09-08 17:44:27 +020084}
85
Johannes Bergc2b13452008-09-11 00:01:55 +020086/* Caller must hold local->bss_lock */
Johannes Berg5484e232008-09-08 17:44:27 +020087static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020088 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020089{
Johannes Bergc2b13452008-09-11 00:01:55 +020090 struct ieee80211_bss *b, *prev = NULL;
91 b = local->bss_hash[STA_HASH(bss->bssid)];
Johannes Berg5484e232008-09-08 17:44:27 +020092 while (b) {
93 if (b == bss) {
94 if (!prev)
Johannes Bergc2b13452008-09-11 00:01:55 +020095 local->bss_hash[STA_HASH(bss->bssid)] =
Johannes Berg5484e232008-09-08 17:44:27 +020096 bss->hnext;
97 else
98 prev->hnext = bss->hnext;
99 break;
100 }
101 prev = b;
102 b = b->hnext;
103 }
104}
105
Johannes Berg99cf5f52009-02-10 21:25:56 +0100106static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200107ieee80211_rx_bss_add(struct ieee80211_local *local, u8 *bssid, int freq,
108 u8 *ssid, u8 ssid_len)
109{
Johannes Bergc2b13452008-09-11 00:01:55 +0200110 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200111
112 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
113 if (!bss)
114 return NULL;
115 atomic_set(&bss->users, 2);
116 memcpy(bss->bssid, bssid, ETH_ALEN);
117 bss->freq = freq;
118 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
119 memcpy(bss->ssid, ssid, ssid_len);
120 bss->ssid_len = ssid_len;
121 }
122
Johannes Bergc2b13452008-09-11 00:01:55 +0200123 spin_lock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200124 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200125 list_add_tail(&bss->list, &local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +0200126 __ieee80211_rx_bss_hash_add(local, bss);
Johannes Bergc2b13452008-09-11 00:01:55 +0200127 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200128 return bss;
129}
130
131#ifdef CONFIG_MAC80211_MESH
Johannes Bergc2b13452008-09-11 00:01:55 +0200132static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200133ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
134 u8 *mesh_cfg, int freq)
135{
Johannes Bergc2b13452008-09-11 00:01:55 +0200136 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200137
Johannes Bergc2b13452008-09-11 00:01:55 +0200138 spin_lock_bh(&local->bss_lock);
139 bss = local->bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
Johannes Berg5484e232008-09-08 17:44:27 +0200140 while (bss) {
141 if (bss_mesh_cfg(bss) &&
142 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
143 bss->freq == freq &&
144 mesh_id_len == bss->mesh_id_len &&
145 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
146 mesh_id_len))) {
147 atomic_inc(&bss->users);
148 break;
149 }
150 bss = bss->hnext;
151 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200152 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200153 return bss;
154}
155
Johannes Bergc2b13452008-09-11 00:01:55 +0200156static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200157ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
158 u8 *mesh_cfg, int mesh_config_len, int freq)
159{
Johannes Bergc2b13452008-09-11 00:01:55 +0200160 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200161
Johannes Berg1239cd52008-10-28 11:12:57 +0100162 if (mesh_config_len != IEEE80211_MESH_CONFIG_LEN)
Johannes Berg5484e232008-09-08 17:44:27 +0200163 return NULL;
164
165 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
166 if (!bss)
167 return NULL;
168
169 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
170 if (!bss->mesh_cfg) {
171 kfree(bss);
172 return NULL;
173 }
174
175 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
176 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
177 if (!bss->mesh_id) {
178 kfree(bss->mesh_cfg);
179 kfree(bss);
180 return NULL;
181 }
182 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
183 }
184
185 atomic_set(&bss->users, 2);
186 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
187 bss->mesh_id_len = mesh_id_len;
188 bss->freq = freq;
Johannes Bergc2b13452008-09-11 00:01:55 +0200189 spin_lock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200190 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200191 list_add_tail(&bss->list, &local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +0200192 __ieee80211_rx_bss_hash_add(local, bss);
Johannes Bergc2b13452008-09-11 00:01:55 +0200193 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200194 return bss;
195}
196#endif
197
Johannes Bergc2b13452008-09-11 00:01:55 +0200198static void ieee80211_rx_bss_free(struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +0200199{
200 kfree(bss->ies);
201 kfree(bss_mesh_id(bss));
202 kfree(bss_mesh_cfg(bss));
203 kfree(bss);
204}
205
206void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +0200207 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +0200208{
209 local_bh_disable();
Johannes Bergc2b13452008-09-11 00:01:55 +0200210 if (!atomic_dec_and_lock(&bss->users, &local->bss_lock)) {
Johannes Berg5484e232008-09-08 17:44:27 +0200211 local_bh_enable();
212 return;
213 }
214
215 __ieee80211_rx_bss_hash_del(local, bss);
216 list_del(&bss->list);
Johannes Bergc2b13452008-09-11 00:01:55 +0200217 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200218 ieee80211_rx_bss_free(bss);
219}
220
Johannes Bergc2b13452008-09-11 00:01:55 +0200221struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200222ieee80211_bss_info_update(struct ieee80211_local *local,
223 struct ieee80211_rx_status *rx_status,
224 struct ieee80211_mgmt *mgmt,
225 size_t len,
226 struct ieee802_11_elems *elems,
Johannes Berg2a519312009-02-10 21:25:55 +0100227 struct ieee80211_channel *channel,
228 bool beacon)
Johannes Berg5484e232008-09-08 17:44:27 +0200229{
Johannes Bergc2b13452008-09-11 00:01:55 +0200230 struct ieee80211_bss *bss;
Johannes Berg2a519312009-02-10 21:25:55 +0100231 int clen, freq = channel->center_freq;
232 enum cfg80211_signal_type sigtype = CFG80211_SIGNAL_TYPE_NONE;
233 s32 signal = 0;
234
235 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
236 sigtype = CFG80211_SIGNAL_TYPE_MBM;
237 signal = rx_status->signal * 100;
238 } else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
239 sigtype = CFG80211_SIGNAL_TYPE_UNSPEC;
240 signal = (rx_status->signal * 100) / local->hw.max_signal;
241 }
242
243 cfg80211_put_bss(
244 cfg80211_inform_bss_frame(local->hw.wiphy, channel,
245 mgmt, len, signal, sigtype,
246 GFP_ATOMIC));
Johannes Berg5484e232008-09-08 17:44:27 +0200247
248#ifdef CONFIG_MAC80211_MESH
249 if (elems->mesh_config)
250 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
251 elems->mesh_id_len, elems->mesh_config, freq);
252 else
253#endif
254 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
255 elems->ssid, elems->ssid_len);
256 if (!bss) {
257#ifdef CONFIG_MAC80211_MESH
258 if (elems->mesh_config)
259 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
260 elems->mesh_id_len, elems->mesh_config,
261 elems->mesh_config_len, freq);
262 else
263#endif
264 bss = ieee80211_rx_bss_add(local, mgmt->bssid, freq,
265 elems->ssid, elems->ssid_len);
266 if (!bss)
267 return NULL;
268 } else {
269#if 0
270 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200271 spin_lock_bh(&local->bss_lock);
272 list_move_tail(&bss->list, &local->bss_list);
273 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200274#endif
275 }
276
277 /* save the ERP value so that it is available at association time */
278 if (elems->erp_info && elems->erp_info_len >= 1) {
279 bss->erp_value = elems->erp_info[0];
280 bss->has_erp_value = 1;
281 }
282
283 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
284 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
285
286 if (elems->tim) {
287 struct ieee80211_tim_ie *tim_ie =
288 (struct ieee80211_tim_ie *)elems->tim;
289 bss->dtim_period = tim_ie->dtim_period;
290 }
291
292 /* set default value for buggy APs */
293 if (!elems->tim || bss->dtim_period == 0)
294 bss->dtim_period = 1;
295
296 bss->supp_rates_len = 0;
297 if (elems->supp_rates) {
298 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
299 if (clen > elems->supp_rates_len)
300 clen = elems->supp_rates_len;
301 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
302 clen);
303 bss->supp_rates_len += clen;
304 }
305 if (elems->ext_supp_rates) {
306 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
307 if (clen > elems->ext_supp_rates_len)
308 clen = elems->ext_supp_rates_len;
309 memcpy(&bss->supp_rates[bss->supp_rates_len],
310 elems->ext_supp_rates, clen);
311 bss->supp_rates_len += clen;
312 }
313
314 bss->band = rx_status->band;
315
316 bss->timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
317 bss->last_update = jiffies;
318 bss->signal = rx_status->signal;
319 bss->noise = rx_status->noise;
320 bss->qual = rx_status->qual;
321 bss->wmm_used = elems->wmm_param || elems->wmm_info;
322
323 if (!beacon)
324 bss->last_probe_resp = jiffies;
325
326 /*
327 * For probe responses, or if we don't have any information yet,
328 * use the IEs from the beacon.
329 */
330 if (!bss->ies || !beacon) {
331 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
332 kfree(bss->ies);
333 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
334 }
335 if (bss->ies) {
336 memcpy(bss->ies, elems->ie_start, elems->total_len);
337 bss->ies_len = elems->total_len;
338 } else
339 bss->ies_len = 0;
340 }
341
342 return bss;
343}
Johannes Berg0a51b272008-09-08 17:44:25 +0200344
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +0530345void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
346 int freq, u8 *ssid, u8 ssid_len)
347{
348 struct ieee80211_bss *bss;
349 struct ieee80211_local *local = sdata->local;
350
351 bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
352 if (bss) {
353 atomic_dec(&bss->users);
354 ieee80211_rx_bss_put(local, bss);
355 }
356}
357
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200358ieee80211_rx_result
Johannes Bergc2b13452008-09-11 00:01:55 +0200359ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
360 struct ieee80211_rx_status *rx_status)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200361{
362 struct ieee80211_mgmt *mgmt;
Johannes Bergc2b13452008-09-11 00:01:55 +0200363 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200364 u8 *elements;
365 struct ieee80211_channel *channel;
366 size_t baselen;
367 int freq;
368 __le16 fc;
369 bool presp, beacon = false;
370 struct ieee802_11_elems elems;
371
372 if (skb->len < 2)
373 return RX_DROP_UNUSABLE;
374
375 mgmt = (struct ieee80211_mgmt *) skb->data;
376 fc = mgmt->frame_control;
377
378 if (ieee80211_is_ctl(fc))
379 return RX_CONTINUE;
380
381 if (skb->len < 24)
382 return RX_DROP_MONITOR;
383
384 presp = ieee80211_is_probe_resp(fc);
385 if (presp) {
386 /* ignore ProbeResp to foreign address */
387 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
388 return RX_DROP_MONITOR;
389
390 presp = true;
391 elements = mgmt->u.probe_resp.variable;
392 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
393 } else {
394 beacon = ieee80211_is_beacon(fc);
395 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
396 elements = mgmt->u.beacon.variable;
397 }
398
399 if (!presp && !beacon)
400 return RX_CONTINUE;
401
402 if (baselen > skb->len)
403 return RX_DROP_MONITOR;
404
405 ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
406
407 if (elems.ds_params && elems.ds_params_len == 1)
408 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
409 else
410 freq = rx_status->freq;
411
412 channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
413
414 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
415 return RX_DROP_MONITOR;
416
417 bss = ieee80211_bss_info_update(sdata->local, rx_status,
418 mgmt, skb->len, &elems,
Johannes Berg2a519312009-02-10 21:25:55 +0100419 channel, beacon);
Jouni Malinend048e502008-10-11 03:29:55 +0300420 if (bss)
421 ieee80211_rx_bss_put(sdata->local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200422
423 dev_kfree_skb(skb);
424 return RX_QUEUED;
425}
426
Vivek Natarajana97b77b2008-12-23 18:39:02 -0800427void ieee80211_send_nullfunc(struct ieee80211_local *local,
Johannes Berg0a51b272008-09-08 17:44:25 +0200428 struct ieee80211_sub_if_data *sdata,
429 int powersave)
430{
431 struct sk_buff *skb;
432 struct ieee80211_hdr *nullfunc;
433 __le16 fc;
434
435 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
436 if (!skb) {
437 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
438 "frame\n", sdata->dev->name);
439 return;
440 }
441 skb_reserve(skb, local->hw.extra_tx_headroom);
442
443 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
444 memset(nullfunc, 0, 24);
445 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
446 IEEE80211_FCTL_TODS);
447 if (powersave)
448 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
449 nullfunc->frame_control = fc;
450 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
451 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
452 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
453
Johannes Berge50db652008-09-09 15:07:09 +0200454 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200455}
456
Johannes Berg2a519312009-02-10 21:25:55 +0100457void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200458{
459 struct ieee80211_local *local = hw_to_local(hw);
460 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200461
Johannes Bergc2b13452008-09-11 00:01:55 +0200462 if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
Johannes Berg5bc75722008-09-11 00:01:51 +0200463 return;
464
Johannes Berg2a519312009-02-10 21:25:55 +0100465 if (WARN_ON(!local->scan_req))
466 return;
Johannes Berg5bc75722008-09-11 00:01:51 +0200467
Johannes Berg2a519312009-02-10 21:25:55 +0100468 if (local->scan_req != &local->int_scan_req)
469 cfg80211_scan_done(local->scan_req, aborted);
470 local->scan_req = NULL;
471
472 local->last_scan_completed = jiffies;
Johannes Berg0a51b272008-09-08 17:44:25 +0200473
Johannes Bergc2b13452008-09-11 00:01:55 +0200474 if (local->hw_scanning) {
475 local->hw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200476 /*
477 * Somebody might have requested channel change during scan
478 * that we won't have acted upon, try now. ieee80211_hw_config
479 * will set the flag based on actual changes.
480 */
481 ieee80211_hw_config(local, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200482 goto done;
483 }
484
Johannes Bergc2b13452008-09-11 00:01:55 +0200485 local->sw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200486 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg0a51b272008-09-08 17:44:25 +0200487
488 netif_tx_lock_bh(local->mdev);
489 netif_addr_lock(local->mdev);
490 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
491 local->ops->configure_filter(local_to_hw(local),
492 FIF_BCN_PRBRESP_PROMISC,
493 &local->filter_flags,
494 local->mdev->mc_count,
495 local->mdev->mc_list);
496
497 netif_addr_unlock(local->mdev);
498 netif_tx_unlock_bh(local->mdev);
499
Johannes Berg078e1e62009-01-22 18:07:31 +0100500 mutex_lock(&local->iflist_mtx);
501 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100502 if (!netif_running(sdata->dev))
503 continue;
504
Johannes Berg0a51b272008-09-08 17:44:25 +0200505 /* Tell AP we're back */
Johannes Berg05c914f2008-09-11 00:01:58 +0200506 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200507 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
508 ieee80211_send_nullfunc(local, sdata, 0);
509 netif_tx_wake_all_queues(sdata->dev);
510 }
511 } else
512 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg078e1e62009-01-22 18:07:31 +0100513
Johannes Berg14b80722009-02-10 21:25:42 +0100514 /* re-enable beaconing */
515 if (sdata->vif.type == NL80211_IFTYPE_AP ||
516 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
517 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
518 ieee80211_if_config(sdata,
519 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg0a51b272008-09-08 17:44:25 +0200520 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100521 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200522
523 done:
524 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg472dbc42008-09-11 00:01:49 +0200525 ieee80211_mesh_notify_scan_completed(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200526}
527EXPORT_SYMBOL(ieee80211_scan_completed);
528
Johannes Bergc2b13452008-09-11 00:01:55 +0200529void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200530{
531 struct ieee80211_local *local =
532 container_of(work, struct ieee80211_local, scan_work.work);
533 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200534 struct ieee80211_channel *chan;
Johannes Berg2a519312009-02-10 21:25:55 +0100535 int skip, i;
Johannes Berg0a51b272008-09-08 17:44:25 +0200536 unsigned long next_delay = 0;
537
Johannes Berg5bc75722008-09-11 00:01:51 +0200538 /*
539 * Avoid re-scheduling when the sdata is going away.
540 */
541 if (!netif_running(sdata->dev))
Johannes Berg0a51b272008-09-08 17:44:25 +0200542 return;
543
544 switch (local->scan_state) {
545 case SCAN_SET_CHANNEL:
Johannes Berg0a51b272008-09-08 17:44:25 +0200546 /* if no more bands/channels left, complete scan */
Johannes Berg2a519312009-02-10 21:25:55 +0100547 if (local->scan_channel_idx >= local->scan_req->n_channels) {
548 ieee80211_scan_completed(local_to_hw(local), false);
Johannes Berg0a51b272008-09-08 17:44:25 +0200549 return;
550 }
551 skip = 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100552 chan = local->scan_req->channels[local->scan_channel_idx];
Johannes Berg0a51b272008-09-08 17:44:25 +0200553
554 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200555 (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg0a51b272008-09-08 17:44:25 +0200556 chan->flags & IEEE80211_CHAN_NO_IBSS))
557 skip = 1;
558
559 if (!skip) {
560 local->scan_channel = chan;
Johannes Berge8975582008-10-09 12:18:51 +0200561 if (ieee80211_hw_config(local,
562 IEEE80211_CONF_CHANGE_CHANNEL))
Johannes Berg0a51b272008-09-08 17:44:25 +0200563 skip = 1;
Johannes Berg0a51b272008-09-08 17:44:25 +0200564 }
565
566 /* advance state machine to next channel/band */
567 local->scan_channel_idx++;
Johannes Berg0a51b272008-09-08 17:44:25 +0200568
569 if (skip)
570 break;
571
572 next_delay = IEEE80211_PROBE_DELAY +
573 usecs_to_jiffies(local->hw.channel_change_time);
574 local->scan_state = SCAN_SEND_PROBE;
575 break;
576 case SCAN_SEND_PROBE:
577 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
578 local->scan_state = SCAN_SET_CHANNEL;
579
Johannes Berg2a519312009-02-10 21:25:55 +0100580 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
581 !local->scan_req->n_ssids)
Johannes Berg0a51b272008-09-08 17:44:25 +0200582 break;
Johannes Berg2a519312009-02-10 21:25:55 +0100583 for (i = 0; i < local->scan_req->n_ssids; i++)
584 ieee80211_send_probe_req(
585 sdata, NULL,
586 local->scan_req->ssids[i].ssid,
587 local->scan_req->ssids[i].ssid_len);
Johannes Berg0a51b272008-09-08 17:44:25 +0200588 next_delay = IEEE80211_CHANNEL_TIME;
589 break;
590 }
591
Johannes Berg5bc75722008-09-11 00:01:51 +0200592 queue_delayed_work(local->hw.workqueue, &local->scan_work,
593 next_delay);
Johannes Berg0a51b272008-09-08 17:44:25 +0200594}
595
596
Johannes Bergc2b13452008-09-11 00:01:55 +0200597int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
Johannes Berg2a519312009-02-10 21:25:55 +0100598 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200599{
600 struct ieee80211_local *local = scan_sdata->local;
601 struct ieee80211_sub_if_data *sdata;
602
Johannes Berg2a519312009-02-10 21:25:55 +0100603 if (!req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200604 return -EINVAL;
605
Johannes Berg2a519312009-02-10 21:25:55 +0100606 if (local->scan_req && local->scan_req != req)
607 return -EBUSY;
608
609 local->scan_req = req;
610
Johannes Berg0a51b272008-09-08 17:44:25 +0200611 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
612 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
613 * BSSID: MACAddress
614 * SSID
615 * ScanType: ACTIVE, PASSIVE
616 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
617 * a Probe frame during active scanning
618 * ChannelList
619 * MinChannelTime (>= ProbeDelay), in TU
620 * MaxChannelTime: (>= MinChannelTime), in TU
621 */
622
623 /* MLME-SCAN.confirm
624 * BSSDescriptionSet
625 * ResultCode: SUCCESS, INVALID_PARAMETERS
626 */
627
Johannes Bergc2b13452008-09-11 00:01:55 +0200628 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200629 if (local->scan_sdata == scan_sdata)
630 return 0;
631 return -EBUSY;
632 }
633
634 if (local->ops->hw_scan) {
Johannes Berg5bc75722008-09-11 00:01:51 +0200635 int rc;
636
Johannes Bergc2b13452008-09-11 00:01:55 +0200637 local->hw_scanning = true;
Johannes Berg2a519312009-02-10 21:25:55 +0100638 rc = local->ops->hw_scan(local_to_hw(local), req);
Johannes Berg5bc75722008-09-11 00:01:51 +0200639 if (rc) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200640 local->hw_scanning = false;
Johannes Berg5bc75722008-09-11 00:01:51 +0200641 return rc;
Johannes Berg0a51b272008-09-08 17:44:25 +0200642 }
Johannes Berg5bc75722008-09-11 00:01:51 +0200643 local->scan_sdata = scan_sdata;
644 return 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200645 }
646
Johannes Bergc2b13452008-09-11 00:01:55 +0200647 local->sw_scanning = true;
Johannes Berg0a51b272008-09-08 17:44:25 +0200648
Johannes Berg078e1e62009-01-22 18:07:31 +0100649 mutex_lock(&local->iflist_mtx);
650 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100651 if (!netif_running(sdata->dev))
652 continue;
653
Johannes Berg14b80722009-02-10 21:25:42 +0100654 /* disable beaconing */
655 if (sdata->vif.type == NL80211_IFTYPE_AP ||
656 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
657 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
658 ieee80211_if_config(sdata,
659 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg078e1e62009-01-22 18:07:31 +0100660
Johannes Berg05c914f2008-09-11 00:01:58 +0200661 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200662 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
663 netif_tx_stop_all_queues(sdata->dev);
664 ieee80211_send_nullfunc(local, sdata, 1);
665 }
666 } else
667 netif_tx_stop_all_queues(sdata->dev);
668 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100669 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200670
Johannes Berg0a51b272008-09-08 17:44:25 +0200671 local->scan_state = SCAN_SET_CHANNEL;
672 local->scan_channel_idx = 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200673 local->scan_sdata = scan_sdata;
Johannes Berg2a519312009-02-10 21:25:55 +0100674 local->scan_req = req;
Johannes Berg0a51b272008-09-08 17:44:25 +0200675
676 netif_addr_lock_bh(local->mdev);
677 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
678 local->ops->configure_filter(local_to_hw(local),
679 FIF_BCN_PRBRESP_PROMISC,
680 &local->filter_flags,
681 local->mdev->mc_count,
682 local->mdev->mc_list);
683 netif_addr_unlock_bh(local->mdev);
684
685 /* TODO: start scan as soon as all nullfunc frames are ACKed */
686 queue_delayed_work(local->hw.workqueue, &local->scan_work,
687 IEEE80211_CHANNEL_TIME);
688
689 return 0;
690}
691
692
Johannes Bergc2b13452008-09-11 00:01:55 +0200693int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +0100694 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200695{
Johannes Berg0a51b272008-09-08 17:44:25 +0200696 struct ieee80211_local *local = sdata->local;
Johannes Berg9116dd02008-09-08 17:47:23 +0200697 struct ieee80211_if_sta *ifsta;
Johannes Berg0a51b272008-09-08 17:44:25 +0200698
Johannes Berg2a519312009-02-10 21:25:55 +0100699 if (!req)
700 return -EINVAL;
701
702 if (local->scan_req && local->scan_req != req)
703 return -EBUSY;
704
705 local->scan_req = req;
706
Johannes Berg05c914f2008-09-11 00:01:58 +0200707 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg2a519312009-02-10 21:25:55 +0100708 return ieee80211_start_scan(sdata, req);
Johannes Berg0a51b272008-09-08 17:44:25 +0200709
Johannes Berg9116dd02008-09-08 17:47:23 +0200710 /*
711 * STA has a state machine that might need to defer scanning
712 * while it's trying to associate/authenticate, therefore we
713 * queue it up to the state machine in that case.
714 */
715
Johannes Bergc2b13452008-09-11 00:01:55 +0200716 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200717 if (local->scan_sdata == sdata)
718 return 0;
719 return -EBUSY;
720 }
721
Johannes Berg9116dd02008-09-08 17:47:23 +0200722 ifsta = &sdata->u.sta;
Johannes Berg0a51b272008-09-08 17:44:25 +0200723 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
724 queue_work(local->hw.workqueue, &ifsta->work);
Johannes Berg9116dd02008-09-08 17:47:23 +0200725
Johannes Berg0a51b272008-09-08 17:44:25 +0200726 return 0;
727}