blob: 34b47b30be88b13f101929e55a00f8edbac58406 [file] [log] [blame]
Johannes Berg2a519312009-02-10 21:25:55 +01001/*
2 * cfg80211 scan result handling
3 *
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
5 */
6#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Johannes Berg2a519312009-02-10 21:25:55 +01008#include <linux/module.h>
9#include <linux/netdevice.h>
10#include <linux/wireless.h>
11#include <linux/nl80211.h>
12#include <linux/etherdevice.h>
13#include <net/arp.h>
14#include <net/cfg80211.h>
Johannes Berg262eb9b22011-07-13 10:39:09 +020015#include <net/cfg80211-wext.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <net/iw_handler.h>
17#include "core.h"
18#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020019#include "wext-compat.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030020#include "rdev-ops.h"
Johannes Berg2a519312009-02-10 21:25:55 +010021
Rajkumar Manoharanf9616e02012-04-13 16:38:40 +053022#define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010023
Amitkumar Karware8e27c62012-10-11 21:03:33 -070024static void bss_release(struct kref *ref)
25{
Johannes Berg9caf0362012-11-29 01:25:20 +010026 struct cfg80211_bss_ies *ies;
Amitkumar Karware8e27c62012-10-11 21:03:33 -070027 struct cfg80211_internal_bss *bss;
28
29 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Bergb629ea32012-11-28 22:14:56 +010030
31 if (WARN_ON(atomic_read(&bss->hold)))
32 return;
33
Johannes Berg9caf0362012-11-29 01:25:20 +010034 ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
35 if (ies)
36 kfree_rcu(ies, rcu_head);
37 ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
38 if (ies)
39 kfree_rcu(ies, rcu_head);
Amitkumar Karware8e27c62012-10-11 21:03:33 -070040
Amitkumar Karware8e27c62012-10-11 21:03:33 -070041 kfree(bss);
42}
43
44/* must hold dev->bss_lock! */
45static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
46 struct cfg80211_internal_bss *bss)
47{
48 list_del_init(&bss->list);
49 rb_erase(&bss->rbn, &dev->bss_tree);
50 kref_put(&bss->ref, bss_release);
51}
52
Sam Leffler15d60302012-10-11 21:03:34 -070053/* must hold dev->bss_lock! */
54static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
55 unsigned long expire_time)
56{
57 struct cfg80211_internal_bss *bss, *tmp;
58 bool expired = false;
59
60 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
61 if (atomic_read(&bss->hold))
62 continue;
63 if (!time_after(expire_time, bss->ts))
64 continue;
65
66 __cfg80211_unlink_bss(dev, bss);
67 expired = true;
68 }
69
70 if (expired)
71 dev->bss_generation++;
72}
73
Johannes Berg01a0ac42009-08-20 21:36:16 +020074void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
Johannes Berg2a519312009-02-10 21:25:55 +010075{
Johannes Berg667503d2009-07-07 03:56:11 +020076 struct cfg80211_scan_request *request;
Johannes Bergfd014282012-06-18 19:17:03 +020077 struct wireless_dev *wdev;
Johannes Berg3d23e342009-09-29 23:27:28 +020078#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +010079 union iwreq_data wrqu;
80#endif
81
Johannes Berg01a0ac42009-08-20 21:36:16 +020082 ASSERT_RDEV_LOCK(rdev);
83
Johannes Berg667503d2009-07-07 03:56:11 +020084 request = rdev->scan_req;
85
Johannes Berg01a0ac42009-08-20 21:36:16 +020086 if (!request)
87 return;
88
Johannes Bergfd014282012-06-18 19:17:03 +020089 wdev = request->wdev;
Johannes Berg2a519312009-02-10 21:25:55 +010090
Johannes Berg6829c872009-07-02 09:13:27 +020091 /*
92 * This must be before sending the other events!
93 * Otherwise, wpa_supplicant gets completely confused with
94 * wext events.
95 */
Johannes Bergfd014282012-06-18 19:17:03 +020096 if (wdev->netdev)
97 cfg80211_sme_scan_done(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +020098
Sam Leffler15d60302012-10-11 21:03:34 -070099 if (request->aborted) {
Johannes Bergfd014282012-06-18 19:17:03 +0200100 nl80211_send_scan_aborted(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700101 } else {
102 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
103 /* flush entries from previous scans */
104 spin_lock_bh(&rdev->bss_lock);
105 __cfg80211_bss_expire(rdev, request->scan_start);
106 spin_unlock_bh(&rdev->bss_lock);
107 }
Johannes Bergfd014282012-06-18 19:17:03 +0200108 nl80211_send_scan_done(rdev, wdev);
Sam Leffler15d60302012-10-11 21:03:34 -0700109 }
Johannes Berg2a519312009-02-10 21:25:55 +0100110
Johannes Berg3d23e342009-09-29 23:27:28 +0200111#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfd014282012-06-18 19:17:03 +0200112 if (wdev->netdev && !request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +0100113 memset(&wrqu, 0, sizeof(wrqu));
114
Johannes Bergfd014282012-06-18 19:17:03 +0200115 wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100116 }
117#endif
118
Johannes Bergfd014282012-06-18 19:17:03 +0200119 if (wdev->netdev)
120 dev_put(wdev->netdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100121
Johannes Berg36e6fea2009-08-12 22:21:21 +0200122 rdev->scan_req = NULL;
Johannes Berg01a0ac42009-08-20 21:36:16 +0200123
124 /*
125 * OK. If this is invoked with "leak" then we can't
126 * free this ... but we've cleaned it up anyway. The
127 * driver failed to call the scan_done callback, so
128 * all bets are off, it might still be trying to use
129 * the scan request or not ... if it accesses the dev
130 * in there (it shouldn't anyway) then it may crash.
131 */
132 if (!leak)
133 kfree(request);
Johannes Berg2a519312009-02-10 21:25:55 +0100134}
Johannes Berg667503d2009-07-07 03:56:11 +0200135
Johannes Berg36e6fea2009-08-12 22:21:21 +0200136void __cfg80211_scan_done(struct work_struct *wk)
137{
138 struct cfg80211_registered_device *rdev;
139
140 rdev = container_of(wk, struct cfg80211_registered_device,
141 scan_done_wk);
142
143 cfg80211_lock_rdev(rdev);
Johannes Berg01a0ac42009-08-20 21:36:16 +0200144 ___cfg80211_scan_done(rdev, false);
Johannes Berg36e6fea2009-08-12 22:21:21 +0200145 cfg80211_unlock_rdev(rdev);
146}
147
Johannes Berg667503d2009-07-07 03:56:11 +0200148void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
149{
Beni Lev4ee3e062012-08-27 12:49:39 +0300150 trace_cfg80211_scan_done(request, aborted);
Johannes Berg667503d2009-07-07 03:56:11 +0200151 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
152
153 request->aborted = aborted;
Alban Browaeyse60d7442009-11-25 15:13:00 +0100154 queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +0200155}
Johannes Berg2a519312009-02-10 21:25:55 +0100156EXPORT_SYMBOL(cfg80211_scan_done);
157
Luciano Coelho807f8a82011-05-11 17:09:35 +0300158void __cfg80211_sched_scan_results(struct work_struct *wk)
159{
160 struct cfg80211_registered_device *rdev;
Sam Leffler15d60302012-10-11 21:03:34 -0700161 struct cfg80211_sched_scan_request *request;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300162
163 rdev = container_of(wk, struct cfg80211_registered_device,
164 sched_scan_results_wk);
165
Sam Leffler15d60302012-10-11 21:03:34 -0700166 request = rdev->sched_scan_req;
167
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300168 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300169
170 /* we don't have sched_scan_req anymore if the scan is stopping */
Sam Leffler15d60302012-10-11 21:03:34 -0700171 if (request) {
172 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
173 /* flush entries from previous scans */
174 spin_lock_bh(&rdev->bss_lock);
175 __cfg80211_bss_expire(rdev, request->scan_start);
176 spin_unlock_bh(&rdev->bss_lock);
177 request->scan_start =
178 jiffies + msecs_to_jiffies(request->interval);
179 }
180 nl80211_send_sched_scan_results(rdev, request->dev);
181 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300182
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300183 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300184}
185
186void cfg80211_sched_scan_results(struct wiphy *wiphy)
187{
Beni Lev4ee3e062012-08-27 12:49:39 +0300188 trace_cfg80211_sched_scan_results(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300189 /* ignore if we're not scanning */
190 if (wiphy_to_dev(wiphy)->sched_scan_req)
191 queue_work(cfg80211_wq,
192 &wiphy_to_dev(wiphy)->sched_scan_results_wk);
193}
194EXPORT_SYMBOL(cfg80211_sched_scan_results);
195
Luciano Coelho85a99942011-05-12 16:28:29 +0300196void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
Luciano Coelho807f8a82011-05-11 17:09:35 +0300197{
Luciano Coelho85a99942011-05-12 16:28:29 +0300198 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300199
Beni Lev4ee3e062012-08-27 12:49:39 +0300200 trace_cfg80211_sched_scan_stopped(wiphy);
201
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300202 mutex_lock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300203 __cfg80211_stop_sched_scan(rdev, true);
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300204 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300205}
Luciano Coelho807f8a82011-05-11 17:09:35 +0300206EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
207
208int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
209 bool driver_initiated)
210{
Luciano Coelho807f8a82011-05-11 17:09:35 +0300211 struct net_device *dev;
212
Luciano Coelhoc10841c2011-06-30 08:32:41 +0300213 lockdep_assert_held(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300214
215 if (!rdev->sched_scan_req)
Luciano Coelho1a84ff72011-07-08 11:16:16 +0300216 return -ENOENT;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300217
218 dev = rdev->sched_scan_req->dev;
219
Luciano Coelho85a99942011-05-12 16:28:29 +0300220 if (!driver_initiated) {
Hila Gonene35e4d22012-06-27 17:19:42 +0300221 int err = rdev_sched_scan_stop(rdev, dev);
Luciano Coelho85a99942011-05-12 16:28:29 +0300222 if (err)
223 return err;
224 }
Luciano Coelho807f8a82011-05-11 17:09:35 +0300225
226 nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
227
228 kfree(rdev->sched_scan_req);
229 rdev->sched_scan_req = NULL;
230
Jesper Juhl3b4670f2011-06-29 22:49:33 +0200231 return 0;
Luciano Coelho807f8a82011-05-11 17:09:35 +0300232}
233
Johannes Berg2a519312009-02-10 21:25:55 +0100234/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500235void cfg80211_bss_age(struct cfg80211_registered_device *dev,
236 unsigned long age_secs)
237{
238 struct cfg80211_internal_bss *bss;
239 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
240
Johannes Berg915de2f2012-11-28 22:39:37 +0100241 list_for_each_entry(bss, &dev->bss_list, list)
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500242 bss->ts -= age_jiffies;
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500243}
244
Johannes Berg2a519312009-02-10 21:25:55 +0100245void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
246{
Sam Leffler15d60302012-10-11 21:03:34 -0700247 __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
Johannes Berg2a519312009-02-10 21:25:55 +0100248}
249
Johannes Bergc21dbf92010-01-26 14:15:46 +0100250const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
Johannes Berg2a519312009-02-10 21:25:55 +0100251{
Johannes Bergc21dbf92010-01-26 14:15:46 +0100252 while (len > 2 && ies[0] != eid) {
Johannes Berg2a519312009-02-10 21:25:55 +0100253 len -= ies[1] + 2;
254 ies += ies[1] + 2;
255 }
256 if (len < 2)
257 return NULL;
258 if (len < 2 + ies[1])
259 return NULL;
260 return ies;
261}
Johannes Bergc21dbf92010-01-26 14:15:46 +0100262EXPORT_SYMBOL(cfg80211_find_ie);
Johannes Berg2a519312009-02-10 21:25:55 +0100263
Eliad Peller0c28ec52011-09-15 11:53:01 +0300264const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
265 const u8 *ies, int len)
266{
267 struct ieee80211_vendor_ie *ie;
268 const u8 *pos = ies, *end = ies + len;
269 int ie_oui;
270
271 while (pos < end) {
272 pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
273 end - pos);
274 if (!pos)
275 return NULL;
276
277 if (end - pos < sizeof(*ie))
278 return NULL;
279
280 ie = (struct ieee80211_vendor_ie *)pos;
281 ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
282 if (ie_oui == oui && ie->oui_type == oui_type)
283 return pos;
284
285 pos += 2 + ie->len;
286 }
287 return NULL;
288}
289EXPORT_SYMBOL(cfg80211_find_vendor_ie);
290
Johannes Berg915de2f2012-11-28 22:39:37 +0100291static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
Johannes Berg2a519312009-02-10 21:25:55 +0100292 const u8 *ssid, size_t ssid_len)
293{
Johannes Berg9caf0362012-11-29 01:25:20 +0100294 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100295 const u8 *ssidie;
296
Joe Perchesac422d32012-05-08 18:56:55 +0000297 if (bssid && !ether_addr_equal(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100298 return false;
299
Johannes Berg79420f02009-02-10 21:25:59 +0100300 if (!ssid)
301 return true;
302
Johannes Berg9caf0362012-11-29 01:25:20 +0100303 ies = rcu_access_pointer(a->ies);
304 if (!ies)
305 return false;
306 ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
Johannes Berg2a519312009-02-10 21:25:55 +0100307 if (!ssidie)
308 return false;
309 if (ssidie[1] != ssid_len)
310 return false;
311 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
312}
313
Johannes Berg4593c4c2013-02-01 19:20:03 +0100314/**
315 * enum bss_compare_mode - BSS compare mode
316 * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
317 * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
318 * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
319 */
320enum bss_compare_mode {
321 BSS_CMP_REGULAR,
322 BSS_CMP_HIDE_ZLEN,
323 BSS_CMP_HIDE_NUL,
324};
325
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100326static int cmp_bss(struct cfg80211_bss *a,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100327 struct cfg80211_bss *b,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100328 enum bss_compare_mode mode)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100329{
Johannes Berg9caf0362012-11-29 01:25:20 +0100330 const struct cfg80211_bss_ies *a_ies, *b_ies;
Johannes Berg3af63412013-01-30 00:40:20 +0100331 const u8 *ie1 = NULL;
332 const u8 *ie2 = NULL;
Johannes Berg5622f5b2013-01-30 00:26:45 +0100333 int i, r;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100334
Johannes Berg3af63412013-01-30 00:40:20 +0100335 if (a->channel != b->channel)
336 return b->channel->center_freq - a->channel->center_freq;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100337
Johannes Berg9caf0362012-11-29 01:25:20 +0100338 a_ies = rcu_access_pointer(a->ies);
339 if (!a_ies)
340 return -1;
341 b_ies = rcu_access_pointer(b->ies);
342 if (!b_ies)
343 return 1;
344
Johannes Berg3af63412013-01-30 00:40:20 +0100345 if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
346 ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
347 a_ies->data, a_ies->len);
348 if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
349 ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
350 b_ies->data, b_ies->len);
351 if (ie1 && ie2) {
352 int mesh_id_cmp;
353
354 if (ie1[1] == ie2[1])
355 mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
356 else
357 mesh_id_cmp = ie2[1] - ie1[1];
358
359 ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
360 a_ies->data, a_ies->len);
361 ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
362 b_ies->data, b_ies->len);
363 if (ie1 && ie2) {
364 if (mesh_id_cmp)
365 return mesh_id_cmp;
366 if (ie1[1] != ie2[1])
367 return ie2[1] - ie1[1];
368 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
369 }
370 }
371
372 /*
373 * we can't use compare_ether_addr here since we need a < > operator.
374 * The binary return value of compare_ether_addr isn't enough
375 */
376 r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
377 if (r)
378 return r;
379
Johannes Berg9caf0362012-11-29 01:25:20 +0100380 ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
381 ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100382
Johannes Berg5622f5b2013-01-30 00:26:45 +0100383 if (!ie1 && !ie2)
384 return 0;
385
Johannes Bergf94f8b12012-11-28 22:42:34 +0100386 /*
Johannes Berg5622f5b2013-01-30 00:26:45 +0100387 * Note that with "hide_ssid", the function returns a match if
388 * the already-present BSS ("b") is a hidden SSID beacon for
389 * the new BSS ("a").
Johannes Bergf94f8b12012-11-28 22:42:34 +0100390 */
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100391
392 /* sort missing IE before (left of) present IE */
393 if (!ie1)
394 return -1;
395 if (!ie2)
396 return 1;
397
Johannes Berg4593c4c2013-02-01 19:20:03 +0100398 switch (mode) {
399 case BSS_CMP_HIDE_ZLEN:
400 /*
401 * In ZLEN mode we assume the BSS entry we're
402 * looking for has a zero-length SSID. So if
403 * the one we're looking at right now has that,
404 * return 0. Otherwise, return the difference
405 * in length, but since we're looking for the
406 * 0-length it's really equivalent to returning
407 * the length of the one we're looking at.
408 *
409 * No content comparison is needed as we assume
410 * the content length is zero.
411 */
412 return ie2[1];
413 case BSS_CMP_REGULAR:
414 default:
415 /* sort by length first, then by contents */
416 if (ie1[1] != ie2[1])
417 return ie2[1] - ie1[1];
Johannes Berg5622f5b2013-01-30 00:26:45 +0100418 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100419 case BSS_CMP_HIDE_NUL:
420 if (ie1[1] != ie2[1])
421 return ie2[1] - ie1[1];
422 /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
423 for (i = 0; i < ie2[1]; i++)
424 if (ie2[i + 2])
425 return -1;
426 return 0;
427 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100428}
429
Johannes Berg2a519312009-02-10 21:25:55 +0100430struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
431 struct ieee80211_channel *channel,
432 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100433 const u8 *ssid, size_t ssid_len,
434 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100435{
436 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
437 struct cfg80211_internal_bss *bss, *res = NULL;
Johannes Bergccb6c132010-07-13 10:55:38 +0200438 unsigned long now = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100439
Beni Lev4ee3e062012-08-27 12:49:39 +0300440 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
441 capa_val);
442
Johannes Berg2a519312009-02-10 21:25:55 +0100443 spin_lock_bh(&dev->bss_lock);
444
445 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100446 if ((bss->pub.capability & capa_mask) != capa_val)
447 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100448 if (channel && bss->pub.channel != channel)
449 continue;
Johannes Bergccb6c132010-07-13 10:55:38 +0200450 /* Don't get expired BSS structs */
451 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
452 !atomic_read(&bss->hold))
453 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100454 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
455 res = bss;
456 kref_get(&res->ref);
457 break;
458 }
459 }
460
461 spin_unlock_bh(&dev->bss_lock);
462 if (!res)
463 return NULL;
Beni Lev4ee3e062012-08-27 12:49:39 +0300464 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100465 return &res->pub;
466}
467EXPORT_SYMBOL(cfg80211_get_bss);
468
Johannes Berg2a519312009-02-10 21:25:55 +0100469static void rb_insert_bss(struct cfg80211_registered_device *dev,
470 struct cfg80211_internal_bss *bss)
471{
472 struct rb_node **p = &dev->bss_tree.rb_node;
473 struct rb_node *parent = NULL;
474 struct cfg80211_internal_bss *tbss;
475 int cmp;
476
477 while (*p) {
478 parent = *p;
479 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
480
Johannes Berg4593c4c2013-02-01 19:20:03 +0100481 cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100482
483 if (WARN_ON(!cmp)) {
484 /* will sort of leak this BSS */
485 return;
486 }
487
488 if (cmp < 0)
489 p = &(*p)->rb_left;
490 else
491 p = &(*p)->rb_right;
492 }
493
494 rb_link_node(&bss->rbn, parent, p);
495 rb_insert_color(&bss->rbn, &dev->bss_tree);
496}
497
498static struct cfg80211_internal_bss *
499rb_find_bss(struct cfg80211_registered_device *dev,
Johannes Berg5622f5b2013-01-30 00:26:45 +0100500 struct cfg80211_internal_bss *res,
Johannes Berg4593c4c2013-02-01 19:20:03 +0100501 enum bss_compare_mode mode)
Johannes Berg2a519312009-02-10 21:25:55 +0100502{
503 struct rb_node *n = dev->bss_tree.rb_node;
504 struct cfg80211_internal_bss *bss;
505 int r;
506
507 while (n) {
508 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100509 r = cmp_bss(&res->pub, &bss->pub, mode);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100510
511 if (r == 0)
512 return bss;
513 else if (r < 0)
514 n = n->rb_left;
515 else
516 n = n->rb_right;
517 }
518
519 return NULL;
520}
521
522static void
523copy_hidden_ies(struct cfg80211_internal_bss *res,
Johannes Berg915de2f2012-11-28 22:39:37 +0100524 struct cfg80211_internal_bss *hidden)
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100525{
Johannes Berg9caf0362012-11-29 01:25:20 +0100526 const struct cfg80211_bss_ies *ies;
527
528 if (rcu_access_pointer(res->pub.beacon_ies))
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100529 return;
530
Johannes Berg9caf0362012-11-29 01:25:20 +0100531 ies = rcu_access_pointer(hidden->pub.beacon_ies);
532 if (WARN_ON(!ies))
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100533 return;
534
Johannes Berg9caf0362012-11-29 01:25:20 +0100535 ies = kmemdup(ies, sizeof(*ies) + ies->len, GFP_ATOMIC);
536 if (unlikely(!ies))
537 return;
538 rcu_assign_pointer(res->pub.beacon_ies, ies);
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100539}
540
541static struct cfg80211_internal_bss *
Johannes Berg2a519312009-02-10 21:25:55 +0100542cfg80211_bss_update(struct cfg80211_registered_device *dev,
Johannes Berg9caf0362012-11-29 01:25:20 +0100543 struct cfg80211_internal_bss *tmp)
Johannes Berg2a519312009-02-10 21:25:55 +0100544{
545 struct cfg80211_internal_bss *found = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100546
Johannes Berg9caf0362012-11-29 01:25:20 +0100547 if (WARN_ON(!tmp->pub.channel))
Johannes Berg2a519312009-02-10 21:25:55 +0100548 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100549
Johannes Berg9caf0362012-11-29 01:25:20 +0100550 tmp->ts = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100551
Johannes Berg2a519312009-02-10 21:25:55 +0100552 spin_lock_bh(&dev->bss_lock);
553
Johannes Berg9caf0362012-11-29 01:25:20 +0100554 if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
555 spin_unlock_bh(&dev->bss_lock);
556 return NULL;
557 }
558
Johannes Berg4593c4c2013-02-01 19:20:03 +0100559 found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR);
Johannes Berg2a519312009-02-10 21:25:55 +0100560
Johannes Bergcd1658f2009-04-16 15:00:58 +0200561 if (found) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100562 found->pub.beacon_interval = tmp->pub.beacon_interval;
563 found->pub.tsf = tmp->pub.tsf;
564 found->pub.signal = tmp->pub.signal;
565 found->pub.capability = tmp->pub.capability;
566 found->ts = tmp->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200567
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200568 /* Update IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100569 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
570 const struct cfg80211_bss_ies *old;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200571
Johannes Berg9caf0362012-11-29 01:25:20 +0100572 old = rcu_access_pointer(found->pub.proberesp_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200573
Johannes Berg9caf0362012-11-29 01:25:20 +0100574 rcu_assign_pointer(found->pub.proberesp_ies,
575 tmp->pub.proberesp_ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200576 /* Override possible earlier Beacon frame IEs */
Johannes Berg9caf0362012-11-29 01:25:20 +0100577 rcu_assign_pointer(found->pub.ies,
578 tmp->pub.proberesp_ies);
579 if (old)
580 kfree_rcu((struct cfg80211_bss_ies *)old,
581 rcu_head);
582 } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
583 const struct cfg80211_bss_ies *old, *ies;
Johannes Berg915de2f2012-11-28 22:39:37 +0100584
Johannes Berg9caf0362012-11-29 01:25:20 +0100585 old = rcu_access_pointer(found->pub.beacon_ies);
586 ies = rcu_access_pointer(found->pub.ies);
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200587
Johannes Berg9caf0362012-11-29 01:25:20 +0100588 rcu_assign_pointer(found->pub.beacon_ies,
589 tmp->pub.beacon_ies);
Sven Neumann01123e22010-12-09 15:05:24 +0100590
591 /* Override IEs if they were from a beacon before */
Johannes Berg9caf0362012-11-29 01:25:20 +0100592 if (old == ies)
593 rcu_assign_pointer(found->pub.ies,
594 tmp->pub.beacon_ies);
Johannes Bergcd1658f2009-04-16 15:00:58 +0200595
Johannes Berg9caf0362012-11-29 01:25:20 +0100596 if (old)
597 kfree_rcu((struct cfg80211_bss_ies *)old,
598 rcu_head);
599 }
Johannes Berg2a519312009-02-10 21:25:55 +0100600 } else {
Johannes Berg9caf0362012-11-29 01:25:20 +0100601 struct cfg80211_internal_bss *new;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100602 struct cfg80211_internal_bss *hidden;
Johannes Berg9caf0362012-11-29 01:25:20 +0100603 struct cfg80211_bss_ies *ies;
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100604
605 /* First check if the beacon is a probe response from
606 * a hidden bss. If so, copy beacon ies (with nullified
607 * ssid) into the probe response bss entry (with real ssid).
608 * It is required basically for PSM implementation
609 * (probe responses do not contain tim ie) */
610
611 /* TODO: The code is not trying to update existing probe
612 * response bss entries when beacon ies are
613 * getting changed. */
Johannes Berg4593c4c2013-02-01 19:20:03 +0100614 hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
615 if (hidden) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100616 copy_hidden_ies(tmp, hidden);
Johannes Berg4593c4c2013-02-01 19:20:03 +0100617 } else {
618 hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_NUL);
619 if (hidden)
620 copy_hidden_ies(tmp, hidden);
621 }
Dmitry Tarnyagindd9dfb92011-11-04 17:12:07 +0100622
Johannes Berg9caf0362012-11-29 01:25:20 +0100623 /*
624 * create a copy -- the "res" variable that is passed in
625 * is allocated on the stack since it's not needed in the
626 * more common case of an update
627 */
628 new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
629 GFP_ATOMIC);
630 if (!new) {
631 ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
632 if (ies)
633 kfree_rcu(ies, rcu_head);
634 ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
635 if (ies)
636 kfree_rcu(ies, rcu_head);
637 spin_unlock_bh(&dev->bss_lock);
638 return NULL;
639 }
640 memcpy(new, tmp, sizeof(*new));
641 kref_init(&new->ref);
642 list_add_tail(&new->list, &dev->bss_list);
643 rb_insert_bss(dev, new);
644 found = new;
Johannes Berg2a519312009-02-10 21:25:55 +0100645 }
646
647 dev->bss_generation++;
648 spin_unlock_bh(&dev->bss_lock);
649
650 kref_get(&found->ref);
651 return found;
652}
653
Johannes Berg0172bb72012-11-23 14:23:30 +0100654static struct ieee80211_channel *
655cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
656 struct ieee80211_channel *channel)
657{
658 const u8 *tmp;
659 u32 freq;
660 int channel_number = -1;
661
662 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
663 if (tmp && tmp[1] == 1) {
664 channel_number = tmp[2];
665 } else {
666 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
667 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
668 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
669
670 channel_number = htop->primary_chan;
671 }
672 }
673
674 if (channel_number < 0)
675 return channel;
676
677 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
678 channel = ieee80211_get_channel(wiphy, freq);
679 if (!channel)
680 return NULL;
681 if (channel->flags & IEEE80211_CHAN_DISABLED)
682 return NULL;
683 return channel;
684}
685
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200686struct cfg80211_bss*
687cfg80211_inform_bss(struct wiphy *wiphy,
688 struct ieee80211_channel *channel,
Johannes Berg7b8bcff2012-03-13 13:57:04 +0100689 const u8 *bssid, u64 tsf, u16 capability,
690 u16 beacon_interval, const u8 *ie, size_t ielen,
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200691 s32 signal, gfp_t gfp)
692{
Johannes Berg9caf0362012-11-29 01:25:20 +0100693 struct cfg80211_bss_ies *ies;
694 struct cfg80211_internal_bss tmp = {}, *res;
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200695
696 if (WARN_ON(!wiphy))
697 return NULL;
698
Sujith22fe88d2010-05-13 10:34:08 +0530699 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200700 (signal < 0 || signal > 100)))
701 return NULL;
702
Johannes Berg0172bb72012-11-23 14:23:30 +0100703 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
704 if (!channel)
705 return NULL;
706
Johannes Berg9caf0362012-11-29 01:25:20 +0100707 memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
708 tmp.pub.channel = channel;
709 tmp.pub.signal = signal;
710 tmp.pub.tsf = tsf;
711 tmp.pub.beacon_interval = beacon_interval;
712 tmp.pub.capability = capability;
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200713 /*
714 * Since we do not know here whether the IEs are from a Beacon or Probe
715 * Response frame, we need to pick one of the options and only use it
716 * with the driver that does not provide the full Beacon/Probe Response
717 * frame. Use Beacon frame pointer to avoid indicating that this should
Johannes Berg50521aa2013-01-30 21:33:19 +0100718 * override the IEs pointer should we have received an earlier
Johannes Berg9caf0362012-11-29 01:25:20 +0100719 * indication of Probe Response data.
Jouni Malinen34a6edd2010-01-06 16:19:24 +0200720 */
Johannes Berg9caf0362012-11-29 01:25:20 +0100721 ies = kmalloc(sizeof(*ies) + ielen, gfp);
722 if (!ies)
723 return NULL;
724 ies->len = ielen;
725 memcpy(ies->data, ie, ielen);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200726
Johannes Berg9caf0362012-11-29 01:25:20 +0100727 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
728 rcu_assign_pointer(tmp.pub.ies, ies);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200729
Johannes Berg9caf0362012-11-29 01:25:20 +0100730 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200731 if (!res)
732 return NULL;
733
734 if (res->pub.capability & WLAN_CAPABILITY_ESS)
735 regulatory_hint_found_beacon(wiphy, channel, gfp);
736
Beni Lev4ee3e062012-08-27 12:49:39 +0300737 trace_cfg80211_return_bss(&res->pub);
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200738 /* cfg80211_bss_update gives us a referenced result */
739 return &res->pub;
740}
741EXPORT_SYMBOL(cfg80211_inform_bss);
742
Johannes Berg2a519312009-02-10 21:25:55 +0100743struct cfg80211_bss *
744cfg80211_inform_bss_frame(struct wiphy *wiphy,
745 struct ieee80211_channel *channel,
746 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c92009-02-18 18:45:06 +0100747 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100748{
Johannes Berg9caf0362012-11-29 01:25:20 +0100749 struct cfg80211_internal_bss tmp = {}, *res;
750 struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +0100751 size_t ielen = len - offsetof(struct ieee80211_mgmt,
752 u.probe_resp.variable);
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100753
Johannes Berg0172bb72012-11-23 14:23:30 +0100754 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
755 offsetof(struct ieee80211_mgmt, u.beacon.variable));
756
Beni Lev4ee3e062012-08-27 12:49:39 +0300757 trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
758
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100759 if (WARN_ON(!mgmt))
760 return NULL;
761
762 if (WARN_ON(!wiphy))
763 return NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100764
Sujith22fe88d2010-05-13 10:34:08 +0530765 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
Hila Gonen768be592012-08-26 11:00:28 +0300766 (signal < 0 || signal > 100)))
Johannes Berg2a519312009-02-10 21:25:55 +0100767 return NULL;
768
Mariusz Kozlowskibef9bac2011-03-26 19:26:55 +0100769 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
Johannes Berg2a519312009-02-10 21:25:55 +0100770 return NULL;
771
Johannes Berg0172bb72012-11-23 14:23:30 +0100772 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
773 ielen, channel);
774 if (!channel)
775 return NULL;
776
Johannes Berg9caf0362012-11-29 01:25:20 +0100777 ies = kmalloc(sizeof(*ies) + ielen, gfp);
778 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +0100779 return NULL;
Johannes Berg9caf0362012-11-29 01:25:20 +0100780 ies->len = ielen;
781 memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
Johannes Berg2a519312009-02-10 21:25:55 +0100782
Johannes Berg9caf0362012-11-29 01:25:20 +0100783 if (ieee80211_is_probe_resp(mgmt->frame_control))
784 rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
785 else
786 rcu_assign_pointer(tmp.pub.beacon_ies, ies);
787 rcu_assign_pointer(tmp.pub.ies, ies);
788
789 memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
790 tmp.pub.channel = channel;
791 tmp.pub.signal = signal;
792 tmp.pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
793 tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
794 tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
Johannes Berg2a519312009-02-10 21:25:55 +0100795
Johannes Berg9caf0362012-11-29 01:25:20 +0100796 res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
Johannes Berg2a519312009-02-10 21:25:55 +0100797 if (!res)
798 return NULL;
799
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500800 if (res->pub.capability & WLAN_CAPABILITY_ESS)
801 regulatory_hint_found_beacon(wiphy, channel, gfp);
802
Beni Lev4ee3e062012-08-27 12:49:39 +0300803 trace_cfg80211_return_bss(&res->pub);
Johannes Berg2a519312009-02-10 21:25:55 +0100804 /* cfg80211_bss_update gives us a referenced result */
805 return &res->pub;
806}
807EXPORT_SYMBOL(cfg80211_inform_bss_frame);
808
Johannes Berg4c0c0b72012-01-20 13:55:26 +0100809void cfg80211_ref_bss(struct cfg80211_bss *pub)
810{
811 struct cfg80211_internal_bss *bss;
812
813 if (!pub)
814 return;
815
816 bss = container_of(pub, struct cfg80211_internal_bss, pub);
817 kref_get(&bss->ref);
818}
819EXPORT_SYMBOL(cfg80211_ref_bss);
820
Johannes Berg2a519312009-02-10 21:25:55 +0100821void cfg80211_put_bss(struct cfg80211_bss *pub)
822{
823 struct cfg80211_internal_bss *bss;
824
825 if (!pub)
826 return;
827
828 bss = container_of(pub, struct cfg80211_internal_bss, pub);
829 kref_put(&bss->ref, bss_release);
830}
831EXPORT_SYMBOL(cfg80211_put_bss);
832
Johannes Bergd491af12009-02-10 21:25:58 +0100833void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
834{
835 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
836 struct cfg80211_internal_bss *bss;
837
838 if (WARN_ON(!pub))
839 return;
840
841 bss = container_of(pub, struct cfg80211_internal_bss, pub);
842
843 spin_lock_bh(&dev->bss_lock);
Johannes Berg32073902010-10-06 21:18:04 +0200844 if (!list_empty(&bss->list)) {
Juuso Oikarinen2b78ac92011-03-28 14:32:32 +0300845 __cfg80211_unlink_bss(dev, bss);
Johannes Berg32073902010-10-06 21:18:04 +0200846 dev->bss_generation++;
Johannes Berg32073902010-10-06 21:18:04 +0200847 }
Johannes Bergd491af12009-02-10 21:25:58 +0100848 spin_unlock_bh(&dev->bss_lock);
Johannes Bergd491af12009-02-10 21:25:58 +0100849}
850EXPORT_SYMBOL(cfg80211_unlink_bss);
851
Johannes Berg3d23e342009-09-29 23:27:28 +0200852#ifdef CONFIG_CFG80211_WEXT
Johannes Berg2a519312009-02-10 21:25:55 +0100853int cfg80211_wext_siwscan(struct net_device *dev,
854 struct iw_request_info *info,
855 union iwreq_data *wrqu, char *extra)
856{
857 struct cfg80211_registered_device *rdev;
858 struct wiphy *wiphy;
859 struct iw_scan_req *wreq = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100860 struct cfg80211_scan_request *creq = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +0100861 int i, err, n_channels = 0;
862 enum ieee80211_band band;
863
864 if (!netif_running(dev))
865 return -ENETDOWN;
866
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200867 if (wrqu->data.length == sizeof(struct iw_scan_req))
868 wreq = (struct iw_scan_req *)extra;
869
Johannes Berg463d0182009-07-14 00:33:35 +0200870 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100871
872 if (IS_ERR(rdev))
873 return PTR_ERR(rdev);
874
875 if (rdev->scan_req) {
876 err = -EBUSY;
877 goto out;
878 }
879
880 wiphy = &rdev->wiphy;
881
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200882 /* Determine number of channels, needed to allocate creq */
883 if (wreq && wreq->num_channels)
884 n_channels = wreq->num_channels;
885 else {
886 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
887 if (wiphy->bands[band])
888 n_channels += wiphy->bands[band]->n_channels;
889 }
Johannes Berg2a519312009-02-10 21:25:55 +0100890
891 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
892 n_channels * sizeof(void *),
893 GFP_ATOMIC);
894 if (!creq) {
895 err = -ENOMEM;
896 goto out;
897 }
898
899 creq->wiphy = wiphy;
Johannes Bergfd014282012-06-18 19:17:03 +0200900 creq->wdev = dev->ieee80211_ptr;
Johannes Berg5ba63532009-08-07 17:54:07 +0200901 /* SSIDs come after channels */
902 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +0100903 creq->n_channels = n_channels;
904 creq->n_ssids = 1;
Sam Leffler15d60302012-10-11 21:03:34 -0700905 creq->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +0100906
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200907 /* translate "Scan on frequencies" request */
Johannes Berg2a519312009-02-10 21:25:55 +0100908 i = 0;
909 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
910 int j;
Johannes Berg584991d2009-11-02 13:32:03 +0100911
Johannes Berg2a519312009-02-10 21:25:55 +0100912 if (!wiphy->bands[band])
913 continue;
Johannes Berg584991d2009-11-02 13:32:03 +0100914
Johannes Berg2a519312009-02-10 21:25:55 +0100915 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +0100916 /* ignore disabled channels */
917 if (wiphy->bands[band]->channels[j].flags &
918 IEEE80211_CHAN_DISABLED)
919 continue;
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200920
921 /* If we have a wireless request structure and the
922 * wireless request specifies frequencies, then search
923 * for the matching hardware channel.
924 */
925 if (wreq && wreq->num_channels) {
926 int k;
927 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
928 for (k = 0; k < wreq->num_channels; k++) {
Holger Schuriga4e7b732009-09-11 10:13:53 +0200929 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200930 if (wext_freq == wiphy_freq)
931 goto wext_freq_found;
932 }
933 goto wext_freq_not_found;
934 }
935
936 wext_freq_found:
Johannes Berg2a519312009-02-10 21:25:55 +0100937 creq->channels[i] = &wiphy->bands[band]->channels[j];
938 i++;
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200939 wext_freq_not_found: ;
Johannes Berg2a519312009-02-10 21:25:55 +0100940 }
941 }
Holger Schurig8862dc52009-09-11 10:13:55 +0200942 /* No channels found? */
943 if (!i) {
944 err = -EINVAL;
945 goto out;
946 }
Johannes Berg2a519312009-02-10 21:25:55 +0100947
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200948 /* Set real number of channels specified in creq->channels[] */
949 creq->n_channels = i;
Johannes Berg2a519312009-02-10 21:25:55 +0100950
Holger Schurigb2e3abd2009-09-09 13:09:54 +0200951 /* translate "Scan for SSID" request */
952 if (wreq) {
Johannes Berg2a519312009-02-10 21:25:55 +0100953 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Johannes Berg65486c82009-12-23 15:33:35 +0100954 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
955 err = -EINVAL;
956 goto out;
957 }
Johannes Berg2a519312009-02-10 21:25:55 +0100958 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
959 creq->ssids[0].ssid_len = wreq->essid_len;
960 }
961 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
962 creq->n_ssids = 0;
963 }
964
Johannes Berg34850ab2011-07-18 18:08:35 +0200965 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +0200966 if (wiphy->bands[i])
967 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +0200968
Johannes Berg2a519312009-02-10 21:25:55 +0100969 rdev->scan_req = creq;
Hila Gonene35e4d22012-06-27 17:19:42 +0300970 err = rdev_scan(rdev, creq);
Johannes Berg2a519312009-02-10 21:25:55 +0100971 if (err) {
972 rdev->scan_req = NULL;
Johannes Berg65486c82009-12-23 15:33:35 +0100973 /* creq will be freed below */
Johannes Berg463d0182009-07-14 00:33:35 +0200974 } else {
Johannes Bergfd014282012-06-18 19:17:03 +0200975 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
Johannes Berg65486c82009-12-23 15:33:35 +0100976 /* creq now owned by driver */
977 creq = NULL;
Johannes Berg463d0182009-07-14 00:33:35 +0200978 dev_hold(dev);
979 }
Johannes Berg2a519312009-02-10 21:25:55 +0100980 out:
Johannes Berg65486c82009-12-23 15:33:35 +0100981 kfree(creq);
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200982 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100983 return err;
984}
Johannes Bergba44cb72009-04-20 18:49:39 +0200985EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100986
987static void ieee80211_scan_add_ies(struct iw_request_info *info,
Johannes Berg9caf0362012-11-29 01:25:20 +0100988 const struct cfg80211_bss_ies *ies,
Johannes Berg2a519312009-02-10 21:25:55 +0100989 char **current_ev, char *end_buf)
990{
Johannes Berg9caf0362012-11-29 01:25:20 +0100991 const u8 *pos, *end, *next;
Johannes Berg2a519312009-02-10 21:25:55 +0100992 struct iw_event iwe;
993
Johannes Berg9caf0362012-11-29 01:25:20 +0100994 if (!ies)
Johannes Berg2a519312009-02-10 21:25:55 +0100995 return;
996
997 /*
998 * If needed, fragment the IEs buffer (at IE boundaries) into short
999 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1000 */
Johannes Berg9caf0362012-11-29 01:25:20 +01001001 pos = ies->data;
1002 end = pos + ies->len;
Johannes Berg2a519312009-02-10 21:25:55 +01001003
1004 while (end - pos > IW_GENERIC_IE_MAX) {
1005 next = pos + 2 + pos[1];
1006 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1007 next = next + 2 + next[1];
1008
1009 memset(&iwe, 0, sizeof(iwe));
1010 iwe.cmd = IWEVGENIE;
1011 iwe.u.data.length = next - pos;
1012 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001013 end_buf, &iwe,
1014 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001015
1016 pos = next;
1017 }
1018
1019 if (end > pos) {
1020 memset(&iwe, 0, sizeof(iwe));
1021 iwe.cmd = IWEVGENIE;
1022 iwe.u.data.length = end - pos;
1023 *current_ev = iwe_stream_add_point(info, *current_ev,
Johannes Berg9caf0362012-11-29 01:25:20 +01001024 end_buf, &iwe,
1025 (void *)pos);
Johannes Berg2a519312009-02-10 21:25:55 +01001026 }
1027}
1028
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001029static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
1030{
1031 unsigned long end = jiffies;
1032
1033 if (end >= start)
1034 return jiffies_to_msecs(end - start);
1035
1036 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
1037}
Johannes Berg2a519312009-02-10 21:25:55 +01001038
1039static char *
Johannes Berg77965c92009-02-18 18:45:06 +01001040ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1041 struct cfg80211_internal_bss *bss, char *current_ev,
1042 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +01001043{
Johannes Berg9caf0362012-11-29 01:25:20 +01001044 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01001045 struct iw_event iwe;
Johannes Berg9caf0362012-11-29 01:25:20 +01001046 const u8 *ie;
Johannes Berg2a519312009-02-10 21:25:55 +01001047 u8 *buf, *cfg, *p;
Johannes Berg9caf0362012-11-29 01:25:20 +01001048 int rem, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001049 bool ismesh = false;
1050
1051 memset(&iwe, 0, sizeof(iwe));
1052 iwe.cmd = SIOCGIWAP;
1053 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1054 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1055 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1056 IW_EV_ADDR_LEN);
1057
1058 memset(&iwe, 0, sizeof(iwe));
1059 iwe.cmd = SIOCGIWFREQ;
1060 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1061 iwe.u.freq.e = 0;
1062 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1063 IW_EV_FREQ_LEN);
1064
1065 memset(&iwe, 0, sizeof(iwe));
1066 iwe.cmd = SIOCGIWFREQ;
1067 iwe.u.freq.m = bss->pub.channel->center_freq;
1068 iwe.u.freq.e = 6;
1069 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1070 IW_EV_FREQ_LEN);
1071
Johannes Berg77965c92009-02-18 18:45:06 +01001072 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +01001073 memset(&iwe, 0, sizeof(iwe));
1074 iwe.cmd = IWEVQUAL;
1075 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1076 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +01001077 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c92009-02-18 18:45:06 +01001078 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01001079 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +01001080 sig = bss->pub.signal / 100;
1081 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +01001082 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +01001083 if (sig < -110) /* rather bad */
1084 sig = -110;
1085 else if (sig > -40) /* perfect */
1086 sig = -40;
1087 /* will give a range of 0 .. 70 */
1088 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +01001089 break;
1090 case CFG80211_SIGNAL_TYPE_UNSPEC:
1091 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +01001092 /* will give range 0 .. 100 */
1093 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +01001094 break;
1095 default:
1096 /* not reached */
1097 break;
1098 }
1099 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1100 &iwe, IW_EV_QUAL_LEN);
1101 }
1102
1103 memset(&iwe, 0, sizeof(iwe));
1104 iwe.cmd = SIOCGIWENCODE;
1105 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1106 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1107 else
1108 iwe.u.data.flags = IW_ENCODE_DISABLED;
1109 iwe.u.data.length = 0;
1110 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1111 &iwe, "");
1112
Johannes Berg9caf0362012-11-29 01:25:20 +01001113 rcu_read_lock();
1114 ies = rcu_dereference(bss->pub.ies);
1115 if (ies) {
1116 rem = ies->len;
1117 ie = ies->data;
1118 } else {
1119 rem = 0;
1120 ie = NULL;
1121 }
1122
1123 while (ies && rem >= 2) {
Johannes Berg2a519312009-02-10 21:25:55 +01001124 /* invalid data */
1125 if (ie[1] > rem - 2)
1126 break;
1127
1128 switch (ie[0]) {
1129 case WLAN_EID_SSID:
1130 memset(&iwe, 0, sizeof(iwe));
1131 iwe.cmd = SIOCGIWESSID;
1132 iwe.u.data.length = ie[1];
1133 iwe.u.data.flags = 1;
1134 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001135 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001136 break;
1137 case WLAN_EID_MESH_ID:
1138 memset(&iwe, 0, sizeof(iwe));
1139 iwe.cmd = SIOCGIWESSID;
1140 iwe.u.data.length = ie[1];
1141 iwe.u.data.flags = 1;
1142 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
Johannes Berg9caf0362012-11-29 01:25:20 +01001143 &iwe, (u8 *)ie + 2);
Johannes Berg2a519312009-02-10 21:25:55 +01001144 break;
1145 case WLAN_EID_MESH_CONFIG:
1146 ismesh = true;
Rui Paulo136cfa22009-11-18 18:40:00 +00001147 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
Johannes Berg2a519312009-02-10 21:25:55 +01001148 break;
1149 buf = kmalloc(50, GFP_ATOMIC);
1150 if (!buf)
1151 break;
Johannes Berg9caf0362012-11-29 01:25:20 +01001152 cfg = (u8 *)ie + 2;
Johannes Berg2a519312009-02-10 21:25:55 +01001153 memset(&iwe, 0, sizeof(iwe));
1154 iwe.cmd = IWEVCUSTOM;
Rui Paulo76aa5e72009-11-18 18:22:59 +00001155 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1156 "0x%02X", cfg[0]);
Johannes Berg2a519312009-02-10 21:25:55 +01001157 iwe.u.data.length = strlen(buf);
1158 current_ev = iwe_stream_add_point(info, current_ev,
1159 end_buf,
1160 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001161 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1162 cfg[1]);
Johannes Berg2a519312009-02-10 21:25:55 +01001163 iwe.u.data.length = strlen(buf);
1164 current_ev = iwe_stream_add_point(info, current_ev,
1165 end_buf,
1166 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001167 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1168 cfg[2]);
Johannes Berg2a519312009-02-10 21:25:55 +01001169 iwe.u.data.length = strlen(buf);
1170 current_ev = iwe_stream_add_point(info, current_ev,
1171 end_buf,
1172 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001173 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
Johannes Berg2a519312009-02-10 21:25:55 +01001174 iwe.u.data.length = strlen(buf);
1175 current_ev = iwe_stream_add_point(info, current_ev,
1176 end_buf,
1177 &iwe, buf);
Rui Paulo76aa5e72009-11-18 18:22:59 +00001178 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1179 iwe.u.data.length = strlen(buf);
1180 current_ev = iwe_stream_add_point(info, current_ev,
1181 end_buf,
1182 &iwe, buf);
1183 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1184 iwe.u.data.length = strlen(buf);
1185 current_ev = iwe_stream_add_point(info, current_ev,
1186 end_buf,
1187 &iwe, buf);
1188 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
Johannes Berg2a519312009-02-10 21:25:55 +01001189 iwe.u.data.length = strlen(buf);
1190 current_ev = iwe_stream_add_point(info, current_ev,
1191 end_buf,
1192 &iwe, buf);
1193 kfree(buf);
1194 break;
1195 case WLAN_EID_SUPP_RATES:
1196 case WLAN_EID_EXT_SUPP_RATES:
1197 /* display all supported rates in readable format */
1198 p = current_ev + iwe_stream_lcp_len(info);
1199
1200 memset(&iwe, 0, sizeof(iwe));
1201 iwe.cmd = SIOCGIWRATE;
1202 /* Those two flags are ignored... */
1203 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1204
1205 for (i = 0; i < ie[1]; i++) {
1206 iwe.u.bitrate.value =
1207 ((ie[i + 2] & 0x7f) * 500000);
1208 p = iwe_stream_add_value(info, current_ev, p,
1209 end_buf, &iwe, IW_EV_PARAM_LEN);
1210 }
1211 current_ev = p;
1212 break;
1213 }
1214 rem -= ie[1] + 2;
1215 ie += ie[1] + 2;
1216 }
1217
Joe Perchesf64f9e72009-11-29 16:55:45 -08001218 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1219 ismesh) {
Johannes Berg2a519312009-02-10 21:25:55 +01001220 memset(&iwe, 0, sizeof(iwe));
1221 iwe.cmd = SIOCGIWMODE;
1222 if (ismesh)
1223 iwe.u.mode = IW_MODE_MESH;
1224 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1225 iwe.u.mode = IW_MODE_MASTER;
1226 else
1227 iwe.u.mode = IW_MODE_ADHOC;
1228 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1229 &iwe, IW_EV_UINT_LEN);
1230 }
1231
1232 buf = kmalloc(30, GFP_ATOMIC);
1233 if (buf) {
1234 memset(&iwe, 0, sizeof(iwe));
1235 iwe.cmd = IWEVCUSTOM;
1236 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1237 iwe.u.data.length = strlen(buf);
1238 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1239 &iwe, buf);
1240 memset(&iwe, 0, sizeof(iwe));
1241 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -05001242 sprintf(buf, " Last beacon: %ums ago",
1243 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01001244 iwe.u.data.length = strlen(buf);
1245 current_ev = iwe_stream_add_point(info, current_ev,
1246 end_buf, &iwe, buf);
1247 kfree(buf);
1248 }
1249
Johannes Berg9caf0362012-11-29 01:25:20 +01001250 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
1251 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01001252
1253 return current_ev;
1254}
1255
1256
1257static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1258 struct iw_request_info *info,
1259 char *buf, size_t len)
1260{
1261 char *current_ev = buf;
1262 char *end_buf = buf + len;
1263 struct cfg80211_internal_bss *bss;
1264
1265 spin_lock_bh(&dev->bss_lock);
1266 cfg80211_bss_expire(dev);
1267
1268 list_for_each_entry(bss, &dev->bss_list, list) {
1269 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1270 spin_unlock_bh(&dev->bss_lock);
1271 return -E2BIG;
1272 }
Johannes Berg77965c92009-02-18 18:45:06 +01001273 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1274 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +01001275 }
1276 spin_unlock_bh(&dev->bss_lock);
1277 return current_ev - buf;
1278}
1279
1280
1281int cfg80211_wext_giwscan(struct net_device *dev,
1282 struct iw_request_info *info,
1283 struct iw_point *data, char *extra)
1284{
1285 struct cfg80211_registered_device *rdev;
1286 int res;
1287
1288 if (!netif_running(dev))
1289 return -ENETDOWN;
1290
Johannes Berg463d0182009-07-14 00:33:35 +02001291 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01001292
1293 if (IS_ERR(rdev))
1294 return PTR_ERR(rdev);
1295
1296 if (rdev->scan_req) {
1297 res = -EAGAIN;
1298 goto out;
1299 }
1300
1301 res = ieee80211_scan_results(rdev, info, extra, data->length);
1302 data->length = 0;
1303 if (res >= 0) {
1304 data->length = res;
1305 res = 0;
1306 }
1307
1308 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +02001309 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01001310 return res;
1311}
Johannes Bergba44cb72009-04-20 18:49:39 +02001312EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +01001313#endif