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