2 * cfg80211 scan result handling
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/wireless.h>
11 #include <linux/nl80211.h>
12 #include <linux/etherdevice.h>
14 #include <net/cfg80211.h>
15 #include <net/iw_handler.h>
18 #include "wext-compat.h"
20 #define IEEE80211_SCAN_RESULT_EXPIRE (15 * HZ)
22 void ___cfg80211_scan_done(struct cfg80211_registered_device
*rdev
, bool leak
)
24 struct cfg80211_scan_request
*request
;
25 struct net_device
*dev
;
26 #ifdef CONFIG_CFG80211_WEXT
27 union iwreq_data wrqu
;
30 ASSERT_RDEV_LOCK(rdev
);
32 request
= rdev
->scan_req
;
40 * This must be before sending the other events!
41 * Otherwise, wpa_supplicant gets completely confused with
44 cfg80211_sme_scan_done(dev
);
47 nl80211_send_scan_aborted(rdev
, dev
);
49 nl80211_send_scan_done(rdev
, dev
);
51 #ifdef CONFIG_CFG80211_WEXT
52 if (!request
->aborted
) {
53 memset(&wrqu
, 0, sizeof(wrqu
));
55 wireless_send_event(dev
, SIOCGIWSCAN
, &wrqu
, NULL
);
61 rdev
->scan_req
= NULL
;
64 * OK. If this is invoked with "leak" then we can't
65 * free this ... but we've cleaned it up anyway. The
66 * driver failed to call the scan_done callback, so
67 * all bets are off, it might still be trying to use
68 * the scan request or not ... if it accesses the dev
69 * in there (it shouldn't anyway) then it may crash.
75 void __cfg80211_scan_done(struct work_struct
*wk
)
77 struct cfg80211_registered_device
*rdev
;
79 rdev
= container_of(wk
, struct cfg80211_registered_device
,
82 cfg80211_lock_rdev(rdev
);
83 ___cfg80211_scan_done(rdev
, false);
84 cfg80211_unlock_rdev(rdev
);
87 void cfg80211_scan_done(struct cfg80211_scan_request
*request
, bool aborted
)
89 WARN_ON(request
!= wiphy_to_dev(request
->wiphy
)->scan_req
);
91 request
->aborted
= aborted
;
92 queue_work(cfg80211_wq
, &wiphy_to_dev(request
->wiphy
)->scan_done_wk
);
94 EXPORT_SYMBOL(cfg80211_scan_done
);
96 void __cfg80211_sched_scan_results(struct work_struct
*wk
)
98 struct cfg80211_registered_device
*rdev
;
100 rdev
= container_of(wk
, struct cfg80211_registered_device
,
101 sched_scan_results_wk
);
103 mutex_lock(&rdev
->sched_scan_mtx
);
105 /* we don't have sched_scan_req anymore if the scan is stopping */
106 if (rdev
->sched_scan_req
)
107 nl80211_send_sched_scan_results(rdev
,
108 rdev
->sched_scan_req
->dev
);
110 mutex_unlock(&rdev
->sched_scan_mtx
);
113 void cfg80211_sched_scan_results(struct wiphy
*wiphy
)
115 /* ignore if we're not scanning */
116 if (wiphy_to_dev(wiphy
)->sched_scan_req
)
117 queue_work(cfg80211_wq
,
118 &wiphy_to_dev(wiphy
)->sched_scan_results_wk
);
120 EXPORT_SYMBOL(cfg80211_sched_scan_results
);
122 void cfg80211_sched_scan_stopped(struct wiphy
*wiphy
)
124 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
126 mutex_lock(&rdev
->sched_scan_mtx
);
127 __cfg80211_stop_sched_scan(rdev
, true);
128 mutex_unlock(&rdev
->sched_scan_mtx
);
130 EXPORT_SYMBOL(cfg80211_sched_scan_stopped
);
132 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device
*rdev
,
133 bool driver_initiated
)
135 struct net_device
*dev
;
137 lockdep_assert_held(&rdev
->sched_scan_mtx
);
139 if (!rdev
->sched_scan_req
)
142 dev
= rdev
->sched_scan_req
->dev
;
144 if (!driver_initiated
) {
145 int err
= rdev
->ops
->sched_scan_stop(&rdev
->wiphy
, dev
);
150 nl80211_send_sched_scan(rdev
, dev
, NL80211_CMD_SCHED_SCAN_STOPPED
);
152 kfree(rdev
->sched_scan_req
);
153 rdev
->sched_scan_req
= NULL
;
158 static void bss_release(struct kref
*ref
)
160 struct cfg80211_internal_bss
*bss
;
162 bss
= container_of(ref
, struct cfg80211_internal_bss
, ref
);
163 if (bss
->pub
.free_priv
)
164 bss
->pub
.free_priv(&bss
->pub
);
166 if (bss
->beacon_ies_allocated
)
167 kfree(bss
->pub
.beacon_ies
);
168 if (bss
->proberesp_ies_allocated
)
169 kfree(bss
->pub
.proberesp_ies
);
171 BUG_ON(atomic_read(&bss
->hold
));
176 /* must hold dev->bss_lock! */
177 void cfg80211_bss_age(struct cfg80211_registered_device
*dev
,
178 unsigned long age_secs
)
180 struct cfg80211_internal_bss
*bss
;
181 unsigned long age_jiffies
= msecs_to_jiffies(age_secs
* MSEC_PER_SEC
);
183 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
184 bss
->ts
-= age_jiffies
;
188 /* must hold dev->bss_lock! */
189 static void __cfg80211_unlink_bss(struct cfg80211_registered_device
*dev
,
190 struct cfg80211_internal_bss
*bss
)
192 list_del_init(&bss
->list
);
193 rb_erase(&bss
->rbn
, &dev
->bss_tree
);
194 kref_put(&bss
->ref
, bss_release
);
197 /* must hold dev->bss_lock! */
198 void cfg80211_bss_expire(struct cfg80211_registered_device
*dev
)
200 struct cfg80211_internal_bss
*bss
, *tmp
;
201 bool expired
= false;
203 list_for_each_entry_safe(bss
, tmp
, &dev
->bss_list
, list
) {
204 if (atomic_read(&bss
->hold
))
206 if (!time_after(jiffies
, bss
->ts
+ IEEE80211_SCAN_RESULT_EXPIRE
))
208 __cfg80211_unlink_bss(dev
, bss
);
213 dev
->bss_generation
++;
216 const u8
*cfg80211_find_ie(u8 eid
, const u8
*ies
, int len
)
218 while (len
> 2 && ies
[0] != eid
) {
224 if (len
< 2 + ies
[1])
228 EXPORT_SYMBOL(cfg80211_find_ie
);
230 static int cmp_ies(u8 num
, u8
*ies1
, size_t len1
, u8
*ies2
, size_t len2
)
232 const u8
*ie1
= cfg80211_find_ie(num
, ies1
, len1
);
233 const u8
*ie2
= cfg80211_find_ie(num
, ies2
, len2
);
241 r
= memcmp(ie1
+ 2, ie2
+ 2, min(ie1
[1], ie2
[1]));
242 if (r
== 0 && ie1
[1] != ie2
[1])
243 return ie2
[1] - ie1
[1];
247 static bool is_bss(struct cfg80211_bss
*a
,
249 const u8
*ssid
, size_t ssid_len
)
253 if (bssid
&& compare_ether_addr(a
->bssid
, bssid
))
259 ssidie
= cfg80211_find_ie(WLAN_EID_SSID
,
260 a
->information_elements
,
261 a
->len_information_elements
);
264 if (ssidie
[1] != ssid_len
)
266 return memcmp(ssidie
+ 2, ssid
, ssid_len
) == 0;
269 static bool is_mesh_bss(struct cfg80211_bss
*a
)
273 if (!WLAN_CAPABILITY_IS_STA_BSS(a
->capability
))
276 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
,
277 a
->information_elements
,
278 a
->len_information_elements
);
282 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
,
283 a
->information_elements
,
284 a
->len_information_elements
);
291 static bool is_mesh(struct cfg80211_bss
*a
,
292 const u8
*meshid
, size_t meshidlen
,
297 if (!WLAN_CAPABILITY_IS_STA_BSS(a
->capability
))
300 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
,
301 a
->information_elements
,
302 a
->len_information_elements
);
305 if (ie
[1] != meshidlen
)
307 if (memcmp(ie
+ 2, meshid
, meshidlen
))
310 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
,
311 a
->information_elements
,
312 a
->len_information_elements
);
315 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
319 * Ignore mesh capability (last two bytes of the IE) when
320 * comparing since that may differ between stations taking
321 * part in the same mesh.
323 return memcmp(ie
+ 2, meshcfg
,
324 sizeof(struct ieee80211_meshconf_ie
) - 2) == 0;
327 static int cmp_bss(struct cfg80211_bss
*a
,
328 struct cfg80211_bss
*b
)
332 if (a
->channel
!= b
->channel
)
333 return b
->channel
->center_freq
- a
->channel
->center_freq
;
335 if (is_mesh_bss(a
) && is_mesh_bss(b
)) {
336 r
= cmp_ies(WLAN_EID_MESH_ID
,
337 a
->information_elements
,
338 a
->len_information_elements
,
339 b
->information_elements
,
340 b
->len_information_elements
);
343 return cmp_ies(WLAN_EID_MESH_CONFIG
,
344 a
->information_elements
,
345 a
->len_information_elements
,
346 b
->information_elements
,
347 b
->len_information_elements
);
350 r
= memcmp(a
->bssid
, b
->bssid
, ETH_ALEN
);
354 return cmp_ies(WLAN_EID_SSID
,
355 a
->information_elements
,
356 a
->len_information_elements
,
357 b
->information_elements
,
358 b
->len_information_elements
);
361 struct cfg80211_bss
*cfg80211_get_bss(struct wiphy
*wiphy
,
362 struct ieee80211_channel
*channel
,
364 const u8
*ssid
, size_t ssid_len
,
365 u16 capa_mask
, u16 capa_val
)
367 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
368 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
369 unsigned long now
= jiffies
;
371 spin_lock_bh(&dev
->bss_lock
);
373 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
374 if ((bss
->pub
.capability
& capa_mask
) != capa_val
)
376 if (channel
&& bss
->pub
.channel
!= channel
)
378 /* Don't get expired BSS structs */
379 if (time_after(now
, bss
->ts
+ IEEE80211_SCAN_RESULT_EXPIRE
) &&
380 !atomic_read(&bss
->hold
))
382 if (is_bss(&bss
->pub
, bssid
, ssid
, ssid_len
)) {
389 spin_unlock_bh(&dev
->bss_lock
);
394 EXPORT_SYMBOL(cfg80211_get_bss
);
396 struct cfg80211_bss
*cfg80211_get_mesh(struct wiphy
*wiphy
,
397 struct ieee80211_channel
*channel
,
398 const u8
*meshid
, size_t meshidlen
,
401 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
402 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
404 spin_lock_bh(&dev
->bss_lock
);
406 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
407 if (channel
&& bss
->pub
.channel
!= channel
)
409 if (is_mesh(&bss
->pub
, meshid
, meshidlen
, meshcfg
)) {
416 spin_unlock_bh(&dev
->bss_lock
);
421 EXPORT_SYMBOL(cfg80211_get_mesh
);
424 static void rb_insert_bss(struct cfg80211_registered_device
*dev
,
425 struct cfg80211_internal_bss
*bss
)
427 struct rb_node
**p
= &dev
->bss_tree
.rb_node
;
428 struct rb_node
*parent
= NULL
;
429 struct cfg80211_internal_bss
*tbss
;
434 tbss
= rb_entry(parent
, struct cfg80211_internal_bss
, rbn
);
436 cmp
= cmp_bss(&bss
->pub
, &tbss
->pub
);
439 /* will sort of leak this BSS */
449 rb_link_node(&bss
->rbn
, parent
, p
);
450 rb_insert_color(&bss
->rbn
, &dev
->bss_tree
);
453 static struct cfg80211_internal_bss
*
454 rb_find_bss(struct cfg80211_registered_device
*dev
,
455 struct cfg80211_internal_bss
*res
)
457 struct rb_node
*n
= dev
->bss_tree
.rb_node
;
458 struct cfg80211_internal_bss
*bss
;
462 bss
= rb_entry(n
, struct cfg80211_internal_bss
, rbn
);
463 r
= cmp_bss(&res
->pub
, &bss
->pub
);
476 static struct cfg80211_internal_bss
*
477 cfg80211_bss_update(struct cfg80211_registered_device
*dev
,
478 struct cfg80211_internal_bss
*res
)
480 struct cfg80211_internal_bss
*found
= NULL
;
483 * The reference to "res" is donated to this function.
486 if (WARN_ON(!res
->pub
.channel
)) {
487 kref_put(&res
->ref
, bss_release
);
493 spin_lock_bh(&dev
->bss_lock
);
495 found
= rb_find_bss(dev
, res
);
498 found
->pub
.beacon_interval
= res
->pub
.beacon_interval
;
499 found
->pub
.tsf
= res
->pub
.tsf
;
500 found
->pub
.signal
= res
->pub
.signal
;
501 found
->pub
.capability
= res
->pub
.capability
;
505 if (res
->pub
.proberesp_ies
) {
506 size_t used
= dev
->wiphy
.bss_priv_size
+ sizeof(*res
);
507 size_t ielen
= res
->pub
.len_proberesp_ies
;
509 if (found
->pub
.proberesp_ies
&&
510 !found
->proberesp_ies_allocated
&&
511 ksize(found
) >= used
+ ielen
) {
512 memcpy(found
->pub
.proberesp_ies
,
513 res
->pub
.proberesp_ies
, ielen
);
514 found
->pub
.len_proberesp_ies
= ielen
;
516 u8
*ies
= found
->pub
.proberesp_ies
;
518 if (found
->proberesp_ies_allocated
)
519 ies
= krealloc(ies
, ielen
, GFP_ATOMIC
);
521 ies
= kmalloc(ielen
, GFP_ATOMIC
);
524 memcpy(ies
, res
->pub
.proberesp_ies
,
526 found
->proberesp_ies_allocated
= true;
527 found
->pub
.proberesp_ies
= ies
;
528 found
->pub
.len_proberesp_ies
= ielen
;
532 /* Override possible earlier Beacon frame IEs */
533 found
->pub
.information_elements
=
534 found
->pub
.proberesp_ies
;
535 found
->pub
.len_information_elements
=
536 found
->pub
.len_proberesp_ies
;
538 if (res
->pub
.beacon_ies
) {
539 size_t used
= dev
->wiphy
.bss_priv_size
+ sizeof(*res
);
540 size_t ielen
= res
->pub
.len_beacon_ies
;
541 bool information_elements_is_beacon_ies
=
542 (found
->pub
.information_elements
==
543 found
->pub
.beacon_ies
);
545 if (found
->pub
.beacon_ies
&&
546 !found
->beacon_ies_allocated
&&
547 ksize(found
) >= used
+ ielen
) {
548 memcpy(found
->pub
.beacon_ies
,
549 res
->pub
.beacon_ies
, ielen
);
550 found
->pub
.len_beacon_ies
= ielen
;
552 u8
*ies
= found
->pub
.beacon_ies
;
554 if (found
->beacon_ies_allocated
)
555 ies
= krealloc(ies
, ielen
, GFP_ATOMIC
);
557 ies
= kmalloc(ielen
, GFP_ATOMIC
);
560 memcpy(ies
, res
->pub
.beacon_ies
,
562 found
->beacon_ies_allocated
= true;
563 found
->pub
.beacon_ies
= ies
;
564 found
->pub
.len_beacon_ies
= ielen
;
568 /* Override IEs if they were from a beacon before */
569 if (information_elements_is_beacon_ies
) {
570 found
->pub
.information_elements
=
571 found
->pub
.beacon_ies
;
572 found
->pub
.len_information_elements
=
573 found
->pub
.len_beacon_ies
;
577 kref_put(&res
->ref
, bss_release
);
579 /* this "consumes" the reference */
580 list_add_tail(&res
->list
, &dev
->bss_list
);
581 rb_insert_bss(dev
, res
);
585 dev
->bss_generation
++;
586 spin_unlock_bh(&dev
->bss_lock
);
588 kref_get(&found
->ref
);
593 cfg80211_inform_bss(struct wiphy
*wiphy
,
594 struct ieee80211_channel
*channel
,
596 u64 timestamp
, u16 capability
, u16 beacon_interval
,
597 const u8
*ie
, size_t ielen
,
598 s32 signal
, gfp_t gfp
)
600 struct cfg80211_internal_bss
*res
;
606 privsz
= wiphy
->bss_priv_size
;
608 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
609 (signal
< 0 || signal
> 100)))
612 res
= kzalloc(sizeof(*res
) + privsz
+ ielen
, gfp
);
616 memcpy(res
->pub
.bssid
, bssid
, ETH_ALEN
);
617 res
->pub
.channel
= channel
;
618 res
->pub
.signal
= signal
;
619 res
->pub
.tsf
= timestamp
;
620 res
->pub
.beacon_interval
= beacon_interval
;
621 res
->pub
.capability
= capability
;
623 * Since we do not know here whether the IEs are from a Beacon or Probe
624 * Response frame, we need to pick one of the options and only use it
625 * with the driver that does not provide the full Beacon/Probe Response
626 * frame. Use Beacon frame pointer to avoid indicating that this should
627 * override the information_elements pointer should we have received an
628 * earlier indication of Probe Response data.
630 * The initial buffer for the IEs is allocated with the BSS entry and
631 * is located after the private area.
633 res
->pub
.beacon_ies
= (u8
*)res
+ sizeof(*res
) + privsz
;
634 memcpy(res
->pub
.beacon_ies
, ie
, ielen
);
635 res
->pub
.len_beacon_ies
= ielen
;
636 res
->pub
.information_elements
= res
->pub
.beacon_ies
;
637 res
->pub
.len_information_elements
= res
->pub
.len_beacon_ies
;
639 kref_init(&res
->ref
);
641 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), res
);
645 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
646 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
648 /* cfg80211_bss_update gives us a referenced result */
651 EXPORT_SYMBOL(cfg80211_inform_bss
);
653 struct cfg80211_bss
*
654 cfg80211_inform_bss_frame(struct wiphy
*wiphy
,
655 struct ieee80211_channel
*channel
,
656 struct ieee80211_mgmt
*mgmt
, size_t len
,
657 s32 signal
, gfp_t gfp
)
659 struct cfg80211_internal_bss
*res
;
660 size_t ielen
= len
- offsetof(struct ieee80211_mgmt
,
661 u
.probe_resp
.variable
);
670 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
671 (signal
< 0 || signal
> 100)))
674 if (WARN_ON(len
< offsetof(struct ieee80211_mgmt
, u
.probe_resp
.variable
)))
677 privsz
= wiphy
->bss_priv_size
;
679 res
= kzalloc(sizeof(*res
) + privsz
+ ielen
, gfp
);
683 memcpy(res
->pub
.bssid
, mgmt
->bssid
, ETH_ALEN
);
684 res
->pub
.channel
= channel
;
685 res
->pub
.signal
= signal
;
686 res
->pub
.tsf
= le64_to_cpu(mgmt
->u
.probe_resp
.timestamp
);
687 res
->pub
.beacon_interval
= le16_to_cpu(mgmt
->u
.probe_resp
.beacon_int
);
688 res
->pub
.capability
= le16_to_cpu(mgmt
->u
.probe_resp
.capab_info
);
690 * The initial buffer for the IEs is allocated with the BSS entry and
691 * is located after the private area.
693 if (ieee80211_is_probe_resp(mgmt
->frame_control
)) {
694 res
->pub
.proberesp_ies
= (u8
*) res
+ sizeof(*res
) + privsz
;
695 memcpy(res
->pub
.proberesp_ies
, mgmt
->u
.probe_resp
.variable
,
697 res
->pub
.len_proberesp_ies
= ielen
;
698 res
->pub
.information_elements
= res
->pub
.proberesp_ies
;
699 res
->pub
.len_information_elements
= res
->pub
.len_proberesp_ies
;
701 res
->pub
.beacon_ies
= (u8
*) res
+ sizeof(*res
) + privsz
;
702 memcpy(res
->pub
.beacon_ies
, mgmt
->u
.beacon
.variable
, ielen
);
703 res
->pub
.len_beacon_ies
= ielen
;
704 res
->pub
.information_elements
= res
->pub
.beacon_ies
;
705 res
->pub
.len_information_elements
= res
->pub
.len_beacon_ies
;
708 kref_init(&res
->ref
);
710 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), res
);
714 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
715 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
717 /* cfg80211_bss_update gives us a referenced result */
720 EXPORT_SYMBOL(cfg80211_inform_bss_frame
);
722 void cfg80211_put_bss(struct cfg80211_bss
*pub
)
724 struct cfg80211_internal_bss
*bss
;
729 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
730 kref_put(&bss
->ref
, bss_release
);
732 EXPORT_SYMBOL(cfg80211_put_bss
);
734 void cfg80211_unlink_bss(struct wiphy
*wiphy
, struct cfg80211_bss
*pub
)
736 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
737 struct cfg80211_internal_bss
*bss
;
742 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
744 spin_lock_bh(&dev
->bss_lock
);
745 if (!list_empty(&bss
->list
)) {
746 __cfg80211_unlink_bss(dev
, bss
);
747 dev
->bss_generation
++;
749 spin_unlock_bh(&dev
->bss_lock
);
751 EXPORT_SYMBOL(cfg80211_unlink_bss
);
753 #ifdef CONFIG_CFG80211_WEXT
754 int cfg80211_wext_siwscan(struct net_device
*dev
,
755 struct iw_request_info
*info
,
756 union iwreq_data
*wrqu
, char *extra
)
758 struct cfg80211_registered_device
*rdev
;
760 struct iw_scan_req
*wreq
= NULL
;
761 struct cfg80211_scan_request
*creq
= NULL
;
762 int i
, err
, n_channels
= 0;
763 enum ieee80211_band band
;
765 if (!netif_running(dev
))
768 if (wrqu
->data
.length
== sizeof(struct iw_scan_req
))
769 wreq
= (struct iw_scan_req
*)extra
;
771 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
774 return PTR_ERR(rdev
);
776 if (rdev
->scan_req
) {
781 wiphy
= &rdev
->wiphy
;
783 /* Determine number of channels, needed to allocate creq */
784 if (wreq
&& wreq
->num_channels
)
785 n_channels
= wreq
->num_channels
;
787 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
788 if (wiphy
->bands
[band
])
789 n_channels
+= wiphy
->bands
[band
]->n_channels
;
792 creq
= kzalloc(sizeof(*creq
) + sizeof(struct cfg80211_ssid
) +
793 n_channels
* sizeof(void *),
802 /* SSIDs come after channels */
803 creq
->ssids
= (void *)&creq
->channels
[n_channels
];
804 creq
->n_channels
= n_channels
;
807 /* translate "Scan on frequencies" request */
809 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
812 if (!wiphy
->bands
[band
])
815 for (j
= 0; j
< wiphy
->bands
[band
]->n_channels
; j
++) {
816 /* ignore disabled channels */
817 if (wiphy
->bands
[band
]->channels
[j
].flags
&
818 IEEE80211_CHAN_DISABLED
)
821 /* If we have a wireless request structure and the
822 * wireless request specifies frequencies, then search
823 * for the matching hardware channel.
825 if (wreq
&& wreq
->num_channels
) {
827 int wiphy_freq
= wiphy
->bands
[band
]->channels
[j
].center_freq
;
828 for (k
= 0; k
< wreq
->num_channels
; k
++) {
829 int wext_freq
= cfg80211_wext_freq(wiphy
, &wreq
->channel_list
[k
]);
830 if (wext_freq
== wiphy_freq
)
831 goto wext_freq_found
;
833 goto wext_freq_not_found
;
837 creq
->channels
[i
] = &wiphy
->bands
[band
]->channels
[j
];
839 wext_freq_not_found
: ;
842 /* No channels found? */
848 /* Set real number of channels specified in creq->channels[] */
849 creq
->n_channels
= i
;
851 /* translate "Scan for SSID" request */
853 if (wrqu
->data
.flags
& IW_SCAN_THIS_ESSID
) {
854 if (wreq
->essid_len
> IEEE80211_MAX_SSID_LEN
) {
858 memcpy(creq
->ssids
[0].ssid
, wreq
->essid
, wreq
->essid_len
);
859 creq
->ssids
[0].ssid_len
= wreq
->essid_len
;
861 if (wreq
->scan_type
== IW_SCAN_TYPE_PASSIVE
)
865 for (i
= 0; i
< IEEE80211_NUM_BANDS
; i
++)
867 creq
->rates
[i
] = (1 << wiphy
->bands
[i
]->n_bitrates
) - 1;
869 rdev
->scan_req
= creq
;
870 err
= rdev
->ops
->scan(wiphy
, dev
, creq
);
872 rdev
->scan_req
= NULL
;
873 /* creq will be freed below */
875 nl80211_send_scan_start(rdev
, dev
);
876 /* creq now owned by driver */
882 cfg80211_unlock_rdev(rdev
);
885 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan
);
887 static void ieee80211_scan_add_ies(struct iw_request_info
*info
,
888 struct cfg80211_bss
*bss
,
889 char **current_ev
, char *end_buf
)
891 u8
*pos
, *end
, *next
;
894 if (!bss
->information_elements
||
895 !bss
->len_information_elements
)
899 * If needed, fragment the IEs buffer (at IE boundaries) into short
900 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
902 pos
= bss
->information_elements
;
903 end
= pos
+ bss
->len_information_elements
;
905 while (end
- pos
> IW_GENERIC_IE_MAX
) {
906 next
= pos
+ 2 + pos
[1];
907 while (next
+ 2 + next
[1] - pos
< IW_GENERIC_IE_MAX
)
908 next
= next
+ 2 + next
[1];
910 memset(&iwe
, 0, sizeof(iwe
));
912 iwe
.u
.data
.length
= next
- pos
;
913 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
920 memset(&iwe
, 0, sizeof(iwe
));
922 iwe
.u
.data
.length
= end
- pos
;
923 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
928 static inline unsigned int elapsed_jiffies_msecs(unsigned long start
)
930 unsigned long end
= jiffies
;
933 return jiffies_to_msecs(end
- start
);
935 return jiffies_to_msecs(end
+ (MAX_JIFFY_OFFSET
- start
) + 1);
939 ieee80211_bss(struct wiphy
*wiphy
, struct iw_request_info
*info
,
940 struct cfg80211_internal_bss
*bss
, char *current_ev
,
945 u8
*ie
= bss
->pub
.information_elements
;
946 int rem
= bss
->pub
.len_information_elements
, i
, sig
;
949 memset(&iwe
, 0, sizeof(iwe
));
951 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
952 memcpy(iwe
.u
.ap_addr
.sa_data
, bss
->pub
.bssid
, ETH_ALEN
);
953 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
956 memset(&iwe
, 0, sizeof(iwe
));
957 iwe
.cmd
= SIOCGIWFREQ
;
958 iwe
.u
.freq
.m
= ieee80211_frequency_to_channel(bss
->pub
.channel
->center_freq
);
960 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
963 memset(&iwe
, 0, sizeof(iwe
));
964 iwe
.cmd
= SIOCGIWFREQ
;
965 iwe
.u
.freq
.m
= bss
->pub
.channel
->center_freq
;
967 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
970 if (wiphy
->signal_type
!= CFG80211_SIGNAL_TYPE_NONE
) {
971 memset(&iwe
, 0, sizeof(iwe
));
973 iwe
.u
.qual
.updated
= IW_QUAL_LEVEL_UPDATED
|
974 IW_QUAL_NOISE_INVALID
|
975 IW_QUAL_QUAL_UPDATED
;
976 switch (wiphy
->signal_type
) {
977 case CFG80211_SIGNAL_TYPE_MBM
:
978 sig
= bss
->pub
.signal
/ 100;
979 iwe
.u
.qual
.level
= sig
;
980 iwe
.u
.qual
.updated
|= IW_QUAL_DBM
;
981 if (sig
< -110) /* rather bad */
983 else if (sig
> -40) /* perfect */
985 /* will give a range of 0 .. 70 */
986 iwe
.u
.qual
.qual
= sig
+ 110;
988 case CFG80211_SIGNAL_TYPE_UNSPEC
:
989 iwe
.u
.qual
.level
= bss
->pub
.signal
;
990 /* will give range 0 .. 100 */
991 iwe
.u
.qual
.qual
= bss
->pub
.signal
;
997 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
998 &iwe
, IW_EV_QUAL_LEN
);
1001 memset(&iwe
, 0, sizeof(iwe
));
1002 iwe
.cmd
= SIOCGIWENCODE
;
1003 if (bss
->pub
.capability
& WLAN_CAPABILITY_PRIVACY
)
1004 iwe
.u
.data
.flags
= IW_ENCODE_ENABLED
| IW_ENCODE_NOKEY
;
1006 iwe
.u
.data
.flags
= IW_ENCODE_DISABLED
;
1007 iwe
.u
.data
.length
= 0;
1008 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1013 if (ie
[1] > rem
- 2)
1018 memset(&iwe
, 0, sizeof(iwe
));
1019 iwe
.cmd
= SIOCGIWESSID
;
1020 iwe
.u
.data
.length
= ie
[1];
1021 iwe
.u
.data
.flags
= 1;
1022 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1025 case WLAN_EID_MESH_ID
:
1026 memset(&iwe
, 0, sizeof(iwe
));
1027 iwe
.cmd
= SIOCGIWESSID
;
1028 iwe
.u
.data
.length
= ie
[1];
1029 iwe
.u
.data
.flags
= 1;
1030 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1033 case WLAN_EID_MESH_CONFIG
:
1035 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
1037 buf
= kmalloc(50, GFP_ATOMIC
);
1041 memset(&iwe
, 0, sizeof(iwe
));
1042 iwe
.cmd
= IWEVCUSTOM
;
1043 sprintf(buf
, "Mesh Network Path Selection Protocol ID: "
1045 iwe
.u
.data
.length
= strlen(buf
);
1046 current_ev
= iwe_stream_add_point(info
, current_ev
,
1049 sprintf(buf
, "Path Selection Metric ID: 0x%02X",
1051 iwe
.u
.data
.length
= strlen(buf
);
1052 current_ev
= iwe_stream_add_point(info
, current_ev
,
1055 sprintf(buf
, "Congestion Control Mode ID: 0x%02X",
1057 iwe
.u
.data
.length
= strlen(buf
);
1058 current_ev
= iwe_stream_add_point(info
, current_ev
,
1061 sprintf(buf
, "Synchronization ID: 0x%02X", cfg
[3]);
1062 iwe
.u
.data
.length
= strlen(buf
);
1063 current_ev
= iwe_stream_add_point(info
, current_ev
,
1066 sprintf(buf
, "Authentication ID: 0x%02X", cfg
[4]);
1067 iwe
.u
.data
.length
= strlen(buf
);
1068 current_ev
= iwe_stream_add_point(info
, current_ev
,
1071 sprintf(buf
, "Formation Info: 0x%02X", cfg
[5]);
1072 iwe
.u
.data
.length
= strlen(buf
);
1073 current_ev
= iwe_stream_add_point(info
, current_ev
,
1076 sprintf(buf
, "Capabilities: 0x%02X", cfg
[6]);
1077 iwe
.u
.data
.length
= strlen(buf
);
1078 current_ev
= iwe_stream_add_point(info
, current_ev
,
1083 case WLAN_EID_SUPP_RATES
:
1084 case WLAN_EID_EXT_SUPP_RATES
:
1085 /* display all supported rates in readable format */
1086 p
= current_ev
+ iwe_stream_lcp_len(info
);
1088 memset(&iwe
, 0, sizeof(iwe
));
1089 iwe
.cmd
= SIOCGIWRATE
;
1090 /* Those two flags are ignored... */
1091 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
1093 for (i
= 0; i
< ie
[1]; i
++) {
1094 iwe
.u
.bitrate
.value
=
1095 ((ie
[i
+ 2] & 0x7f) * 500000);
1096 p
= iwe_stream_add_value(info
, current_ev
, p
,
1097 end_buf
, &iwe
, IW_EV_PARAM_LEN
);
1106 if (bss
->pub
.capability
& (WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_IBSS
) ||
1108 memset(&iwe
, 0, sizeof(iwe
));
1109 iwe
.cmd
= SIOCGIWMODE
;
1111 iwe
.u
.mode
= IW_MODE_MESH
;
1112 else if (bss
->pub
.capability
& WLAN_CAPABILITY_ESS
)
1113 iwe
.u
.mode
= IW_MODE_MASTER
;
1115 iwe
.u
.mode
= IW_MODE_ADHOC
;
1116 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
1117 &iwe
, IW_EV_UINT_LEN
);
1120 buf
= kmalloc(30, GFP_ATOMIC
);
1122 memset(&iwe
, 0, sizeof(iwe
));
1123 iwe
.cmd
= IWEVCUSTOM
;
1124 sprintf(buf
, "tsf=%016llx", (unsigned long long)(bss
->pub
.tsf
));
1125 iwe
.u
.data
.length
= strlen(buf
);
1126 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1128 memset(&iwe
, 0, sizeof(iwe
));
1129 iwe
.cmd
= IWEVCUSTOM
;
1130 sprintf(buf
, " Last beacon: %ums ago",
1131 elapsed_jiffies_msecs(bss
->ts
));
1132 iwe
.u
.data
.length
= strlen(buf
);
1133 current_ev
= iwe_stream_add_point(info
, current_ev
,
1134 end_buf
, &iwe
, buf
);
1138 ieee80211_scan_add_ies(info
, &bss
->pub
, ¤t_ev
, end_buf
);
1144 static int ieee80211_scan_results(struct cfg80211_registered_device
*dev
,
1145 struct iw_request_info
*info
,
1146 char *buf
, size_t len
)
1148 char *current_ev
= buf
;
1149 char *end_buf
= buf
+ len
;
1150 struct cfg80211_internal_bss
*bss
;
1152 spin_lock_bh(&dev
->bss_lock
);
1153 cfg80211_bss_expire(dev
);
1155 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
1156 if (buf
+ len
- current_ev
<= IW_EV_ADDR_LEN
) {
1157 spin_unlock_bh(&dev
->bss_lock
);
1160 current_ev
= ieee80211_bss(&dev
->wiphy
, info
, bss
,
1161 current_ev
, end_buf
);
1163 spin_unlock_bh(&dev
->bss_lock
);
1164 return current_ev
- buf
;
1168 int cfg80211_wext_giwscan(struct net_device
*dev
,
1169 struct iw_request_info
*info
,
1170 struct iw_point
*data
, char *extra
)
1172 struct cfg80211_registered_device
*rdev
;
1175 if (!netif_running(dev
))
1178 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
1181 return PTR_ERR(rdev
);
1183 if (rdev
->scan_req
) {
1188 res
= ieee80211_scan_results(rdev
, info
, extra
, data
->length
);
1196 cfg80211_unlock_rdev(rdev
);
1199 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan
);