blob: e6c1f11595da05ec2e87976475e693bbd89d4022 [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>
7#include <linux/module.h>
8#include <linux/netdevice.h>
9#include <linux/wireless.h>
10#include <linux/nl80211.h>
11#include <linux/etherdevice.h>
12#include <net/arp.h>
13#include <net/cfg80211.h>
14#include <net/iw_handler.h>
15#include "core.h"
16#include "nl80211.h"
Johannes Berga9a11622009-07-27 12:01:53 +020017#include "wext-compat.h"
Johannes Berg2a519312009-02-10 21:25:55 +010018
Helmut Schaa09f97e02009-07-23 12:14:29 +020019#define IEEE80211_SCAN_RESULT_EXPIRE (15 * HZ)
Johannes Berg2a519312009-02-10 21:25:55 +010020
Johannes Berg667503d2009-07-07 03:56:11 +020021void __cfg80211_scan_done(struct work_struct *wk)
Johannes Berg2a519312009-02-10 21:25:55 +010022{
Johannes Berg667503d2009-07-07 03:56:11 +020023 struct cfg80211_registered_device *rdev;
24 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +010025 struct net_device *dev;
26#ifdef CONFIG_WIRELESS_EXT
27 union iwreq_data wrqu;
28#endif
29
Johannes Berg667503d2009-07-07 03:56:11 +020030 rdev = container_of(wk, struct cfg80211_registered_device,
31 scan_done_wk);
32
33 mutex_lock(&rdev->mtx);
34 request = rdev->scan_req;
35
Johannes Berg463d0182009-07-14 00:33:35 +020036 dev = request->dev;
Johannes Berg2a519312009-02-10 21:25:55 +010037
Johannes Berg6829c872009-07-02 09:13:27 +020038 /*
39 * This must be before sending the other events!
40 * Otherwise, wpa_supplicant gets completely confused with
41 * wext events.
42 */
43 cfg80211_sme_scan_done(dev);
44
Johannes Berg667503d2009-07-07 03:56:11 +020045 if (request->aborted)
Johannes Berg2a519312009-02-10 21:25:55 +010046 nl80211_send_scan_aborted(wiphy_to_dev(request->wiphy), dev);
47 else
48 nl80211_send_scan_done(wiphy_to_dev(request->wiphy), dev);
49
50#ifdef CONFIG_WIRELESS_EXT
Johannes Berg667503d2009-07-07 03:56:11 +020051 if (!request->aborted) {
Johannes Berg2a519312009-02-10 21:25:55 +010052 memset(&wrqu, 0, sizeof(wrqu));
53
54 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
55 }
56#endif
57
58 dev_put(dev);
59
Johannes Berg667503d2009-07-07 03:56:11 +020060 cfg80211_unlock_rdev(rdev);
Christian Lamparter9e81ecc2009-07-19 05:05:37 +020061 wiphy_to_dev(request->wiphy)->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +010062 kfree(request);
63}
Johannes Berg667503d2009-07-07 03:56:11 +020064
65void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
66{
Johannes Berg667503d2009-07-07 03:56:11 +020067 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
68
69 request->aborted = aborted;
70 schedule_work(&wiphy_to_dev(request->wiphy)->scan_done_wk);
Johannes Berg667503d2009-07-07 03:56:11 +020071}
Johannes Berg2a519312009-02-10 21:25:55 +010072EXPORT_SYMBOL(cfg80211_scan_done);
73
74static void bss_release(struct kref *ref)
75{
76 struct cfg80211_internal_bss *bss;
77
78 bss = container_of(ref, struct cfg80211_internal_bss, ref);
Johannes Berg78c1c7e2009-02-10 21:25:57 +010079 if (bss->pub.free_priv)
80 bss->pub.free_priv(&bss->pub);
Johannes Bergcd1658f2009-04-16 15:00:58 +020081
82 if (bss->ies_allocated)
83 kfree(bss->pub.information_elements);
84
Johannes Berg19957bb2009-07-02 17:20:43 +020085 BUG_ON(atomic_read(&bss->hold));
86
Johannes Berg2a519312009-02-10 21:25:55 +010087 kfree(bss);
88}
89
90/* must hold dev->bss_lock! */
Dan Williamscb3a8ee2009-02-11 17:14:43 -050091void cfg80211_bss_age(struct cfg80211_registered_device *dev,
92 unsigned long age_secs)
93{
94 struct cfg80211_internal_bss *bss;
95 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
96
97 list_for_each_entry(bss, &dev->bss_list, list) {
98 bss->ts -= age_jiffies;
99 }
100}
101
102/* must hold dev->bss_lock! */
Johannes Berg2a519312009-02-10 21:25:55 +0100103void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
104{
105 struct cfg80211_internal_bss *bss, *tmp;
106 bool expired = false;
107
108 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200109 if (atomic_read(&bss->hold))
110 continue;
111 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
Johannes Berg2a519312009-02-10 21:25:55 +0100112 continue;
113 list_del(&bss->list);
114 rb_erase(&bss->rbn, &dev->bss_tree);
115 kref_put(&bss->ref, bss_release);
116 expired = true;
117 }
118
119 if (expired)
120 dev->bss_generation++;
121}
122
123static u8 *find_ie(u8 num, u8 *ies, size_t len)
124{
125 while (len > 2 && ies[0] != num) {
126 len -= ies[1] + 2;
127 ies += ies[1] + 2;
128 }
129 if (len < 2)
130 return NULL;
131 if (len < 2 + ies[1])
132 return NULL;
133 return ies;
134}
135
136static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
137{
138 const u8 *ie1 = find_ie(num, ies1, len1);
139 const u8 *ie2 = find_ie(num, ies2, len2);
140 int r;
141
142 if (!ie1 && !ie2)
143 return 0;
Johannes Bergcd3468b2009-07-29 22:07:44 +0200144 if (!ie1 || !ie2)
Johannes Berg2a519312009-02-10 21:25:55 +0100145 return -1;
146
147 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
148 if (r == 0 && ie1[1] != ie2[1])
149 return ie2[1] - ie1[1];
150 return r;
151}
152
153static bool is_bss(struct cfg80211_bss *a,
154 const u8 *bssid,
155 const u8 *ssid, size_t ssid_len)
156{
157 const u8 *ssidie;
158
Johannes Berg79420f02009-02-10 21:25:59 +0100159 if (bssid && compare_ether_addr(a->bssid, bssid))
Johannes Berg2a519312009-02-10 21:25:55 +0100160 return false;
161
Johannes Berg79420f02009-02-10 21:25:59 +0100162 if (!ssid)
163 return true;
164
Johannes Berg2a519312009-02-10 21:25:55 +0100165 ssidie = find_ie(WLAN_EID_SSID,
166 a->information_elements,
167 a->len_information_elements);
168 if (!ssidie)
169 return false;
170 if (ssidie[1] != ssid_len)
171 return false;
172 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
173}
174
175static bool is_mesh(struct cfg80211_bss *a,
176 const u8 *meshid, size_t meshidlen,
177 const u8 *meshcfg)
178{
179 const u8 *ie;
180
181 if (!is_zero_ether_addr(a->bssid))
182 return false;
183
184 ie = find_ie(WLAN_EID_MESH_ID,
185 a->information_elements,
186 a->len_information_elements);
187 if (!ie)
188 return false;
189 if (ie[1] != meshidlen)
190 return false;
191 if (memcmp(ie + 2, meshid, meshidlen))
192 return false;
193
194 ie = find_ie(WLAN_EID_MESH_CONFIG,
195 a->information_elements,
196 a->len_information_elements);
Johannes Bergcd3468b2009-07-29 22:07:44 +0200197 if (!ie)
198 return false;
Johannes Berg2a519312009-02-10 21:25:55 +0100199 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
200 return false;
201
202 /*
203 * Ignore mesh capability (last two bytes of the IE) when
204 * comparing since that may differ between stations taking
205 * part in the same mesh.
206 */
207 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
208}
209
210static int cmp_bss(struct cfg80211_bss *a,
211 struct cfg80211_bss *b)
212{
213 int r;
214
215 if (a->channel != b->channel)
216 return b->channel->center_freq - a->channel->center_freq;
217
218 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
219 if (r)
220 return r;
221
222 if (is_zero_ether_addr(a->bssid)) {
223 r = cmp_ies(WLAN_EID_MESH_ID,
224 a->information_elements,
225 a->len_information_elements,
226 b->information_elements,
227 b->len_information_elements);
228 if (r)
229 return r;
230 return cmp_ies(WLAN_EID_MESH_CONFIG,
231 a->information_elements,
232 a->len_information_elements,
233 b->information_elements,
234 b->len_information_elements);
235 }
236
237 return cmp_ies(WLAN_EID_SSID,
238 a->information_elements,
239 a->len_information_elements,
240 b->information_elements,
241 b->len_information_elements);
242}
243
244struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
245 struct ieee80211_channel *channel,
246 const u8 *bssid,
Johannes Berg79420f02009-02-10 21:25:59 +0100247 const u8 *ssid, size_t ssid_len,
248 u16 capa_mask, u16 capa_val)
Johannes Berg2a519312009-02-10 21:25:55 +0100249{
250 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
251 struct cfg80211_internal_bss *bss, *res = NULL;
252
253 spin_lock_bh(&dev->bss_lock);
254
255 list_for_each_entry(bss, &dev->bss_list, list) {
Johannes Berg79420f02009-02-10 21:25:59 +0100256 if ((bss->pub.capability & capa_mask) != capa_val)
257 continue;
Johannes Berg2a519312009-02-10 21:25:55 +0100258 if (channel && bss->pub.channel != channel)
259 continue;
260 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
261 res = bss;
262 kref_get(&res->ref);
263 break;
264 }
265 }
266
267 spin_unlock_bh(&dev->bss_lock);
268 if (!res)
269 return NULL;
270 return &res->pub;
271}
272EXPORT_SYMBOL(cfg80211_get_bss);
273
274struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
275 struct ieee80211_channel *channel,
276 const u8 *meshid, size_t meshidlen,
277 const u8 *meshcfg)
278{
279 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
280 struct cfg80211_internal_bss *bss, *res = NULL;
281
282 spin_lock_bh(&dev->bss_lock);
283
284 list_for_each_entry(bss, &dev->bss_list, list) {
285 if (channel && bss->pub.channel != channel)
286 continue;
287 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
288 res = bss;
289 kref_get(&res->ref);
290 break;
291 }
292 }
293
294 spin_unlock_bh(&dev->bss_lock);
295 if (!res)
296 return NULL;
297 return &res->pub;
298}
299EXPORT_SYMBOL(cfg80211_get_mesh);
300
301
302static void rb_insert_bss(struct cfg80211_registered_device *dev,
303 struct cfg80211_internal_bss *bss)
304{
305 struct rb_node **p = &dev->bss_tree.rb_node;
306 struct rb_node *parent = NULL;
307 struct cfg80211_internal_bss *tbss;
308 int cmp;
309
310 while (*p) {
311 parent = *p;
312 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
313
314 cmp = cmp_bss(&bss->pub, &tbss->pub);
315
316 if (WARN_ON(!cmp)) {
317 /* will sort of leak this BSS */
318 return;
319 }
320
321 if (cmp < 0)
322 p = &(*p)->rb_left;
323 else
324 p = &(*p)->rb_right;
325 }
326
327 rb_link_node(&bss->rbn, parent, p);
328 rb_insert_color(&bss->rbn, &dev->bss_tree);
329}
330
331static struct cfg80211_internal_bss *
332rb_find_bss(struct cfg80211_registered_device *dev,
333 struct cfg80211_internal_bss *res)
334{
335 struct rb_node *n = dev->bss_tree.rb_node;
336 struct cfg80211_internal_bss *bss;
337 int r;
338
339 while (n) {
340 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
341 r = cmp_bss(&res->pub, &bss->pub);
342
343 if (r == 0)
344 return bss;
345 else if (r < 0)
346 n = n->rb_left;
347 else
348 n = n->rb_right;
349 }
350
351 return NULL;
352}
353
354static struct cfg80211_internal_bss *
355cfg80211_bss_update(struct cfg80211_registered_device *dev,
356 struct cfg80211_internal_bss *res,
357 bool overwrite)
358{
359 struct cfg80211_internal_bss *found = NULL;
360 const u8 *meshid, *meshcfg;
361
362 /*
363 * The reference to "res" is donated to this function.
364 */
365
366 if (WARN_ON(!res->pub.channel)) {
367 kref_put(&res->ref, bss_release);
368 return NULL;
369 }
370
371 res->ts = jiffies;
372
373 if (is_zero_ether_addr(res->pub.bssid)) {
374 /* must be mesh, verify */
375 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
376 res->pub.len_information_elements);
377 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
378 res->pub.information_elements,
379 res->pub.len_information_elements);
380 if (!meshid || !meshcfg ||
381 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
382 /* bogus mesh */
383 kref_put(&res->ref, bss_release);
384 return NULL;
385 }
386 }
387
388 spin_lock_bh(&dev->bss_lock);
389
390 found = rb_find_bss(dev, res);
391
Johannes Bergcd1658f2009-04-16 15:00:58 +0200392 if (found) {
Johannes Berg2a519312009-02-10 21:25:55 +0100393 found->pub.beacon_interval = res->pub.beacon_interval;
394 found->pub.tsf = res->pub.tsf;
395 found->pub.signal = res->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100396 found->pub.capability = res->pub.capability;
397 found->ts = res->ts;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200398
399 /* overwrite IEs */
400 if (overwrite) {
401 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
402 size_t ielen = res->pub.len_information_elements;
403
Michael Buesch44e1b982009-04-26 11:27:33 +0200404 if (!found->ies_allocated && ksize(found) >= used + ielen) {
Johannes Bergcd1658f2009-04-16 15:00:58 +0200405 memcpy(found->pub.information_elements,
406 res->pub.information_elements, ielen);
407 found->pub.len_information_elements = ielen;
408 } else {
409 u8 *ies = found->pub.information_elements;
410
Michael Buesch273de922009-04-25 22:28:55 +0200411 if (found->ies_allocated)
412 ies = krealloc(ies, ielen, GFP_ATOMIC);
413 else
Johannes Bergcd1658f2009-04-16 15:00:58 +0200414 ies = kmalloc(ielen, GFP_ATOMIC);
415
416 if (ies) {
417 memcpy(ies, res->pub.information_elements, ielen);
418 found->ies_allocated = true;
419 found->pub.information_elements = ies;
Johannes Bergc0f0aac2009-04-30 20:09:56 +0200420 found->pub.len_information_elements = ielen;
Johannes Bergcd1658f2009-04-16 15:00:58 +0200421 }
422 }
423 }
424
Johannes Berg2a519312009-02-10 21:25:55 +0100425 kref_put(&res->ref, bss_release);
426 } else {
427 /* this "consumes" the reference */
428 list_add_tail(&res->list, &dev->bss_list);
429 rb_insert_bss(dev, res);
430 found = res;
431 }
432
433 dev->bss_generation++;
434 spin_unlock_bh(&dev->bss_lock);
435
436 kref_get(&found->ref);
437 return found;
438}
439
Jussi Kivilinna06aa7af2009-03-26 23:40:09 +0200440struct cfg80211_bss*
441cfg80211_inform_bss(struct wiphy *wiphy,
442 struct ieee80211_channel *channel,
443 const u8 *bssid,
444 u64 timestamp, u16 capability, u16 beacon_interval,
445 const u8 *ie, size_t ielen,
446 s32 signal, gfp_t gfp)
447{
448 struct cfg80211_internal_bss *res;
449 size_t privsz;
450
451 if (WARN_ON(!wiphy))
452 return NULL;
453
454 privsz = wiphy->bss_priv_size;
455
456 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
457 (signal < 0 || signal > 100)))
458 return NULL;
459
460 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
461 if (!res)
462 return NULL;
463
464 memcpy(res->pub.bssid, bssid, ETH_ALEN);
465 res->pub.channel = channel;
466 res->pub.signal = signal;
467 res->pub.tsf = timestamp;
468 res->pub.beacon_interval = beacon_interval;
469 res->pub.capability = capability;
470 /* point to after the private area */
471 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
472 memcpy(res->pub.information_elements, ie, ielen);
473 res->pub.len_information_elements = ielen;
474
475 kref_init(&res->ref);
476
477 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
478 if (!res)
479 return NULL;
480
481 if (res->pub.capability & WLAN_CAPABILITY_ESS)
482 regulatory_hint_found_beacon(wiphy, channel, gfp);
483
484 /* cfg80211_bss_update gives us a referenced result */
485 return &res->pub;
486}
487EXPORT_SYMBOL(cfg80211_inform_bss);
488
Johannes Berg2a519312009-02-10 21:25:55 +0100489struct cfg80211_bss *
490cfg80211_inform_bss_frame(struct wiphy *wiphy,
491 struct ieee80211_channel *channel,
492 struct ieee80211_mgmt *mgmt, size_t len,
Johannes Berg77965c972009-02-18 18:45:06 +0100493 s32 signal, gfp_t gfp)
Johannes Berg2a519312009-02-10 21:25:55 +0100494{
495 struct cfg80211_internal_bss *res;
496 size_t ielen = len - offsetof(struct ieee80211_mgmt,
497 u.probe_resp.variable);
498 bool overwrite;
499 size_t privsz = wiphy->bss_priv_size;
500
Johannes Berg77965c972009-02-18 18:45:06 +0100501 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
Johannes Berg2a519312009-02-10 21:25:55 +0100502 (signal < 0 || signal > 100)))
503 return NULL;
504
505 if (WARN_ON(!mgmt || !wiphy ||
506 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
507 return NULL;
508
509 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
510 if (!res)
511 return NULL;
512
513 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
514 res->pub.channel = channel;
Johannes Berg2a519312009-02-10 21:25:55 +0100515 res->pub.signal = signal;
516 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
517 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
518 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
519 /* point to after the private area */
520 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
521 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
522 res->pub.len_information_elements = ielen;
523
524 kref_init(&res->ref);
525
526 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
527
528 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
529 if (!res)
530 return NULL;
531
Luis R. Rodrigueze38f8a72009-02-21 00:20:39 -0500532 if (res->pub.capability & WLAN_CAPABILITY_ESS)
533 regulatory_hint_found_beacon(wiphy, channel, gfp);
534
Johannes Berg2a519312009-02-10 21:25:55 +0100535 /* cfg80211_bss_update gives us a referenced result */
536 return &res->pub;
537}
538EXPORT_SYMBOL(cfg80211_inform_bss_frame);
539
540void cfg80211_put_bss(struct cfg80211_bss *pub)
541{
542 struct cfg80211_internal_bss *bss;
543
544 if (!pub)
545 return;
546
547 bss = container_of(pub, struct cfg80211_internal_bss, pub);
548 kref_put(&bss->ref, bss_release);
549}
550EXPORT_SYMBOL(cfg80211_put_bss);
551
Johannes Bergd491af12009-02-10 21:25:58 +0100552void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
553{
554 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
555 struct cfg80211_internal_bss *bss;
556
557 if (WARN_ON(!pub))
558 return;
559
560 bss = container_of(pub, struct cfg80211_internal_bss, pub);
561
562 spin_lock_bh(&dev->bss_lock);
563
564 list_del(&bss->list);
Johannes Bergf5ea9122009-08-07 16:17:38 +0200565 dev->bss_generation++;
Johannes Bergd491af12009-02-10 21:25:58 +0100566 rb_erase(&bss->rbn, &dev->bss_tree);
567
568 spin_unlock_bh(&dev->bss_lock);
569
570 kref_put(&bss->ref, bss_release);
571}
572EXPORT_SYMBOL(cfg80211_unlink_bss);
573
Johannes Berg2a519312009-02-10 21:25:55 +0100574#ifdef CONFIG_WIRELESS_EXT
575int cfg80211_wext_siwscan(struct net_device *dev,
576 struct iw_request_info *info,
577 union iwreq_data *wrqu, char *extra)
578{
579 struct cfg80211_registered_device *rdev;
580 struct wiphy *wiphy;
581 struct iw_scan_req *wreq = NULL;
582 struct cfg80211_scan_request *creq;
583 int i, err, n_channels = 0;
584 enum ieee80211_band band;
585
586 if (!netif_running(dev))
587 return -ENETDOWN;
588
Johannes Berg463d0182009-07-14 00:33:35 +0200589 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100590
591 if (IS_ERR(rdev))
592 return PTR_ERR(rdev);
593
594 if (rdev->scan_req) {
595 err = -EBUSY;
596 goto out;
597 }
598
599 wiphy = &rdev->wiphy;
600
601 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
602 if (wiphy->bands[band])
603 n_channels += wiphy->bands[band]->n_channels;
604
605 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
606 n_channels * sizeof(void *),
607 GFP_ATOMIC);
608 if (!creq) {
609 err = -ENOMEM;
610 goto out;
611 }
612
613 creq->wiphy = wiphy;
Johannes Berg463d0182009-07-14 00:33:35 +0200614 creq->dev = dev;
Johannes Berg5ba63532009-08-07 17:54:07 +0200615 /* SSIDs come after channels */
616 creq->ssids = (void *)&creq->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +0100617 creq->n_channels = n_channels;
618 creq->n_ssids = 1;
619
620 /* all channels */
621 i = 0;
622 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
623 int j;
624 if (!wiphy->bands[band])
625 continue;
626 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
627 creq->channels[i] = &wiphy->bands[band]->channels[j];
628 i++;
629 }
630 }
631
632 /* translate scan request */
633 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
634 wreq = (struct iw_scan_req *)extra;
635
636 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
637 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
638 return -EINVAL;
639 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
640 creq->ssids[0].ssid_len = wreq->essid_len;
641 }
642 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
643 creq->n_ssids = 0;
644 }
645
646 rdev->scan_req = creq;
647 err = rdev->ops->scan(wiphy, dev, creq);
648 if (err) {
649 rdev->scan_req = NULL;
650 kfree(creq);
Johannes Berg463d0182009-07-14 00:33:35 +0200651 } else {
Johannes Berga538e2d2009-06-16 19:56:42 +0200652 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +0200653 dev_hold(dev);
654 }
Johannes Berg2a519312009-02-10 21:25:55 +0100655 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200656 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100657 return err;
658}
Johannes Bergba44cb72009-04-20 18:49:39 +0200659EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100660
661static void ieee80211_scan_add_ies(struct iw_request_info *info,
662 struct cfg80211_bss *bss,
663 char **current_ev, char *end_buf)
664{
665 u8 *pos, *end, *next;
666 struct iw_event iwe;
667
668 if (!bss->information_elements ||
669 !bss->len_information_elements)
670 return;
671
672 /*
673 * If needed, fragment the IEs buffer (at IE boundaries) into short
674 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
675 */
676 pos = bss->information_elements;
677 end = pos + bss->len_information_elements;
678
679 while (end - pos > IW_GENERIC_IE_MAX) {
680 next = pos + 2 + pos[1];
681 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
682 next = next + 2 + next[1];
683
684 memset(&iwe, 0, sizeof(iwe));
685 iwe.cmd = IWEVGENIE;
686 iwe.u.data.length = next - pos;
687 *current_ev = iwe_stream_add_point(info, *current_ev,
688 end_buf, &iwe, pos);
689
690 pos = next;
691 }
692
693 if (end > pos) {
694 memset(&iwe, 0, sizeof(iwe));
695 iwe.cmd = IWEVGENIE;
696 iwe.u.data.length = end - pos;
697 *current_ev = iwe_stream_add_point(info, *current_ev,
698 end_buf, &iwe, pos);
699 }
700}
701
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500702static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
703{
704 unsigned long end = jiffies;
705
706 if (end >= start)
707 return jiffies_to_msecs(end - start);
708
709 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
710}
Johannes Berg2a519312009-02-10 21:25:55 +0100711
712static char *
Johannes Berg77965c972009-02-18 18:45:06 +0100713ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
714 struct cfg80211_internal_bss *bss, char *current_ev,
715 char *end_buf)
Johannes Berg2a519312009-02-10 21:25:55 +0100716{
717 struct iw_event iwe;
718 u8 *buf, *cfg, *p;
719 u8 *ie = bss->pub.information_elements;
Johannes Berga77b8552009-02-18 18:27:22 +0100720 int rem = bss->pub.len_information_elements, i, sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100721 bool ismesh = false;
722
723 memset(&iwe, 0, sizeof(iwe));
724 iwe.cmd = SIOCGIWAP;
725 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
726 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
727 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
728 IW_EV_ADDR_LEN);
729
730 memset(&iwe, 0, sizeof(iwe));
731 iwe.cmd = SIOCGIWFREQ;
732 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
733 iwe.u.freq.e = 0;
734 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
735 IW_EV_FREQ_LEN);
736
737 memset(&iwe, 0, sizeof(iwe));
738 iwe.cmd = SIOCGIWFREQ;
739 iwe.u.freq.m = bss->pub.channel->center_freq;
740 iwe.u.freq.e = 6;
741 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
742 IW_EV_FREQ_LEN);
743
Johannes Berg77965c972009-02-18 18:45:06 +0100744 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
Johannes Berg2a519312009-02-10 21:25:55 +0100745 memset(&iwe, 0, sizeof(iwe));
746 iwe.cmd = IWEVQUAL;
747 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
748 IW_QUAL_NOISE_INVALID |
Johannes Berga77b8552009-02-18 18:27:22 +0100749 IW_QUAL_QUAL_UPDATED;
Johannes Berg77965c972009-02-18 18:45:06 +0100750 switch (wiphy->signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +0100751 case CFG80211_SIGNAL_TYPE_MBM:
Johannes Berga77b8552009-02-18 18:27:22 +0100752 sig = bss->pub.signal / 100;
753 iwe.u.qual.level = sig;
Johannes Berg2a519312009-02-10 21:25:55 +0100754 iwe.u.qual.updated |= IW_QUAL_DBM;
Johannes Berga77b8552009-02-18 18:27:22 +0100755 if (sig < -110) /* rather bad */
756 sig = -110;
757 else if (sig > -40) /* perfect */
758 sig = -40;
759 /* will give a range of 0 .. 70 */
760 iwe.u.qual.qual = sig + 110;
Johannes Berg2a519312009-02-10 21:25:55 +0100761 break;
762 case CFG80211_SIGNAL_TYPE_UNSPEC:
763 iwe.u.qual.level = bss->pub.signal;
Johannes Berga77b8552009-02-18 18:27:22 +0100764 /* will give range 0 .. 100 */
765 iwe.u.qual.qual = bss->pub.signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100766 break;
767 default:
768 /* not reached */
769 break;
770 }
771 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
772 &iwe, IW_EV_QUAL_LEN);
773 }
774
775 memset(&iwe, 0, sizeof(iwe));
776 iwe.cmd = SIOCGIWENCODE;
777 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
778 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
779 else
780 iwe.u.data.flags = IW_ENCODE_DISABLED;
781 iwe.u.data.length = 0;
782 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
783 &iwe, "");
784
785 while (rem >= 2) {
786 /* invalid data */
787 if (ie[1] > rem - 2)
788 break;
789
790 switch (ie[0]) {
791 case WLAN_EID_SSID:
792 memset(&iwe, 0, sizeof(iwe));
793 iwe.cmd = SIOCGIWESSID;
794 iwe.u.data.length = ie[1];
795 iwe.u.data.flags = 1;
796 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
797 &iwe, ie + 2);
798 break;
799 case WLAN_EID_MESH_ID:
800 memset(&iwe, 0, sizeof(iwe));
801 iwe.cmd = SIOCGIWESSID;
802 iwe.u.data.length = ie[1];
803 iwe.u.data.flags = 1;
804 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
805 &iwe, ie + 2);
806 break;
807 case WLAN_EID_MESH_CONFIG:
808 ismesh = true;
809 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
810 break;
811 buf = kmalloc(50, GFP_ATOMIC);
812 if (!buf)
813 break;
814 cfg = ie + 2;
815 memset(&iwe, 0, sizeof(iwe));
816 iwe.cmd = IWEVCUSTOM;
817 sprintf(buf, "Mesh network (version %d)", cfg[0]);
818 iwe.u.data.length = strlen(buf);
819 current_ev = iwe_stream_add_point(info, current_ev,
820 end_buf,
821 &iwe, buf);
822 sprintf(buf, "Path Selection Protocol ID: "
823 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
824 cfg[4]);
825 iwe.u.data.length = strlen(buf);
826 current_ev = iwe_stream_add_point(info, current_ev,
827 end_buf,
828 &iwe, buf);
829 sprintf(buf, "Path Selection Metric ID: "
830 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
831 cfg[8]);
832 iwe.u.data.length = strlen(buf);
833 current_ev = iwe_stream_add_point(info, current_ev,
834 end_buf,
835 &iwe, buf);
836 sprintf(buf, "Congestion Control Mode ID: "
837 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
838 cfg[11], cfg[12]);
839 iwe.u.data.length = strlen(buf);
840 current_ev = iwe_stream_add_point(info, current_ev,
841 end_buf,
842 &iwe, buf);
843 sprintf(buf, "Channel Precedence: "
844 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
845 cfg[15], cfg[16]);
846 iwe.u.data.length = strlen(buf);
847 current_ev = iwe_stream_add_point(info, current_ev,
848 end_buf,
849 &iwe, buf);
850 kfree(buf);
851 break;
852 case WLAN_EID_SUPP_RATES:
853 case WLAN_EID_EXT_SUPP_RATES:
854 /* display all supported rates in readable format */
855 p = current_ev + iwe_stream_lcp_len(info);
856
857 memset(&iwe, 0, sizeof(iwe));
858 iwe.cmd = SIOCGIWRATE;
859 /* Those two flags are ignored... */
860 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
861
862 for (i = 0; i < ie[1]; i++) {
863 iwe.u.bitrate.value =
864 ((ie[i + 2] & 0x7f) * 500000);
865 p = iwe_stream_add_value(info, current_ev, p,
866 end_buf, &iwe, IW_EV_PARAM_LEN);
867 }
868 current_ev = p;
869 break;
870 }
871 rem -= ie[1] + 2;
872 ie += ie[1] + 2;
873 }
874
875 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
876 || ismesh) {
877 memset(&iwe, 0, sizeof(iwe));
878 iwe.cmd = SIOCGIWMODE;
879 if (ismesh)
880 iwe.u.mode = IW_MODE_MESH;
881 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
882 iwe.u.mode = IW_MODE_MASTER;
883 else
884 iwe.u.mode = IW_MODE_ADHOC;
885 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
886 &iwe, IW_EV_UINT_LEN);
887 }
888
889 buf = kmalloc(30, GFP_ATOMIC);
890 if (buf) {
891 memset(&iwe, 0, sizeof(iwe));
892 iwe.cmd = IWEVCUSTOM;
893 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
894 iwe.u.data.length = strlen(buf);
895 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
896 &iwe, buf);
897 memset(&iwe, 0, sizeof(iwe));
898 iwe.cmd = IWEVCUSTOM;
Dan Williamscb3a8ee2009-02-11 17:14:43 -0500899 sprintf(buf, " Last beacon: %ums ago",
900 elapsed_jiffies_msecs(bss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +0100901 iwe.u.data.length = strlen(buf);
902 current_ev = iwe_stream_add_point(info, current_ev,
903 end_buf, &iwe, buf);
904 kfree(buf);
905 }
906
907 ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
908
909 return current_ev;
910}
911
912
913static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
914 struct iw_request_info *info,
915 char *buf, size_t len)
916{
917 char *current_ev = buf;
918 char *end_buf = buf + len;
919 struct cfg80211_internal_bss *bss;
920
921 spin_lock_bh(&dev->bss_lock);
922 cfg80211_bss_expire(dev);
923
924 list_for_each_entry(bss, &dev->bss_list, list) {
925 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
926 spin_unlock_bh(&dev->bss_lock);
927 return -E2BIG;
928 }
Johannes Berg77965c972009-02-18 18:45:06 +0100929 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
930 current_ev, end_buf);
Johannes Berg2a519312009-02-10 21:25:55 +0100931 }
932 spin_unlock_bh(&dev->bss_lock);
933 return current_ev - buf;
934}
935
936
937int cfg80211_wext_giwscan(struct net_device *dev,
938 struct iw_request_info *info,
939 struct iw_point *data, char *extra)
940{
941 struct cfg80211_registered_device *rdev;
942 int res;
943
944 if (!netif_running(dev))
945 return -ENETDOWN;
946
Johannes Berg463d0182009-07-14 00:33:35 +0200947 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +0100948
949 if (IS_ERR(rdev))
950 return PTR_ERR(rdev);
951
952 if (rdev->scan_req) {
953 res = -EAGAIN;
954 goto out;
955 }
956
957 res = ieee80211_scan_results(rdev, info, extra, data->length);
958 data->length = 0;
959 if (res >= 0) {
960 data->length = res;
961 res = 0;
962 }
963
964 out:
Johannes Berg4d0c8ae2009-07-07 03:56:09 +0200965 cfg80211_unlock_rdev(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +0100966 return res;
967}
Johannes Bergba44cb72009-04-20 18:49:39 +0200968EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
Johannes Berg2a519312009-02-10 21:25:55 +0100969#endif