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