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