blob: eddca4e1e13c2810646c02cc02b9ad0022972da6 [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>
Johannes Berg078e1e62009-01-22 18:07:31 +010023#include <linux/rtnetlink.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020024#include <net/mac80211.h>
25#include <net/iw_handler.h>
26
27#include "ieee80211_i.h"
Johannes Berg5484e232008-09-08 17:44:27 +020028#include "mesh.h"
Johannes Berg0a51b272008-09-08 17:44:25 +020029
30#define IEEE80211_PROBE_DELAY (HZ / 33)
31#define IEEE80211_CHANNEL_TIME (HZ / 33)
32#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
33
Johannes Berg5484e232008-09-08 17:44:27 +020034void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
35{
Johannes Bergc2b13452008-09-11 00:01:55 +020036 spin_lock_init(&local->bss_lock);
37 INIT_LIST_HEAD(&local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +020038}
39
40void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
41{
Johannes Bergc2b13452008-09-11 00:01:55 +020042 struct ieee80211_bss *bss, *tmp;
Johannes Berg5484e232008-09-08 17:44:27 +020043
Johannes Bergc2b13452008-09-11 00:01:55 +020044 list_for_each_entry_safe(bss, tmp, &local->bss_list, list)
Johannes Berg5484e232008-09-08 17:44:27 +020045 ieee80211_rx_bss_put(local, bss);
46}
47
Johannes Bergc2b13452008-09-11 00:01:55 +020048struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +020049ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
50 u8 *ssid, u8 ssid_len)
51{
Johannes Bergc2b13452008-09-11 00:01:55 +020052 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +020053
Johannes Bergc2b13452008-09-11 00:01:55 +020054 spin_lock_bh(&local->bss_lock);
55 bss = local->bss_hash[STA_HASH(bssid)];
Johannes Berg5484e232008-09-08 17:44:27 +020056 while (bss) {
57 if (!bss_mesh_cfg(bss) &&
58 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
59 bss->freq == freq &&
60 bss->ssid_len == ssid_len &&
61 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
62 atomic_inc(&bss->users);
63 break;
64 }
65 bss = bss->hnext;
66 }
Johannes Bergc2b13452008-09-11 00:01:55 +020067 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +020068 return bss;
69}
70
Johannes Bergc2b13452008-09-11 00:01:55 +020071/* Caller must hold local->bss_lock */
Johannes Berg5484e232008-09-08 17:44:27 +020072static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020073 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020074{
75 u8 hash_idx;
76
77 if (bss_mesh_cfg(bss))
78 hash_idx = mesh_id_hash(bss_mesh_id(bss),
79 bss_mesh_id_len(bss));
80 else
81 hash_idx = STA_HASH(bss->bssid);
82
Johannes Bergc2b13452008-09-11 00:01:55 +020083 bss->hnext = local->bss_hash[hash_idx];
84 local->bss_hash[hash_idx] = bss;
Johannes Berg5484e232008-09-08 17:44:27 +020085}
86
Johannes Bergc2b13452008-09-11 00:01:55 +020087/* Caller must hold local->bss_lock */
Johannes Berg5484e232008-09-08 17:44:27 +020088static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020089 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020090{
Johannes Bergc2b13452008-09-11 00:01:55 +020091 struct ieee80211_bss *b, *prev = NULL;
92 b = local->bss_hash[STA_HASH(bss->bssid)];
Johannes Berg5484e232008-09-08 17:44:27 +020093 while (b) {
94 if (b == bss) {
95 if (!prev)
Johannes Bergc2b13452008-09-11 00:01:55 +020096 local->bss_hash[STA_HASH(bss->bssid)] =
Johannes Berg5484e232008-09-08 17:44:27 +020097 bss->hnext;
98 else
99 prev->hnext = bss->hnext;
100 break;
101 }
102 prev = b;
103 b = b->hnext;
104 }
105}
106
Johannes Bergc2b13452008-09-11 00:01:55 +0200107struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200108ieee80211_rx_bss_add(struct ieee80211_local *local, u8 *bssid, int freq,
109 u8 *ssid, u8 ssid_len)
110{
Johannes Bergc2b13452008-09-11 00:01:55 +0200111 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200112
113 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
114 if (!bss)
115 return NULL;
116 atomic_set(&bss->users, 2);
117 memcpy(bss->bssid, bssid, ETH_ALEN);
118 bss->freq = freq;
119 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
120 memcpy(bss->ssid, ssid, ssid_len);
121 bss->ssid_len = ssid_len;
122 }
123
Johannes Bergc2b13452008-09-11 00:01:55 +0200124 spin_lock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200125 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200126 list_add_tail(&bss->list, &local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +0200127 __ieee80211_rx_bss_hash_add(local, bss);
Johannes Bergc2b13452008-09-11 00:01:55 +0200128 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200129 return bss;
130}
131
132#ifdef CONFIG_MAC80211_MESH
Johannes Bergc2b13452008-09-11 00:01:55 +0200133static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200134ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
135 u8 *mesh_cfg, int freq)
136{
Johannes Bergc2b13452008-09-11 00:01:55 +0200137 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200138
Johannes Bergc2b13452008-09-11 00:01:55 +0200139 spin_lock_bh(&local->bss_lock);
140 bss = local->bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
Johannes Berg5484e232008-09-08 17:44:27 +0200141 while (bss) {
142 if (bss_mesh_cfg(bss) &&
143 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
144 bss->freq == freq &&
145 mesh_id_len == bss->mesh_id_len &&
146 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
147 mesh_id_len))) {
148 atomic_inc(&bss->users);
149 break;
150 }
151 bss = bss->hnext;
152 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200153 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200154 return bss;
155}
156
Johannes Bergc2b13452008-09-11 00:01:55 +0200157static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200158ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
159 u8 *mesh_cfg, int mesh_config_len, int freq)
160{
Johannes Bergc2b13452008-09-11 00:01:55 +0200161 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200162
Johannes Berg1239cd52008-10-28 11:12:57 +0100163 if (mesh_config_len != IEEE80211_MESH_CONFIG_LEN)
Johannes Berg5484e232008-09-08 17:44:27 +0200164 return NULL;
165
166 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
167 if (!bss)
168 return NULL;
169
170 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
171 if (!bss->mesh_cfg) {
172 kfree(bss);
173 return NULL;
174 }
175
176 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
177 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
178 if (!bss->mesh_id) {
179 kfree(bss->mesh_cfg);
180 kfree(bss);
181 return NULL;
182 }
183 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
184 }
185
186 atomic_set(&bss->users, 2);
187 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
188 bss->mesh_id_len = mesh_id_len;
189 bss->freq = freq;
Johannes Bergc2b13452008-09-11 00:01:55 +0200190 spin_lock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200191 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200192 list_add_tail(&bss->list, &local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +0200193 __ieee80211_rx_bss_hash_add(local, bss);
Johannes Bergc2b13452008-09-11 00:01:55 +0200194 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200195 return bss;
196}
197#endif
198
Johannes Bergc2b13452008-09-11 00:01:55 +0200199static void ieee80211_rx_bss_free(struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +0200200{
201 kfree(bss->ies);
202 kfree(bss_mesh_id(bss));
203 kfree(bss_mesh_cfg(bss));
204 kfree(bss);
205}
206
207void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +0200208 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +0200209{
210 local_bh_disable();
Johannes Bergc2b13452008-09-11 00:01:55 +0200211 if (!atomic_dec_and_lock(&bss->users, &local->bss_lock)) {
Johannes Berg5484e232008-09-08 17:44:27 +0200212 local_bh_enable();
213 return;
214 }
215
216 __ieee80211_rx_bss_hash_del(local, bss);
217 list_del(&bss->list);
Johannes Bergc2b13452008-09-11 00:01:55 +0200218 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200219 ieee80211_rx_bss_free(bss);
220}
221
Johannes Bergc2b13452008-09-11 00:01:55 +0200222struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200223ieee80211_bss_info_update(struct ieee80211_local *local,
224 struct ieee80211_rx_status *rx_status,
225 struct ieee80211_mgmt *mgmt,
226 size_t len,
227 struct ieee802_11_elems *elems,
228 int freq, bool beacon)
229{
Johannes Bergc2b13452008-09-11 00:01:55 +0200230 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200231 int clen;
232
233#ifdef CONFIG_MAC80211_MESH
234 if (elems->mesh_config)
235 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
236 elems->mesh_id_len, elems->mesh_config, freq);
237 else
238#endif
239 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
240 elems->ssid, elems->ssid_len);
241 if (!bss) {
242#ifdef CONFIG_MAC80211_MESH
243 if (elems->mesh_config)
244 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
245 elems->mesh_id_len, elems->mesh_config,
246 elems->mesh_config_len, freq);
247 else
248#endif
249 bss = ieee80211_rx_bss_add(local, mgmt->bssid, freq,
250 elems->ssid, elems->ssid_len);
251 if (!bss)
252 return NULL;
253 } else {
254#if 0
255 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200256 spin_lock_bh(&local->bss_lock);
257 list_move_tail(&bss->list, &local->bss_list);
258 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200259#endif
260 }
261
262 /* save the ERP value so that it is available at association time */
263 if (elems->erp_info && elems->erp_info_len >= 1) {
264 bss->erp_value = elems->erp_info[0];
265 bss->has_erp_value = 1;
266 }
267
268 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
269 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
270
271 if (elems->tim) {
272 struct ieee80211_tim_ie *tim_ie =
273 (struct ieee80211_tim_ie *)elems->tim;
274 bss->dtim_period = tim_ie->dtim_period;
275 }
276
277 /* set default value for buggy APs */
278 if (!elems->tim || bss->dtim_period == 0)
279 bss->dtim_period = 1;
280
281 bss->supp_rates_len = 0;
282 if (elems->supp_rates) {
283 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
284 if (clen > elems->supp_rates_len)
285 clen = elems->supp_rates_len;
286 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
287 clen);
288 bss->supp_rates_len += clen;
289 }
290 if (elems->ext_supp_rates) {
291 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
292 if (clen > elems->ext_supp_rates_len)
293 clen = elems->ext_supp_rates_len;
294 memcpy(&bss->supp_rates[bss->supp_rates_len],
295 elems->ext_supp_rates, clen);
296 bss->supp_rates_len += clen;
297 }
298
299 bss->band = rx_status->band;
300
301 bss->timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
302 bss->last_update = jiffies;
303 bss->signal = rx_status->signal;
304 bss->noise = rx_status->noise;
305 bss->qual = rx_status->qual;
306 bss->wmm_used = elems->wmm_param || elems->wmm_info;
307
308 if (!beacon)
309 bss->last_probe_resp = jiffies;
310
311 /*
312 * For probe responses, or if we don't have any information yet,
313 * use the IEs from the beacon.
314 */
315 if (!bss->ies || !beacon) {
316 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
317 kfree(bss->ies);
318 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
319 }
320 if (bss->ies) {
321 memcpy(bss->ies, elems->ie_start, elems->total_len);
322 bss->ies_len = elems->total_len;
323 } else
324 bss->ies_len = 0;
325 }
326
327 return bss;
328}
Johannes Berg0a51b272008-09-08 17:44:25 +0200329
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +0530330void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
331 int freq, u8 *ssid, u8 ssid_len)
332{
333 struct ieee80211_bss *bss;
334 struct ieee80211_local *local = sdata->local;
335
336 bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
337 if (bss) {
338 atomic_dec(&bss->users);
339 ieee80211_rx_bss_put(local, bss);
340 }
341}
342
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200343ieee80211_rx_result
Johannes Bergc2b13452008-09-11 00:01:55 +0200344ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
345 struct ieee80211_rx_status *rx_status)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200346{
347 struct ieee80211_mgmt *mgmt;
Johannes Bergc2b13452008-09-11 00:01:55 +0200348 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200349 u8 *elements;
350 struct ieee80211_channel *channel;
351 size_t baselen;
352 int freq;
353 __le16 fc;
354 bool presp, beacon = false;
355 struct ieee802_11_elems elems;
356
357 if (skb->len < 2)
358 return RX_DROP_UNUSABLE;
359
360 mgmt = (struct ieee80211_mgmt *) skb->data;
361 fc = mgmt->frame_control;
362
363 if (ieee80211_is_ctl(fc))
364 return RX_CONTINUE;
365
366 if (skb->len < 24)
367 return RX_DROP_MONITOR;
368
369 presp = ieee80211_is_probe_resp(fc);
370 if (presp) {
371 /* ignore ProbeResp to foreign address */
372 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
373 return RX_DROP_MONITOR;
374
375 presp = true;
376 elements = mgmt->u.probe_resp.variable;
377 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
378 } else {
379 beacon = ieee80211_is_beacon(fc);
380 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
381 elements = mgmt->u.beacon.variable;
382 }
383
384 if (!presp && !beacon)
385 return RX_CONTINUE;
386
387 if (baselen > skb->len)
388 return RX_DROP_MONITOR;
389
390 ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
391
392 if (elems.ds_params && elems.ds_params_len == 1)
393 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
394 else
395 freq = rx_status->freq;
396
397 channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
398
399 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
400 return RX_DROP_MONITOR;
401
402 bss = ieee80211_bss_info_update(sdata->local, rx_status,
403 mgmt, skb->len, &elems,
404 freq, beacon);
Jouni Malinend048e502008-10-11 03:29:55 +0300405 if (bss)
406 ieee80211_rx_bss_put(sdata->local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200407
408 dev_kfree_skb(skb);
409 return RX_QUEUED;
410}
411
Vivek Natarajana97b77b2008-12-23 18:39:02 -0800412void ieee80211_send_nullfunc(struct ieee80211_local *local,
Johannes Berg0a51b272008-09-08 17:44:25 +0200413 struct ieee80211_sub_if_data *sdata,
414 int powersave)
415{
416 struct sk_buff *skb;
417 struct ieee80211_hdr *nullfunc;
418 __le16 fc;
419
420 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
421 if (!skb) {
422 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
423 "frame\n", sdata->dev->name);
424 return;
425 }
426 skb_reserve(skb, local->hw.extra_tx_headroom);
427
428 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
429 memset(nullfunc, 0, 24);
430 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
431 IEEE80211_FCTL_TODS);
432 if (powersave)
433 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
434 nullfunc->frame_control = fc;
435 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
436 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
437 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
438
Johannes Berge50db652008-09-09 15:07:09 +0200439 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200440}
441
Johannes Berg0a51b272008-09-08 17:44:25 +0200442void ieee80211_scan_completed(struct ieee80211_hw *hw)
443{
444 struct ieee80211_local *local = hw_to_local(hw);
445 struct ieee80211_sub_if_data *sdata;
446 union iwreq_data wrqu;
447
Johannes Bergc2b13452008-09-11 00:01:55 +0200448 if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
Johannes Berg5bc75722008-09-11 00:01:51 +0200449 return;
450
Johannes Berg0a51b272008-09-08 17:44:25 +0200451 local->last_scan_completed = jiffies;
452 memset(&wrqu, 0, sizeof(wrqu));
Johannes Berg5bc75722008-09-11 00:01:51 +0200453
454 /*
455 * local->scan_sdata could have been NULLed by the interface
456 * down code in case we were scanning on an interface that is
457 * being taken down.
458 */
459 sdata = local->scan_sdata;
460 if (sdata)
461 wireless_send_event(sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg0a51b272008-09-08 17:44:25 +0200462
Johannes Bergc2b13452008-09-11 00:01:55 +0200463 if (local->hw_scanning) {
464 local->hw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200465 /*
466 * Somebody might have requested channel change during scan
467 * that we won't have acted upon, try now. ieee80211_hw_config
468 * will set the flag based on actual changes.
469 */
470 ieee80211_hw_config(local, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200471 goto done;
472 }
473
Johannes Bergc2b13452008-09-11 00:01:55 +0200474 local->sw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200475 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg0a51b272008-09-08 17:44:25 +0200476
477 netif_tx_lock_bh(local->mdev);
478 netif_addr_lock(local->mdev);
479 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
480 local->ops->configure_filter(local_to_hw(local),
481 FIF_BCN_PRBRESP_PROMISC,
482 &local->filter_flags,
483 local->mdev->mc_count,
484 local->mdev->mc_list);
485
486 netif_addr_unlock(local->mdev);
487 netif_tx_unlock_bh(local->mdev);
488
Johannes Berg078e1e62009-01-22 18:07:31 +0100489 mutex_lock(&local->iflist_mtx);
490 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100491 if (!netif_running(sdata->dev))
492 continue;
493
Johannes Berg0a51b272008-09-08 17:44:25 +0200494 /* Tell AP we're back */
Johannes Berg05c914f2008-09-11 00:01:58 +0200495 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200496 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
497 ieee80211_send_nullfunc(local, sdata, 0);
498 netif_tx_wake_all_queues(sdata->dev);
499 }
500 } else
501 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg078e1e62009-01-22 18:07:31 +0100502
Johannes Berg14b80722009-02-10 21:25:42 +0100503 /* re-enable beaconing */
504 if (sdata->vif.type == NL80211_IFTYPE_AP ||
505 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
506 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
507 ieee80211_if_config(sdata,
508 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg0a51b272008-09-08 17:44:25 +0200509 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100510 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200511
512 done:
513 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg472dbc42008-09-11 00:01:49 +0200514 ieee80211_mesh_notify_scan_completed(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200515}
516EXPORT_SYMBOL(ieee80211_scan_completed);
517
Johannes Bergc2b13452008-09-11 00:01:55 +0200518void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200519{
520 struct ieee80211_local *local =
521 container_of(work, struct ieee80211_local, scan_work.work);
522 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
523 struct ieee80211_supported_band *sband;
524 struct ieee80211_channel *chan;
525 int skip;
526 unsigned long next_delay = 0;
527
Johannes Berg5bc75722008-09-11 00:01:51 +0200528 /*
529 * Avoid re-scheduling when the sdata is going away.
530 */
531 if (!netif_running(sdata->dev))
Johannes Berg0a51b272008-09-08 17:44:25 +0200532 return;
533
534 switch (local->scan_state) {
535 case SCAN_SET_CHANNEL:
536 /*
537 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
538 * after we successfully scanned the last channel of the last
539 * band (and the last band is supported by the hw)
540 */
541 if (local->scan_band < IEEE80211_NUM_BANDS)
542 sband = local->hw.wiphy->bands[local->scan_band];
543 else
544 sband = NULL;
545
546 /*
547 * If we are at an unsupported band and have more bands
548 * left to scan, advance to the next supported one.
549 */
550 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
551 local->scan_band++;
552 sband = local->hw.wiphy->bands[local->scan_band];
553 local->scan_channel_idx = 0;
554 }
555
556 /* if no more bands/channels left, complete scan */
557 if (!sband || local->scan_channel_idx >= sband->n_channels) {
558 ieee80211_scan_completed(local_to_hw(local));
559 return;
560 }
561 skip = 0;
562 chan = &sband->channels[local->scan_channel_idx];
563
564 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200565 (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg0a51b272008-09-08 17:44:25 +0200566 chan->flags & IEEE80211_CHAN_NO_IBSS))
567 skip = 1;
568
569 if (!skip) {
570 local->scan_channel = chan;
Johannes Berge8975582008-10-09 12:18:51 +0200571 if (ieee80211_hw_config(local,
572 IEEE80211_CONF_CHANGE_CHANNEL))
Johannes Berg0a51b272008-09-08 17:44:25 +0200573 skip = 1;
Johannes Berg0a51b272008-09-08 17:44:25 +0200574 }
575
576 /* advance state machine to next channel/band */
577 local->scan_channel_idx++;
578 if (local->scan_channel_idx >= sband->n_channels) {
579 /*
580 * scan_band may end up == IEEE80211_NUM_BANDS, but
581 * we'll catch that case above and complete the scan
582 * if that is the case.
583 */
584 local->scan_band++;
585 local->scan_channel_idx = 0;
586 }
587
588 if (skip)
589 break;
590
591 next_delay = IEEE80211_PROBE_DELAY +
592 usecs_to_jiffies(local->hw.channel_change_time);
593 local->scan_state = SCAN_SEND_PROBE;
594 break;
595 case SCAN_SEND_PROBE:
596 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
597 local->scan_state = SCAN_SET_CHANNEL;
598
599 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
600 break;
601 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
602 local->scan_ssid_len);
603 next_delay = IEEE80211_CHANNEL_TIME;
604 break;
605 }
606
Johannes Berg5bc75722008-09-11 00:01:51 +0200607 queue_delayed_work(local->hw.workqueue, &local->scan_work,
608 next_delay);
Johannes Berg0a51b272008-09-08 17:44:25 +0200609}
610
611
Johannes Bergc2b13452008-09-11 00:01:55 +0200612int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
613 u8 *ssid, size_t ssid_len)
Johannes Berg0a51b272008-09-08 17:44:25 +0200614{
615 struct ieee80211_local *local = scan_sdata->local;
616 struct ieee80211_sub_if_data *sdata;
617
618 if (ssid_len > IEEE80211_MAX_SSID_LEN)
619 return -EINVAL;
620
621 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
622 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
623 * BSSID: MACAddress
624 * SSID
625 * ScanType: ACTIVE, PASSIVE
626 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
627 * a Probe frame during active scanning
628 * ChannelList
629 * MinChannelTime (>= ProbeDelay), in TU
630 * MaxChannelTime: (>= MinChannelTime), in TU
631 */
632
633 /* MLME-SCAN.confirm
634 * BSSDescriptionSet
635 * ResultCode: SUCCESS, INVALID_PARAMETERS
636 */
637
Johannes Bergc2b13452008-09-11 00:01:55 +0200638 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200639 if (local->scan_sdata == scan_sdata)
640 return 0;
641 return -EBUSY;
642 }
643
644 if (local->ops->hw_scan) {
Johannes Berg5bc75722008-09-11 00:01:51 +0200645 int rc;
646
Johannes Bergc2b13452008-09-11 00:01:55 +0200647 local->hw_scanning = true;
Johannes Berg5bc75722008-09-11 00:01:51 +0200648 rc = local->ops->hw_scan(local_to_hw(local), ssid, ssid_len);
649 if (rc) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200650 local->hw_scanning = false;
Johannes Berg5bc75722008-09-11 00:01:51 +0200651 return rc;
Johannes Berg0a51b272008-09-08 17:44:25 +0200652 }
Johannes Berg5bc75722008-09-11 00:01:51 +0200653 local->scan_sdata = scan_sdata;
654 return 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200655 }
656
Johannes Bergc2b13452008-09-11 00:01:55 +0200657 local->sw_scanning = true;
Johannes Berg0a51b272008-09-08 17:44:25 +0200658
Johannes Berg078e1e62009-01-22 18:07:31 +0100659 mutex_lock(&local->iflist_mtx);
660 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100661 if (!netif_running(sdata->dev))
662 continue;
663
Johannes Berg14b80722009-02-10 21:25:42 +0100664 /* disable beaconing */
665 if (sdata->vif.type == NL80211_IFTYPE_AP ||
666 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
667 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
668 ieee80211_if_config(sdata,
669 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg078e1e62009-01-22 18:07:31 +0100670
Johannes Berg05c914f2008-09-11 00:01:58 +0200671 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200672 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
673 netif_tx_stop_all_queues(sdata->dev);
674 ieee80211_send_nullfunc(local, sdata, 1);
675 }
676 } else
677 netif_tx_stop_all_queues(sdata->dev);
678 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100679 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200680
681 if (ssid) {
682 local->scan_ssid_len = ssid_len;
683 memcpy(local->scan_ssid, ssid, ssid_len);
684 } else
685 local->scan_ssid_len = 0;
686 local->scan_state = SCAN_SET_CHANNEL;
687 local->scan_channel_idx = 0;
688 local->scan_band = IEEE80211_BAND_2GHZ;
689 local->scan_sdata = scan_sdata;
690
691 netif_addr_lock_bh(local->mdev);
692 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
693 local->ops->configure_filter(local_to_hw(local),
694 FIF_BCN_PRBRESP_PROMISC,
695 &local->filter_flags,
696 local->mdev->mc_count,
697 local->mdev->mc_list);
698 netif_addr_unlock_bh(local->mdev);
699
700 /* TODO: start scan as soon as all nullfunc frames are ACKed */
701 queue_delayed_work(local->hw.workqueue, &local->scan_work,
702 IEEE80211_CHANNEL_TIME);
703
704 return 0;
705}
706
707
Johannes Bergc2b13452008-09-11 00:01:55 +0200708int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
709 u8 *ssid, size_t ssid_len)
Johannes Berg0a51b272008-09-08 17:44:25 +0200710{
Johannes Berg0a51b272008-09-08 17:44:25 +0200711 struct ieee80211_local *local = sdata->local;
Johannes Berg9116dd02008-09-08 17:47:23 +0200712 struct ieee80211_if_sta *ifsta;
Johannes Berg0a51b272008-09-08 17:44:25 +0200713
Johannes Berg05c914f2008-09-11 00:01:58 +0200714 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Bergc2b13452008-09-11 00:01:55 +0200715 return ieee80211_start_scan(sdata, ssid, ssid_len);
Johannes Berg0a51b272008-09-08 17:44:25 +0200716
Johannes Berg9116dd02008-09-08 17:47:23 +0200717 /*
718 * STA has a state machine that might need to defer scanning
719 * while it's trying to associate/authenticate, therefore we
720 * queue it up to the state machine in that case.
721 */
722
Johannes Bergc2b13452008-09-11 00:01:55 +0200723 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200724 if (local->scan_sdata == sdata)
725 return 0;
726 return -EBUSY;
727 }
728
Johannes Berg9116dd02008-09-08 17:47:23 +0200729 ifsta = &sdata->u.sta;
730
Johannes Berg0a51b272008-09-08 17:44:25 +0200731 ifsta->scan_ssid_len = ssid_len;
732 if (ssid_len)
733 memcpy(ifsta->scan_ssid, ssid, ssid_len);
734 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
735 queue_work(local->hw.workqueue, &ifsta->work);
Johannes Berg9116dd02008-09-08 17:47:23 +0200736
Johannes Berg0a51b272008-09-08 17:44:25 +0200737 return 0;
738}
739
740
Johannes Bergc2b13452008-09-11 00:01:55 +0200741static void ieee80211_scan_add_ies(struct iw_request_info *info,
742 struct ieee80211_bss *bss,
743 char **current_ev, char *end_buf)
Johannes Berg0a51b272008-09-08 17:44:25 +0200744{
745 u8 *pos, *end, *next;
746 struct iw_event iwe;
747
748 if (bss == NULL || bss->ies == NULL)
749 return;
750
751 /*
752 * If needed, fragment the IEs buffer (at IE boundaries) into short
753 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
754 */
755 pos = bss->ies;
756 end = pos + bss->ies_len;
757
758 while (end - pos > IW_GENERIC_IE_MAX) {
759 next = pos + 2 + pos[1];
760 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
761 next = next + 2 + next[1];
762
763 memset(&iwe, 0, sizeof(iwe));
764 iwe.cmd = IWEVGENIE;
765 iwe.u.data.length = next - pos;
766 *current_ev = iwe_stream_add_point(info, *current_ev,
767 end_buf, &iwe, pos);
768
769 pos = next;
770 }
771
772 if (end > pos) {
773 memset(&iwe, 0, sizeof(iwe));
774 iwe.cmd = IWEVGENIE;
775 iwe.u.data.length = end - pos;
776 *current_ev = iwe_stream_add_point(info, *current_ev,
777 end_buf, &iwe, pos);
778 }
779}
780
781
782static char *
Johannes Bergc2b13452008-09-11 00:01:55 +0200783ieee80211_scan_result(struct ieee80211_local *local,
784 struct iw_request_info *info,
785 struct ieee80211_bss *bss,
786 char *current_ev, char *end_buf)
Johannes Berg0a51b272008-09-08 17:44:25 +0200787{
788 struct iw_event iwe;
789 char *buf;
790
791 if (time_after(jiffies,
792 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
793 return current_ev;
794
795 memset(&iwe, 0, sizeof(iwe));
796 iwe.cmd = SIOCGIWAP;
797 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
798 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
799 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
800 IW_EV_ADDR_LEN);
801
802 memset(&iwe, 0, sizeof(iwe));
803 iwe.cmd = SIOCGIWESSID;
804 if (bss_mesh_cfg(bss)) {
805 iwe.u.data.length = bss_mesh_id_len(bss);
806 iwe.u.data.flags = 1;
807 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
808 &iwe, bss_mesh_id(bss));
809 } else {
810 iwe.u.data.length = bss->ssid_len;
811 iwe.u.data.flags = 1;
812 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
813 &iwe, bss->ssid);
814 }
815
816 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
817 || bss_mesh_cfg(bss)) {
818 memset(&iwe, 0, sizeof(iwe));
819 iwe.cmd = SIOCGIWMODE;
820 if (bss_mesh_cfg(bss))
821 iwe.u.mode = IW_MODE_MESH;
822 else if (bss->capability & WLAN_CAPABILITY_ESS)
823 iwe.u.mode = IW_MODE_MASTER;
824 else
825 iwe.u.mode = IW_MODE_ADHOC;
826 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
827 &iwe, IW_EV_UINT_LEN);
828 }
829
830 memset(&iwe, 0, sizeof(iwe));
831 iwe.cmd = SIOCGIWFREQ;
832 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
833 iwe.u.freq.e = 0;
834 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
835 IW_EV_FREQ_LEN);
836
837 memset(&iwe, 0, sizeof(iwe));
838 iwe.cmd = SIOCGIWFREQ;
839 iwe.u.freq.m = bss->freq;
840 iwe.u.freq.e = 6;
841 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
842 IW_EV_FREQ_LEN);
843 memset(&iwe, 0, sizeof(iwe));
844 iwe.cmd = IWEVQUAL;
845 iwe.u.qual.qual = bss->qual;
846 iwe.u.qual.level = bss->signal;
847 iwe.u.qual.noise = bss->noise;
848 iwe.u.qual.updated = local->wstats_flags;
849 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
850 IW_EV_QUAL_LEN);
851
852 memset(&iwe, 0, sizeof(iwe));
853 iwe.cmd = SIOCGIWENCODE;
854 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
855 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
856 else
857 iwe.u.data.flags = IW_ENCODE_DISABLED;
858 iwe.u.data.length = 0;
859 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
860 &iwe, "");
861
Johannes Bergc2b13452008-09-11 00:01:55 +0200862 ieee80211_scan_add_ies(info, bss, &current_ev, end_buf);
Johannes Berg0a51b272008-09-08 17:44:25 +0200863
864 if (bss->supp_rates_len > 0) {
865 /* display all supported rates in readable format */
866 char *p = current_ev + iwe_stream_lcp_len(info);
867 int i;
868
869 memset(&iwe, 0, sizeof(iwe));
870 iwe.cmd = SIOCGIWRATE;
871 /* Those two flags are ignored... */
872 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
873
874 for (i = 0; i < bss->supp_rates_len; i++) {
875 iwe.u.bitrate.value = ((bss->supp_rates[i] &
876 0x7f) * 500000);
877 p = iwe_stream_add_value(info, current_ev, p,
878 end_buf, &iwe, IW_EV_PARAM_LEN);
879 }
880 current_ev = p;
881 }
882
883 buf = kmalloc(30, GFP_ATOMIC);
884 if (buf) {
885 memset(&iwe, 0, sizeof(iwe));
886 iwe.cmd = IWEVCUSTOM;
887 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
888 iwe.u.data.length = strlen(buf);
889 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
890 &iwe, buf);
891 memset(&iwe, 0, sizeof(iwe));
892 iwe.cmd = IWEVCUSTOM;
893 sprintf(buf, " Last beacon: %dms ago",
894 jiffies_to_msecs(jiffies - bss->last_update));
895 iwe.u.data.length = strlen(buf);
896 current_ev = iwe_stream_add_point(info, current_ev,
897 end_buf, &iwe, buf);
898 kfree(buf);
899 }
900
901 if (bss_mesh_cfg(bss)) {
902 u8 *cfg = bss_mesh_cfg(bss);
903 buf = kmalloc(50, GFP_ATOMIC);
904 if (buf) {
905 memset(&iwe, 0, sizeof(iwe));
906 iwe.cmd = IWEVCUSTOM;
907 sprintf(buf, "Mesh network (version %d)", cfg[0]);
908 iwe.u.data.length = strlen(buf);
909 current_ev = iwe_stream_add_point(info, current_ev,
910 end_buf,
911 &iwe, buf);
912 sprintf(buf, "Path Selection Protocol ID: "
913 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
914 cfg[4]);
915 iwe.u.data.length = strlen(buf);
916 current_ev = iwe_stream_add_point(info, current_ev,
917 end_buf,
918 &iwe, buf);
919 sprintf(buf, "Path Selection Metric ID: "
920 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
921 cfg[8]);
922 iwe.u.data.length = strlen(buf);
923 current_ev = iwe_stream_add_point(info, current_ev,
924 end_buf,
925 &iwe, buf);
926 sprintf(buf, "Congestion Control Mode ID: "
927 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
928 cfg[11], cfg[12]);
929 iwe.u.data.length = strlen(buf);
930 current_ev = iwe_stream_add_point(info, current_ev,
931 end_buf,
932 &iwe, buf);
933 sprintf(buf, "Channel Precedence: "
934 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
935 cfg[15], cfg[16]);
936 iwe.u.data.length = strlen(buf);
937 current_ev = iwe_stream_add_point(info, current_ev,
938 end_buf,
939 &iwe, buf);
940 kfree(buf);
941 }
942 }
943
944 return current_ev;
945}
946
947
Johannes Bergc2b13452008-09-11 00:01:55 +0200948int ieee80211_scan_results(struct ieee80211_local *local,
949 struct iw_request_info *info,
950 char *buf, size_t len)
Johannes Berg0a51b272008-09-08 17:44:25 +0200951{
952 char *current_ev = buf;
953 char *end_buf = buf + len;
Johannes Bergc2b13452008-09-11 00:01:55 +0200954 struct ieee80211_bss *bss;
Johannes Berg0a51b272008-09-08 17:44:25 +0200955
Johannes Bergc2b13452008-09-11 00:01:55 +0200956 spin_lock_bh(&local->bss_lock);
957 list_for_each_entry(bss, &local->bss_list, list) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200958 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200959 spin_unlock_bh(&local->bss_lock);
Johannes Berg0a51b272008-09-08 17:44:25 +0200960 return -E2BIG;
961 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200962 current_ev = ieee80211_scan_result(local, info, bss,
Johannes Berg0a51b272008-09-08 17:44:25 +0200963 current_ev, end_buf);
964 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200965 spin_unlock_bh(&local->bss_lock);
Johannes Berg0a51b272008-09-08 17:44:25 +0200966 return current_ev - buf;
967}