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