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