blob: c6b275b10cf93eb461fcf94c9e7aa5281e130eaa [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
17 * clean up IBSS code (in MLME), see why it adds a BSS to the list
18 * use cfg80211's BSS handling (depends on IBSS TODO above)
Johannes Berg5484e232008-09-08 17:44:27 +020019 * order BSS list by RSSI(?) ("quality of AP")
20 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
21 * SSID)
22 */
23
Johannes Berg0a51b272008-09-08 17:44:25 +020024#include <linux/wireless.h>
25#include <linux/if_arp.h>
Johannes Berg078e1e62009-01-22 18:07:31 +010026#include <linux/rtnetlink.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020027#include <net/mac80211.h>
28#include <net/iw_handler.h>
29
30#include "ieee80211_i.h"
Johannes Berg5484e232008-09-08 17:44:27 +020031#include "mesh.h"
Johannes Berg0a51b272008-09-08 17:44:25 +020032
33#define IEEE80211_PROBE_DELAY (HZ / 33)
34#define IEEE80211_CHANNEL_TIME (HZ / 33)
35#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
36
Johannes Berg5484e232008-09-08 17:44:27 +020037void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
38{
Johannes Bergc2b13452008-09-11 00:01:55 +020039 spin_lock_init(&local->bss_lock);
40 INIT_LIST_HEAD(&local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +020041}
42
43void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
44{
Johannes Bergc2b13452008-09-11 00:01:55 +020045 struct ieee80211_bss *bss, *tmp;
Johannes Berg5484e232008-09-08 17:44:27 +020046
Johannes Bergc2b13452008-09-11 00:01:55 +020047 list_for_each_entry_safe(bss, tmp, &local->bss_list, list)
Johannes Berg5484e232008-09-08 17:44:27 +020048 ieee80211_rx_bss_put(local, bss);
49}
50
Johannes Bergc2b13452008-09-11 00:01:55 +020051struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +020052ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
53 u8 *ssid, u8 ssid_len)
54{
Johannes Bergc2b13452008-09-11 00:01:55 +020055 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +020056
Johannes Bergc2b13452008-09-11 00:01:55 +020057 spin_lock_bh(&local->bss_lock);
58 bss = local->bss_hash[STA_HASH(bssid)];
Johannes Berg5484e232008-09-08 17:44:27 +020059 while (bss) {
60 if (!bss_mesh_cfg(bss) &&
61 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
62 bss->freq == freq &&
63 bss->ssid_len == ssid_len &&
64 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
65 atomic_inc(&bss->users);
66 break;
67 }
68 bss = bss->hnext;
69 }
Johannes Bergc2b13452008-09-11 00:01:55 +020070 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +020071 return bss;
72}
73
Johannes Bergc2b13452008-09-11 00:01:55 +020074/* Caller must hold local->bss_lock */
Johannes Berg5484e232008-09-08 17:44:27 +020075static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020076 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020077{
78 u8 hash_idx;
79
80 if (bss_mesh_cfg(bss))
81 hash_idx = mesh_id_hash(bss_mesh_id(bss),
82 bss_mesh_id_len(bss));
83 else
84 hash_idx = STA_HASH(bss->bssid);
85
Johannes Bergc2b13452008-09-11 00:01:55 +020086 bss->hnext = local->bss_hash[hash_idx];
87 local->bss_hash[hash_idx] = bss;
Johannes Berg5484e232008-09-08 17:44:27 +020088}
89
Johannes Bergc2b13452008-09-11 00:01:55 +020090/* Caller must hold local->bss_lock */
Johannes Berg5484e232008-09-08 17:44:27 +020091static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020092 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020093{
Johannes Bergc2b13452008-09-11 00:01:55 +020094 struct ieee80211_bss *b, *prev = NULL;
95 b = local->bss_hash[STA_HASH(bss->bssid)];
Johannes Berg5484e232008-09-08 17:44:27 +020096 while (b) {
97 if (b == bss) {
98 if (!prev)
Johannes Bergc2b13452008-09-11 00:01:55 +020099 local->bss_hash[STA_HASH(bss->bssid)] =
Johannes Berg5484e232008-09-08 17:44:27 +0200100 bss->hnext;
101 else
102 prev->hnext = bss->hnext;
103 break;
104 }
105 prev = b;
106 b = b->hnext;
107 }
108}
109
Johannes Bergc2b13452008-09-11 00:01:55 +0200110struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200111ieee80211_rx_bss_add(struct ieee80211_local *local, u8 *bssid, int freq,
112 u8 *ssid, u8 ssid_len)
113{
Johannes Bergc2b13452008-09-11 00:01:55 +0200114 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200115
116 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
117 if (!bss)
118 return NULL;
119 atomic_set(&bss->users, 2);
120 memcpy(bss->bssid, bssid, ETH_ALEN);
121 bss->freq = freq;
122 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
123 memcpy(bss->ssid, ssid, ssid_len);
124 bss->ssid_len = ssid_len;
125 }
126
Johannes Bergc2b13452008-09-11 00:01:55 +0200127 spin_lock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200128 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200129 list_add_tail(&bss->list, &local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +0200130 __ieee80211_rx_bss_hash_add(local, bss);
Johannes Bergc2b13452008-09-11 00:01:55 +0200131 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200132 return bss;
133}
134
135#ifdef CONFIG_MAC80211_MESH
Johannes Bergc2b13452008-09-11 00:01:55 +0200136static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200137ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
138 u8 *mesh_cfg, int freq)
139{
Johannes Bergc2b13452008-09-11 00:01:55 +0200140 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200141
Johannes Bergc2b13452008-09-11 00:01:55 +0200142 spin_lock_bh(&local->bss_lock);
143 bss = local->bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
Johannes Berg5484e232008-09-08 17:44:27 +0200144 while (bss) {
145 if (bss_mesh_cfg(bss) &&
146 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
147 bss->freq == freq &&
148 mesh_id_len == bss->mesh_id_len &&
149 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
150 mesh_id_len))) {
151 atomic_inc(&bss->users);
152 break;
153 }
154 bss = bss->hnext;
155 }
Johannes Bergc2b13452008-09-11 00:01:55 +0200156 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200157 return bss;
158}
159
Johannes Bergc2b13452008-09-11 00:01:55 +0200160static struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200161ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
162 u8 *mesh_cfg, int mesh_config_len, int freq)
163{
Johannes Bergc2b13452008-09-11 00:01:55 +0200164 struct ieee80211_bss *bss;
Johannes Berg5484e232008-09-08 17:44:27 +0200165
Johannes Berg1239cd52008-10-28 11:12:57 +0100166 if (mesh_config_len != IEEE80211_MESH_CONFIG_LEN)
Johannes Berg5484e232008-09-08 17:44:27 +0200167 return NULL;
168
169 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
170 if (!bss)
171 return NULL;
172
173 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
174 if (!bss->mesh_cfg) {
175 kfree(bss);
176 return NULL;
177 }
178
179 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
180 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
181 if (!bss->mesh_id) {
182 kfree(bss->mesh_cfg);
183 kfree(bss);
184 return NULL;
185 }
186 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
187 }
188
189 atomic_set(&bss->users, 2);
190 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
191 bss->mesh_id_len = mesh_id_len;
192 bss->freq = freq;
Johannes Bergc2b13452008-09-11 00:01:55 +0200193 spin_lock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200194 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200195 list_add_tail(&bss->list, &local->bss_list);
Johannes Berg5484e232008-09-08 17:44:27 +0200196 __ieee80211_rx_bss_hash_add(local, bss);
Johannes Bergc2b13452008-09-11 00:01:55 +0200197 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200198 return bss;
199}
200#endif
201
Johannes Bergc2b13452008-09-11 00:01:55 +0200202static void ieee80211_rx_bss_free(struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +0200203{
204 kfree(bss->ies);
205 kfree(bss_mesh_id(bss));
206 kfree(bss_mesh_cfg(bss));
207 kfree(bss);
208}
209
210void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +0200211 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +0200212{
213 local_bh_disable();
Johannes Bergc2b13452008-09-11 00:01:55 +0200214 if (!atomic_dec_and_lock(&bss->users, &local->bss_lock)) {
Johannes Berg5484e232008-09-08 17:44:27 +0200215 local_bh_enable();
216 return;
217 }
218
219 __ieee80211_rx_bss_hash_del(local, bss);
220 list_del(&bss->list);
Johannes Bergc2b13452008-09-11 00:01:55 +0200221 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200222 ieee80211_rx_bss_free(bss);
223}
224
Johannes Bergc2b13452008-09-11 00:01:55 +0200225struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200226ieee80211_bss_info_update(struct ieee80211_local *local,
227 struct ieee80211_rx_status *rx_status,
228 struct ieee80211_mgmt *mgmt,
229 size_t len,
230 struct ieee802_11_elems *elems,
Johannes Berg2a519312009-02-10 21:25:55 +0100231 struct ieee80211_channel *channel,
232 bool beacon)
Johannes Berg5484e232008-09-08 17:44:27 +0200233{
Johannes Bergc2b13452008-09-11 00:01:55 +0200234 struct ieee80211_bss *bss;
Johannes Berg2a519312009-02-10 21:25:55 +0100235 int clen, freq = channel->center_freq;
236 enum cfg80211_signal_type sigtype = CFG80211_SIGNAL_TYPE_NONE;
237 s32 signal = 0;
238
239 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
240 sigtype = CFG80211_SIGNAL_TYPE_MBM;
241 signal = rx_status->signal * 100;
242 } else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
243 sigtype = CFG80211_SIGNAL_TYPE_UNSPEC;
244 signal = (rx_status->signal * 100) / local->hw.max_signal;
245 }
246
247 cfg80211_put_bss(
248 cfg80211_inform_bss_frame(local->hw.wiphy, channel,
249 mgmt, len, signal, sigtype,
250 GFP_ATOMIC));
Johannes Berg5484e232008-09-08 17:44:27 +0200251
252#ifdef CONFIG_MAC80211_MESH
253 if (elems->mesh_config)
254 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
255 elems->mesh_id_len, elems->mesh_config, freq);
256 else
257#endif
258 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
259 elems->ssid, elems->ssid_len);
260 if (!bss) {
261#ifdef CONFIG_MAC80211_MESH
262 if (elems->mesh_config)
263 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
264 elems->mesh_id_len, elems->mesh_config,
265 elems->mesh_config_len, freq);
266 else
267#endif
268 bss = ieee80211_rx_bss_add(local, mgmt->bssid, freq,
269 elems->ssid, elems->ssid_len);
270 if (!bss)
271 return NULL;
272 } else {
273#if 0
274 /* TODO: order by RSSI? */
Johannes Bergc2b13452008-09-11 00:01:55 +0200275 spin_lock_bh(&local->bss_lock);
276 list_move_tail(&bss->list, &local->bss_list);
277 spin_unlock_bh(&local->bss_lock);
Johannes Berg5484e232008-09-08 17:44:27 +0200278#endif
279 }
280
281 /* save the ERP value so that it is available at association time */
282 if (elems->erp_info && elems->erp_info_len >= 1) {
283 bss->erp_value = elems->erp_info[0];
284 bss->has_erp_value = 1;
285 }
286
287 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
288 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
289
290 if (elems->tim) {
291 struct ieee80211_tim_ie *tim_ie =
292 (struct ieee80211_tim_ie *)elems->tim;
293 bss->dtim_period = tim_ie->dtim_period;
294 }
295
296 /* set default value for buggy APs */
297 if (!elems->tim || bss->dtim_period == 0)
298 bss->dtim_period = 1;
299
300 bss->supp_rates_len = 0;
301 if (elems->supp_rates) {
302 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
303 if (clen > elems->supp_rates_len)
304 clen = elems->supp_rates_len;
305 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
306 clen);
307 bss->supp_rates_len += clen;
308 }
309 if (elems->ext_supp_rates) {
310 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
311 if (clen > elems->ext_supp_rates_len)
312 clen = elems->ext_supp_rates_len;
313 memcpy(&bss->supp_rates[bss->supp_rates_len],
314 elems->ext_supp_rates, clen);
315 bss->supp_rates_len += clen;
316 }
317
318 bss->band = rx_status->band;
319
320 bss->timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
321 bss->last_update = jiffies;
322 bss->signal = rx_status->signal;
323 bss->noise = rx_status->noise;
324 bss->qual = rx_status->qual;
325 bss->wmm_used = elems->wmm_param || elems->wmm_info;
326
327 if (!beacon)
328 bss->last_probe_resp = jiffies;
329
330 /*
331 * For probe responses, or if we don't have any information yet,
332 * use the IEs from the beacon.
333 */
334 if (!bss->ies || !beacon) {
335 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
336 kfree(bss->ies);
337 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
338 }
339 if (bss->ies) {
340 memcpy(bss->ies, elems->ie_start, elems->total_len);
341 bss->ies_len = elems->total_len;
342 } else
343 bss->ies_len = 0;
344 }
345
346 return bss;
347}
Johannes Berg0a51b272008-09-08 17:44:25 +0200348
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +0530349void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
350 int freq, u8 *ssid, u8 ssid_len)
351{
352 struct ieee80211_bss *bss;
353 struct ieee80211_local *local = sdata->local;
354
355 bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
356 if (bss) {
357 atomic_dec(&bss->users);
358 ieee80211_rx_bss_put(local, bss);
359 }
360}
361
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200362ieee80211_rx_result
Johannes Bergc2b13452008-09-11 00:01:55 +0200363ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
364 struct ieee80211_rx_status *rx_status)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200365{
366 struct ieee80211_mgmt *mgmt;
Johannes Bergc2b13452008-09-11 00:01:55 +0200367 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200368 u8 *elements;
369 struct ieee80211_channel *channel;
370 size_t baselen;
371 int freq;
372 __le16 fc;
373 bool presp, beacon = false;
374 struct ieee802_11_elems elems;
375
376 if (skb->len < 2)
377 return RX_DROP_UNUSABLE;
378
379 mgmt = (struct ieee80211_mgmt *) skb->data;
380 fc = mgmt->frame_control;
381
382 if (ieee80211_is_ctl(fc))
383 return RX_CONTINUE;
384
385 if (skb->len < 24)
386 return RX_DROP_MONITOR;
387
388 presp = ieee80211_is_probe_resp(fc);
389 if (presp) {
390 /* ignore ProbeResp to foreign address */
391 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
392 return RX_DROP_MONITOR;
393
394 presp = true;
395 elements = mgmt->u.probe_resp.variable;
396 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
397 } else {
398 beacon = ieee80211_is_beacon(fc);
399 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
400 elements = mgmt->u.beacon.variable;
401 }
402
403 if (!presp && !beacon)
404 return RX_CONTINUE;
405
406 if (baselen > skb->len)
407 return RX_DROP_MONITOR;
408
409 ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
410
411 if (elems.ds_params && elems.ds_params_len == 1)
412 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
413 else
414 freq = rx_status->freq;
415
416 channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
417
418 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
419 return RX_DROP_MONITOR;
420
421 bss = ieee80211_bss_info_update(sdata->local, rx_status,
422 mgmt, skb->len, &elems,
Johannes Berg2a519312009-02-10 21:25:55 +0100423 channel, beacon);
Jouni Malinend048e502008-10-11 03:29:55 +0300424 if (bss)
425 ieee80211_rx_bss_put(sdata->local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200426
427 dev_kfree_skb(skb);
428 return RX_QUEUED;
429}
430
Vivek Natarajana97b77b2008-12-23 18:39:02 -0800431void ieee80211_send_nullfunc(struct ieee80211_local *local,
Johannes Berg0a51b272008-09-08 17:44:25 +0200432 struct ieee80211_sub_if_data *sdata,
433 int powersave)
434{
435 struct sk_buff *skb;
436 struct ieee80211_hdr *nullfunc;
437 __le16 fc;
438
439 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
440 if (!skb) {
441 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
442 "frame\n", sdata->dev->name);
443 return;
444 }
445 skb_reserve(skb, local->hw.extra_tx_headroom);
446
447 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
448 memset(nullfunc, 0, 24);
449 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
450 IEEE80211_FCTL_TODS);
451 if (powersave)
452 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
453 nullfunc->frame_control = fc;
454 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
455 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
456 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
457
Johannes Berge50db652008-09-09 15:07:09 +0200458 ieee80211_tx_skb(sdata, skb, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200459}
460
Johannes Berg2a519312009-02-10 21:25:55 +0100461void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200462{
463 struct ieee80211_local *local = hw_to_local(hw);
464 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200465
Johannes Bergc2b13452008-09-11 00:01:55 +0200466 if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
Johannes Berg5bc75722008-09-11 00:01:51 +0200467 return;
468
Johannes Berg2a519312009-02-10 21:25:55 +0100469 if (WARN_ON(!local->scan_req))
470 return;
Johannes Berg5bc75722008-09-11 00:01:51 +0200471
Johannes Berg2a519312009-02-10 21:25:55 +0100472 if (local->scan_req != &local->int_scan_req)
473 cfg80211_scan_done(local->scan_req, aborted);
474 local->scan_req = NULL;
475
476 local->last_scan_completed = jiffies;
Johannes Berg0a51b272008-09-08 17:44:25 +0200477
Johannes Bergc2b13452008-09-11 00:01:55 +0200478 if (local->hw_scanning) {
479 local->hw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200480 /*
481 * Somebody might have requested channel change during scan
482 * that we won't have acted upon, try now. ieee80211_hw_config
483 * will set the flag based on actual changes.
484 */
485 ieee80211_hw_config(local, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200486 goto done;
487 }
488
Johannes Bergc2b13452008-09-11 00:01:55 +0200489 local->sw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200490 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg0a51b272008-09-08 17:44:25 +0200491
492 netif_tx_lock_bh(local->mdev);
493 netif_addr_lock(local->mdev);
494 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
495 local->ops->configure_filter(local_to_hw(local),
496 FIF_BCN_PRBRESP_PROMISC,
497 &local->filter_flags,
498 local->mdev->mc_count,
499 local->mdev->mc_list);
500
501 netif_addr_unlock(local->mdev);
502 netif_tx_unlock_bh(local->mdev);
503
Johannes Berg078e1e62009-01-22 18:07:31 +0100504 mutex_lock(&local->iflist_mtx);
505 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100506 if (!netif_running(sdata->dev))
507 continue;
508
Johannes Berg0a51b272008-09-08 17:44:25 +0200509 /* Tell AP we're back */
Johannes Berg05c914f2008-09-11 00:01:58 +0200510 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200511 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
512 ieee80211_send_nullfunc(local, sdata, 0);
513 netif_tx_wake_all_queues(sdata->dev);
514 }
515 } else
516 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg078e1e62009-01-22 18:07:31 +0100517
Johannes Berg14b80722009-02-10 21:25:42 +0100518 /* re-enable beaconing */
519 if (sdata->vif.type == NL80211_IFTYPE_AP ||
520 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
521 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
522 ieee80211_if_config(sdata,
523 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg0a51b272008-09-08 17:44:25 +0200524 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100525 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200526
527 done:
528 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg472dbc42008-09-11 00:01:49 +0200529 ieee80211_mesh_notify_scan_completed(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200530}
531EXPORT_SYMBOL(ieee80211_scan_completed);
532
Johannes Bergc2b13452008-09-11 00:01:55 +0200533void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200534{
535 struct ieee80211_local *local =
536 container_of(work, struct ieee80211_local, scan_work.work);
537 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200538 struct ieee80211_channel *chan;
Johannes Berg2a519312009-02-10 21:25:55 +0100539 int skip, i;
Johannes Berg0a51b272008-09-08 17:44:25 +0200540 unsigned long next_delay = 0;
541
Johannes Berg5bc75722008-09-11 00:01:51 +0200542 /*
543 * Avoid re-scheduling when the sdata is going away.
544 */
545 if (!netif_running(sdata->dev))
Johannes Berg0a51b272008-09-08 17:44:25 +0200546 return;
547
548 switch (local->scan_state) {
549 case SCAN_SET_CHANNEL:
Johannes Berg0a51b272008-09-08 17:44:25 +0200550 /* if no more bands/channels left, complete scan */
Johannes Berg2a519312009-02-10 21:25:55 +0100551 if (local->scan_channel_idx >= local->scan_req->n_channels) {
552 ieee80211_scan_completed(local_to_hw(local), false);
Johannes Berg0a51b272008-09-08 17:44:25 +0200553 return;
554 }
555 skip = 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100556 chan = local->scan_req->channels[local->scan_channel_idx];
Johannes Berg0a51b272008-09-08 17:44:25 +0200557
558 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200559 (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg0a51b272008-09-08 17:44:25 +0200560 chan->flags & IEEE80211_CHAN_NO_IBSS))
561 skip = 1;
562
563 if (!skip) {
564 local->scan_channel = chan;
Johannes Berge8975582008-10-09 12:18:51 +0200565 if (ieee80211_hw_config(local,
566 IEEE80211_CONF_CHANGE_CHANNEL))
Johannes Berg0a51b272008-09-08 17:44:25 +0200567 skip = 1;
Johannes Berg0a51b272008-09-08 17:44:25 +0200568 }
569
570 /* advance state machine to next channel/band */
571 local->scan_channel_idx++;
Johannes Berg0a51b272008-09-08 17:44:25 +0200572
573 if (skip)
574 break;
575
576 next_delay = IEEE80211_PROBE_DELAY +
577 usecs_to_jiffies(local->hw.channel_change_time);
578 local->scan_state = SCAN_SEND_PROBE;
579 break;
580 case SCAN_SEND_PROBE:
581 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
582 local->scan_state = SCAN_SET_CHANNEL;
583
Johannes Berg2a519312009-02-10 21:25:55 +0100584 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
585 !local->scan_req->n_ssids)
Johannes Berg0a51b272008-09-08 17:44:25 +0200586 break;
Johannes Berg2a519312009-02-10 21:25:55 +0100587 for (i = 0; i < local->scan_req->n_ssids; i++)
588 ieee80211_send_probe_req(
589 sdata, NULL,
590 local->scan_req->ssids[i].ssid,
591 local->scan_req->ssids[i].ssid_len);
Johannes Berg0a51b272008-09-08 17:44:25 +0200592 next_delay = IEEE80211_CHANNEL_TIME;
593 break;
594 }
595
Johannes Berg5bc75722008-09-11 00:01:51 +0200596 queue_delayed_work(local->hw.workqueue, &local->scan_work,
597 next_delay);
Johannes Berg0a51b272008-09-08 17:44:25 +0200598}
599
600
Johannes Bergc2b13452008-09-11 00:01:55 +0200601int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
Johannes Berg2a519312009-02-10 21:25:55 +0100602 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200603{
604 struct ieee80211_local *local = scan_sdata->local;
605 struct ieee80211_sub_if_data *sdata;
606
Johannes Berg2a519312009-02-10 21:25:55 +0100607 if (!req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200608 return -EINVAL;
609
Johannes Berg2a519312009-02-10 21:25:55 +0100610 if (local->scan_req && local->scan_req != req)
611 return -EBUSY;
612
613 local->scan_req = req;
614
Johannes Berg0a51b272008-09-08 17:44:25 +0200615 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
616 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
617 * BSSID: MACAddress
618 * SSID
619 * ScanType: ACTIVE, PASSIVE
620 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
621 * a Probe frame during active scanning
622 * ChannelList
623 * MinChannelTime (>= ProbeDelay), in TU
624 * MaxChannelTime: (>= MinChannelTime), in TU
625 */
626
627 /* MLME-SCAN.confirm
628 * BSSDescriptionSet
629 * ResultCode: SUCCESS, INVALID_PARAMETERS
630 */
631
Johannes Bergc2b13452008-09-11 00:01:55 +0200632 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200633 if (local->scan_sdata == scan_sdata)
634 return 0;
635 return -EBUSY;
636 }
637
638 if (local->ops->hw_scan) {
Johannes Berg5bc75722008-09-11 00:01:51 +0200639 int rc;
640
Johannes Bergc2b13452008-09-11 00:01:55 +0200641 local->hw_scanning = true;
Johannes Berg2a519312009-02-10 21:25:55 +0100642 rc = local->ops->hw_scan(local_to_hw(local), req);
Johannes Berg5bc75722008-09-11 00:01:51 +0200643 if (rc) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200644 local->hw_scanning = false;
Johannes Berg5bc75722008-09-11 00:01:51 +0200645 return rc;
Johannes Berg0a51b272008-09-08 17:44:25 +0200646 }
Johannes Berg5bc75722008-09-11 00:01:51 +0200647 local->scan_sdata = scan_sdata;
648 return 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200649 }
650
Johannes Bergc2b13452008-09-11 00:01:55 +0200651 local->sw_scanning = true;
Johannes Berg0a51b272008-09-08 17:44:25 +0200652
Johannes Berg078e1e62009-01-22 18:07:31 +0100653 mutex_lock(&local->iflist_mtx);
654 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100655 if (!netif_running(sdata->dev))
656 continue;
657
Johannes Berg14b80722009-02-10 21:25:42 +0100658 /* disable beaconing */
659 if (sdata->vif.type == NL80211_IFTYPE_AP ||
660 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
661 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
662 ieee80211_if_config(sdata,
663 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg078e1e62009-01-22 18:07:31 +0100664
Johannes Berg05c914f2008-09-11 00:01:58 +0200665 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200666 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
667 netif_tx_stop_all_queues(sdata->dev);
668 ieee80211_send_nullfunc(local, sdata, 1);
669 }
670 } else
671 netif_tx_stop_all_queues(sdata->dev);
672 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100673 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200674
Johannes Berg0a51b272008-09-08 17:44:25 +0200675 local->scan_state = SCAN_SET_CHANNEL;
676 local->scan_channel_idx = 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200677 local->scan_sdata = scan_sdata;
Johannes Berg2a519312009-02-10 21:25:55 +0100678 local->scan_req = req;
Johannes Berg0a51b272008-09-08 17:44:25 +0200679
680 netif_addr_lock_bh(local->mdev);
681 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
682 local->ops->configure_filter(local_to_hw(local),
683 FIF_BCN_PRBRESP_PROMISC,
684 &local->filter_flags,
685 local->mdev->mc_count,
686 local->mdev->mc_list);
687 netif_addr_unlock_bh(local->mdev);
688
689 /* TODO: start scan as soon as all nullfunc frames are ACKed */
690 queue_delayed_work(local->hw.workqueue, &local->scan_work,
691 IEEE80211_CHANNEL_TIME);
692
693 return 0;
694}
695
696
Johannes Bergc2b13452008-09-11 00:01:55 +0200697int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +0100698 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200699{
Johannes Berg0a51b272008-09-08 17:44:25 +0200700 struct ieee80211_local *local = sdata->local;
Johannes Berg9116dd02008-09-08 17:47:23 +0200701 struct ieee80211_if_sta *ifsta;
Johannes Berg0a51b272008-09-08 17:44:25 +0200702
Johannes Berg2a519312009-02-10 21:25:55 +0100703 if (!req)
704 return -EINVAL;
705
706 if (local->scan_req && local->scan_req != req)
707 return -EBUSY;
708
709 local->scan_req = req;
710
Johannes Berg05c914f2008-09-11 00:01:58 +0200711 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg2a519312009-02-10 21:25:55 +0100712 return ieee80211_start_scan(sdata, req);
Johannes Berg0a51b272008-09-08 17:44:25 +0200713
Johannes Berg9116dd02008-09-08 17:47:23 +0200714 /*
715 * STA has a state machine that might need to defer scanning
716 * while it's trying to associate/authenticate, therefore we
717 * queue it up to the state machine in that case.
718 */
719
Johannes Bergc2b13452008-09-11 00:01:55 +0200720 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200721 if (local->scan_sdata == sdata)
722 return 0;
723 return -EBUSY;
724 }
725
Johannes Berg9116dd02008-09-08 17:47:23 +0200726 ifsta = &sdata->u.sta;
Johannes Berg0a51b272008-09-08 17:44:25 +0200727 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
728 queue_work(local->hw.workqueue, &ifsta->work);
Johannes Berg9116dd02008-09-08 17:47:23 +0200729
Johannes Berg0a51b272008-09-08 17:44:25 +0200730 return 0;
731}