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