7d9f5264a63cc5916f4a12ea52c19bbde17f0011
[firefly-linux-kernel-4.4.55.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/slab.h>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/wireless.h>
11 #include <linux/nl80211.h>
12 #include <linux/etherdevice.h>
13 #include <net/arp.h>
14 #include <net/cfg80211.h>
15 #include <net/cfg80211-wext.h>
16 #include <net/iw_handler.h>
17 #include "core.h"
18 #include "nl80211.h"
19 #include "wext-compat.h"
20 #include "rdev-ops.h"
21
22 /**
23  * DOC: BSS tree/list structure
24  *
25  * At the top level, the BSS list is kept in both a list in each
26  * registered device (@bss_list) as well as an RB-tree for faster
27  * lookup. In the RB-tree, entries can be looked up using their
28  * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
29  * for other BSSes.
30  *
31  * Due to the possibility of hidden SSIDs, there's a second level
32  * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
33  * The hidden_list connects all BSSes belonging to a single AP
34  * that has a hidden SSID, and connects beacon and probe response
35  * entries. For a probe response entry for a hidden SSID, the
36  * hidden_beacon_bss pointer points to the BSS struct holding the
37  * beacon's information.
38  *
39  * Reference counting is done for all these references except for
40  * the hidden_list, so that a beacon BSS struct that is otherwise
41  * not referenced has one reference for being on the bss_list and
42  * one for each probe response entry that points to it using the
43  * hidden_beacon_bss pointer. When a BSS struct that has such a
44  * pointer is get/put, the refcount update is also propagated to
45  * the referenced struct, this ensure that it cannot get removed
46  * while somebody is using the probe response version.
47  *
48  * Note that the hidden_beacon_bss pointer never changes, due to
49  * the reference counting. Therefore, no locking is needed for
50  * it.
51  *
52  * Also note that the hidden_beacon_bss pointer is only relevant
53  * if the driver uses something other than the IEs, e.g. private
54  * data stored stored in the BSS struct, since the beacon IEs are
55  * also linked into the probe response struct.
56  */
57
58 #define IEEE80211_SCAN_RESULT_EXPIRE    (30 * HZ)
59
60 static void bss_free(struct cfg80211_internal_bss *bss)
61 {
62         struct cfg80211_bss_ies *ies;
63
64         if (WARN_ON(atomic_read(&bss->hold)))
65                 return;
66
67         ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
68         if (ies && !bss->pub.hidden_beacon_bss)
69                 kfree_rcu(ies, rcu_head);
70         ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
71         if (ies)
72                 kfree_rcu(ies, rcu_head);
73
74         /*
75          * This happens when the module is removed, it doesn't
76          * really matter any more save for completeness
77          */
78         if (!list_empty(&bss->hidden_list))
79                 list_del(&bss->hidden_list);
80
81         kfree(bss);
82 }
83
84 static inline void bss_ref_get(struct cfg80211_registered_device *dev,
85                                struct cfg80211_internal_bss *bss)
86 {
87         lockdep_assert_held(&dev->bss_lock);
88
89         bss->refcount++;
90         if (bss->pub.hidden_beacon_bss) {
91                 bss = container_of(bss->pub.hidden_beacon_bss,
92                                    struct cfg80211_internal_bss,
93                                    pub);
94                 bss->refcount++;
95         }
96 }
97
98 static inline void bss_ref_put(struct cfg80211_registered_device *dev,
99                                struct cfg80211_internal_bss *bss)
100 {
101         lockdep_assert_held(&dev->bss_lock);
102
103         if (bss->pub.hidden_beacon_bss) {
104                 struct cfg80211_internal_bss *hbss;
105                 hbss = container_of(bss->pub.hidden_beacon_bss,
106                                     struct cfg80211_internal_bss,
107                                     pub);
108                 hbss->refcount--;
109                 if (hbss->refcount == 0)
110                         bss_free(hbss);
111         }
112         bss->refcount--;
113         if (bss->refcount == 0)
114                 bss_free(bss);
115 }
116
117 static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
118                                   struct cfg80211_internal_bss *bss)
119 {
120         lockdep_assert_held(&dev->bss_lock);
121
122         if (!list_empty(&bss->hidden_list)) {
123                 /*
124                  * don't remove the beacon entry if it has
125                  * probe responses associated with it
126                  */
127                 if (!bss->pub.hidden_beacon_bss)
128                         return false;
129                 /*
130                  * if it's a probe response entry break its
131                  * link to the other entries in the group
132                  */
133                 list_del_init(&bss->hidden_list);
134         }
135
136         list_del_init(&bss->list);
137         rb_erase(&bss->rbn, &dev->bss_tree);
138         bss_ref_put(dev, bss);
139         return true;
140 }
141
142 static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
143                                   unsigned long expire_time)
144 {
145         struct cfg80211_internal_bss *bss, *tmp;
146         bool expired = false;
147
148         lockdep_assert_held(&dev->bss_lock);
149
150         list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
151                 if (atomic_read(&bss->hold))
152                         continue;
153                 if (!time_after(expire_time, bss->ts))
154                         continue;
155
156                 if (__cfg80211_unlink_bss(dev, bss))
157                         expired = true;
158         }
159
160         if (expired)
161                 dev->bss_generation++;
162 }
163
164 void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
165                            bool send_message)
166 {
167         struct cfg80211_scan_request *request;
168         struct wireless_dev *wdev;
169         struct sk_buff *msg;
170 #ifdef CONFIG_CFG80211_WEXT
171         union iwreq_data wrqu;
172 #endif
173
174         ASSERT_RTNL();
175
176         if (rdev->scan_msg) {
177                 nl80211_send_scan_result(rdev, rdev->scan_msg);
178                 rdev->scan_msg = NULL;
179                 return;
180         }
181
182         request = rdev->scan_req;
183         if (!request)
184                 return;
185
186         wdev = request->wdev;
187
188         /*
189          * This must be before sending the other events!
190          * Otherwise, wpa_supplicant gets completely confused with
191          * wext events.
192          */
193         if (wdev->netdev)
194                 cfg80211_sme_scan_done(wdev->netdev);
195
196         if (!request->aborted &&
197             request->flags & NL80211_SCAN_FLAG_FLUSH) {
198                 /* flush entries from previous scans */
199                 spin_lock_bh(&rdev->bss_lock);
200                 __cfg80211_bss_expire(rdev, request->scan_start);
201                 spin_unlock_bh(&rdev->bss_lock);
202         }
203
204         msg = nl80211_build_scan_msg(rdev, wdev, request->aborted);
205
206 #ifdef CONFIG_CFG80211_WEXT
207         if (wdev->netdev && !request->aborted) {
208                 memset(&wrqu, 0, sizeof(wrqu));
209
210                 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
211         }
212 #endif
213
214         if (wdev->netdev)
215                 dev_put(wdev->netdev);
216
217         rdev->scan_req = NULL;
218         kfree(request);
219
220         if (!send_message)
221                 rdev->scan_msg = msg;
222         else
223                 nl80211_send_scan_result(rdev, msg);
224 }
225
226 void __cfg80211_scan_done(struct work_struct *wk)
227 {
228         struct cfg80211_registered_device *rdev;
229
230         rdev = container_of(wk, struct cfg80211_registered_device,
231                             scan_done_wk);
232
233         rtnl_lock();
234         ___cfg80211_scan_done(rdev, true);
235         rtnl_unlock();
236 }
237
238 void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
239 {
240         trace_cfg80211_scan_done(request, aborted);
241         WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
242
243         request->aborted = aborted;
244         request->notified = true;
245         queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
246 }
247 EXPORT_SYMBOL(cfg80211_scan_done);
248
249 void __cfg80211_sched_scan_results(struct work_struct *wk)
250 {
251         struct cfg80211_registered_device *rdev;
252         struct cfg80211_sched_scan_request *request;
253
254         rdev = container_of(wk, struct cfg80211_registered_device,
255                             sched_scan_results_wk);
256
257         rtnl_lock();
258
259         request = rdev->sched_scan_req;
260
261         /* we don't have sched_scan_req anymore if the scan is stopping */
262         if (request) {
263                 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
264                         /* flush entries from previous scans */
265                         spin_lock_bh(&rdev->bss_lock);
266                         __cfg80211_bss_expire(rdev, request->scan_start);
267                         spin_unlock_bh(&rdev->bss_lock);
268                         request->scan_start =
269                                 jiffies + msecs_to_jiffies(request->interval);
270                 }
271                 nl80211_send_sched_scan_results(rdev, request->dev);
272         }
273
274         rtnl_unlock();
275 }
276
277 void cfg80211_sched_scan_results(struct wiphy *wiphy)
278 {
279         trace_cfg80211_sched_scan_results(wiphy);
280         /* ignore if we're not scanning */
281         if (wiphy_to_dev(wiphy)->sched_scan_req)
282                 queue_work(cfg80211_wq,
283                            &wiphy_to_dev(wiphy)->sched_scan_results_wk);
284 }
285 EXPORT_SYMBOL(cfg80211_sched_scan_results);
286
287 void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
288 {
289         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
290
291         trace_cfg80211_sched_scan_stopped(wiphy);
292
293         rtnl_lock();
294         __cfg80211_stop_sched_scan(rdev, true);
295         rtnl_unlock();
296 }
297 EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
298
299 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
300                                bool driver_initiated)
301 {
302         struct net_device *dev;
303
304         ASSERT_RTNL();
305
306         if (!rdev->sched_scan_req)
307                 return -ENOENT;
308
309         dev = rdev->sched_scan_req->dev;
310
311         if (!driver_initiated) {
312                 int err = rdev_sched_scan_stop(rdev, dev);
313                 if (err)
314                         return err;
315         }
316
317         nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
318
319         kfree(rdev->sched_scan_req);
320         rdev->sched_scan_req = NULL;
321
322         return 0;
323 }
324
325 void cfg80211_bss_age(struct cfg80211_registered_device *dev,
326                       unsigned long age_secs)
327 {
328         struct cfg80211_internal_bss *bss;
329         unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
330
331         spin_lock_bh(&dev->bss_lock);
332         list_for_each_entry(bss, &dev->bss_list, list)
333                 bss->ts -= age_jiffies;
334         spin_unlock_bh(&dev->bss_lock);
335 }
336
337 void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
338 {
339         __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
340 }
341
342 const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
343 {
344         while (len > 2 && ies[0] != eid) {
345                 len -= ies[1] + 2;
346                 ies += ies[1] + 2;
347         }
348         if (len < 2)
349                 return NULL;
350         if (len < 2 + ies[1])
351                 return NULL;
352         return ies;
353 }
354 EXPORT_SYMBOL(cfg80211_find_ie);
355
356 const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
357                                   const u8 *ies, int len)
358 {
359         struct ieee80211_vendor_ie *ie;
360         const u8 *pos = ies, *end = ies + len;
361         int ie_oui;
362
363         while (pos < end) {
364                 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
365                                        end - pos);
366                 if (!pos)
367                         return NULL;
368
369                 ie = (struct ieee80211_vendor_ie *)pos;
370
371                 /* make sure we can access ie->len */
372                 BUILD_BUG_ON(offsetof(struct ieee80211_vendor_ie, len) != 1);
373
374                 if (ie->len < sizeof(*ie))
375                         goto cont;
376
377                 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
378                 if (ie_oui == oui && ie->oui_type == oui_type)
379                         return pos;
380 cont:
381                 pos += 2 + ie->len;
382         }
383         return NULL;
384 }
385 EXPORT_SYMBOL(cfg80211_find_vendor_ie);
386
387 static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
388                    const u8 *ssid, size_t ssid_len)
389 {
390         const struct cfg80211_bss_ies *ies;
391         const u8 *ssidie;
392
393         if (bssid && !ether_addr_equal(a->bssid, bssid))
394                 return false;
395
396         if (!ssid)
397                 return true;
398
399         ies = rcu_access_pointer(a->ies);
400         if (!ies)
401                 return false;
402         ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
403         if (!ssidie)
404                 return false;
405         if (ssidie[1] != ssid_len)
406                 return false;
407         return memcmp(ssidie + 2, ssid, ssid_len) == 0;
408 }
409
410 /**
411  * enum bss_compare_mode - BSS compare mode
412  * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
413  * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
414  * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
415  */
416 enum bss_compare_mode {
417         BSS_CMP_REGULAR,
418         BSS_CMP_HIDE_ZLEN,
419         BSS_CMP_HIDE_NUL,
420 };
421
422 static int cmp_bss(struct cfg80211_bss *a,
423                    struct cfg80211_bss *b,
424                    enum bss_compare_mode mode)
425 {
426         const struct cfg80211_bss_ies *a_ies, *b_ies;
427         const u8 *ie1 = NULL;
428         const u8 *ie2 = NULL;
429         int i, r;
430
431         if (a->channel != b->channel)
432                 return b->channel->center_freq - a->channel->center_freq;
433
434         a_ies = rcu_access_pointer(a->ies);
435         if (!a_ies)
436                 return -1;
437         b_ies = rcu_access_pointer(b->ies);
438         if (!b_ies)
439                 return 1;
440
441         if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
442                 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
443                                        a_ies->data, a_ies->len);
444         if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
445                 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
446                                        b_ies->data, b_ies->len);
447         if (ie1 && ie2) {
448                 int mesh_id_cmp;
449
450                 if (ie1[1] == ie2[1])
451                         mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
452                 else
453                         mesh_id_cmp = ie2[1] - ie1[1];
454
455                 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
456                                        a_ies->data, a_ies->len);
457                 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
458                                        b_ies->data, b_ies->len);
459                 if (ie1 && ie2) {
460                         if (mesh_id_cmp)
461                                 return mesh_id_cmp;
462                         if (ie1[1] != ie2[1])
463                                 return ie2[1] - ie1[1];
464                         return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
465                 }
466         }
467
468         r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
469         if (r)
470                 return r;
471
472         ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
473         ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
474
475         if (!ie1 && !ie2)
476                 return 0;
477
478         /*
479          * Note that with "hide_ssid", the function returns a match if
480          * the already-present BSS ("b") is a hidden SSID beacon for
481          * the new BSS ("a").
482          */
483
484         /* sort missing IE before (left of) present IE */
485         if (!ie1)
486                 return -1;
487         if (!ie2)
488                 return 1;
489
490         switch (mode) {
491         case BSS_CMP_HIDE_ZLEN:
492                 /*
493                  * In ZLEN mode we assume the BSS entry we're
494                  * looking for has a zero-length SSID. So if
495                  * the one we're looking at right now has that,
496                  * return 0. Otherwise, return the difference
497                  * in length, but since we're looking for the
498                  * 0-length it's really equivalent to returning
499                  * the length of the one we're looking at.
500                  *
501                  * No content comparison is needed as we assume
502                  * the content length is zero.
503                  */
504                 return ie2[1];
505         case BSS_CMP_REGULAR:
506         default:
507                 /* sort by length first, then by contents */
508                 if (ie1[1] != ie2[1])
509                         return ie2[1] - ie1[1];
510                 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
511         case BSS_CMP_HIDE_NUL:
512                 if (ie1[1] != ie2[1])
513                         return ie2[1] - ie1[1];
514                 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
515                 for (i = 0; i < ie2[1]; i++)
516                         if (ie2[i + 2])
517                                 return -1;
518                 return 0;
519         }
520 }
521
522 /* Returned bss is reference counted and must be cleaned up appropriately. */
523 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
524                                       struct ieee80211_channel *channel,
525                                       const u8 *bssid,
526                                       const u8 *ssid, size_t ssid_len,
527                                       u16 capa_mask, u16 capa_val)
528 {
529         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
530         struct cfg80211_internal_bss *bss, *res = NULL;
531         unsigned long now = jiffies;
532
533         trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
534                                capa_val);
535
536         spin_lock_bh(&dev->bss_lock);
537
538         list_for_each_entry(bss, &dev->bss_list, list) {
539                 if ((bss->pub.capability & capa_mask) != capa_val)
540                         continue;
541                 if (channel && bss->pub.channel != channel)
542                         continue;
543                 /* Don't get expired BSS structs */
544                 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
545                     !atomic_read(&bss->hold))
546                         continue;
547                 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
548                         res = bss;
549                         bss_ref_get(dev, res);
550                         break;
551                 }
552         }
553
554         spin_unlock_bh(&dev->bss_lock);
555         if (!res)
556                 return NULL;
557         trace_cfg80211_return_bss(&res->pub);
558         return &res->pub;
559 }
560 EXPORT_SYMBOL(cfg80211_get_bss);
561
562 static void rb_insert_bss(struct cfg80211_registered_device *dev,
563                           struct cfg80211_internal_bss *bss)
564 {
565         struct rb_node **p = &dev->bss_tree.rb_node;
566         struct rb_node *parent = NULL;
567         struct cfg80211_internal_bss *tbss;
568         int cmp;
569
570         while (*p) {
571                 parent = *p;
572                 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
573
574                 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
575
576                 if (WARN_ON(!cmp)) {
577                         /* will sort of leak this BSS */
578                         return;
579                 }
580
581                 if (cmp < 0)
582                         p = &(*p)->rb_left;
583                 else
584                         p = &(*p)->rb_right;
585         }
586
587         rb_link_node(&bss->rbn, parent, p);
588         rb_insert_color(&bss->rbn, &dev->bss_tree);
589 }
590
591 static struct cfg80211_internal_bss *
592 rb_find_bss(struct cfg80211_registered_device *dev,
593             struct cfg80211_internal_bss *res,
594             enum bss_compare_mode mode)
595 {
596         struct rb_node *n = dev->bss_tree.rb_node;
597         struct cfg80211_internal_bss *bss;
598         int r;
599
600         while (n) {
601                 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
602                 r = cmp_bss(&res->pub, &bss->pub, mode);
603
604                 if (r == 0)
605                         return bss;
606                 else if (r < 0)
607                         n = n->rb_left;
608                 else
609                         n = n->rb_right;
610         }
611
612         return NULL;
613 }
614
615 static bool cfg80211_combine_bsses(struct cfg80211_registered_device *dev,
616                                    struct cfg80211_internal_bss *new)
617 {
618         const struct cfg80211_bss_ies *ies;
619         struct cfg80211_internal_bss *bss;
620         const u8 *ie;
621         int i, ssidlen;
622         u8 fold = 0;
623
624         ies = rcu_access_pointer(new->pub.beacon_ies);
625         if (WARN_ON(!ies))
626                 return false;
627
628         ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
629         if (!ie) {
630                 /* nothing to do */
631                 return true;
632         }
633
634         ssidlen = ie[1];
635         for (i = 0; i < ssidlen; i++)
636                 fold |= ie[2 + i];
637
638         if (fold) {
639                 /* not a hidden SSID */
640                 return true;
641         }
642
643         /* This is the bad part ... */
644
645         list_for_each_entry(bss, &dev->bss_list, list) {
646                 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
647                         continue;
648                 if (bss->pub.channel != new->pub.channel)
649                         continue;
650                 if (bss->pub.scan_width != new->pub.scan_width)
651                         continue;
652                 if (rcu_access_pointer(bss->pub.beacon_ies))
653                         continue;
654                 ies = rcu_access_pointer(bss->pub.ies);
655                 if (!ies)
656                         continue;
657                 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
658                 if (!ie)
659                         continue;
660                 if (ssidlen && ie[1] != ssidlen)
661                         continue;
662                 /* that would be odd ... */
663                 if (bss->pub.beacon_ies)
664                         continue;
665                 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
666                         continue;
667                 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
668                         list_del(&bss->hidden_list);
669                 /* combine them */
670                 list_add(&bss->hidden_list, &new->hidden_list);
671                 bss->pub.hidden_beacon_bss = &new->pub;
672                 new->refcount += bss->refcount;
673                 rcu_assign_pointer(bss->pub.beacon_ies,
674                                    new->pub.beacon_ies);
675         }
676
677         return true;
678 }
679
680 /* Returned bss is reference counted and must be cleaned up appropriately. */
681 static struct cfg80211_internal_bss *
682 cfg80211_bss_update(struct cfg80211_registered_device *dev,
683                     struct cfg80211_internal_bss *tmp,
684                     bool signal_valid)
685 {
686         struct cfg80211_internal_bss *found = NULL;
687
688         if (WARN_ON(!tmp->pub.channel))
689                 return NULL;
690
691         tmp->ts = jiffies;
692
693         spin_lock_bh(&dev->bss_lock);
694
695         if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
696                 spin_unlock_bh(&dev->bss_lock);
697                 return NULL;
698         }
699
700         found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR);
701
702         if (found) {
703                 /* Update IEs */
704                 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
705                         const struct cfg80211_bss_ies *old;
706
707                         old = rcu_access_pointer(found->pub.proberesp_ies);
708
709                         rcu_assign_pointer(found->pub.proberesp_ies,
710                                            tmp->pub.proberesp_ies);
711                         /* Override possible earlier Beacon frame IEs */
712                         rcu_assign_pointer(found->pub.ies,
713                                            tmp->pub.proberesp_ies);
714                         if (old)
715                                 kfree_rcu((struct cfg80211_bss_ies *)old,
716                                           rcu_head);
717                 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
718                         const struct cfg80211_bss_ies *old;
719                         struct cfg80211_internal_bss *bss;
720
721                         if (found->pub.hidden_beacon_bss &&
722                             !list_empty(&found->hidden_list)) {
723                                 const struct cfg80211_bss_ies *f;
724
725                                 /*
726                                  * The found BSS struct is one of the probe
727                                  * response members of a group, but we're
728                                  * receiving a beacon (beacon_ies in the tmp
729                                  * bss is used). This can only mean that the
730                                  * AP changed its beacon from not having an
731                                  * SSID to showing it, which is confusing so
732                                  * drop this information.
733                                  */
734
735                                 f = rcu_access_pointer(tmp->pub.beacon_ies);
736                                 kfree_rcu((struct cfg80211_bss_ies *)f,
737                                           rcu_head);
738                                 goto drop;
739                         }
740
741                         old = rcu_access_pointer(found->pub.beacon_ies);
742
743                         rcu_assign_pointer(found->pub.beacon_ies,
744                                            tmp->pub.beacon_ies);
745
746                         /* Override IEs if they were from a beacon before */
747                         if (old == rcu_access_pointer(found->pub.ies))
748                                 rcu_assign_pointer(found->pub.ies,
749                                                    tmp->pub.beacon_ies);
750
751                         /* Assign beacon IEs to all sub entries */
752                         list_for_each_entry(bss, &found->hidden_list,
753                                             hidden_list) {
754                                 const struct cfg80211_bss_ies *ies;
755
756                                 ies = rcu_access_pointer(bss->pub.beacon_ies);
757                                 WARN_ON(ies != old);
758
759                                 rcu_assign_pointer(bss->pub.beacon_ies,
760                                                    tmp->pub.beacon_ies);
761                         }
762
763                         if (old)
764                                 kfree_rcu((struct cfg80211_bss_ies *)old,
765                                           rcu_head);
766                 }
767
768                 found->pub.beacon_interval = tmp->pub.beacon_interval;
769                 /*
770                  * don't update the signal if beacon was heard on
771                  * adjacent channel.
772                  */
773                 if (signal_valid)
774                         found->pub.signal = tmp->pub.signal;
775                 found->pub.capability = tmp->pub.capability;
776                 found->ts = tmp->ts;
777         } else {
778                 struct cfg80211_internal_bss *new;
779                 struct cfg80211_internal_bss *hidden;
780                 struct cfg80211_bss_ies *ies;
781
782                 /*
783                  * create a copy -- the "res" variable that is passed in
784                  * is allocated on the stack since it's not needed in the
785                  * more common case of an update
786                  */
787                 new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
788                               GFP_ATOMIC);
789                 if (!new) {
790                         ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
791                         if (ies)
792                                 kfree_rcu(ies, rcu_head);
793                         ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
794                         if (ies)
795                                 kfree_rcu(ies, rcu_head);
796                         goto drop;
797                 }
798                 memcpy(new, tmp, sizeof(*new));
799                 new->refcount = 1;
800                 INIT_LIST_HEAD(&new->hidden_list);
801
802                 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
803                         hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
804                         if (!hidden)
805                                 hidden = rb_find_bss(dev, tmp,
806                                                      BSS_CMP_HIDE_NUL);
807                         if (hidden) {
808                                 new->pub.hidden_beacon_bss = &hidden->pub;
809                                 list_add(&new->hidden_list,
810                                          &hidden->hidden_list);
811                                 hidden->refcount++;
812                                 rcu_assign_pointer(new->pub.beacon_ies,
813                                                    hidden->pub.beacon_ies);
814                         }
815                 } else {
816                         /*
817                          * Ok so we found a beacon, and don't have an entry. If
818                          * it's a beacon with hidden SSID, we might be in for an
819                          * expensive search for any probe responses that should
820                          * be grouped with this beacon for updates ...
821                          */
822                         if (!cfg80211_combine_bsses(dev, new)) {
823                                 kfree(new);
824                                 goto drop;
825                         }
826                 }
827
828                 list_add_tail(&new->list, &dev->bss_list);
829                 rb_insert_bss(dev, new);
830                 found = new;
831         }
832
833         dev->bss_generation++;
834         bss_ref_get(dev, found);
835         spin_unlock_bh(&dev->bss_lock);
836
837         return found;
838  drop:
839         spin_unlock_bh(&dev->bss_lock);
840         return NULL;
841 }
842
843 static struct ieee80211_channel *
844 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
845                          struct ieee80211_channel *channel)
846 {
847         const u8 *tmp;
848         u32 freq;
849         int channel_number = -1;
850
851         tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
852         if (tmp && tmp[1] == 1) {
853                 channel_number = tmp[2];
854         } else {
855                 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
856                 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
857                         struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
858
859                         channel_number = htop->primary_chan;
860                 }
861         }
862
863         if (channel_number < 0)
864                 return channel;
865
866         freq = ieee80211_channel_to_frequency(channel_number, channel->band);
867         channel = ieee80211_get_channel(wiphy, freq);
868         if (!channel)
869                 return NULL;
870         if (channel->flags & IEEE80211_CHAN_DISABLED)
871                 return NULL;
872         return channel;
873 }
874
875 /* Returned bss is reference counted and must be cleaned up appropriately. */
876 struct cfg80211_bss*
877 cfg80211_inform_bss_width(struct wiphy *wiphy,
878                           struct ieee80211_channel *rx_channel,
879                           enum nl80211_bss_scan_width scan_width,
880                           const u8 *bssid, u64 tsf, u16 capability,
881                           u16 beacon_interval, const u8 *ie, size_t ielen,
882                           s32 signal, gfp_t gfp)
883 {
884         struct cfg80211_bss_ies *ies;
885         struct ieee80211_channel *channel;
886         struct cfg80211_internal_bss tmp = {}, *res;
887
888         if (WARN_ON(!wiphy))
889                 return NULL;
890
891         if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
892                         (signal < 0 || signal > 100)))
893                 return NULL;
894
895         channel = cfg80211_get_bss_channel(wiphy, ie, ielen, rx_channel);
896         if (!channel)
897                 return NULL;
898
899         memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
900         tmp.pub.channel = channel;
901         tmp.pub.scan_width = scan_width;
902         tmp.pub.signal = signal;
903         tmp.pub.beacon_interval = beacon_interval;
904         tmp.pub.capability = capability;
905         /*
906          * Since we do not know here whether the IEs are from a Beacon or Probe
907          * Response frame, we need to pick one of the options and only use it
908          * with the driver that does not provide the full Beacon/Probe Response
909          * frame. Use Beacon frame pointer to avoid indicating that this should
910          * override the IEs pointer should we have received an earlier
911          * indication of Probe Response data.
912          */
913         ies = kmalloc(sizeof(*ies) + ielen, gfp);
914         if (!ies)
915                 return NULL;
916         ies->len = ielen;
917         ies->tsf = tsf;
918         memcpy(ies->data, ie, ielen);
919
920         rcu_assign_pointer(tmp.pub.beacon_ies, ies);
921         rcu_assign_pointer(tmp.pub.ies, ies);
922
923         res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp,
924                                   rx_channel == channel);
925         if (!res)
926                 return NULL;
927
928         if (res->pub.capability & WLAN_CAPABILITY_ESS)
929                 regulatory_hint_found_beacon(wiphy, channel, gfp);
930
931         trace_cfg80211_return_bss(&res->pub);
932         /* cfg80211_bss_update gives us a referenced result */
933         return &res->pub;
934 }
935 EXPORT_SYMBOL(cfg80211_inform_bss_width);
936
937 /* Returned bss is reference counted and must be cleaned up appropriately. */
938 struct cfg80211_bss *
939 cfg80211_inform_bss_width_frame(struct wiphy *wiphy,
940                                 struct ieee80211_channel *rx_channel,
941                                 enum nl80211_bss_scan_width scan_width,
942                                 struct ieee80211_mgmt *mgmt, size_t len,
943                                 s32 signal, gfp_t gfp)
944 {
945         struct cfg80211_internal_bss tmp = {}, *res;
946         struct cfg80211_bss_ies *ies;
947         struct ieee80211_channel *channel;
948         size_t ielen = len - offsetof(struct ieee80211_mgmt,
949                                       u.probe_resp.variable);
950
951         BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
952                         offsetof(struct ieee80211_mgmt, u.beacon.variable));
953
954         trace_cfg80211_inform_bss_width_frame(wiphy, rx_channel, scan_width, mgmt,
955                                               len, signal);
956
957         if (WARN_ON(!mgmt))
958                 return NULL;
959
960         if (WARN_ON(!wiphy))
961                 return NULL;
962
963         if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
964                     (signal < 0 || signal > 100)))
965                 return NULL;
966
967         if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
968                 return NULL;
969
970         channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
971                                            ielen, rx_channel);
972         if (!channel)
973                 return NULL;
974
975         ies = kmalloc(sizeof(*ies) + ielen, gfp);
976         if (!ies)
977                 return NULL;
978         ies->len = ielen;
979         ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
980         memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
981
982         if (ieee80211_is_probe_resp(mgmt->frame_control))
983                 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
984         else
985                 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
986         rcu_assign_pointer(tmp.pub.ies, ies);
987         
988         memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
989         tmp.pub.channel = channel;
990         tmp.pub.scan_width = scan_width;
991         tmp.pub.signal = signal;
992         tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
993         tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
994
995         res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp,
996                                   rx_channel == channel);
997         if (!res)
998                 return NULL;
999
1000         if (res->pub.capability & WLAN_CAPABILITY_ESS)
1001                 regulatory_hint_found_beacon(wiphy, channel, gfp);
1002
1003         trace_cfg80211_return_bss(&res->pub);
1004         /* cfg80211_bss_update gives us a referenced result */
1005         return &res->pub;
1006 }
1007 EXPORT_SYMBOL(cfg80211_inform_bss_width_frame);
1008
1009 void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1010 {
1011         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
1012         struct cfg80211_internal_bss *bss;
1013
1014         if (!pub)
1015                 return;
1016
1017         bss = container_of(pub, struct cfg80211_internal_bss, pub);
1018
1019         spin_lock_bh(&dev->bss_lock);
1020         bss_ref_get(dev, bss);
1021         spin_unlock_bh(&dev->bss_lock);
1022 }
1023 EXPORT_SYMBOL(cfg80211_ref_bss);
1024
1025 void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1026 {
1027         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
1028         struct cfg80211_internal_bss *bss;
1029
1030         if (!pub)
1031                 return;
1032
1033         bss = container_of(pub, struct cfg80211_internal_bss, pub);
1034
1035         spin_lock_bh(&dev->bss_lock);
1036         bss_ref_put(dev, bss);
1037         spin_unlock_bh(&dev->bss_lock);
1038 }
1039 EXPORT_SYMBOL(cfg80211_put_bss);
1040
1041 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1042 {
1043         struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
1044         struct cfg80211_internal_bss *bss;
1045
1046         if (WARN_ON(!pub))
1047                 return;
1048
1049         bss = container_of(pub, struct cfg80211_internal_bss, pub);
1050
1051         spin_lock_bh(&dev->bss_lock);
1052         if (!list_empty(&bss->list)) {
1053                 if (__cfg80211_unlink_bss(dev, bss))
1054                         dev->bss_generation++;
1055         }
1056         spin_unlock_bh(&dev->bss_lock);
1057 }
1058 EXPORT_SYMBOL(cfg80211_unlink_bss);
1059
1060 #ifdef CONFIG_CFG80211_WEXT
1061 static struct cfg80211_registered_device *
1062 cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1063 {
1064         struct cfg80211_registered_device *rdev;
1065         struct net_device *dev;
1066
1067         ASSERT_RTNL();
1068
1069         dev = dev_get_by_index(net, ifindex);
1070         if (!dev)
1071                 return ERR_PTR(-ENODEV);
1072         if (dev->ieee80211_ptr)
1073                 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
1074         else
1075                 rdev = ERR_PTR(-ENODEV);
1076         dev_put(dev);
1077         return rdev;
1078 }
1079
1080 int cfg80211_wext_siwscan(struct net_device *dev,
1081                           struct iw_request_info *info,
1082                           union iwreq_data *wrqu, char *extra)
1083 {
1084         struct cfg80211_registered_device *rdev;
1085         struct wiphy *wiphy;
1086         struct iw_scan_req *wreq = NULL;
1087         struct cfg80211_scan_request *creq = NULL;
1088         int i, err, n_channels = 0;
1089         enum ieee80211_band band;
1090
1091         if (!netif_running(dev))
1092                 return -ENETDOWN;
1093
1094         if (wrqu->data.length == sizeof(struct iw_scan_req))
1095                 wreq = (struct iw_scan_req *)extra;
1096
1097         rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1098
1099         if (IS_ERR(rdev))
1100                 return PTR_ERR(rdev);
1101
1102         if (rdev->scan_req || rdev->scan_msg) {
1103                 err = -EBUSY;
1104                 goto out;
1105         }
1106
1107         wiphy = &rdev->wiphy;
1108
1109         /* Determine number of channels, needed to allocate creq */
1110         if (wreq && wreq->num_channels)
1111                 n_channels = wreq->num_channels;
1112         else
1113                 n_channels = ieee80211_get_num_supported_channels(wiphy);
1114
1115         creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1116                        n_channels * sizeof(void *),
1117                        GFP_ATOMIC);
1118         if (!creq) {
1119                 err = -ENOMEM;
1120                 goto out;
1121         }
1122
1123         creq->wiphy = wiphy;
1124         creq->wdev = dev->ieee80211_ptr;
1125         /* SSIDs come after channels */
1126         creq->ssids = (void *)&creq->channels[n_channels];
1127         creq->n_channels = n_channels;
1128         creq->n_ssids = 1;
1129         creq->scan_start = jiffies;
1130
1131         /* translate "Scan on frequencies" request */
1132         i = 0;
1133         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1134                 int j;
1135
1136                 if (!wiphy->bands[band])
1137                         continue;
1138
1139                 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1140                         /* ignore disabled channels */
1141                         if (wiphy->bands[band]->channels[j].flags &
1142                                                 IEEE80211_CHAN_DISABLED)
1143                                 continue;
1144
1145                         /* If we have a wireless request structure and the
1146                          * wireless request specifies frequencies, then search
1147                          * for the matching hardware channel.
1148                          */
1149                         if (wreq && wreq->num_channels) {
1150                                 int k;
1151                                 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1152                                 for (k = 0; k < wreq->num_channels; k++) {
1153                                         int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
1154                                         if (wext_freq == wiphy_freq)
1155                                                 goto wext_freq_found;
1156                                 }
1157                                 goto wext_freq_not_found;
1158                         }
1159
1160                 wext_freq_found:
1161                         creq->channels[i] = &wiphy->bands[band]->channels[j];
1162                         i++;
1163                 wext_freq_not_found: ;
1164                 }
1165         }
1166         /* No channels found? */
1167         if (!i) {
1168                 err = -EINVAL;
1169                 goto out;
1170         }
1171
1172         /* Set real number of channels specified in creq->channels[] */
1173         creq->n_channels = i;
1174
1175         /* translate "Scan for SSID" request */
1176         if (wreq) {
1177                 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1178                         if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1179                                 err = -EINVAL;
1180                                 goto out;
1181                         }
1182                         memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1183                         creq->ssids[0].ssid_len = wreq->essid_len;
1184                 }
1185                 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1186                         creq->n_ssids = 0;
1187         }
1188
1189         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
1190                 if (wiphy->bands[i])
1191                         creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
1192
1193         rdev->scan_req = creq;
1194         err = rdev_scan(rdev, creq);
1195         if (err) {
1196                 rdev->scan_req = NULL;
1197                 /* creq will be freed below */
1198         } else {
1199                 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
1200                 /* creq now owned by driver */
1201                 creq = NULL;
1202                 dev_hold(dev);
1203         }
1204  out:
1205         kfree(creq);
1206         return err;
1207 }
1208 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
1209
1210 static void ieee80211_scan_add_ies(struct iw_request_info *info,
1211                                    const struct cfg80211_bss_ies *ies,
1212                                    char **current_ev, char *end_buf)
1213 {
1214         const u8 *pos, *end, *next;
1215         struct iw_event iwe;
1216
1217         if (!ies)
1218                 return;
1219
1220         /*
1221          * If needed, fragment the IEs buffer (at IE boundaries) into short
1222          * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1223          */
1224         pos = ies->data;
1225         end = pos + ies->len;
1226
1227         while (end - pos > IW_GENERIC_IE_MAX) {
1228                 next = pos + 2 + pos[1];
1229                 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1230                         next = next + 2 + next[1];
1231
1232                 memset(&iwe, 0, sizeof(iwe));
1233                 iwe.cmd = IWEVGENIE;
1234                 iwe.u.data.length = next - pos;
1235                 *current_ev = iwe_stream_add_point(info, *current_ev,
1236                                                    end_buf, &iwe,
1237                                                    (void *)pos);
1238
1239                 pos = next;
1240         }
1241
1242         if (end > pos) {
1243                 memset(&iwe, 0, sizeof(iwe));
1244                 iwe.cmd = IWEVGENIE;
1245                 iwe.u.data.length = end - pos;
1246                 *current_ev = iwe_stream_add_point(info, *current_ev,
1247                                                    end_buf, &iwe,
1248                                                    (void *)pos);
1249         }
1250 }
1251
1252 static char *
1253 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1254               struct cfg80211_internal_bss *bss, char *current_ev,
1255               char *end_buf)
1256 {
1257         const struct cfg80211_bss_ies *ies;
1258         struct iw_event iwe;
1259         const u8 *ie;
1260         u8 *buf, *cfg, *p;
1261         int rem, i, sig;
1262         bool ismesh = false;
1263
1264         memset(&iwe, 0, sizeof(iwe));
1265         iwe.cmd = SIOCGIWAP;
1266         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1267         memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1268         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1269                                           IW_EV_ADDR_LEN);
1270
1271         memset(&iwe, 0, sizeof(iwe));
1272         iwe.cmd = SIOCGIWFREQ;
1273         iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1274         iwe.u.freq.e = 0;
1275         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1276                                           IW_EV_FREQ_LEN);
1277
1278         memset(&iwe, 0, sizeof(iwe));
1279         iwe.cmd = SIOCGIWFREQ;
1280         iwe.u.freq.m = bss->pub.channel->center_freq;
1281         iwe.u.freq.e = 6;
1282         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1283                                           IW_EV_FREQ_LEN);
1284
1285         if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
1286                 memset(&iwe, 0, sizeof(iwe));
1287                 iwe.cmd = IWEVQUAL;
1288                 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1289                                      IW_QUAL_NOISE_INVALID |
1290                                      IW_QUAL_QUAL_UPDATED;
1291                 switch (wiphy->signal_type) {
1292                 case CFG80211_SIGNAL_TYPE_MBM:
1293                         sig = bss->pub.signal / 100;
1294                         iwe.u.qual.level = sig;
1295                         iwe.u.qual.updated |= IW_QUAL_DBM;
1296                         if (sig < -110)         /* rather bad */
1297                                 sig = -110;
1298                         else if (sig > -40)     /* perfect */
1299                                 sig = -40;
1300                         /* will give a range of 0 .. 70 */
1301                         iwe.u.qual.qual = sig + 110;
1302                         break;
1303                 case CFG80211_SIGNAL_TYPE_UNSPEC:
1304                         iwe.u.qual.level = bss->pub.signal;
1305                         /* will give range 0 .. 100 */
1306                         iwe.u.qual.qual = bss->pub.signal;
1307                         break;
1308                 default:
1309                         /* not reached */
1310                         break;
1311                 }
1312                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1313                                                   &iwe, IW_EV_QUAL_LEN);
1314         }
1315
1316         memset(&iwe, 0, sizeof(iwe));
1317         iwe.cmd = SIOCGIWENCODE;
1318         if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1319                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1320         else
1321                 iwe.u.data.flags = IW_ENCODE_DISABLED;
1322         iwe.u.data.length = 0;
1323         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1324                                           &iwe, "");
1325
1326         rcu_read_lock();
1327         ies = rcu_dereference(bss->pub.ies);
1328         rem = ies->len;
1329         ie = ies->data;
1330
1331         while (rem >= 2) {
1332                 /* invalid data */
1333                 if (ie[1] > rem - 2)
1334                         break;
1335
1336                 switch (ie[0]) {
1337                 case WLAN_EID_SSID:
1338                         memset(&iwe, 0, sizeof(iwe));
1339                         iwe.cmd = SIOCGIWESSID;
1340                         iwe.u.data.length = ie[1];
1341                         iwe.u.data.flags = 1;
1342                         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1343                                                           &iwe, (u8 *)ie + 2);
1344                         break;
1345                 case WLAN_EID_MESH_ID:
1346                         memset(&iwe, 0, sizeof(iwe));
1347                         iwe.cmd = SIOCGIWESSID;
1348                         iwe.u.data.length = ie[1];
1349                         iwe.u.data.flags = 1;
1350                         current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1351                                                           &iwe, (u8 *)ie + 2);
1352                         break;
1353                 case WLAN_EID_MESH_CONFIG:
1354                         ismesh = true;
1355                         if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
1356                                 break;
1357                         buf = kmalloc(50, GFP_ATOMIC);
1358                         if (!buf)
1359                                 break;
1360                         cfg = (u8 *)ie + 2;
1361                         memset(&iwe, 0, sizeof(iwe));
1362                         iwe.cmd = IWEVCUSTOM;
1363                         sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1364                                 "0x%02X", cfg[0]);
1365                         iwe.u.data.length = strlen(buf);
1366                         current_ev = iwe_stream_add_point(info, current_ev,
1367                                                           end_buf,
1368                                                           &iwe, buf);
1369                         sprintf(buf, "Path Selection Metric ID: 0x%02X",
1370                                 cfg[1]);
1371                         iwe.u.data.length = strlen(buf);
1372                         current_ev = iwe_stream_add_point(info, current_ev,
1373                                                           end_buf,
1374                                                           &iwe, buf);
1375                         sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1376                                 cfg[2]);
1377                         iwe.u.data.length = strlen(buf);
1378                         current_ev = iwe_stream_add_point(info, current_ev,
1379                                                           end_buf,
1380                                                           &iwe, buf);
1381                         sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
1382                         iwe.u.data.length = strlen(buf);
1383                         current_ev = iwe_stream_add_point(info, current_ev,
1384                                                           end_buf,
1385                                                           &iwe, buf);
1386                         sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1387                         iwe.u.data.length = strlen(buf);
1388                         current_ev = iwe_stream_add_point(info, current_ev,
1389                                                           end_buf,
1390                                                           &iwe, buf);
1391                         sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1392                         iwe.u.data.length = strlen(buf);
1393                         current_ev = iwe_stream_add_point(info, current_ev,
1394                                                           end_buf,
1395                                                           &iwe, buf);
1396                         sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
1397                         iwe.u.data.length = strlen(buf);
1398                         current_ev = iwe_stream_add_point(info, current_ev,
1399                                                           end_buf,
1400                                                           &iwe, buf);
1401                         kfree(buf);
1402                         break;
1403                 case WLAN_EID_SUPP_RATES:
1404                 case WLAN_EID_EXT_SUPP_RATES:
1405                         /* display all supported rates in readable format */
1406                         p = current_ev + iwe_stream_lcp_len(info);
1407
1408                         memset(&iwe, 0, sizeof(iwe));
1409                         iwe.cmd = SIOCGIWRATE;
1410                         /* Those two flags are ignored... */
1411                         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1412
1413                         for (i = 0; i < ie[1]; i++) {
1414                                 iwe.u.bitrate.value =
1415                                         ((ie[i + 2] & 0x7f) * 500000);
1416                                 p = iwe_stream_add_value(info, current_ev, p,
1417                                                 end_buf, &iwe, IW_EV_PARAM_LEN);
1418                         }
1419                         current_ev = p;
1420                         break;
1421                 }
1422                 rem -= ie[1] + 2;
1423                 ie += ie[1] + 2;
1424         }
1425
1426         if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1427             ismesh) {
1428                 memset(&iwe, 0, sizeof(iwe));
1429                 iwe.cmd = SIOCGIWMODE;
1430                 if (ismesh)
1431                         iwe.u.mode = IW_MODE_MESH;
1432                 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1433                         iwe.u.mode = IW_MODE_MASTER;
1434                 else
1435                         iwe.u.mode = IW_MODE_ADHOC;
1436                 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1437                                                   &iwe, IW_EV_UINT_LEN);
1438         }
1439
1440         buf = kmalloc(31, GFP_ATOMIC);
1441         if (buf) {
1442                 memset(&iwe, 0, sizeof(iwe));
1443                 iwe.cmd = IWEVCUSTOM;
1444                 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
1445                 iwe.u.data.length = strlen(buf);
1446                 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1447                                                   &iwe, buf);
1448                 memset(&iwe, 0, sizeof(iwe));
1449                 iwe.cmd = IWEVCUSTOM;
1450                 sprintf(buf, " Last beacon: %ums ago",
1451                         elapsed_jiffies_msecs(bss->ts));
1452                 iwe.u.data.length = strlen(buf);
1453                 current_ev = iwe_stream_add_point(info, current_ev,
1454                                                   end_buf, &iwe, buf);
1455                 kfree(buf);
1456         }
1457
1458         ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1459         rcu_read_unlock();
1460
1461         return current_ev;
1462 }
1463
1464
1465 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1466                                   struct iw_request_info *info,
1467                                   char *buf, size_t len)
1468 {
1469         char *current_ev = buf;
1470         char *end_buf = buf + len;
1471         struct cfg80211_internal_bss *bss;
1472
1473         spin_lock_bh(&dev->bss_lock);
1474         cfg80211_bss_expire(dev);
1475
1476         list_for_each_entry(bss, &dev->bss_list, list) {
1477                 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1478                         spin_unlock_bh(&dev->bss_lock);
1479                         return -E2BIG;
1480                 }
1481                 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1482                                            current_ev, end_buf);
1483         }
1484         spin_unlock_bh(&dev->bss_lock);
1485         return current_ev - buf;
1486 }
1487
1488
1489 int cfg80211_wext_giwscan(struct net_device *dev,
1490                           struct iw_request_info *info,
1491                           struct iw_point *data, char *extra)
1492 {
1493         struct cfg80211_registered_device *rdev;
1494         int res;
1495
1496         if (!netif_running(dev))
1497                 return -ENETDOWN;
1498
1499         rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1500
1501         if (IS_ERR(rdev))
1502                 return PTR_ERR(rdev);
1503
1504         if (rdev->scan_req || rdev->scan_msg)
1505                 return -EAGAIN;
1506
1507         res = ieee80211_scan_results(rdev, info, extra, data->length);
1508         data->length = 0;
1509         if (res >= 0) {
1510                 data->length = res;
1511                 res = 0;
1512         }
1513
1514         return res;
1515 }
1516 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
1517 #endif