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