blob: 88f108edfb586ef3b2d17d15cf66b00da84f93f0 [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 Bergf9d15d12014-01-22 11:14:19 +0200164void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
165 bool send_message)
Johannes Berg2a519312009-02-10 21:25:55 +0100166{
Johannes Berg667503dd2009-07-07 03:56:11 +0200167 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +0200168 struct wireless_dev *wdev;
Johannes Bergf9d15d12014-01-22 11:14:19 +0200169 struct sk_buff *msg;
Johannes Berg3d23e342009-09-29 23:27:28 +0200170#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100171 union iwreq_data wrqu;
172#endif
173
Johannes Berg5fe231e2013-05-08 21:45:15 +0200174 ASSERT_RTNL();
Johannes Berg01a0ac42009-08-20 21:36:16 +0200175
Johannes Bergf9d15d12014-01-22 11:14:19 +0200176 if (rdev->scan_msg) {
177 nl80211_send_scan_result(rdev, rdev->scan_msg);
178 rdev->scan_msg = NULL;
179 return;
180 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200181
Johannes Bergf9d15d12014-01-22 11:14:19 +0200182 request = rdev->scan_req;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200183 if (!request)
184 return;
185
Johannes Bergfd014282012-06-18 19:17:03 +0200186 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +0100187
Johannes Berg6829c872009-07-02 09:13:27 +0200188 /*
189 * This must be before sending the other events!
190 * Otherwise, wpa_supplicant gets completely confused with
191 * wext events.
192 */
Johannes Bergfd014282012-06-18 19:17:03 +0200193 if (wdev->netdev)
194 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200195
Johannes Bergf9d15d12014-01-22 11:14:19 +0200196 if (!request->aborted &&
197 request->flags & NL80211_SCAN_FLAG_FLUSH) {
198 /* flush entries from previous scans */
199 spin_lock_bh(&rdev->bss_lock);
200 __cfg80211_bss_expire(rdev, request->scan_start);
201 spin_unlock_bh(&rdev->bss_lock);
Sam Leffler15d60302012-10-11 21:03:34 -0700202 }
Johannes Berg2a519312009-02-10 21:25:55 +0100203
Johannes Bergf9d15d12014-01-22 11:14:19 +0200204 msg = nl80211_build_scan_msg(rdev, wdev, request->aborted);
205
Johannes Berg3d23e342009-09-29 23:27:28 +0200206#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200207 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100208 memset(&wrqu, 0, sizeof(wrqu));
209
Johannes Bergfd014282012-06-18 19:17:03 +0200210 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100211 }
212#endif
213
Johannes Bergfd014282012-06-18 19:17:03 +0200214 if (wdev->netdev)
215 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100216
Johannes Berg36e6fea2009-08-12 22:21:21 +0200217 rdev->scan_req = NULL;
Eliad Peller4a58e7c2013-12-05 18:30:17 +0200218 kfree(request);
Johannes Bergf9d15d12014-01-22 11:14:19 +0200219
220 if (!send_message)
221 rdev->scan_msg = msg;
222 else
223 nl80211_send_scan_result(rdev, msg);
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 Bergf9d15d12014-01-22 11:14:19 +0200234 ___cfg80211_scan_done(rdev, true);
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
Johannes Berg5fe231e2013-05-08 21:45:15 +0200257 rtnl_lock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300258
Johannes Berg79845c62013-10-21 11:33:35 +0200259 request = rdev->sched_scan_req;
260
Luciano Coelho807f8a82011-05-11 17:09:35 +0300261 /* 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
Eliad Peller792e6aa2014-04-30 16:14:23 +0300287void cfg80211_sched_scan_stopped_rtnl(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
Eliad Peller792e6aa2014-04-30 16:14:23 +0300291 ASSERT_RTNL();
292
Beni Lev4ee3e062012-08-27 12:49:39 +0300293 trace_cfg80211_sched_scan_stopped(wiphy);
294
Luciano Coelho807f8a82011-05-11 17:09:35 +0300295 __cfg80211_stop_sched_scan(rdev, true);
Eliad Peller792e6aa2014-04-30 16:14:23 +0300296}
297EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
298
299void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
300{
301 rtnl_lock();
302 cfg80211_sched_scan_stopped_rtnl(wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +0200303 rtnl_unlock();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300304}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300305EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
306
307int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
308 bool driver_initiated)
309{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300310 struct net_device *dev;
311
Johannes Berg5fe231e2013-05-08 21:45:15 +0200312 ASSERT_RTNL();
Luciano Coelho807f8a82011-05-11 17:09:35 +0300313
314 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300315 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300316
317 dev = rdev->sched_scan_req->dev;
318
Luciano Coelho85a99942011-05-12 16:28:29 +0300319 if (!driver_initiated) {
Hila Gonene35e4d22012-06-27 17:19:42 +0300320 int err = rdev_sched_scan_stop(rdev, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300321 if (err)
322 return err;
323 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300324
325 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
326
327 kfree(rdev->sched_scan_req);
328 rdev->sched_scan_req = NULL;
329
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200330 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300331}
332
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500333void cfg80211_bss_age(struct cfg80211_registered_device *dev,
334 unsigned long age_secs)
335{
336 struct cfg80211_internal_bss *bss;
337 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
338
Johannes Berg2ca813a2013-02-01 01:04:14 +0100339 spin_lock_bh(&dev->bss_lock);
Johannes Berg915de2f2012-11-28 22:39:37 +0100340 list_for_each_entry(bss, &dev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500341 bss->ts -= age_jiffies;
Johannes Berg2ca813a2013-02-01 01:04:14 +0100342 spin_unlock_bh(&dev->bss_lock);
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500343}
344
Johannes Berg2a519312009-02-10 21:25:55 +0100345void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
346{
Sam Leffler15d60302012-10-11 21:03:34 -0700347 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100348}
349
Johannes Bergc21dbf92010-01-26 14:15:46 +0100350const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100351{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100352 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100353 len -= ies[1] + 2;
354 ies += ies[1] + 2;
355 }
356 if (len < 2)
357 return NULL;
358 if (len < 2 + ies[1])
359 return NULL;
360 return ies;
361}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100362EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100363
Eliad Peller0c28ec52011-09-15 11:53:01 +0300364const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
365 const u8 *ies, int len)
366{
367 struct ieee80211_vendor_ie *ie;
368 const u8 *pos = ies, *end = ies + len;
369 int ie_oui;
370
371 while (pos < end) {
372 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
373 end - pos);
374 if (!pos)
375 return NULL;
376
Eliad Peller0c28ec52011-09-15 11:53:01 +0300377 ie = (struct ieee80211_vendor_ie *)pos;
Luciano Coelho67194292013-02-12 20:11:38 +0200378
379 /* make sure we can access ie->len */
380 BUILD_BUG_ON(offsetof(struct ieee80211_vendor_ie, len) != 1);
381
382 if (ie->len < sizeof(*ie))
383 goto cont;
384
Eliad Peller0c28ec52011-09-15 11:53:01 +0300385 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
386 if (ie_oui == oui && ie->oui_type == oui_type)
387 return pos;
Luciano Coelho67194292013-02-12 20:11:38 +0200388cont:
Eliad Peller0c28ec52011-09-15 11:53:01 +0300389 pos += 2 + ie->len;
390 }
391 return NULL;
392}
393EXPORT_SYMBOL(cfg80211_find_vendor_ie);
394
Johannes Berg915de2f2012-11-28 22:39:37 +0100395static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
Johannes Berg2a519312009-02-10 21:25:55 +0100396 const u8 *ssid, size_t ssid_len)
397{
Johannes Berg9caf0362012-11-29 01:25:20 +0100398 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100399 const u8 *ssidie;
400
Joe Perchesac422d32012-05-08 18:56:55 +0000401 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100402 return false;
403
Johannes Berg79420f02009-02-10 21:25:59 +0100404 if (!ssid)
405 return true;
406
Johannes Berg9caf0362012-11-29 01:25:20 +0100407 ies = rcu_access_pointer(a->ies);
408 if (!ies)
409 return false;
410 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100411 if (!ssidie)
412 return false;
413 if (ssidie[1] != ssid_len)
414 return false;
415 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
416}
417
Johannes Berg4593c4c2013-02-01 19:20:03 +0100418/**
419 * enum bss_compare_mode - BSS compare mode
420 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
421 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
422 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
423 */
424enum bss_compare_mode {
425 BSS_CMP_REGULAR,
426 BSS_CMP_HIDE_ZLEN,
427 BSS_CMP_HIDE_NUL,
428};
429
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100430static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100431 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100432 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100433{
Johannes Berg9caf0362012-11-29 01:25:20 +0100434 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100435 const u8 *ie1 = NULL;
436 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100437 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100438
Johannes Berg3af63412013-01-30 00:40:20 +0100439 if (a->channel != b->channel)
440 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100441
Johannes Berg9caf0362012-11-29 01:25:20 +0100442 a_ies = rcu_access_pointer(a->ies);
443 if (!a_ies)
444 return -1;
445 b_ies = rcu_access_pointer(b->ies);
446 if (!b_ies)
447 return 1;
448
Johannes Berg3af63412013-01-30 00:40:20 +0100449 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
450 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
451 a_ies->data, a_ies->len);
452 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
453 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
454 b_ies->data, b_ies->len);
455 if (ie1 && ie2) {
456 int mesh_id_cmp;
457
458 if (ie1[1] == ie2[1])
459 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
460 else
461 mesh_id_cmp = ie2[1] - ie1[1];
462
463 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
464 a_ies->data, a_ies->len);
465 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
466 b_ies->data, b_ies->len);
467 if (ie1 && ie2) {
468 if (mesh_id_cmp)
469 return mesh_id_cmp;
470 if (ie1[1] != ie2[1])
471 return ie2[1] - ie1[1];
472 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
473 }
474 }
475
Johannes Berg3af63412013-01-30 00:40:20 +0100476 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
477 if (r)
478 return r;
479
Johannes Berg9caf0362012-11-29 01:25:20 +0100480 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
481 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100482
Johannes Berg5622f5b2013-01-30 00:26:45 +0100483 if (!ie1 && !ie2)
484 return 0;
485
Johannes Bergf94f8b12012-11-28 22:42:34 +0100486 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100487 * Note that with "hide_ssid", the function returns a match if
488 * the already-present BSS ("b") is a hidden SSID beacon for
489 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100490 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100491
492 /* sort missing IE before (left of) present IE */
493 if (!ie1)
494 return -1;
495 if (!ie2)
496 return 1;
497
Johannes Berg4593c4c2013-02-01 19:20:03 +0100498 switch (mode) {
499 case BSS_CMP_HIDE_ZLEN:
500 /*
501 * In ZLEN mode we assume the BSS entry we're
502 * looking for has a zero-length SSID. So if
503 * the one we're looking at right now has that,
504 * return 0. Otherwise, return the difference
505 * in length, but since we're looking for the
506 * 0-length it's really equivalent to returning
507 * the length of the one we're looking at.
508 *
509 * No content comparison is needed as we assume
510 * the content length is zero.
511 */
512 return ie2[1];
513 case BSS_CMP_REGULAR:
514 default:
515 /* sort by length first, then by contents */
516 if (ie1[1] != ie2[1])
517 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100518 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100519 case BSS_CMP_HIDE_NUL:
520 if (ie1[1] != ie2[1])
521 return ie2[1] - ie1[1];
522 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
523 for (i = 0; i < ie2[1]; i++)
524 if (ie2[i + 2])
525 return -1;
526 return 0;
527 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100528}
529
Ben Greear0e3a39b2013-06-19 14:06:27 -0700530/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100531struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
532 struct ieee80211_channel *channel,
533 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100534 const u8 *ssid, size_t ssid_len,
535 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100536{
537 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
538 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200539 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100540
Beni Lev4ee3e062012-08-27 12:49:39 +0300541 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
542 capa_val);
543
Johannes Berg2a519312009-02-10 21:25:55 +0100544 spin_lock_bh(&dev->bss_lock);
545
546 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100547 if ((bss->pub.capability & capa_mask) != capa_val)
548 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100549 if (channel && bss->pub.channel != channel)
550 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200551 /* Don't get expired BSS structs */
552 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
553 !atomic_read(&bss->hold))
554 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100555 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
556 res = bss;
Johannes Berg776b3582013-02-01 02:06:18 +0100557 bss_ref_get(dev, res);
Johannes Berg2a519312009-02-10 21:25:55 +0100558 break;
559 }
560 }
561
562 spin_unlock_bh(&dev->bss_lock);
563 if (!res)
564 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300565 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100566 return &res->pub;
567}
568EXPORT_SYMBOL(cfg80211_get_bss);
569
Johannes Berg2a519312009-02-10 21:25:55 +0100570static void rb_insert_bss(struct cfg80211_registered_device *dev,
571 struct cfg80211_internal_bss *bss)
572{
573 struct rb_node **p = &dev->bss_tree.rb_node;
574 struct rb_node *parent = NULL;
575 struct cfg80211_internal_bss *tbss;
576 int cmp;
577
578 while (*p) {
579 parent = *p;
580 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
581
Johannes Berg4593c4c2013-02-01 19:20:03 +0100582 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100583
584 if (WARN_ON(!cmp)) {
585 /* will sort of leak this BSS */
586 return;
587 }
588
589 if (cmp < 0)
590 p = &(*p)->rb_left;
591 else
592 p = &(*p)->rb_right;
593 }
594
595 rb_link_node(&bss->rbn, parent, p);
596 rb_insert_color(&bss->rbn, &dev->bss_tree);
597}
598
599static struct cfg80211_internal_bss *
600rb_find_bss(struct cfg80211_registered_device *dev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100601 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100602 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +0100603{
604 struct rb_node *n = dev->bss_tree.rb_node;
605 struct cfg80211_internal_bss *bss;
606 int r;
607
608 while (n) {
609 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100610 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100611
612 if (r == 0)
613 return bss;
614 else if (r < 0)
615 n = n->rb_left;
616 else
617 n = n->rb_right;
618 }
619
620 return NULL;
621}
622
Johannes Berg776b3582013-02-01 02:06:18 +0100623static bool cfg80211_combine_bsses(struct cfg80211_registered_device *dev,
624 struct cfg80211_internal_bss *new)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100625{
Johannes Berg9caf0362012-11-29 01:25:20 +0100626 const struct cfg80211_bss_ies *ies;
Johannes Berg776b3582013-02-01 02:06:18 +0100627 struct cfg80211_internal_bss *bss;
628 const u8 *ie;
629 int i, ssidlen;
630 u8 fold = 0;
Johannes Berg9caf0362012-11-29 01:25:20 +0100631
Johannes Berg776b3582013-02-01 02:06:18 +0100632 ies = rcu_access_pointer(new->pub.beacon_ies);
Johannes Berg9caf0362012-11-29 01:25:20 +0100633 if (WARN_ON(!ies))
Johannes Berg776b3582013-02-01 02:06:18 +0100634 return false;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100635
Johannes Berg776b3582013-02-01 02:06:18 +0100636 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
637 if (!ie) {
638 /* nothing to do */
639 return true;
640 }
641
642 ssidlen = ie[1];
643 for (i = 0; i < ssidlen; i++)
644 fold |= ie[2 + i];
645
646 if (fold) {
647 /* not a hidden SSID */
648 return true;
649 }
650
651 /* This is the bad part ... */
652
653 list_for_each_entry(bss, &dev->bss_list, list) {
654 if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
655 continue;
656 if (bss->pub.channel != new->pub.channel)
657 continue;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200658 if (bss->pub.scan_width != new->pub.scan_width)
659 continue;
Johannes Berg776b3582013-02-01 02:06:18 +0100660 if (rcu_access_pointer(bss->pub.beacon_ies))
661 continue;
662 ies = rcu_access_pointer(bss->pub.ies);
663 if (!ies)
664 continue;
665 ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
666 if (!ie)
667 continue;
668 if (ssidlen && ie[1] != ssidlen)
669 continue;
Johannes Berg776b3582013-02-01 02:06:18 +0100670 if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
671 continue;
672 if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
673 list_del(&bss->hidden_list);
674 /* combine them */
675 list_add(&bss->hidden_list, &new->hidden_list);
676 bss->pub.hidden_beacon_bss = &new->pub;
677 new->refcount += bss->refcount;
678 rcu_assign_pointer(bss->pub.beacon_ies,
679 new->pub.beacon_ies);
680 }
681
682 return true;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100683}
684
Ben Greear0e3a39b2013-06-19 14:06:27 -0700685/* Returned bss is reference counted and must be cleaned up appropriately. */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100686static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100687cfg80211_bss_update(struct cfg80211_registered_device *dev,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200688 struct cfg80211_internal_bss *tmp,
689 bool signal_valid)
Johannes Berg2a519312009-02-10 21:25:55 +0100690{
691 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100692
Johannes Berg9caf0362012-11-29 01:25:20 +0100693 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +0100694 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100695
Johannes Berg9caf0362012-11-29 01:25:20 +0100696 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100697
Johannes Berg2a519312009-02-10 21:25:55 +0100698 spin_lock_bh(&dev->bss_lock);
699
Johannes Berg9caf0362012-11-29 01:25:20 +0100700 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
701 spin_unlock_bh(&dev->bss_lock);
702 return NULL;
703 }
704
Johannes Berg4593c4c2013-02-01 19:20:03 +0100705 found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100706
Johannes Bergcd1658f2009-04-16 15:00:58 +0200707 if (found) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200708 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100709 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
710 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200711
Johannes Berg9caf0362012-11-29 01:25:20 +0100712 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200713
Johannes Berg9caf0362012-11-29 01:25:20 +0100714 rcu_assign_pointer(found->pub.proberesp_ies,
715 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200716 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100717 rcu_assign_pointer(found->pub.ies,
718 tmp->pub.proberesp_ies);
719 if (old)
720 kfree_rcu((struct cfg80211_bss_ies *)old,
721 rcu_head);
722 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
Johannes Berg9537f222013-02-01 01:19:48 +0100723 const struct cfg80211_bss_ies *old;
Johannes Berg776b3582013-02-01 02:06:18 +0100724 struct cfg80211_internal_bss *bss;
725
726 if (found->pub.hidden_beacon_bss &&
727 !list_empty(&found->hidden_list)) {
Johannes Berg1345ee62013-03-06 10:31:05 +0100728 const struct cfg80211_bss_ies *f;
729
Johannes Berg776b3582013-02-01 02:06:18 +0100730 /*
731 * The found BSS struct is one of the probe
732 * response members of a group, but we're
733 * receiving a beacon (beacon_ies in the tmp
734 * bss is used). This can only mean that the
735 * AP changed its beacon from not having an
736 * SSID to showing it, which is confusing so
737 * drop this information.
738 */
Johannes Berg1345ee62013-03-06 10:31:05 +0100739
740 f = rcu_access_pointer(tmp->pub.beacon_ies);
741 kfree_rcu((struct cfg80211_bss_ies *)f,
742 rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +0100743 goto drop;
744 }
Johannes Berg915de2f2012-11-28 22:39:37 +0100745
Johannes Berg9caf0362012-11-29 01:25:20 +0100746 old = rcu_access_pointer(found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200747
Johannes Berg9caf0362012-11-29 01:25:20 +0100748 rcu_assign_pointer(found->pub.beacon_ies,
749 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +0100750
751 /* Override IEs if they were from a beacon before */
Johannes Berg9537f222013-02-01 01:19:48 +0100752 if (old == rcu_access_pointer(found->pub.ies))
Johannes Berg9caf0362012-11-29 01:25:20 +0100753 rcu_assign_pointer(found->pub.ies,
754 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200755
Johannes Berg776b3582013-02-01 02:06:18 +0100756 /* Assign beacon IEs to all sub entries */
757 list_for_each_entry(bss, &found->hidden_list,
758 hidden_list) {
759 const struct cfg80211_bss_ies *ies;
760
761 ies = rcu_access_pointer(bss->pub.beacon_ies);
762 WARN_ON(ies != old);
763
764 rcu_assign_pointer(bss->pub.beacon_ies,
765 tmp->pub.beacon_ies);
766 }
767
Johannes Berg9caf0362012-11-29 01:25:20 +0100768 if (old)
769 kfree_rcu((struct cfg80211_bss_ies *)old,
770 rcu_head);
771 }
Johannes Berg1345ee62013-03-06 10:31:05 +0100772
773 found->pub.beacon_interval = tmp->pub.beacon_interval;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200774 /*
775 * don't update the signal if beacon was heard on
776 * adjacent channel.
777 */
778 if (signal_valid)
779 found->pub.signal = tmp->pub.signal;
Johannes Berg1345ee62013-03-06 10:31:05 +0100780 found->pub.capability = tmp->pub.capability;
781 found->ts = tmp->ts;
Johannes Berg2a519312009-02-10 21:25:55 +0100782 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +0100783 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100784 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +0100785 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100786
Johannes Berg9caf0362012-11-29 01:25:20 +0100787 /*
788 * create a copy -- the "res" variable that is passed in
789 * is allocated on the stack since it's not needed in the
790 * more common case of an update
791 */
792 new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
793 GFP_ATOMIC);
794 if (!new) {
795 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
796 if (ies)
797 kfree_rcu(ies, rcu_head);
798 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
799 if (ies)
800 kfree_rcu(ies, rcu_head);
Johannes Berg776b3582013-02-01 02:06:18 +0100801 goto drop;
Johannes Berg9caf0362012-11-29 01:25:20 +0100802 }
803 memcpy(new, tmp, sizeof(*new));
Johannes Berg776b3582013-02-01 02:06:18 +0100804 new->refcount = 1;
805 INIT_LIST_HEAD(&new->hidden_list);
806
807 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
808 hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
809 if (!hidden)
810 hidden = rb_find_bss(dev, tmp,
811 BSS_CMP_HIDE_NUL);
812 if (hidden) {
813 new->pub.hidden_beacon_bss = &hidden->pub;
814 list_add(&new->hidden_list,
815 &hidden->hidden_list);
816 hidden->refcount++;
817 rcu_assign_pointer(new->pub.beacon_ies,
818 hidden->pub.beacon_ies);
819 }
820 } else {
821 /*
822 * Ok so we found a beacon, and don't have an entry. If
823 * it's a beacon with hidden SSID, we might be in for an
824 * expensive search for any probe responses that should
825 * be grouped with this beacon for updates ...
826 */
827 if (!cfg80211_combine_bsses(dev, new)) {
828 kfree(new);
829 goto drop;
830 }
831 }
832
Johannes Berg9caf0362012-11-29 01:25:20 +0100833 list_add_tail(&new->list, &dev->bss_list);
834 rb_insert_bss(dev, new);
835 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +0100836 }
837
838 dev->bss_generation++;
Johannes Berg776b3582013-02-01 02:06:18 +0100839 bss_ref_get(dev, found);
Johannes Berg2a519312009-02-10 21:25:55 +0100840 spin_unlock_bh(&dev->bss_lock);
841
Johannes Berg2a519312009-02-10 21:25:55 +0100842 return found;
Johannes Berg776b3582013-02-01 02:06:18 +0100843 drop:
844 spin_unlock_bh(&dev->bss_lock);
845 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100846}
847
Johannes Berg0172bb72012-11-23 14:23:30 +0100848static struct ieee80211_channel *
849cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
850 struct ieee80211_channel *channel)
851{
852 const u8 *tmp;
853 u32 freq;
854 int channel_number = -1;
855
856 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
857 if (tmp && tmp[1] == 1) {
858 channel_number = tmp[2];
859 } else {
860 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
861 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
862 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
863
864 channel_number = htop->primary_chan;
865 }
866 }
867
868 if (channel_number < 0)
869 return channel;
870
871 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
872 channel = ieee80211_get_channel(wiphy, freq);
873 if (!channel)
874 return NULL;
875 if (channel->flags & IEEE80211_CHAN_DISABLED)
876 return NULL;
877 return channel;
878}
879
Ben Greear0e3a39b2013-06-19 14:06:27 -0700880/* Returned bss is reference counted and must be cleaned up appropriately. */
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200881struct cfg80211_bss*
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200882cfg80211_inform_bss_width(struct wiphy *wiphy,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200883 struct ieee80211_channel *rx_channel,
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200884 enum nl80211_bss_scan_width scan_width,
885 const u8 *bssid, u64 tsf, u16 capability,
886 u16 beacon_interval, const u8 *ie, size_t ielen,
887 s32 signal, gfp_t gfp)
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200888{
Johannes Berg9caf0362012-11-29 01:25:20 +0100889 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200890 struct ieee80211_channel *channel;
Johannes Berg9caf0362012-11-29 01:25:20 +0100891 struct cfg80211_internal_bss tmp = {}, *res;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200892
893 if (WARN_ON(!wiphy))
894 return NULL;
895
Sujith22fe88d2010-05-13 10:34:08 +0530896 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200897 (signal < 0 || signal > 100)))
898 return NULL;
899
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200900 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, rx_channel);
Johannes Berg0172bb72012-11-23 14:23:30 +0100901 if (!channel)
902 return NULL;
903
Johannes Berg9caf0362012-11-29 01:25:20 +0100904 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
905 tmp.pub.channel = channel;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200906 tmp.pub.scan_width = scan_width;
Johannes Berg9caf0362012-11-29 01:25:20 +0100907 tmp.pub.signal = signal;
Johannes Berg9caf0362012-11-29 01:25:20 +0100908 tmp.pub.beacon_interval = beacon_interval;
909 tmp.pub.capability = capability;
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200910 /*
911 * Since we do not know here whether the IEs are from a Beacon or Probe
912 * Response frame, we need to pick one of the options and only use it
913 * with the driver that does not provide the full Beacon/Probe Response
914 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg50521aa2013-01-30 21:33:19 +0100915 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +0100916 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200917 */
Johannes Berg9caf0362012-11-29 01:25:20 +0100918 ies = kmalloc(sizeof(*ies) + ielen, gfp);
919 if (!ies)
920 return NULL;
921 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100922 ies->tsf = tsf;
Johannes Berg9caf0362012-11-29 01:25:20 +0100923 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200924
Johannes Berg9caf0362012-11-29 01:25:20 +0100925 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
926 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200927
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200928 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp,
929 rx_channel == channel);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200930 if (!res)
931 return NULL;
932
933 if (res->pub.capability & WLAN_CAPABILITY_ESS)
934 regulatory_hint_found_beacon(wiphy, channel, gfp);
935
Beni Lev4ee3e062012-08-27 12:49:39 +0300936 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200937 /* cfg80211_bss_update gives us a referenced result */
938 return &res->pub;
939}
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200940EXPORT_SYMBOL(cfg80211_inform_bss_width);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200941
Ben Greear0e3a39b2013-06-19 14:06:27 -0700942/* Returned bss is reference counted and must be cleaned up appropriately. */
Johannes Berg2a519312009-02-10 21:25:55 +0100943struct cfg80211_bss *
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200944cfg80211_inform_bss_width_frame(struct wiphy *wiphy,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200945 struct ieee80211_channel *rx_channel,
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200946 enum nl80211_bss_scan_width scan_width,
947 struct ieee80211_mgmt *mgmt, size_t len,
948 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100949{
Johannes Berg9caf0362012-11-29 01:25:20 +0100950 struct cfg80211_internal_bss tmp = {}, *res;
951 struct cfg80211_bss_ies *ies;
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200952 struct ieee80211_channel *channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100953 size_t ielen = len - offsetof(struct ieee80211_mgmt,
954 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100955
Johannes Berg0172bb72012-11-23 14:23:30 +0100956 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
957 offsetof(struct ieee80211_mgmt, u.beacon.variable));
958
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200959 trace_cfg80211_inform_bss_width_frame(wiphy, rx_channel, scan_width, mgmt,
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200960 len, signal);
Beni Lev4ee3e062012-08-27 12:49:39 +0300961
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100962 if (WARN_ON(!mgmt))
963 return NULL;
964
965 if (WARN_ON(!wiphy))
966 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100967
Sujith22fe88d2010-05-13 10:34:08 +0530968 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300969 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100970 return NULL;
971
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100972 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100973 return NULL;
974
Johannes Berg0172bb72012-11-23 14:23:30 +0100975 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +0200976 ielen, rx_channel);
Johannes Berg0172bb72012-11-23 14:23:30 +0100977 if (!channel)
978 return NULL;
979
Johannes Berg9caf0362012-11-29 01:25:20 +0100980 ies = kmalloc(sizeof(*ies) + ielen, gfp);
981 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +0100982 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100983 ies->len = ielen;
Johannes Berg8cef2c92013-02-05 16:54:31 +0100984 ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
Johannes Berg9caf0362012-11-29 01:25:20 +0100985 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +0100986
Johannes Berg9caf0362012-11-29 01:25:20 +0100987 if (ieee80211_is_probe_resp(mgmt->frame_control))
988 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
989 else
990 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
991 rcu_assign_pointer(tmp.pub.ies, ies);
992
993 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
994 tmp.pub.channel = channel;
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +0200995 tmp.pub.scan_width = scan_width;
Johannes Berg9caf0362012-11-29 01:25:20 +0100996 tmp.pub.signal = signal;
Johannes Berg9caf0362012-11-29 01:25:20 +0100997 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
998 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Johannes Berg2a519312009-02-10 21:25:55 +0100999
Emmanuel Grumbach3afc2162014-03-04 16:50:13 +02001000 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp,
1001 rx_channel == channel);
Johannes Berg2a519312009-02-10 21:25:55 +01001002 if (!res)
1003 return NULL;
1004
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -05001005 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1006 regulatory_hint_found_beacon(wiphy, channel, gfp);
1007
Beni Lev4ee3e062012-08-27 12:49:39 +03001008 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +01001009 /* cfg80211_bss_update gives us a referenced result */
1010 return &res->pub;
1011}
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02001012EXPORT_SYMBOL(cfg80211_inform_bss_width_frame);
Johannes Berg2a519312009-02-10 21:25:55 +01001013
Johannes Berg5b112d32013-02-01 01:49:58 +01001014void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001015{
Johannes Berg776b3582013-02-01 02:06:18 +01001016 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001017 struct cfg80211_internal_bss *bss;
1018
1019 if (!pub)
1020 return;
1021
1022 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001023
1024 spin_lock_bh(&dev->bss_lock);
1025 bss_ref_get(dev, bss);
1026 spin_unlock_bh(&dev->bss_lock);
Johannes Berg4c0c0b72012-01-20 13:55:26 +01001027}
1028EXPORT_SYMBOL(cfg80211_ref_bss);
1029
Johannes Berg5b112d32013-02-01 01:49:58 +01001030void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
Johannes Berg2a519312009-02-10 21:25:55 +01001031{
Johannes Berg776b3582013-02-01 02:06:18 +01001032 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001033 struct cfg80211_internal_bss *bss;
1034
1035 if (!pub)
1036 return;
1037
1038 bss = container_of(pub, struct cfg80211_internal_bss, pub);
Johannes Berg776b3582013-02-01 02:06:18 +01001039
1040 spin_lock_bh(&dev->bss_lock);
1041 bss_ref_put(dev, bss);
1042 spin_unlock_bh(&dev->bss_lock);
Johannes Berg2a519312009-02-10 21:25:55 +01001043}
1044EXPORT_SYMBOL(cfg80211_put_bss);
1045
Johannes Bergd491af12009-02-10 21:25:58 +01001046void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
1047{
1048 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
1049 struct cfg80211_internal_bss *bss;
1050
1051 if (WARN_ON(!pub))
1052 return;
1053
1054 bss = container_of(pub, struct cfg80211_internal_bss, pub);
1055
1056 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +02001057 if (!list_empty(&bss->list)) {
Johannes Berg776b3582013-02-01 02:06:18 +01001058 if (__cfg80211_unlink_bss(dev, bss))
1059 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +02001060 }
Johannes Bergd491af12009-02-10 21:25:58 +01001061 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +01001062}
1063EXPORT_SYMBOL(cfg80211_unlink_bss);
1064
Johannes Berg3d23e342009-09-29 23:27:28 +02001065#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9f419f32013-05-08 21:34:22 +02001066static struct cfg80211_registered_device *
1067cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
1068{
Johannes Berg5fe231e2013-05-08 21:45:15 +02001069 struct cfg80211_registered_device *rdev;
Johannes Berg9f419f32013-05-08 21:34:22 +02001070 struct net_device *dev;
1071
Johannes Berg5fe231e2013-05-08 21:45:15 +02001072 ASSERT_RTNL();
1073
Johannes Berg9f419f32013-05-08 21:34:22 +02001074 dev = dev_get_by_index(net, ifindex);
1075 if (!dev)
Johannes Berg5fe231e2013-05-08 21:45:15 +02001076 return ERR_PTR(-ENODEV);
1077 if (dev->ieee80211_ptr)
Johannes Berg9f419f32013-05-08 21:34:22 +02001078 rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001079 else
Johannes Berg9f419f32013-05-08 21:34:22 +02001080 rdev = ERR_PTR(-ENODEV);
1081 dev_put(dev);
Johannes Berg9f419f32013-05-08 21:34:22 +02001082 return rdev;
1083}
1084
Johannes Berg2a519312009-02-10 21:25:55 +01001085int cfg80211_wext_siwscan(struct net_device *dev,
1086 struct iw_request_info *info,
1087 union iwreq_data *wrqu, char *extra)
1088{
1089 struct cfg80211_registered_device *rdev;
1090 struct wiphy *wiphy;
1091 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001092 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01001093 int i, err, n_channels = 0;
1094 enum ieee80211_band band;
1095
1096 if (!netif_running(dev))
1097 return -ENETDOWN;
1098
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001099 if (wrqu->data.length == sizeof(struct iw_scan_req))
1100 wreq = (struct iw_scan_req *)extra;
1101
Johannes Berg463d0182009-07-14 00:33:35 +02001102 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001103
1104 if (IS_ERR(rdev))
1105 return PTR_ERR(rdev);
1106
Johannes Bergf9d15d12014-01-22 11:14:19 +02001107 if (rdev->scan_req || rdev->scan_msg) {
Johannes Berg2a519312009-02-10 21:25:55 +01001108 err = -EBUSY;
1109 goto out;
1110 }
1111
1112 wiphy = &rdev->wiphy;
1113
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001114 /* Determine number of channels, needed to allocate creq */
1115 if (wreq && wreq->num_channels)
1116 n_channels = wreq->num_channels;
Ilan Peerbdfbec22014-01-09 11:37:23 +02001117 else
1118 n_channels = ieee80211_get_num_supported_channels(wiphy);
Johannes Berg2a519312009-02-10 21:25:55 +01001119
1120 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1121 n_channels * sizeof(void *),
1122 GFP_ATOMIC);
1123 if (!creq) {
1124 err = -ENOMEM;
1125 goto out;
1126 }
1127
1128 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +02001129 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +02001130 /* SSIDs come after channels */
1131 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01001132 creq->n_channels = n_channels;
1133 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -07001134 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01001135
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001136 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +01001137 i = 0;
1138 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1139 int j;
Johannes Berg584991d2009-11-02 13:32:03 +01001140
Johannes Berg2a519312009-02-10 21:25:55 +01001141 if (!wiphy->bands[band])
1142 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01001143
Johannes Berg2a519312009-02-10 21:25:55 +01001144 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001145 /* ignore disabled channels */
1146 if (wiphy->bands[band]->channels[j].flags &
1147 IEEE80211_CHAN_DISABLED)
1148 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001149
1150 /* If we have a wireless request structure and the
1151 * wireless request specifies frequencies, then search
1152 * for the matching hardware channel.
1153 */
1154 if (wreq && wreq->num_channels) {
1155 int k;
1156 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1157 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +02001158 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001159 if (wext_freq == wiphy_freq)
1160 goto wext_freq_found;
1161 }
1162 goto wext_freq_not_found;
1163 }
1164
1165 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001166 creq->channels[i] = &wiphy->bands[band]->channels[j];
1167 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001168 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001169 }
1170 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001171 /* No channels found? */
1172 if (!i) {
1173 err = -EINVAL;
1174 goto out;
1175 }
Johannes Berg2a519312009-02-10 21:25:55 +01001176
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001177 /* Set real number of channels specified in creq->channels[] */
1178 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001179
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001180 /* translate "Scan for SSID" request */
1181 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001182 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001183 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1184 err = -EINVAL;
1185 goto out;
1186 }
Johannes Berg2a519312009-02-10 21:25:55 +01001187 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1188 creq->ssids[0].ssid_len = wreq->essid_len;
1189 }
1190 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1191 creq->n_ssids = 0;
1192 }
1193
Johannes Berg34850ab2011-07-18 18:08:35 +02001194 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001195 if (wiphy->bands[i])
1196 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001197
Johannes Berg2a519312009-02-10 21:25:55 +01001198 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001199 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001200 if (err) {
1201 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001202 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001203 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001204 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001205 /* creq now owned by driver */
1206 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001207 dev_hold(dev);
1208 }
Johannes Berg2a519312009-02-10 21:25:55 +01001209 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001210 kfree(creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001211 return err;
1212}
Johannes Bergba44cb72009-04-20 18:49:39 +02001213EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001214
1215static void ieee80211_scan_add_ies(struct iw_request_info *info,
Johannes Berg9caf0362012-11-29 01:25:20 +01001216 const struct cfg80211_bss_ies *ies,
Johannes Berg2a519312009-02-10 21:25:55 +01001217 char **current_ev, char *end_buf)
1218{
Johannes Berg9caf0362012-11-29 01:25:20 +01001219 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01001220 struct iw_event iwe;
1221
Johannes Berg9caf0362012-11-29 01:25:20 +01001222 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001223 return;
1224
1225 /*
1226 * If needed, fragment the IEs buffer (at IE boundaries) into short
1227 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1228 */
Johannes Berg9caf0362012-11-29 01:25:20 +01001229 pos = ies->data;
1230 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001231
1232 while (end - pos > IW_GENERIC_IE_MAX) {
1233 next = pos + 2 + pos[1];
1234 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1235 next = next + 2 + next[1];
1236
1237 memset(&iwe, 0, sizeof(iwe));
1238 iwe.cmd = IWEVGENIE;
1239 iwe.u.data.length = next - pos;
1240 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001241 end_buf, &iwe,
1242 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001243
1244 pos = next;
1245 }
1246
1247 if (end > pos) {
1248 memset(&iwe, 0, sizeof(iwe));
1249 iwe.cmd = IWEVGENIE;
1250 iwe.u.data.length = end - pos;
1251 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001252 end_buf, &iwe,
1253 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001254 }
1255}
1256
Johannes Berg2a519312009-02-10 21:25:55 +01001257static char *
Johannes Berg77965c92009-02-18 18:45:06 +01001258ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1259 struct cfg80211_internal_bss *bss, char *current_ev,
1260 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001261{
Johannes Berg9caf0362012-11-29 01:25:20 +01001262 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01001263 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01001264 const u8 *ie;
Johannes Berg2a519312009-02-10 21:25:55 +01001265 u8 *buf, *cfg, *p;
Johannes Berg9caf0362012-11-29 01:25:20 +01001266 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001267 bool ismesh = false;
1268
1269 memset(&iwe, 0, sizeof(iwe));
1270 iwe.cmd = SIOCGIWAP;
1271 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1272 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1273 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1274 IW_EV_ADDR_LEN);
1275
1276 memset(&iwe, 0, sizeof(iwe));
1277 iwe.cmd = SIOCGIWFREQ;
1278 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1279 iwe.u.freq.e = 0;
1280 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1281 IW_EV_FREQ_LEN);
1282
1283 memset(&iwe, 0, sizeof(iwe));
1284 iwe.cmd = SIOCGIWFREQ;
1285 iwe.u.freq.m = bss->pub.channel->center_freq;
1286 iwe.u.freq.e = 6;
1287 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1288 IW_EV_FREQ_LEN);
1289
Johannes Berg77965c92009-02-18 18:45:06 +01001290 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01001291 memset(&iwe, 0, sizeof(iwe));
1292 iwe.cmd = IWEVQUAL;
1293 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1294 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01001295 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +01001296 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01001297 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01001298 sig = bss->pub.signal / 100;
1299 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001300 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01001301 if (sig < -110) /* rather bad */
1302 sig = -110;
1303 else if (sig > -40) /* perfect */
1304 sig = -40;
1305 /* will give a range of 0 .. 70 */
1306 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01001307 break;
1308 case CFG80211_SIGNAL_TYPE_UNSPEC:
1309 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01001310 /* will give range 0 .. 100 */
1311 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01001312 break;
1313 default:
1314 /* not reached */
1315 break;
1316 }
1317 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1318 &iwe, IW_EV_QUAL_LEN);
1319 }
1320
1321 memset(&iwe, 0, sizeof(iwe));
1322 iwe.cmd = SIOCGIWENCODE;
1323 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1324 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1325 else
1326 iwe.u.data.flags = IW_ENCODE_DISABLED;
1327 iwe.u.data.length = 0;
1328 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1329 &iwe, "");
1330
Johannes Berg9caf0362012-11-29 01:25:20 +01001331 rcu_read_lock();
1332 ies = rcu_dereference(bss->pub.ies);
Johannes Berg83c7aa12013-02-05 16:51:29 +01001333 rem = ies->len;
1334 ie = ies->data;
Johannes Berg9caf0362012-11-29 01:25:20 +01001335
Johannes Berg83c7aa12013-02-05 16:51:29 +01001336 while (rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01001337 /* invalid data */
1338 if (ie[1] > rem - 2)
1339 break;
1340
1341 switch (ie[0]) {
1342 case WLAN_EID_SSID:
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_ID:
1351 memset(&iwe, 0, sizeof(iwe));
1352 iwe.cmd = SIOCGIWESSID;
1353 iwe.u.data.length = ie[1];
1354 iwe.u.data.flags = 1;
1355 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001356 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001357 break;
1358 case WLAN_EID_MESH_CONFIG:
1359 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001360 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001361 break;
1362 buf = kmalloc(50, GFP_ATOMIC);
1363 if (!buf)
1364 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01001365 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01001366 memset(&iwe, 0, sizeof(iwe));
1367 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001368 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1369 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001370 iwe.u.data.length = strlen(buf);
1371 current_ev = iwe_stream_add_point(info, current_ev,
1372 end_buf,
1373 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001374 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1375 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001376 iwe.u.data.length = strlen(buf);
1377 current_ev = iwe_stream_add_point(info, current_ev,
1378 end_buf,
1379 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001380 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1381 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001382 iwe.u.data.length = strlen(buf);
1383 current_ev = iwe_stream_add_point(info, current_ev,
1384 end_buf,
1385 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001386 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01001387 iwe.u.data.length = strlen(buf);
1388 current_ev = iwe_stream_add_point(info, current_ev,
1389 end_buf,
1390 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001391 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1392 iwe.u.data.length = strlen(buf);
1393 current_ev = iwe_stream_add_point(info, current_ev,
1394 end_buf,
1395 &iwe, buf);
1396 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1397 iwe.u.data.length = strlen(buf);
1398 current_ev = iwe_stream_add_point(info, current_ev,
1399 end_buf,
1400 &iwe, buf);
1401 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001402 iwe.u.data.length = strlen(buf);
1403 current_ev = iwe_stream_add_point(info, current_ev,
1404 end_buf,
1405 &iwe, buf);
1406 kfree(buf);
1407 break;
1408 case WLAN_EID_SUPP_RATES:
1409 case WLAN_EID_EXT_SUPP_RATES:
1410 /* display all supported rates in readable format */
1411 p = current_ev + iwe_stream_lcp_len(info);
1412
1413 memset(&iwe, 0, sizeof(iwe));
1414 iwe.cmd = SIOCGIWRATE;
1415 /* Those two flags are ignored... */
1416 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1417
1418 for (i = 0; i < ie[1]; i++) {
1419 iwe.u.bitrate.value =
1420 ((ie[i + 2] & 0x7f) * 500000);
1421 p = iwe_stream_add_value(info, current_ev, p,
1422 end_buf, &iwe, IW_EV_PARAM_LEN);
1423 }
1424 current_ev = p;
1425 break;
1426 }
1427 rem -= ie[1] + 2;
1428 ie += ie[1] + 2;
1429 }
1430
Joe Perchesf64f9e72009-11-29 16:55:45 -08001431 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1432 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001433 memset(&iwe, 0, sizeof(iwe));
1434 iwe.cmd = SIOCGIWMODE;
1435 if (ismesh)
1436 iwe.u.mode = IW_MODE_MESH;
1437 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1438 iwe.u.mode = IW_MODE_MASTER;
1439 else
1440 iwe.u.mode = IW_MODE_ADHOC;
1441 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1442 &iwe, IW_EV_UINT_LEN);
1443 }
1444
Dan Carpenterc49dc902013-01-24 09:40:00 +03001445 buf = kmalloc(31, GFP_ATOMIC);
Johannes Berg2a519312009-02-10 21:25:55 +01001446 if (buf) {
1447 memset(&iwe, 0, sizeof(iwe));
1448 iwe.cmd = IWEVCUSTOM;
Johannes Berg8cef2c92013-02-05 16:54:31 +01001449 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
Johannes Berg2a519312009-02-10 21:25:55 +01001450 iwe.u.data.length = strlen(buf);
1451 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1452 &iwe, buf);
1453 memset(&iwe, 0, sizeof(iwe));
1454 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001455 sprintf(buf, " Last beacon: %ums ago",
1456 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001457 iwe.u.data.length = strlen(buf);
1458 current_ev = iwe_stream_add_point(info, current_ev,
1459 end_buf, &iwe, buf);
1460 kfree(buf);
1461 }
1462
Johannes Berg9caf0362012-11-29 01:25:20 +01001463 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1464 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01001465
1466 return current_ev;
1467}
1468
1469
1470static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1471 struct iw_request_info *info,
1472 char *buf, size_t len)
1473{
1474 char *current_ev = buf;
1475 char *end_buf = buf + len;
1476 struct cfg80211_internal_bss *bss;
1477
1478 spin_lock_bh(&dev->bss_lock);
1479 cfg80211_bss_expire(dev);
1480
1481 list_for_each_entry(bss, &dev->bss_list, list) {
1482 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1483 spin_unlock_bh(&dev->bss_lock);
1484 return -E2BIG;
1485 }
Johannes Berg77965c92009-02-18 18:45:06 +01001486 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1487 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001488 }
1489 spin_unlock_bh(&dev->bss_lock);
1490 return current_ev - buf;
1491}
1492
1493
1494int cfg80211_wext_giwscan(struct net_device *dev,
1495 struct iw_request_info *info,
1496 struct iw_point *data, char *extra)
1497{
1498 struct cfg80211_registered_device *rdev;
1499 int res;
1500
1501 if (!netif_running(dev))
1502 return -ENETDOWN;
1503
Johannes Berg463d0182009-07-14 00:33:35 +02001504 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001505
1506 if (IS_ERR(rdev))
1507 return PTR_ERR(rdev);
1508
Johannes Bergf9d15d12014-01-22 11:14:19 +02001509 if (rdev->scan_req || rdev->scan_msg)
Johannes Berg5fe231e2013-05-08 21:45:15 +02001510 return -EAGAIN;
Johannes Berg2a519312009-02-10 21:25:55 +01001511
1512 res = ieee80211_scan_results(rdev, info, extra, data->length);
1513 data->length = 0;
1514 if (res >= 0) {
1515 data->length = res;
1516 res = 0;
1517 }
1518
Johannes Berg2a519312009-02-10 21:25:55 +01001519 return res;
1520}
Johannes Bergba44cb72009-04-20 18:49:39 +02001521EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001522#endif