blob: 82b33e70848852a6ae897292f484669b98a3c253 [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"
17
18#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
19
20void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
21{
22 struct net_device *dev;
23#ifdef CONFIG_WIRELESS_EXT
24 union iwreq_data wrqu;
25#endif
26
27 dev = dev_get_by_index(&init_net, request->ifidx);
28 if (!dev)
29 goto out;
30
31 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
Johannes Berg2a519312009-02-10 21:25:55 +010032
Johannes Berg6829c872009-07-02 09:13:27 +020033 /*
34 * This must be before sending the other events!
35 * Otherwise, wpa_supplicant gets completely confused with
36 * wext events.
37 */
38 cfg80211_sme_scan_done(dev);
39
Johannes Berg2a519312009-02-10 21:25:55 +010040 if (aborted)
41 nl80211_send_scan_aborted(wiphy_to_dev(request->wiphy), dev);
42 else
43 nl80211_send_scan_done(wiphy_to_dev(request->wiphy), dev);
44
Johannes Berg362a4152009-05-24 16:43:15 +020045 wiphy_to_dev(request->wiphy)->scan_req = NULL;
46
Johannes Berg2a519312009-02-10 21:25:55 +010047#ifdef CONFIG_WIRELESS_EXT
48 if (!aborted) {
49 memset(&wrqu, 0, sizeof(wrqu));
50
51 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
52 }
53#endif
54
55 dev_put(dev);
56
57 out:
58 kfree(request);
59}
60EXPORT_SYMBOL(cfg80211_scan_done);
61
62static void bss_release(struct kref *ref)
63{
64 struct cfg80211_internal_bss *bss;
65
66 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Berg78c1c7e2009-02-10 21:25:57 +010067 if (bss->pub.free_priv)
68 bss->pub.free_priv(&bss->pub);
Johannes Bergcd1658f2009-04-16 15:00:58 +020069
70 if (bss->ies_allocated)
71 kfree(bss->pub.information_elements);
72
Johannes Berg2a519312009-02-10 21:25:55 +010073 kfree(bss);
74}
75
76/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -050077void cfg80211_bss_age(struct cfg80211_registered_device *dev,
78 unsigned long age_secs)
79{
80 struct cfg80211_internal_bss *bss;
81 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
82
83 list_for_each_entry(bss, &dev->bss_list, list) {
84 bss->ts -= age_jiffies;
85 }
86}
87
88/* must hold dev->bss_lock! */
Johannes Berg2a519312009-02-10 21:25:55 +010089void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
90{
91 struct cfg80211_internal_bss *bss, *tmp;
92 bool expired = false;
93
94 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
Kalle Valoa08c1c12009-03-22 21:57:28 +020095 if (bss->hold ||
96 !time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
Johannes Berg2a519312009-02-10 21:25:55 +010097 continue;
98 list_del(&bss->list);
99 rb_erase(&bss->rbn, &dev->bss_tree);
100 kref_put(&bss->ref, bss_release);
101 expired = true;
102 }
103
104 if (expired)
105 dev->bss_generation++;
106}
107
108static u8 *find_ie(u8 num, u8 *ies, size_t len)
109{
110 while (len > 2 && ies[0] != num) {
111 len -= ies[1] + 2;
112 ies += ies[1] + 2;
113 }
114 if (len < 2)
115 return NULL;
116 if (len < 2 + ies[1])
117 return NULL;
118 return ies;
119}
120
121static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
122{
123 const u8 *ie1 = find_ie(num, ies1, len1);
124 const u8 *ie2 = find_ie(num, ies2, len2);
125 int r;
126
127 if (!ie1 && !ie2)
128 return 0;
129 if (!ie1)
130 return -1;
131
132 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
133 if (r == 0 && ie1[1] != ie2[1])
134 return ie2[1] - ie1[1];
135 return r;
136}
137
138static bool is_bss(struct cfg80211_bss *a,
139 const u8 *bssid,
140 const u8 *ssid, size_t ssid_len)
141{
142 const u8 *ssidie;
143
Johannes Berg79420f02009-02-10 21:25:59 +0100144 if (bssid && compare_ether_addr(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100145 return false;
146
Johannes Berg79420f02009-02-10 21:25:59 +0100147 if (!ssid)
148 return true;
149
Johannes Berg2a519312009-02-10 21:25:55 +0100150 ssidie = find_ie(WLAN_EID_SSID,
151 a->information_elements,
152 a->len_information_elements);
153 if (!ssidie)
154 return false;
155 if (ssidie[1] != ssid_len)
156 return false;
157 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
158}
159
160static bool is_mesh(struct cfg80211_bss *a,
161 const u8 *meshid, size_t meshidlen,
162 const u8 *meshcfg)
163{
164 const u8 *ie;
165
166 if (!is_zero_ether_addr(a->bssid))
167 return false;
168
169 ie = find_ie(WLAN_EID_MESH_ID,
170 a->information_elements,
171 a->len_information_elements);
172 if (!ie)
173 return false;
174 if (ie[1] != meshidlen)
175 return false;
176 if (memcmp(ie + 2, meshid, meshidlen))
177 return false;
178
179 ie = find_ie(WLAN_EID_MESH_CONFIG,
180 a->information_elements,
181 a->len_information_elements);
182 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
183 return false;
184
185 /*
186 * Ignore mesh capability (last two bytes of the IE) when
187 * comparing since that may differ between stations taking
188 * part in the same mesh.
189 */
190 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
191}
192
193static int cmp_bss(struct cfg80211_bss *a,
194 struct cfg80211_bss *b)
195{
196 int r;
197
198 if (a->channel != b->channel)
199 return b->channel->center_freq - a->channel->center_freq;
200
201 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
202 if (r)
203 return r;
204
205 if (is_zero_ether_addr(a->bssid)) {
206 r = cmp_ies(WLAN_EID_MESH_ID,
207 a->information_elements,
208 a->len_information_elements,
209 b->information_elements,
210 b->len_information_elements);
211 if (r)
212 return r;
213 return cmp_ies(WLAN_EID_MESH_CONFIG,
214 a->information_elements,
215 a->len_information_elements,
216 b->information_elements,
217 b->len_information_elements);
218 }
219
220 return cmp_ies(WLAN_EID_SSID,
221 a->information_elements,
222 a->len_information_elements,
223 b->information_elements,
224 b->len_information_elements);
225}
226
227struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
228 struct ieee80211_channel *channel,
229 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100230 const u8 *ssid, size_t ssid_len,
231 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100232{
233 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
234 struct cfg80211_internal_bss *bss, *res = NULL;
235
236 spin_lock_bh(&dev->bss_lock);
237
238 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100239 if ((bss->pub.capability & capa_mask) != capa_val)
240 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100241 if (channel && bss->pub.channel != channel)
242 continue;
243 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
244 res = bss;
245 kref_get(&res->ref);
246 break;
247 }
248 }
249
250 spin_unlock_bh(&dev->bss_lock);
251 if (!res)
252 return NULL;
253 return &res->pub;
254}
255EXPORT_SYMBOL(cfg80211_get_bss);
256
257struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
258 struct ieee80211_channel *channel,
259 const u8 *meshid, size_t meshidlen,
260 const u8 *meshcfg)
261{
262 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
263 struct cfg80211_internal_bss *bss, *res = NULL;
264
265 spin_lock_bh(&dev->bss_lock);
266
267 list_for_each_entry(bss, &dev->bss_list, list) {
268 if (channel && bss->pub.channel != channel)
269 continue;
270 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
271 res = bss;
272 kref_get(&res->ref);
273 break;
274 }
275 }
276
277 spin_unlock_bh(&dev->bss_lock);
278 if (!res)
279 return NULL;
280 return &res->pub;
281}
282EXPORT_SYMBOL(cfg80211_get_mesh);
283
284
285static void rb_insert_bss(struct cfg80211_registered_device *dev,
286 struct cfg80211_internal_bss *bss)
287{
288 struct rb_node **p = &dev->bss_tree.rb_node;
289 struct rb_node *parent = NULL;
290 struct cfg80211_internal_bss *tbss;
291 int cmp;
292
293 while (*p) {
294 parent = *p;
295 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
296
297 cmp = cmp_bss(&bss->pub, &tbss->pub);
298
299 if (WARN_ON(!cmp)) {
300 /* will sort of leak this BSS */
301 return;
302 }
303
304 if (cmp < 0)
305 p = &(*p)->rb_left;
306 else
307 p = &(*p)->rb_right;
308 }
309
310 rb_link_node(&bss->rbn, parent, p);
311 rb_insert_color(&bss->rbn, &dev->bss_tree);
312}
313
314static struct cfg80211_internal_bss *
315rb_find_bss(struct cfg80211_registered_device *dev,
316 struct cfg80211_internal_bss *res)
317{
318 struct rb_node *n = dev->bss_tree.rb_node;
319 struct cfg80211_internal_bss *bss;
320 int r;
321
322 while (n) {
323 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
324 r = cmp_bss(&res->pub, &bss->pub);
325
326 if (r == 0)
327 return bss;
328 else if (r < 0)
329 n = n->rb_left;
330 else
331 n = n->rb_right;
332 }
333
334 return NULL;
335}
336
337static struct cfg80211_internal_bss *
338cfg80211_bss_update(struct cfg80211_registered_device *dev,
339 struct cfg80211_internal_bss *res,
340 bool overwrite)
341{
342 struct cfg80211_internal_bss *found = NULL;
343 const u8 *meshid, *meshcfg;
344
345 /*
346 * The reference to "res" is donated to this function.
347 */
348
349 if (WARN_ON(!res->pub.channel)) {
350 kref_put(&res->ref, bss_release);
351 return NULL;
352 }
353
354 res->ts = jiffies;
355
356 if (is_zero_ether_addr(res->pub.bssid)) {
357 /* must be mesh, verify */
358 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
359 res->pub.len_information_elements);
360 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
361 res->pub.information_elements,
362 res->pub.len_information_elements);
363 if (!meshid || !meshcfg ||
364 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
365 /* bogus mesh */
366 kref_put(&res->ref, bss_release);
367 return NULL;
368 }
369 }
370
371 spin_lock_bh(&dev->bss_lock);
372
373 found = rb_find_bss(dev, res);
374
Johannes Bergcd1658f2009-04-16 15:00:58 +0200375 if (found) {
Johannes Berg2a519312009-02-10 21:25:55 +0100376 found->pub.beacon_interval = res->pub.beacon_interval;
377 found->pub.tsf = res->pub.tsf;
378 found->pub.signal = res->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100379 found->pub.capability = res->pub.capability;
380 found->ts = res->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200381
382 /* overwrite IEs */
383 if (overwrite) {
384 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
385 size_t ielen = res->pub.len_information_elements;
386
Michael Buesch44e1b982009-04-26 11:27:33 +0200387 if (!found->ies_allocated && ksize(found) >= used + ielen) {
Johannes Bergcd1658f2009-04-16 15:00:58 +0200388 memcpy(found->pub.information_elements,
389 res->pub.information_elements, ielen);
390 found->pub.len_information_elements = ielen;
391 } else {
392 u8 *ies = found->pub.information_elements;
393
Michael Buesch273de922009-04-25 22:28:55 +0200394 if (found->ies_allocated)
395 ies = krealloc(ies, ielen, GFP_ATOMIC);
396 else
Johannes Bergcd1658f2009-04-16 15:00:58 +0200397 ies = kmalloc(ielen, GFP_ATOMIC);
398
399 if (ies) {
400 memcpy(ies, res->pub.information_elements, ielen);
401 found->ies_allocated = true;
402 found->pub.information_elements = ies;
Johannes Bergc0f0aac2009-04-30 20:09:56 +0200403 found->pub.len_information_elements = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200404 }
405 }
406 }
407
Johannes Berg2a519312009-02-10 21:25:55 +0100408 kref_put(&res->ref, bss_release);
409 } else {
410 /* this "consumes" the reference */
411 list_add_tail(&res->list, &dev->bss_list);
412 rb_insert_bss(dev, res);
413 found = res;
414 }
415
416 dev->bss_generation++;
417 spin_unlock_bh(&dev->bss_lock);
418
419 kref_get(&found->ref);
420 return found;
421}
422
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200423struct cfg80211_bss*
424cfg80211_inform_bss(struct wiphy *wiphy,
425 struct ieee80211_channel *channel,
426 const u8 *bssid,
427 u64 timestamp, u16 capability, u16 beacon_interval,
428 const u8 *ie, size_t ielen,
429 s32 signal, gfp_t gfp)
430{
431 struct cfg80211_internal_bss *res;
432 size_t privsz;
433
434 if (WARN_ON(!wiphy))
435 return NULL;
436
437 privsz = wiphy->bss_priv_size;
438
439 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
440 (signal < 0 || signal > 100)))
441 return NULL;
442
443 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
444 if (!res)
445 return NULL;
446
447 memcpy(res->pub.bssid, bssid, ETH_ALEN);
448 res->pub.channel = channel;
449 res->pub.signal = signal;
450 res->pub.tsf = timestamp;
451 res->pub.beacon_interval = beacon_interval;
452 res->pub.capability = capability;
453 /* point to after the private area */
454 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
455 memcpy(res->pub.information_elements, ie, ielen);
456 res->pub.len_information_elements = ielen;
457
458 kref_init(&res->ref);
459
460 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
461 if (!res)
462 return NULL;
463
464 if (res->pub.capability & WLAN_CAPABILITY_ESS)
465 regulatory_hint_found_beacon(wiphy, channel, gfp);
466
467 /* cfg80211_bss_update gives us a referenced result */
468 return &res->pub;
469}
470EXPORT_SYMBOL(cfg80211_inform_bss);
471
Johannes Berg2a519312009-02-10 21:25:55 +0100472struct cfg80211_bss *
473cfg80211_inform_bss_frame(struct wiphy *wiphy,
474 struct ieee80211_channel *channel,
475 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100476 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100477{
478 struct cfg80211_internal_bss *res;
479 size_t ielen = len - offsetof(struct ieee80211_mgmt,
480 u.probe_resp.variable);
481 bool overwrite;
482 size_t privsz = wiphy->bss_priv_size;
483
Johannes Berg77965c92009-02-18 18:45:06 +0100484 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
Johannes Berg2a519312009-02-10 21:25:55 +0100485 (signal < 0 || signal > 100)))
486 return NULL;
487
488 if (WARN_ON(!mgmt || !wiphy ||
489 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
490 return NULL;
491
492 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
493 if (!res)
494 return NULL;
495
496 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
497 res->pub.channel = channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100498 res->pub.signal = signal;
499 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
500 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
501 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
502 /* point to after the private area */
503 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
504 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
505 res->pub.len_information_elements = ielen;
506
507 kref_init(&res->ref);
508
509 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
510
511 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
512 if (!res)
513 return NULL;
514
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500515 if (res->pub.capability & WLAN_CAPABILITY_ESS)
516 regulatory_hint_found_beacon(wiphy, channel, gfp);
517
Johannes Berg2a519312009-02-10 21:25:55 +0100518 /* cfg80211_bss_update gives us a referenced result */
519 return &res->pub;
520}
521EXPORT_SYMBOL(cfg80211_inform_bss_frame);
522
523void cfg80211_put_bss(struct cfg80211_bss *pub)
524{
525 struct cfg80211_internal_bss *bss;
526
527 if (!pub)
528 return;
529
530 bss = container_of(pub, struct cfg80211_internal_bss, pub);
531 kref_put(&bss->ref, bss_release);
532}
533EXPORT_SYMBOL(cfg80211_put_bss);
534
Johannes Bergd491af12009-02-10 21:25:58 +0100535void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
536{
537 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
538 struct cfg80211_internal_bss *bss;
539
540 if (WARN_ON(!pub))
541 return;
542
543 bss = container_of(pub, struct cfg80211_internal_bss, pub);
544
545 spin_lock_bh(&dev->bss_lock);
546
547 list_del(&bss->list);
548 rb_erase(&bss->rbn, &dev->bss_tree);
549
550 spin_unlock_bh(&dev->bss_lock);
551
552 kref_put(&bss->ref, bss_release);
553}
554EXPORT_SYMBOL(cfg80211_unlink_bss);
555
Kalle Valoa08c1c12009-03-22 21:57:28 +0200556void cfg80211_hold_bss(struct cfg80211_bss *pub)
557{
558 struct cfg80211_internal_bss *bss;
559
560 if (!pub)
561 return;
562
563 bss = container_of(pub, struct cfg80211_internal_bss, pub);
564 bss->hold = true;
565}
566EXPORT_SYMBOL(cfg80211_hold_bss);
567
568void cfg80211_unhold_bss(struct cfg80211_bss *pub)
569{
570 struct cfg80211_internal_bss *bss;
571
572 if (!pub)
573 return;
574
575 bss = container_of(pub, struct cfg80211_internal_bss, pub);
576 bss->hold = false;
577}
578EXPORT_SYMBOL(cfg80211_unhold_bss);
579
Johannes Berg2a519312009-02-10 21:25:55 +0100580#ifdef CONFIG_WIRELESS_EXT
581int cfg80211_wext_siwscan(struct net_device *dev,
582 struct iw_request_info *info,
583 union iwreq_data *wrqu, char *extra)
584{
585 struct cfg80211_registered_device *rdev;
586 struct wiphy *wiphy;
587 struct iw_scan_req *wreq = NULL;
588 struct cfg80211_scan_request *creq;
589 int i, err, n_channels = 0;
590 enum ieee80211_band band;
591
592 if (!netif_running(dev))
593 return -ENETDOWN;
594
595 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
596
597 if (IS_ERR(rdev))
598 return PTR_ERR(rdev);
599
600 if (rdev->scan_req) {
601 err = -EBUSY;
602 goto out;
603 }
604
605 wiphy = &rdev->wiphy;
606
607 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
608 if (wiphy->bands[band])
609 n_channels += wiphy->bands[band]->n_channels;
610
611 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
612 n_channels * sizeof(void *),
613 GFP_ATOMIC);
614 if (!creq) {
615 err = -ENOMEM;
616 goto out;
617 }
618
619 creq->wiphy = wiphy;
620 creq->ifidx = dev->ifindex;
621 creq->ssids = (void *)(creq + 1);
622 creq->channels = (void *)(creq->ssids + 1);
623 creq->n_channels = n_channels;
624 creq->n_ssids = 1;
625
626 /* all channels */
627 i = 0;
628 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
629 int j;
630 if (!wiphy->bands[band])
631 continue;
632 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
633 creq->channels[i] = &wiphy->bands[band]->channels[j];
634 i++;
635 }
636 }
637
638 /* translate scan request */
639 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
640 wreq = (struct iw_scan_req *)extra;
641
642 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
643 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
644 return -EINVAL;
645 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
646 creq->ssids[0].ssid_len = wreq->essid_len;
647 }
648 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
649 creq->n_ssids = 0;
650 }
651
652 rdev->scan_req = creq;
653 err = rdev->ops->scan(wiphy, dev, creq);
654 if (err) {
655 rdev->scan_req = NULL;
656 kfree(creq);
Johannes Berga538e2d2009-06-16 19:56:42 +0200657 } else
658 nl80211_send_scan_start(rdev, dev);
Johannes Berg2a519312009-02-10 21:25:55 +0100659 out:
660 cfg80211_put_dev(rdev);
661 return err;
662}
Johannes Bergba44cb72009-04-20 18:49:39 +0200663EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100664
665static void ieee80211_scan_add_ies(struct iw_request_info *info,
666 struct cfg80211_bss *bss,
667 char **current_ev, char *end_buf)
668{
669 u8 *pos, *end, *next;
670 struct iw_event iwe;
671
672 if (!bss->information_elements ||
673 !bss->len_information_elements)
674 return;
675
676 /*
677 * If needed, fragment the IEs buffer (at IE boundaries) into short
678 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
679 */
680 pos = bss->information_elements;
681 end = pos + bss->len_information_elements;
682
683 while (end - pos > IW_GENERIC_IE_MAX) {
684 next = pos + 2 + pos[1];
685 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
686 next = next + 2 + next[1];
687
688 memset(&iwe, 0, sizeof(iwe));
689 iwe.cmd = IWEVGENIE;
690 iwe.u.data.length = next - pos;
691 *current_ev = iwe_stream_add_point(info, *current_ev,
692 end_buf, &iwe, pos);
693
694 pos = next;
695 }
696
697 if (end > pos) {
698 memset(&iwe, 0, sizeof(iwe));
699 iwe.cmd = IWEVGENIE;
700 iwe.u.data.length = end - pos;
701 *current_ev = iwe_stream_add_point(info, *current_ev,
702 end_buf, &iwe, pos);
703 }
704}
705
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500706static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
707{
708 unsigned long end = jiffies;
709
710 if (end >= start)
711 return jiffies_to_msecs(end - start);
712
713 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
714}
Johannes Berg2a519312009-02-10 21:25:55 +0100715
716static char *
Johannes Berg77965c92009-02-18 18:45:06 +0100717ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
718 struct cfg80211_internal_bss *bss, char *current_ev,
719 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +0100720{
721 struct iw_event iwe;
722 u8 *buf, *cfg, *p;
723 u8 *ie = bss->pub.information_elements;
Johannes Berga77b8552009-02-18 18:27:22 +0100724 int rem = bss->pub.len_information_elements, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100725 bool ismesh = false;
726
727 memset(&iwe, 0, sizeof(iwe));
728 iwe.cmd = SIOCGIWAP;
729 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
730 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
731 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
732 IW_EV_ADDR_LEN);
733
734 memset(&iwe, 0, sizeof(iwe));
735 iwe.cmd = SIOCGIWFREQ;
736 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
737 iwe.u.freq.e = 0;
738 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
739 IW_EV_FREQ_LEN);
740
741 memset(&iwe, 0, sizeof(iwe));
742 iwe.cmd = SIOCGIWFREQ;
743 iwe.u.freq.m = bss->pub.channel->center_freq;
744 iwe.u.freq.e = 6;
745 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
746 IW_EV_FREQ_LEN);
747
Johannes Berg77965c92009-02-18 18:45:06 +0100748 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +0100749 memset(&iwe, 0, sizeof(iwe));
750 iwe.cmd = IWEVQUAL;
751 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
752 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +0100753 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +0100754 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +0100755 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +0100756 sig = bss->pub.signal / 100;
757 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100758 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +0100759 if (sig < -110) /* rather bad */
760 sig = -110;
761 else if (sig > -40) /* perfect */
762 sig = -40;
763 /* will give a range of 0 .. 70 */
764 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +0100765 break;
766 case CFG80211_SIGNAL_TYPE_UNSPEC:
767 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +0100768 /* will give range 0 .. 100 */
769 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100770 break;
771 default:
772 /* not reached */
773 break;
774 }
775 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
776 &iwe, IW_EV_QUAL_LEN);
777 }
778
779 memset(&iwe, 0, sizeof(iwe));
780 iwe.cmd = SIOCGIWENCODE;
781 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
782 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
783 else
784 iwe.u.data.flags = IW_ENCODE_DISABLED;
785 iwe.u.data.length = 0;
786 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
787 &iwe, "");
788
789 while (rem >= 2) {
790 /* invalid data */
791 if (ie[1] > rem - 2)
792 break;
793
794 switch (ie[0]) {
795 case WLAN_EID_SSID:
796 memset(&iwe, 0, sizeof(iwe));
797 iwe.cmd = SIOCGIWESSID;
798 iwe.u.data.length = ie[1];
799 iwe.u.data.flags = 1;
800 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
801 &iwe, ie + 2);
802 break;
803 case WLAN_EID_MESH_ID:
804 memset(&iwe, 0, sizeof(iwe));
805 iwe.cmd = SIOCGIWESSID;
806 iwe.u.data.length = ie[1];
807 iwe.u.data.flags = 1;
808 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
809 &iwe, ie + 2);
810 break;
811 case WLAN_EID_MESH_CONFIG:
812 ismesh = true;
813 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
814 break;
815 buf = kmalloc(50, GFP_ATOMIC);
816 if (!buf)
817 break;
818 cfg = ie + 2;
819 memset(&iwe, 0, sizeof(iwe));
820 iwe.cmd = IWEVCUSTOM;
821 sprintf(buf, "Mesh network (version %d)", cfg[0]);
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 Protocol ID: "
827 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
828 cfg[4]);
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, "Path Selection Metric ID: "
834 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
835 cfg[8]);
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, "Congestion Control Mode ID: "
841 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
842 cfg[11], cfg[12]);
843 iwe.u.data.length = strlen(buf);
844 current_ev = iwe_stream_add_point(info, current_ev,
845 end_buf,
846 &iwe, buf);
847 sprintf(buf, "Channel Precedence: "
848 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
849 cfg[15], cfg[16]);
850 iwe.u.data.length = strlen(buf);
851 current_ev = iwe_stream_add_point(info, current_ev,
852 end_buf,
853 &iwe, buf);
854 kfree(buf);
855 break;
856 case WLAN_EID_SUPP_RATES:
857 case WLAN_EID_EXT_SUPP_RATES:
858 /* display all supported rates in readable format */
859 p = current_ev + iwe_stream_lcp_len(info);
860
861 memset(&iwe, 0, sizeof(iwe));
862 iwe.cmd = SIOCGIWRATE;
863 /* Those two flags are ignored... */
864 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
865
866 for (i = 0; i < ie[1]; i++) {
867 iwe.u.bitrate.value =
868 ((ie[i + 2] & 0x7f) * 500000);
869 p = iwe_stream_add_value(info, current_ev, p,
870 end_buf, &iwe, IW_EV_PARAM_LEN);
871 }
872 current_ev = p;
873 break;
874 }
875 rem -= ie[1] + 2;
876 ie += ie[1] + 2;
877 }
878
879 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
880 || ismesh) {
881 memset(&iwe, 0, sizeof(iwe));
882 iwe.cmd = SIOCGIWMODE;
883 if (ismesh)
884 iwe.u.mode = IW_MODE_MESH;
885 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
886 iwe.u.mode = IW_MODE_MASTER;
887 else
888 iwe.u.mode = IW_MODE_ADHOC;
889 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
890 &iwe, IW_EV_UINT_LEN);
891 }
892
893 buf = kmalloc(30, GFP_ATOMIC);
894 if (buf) {
895 memset(&iwe, 0, sizeof(iwe));
896 iwe.cmd = IWEVCUSTOM;
897 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
898 iwe.u.data.length = strlen(buf);
899 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
900 &iwe, buf);
901 memset(&iwe, 0, sizeof(iwe));
902 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500903 sprintf(buf, " Last beacon: %ums ago",
904 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +0100905 iwe.u.data.length = strlen(buf);
906 current_ev = iwe_stream_add_point(info, current_ev,
907 end_buf, &iwe, buf);
908 kfree(buf);
909 }
910
911 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
912
913 return current_ev;
914}
915
916
917static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
918 struct iw_request_info *info,
919 char *buf, size_t len)
920{
921 char *current_ev = buf;
922 char *end_buf = buf + len;
923 struct cfg80211_internal_bss *bss;
924
925 spin_lock_bh(&dev->bss_lock);
926 cfg80211_bss_expire(dev);
927
928 list_for_each_entry(bss, &dev->bss_list, list) {
929 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
930 spin_unlock_bh(&dev->bss_lock);
931 return -E2BIG;
932 }
Johannes Berg77965c92009-02-18 18:45:06 +0100933 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
934 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +0100935 }
936 spin_unlock_bh(&dev->bss_lock);
937 return current_ev - buf;
938}
939
940
941int cfg80211_wext_giwscan(struct net_device *dev,
942 struct iw_request_info *info,
943 struct iw_point *data, char *extra)
944{
945 struct cfg80211_registered_device *rdev;
946 int res;
947
948 if (!netif_running(dev))
949 return -ENETDOWN;
950
951 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
952
953 if (IS_ERR(rdev))
954 return PTR_ERR(rdev);
955
956 if (rdev->scan_req) {
957 res = -EAGAIN;
958 goto out;
959 }
960
961 res = ieee80211_scan_results(rdev, info, extra, data->length);
962 data->length = 0;
963 if (res >= 0) {
964 data->length = res;
965 res = 0;
966 }
967
968 out:
969 cfg80211_put_dev(rdev);
970 return res;
971}
Johannes Bergba44cb72009-04-20 18:49:39 +0200972EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100973#endif