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 static void bss_release(struct kref
*ref
)
98 struct cfg80211_internal_bss
*bss
;
100 bss
= container_of(ref
, struct cfg80211_internal_bss
, ref
);
101 if (bss
->pub
.free_priv
)
102 bss
->pub
.free_priv(&bss
->pub
);
104 if (bss
->beacon_ies_allocated
)
105 kfree(bss
->pub
.beacon_ies
);
106 if (bss
->proberesp_ies_allocated
)
107 kfree(bss
->pub
.proberesp_ies
);
109 BUG_ON(atomic_read(&bss
->hold
));
114 /* must hold dev->bss_lock! */
115 void cfg80211_bss_age(struct cfg80211_registered_device
*dev
,
116 unsigned long age_secs
)
118 struct cfg80211_internal_bss
*bss
;
119 unsigned long age_jiffies
= msecs_to_jiffies(age_secs
* MSEC_PER_SEC
);
121 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
122 bss
->ts
-= age_jiffies
;
126 /* must hold dev->bss_lock! */
127 static void __cfg80211_unlink_bss(struct cfg80211_registered_device
*dev
,
128 struct cfg80211_internal_bss
*bss
)
130 list_del_init(&bss
->list
);
131 rb_erase(&bss
->rbn
, &dev
->bss_tree
);
132 kref_put(&bss
->ref
, bss_release
);
135 /* must hold dev->bss_lock! */
136 void cfg80211_bss_expire(struct cfg80211_registered_device
*dev
)
138 struct cfg80211_internal_bss
*bss
, *tmp
;
139 bool expired
= false;
141 list_for_each_entry_safe(bss
, tmp
, &dev
->bss_list
, list
) {
142 if (atomic_read(&bss
->hold
))
144 if (!time_after(jiffies
, bss
->ts
+ IEEE80211_SCAN_RESULT_EXPIRE
))
146 __cfg80211_unlink_bss(dev
, bss
);
151 dev
->bss_generation
++;
154 const u8
*cfg80211_find_ie(u8 eid
, const u8
*ies
, int len
)
156 while (len
> 2 && ies
[0] != eid
) {
162 if (len
< 2 + ies
[1])
166 EXPORT_SYMBOL(cfg80211_find_ie
);
168 static int cmp_ies(u8 num
, u8
*ies1
, size_t len1
, u8
*ies2
, size_t len2
)
170 const u8
*ie1
= cfg80211_find_ie(num
, ies1
, len1
);
171 const u8
*ie2
= cfg80211_find_ie(num
, ies2
, len2
);
179 r
= memcmp(ie1
+ 2, ie2
+ 2, min(ie1
[1], ie2
[1]));
180 if (r
== 0 && ie1
[1] != ie2
[1])
181 return ie2
[1] - ie1
[1];
185 static bool is_bss(struct cfg80211_bss
*a
,
187 const u8
*ssid
, size_t ssid_len
)
191 if (bssid
&& compare_ether_addr(a
->bssid
, bssid
))
197 ssidie
= cfg80211_find_ie(WLAN_EID_SSID
,
198 a
->information_elements
,
199 a
->len_information_elements
);
202 if (ssidie
[1] != ssid_len
)
204 return memcmp(ssidie
+ 2, ssid
, ssid_len
) == 0;
207 static bool is_mesh(struct cfg80211_bss
*a
,
208 const u8
*meshid
, size_t meshidlen
,
213 if (!is_zero_ether_addr(a
->bssid
))
216 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
,
217 a
->information_elements
,
218 a
->len_information_elements
);
221 if (ie
[1] != meshidlen
)
223 if (memcmp(ie
+ 2, meshid
, meshidlen
))
226 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
,
227 a
->information_elements
,
228 a
->len_information_elements
);
231 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
235 * Ignore mesh capability (last two bytes of the IE) when
236 * comparing since that may differ between stations taking
237 * part in the same mesh.
239 return memcmp(ie
+ 2, meshcfg
,
240 sizeof(struct ieee80211_meshconf_ie
) - 2) == 0;
243 static int cmp_bss(struct cfg80211_bss
*a
,
244 struct cfg80211_bss
*b
)
248 if (a
->channel
!= b
->channel
)
249 return b
->channel
->center_freq
- a
->channel
->center_freq
;
251 r
= memcmp(a
->bssid
, b
->bssid
, ETH_ALEN
);
255 if (is_zero_ether_addr(a
->bssid
)) {
256 r
= cmp_ies(WLAN_EID_MESH_ID
,
257 a
->information_elements
,
258 a
->len_information_elements
,
259 b
->information_elements
,
260 b
->len_information_elements
);
263 return cmp_ies(WLAN_EID_MESH_CONFIG
,
264 a
->information_elements
,
265 a
->len_information_elements
,
266 b
->information_elements
,
267 b
->len_information_elements
);
270 return cmp_ies(WLAN_EID_SSID
,
271 a
->information_elements
,
272 a
->len_information_elements
,
273 b
->information_elements
,
274 b
->len_information_elements
);
277 struct cfg80211_bss
*cfg80211_get_bss(struct wiphy
*wiphy
,
278 struct ieee80211_channel
*channel
,
280 const u8
*ssid
, size_t ssid_len
,
281 u16 capa_mask
, u16 capa_val
)
283 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
284 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
285 unsigned long now
= jiffies
;
287 spin_lock_bh(&dev
->bss_lock
);
289 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
290 if ((bss
->pub
.capability
& capa_mask
) != capa_val
)
292 if (channel
&& bss
->pub
.channel
!= channel
)
294 /* Don't get expired BSS structs */
295 if (time_after(now
, bss
->ts
+ IEEE80211_SCAN_RESULT_EXPIRE
) &&
296 !atomic_read(&bss
->hold
))
298 if (is_bss(&bss
->pub
, bssid
, ssid
, ssid_len
)) {
305 spin_unlock_bh(&dev
->bss_lock
);
310 EXPORT_SYMBOL(cfg80211_get_bss
);
312 struct cfg80211_bss
*cfg80211_get_mesh(struct wiphy
*wiphy
,
313 struct ieee80211_channel
*channel
,
314 const u8
*meshid
, size_t meshidlen
,
317 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
318 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
320 spin_lock_bh(&dev
->bss_lock
);
322 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
323 if (channel
&& bss
->pub
.channel
!= channel
)
325 if (is_mesh(&bss
->pub
, meshid
, meshidlen
, meshcfg
)) {
332 spin_unlock_bh(&dev
->bss_lock
);
337 EXPORT_SYMBOL(cfg80211_get_mesh
);
340 static void rb_insert_bss(struct cfg80211_registered_device
*dev
,
341 struct cfg80211_internal_bss
*bss
)
343 struct rb_node
**p
= &dev
->bss_tree
.rb_node
;
344 struct rb_node
*parent
= NULL
;
345 struct cfg80211_internal_bss
*tbss
;
350 tbss
= rb_entry(parent
, struct cfg80211_internal_bss
, rbn
);
352 cmp
= cmp_bss(&bss
->pub
, &tbss
->pub
);
355 /* will sort of leak this BSS */
365 rb_link_node(&bss
->rbn
, parent
, p
);
366 rb_insert_color(&bss
->rbn
, &dev
->bss_tree
);
369 static struct cfg80211_internal_bss
*
370 rb_find_bss(struct cfg80211_registered_device
*dev
,
371 struct cfg80211_internal_bss
*res
)
373 struct rb_node
*n
= dev
->bss_tree
.rb_node
;
374 struct cfg80211_internal_bss
*bss
;
378 bss
= rb_entry(n
, struct cfg80211_internal_bss
, rbn
);
379 r
= cmp_bss(&res
->pub
, &bss
->pub
);
392 static struct cfg80211_internal_bss
*
393 cfg80211_bss_update(struct cfg80211_registered_device
*dev
,
394 struct cfg80211_internal_bss
*res
)
396 struct cfg80211_internal_bss
*found
= NULL
;
397 const u8
*meshid
, *meshcfg
;
400 * The reference to "res" is donated to this function.
403 if (WARN_ON(!res
->pub
.channel
)) {
404 kref_put(&res
->ref
, bss_release
);
410 if (is_zero_ether_addr(res
->pub
.bssid
)) {
411 /* must be mesh, verify */
412 meshid
= cfg80211_find_ie(WLAN_EID_MESH_ID
,
413 res
->pub
.information_elements
,
414 res
->pub
.len_information_elements
);
415 meshcfg
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
,
416 res
->pub
.information_elements
,
417 res
->pub
.len_information_elements
);
418 if (!meshid
|| !meshcfg
||
419 meshcfg
[1] != sizeof(struct ieee80211_meshconf_ie
)) {
421 kref_put(&res
->ref
, bss_release
);
426 spin_lock_bh(&dev
->bss_lock
);
428 found
= rb_find_bss(dev
, res
);
431 found
->pub
.beacon_interval
= res
->pub
.beacon_interval
;
432 found
->pub
.tsf
= res
->pub
.tsf
;
433 found
->pub
.signal
= res
->pub
.signal
;
434 found
->pub
.capability
= res
->pub
.capability
;
438 if (res
->pub
.proberesp_ies
) {
439 size_t used
= dev
->wiphy
.bss_priv_size
+ sizeof(*res
);
440 size_t ielen
= res
->pub
.len_proberesp_ies
;
442 if (found
->pub
.proberesp_ies
&&
443 !found
->proberesp_ies_allocated
&&
444 ksize(found
) >= used
+ ielen
) {
445 memcpy(found
->pub
.proberesp_ies
,
446 res
->pub
.proberesp_ies
, ielen
);
447 found
->pub
.len_proberesp_ies
= ielen
;
449 u8
*ies
= found
->pub
.proberesp_ies
;
451 if (found
->proberesp_ies_allocated
)
452 ies
= krealloc(ies
, ielen
, GFP_ATOMIC
);
454 ies
= kmalloc(ielen
, GFP_ATOMIC
);
457 memcpy(ies
, res
->pub
.proberesp_ies
,
459 found
->proberesp_ies_allocated
= true;
460 found
->pub
.proberesp_ies
= ies
;
461 found
->pub
.len_proberesp_ies
= ielen
;
465 /* Override possible earlier Beacon frame IEs */
466 found
->pub
.information_elements
=
467 found
->pub
.proberesp_ies
;
468 found
->pub
.len_information_elements
=
469 found
->pub
.len_proberesp_ies
;
471 if (res
->pub
.beacon_ies
) {
472 size_t used
= dev
->wiphy
.bss_priv_size
+ sizeof(*res
);
473 size_t ielen
= res
->pub
.len_beacon_ies
;
474 bool information_elements_is_beacon_ies
=
475 (found
->pub
.information_elements
==
476 found
->pub
.beacon_ies
);
478 if (found
->pub
.beacon_ies
&&
479 !found
->beacon_ies_allocated
&&
480 ksize(found
) >= used
+ ielen
) {
481 memcpy(found
->pub
.beacon_ies
,
482 res
->pub
.beacon_ies
, ielen
);
483 found
->pub
.len_beacon_ies
= ielen
;
485 u8
*ies
= found
->pub
.beacon_ies
;
487 if (found
->beacon_ies_allocated
)
488 ies
= krealloc(ies
, ielen
, GFP_ATOMIC
);
490 ies
= kmalloc(ielen
, GFP_ATOMIC
);
493 memcpy(ies
, res
->pub
.beacon_ies
,
495 found
->beacon_ies_allocated
= true;
496 found
->pub
.beacon_ies
= ies
;
497 found
->pub
.len_beacon_ies
= ielen
;
501 /* Override IEs if they were from a beacon before */
502 if (information_elements_is_beacon_ies
) {
503 found
->pub
.information_elements
=
504 found
->pub
.beacon_ies
;
505 found
->pub
.len_information_elements
=
506 found
->pub
.len_beacon_ies
;
510 kref_put(&res
->ref
, bss_release
);
512 /* this "consumes" the reference */
513 list_add_tail(&res
->list
, &dev
->bss_list
);
514 rb_insert_bss(dev
, res
);
518 dev
->bss_generation
++;
519 spin_unlock_bh(&dev
->bss_lock
);
521 kref_get(&found
->ref
);
526 cfg80211_inform_bss(struct wiphy
*wiphy
,
527 struct ieee80211_channel
*channel
,
529 u64 timestamp
, u16 capability
, u16 beacon_interval
,
530 const u8
*ie
, size_t ielen
,
531 s32 signal
, gfp_t gfp
)
533 struct cfg80211_internal_bss
*res
;
539 privsz
= wiphy
->bss_priv_size
;
541 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
542 (signal
< 0 || signal
> 100)))
545 res
= kzalloc(sizeof(*res
) + privsz
+ ielen
, gfp
);
549 memcpy(res
->pub
.bssid
, bssid
, ETH_ALEN
);
550 res
->pub
.channel
= channel
;
551 res
->pub
.signal
= signal
;
552 res
->pub
.tsf
= timestamp
;
553 res
->pub
.beacon_interval
= beacon_interval
;
554 res
->pub
.capability
= capability
;
556 * Since we do not know here whether the IEs are from a Beacon or Probe
557 * Response frame, we need to pick one of the options and only use it
558 * with the driver that does not provide the full Beacon/Probe Response
559 * frame. Use Beacon frame pointer to avoid indicating that this should
560 * override the information_elements pointer should we have received an
561 * earlier indication of Probe Response data.
563 * The initial buffer for the IEs is allocated with the BSS entry and
564 * is located after the private area.
566 res
->pub
.beacon_ies
= (u8
*)res
+ sizeof(*res
) + privsz
;
567 memcpy(res
->pub
.beacon_ies
, ie
, ielen
);
568 res
->pub
.len_beacon_ies
= ielen
;
569 res
->pub
.information_elements
= res
->pub
.beacon_ies
;
570 res
->pub
.len_information_elements
= res
->pub
.len_beacon_ies
;
572 kref_init(&res
->ref
);
574 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), res
);
578 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
579 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
581 /* cfg80211_bss_update gives us a referenced result */
584 EXPORT_SYMBOL(cfg80211_inform_bss
);
586 struct cfg80211_bss
*
587 cfg80211_inform_bss_frame(struct wiphy
*wiphy
,
588 struct ieee80211_channel
*channel
,
589 struct ieee80211_mgmt
*mgmt
, size_t len
,
590 s32 signal
, gfp_t gfp
)
592 struct cfg80211_internal_bss
*res
;
593 size_t ielen
= len
- offsetof(struct ieee80211_mgmt
,
594 u
.probe_resp
.variable
);
603 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
604 (signal
< 0 || signal
> 100)))
607 if (WARN_ON(len
< offsetof(struct ieee80211_mgmt
, u
.probe_resp
.variable
)))
610 privsz
= wiphy
->bss_priv_size
;
612 res
= kzalloc(sizeof(*res
) + privsz
+ ielen
, gfp
);
616 memcpy(res
->pub
.bssid
, mgmt
->bssid
, ETH_ALEN
);
617 res
->pub
.channel
= channel
;
618 res
->pub
.signal
= signal
;
619 res
->pub
.tsf
= le64_to_cpu(mgmt
->u
.probe_resp
.timestamp
);
620 res
->pub
.beacon_interval
= le16_to_cpu(mgmt
->u
.probe_resp
.beacon_int
);
621 res
->pub
.capability
= le16_to_cpu(mgmt
->u
.probe_resp
.capab_info
);
623 * The initial buffer for the IEs is allocated with the BSS entry and
624 * is located after the private area.
626 if (ieee80211_is_probe_resp(mgmt
->frame_control
)) {
627 res
->pub
.proberesp_ies
= (u8
*) res
+ sizeof(*res
) + privsz
;
628 memcpy(res
->pub
.proberesp_ies
, mgmt
->u
.probe_resp
.variable
,
630 res
->pub
.len_proberesp_ies
= ielen
;
631 res
->pub
.information_elements
= res
->pub
.proberesp_ies
;
632 res
->pub
.len_information_elements
= res
->pub
.len_proberesp_ies
;
634 res
->pub
.beacon_ies
= (u8
*) res
+ sizeof(*res
) + privsz
;
635 memcpy(res
->pub
.beacon_ies
, mgmt
->u
.beacon
.variable
, ielen
);
636 res
->pub
.len_beacon_ies
= ielen
;
637 res
->pub
.information_elements
= res
->pub
.beacon_ies
;
638 res
->pub
.len_information_elements
= res
->pub
.len_beacon_ies
;
641 kref_init(&res
->ref
);
643 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), res
);
647 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
648 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
650 /* cfg80211_bss_update gives us a referenced result */
653 EXPORT_SYMBOL(cfg80211_inform_bss_frame
);
655 void cfg80211_put_bss(struct cfg80211_bss
*pub
)
657 struct cfg80211_internal_bss
*bss
;
662 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
663 kref_put(&bss
->ref
, bss_release
);
665 EXPORT_SYMBOL(cfg80211_put_bss
);
667 void cfg80211_unlink_bss(struct wiphy
*wiphy
, struct cfg80211_bss
*pub
)
669 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
670 struct cfg80211_internal_bss
*bss
;
675 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
677 spin_lock_bh(&dev
->bss_lock
);
678 if (!list_empty(&bss
->list
)) {
679 __cfg80211_unlink_bss(dev
, bss
);
680 dev
->bss_generation
++;
682 spin_unlock_bh(&dev
->bss_lock
);
684 EXPORT_SYMBOL(cfg80211_unlink_bss
);
686 #ifdef CONFIG_CFG80211_WEXT
687 int cfg80211_wext_siwscan(struct net_device
*dev
,
688 struct iw_request_info
*info
,
689 union iwreq_data
*wrqu
, char *extra
)
691 struct cfg80211_registered_device
*rdev
;
693 struct iw_scan_req
*wreq
= NULL
;
694 struct cfg80211_scan_request
*creq
= NULL
;
695 int i
, err
, n_channels
= 0;
696 enum ieee80211_band band
;
698 if (!netif_running(dev
))
701 if (wrqu
->data
.length
== sizeof(struct iw_scan_req
))
702 wreq
= (struct iw_scan_req
*)extra
;
704 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
707 return PTR_ERR(rdev
);
709 if (rdev
->scan_req
) {
714 wiphy
= &rdev
->wiphy
;
716 /* Determine number of channels, needed to allocate creq */
717 if (wreq
&& wreq
->num_channels
)
718 n_channels
= wreq
->num_channels
;
720 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
721 if (wiphy
->bands
[band
])
722 n_channels
+= wiphy
->bands
[band
]->n_channels
;
725 creq
= kzalloc(sizeof(*creq
) + sizeof(struct cfg80211_ssid
) +
726 n_channels
* sizeof(void *),
735 /* SSIDs come after channels */
736 creq
->ssids
= (void *)&creq
->channels
[n_channels
];
737 creq
->n_channels
= n_channels
;
740 /* translate "Scan on frequencies" request */
742 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
745 if (!wiphy
->bands
[band
])
748 for (j
= 0; j
< wiphy
->bands
[band
]->n_channels
; j
++) {
749 /* ignore disabled channels */
750 if (wiphy
->bands
[band
]->channels
[j
].flags
&
751 IEEE80211_CHAN_DISABLED
)
754 /* If we have a wireless request structure and the
755 * wireless request specifies frequencies, then search
756 * for the matching hardware channel.
758 if (wreq
&& wreq
->num_channels
) {
760 int wiphy_freq
= wiphy
->bands
[band
]->channels
[j
].center_freq
;
761 for (k
= 0; k
< wreq
->num_channels
; k
++) {
762 int wext_freq
= cfg80211_wext_freq(wiphy
, &wreq
->channel_list
[k
]);
763 if (wext_freq
== wiphy_freq
)
764 goto wext_freq_found
;
766 goto wext_freq_not_found
;
770 creq
->channels
[i
] = &wiphy
->bands
[band
]->channels
[j
];
772 wext_freq_not_found
: ;
775 /* No channels found? */
781 /* Set real number of channels specified in creq->channels[] */
782 creq
->n_channels
= i
;
784 /* translate "Scan for SSID" request */
786 if (wrqu
->data
.flags
& IW_SCAN_THIS_ESSID
) {
787 if (wreq
->essid_len
> IEEE80211_MAX_SSID_LEN
) {
791 memcpy(creq
->ssids
[0].ssid
, wreq
->essid
, wreq
->essid_len
);
792 creq
->ssids
[0].ssid_len
= wreq
->essid_len
;
794 if (wreq
->scan_type
== IW_SCAN_TYPE_PASSIVE
)
798 rdev
->scan_req
= creq
;
799 err
= rdev
->ops
->scan(wiphy
, dev
, creq
);
801 rdev
->scan_req
= NULL
;
802 /* creq will be freed below */
804 nl80211_send_scan_start(rdev
, dev
);
805 /* creq now owned by driver */
811 cfg80211_unlock_rdev(rdev
);
814 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan
);
816 static void ieee80211_scan_add_ies(struct iw_request_info
*info
,
817 struct cfg80211_bss
*bss
,
818 char **current_ev
, char *end_buf
)
820 u8
*pos
, *end
, *next
;
823 if (!bss
->information_elements
||
824 !bss
->len_information_elements
)
828 * If needed, fragment the IEs buffer (at IE boundaries) into short
829 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
831 pos
= bss
->information_elements
;
832 end
= pos
+ bss
->len_information_elements
;
834 while (end
- pos
> IW_GENERIC_IE_MAX
) {
835 next
= pos
+ 2 + pos
[1];
836 while (next
+ 2 + next
[1] - pos
< IW_GENERIC_IE_MAX
)
837 next
= next
+ 2 + next
[1];
839 memset(&iwe
, 0, sizeof(iwe
));
841 iwe
.u
.data
.length
= next
- pos
;
842 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
849 memset(&iwe
, 0, sizeof(iwe
));
851 iwe
.u
.data
.length
= end
- pos
;
852 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
857 static inline unsigned int elapsed_jiffies_msecs(unsigned long start
)
859 unsigned long end
= jiffies
;
862 return jiffies_to_msecs(end
- start
);
864 return jiffies_to_msecs(end
+ (MAX_JIFFY_OFFSET
- start
) + 1);
868 ieee80211_bss(struct wiphy
*wiphy
, struct iw_request_info
*info
,
869 struct cfg80211_internal_bss
*bss
, char *current_ev
,
874 u8
*ie
= bss
->pub
.information_elements
;
875 int rem
= bss
->pub
.len_information_elements
, i
, sig
;
878 memset(&iwe
, 0, sizeof(iwe
));
880 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
881 memcpy(iwe
.u
.ap_addr
.sa_data
, bss
->pub
.bssid
, ETH_ALEN
);
882 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
885 memset(&iwe
, 0, sizeof(iwe
));
886 iwe
.cmd
= SIOCGIWFREQ
;
887 iwe
.u
.freq
.m
= ieee80211_frequency_to_channel(bss
->pub
.channel
->center_freq
);
889 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
892 memset(&iwe
, 0, sizeof(iwe
));
893 iwe
.cmd
= SIOCGIWFREQ
;
894 iwe
.u
.freq
.m
= bss
->pub
.channel
->center_freq
;
896 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
899 if (wiphy
->signal_type
!= CFG80211_SIGNAL_TYPE_NONE
) {
900 memset(&iwe
, 0, sizeof(iwe
));
902 iwe
.u
.qual
.updated
= IW_QUAL_LEVEL_UPDATED
|
903 IW_QUAL_NOISE_INVALID
|
904 IW_QUAL_QUAL_UPDATED
;
905 switch (wiphy
->signal_type
) {
906 case CFG80211_SIGNAL_TYPE_MBM
:
907 sig
= bss
->pub
.signal
/ 100;
908 iwe
.u
.qual
.level
= sig
;
909 iwe
.u
.qual
.updated
|= IW_QUAL_DBM
;
910 if (sig
< -110) /* rather bad */
912 else if (sig
> -40) /* perfect */
914 /* will give a range of 0 .. 70 */
915 iwe
.u
.qual
.qual
= sig
+ 110;
917 case CFG80211_SIGNAL_TYPE_UNSPEC
:
918 iwe
.u
.qual
.level
= bss
->pub
.signal
;
919 /* will give range 0 .. 100 */
920 iwe
.u
.qual
.qual
= bss
->pub
.signal
;
926 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
927 &iwe
, IW_EV_QUAL_LEN
);
930 memset(&iwe
, 0, sizeof(iwe
));
931 iwe
.cmd
= SIOCGIWENCODE
;
932 if (bss
->pub
.capability
& WLAN_CAPABILITY_PRIVACY
)
933 iwe
.u
.data
.flags
= IW_ENCODE_ENABLED
| IW_ENCODE_NOKEY
;
935 iwe
.u
.data
.flags
= IW_ENCODE_DISABLED
;
936 iwe
.u
.data
.length
= 0;
937 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
947 memset(&iwe
, 0, sizeof(iwe
));
948 iwe
.cmd
= SIOCGIWESSID
;
949 iwe
.u
.data
.length
= ie
[1];
950 iwe
.u
.data
.flags
= 1;
951 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
954 case WLAN_EID_MESH_ID
:
955 memset(&iwe
, 0, sizeof(iwe
));
956 iwe
.cmd
= SIOCGIWESSID
;
957 iwe
.u
.data
.length
= ie
[1];
958 iwe
.u
.data
.flags
= 1;
959 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
962 case WLAN_EID_MESH_CONFIG
:
964 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
966 buf
= kmalloc(50, GFP_ATOMIC
);
970 memset(&iwe
, 0, sizeof(iwe
));
971 iwe
.cmd
= IWEVCUSTOM
;
972 sprintf(buf
, "Mesh Network Path Selection Protocol ID: "
974 iwe
.u
.data
.length
= strlen(buf
);
975 current_ev
= iwe_stream_add_point(info
, current_ev
,
978 sprintf(buf
, "Path Selection Metric ID: 0x%02X",
980 iwe
.u
.data
.length
= strlen(buf
);
981 current_ev
= iwe_stream_add_point(info
, current_ev
,
984 sprintf(buf
, "Congestion Control Mode ID: 0x%02X",
986 iwe
.u
.data
.length
= strlen(buf
);
987 current_ev
= iwe_stream_add_point(info
, current_ev
,
990 sprintf(buf
, "Synchronization ID: 0x%02X", cfg
[3]);
991 iwe
.u
.data
.length
= strlen(buf
);
992 current_ev
= iwe_stream_add_point(info
, current_ev
,
995 sprintf(buf
, "Authentication ID: 0x%02X", cfg
[4]);
996 iwe
.u
.data
.length
= strlen(buf
);
997 current_ev
= iwe_stream_add_point(info
, current_ev
,
1000 sprintf(buf
, "Formation Info: 0x%02X", cfg
[5]);
1001 iwe
.u
.data
.length
= strlen(buf
);
1002 current_ev
= iwe_stream_add_point(info
, current_ev
,
1005 sprintf(buf
, "Capabilities: 0x%02X", cfg
[6]);
1006 iwe
.u
.data
.length
= strlen(buf
);
1007 current_ev
= iwe_stream_add_point(info
, current_ev
,
1012 case WLAN_EID_SUPP_RATES
:
1013 case WLAN_EID_EXT_SUPP_RATES
:
1014 /* display all supported rates in readable format */
1015 p
= current_ev
+ iwe_stream_lcp_len(info
);
1017 memset(&iwe
, 0, sizeof(iwe
));
1018 iwe
.cmd
= SIOCGIWRATE
;
1019 /* Those two flags are ignored... */
1020 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
1022 for (i
= 0; i
< ie
[1]; i
++) {
1023 iwe
.u
.bitrate
.value
=
1024 ((ie
[i
+ 2] & 0x7f) * 500000);
1025 p
= iwe_stream_add_value(info
, current_ev
, p
,
1026 end_buf
, &iwe
, IW_EV_PARAM_LEN
);
1035 if (bss
->pub
.capability
& (WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_IBSS
) ||
1037 memset(&iwe
, 0, sizeof(iwe
));
1038 iwe
.cmd
= SIOCGIWMODE
;
1040 iwe
.u
.mode
= IW_MODE_MESH
;
1041 else if (bss
->pub
.capability
& WLAN_CAPABILITY_ESS
)
1042 iwe
.u
.mode
= IW_MODE_MASTER
;
1044 iwe
.u
.mode
= IW_MODE_ADHOC
;
1045 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
1046 &iwe
, IW_EV_UINT_LEN
);
1049 buf
= kmalloc(30, GFP_ATOMIC
);
1051 memset(&iwe
, 0, sizeof(iwe
));
1052 iwe
.cmd
= IWEVCUSTOM
;
1053 sprintf(buf
, "tsf=%016llx", (unsigned long long)(bss
->pub
.tsf
));
1054 iwe
.u
.data
.length
= strlen(buf
);
1055 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1057 memset(&iwe
, 0, sizeof(iwe
));
1058 iwe
.cmd
= IWEVCUSTOM
;
1059 sprintf(buf
, " Last beacon: %ums ago",
1060 elapsed_jiffies_msecs(bss
->ts
));
1061 iwe
.u
.data
.length
= strlen(buf
);
1062 current_ev
= iwe_stream_add_point(info
, current_ev
,
1063 end_buf
, &iwe
, buf
);
1067 ieee80211_scan_add_ies(info
, &bss
->pub
, ¤t_ev
, end_buf
);
1073 static int ieee80211_scan_results(struct cfg80211_registered_device
*dev
,
1074 struct iw_request_info
*info
,
1075 char *buf
, size_t len
)
1077 char *current_ev
= buf
;
1078 char *end_buf
= buf
+ len
;
1079 struct cfg80211_internal_bss
*bss
;
1081 spin_lock_bh(&dev
->bss_lock
);
1082 cfg80211_bss_expire(dev
);
1084 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
1085 if (buf
+ len
- current_ev
<= IW_EV_ADDR_LEN
) {
1086 spin_unlock_bh(&dev
->bss_lock
);
1089 current_ev
= ieee80211_bss(&dev
->wiphy
, info
, bss
,
1090 current_ev
, end_buf
);
1092 spin_unlock_bh(&dev
->bss_lock
);
1093 return current_ev
- buf
;
1097 int cfg80211_wext_giwscan(struct net_device
*dev
,
1098 struct iw_request_info
*info
,
1099 struct iw_point
*data
, char *extra
)
1101 struct cfg80211_registered_device
*rdev
;
1104 if (!netif_running(dev
))
1107 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
1110 return PTR_ERR(rdev
);
1112 if (rdev
->scan_req
) {
1117 res
= ieee80211_scan_results(rdev
, info
, extra
, data
->length
);
1125 cfg80211_unlock_rdev(rdev
);
1128 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan
);