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