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