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 ies
= (void *)rcu_access_pointer(bss
->pub
.beacon_ies
);
36 kfree_rcu(ies
, rcu_head
);
37 ies
= (void *)rcu_access_pointer(bss
->pub
.proberesp_ies
);
39 kfree_rcu(ies
, rcu_head
);
44 /* must hold dev->bss_lock! */
45 static void __cfg80211_unlink_bss(struct cfg80211_registered_device
*dev
,
46 struct cfg80211_internal_bss
*bss
)
48 list_del_init(&bss
->list
);
49 rb_erase(&bss
->rbn
, &dev
->bss_tree
);
50 kref_put(&bss
->ref
, bss_release
);
53 /* must hold dev->bss_lock! */
54 static void __cfg80211_bss_expire(struct cfg80211_registered_device
*dev
,
55 unsigned long expire_time
)
57 struct cfg80211_internal_bss
*bss
, *tmp
;
60 list_for_each_entry_safe(bss
, tmp
, &dev
->bss_list
, list
) {
61 if (atomic_read(&bss
->hold
))
63 if (!time_after(expire_time
, bss
->ts
))
66 __cfg80211_unlink_bss(dev
, bss
);
71 dev
->bss_generation
++;
74 void ___cfg80211_scan_done(struct cfg80211_registered_device
*rdev
, bool leak
)
76 struct cfg80211_scan_request
*request
;
77 struct wireless_dev
*wdev
;
78 #ifdef CONFIG_CFG80211_WEXT
79 union iwreq_data wrqu
;
82 ASSERT_RDEV_LOCK(rdev
);
84 request
= rdev
->scan_req
;
92 * This must be before sending the other events!
93 * Otherwise, wpa_supplicant gets completely confused with
97 cfg80211_sme_scan_done(wdev
->netdev
);
99 if (request
->aborted
) {
100 nl80211_send_scan_aborted(rdev
, wdev
);
102 if (request
->flags
& NL80211_SCAN_FLAG_FLUSH
) {
103 /* flush entries from previous scans */
104 spin_lock_bh(&rdev
->bss_lock
);
105 __cfg80211_bss_expire(rdev
, request
->scan_start
);
106 spin_unlock_bh(&rdev
->bss_lock
);
108 nl80211_send_scan_done(rdev
, wdev
);
111 #ifdef CONFIG_CFG80211_WEXT
112 if (wdev
->netdev
&& !request
->aborted
) {
113 memset(&wrqu
, 0, sizeof(wrqu
));
115 wireless_send_event(wdev
->netdev
, SIOCGIWSCAN
, &wrqu
, NULL
);
120 dev_put(wdev
->netdev
);
122 rdev
->scan_req
= NULL
;
125 * OK. If this is invoked with "leak" then we can't
126 * free this ... but we've cleaned it up anyway. The
127 * driver failed to call the scan_done callback, so
128 * all bets are off, it might still be trying to use
129 * the scan request or not ... if it accesses the dev
130 * in there (it shouldn't anyway) then it may crash.
136 void __cfg80211_scan_done(struct work_struct
*wk
)
138 struct cfg80211_registered_device
*rdev
;
140 rdev
= container_of(wk
, struct cfg80211_registered_device
,
143 cfg80211_lock_rdev(rdev
);
144 ___cfg80211_scan_done(rdev
, false);
145 cfg80211_unlock_rdev(rdev
);
148 void cfg80211_scan_done(struct cfg80211_scan_request
*request
, bool aborted
)
150 trace_cfg80211_scan_done(request
, aborted
);
151 WARN_ON(request
!= wiphy_to_dev(request
->wiphy
)->scan_req
);
153 request
->aborted
= aborted
;
154 queue_work(cfg80211_wq
, &wiphy_to_dev(request
->wiphy
)->scan_done_wk
);
156 EXPORT_SYMBOL(cfg80211_scan_done
);
158 void __cfg80211_sched_scan_results(struct work_struct
*wk
)
160 struct cfg80211_registered_device
*rdev
;
161 struct cfg80211_sched_scan_request
*request
;
163 rdev
= container_of(wk
, struct cfg80211_registered_device
,
164 sched_scan_results_wk
);
166 request
= rdev
->sched_scan_req
;
168 mutex_lock(&rdev
->sched_scan_mtx
);
170 /* we don't have sched_scan_req anymore if the scan is stopping */
172 if (request
->flags
& NL80211_SCAN_FLAG_FLUSH
) {
173 /* flush entries from previous scans */
174 spin_lock_bh(&rdev
->bss_lock
);
175 __cfg80211_bss_expire(rdev
, request
->scan_start
);
176 spin_unlock_bh(&rdev
->bss_lock
);
177 request
->scan_start
=
178 jiffies
+ msecs_to_jiffies(request
->interval
);
180 nl80211_send_sched_scan_results(rdev
, request
->dev
);
183 mutex_unlock(&rdev
->sched_scan_mtx
);
186 void cfg80211_sched_scan_results(struct wiphy
*wiphy
)
188 trace_cfg80211_sched_scan_results(wiphy
);
189 /* ignore if we're not scanning */
190 if (wiphy_to_dev(wiphy
)->sched_scan_req
)
191 queue_work(cfg80211_wq
,
192 &wiphy_to_dev(wiphy
)->sched_scan_results_wk
);
194 EXPORT_SYMBOL(cfg80211_sched_scan_results
);
196 void cfg80211_sched_scan_stopped(struct wiphy
*wiphy
)
198 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
200 trace_cfg80211_sched_scan_stopped(wiphy
);
202 mutex_lock(&rdev
->sched_scan_mtx
);
203 __cfg80211_stop_sched_scan(rdev
, true);
204 mutex_unlock(&rdev
->sched_scan_mtx
);
206 EXPORT_SYMBOL(cfg80211_sched_scan_stopped
);
208 int __cfg80211_stop_sched_scan(struct cfg80211_registered_device
*rdev
,
209 bool driver_initiated
)
211 struct net_device
*dev
;
213 lockdep_assert_held(&rdev
->sched_scan_mtx
);
215 if (!rdev
->sched_scan_req
)
218 dev
= rdev
->sched_scan_req
->dev
;
220 if (!driver_initiated
) {
221 int err
= rdev_sched_scan_stop(rdev
, dev
);
226 nl80211_send_sched_scan(rdev
, dev
, NL80211_CMD_SCHED_SCAN_STOPPED
);
228 kfree(rdev
->sched_scan_req
);
229 rdev
->sched_scan_req
= NULL
;
234 /* must hold dev->bss_lock! */
235 void cfg80211_bss_age(struct cfg80211_registered_device
*dev
,
236 unsigned long age_secs
)
238 struct cfg80211_internal_bss
*bss
;
239 unsigned long age_jiffies
= msecs_to_jiffies(age_secs
* MSEC_PER_SEC
);
241 list_for_each_entry(bss
, &dev
->bss_list
, list
)
242 bss
->ts
-= age_jiffies
;
245 void cfg80211_bss_expire(struct cfg80211_registered_device
*dev
)
247 __cfg80211_bss_expire(dev
, jiffies
- IEEE80211_SCAN_RESULT_EXPIRE
);
250 const u8
*cfg80211_find_ie(u8 eid
, const u8
*ies
, int len
)
252 while (len
> 2 && ies
[0] != eid
) {
258 if (len
< 2 + ies
[1])
262 EXPORT_SYMBOL(cfg80211_find_ie
);
264 const u8
*cfg80211_find_vendor_ie(unsigned int oui
, u8 oui_type
,
265 const u8
*ies
, int len
)
267 struct ieee80211_vendor_ie
*ie
;
268 const u8
*pos
= ies
, *end
= ies
+ len
;
272 pos
= cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC
, pos
,
277 if (end
- pos
< sizeof(*ie
))
280 ie
= (struct ieee80211_vendor_ie
*)pos
;
281 ie_oui
= ie
->oui
[0] << 16 | ie
->oui
[1] << 8 | ie
->oui
[2];
282 if (ie_oui
== oui
&& ie
->oui_type
== oui_type
)
289 EXPORT_SYMBOL(cfg80211_find_vendor_ie
);
291 static int cmp_ies(u8 num
, const u8
*ies1
, int len1
, const u8
*ies2
, int len2
)
293 const u8
*ie1
= cfg80211_find_ie(num
, ies1
, len1
);
294 const u8
*ie2
= cfg80211_find_ie(num
, ies2
, len2
);
296 /* equal if both missing */
299 /* sort missing IE before (left of) present IE */
305 /* sort by length first, then by contents */
306 if (ie1
[1] != ie2
[1])
307 return ie2
[1] - ie1
[1];
308 return memcmp(ie1
+ 2, ie2
+ 2, ie1
[1]);
311 static bool is_bss(struct cfg80211_bss
*a
, const u8
*bssid
,
312 const u8
*ssid
, size_t ssid_len
)
314 const struct cfg80211_bss_ies
*ies
;
317 if (bssid
&& !ether_addr_equal(a
->bssid
, bssid
))
323 ies
= rcu_access_pointer(a
->ies
);
326 ssidie
= cfg80211_find_ie(WLAN_EID_SSID
, ies
->data
, ies
->len
);
329 if (ssidie
[1] != ssid_len
)
331 return memcmp(ssidie
+ 2, ssid
, ssid_len
) == 0;
334 static bool is_mesh_bss(struct cfg80211_bss
*a
)
336 const struct cfg80211_bss_ies
*ies
;
339 if (!WLAN_CAPABILITY_IS_STA_BSS(a
->capability
))
342 ies
= rcu_access_pointer(a
->ies
);
346 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
, ies
->data
, ies
->len
);
350 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
, ies
->data
, ies
->len
);
357 static bool is_mesh(struct cfg80211_bss
*a
,
358 const u8
*meshid
, size_t meshidlen
,
361 const struct cfg80211_bss_ies
*ies
;
364 if (!WLAN_CAPABILITY_IS_STA_BSS(a
->capability
))
367 ies
= rcu_access_pointer(a
->ies
);
371 ie
= cfg80211_find_ie(WLAN_EID_MESH_ID
, ies
->data
, ies
->len
);
374 if (ie
[1] != meshidlen
)
376 if (memcmp(ie
+ 2, meshid
, meshidlen
))
379 ie
= cfg80211_find_ie(WLAN_EID_MESH_CONFIG
, ies
->data
, ies
->len
);
382 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
386 * Ignore mesh capability (last two bytes of the IE) when
387 * comparing since that may differ between stations taking
388 * part in the same mesh.
390 return memcmp(ie
+ 2, meshcfg
,
391 sizeof(struct ieee80211_meshconf_ie
) - 2) == 0;
394 static int cmp_bss_core(struct cfg80211_bss
*a
, struct cfg80211_bss
*b
)
396 const struct cfg80211_bss_ies
*a_ies
, *b_ies
;
399 if (a
->channel
!= b
->channel
)
400 return b
->channel
->center_freq
- a
->channel
->center_freq
;
402 if (is_mesh_bss(a
) && is_mesh_bss(b
)) {
403 a_ies
= rcu_access_pointer(a
->ies
);
406 b_ies
= rcu_access_pointer(b
->ies
);
410 r
= cmp_ies(WLAN_EID_MESH_ID
,
411 a_ies
->data
, a_ies
->len
,
412 b_ies
->data
, b_ies
->len
);
415 return cmp_ies(WLAN_EID_MESH_CONFIG
,
416 a_ies
->data
, a_ies
->len
,
417 b_ies
->data
, b_ies
->len
);
421 * we can't use compare_ether_addr here since we need a < > operator.
422 * The binary return value of compare_ether_addr isn't enough
424 return memcmp(a
->bssid
, b
->bssid
, sizeof(a
->bssid
));
427 static int cmp_bss(struct cfg80211_bss
*a
,
428 struct cfg80211_bss
*b
)
430 const struct cfg80211_bss_ies
*a_ies
, *b_ies
;
433 r
= cmp_bss_core(a
, b
);
437 a_ies
= rcu_access_pointer(a
->ies
);
440 b_ies
= rcu_access_pointer(b
->ies
);
444 return cmp_ies(WLAN_EID_SSID
,
445 a_ies
->data
, a_ies
->len
,
446 b_ies
->data
, b_ies
->len
);
449 static int cmp_hidden_bss(struct cfg80211_bss
*a
, struct cfg80211_bss
*b
)
451 const struct cfg80211_bss_ies
*a_ies
, *b_ies
;
457 r
= cmp_bss_core(a
, b
);
461 a_ies
= rcu_access_pointer(a
->ies
);
464 b_ies
= rcu_access_pointer(b
->ies
);
468 ie1
= cfg80211_find_ie(WLAN_EID_SSID
, a_ies
->data
, a_ies
->len
);
469 ie2
= cfg80211_find_ie(WLAN_EID_SSID
, b_ies
->data
, b_ies
->len
);
472 * Key comparator must use same algorithm in any rb-tree
473 * search function (order is important), otherwise ordering
474 * of items in the tree is broken and search gives incorrect
475 * results. This code uses same order as cmp_ies() does.
477 * Note that due to the differring behaviour with hidden SSIDs
478 * this function only works when "b" is the tree element and
479 * "a" is the key we're looking for.
482 /* sort missing IE before (left of) present IE */
488 /* zero-size SSID is used as an indication of the hidden bss */
492 /* sort by length first, then by contents */
493 if (ie1
[1] != ie2
[1])
494 return ie2
[1] - ie1
[1];
497 * zeroed SSID ie is another indication of a hidden bss;
498 * if it isn't zeroed just return the regular sort value
499 * to find the next candidate
501 for (i
= 0; i
< ie2
[1]; i
++)
503 return memcmp(ie1
+ 2, ie2
+ 2, ie1
[1]);
508 struct cfg80211_bss
*cfg80211_get_bss(struct wiphy
*wiphy
,
509 struct ieee80211_channel
*channel
,
511 const u8
*ssid
, size_t ssid_len
,
512 u16 capa_mask
, u16 capa_val
)
514 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
515 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
516 unsigned long now
= jiffies
;
518 trace_cfg80211_get_bss(wiphy
, channel
, bssid
, ssid
, ssid_len
, capa_mask
,
521 spin_lock_bh(&dev
->bss_lock
);
523 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
524 if ((bss
->pub
.capability
& capa_mask
) != capa_val
)
526 if (channel
&& bss
->pub
.channel
!= channel
)
528 /* Don't get expired BSS structs */
529 if (time_after(now
, bss
->ts
+ IEEE80211_SCAN_RESULT_EXPIRE
) &&
530 !atomic_read(&bss
->hold
))
532 if (is_bss(&bss
->pub
, bssid
, ssid
, ssid_len
)) {
539 spin_unlock_bh(&dev
->bss_lock
);
542 trace_cfg80211_return_bss(&res
->pub
);
545 EXPORT_SYMBOL(cfg80211_get_bss
);
547 struct cfg80211_bss
*cfg80211_get_mesh(struct wiphy
*wiphy
,
548 struct ieee80211_channel
*channel
,
549 const u8
*meshid
, size_t meshidlen
,
552 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
553 struct cfg80211_internal_bss
*bss
, *res
= NULL
;
555 spin_lock_bh(&dev
->bss_lock
);
557 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
558 if (channel
&& bss
->pub
.channel
!= channel
)
560 if (is_mesh(&bss
->pub
, meshid
, meshidlen
, meshcfg
)) {
567 spin_unlock_bh(&dev
->bss_lock
);
572 EXPORT_SYMBOL(cfg80211_get_mesh
);
575 static void rb_insert_bss(struct cfg80211_registered_device
*dev
,
576 struct cfg80211_internal_bss
*bss
)
578 struct rb_node
**p
= &dev
->bss_tree
.rb_node
;
579 struct rb_node
*parent
= NULL
;
580 struct cfg80211_internal_bss
*tbss
;
585 tbss
= rb_entry(parent
, struct cfg80211_internal_bss
, rbn
);
587 cmp
= cmp_bss(&bss
->pub
, &tbss
->pub
);
590 /* will sort of leak this BSS */
600 rb_link_node(&bss
->rbn
, parent
, p
);
601 rb_insert_color(&bss
->rbn
, &dev
->bss_tree
);
604 static struct cfg80211_internal_bss
*
605 rb_find_bss(struct cfg80211_registered_device
*dev
,
606 struct cfg80211_internal_bss
*res
)
608 struct rb_node
*n
= dev
->bss_tree
.rb_node
;
609 struct cfg80211_internal_bss
*bss
;
613 bss
= rb_entry(n
, struct cfg80211_internal_bss
, rbn
);
614 r
= cmp_bss(&res
->pub
, &bss
->pub
);
627 static struct cfg80211_internal_bss
*
628 rb_find_hidden_bss(struct cfg80211_registered_device
*dev
,
629 struct cfg80211_internal_bss
*res
)
631 struct rb_node
*n
= dev
->bss_tree
.rb_node
;
632 struct cfg80211_internal_bss
*bss
;
636 bss
= rb_entry(n
, struct cfg80211_internal_bss
, rbn
);
637 r
= cmp_hidden_bss(&res
->pub
, &bss
->pub
);
651 copy_hidden_ies(struct cfg80211_internal_bss
*res
,
652 struct cfg80211_internal_bss
*hidden
)
654 const struct cfg80211_bss_ies
*ies
;
656 if (rcu_access_pointer(res
->pub
.beacon_ies
))
659 ies
= rcu_access_pointer(hidden
->pub
.beacon_ies
);
663 ies
= kmemdup(ies
, sizeof(*ies
) + ies
->len
, GFP_ATOMIC
);
666 rcu_assign_pointer(res
->pub
.beacon_ies
, ies
);
669 static struct cfg80211_internal_bss
*
670 cfg80211_bss_update(struct cfg80211_registered_device
*dev
,
671 struct cfg80211_internal_bss
*tmp
)
673 struct cfg80211_internal_bss
*found
= NULL
;
675 if (WARN_ON(!tmp
->pub
.channel
))
680 spin_lock_bh(&dev
->bss_lock
);
682 if (WARN_ON(!rcu_access_pointer(tmp
->pub
.ies
))) {
683 spin_unlock_bh(&dev
->bss_lock
);
687 found
= rb_find_bss(dev
, tmp
);
690 found
->pub
.beacon_interval
= tmp
->pub
.beacon_interval
;
691 found
->pub
.tsf
= tmp
->pub
.tsf
;
692 found
->pub
.signal
= tmp
->pub
.signal
;
693 found
->pub
.capability
= tmp
->pub
.capability
;
697 if (rcu_access_pointer(tmp
->pub
.proberesp_ies
)) {
698 const struct cfg80211_bss_ies
*old
;
700 old
= rcu_access_pointer(found
->pub
.proberesp_ies
);
702 rcu_assign_pointer(found
->pub
.proberesp_ies
,
703 tmp
->pub
.proberesp_ies
);
704 /* Override possible earlier Beacon frame IEs */
705 rcu_assign_pointer(found
->pub
.ies
,
706 tmp
->pub
.proberesp_ies
);
708 kfree_rcu((struct cfg80211_bss_ies
*)old
,
710 } else if (rcu_access_pointer(tmp
->pub
.beacon_ies
)) {
711 const struct cfg80211_bss_ies
*old
, *ies
;
713 old
= rcu_access_pointer(found
->pub
.beacon_ies
);
714 ies
= rcu_access_pointer(found
->pub
.ies
);
716 rcu_assign_pointer(found
->pub
.beacon_ies
,
717 tmp
->pub
.beacon_ies
);
719 /* Override IEs if they were from a beacon before */
721 rcu_assign_pointer(found
->pub
.ies
,
722 tmp
->pub
.beacon_ies
);
725 kfree_rcu((struct cfg80211_bss_ies
*)old
,
729 struct cfg80211_internal_bss
*new;
730 struct cfg80211_internal_bss
*hidden
;
731 struct cfg80211_bss_ies
*ies
;
733 /* First check if the beacon is a probe response from
734 * a hidden bss. If so, copy beacon ies (with nullified
735 * ssid) into the probe response bss entry (with real ssid).
736 * It is required basically for PSM implementation
737 * (probe responses do not contain tim ie) */
739 /* TODO: The code is not trying to update existing probe
740 * response bss entries when beacon ies are
741 * getting changed. */
742 hidden
= rb_find_hidden_bss(dev
, tmp
);
744 copy_hidden_ies(tmp
, hidden
);
747 * create a copy -- the "res" variable that is passed in
748 * is allocated on the stack since it's not needed in the
749 * more common case of an update
751 new = kzalloc(sizeof(*new) + dev
->wiphy
.bss_priv_size
,
754 ies
= (void *)rcu_dereference(tmp
->pub
.beacon_ies
);
756 kfree_rcu(ies
, rcu_head
);
757 ies
= (void *)rcu_dereference(tmp
->pub
.proberesp_ies
);
759 kfree_rcu(ies
, rcu_head
);
760 spin_unlock_bh(&dev
->bss_lock
);
763 memcpy(new, tmp
, sizeof(*new));
764 kref_init(&new->ref
);
765 list_add_tail(&new->list
, &dev
->bss_list
);
766 rb_insert_bss(dev
, new);
770 dev
->bss_generation
++;
771 spin_unlock_bh(&dev
->bss_lock
);
773 kref_get(&found
->ref
);
777 static struct ieee80211_channel
*
778 cfg80211_get_bss_channel(struct wiphy
*wiphy
, const u8
*ie
, size_t ielen
,
779 struct ieee80211_channel
*channel
)
783 int channel_number
= -1;
785 tmp
= cfg80211_find_ie(WLAN_EID_DS_PARAMS
, ie
, ielen
);
786 if (tmp
&& tmp
[1] == 1) {
787 channel_number
= tmp
[2];
789 tmp
= cfg80211_find_ie(WLAN_EID_HT_OPERATION
, ie
, ielen
);
790 if (tmp
&& tmp
[1] >= sizeof(struct ieee80211_ht_operation
)) {
791 struct ieee80211_ht_operation
*htop
= (void *)(tmp
+ 2);
793 channel_number
= htop
->primary_chan
;
797 if (channel_number
< 0)
800 freq
= ieee80211_channel_to_frequency(channel_number
, channel
->band
);
801 channel
= ieee80211_get_channel(wiphy
, freq
);
804 if (channel
->flags
& IEEE80211_CHAN_DISABLED
)
810 cfg80211_inform_bss(struct wiphy
*wiphy
,
811 struct ieee80211_channel
*channel
,
812 const u8
*bssid
, u64 tsf
, u16 capability
,
813 u16 beacon_interval
, const u8
*ie
, size_t ielen
,
814 s32 signal
, gfp_t gfp
)
816 struct cfg80211_bss_ies
*ies
;
817 struct cfg80211_internal_bss tmp
= {}, *res
;
822 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
823 (signal
< 0 || signal
> 100)))
826 channel
= cfg80211_get_bss_channel(wiphy
, ie
, ielen
, channel
);
830 memcpy(tmp
.pub
.bssid
, bssid
, ETH_ALEN
);
831 tmp
.pub
.channel
= channel
;
832 tmp
.pub
.signal
= signal
;
834 tmp
.pub
.beacon_interval
= beacon_interval
;
835 tmp
.pub
.capability
= capability
;
837 * Since we do not know here whether the IEs are from a Beacon or Probe
838 * Response frame, we need to pick one of the options and only use it
839 * with the driver that does not provide the full Beacon/Probe Response
840 * frame. Use Beacon frame pointer to avoid indicating that this should
841 * override the iies pointer should we have received an earlier
842 * indication of Probe Response data.
844 * The initial buffer for the IEs is allocated with the BSS entry and
845 * is located after the private area.
847 ies
= kmalloc(sizeof(*ies
) + ielen
, gfp
);
851 memcpy(ies
->data
, ie
, ielen
);
853 rcu_assign_pointer(tmp
.pub
.beacon_ies
, ies
);
854 rcu_assign_pointer(tmp
.pub
.ies
, ies
);
856 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), &tmp
);
860 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
861 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
863 trace_cfg80211_return_bss(&res
->pub
);
864 /* cfg80211_bss_update gives us a referenced result */
867 EXPORT_SYMBOL(cfg80211_inform_bss
);
869 struct cfg80211_bss
*
870 cfg80211_inform_bss_frame(struct wiphy
*wiphy
,
871 struct ieee80211_channel
*channel
,
872 struct ieee80211_mgmt
*mgmt
, size_t len
,
873 s32 signal
, gfp_t gfp
)
875 struct cfg80211_internal_bss tmp
= {}, *res
;
876 struct cfg80211_bss_ies
*ies
;
877 size_t ielen
= len
- offsetof(struct ieee80211_mgmt
,
878 u
.probe_resp
.variable
);
880 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt
, u
.probe_resp
.variable
) !=
881 offsetof(struct ieee80211_mgmt
, u
.beacon
.variable
));
883 trace_cfg80211_inform_bss_frame(wiphy
, channel
, mgmt
, len
, signal
);
891 if (WARN_ON(wiphy
->signal_type
== CFG80211_SIGNAL_TYPE_UNSPEC
&&
892 (signal
< 0 || signal
> 100)))
895 if (WARN_ON(len
< offsetof(struct ieee80211_mgmt
, u
.probe_resp
.variable
)))
898 channel
= cfg80211_get_bss_channel(wiphy
, mgmt
->u
.beacon
.variable
,
903 ies
= kmalloc(sizeof(*ies
) + ielen
, gfp
);
907 memcpy(ies
->data
, mgmt
->u
.probe_resp
.variable
, ielen
);
909 if (ieee80211_is_probe_resp(mgmt
->frame_control
))
910 rcu_assign_pointer(tmp
.pub
.proberesp_ies
, ies
);
912 rcu_assign_pointer(tmp
.pub
.beacon_ies
, ies
);
913 rcu_assign_pointer(tmp
.pub
.ies
, ies
);
915 memcpy(tmp
.pub
.bssid
, mgmt
->bssid
, ETH_ALEN
);
916 tmp
.pub
.channel
= channel
;
917 tmp
.pub
.signal
= signal
;
918 tmp
.pub
.tsf
= le64_to_cpu(mgmt
->u
.probe_resp
.timestamp
);
919 tmp
.pub
.beacon_interval
= le16_to_cpu(mgmt
->u
.probe_resp
.beacon_int
);
920 tmp
.pub
.capability
= le16_to_cpu(mgmt
->u
.probe_resp
.capab_info
);
922 res
= cfg80211_bss_update(wiphy_to_dev(wiphy
), &tmp
);
926 if (res
->pub
.capability
& WLAN_CAPABILITY_ESS
)
927 regulatory_hint_found_beacon(wiphy
, channel
, gfp
);
929 trace_cfg80211_return_bss(&res
->pub
);
930 /* cfg80211_bss_update gives us a referenced result */
933 EXPORT_SYMBOL(cfg80211_inform_bss_frame
);
935 void cfg80211_ref_bss(struct cfg80211_bss
*pub
)
937 struct cfg80211_internal_bss
*bss
;
942 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
945 EXPORT_SYMBOL(cfg80211_ref_bss
);
947 void cfg80211_put_bss(struct cfg80211_bss
*pub
)
949 struct cfg80211_internal_bss
*bss
;
954 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
955 kref_put(&bss
->ref
, bss_release
);
957 EXPORT_SYMBOL(cfg80211_put_bss
);
959 void cfg80211_unlink_bss(struct wiphy
*wiphy
, struct cfg80211_bss
*pub
)
961 struct cfg80211_registered_device
*dev
= wiphy_to_dev(wiphy
);
962 struct cfg80211_internal_bss
*bss
;
967 bss
= container_of(pub
, struct cfg80211_internal_bss
, pub
);
969 spin_lock_bh(&dev
->bss_lock
);
970 if (!list_empty(&bss
->list
)) {
971 __cfg80211_unlink_bss(dev
, bss
);
972 dev
->bss_generation
++;
974 spin_unlock_bh(&dev
->bss_lock
);
976 EXPORT_SYMBOL(cfg80211_unlink_bss
);
978 #ifdef CONFIG_CFG80211_WEXT
979 int cfg80211_wext_siwscan(struct net_device
*dev
,
980 struct iw_request_info
*info
,
981 union iwreq_data
*wrqu
, char *extra
)
983 struct cfg80211_registered_device
*rdev
;
985 struct iw_scan_req
*wreq
= NULL
;
986 struct cfg80211_scan_request
*creq
= NULL
;
987 int i
, err
, n_channels
= 0;
988 enum ieee80211_band band
;
990 if (!netif_running(dev
))
993 if (wrqu
->data
.length
== sizeof(struct iw_scan_req
))
994 wreq
= (struct iw_scan_req
*)extra
;
996 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
999 return PTR_ERR(rdev
);
1001 if (rdev
->scan_req
) {
1006 wiphy
= &rdev
->wiphy
;
1008 /* Determine number of channels, needed to allocate creq */
1009 if (wreq
&& wreq
->num_channels
)
1010 n_channels
= wreq
->num_channels
;
1012 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
1013 if (wiphy
->bands
[band
])
1014 n_channels
+= wiphy
->bands
[band
]->n_channels
;
1017 creq
= kzalloc(sizeof(*creq
) + sizeof(struct cfg80211_ssid
) +
1018 n_channels
* sizeof(void *),
1025 creq
->wiphy
= wiphy
;
1026 creq
->wdev
= dev
->ieee80211_ptr
;
1027 /* SSIDs come after channels */
1028 creq
->ssids
= (void *)&creq
->channels
[n_channels
];
1029 creq
->n_channels
= n_channels
;
1031 creq
->scan_start
= jiffies
;
1033 /* translate "Scan on frequencies" request */
1035 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
1038 if (!wiphy
->bands
[band
])
1041 for (j
= 0; j
< wiphy
->bands
[band
]->n_channels
; j
++) {
1042 /* ignore disabled channels */
1043 if (wiphy
->bands
[band
]->channels
[j
].flags
&
1044 IEEE80211_CHAN_DISABLED
)
1047 /* If we have a wireless request structure and the
1048 * wireless request specifies frequencies, then search
1049 * for the matching hardware channel.
1051 if (wreq
&& wreq
->num_channels
) {
1053 int wiphy_freq
= wiphy
->bands
[band
]->channels
[j
].center_freq
;
1054 for (k
= 0; k
< wreq
->num_channels
; k
++) {
1055 int wext_freq
= cfg80211_wext_freq(wiphy
, &wreq
->channel_list
[k
]);
1056 if (wext_freq
== wiphy_freq
)
1057 goto wext_freq_found
;
1059 goto wext_freq_not_found
;
1063 creq
->channels
[i
] = &wiphy
->bands
[band
]->channels
[j
];
1065 wext_freq_not_found
: ;
1068 /* No channels found? */
1074 /* Set real number of channels specified in creq->channels[] */
1075 creq
->n_channels
= i
;
1077 /* translate "Scan for SSID" request */
1079 if (wrqu
->data
.flags
& IW_SCAN_THIS_ESSID
) {
1080 if (wreq
->essid_len
> IEEE80211_MAX_SSID_LEN
) {
1084 memcpy(creq
->ssids
[0].ssid
, wreq
->essid
, wreq
->essid_len
);
1085 creq
->ssids
[0].ssid_len
= wreq
->essid_len
;
1087 if (wreq
->scan_type
== IW_SCAN_TYPE_PASSIVE
)
1091 for (i
= 0; i
< IEEE80211_NUM_BANDS
; i
++)
1092 if (wiphy
->bands
[i
])
1093 creq
->rates
[i
] = (1 << wiphy
->bands
[i
]->n_bitrates
) - 1;
1095 rdev
->scan_req
= creq
;
1096 err
= rdev_scan(rdev
, creq
);
1098 rdev
->scan_req
= NULL
;
1099 /* creq will be freed below */
1101 nl80211_send_scan_start(rdev
, dev
->ieee80211_ptr
);
1102 /* creq now owned by driver */
1108 cfg80211_unlock_rdev(rdev
);
1111 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan
);
1113 static void ieee80211_scan_add_ies(struct iw_request_info
*info
,
1114 const struct cfg80211_bss_ies
*ies
,
1115 char **current_ev
, char *end_buf
)
1117 const u8
*pos
, *end
, *next
;
1118 struct iw_event iwe
;
1124 * If needed, fragment the IEs buffer (at IE boundaries) into short
1125 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1128 end
= pos
+ ies
->len
;
1130 while (end
- pos
> IW_GENERIC_IE_MAX
) {
1131 next
= pos
+ 2 + pos
[1];
1132 while (next
+ 2 + next
[1] - pos
< IW_GENERIC_IE_MAX
)
1133 next
= next
+ 2 + next
[1];
1135 memset(&iwe
, 0, sizeof(iwe
));
1136 iwe
.cmd
= IWEVGENIE
;
1137 iwe
.u
.data
.length
= next
- pos
;
1138 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
1146 memset(&iwe
, 0, sizeof(iwe
));
1147 iwe
.cmd
= IWEVGENIE
;
1148 iwe
.u
.data
.length
= end
- pos
;
1149 *current_ev
= iwe_stream_add_point(info
, *current_ev
,
1155 static inline unsigned int elapsed_jiffies_msecs(unsigned long start
)
1157 unsigned long end
= jiffies
;
1160 return jiffies_to_msecs(end
- start
);
1162 return jiffies_to_msecs(end
+ (MAX_JIFFY_OFFSET
- start
) + 1);
1166 ieee80211_bss(struct wiphy
*wiphy
, struct iw_request_info
*info
,
1167 struct cfg80211_internal_bss
*bss
, char *current_ev
,
1170 const struct cfg80211_bss_ies
*ies
;
1171 struct iw_event iwe
;
1175 bool ismesh
= false;
1177 memset(&iwe
, 0, sizeof(iwe
));
1178 iwe
.cmd
= SIOCGIWAP
;
1179 iwe
.u
.ap_addr
.sa_family
= ARPHRD_ETHER
;
1180 memcpy(iwe
.u
.ap_addr
.sa_data
, bss
->pub
.bssid
, ETH_ALEN
);
1181 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
1184 memset(&iwe
, 0, sizeof(iwe
));
1185 iwe
.cmd
= SIOCGIWFREQ
;
1186 iwe
.u
.freq
.m
= ieee80211_frequency_to_channel(bss
->pub
.channel
->center_freq
);
1188 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
1191 memset(&iwe
, 0, sizeof(iwe
));
1192 iwe
.cmd
= SIOCGIWFREQ
;
1193 iwe
.u
.freq
.m
= bss
->pub
.channel
->center_freq
;
1195 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
, &iwe
,
1198 if (wiphy
->signal_type
!= CFG80211_SIGNAL_TYPE_NONE
) {
1199 memset(&iwe
, 0, sizeof(iwe
));
1201 iwe
.u
.qual
.updated
= IW_QUAL_LEVEL_UPDATED
|
1202 IW_QUAL_NOISE_INVALID
|
1203 IW_QUAL_QUAL_UPDATED
;
1204 switch (wiphy
->signal_type
) {
1205 case CFG80211_SIGNAL_TYPE_MBM
:
1206 sig
= bss
->pub
.signal
/ 100;
1207 iwe
.u
.qual
.level
= sig
;
1208 iwe
.u
.qual
.updated
|= IW_QUAL_DBM
;
1209 if (sig
< -110) /* rather bad */
1211 else if (sig
> -40) /* perfect */
1213 /* will give a range of 0 .. 70 */
1214 iwe
.u
.qual
.qual
= sig
+ 110;
1216 case CFG80211_SIGNAL_TYPE_UNSPEC
:
1217 iwe
.u
.qual
.level
= bss
->pub
.signal
;
1218 /* will give range 0 .. 100 */
1219 iwe
.u
.qual
.qual
= bss
->pub
.signal
;
1225 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
1226 &iwe
, IW_EV_QUAL_LEN
);
1229 memset(&iwe
, 0, sizeof(iwe
));
1230 iwe
.cmd
= SIOCGIWENCODE
;
1231 if (bss
->pub
.capability
& WLAN_CAPABILITY_PRIVACY
)
1232 iwe
.u
.data
.flags
= IW_ENCODE_ENABLED
| IW_ENCODE_NOKEY
;
1234 iwe
.u
.data
.flags
= IW_ENCODE_DISABLED
;
1235 iwe
.u
.data
.length
= 0;
1236 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1240 ies
= rcu_dereference(bss
->pub
.ies
);
1249 while (ies
&& rem
>= 2) {
1251 if (ie
[1] > rem
- 2)
1256 memset(&iwe
, 0, sizeof(iwe
));
1257 iwe
.cmd
= SIOCGIWESSID
;
1258 iwe
.u
.data
.length
= ie
[1];
1259 iwe
.u
.data
.flags
= 1;
1260 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1261 &iwe
, (u8
*)ie
+ 2);
1263 case WLAN_EID_MESH_ID
:
1264 memset(&iwe
, 0, sizeof(iwe
));
1265 iwe
.cmd
= SIOCGIWESSID
;
1266 iwe
.u
.data
.length
= ie
[1];
1267 iwe
.u
.data
.flags
= 1;
1268 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1269 &iwe
, (u8
*)ie
+ 2);
1271 case WLAN_EID_MESH_CONFIG
:
1273 if (ie
[1] != sizeof(struct ieee80211_meshconf_ie
))
1275 buf
= kmalloc(50, GFP_ATOMIC
);
1279 memset(&iwe
, 0, sizeof(iwe
));
1280 iwe
.cmd
= IWEVCUSTOM
;
1281 sprintf(buf
, "Mesh Network Path Selection Protocol ID: "
1283 iwe
.u
.data
.length
= strlen(buf
);
1284 current_ev
= iwe_stream_add_point(info
, current_ev
,
1287 sprintf(buf
, "Path Selection Metric ID: 0x%02X",
1289 iwe
.u
.data
.length
= strlen(buf
);
1290 current_ev
= iwe_stream_add_point(info
, current_ev
,
1293 sprintf(buf
, "Congestion Control Mode ID: 0x%02X",
1295 iwe
.u
.data
.length
= strlen(buf
);
1296 current_ev
= iwe_stream_add_point(info
, current_ev
,
1299 sprintf(buf
, "Synchronization ID: 0x%02X", cfg
[3]);
1300 iwe
.u
.data
.length
= strlen(buf
);
1301 current_ev
= iwe_stream_add_point(info
, current_ev
,
1304 sprintf(buf
, "Authentication ID: 0x%02X", cfg
[4]);
1305 iwe
.u
.data
.length
= strlen(buf
);
1306 current_ev
= iwe_stream_add_point(info
, current_ev
,
1309 sprintf(buf
, "Formation Info: 0x%02X", cfg
[5]);
1310 iwe
.u
.data
.length
= strlen(buf
);
1311 current_ev
= iwe_stream_add_point(info
, current_ev
,
1314 sprintf(buf
, "Capabilities: 0x%02X", cfg
[6]);
1315 iwe
.u
.data
.length
= strlen(buf
);
1316 current_ev
= iwe_stream_add_point(info
, current_ev
,
1321 case WLAN_EID_SUPP_RATES
:
1322 case WLAN_EID_EXT_SUPP_RATES
:
1323 /* display all supported rates in readable format */
1324 p
= current_ev
+ iwe_stream_lcp_len(info
);
1326 memset(&iwe
, 0, sizeof(iwe
));
1327 iwe
.cmd
= SIOCGIWRATE
;
1328 /* Those two flags are ignored... */
1329 iwe
.u
.bitrate
.fixed
= iwe
.u
.bitrate
.disabled
= 0;
1331 for (i
= 0; i
< ie
[1]; i
++) {
1332 iwe
.u
.bitrate
.value
=
1333 ((ie
[i
+ 2] & 0x7f) * 500000);
1334 p
= iwe_stream_add_value(info
, current_ev
, p
,
1335 end_buf
, &iwe
, IW_EV_PARAM_LEN
);
1344 if (bss
->pub
.capability
& (WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_IBSS
) ||
1346 memset(&iwe
, 0, sizeof(iwe
));
1347 iwe
.cmd
= SIOCGIWMODE
;
1349 iwe
.u
.mode
= IW_MODE_MESH
;
1350 else if (bss
->pub
.capability
& WLAN_CAPABILITY_ESS
)
1351 iwe
.u
.mode
= IW_MODE_MASTER
;
1353 iwe
.u
.mode
= IW_MODE_ADHOC
;
1354 current_ev
= iwe_stream_add_event(info
, current_ev
, end_buf
,
1355 &iwe
, IW_EV_UINT_LEN
);
1358 buf
= kmalloc(30, GFP_ATOMIC
);
1360 memset(&iwe
, 0, sizeof(iwe
));
1361 iwe
.cmd
= IWEVCUSTOM
;
1362 sprintf(buf
, "tsf=%016llx", (unsigned long long)(bss
->pub
.tsf
));
1363 iwe
.u
.data
.length
= strlen(buf
);
1364 current_ev
= iwe_stream_add_point(info
, current_ev
, end_buf
,
1366 memset(&iwe
, 0, sizeof(iwe
));
1367 iwe
.cmd
= IWEVCUSTOM
;
1368 sprintf(buf
, " Last beacon: %ums ago",
1369 elapsed_jiffies_msecs(bss
->ts
));
1370 iwe
.u
.data
.length
= strlen(buf
);
1371 current_ev
= iwe_stream_add_point(info
, current_ev
,
1372 end_buf
, &iwe
, buf
);
1376 ieee80211_scan_add_ies(info
, ies
, ¤t_ev
, end_buf
);
1383 static int ieee80211_scan_results(struct cfg80211_registered_device
*dev
,
1384 struct iw_request_info
*info
,
1385 char *buf
, size_t len
)
1387 char *current_ev
= buf
;
1388 char *end_buf
= buf
+ len
;
1389 struct cfg80211_internal_bss
*bss
;
1391 spin_lock_bh(&dev
->bss_lock
);
1392 cfg80211_bss_expire(dev
);
1394 list_for_each_entry(bss
, &dev
->bss_list
, list
) {
1395 if (buf
+ len
- current_ev
<= IW_EV_ADDR_LEN
) {
1396 spin_unlock_bh(&dev
->bss_lock
);
1399 current_ev
= ieee80211_bss(&dev
->wiphy
, info
, bss
,
1400 current_ev
, end_buf
);
1402 spin_unlock_bh(&dev
->bss_lock
);
1403 return current_ev
- buf
;
1407 int cfg80211_wext_giwscan(struct net_device
*dev
,
1408 struct iw_request_info
*info
,
1409 struct iw_point
*data
, char *extra
)
1411 struct cfg80211_registered_device
*rdev
;
1414 if (!netif_running(dev
))
1417 rdev
= cfg80211_get_dev_from_ifindex(dev_net(dev
), dev
->ifindex
);
1420 return PTR_ERR(rdev
);
1422 if (rdev
->scan_req
) {
1427 res
= ieee80211_scan_results(rdev
, info
, extra
, data
->length
);
1435 cfg80211_unlock_rdev(rdev
);
1438 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan
);