blob: cfc4e1aa8dad706be6a6fad031b5082a9c054e4c [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
Rajkumar Manoharanf9616e02012-04-13 16:38:40 +053022#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010023
Amitkumar Karware8e27c62012-10-11 21:03:33 -070024static void bss_release(struct kref *ref)
25{
Johannes Berg9caf0362012-11-29 01:25:20 +010026 struct cfg80211_bss_ies *ies;
Amitkumar Karware8e27c62012-10-11 21:03:33 -070027 struct cfg80211_internal_bss *bss;
28
29 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Bergb629ea32012-11-28 22:14:56 +010030
31 if (WARN_ON(atomic_read(&bss->hold)))
32 return;
33
Johannes Berg9caf0362012-11-29 01:25:20 +010034 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
35 if (ies)
36 kfree_rcu(ies, rcu_head);
37 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
38 if (ies)
39 kfree_rcu(ies, rcu_head);
Amitkumar Karware8e27c62012-10-11 21:03:33 -070040
Amitkumar Karware8e27c62012-10-11 21:03:33 -070041 kfree(bss);
42}
43
44/* must hold dev->bss_lock! */
45static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
46 struct cfg80211_internal_bss *bss)
47{
48 list_del_init(&bss->list);
49 rb_erase(&bss->rbn, &dev->bss_tree);
50 kref_put(&bss->ref, bss_release);
51}
52
Sam Leffler15d60302012-10-11 21:03:34 -070053/* must hold dev->bss_lock! */
54static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
55 unsigned long expire_time)
56{
57 struct cfg80211_internal_bss *bss, *tmp;
58 bool expired = false;
59
60 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
61 if (atomic_read(&bss->hold))
62 continue;
63 if (!time_after(expire_time, bss->ts))
64 continue;
65
66 __cfg80211_unlink_bss(dev, bss);
67 expired = true;
68 }
69
70 if (expired)
71 dev->bss_generation++;
72}
73
Johannes Berg01a0ac42009-08-20 21:36:16 +020074void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +010075{
Johannes Berg667503d2009-07-07 03:56:11 +020076 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +020077 struct wireless_dev *wdev;
Johannes Berg3d23e342009-09-29 23:27:28 +020078#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +010079 union iwreq_data wrqu;
80#endif
81
Johannes Berg01a0ac42009-08-20 21:36:16 +020082 ASSERT_RDEV_LOCK(rdev);
83
Johannes Berg667503d2009-07-07 03:56:11 +020084 request = rdev->scan_req;
85
Johannes Berg01a0ac42009-08-20 21:36:16 +020086 if (!request)
87 return;
88
Johannes Bergfd014282012-06-18 19:17:03 +020089 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +010090
Johannes Berg6829c872009-07-02 09:13:27 +020091 /*
92 * This must be before sending the other events!
93 * Otherwise, wpa_supplicant gets completely confused with
94 * wext events.
95 */
Johannes Bergfd014282012-06-18 19:17:03 +020096 if (wdev->netdev)
97 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +020098
Sam Leffler15d60302012-10-11 21:03:34 -070099 if (request->aborted) {
Johannes Bergfd014282012-06-18 19:17:03 +0200100 nl80211_send_scan_aborted(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700101 } else {
102 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
103 /* flush entries from previous scans */
104 spin_lock_bh(&rdev->bss_lock);
105 __cfg80211_bss_expire(rdev, request->scan_start);
106 spin_unlock_bh(&rdev->bss_lock);
107 }
Johannes Bergfd014282012-06-18 19:17:03 +0200108 nl80211_send_scan_done(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700109 }
Johannes Berg2a519312009-02-10 21:25:55 +0100110
Johannes Berg3d23e342009-09-29 23:27:28 +0200111#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200112 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100113 memset(&wrqu, 0, sizeof(wrqu));
114
Johannes Bergfd014282012-06-18 19:17:03 +0200115 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100116 }
117#endif
118
Johannes Bergfd014282012-06-18 19:17:03 +0200119 if (wdev->netdev)
120 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100121
Johannes Berg36e6fea2009-08-12 22:21:21 +0200122 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200123
124 /*
125 * OK. If this is invoked with "leak" then we can't
126 * free this ... but we've cleaned it up anyway. The
127 * driver failed to call the scan_done callback, so
128 * all bets are off, it might still be trying to use
129 * the scan request or not ... if it accesses the dev
130 * in there (it shouldn't anyway) then it may crash.
131 */
132 if (!leak)
133 kfree(request);
Johannes Berg2a519312009-02-10 21:25:55 +0100134}
Johannes Berg667503d2009-07-07 03:56:11 +0200135
Johannes Berg36e6fea2009-08-12 22:21:21 +0200136void __cfg80211_scan_done(struct work_struct *wk)
137{
138 struct cfg80211_registered_device *rdev;
139
140 rdev = container_of(wk, struct cfg80211_registered_device,
141 scan_done_wk);
142
143 cfg80211_lock_rdev(rdev);
Johannes Berg01a0ac42009-08-20 21:36:16 +0200144 ___cfg80211_scan_done(rdev, false);
Johannes Berg36e6fea2009-08-12 22:21:21 +0200145 cfg80211_unlock_rdev(rdev);
146}
147
Johannes Berg667503d2009-07-07 03:56:11 +0200148void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
149{
Beni Lev4ee3e062012-08-27 12:49:39 +0300150 trace_cfg80211_scan_done(request, aborted);
Johannes Berg667503d2009-07-07 03:56:11 +0200151 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
152
153 request->aborted = aborted;
Alban Browaeyse60d7442009-11-25 15:13:00 +0100154 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200155}
Johannes Berg2a519312009-02-10 21:25:55 +0100156EXPORT_SYMBOL(cfg80211_scan_done);
157
Luciano Coelho807f8a82011-05-11 17:09:35 +0300158void __cfg80211_sched_scan_results(struct work_struct *wk)
159{
160 struct cfg80211_registered_device *rdev;
Sam Leffler15d60302012-10-11 21:03:34 -0700161 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300162
163 rdev = container_of(wk, struct cfg80211_registered_device,
164 sched_scan_results_wk);
165
Sam Leffler15d60302012-10-11 21:03:34 -0700166 request = rdev->sched_scan_req;
167
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300168 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300169
170 /* we don't have sched_scan_req anymore if the scan is stopping */
Sam Leffler15d60302012-10-11 21:03:34 -0700171 if (request) {
172 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
173 /* flush entries from previous scans */
174 spin_lock_bh(&rdev->bss_lock);
175 __cfg80211_bss_expire(rdev, request->scan_start);
176 spin_unlock_bh(&rdev->bss_lock);
177 request->scan_start =
178 jiffies + msecs_to_jiffies(request->interval);
179 }
180 nl80211_send_sched_scan_results(rdev, request->dev);
181 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300182
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300183 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300184}
185
186void cfg80211_sched_scan_results(struct wiphy *wiphy)
187{
Beni Lev4ee3e062012-08-27 12:49:39 +0300188 trace_cfg80211_sched_scan_results(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300189 /* ignore if we're not scanning */
190 if (wiphy_to_dev(wiphy)->sched_scan_req)
191 queue_work(cfg80211_wq,
192 &wiphy_to_dev(wiphy)->sched_scan_results_wk);
193}
194EXPORT_SYMBOL(cfg80211_sched_scan_results);
195
Luciano Coelho85a99942011-05-12 16:28:29 +0300196void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300197{
Luciano Coelho85a99942011-05-12 16:28:29 +0300198 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300199
Beni Lev4ee3e062012-08-27 12:49:39 +0300200 trace_cfg80211_sched_scan_stopped(wiphy);
201
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300202 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300203 __cfg80211_stop_sched_scan(rdev, true);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300204 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300205}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300206EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
207
208int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
209 bool driver_initiated)
210{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300211 struct net_device *dev;
212
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300213 lockdep_assert_held(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300214
215 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300216 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300217
218 dev = rdev->sched_scan_req->dev;
219
Luciano Coelho85a99942011-05-12 16:28:29 +0300220 if (!driver_initiated) {
Hila Gonene35e4d22012-06-27 17:19:42 +0300221 int err = rdev_sched_scan_stop(rdev, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300222 if (err)
223 return err;
224 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300225
226 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
227
228 kfree(rdev->sched_scan_req);
229 rdev->sched_scan_req = NULL;
230
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200231 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300232}
233
Johannes Berg2a519312009-02-10 21:25:55 +0100234/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500235void cfg80211_bss_age(struct cfg80211_registered_device *dev,
236 unsigned long age_secs)
237{
238 struct cfg80211_internal_bss *bss;
239 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
240
Johannes Berg915de2f2012-11-28 22:39:37 +0100241 list_for_each_entry(bss, &dev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500242 bss->ts -= age_jiffies;
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500243}
244
Johannes Berg2a519312009-02-10 21:25:55 +0100245void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
246{
Sam Leffler15d60302012-10-11 21:03:34 -0700247 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100248}
249
Johannes Bergc21dbf92010-01-26 14:15:46 +0100250const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100251{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100252 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100253 len -= ies[1] + 2;
254 ies += ies[1] + 2;
255 }
256 if (len < 2)
257 return NULL;
258 if (len < 2 + ies[1])
259 return NULL;
260 return ies;
261}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100262EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100263
Eliad Peller0c28ec52011-09-15 11:53:01 +0300264const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
265 const u8 *ies, int len)
266{
267 struct ieee80211_vendor_ie *ie;
268 const u8 *pos = ies, *end = ies + len;
269 int ie_oui;
270
271 while (pos < end) {
272 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
273 end - pos);
274 if (!pos)
275 return NULL;
276
277 if (end - pos < sizeof(*ie))
278 return NULL;
279
280 ie = (struct ieee80211_vendor_ie *)pos;
281 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
282 if (ie_oui == oui && ie->oui_type == oui_type)
283 return pos;
284
285 pos += 2 + ie->len;
286 }
287 return NULL;
288}
289EXPORT_SYMBOL(cfg80211_find_vendor_ie);
290
Johannes Berg9caf0362012-11-29 01:25:20 +0100291static int cmp_ies(u8 num, const u8 *ies1, int len1, const u8 *ies2, int len2)
Johannes Berg2a519312009-02-10 21:25:55 +0100292{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100293 const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
294 const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
Johannes Berg2a519312009-02-10 21:25:55 +0100295
Johannes Berg3b6ef632011-11-04 11:01:46 +0100296 /* equal if both missing */
Johannes Berg2a519312009-02-10 21:25:55 +0100297 if (!ie1 && !ie2)
298 return 0;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100299 /* sort missing IE before (left of) present IE */
300 if (!ie1)
Johannes Berg2a519312009-02-10 21:25:55 +0100301 return -1;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100302 if (!ie2)
303 return 1;
Johannes Berg2a519312009-02-10 21:25:55 +0100304
Johannes Berg3b6ef632011-11-04 11:01:46 +0100305 /* sort by length first, then by contents */
306 if (ie1[1] != ie2[1])
Johannes Berg2a519312009-02-10 21:25:55 +0100307 return ie2[1] - ie1[1];
Johannes Berg3b6ef632011-11-04 11:01:46 +0100308 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg2a519312009-02-10 21:25:55 +0100309}
310
Johannes Berg915de2f2012-11-28 22:39:37 +0100311static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
Johannes Berg2a519312009-02-10 21:25:55 +0100312 const u8 *ssid, size_t ssid_len)
313{
Johannes Berg9caf0362012-11-29 01:25:20 +0100314 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100315 const u8 *ssidie;
316
Joe Perchesac422d32012-05-08 18:56:55 +0000317 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100318 return false;
319
Johannes Berg79420f02009-02-10 21:25:59 +0100320 if (!ssid)
321 return true;
322
Johannes Berg9caf0362012-11-29 01:25:20 +0100323 ies = rcu_access_pointer(a->ies);
324 if (!ies)
325 return false;
326 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100327 if (!ssidie)
328 return false;
329 if (ssidie[1] != ssid_len)
330 return false;
331 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
332}
333
Eliad Peller333ba732011-05-29 15:53:20 +0300334static bool is_mesh_bss(struct cfg80211_bss *a)
335{
Johannes Berg9caf0362012-11-29 01:25:20 +0100336 const struct cfg80211_bss_ies *ies;
Eliad Peller333ba732011-05-29 15:53:20 +0300337 const u8 *ie;
338
339 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
340 return false;
341
Johannes Berg9caf0362012-11-29 01:25:20 +0100342 ies = rcu_access_pointer(a->ies);
343 if (!ies)
344 return false;
345
346 ie = cfg80211_find_ie(WLAN_EID_MESH_ID, ies->data, ies->len);
Eliad Peller333ba732011-05-29 15:53:20 +0300347 if (!ie)
348 return false;
349
Johannes Berg9caf0362012-11-29 01:25:20 +0100350 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, ies->data, ies->len);
Eliad Peller333ba732011-05-29 15:53:20 +0300351 if (!ie)
352 return false;
353
354 return true;
355}
356
Johannes Berg2a519312009-02-10 21:25:55 +0100357static bool is_mesh(struct cfg80211_bss *a,
358 const u8 *meshid, size_t meshidlen,
359 const u8 *meshcfg)
360{
Johannes Berg9caf0362012-11-29 01:25:20 +0100361 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100362 const u8 *ie;
363
Eliad Peller333ba732011-05-29 15:53:20 +0300364 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
Johannes Berg2a519312009-02-10 21:25:55 +0100365 return false;
366
Johannes Berg9caf0362012-11-29 01:25:20 +0100367 ies = rcu_access_pointer(a->ies);
368 if (!ies)
369 return false;
370
371 ie = cfg80211_find_ie(WLAN_EID_MESH_ID, ies->data, ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100372 if (!ie)
373 return false;
374 if (ie[1] != meshidlen)
375 return false;
376 if (memcmp(ie + 2, meshid, meshidlen))
377 return false;
378
Johannes Berg9caf0362012-11-29 01:25:20 +0100379 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, ies->data, ies->len);
Johannes Bergcd3468b2009-07-29 22:07:44 +0200380 if (!ie)
381 return false;
Rui Paulo136cfa22009-11-18 18:40:00 +0000382 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +0100383 return false;
384
385 /*
386 * Ignore mesh capability (last two bytes of the IE) when
387 * comparing since that may differ between stations taking
388 * part in the same mesh.
389 */
Rui Paulo136cfa22009-11-18 18:40:00 +0000390 return memcmp(ie + 2, meshcfg,
Johannes Berg915de2f2012-11-28 22:39:37 +0100391 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100392}
393
Johannes Berg915de2f2012-11-28 22:39:37 +0100394static int cmp_bss_core(struct cfg80211_bss *a, struct cfg80211_bss *b)
Johannes Berg2a519312009-02-10 21:25:55 +0100395{
Johannes Berg9caf0362012-11-29 01:25:20 +0100396 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100397 int r;
398
399 if (a->channel != b->channel)
400 return b->channel->center_freq - a->channel->center_freq;
401
Eliad Peller333ba732011-05-29 15:53:20 +0300402 if (is_mesh_bss(a) && is_mesh_bss(b)) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100403 a_ies = rcu_access_pointer(a->ies);
404 if (!a_ies)
405 return -1;
406 b_ies = rcu_access_pointer(b->ies);
407 if (!b_ies)
408 return 1;
409
Johannes Berg2a519312009-02-10 21:25:55 +0100410 r = cmp_ies(WLAN_EID_MESH_ID,
Johannes Berg9caf0362012-11-29 01:25:20 +0100411 a_ies->data, a_ies->len,
412 b_ies->data, b_ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100413 if (r)
414 return r;
415 return cmp_ies(WLAN_EID_MESH_CONFIG,
Johannes Berg9caf0362012-11-29 01:25:20 +0100416 a_ies->data, a_ies->len,
417 b_ies->data, b_ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100418 }
419
Emmanuel Grumbachef9456a2012-04-30 10:23:36 +0300420 /*
421 * we can't use compare_ether_addr here since we need a < > operator.
422 * The binary return value of compare_ether_addr isn't enough
423 */
424 return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100425}
426
427static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100428 struct cfg80211_bss *b,
429 bool hide_ssid)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100430{
Johannes Berg9caf0362012-11-29 01:25:20 +0100431 const struct cfg80211_bss_ies *a_ies, *b_ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100432 const u8 *ie1;
433 const u8 *ie2;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100434 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100435
436 r = cmp_bss_core(a, b);
437 if (r)
438 return r;
439
Johannes Berg9caf0362012-11-29 01:25:20 +0100440 a_ies = rcu_access_pointer(a->ies);
441 if (!a_ies)
442 return -1;
443 b_ies = rcu_access_pointer(b->ies);
444 if (!b_ies)
445 return 1;
446
447 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
448 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100449
Johannes Berg5622f5b2013-01-30 00:26:45 +0100450 if (!ie1 && !ie2)
451 return 0;
452
Johannes Bergf94f8b12012-11-28 22:42:34 +0100453 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100454 * Note that with "hide_ssid", the function returns a match if
455 * the already-present BSS ("b") is a hidden SSID beacon for
456 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100457 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100458
459 /* sort missing IE before (left of) present IE */
460 if (!ie1)
461 return -1;
462 if (!ie2)
463 return 1;
464
Johannes Berg5622f5b2013-01-30 00:26:45 +0100465 /* zero-length SSID is used as an indication of the hidden bss */
466 if (hide_ssid && !ie2[1])
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100467 return 0;
468
469 /* sort by length first, then by contents */
470 if (ie1[1] != ie2[1])
471 return ie2[1] - ie1[1];
472
Johannes Berg5622f5b2013-01-30 00:26:45 +0100473 if (!hide_ssid)
474 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
475
Johannes Bergf94f8b12012-11-28 22:42:34 +0100476 /*
477 * zeroed SSID ie is another indication of a hidden bss;
478 * if it isn't zeroed just return the regular sort value
479 * to find the next candidate
480 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100481 for (i = 0; i < ie2[1]; i++)
482 if (ie2[i + 2])
Johannes Bergf94f8b12012-11-28 22:42:34 +0100483 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100484
485 return 0;
486}
487
Johannes Berg2a519312009-02-10 21:25:55 +0100488struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
489 struct ieee80211_channel *channel,
490 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100491 const u8 *ssid, size_t ssid_len,
492 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100493{
494 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
495 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200496 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100497
Beni Lev4ee3e062012-08-27 12:49:39 +0300498 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
499 capa_val);
500
Johannes Berg2a519312009-02-10 21:25:55 +0100501 spin_lock_bh(&dev->bss_lock);
502
503 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100504 if ((bss->pub.capability & capa_mask) != capa_val)
505 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100506 if (channel && bss->pub.channel != channel)
507 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200508 /* Don't get expired BSS structs */
509 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
510 !atomic_read(&bss->hold))
511 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100512 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
513 res = bss;
514 kref_get(&res->ref);
515 break;
516 }
517 }
518
519 spin_unlock_bh(&dev->bss_lock);
520 if (!res)
521 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300522 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100523 return &res->pub;
524}
525EXPORT_SYMBOL(cfg80211_get_bss);
526
527struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
528 struct ieee80211_channel *channel,
529 const u8 *meshid, size_t meshidlen,
530 const u8 *meshcfg)
531{
532 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
533 struct cfg80211_internal_bss *bss, *res = NULL;
534
535 spin_lock_bh(&dev->bss_lock);
536
537 list_for_each_entry(bss, &dev->bss_list, list) {
538 if (channel && bss->pub.channel != channel)
539 continue;
540 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
541 res = bss;
542 kref_get(&res->ref);
543 break;
544 }
545 }
546
547 spin_unlock_bh(&dev->bss_lock);
548 if (!res)
549 return NULL;
550 return &res->pub;
551}
552EXPORT_SYMBOL(cfg80211_get_mesh);
553
554
555static void rb_insert_bss(struct cfg80211_registered_device *dev,
556 struct cfg80211_internal_bss *bss)
557{
558 struct rb_node **p = &dev->bss_tree.rb_node;
559 struct rb_node *parent = NULL;
560 struct cfg80211_internal_bss *tbss;
561 int cmp;
562
563 while (*p) {
564 parent = *p;
565 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
566
Johannes Berg5622f5b2013-01-30 00:26:45 +0100567 cmp = cmp_bss(&bss->pub, &tbss->pub, false);
Johannes Berg2a519312009-02-10 21:25:55 +0100568
569 if (WARN_ON(!cmp)) {
570 /* will sort of leak this BSS */
571 return;
572 }
573
574 if (cmp < 0)
575 p = &(*p)->rb_left;
576 else
577 p = &(*p)->rb_right;
578 }
579
580 rb_link_node(&bss->rbn, parent, p);
581 rb_insert_color(&bss->rbn, &dev->bss_tree);
582}
583
584static struct cfg80211_internal_bss *
585rb_find_bss(struct cfg80211_registered_device *dev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100586 struct cfg80211_internal_bss *res,
587 bool hidden)
Johannes Berg2a519312009-02-10 21:25:55 +0100588{
589 struct rb_node *n = dev->bss_tree.rb_node;
590 struct cfg80211_internal_bss *bss;
591 int r;
592
593 while (n) {
594 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg5622f5b2013-01-30 00:26:45 +0100595 r = cmp_bss(&res->pub, &bss->pub, hidden);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100596
597 if (r == 0)
598 return bss;
599 else if (r < 0)
600 n = n->rb_left;
601 else
602 n = n->rb_right;
603 }
604
605 return NULL;
606}
607
608static void
609copy_hidden_ies(struct cfg80211_internal_bss *res,
Johannes Berg915de2f2012-11-28 22:39:37 +0100610 struct cfg80211_internal_bss *hidden)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100611{
Johannes Berg9caf0362012-11-29 01:25:20 +0100612 const struct cfg80211_bss_ies *ies;
613
614 if (rcu_access_pointer(res->pub.beacon_ies))
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100615 return;
616
Johannes Berg9caf0362012-11-29 01:25:20 +0100617 ies = rcu_access_pointer(hidden->pub.beacon_ies);
618 if (WARN_ON(!ies))
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100619 return;
620
Johannes Berg9caf0362012-11-29 01:25:20 +0100621 ies = kmemdup(ies, sizeof(*ies) + ies->len, GFP_ATOMIC);
622 if (unlikely(!ies))
623 return;
624 rcu_assign_pointer(res->pub.beacon_ies, ies);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100625}
626
627static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100628cfg80211_bss_update(struct cfg80211_registered_device *dev,
Johannes Berg9caf0362012-11-29 01:25:20 +0100629 struct cfg80211_internal_bss *tmp)
Johannes Berg2a519312009-02-10 21:25:55 +0100630{
631 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100632
Johannes Berg9caf0362012-11-29 01:25:20 +0100633 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +0100634 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100635
Johannes Berg9caf0362012-11-29 01:25:20 +0100636 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100637
Johannes Berg2a519312009-02-10 21:25:55 +0100638 spin_lock_bh(&dev->bss_lock);
639
Johannes Berg9caf0362012-11-29 01:25:20 +0100640 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
641 spin_unlock_bh(&dev->bss_lock);
642 return NULL;
643 }
644
Johannes Berg5622f5b2013-01-30 00:26:45 +0100645 found = rb_find_bss(dev, tmp, false);
Johannes Berg2a519312009-02-10 21:25:55 +0100646
Johannes Bergcd1658f2009-04-16 15:00:58 +0200647 if (found) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100648 found->pub.beacon_interval = tmp->pub.beacon_interval;
649 found->pub.tsf = tmp->pub.tsf;
650 found->pub.signal = tmp->pub.signal;
651 found->pub.capability = tmp->pub.capability;
652 found->ts = tmp->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200653
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200654 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100655 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
656 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200657
Johannes Berg9caf0362012-11-29 01:25:20 +0100658 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200659
Johannes Berg9caf0362012-11-29 01:25:20 +0100660 rcu_assign_pointer(found->pub.proberesp_ies,
661 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200662 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100663 rcu_assign_pointer(found->pub.ies,
664 tmp->pub.proberesp_ies);
665 if (old)
666 kfree_rcu((struct cfg80211_bss_ies *)old,
667 rcu_head);
668 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
669 const struct cfg80211_bss_ies *old, *ies;
Johannes Berg915de2f2012-11-28 22:39:37 +0100670
Johannes Berg9caf0362012-11-29 01:25:20 +0100671 old = rcu_access_pointer(found->pub.beacon_ies);
672 ies = rcu_access_pointer(found->pub.ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200673
Johannes Berg9caf0362012-11-29 01:25:20 +0100674 rcu_assign_pointer(found->pub.beacon_ies,
675 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +0100676
677 /* Override IEs if they were from a beacon before */
Johannes Berg9caf0362012-11-29 01:25:20 +0100678 if (old == ies)
679 rcu_assign_pointer(found->pub.ies,
680 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200681
Johannes Berg9caf0362012-11-29 01:25:20 +0100682 if (old)
683 kfree_rcu((struct cfg80211_bss_ies *)old,
684 rcu_head);
685 }
Johannes Berg2a519312009-02-10 21:25:55 +0100686 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +0100687 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100688 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +0100689 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100690
691 /* First check if the beacon is a probe response from
692 * a hidden bss. If so, copy beacon ies (with nullified
693 * ssid) into the probe response bss entry (with real ssid).
694 * It is required basically for PSM implementation
695 * (probe responses do not contain tim ie) */
696
697 /* TODO: The code is not trying to update existing probe
698 * response bss entries when beacon ies are
699 * getting changed. */
Johannes Berg5622f5b2013-01-30 00:26:45 +0100700 hidden = rb_find_bss(dev, tmp, true);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100701 if (hidden)
Johannes Berg9caf0362012-11-29 01:25:20 +0100702 copy_hidden_ies(tmp, hidden);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100703
Johannes Berg9caf0362012-11-29 01:25:20 +0100704 /*
705 * create a copy -- the "res" variable that is passed in
706 * is allocated on the stack since it's not needed in the
707 * more common case of an update
708 */
709 new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
710 GFP_ATOMIC);
711 if (!new) {
712 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
713 if (ies)
714 kfree_rcu(ies, rcu_head);
715 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
716 if (ies)
717 kfree_rcu(ies, rcu_head);
718 spin_unlock_bh(&dev->bss_lock);
719 return NULL;
720 }
721 memcpy(new, tmp, sizeof(*new));
722 kref_init(&new->ref);
723 list_add_tail(&new->list, &dev->bss_list);
724 rb_insert_bss(dev, new);
725 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +0100726 }
727
728 dev->bss_generation++;
729 spin_unlock_bh(&dev->bss_lock);
730
731 kref_get(&found->ref);
732 return found;
733}
734
Johannes Berg0172bb72012-11-23 14:23:30 +0100735static struct ieee80211_channel *
736cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
737 struct ieee80211_channel *channel)
738{
739 const u8 *tmp;
740 u32 freq;
741 int channel_number = -1;
742
743 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
744 if (tmp && tmp[1] == 1) {
745 channel_number = tmp[2];
746 } else {
747 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
748 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
749 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
750
751 channel_number = htop->primary_chan;
752 }
753 }
754
755 if (channel_number < 0)
756 return channel;
757
758 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
759 channel = ieee80211_get_channel(wiphy, freq);
760 if (!channel)
761 return NULL;
762 if (channel->flags & IEEE80211_CHAN_DISABLED)
763 return NULL;
764 return channel;
765}
766
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200767struct cfg80211_bss*
768cfg80211_inform_bss(struct wiphy *wiphy,
769 struct ieee80211_channel *channel,
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100770 const u8 *bssid, u64 tsf, u16 capability,
771 u16 beacon_interval, const u8 *ie, size_t ielen,
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200772 s32 signal, gfp_t gfp)
773{
Johannes Berg9caf0362012-11-29 01:25:20 +0100774 struct cfg80211_bss_ies *ies;
775 struct cfg80211_internal_bss tmp = {}, *res;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200776
777 if (WARN_ON(!wiphy))
778 return NULL;
779
Sujith22fe88d2010-05-13 10:34:08 +0530780 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200781 (signal < 0 || signal > 100)))
782 return NULL;
783
Johannes Berg0172bb72012-11-23 14:23:30 +0100784 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
785 if (!channel)
786 return NULL;
787
Johannes Berg9caf0362012-11-29 01:25:20 +0100788 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
789 tmp.pub.channel = channel;
790 tmp.pub.signal = signal;
791 tmp.pub.tsf = tsf;
792 tmp.pub.beacon_interval = beacon_interval;
793 tmp.pub.capability = capability;
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200794 /*
795 * Since we do not know here whether the IEs are from a Beacon or Probe
796 * Response frame, we need to pick one of the options and only use it
797 * with the driver that does not provide the full Beacon/Probe Response
798 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg9caf0362012-11-29 01:25:20 +0100799 * override the iies pointer should we have received an earlier
800 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200801 *
802 * The initial buffer for the IEs is allocated with the BSS entry and
803 * is located after the private area.
804 */
Johannes Berg9caf0362012-11-29 01:25:20 +0100805 ies = kmalloc(sizeof(*ies) + ielen, gfp);
806 if (!ies)
807 return NULL;
808 ies->len = ielen;
809 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200810
Johannes Berg9caf0362012-11-29 01:25:20 +0100811 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
812 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200813
Johannes Berg9caf0362012-11-29 01:25:20 +0100814 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200815 if (!res)
816 return NULL;
817
818 if (res->pub.capability & WLAN_CAPABILITY_ESS)
819 regulatory_hint_found_beacon(wiphy, channel, gfp);
820
Beni Lev4ee3e062012-08-27 12:49:39 +0300821 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200822 /* cfg80211_bss_update gives us a referenced result */
823 return &res->pub;
824}
825EXPORT_SYMBOL(cfg80211_inform_bss);
826
Johannes Berg2a519312009-02-10 21:25:55 +0100827struct cfg80211_bss *
828cfg80211_inform_bss_frame(struct wiphy *wiphy,
829 struct ieee80211_channel *channel,
830 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100831 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100832{
Johannes Berg9caf0362012-11-29 01:25:20 +0100833 struct cfg80211_internal_bss tmp = {}, *res;
834 struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100835 size_t ielen = len - offsetof(struct ieee80211_mgmt,
836 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100837
Johannes Berg0172bb72012-11-23 14:23:30 +0100838 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
839 offsetof(struct ieee80211_mgmt, u.beacon.variable));
840
Beni Lev4ee3e062012-08-27 12:49:39 +0300841 trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
842
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100843 if (WARN_ON(!mgmt))
844 return NULL;
845
846 if (WARN_ON(!wiphy))
847 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100848
Sujith22fe88d2010-05-13 10:34:08 +0530849 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300850 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100851 return NULL;
852
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100853 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100854 return NULL;
855
Johannes Berg0172bb72012-11-23 14:23:30 +0100856 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
857 ielen, channel);
858 if (!channel)
859 return NULL;
860
Johannes Berg9caf0362012-11-29 01:25:20 +0100861 ies = kmalloc(sizeof(*ies) + ielen, gfp);
862 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +0100863 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100864 ies->len = ielen;
865 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +0100866
Johannes Berg9caf0362012-11-29 01:25:20 +0100867 if (ieee80211_is_probe_resp(mgmt->frame_control))
868 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
869 else
870 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
871 rcu_assign_pointer(tmp.pub.ies, ies);
872
873 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
874 tmp.pub.channel = channel;
875 tmp.pub.signal = signal;
876 tmp.pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
877 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
878 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Johannes Berg2a519312009-02-10 21:25:55 +0100879
Johannes Berg9caf0362012-11-29 01:25:20 +0100880 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Johannes Berg2a519312009-02-10 21:25:55 +0100881 if (!res)
882 return NULL;
883
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500884 if (res->pub.capability & WLAN_CAPABILITY_ESS)
885 regulatory_hint_found_beacon(wiphy, channel, gfp);
886
Beni Lev4ee3e062012-08-27 12:49:39 +0300887 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100888 /* cfg80211_bss_update gives us a referenced result */
889 return &res->pub;
890}
891EXPORT_SYMBOL(cfg80211_inform_bss_frame);
892
Johannes Berg4c0c0b72012-01-20 13:55:26 +0100893void cfg80211_ref_bss(struct cfg80211_bss *pub)
894{
895 struct cfg80211_internal_bss *bss;
896
897 if (!pub)
898 return;
899
900 bss = container_of(pub, struct cfg80211_internal_bss, pub);
901 kref_get(&bss->ref);
902}
903EXPORT_SYMBOL(cfg80211_ref_bss);
904
Johannes Berg2a519312009-02-10 21:25:55 +0100905void cfg80211_put_bss(struct cfg80211_bss *pub)
906{
907 struct cfg80211_internal_bss *bss;
908
909 if (!pub)
910 return;
911
912 bss = container_of(pub, struct cfg80211_internal_bss, pub);
913 kref_put(&bss->ref, bss_release);
914}
915EXPORT_SYMBOL(cfg80211_put_bss);
916
Johannes Bergd491af12009-02-10 21:25:58 +0100917void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
918{
919 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
920 struct cfg80211_internal_bss *bss;
921
922 if (WARN_ON(!pub))
923 return;
924
925 bss = container_of(pub, struct cfg80211_internal_bss, pub);
926
927 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +0200928 if (!list_empty(&bss->list)) {
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300929 __cfg80211_unlink_bss(dev, bss);
Johannes Berg32073902010-10-06 21:18:04 +0200930 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +0200931 }
Johannes Bergd491af12009-02-10 21:25:58 +0100932 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +0100933}
934EXPORT_SYMBOL(cfg80211_unlink_bss);
935
Johannes Berg3d23e342009-09-29 23:27:28 +0200936#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100937int cfg80211_wext_siwscan(struct net_device *dev,
938 struct iw_request_info *info,
939 union iwreq_data *wrqu, char *extra)
940{
941 struct cfg80211_registered_device *rdev;
942 struct wiphy *wiphy;
943 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100944 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100945 int i, err, n_channels = 0;
946 enum ieee80211_band band;
947
948 if (!netif_running(dev))
949 return -ENETDOWN;
950
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200951 if (wrqu->data.length == sizeof(struct iw_scan_req))
952 wreq = (struct iw_scan_req *)extra;
953
Johannes Berg463d0182009-07-14 00:33:35 +0200954 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100955
956 if (IS_ERR(rdev))
957 return PTR_ERR(rdev);
958
959 if (rdev->scan_req) {
960 err = -EBUSY;
961 goto out;
962 }
963
964 wiphy = &rdev->wiphy;
965
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200966 /* Determine number of channels, needed to allocate creq */
967 if (wreq && wreq->num_channels)
968 n_channels = wreq->num_channels;
969 else {
970 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
971 if (wiphy->bands[band])
972 n_channels += wiphy->bands[band]->n_channels;
973 }
Johannes Berg2a519312009-02-10 21:25:55 +0100974
975 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
976 n_channels * sizeof(void *),
977 GFP_ATOMIC);
978 if (!creq) {
979 err = -ENOMEM;
980 goto out;
981 }
982
983 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +0200984 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +0200985 /* SSIDs come after channels */
986 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +0100987 creq->n_channels = n_channels;
988 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -0700989 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100990
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200991 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +0100992 i = 0;
993 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
994 int j;
Johannes Berg584991d2009-11-02 13:32:03 +0100995
Johannes Berg2a519312009-02-10 21:25:55 +0100996 if (!wiphy->bands[band])
997 continue;
Johannes Berg584991d2009-11-02 13:32:03 +0100998
Johannes Berg2a519312009-02-10 21:25:55 +0100999 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001000 /* ignore disabled channels */
1001 if (wiphy->bands[band]->channels[j].flags &
1002 IEEE80211_CHAN_DISABLED)
1003 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001004
1005 /* If we have a wireless request structure and the
1006 * wireless request specifies frequencies, then search
1007 * for the matching hardware channel.
1008 */
1009 if (wreq && wreq->num_channels) {
1010 int k;
1011 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1012 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +02001013 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001014 if (wext_freq == wiphy_freq)
1015 goto wext_freq_found;
1016 }
1017 goto wext_freq_not_found;
1018 }
1019
1020 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001021 creq->channels[i] = &wiphy->bands[band]->channels[j];
1022 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001023 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001024 }
1025 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001026 /* No channels found? */
1027 if (!i) {
1028 err = -EINVAL;
1029 goto out;
1030 }
Johannes Berg2a519312009-02-10 21:25:55 +01001031
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001032 /* Set real number of channels specified in creq->channels[] */
1033 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001034
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001035 /* translate "Scan for SSID" request */
1036 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001037 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001038 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1039 err = -EINVAL;
1040 goto out;
1041 }
Johannes Berg2a519312009-02-10 21:25:55 +01001042 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1043 creq->ssids[0].ssid_len = wreq->essid_len;
1044 }
1045 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1046 creq->n_ssids = 0;
1047 }
1048
Johannes Berg34850ab2011-07-18 18:08:35 +02001049 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001050 if (wiphy->bands[i])
1051 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001052
Johannes Berg2a519312009-02-10 21:25:55 +01001053 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001054 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001055 if (err) {
1056 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001057 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001058 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001059 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001060 /* creq now owned by driver */
1061 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001062 dev_hold(dev);
1063 }
Johannes Berg2a519312009-02-10 21:25:55 +01001064 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001065 kfree(creq);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001066 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001067 return err;
1068}
Johannes Bergba44cb72009-04-20 18:49:39 +02001069EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001070
1071static void ieee80211_scan_add_ies(struct iw_request_info *info,
Johannes Berg9caf0362012-11-29 01:25:20 +01001072 const struct cfg80211_bss_ies *ies,
Johannes Berg2a519312009-02-10 21:25:55 +01001073 char **current_ev, char *end_buf)
1074{
Johannes Berg9caf0362012-11-29 01:25:20 +01001075 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +01001076 struct iw_event iwe;
1077
Johannes Berg9caf0362012-11-29 01:25:20 +01001078 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +01001079 return;
1080
1081 /*
1082 * If needed, fragment the IEs buffer (at IE boundaries) into short
1083 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1084 */
Johannes Berg9caf0362012-11-29 01:25:20 +01001085 pos = ies->data;
1086 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001087
1088 while (end - pos > IW_GENERIC_IE_MAX) {
1089 next = pos + 2 + pos[1];
1090 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1091 next = next + 2 + next[1];
1092
1093 memset(&iwe, 0, sizeof(iwe));
1094 iwe.cmd = IWEVGENIE;
1095 iwe.u.data.length = next - pos;
1096 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001097 end_buf, &iwe,
1098 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001099
1100 pos = next;
1101 }
1102
1103 if (end > pos) {
1104 memset(&iwe, 0, sizeof(iwe));
1105 iwe.cmd = IWEVGENIE;
1106 iwe.u.data.length = end - pos;
1107 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001108 end_buf, &iwe,
1109 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001110 }
1111}
1112
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001113static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
1114{
1115 unsigned long end = jiffies;
1116
1117 if (end >= start)
1118 return jiffies_to_msecs(end - start);
1119
1120 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
1121}
Johannes Berg2a519312009-02-10 21:25:55 +01001122
1123static char *
Johannes Berg77965c92009-02-18 18:45:06 +01001124ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1125 struct cfg80211_internal_bss *bss, char *current_ev,
1126 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001127{
Johannes Berg9caf0362012-11-29 01:25:20 +01001128 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01001129 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01001130 const u8 *ie;
Johannes Berg2a519312009-02-10 21:25:55 +01001131 u8 *buf, *cfg, *p;
Johannes Berg9caf0362012-11-29 01:25:20 +01001132 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001133 bool ismesh = false;
1134
1135 memset(&iwe, 0, sizeof(iwe));
1136 iwe.cmd = SIOCGIWAP;
1137 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1138 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1139 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1140 IW_EV_ADDR_LEN);
1141
1142 memset(&iwe, 0, sizeof(iwe));
1143 iwe.cmd = SIOCGIWFREQ;
1144 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1145 iwe.u.freq.e = 0;
1146 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1147 IW_EV_FREQ_LEN);
1148
1149 memset(&iwe, 0, sizeof(iwe));
1150 iwe.cmd = SIOCGIWFREQ;
1151 iwe.u.freq.m = bss->pub.channel->center_freq;
1152 iwe.u.freq.e = 6;
1153 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1154 IW_EV_FREQ_LEN);
1155
Johannes Berg77965c92009-02-18 18:45:06 +01001156 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01001157 memset(&iwe, 0, sizeof(iwe));
1158 iwe.cmd = IWEVQUAL;
1159 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1160 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01001161 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +01001162 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01001163 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01001164 sig = bss->pub.signal / 100;
1165 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001166 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01001167 if (sig < -110) /* rather bad */
1168 sig = -110;
1169 else if (sig > -40) /* perfect */
1170 sig = -40;
1171 /* will give a range of 0 .. 70 */
1172 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01001173 break;
1174 case CFG80211_SIGNAL_TYPE_UNSPEC:
1175 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01001176 /* will give range 0 .. 100 */
1177 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01001178 break;
1179 default:
1180 /* not reached */
1181 break;
1182 }
1183 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1184 &iwe, IW_EV_QUAL_LEN);
1185 }
1186
1187 memset(&iwe, 0, sizeof(iwe));
1188 iwe.cmd = SIOCGIWENCODE;
1189 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1190 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1191 else
1192 iwe.u.data.flags = IW_ENCODE_DISABLED;
1193 iwe.u.data.length = 0;
1194 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1195 &iwe, "");
1196
Johannes Berg9caf0362012-11-29 01:25:20 +01001197 rcu_read_lock();
1198 ies = rcu_dereference(bss->pub.ies);
1199 if (ies) {
1200 rem = ies->len;
1201 ie = ies->data;
1202 } else {
1203 rem = 0;
1204 ie = NULL;
1205 }
1206
1207 while (ies && rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01001208 /* invalid data */
1209 if (ie[1] > rem - 2)
1210 break;
1211
1212 switch (ie[0]) {
1213 case WLAN_EID_SSID:
1214 memset(&iwe, 0, sizeof(iwe));
1215 iwe.cmd = SIOCGIWESSID;
1216 iwe.u.data.length = ie[1];
1217 iwe.u.data.flags = 1;
1218 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001219 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001220 break;
1221 case WLAN_EID_MESH_ID:
1222 memset(&iwe, 0, sizeof(iwe));
1223 iwe.cmd = SIOCGIWESSID;
1224 iwe.u.data.length = ie[1];
1225 iwe.u.data.flags = 1;
1226 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001227 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001228 break;
1229 case WLAN_EID_MESH_CONFIG:
1230 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001231 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001232 break;
1233 buf = kmalloc(50, GFP_ATOMIC);
1234 if (!buf)
1235 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01001236 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01001237 memset(&iwe, 0, sizeof(iwe));
1238 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001239 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1240 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001241 iwe.u.data.length = strlen(buf);
1242 current_ev = iwe_stream_add_point(info, current_ev,
1243 end_buf,
1244 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001245 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1246 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001247 iwe.u.data.length = strlen(buf);
1248 current_ev = iwe_stream_add_point(info, current_ev,
1249 end_buf,
1250 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001251 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1252 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001253 iwe.u.data.length = strlen(buf);
1254 current_ev = iwe_stream_add_point(info, current_ev,
1255 end_buf,
1256 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001257 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01001258 iwe.u.data.length = strlen(buf);
1259 current_ev = iwe_stream_add_point(info, current_ev,
1260 end_buf,
1261 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001262 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1263 iwe.u.data.length = strlen(buf);
1264 current_ev = iwe_stream_add_point(info, current_ev,
1265 end_buf,
1266 &iwe, buf);
1267 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1268 iwe.u.data.length = strlen(buf);
1269 current_ev = iwe_stream_add_point(info, current_ev,
1270 end_buf,
1271 &iwe, buf);
1272 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001273 iwe.u.data.length = strlen(buf);
1274 current_ev = iwe_stream_add_point(info, current_ev,
1275 end_buf,
1276 &iwe, buf);
1277 kfree(buf);
1278 break;
1279 case WLAN_EID_SUPP_RATES:
1280 case WLAN_EID_EXT_SUPP_RATES:
1281 /* display all supported rates in readable format */
1282 p = current_ev + iwe_stream_lcp_len(info);
1283
1284 memset(&iwe, 0, sizeof(iwe));
1285 iwe.cmd = SIOCGIWRATE;
1286 /* Those two flags are ignored... */
1287 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1288
1289 for (i = 0; i < ie[1]; i++) {
1290 iwe.u.bitrate.value =
1291 ((ie[i + 2] & 0x7f) * 500000);
1292 p = iwe_stream_add_value(info, current_ev, p,
1293 end_buf, &iwe, IW_EV_PARAM_LEN);
1294 }
1295 current_ev = p;
1296 break;
1297 }
1298 rem -= ie[1] + 2;
1299 ie += ie[1] + 2;
1300 }
1301
Joe Perchesf64f9e72009-11-29 16:55:45 -08001302 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1303 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001304 memset(&iwe, 0, sizeof(iwe));
1305 iwe.cmd = SIOCGIWMODE;
1306 if (ismesh)
1307 iwe.u.mode = IW_MODE_MESH;
1308 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1309 iwe.u.mode = IW_MODE_MASTER;
1310 else
1311 iwe.u.mode = IW_MODE_ADHOC;
1312 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1313 &iwe, IW_EV_UINT_LEN);
1314 }
1315
1316 buf = kmalloc(30, GFP_ATOMIC);
1317 if (buf) {
1318 memset(&iwe, 0, sizeof(iwe));
1319 iwe.cmd = IWEVCUSTOM;
1320 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1321 iwe.u.data.length = strlen(buf);
1322 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1323 &iwe, buf);
1324 memset(&iwe, 0, sizeof(iwe));
1325 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001326 sprintf(buf, " Last beacon: %ums ago",
1327 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001328 iwe.u.data.length = strlen(buf);
1329 current_ev = iwe_stream_add_point(info, current_ev,
1330 end_buf, &iwe, buf);
1331 kfree(buf);
1332 }
1333
Johannes Berg9caf0362012-11-29 01:25:20 +01001334 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1335 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01001336
1337 return current_ev;
1338}
1339
1340
1341static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1342 struct iw_request_info *info,
1343 char *buf, size_t len)
1344{
1345 char *current_ev = buf;
1346 char *end_buf = buf + len;
1347 struct cfg80211_internal_bss *bss;
1348
1349 spin_lock_bh(&dev->bss_lock);
1350 cfg80211_bss_expire(dev);
1351
1352 list_for_each_entry(bss, &dev->bss_list, list) {
1353 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1354 spin_unlock_bh(&dev->bss_lock);
1355 return -E2BIG;
1356 }
Johannes Berg77965c92009-02-18 18:45:06 +01001357 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1358 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001359 }
1360 spin_unlock_bh(&dev->bss_lock);
1361 return current_ev - buf;
1362}
1363
1364
1365int cfg80211_wext_giwscan(struct net_device *dev,
1366 struct iw_request_info *info,
1367 struct iw_point *data, char *extra)
1368{
1369 struct cfg80211_registered_device *rdev;
1370 int res;
1371
1372 if (!netif_running(dev))
1373 return -ENETDOWN;
1374
Johannes Berg463d0182009-07-14 00:33:35 +02001375 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001376
1377 if (IS_ERR(rdev))
1378 return PTR_ERR(rdev);
1379
1380 if (rdev->scan_req) {
1381 res = -EAGAIN;
1382 goto out;
1383 }
1384
1385 res = ieee80211_scan_results(rdev, info, extra, data->length);
1386 data->length = 0;
1387 if (res >= 0) {
1388 data->length = res;
1389 res = 0;
1390 }
1391
1392 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001393 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001394 return res;
1395}
Johannes Bergba44cb72009-04-20 18:49:39 +02001396EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001397#endif