blob: 416bb41099f361159d0bc578533d08a371a7de03 [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:
16 * order BSS list by RSSI(?) ("quality of AP")
17 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
18 * SSID)
19 */
20
Johannes Berg0a51b272008-09-08 17:44:25 +020021#include <linux/wireless.h>
22#include <linux/if_arp.h>
23#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 Bergc2b13452008-09-11 00:01:55 +0200106struct 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
162 if (mesh_config_len != MESH_CFG_LEN)
163 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,
227 int freq, bool beacon)
228{
Johannes Bergc2b13452008-09-11 00:01:55 +0200229 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200230 int clen;
231
232#ifdef CONFIG_MAC80211_MESH
233 if (elems->mesh_config)
234 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
235 elems->mesh_id_len, elems->mesh_config, freq);
236 else
237#endif
238 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
239 elems->ssid, elems->ssid_len);
240 if (!bss) {
241#ifdef CONFIG_MAC80211_MESH
242 if (elems->mesh_config)
243 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
244 elems->mesh_id_len, elems->mesh_config,
245 elems->mesh_config_len, freq);
246 else
247#endif
248 bss = ieee80211_rx_bss_add(local, mgmt->bssid, freq,
249 elems->ssid, elems->ssid_len);
250 if (!bss)
251 return NULL;
252 } else {
253#if 0
254 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200255 spin_lock_bh(&local->bss_lock);
256 list_move_tail(&bss->list, &local->bss_list);
257 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200258#endif
259 }
260
261 /* save the ERP value so that it is available at association time */
262 if (elems->erp_info && elems->erp_info_len >= 1) {
263 bss->erp_value = elems->erp_info[0];
264 bss->has_erp_value = 1;
265 }
266
267 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
268 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
269
270 if (elems->tim) {
271 struct ieee80211_tim_ie *tim_ie =
272 (struct ieee80211_tim_ie *)elems->tim;
273 bss->dtim_period = tim_ie->dtim_period;
274 }
275
276 /* set default value for buggy APs */
277 if (!elems->tim || bss->dtim_period == 0)
278 bss->dtim_period = 1;
279
280 bss->supp_rates_len = 0;
281 if (elems->supp_rates) {
282 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
283 if (clen > elems->supp_rates_len)
284 clen = elems->supp_rates_len;
285 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
286 clen);
287 bss->supp_rates_len += clen;
288 }
289 if (elems->ext_supp_rates) {
290 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
291 if (clen > elems->ext_supp_rates_len)
292 clen = elems->ext_supp_rates_len;
293 memcpy(&bss->supp_rates[bss->supp_rates_len],
294 elems->ext_supp_rates, clen);
295 bss->supp_rates_len += clen;
296 }
297
298 bss->band = rx_status->band;
299
300 bss->timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
301 bss->last_update = jiffies;
302 bss->signal = rx_status->signal;
303 bss->noise = rx_status->noise;
304 bss->qual = rx_status->qual;
305 bss->wmm_used = elems->wmm_param || elems->wmm_info;
306
307 if (!beacon)
308 bss->last_probe_resp = jiffies;
309
310 /*
311 * For probe responses, or if we don't have any information yet,
312 * use the IEs from the beacon.
313 */
314 if (!bss->ies || !beacon) {
315 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
316 kfree(bss->ies);
317 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
318 }
319 if (bss->ies) {
320 memcpy(bss->ies, elems->ie_start, elems->total_len);
321 bss->ies_len = elems->total_len;
322 } else
323 bss->ies_len = 0;
324 }
325
326 return bss;
327}
Johannes Berg0a51b272008-09-08 17:44:25 +0200328
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200329ieee80211_rx_result
Johannes Bergc2b13452008-09-11 00:01:55 +0200330ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
331 struct ieee80211_rx_status *rx_status)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200332{
333 struct ieee80211_mgmt *mgmt;
Johannes Bergc2b13452008-09-11 00:01:55 +0200334 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200335 u8 *elements;
336 struct ieee80211_channel *channel;
337 size_t baselen;
338 int freq;
339 __le16 fc;
340 bool presp, beacon = false;
341 struct ieee802_11_elems elems;
342
343 if (skb->len < 2)
344 return RX_DROP_UNUSABLE;
345
346 mgmt = (struct ieee80211_mgmt *) skb->data;
347 fc = mgmt->frame_control;
348
349 if (ieee80211_is_ctl(fc))
350 return RX_CONTINUE;
351
352 if (skb->len < 24)
353 return RX_DROP_MONITOR;
354
355 presp = ieee80211_is_probe_resp(fc);
356 if (presp) {
357 /* ignore ProbeResp to foreign address */
358 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
359 return RX_DROP_MONITOR;
360
361 presp = true;
362 elements = mgmt->u.probe_resp.variable;
363 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
364 } else {
365 beacon = ieee80211_is_beacon(fc);
366 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
367 elements = mgmt->u.beacon.variable;
368 }
369
370 if (!presp && !beacon)
371 return RX_CONTINUE;
372
373 if (baselen > skb->len)
374 return RX_DROP_MONITOR;
375
376 ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
377
378 if (elems.ds_params && elems.ds_params_len == 1)
379 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
380 else
381 freq = rx_status->freq;
382
383 channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
384
385 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
386 return RX_DROP_MONITOR;
387
388 bss = ieee80211_bss_info_update(sdata->local, rx_status,
389 mgmt, skb->len, &elems,
390 freq, beacon);
Jouni Malinend048e502008-10-11 03:29:55 +0300391 if (bss)
392 ieee80211_rx_bss_put(sdata->local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200393
394 dev_kfree_skb(skb);
395 return RX_QUEUED;
396}
397
Johannes Berg0a51b272008-09-08 17:44:25 +0200398static void ieee80211_send_nullfunc(struct ieee80211_local *local,
399 struct ieee80211_sub_if_data *sdata,
400 int powersave)
401{
402 struct sk_buff *skb;
403 struct ieee80211_hdr *nullfunc;
404 __le16 fc;
405
406 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
407 if (!skb) {
408 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
409 "frame\n", sdata->dev->name);
410 return;
411 }
412 skb_reserve(skb, local->hw.extra_tx_headroom);
413
414 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
415 memset(nullfunc, 0, 24);
416 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
417 IEEE80211_FCTL_TODS);
418 if (powersave)
419 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
420 nullfunc->frame_control = fc;
421 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
422 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
423 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
424
Johannes Berge50db652008-09-09 15:07:09 +0200425 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200426}
427
Johannes Berg0a51b272008-09-08 17:44:25 +0200428void ieee80211_scan_completed(struct ieee80211_hw *hw)
429{
430 struct ieee80211_local *local = hw_to_local(hw);
431 struct ieee80211_sub_if_data *sdata;
432 union iwreq_data wrqu;
433
Johannes Bergc2b13452008-09-11 00:01:55 +0200434 if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
Johannes Berg5bc75722008-09-11 00:01:51 +0200435 return;
436
Johannes Berg0a51b272008-09-08 17:44:25 +0200437 local->last_scan_completed = jiffies;
438 memset(&wrqu, 0, sizeof(wrqu));
Johannes Berg5bc75722008-09-11 00:01:51 +0200439
440 /*
441 * local->scan_sdata could have been NULLed by the interface
442 * down code in case we were scanning on an interface that is
443 * being taken down.
444 */
445 sdata = local->scan_sdata;
446 if (sdata)
447 wireless_send_event(sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg0a51b272008-09-08 17:44:25 +0200448
Johannes Bergc2b13452008-09-11 00:01:55 +0200449 if (local->hw_scanning) {
450 local->hw_scanning = false;
Johannes Berg0a51b272008-09-08 17:44:25 +0200451 if (ieee80211_hw_config(local))
452 printk(KERN_DEBUG "%s: failed to restore operational "
453 "channel after scan\n", wiphy_name(local->hw.wiphy));
Johannes Berg0a51b272008-09-08 17:44:25 +0200454
455 goto done;
456 }
457
Johannes Bergc2b13452008-09-11 00:01:55 +0200458 local->sw_scanning = false;
Johannes Berg0a51b272008-09-08 17:44:25 +0200459 if (ieee80211_hw_config(local))
460 printk(KERN_DEBUG "%s: failed to restore operational "
461 "channel after scan\n", wiphy_name(local->hw.wiphy));
462
463
464 netif_tx_lock_bh(local->mdev);
465 netif_addr_lock(local->mdev);
466 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
467 local->ops->configure_filter(local_to_hw(local),
468 FIF_BCN_PRBRESP_PROMISC,
469 &local->filter_flags,
470 local->mdev->mc_count,
471 local->mdev->mc_list);
472
473 netif_addr_unlock(local->mdev);
474 netif_tx_unlock_bh(local->mdev);
475
476 rcu_read_lock();
477 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
478 /* Tell AP we're back */
Johannes Berg05c914f2008-09-11 00:01:58 +0200479 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200480 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
481 ieee80211_send_nullfunc(local, sdata, 0);
482 netif_tx_wake_all_queues(sdata->dev);
483 }
484 } else
485 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg0a51b272008-09-08 17:44:25 +0200486 }
487 rcu_read_unlock();
488
489 done:
490 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg472dbc42008-09-11 00:01:49 +0200491 ieee80211_mesh_notify_scan_completed(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200492}
493EXPORT_SYMBOL(ieee80211_scan_completed);
494
495
Johannes Bergc2b13452008-09-11 00:01:55 +0200496void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200497{
498 struct ieee80211_local *local =
499 container_of(work, struct ieee80211_local, scan_work.work);
500 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
501 struct ieee80211_supported_band *sband;
502 struct ieee80211_channel *chan;
503 int skip;
504 unsigned long next_delay = 0;
505
Johannes Berg5bc75722008-09-11 00:01:51 +0200506 /*
507 * Avoid re-scheduling when the sdata is going away.
508 */
509 if (!netif_running(sdata->dev))
Johannes Berg0a51b272008-09-08 17:44:25 +0200510 return;
511
512 switch (local->scan_state) {
513 case SCAN_SET_CHANNEL:
514 /*
515 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
516 * after we successfully scanned the last channel of the last
517 * band (and the last band is supported by the hw)
518 */
519 if (local->scan_band < IEEE80211_NUM_BANDS)
520 sband = local->hw.wiphy->bands[local->scan_band];
521 else
522 sband = NULL;
523
524 /*
525 * If we are at an unsupported band and have more bands
526 * left to scan, advance to the next supported one.
527 */
528 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
529 local->scan_band++;
530 sband = local->hw.wiphy->bands[local->scan_band];
531 local->scan_channel_idx = 0;
532 }
533
534 /* if no more bands/channels left, complete scan */
535 if (!sband || local->scan_channel_idx >= sband->n_channels) {
536 ieee80211_scan_completed(local_to_hw(local));
537 return;
538 }
539 skip = 0;
540 chan = &sband->channels[local->scan_channel_idx];
541
542 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200543 (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg0a51b272008-09-08 17:44:25 +0200544 chan->flags & IEEE80211_CHAN_NO_IBSS))
545 skip = 1;
546
547 if (!skip) {
548 local->scan_channel = chan;
549 if (ieee80211_hw_config(local)) {
550 printk(KERN_DEBUG "%s: failed to set freq to "
551 "%d MHz for scan\n", wiphy_name(local->hw.wiphy),
552 chan->center_freq);
553 skip = 1;
554 }
555 }
556
557 /* advance state machine to next channel/band */
558 local->scan_channel_idx++;
559 if (local->scan_channel_idx >= sband->n_channels) {
560 /*
561 * scan_band may end up == IEEE80211_NUM_BANDS, but
562 * we'll catch that case above and complete the scan
563 * if that is the case.
564 */
565 local->scan_band++;
566 local->scan_channel_idx = 0;
567 }
568
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
580 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
581 break;
582 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
583 local->scan_ssid_len);
584 next_delay = IEEE80211_CHANNEL_TIME;
585 break;
586 }
587
Johannes Berg5bc75722008-09-11 00:01:51 +0200588 queue_delayed_work(local->hw.workqueue, &local->scan_work,
589 next_delay);
Johannes Berg0a51b272008-09-08 17:44:25 +0200590}
591
592
Johannes Bergc2b13452008-09-11 00:01:55 +0200593int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
594 u8 *ssid, size_t ssid_len)
Johannes Berg0a51b272008-09-08 17:44:25 +0200595{
596 struct ieee80211_local *local = scan_sdata->local;
597 struct ieee80211_sub_if_data *sdata;
598
599 if (ssid_len > IEEE80211_MAX_SSID_LEN)
600 return -EINVAL;
601
602 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
603 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
604 * BSSID: MACAddress
605 * SSID
606 * ScanType: ACTIVE, PASSIVE
607 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
608 * a Probe frame during active scanning
609 * ChannelList
610 * MinChannelTime (>= ProbeDelay), in TU
611 * MaxChannelTime: (>= MinChannelTime), in TU
612 */
613
614 /* MLME-SCAN.confirm
615 * BSSDescriptionSet
616 * ResultCode: SUCCESS, INVALID_PARAMETERS
617 */
618
Johannes Bergc2b13452008-09-11 00:01:55 +0200619 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200620 if (local->scan_sdata == scan_sdata)
621 return 0;
622 return -EBUSY;
623 }
624
625 if (local->ops->hw_scan) {
Johannes Berg5bc75722008-09-11 00:01:51 +0200626 int rc;
627
Johannes Bergc2b13452008-09-11 00:01:55 +0200628 local->hw_scanning = true;
Johannes Berg5bc75722008-09-11 00:01:51 +0200629 rc = local->ops->hw_scan(local_to_hw(local), ssid, ssid_len);
630 if (rc) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200631 local->hw_scanning = false;
Johannes Berg5bc75722008-09-11 00:01:51 +0200632 return rc;
Johannes Berg0a51b272008-09-08 17:44:25 +0200633 }
Johannes Berg5bc75722008-09-11 00:01:51 +0200634 local->scan_sdata = scan_sdata;
635 return 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200636 }
637
Johannes Bergc2b13452008-09-11 00:01:55 +0200638 local->sw_scanning = true;
Johannes Berg0a51b272008-09-08 17:44:25 +0200639
640 rcu_read_lock();
641 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200642 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200643 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
644 netif_tx_stop_all_queues(sdata->dev);
645 ieee80211_send_nullfunc(local, sdata, 1);
646 }
647 } else
648 netif_tx_stop_all_queues(sdata->dev);
649 }
650 rcu_read_unlock();
651
652 if (ssid) {
653 local->scan_ssid_len = ssid_len;
654 memcpy(local->scan_ssid, ssid, ssid_len);
655 } else
656 local->scan_ssid_len = 0;
657 local->scan_state = SCAN_SET_CHANNEL;
658 local->scan_channel_idx = 0;
659 local->scan_band = IEEE80211_BAND_2GHZ;
660 local->scan_sdata = scan_sdata;
661
662 netif_addr_lock_bh(local->mdev);
663 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
664 local->ops->configure_filter(local_to_hw(local),
665 FIF_BCN_PRBRESP_PROMISC,
666 &local->filter_flags,
667 local->mdev->mc_count,
668 local->mdev->mc_list);
669 netif_addr_unlock_bh(local->mdev);
670
671 /* TODO: start scan as soon as all nullfunc frames are ACKed */
672 queue_delayed_work(local->hw.workqueue, &local->scan_work,
673 IEEE80211_CHANNEL_TIME);
674
675 return 0;
676}
677
678
Johannes Bergc2b13452008-09-11 00:01:55 +0200679int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
680 u8 *ssid, size_t ssid_len)
Johannes Berg0a51b272008-09-08 17:44:25 +0200681{
Johannes Berg0a51b272008-09-08 17:44:25 +0200682 struct ieee80211_local *local = sdata->local;
Johannes Berg9116dd02008-09-08 17:47:23 +0200683 struct ieee80211_if_sta *ifsta;
Johannes Berg0a51b272008-09-08 17:44:25 +0200684
Johannes Berg05c914f2008-09-11 00:01:58 +0200685 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Bergc2b13452008-09-11 00:01:55 +0200686 return ieee80211_start_scan(sdata, ssid, ssid_len);
Johannes Berg0a51b272008-09-08 17:44:25 +0200687
Johannes Berg9116dd02008-09-08 17:47:23 +0200688 /*
689 * STA has a state machine that might need to defer scanning
690 * while it's trying to associate/authenticate, therefore we
691 * queue it up to the state machine in that case.
692 */
693
Johannes Bergc2b13452008-09-11 00:01:55 +0200694 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200695 if (local->scan_sdata == sdata)
696 return 0;
697 return -EBUSY;
698 }
699
Johannes Berg9116dd02008-09-08 17:47:23 +0200700 ifsta = &sdata->u.sta;
701
Johannes Berg0a51b272008-09-08 17:44:25 +0200702 ifsta->scan_ssid_len = ssid_len;
703 if (ssid_len)
704 memcpy(ifsta->scan_ssid, ssid, ssid_len);
705 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
706 queue_work(local->hw.workqueue, &ifsta->work);
Johannes Berg9116dd02008-09-08 17:47:23 +0200707
Johannes Berg0a51b272008-09-08 17:44:25 +0200708 return 0;
709}
710
711
Johannes Bergc2b13452008-09-11 00:01:55 +0200712static void ieee80211_scan_add_ies(struct iw_request_info *info,
713 struct ieee80211_bss *bss,
714 char **current_ev, char *end_buf)
Johannes Berg0a51b272008-09-08 17:44:25 +0200715{
716 u8 *pos, *end, *next;
717 struct iw_event iwe;
718
719 if (bss == NULL || bss->ies == NULL)
720 return;
721
722 /*
723 * If needed, fragment the IEs buffer (at IE boundaries) into short
724 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
725 */
726 pos = bss->ies;
727 end = pos + bss->ies_len;
728
729 while (end - pos > IW_GENERIC_IE_MAX) {
730 next = pos + 2 + pos[1];
731 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
732 next = next + 2 + next[1];
733
734 memset(&iwe, 0, sizeof(iwe));
735 iwe.cmd = IWEVGENIE;
736 iwe.u.data.length = next - pos;
737 *current_ev = iwe_stream_add_point(info, *current_ev,
738 end_buf, &iwe, pos);
739
740 pos = next;
741 }
742
743 if (end > pos) {
744 memset(&iwe, 0, sizeof(iwe));
745 iwe.cmd = IWEVGENIE;
746 iwe.u.data.length = end - pos;
747 *current_ev = iwe_stream_add_point(info, *current_ev,
748 end_buf, &iwe, pos);
749 }
750}
751
752
753static char *
Johannes Bergc2b13452008-09-11 00:01:55 +0200754ieee80211_scan_result(struct ieee80211_local *local,
755 struct iw_request_info *info,
756 struct ieee80211_bss *bss,
757 char *current_ev, char *end_buf)
Johannes Berg0a51b272008-09-08 17:44:25 +0200758{
759 struct iw_event iwe;
760 char *buf;
761
762 if (time_after(jiffies,
763 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
764 return current_ev;
765
766 memset(&iwe, 0, sizeof(iwe));
767 iwe.cmd = SIOCGIWAP;
768 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
769 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
770 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
771 IW_EV_ADDR_LEN);
772
773 memset(&iwe, 0, sizeof(iwe));
774 iwe.cmd = SIOCGIWESSID;
775 if (bss_mesh_cfg(bss)) {
776 iwe.u.data.length = bss_mesh_id_len(bss);
777 iwe.u.data.flags = 1;
778 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
779 &iwe, bss_mesh_id(bss));
780 } else {
781 iwe.u.data.length = bss->ssid_len;
782 iwe.u.data.flags = 1;
783 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
784 &iwe, bss->ssid);
785 }
786
787 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
788 || bss_mesh_cfg(bss)) {
789 memset(&iwe, 0, sizeof(iwe));
790 iwe.cmd = SIOCGIWMODE;
791 if (bss_mesh_cfg(bss))
792 iwe.u.mode = IW_MODE_MESH;
793 else if (bss->capability & WLAN_CAPABILITY_ESS)
794 iwe.u.mode = IW_MODE_MASTER;
795 else
796 iwe.u.mode = IW_MODE_ADHOC;
797 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
798 &iwe, IW_EV_UINT_LEN);
799 }
800
801 memset(&iwe, 0, sizeof(iwe));
802 iwe.cmd = SIOCGIWFREQ;
803 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
804 iwe.u.freq.e = 0;
805 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
806 IW_EV_FREQ_LEN);
807
808 memset(&iwe, 0, sizeof(iwe));
809 iwe.cmd = SIOCGIWFREQ;
810 iwe.u.freq.m = bss->freq;
811 iwe.u.freq.e = 6;
812 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
813 IW_EV_FREQ_LEN);
814 memset(&iwe, 0, sizeof(iwe));
815 iwe.cmd = IWEVQUAL;
816 iwe.u.qual.qual = bss->qual;
817 iwe.u.qual.level = bss->signal;
818 iwe.u.qual.noise = bss->noise;
819 iwe.u.qual.updated = local->wstats_flags;
820 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
821 IW_EV_QUAL_LEN);
822
823 memset(&iwe, 0, sizeof(iwe));
824 iwe.cmd = SIOCGIWENCODE;
825 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
826 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
827 else
828 iwe.u.data.flags = IW_ENCODE_DISABLED;
829 iwe.u.data.length = 0;
830 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
831 &iwe, "");
832
Johannes Bergc2b13452008-09-11 00:01:55 +0200833 ieee80211_scan_add_ies(info, bss, &current_ev, end_buf);
Johannes Berg0a51b272008-09-08 17:44:25 +0200834
835 if (bss->supp_rates_len > 0) {
836 /* display all supported rates in readable format */
837 char *p = current_ev + iwe_stream_lcp_len(info);
838 int i;
839
840 memset(&iwe, 0, sizeof(iwe));
841 iwe.cmd = SIOCGIWRATE;
842 /* Those two flags are ignored... */
843 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
844
845 for (i = 0; i < bss->supp_rates_len; i++) {
846 iwe.u.bitrate.value = ((bss->supp_rates[i] &
847 0x7f) * 500000);
848 p = iwe_stream_add_value(info, current_ev, p,
849 end_buf, &iwe, IW_EV_PARAM_LEN);
850 }
851 current_ev = p;
852 }
853
854 buf = kmalloc(30, GFP_ATOMIC);
855 if (buf) {
856 memset(&iwe, 0, sizeof(iwe));
857 iwe.cmd = IWEVCUSTOM;
858 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
859 iwe.u.data.length = strlen(buf);
860 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
861 &iwe, buf);
862 memset(&iwe, 0, sizeof(iwe));
863 iwe.cmd = IWEVCUSTOM;
864 sprintf(buf, " Last beacon: %dms ago",
865 jiffies_to_msecs(jiffies - bss->last_update));
866 iwe.u.data.length = strlen(buf);
867 current_ev = iwe_stream_add_point(info, current_ev,
868 end_buf, &iwe, buf);
869 kfree(buf);
870 }
871
872 if (bss_mesh_cfg(bss)) {
873 u8 *cfg = bss_mesh_cfg(bss);
874 buf = kmalloc(50, GFP_ATOMIC);
875 if (buf) {
876 memset(&iwe, 0, sizeof(iwe));
877 iwe.cmd = IWEVCUSTOM;
878 sprintf(buf, "Mesh network (version %d)", cfg[0]);
879 iwe.u.data.length = strlen(buf);
880 current_ev = iwe_stream_add_point(info, current_ev,
881 end_buf,
882 &iwe, buf);
883 sprintf(buf, "Path Selection Protocol ID: "
884 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
885 cfg[4]);
886 iwe.u.data.length = strlen(buf);
887 current_ev = iwe_stream_add_point(info, current_ev,
888 end_buf,
889 &iwe, buf);
890 sprintf(buf, "Path Selection Metric ID: "
891 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
892 cfg[8]);
893 iwe.u.data.length = strlen(buf);
894 current_ev = iwe_stream_add_point(info, current_ev,
895 end_buf,
896 &iwe, buf);
897 sprintf(buf, "Congestion Control Mode ID: "
898 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
899 cfg[11], cfg[12]);
900 iwe.u.data.length = strlen(buf);
901 current_ev = iwe_stream_add_point(info, current_ev,
902 end_buf,
903 &iwe, buf);
904 sprintf(buf, "Channel Precedence: "
905 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
906 cfg[15], cfg[16]);
907 iwe.u.data.length = strlen(buf);
908 current_ev = iwe_stream_add_point(info, current_ev,
909 end_buf,
910 &iwe, buf);
911 kfree(buf);
912 }
913 }
914
915 return current_ev;
916}
917
918
Johannes Bergc2b13452008-09-11 00:01:55 +0200919int ieee80211_scan_results(struct ieee80211_local *local,
920 struct iw_request_info *info,
921 char *buf, size_t len)
Johannes Berg0a51b272008-09-08 17:44:25 +0200922{
923 char *current_ev = buf;
924 char *end_buf = buf + len;
Johannes Bergc2b13452008-09-11 00:01:55 +0200925 struct ieee80211_bss *bss;
Johannes Berg0a51b272008-09-08 17:44:25 +0200926
Johannes Bergc2b13452008-09-11 00:01:55 +0200927 spin_lock_bh(&local->bss_lock);
928 list_for_each_entry(bss, &local->bss_list, list) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200929 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200930 spin_unlock_bh(&local->bss_lock);
Johannes Berg0a51b272008-09-08 17:44:25 +0200931 return -E2BIG;
932 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200933 current_ev = ieee80211_scan_result(local, info, bss,
Johannes Berg0a51b272008-09-08 17:44:25 +0200934 current_ev, end_buf);
935 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200936 spin_unlock_bh(&local->bss_lock);
Johannes Berg0a51b272008-09-08 17:44:25 +0200937 return current_ev - buf;
938}