blob: ad1e4068ce06644218e6e005acdc129a1610ae56 [file] [log] [blame]
Johannes Berg2a519312009-02-10 21:25:55 +01001/*
2 * cfg80211 scan result handling
3 *
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Johannes Berg2a519312009-02-10 21:25:55 +01008#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>
Johannes Berg262eb9b22011-07-13 10:39:09 +020015#include <net/cfg80211-wext.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <net/iw_handler.h>
17#include "core.h"
18#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020019#include "wext-compat.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030020#include "rdev-ops.h"
Johannes Berg2a519312009-02-10 21:25:55 +010021
Johannes Berg776b3582013-02-01 02:06:18 +010022/**
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
Rajkumar Manoharanf9616e02012-04-13 16:38:40 +053058#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010059
Johannes Berg776b3582013-02-01 02:06:18 +010060static void bss_free(struct cfg80211_internal_bss *bss)
Amitkumar Karware8e27c62012-10-11 21:03:33 -070061{
Johannes Berg9caf0362012-11-29 01:25:20 +010062 struct cfg80211_bss_ies *ies;
Johannes Bergb629ea32012-11-28 22:14:56 +010063
64 if (WARN_ON(atomic_read(&bss->hold)))
65 return;
66
Johannes Berg9caf0362012-11-29 01:25:20 +010067 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
Johannes Berg776b3582013-02-01 02:06:18 +010068 if (ies && !bss->pub.hidden_beacon_bss)
Johannes Berg9caf0362012-11-29 01:25:20 +010069 kfree_rcu(ies, rcu_head);
70 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
71 if (ies)
72 kfree_rcu(ies, rcu_head);
Amitkumar Karware8e27c62012-10-11 21:03:33 -070073
Johannes Berg776b3582013-02-01 02:06:18 +010074 /*
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
Amitkumar Karware8e27c62012-10-11 21:03:33 -070081 kfree(bss);
82}
83
Johannes Berg776b3582013-02-01 02:06:18 +010084static inline void bss_ref_get(struct cfg80211_registered_device *dev,
85 struct cfg80211_internal_bss *bss)
Johannes Berg0532d4f2013-02-01 01:34:36 +010086{
Johannes Berg776b3582013-02-01 02:06:18 +010087 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 }
Johannes Berg0532d4f2013-02-01 01:34:36 +010096}
97
Johannes Berg776b3582013-02-01 02:06:18 +010098static inline void bss_ref_put(struct cfg80211_registered_device *dev,
99 struct cfg80211_internal_bss *bss)
Johannes Berg0532d4f2013-02-01 01:34:36 +0100100{
Johannes Berg776b3582013-02-01 02:06:18 +0100101 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);
Johannes Berg0532d4f2013-02-01 01:34:36 +0100115}
116
Johannes Berg776b3582013-02-01 02:06:18 +0100117static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
Amitkumar Karware8e27c62012-10-11 21:03:33 -0700118 struct cfg80211_internal_bss *bss)
119{
Johannes Berg4b1af472013-02-01 01:05:43 +0100120 lockdep_assert_held(&dev->bss_lock);
121
Johannes Berg776b3582013-02-01 02:06:18 +0100122 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
Amitkumar Karware8e27c62012-10-11 21:03:33 -0700136 list_del_init(&bss->list);
137 rb_erase(&bss->rbn, &dev->bss_tree);
Johannes Berg776b3582013-02-01 02:06:18 +0100138 bss_ref_put(dev, bss);
139 return true;
Amitkumar Karware8e27c62012-10-11 21:03:33 -0700140}
141
Sam Leffler15d60302012-10-11 21:03:34 -0700142static 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
Johannes Berg4b1af472013-02-01 01:05:43 +0100148 lockdep_assert_held(&dev->bss_lock);
149
Sam Leffler15d60302012-10-11 21:03:34 -0700150 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
Johannes Berg776b3582013-02-01 02:06:18 +0100156 if (__cfg80211_unlink_bss(dev, bss))
157 expired = true;
Sam Leffler15d60302012-10-11 21:03:34 -0700158 }
159
160 if (expired)
161 dev->bss_generation++;
162}
163
Johannes Berg01a0ac42009-08-20 21:36:16 +0200164void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +0100165{
Johannes Berg667503dd2009-07-07 03:56:11 +0200166 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +0200167 struct wireless_dev *wdev;
Johannes Berg3d23e342009-09-29 23:27:28 +0200168#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100169 union iwreq_data wrqu;
170#endif
171
Johannes Berg5fe231e2013-05-08 21:45:15 +0200172 ASSERT_RTNL();
Johannes Berg01a0ac42009-08-20 21:36:16 +0200173
Johannes Berg667503dd2009-07-07 03:56:11 +0200174 request = rdev->scan_req;
175
Johannes Berg01a0ac42009-08-20 21:36:16 +0200176 if (!request)
177 return;
178
Johannes Bergfd014282012-06-18 19:17:03 +0200179 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +0100180
Johannes Berg6829c872009-07-02 09:13:27 +0200181 /*
182 * This must be before sending the other events!
183 * Otherwise, wpa_supplicant gets completely confused with
184 * wext events.
185 */
Johannes Bergfd014282012-06-18 19:17:03 +0200186 if (wdev->netdev)
187 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200188
Sam Leffler15d60302012-10-11 21:03:34 -0700189 if (request->aborted) {
Johannes Bergfd014282012-06-18 19:17:03 +0200190 nl80211_send_scan_aborted(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700191 } else {
192 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
193 /* flush entries from previous scans */
194 spin_lock_bh(&rdev->bss_lock);
195 __cfg80211_bss_expire(rdev, request->scan_start);
196 spin_unlock_bh(&rdev->bss_lock);
197 }
Johannes Bergfd014282012-06-18 19:17:03 +0200198 nl80211_send_scan_done(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700199 }
Johannes Berg2a519312009-02-10 21:25:55 +0100200
Johannes Berg3d23e342009-09-29 23:27:28 +0200201#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200202 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100203 memset(&wrqu, 0, sizeof(wrqu));
204
Johannes Bergfd014282012-06-18 19:17:03 +0200205 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100206 }
207#endif
208
Johannes Bergfd014282012-06-18 19:17:03 +0200209 if (wdev->netdev)
210 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100211
Johannes Berg36e6fea2009-08-12 22:21:21 +0200212 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200213
214 /*
215 * OK. If this is invoked with "leak" then we can't
216 * free this ... but we've cleaned it up anyway. The
217 * driver failed to call the scan_done callback, so
218 * all bets are off, it might still be trying to use
219 * the scan request or not ... if it accesses the dev
220 * in there (it shouldn't anyway) then it may crash.
221 */
222 if (!leak)
223 kfree(request);
Johannes Berg2a519312009-02-10 21:25:55 +0100224}
Johannes Berg667503dd2009-07-07 03:56:11 +0200225
Johannes Berg36e6fea2009-08-12 22:21:21 +0200226void __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
Johannes Berg5fe231e2013-05-08 21:45:15 +0200233 rtnl_lock();
Johannes Berg01a0ac42009-08-20 21:36:16 +0200234 ___cfg80211_scan_done(rdev, false);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200235 rtnl_unlock();
Johannes Berg36e6fea2009-08-12 22:21:21 +0200236}
237
Johannes Berg667503dd2009-07-07 03:56:11 +0200238void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
239{
Beni Lev4ee3e062012-08-27 12:49:39 +0300240 trace_cfg80211_scan_done(request, aborted);
Johannes Berg667503dd2009-07-07 03:56:11 +0200241 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
242
243 request->aborted = aborted;
Johannes Berg5fe231e2013-05-08 21:45:15 +0200244 request->notified = true;
Alban Browaeyse60d7442009-11-25 15:13:00 +0100245 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503dd2009-07-07 03:56:11 +0200246}
Johannes Berg2a519312009-02-10 21:25:55 +0100247EXPORT_SYMBOL(cfg80211_scan_done);
248
Luciano Coelho807f8a82011-05-11 17:09:35 +0300249void __cfg80211_sched_scan_results(struct work_struct *wk)
250{
251 struct cfg80211_registered_device *rdev;
Sam Leffler15d60302012-10-11 21:03:34 -0700252 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300253
254 rdev = container_of(wk, struct cfg80211_registered_device,
255 sched_scan_results_wk);
256
Sam Leffler15d60302012-10-11 21:03:34 -0700257 request = rdev->sched_scan_req;
258
Johannes Berg5fe231e2013-05-08 21:45:15 +0200259 rtnl_lock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300260
261 /* we don't have sched_scan_req anymore if the scan is stopping */
Sam Leffler15d60302012-10-11 21:03:34 -0700262 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 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300273
Johannes Berg5fe231e2013-05-08 21:45:15 +0200274 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300275}
276
277void cfg80211_sched_scan_results(struct wiphy *wiphy)
278{
Beni Lev4ee3e062012-08-27 12:49:39 +0300279 trace_cfg80211_sched_scan_results(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300280 /* 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}
285EXPORT_SYMBOL(cfg80211_sched_scan_results);
286
Luciano Coelho85a99942011-05-12 16:28:29 +0300287void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300288{
Luciano Coelho85a99942011-05-12 16:28:29 +0300289 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300290
Beni Lev4ee3e062012-08-27 12:49:39 +0300291 trace_cfg80211_sched_scan_stopped(wiphy);
292
Johannes Berg5fe231e2013-05-08 21:45:15 +0200293 rtnl_lock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300294 __cfg80211_stop_sched_scan(rdev, true);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200295 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300296}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300297EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
298
299int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
300 bool driver_initiated)
301{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300302 struct net_device *dev;
303
Johannes Berg5fe231e2013-05-08 21:45:15 +0200304 ASSERT_RTNL();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300305
306 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300307 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300308
309 dev = rdev->sched_scan_req->dev;
310
Luciano Coelho85a99942011-05-12 16:28:29 +0300311 if (!driver_initiated) {
Hila Gonene35e4d22012-06-27 17:19:42 +0300312 int err = rdev_sched_scan_stop(rdev, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300313 if (err)
314 return err;
315 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300316
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
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200322 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300323}
324
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500325void 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
Johannes Berg2ca813a2013-02-01 01:04:14 +0100331 spin_lock_bh(&dev->bss_lock);
Johannes Berg915de2f2012-11-28 22:39:37 +0100332 list_for_each_entry(bss, &dev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500333 bss->ts -= age_jiffies;
Johannes Berg2ca813a2013-02-01 01:04:14 +0100334 spin_unlock_bh(&dev->bss_lock);
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500335}
336
Johannes Berg2a519312009-02-10 21:25:55 +0100337void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
338{
Sam Leffler15d60302012-10-11 21:03:34 -0700339 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100340}
341
Johannes Bergc21dbf92010-01-26 14:15:46 +0100342const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100343{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100344 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100345 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}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100354EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100355
Eliad Peller0c28ec52011-09-15 11:53:01 +0300356const 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
Eliad Peller0c28ec52011-09-15 11:53:01 +0300369 ie = (struct ieee80211_vendor_ie *)pos;
Luciano Coelho67194292013-02-12 20:11:38 +0200370
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
Eliad Peller0c28ec52011-09-15 11:53:01 +0300377 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;
Luciano Coelho67194292013-02-12 20:11:38 +0200380cont:
Eliad Peller0c28ec52011-09-15 11:53:01 +0300381 pos += 2 + ie->len;
382 }
383 return NULL;
384}
385EXPORT_SYMBOL(cfg80211_find_vendor_ie);
386
Johannes Berg915de2f2012-11-28 22:39:37 +0100387static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
Johannes Berg2a519312009-02-10 21:25:55 +0100388 const u8 *ssid, size_t ssid_len)
389{
Johannes Berg9caf0362012-11-29 01:25:20 +0100390 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100391 const u8 *ssidie;
392
Joe Perchesac422d32012-05-08 18:56:55 +0000393 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100394 return false;
395
Johannes Berg79420f02009-02-10 21:25:59 +0100396 if (!ssid)
397 return true;
398
Johannes Berg9caf0362012-11-29 01:25:20 +0100399 ies = rcu_access_pointer(a->ies);
400 if (!ies)
401 return false;
402 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100403 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
Johannes Berg4593c4c2013-02-01 19:20:03 +0100410/**
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 */
416enum bss_compare_mode {
417 BSS_CMP_REGULAR,
418 BSS_CMP_HIDE_ZLEN,
419 BSS_CMP_HIDE_NUL,
420};
421
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100422static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100423 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100424 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100425{
Johannes Berg9caf0362012-11-29 01:25:20 +0100426 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100427 const u8 *ie1 = NULL;
428 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100429 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100430
Johannes Berg3af63412013-01-30 00:40:20 +0100431 if (a->channel != b->channel)
432 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100433
Johannes Berg9caf0362012-11-29 01:25:20 +0100434 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
Johannes Berg3af63412013-01-30 00:40:20 +0100441 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 /*
469 * we can't use compare_ether_addr here since we need a < > operator.
470 * The binary return value of compare_ether_addr isn't enough
471 */
472 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
473 if (r)
474 return r;
475
Johannes Berg9caf0362012-11-29 01:25:20 +0100476 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
477 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100478
Johannes Berg5622f5b2013-01-30 00:26:45 +0100479 if (!ie1 && !ie2)
480 return 0;
481
Johannes Bergf94f8b12012-11-28 22:42:34 +0100482 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100483 * Note that with "hide_ssid", the function returns a match if
484 * the already-present BSS ("b") is a hidden SSID beacon for
485 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100486 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100487
488 /* sort missing IE before (left of) present IE */
489 if (!ie1)
490 return -1;
491 if (!ie2)
492 return 1;
493
Johannes Berg4593c4c2013-02-01 19:20:03 +0100494 switch (mode) {
495 case BSS_CMP_HIDE_ZLEN:
496 /*
497 * In ZLEN mode we assume the BSS entry we're
498 * looking for has a zero-length SSID. So if
499 * the one we're looking at right now has that,
500 * return 0. Otherwise, return the difference
501 * in length, but since we're looking for the
502 * 0-length it's really equivalent to returning
503 * the length of the one we're looking at.
504 *
505 * No content comparison is needed as we assume
506 * the content length is zero.
507 */
508 return ie2[1];
509 case BSS_CMP_REGULAR:
510 default:
511 /* sort by length first, then by contents */
512 if (ie1[1] != ie2[1])
513 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100514 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100515 case BSS_CMP_HIDE_NUL:
516 if (ie1[1] != ie2[1])
517 return ie2[1] - ie1[1];
518 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
519 for (i = 0; i < ie2[1]; i++)
520 if (ie2[i + 2])
521 return -1;
522 return 0;
523 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100524}
525
Ben Greear0e3a39b2013-06-19 14:06:27 -0700526/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100527struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
528 struct ieee80211_channel *channel,
529 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100530 const u8 *ssid, size_t ssid_len,
531 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100532{
533 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
534 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200535 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100536
Beni Lev4ee3e062012-08-27 12:49:39 +0300537 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
538 capa_val);
539
Johannes Berg2a519312009-02-10 21:25:55 +0100540 spin_lock_bh(&dev->bss_lock);
541
542 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100543 if ((bss->pub.capability & capa_mask) != capa_val)
544 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100545 if (channel && bss->pub.channel != channel)
546 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200547 /* Don't get expired BSS structs */
548 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
549 !atomic_read(&bss->hold))
550 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100551 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
552 res = bss;
Johannes Berg776b3582013-02-01 02:06:18 +0100553 bss_ref_get(dev, res);
Johannes Berg2a519312009-02-10 21:25:55 +0100554 break;
555 }
556 }
557
558 spin_unlock_bh(&dev->bss_lock);
559 if (!res)
560 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300561 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100562 return &res->pub;
563}
564EXPORT_SYMBOL(cfg80211_get_bss);
565
Johannes Berg2a519312009-02-10 21:25:55 +0100566static void rb_insert_bss(struct cfg80211_registered_device *dev,
567 struct cfg80211_internal_bss *bss)
568{
569 struct rb_node **p = &dev->bss_tree.rb_node;
570 struct rb_node *parent = NULL;
571 struct cfg80211_internal_bss *tbss;
572 int cmp;
573
574 while (*p) {
575 parent = *p;
576 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
577
Johannes Berg4593c4c2013-02-01 19:20:03 +0100578 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100579
580 if (WARN_ON(!cmp)) {
581 /* will sort of leak this BSS */
582 return;
583 }
584
585 if (cmp < 0)
586 p = &(*p)->rb_left;
587 else
588 p = &(*p)->rb_right;
589 }
590
591 rb_link_node(&bss->rbn, parent, p);
592 rb_insert_color(&bss->rbn, &dev->bss_tree);
593}
594
595static struct cfg80211_internal_bss *
596rb_find_bss(struct cfg80211_registered_device *dev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100597 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100598 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +0100599{
600 struct rb_node *n = dev->bss_tree.rb_node;
601 struct cfg80211_internal_bss *bss;
602 int r;
603
604 while (n) {
605 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100606 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100607
608 if (r == 0)
609 return bss;
610 else if (r < 0)
611 n = n->rb_left;
612 else
613 n = n->rb_right;
614 }
615
616 return NULL;
617}
618
Johannes Berg776b3582013-02-01 02:06:18 +0100619static bool cfg80211_combine_bsses(struct cfg80211_registered_device *dev,
620 struct cfg80211_internal_bss *new)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100621{
Johannes Berg9caf0362012-11-29 01:25:20 +0100622 const struct cfg80211_bss_ies *ies;
Johannes Berg776b3582013-02-01 02:06:18 +0100623 struct cfg80211_internal_bss *bss;
624 const u8 *ie;
625 int i, ssidlen;
626 u8 fold = 0;
Johannes Berg9caf0362012-11-29 01:25:20 +0100627
Johannes Berg776b3582013-02-01 02:06:18 +0100628 ies = rcu_access_pointer(new->pub.beacon_ies);
Johannes Berg9caf0362012-11-29 01:25:20 +0100629 if (WARN_ON(!ies))
Johannes Berg776b3582013-02-01 02:06:18 +0100630 return false;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100631
Johannes Berg776b3582013-02-01 02:06:18 +0100632 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
633 if (!ie) {
634 /* nothing to do */
635 return true;
636 }
637
638 ssidlen = ie[1];
639 for (i = 0; i < ssidlen; i++)
640 fold |= ie[2 + i];
641
642 if (fold) {
643 /* not a hidden SSID */
644 return true;
645 }
646
647 /* This is the bad part ... */
648
649 list_for_each_entry(bss, &dev->bss_list, list) {
650 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
651 continue;
652 if (bss->pub.channel != new->pub.channel)
653 continue;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200654 if (bss->pub.scan_width != new->pub.scan_width)
655 continue;
Johannes Berg776b3582013-02-01 02:06:18 +0100656 if (rcu_access_pointer(bss->pub.beacon_ies))
657 continue;
658 ies = rcu_access_pointer(bss->pub.ies);
659 if (!ies)
660 continue;
661 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
662 if (!ie)
663 continue;
664 if (ssidlen && ie[1] != ssidlen)
665 continue;
666 /* that would be odd ... */
667 if (bss->pub.beacon_ies)
668 continue;
669 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
670 continue;
671 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
672 list_del(&bss->hidden_list);
673 /* combine them */
674 list_add(&bss->hidden_list, &new->hidden_list);
675 bss->pub.hidden_beacon_bss = &new->pub;
676 new->refcount += bss->refcount;
677 rcu_assign_pointer(bss->pub.beacon_ies,
678 new->pub.beacon_ies);
679 }
680
681 return true;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100682}
683
Ben Greear0e3a39b2013-06-19 14:06:27 -0700684/* Returned bss is reference counted and must be cleaned up appropriately. */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100685static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100686cfg80211_bss_update(struct cfg80211_registered_device *dev,
Johannes Berg9caf0362012-11-29 01:25:20 +0100687 struct cfg80211_internal_bss *tmp)
Johannes Berg2a519312009-02-10 21:25:55 +0100688{
689 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100690
Johannes Berg9caf0362012-11-29 01:25:20 +0100691 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +0100692 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100693
Johannes Berg9caf0362012-11-29 01:25:20 +0100694 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100695
Johannes Berg2a519312009-02-10 21:25:55 +0100696 spin_lock_bh(&dev->bss_lock);
697
Johannes Berg9caf0362012-11-29 01:25:20 +0100698 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
699 spin_unlock_bh(&dev->bss_lock);
700 return NULL;
701 }
702
Johannes Berg4593c4c2013-02-01 19:20:03 +0100703 found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100704
Johannes Bergcd1658f2009-04-16 15:00:58 +0200705 if (found) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200706 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100707 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
708 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200709
Johannes Berg9caf0362012-11-29 01:25:20 +0100710 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200711
Johannes Berg9caf0362012-11-29 01:25:20 +0100712 rcu_assign_pointer(found->pub.proberesp_ies,
713 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200714 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100715 rcu_assign_pointer(found->pub.ies,
716 tmp->pub.proberesp_ies);
717 if (old)
718 kfree_rcu((struct cfg80211_bss_ies *)old,
719 rcu_head);
720 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
Johannes Berg9537f222013-02-01 01:19:48 +0100721 const struct cfg80211_bss_ies *old;
Johannes Berg776b3582013-02-01 02:06:18 +0100722 struct cfg80211_internal_bss *bss;
723
724 if (found->pub.hidden_beacon_bss &&
725 !list_empty(&found->hidden_list)) {
Johannes Berg1345ee62013-03-06 10:31:05 +0100726 const struct cfg80211_bss_ies *f;
727
Johannes Berg776b3582013-02-01 02:06:18 +0100728 /*
729 * The found BSS struct is one of the probe
730 * response members of a group, but we're
731 * receiving a beacon (beacon_ies in the tmp
732 * bss is used). This can only mean that the
733 * AP changed its beacon from not having an
734 * SSID to showing it, which is confusing so
735 * drop this information.
736 */
Johannes Berg1345ee62013-03-06 10:31:05 +0100737
738 f = rcu_access_pointer(tmp->pub.beacon_ies);
739 kfree_rcu((struct cfg80211_bss_ies *)f,
740 rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +0100741 goto drop;
742 }
Johannes Berg915de2f2012-11-28 22:39:37 +0100743
Johannes Berg9caf0362012-11-29 01:25:20 +0100744 old = rcu_access_pointer(found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200745
Johannes Berg9caf0362012-11-29 01:25:20 +0100746 rcu_assign_pointer(found->pub.beacon_ies,
747 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +0100748
749 /* Override IEs if they were from a beacon before */
Johannes Berg9537f222013-02-01 01:19:48 +0100750 if (old == rcu_access_pointer(found->pub.ies))
Johannes Berg9caf0362012-11-29 01:25:20 +0100751 rcu_assign_pointer(found->pub.ies,
752 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200753
Johannes Berg776b3582013-02-01 02:06:18 +0100754 /* Assign beacon IEs to all sub entries */
755 list_for_each_entry(bss, &found->hidden_list,
756 hidden_list) {
757 const struct cfg80211_bss_ies *ies;
758
759 ies = rcu_access_pointer(bss->pub.beacon_ies);
760 WARN_ON(ies != old);
761
762 rcu_assign_pointer(bss->pub.beacon_ies,
763 tmp->pub.beacon_ies);
764 }
765
Johannes Berg9caf0362012-11-29 01:25:20 +0100766 if (old)
767 kfree_rcu((struct cfg80211_bss_ies *)old,
768 rcu_head);
769 }
Johannes Berg1345ee62013-03-06 10:31:05 +0100770
771 found->pub.beacon_interval = tmp->pub.beacon_interval;
772 found->pub.signal = tmp->pub.signal;
773 found->pub.capability = tmp->pub.capability;
774 found->ts = tmp->ts;
Johannes Berg2a519312009-02-10 21:25:55 +0100775 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +0100776 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100777 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +0100778 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100779
Johannes Berg9caf0362012-11-29 01:25:20 +0100780 /*
781 * create a copy -- the "res" variable that is passed in
782 * is allocated on the stack since it's not needed in the
783 * more common case of an update
784 */
785 new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
786 GFP_ATOMIC);
787 if (!new) {
788 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
789 if (ies)
790 kfree_rcu(ies, rcu_head);
791 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
792 if (ies)
793 kfree_rcu(ies, rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +0100794 goto drop;
Johannes Berg9caf0362012-11-29 01:25:20 +0100795 }
796 memcpy(new, tmp, sizeof(*new));
Johannes Berg776b3582013-02-01 02:06:18 +0100797 new->refcount = 1;
798 INIT_LIST_HEAD(&new->hidden_list);
799
800 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
801 hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
802 if (!hidden)
803 hidden = rb_find_bss(dev, tmp,
804 BSS_CMP_HIDE_NUL);
805 if (hidden) {
806 new->pub.hidden_beacon_bss = &hidden->pub;
807 list_add(&new->hidden_list,
808 &hidden->hidden_list);
809 hidden->refcount++;
810 rcu_assign_pointer(new->pub.beacon_ies,
811 hidden->pub.beacon_ies);
812 }
813 } else {
814 /*
815 * Ok so we found a beacon, and don't have an entry. If
816 * it's a beacon with hidden SSID, we might be in for an
817 * expensive search for any probe responses that should
818 * be grouped with this beacon for updates ...
819 */
820 if (!cfg80211_combine_bsses(dev, new)) {
821 kfree(new);
822 goto drop;
823 }
824 }
825
Johannes Berg9caf0362012-11-29 01:25:20 +0100826 list_add_tail(&new->list, &dev->bss_list);
827 rb_insert_bss(dev, new);
828 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +0100829 }
830
831 dev->bss_generation++;
Johannes Berg776b3582013-02-01 02:06:18 +0100832 bss_ref_get(dev, found);
Johannes Berg2a519312009-02-10 21:25:55 +0100833 spin_unlock_bh(&dev->bss_lock);
834
Johannes Berg2a519312009-02-10 21:25:55 +0100835 return found;
Johannes Berg776b3582013-02-01 02:06:18 +0100836 drop:
837 spin_unlock_bh(&dev->bss_lock);
838 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100839}
840
Johannes Berg0172bb72012-11-23 14:23:30 +0100841static struct ieee80211_channel *
842cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
843 struct ieee80211_channel *channel)
844{
845 const u8 *tmp;
846 u32 freq;
847 int channel_number = -1;
848
849 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
850 if (tmp && tmp[1] == 1) {
851 channel_number = tmp[2];
852 } else {
853 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
854 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
855 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
856
857 channel_number = htop->primary_chan;
858 }
859 }
860
861 if (channel_number < 0)
862 return channel;
863
864 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
865 channel = ieee80211_get_channel(wiphy, freq);
866 if (!channel)
867 return NULL;
868 if (channel->flags & IEEE80211_CHAN_DISABLED)
869 return NULL;
870 return channel;
871}
872
Ben Greear0e3a39b2013-06-19 14:06:27 -0700873/* Returned bss is reference counted and must be cleaned up appropriately. */
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200874struct cfg80211_bss*
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200875cfg80211_inform_bss_width(struct wiphy *wiphy,
876 struct ieee80211_channel *channel,
877 enum nl80211_bss_scan_width scan_width,
878 const u8 *bssid, u64 tsf, u16 capability,
879 u16 beacon_interval, const u8 *ie, size_t ielen,
880 s32 signal, gfp_t gfp)
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200881{
Johannes Berg9caf0362012-11-29 01:25:20 +0100882 struct cfg80211_bss_ies *ies;
883 struct cfg80211_internal_bss tmp = {}, *res;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200884
885 if (WARN_ON(!wiphy))
886 return NULL;
887
Sujith22fe88d2010-05-13 10:34:08 +0530888 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200889 (signal < 0 || signal > 100)))
890 return NULL;
891
Johannes Berg0172bb72012-11-23 14:23:30 +0100892 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
893 if (!channel)
894 return NULL;
895
Johannes Berg9caf0362012-11-29 01:25:20 +0100896 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
897 tmp.pub.channel = channel;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200898 tmp.pub.scan_width = scan_width;
Johannes Berg9caf0362012-11-29 01:25:20 +0100899 tmp.pub.signal = signal;
Johannes Berg9caf0362012-11-29 01:25:20 +0100900 tmp.pub.beacon_interval = beacon_interval;
901 tmp.pub.capability = capability;
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200902 /*
903 * Since we do not know here whether the IEs are from a Beacon or Probe
904 * Response frame, we need to pick one of the options and only use it
905 * with the driver that does not provide the full Beacon/Probe Response
906 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg50521aa2013-01-30 21:33:19 +0100907 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +0100908 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200909 */
Johannes Berg9caf0362012-11-29 01:25:20 +0100910 ies = kmalloc(sizeof(*ies) + ielen, gfp);
911 if (!ies)
912 return NULL;
913 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100914 ies->tsf = tsf;
Johannes Berg9caf0362012-11-29 01:25:20 +0100915 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200916
Johannes Berg9caf0362012-11-29 01:25:20 +0100917 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
918 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200919
Johannes Berg9caf0362012-11-29 01:25:20 +0100920 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200921 if (!res)
922 return NULL;
923
924 if (res->pub.capability & WLAN_CAPABILITY_ESS)
925 regulatory_hint_found_beacon(wiphy, channel, gfp);
926
Beni Lev4ee3e062012-08-27 12:49:39 +0300927 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200928 /* cfg80211_bss_update gives us a referenced result */
929 return &res->pub;
930}
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200931EXPORT_SYMBOL(cfg80211_inform_bss_width);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200932
Ben Greear0e3a39b2013-06-19 14:06:27 -0700933/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100934struct cfg80211_bss *
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200935cfg80211_inform_bss_width_frame(struct wiphy *wiphy,
936 struct ieee80211_channel *channel,
937 enum nl80211_bss_scan_width scan_width,
938 struct ieee80211_mgmt *mgmt, size_t len,
939 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100940{
Johannes Berg9caf0362012-11-29 01:25:20 +0100941 struct cfg80211_internal_bss tmp = {}, *res;
942 struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100943 size_t ielen = len - offsetof(struct ieee80211_mgmt,
944 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100945
Johannes Berg0172bb72012-11-23 14:23:30 +0100946 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
947 offsetof(struct ieee80211_mgmt, u.beacon.variable));
948
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200949 trace_cfg80211_inform_bss_width_frame(wiphy, channel, scan_width, mgmt,
950 len, signal);
Beni Lev4ee3e062012-08-27 12:49:39 +0300951
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100952 if (WARN_ON(!mgmt))
953 return NULL;
954
955 if (WARN_ON(!wiphy))
956 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100957
Sujith22fe88d2010-05-13 10:34:08 +0530958 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300959 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100960 return NULL;
961
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100962 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100963 return NULL;
964
Johannes Berg0172bb72012-11-23 14:23:30 +0100965 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
966 ielen, channel);
967 if (!channel)
968 return NULL;
969
Johannes Berg9caf0362012-11-29 01:25:20 +0100970 ies = kmalloc(sizeof(*ies) + ielen, gfp);
971 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +0100972 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100973 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100974 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
Johannes Berg9caf0362012-11-29 01:25:20 +0100975 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +0100976
Johannes Berg9caf0362012-11-29 01:25:20 +0100977 if (ieee80211_is_probe_resp(mgmt->frame_control))
978 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
979 else
980 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
981 rcu_assign_pointer(tmp.pub.ies, ies);
982
983 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
984 tmp.pub.channel = channel;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200985 tmp.pub.scan_width = scan_width;
Johannes Berg9caf0362012-11-29 01:25:20 +0100986 tmp.pub.signal = signal;
Johannes Berg9caf0362012-11-29 01:25:20 +0100987 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
988 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Johannes Berg2a519312009-02-10 21:25:55 +0100989
Johannes Berg9caf0362012-11-29 01:25:20 +0100990 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Johannes Berg2a519312009-02-10 21:25:55 +0100991 if (!res)
992 return NULL;
993
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500994 if (res->pub.capability & WLAN_CAPABILITY_ESS)
995 regulatory_hint_found_beacon(wiphy, channel, gfp);
996
Beni Lev4ee3e062012-08-27 12:49:39 +0300997 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100998 /* cfg80211_bss_update gives us a referenced result */
999 return &res->pub;
1000}
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02001001EXPORT_SYMBOL(cfg80211_inform_bss_width_frame);
Johannes Berg2a519312009-02-10 21:25:55 +01001002
Johannes Berg5b112d32013-02-01 01:49:58 +01001003void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001004{
Johannes Berg776b3582013-02-01 02:06:18 +01001005 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001006 struct cfg80211_internal_bss *bss;
1007
1008 if (!pub)
1009 return;
1010
1011 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001012
1013 spin_lock_bh(&dev->bss_lock);
1014 bss_ref_get(dev, bss);
1015 spin_unlock_bh(&dev->bss_lock);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001016}
1017EXPORT_SYMBOL(cfg80211_ref_bss);
1018
Johannes Berg5b112d32013-02-01 01:49:58 +01001019void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg2a519312009-02-10 21:25:55 +01001020{
Johannes Berg776b3582013-02-01 02:06:18 +01001021 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001022 struct cfg80211_internal_bss *bss;
1023
1024 if (!pub)
1025 return;
1026
1027 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001028
1029 spin_lock_bh(&dev->bss_lock);
1030 bss_ref_put(dev, bss);
1031 spin_unlock_bh(&dev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001032}
1033EXPORT_SYMBOL(cfg80211_put_bss);
1034
Johannes Bergd491af12009-02-10 21:25:58 +01001035void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1036{
1037 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
1038 struct cfg80211_internal_bss *bss;
1039
1040 if (WARN_ON(!pub))
1041 return;
1042
1043 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1044
1045 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +02001046 if (!list_empty(&bss->list)) {
Johannes Berg776b3582013-02-01 02:06:18 +01001047 if (__cfg80211_unlink_bss(dev, bss))
1048 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +02001049 }
Johannes Bergd491af12009-02-10 21:25:58 +01001050 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +01001051}
1052EXPORT_SYMBOL(cfg80211_unlink_bss);
1053
Johannes Berg3d23e342009-09-29 23:27:28 +02001054#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9f419f32013-05-08 21:34:22 +02001055static struct cfg80211_registered_device *
1056cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1057{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001058 struct cfg80211_registered_device *rdev;
Johannes Berg9f419f32013-05-08 21:34:22 +02001059 struct net_device *dev;
1060
Johannes Berg5fe231e2013-05-08 21:45:15 +02001061 ASSERT_RTNL();
1062
Johannes Berg9f419f32013-05-08 21:34:22 +02001063 dev = dev_get_by_index(net, ifindex);
1064 if (!dev)
Johannes Berg5fe231e2013-05-08 21:45:15 +02001065 return ERR_PTR(-ENODEV);
1066 if (dev->ieee80211_ptr)
Johannes Berg9f419f32013-05-08 21:34:22 +02001067 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001068 else
Johannes Berg9f419f32013-05-08 21:34:22 +02001069 rdev = ERR_PTR(-ENODEV);
1070 dev_put(dev);
Johannes Berg9f419f32013-05-08 21:34:22 +02001071 return rdev;
1072}
1073
Johannes Berg2a519312009-02-10 21:25:55 +01001074int cfg80211_wext_siwscan(struct net_device *dev,
1075 struct iw_request_info *info,
1076 union iwreq_data *wrqu, char *extra)
1077{
1078 struct cfg80211_registered_device *rdev;
1079 struct wiphy *wiphy;
1080 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001081 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001082 int i, err, n_channels = 0;
1083 enum ieee80211_band band;
1084
1085 if (!netif_running(dev))
1086 return -ENETDOWN;
1087
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001088 if (wrqu->data.length == sizeof(struct iw_scan_req))
1089 wreq = (struct iw_scan_req *)extra;
1090
Johannes Berg463d0182009-07-14 00:33:35 +02001091 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001092
1093 if (IS_ERR(rdev))
1094 return PTR_ERR(rdev);
1095
1096 if (rdev->scan_req) {
1097 err = -EBUSY;
1098 goto out;
1099 }
1100
1101 wiphy = &rdev->wiphy;
1102
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001103 /* Determine number of channels, needed to allocate creq */
1104 if (wreq && wreq->num_channels)
1105 n_channels = wreq->num_channels;
1106 else {
1107 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1108 if (wiphy->bands[band])
1109 n_channels += wiphy->bands[band]->n_channels;
1110 }
Johannes Berg2a519312009-02-10 21:25:55 +01001111
1112 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1113 n_channels * sizeof(void *),
1114 GFP_ATOMIC);
1115 if (!creq) {
1116 err = -ENOMEM;
1117 goto out;
1118 }
1119
1120 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +02001121 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02001122 /* SSIDs come after channels */
1123 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01001124 creq->n_channels = n_channels;
1125 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07001126 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001127
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001128 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01001129 i = 0;
1130 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1131 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01001132
Johannes Berg2a519312009-02-10 21:25:55 +01001133 if (!wiphy->bands[band])
1134 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01001135
Johannes Berg2a519312009-02-10 21:25:55 +01001136 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001137 /* ignore disabled channels */
1138 if (wiphy->bands[band]->channels[j].flags &
1139 IEEE80211_CHAN_DISABLED)
1140 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001141
1142 /* If we have a wireless request structure and the
1143 * wireless request specifies frequencies, then search
1144 * for the matching hardware channel.
1145 */
1146 if (wreq && wreq->num_channels) {
1147 int k;
1148 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1149 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +02001150 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001151 if (wext_freq == wiphy_freq)
1152 goto wext_freq_found;
1153 }
1154 goto wext_freq_not_found;
1155 }
1156
1157 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001158 creq->channels[i] = &wiphy->bands[band]->channels[j];
1159 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001160 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001161 }
1162 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001163 /* No channels found? */
1164 if (!i) {
1165 err = -EINVAL;
1166 goto out;
1167 }
Johannes Berg2a519312009-02-10 21:25:55 +01001168
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001169 /* Set real number of channels specified in creq->channels[] */
1170 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001171
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001172 /* translate "Scan for SSID" request */
1173 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001174 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001175 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1176 err = -EINVAL;
1177 goto out;
1178 }
Johannes Berg2a519312009-02-10 21:25:55 +01001179 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1180 creq->ssids[0].ssid_len = wreq->essid_len;
1181 }
1182 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1183 creq->n_ssids = 0;
1184 }
1185
Johannes Berg34850ab2011-07-18 18:08:35 +02001186 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001187 if (wiphy->bands[i])
1188 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001189
Johannes Berg2a519312009-02-10 21:25:55 +01001190 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001191 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001192 if (err) {
1193 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001194 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001195 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001196 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001197 /* creq now owned by driver */
1198 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001199 dev_hold(dev);
1200 }
Johannes Berg2a519312009-02-10 21:25:55 +01001201 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001202 kfree(creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001203 return err;
1204}
Johannes Bergba44cb72009-04-20 18:49:39 +02001205EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001206
1207static void ieee80211_scan_add_ies(struct iw_request_info *info,
Johannes Berg9caf0362012-11-29 01:25:20 +01001208 const struct cfg80211_bss_ies *ies,
Johannes Berg2a519312009-02-10 21:25:55 +01001209 char **current_ev, char *end_buf)
1210{
Johannes Berg9caf0362012-11-29 01:25:20 +01001211 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01001212 struct iw_event iwe;
1213
Johannes Berg9caf0362012-11-29 01:25:20 +01001214 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001215 return;
1216
1217 /*
1218 * If needed, fragment the IEs buffer (at IE boundaries) into short
1219 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1220 */
Johannes Berg9caf0362012-11-29 01:25:20 +01001221 pos = ies->data;
1222 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001223
1224 while (end - pos > IW_GENERIC_IE_MAX) {
1225 next = pos + 2 + pos[1];
1226 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1227 next = next + 2 + next[1];
1228
1229 memset(&iwe, 0, sizeof(iwe));
1230 iwe.cmd = IWEVGENIE;
1231 iwe.u.data.length = next - pos;
1232 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001233 end_buf, &iwe,
1234 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001235
1236 pos = next;
1237 }
1238
1239 if (end > pos) {
1240 memset(&iwe, 0, sizeof(iwe));
1241 iwe.cmd = IWEVGENIE;
1242 iwe.u.data.length = end - pos;
1243 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001244 end_buf, &iwe,
1245 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001246 }
1247}
1248
Johannes Berg2a519312009-02-10 21:25:55 +01001249static char *
Johannes Berg77965c92009-02-18 18:45:06 +01001250ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1251 struct cfg80211_internal_bss *bss, char *current_ev,
1252 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001253{
Johannes Berg9caf0362012-11-29 01:25:20 +01001254 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01001255 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01001256 const u8 *ie;
Johannes Berg2a519312009-02-10 21:25:55 +01001257 u8 *buf, *cfg, *p;
Johannes Berg9caf0362012-11-29 01:25:20 +01001258 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001259 bool ismesh = false;
1260
1261 memset(&iwe, 0, sizeof(iwe));
1262 iwe.cmd = SIOCGIWAP;
1263 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1264 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1265 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1266 IW_EV_ADDR_LEN);
1267
1268 memset(&iwe, 0, sizeof(iwe));
1269 iwe.cmd = SIOCGIWFREQ;
1270 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1271 iwe.u.freq.e = 0;
1272 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1273 IW_EV_FREQ_LEN);
1274
1275 memset(&iwe, 0, sizeof(iwe));
1276 iwe.cmd = SIOCGIWFREQ;
1277 iwe.u.freq.m = bss->pub.channel->center_freq;
1278 iwe.u.freq.e = 6;
1279 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1280 IW_EV_FREQ_LEN);
1281
Johannes Berg77965c92009-02-18 18:45:06 +01001282 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01001283 memset(&iwe, 0, sizeof(iwe));
1284 iwe.cmd = IWEVQUAL;
1285 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1286 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01001287 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +01001288 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01001289 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01001290 sig = bss->pub.signal / 100;
1291 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001292 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01001293 if (sig < -110) /* rather bad */
1294 sig = -110;
1295 else if (sig > -40) /* perfect */
1296 sig = -40;
1297 /* will give a range of 0 .. 70 */
1298 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01001299 break;
1300 case CFG80211_SIGNAL_TYPE_UNSPEC:
1301 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01001302 /* will give range 0 .. 100 */
1303 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01001304 break;
1305 default:
1306 /* not reached */
1307 break;
1308 }
1309 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1310 &iwe, IW_EV_QUAL_LEN);
1311 }
1312
1313 memset(&iwe, 0, sizeof(iwe));
1314 iwe.cmd = SIOCGIWENCODE;
1315 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1316 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1317 else
1318 iwe.u.data.flags = IW_ENCODE_DISABLED;
1319 iwe.u.data.length = 0;
1320 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1321 &iwe, "");
1322
Johannes Berg9caf0362012-11-29 01:25:20 +01001323 rcu_read_lock();
1324 ies = rcu_dereference(bss->pub.ies);
Johannes Berg83c7aa12013-02-05 16:51:29 +01001325 rem = ies->len;
1326 ie = ies->data;
Johannes Berg9caf0362012-11-29 01:25:20 +01001327
Johannes Berg83c7aa12013-02-05 16:51:29 +01001328 while (rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01001329 /* invalid data */
1330 if (ie[1] > rem - 2)
1331 break;
1332
1333 switch (ie[0]) {
1334 case WLAN_EID_SSID:
1335 memset(&iwe, 0, sizeof(iwe));
1336 iwe.cmd = SIOCGIWESSID;
1337 iwe.u.data.length = ie[1];
1338 iwe.u.data.flags = 1;
1339 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001340 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001341 break;
1342 case WLAN_EID_MESH_ID:
1343 memset(&iwe, 0, sizeof(iwe));
1344 iwe.cmd = SIOCGIWESSID;
1345 iwe.u.data.length = ie[1];
1346 iwe.u.data.flags = 1;
1347 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001348 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001349 break;
1350 case WLAN_EID_MESH_CONFIG:
1351 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001352 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001353 break;
1354 buf = kmalloc(50, GFP_ATOMIC);
1355 if (!buf)
1356 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01001357 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01001358 memset(&iwe, 0, sizeof(iwe));
1359 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001360 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1361 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001362 iwe.u.data.length = strlen(buf);
1363 current_ev = iwe_stream_add_point(info, current_ev,
1364 end_buf,
1365 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001366 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1367 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001368 iwe.u.data.length = strlen(buf);
1369 current_ev = iwe_stream_add_point(info, current_ev,
1370 end_buf,
1371 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001372 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1373 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001374 iwe.u.data.length = strlen(buf);
1375 current_ev = iwe_stream_add_point(info, current_ev,
1376 end_buf,
1377 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001378 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01001379 iwe.u.data.length = strlen(buf);
1380 current_ev = iwe_stream_add_point(info, current_ev,
1381 end_buf,
1382 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001383 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1384 iwe.u.data.length = strlen(buf);
1385 current_ev = iwe_stream_add_point(info, current_ev,
1386 end_buf,
1387 &iwe, buf);
1388 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1389 iwe.u.data.length = strlen(buf);
1390 current_ev = iwe_stream_add_point(info, current_ev,
1391 end_buf,
1392 &iwe, buf);
1393 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001394 iwe.u.data.length = strlen(buf);
1395 current_ev = iwe_stream_add_point(info, current_ev,
1396 end_buf,
1397 &iwe, buf);
1398 kfree(buf);
1399 break;
1400 case WLAN_EID_SUPP_RATES:
1401 case WLAN_EID_EXT_SUPP_RATES:
1402 /* display all supported rates in readable format */
1403 p = current_ev + iwe_stream_lcp_len(info);
1404
1405 memset(&iwe, 0, sizeof(iwe));
1406 iwe.cmd = SIOCGIWRATE;
1407 /* Those two flags are ignored... */
1408 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1409
1410 for (i = 0; i < ie[1]; i++) {
1411 iwe.u.bitrate.value =
1412 ((ie[i + 2] & 0x7f) * 500000);
1413 p = iwe_stream_add_value(info, current_ev, p,
1414 end_buf, &iwe, IW_EV_PARAM_LEN);
1415 }
1416 current_ev = p;
1417 break;
1418 }
1419 rem -= ie[1] + 2;
1420 ie += ie[1] + 2;
1421 }
1422
Joe Perchesf64f9e72009-11-29 16:55:45 -08001423 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1424 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001425 memset(&iwe, 0, sizeof(iwe));
1426 iwe.cmd = SIOCGIWMODE;
1427 if (ismesh)
1428 iwe.u.mode = IW_MODE_MESH;
1429 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1430 iwe.u.mode = IW_MODE_MASTER;
1431 else
1432 iwe.u.mode = IW_MODE_ADHOC;
1433 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1434 &iwe, IW_EV_UINT_LEN);
1435 }
1436
Dan Carpenterc49dc902013-01-24 09:40:00 +03001437 buf = kmalloc(31, GFP_ATOMIC);
Johannes Berg2a519312009-02-10 21:25:55 +01001438 if (buf) {
1439 memset(&iwe, 0, sizeof(iwe));
1440 iwe.cmd = IWEVCUSTOM;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001441 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
Johannes Berg2a519312009-02-10 21:25:55 +01001442 iwe.u.data.length = strlen(buf);
1443 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1444 &iwe, buf);
1445 memset(&iwe, 0, sizeof(iwe));
1446 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001447 sprintf(buf, " Last beacon: %ums ago",
1448 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001449 iwe.u.data.length = strlen(buf);
1450 current_ev = iwe_stream_add_point(info, current_ev,
1451 end_buf, &iwe, buf);
1452 kfree(buf);
1453 }
1454
Johannes Berg9caf0362012-11-29 01:25:20 +01001455 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1456 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01001457
1458 return current_ev;
1459}
1460
1461
1462static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1463 struct iw_request_info *info,
1464 char *buf, size_t len)
1465{
1466 char *current_ev = buf;
1467 char *end_buf = buf + len;
1468 struct cfg80211_internal_bss *bss;
1469
1470 spin_lock_bh(&dev->bss_lock);
1471 cfg80211_bss_expire(dev);
1472
1473 list_for_each_entry(bss, &dev->bss_list, list) {
1474 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1475 spin_unlock_bh(&dev->bss_lock);
1476 return -E2BIG;
1477 }
Johannes Berg77965c92009-02-18 18:45:06 +01001478 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1479 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001480 }
1481 spin_unlock_bh(&dev->bss_lock);
1482 return current_ev - buf;
1483}
1484
1485
1486int cfg80211_wext_giwscan(struct net_device *dev,
1487 struct iw_request_info *info,
1488 struct iw_point *data, char *extra)
1489{
1490 struct cfg80211_registered_device *rdev;
1491 int res;
1492
1493 if (!netif_running(dev))
1494 return -ENETDOWN;
1495
Johannes Berg463d0182009-07-14 00:33:35 +02001496 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001497
1498 if (IS_ERR(rdev))
1499 return PTR_ERR(rdev);
1500
Johannes Berg5fe231e2013-05-08 21:45:15 +02001501 if (rdev->scan_req)
1502 return -EAGAIN;
Johannes Berg2a519312009-02-10 21:25:55 +01001503
1504 res = ieee80211_scan_results(rdev, info, extra, data->length);
1505 data->length = 0;
1506 if (res >= 0) {
1507 data->length = res;
1508 res = 0;
1509 }
1510
Johannes Berg2a519312009-02-10 21:25:55 +01001511 return res;
1512}
Johannes Bergba44cb72009-04-20 18:49:39 +02001513EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001514#endif