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/cfg80211-wext.h>
16 #include <net/iw_handler.h>
19 #include "wext-compat.h"
22 #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
24 static void bss_release(struct kref
*ref
)
26 struct cfg80211_bss_ies
*ies
;
27 struct cfg80211_internal_bss
*bss
;
29 bss
= container_of(ref
, struct cfg80211_internal_bss
, ref
);
31 if (WARN_ON(atomic_read(&bss
->hold
)))
34 if (bss
->pub
.free_priv
)
35 bss
->pub
.free_priv(&bss
->pub
);
37 ies
= (void *)rcu_access_pointer(bss
->pub
.beacon_ies
);
39 kfree_rcu(ies
, rcu_head
);
40 ies
= (void *)rcu_access_pointer(bss
->pub
.proberesp_ies
);
42 kfree_rcu(ies
, rcu_head
);
47 /* must hold dev->bss_lock! */
48 static void __cfg80211_unlink_bss(struct cfg80211_registered_device
*dev
,
49 struct cfg80211_internal_bss
*bss
)
51 list_del_init(&bss
->list
);
52 rb_erase(&bss
->rbn
, &dev
->bss_tree
);
53 kref_put(&bss
->ref
, bss_release
);
56 /* must hold dev->bss_lock! */
57 static void __cfg80211_bss_expire(struct cfg80211_registered_device
*dev
,
58 unsigned long expire_time
)
60 struct cfg80211_internal_bss
*bss
, *tmp
;
63 list_for_each_entry_safe(bss
, tmp
, &dev
->bss_list
, list
) {
64 if (atomic_read(&bss
->hold
))
66 if (!time_after(expire_time
, bss
->ts
))
69 __cfg80211_unlink_bss(dev
, bss
);
74 dev
->bss_generation
++;
77 void ___cfg80211_scan_done(struct cfg80211_registered_device
*rdev
, bool leak
)
79 struct cfg80211_scan_request
*request
;
80 struct wireless_dev
*wdev
;
81 #ifdef CONFIG_CFG80211_WEXT
82 union iwreq_data wrqu
;
85 ASSERT_RDEV_LOCK(rdev
);
87 request
= rdev
->scan_req
;
95 * This must be before sending the other events!
96 * Otherwise, wpa_supplicant gets completely confused with
100 cfg80211_sme_scan_done(wdev
->netdev
);
102 if (request
->aborted
) {
103 nl80211_send_scan_aborted(rdev
, wdev
);
105 if (request
->flags
& NL80211_SCAN_FLAG_FLUSH
) {
106 /* flush entries from previous scans */
107 spin_lock_bh(&rdev
->bss_lock
);
108 __cfg80211_bss_expire(rdev
, request
->scan_start
);
109 spin_unlock_bh(&rdev
->bss_lock
);
111 nl80211_send_scan_done(rdev
, wdev
);
114 #ifdef CONFIG_CFG80211_WEXT
115 if (wdev
->netdev
&& !request
->aborted
) {
116 memset(&wrqu
, 0, sizeof(wrqu
));
118 wireless_send_event(wdev
->netdev
, SIOCGIWSCAN
, &wrqu
, NULL
);
123 dev_put(wdev
->netdev
);
125 rdev
->scan_req
= NULL
;
128 * OK. If this is invoked with "leak" then we can't
129 * free this ... but we've cleaned it up anyway. The
130 * driver failed to call the scan_done callback, so
131 * all bets are off, it might still be trying to use
132 * the scan request or not ... if it accesses the dev
133 * in there (it shouldn't anyway) then it may crash.
139 void __cfg80211_scan_done(struct work_struct
*wk
)
141 struct cfg80211_registered_device
*rdev
;
143 rdev
= container_of(wk
, struct cfg80211_registered_device
,
146 cfg80211_lock_rdev(rdev
);
147 ___cfg80211_scan_done(rdev
, false);
148 cfg80211_unlock_rdev(rdev
);
151 void cfg80211_scan_done(struct cfg80211_scan_request
*request
, bool aborted
)
153 trace_cfg80211_scan_done(request
, aborted
);
154 WARN_ON(request
!= wiphy_to_dev(request
->wiphy
)->scan_req
);
156 request
->aborted
= aborted
;
157 queue_work(cfg80211_wq
, &wiphy_to_dev(request
->wiphy
)->scan_done_wk
);
159 EXPORT_SYMBOL(cfg80211_scan_done
);
161 void __cfg80211_sched_scan_results(struct work_struct
*wk
)
163 struct cfg80211_registered_device
*rdev
;
164 struct cfg80211_sched_scan_request
*request
;
166 rdev
= container_of(wk
, struct cfg80211_registered_device
,
167 sched_scan_results_wk
);
169 request
= rdev
->sched_scan_req
;
171 mutex_lock(&rdev
->sched_scan_mtx
);
173 /* we don't have sched_scan_req anymore if the scan is stopping */
175 if (request
->flags
& NL80211_SCAN_FLAG_FLUSH
) {
176 /* flush entries from previous scans */
177 spin_lock_bh(&rdev
->bss_lock
);
178 __cfg80211_bss_expire(rdev
, request
->scan_start
);
179 spin_unlock_bh(&rdev
->bss_lock
);
180 request
->scan_start
=
181 jiffies
+ msecs_to_jiffies(request
->interval
);
183 nl80211_send_sched_scan_results(rdev
, request
->dev
);
186 mutex_unlock(&rdev
->sched_scan_mtx
);
189 void cfg80211_sched_scan_results(struct wiphy
*wiphy
)
191 trace_cfg80211_sched_scan_results(wiphy
);
192 /* ignore if we're not scanning */
193 if (wiphy_to_dev(wiphy
)->sched_scan_req
)
194 queue_work(cfg80211_wq
,
195 &wiphy_to_dev(wiphy
)->sched_scan_results_wk
);
197 EXPORT_SYMBOL(cfg80211_sched_scan_results
);
199 void cfg80211_sched_scan_stopped(struct wiphy
*wiphy
)
201 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
203 trace_cfg80211_sched_scan_stopped(wiphy
);
205 mutex_lock(&rdev
->sched_scan_mtx
);
206 __cfg80211_stop_sched_scan(rdev
, true);
207 mutex_unlock(&rdev
->sched_scan_mtx
);
209 EXPORT_SYMBOL(cfg80211_sched_scan_stopped
);
211 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device
*rdev
,
212 bool driver_initiated
)
214 struct net_device
*dev
;
216 lockdep_assert_held(&rdev
->sched_scan_mtx
);
218 if (!rdev
->sched_scan_req
)
221 dev
= rdev
->sched_scan_req
->dev
;
223 if (!driver_initiated
) {
224 int err
= rdev_sched_scan_stop(rdev
, dev
);
229 nl80211_send_sched_scan(rdev
, dev
, NL80211_CMD_SCHED_SCAN_STOPPED
);
231 kfree(rdev
->sched_scan_req
);
232 rdev
->sched_scan_req
= NULL
;
237 /* must hold dev->bss_lock! */
238 void cfg80211_bss_age(struct cfg80211_registered_device
*dev
,
239 unsigned long age_secs
)
241 struct cfg80211_internal_bss
*bss
;
242 unsigned long age_jiffies
= msecs_to_jiffies(age_secs
* MSEC_PER_SEC
);
244 list_for_each_entry(bss
, &dev
->bss_list
, list
)
245 bss
->ts
-= age_jiffies
;
248 void cfg80211_bss_expire(struct cfg80211_registered_device
*dev
)
250 __cfg80211_bss_expire(dev
, jiffies
- IEEE80211_SCAN_RESULT_EXPIRE
);
253 const u8
*cfg80211_find_ie(u8 eid
, const u8
*ies
, int len
)
255 while (len
> 2 && ies
[0] != eid
) {
261 if (len
< 2 + ies
[1])
265 EXPORT_SYMBOL(cfg80211_find_ie
);
267 const u8
*cfg80211_find_vendor_ie(unsigned int oui
, u8 oui_type
,
268 const u8
*ies
, int len
)
270 struct ieee80211_vendor_ie
*ie
;
271 const u8
*pos
= ies
, *end
= ies
+ len
;
275 pos
= cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC
, pos
,
280 if (end
- pos
< sizeof(*ie
))
283 ie
= (struct ieee80211_vendor_ie
*)pos
;
284 ie_oui
= ie
->oui
[0] << 16 | ie
->oui
[1] << 8 | ie
->oui
[2];
285 if (ie_oui
== oui
&& ie
->oui_type
== oui_type
)
292 EXPORT_SYMBOL(cfg80211_find_vendor_ie
);
294 static int cmp_ies(u8 num
, const u8
*ies1
, int len1
, const u8
*ies2
, int len2
)
296 const u8
*ie1
= cfg80211_find_ie(num
, ies1
, len1
);
297 const u8
*ie2
= cfg80211_find_ie(num
, ies2
, len2
);
299 /* equal if both missing */
302 /* sort missing IE before (left of) present IE */
308 /* sort by length first, then by contents */
309 if (ie1
[1] != ie2
[1])
310 return ie2
[1] - ie1
[1];
311 return memcmp(ie1
+ 2, ie2
+ 2, ie1
[1]);
314 static bool is_bss(struct cfg80211_bss
*a
, const u8
*bssid
,
315 const u8
*ssid
, size_t ssid_len
)
317 const struct cfg80211_bss_ies
*ies
;
320 if (bssid
&& !ether_addr_equal(a
->bssid
, bssid
))
326 ies
= rcu_access_pointer(a
->ies
);
329 ssidie
= cfg80211_find_ie(WLAN_EID_SSID
, ies
->data
, ies
->len
);
332 if (ssidie
[1] != ssid_len
)
334 return memcmp(ssidie
+ 2, ssid
, ssid_len
) == 0;
337 static bool is_mesh_bss(struct cfg80211_bss
*a
)
339 const struct cfg80211_bss_ies
*ies
;
342 if (!WLAN_CAPABILITY_IS_STA_BSS(a
->capability
))
345 ies
= rcu_access_pointer(a
->ies
);
349 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
, ies
->data
, ies
->len
);
353 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
, ies
->data
, ies
->len
);
360 static bool is_mesh(struct cfg80211_bss
*a
,
361 const u8
*meshid
, size_t meshidlen
,
364 const struct cfg80211_bss_ies
*ies
;
367 if (!WLAN_CAPABILITY_IS_STA_BSS(a
->capability
))
370 ies
= rcu_access_pointer(a
->ies
);
374 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
, ies
->data
, ies
->len
);
377 if (ie
[1] != meshidlen
)
379 if (memcmp(ie
+ 2, meshid
, meshidlen
))
382 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
, ies
->data
, ies
->len
);
385 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
389 * Ignore mesh capability (last two bytes of the IE) when
390 * comparing since that may differ between stations taking
391 * part in the same mesh.
393 return memcmp(ie
+ 2, meshcfg
,
394 sizeof(struct ieee80211_meshconf_ie
) - 2) == 0;
397 static int cmp_bss_core(struct cfg80211_bss
*a
, struct cfg80211_bss
*b
)
399 const struct cfg80211_bss_ies
*a_ies
, *b_ies
;
402 if (a
->channel
!= b
->channel
)
403 return b
->channel
->center_freq
- a
->channel
->center_freq
;
405 if (is_mesh_bss(a
) && is_mesh_bss(b
)) {
406 a_ies
= rcu_access_pointer(a
->ies
);
409 b_ies
= rcu_access_pointer(b
->ies
);
413 r
= cmp_ies(WLAN_EID_MESH_ID
,
414 a_ies
->data
, a_ies
->len
,
415 b_ies
->data
, b_ies
->len
);
418 return cmp_ies(WLAN_EID_MESH_CONFIG
,
419 a_ies
->data
, a_ies
->len
,
420 b_ies
->data
, b_ies
->len
);
424 * we can't use compare_ether_addr here since we need a < > operator.
425 * The binary return value of compare_ether_addr isn't enough
427 return memcmp(a
->bssid
, b
->bssid
, sizeof(a
->bssid
));
430 static int cmp_bss(struct cfg80211_bss
*a
,
431 struct cfg80211_bss
*b
)
433 const struct cfg80211_bss_ies
*a_ies
, *b_ies
;
436 r
= cmp_bss_core(a
, b
);
440 a_ies
= rcu_access_pointer(a
->ies
);
443 b_ies
= rcu_access_pointer(b
->ies
);
447 return cmp_ies(WLAN_EID_SSID
,
448 a_ies
->data
, a_ies
->len
,
449 b_ies
->data
, b_ies
->len
);
452 static int cmp_hidden_bss(struct cfg80211_bss
*a
, struct cfg80211_bss
*b
)
454 const struct cfg80211_bss_ies
*a_ies
, *b_ies
;
460 r
= cmp_bss_core(a
, b
);
464 a_ies
= rcu_access_pointer(a
->ies
);
467 b_ies
= rcu_access_pointer(b
->ies
);
471 ie1
= cfg80211_find_ie(WLAN_EID_SSID
, a_ies
->data
, a_ies
->len
);
472 ie2
= cfg80211_find_ie(WLAN_EID_SSID
, b_ies
->data
, b_ies
->len
);
475 * Key comparator must use same algorithm in any rb-tree
476 * search function (order is important), otherwise ordering
477 * of items in the tree is broken and search gives incorrect
478 * results. This code uses same order as cmp_ies() does.
480 * Note that due to the differring behaviour with hidden SSIDs
481 * this function only works when "b" is the tree element and
482 * "a" is the key we're looking for.
485 /* sort missing IE before (left of) present IE */
491 /* zero-size SSID is used as an indication of the hidden bss */
495 /* sort by length first, then by contents */
496 if (ie1
[1] != ie2
[1])
497 return ie2
[1] - ie1
[1];
500 * zeroed SSID ie is another indication of a hidden bss;
501 * if it isn't zeroed just return the regular sort value
502 * to find the next candidate
504 for (i
= 0; i
< ie2
[1]; i
++)
506 return memcmp(ie1
+ 2, ie2
+ 2, ie1
[1]);
511 struct cfg80211_bss
*cfg80211_get_bss(struct wiphy
*wiphy
,
512 struct ieee80211_channel
*channel
,
514 const u8
*ssid
, size_t ssid_len
,
515 u16 capa_mask
, u16 capa_val
)
517 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
518 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
519 unsigned long now
= jiffies
;
521 trace_cfg80211_get_bss(wiphy
, channel
, bssid
, ssid
, ssid_len
, capa_mask
,
524 spin_lock_bh(&dev
->bss_lock
);
526 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
527 if ((bss
->pub
.capability
& capa_mask
) != capa_val
)
529 if (channel
&& bss
->pub
.channel
!= channel
)
531 /* Don't get expired BSS structs */
532 if (time_after(now
, bss
->ts
+ IEEE80211_SCAN_RESULT_EXPIRE
) &&
533 !atomic_read(&bss
->hold
))
535 if (is_bss(&bss
->pub
, bssid
, ssid
, ssid_len
)) {
542 spin_unlock_bh(&dev
->bss_lock
);
545 trace_cfg80211_return_bss(&res
->pub
);
548 EXPORT_SYMBOL(cfg80211_get_bss
);
550 struct cfg80211_bss
*cfg80211_get_mesh(struct wiphy
*wiphy
,
551 struct ieee80211_channel
*channel
,
552 const u8
*meshid
, size_t meshidlen
,
555 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
556 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
558 spin_lock_bh(&dev
->bss_lock
);
560 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
561 if (channel
&& bss
->pub
.channel
!= channel
)
563 if (is_mesh(&bss
->pub
, meshid
, meshidlen
, meshcfg
)) {
570 spin_unlock_bh(&dev
->bss_lock
);
575 EXPORT_SYMBOL(cfg80211_get_mesh
);
578 static void rb_insert_bss(struct cfg80211_registered_device
*dev
,
579 struct cfg80211_internal_bss
*bss
)
581 struct rb_node
**p
= &dev
->bss_tree
.rb_node
;
582 struct rb_node
*parent
= NULL
;
583 struct cfg80211_internal_bss
*tbss
;
588 tbss
= rb_entry(parent
, struct cfg80211_internal_bss
, rbn
);
590 cmp
= cmp_bss(&bss
->pub
, &tbss
->pub
);
593 /* will sort of leak this BSS */
603 rb_link_node(&bss
->rbn
, parent
, p
);
604 rb_insert_color(&bss
->rbn
, &dev
->bss_tree
);
607 static struct cfg80211_internal_bss
*
608 rb_find_bss(struct cfg80211_registered_device
*dev
,
609 struct cfg80211_internal_bss
*res
)
611 struct rb_node
*n
= dev
->bss_tree
.rb_node
;
612 struct cfg80211_internal_bss
*bss
;
616 bss
= rb_entry(n
, struct cfg80211_internal_bss
, rbn
);
617 r
= cmp_bss(&res
->pub
, &bss
->pub
);
630 static struct cfg80211_internal_bss
*
631 rb_find_hidden_bss(struct cfg80211_registered_device
*dev
,
632 struct cfg80211_internal_bss
*res
)
634 struct rb_node
*n
= dev
->bss_tree
.rb_node
;
635 struct cfg80211_internal_bss
*bss
;
639 bss
= rb_entry(n
, struct cfg80211_internal_bss
, rbn
);
640 r
= cmp_hidden_bss(&res
->pub
, &bss
->pub
);
654 copy_hidden_ies(struct cfg80211_internal_bss
*res
,
655 struct cfg80211_internal_bss
*hidden
)
657 const struct cfg80211_bss_ies
*ies
;
659 if (rcu_access_pointer(res
->pub
.beacon_ies
))
662 ies
= rcu_access_pointer(hidden
->pub
.beacon_ies
);
666 ies
= kmemdup(ies
, sizeof(*ies
) + ies
->len
, GFP_ATOMIC
);
669 rcu_assign_pointer(res
->pub
.beacon_ies
, ies
);
672 static struct cfg80211_internal_bss
*
673 cfg80211_bss_update(struct cfg80211_registered_device
*dev
,
674 struct cfg80211_internal_bss
*tmp
)
676 struct cfg80211_internal_bss
*found
= NULL
;
678 if (WARN_ON(!tmp
->pub
.channel
))
683 spin_lock_bh(&dev
->bss_lock
);
685 if (WARN_ON(!rcu_access_pointer(tmp
->pub
.ies
))) {
686 spin_unlock_bh(&dev
->bss_lock
);
690 found
= rb_find_bss(dev
, tmp
);
693 found
->pub
.beacon_interval
= tmp
->pub
.beacon_interval
;
694 found
->pub
.tsf
= tmp
->pub
.tsf
;
695 found
->pub
.signal
= tmp
->pub
.signal
;
696 found
->pub
.capability
= tmp
->pub
.capability
;
700 if (rcu_access_pointer(tmp
->pub
.proberesp_ies
)) {
701 const struct cfg80211_bss_ies
*old
;
703 old
= rcu_access_pointer(found
->pub
.proberesp_ies
);
705 rcu_assign_pointer(found
->pub
.proberesp_ies
,
706 tmp
->pub
.proberesp_ies
);
707 /* Override possible earlier Beacon frame IEs */
708 rcu_assign_pointer(found
->pub
.ies
,
709 tmp
->pub
.proberesp_ies
);
711 kfree_rcu((struct cfg80211_bss_ies
*)old
,
713 } else if (rcu_access_pointer(tmp
->pub
.beacon_ies
)) {
714 const struct cfg80211_bss_ies
*old
, *ies
;
716 old
= rcu_access_pointer(found
->pub
.beacon_ies
);
717 ies
= rcu_access_pointer(found
->pub
.ies
);
719 rcu_assign_pointer(found
->pub
.beacon_ies
,
720 tmp
->pub
.beacon_ies
);
722 /* Override IEs if they were from a beacon before */
724 rcu_assign_pointer(found
->pub
.ies
,
725 tmp
->pub
.beacon_ies
);
728 kfree_rcu((struct cfg80211_bss_ies
*)old
,
732 struct cfg80211_internal_bss
*new;
733 struct cfg80211_internal_bss
*hidden
;
734 struct cfg80211_bss_ies
*ies
;
736 /* First check if the beacon is a probe response from
737 * a hidden bss. If so, copy beacon ies (with nullified
738 * ssid) into the probe response bss entry (with real ssid).
739 * It is required basically for PSM implementation
740 * (probe responses do not contain tim ie) */
742 /* TODO: The code is not trying to update existing probe
743 * response bss entries when beacon ies are
744 * getting changed. */
745 hidden
= rb_find_hidden_bss(dev
, tmp
);
747 copy_hidden_ies(tmp
, hidden
);
750 * create a copy -- the "res" variable that is passed in
751 * is allocated on the stack since it's not needed in the
752 * more common case of an update
754 new = kzalloc(sizeof(*new) + dev
->wiphy
.bss_priv_size
,
757 ies
= (void *)rcu_dereference(tmp
->pub
.beacon_ies
);
759 kfree_rcu(ies
, rcu_head
);
760 ies
= (void *)rcu_dereference(tmp
->pub
.proberesp_ies
);
762 kfree_rcu(ies
, rcu_head
);
763 spin_unlock_bh(&dev
->bss_lock
);
766 memcpy(new, tmp
, sizeof(*new));
767 kref_init(&new->ref
);
768 list_add_tail(&new->list
, &dev
->bss_list
);
769 rb_insert_bss(dev
, new);
773 dev
->bss_generation
++;
774 spin_unlock_bh(&dev
->bss_lock
);
776 kref_get(&found
->ref
);
780 static struct ieee80211_channel
*
781 cfg80211_get_bss_channel(struct wiphy
*wiphy
, const u8
*ie
, size_t ielen
,
782 struct ieee80211_channel
*channel
)
786 int channel_number
= -1;
788 tmp
= cfg80211_find_ie(WLAN_EID_DS_PARAMS
, ie
, ielen
);
789 if (tmp
&& tmp
[1] == 1) {
790 channel_number
= tmp
[2];
792 tmp
= cfg80211_find_ie(WLAN_EID_HT_OPERATION
, ie
, ielen
);
793 if (tmp
&& tmp
[1] >= sizeof(struct ieee80211_ht_operation
)) {
794 struct ieee80211_ht_operation
*htop
= (void *)(tmp
+ 2);
796 channel_number
= htop
->primary_chan
;
800 if (channel_number
< 0)
803 freq
= ieee80211_channel_to_frequency(channel_number
, channel
->band
);
804 channel
= ieee80211_get_channel(wiphy
, freq
);
807 if (channel
->flags
& IEEE80211_CHAN_DISABLED
)
813 cfg80211_inform_bss(struct wiphy
*wiphy
,
814 struct ieee80211_channel
*channel
,
815 const u8
*bssid
, u64 tsf
, u16 capability
,
816 u16 beacon_interval
, const u8
*ie
, size_t ielen
,
817 s32 signal
, gfp_t gfp
)
819 struct cfg80211_bss_ies
*ies
;
820 struct cfg80211_internal_bss tmp
= {}, *res
;
825 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
826 (signal
< 0 || signal
> 100)))
829 channel
= cfg80211_get_bss_channel(wiphy
, ie
, ielen
, channel
);
833 memcpy(tmp
.pub
.bssid
, bssid
, ETH_ALEN
);
834 tmp
.pub
.channel
= channel
;
835 tmp
.pub
.signal
= signal
;
837 tmp
.pub
.beacon_interval
= beacon_interval
;
838 tmp
.pub
.capability
= capability
;
840 * Since we do not know here whether the IEs are from a Beacon or Probe
841 * Response frame, we need to pick one of the options and only use it
842 * with the driver that does not provide the full Beacon/Probe Response
843 * frame. Use Beacon frame pointer to avoid indicating that this should
844 * override the iies pointer should we have received an earlier
845 * indication of Probe Response data.
847 * The initial buffer for the IEs is allocated with the BSS entry and
848 * is located after the private area.
850 ies
= kmalloc(sizeof(*ies
) + ielen
, gfp
);
854 memcpy(ies
->data
, ie
, ielen
);
856 rcu_assign_pointer(tmp
.pub
.beacon_ies
, ies
);
857 rcu_assign_pointer(tmp
.pub
.ies
, ies
);
859 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), &tmp
);
863 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
864 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
866 trace_cfg80211_return_bss(&res
->pub
);
867 /* cfg80211_bss_update gives us a referenced result */
870 EXPORT_SYMBOL(cfg80211_inform_bss
);
872 struct cfg80211_bss
*
873 cfg80211_inform_bss_frame(struct wiphy
*wiphy
,
874 struct ieee80211_channel
*channel
,
875 struct ieee80211_mgmt
*mgmt
, size_t len
,
876 s32 signal
, gfp_t gfp
)
878 struct cfg80211_internal_bss tmp
= {}, *res
;
879 struct cfg80211_bss_ies
*ies
;
880 size_t ielen
= len
- offsetof(struct ieee80211_mgmt
,
881 u
.probe_resp
.variable
);
883 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt
, u
.probe_resp
.variable
) !=
884 offsetof(struct ieee80211_mgmt
, u
.beacon
.variable
));
886 trace_cfg80211_inform_bss_frame(wiphy
, channel
, mgmt
, len
, signal
);
894 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
895 (signal
< 0 || signal
> 100)))
898 if (WARN_ON(len
< offsetof(struct ieee80211_mgmt
, u
.probe_resp
.variable
)))
901 channel
= cfg80211_get_bss_channel(wiphy
, mgmt
->u
.beacon
.variable
,
906 ies
= kmalloc(sizeof(*ies
) + ielen
, gfp
);
910 memcpy(ies
->data
, mgmt
->u
.probe_resp
.variable
, ielen
);
912 if (ieee80211_is_probe_resp(mgmt
->frame_control
))
913 rcu_assign_pointer(tmp
.pub
.proberesp_ies
, ies
);
915 rcu_assign_pointer(tmp
.pub
.beacon_ies
, ies
);
916 rcu_assign_pointer(tmp
.pub
.ies
, ies
);
918 memcpy(tmp
.pub
.bssid
, mgmt
->bssid
, ETH_ALEN
);
919 tmp
.pub
.channel
= channel
;
920 tmp
.pub
.signal
= signal
;
921 tmp
.pub
.tsf
= le64_to_cpu(mgmt
->u
.probe_resp
.timestamp
);
922 tmp
.pub
.beacon_interval
= le16_to_cpu(mgmt
->u
.probe_resp
.beacon_int
);
923 tmp
.pub
.capability
= le16_to_cpu(mgmt
->u
.probe_resp
.capab_info
);
925 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), &tmp
);
929 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
930 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
932 trace_cfg80211_return_bss(&res
->pub
);
933 /* cfg80211_bss_update gives us a referenced result */
936 EXPORT_SYMBOL(cfg80211_inform_bss_frame
);
938 void cfg80211_ref_bss(struct cfg80211_bss
*pub
)
940 struct cfg80211_internal_bss
*bss
;
945 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
948 EXPORT_SYMBOL(cfg80211_ref_bss
);
950 void cfg80211_put_bss(struct cfg80211_bss
*pub
)
952 struct cfg80211_internal_bss
*bss
;
957 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
958 kref_put(&bss
->ref
, bss_release
);
960 EXPORT_SYMBOL(cfg80211_put_bss
);
962 void cfg80211_unlink_bss(struct wiphy
*wiphy
, struct cfg80211_bss
*pub
)
964 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
965 struct cfg80211_internal_bss
*bss
;
970 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
972 spin_lock_bh(&dev
->bss_lock
);
973 if (!list_empty(&bss
->list
)) {
974 __cfg80211_unlink_bss(dev
, bss
);
975 dev
->bss_generation
++;
977 spin_unlock_bh(&dev
->bss_lock
);
979 EXPORT_SYMBOL(cfg80211_unlink_bss
);
981 #ifdef CONFIG_CFG80211_WEXT
982 int cfg80211_wext_siwscan(struct net_device
*dev
,
983 struct iw_request_info
*info
,
984 union iwreq_data
*wrqu
, char *extra
)
986 struct cfg80211_registered_device
*rdev
;
988 struct iw_scan_req
*wreq
= NULL
;
989 struct cfg80211_scan_request
*creq
= NULL
;
990 int i
, err
, n_channels
= 0;
991 enum ieee80211_band band
;
993 if (!netif_running(dev
))
996 if (wrqu
->data
.length
== sizeof(struct iw_scan_req
))
997 wreq
= (struct iw_scan_req
*)extra
;
999 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
1002 return PTR_ERR(rdev
);
1004 if (rdev
->scan_req
) {
1009 wiphy
= &rdev
->wiphy
;
1011 /* Determine number of channels, needed to allocate creq */
1012 if (wreq
&& wreq
->num_channels
)
1013 n_channels
= wreq
->num_channels
;
1015 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1016 if (wiphy
->bands
[band
])
1017 n_channels
+= wiphy
->bands
[band
]->n_channels
;
1020 creq
= kzalloc(sizeof(*creq
) + sizeof(struct cfg80211_ssid
) +
1021 n_channels
* sizeof(void *),
1028 creq
->wiphy
= wiphy
;
1029 creq
->wdev
= dev
->ieee80211_ptr
;
1030 /* SSIDs come after channels */
1031 creq
->ssids
= (void *)&creq
->channels
[n_channels
];
1032 creq
->n_channels
= n_channels
;
1034 creq
->scan_start
= jiffies
;
1036 /* translate "Scan on frequencies" request */
1038 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1041 if (!wiphy
->bands
[band
])
1044 for (j
= 0; j
< wiphy
->bands
[band
]->n_channels
; j
++) {
1045 /* ignore disabled channels */
1046 if (wiphy
->bands
[band
]->channels
[j
].flags
&
1047 IEEE80211_CHAN_DISABLED
)
1050 /* If we have a wireless request structure and the
1051 * wireless request specifies frequencies, then search
1052 * for the matching hardware channel.
1054 if (wreq
&& wreq
->num_channels
) {
1056 int wiphy_freq
= wiphy
->bands
[band
]->channels
[j
].center_freq
;
1057 for (k
= 0; k
< wreq
->num_channels
; k
++) {
1058 int wext_freq
= cfg80211_wext_freq(wiphy
, &wreq
->channel_list
[k
]);
1059 if (wext_freq
== wiphy_freq
)
1060 goto wext_freq_found
;
1062 goto wext_freq_not_found
;
1066 creq
->channels
[i
] = &wiphy
->bands
[band
]->channels
[j
];
1068 wext_freq_not_found
: ;
1071 /* No channels found? */
1077 /* Set real number of channels specified in creq->channels[] */
1078 creq
->n_channels
= i
;
1080 /* translate "Scan for SSID" request */
1082 if (wrqu
->data
.flags
& IW_SCAN_THIS_ESSID
) {
1083 if (wreq
->essid_len
> IEEE80211_MAX_SSID_LEN
) {
1087 memcpy(creq
->ssids
[0].ssid
, wreq
->essid
, wreq
->essid_len
);
1088 creq
->ssids
[0].ssid_len
= wreq
->essid_len
;
1090 if (wreq
->scan_type
== IW_SCAN_TYPE_PASSIVE
)
1094 for (i
= 0; i
< IEEE80211_NUM_BANDS
; i
++)
1095 if (wiphy
->bands
[i
])
1096 creq
->rates
[i
] = (1 << wiphy
->bands
[i
]->n_bitrates
) - 1;
1098 rdev
->scan_req
= creq
;
1099 err
= rdev_scan(rdev
, creq
);
1101 rdev
->scan_req
= NULL
;
1102 /* creq will be freed below */
1104 nl80211_send_scan_start(rdev
, dev
->ieee80211_ptr
);
1105 /* creq now owned by driver */
1111 cfg80211_unlock_rdev(rdev
);
1114 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan
);
1116 static void ieee80211_scan_add_ies(struct iw_request_info
*info
,
1117 const struct cfg80211_bss_ies
*ies
,
1118 char **current_ev
, char *end_buf
)
1120 const u8
*pos
, *end
, *next
;
1121 struct iw_event iwe
;
1127 * If needed, fragment the IEs buffer (at IE boundaries) into short
1128 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1131 end
= pos
+ ies
->len
;
1133 while (end
- pos
> IW_GENERIC_IE_MAX
) {
1134 next
= pos
+ 2 + pos
[1];
1135 while (next
+ 2 + next
[1] - pos
< IW_GENERIC_IE_MAX
)
1136 next
= next
+ 2 + next
[1];
1138 memset(&iwe
, 0, sizeof(iwe
));
1139 iwe
.cmd
= IWEVGENIE
;
1140 iwe
.u
.data
.length
= next
- pos
;
1141 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
1149 memset(&iwe
, 0, sizeof(iwe
));
1150 iwe
.cmd
= IWEVGENIE
;
1151 iwe
.u
.data
.length
= end
- pos
;
1152 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
1158 static inline unsigned int elapsed_jiffies_msecs(unsigned long start
)
1160 unsigned long end
= jiffies
;
1163 return jiffies_to_msecs(end
- start
);
1165 return jiffies_to_msecs(end
+ (MAX_JIFFY_OFFSET
- start
) + 1);
1169 ieee80211_bss(struct wiphy
*wiphy
, struct iw_request_info
*info
,
1170 struct cfg80211_internal_bss
*bss
, char *current_ev
,
1173 const struct cfg80211_bss_ies
*ies
;
1174 struct iw_event iwe
;
1178 bool ismesh
= false;
1180 memset(&iwe
, 0, sizeof(iwe
));
1181 iwe
.cmd
= SIOCGIWAP
;
1182 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
1183 memcpy(iwe
.u
.ap_addr
.sa_data
, bss
->pub
.bssid
, ETH_ALEN
);
1184 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
1187 memset(&iwe
, 0, sizeof(iwe
));
1188 iwe
.cmd
= SIOCGIWFREQ
;
1189 iwe
.u
.freq
.m
= ieee80211_frequency_to_channel(bss
->pub
.channel
->center_freq
);
1191 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
1194 memset(&iwe
, 0, sizeof(iwe
));
1195 iwe
.cmd
= SIOCGIWFREQ
;
1196 iwe
.u
.freq
.m
= bss
->pub
.channel
->center_freq
;
1198 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
1201 if (wiphy
->signal_type
!= CFG80211_SIGNAL_TYPE_NONE
) {
1202 memset(&iwe
, 0, sizeof(iwe
));
1204 iwe
.u
.qual
.updated
= IW_QUAL_LEVEL_UPDATED
|
1205 IW_QUAL_NOISE_INVALID
|
1206 IW_QUAL_QUAL_UPDATED
;
1207 switch (wiphy
->signal_type
) {
1208 case CFG80211_SIGNAL_TYPE_MBM
:
1209 sig
= bss
->pub
.signal
/ 100;
1210 iwe
.u
.qual
.level
= sig
;
1211 iwe
.u
.qual
.updated
|= IW_QUAL_DBM
;
1212 if (sig
< -110) /* rather bad */
1214 else if (sig
> -40) /* perfect */
1216 /* will give a range of 0 .. 70 */
1217 iwe
.u
.qual
.qual
= sig
+ 110;
1219 case CFG80211_SIGNAL_TYPE_UNSPEC
:
1220 iwe
.u
.qual
.level
= bss
->pub
.signal
;
1221 /* will give range 0 .. 100 */
1222 iwe
.u
.qual
.qual
= bss
->pub
.signal
;
1228 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
1229 &iwe
, IW_EV_QUAL_LEN
);
1232 memset(&iwe
, 0, sizeof(iwe
));
1233 iwe
.cmd
= SIOCGIWENCODE
;
1234 if (bss
->pub
.capability
& WLAN_CAPABILITY_PRIVACY
)
1235 iwe
.u
.data
.flags
= IW_ENCODE_ENABLED
| IW_ENCODE_NOKEY
;
1237 iwe
.u
.data
.flags
= IW_ENCODE_DISABLED
;
1238 iwe
.u
.data
.length
= 0;
1239 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1243 ies
= rcu_dereference(bss
->pub
.ies
);
1252 while (ies
&& rem
>= 2) {
1254 if (ie
[1] > rem
- 2)
1259 memset(&iwe
, 0, sizeof(iwe
));
1260 iwe
.cmd
= SIOCGIWESSID
;
1261 iwe
.u
.data
.length
= ie
[1];
1262 iwe
.u
.data
.flags
= 1;
1263 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1264 &iwe
, (u8
*)ie
+ 2);
1266 case WLAN_EID_MESH_ID
:
1267 memset(&iwe
, 0, sizeof(iwe
));
1268 iwe
.cmd
= SIOCGIWESSID
;
1269 iwe
.u
.data
.length
= ie
[1];
1270 iwe
.u
.data
.flags
= 1;
1271 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1272 &iwe
, (u8
*)ie
+ 2);
1274 case WLAN_EID_MESH_CONFIG
:
1276 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
1278 buf
= kmalloc(50, GFP_ATOMIC
);
1282 memset(&iwe
, 0, sizeof(iwe
));
1283 iwe
.cmd
= IWEVCUSTOM
;
1284 sprintf(buf
, "Mesh Network Path Selection Protocol ID: "
1286 iwe
.u
.data
.length
= strlen(buf
);
1287 current_ev
= iwe_stream_add_point(info
, current_ev
,
1290 sprintf(buf
, "Path Selection Metric ID: 0x%02X",
1292 iwe
.u
.data
.length
= strlen(buf
);
1293 current_ev
= iwe_stream_add_point(info
, current_ev
,
1296 sprintf(buf
, "Congestion Control Mode ID: 0x%02X",
1298 iwe
.u
.data
.length
= strlen(buf
);
1299 current_ev
= iwe_stream_add_point(info
, current_ev
,
1302 sprintf(buf
, "Synchronization ID: 0x%02X", cfg
[3]);
1303 iwe
.u
.data
.length
= strlen(buf
);
1304 current_ev
= iwe_stream_add_point(info
, current_ev
,
1307 sprintf(buf
, "Authentication ID: 0x%02X", cfg
[4]);
1308 iwe
.u
.data
.length
= strlen(buf
);
1309 current_ev
= iwe_stream_add_point(info
, current_ev
,
1312 sprintf(buf
, "Formation Info: 0x%02X", cfg
[5]);
1313 iwe
.u
.data
.length
= strlen(buf
);
1314 current_ev
= iwe_stream_add_point(info
, current_ev
,
1317 sprintf(buf
, "Capabilities: 0x%02X", cfg
[6]);
1318 iwe
.u
.data
.length
= strlen(buf
);
1319 current_ev
= iwe_stream_add_point(info
, current_ev
,
1324 case WLAN_EID_SUPP_RATES
:
1325 case WLAN_EID_EXT_SUPP_RATES
:
1326 /* display all supported rates in readable format */
1327 p
= current_ev
+ iwe_stream_lcp_len(info
);
1329 memset(&iwe
, 0, sizeof(iwe
));
1330 iwe
.cmd
= SIOCGIWRATE
;
1331 /* Those two flags are ignored... */
1332 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
1334 for (i
= 0; i
< ie
[1]; i
++) {
1335 iwe
.u
.bitrate
.value
=
1336 ((ie
[i
+ 2] & 0x7f) * 500000);
1337 p
= iwe_stream_add_value(info
, current_ev
, p
,
1338 end_buf
, &iwe
, IW_EV_PARAM_LEN
);
1347 if (bss
->pub
.capability
& (WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_IBSS
) ||
1349 memset(&iwe
, 0, sizeof(iwe
));
1350 iwe
.cmd
= SIOCGIWMODE
;
1352 iwe
.u
.mode
= IW_MODE_MESH
;
1353 else if (bss
->pub
.capability
& WLAN_CAPABILITY_ESS
)
1354 iwe
.u
.mode
= IW_MODE_MASTER
;
1356 iwe
.u
.mode
= IW_MODE_ADHOC
;
1357 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
1358 &iwe
, IW_EV_UINT_LEN
);
1361 buf
= kmalloc(31, GFP_ATOMIC
);
1363 memset(&iwe
, 0, sizeof(iwe
));
1364 iwe
.cmd
= IWEVCUSTOM
;
1365 sprintf(buf
, "tsf=%016llx", (unsigned long long)(bss
->pub
.tsf
));
1366 iwe
.u
.data
.length
= strlen(buf
);
1367 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1369 memset(&iwe
, 0, sizeof(iwe
));
1370 iwe
.cmd
= IWEVCUSTOM
;
1371 sprintf(buf
, " Last beacon: %ums ago",
1372 elapsed_jiffies_msecs(bss
->ts
));
1373 iwe
.u
.data
.length
= strlen(buf
);
1374 current_ev
= iwe_stream_add_point(info
, current_ev
,
1375 end_buf
, &iwe
, buf
);
1379 ieee80211_scan_add_ies(info
, ies
, ¤t_ev
, end_buf
);
1386 static int ieee80211_scan_results(struct cfg80211_registered_device
*dev
,
1387 struct iw_request_info
*info
,
1388 char *buf
, size_t len
)
1390 char *current_ev
= buf
;
1391 char *end_buf
= buf
+ len
;
1392 struct cfg80211_internal_bss
*bss
;
1394 spin_lock_bh(&dev
->bss_lock
);
1395 cfg80211_bss_expire(dev
);
1397 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
1398 if (buf
+ len
- current_ev
<= IW_EV_ADDR_LEN
) {
1399 spin_unlock_bh(&dev
->bss_lock
);
1402 current_ev
= ieee80211_bss(&dev
->wiphy
, info
, bss
,
1403 current_ev
, end_buf
);
1405 spin_unlock_bh(&dev
->bss_lock
);
1406 return current_ev
- buf
;
1410 int cfg80211_wext_giwscan(struct net_device
*dev
,
1411 struct iw_request_info
*info
,
1412 struct iw_point
*data
, char *extra
)
1414 struct cfg80211_registered_device
*rdev
;
1417 if (!netif_running(dev
))
1420 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
1423 return PTR_ERR(rdev
);
1425 if (rdev
->scan_req
) {
1430 res
= ieee80211_scan_results(rdev
, info
, extra
, data
->length
);
1438 cfg80211_unlock_rdev(rdev
);
1441 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan
);