blob: 67714d7ed5b4a263253b69d903ea7401ee990126 [file] [log] [blame]
Johannes Berg2a519312009-02-10 21:25:55 +01001/*
2 * cfg80211 scan result handling
3 *
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/netdevice.h>
9#include <linux/wireless.h>
10#include <linux/nl80211.h>
11#include <linux/etherdevice.h>
12#include <net/arp.h>
13#include <net/cfg80211.h>
14#include <net/iw_handler.h>
15#include "core.h"
16#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020017#include "wext-compat.h"
Johannes Berg2a519312009-02-10 21:25:55 +010018
Helmut Schaa09f97e02009-07-23 12:14:29 +020019#define IEEE80211_SCAN_RESULT_EXPIRE (15 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010020
Johannes Berg667503d2009-07-07 03:56:11 +020021void __cfg80211_scan_done(struct work_struct *wk)
Johannes Berg2a519312009-02-10 21:25:55 +010022{
Johannes Berg667503d2009-07-07 03:56:11 +020023 struct cfg80211_registered_device *rdev;
24 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +010025 struct net_device *dev;
26#ifdef CONFIG_WIRELESS_EXT
27 union iwreq_data wrqu;
28#endif
29
Johannes Berg667503d2009-07-07 03:56:11 +020030 rdev = container_of(wk, struct cfg80211_registered_device,
31 scan_done_wk);
32
33 mutex_lock(&rdev->mtx);
34 request = rdev->scan_req;
35
Johannes Berg463d0182009-07-14 00:33:35 +020036 dev = request->dev;
Johannes Berg2a519312009-02-10 21:25:55 +010037
Johannes Berg6829c872009-07-02 09:13:27 +020038 /*
39 * This must be before sending the other events!
40 * Otherwise, wpa_supplicant gets completely confused with
41 * wext events.
42 */
43 cfg80211_sme_scan_done(dev);
44
Johannes Berg667503d2009-07-07 03:56:11 +020045 if (request->aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010046 nl80211_send_scan_aborted(wiphy_to_dev(request->wiphy), dev);
47 else
48 nl80211_send_scan_done(wiphy_to_dev(request->wiphy), dev);
49
50#ifdef CONFIG_WIRELESS_EXT
Johannes Berg667503d2009-07-07 03:56:11 +020051 if (!request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +010052 memset(&wrqu, 0, sizeof(wrqu));
53
54 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
55 }
56#endif
57
58 dev_put(dev);
59
Johannes Berg667503d2009-07-07 03:56:11 +020060 cfg80211_unlock_rdev(rdev);
Christian Lamparter9e81ecc2009-07-19 05:05:37 +020061 wiphy_to_dev(request->wiphy)->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010062 kfree(request);
63}
Johannes Berg667503d2009-07-07 03:56:11 +020064
65void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
66{
Johannes Berg667503d2009-07-07 03:56:11 +020067 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
68
69 request->aborted = aborted;
70 schedule_work(&wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +020071}
Johannes Berg2a519312009-02-10 21:25:55 +010072EXPORT_SYMBOL(cfg80211_scan_done);
73
74static void bss_release(struct kref *ref)
75{
76 struct cfg80211_internal_bss *bss;
77
78 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Berg78c1c7e2009-02-10 21:25:57 +010079 if (bss->pub.free_priv)
80 bss->pub.free_priv(&bss->pub);
Johannes Bergcd1658f2009-04-16 15:00:58 +020081
82 if (bss->ies_allocated)
83 kfree(bss->pub.information_elements);
84
Johannes Berg19957bb2009-07-02 17:20:43 +020085 BUG_ON(atomic_read(&bss->hold));
86
Johannes Berg2a519312009-02-10 21:25:55 +010087 kfree(bss);
88}
89
90/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -050091void cfg80211_bss_age(struct cfg80211_registered_device *dev,
92 unsigned long age_secs)
93{
94 struct cfg80211_internal_bss *bss;
95 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
96
97 list_for_each_entry(bss, &dev->bss_list, list) {
98 bss->ts -= age_jiffies;
99 }
100}
101
102/* must hold dev->bss_lock! */
Johannes Berg2a519312009-02-10 21:25:55 +0100103void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
104{
105 struct cfg80211_internal_bss *bss, *tmp;
106 bool expired = false;
107
108 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200109 if (atomic_read(&bss->hold))
110 continue;
111 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
Johannes Berg2a519312009-02-10 21:25:55 +0100112 continue;
113 list_del(&bss->list);
114 rb_erase(&bss->rbn, &dev->bss_tree);
115 kref_put(&bss->ref, bss_release);
116 expired = true;
117 }
118
119 if (expired)
120 dev->bss_generation++;
121}
122
123static u8 *find_ie(u8 num, u8 *ies, size_t len)
124{
125 while (len > 2 && ies[0] != num) {
126 len -= ies[1] + 2;
127 ies += ies[1] + 2;
128 }
129 if (len < 2)
130 return NULL;
131 if (len < 2 + ies[1])
132 return NULL;
133 return ies;
134}
135
136static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
137{
138 const u8 *ie1 = find_ie(num, ies1, len1);
139 const u8 *ie2 = find_ie(num, ies2, len2);
140 int r;
141
142 if (!ie1 && !ie2)
143 return 0;
144 if (!ie1)
145 return -1;
146
147 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
148 if (r == 0 && ie1[1] != ie2[1])
149 return ie2[1] - ie1[1];
150 return r;
151}
152
153static bool is_bss(struct cfg80211_bss *a,
154 const u8 *bssid,
155 const u8 *ssid, size_t ssid_len)
156{
157 const u8 *ssidie;
158
Johannes Berg79420f02009-02-10 21:25:59 +0100159 if (bssid && compare_ether_addr(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100160 return false;
161
Johannes Berg79420f02009-02-10 21:25:59 +0100162 if (!ssid)
163 return true;
164
Johannes Berg2a519312009-02-10 21:25:55 +0100165 ssidie = find_ie(WLAN_EID_SSID,
166 a->information_elements,
167 a->len_information_elements);
168 if (!ssidie)
169 return false;
170 if (ssidie[1] != ssid_len)
171 return false;
172 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
173}
174
175static bool is_mesh(struct cfg80211_bss *a,
176 const u8 *meshid, size_t meshidlen,
177 const u8 *meshcfg)
178{
179 const u8 *ie;
180
181 if (!is_zero_ether_addr(a->bssid))
182 return false;
183
184 ie = find_ie(WLAN_EID_MESH_ID,
185 a->information_elements,
186 a->len_information_elements);
187 if (!ie)
188 return false;
189 if (ie[1] != meshidlen)
190 return false;
191 if (memcmp(ie + 2, meshid, meshidlen))
192 return false;
193
194 ie = find_ie(WLAN_EID_MESH_CONFIG,
195 a->information_elements,
196 a->len_information_elements);
197 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
198 return false;
199
200 /*
201 * Ignore mesh capability (last two bytes of the IE) when
202 * comparing since that may differ between stations taking
203 * part in the same mesh.
204 */
205 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
206}
207
208static int cmp_bss(struct cfg80211_bss *a,
209 struct cfg80211_bss *b)
210{
211 int r;
212
213 if (a->channel != b->channel)
214 return b->channel->center_freq - a->channel->center_freq;
215
216 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
217 if (r)
218 return r;
219
220 if (is_zero_ether_addr(a->bssid)) {
221 r = cmp_ies(WLAN_EID_MESH_ID,
222 a->information_elements,
223 a->len_information_elements,
224 b->information_elements,
225 b->len_information_elements);
226 if (r)
227 return r;
228 return cmp_ies(WLAN_EID_MESH_CONFIG,
229 a->information_elements,
230 a->len_information_elements,
231 b->information_elements,
232 b->len_information_elements);
233 }
234
235 return cmp_ies(WLAN_EID_SSID,
236 a->information_elements,
237 a->len_information_elements,
238 b->information_elements,
239 b->len_information_elements);
240}
241
242struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
243 struct ieee80211_channel *channel,
244 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100245 const u8 *ssid, size_t ssid_len,
246 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100247{
248 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
249 struct cfg80211_internal_bss *bss, *res = NULL;
250
251 spin_lock_bh(&dev->bss_lock);
252
253 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100254 if ((bss->pub.capability & capa_mask) != capa_val)
255 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100256 if (channel && bss->pub.channel != channel)
257 continue;
258 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
259 res = bss;
260 kref_get(&res->ref);
261 break;
262 }
263 }
264
265 spin_unlock_bh(&dev->bss_lock);
266 if (!res)
267 return NULL;
268 return &res->pub;
269}
270EXPORT_SYMBOL(cfg80211_get_bss);
271
272struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
273 struct ieee80211_channel *channel,
274 const u8 *meshid, size_t meshidlen,
275 const u8 *meshcfg)
276{
277 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
278 struct cfg80211_internal_bss *bss, *res = NULL;
279
280 spin_lock_bh(&dev->bss_lock);
281
282 list_for_each_entry(bss, &dev->bss_list, list) {
283 if (channel && bss->pub.channel != channel)
284 continue;
285 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
286 res = bss;
287 kref_get(&res->ref);
288 break;
289 }
290 }
291
292 spin_unlock_bh(&dev->bss_lock);
293 if (!res)
294 return NULL;
295 return &res->pub;
296}
297EXPORT_SYMBOL(cfg80211_get_mesh);
298
299
300static void rb_insert_bss(struct cfg80211_registered_device *dev,
301 struct cfg80211_internal_bss *bss)
302{
303 struct rb_node **p = &dev->bss_tree.rb_node;
304 struct rb_node *parent = NULL;
305 struct cfg80211_internal_bss *tbss;
306 int cmp;
307
308 while (*p) {
309 parent = *p;
310 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
311
312 cmp = cmp_bss(&bss->pub, &tbss->pub);
313
314 if (WARN_ON(!cmp)) {
315 /* will sort of leak this BSS */
316 return;
317 }
318
319 if (cmp < 0)
320 p = &(*p)->rb_left;
321 else
322 p = &(*p)->rb_right;
323 }
324
325 rb_link_node(&bss->rbn, parent, p);
326 rb_insert_color(&bss->rbn, &dev->bss_tree);
327}
328
329static struct cfg80211_internal_bss *
330rb_find_bss(struct cfg80211_registered_device *dev,
331 struct cfg80211_internal_bss *res)
332{
333 struct rb_node *n = dev->bss_tree.rb_node;
334 struct cfg80211_internal_bss *bss;
335 int r;
336
337 while (n) {
338 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
339 r = cmp_bss(&res->pub, &bss->pub);
340
341 if (r == 0)
342 return bss;
343 else if (r < 0)
344 n = n->rb_left;
345 else
346 n = n->rb_right;
347 }
348
349 return NULL;
350}
351
352static struct cfg80211_internal_bss *
353cfg80211_bss_update(struct cfg80211_registered_device *dev,
354 struct cfg80211_internal_bss *res,
355 bool overwrite)
356{
357 struct cfg80211_internal_bss *found = NULL;
358 const u8 *meshid, *meshcfg;
359
360 /*
361 * The reference to "res" is donated to this function.
362 */
363
364 if (WARN_ON(!res->pub.channel)) {
365 kref_put(&res->ref, bss_release);
366 return NULL;
367 }
368
369 res->ts = jiffies;
370
371 if (is_zero_ether_addr(res->pub.bssid)) {
372 /* must be mesh, verify */
373 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
374 res->pub.len_information_elements);
375 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
376 res->pub.information_elements,
377 res->pub.len_information_elements);
378 if (!meshid || !meshcfg ||
379 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
380 /* bogus mesh */
381 kref_put(&res->ref, bss_release);
382 return NULL;
383 }
384 }
385
386 spin_lock_bh(&dev->bss_lock);
387
388 found = rb_find_bss(dev, res);
389
Johannes Bergcd1658f2009-04-16 15:00:58 +0200390 if (found) {
Johannes Berg2a519312009-02-10 21:25:55 +0100391 found->pub.beacon_interval = res->pub.beacon_interval;
392 found->pub.tsf = res->pub.tsf;
393 found->pub.signal = res->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100394 found->pub.capability = res->pub.capability;
395 found->ts = res->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200396
397 /* overwrite IEs */
398 if (overwrite) {
399 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
400 size_t ielen = res->pub.len_information_elements;
401
Michael Buesch44e1b982009-04-26 11:27:33 +0200402 if (!found->ies_allocated && ksize(found) >= used + ielen) {
Johannes Bergcd1658f2009-04-16 15:00:58 +0200403 memcpy(found->pub.information_elements,
404 res->pub.information_elements, ielen);
405 found->pub.len_information_elements = ielen;
406 } else {
407 u8 *ies = found->pub.information_elements;
408
Michael Buesch273de922009-04-25 22:28:55 +0200409 if (found->ies_allocated)
410 ies = krealloc(ies, ielen, GFP_ATOMIC);
411 else
Johannes Bergcd1658f2009-04-16 15:00:58 +0200412 ies = kmalloc(ielen, GFP_ATOMIC);
413
414 if (ies) {
415 memcpy(ies, res->pub.information_elements, ielen);
416 found->ies_allocated = true;
417 found->pub.information_elements = ies;
Johannes Bergc0f0aac2009-04-30 20:09:56 +0200418 found->pub.len_information_elements = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200419 }
420 }
421 }
422
Johannes Berg2a519312009-02-10 21:25:55 +0100423 kref_put(&res->ref, bss_release);
424 } else {
425 /* this "consumes" the reference */
426 list_add_tail(&res->list, &dev->bss_list);
427 rb_insert_bss(dev, res);
428 found = res;
429 }
430
431 dev->bss_generation++;
432 spin_unlock_bh(&dev->bss_lock);
433
434 kref_get(&found->ref);
435 return found;
436}
437
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200438struct cfg80211_bss*
439cfg80211_inform_bss(struct wiphy *wiphy,
440 struct ieee80211_channel *channel,
441 const u8 *bssid,
442 u64 timestamp, u16 capability, u16 beacon_interval,
443 const u8 *ie, size_t ielen,
444 s32 signal, gfp_t gfp)
445{
446 struct cfg80211_internal_bss *res;
447 size_t privsz;
448
449 if (WARN_ON(!wiphy))
450 return NULL;
451
452 privsz = wiphy->bss_priv_size;
453
454 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
455 (signal < 0 || signal > 100)))
456 return NULL;
457
458 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
459 if (!res)
460 return NULL;
461
462 memcpy(res->pub.bssid, bssid, ETH_ALEN);
463 res->pub.channel = channel;
464 res->pub.signal = signal;
465 res->pub.tsf = timestamp;
466 res->pub.beacon_interval = beacon_interval;
467 res->pub.capability = capability;
468 /* point to after the private area */
469 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
470 memcpy(res->pub.information_elements, ie, ielen);
471 res->pub.len_information_elements = ielen;
472
473 kref_init(&res->ref);
474
475 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
476 if (!res)
477 return NULL;
478
479 if (res->pub.capability & WLAN_CAPABILITY_ESS)
480 regulatory_hint_found_beacon(wiphy, channel, gfp);
481
482 /* cfg80211_bss_update gives us a referenced result */
483 return &res->pub;
484}
485EXPORT_SYMBOL(cfg80211_inform_bss);
486
Johannes Berg2a519312009-02-10 21:25:55 +0100487struct cfg80211_bss *
488cfg80211_inform_bss_frame(struct wiphy *wiphy,
489 struct ieee80211_channel *channel,
490 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100491 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100492{
493 struct cfg80211_internal_bss *res;
494 size_t ielen = len - offsetof(struct ieee80211_mgmt,
495 u.probe_resp.variable);
496 bool overwrite;
497 size_t privsz = wiphy->bss_priv_size;
498
Johannes Berg77965c92009-02-18 18:45:06 +0100499 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
Johannes Berg2a519312009-02-10 21:25:55 +0100500 (signal < 0 || signal > 100)))
501 return NULL;
502
503 if (WARN_ON(!mgmt || !wiphy ||
504 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
505 return NULL;
506
507 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
508 if (!res)
509 return NULL;
510
511 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
512 res->pub.channel = channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100513 res->pub.signal = signal;
514 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
515 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
516 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
517 /* point to after the private area */
518 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
519 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
520 res->pub.len_information_elements = ielen;
521
522 kref_init(&res->ref);
523
524 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
525
526 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
527 if (!res)
528 return NULL;
529
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500530 if (res->pub.capability & WLAN_CAPABILITY_ESS)
531 regulatory_hint_found_beacon(wiphy, channel, gfp);
532
Johannes Berg2a519312009-02-10 21:25:55 +0100533 /* cfg80211_bss_update gives us a referenced result */
534 return &res->pub;
535}
536EXPORT_SYMBOL(cfg80211_inform_bss_frame);
537
538void cfg80211_put_bss(struct cfg80211_bss *pub)
539{
540 struct cfg80211_internal_bss *bss;
541
542 if (!pub)
543 return;
544
545 bss = container_of(pub, struct cfg80211_internal_bss, pub);
546 kref_put(&bss->ref, bss_release);
547}
548EXPORT_SYMBOL(cfg80211_put_bss);
549
Johannes Bergd491af12009-02-10 21:25:58 +0100550void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
551{
552 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
553 struct cfg80211_internal_bss *bss;
554
555 if (WARN_ON(!pub))
556 return;
557
558 bss = container_of(pub, struct cfg80211_internal_bss, pub);
559
560 spin_lock_bh(&dev->bss_lock);
561
562 list_del(&bss->list);
563 rb_erase(&bss->rbn, &dev->bss_tree);
564
565 spin_unlock_bh(&dev->bss_lock);
566
567 kref_put(&bss->ref, bss_release);
568}
569EXPORT_SYMBOL(cfg80211_unlink_bss);
570
Johannes Berg2a519312009-02-10 21:25:55 +0100571#ifdef CONFIG_WIRELESS_EXT
572int cfg80211_wext_siwscan(struct net_device *dev,
573 struct iw_request_info *info,
574 union iwreq_data *wrqu, char *extra)
575{
576 struct cfg80211_registered_device *rdev;
577 struct wiphy *wiphy;
578 struct iw_scan_req *wreq = NULL;
579 struct cfg80211_scan_request *creq;
580 int i, err, n_channels = 0;
581 enum ieee80211_band band;
582
583 if (!netif_running(dev))
584 return -ENETDOWN;
585
Johannes Berg463d0182009-07-14 00:33:35 +0200586 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100587
588 if (IS_ERR(rdev))
589 return PTR_ERR(rdev);
590
591 if (rdev->scan_req) {
592 err = -EBUSY;
593 goto out;
594 }
595
596 wiphy = &rdev->wiphy;
597
598 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
599 if (wiphy->bands[band])
600 n_channels += wiphy->bands[band]->n_channels;
601
602 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
603 n_channels * sizeof(void *),
604 GFP_ATOMIC);
605 if (!creq) {
606 err = -ENOMEM;
607 goto out;
608 }
609
610 creq->wiphy = wiphy;
Johannes Berg463d0182009-07-14 00:33:35 +0200611 creq->dev = dev;
Johannes Berg2a519312009-02-10 21:25:55 +0100612 creq->ssids = (void *)(creq + 1);
613 creq->channels = (void *)(creq->ssids + 1);
614 creq->n_channels = n_channels;
615 creq->n_ssids = 1;
616
617 /* all channels */
618 i = 0;
619 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
620 int j;
621 if (!wiphy->bands[band])
622 continue;
623 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
624 creq->channels[i] = &wiphy->bands[band]->channels[j];
625 i++;
626 }
627 }
628
629 /* translate scan request */
630 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
631 wreq = (struct iw_scan_req *)extra;
632
633 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
634 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
635 return -EINVAL;
636 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
637 creq->ssids[0].ssid_len = wreq->essid_len;
638 }
639 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
640 creq->n_ssids = 0;
641 }
642
643 rdev->scan_req = creq;
644 err = rdev->ops->scan(wiphy, dev, creq);
645 if (err) {
646 rdev->scan_req = NULL;
647 kfree(creq);
Johannes Berg463d0182009-07-14 00:33:35 +0200648 } else {
Johannes Berga538e2d2009-06-16 19:56:42 +0200649 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +0200650 dev_hold(dev);
651 }
Johannes Berg2a519312009-02-10 21:25:55 +0100652 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200653 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100654 return err;
655}
Johannes Bergba44cb72009-04-20 18:49:39 +0200656EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100657
658static void ieee80211_scan_add_ies(struct iw_request_info *info,
659 struct cfg80211_bss *bss,
660 char **current_ev, char *end_buf)
661{
662 u8 *pos, *end, *next;
663 struct iw_event iwe;
664
665 if (!bss->information_elements ||
666 !bss->len_information_elements)
667 return;
668
669 /*
670 * If needed, fragment the IEs buffer (at IE boundaries) into short
671 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
672 */
673 pos = bss->information_elements;
674 end = pos + bss->len_information_elements;
675
676 while (end - pos > IW_GENERIC_IE_MAX) {
677 next = pos + 2 + pos[1];
678 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
679 next = next + 2 + next[1];
680
681 memset(&iwe, 0, sizeof(iwe));
682 iwe.cmd = IWEVGENIE;
683 iwe.u.data.length = next - pos;
684 *current_ev = iwe_stream_add_point(info, *current_ev,
685 end_buf, &iwe, pos);
686
687 pos = next;
688 }
689
690 if (end > pos) {
691 memset(&iwe, 0, sizeof(iwe));
692 iwe.cmd = IWEVGENIE;
693 iwe.u.data.length = end - pos;
694 *current_ev = iwe_stream_add_point(info, *current_ev,
695 end_buf, &iwe, pos);
696 }
697}
698
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500699static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
700{
701 unsigned long end = jiffies;
702
703 if (end >= start)
704 return jiffies_to_msecs(end - start);
705
706 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
707}
Johannes Berg2a519312009-02-10 21:25:55 +0100708
709static char *
Johannes Berg77965c92009-02-18 18:45:06 +0100710ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
711 struct cfg80211_internal_bss *bss, char *current_ev,
712 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +0100713{
714 struct iw_event iwe;
715 u8 *buf, *cfg, *p;
716 u8 *ie = bss->pub.information_elements;
Johannes Berga77b8552009-02-18 18:27:22 +0100717 int rem = bss->pub.len_information_elements, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100718 bool ismesh = false;
719
720 memset(&iwe, 0, sizeof(iwe));
721 iwe.cmd = SIOCGIWAP;
722 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
723 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
724 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
725 IW_EV_ADDR_LEN);
726
727 memset(&iwe, 0, sizeof(iwe));
728 iwe.cmd = SIOCGIWFREQ;
729 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
730 iwe.u.freq.e = 0;
731 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
732 IW_EV_FREQ_LEN);
733
734 memset(&iwe, 0, sizeof(iwe));
735 iwe.cmd = SIOCGIWFREQ;
736 iwe.u.freq.m = bss->pub.channel->center_freq;
737 iwe.u.freq.e = 6;
738 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
739 IW_EV_FREQ_LEN);
740
Johannes Berg77965c92009-02-18 18:45:06 +0100741 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +0100742 memset(&iwe, 0, sizeof(iwe));
743 iwe.cmd = IWEVQUAL;
744 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
745 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +0100746 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +0100747 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +0100748 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +0100749 sig = bss->pub.signal / 100;
750 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100751 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +0100752 if (sig < -110) /* rather bad */
753 sig = -110;
754 else if (sig > -40) /* perfect */
755 sig = -40;
756 /* will give a range of 0 .. 70 */
757 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +0100758 break;
759 case CFG80211_SIGNAL_TYPE_UNSPEC:
760 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +0100761 /* will give range 0 .. 100 */
762 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100763 break;
764 default:
765 /* not reached */
766 break;
767 }
768 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
769 &iwe, IW_EV_QUAL_LEN);
770 }
771
772 memset(&iwe, 0, sizeof(iwe));
773 iwe.cmd = SIOCGIWENCODE;
774 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
775 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
776 else
777 iwe.u.data.flags = IW_ENCODE_DISABLED;
778 iwe.u.data.length = 0;
779 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
780 &iwe, "");
781
782 while (rem >= 2) {
783 /* invalid data */
784 if (ie[1] > rem - 2)
785 break;
786
787 switch (ie[0]) {
788 case WLAN_EID_SSID:
789 memset(&iwe, 0, sizeof(iwe));
790 iwe.cmd = SIOCGIWESSID;
791 iwe.u.data.length = ie[1];
792 iwe.u.data.flags = 1;
793 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
794 &iwe, ie + 2);
795 break;
796 case WLAN_EID_MESH_ID:
797 memset(&iwe, 0, sizeof(iwe));
798 iwe.cmd = SIOCGIWESSID;
799 iwe.u.data.length = ie[1];
800 iwe.u.data.flags = 1;
801 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
802 &iwe, ie + 2);
803 break;
804 case WLAN_EID_MESH_CONFIG:
805 ismesh = true;
806 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
807 break;
808 buf = kmalloc(50, GFP_ATOMIC);
809 if (!buf)
810 break;
811 cfg = ie + 2;
812 memset(&iwe, 0, sizeof(iwe));
813 iwe.cmd = IWEVCUSTOM;
814 sprintf(buf, "Mesh network (version %d)", cfg[0]);
815 iwe.u.data.length = strlen(buf);
816 current_ev = iwe_stream_add_point(info, current_ev,
817 end_buf,
818 &iwe, buf);
819 sprintf(buf, "Path Selection Protocol ID: "
820 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
821 cfg[4]);
822 iwe.u.data.length = strlen(buf);
823 current_ev = iwe_stream_add_point(info, current_ev,
824 end_buf,
825 &iwe, buf);
826 sprintf(buf, "Path Selection Metric ID: "
827 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
828 cfg[8]);
829 iwe.u.data.length = strlen(buf);
830 current_ev = iwe_stream_add_point(info, current_ev,
831 end_buf,
832 &iwe, buf);
833 sprintf(buf, "Congestion Control Mode ID: "
834 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
835 cfg[11], cfg[12]);
836 iwe.u.data.length = strlen(buf);
837 current_ev = iwe_stream_add_point(info, current_ev,
838 end_buf,
839 &iwe, buf);
840 sprintf(buf, "Channel Precedence: "
841 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
842 cfg[15], cfg[16]);
843 iwe.u.data.length = strlen(buf);
844 current_ev = iwe_stream_add_point(info, current_ev,
845 end_buf,
846 &iwe, buf);
847 kfree(buf);
848 break;
849 case WLAN_EID_SUPP_RATES:
850 case WLAN_EID_EXT_SUPP_RATES:
851 /* display all supported rates in readable format */
852 p = current_ev + iwe_stream_lcp_len(info);
853
854 memset(&iwe, 0, sizeof(iwe));
855 iwe.cmd = SIOCGIWRATE;
856 /* Those two flags are ignored... */
857 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
858
859 for (i = 0; i < ie[1]; i++) {
860 iwe.u.bitrate.value =
861 ((ie[i + 2] & 0x7f) * 500000);
862 p = iwe_stream_add_value(info, current_ev, p,
863 end_buf, &iwe, IW_EV_PARAM_LEN);
864 }
865 current_ev = p;
866 break;
867 }
868 rem -= ie[1] + 2;
869 ie += ie[1] + 2;
870 }
871
872 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
873 || ismesh) {
874 memset(&iwe, 0, sizeof(iwe));
875 iwe.cmd = SIOCGIWMODE;
876 if (ismesh)
877 iwe.u.mode = IW_MODE_MESH;
878 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
879 iwe.u.mode = IW_MODE_MASTER;
880 else
881 iwe.u.mode = IW_MODE_ADHOC;
882 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
883 &iwe, IW_EV_UINT_LEN);
884 }
885
886 buf = kmalloc(30, GFP_ATOMIC);
887 if (buf) {
888 memset(&iwe, 0, sizeof(iwe));
889 iwe.cmd = IWEVCUSTOM;
890 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
891 iwe.u.data.length = strlen(buf);
892 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
893 &iwe, buf);
894 memset(&iwe, 0, sizeof(iwe));
895 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500896 sprintf(buf, " Last beacon: %ums ago",
897 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +0100898 iwe.u.data.length = strlen(buf);
899 current_ev = iwe_stream_add_point(info, current_ev,
900 end_buf, &iwe, buf);
901 kfree(buf);
902 }
903
904 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
905
906 return current_ev;
907}
908
909
910static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
911 struct iw_request_info *info,
912 char *buf, size_t len)
913{
914 char *current_ev = buf;
915 char *end_buf = buf + len;
916 struct cfg80211_internal_bss *bss;
917
918 spin_lock_bh(&dev->bss_lock);
919 cfg80211_bss_expire(dev);
920
921 list_for_each_entry(bss, &dev->bss_list, list) {
922 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
923 spin_unlock_bh(&dev->bss_lock);
924 return -E2BIG;
925 }
Johannes Berg77965c92009-02-18 18:45:06 +0100926 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
927 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +0100928 }
929 spin_unlock_bh(&dev->bss_lock);
930 return current_ev - buf;
931}
932
933
934int cfg80211_wext_giwscan(struct net_device *dev,
935 struct iw_request_info *info,
936 struct iw_point *data, char *extra)
937{
938 struct cfg80211_registered_device *rdev;
939 int res;
940
941 if (!netif_running(dev))
942 return -ENETDOWN;
943
Johannes Berg463d0182009-07-14 00:33:35 +0200944 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100945
946 if (IS_ERR(rdev))
947 return PTR_ERR(rdev);
948
949 if (rdev->scan_req) {
950 res = -EAGAIN;
951 goto out;
952 }
953
954 res = ieee80211_scan_results(rdev, info, extra, data->length);
955 data->length = 0;
956 if (res >= 0) {
957 data->length = res;
958 res = 0;
959 }
960
961 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200962 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100963 return res;
964}
Johannes Bergba44cb72009-04-20 18:49:39 +0200965EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100966#endif