Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / net / wireless / scan.c
1 /*
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
20 void 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 }
52 EXPORT_SYMBOL(cfg80211_scan_done);
53
54 static void bss_release(struct kref *ref)
55 {
56         struct cfg80211_internal_bss *bss;
57
58         bss = container_of(ref, struct cfg80211_internal_bss, ref);
59         if (bss->pub.free_priv)
60                 bss->pub.free_priv(&bss->pub);
61         kfree(bss);
62 }
63
64 /* must hold dev->bss_lock! */
65 void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
66 {
67         struct cfg80211_internal_bss *bss, *tmp;
68         bool expired = false;
69
70         list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
71                 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
72                         continue;
73                 list_del(&bss->list);
74                 rb_erase(&bss->rbn, &dev->bss_tree);
75                 kref_put(&bss->ref, bss_release);
76                 expired = true;
77         }
78
79         if (expired)
80                 dev->bss_generation++;
81 }
82
83 static u8 *find_ie(u8 num, u8 *ies, size_t len)
84 {
85         while (len > 2 && ies[0] != num) {
86                 len -= ies[1] + 2;
87                 ies += ies[1] + 2;
88         }
89         if (len < 2)
90                 return NULL;
91         if (len < 2 + ies[1])
92                 return NULL;
93         return ies;
94 }
95
96 static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
97 {
98         const u8 *ie1 = find_ie(num, ies1, len1);
99         const u8 *ie2 = find_ie(num, ies2, len2);
100         int r;
101
102         if (!ie1 && !ie2)
103                 return 0;
104         if (!ie1)
105                 return -1;
106
107         r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
108         if (r == 0 && ie1[1] != ie2[1])
109                 return ie2[1] - ie1[1];
110         return r;
111 }
112
113 static bool is_bss(struct cfg80211_bss *a,
114                    const u8 *bssid,
115                    const u8 *ssid, size_t ssid_len)
116 {
117         const u8 *ssidie;
118
119         if (bssid && compare_ether_addr(a->bssid, bssid))
120                 return false;
121
122         if (!ssid)
123                 return true;
124
125         ssidie = find_ie(WLAN_EID_SSID,
126                          a->information_elements,
127                          a->len_information_elements);
128         if (!ssidie)
129                 return false;
130         if (ssidie[1] != ssid_len)
131                 return false;
132         return memcmp(ssidie + 2, ssid, ssid_len) == 0;
133 }
134
135 static bool is_mesh(struct cfg80211_bss *a,
136                     const u8 *meshid, size_t meshidlen,
137                     const u8 *meshcfg)
138 {
139         const u8 *ie;
140
141         if (!is_zero_ether_addr(a->bssid))
142                 return false;
143
144         ie = find_ie(WLAN_EID_MESH_ID,
145                      a->information_elements,
146                      a->len_information_elements);
147         if (!ie)
148                 return false;
149         if (ie[1] != meshidlen)
150                 return false;
151         if (memcmp(ie + 2, meshid, meshidlen))
152                 return false;
153
154         ie = find_ie(WLAN_EID_MESH_CONFIG,
155                      a->information_elements,
156                      a->len_information_elements);
157         if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
158                 return false;
159
160         /*
161          * Ignore mesh capability (last two bytes of the IE) when
162          * comparing since that may differ between stations taking
163          * part in the same mesh.
164          */
165         return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
166 }
167
168 static int cmp_bss(struct cfg80211_bss *a,
169                    struct cfg80211_bss *b)
170 {
171         int r;
172
173         if (a->channel != b->channel)
174                 return b->channel->center_freq - a->channel->center_freq;
175
176         r = memcmp(a->bssid, b->bssid, ETH_ALEN);
177         if (r)
178                 return r;
179
180         if (is_zero_ether_addr(a->bssid)) {
181                 r = cmp_ies(WLAN_EID_MESH_ID,
182                             a->information_elements,
183                             a->len_information_elements,
184                             b->information_elements,
185                             b->len_information_elements);
186                 if (r)
187                         return r;
188                 return cmp_ies(WLAN_EID_MESH_CONFIG,
189                                a->information_elements,
190                                a->len_information_elements,
191                                b->information_elements,
192                                b->len_information_elements);
193         }
194
195         return cmp_ies(WLAN_EID_SSID,
196                        a->information_elements,
197                        a->len_information_elements,
198                        b->information_elements,
199                        b->len_information_elements);
200 }
201
202 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
203                                       struct ieee80211_channel *channel,
204                                       const u8 *bssid,
205                                       const u8 *ssid, size_t ssid_len,
206                                       u16 capa_mask, u16 capa_val)
207 {
208         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
209         struct cfg80211_internal_bss *bss, *res = NULL;
210
211         spin_lock_bh(&dev->bss_lock);
212
213         list_for_each_entry(bss, &dev->bss_list, list) {
214                 if ((bss->pub.capability & capa_mask) != capa_val)
215                         continue;
216                 if (channel && bss->pub.channel != channel)
217                         continue;
218                 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
219                         res = bss;
220                         kref_get(&res->ref);
221                         break;
222                 }
223         }
224
225         spin_unlock_bh(&dev->bss_lock);
226         if (!res)
227                 return NULL;
228         return &res->pub;
229 }
230 EXPORT_SYMBOL(cfg80211_get_bss);
231
232 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
233                                        struct ieee80211_channel *channel,
234                                        const u8 *meshid, size_t meshidlen,
235                                        const u8 *meshcfg)
236 {
237         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
238         struct cfg80211_internal_bss *bss, *res = NULL;
239
240         spin_lock_bh(&dev->bss_lock);
241
242         list_for_each_entry(bss, &dev->bss_list, list) {
243                 if (channel && bss->pub.channel != channel)
244                         continue;
245                 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
246                         res = bss;
247                         kref_get(&res->ref);
248                         break;
249                 }
250         }
251
252         spin_unlock_bh(&dev->bss_lock);
253         if (!res)
254                 return NULL;
255         return &res->pub;
256 }
257 EXPORT_SYMBOL(cfg80211_get_mesh);
258
259
260 static void rb_insert_bss(struct cfg80211_registered_device *dev,
261                           struct cfg80211_internal_bss *bss)
262 {
263         struct rb_node **p = &dev->bss_tree.rb_node;
264         struct rb_node *parent = NULL;
265         struct cfg80211_internal_bss *tbss;
266         int cmp;
267
268         while (*p) {
269                 parent = *p;
270                 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
271
272                 cmp = cmp_bss(&bss->pub, &tbss->pub);
273
274                 if (WARN_ON(!cmp)) {
275                         /* will sort of leak this BSS */
276                         return;
277                 }
278
279                 if (cmp < 0)
280                         p = &(*p)->rb_left;
281                 else
282                         p = &(*p)->rb_right;
283         }
284
285         rb_link_node(&bss->rbn, parent, p);
286         rb_insert_color(&bss->rbn, &dev->bss_tree);
287 }
288
289 static struct cfg80211_internal_bss *
290 rb_find_bss(struct cfg80211_registered_device *dev,
291             struct cfg80211_internal_bss *res)
292 {
293         struct rb_node *n = dev->bss_tree.rb_node;
294         struct cfg80211_internal_bss *bss;
295         int r;
296
297         while (n) {
298                 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
299                 r = cmp_bss(&res->pub, &bss->pub);
300
301                 if (r == 0)
302                         return bss;
303                 else if (r < 0)
304                         n = n->rb_left;
305                 else
306                         n = n->rb_right;
307         }
308
309         return NULL;
310 }
311
312 static struct cfg80211_internal_bss *
313 cfg80211_bss_update(struct cfg80211_registered_device *dev,
314                     struct cfg80211_internal_bss *res,
315                     bool overwrite)
316 {
317         struct cfg80211_internal_bss *found = NULL;
318         const u8 *meshid, *meshcfg;
319
320         /*
321          * The reference to "res" is donated to this function.
322          */
323
324         if (WARN_ON(!res->pub.channel)) {
325                 kref_put(&res->ref, bss_release);
326                 return NULL;
327         }
328
329         res->ts = jiffies;
330
331         if (is_zero_ether_addr(res->pub.bssid)) {
332                 /* must be mesh, verify */
333                 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
334                                  res->pub.len_information_elements);
335                 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
336                                   res->pub.information_elements,
337                                   res->pub.len_information_elements);
338                 if (!meshid || !meshcfg ||
339                     meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
340                         /* bogus mesh */
341                         kref_put(&res->ref, bss_release);
342                         return NULL;
343                 }
344         }
345
346         spin_lock_bh(&dev->bss_lock);
347
348         found = rb_find_bss(dev, res);
349
350         if (found && overwrite) {
351                 list_replace(&found->list, &res->list);
352                 rb_replace_node(&found->rbn, &res->rbn,
353                                 &dev->bss_tree);
354                 kref_put(&found->ref, bss_release);
355                 found = res;
356         } else if (found) {
357                 kref_get(&found->ref);
358                 found->pub.beacon_interval = res->pub.beacon_interval;
359                 found->pub.tsf = res->pub.tsf;
360                 found->pub.signal = res->pub.signal;
361                 found->pub.signal_type = res->pub.signal_type;
362                 found->pub.capability = res->pub.capability;
363                 found->ts = res->ts;
364                 kref_put(&res->ref, bss_release);
365         } else {
366                 /* this "consumes" the reference */
367                 list_add_tail(&res->list, &dev->bss_list);
368                 rb_insert_bss(dev, res);
369                 found = res;
370         }
371
372         dev->bss_generation++;
373         spin_unlock_bh(&dev->bss_lock);
374
375         kref_get(&found->ref);
376         return found;
377 }
378
379 struct cfg80211_bss *
380 cfg80211_inform_bss_frame(struct wiphy *wiphy,
381                           struct ieee80211_channel *channel,
382                           struct ieee80211_mgmt *mgmt, size_t len,
383                           s32 signal, enum cfg80211_signal_type sigtype,
384                           gfp_t gfp)
385 {
386         struct cfg80211_internal_bss *res;
387         size_t ielen = len - offsetof(struct ieee80211_mgmt,
388                                       u.probe_resp.variable);
389         bool overwrite;
390         size_t privsz = wiphy->bss_priv_size;
391
392         if (WARN_ON(sigtype == NL80211_BSS_SIGNAL_UNSPEC &&
393                     (signal < 0 || signal > 100)))
394                 return NULL;
395
396         if (WARN_ON(!mgmt || !wiphy ||
397                     len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
398                 return NULL;
399
400         res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
401         if (!res)
402                 return NULL;
403
404         memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
405         res->pub.channel = channel;
406         res->pub.signal_type = sigtype;
407         res->pub.signal = signal;
408         res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
409         res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
410         res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
411         /* point to after the private area */
412         res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
413         memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
414         res->pub.len_information_elements = ielen;
415
416         kref_init(&res->ref);
417
418         overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
419
420         res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
421         if (!res)
422                 return NULL;
423
424         /* cfg80211_bss_update gives us a referenced result */
425         return &res->pub;
426 }
427 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
428
429 void cfg80211_put_bss(struct cfg80211_bss *pub)
430 {
431         struct cfg80211_internal_bss *bss;
432
433         if (!pub)
434                 return;
435
436         bss = container_of(pub, struct cfg80211_internal_bss, pub);
437         kref_put(&bss->ref, bss_release);
438 }
439 EXPORT_SYMBOL(cfg80211_put_bss);
440
441 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
442 {
443         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
444         struct cfg80211_internal_bss *bss;
445
446         if (WARN_ON(!pub))
447                 return;
448
449         bss = container_of(pub, struct cfg80211_internal_bss, pub);
450
451         spin_lock_bh(&dev->bss_lock);
452
453         list_del(&bss->list);
454         rb_erase(&bss->rbn, &dev->bss_tree);
455
456         spin_unlock_bh(&dev->bss_lock);
457
458         kref_put(&bss->ref, bss_release);
459 }
460 EXPORT_SYMBOL(cfg80211_unlink_bss);
461
462 #ifdef CONFIG_WIRELESS_EXT
463 int cfg80211_wext_siwscan(struct net_device *dev,
464                           struct iw_request_info *info,
465                           union iwreq_data *wrqu, char *extra)
466 {
467         struct cfg80211_registered_device *rdev;
468         struct wiphy *wiphy;
469         struct iw_scan_req *wreq = NULL;
470         struct cfg80211_scan_request *creq;
471         int i, err, n_channels = 0;
472         enum ieee80211_band band;
473
474         if (!netif_running(dev))
475                 return -ENETDOWN;
476
477         rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
478
479         if (IS_ERR(rdev))
480                 return PTR_ERR(rdev);
481
482         if (rdev->scan_req) {
483                 err = -EBUSY;
484                 goto out;
485         }
486
487         wiphy = &rdev->wiphy;
488
489         for (band = 0; band < IEEE80211_NUM_BANDS; band++)
490                 if (wiphy->bands[band])
491                         n_channels += wiphy->bands[band]->n_channels;
492
493         creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
494                        n_channels * sizeof(void *),
495                        GFP_ATOMIC);
496         if (!creq) {
497                 err = -ENOMEM;
498                 goto out;
499         }
500
501         creq->wiphy = wiphy;
502         creq->ifidx = dev->ifindex;
503         creq->ssids = (void *)(creq + 1);
504         creq->channels = (void *)(creq->ssids + 1);
505         creq->n_channels = n_channels;
506         creq->n_ssids = 1;
507
508         /* all channels */
509         i = 0;
510         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
511                 int j;
512                 if (!wiphy->bands[band])
513                         continue;
514                 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
515                         creq->channels[i] = &wiphy->bands[band]->channels[j];
516                         i++;
517                 }
518         }
519
520         /* translate scan request */
521         if (wrqu->data.length == sizeof(struct iw_scan_req)) {
522                 wreq = (struct iw_scan_req *)extra;
523
524                 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
525                         if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
526                                 return -EINVAL;
527                         memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
528                         creq->ssids[0].ssid_len = wreq->essid_len;
529                 }
530                 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
531                         creq->n_ssids = 0;
532         }
533
534         rdev->scan_req = creq;
535         err = rdev->ops->scan(wiphy, dev, creq);
536         if (err) {
537                 rdev->scan_req = NULL;
538                 kfree(creq);
539         }
540  out:
541         cfg80211_put_dev(rdev);
542         return err;
543 }
544 EXPORT_SYMBOL(cfg80211_wext_siwscan);
545
546 static void ieee80211_scan_add_ies(struct iw_request_info *info,
547                                    struct cfg80211_bss *bss,
548                                    char **current_ev, char *end_buf)
549 {
550         u8 *pos, *end, *next;
551         struct iw_event iwe;
552
553         if (!bss->information_elements ||
554             !bss->len_information_elements)
555                 return;
556
557         /*
558          * If needed, fragment the IEs buffer (at IE boundaries) into short
559          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
560          */
561         pos = bss->information_elements;
562         end = pos + bss->len_information_elements;
563
564         while (end - pos > IW_GENERIC_IE_MAX) {
565                 next = pos + 2 + pos[1];
566                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
567                         next = next + 2 + next[1];
568
569                 memset(&iwe, 0, sizeof(iwe));
570                 iwe.cmd = IWEVGENIE;
571                 iwe.u.data.length = next - pos;
572                 *current_ev = iwe_stream_add_point(info, *current_ev,
573                                                    end_buf, &iwe, pos);
574
575                 pos = next;
576         }
577
578         if (end > pos) {
579                 memset(&iwe, 0, sizeof(iwe));
580                 iwe.cmd = IWEVGENIE;
581                 iwe.u.data.length = end - pos;
582                 *current_ev = iwe_stream_add_point(info, *current_ev,
583                                                    end_buf, &iwe, pos);
584         }
585 }
586
587
588 static char *
589 ieee80211_bss(struct iw_request_info *info,
590                       struct cfg80211_internal_bss *bss,
591                       char *current_ev, char *end_buf)
592 {
593         struct iw_event iwe;
594         u8 *buf, *cfg, *p;
595         u8 *ie = bss->pub.information_elements;
596         int rem = bss->pub.len_information_elements, i;
597         bool ismesh = false;
598
599         memset(&iwe, 0, sizeof(iwe));
600         iwe.cmd = SIOCGIWAP;
601         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
602         memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
603         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
604                                           IW_EV_ADDR_LEN);
605
606         memset(&iwe, 0, sizeof(iwe));
607         iwe.cmd = SIOCGIWFREQ;
608         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
609         iwe.u.freq.e = 0;
610         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
611                                           IW_EV_FREQ_LEN);
612
613         memset(&iwe, 0, sizeof(iwe));
614         iwe.cmd = SIOCGIWFREQ;
615         iwe.u.freq.m = bss->pub.channel->center_freq;
616         iwe.u.freq.e = 6;
617         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
618                                           IW_EV_FREQ_LEN);
619
620         if (bss->pub.signal_type != CFG80211_SIGNAL_TYPE_NONE) {
621                 memset(&iwe, 0, sizeof(iwe));
622                 iwe.cmd = IWEVQUAL;
623                 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
624                                      IW_QUAL_NOISE_INVALID |
625                                      IW_QUAL_QUAL_INVALID;
626                 switch (bss->pub.signal_type) {
627                 case CFG80211_SIGNAL_TYPE_MBM:
628                         iwe.u.qual.level = bss->pub.signal / 100;
629                         iwe.u.qual.updated |= IW_QUAL_DBM;
630                         break;
631                 case CFG80211_SIGNAL_TYPE_UNSPEC:
632                         iwe.u.qual.level = bss->pub.signal;
633                         break;
634                 default:
635                         /* not reached */
636                         break;
637                 }
638                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
639                                                   &iwe, IW_EV_QUAL_LEN);
640         }
641
642         memset(&iwe, 0, sizeof(iwe));
643         iwe.cmd = SIOCGIWENCODE;
644         if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
645                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
646         else
647                 iwe.u.data.flags = IW_ENCODE_DISABLED;
648         iwe.u.data.length = 0;
649         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
650                                           &iwe, "");
651
652         while (rem >= 2) {
653                 /* invalid data */
654                 if (ie[1] > rem - 2)
655                         break;
656
657                 switch (ie[0]) {
658                 case WLAN_EID_SSID:
659                         memset(&iwe, 0, sizeof(iwe));
660                         iwe.cmd = SIOCGIWESSID;
661                         iwe.u.data.length = ie[1];
662                         iwe.u.data.flags = 1;
663                         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
664                                                           &iwe, ie + 2);
665                         break;
666                 case WLAN_EID_MESH_ID:
667                         memset(&iwe, 0, sizeof(iwe));
668                         iwe.cmd = SIOCGIWESSID;
669                         iwe.u.data.length = ie[1];
670                         iwe.u.data.flags = 1;
671                         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
672                                                           &iwe, ie + 2);
673                         break;
674                 case WLAN_EID_MESH_CONFIG:
675                         ismesh = true;
676                         if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
677                                 break;
678                         buf = kmalloc(50, GFP_ATOMIC);
679                         if (!buf)
680                                 break;
681                         cfg = ie + 2;
682                         memset(&iwe, 0, sizeof(iwe));
683                         iwe.cmd = IWEVCUSTOM;
684                         sprintf(buf, "Mesh network (version %d)", cfg[0]);
685                         iwe.u.data.length = strlen(buf);
686                         current_ev = iwe_stream_add_point(info, current_ev,
687                                                           end_buf,
688                                                           &iwe, buf);
689                         sprintf(buf, "Path Selection Protocol ID: "
690                                 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
691                                                         cfg[4]);
692                         iwe.u.data.length = strlen(buf);
693                         current_ev = iwe_stream_add_point(info, current_ev,
694                                                           end_buf,
695                                                           &iwe, buf);
696                         sprintf(buf, "Path Selection Metric ID: "
697                                 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
698                                                         cfg[8]);
699                         iwe.u.data.length = strlen(buf);
700                         current_ev = iwe_stream_add_point(info, current_ev,
701                                                           end_buf,
702                                                           &iwe, buf);
703                         sprintf(buf, "Congestion Control Mode ID: "
704                                 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
705                                                         cfg[11], cfg[12]);
706                         iwe.u.data.length = strlen(buf);
707                         current_ev = iwe_stream_add_point(info, current_ev,
708                                                           end_buf,
709                                                           &iwe, buf);
710                         sprintf(buf, "Channel Precedence: "
711                                 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
712                                                         cfg[15], cfg[16]);
713                         iwe.u.data.length = strlen(buf);
714                         current_ev = iwe_stream_add_point(info, current_ev,
715                                                           end_buf,
716                                                           &iwe, buf);
717                         kfree(buf);
718                         break;
719                 case WLAN_EID_SUPP_RATES:
720                 case WLAN_EID_EXT_SUPP_RATES:
721                         /* display all supported rates in readable format */
722                         p = current_ev + iwe_stream_lcp_len(info);
723
724                         memset(&iwe, 0, sizeof(iwe));
725                         iwe.cmd = SIOCGIWRATE;
726                         /* Those two flags are ignored... */
727                         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
728
729                         for (i = 0; i < ie[1]; i++) {
730                                 iwe.u.bitrate.value =
731                                         ((ie[i + 2] & 0x7f) * 500000);
732                                 p = iwe_stream_add_value(info, current_ev, p,
733                                                 end_buf, &iwe, IW_EV_PARAM_LEN);
734                         }
735                         current_ev = p;
736                         break;
737                 }
738                 rem -= ie[1] + 2;
739                 ie += ie[1] + 2;
740         }
741
742         if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
743             || ismesh) {
744                 memset(&iwe, 0, sizeof(iwe));
745                 iwe.cmd = SIOCGIWMODE;
746                 if (ismesh)
747                         iwe.u.mode = IW_MODE_MESH;
748                 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
749                         iwe.u.mode = IW_MODE_MASTER;
750                 else
751                         iwe.u.mode = IW_MODE_ADHOC;
752                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
753                                                   &iwe, IW_EV_UINT_LEN);
754         }
755
756         buf = kmalloc(30, GFP_ATOMIC);
757         if (buf) {
758                 memset(&iwe, 0, sizeof(iwe));
759                 iwe.cmd = IWEVCUSTOM;
760                 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
761                 iwe.u.data.length = strlen(buf);
762                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
763                                                   &iwe, buf);
764                 memset(&iwe, 0, sizeof(iwe));
765                 iwe.cmd = IWEVCUSTOM;
766                 sprintf(buf, " Last beacon: %dms ago",
767                         jiffies_to_msecs(jiffies - bss->ts));
768                 iwe.u.data.length = strlen(buf);
769                 current_ev = iwe_stream_add_point(info, current_ev,
770                                                   end_buf, &iwe, buf);
771                 kfree(buf);
772         }
773
774         ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
775
776         return current_ev;
777 }
778
779
780 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
781                                   struct iw_request_info *info,
782                                   char *buf, size_t len)
783 {
784         char *current_ev = buf;
785         char *end_buf = buf + len;
786         struct cfg80211_internal_bss *bss;
787
788         spin_lock_bh(&dev->bss_lock);
789         cfg80211_bss_expire(dev);
790
791         list_for_each_entry(bss, &dev->bss_list, list) {
792                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
793                         spin_unlock_bh(&dev->bss_lock);
794                         return -E2BIG;
795                 }
796                 current_ev = ieee80211_bss(info, bss,
797                                                    current_ev, end_buf);
798         }
799         spin_unlock_bh(&dev->bss_lock);
800         return current_ev - buf;
801 }
802
803
804 int cfg80211_wext_giwscan(struct net_device *dev,
805                           struct iw_request_info *info,
806                           struct iw_point *data, char *extra)
807 {
808         struct cfg80211_registered_device *rdev;
809         int res;
810
811         if (!netif_running(dev))
812                 return -ENETDOWN;
813
814         rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
815
816         if (IS_ERR(rdev))
817                 return PTR_ERR(rdev);
818
819         if (rdev->scan_req) {
820                 res = -EAGAIN;
821                 goto out;
822         }
823
824         res = ieee80211_scan_results(rdev, info, extra, data->length);
825         data->length = 0;
826         if (res >= 0) {
827                 data->length = res;
828                 res = 0;
829         }
830
831  out:
832         cfg80211_put_dev(rdev);
833         return res;
834 }
835 EXPORT_SYMBOL(cfg80211_wext_giwscan);
836 #endif