blob: a8d5a9a07e491aa145d71ffc911765b48000ef04 [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"
Johannes Berg2a519312009-02-10 21:25:55 +010020
Rajkumar Manoharanf9616e02012-04-13 16:38:40 +053021#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010022
Amitkumar Karware8e27c62012-10-11 21:03:33 -070023static void bss_release(struct kref *ref)
24{
25 struct cfg80211_internal_bss *bss;
26
27 bss = container_of(ref, struct cfg80211_internal_bss, ref);
28 if (bss->pub.free_priv)
29 bss->pub.free_priv(&bss->pub);
30
31 if (bss->beacon_ies_allocated)
32 kfree(bss->pub.beacon_ies);
33 if (bss->proberesp_ies_allocated)
34 kfree(bss->pub.proberesp_ies);
35
36 BUG_ON(atomic_read(&bss->hold));
37
38 kfree(bss);
39}
40
41/* must hold dev->bss_lock! */
42static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
43 struct cfg80211_internal_bss *bss)
44{
45 list_del_init(&bss->list);
46 rb_erase(&bss->rbn, &dev->bss_tree);
47 kref_put(&bss->ref, bss_release);
48}
49
Sam Leffler15d60302012-10-11 21:03:34 -070050/* must hold dev->bss_lock! */
51static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
52 unsigned long expire_time)
53{
54 struct cfg80211_internal_bss *bss, *tmp;
55 bool expired = false;
56
57 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
58 if (atomic_read(&bss->hold))
59 continue;
60 if (!time_after(expire_time, bss->ts))
61 continue;
62
63 __cfg80211_unlink_bss(dev, bss);
64 expired = true;
65 }
66
67 if (expired)
68 dev->bss_generation++;
69}
70
Johannes Berg01a0ac42009-08-20 21:36:16 +020071void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +010072{
Johannes Berg667503d2009-07-07 03:56:11 +020073 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +020074 struct wireless_dev *wdev;
Johannes Berg3d23e342009-09-29 23:27:28 +020075#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +010076 union iwreq_data wrqu;
77#endif
78
Johannes Berg01a0ac42009-08-20 21:36:16 +020079 ASSERT_RDEV_LOCK(rdev);
80
Johannes Berg667503d2009-07-07 03:56:11 +020081 request = rdev->scan_req;
82
Johannes Berg01a0ac42009-08-20 21:36:16 +020083 if (!request)
84 return;
85
Johannes Bergfd014282012-06-18 19:17:03 +020086 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +010087
Johannes Berg6829c872009-07-02 09:13:27 +020088 /*
89 * This must be before sending the other events!
90 * Otherwise, wpa_supplicant gets completely confused with
91 * wext events.
92 */
Johannes Bergfd014282012-06-18 19:17:03 +020093 if (wdev->netdev)
94 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +020095
Sam Leffler15d60302012-10-11 21:03:34 -070096 if (request->aborted) {
Johannes Bergfd014282012-06-18 19:17:03 +020097 nl80211_send_scan_aborted(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -070098 } else {
99 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
100 /* flush entries from previous scans */
101 spin_lock_bh(&rdev->bss_lock);
102 __cfg80211_bss_expire(rdev, request->scan_start);
103 spin_unlock_bh(&rdev->bss_lock);
104 }
Johannes Bergfd014282012-06-18 19:17:03 +0200105 nl80211_send_scan_done(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700106 }
Johannes Berg2a519312009-02-10 21:25:55 +0100107
Johannes Berg3d23e342009-09-29 23:27:28 +0200108#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200109 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100110 memset(&wrqu, 0, sizeof(wrqu));
111
Johannes Bergfd014282012-06-18 19:17:03 +0200112 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100113 }
114#endif
115
Johannes Bergfd014282012-06-18 19:17:03 +0200116 if (wdev->netdev)
117 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100118
Johannes Berg36e6fea2009-08-12 22:21:21 +0200119 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200120
121 /*
122 * OK. If this is invoked with "leak" then we can't
123 * free this ... but we've cleaned it up anyway. The
124 * driver failed to call the scan_done callback, so
125 * all bets are off, it might still be trying to use
126 * the scan request or not ... if it accesses the dev
127 * in there (it shouldn't anyway) then it may crash.
128 */
129 if (!leak)
130 kfree(request);
Johannes Berg2a519312009-02-10 21:25:55 +0100131}
Johannes Berg667503d2009-07-07 03:56:11 +0200132
Johannes Berg36e6fea2009-08-12 22:21:21 +0200133void __cfg80211_scan_done(struct work_struct *wk)
134{
135 struct cfg80211_registered_device *rdev;
136
137 rdev = container_of(wk, struct cfg80211_registered_device,
138 scan_done_wk);
139
140 cfg80211_lock_rdev(rdev);
Johannes Berg01a0ac42009-08-20 21:36:16 +0200141 ___cfg80211_scan_done(rdev, false);
Johannes Berg36e6fea2009-08-12 22:21:21 +0200142 cfg80211_unlock_rdev(rdev);
143}
144
Johannes Berg667503d2009-07-07 03:56:11 +0200145void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
146{
Johannes Berg667503d2009-07-07 03:56:11 +0200147 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
148
149 request->aborted = aborted;
Alban Browaeyse60d7442009-11-25 15:13:00 +0100150 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200151}
Johannes Berg2a519312009-02-10 21:25:55 +0100152EXPORT_SYMBOL(cfg80211_scan_done);
153
Luciano Coelho807f8a82011-05-11 17:09:35 +0300154void __cfg80211_sched_scan_results(struct work_struct *wk)
155{
156 struct cfg80211_registered_device *rdev;
Sam Leffler15d60302012-10-11 21:03:34 -0700157 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300158
159 rdev = container_of(wk, struct cfg80211_registered_device,
160 sched_scan_results_wk);
161
Sam Leffler15d60302012-10-11 21:03:34 -0700162 request = rdev->sched_scan_req;
163
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300164 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300165
166 /* we don't have sched_scan_req anymore if the scan is stopping */
Sam Leffler15d60302012-10-11 21:03:34 -0700167 if (request) {
168 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
169 /* flush entries from previous scans */
170 spin_lock_bh(&rdev->bss_lock);
171 __cfg80211_bss_expire(rdev, request->scan_start);
172 spin_unlock_bh(&rdev->bss_lock);
173 request->scan_start =
174 jiffies + msecs_to_jiffies(request->interval);
175 }
176 nl80211_send_sched_scan_results(rdev, request->dev);
177 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300178
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300179 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300180}
181
182void cfg80211_sched_scan_results(struct wiphy *wiphy)
183{
184 /* ignore if we're not scanning */
185 if (wiphy_to_dev(wiphy)->sched_scan_req)
186 queue_work(cfg80211_wq,
187 &wiphy_to_dev(wiphy)->sched_scan_results_wk);
188}
189EXPORT_SYMBOL(cfg80211_sched_scan_results);
190
Luciano Coelho85a99942011-05-12 16:28:29 +0300191void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300192{
Luciano Coelho85a99942011-05-12 16:28:29 +0300193 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300194
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300195 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300196 __cfg80211_stop_sched_scan(rdev, true);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300197 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300198}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300199EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
200
201int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
202 bool driver_initiated)
203{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300204 struct net_device *dev;
205
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300206 lockdep_assert_held(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300207
208 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300209 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300210
211 dev = rdev->sched_scan_req->dev;
212
Luciano Coelho85a99942011-05-12 16:28:29 +0300213 if (!driver_initiated) {
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200214 int err = rdev->ops->sched_scan_stop(&rdev->wiphy, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300215 if (err)
216 return err;
217 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300218
219 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
220
221 kfree(rdev->sched_scan_req);
222 rdev->sched_scan_req = NULL;
223
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200224 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300225}
226
Johannes Berg2a519312009-02-10 21:25:55 +0100227/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500228void cfg80211_bss_age(struct cfg80211_registered_device *dev,
229 unsigned long age_secs)
230{
231 struct cfg80211_internal_bss *bss;
232 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
233
234 list_for_each_entry(bss, &dev->bss_list, list) {
235 bss->ts -= age_jiffies;
236 }
237}
238
Johannes Berg2a519312009-02-10 21:25:55 +0100239void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
240{
Sam Leffler15d60302012-10-11 21:03:34 -0700241 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100242}
243
Johannes Bergc21dbf92010-01-26 14:15:46 +0100244const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100245{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100246 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100247 len -= ies[1] + 2;
248 ies += ies[1] + 2;
249 }
250 if (len < 2)
251 return NULL;
252 if (len < 2 + ies[1])
253 return NULL;
254 return ies;
255}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100256EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100257
Eliad Peller0c28ec52011-09-15 11:53:01 +0300258const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
259 const u8 *ies, int len)
260{
261 struct ieee80211_vendor_ie *ie;
262 const u8 *pos = ies, *end = ies + len;
263 int ie_oui;
264
265 while (pos < end) {
266 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
267 end - pos);
268 if (!pos)
269 return NULL;
270
271 if (end - pos < sizeof(*ie))
272 return NULL;
273
274 ie = (struct ieee80211_vendor_ie *)pos;
275 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
276 if (ie_oui == oui && ie->oui_type == oui_type)
277 return pos;
278
279 pos += 2 + ie->len;
280 }
281 return NULL;
282}
283EXPORT_SYMBOL(cfg80211_find_vendor_ie);
284
Johannes Berg2a519312009-02-10 21:25:55 +0100285static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
286{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100287 const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
288 const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
Johannes Berg2a519312009-02-10 21:25:55 +0100289
Johannes Berg3b6ef632011-11-04 11:01:46 +0100290 /* equal if both missing */
Johannes Berg2a519312009-02-10 21:25:55 +0100291 if (!ie1 && !ie2)
292 return 0;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100293 /* sort missing IE before (left of) present IE */
294 if (!ie1)
Johannes Berg2a519312009-02-10 21:25:55 +0100295 return -1;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100296 if (!ie2)
297 return 1;
Johannes Berg2a519312009-02-10 21:25:55 +0100298
Johannes Berg3b6ef632011-11-04 11:01:46 +0100299 /* sort by length first, then by contents */
300 if (ie1[1] != ie2[1])
Johannes Berg2a519312009-02-10 21:25:55 +0100301 return ie2[1] - ie1[1];
Johannes Berg3b6ef632011-11-04 11:01:46 +0100302 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg2a519312009-02-10 21:25:55 +0100303}
304
305static bool is_bss(struct cfg80211_bss *a,
306 const u8 *bssid,
307 const u8 *ssid, size_t ssid_len)
308{
309 const u8 *ssidie;
310
Joe Perchesac422d32012-05-08 18:56:55 +0000311 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100312 return false;
313
Johannes Berg79420f02009-02-10 21:25:59 +0100314 if (!ssid)
315 return true;
316
Johannes Bergc21dbf92010-01-26 14:15:46 +0100317 ssidie = cfg80211_find_ie(WLAN_EID_SSID,
318 a->information_elements,
319 a->len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100320 if (!ssidie)
321 return false;
322 if (ssidie[1] != ssid_len)
323 return false;
324 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
325}
326
Eliad Peller333ba732011-05-29 15:53:20 +0300327static bool is_mesh_bss(struct cfg80211_bss *a)
328{
329 const u8 *ie;
330
331 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
332 return false;
333
334 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
335 a->information_elements,
336 a->len_information_elements);
337 if (!ie)
338 return false;
339
340 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
341 a->information_elements,
342 a->len_information_elements);
343 if (!ie)
344 return false;
345
346 return true;
347}
348
Johannes Berg2a519312009-02-10 21:25:55 +0100349static bool is_mesh(struct cfg80211_bss *a,
350 const u8 *meshid, size_t meshidlen,
351 const u8 *meshcfg)
352{
353 const u8 *ie;
354
Eliad Peller333ba732011-05-29 15:53:20 +0300355 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
Johannes Berg2a519312009-02-10 21:25:55 +0100356 return false;
357
Johannes Bergc21dbf92010-01-26 14:15:46 +0100358 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
359 a->information_elements,
360 a->len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100361 if (!ie)
362 return false;
363 if (ie[1] != meshidlen)
364 return false;
365 if (memcmp(ie + 2, meshid, meshidlen))
366 return false;
367
Johannes Bergc21dbf92010-01-26 14:15:46 +0100368 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
369 a->information_elements,
370 a->len_information_elements);
Johannes Bergcd3468b2009-07-29 22:07:44 +0200371 if (!ie)
372 return false;
Rui Paulo136cfa22009-11-18 18:40:00 +0000373 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +0100374 return false;
375
376 /*
377 * Ignore mesh capability (last two bytes of the IE) when
378 * comparing since that may differ between stations taking
379 * part in the same mesh.
380 */
Rui Paulo136cfa22009-11-18 18:40:00 +0000381 return memcmp(ie + 2, meshcfg,
382 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100383}
384
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100385static int cmp_bss_core(struct cfg80211_bss *a,
386 struct cfg80211_bss *b)
Johannes Berg2a519312009-02-10 21:25:55 +0100387{
388 int r;
389
390 if (a->channel != b->channel)
391 return b->channel->center_freq - a->channel->center_freq;
392
Eliad Peller333ba732011-05-29 15:53:20 +0300393 if (is_mesh_bss(a) && is_mesh_bss(b)) {
Johannes Berg2a519312009-02-10 21:25:55 +0100394 r = cmp_ies(WLAN_EID_MESH_ID,
395 a->information_elements,
396 a->len_information_elements,
397 b->information_elements,
398 b->len_information_elements);
399 if (r)
400 return r;
401 return cmp_ies(WLAN_EID_MESH_CONFIG,
402 a->information_elements,
403 a->len_information_elements,
404 b->information_elements,
405 b->len_information_elements);
406 }
407
Emmanuel Grumbachef9456a2012-04-30 10:23:36 +0300408 /*
409 * we can't use compare_ether_addr here since we need a < > operator.
410 * The binary return value of compare_ether_addr isn't enough
411 */
412 return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100413}
414
415static int cmp_bss(struct cfg80211_bss *a,
416 struct cfg80211_bss *b)
417{
418 int r;
419
420 r = cmp_bss_core(a, b);
Javier Cardona0a35d362011-05-04 10:24:56 -0700421 if (r)
422 return r;
423
Johannes Berg2a519312009-02-10 21:25:55 +0100424 return cmp_ies(WLAN_EID_SSID,
425 a->information_elements,
426 a->len_information_elements,
427 b->information_elements,
428 b->len_information_elements);
429}
430
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100431static int cmp_hidden_bss(struct cfg80211_bss *a,
432 struct cfg80211_bss *b)
433{
434 const u8 *ie1;
435 const u8 *ie2;
436 int i;
437 int r;
438
439 r = cmp_bss_core(a, b);
440 if (r)
441 return r;
442
443 ie1 = cfg80211_find_ie(WLAN_EID_SSID,
444 a->information_elements,
445 a->len_information_elements);
446 ie2 = cfg80211_find_ie(WLAN_EID_SSID,
447 b->information_elements,
448 b->len_information_elements);
449
450 /* Key comparator must use same algorithm in any rb-tree
451 * search function (order is important), otherwise ordering
452 * of items in the tree is broken and search gives incorrect
453 * results. This code uses same order as cmp_ies() does. */
454
455 /* sort missing IE before (left of) present IE */
456 if (!ie1)
457 return -1;
458 if (!ie2)
459 return 1;
460
461 /* zero-size SSID is used as an indication of the hidden bss */
462 if (!ie2[1])
463 return 0;
464
465 /* sort by length first, then by contents */
466 if (ie1[1] != ie2[1])
467 return ie2[1] - ie1[1];
468
469 /* zeroed SSID ie is another indication of a hidden bss */
470 for (i = 0; i < ie2[1]; i++)
471 if (ie2[i + 2])
472 return -1;
473
474 return 0;
475}
476
Johannes Berg2a519312009-02-10 21:25:55 +0100477struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
478 struct ieee80211_channel *channel,
479 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100480 const u8 *ssid, size_t ssid_len,
481 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100482{
483 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
484 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200485 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100486
487 spin_lock_bh(&dev->bss_lock);
488
489 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100490 if ((bss->pub.capability & capa_mask) != capa_val)
491 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100492 if (channel && bss->pub.channel != channel)
493 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200494 /* Don't get expired BSS structs */
495 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
496 !atomic_read(&bss->hold))
497 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100498 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
499 res = bss;
500 kref_get(&res->ref);
501 break;
502 }
503 }
504
505 spin_unlock_bh(&dev->bss_lock);
506 if (!res)
507 return NULL;
508 return &res->pub;
509}
510EXPORT_SYMBOL(cfg80211_get_bss);
511
512struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
513 struct ieee80211_channel *channel,
514 const u8 *meshid, size_t meshidlen,
515 const u8 *meshcfg)
516{
517 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
518 struct cfg80211_internal_bss *bss, *res = NULL;
519
520 spin_lock_bh(&dev->bss_lock);
521
522 list_for_each_entry(bss, &dev->bss_list, list) {
523 if (channel && bss->pub.channel != channel)
524 continue;
525 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
526 res = bss;
527 kref_get(&res->ref);
528 break;
529 }
530 }
531
532 spin_unlock_bh(&dev->bss_lock);
533 if (!res)
534 return NULL;
535 return &res->pub;
536}
537EXPORT_SYMBOL(cfg80211_get_mesh);
538
539
540static void rb_insert_bss(struct cfg80211_registered_device *dev,
541 struct cfg80211_internal_bss *bss)
542{
543 struct rb_node **p = &dev->bss_tree.rb_node;
544 struct rb_node *parent = NULL;
545 struct cfg80211_internal_bss *tbss;
546 int cmp;
547
548 while (*p) {
549 parent = *p;
550 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
551
552 cmp = cmp_bss(&bss->pub, &tbss->pub);
553
554 if (WARN_ON(!cmp)) {
555 /* will sort of leak this BSS */
556 return;
557 }
558
559 if (cmp < 0)
560 p = &(*p)->rb_left;
561 else
562 p = &(*p)->rb_right;
563 }
564
565 rb_link_node(&bss->rbn, parent, p);
566 rb_insert_color(&bss->rbn, &dev->bss_tree);
567}
568
569static struct cfg80211_internal_bss *
570rb_find_bss(struct cfg80211_registered_device *dev,
571 struct cfg80211_internal_bss *res)
572{
573 struct rb_node *n = dev->bss_tree.rb_node;
574 struct cfg80211_internal_bss *bss;
575 int r;
576
577 while (n) {
578 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
579 r = cmp_bss(&res->pub, &bss->pub);
580
581 if (r == 0)
582 return bss;
583 else if (r < 0)
584 n = n->rb_left;
585 else
586 n = n->rb_right;
587 }
588
589 return NULL;
590}
591
592static struct cfg80211_internal_bss *
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100593rb_find_hidden_bss(struct cfg80211_registered_device *dev,
594 struct cfg80211_internal_bss *res)
595{
596 struct rb_node *n = dev->bss_tree.rb_node;
597 struct cfg80211_internal_bss *bss;
598 int r;
599
600 while (n) {
601 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
602 r = cmp_hidden_bss(&res->pub, &bss->pub);
603
604 if (r == 0)
605 return bss;
606 else if (r < 0)
607 n = n->rb_left;
608 else
609 n = n->rb_right;
610 }
611
612 return NULL;
613}
614
615static void
616copy_hidden_ies(struct cfg80211_internal_bss *res,
617 struct cfg80211_internal_bss *hidden)
618{
619 if (unlikely(res->pub.beacon_ies))
620 return;
621 if (WARN_ON(!hidden->pub.beacon_ies))
622 return;
623
624 res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC);
625 if (unlikely(!res->pub.beacon_ies))
626 return;
627
628 res->beacon_ies_allocated = true;
629 res->pub.len_beacon_ies = hidden->pub.len_beacon_ies;
630 memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies,
631 res->pub.len_beacon_ies);
632}
633
634static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100635cfg80211_bss_update(struct cfg80211_registered_device *dev,
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200636 struct cfg80211_internal_bss *res)
Johannes Berg2a519312009-02-10 21:25:55 +0100637{
638 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100639
640 /*
641 * The reference to "res" is donated to this function.
642 */
643
644 if (WARN_ON(!res->pub.channel)) {
645 kref_put(&res->ref, bss_release);
646 return NULL;
647 }
648
649 res->ts = jiffies;
650
Johannes Berg2a519312009-02-10 21:25:55 +0100651 spin_lock_bh(&dev->bss_lock);
652
653 found = rb_find_bss(dev, res);
654
Johannes Bergcd1658f2009-04-16 15:00:58 +0200655 if (found) {
Johannes Berg2a519312009-02-10 21:25:55 +0100656 found->pub.beacon_interval = res->pub.beacon_interval;
657 found->pub.tsf = res->pub.tsf;
658 found->pub.signal = res->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100659 found->pub.capability = res->pub.capability;
660 found->ts = res->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200661
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200662 /* Update IEs */
663 if (res->pub.proberesp_ies) {
Johannes Bergcd1658f2009-04-16 15:00:58 +0200664 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200665 size_t ielen = res->pub.len_proberesp_ies;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200666
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200667 if (found->pub.proberesp_ies &&
668 !found->proberesp_ies_allocated &&
669 ksize(found) >= used + ielen) {
670 memcpy(found->pub.proberesp_ies,
671 res->pub.proberesp_ies, ielen);
672 found->pub.len_proberesp_ies = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200673 } else {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200674 u8 *ies = found->pub.proberesp_ies;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200675
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200676 if (found->proberesp_ies_allocated)
Michael Buesch273de922009-04-25 22:28:55 +0200677 ies = krealloc(ies, ielen, GFP_ATOMIC);
678 else
Johannes Bergcd1658f2009-04-16 15:00:58 +0200679 ies = kmalloc(ielen, GFP_ATOMIC);
680
681 if (ies) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200682 memcpy(ies, res->pub.proberesp_ies,
683 ielen);
684 found->proberesp_ies_allocated = true;
685 found->pub.proberesp_ies = ies;
686 found->pub.len_proberesp_ies = ielen;
687 }
688 }
689
690 /* Override possible earlier Beacon frame IEs */
691 found->pub.information_elements =
692 found->pub.proberesp_ies;
693 found->pub.len_information_elements =
694 found->pub.len_proberesp_ies;
695 }
696 if (res->pub.beacon_ies) {
697 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
698 size_t ielen = res->pub.len_beacon_ies;
Sven Neumann01123e22010-12-09 15:05:24 +0100699 bool information_elements_is_beacon_ies =
700 (found->pub.information_elements ==
701 found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200702
703 if (found->pub.beacon_ies &&
704 !found->beacon_ies_allocated &&
705 ksize(found) >= used + ielen) {
706 memcpy(found->pub.beacon_ies,
707 res->pub.beacon_ies, ielen);
708 found->pub.len_beacon_ies = ielen;
709 } else {
710 u8 *ies = found->pub.beacon_ies;
711
712 if (found->beacon_ies_allocated)
713 ies = krealloc(ies, ielen, GFP_ATOMIC);
714 else
715 ies = kmalloc(ielen, GFP_ATOMIC);
716
717 if (ies) {
718 memcpy(ies, res->pub.beacon_ies,
719 ielen);
720 found->beacon_ies_allocated = true;
721 found->pub.beacon_ies = ies;
722 found->pub.len_beacon_ies = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200723 }
724 }
Sven Neumann01123e22010-12-09 15:05:24 +0100725
726 /* Override IEs if they were from a beacon before */
727 if (information_elements_is_beacon_ies) {
728 found->pub.information_elements =
729 found->pub.beacon_ies;
730 found->pub.len_information_elements =
731 found->pub.len_beacon_ies;
732 }
Johannes Bergcd1658f2009-04-16 15:00:58 +0200733 }
734
Johannes Berg2a519312009-02-10 21:25:55 +0100735 kref_put(&res->ref, bss_release);
736 } else {
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100737 struct cfg80211_internal_bss *hidden;
738
739 /* First check if the beacon is a probe response from
740 * a hidden bss. If so, copy beacon ies (with nullified
741 * ssid) into the probe response bss entry (with real ssid).
742 * It is required basically for PSM implementation
743 * (probe responses do not contain tim ie) */
744
745 /* TODO: The code is not trying to update existing probe
746 * response bss entries when beacon ies are
747 * getting changed. */
748 hidden = rb_find_hidden_bss(dev, res);
749 if (hidden)
750 copy_hidden_ies(res, hidden);
751
Johannes Berg2a519312009-02-10 21:25:55 +0100752 /* this "consumes" the reference */
753 list_add_tail(&res->list, &dev->bss_list);
754 rb_insert_bss(dev, res);
755 found = res;
756 }
757
758 dev->bss_generation++;
759 spin_unlock_bh(&dev->bss_lock);
760
761 kref_get(&found->ref);
762 return found;
763}
764
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200765struct cfg80211_bss*
766cfg80211_inform_bss(struct wiphy *wiphy,
767 struct ieee80211_channel *channel,
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100768 const u8 *bssid, u64 tsf, u16 capability,
769 u16 beacon_interval, const u8 *ie, size_t ielen,
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200770 s32 signal, gfp_t gfp)
771{
772 struct cfg80211_internal_bss *res;
773 size_t privsz;
774
775 if (WARN_ON(!wiphy))
776 return NULL;
777
778 privsz = wiphy->bss_priv_size;
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
784 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
785 if (!res)
786 return NULL;
787
788 memcpy(res->pub.bssid, bssid, ETH_ALEN);
789 res->pub.channel = channel;
790 res->pub.signal = signal;
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100791 res->pub.tsf = tsf;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200792 res->pub.beacon_interval = beacon_interval;
793 res->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
799 * override the information_elements pointer should we have received an
800 * earlier indication of Probe Response data.
801 *
802 * The initial buffer for the IEs is allocated with the BSS entry and
803 * is located after the private area.
804 */
805 res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
806 memcpy(res->pub.beacon_ies, ie, ielen);
807 res->pub.len_beacon_ies = ielen;
808 res->pub.information_elements = res->pub.beacon_ies;
809 res->pub.len_information_elements = res->pub.len_beacon_ies;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200810
811 kref_init(&res->ref);
812
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200813 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200814 if (!res)
815 return NULL;
816
817 if (res->pub.capability & WLAN_CAPABILITY_ESS)
818 regulatory_hint_found_beacon(wiphy, channel, gfp);
819
820 /* cfg80211_bss_update gives us a referenced result */
821 return &res->pub;
822}
823EXPORT_SYMBOL(cfg80211_inform_bss);
824
Johannes Berg2a519312009-02-10 21:25:55 +0100825struct cfg80211_bss *
826cfg80211_inform_bss_frame(struct wiphy *wiphy,
827 struct ieee80211_channel *channel,
828 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100829 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100830{
831 struct cfg80211_internal_bss *res;
832 size_t ielen = len - offsetof(struct ieee80211_mgmt,
833 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100834 size_t privsz;
835
836 if (WARN_ON(!mgmt))
837 return NULL;
838
839 if (WARN_ON(!wiphy))
840 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100841
Sujith22fe88d2010-05-13 10:34:08 +0530842 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300843 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100844 return NULL;
845
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100846 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100847 return NULL;
848
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100849 privsz = wiphy->bss_priv_size;
850
Johannes Berg2a519312009-02-10 21:25:55 +0100851 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
852 if (!res)
853 return NULL;
854
855 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
856 res->pub.channel = channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100857 res->pub.signal = signal;
858 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
859 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
860 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200861 /*
862 * The initial buffer for the IEs is allocated with the BSS entry and
863 * is located after the private area.
864 */
865 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
866 res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
867 memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
868 ielen);
869 res->pub.len_proberesp_ies = ielen;
870 res->pub.information_elements = res->pub.proberesp_ies;
871 res->pub.len_information_elements = res->pub.len_proberesp_ies;
872 } else {
873 res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
874 memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
875 res->pub.len_beacon_ies = ielen;
876 res->pub.information_elements = res->pub.beacon_ies;
877 res->pub.len_information_elements = res->pub.len_beacon_ies;
878 }
Johannes Berg2a519312009-02-10 21:25:55 +0100879
880 kref_init(&res->ref);
881
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200882 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
Johannes Berg2a519312009-02-10 21:25:55 +0100883 if (!res)
884 return NULL;
885
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500886 if (res->pub.capability & WLAN_CAPABILITY_ESS)
887 regulatory_hint_found_beacon(wiphy, channel, gfp);
888
Johannes Berg2a519312009-02-10 21:25:55 +0100889 /* cfg80211_bss_update gives us a referenced result */
890 return &res->pub;
891}
892EXPORT_SYMBOL(cfg80211_inform_bss_frame);
893
Johannes Berg4c0c0b72012-01-20 13:55:26 +0100894void cfg80211_ref_bss(struct cfg80211_bss *pub)
895{
896 struct cfg80211_internal_bss *bss;
897
898 if (!pub)
899 return;
900
901 bss = container_of(pub, struct cfg80211_internal_bss, pub);
902 kref_get(&bss->ref);
903}
904EXPORT_SYMBOL(cfg80211_ref_bss);
905
Johannes Berg2a519312009-02-10 21:25:55 +0100906void cfg80211_put_bss(struct cfg80211_bss *pub)
907{
908 struct cfg80211_internal_bss *bss;
909
910 if (!pub)
911 return;
912
913 bss = container_of(pub, struct cfg80211_internal_bss, pub);
914 kref_put(&bss->ref, bss_release);
915}
916EXPORT_SYMBOL(cfg80211_put_bss);
917
Johannes Bergd491af12009-02-10 21:25:58 +0100918void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
919{
920 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
921 struct cfg80211_internal_bss *bss;
922
923 if (WARN_ON(!pub))
924 return;
925
926 bss = container_of(pub, struct cfg80211_internal_bss, pub);
927
928 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +0200929 if (!list_empty(&bss->list)) {
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300930 __cfg80211_unlink_bss(dev, bss);
Johannes Berg32073902010-10-06 21:18:04 +0200931 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +0200932 }
Johannes Bergd491af12009-02-10 21:25:58 +0100933 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +0100934}
935EXPORT_SYMBOL(cfg80211_unlink_bss);
936
Johannes Berg3d23e342009-09-29 23:27:28 +0200937#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100938int cfg80211_wext_siwscan(struct net_device *dev,
939 struct iw_request_info *info,
940 union iwreq_data *wrqu, char *extra)
941{
942 struct cfg80211_registered_device *rdev;
943 struct wiphy *wiphy;
944 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100945 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100946 int i, err, n_channels = 0;
947 enum ieee80211_band band;
948
949 if (!netif_running(dev))
950 return -ENETDOWN;
951
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200952 if (wrqu->data.length == sizeof(struct iw_scan_req))
953 wreq = (struct iw_scan_req *)extra;
954
Johannes Berg463d0182009-07-14 00:33:35 +0200955 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100956
957 if (IS_ERR(rdev))
958 return PTR_ERR(rdev);
959
960 if (rdev->scan_req) {
961 err = -EBUSY;
962 goto out;
963 }
964
965 wiphy = &rdev->wiphy;
966
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200967 /* Determine number of channels, needed to allocate creq */
968 if (wreq && wreq->num_channels)
969 n_channels = wreq->num_channels;
970 else {
971 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
972 if (wiphy->bands[band])
973 n_channels += wiphy->bands[band]->n_channels;
974 }
Johannes Berg2a519312009-02-10 21:25:55 +0100975
976 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
977 n_channels * sizeof(void *),
978 GFP_ATOMIC);
979 if (!creq) {
980 err = -ENOMEM;
981 goto out;
982 }
983
984 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +0200985 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +0200986 /* SSIDs come after channels */
987 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +0100988 creq->n_channels = n_channels;
989 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -0700990 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100991
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200992 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +0100993 i = 0;
994 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
995 int j;
Johannes Berg584991d2009-11-02 13:32:03 +0100996
Johannes Berg2a519312009-02-10 21:25:55 +0100997 if (!wiphy->bands[band])
998 continue;
Johannes Berg584991d2009-11-02 13:32:03 +0100999
Johannes Berg2a519312009-02-10 21:25:55 +01001000 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001001 /* ignore disabled channels */
1002 if (wiphy->bands[band]->channels[j].flags &
1003 IEEE80211_CHAN_DISABLED)
1004 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001005
1006 /* If we have a wireless request structure and the
1007 * wireless request specifies frequencies, then search
1008 * for the matching hardware channel.
1009 */
1010 if (wreq && wreq->num_channels) {
1011 int k;
1012 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1013 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +02001014 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001015 if (wext_freq == wiphy_freq)
1016 goto wext_freq_found;
1017 }
1018 goto wext_freq_not_found;
1019 }
1020
1021 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001022 creq->channels[i] = &wiphy->bands[band]->channels[j];
1023 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001024 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001025 }
1026 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001027 /* No channels found? */
1028 if (!i) {
1029 err = -EINVAL;
1030 goto out;
1031 }
Johannes Berg2a519312009-02-10 21:25:55 +01001032
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001033 /* Set real number of channels specified in creq->channels[] */
1034 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001035
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001036 /* translate "Scan for SSID" request */
1037 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001038 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001039 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1040 err = -EINVAL;
1041 goto out;
1042 }
Johannes Berg2a519312009-02-10 21:25:55 +01001043 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1044 creq->ssids[0].ssid_len = wreq->essid_len;
1045 }
1046 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1047 creq->n_ssids = 0;
1048 }
1049
Johannes Berg34850ab2011-07-18 18:08:35 +02001050 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001051 if (wiphy->bands[i])
1052 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001053
Johannes Berg2a519312009-02-10 21:25:55 +01001054 rdev->scan_req = creq;
Johannes Bergfd014282012-06-18 19:17:03 +02001055 err = rdev->ops->scan(wiphy, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001056 if (err) {
1057 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001058 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001059 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001060 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001061 /* creq now owned by driver */
1062 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001063 dev_hold(dev);
1064 }
Johannes Berg2a519312009-02-10 21:25:55 +01001065 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001066 kfree(creq);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001067 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001068 return err;
1069}
Johannes Bergba44cb72009-04-20 18:49:39 +02001070EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001071
1072static void ieee80211_scan_add_ies(struct iw_request_info *info,
1073 struct cfg80211_bss *bss,
1074 char **current_ev, char *end_buf)
1075{
1076 u8 *pos, *end, *next;
1077 struct iw_event iwe;
1078
1079 if (!bss->information_elements ||
1080 !bss->len_information_elements)
1081 return;
1082
1083 /*
1084 * If needed, fragment the IEs buffer (at IE boundaries) into short
1085 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1086 */
1087 pos = bss->information_elements;
1088 end = pos + bss->len_information_elements;
1089
1090 while (end - pos > IW_GENERIC_IE_MAX) {
1091 next = pos + 2 + pos[1];
1092 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1093 next = next + 2 + next[1];
1094
1095 memset(&iwe, 0, sizeof(iwe));
1096 iwe.cmd = IWEVGENIE;
1097 iwe.u.data.length = next - pos;
1098 *current_ev = iwe_stream_add_point(info, *current_ev,
1099 end_buf, &iwe, pos);
1100
1101 pos = next;
1102 }
1103
1104 if (end > pos) {
1105 memset(&iwe, 0, sizeof(iwe));
1106 iwe.cmd = IWEVGENIE;
1107 iwe.u.data.length = end - pos;
1108 *current_ev = iwe_stream_add_point(info, *current_ev,
1109 end_buf, &iwe, pos);
1110 }
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{
1128 struct iw_event iwe;
1129 u8 *buf, *cfg, *p;
1130 u8 *ie = bss->pub.information_elements;
Johannes Berga77b8552009-02-18 18:27:22 +01001131 int rem = bss->pub.len_information_elements, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001132 bool ismesh = false;
1133
1134 memset(&iwe, 0, sizeof(iwe));
1135 iwe.cmd = SIOCGIWAP;
1136 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1137 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1138 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1139 IW_EV_ADDR_LEN);
1140
1141 memset(&iwe, 0, sizeof(iwe));
1142 iwe.cmd = SIOCGIWFREQ;
1143 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1144 iwe.u.freq.e = 0;
1145 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1146 IW_EV_FREQ_LEN);
1147
1148 memset(&iwe, 0, sizeof(iwe));
1149 iwe.cmd = SIOCGIWFREQ;
1150 iwe.u.freq.m = bss->pub.channel->center_freq;
1151 iwe.u.freq.e = 6;
1152 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1153 IW_EV_FREQ_LEN);
1154
Johannes Berg77965c92009-02-18 18:45:06 +01001155 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01001156 memset(&iwe, 0, sizeof(iwe));
1157 iwe.cmd = IWEVQUAL;
1158 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1159 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01001160 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +01001161 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01001162 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01001163 sig = bss->pub.signal / 100;
1164 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001165 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01001166 if (sig < -110) /* rather bad */
1167 sig = -110;
1168 else if (sig > -40) /* perfect */
1169 sig = -40;
1170 /* will give a range of 0 .. 70 */
1171 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01001172 break;
1173 case CFG80211_SIGNAL_TYPE_UNSPEC:
1174 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01001175 /* will give range 0 .. 100 */
1176 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01001177 break;
1178 default:
1179 /* not reached */
1180 break;
1181 }
1182 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1183 &iwe, IW_EV_QUAL_LEN);
1184 }
1185
1186 memset(&iwe, 0, sizeof(iwe));
1187 iwe.cmd = SIOCGIWENCODE;
1188 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1189 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1190 else
1191 iwe.u.data.flags = IW_ENCODE_DISABLED;
1192 iwe.u.data.length = 0;
1193 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1194 &iwe, "");
1195
1196 while (rem >= 2) {
1197 /* invalid data */
1198 if (ie[1] > rem - 2)
1199 break;
1200
1201 switch (ie[0]) {
1202 case WLAN_EID_SSID:
1203 memset(&iwe, 0, sizeof(iwe));
1204 iwe.cmd = SIOCGIWESSID;
1205 iwe.u.data.length = ie[1];
1206 iwe.u.data.flags = 1;
1207 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1208 &iwe, ie + 2);
1209 break;
1210 case WLAN_EID_MESH_ID:
1211 memset(&iwe, 0, sizeof(iwe));
1212 iwe.cmd = SIOCGIWESSID;
1213 iwe.u.data.length = ie[1];
1214 iwe.u.data.flags = 1;
1215 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1216 &iwe, ie + 2);
1217 break;
1218 case WLAN_EID_MESH_CONFIG:
1219 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001220 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001221 break;
1222 buf = kmalloc(50, GFP_ATOMIC);
1223 if (!buf)
1224 break;
1225 cfg = ie + 2;
1226 memset(&iwe, 0, sizeof(iwe));
1227 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001228 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1229 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001230 iwe.u.data.length = strlen(buf);
1231 current_ev = iwe_stream_add_point(info, current_ev,
1232 end_buf,
1233 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001234 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1235 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001236 iwe.u.data.length = strlen(buf);
1237 current_ev = iwe_stream_add_point(info, current_ev,
1238 end_buf,
1239 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001240 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1241 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001242 iwe.u.data.length = strlen(buf);
1243 current_ev = iwe_stream_add_point(info, current_ev,
1244 end_buf,
1245 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001246 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
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, "Authentication ID: 0x%02X", cfg[4]);
1252 iwe.u.data.length = strlen(buf);
1253 current_ev = iwe_stream_add_point(info, current_ev,
1254 end_buf,
1255 &iwe, buf);
1256 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1257 iwe.u.data.length = strlen(buf);
1258 current_ev = iwe_stream_add_point(info, current_ev,
1259 end_buf,
1260 &iwe, buf);
1261 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001262 iwe.u.data.length = strlen(buf);
1263 current_ev = iwe_stream_add_point(info, current_ev,
1264 end_buf,
1265 &iwe, buf);
1266 kfree(buf);
1267 break;
1268 case WLAN_EID_SUPP_RATES:
1269 case WLAN_EID_EXT_SUPP_RATES:
1270 /* display all supported rates in readable format */
1271 p = current_ev + iwe_stream_lcp_len(info);
1272
1273 memset(&iwe, 0, sizeof(iwe));
1274 iwe.cmd = SIOCGIWRATE;
1275 /* Those two flags are ignored... */
1276 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1277
1278 for (i = 0; i < ie[1]; i++) {
1279 iwe.u.bitrate.value =
1280 ((ie[i + 2] & 0x7f) * 500000);
1281 p = iwe_stream_add_value(info, current_ev, p,
1282 end_buf, &iwe, IW_EV_PARAM_LEN);
1283 }
1284 current_ev = p;
1285 break;
1286 }
1287 rem -= ie[1] + 2;
1288 ie += ie[1] + 2;
1289 }
1290
Joe Perchesf64f9e72009-11-29 16:55:45 -08001291 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1292 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001293 memset(&iwe, 0, sizeof(iwe));
1294 iwe.cmd = SIOCGIWMODE;
1295 if (ismesh)
1296 iwe.u.mode = IW_MODE_MESH;
1297 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1298 iwe.u.mode = IW_MODE_MASTER;
1299 else
1300 iwe.u.mode = IW_MODE_ADHOC;
1301 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1302 &iwe, IW_EV_UINT_LEN);
1303 }
1304
1305 buf = kmalloc(30, GFP_ATOMIC);
1306 if (buf) {
1307 memset(&iwe, 0, sizeof(iwe));
1308 iwe.cmd = IWEVCUSTOM;
1309 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1310 iwe.u.data.length = strlen(buf);
1311 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1312 &iwe, buf);
1313 memset(&iwe, 0, sizeof(iwe));
1314 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001315 sprintf(buf, " Last beacon: %ums ago",
1316 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001317 iwe.u.data.length = strlen(buf);
1318 current_ev = iwe_stream_add_point(info, current_ev,
1319 end_buf, &iwe, buf);
1320 kfree(buf);
1321 }
1322
1323 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
1324
1325 return current_ev;
1326}
1327
1328
1329static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1330 struct iw_request_info *info,
1331 char *buf, size_t len)
1332{
1333 char *current_ev = buf;
1334 char *end_buf = buf + len;
1335 struct cfg80211_internal_bss *bss;
1336
1337 spin_lock_bh(&dev->bss_lock);
1338 cfg80211_bss_expire(dev);
1339
1340 list_for_each_entry(bss, &dev->bss_list, list) {
1341 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1342 spin_unlock_bh(&dev->bss_lock);
1343 return -E2BIG;
1344 }
Johannes Berg77965c92009-02-18 18:45:06 +01001345 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1346 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001347 }
1348 spin_unlock_bh(&dev->bss_lock);
1349 return current_ev - buf;
1350}
1351
1352
1353int cfg80211_wext_giwscan(struct net_device *dev,
1354 struct iw_request_info *info,
1355 struct iw_point *data, char *extra)
1356{
1357 struct cfg80211_registered_device *rdev;
1358 int res;
1359
1360 if (!netif_running(dev))
1361 return -ENETDOWN;
1362
Johannes Berg463d0182009-07-14 00:33:35 +02001363 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001364
1365 if (IS_ERR(rdev))
1366 return PTR_ERR(rdev);
1367
1368 if (rdev->scan_req) {
1369 res = -EAGAIN;
1370 goto out;
1371 }
1372
1373 res = ieee80211_scan_results(rdev, info, extra, data->length);
1374 data->length = 0;
1375 if (res >= 0) {
1376 data->length = res;
1377 res = 0;
1378 }
1379
1380 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001381 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001382 return res;
1383}
Johannes Bergba44cb72009-04-20 18:49:39 +02001384EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001385#endif