blob: a957077dd961f063f6ca9b6bd251ff684e9501e4 [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{
26 struct cfg80211_internal_bss *bss;
27
28 bss = container_of(ref, struct cfg80211_internal_bss, ref);
29 if (bss->pub.free_priv)
30 bss->pub.free_priv(&bss->pub);
31
32 if (bss->beacon_ies_allocated)
33 kfree(bss->pub.beacon_ies);
34 if (bss->proberesp_ies_allocated)
35 kfree(bss->pub.proberesp_ies);
36
37 BUG_ON(atomic_read(&bss->hold));
38
39 kfree(bss);
40}
41
42/* must hold dev->bss_lock! */
43static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
44 struct cfg80211_internal_bss *bss)
45{
46 list_del_init(&bss->list);
47 rb_erase(&bss->rbn, &dev->bss_tree);
48 kref_put(&bss->ref, bss_release);
49}
50
Sam Leffler15d60302012-10-11 21:03:34 -070051/* must hold dev->bss_lock! */
52static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
53 unsigned long expire_time)
54{
55 struct cfg80211_internal_bss *bss, *tmp;
56 bool expired = false;
57
58 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
59 if (atomic_read(&bss->hold))
60 continue;
61 if (!time_after(expire_time, bss->ts))
62 continue;
63
64 __cfg80211_unlink_bss(dev, bss);
65 expired = true;
66 }
67
68 if (expired)
69 dev->bss_generation++;
70}
71
Johannes Berg01a0ac42009-08-20 21:36:16 +020072void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +010073{
Johannes Berg667503d2009-07-07 03:56:11 +020074 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +020075 struct wireless_dev *wdev;
Johannes Berg3d23e342009-09-29 23:27:28 +020076#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +010077 union iwreq_data wrqu;
78#endif
79
Johannes Berg01a0ac42009-08-20 21:36:16 +020080 ASSERT_RDEV_LOCK(rdev);
81
Johannes Berg667503d2009-07-07 03:56:11 +020082 request = rdev->scan_req;
83
Johannes Berg01a0ac42009-08-20 21:36:16 +020084 if (!request)
85 return;
86
Johannes Bergfd014282012-06-18 19:17:03 +020087 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +010088
Johannes Berg6829c872009-07-02 09:13:27 +020089 /*
90 * This must be before sending the other events!
91 * Otherwise, wpa_supplicant gets completely confused with
92 * wext events.
93 */
Johannes Bergfd014282012-06-18 19:17:03 +020094 if (wdev->netdev)
95 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +020096
Sam Leffler15d60302012-10-11 21:03:34 -070097 if (request->aborted) {
Johannes Bergfd014282012-06-18 19:17:03 +020098 nl80211_send_scan_aborted(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -070099 } else {
100 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
101 /* flush entries from previous scans */
102 spin_lock_bh(&rdev->bss_lock);
103 __cfg80211_bss_expire(rdev, request->scan_start);
104 spin_unlock_bh(&rdev->bss_lock);
105 }
Johannes Bergfd014282012-06-18 19:17:03 +0200106 nl80211_send_scan_done(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700107 }
Johannes Berg2a519312009-02-10 21:25:55 +0100108
Johannes Berg3d23e342009-09-29 23:27:28 +0200109#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200110 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100111 memset(&wrqu, 0, sizeof(wrqu));
112
Johannes Bergfd014282012-06-18 19:17:03 +0200113 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100114 }
115#endif
116
Johannes Bergfd014282012-06-18 19:17:03 +0200117 if (wdev->netdev)
118 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100119
Johannes Berg36e6fea2009-08-12 22:21:21 +0200120 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200121
122 /*
123 * OK. If this is invoked with "leak" then we can't
124 * free this ... but we've cleaned it up anyway. The
125 * driver failed to call the scan_done callback, so
126 * all bets are off, it might still be trying to use
127 * the scan request or not ... if it accesses the dev
128 * in there (it shouldn't anyway) then it may crash.
129 */
130 if (!leak)
131 kfree(request);
Johannes Berg2a519312009-02-10 21:25:55 +0100132}
Johannes Berg667503d2009-07-07 03:56:11 +0200133
Johannes Berg36e6fea2009-08-12 22:21:21 +0200134void __cfg80211_scan_done(struct work_struct *wk)
135{
136 struct cfg80211_registered_device *rdev;
137
138 rdev = container_of(wk, struct cfg80211_registered_device,
139 scan_done_wk);
140
141 cfg80211_lock_rdev(rdev);
Johannes Berg01a0ac42009-08-20 21:36:16 +0200142 ___cfg80211_scan_done(rdev, false);
Johannes Berg36e6fea2009-08-12 22:21:21 +0200143 cfg80211_unlock_rdev(rdev);
144}
145
Johannes Berg667503d2009-07-07 03:56:11 +0200146void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
147{
Johannes Berg667503d2009-07-07 03:56:11 +0200148 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
149
150 request->aborted = aborted;
Alban Browaeyse60d7442009-11-25 15:13:00 +0100151 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200152}
Johannes Berg2a519312009-02-10 21:25:55 +0100153EXPORT_SYMBOL(cfg80211_scan_done);
154
Luciano Coelho807f8a82011-05-11 17:09:35 +0300155void __cfg80211_sched_scan_results(struct work_struct *wk)
156{
157 struct cfg80211_registered_device *rdev;
Sam Leffler15d60302012-10-11 21:03:34 -0700158 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300159
160 rdev = container_of(wk, struct cfg80211_registered_device,
161 sched_scan_results_wk);
162
Sam Leffler15d60302012-10-11 21:03:34 -0700163 request = rdev->sched_scan_req;
164
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300165 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300166
167 /* we don't have sched_scan_req anymore if the scan is stopping */
Sam Leffler15d60302012-10-11 21:03:34 -0700168 if (request) {
169 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
170 /* flush entries from previous scans */
171 spin_lock_bh(&rdev->bss_lock);
172 __cfg80211_bss_expire(rdev, request->scan_start);
173 spin_unlock_bh(&rdev->bss_lock);
174 request->scan_start =
175 jiffies + msecs_to_jiffies(request->interval);
176 }
177 nl80211_send_sched_scan_results(rdev, request->dev);
178 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300179
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300180 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300181}
182
183void cfg80211_sched_scan_results(struct wiphy *wiphy)
184{
185 /* ignore if we're not scanning */
186 if (wiphy_to_dev(wiphy)->sched_scan_req)
187 queue_work(cfg80211_wq,
188 &wiphy_to_dev(wiphy)->sched_scan_results_wk);
189}
190EXPORT_SYMBOL(cfg80211_sched_scan_results);
191
Luciano Coelho85a99942011-05-12 16:28:29 +0300192void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300193{
Luciano Coelho85a99942011-05-12 16:28:29 +0300194 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300195
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300196 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300197 __cfg80211_stop_sched_scan(rdev, true);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300198 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300199}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300200EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
201
202int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
203 bool driver_initiated)
204{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300205 struct net_device *dev;
206
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300207 lockdep_assert_held(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300208
209 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300210 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300211
212 dev = rdev->sched_scan_req->dev;
213
Luciano Coelho85a99942011-05-12 16:28:29 +0300214 if (!driver_initiated) {
Hila Gonene35e4d22012-06-27 17:19:42 +0300215 int err = rdev_sched_scan_stop(rdev, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300216 if (err)
217 return err;
218 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300219
220 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
221
222 kfree(rdev->sched_scan_req);
223 rdev->sched_scan_req = NULL;
224
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200225 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300226}
227
Johannes Berg2a519312009-02-10 21:25:55 +0100228/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500229void cfg80211_bss_age(struct cfg80211_registered_device *dev,
230 unsigned long age_secs)
231{
232 struct cfg80211_internal_bss *bss;
233 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
234
235 list_for_each_entry(bss, &dev->bss_list, list) {
236 bss->ts -= age_jiffies;
237 }
238}
239
Johannes Berg2a519312009-02-10 21:25:55 +0100240void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
241{
Sam Leffler15d60302012-10-11 21:03:34 -0700242 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100243}
244
Johannes Bergc21dbf92010-01-26 14:15:46 +0100245const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100246{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100247 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100248 len -= ies[1] + 2;
249 ies += ies[1] + 2;
250 }
251 if (len < 2)
252 return NULL;
253 if (len < 2 + ies[1])
254 return NULL;
255 return ies;
256}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100257EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100258
Eliad Peller0c28ec52011-09-15 11:53:01 +0300259const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
260 const u8 *ies, int len)
261{
262 struct ieee80211_vendor_ie *ie;
263 const u8 *pos = ies, *end = ies + len;
264 int ie_oui;
265
266 while (pos < end) {
267 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
268 end - pos);
269 if (!pos)
270 return NULL;
271
272 if (end - pos < sizeof(*ie))
273 return NULL;
274
275 ie = (struct ieee80211_vendor_ie *)pos;
276 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
277 if (ie_oui == oui && ie->oui_type == oui_type)
278 return pos;
279
280 pos += 2 + ie->len;
281 }
282 return NULL;
283}
284EXPORT_SYMBOL(cfg80211_find_vendor_ie);
285
Johannes Berg2a519312009-02-10 21:25:55 +0100286static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
287{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100288 const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
289 const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
Johannes Berg2a519312009-02-10 21:25:55 +0100290
Johannes Berg3b6ef632011-11-04 11:01:46 +0100291 /* equal if both missing */
Johannes Berg2a519312009-02-10 21:25:55 +0100292 if (!ie1 && !ie2)
293 return 0;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100294 /* sort missing IE before (left of) present IE */
295 if (!ie1)
Johannes Berg2a519312009-02-10 21:25:55 +0100296 return -1;
Johannes Berg3b6ef632011-11-04 11:01:46 +0100297 if (!ie2)
298 return 1;
Johannes Berg2a519312009-02-10 21:25:55 +0100299
Johannes Berg3b6ef632011-11-04 11:01:46 +0100300 /* sort by length first, then by contents */
301 if (ie1[1] != ie2[1])
Johannes Berg2a519312009-02-10 21:25:55 +0100302 return ie2[1] - ie1[1];
Johannes Berg3b6ef632011-11-04 11:01:46 +0100303 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg2a519312009-02-10 21:25:55 +0100304}
305
306static bool is_bss(struct cfg80211_bss *a,
307 const u8 *bssid,
308 const u8 *ssid, size_t ssid_len)
309{
310 const u8 *ssidie;
311
Joe Perchesac422d32012-05-08 18:56:55 +0000312 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100313 return false;
314
Johannes Berg79420f02009-02-10 21:25:59 +0100315 if (!ssid)
316 return true;
317
Johannes Bergc21dbf92010-01-26 14:15:46 +0100318 ssidie = cfg80211_find_ie(WLAN_EID_SSID,
319 a->information_elements,
320 a->len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100321 if (!ssidie)
322 return false;
323 if (ssidie[1] != ssid_len)
324 return false;
325 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
326}
327
Eliad Peller333ba732011-05-29 15:53:20 +0300328static bool is_mesh_bss(struct cfg80211_bss *a)
329{
330 const u8 *ie;
331
332 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
333 return false;
334
335 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
336 a->information_elements,
337 a->len_information_elements);
338 if (!ie)
339 return false;
340
341 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
342 a->information_elements,
343 a->len_information_elements);
344 if (!ie)
345 return false;
346
347 return true;
348}
349
Johannes Berg2a519312009-02-10 21:25:55 +0100350static bool is_mesh(struct cfg80211_bss *a,
351 const u8 *meshid, size_t meshidlen,
352 const u8 *meshcfg)
353{
354 const u8 *ie;
355
Eliad Peller333ba732011-05-29 15:53:20 +0300356 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
Johannes Berg2a519312009-02-10 21:25:55 +0100357 return false;
358
Johannes Bergc21dbf92010-01-26 14:15:46 +0100359 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
360 a->information_elements,
361 a->len_information_elements);
Johannes Berg2a519312009-02-10 21:25:55 +0100362 if (!ie)
363 return false;
364 if (ie[1] != meshidlen)
365 return false;
366 if (memcmp(ie + 2, meshid, meshidlen))
367 return false;
368
Johannes Bergc21dbf92010-01-26 14:15:46 +0100369 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
370 a->information_elements,
371 a->len_information_elements);
Johannes Bergcd3468b2009-07-29 22:07:44 +0200372 if (!ie)
373 return false;
Rui Paulo136cfa22009-11-18 18:40:00 +0000374 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +0100375 return false;
376
377 /*
378 * Ignore mesh capability (last two bytes of the IE) when
379 * comparing since that may differ between stations taking
380 * part in the same mesh.
381 */
Rui Paulo136cfa22009-11-18 18:40:00 +0000382 return memcmp(ie + 2, meshcfg,
383 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100384}
385
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100386static int cmp_bss_core(struct cfg80211_bss *a,
387 struct cfg80211_bss *b)
Johannes Berg2a519312009-02-10 21:25:55 +0100388{
389 int r;
390
391 if (a->channel != b->channel)
392 return b->channel->center_freq - a->channel->center_freq;
393
Eliad Peller333ba732011-05-29 15:53:20 +0300394 if (is_mesh_bss(a) && is_mesh_bss(b)) {
Johannes Berg2a519312009-02-10 21:25:55 +0100395 r = cmp_ies(WLAN_EID_MESH_ID,
396 a->information_elements,
397 a->len_information_elements,
398 b->information_elements,
399 b->len_information_elements);
400 if (r)
401 return r;
402 return cmp_ies(WLAN_EID_MESH_CONFIG,
403 a->information_elements,
404 a->len_information_elements,
405 b->information_elements,
406 b->len_information_elements);
407 }
408
Emmanuel Grumbachef9456a2012-04-30 10:23:36 +0300409 /*
410 * we can't use compare_ether_addr here since we need a < > operator.
411 * The binary return value of compare_ether_addr isn't enough
412 */
413 return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100414}
415
416static int cmp_bss(struct cfg80211_bss *a,
417 struct cfg80211_bss *b)
418{
419 int r;
420
421 r = cmp_bss_core(a, b);
Javier Cardona0a35d362011-05-04 10:24:56 -0700422 if (r)
423 return r;
424
Johannes Berg2a519312009-02-10 21:25:55 +0100425 return cmp_ies(WLAN_EID_SSID,
426 a->information_elements,
427 a->len_information_elements,
428 b->information_elements,
429 b->len_information_elements);
430}
431
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100432static int cmp_hidden_bss(struct cfg80211_bss *a,
433 struct cfg80211_bss *b)
434{
435 const u8 *ie1;
436 const u8 *ie2;
437 int i;
438 int r;
439
440 r = cmp_bss_core(a, b);
441 if (r)
442 return r;
443
444 ie1 = cfg80211_find_ie(WLAN_EID_SSID,
445 a->information_elements,
446 a->len_information_elements);
447 ie2 = cfg80211_find_ie(WLAN_EID_SSID,
448 b->information_elements,
449 b->len_information_elements);
450
451 /* Key comparator must use same algorithm in any rb-tree
452 * search function (order is important), otherwise ordering
453 * of items in the tree is broken and search gives incorrect
454 * results. This code uses same order as cmp_ies() does. */
455
456 /* sort missing IE before (left of) present IE */
457 if (!ie1)
458 return -1;
459 if (!ie2)
460 return 1;
461
462 /* zero-size SSID is used as an indication of the hidden bss */
463 if (!ie2[1])
464 return 0;
465
466 /* sort by length first, then by contents */
467 if (ie1[1] != ie2[1])
468 return ie2[1] - ie1[1];
469
470 /* zeroed SSID ie is another indication of a hidden bss */
471 for (i = 0; i < ie2[1]; i++)
472 if (ie2[i + 2])
473 return -1;
474
475 return 0;
476}
477
Johannes Berg2a519312009-02-10 21:25:55 +0100478struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
479 struct ieee80211_channel *channel,
480 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100481 const u8 *ssid, size_t ssid_len,
482 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100483{
484 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
485 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200486 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100487
488 spin_lock_bh(&dev->bss_lock);
489
490 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100491 if ((bss->pub.capability & capa_mask) != capa_val)
492 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100493 if (channel && bss->pub.channel != channel)
494 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200495 /* Don't get expired BSS structs */
496 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
497 !atomic_read(&bss->hold))
498 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100499 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
500 res = bss;
501 kref_get(&res->ref);
502 break;
503 }
504 }
505
506 spin_unlock_bh(&dev->bss_lock);
507 if (!res)
508 return NULL;
509 return &res->pub;
510}
511EXPORT_SYMBOL(cfg80211_get_bss);
512
513struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
514 struct ieee80211_channel *channel,
515 const u8 *meshid, size_t meshidlen,
516 const u8 *meshcfg)
517{
518 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
519 struct cfg80211_internal_bss *bss, *res = NULL;
520
521 spin_lock_bh(&dev->bss_lock);
522
523 list_for_each_entry(bss, &dev->bss_list, list) {
524 if (channel && bss->pub.channel != channel)
525 continue;
526 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
527 res = bss;
528 kref_get(&res->ref);
529 break;
530 }
531 }
532
533 spin_unlock_bh(&dev->bss_lock);
534 if (!res)
535 return NULL;
536 return &res->pub;
537}
538EXPORT_SYMBOL(cfg80211_get_mesh);
539
540
541static void rb_insert_bss(struct cfg80211_registered_device *dev,
542 struct cfg80211_internal_bss *bss)
543{
544 struct rb_node **p = &dev->bss_tree.rb_node;
545 struct rb_node *parent = NULL;
546 struct cfg80211_internal_bss *tbss;
547 int cmp;
548
549 while (*p) {
550 parent = *p;
551 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
552
553 cmp = cmp_bss(&bss->pub, &tbss->pub);
554
555 if (WARN_ON(!cmp)) {
556 /* will sort of leak this BSS */
557 return;
558 }
559
560 if (cmp < 0)
561 p = &(*p)->rb_left;
562 else
563 p = &(*p)->rb_right;
564 }
565
566 rb_link_node(&bss->rbn, parent, p);
567 rb_insert_color(&bss->rbn, &dev->bss_tree);
568}
569
570static struct cfg80211_internal_bss *
571rb_find_bss(struct cfg80211_registered_device *dev,
572 struct cfg80211_internal_bss *res)
573{
574 struct rb_node *n = dev->bss_tree.rb_node;
575 struct cfg80211_internal_bss *bss;
576 int r;
577
578 while (n) {
579 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
580 r = cmp_bss(&res->pub, &bss->pub);
581
582 if (r == 0)
583 return bss;
584 else if (r < 0)
585 n = n->rb_left;
586 else
587 n = n->rb_right;
588 }
589
590 return NULL;
591}
592
593static struct cfg80211_internal_bss *
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100594rb_find_hidden_bss(struct cfg80211_registered_device *dev,
595 struct cfg80211_internal_bss *res)
596{
597 struct rb_node *n = dev->bss_tree.rb_node;
598 struct cfg80211_internal_bss *bss;
599 int r;
600
601 while (n) {
602 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
603 r = cmp_hidden_bss(&res->pub, &bss->pub);
604
605 if (r == 0)
606 return bss;
607 else if (r < 0)
608 n = n->rb_left;
609 else
610 n = n->rb_right;
611 }
612
613 return NULL;
614}
615
616static void
617copy_hidden_ies(struct cfg80211_internal_bss *res,
618 struct cfg80211_internal_bss *hidden)
619{
620 if (unlikely(res->pub.beacon_ies))
621 return;
622 if (WARN_ON(!hidden->pub.beacon_ies))
623 return;
624
625 res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC);
626 if (unlikely(!res->pub.beacon_ies))
627 return;
628
629 res->beacon_ies_allocated = true;
630 res->pub.len_beacon_ies = hidden->pub.len_beacon_ies;
631 memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies,
632 res->pub.len_beacon_ies);
633}
634
635static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100636cfg80211_bss_update(struct cfg80211_registered_device *dev,
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200637 struct cfg80211_internal_bss *res)
Johannes Berg2a519312009-02-10 21:25:55 +0100638{
639 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100640
641 /*
642 * The reference to "res" is donated to this function.
643 */
644
645 if (WARN_ON(!res->pub.channel)) {
646 kref_put(&res->ref, bss_release);
647 return NULL;
648 }
649
650 res->ts = jiffies;
651
Johannes Berg2a519312009-02-10 21:25:55 +0100652 spin_lock_bh(&dev->bss_lock);
653
654 found = rb_find_bss(dev, res);
655
Johannes Bergcd1658f2009-04-16 15:00:58 +0200656 if (found) {
Johannes Berg2a519312009-02-10 21:25:55 +0100657 found->pub.beacon_interval = res->pub.beacon_interval;
658 found->pub.tsf = res->pub.tsf;
659 found->pub.signal = res->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100660 found->pub.capability = res->pub.capability;
661 found->ts = res->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200662
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200663 /* Update IEs */
664 if (res->pub.proberesp_ies) {
Johannes Bergcd1658f2009-04-16 15:00:58 +0200665 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200666 size_t ielen = res->pub.len_proberesp_ies;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200667
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200668 if (found->pub.proberesp_ies &&
669 !found->proberesp_ies_allocated &&
670 ksize(found) >= used + ielen) {
671 memcpy(found->pub.proberesp_ies,
672 res->pub.proberesp_ies, ielen);
673 found->pub.len_proberesp_ies = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200674 } else {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200675 u8 *ies = found->pub.proberesp_ies;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200676
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200677 if (found->proberesp_ies_allocated)
Michael Buesch273de922009-04-25 22:28:55 +0200678 ies = krealloc(ies, ielen, GFP_ATOMIC);
679 else
Johannes Bergcd1658f2009-04-16 15:00:58 +0200680 ies = kmalloc(ielen, GFP_ATOMIC);
681
682 if (ies) {
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200683 memcpy(ies, res->pub.proberesp_ies,
684 ielen);
685 found->proberesp_ies_allocated = true;
686 found->pub.proberesp_ies = ies;
687 found->pub.len_proberesp_ies = ielen;
688 }
689 }
690
691 /* Override possible earlier Beacon frame IEs */
692 found->pub.information_elements =
693 found->pub.proberesp_ies;
694 found->pub.len_information_elements =
695 found->pub.len_proberesp_ies;
696 }
697 if (res->pub.beacon_ies) {
698 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
699 size_t ielen = res->pub.len_beacon_ies;
Sven Neumann01123e22010-12-09 15:05:24 +0100700 bool information_elements_is_beacon_ies =
701 (found->pub.information_elements ==
702 found->pub.beacon_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200703
704 if (found->pub.beacon_ies &&
705 !found->beacon_ies_allocated &&
706 ksize(found) >= used + ielen) {
707 memcpy(found->pub.beacon_ies,
708 res->pub.beacon_ies, ielen);
709 found->pub.len_beacon_ies = ielen;
710 } else {
711 u8 *ies = found->pub.beacon_ies;
712
713 if (found->beacon_ies_allocated)
714 ies = krealloc(ies, ielen, GFP_ATOMIC);
715 else
716 ies = kmalloc(ielen, GFP_ATOMIC);
717
718 if (ies) {
719 memcpy(ies, res->pub.beacon_ies,
720 ielen);
721 found->beacon_ies_allocated = true;
722 found->pub.beacon_ies = ies;
723 found->pub.len_beacon_ies = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200724 }
725 }
Sven Neumann01123e22010-12-09 15:05:24 +0100726
727 /* Override IEs if they were from a beacon before */
728 if (information_elements_is_beacon_ies) {
729 found->pub.information_elements =
730 found->pub.beacon_ies;
731 found->pub.len_information_elements =
732 found->pub.len_beacon_ies;
733 }
Johannes Bergcd1658f2009-04-16 15:00:58 +0200734 }
735
Johannes Berg2a519312009-02-10 21:25:55 +0100736 kref_put(&res->ref, bss_release);
737 } else {
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100738 struct cfg80211_internal_bss *hidden;
739
740 /* First check if the beacon is a probe response from
741 * a hidden bss. If so, copy beacon ies (with nullified
742 * ssid) into the probe response bss entry (with real ssid).
743 * It is required basically for PSM implementation
744 * (probe responses do not contain tim ie) */
745
746 /* TODO: The code is not trying to update existing probe
747 * response bss entries when beacon ies are
748 * getting changed. */
749 hidden = rb_find_hidden_bss(dev, res);
750 if (hidden)
751 copy_hidden_ies(res, hidden);
752
Johannes Berg2a519312009-02-10 21:25:55 +0100753 /* this "consumes" the reference */
754 list_add_tail(&res->list, &dev->bss_list);
755 rb_insert_bss(dev, res);
756 found = res;
757 }
758
759 dev->bss_generation++;
760 spin_unlock_bh(&dev->bss_lock);
761
762 kref_get(&found->ref);
763 return found;
764}
765
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200766struct cfg80211_bss*
767cfg80211_inform_bss(struct wiphy *wiphy,
768 struct ieee80211_channel *channel,
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100769 const u8 *bssid, u64 tsf, u16 capability,
770 u16 beacon_interval, const u8 *ie, size_t ielen,
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200771 s32 signal, gfp_t gfp)
772{
773 struct cfg80211_internal_bss *res;
774 size_t privsz;
775
776 if (WARN_ON(!wiphy))
777 return NULL;
778
779 privsz = wiphy->bss_priv_size;
780
Sujith22fe88d2010-05-13 10:34:08 +0530781 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200782 (signal < 0 || signal > 100)))
783 return NULL;
784
785 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
786 if (!res)
787 return NULL;
788
789 memcpy(res->pub.bssid, bssid, ETH_ALEN);
790 res->pub.channel = channel;
791 res->pub.signal = signal;
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100792 res->pub.tsf = tsf;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200793 res->pub.beacon_interval = beacon_interval;
794 res->pub.capability = capability;
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200795 /*
796 * Since we do not know here whether the IEs are from a Beacon or Probe
797 * Response frame, we need to pick one of the options and only use it
798 * with the driver that does not provide the full Beacon/Probe Response
799 * frame. Use Beacon frame pointer to avoid indicating that this should
800 * override the information_elements pointer should we have received an
801 * earlier indication of Probe Response data.
802 *
803 * The initial buffer for the IEs is allocated with the BSS entry and
804 * is located after the private area.
805 */
806 res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
807 memcpy(res->pub.beacon_ies, ie, ielen);
808 res->pub.len_beacon_ies = ielen;
809 res->pub.information_elements = res->pub.beacon_ies;
810 res->pub.len_information_elements = res->pub.len_beacon_ies;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200811
812 kref_init(&res->ref);
813
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200814 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
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
821 /* cfg80211_bss_update gives us a referenced result */
822 return &res->pub;
823}
824EXPORT_SYMBOL(cfg80211_inform_bss);
825
Johannes Berg2a519312009-02-10 21:25:55 +0100826struct cfg80211_bss *
827cfg80211_inform_bss_frame(struct wiphy *wiphy,
828 struct ieee80211_channel *channel,
829 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100830 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100831{
832 struct cfg80211_internal_bss *res;
833 size_t ielen = len - offsetof(struct ieee80211_mgmt,
834 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100835 size_t privsz;
836
837 if (WARN_ON(!mgmt))
838 return NULL;
839
840 if (WARN_ON(!wiphy))
841 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100842
Sujith22fe88d2010-05-13 10:34:08 +0530843 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300844 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100845 return NULL;
846
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100847 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100848 return NULL;
849
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100850 privsz = wiphy->bss_priv_size;
851
Johannes Berg2a519312009-02-10 21:25:55 +0100852 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
853 if (!res)
854 return NULL;
855
856 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
857 res->pub.channel = channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100858 res->pub.signal = signal;
859 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
860 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
861 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200862 /*
863 * The initial buffer for the IEs is allocated with the BSS entry and
864 * is located after the private area.
865 */
866 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
867 res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
868 memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
869 ielen);
870 res->pub.len_proberesp_ies = ielen;
871 res->pub.information_elements = res->pub.proberesp_ies;
872 res->pub.len_information_elements = res->pub.len_proberesp_ies;
873 } else {
874 res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
875 memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
876 res->pub.len_beacon_ies = ielen;
877 res->pub.information_elements = res->pub.beacon_ies;
878 res->pub.len_information_elements = res->pub.len_beacon_ies;
879 }
Johannes Berg2a519312009-02-10 21:25:55 +0100880
881 kref_init(&res->ref);
882
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200883 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
Johannes Berg2a519312009-02-10 21:25:55 +0100884 if (!res)
885 return NULL;
886
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500887 if (res->pub.capability & WLAN_CAPABILITY_ESS)
888 regulatory_hint_found_beacon(wiphy, channel, gfp);
889
Johannes Berg2a519312009-02-10 21:25:55 +0100890 /* cfg80211_bss_update gives us a referenced result */
891 return &res->pub;
892}
893EXPORT_SYMBOL(cfg80211_inform_bss_frame);
894
Johannes Berg4c0c0b72012-01-20 13:55:26 +0100895void cfg80211_ref_bss(struct cfg80211_bss *pub)
896{
897 struct cfg80211_internal_bss *bss;
898
899 if (!pub)
900 return;
901
902 bss = container_of(pub, struct cfg80211_internal_bss, pub);
903 kref_get(&bss->ref);
904}
905EXPORT_SYMBOL(cfg80211_ref_bss);
906
Johannes Berg2a519312009-02-10 21:25:55 +0100907void cfg80211_put_bss(struct cfg80211_bss *pub)
908{
909 struct cfg80211_internal_bss *bss;
910
911 if (!pub)
912 return;
913
914 bss = container_of(pub, struct cfg80211_internal_bss, pub);
915 kref_put(&bss->ref, bss_release);
916}
917EXPORT_SYMBOL(cfg80211_put_bss);
918
Johannes Bergd491af12009-02-10 21:25:58 +0100919void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
920{
921 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
922 struct cfg80211_internal_bss *bss;
923
924 if (WARN_ON(!pub))
925 return;
926
927 bss = container_of(pub, struct cfg80211_internal_bss, pub);
928
929 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +0200930 if (!list_empty(&bss->list)) {
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300931 __cfg80211_unlink_bss(dev, bss);
Johannes Berg32073902010-10-06 21:18:04 +0200932 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +0200933 }
Johannes Bergd491af12009-02-10 21:25:58 +0100934 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +0100935}
936EXPORT_SYMBOL(cfg80211_unlink_bss);
937
Johannes Berg3d23e342009-09-29 23:27:28 +0200938#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100939int cfg80211_wext_siwscan(struct net_device *dev,
940 struct iw_request_info *info,
941 union iwreq_data *wrqu, char *extra)
942{
943 struct cfg80211_registered_device *rdev;
944 struct wiphy *wiphy;
945 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100946 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100947 int i, err, n_channels = 0;
948 enum ieee80211_band band;
949
950 if (!netif_running(dev))
951 return -ENETDOWN;
952
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200953 if (wrqu->data.length == sizeof(struct iw_scan_req))
954 wreq = (struct iw_scan_req *)extra;
955
Johannes Berg463d0182009-07-14 00:33:35 +0200956 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100957
958 if (IS_ERR(rdev))
959 return PTR_ERR(rdev);
960
961 if (rdev->scan_req) {
962 err = -EBUSY;
963 goto out;
964 }
965
966 wiphy = &rdev->wiphy;
967
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200968 /* Determine number of channels, needed to allocate creq */
969 if (wreq && wreq->num_channels)
970 n_channels = wreq->num_channels;
971 else {
972 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
973 if (wiphy->bands[band])
974 n_channels += wiphy->bands[band]->n_channels;
975 }
Johannes Berg2a519312009-02-10 21:25:55 +0100976
977 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
978 n_channels * sizeof(void *),
979 GFP_ATOMIC);
980 if (!creq) {
981 err = -ENOMEM;
982 goto out;
983 }
984
985 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +0200986 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +0200987 /* SSIDs come after channels */
988 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +0100989 creq->n_channels = n_channels;
990 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -0700991 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100992
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200993 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +0100994 i = 0;
995 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
996 int j;
Johannes Berg584991d2009-11-02 13:32:03 +0100997
Johannes Berg2a519312009-02-10 21:25:55 +0100998 if (!wiphy->bands[band])
999 continue;
Johannes Berg584991d2009-11-02 13:32:03 +01001000
Johannes Berg2a519312009-02-10 21:25:55 +01001001 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01001002 /* ignore disabled channels */
1003 if (wiphy->bands[band]->channels[j].flags &
1004 IEEE80211_CHAN_DISABLED)
1005 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001006
1007 /* If we have a wireless request structure and the
1008 * wireless request specifies frequencies, then search
1009 * for the matching hardware channel.
1010 */
1011 if (wreq && wreq->num_channels) {
1012 int k;
1013 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1014 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +02001015 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001016 if (wext_freq == wiphy_freq)
1017 goto wext_freq_found;
1018 }
1019 goto wext_freq_not_found;
1020 }
1021
1022 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +01001023 creq->channels[i] = &wiphy->bands[band]->channels[j];
1024 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001025 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +01001026 }
1027 }
Holger Schurig8862dc52009-09-11 10:13:55 +02001028 /* No channels found? */
1029 if (!i) {
1030 err = -EINVAL;
1031 goto out;
1032 }
Johannes Berg2a519312009-02-10 21:25:55 +01001033
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001034 /* Set real number of channels specified in creq->channels[] */
1035 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +01001036
Holger Schurigb2e3abd2009-09-09 13:09:54 +02001037 /* translate "Scan for SSID" request */
1038 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +01001039 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +01001040 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1041 err = -EINVAL;
1042 goto out;
1043 }
Johannes Berg2a519312009-02-10 21:25:55 +01001044 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1045 creq->ssids[0].ssid_len = wreq->essid_len;
1046 }
1047 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1048 creq->n_ssids = 0;
1049 }
1050
Johannes Berg34850ab2011-07-18 18:08:35 +02001051 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02001052 if (wiphy->bands[i])
1053 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02001054
Johannes Berg2a519312009-02-10 21:25:55 +01001055 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +03001056 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +01001057 if (err) {
1058 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +01001059 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +02001060 } else {
Johannes Bergfd014282012-06-18 19:17:03 +02001061 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +01001062 /* creq now owned by driver */
1063 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +02001064 dev_hold(dev);
1065 }
Johannes Berg2a519312009-02-10 21:25:55 +01001066 out:
Johannes Berg65486c82009-12-23 15:33:35 +01001067 kfree(creq);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001068 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001069 return err;
1070}
Johannes Bergba44cb72009-04-20 18:49:39 +02001071EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001072
1073static void ieee80211_scan_add_ies(struct iw_request_info *info,
1074 struct cfg80211_bss *bss,
1075 char **current_ev, char *end_buf)
1076{
1077 u8 *pos, *end, *next;
1078 struct iw_event iwe;
1079
1080 if (!bss->information_elements ||
1081 !bss->len_information_elements)
1082 return;
1083
1084 /*
1085 * If needed, fragment the IEs buffer (at IE boundaries) into short
1086 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1087 */
1088 pos = bss->information_elements;
1089 end = pos + bss->len_information_elements;
1090
1091 while (end - pos > IW_GENERIC_IE_MAX) {
1092 next = pos + 2 + pos[1];
1093 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1094 next = next + 2 + next[1];
1095
1096 memset(&iwe, 0, sizeof(iwe));
1097 iwe.cmd = IWEVGENIE;
1098 iwe.u.data.length = next - pos;
1099 *current_ev = iwe_stream_add_point(info, *current_ev,
1100 end_buf, &iwe, pos);
1101
1102 pos = next;
1103 }
1104
1105 if (end > pos) {
1106 memset(&iwe, 0, sizeof(iwe));
1107 iwe.cmd = IWEVGENIE;
1108 iwe.u.data.length = end - pos;
1109 *current_ev = iwe_stream_add_point(info, *current_ev,
1110 end_buf, &iwe, pos);
1111 }
1112}
1113
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001114static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
1115{
1116 unsigned long end = jiffies;
1117
1118 if (end >= start)
1119 return jiffies_to_msecs(end - start);
1120
1121 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
1122}
Johannes Berg2a519312009-02-10 21:25:55 +01001123
1124static char *
Johannes Berg77965c92009-02-18 18:45:06 +01001125ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1126 struct cfg80211_internal_bss *bss, char *current_ev,
1127 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001128{
1129 struct iw_event iwe;
1130 u8 *buf, *cfg, *p;
1131 u8 *ie = bss->pub.information_elements;
Johannes Berga77b8552009-02-18 18:27:22 +01001132 int rem = bss->pub.len_information_elements, 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
1197 while (rem >= 2) {
1198 /* invalid data */
1199 if (ie[1] > rem - 2)
1200 break;
1201
1202 switch (ie[0]) {
1203 case WLAN_EID_SSID:
1204 memset(&iwe, 0, sizeof(iwe));
1205 iwe.cmd = SIOCGIWESSID;
1206 iwe.u.data.length = ie[1];
1207 iwe.u.data.flags = 1;
1208 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1209 &iwe, ie + 2);
1210 break;
1211 case WLAN_EID_MESH_ID:
1212 memset(&iwe, 0, sizeof(iwe));
1213 iwe.cmd = SIOCGIWESSID;
1214 iwe.u.data.length = ie[1];
1215 iwe.u.data.flags = 1;
1216 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1217 &iwe, ie + 2);
1218 break;
1219 case WLAN_EID_MESH_CONFIG:
1220 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001221 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001222 break;
1223 buf = kmalloc(50, GFP_ATOMIC);
1224 if (!buf)
1225 break;
1226 cfg = ie + 2;
1227 memset(&iwe, 0, sizeof(iwe));
1228 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001229 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1230 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001231 iwe.u.data.length = strlen(buf);
1232 current_ev = iwe_stream_add_point(info, current_ev,
1233 end_buf,
1234 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001235 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1236 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001237 iwe.u.data.length = strlen(buf);
1238 current_ev = iwe_stream_add_point(info, current_ev,
1239 end_buf,
1240 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001241 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1242 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001243 iwe.u.data.length = strlen(buf);
1244 current_ev = iwe_stream_add_point(info, current_ev,
1245 end_buf,
1246 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001247 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01001248 iwe.u.data.length = strlen(buf);
1249 current_ev = iwe_stream_add_point(info, current_ev,
1250 end_buf,
1251 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001252 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1253 iwe.u.data.length = strlen(buf);
1254 current_ev = iwe_stream_add_point(info, current_ev,
1255 end_buf,
1256 &iwe, buf);
1257 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1258 iwe.u.data.length = strlen(buf);
1259 current_ev = iwe_stream_add_point(info, current_ev,
1260 end_buf,
1261 &iwe, buf);
1262 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001263 iwe.u.data.length = strlen(buf);
1264 current_ev = iwe_stream_add_point(info, current_ev,
1265 end_buf,
1266 &iwe, buf);
1267 kfree(buf);
1268 break;
1269 case WLAN_EID_SUPP_RATES:
1270 case WLAN_EID_EXT_SUPP_RATES:
1271 /* display all supported rates in readable format */
1272 p = current_ev + iwe_stream_lcp_len(info);
1273
1274 memset(&iwe, 0, sizeof(iwe));
1275 iwe.cmd = SIOCGIWRATE;
1276 /* Those two flags are ignored... */
1277 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1278
1279 for (i = 0; i < ie[1]; i++) {
1280 iwe.u.bitrate.value =
1281 ((ie[i + 2] & 0x7f) * 500000);
1282 p = iwe_stream_add_value(info, current_ev, p,
1283 end_buf, &iwe, IW_EV_PARAM_LEN);
1284 }
1285 current_ev = p;
1286 break;
1287 }
1288 rem -= ie[1] + 2;
1289 ie += ie[1] + 2;
1290 }
1291
Joe Perchesf64f9e72009-11-29 16:55:45 -08001292 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1293 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001294 memset(&iwe, 0, sizeof(iwe));
1295 iwe.cmd = SIOCGIWMODE;
1296 if (ismesh)
1297 iwe.u.mode = IW_MODE_MESH;
1298 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1299 iwe.u.mode = IW_MODE_MASTER;
1300 else
1301 iwe.u.mode = IW_MODE_ADHOC;
1302 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1303 &iwe, IW_EV_UINT_LEN);
1304 }
1305
1306 buf = kmalloc(30, GFP_ATOMIC);
1307 if (buf) {
1308 memset(&iwe, 0, sizeof(iwe));
1309 iwe.cmd = IWEVCUSTOM;
1310 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1311 iwe.u.data.length = strlen(buf);
1312 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1313 &iwe, buf);
1314 memset(&iwe, 0, sizeof(iwe));
1315 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001316 sprintf(buf, " Last beacon: %ums ago",
1317 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001318 iwe.u.data.length = strlen(buf);
1319 current_ev = iwe_stream_add_point(info, current_ev,
1320 end_buf, &iwe, buf);
1321 kfree(buf);
1322 }
1323
1324 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
1325
1326 return current_ev;
1327}
1328
1329
1330static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1331 struct iw_request_info *info,
1332 char *buf, size_t len)
1333{
1334 char *current_ev = buf;
1335 char *end_buf = buf + len;
1336 struct cfg80211_internal_bss *bss;
1337
1338 spin_lock_bh(&dev->bss_lock);
1339 cfg80211_bss_expire(dev);
1340
1341 list_for_each_entry(bss, &dev->bss_list, list) {
1342 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1343 spin_unlock_bh(&dev->bss_lock);
1344 return -E2BIG;
1345 }
Johannes Berg77965c92009-02-18 18:45:06 +01001346 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1347 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001348 }
1349 spin_unlock_bh(&dev->bss_lock);
1350 return current_ev - buf;
1351}
1352
1353
1354int cfg80211_wext_giwscan(struct net_device *dev,
1355 struct iw_request_info *info,
1356 struct iw_point *data, char *extra)
1357{
1358 struct cfg80211_registered_device *rdev;
1359 int res;
1360
1361 if (!netif_running(dev))
1362 return -ENETDOWN;
1363
Johannes Berg463d0182009-07-14 00:33:35 +02001364 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001365
1366 if (IS_ERR(rdev))
1367 return PTR_ERR(rdev);
1368
1369 if (rdev->scan_req) {
1370 res = -EAGAIN;
1371 goto out;
1372 }
1373
1374 res = ieee80211_scan_results(rdev, info, extra, data->length);
1375 data->length = 0;
1376 if (res >= 0) {
1377 data->length = res;
1378 res = 0;
1379 }
1380
1381 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001382 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001383 return res;
1384}
Johannes Bergba44cb72009-04-20 18:49:39 +02001385EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001386#endif