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_internal_bss *bss;
28 bss = container_of(ref, struct cfg80211_internal_bss, ref);
30 if (WARN_ON(atomic_read(&bss->hold)))
33 if (bss->pub.free_priv)
34 bss->pub.free_priv(&bss->pub);
36 if (bss->beacon_ies_allocated)
37 kfree(bss->pub.beacon_ies);
38 if (bss->proberesp_ies_allocated)
39 kfree(bss->pub.proberesp_ies);
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, u8 *ies1, size_t len1, u8 *ies2, size_t 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)
316 if (bssid && !ether_addr_equal(a->bssid, bssid))
322 ssidie = cfg80211_find_ie(WLAN_EID_SSID,
323 a->information_elements,
324 a->len_information_elements);
327 if (ssidie[1] != ssid_len)
329 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
332 static bool is_mesh_bss(struct cfg80211_bss *a)
336 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
339 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
340 a->information_elements,
341 a->len_information_elements);
345 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
346 a->information_elements,
347 a->len_information_elements);
354 static bool is_mesh(struct cfg80211_bss *a,
355 const u8 *meshid, size_t meshidlen,
360 if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
363 ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
364 a->information_elements,
365 a->len_information_elements);
368 if (ie[1] != meshidlen)
370 if (memcmp(ie + 2, meshid, meshidlen))
373 ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
374 a->information_elements,
375 a->len_information_elements);
378 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
382 * Ignore mesh capability (last two bytes of the IE) when
383 * comparing since that may differ between stations taking
384 * part in the same mesh.
386 return memcmp(ie + 2, meshcfg,
387 sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
390 static int cmp_bss_core(struct cfg80211_bss *a, struct cfg80211_bss *b)
394 if (a->channel != b->channel)
395 return b->channel->center_freq - a->channel->center_freq;
397 if (is_mesh_bss(a) && is_mesh_bss(b)) {
398 r = cmp_ies(WLAN_EID_MESH_ID,
399 a->information_elements,
400 a->len_information_elements,
401 b->information_elements,
402 b->len_information_elements);
405 return cmp_ies(WLAN_EID_MESH_CONFIG,
406 a->information_elements,
407 a->len_information_elements,
408 b->information_elements,
409 b->len_information_elements);
413 * we can't use compare_ether_addr here since we need a < > operator.
414 * The binary return value of compare_ether_addr isn't enough
416 return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
419 static int cmp_bss(struct cfg80211_bss *a,
420 struct cfg80211_bss *b)
424 r = cmp_bss_core(a, b);
428 return cmp_ies(WLAN_EID_SSID,
429 a->information_elements,
430 a->len_information_elements,
431 b->information_elements,
432 b->len_information_elements);
435 static int cmp_hidden_bss(struct cfg80211_bss *a, struct cfg80211_bss *b)
442 r = cmp_bss_core(a, b);
446 ie1 = cfg80211_find_ie(WLAN_EID_SSID,
447 a->information_elements,
448 a->len_information_elements);
449 ie2 = cfg80211_find_ie(WLAN_EID_SSID,
450 b->information_elements,
451 b->len_information_elements);
454 * Key comparator must use same algorithm in any rb-tree
455 * search function (order is important), otherwise ordering
456 * of items in the tree is broken and search gives incorrect
457 * results. This code uses same order as cmp_ies() does.
459 * Note that due to the differring behaviour with hidden SSIDs
460 * this function only works when "b" is the tree element and
461 * "a" is the key we're looking for.
464 /* sort missing IE before (left of) present IE */
470 /* zero-size SSID is used as an indication of the hidden bss */
474 /* sort by length first, then by contents */
475 if (ie1[1] != ie2[1])
476 return ie2[1] - ie1[1];
479 * zeroed SSID ie is another indication of a hidden bss;
480 * if it isn't zeroed just return the regular sort value
481 * to find the next candidate
483 for (i = 0; i < ie2[1]; i++)
485 return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
490 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
491 struct ieee80211_channel *channel,
493 const u8 *ssid, size_t ssid_len,
494 u16 capa_mask, u16 capa_val)
496 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
497 struct cfg80211_internal_bss *bss, *res = NULL;
498 unsigned long now = jiffies;
500 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
503 spin_lock_bh(&dev->bss_lock);
505 list_for_each_entry(bss, &dev->bss_list, list) {
506 if ((bss->pub.capability & capa_mask) != capa_val)
508 if (channel && bss->pub.channel != channel)
510 /* Don't get expired BSS structs */
511 if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
512 !atomic_read(&bss->hold))
514 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
521 spin_unlock_bh(&dev->bss_lock);
524 trace_cfg80211_return_bss(&res->pub);
527 EXPORT_SYMBOL(cfg80211_get_bss);
529 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
530 struct ieee80211_channel *channel,
531 const u8 *meshid, size_t meshidlen,
534 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
535 struct cfg80211_internal_bss *bss, *res = NULL;
537 spin_lock_bh(&dev->bss_lock);
539 list_for_each_entry(bss, &dev->bss_list, list) {
540 if (channel && bss->pub.channel != channel)
542 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
549 spin_unlock_bh(&dev->bss_lock);
554 EXPORT_SYMBOL(cfg80211_get_mesh);
557 static void rb_insert_bss(struct cfg80211_registered_device *dev,
558 struct cfg80211_internal_bss *bss)
560 struct rb_node **p = &dev->bss_tree.rb_node;
561 struct rb_node *parent = NULL;
562 struct cfg80211_internal_bss *tbss;
567 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
569 cmp = cmp_bss(&bss->pub, &tbss->pub);
572 /* will sort of leak this BSS */
582 rb_link_node(&bss->rbn, parent, p);
583 rb_insert_color(&bss->rbn, &dev->bss_tree);
586 static struct cfg80211_internal_bss *
587 rb_find_bss(struct cfg80211_registered_device *dev,
588 struct cfg80211_internal_bss *res)
590 struct rb_node *n = dev->bss_tree.rb_node;
591 struct cfg80211_internal_bss *bss;
595 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
596 r = cmp_bss(&res->pub, &bss->pub);
609 static struct cfg80211_internal_bss *
610 rb_find_hidden_bss(struct cfg80211_registered_device *dev,
611 struct cfg80211_internal_bss *res)
613 struct rb_node *n = dev->bss_tree.rb_node;
614 struct cfg80211_internal_bss *bss;
618 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
619 r = cmp_hidden_bss(&res->pub, &bss->pub);
633 copy_hidden_ies(struct cfg80211_internal_bss *res,
634 struct cfg80211_internal_bss *hidden)
636 if (unlikely(res->pub.beacon_ies))
638 if (WARN_ON(!hidden->pub.beacon_ies))
641 res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC);
642 if (unlikely(!res->pub.beacon_ies))
645 res->beacon_ies_allocated = true;
646 res->pub.len_beacon_ies = hidden->pub.len_beacon_ies;
647 memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies,
648 res->pub.len_beacon_ies);
651 static struct cfg80211_internal_bss *
652 cfg80211_bss_update(struct cfg80211_registered_device *dev,
653 struct cfg80211_internal_bss *res)
655 struct cfg80211_internal_bss *found = NULL;
658 * The reference to "res" is donated to this function.
661 if (WARN_ON(!res->pub.channel)) {
662 kref_put(&res->ref, bss_release);
668 spin_lock_bh(&dev->bss_lock);
670 found = rb_find_bss(dev, res);
673 found->pub.beacon_interval = res->pub.beacon_interval;
674 found->pub.tsf = res->pub.tsf;
675 found->pub.signal = res->pub.signal;
676 found->pub.capability = res->pub.capability;
680 if (res->pub.proberesp_ies) {
681 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
682 size_t ielen = res->pub.len_proberesp_ies;
684 if (found->pub.proberesp_ies &&
685 !found->proberesp_ies_allocated &&
686 ksize(found) >= used + ielen) {
687 memcpy(found->pub.proberesp_ies,
688 res->pub.proberesp_ies, ielen);
689 found->pub.len_proberesp_ies = ielen;
691 u8 *ies = found->pub.proberesp_ies;
693 if (found->proberesp_ies_allocated)
694 ies = krealloc(ies, ielen, GFP_ATOMIC);
696 ies = kmalloc(ielen, GFP_ATOMIC);
699 memcpy(ies, res->pub.proberesp_ies,
701 found->proberesp_ies_allocated = true;
702 found->pub.proberesp_ies = ies;
703 found->pub.len_proberesp_ies = ielen;
707 /* Override possible earlier Beacon frame IEs */
708 found->pub.information_elements =
709 found->pub.proberesp_ies;
710 found->pub.len_information_elements =
711 found->pub.len_proberesp_ies;
714 if (res->pub.beacon_ies) {
715 size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
716 size_t ielen = res->pub.len_beacon_ies;
717 bool information_elements_is_beacon_ies =
718 (found->pub.information_elements ==
719 found->pub.beacon_ies);
721 if (found->pub.beacon_ies &&
722 !found->beacon_ies_allocated &&
723 ksize(found) >= used + ielen) {
724 memcpy(found->pub.beacon_ies,
725 res->pub.beacon_ies, ielen);
726 found->pub.len_beacon_ies = ielen;
728 u8 *ies = found->pub.beacon_ies;
730 if (found->beacon_ies_allocated)
731 ies = krealloc(ies, ielen, GFP_ATOMIC);
733 ies = kmalloc(ielen, GFP_ATOMIC);
736 memcpy(ies, res->pub.beacon_ies,
738 found->beacon_ies_allocated = true;
739 found->pub.beacon_ies = ies;
740 found->pub.len_beacon_ies = ielen;
744 /* Override IEs if they were from a beacon before */
745 if (information_elements_is_beacon_ies) {
746 found->pub.information_elements =
747 found->pub.beacon_ies;
748 found->pub.len_information_elements =
749 found->pub.len_beacon_ies;
753 kref_put(&res->ref, bss_release);
755 struct cfg80211_internal_bss *hidden;
757 /* First check if the beacon is a probe response from
758 * a hidden bss. If so, copy beacon ies (with nullified
759 * ssid) into the probe response bss entry (with real ssid).
760 * It is required basically for PSM implementation
761 * (probe responses do not contain tim ie) */
763 /* TODO: The code is not trying to update existing probe
764 * response bss entries when beacon ies are
765 * getting changed. */
766 hidden = rb_find_hidden_bss(dev, res);
768 copy_hidden_ies(res, hidden);
770 /* this "consumes" the reference */
771 list_add_tail(&res->list, &dev->bss_list);
772 rb_insert_bss(dev, res);
776 dev->bss_generation++;
777 spin_unlock_bh(&dev->bss_lock);
779 kref_get(&found->ref);
783 static struct ieee80211_channel *
784 cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
785 struct ieee80211_channel *channel)
789 int channel_number = -1;
791 tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
792 if (tmp && tmp[1] == 1) {
793 channel_number = tmp[2];
795 tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
796 if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
797 struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
799 channel_number = htop->primary_chan;
803 if (channel_number < 0)
806 freq = ieee80211_channel_to_frequency(channel_number, channel->band);
807 channel = ieee80211_get_channel(wiphy, freq);
810 if (channel->flags & IEEE80211_CHAN_DISABLED)
816 cfg80211_inform_bss(struct wiphy *wiphy,
817 struct ieee80211_channel *channel,
818 const u8 *bssid, u64 tsf, u16 capability,
819 u16 beacon_interval, const u8 *ie, size_t ielen,
820 s32 signal, gfp_t gfp)
822 struct cfg80211_internal_bss *res;
828 privsz = wiphy->bss_priv_size;
830 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
831 (signal < 0 || signal > 100)))
834 channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
838 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
842 memcpy(res->pub.bssid, bssid, ETH_ALEN);
843 res->pub.channel = channel;
844 res->pub.signal = signal;
846 res->pub.beacon_interval = beacon_interval;
847 res->pub.capability = capability;
849 * Since we do not know here whether the IEs are from a Beacon or Probe
850 * Response frame, we need to pick one of the options and only use it
851 * with the driver that does not provide the full Beacon/Probe Response
852 * frame. Use Beacon frame pointer to avoid indicating that this should
853 * override the information_elements pointer should we have received an
854 * earlier indication of Probe Response data.
856 * The initial buffer for the IEs is allocated with the BSS entry and
857 * is located after the private area.
859 res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
860 memcpy(res->pub.beacon_ies, ie, ielen);
861 res->pub.len_beacon_ies = ielen;
862 res->pub.information_elements = res->pub.beacon_ies;
863 res->pub.len_information_elements = res->pub.len_beacon_ies;
865 kref_init(&res->ref);
867 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
871 if (res->pub.capability & WLAN_CAPABILITY_ESS)
872 regulatory_hint_found_beacon(wiphy, channel, gfp);
874 trace_cfg80211_return_bss(&res->pub);
875 /* cfg80211_bss_update gives us a referenced result */
878 EXPORT_SYMBOL(cfg80211_inform_bss);
880 struct cfg80211_bss *
881 cfg80211_inform_bss_frame(struct wiphy *wiphy,
882 struct ieee80211_channel *channel,
883 struct ieee80211_mgmt *mgmt, size_t len,
884 s32 signal, gfp_t gfp)
886 struct cfg80211_internal_bss *res;
887 size_t ielen = len - offsetof(struct ieee80211_mgmt,
888 u.probe_resp.variable);
891 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
892 offsetof(struct ieee80211_mgmt, u.beacon.variable));
894 trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
902 if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
903 (signal < 0 || signal > 100)))
906 if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
909 privsz = wiphy->bss_priv_size;
911 channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
916 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
920 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
921 res->pub.channel = channel;
922 res->pub.signal = signal;
923 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
924 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
925 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
927 * The initial buffer for the IEs is allocated with the BSS entry and
928 * is located after the private area.
930 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
931 res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
932 memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
934 res->pub.len_proberesp_ies = ielen;
935 res->pub.information_elements = res->pub.proberesp_ies;
936 res->pub.len_information_elements = res->pub.len_proberesp_ies;
938 res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
939 memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
940 res->pub.len_beacon_ies = ielen;
941 res->pub.information_elements = res->pub.beacon_ies;
942 res->pub.len_information_elements = res->pub.len_beacon_ies;
945 kref_init(&res->ref);
947 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
951 if (res->pub.capability & WLAN_CAPABILITY_ESS)
952 regulatory_hint_found_beacon(wiphy, channel, gfp);
954 trace_cfg80211_return_bss(&res->pub);
955 /* cfg80211_bss_update gives us a referenced result */
958 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
960 void cfg80211_ref_bss(struct cfg80211_bss *pub)
962 struct cfg80211_internal_bss *bss;
967 bss = container_of(pub, struct cfg80211_internal_bss, pub);
970 EXPORT_SYMBOL(cfg80211_ref_bss);
972 void cfg80211_put_bss(struct cfg80211_bss *pub)
974 struct cfg80211_internal_bss *bss;
979 bss = container_of(pub, struct cfg80211_internal_bss, pub);
980 kref_put(&bss->ref, bss_release);
982 EXPORT_SYMBOL(cfg80211_put_bss);
984 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
986 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
987 struct cfg80211_internal_bss *bss;
992 bss = container_of(pub, struct cfg80211_internal_bss, pub);
994 spin_lock_bh(&dev->bss_lock);
995 if (!list_empty(&bss->list)) {
996 __cfg80211_unlink_bss(dev, bss);
997 dev->bss_generation++;
999 spin_unlock_bh(&dev->bss_lock);
1001 EXPORT_SYMBOL(cfg80211_unlink_bss);
1003 #ifdef CONFIG_CFG80211_WEXT
1004 int cfg80211_wext_siwscan(struct net_device *dev,
1005 struct iw_request_info *info,
1006 union iwreq_data *wrqu, char *extra)
1008 struct cfg80211_registered_device *rdev;
1009 struct wiphy *wiphy;
1010 struct iw_scan_req *wreq = NULL;
1011 struct cfg80211_scan_request *creq = NULL;
1012 int i, err, n_channels = 0;
1013 enum ieee80211_band band;
1015 if (!netif_running(dev))
1018 if (wrqu->data.length == sizeof(struct iw_scan_req))
1019 wreq = (struct iw_scan_req *)extra;
1021 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1024 return PTR_ERR(rdev);
1026 if (rdev->scan_req) {
1031 wiphy = &rdev->wiphy;
1033 /* Determine number of channels, needed to allocate creq */
1034 if (wreq && wreq->num_channels)
1035 n_channels = wreq->num_channels;
1037 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1038 if (wiphy->bands[band])
1039 n_channels += wiphy->bands[band]->n_channels;
1042 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1043 n_channels * sizeof(void *),
1050 creq->wiphy = wiphy;
1051 creq->wdev = dev->ieee80211_ptr;
1052 /* SSIDs come after channels */
1053 creq->ssids = (void *)&creq->channels[n_channels];
1054 creq->n_channels = n_channels;
1056 creq->scan_start = jiffies;
1058 /* translate "Scan on frequencies" request */
1060 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1063 if (!wiphy->bands[band])
1066 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1067 /* ignore disabled channels */
1068 if (wiphy->bands[band]->channels[j].flags &
1069 IEEE80211_CHAN_DISABLED)
1072 /* If we have a wireless request structure and the
1073 * wireless request specifies frequencies, then search
1074 * for the matching hardware channel.
1076 if (wreq && wreq->num_channels) {
1078 int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
1079 for (k = 0; k < wreq->num_channels; k++) {
1080 int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
1081 if (wext_freq == wiphy_freq)
1082 goto wext_freq_found;
1084 goto wext_freq_not_found;
1088 creq->channels[i] = &wiphy->bands[band]->channels[j];
1090 wext_freq_not_found: ;
1093 /* No channels found? */
1099 /* Set real number of channels specified in creq->channels[] */
1100 creq->n_channels = i;
1102 /* translate "Scan for SSID" request */
1104 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1105 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
1109 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
1110 creq->ssids[0].ssid_len = wreq->essid_len;
1112 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
1116 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
1117 if (wiphy->bands[i])
1118 creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
1120 rdev->scan_req = creq;
1121 err = rdev_scan(rdev, creq);
1123 rdev->scan_req = NULL;
1124 /* creq will be freed below */
1126 nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
1127 /* creq now owned by driver */
1133 cfg80211_unlock_rdev(rdev);
1136 EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
1138 static void ieee80211_scan_add_ies(struct iw_request_info *info,
1139 struct cfg80211_bss *bss,
1140 char **current_ev, char *end_buf)
1142 u8 *pos, *end, *next;
1143 struct iw_event iwe;
1145 if (!bss->information_elements ||
1146 !bss->len_information_elements)
1150 * If needed, fragment the IEs buffer (at IE boundaries) into short
1151 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
1153 pos = bss->information_elements;
1154 end = pos + bss->len_information_elements;
1156 while (end - pos > IW_GENERIC_IE_MAX) {
1157 next = pos + 2 + pos[1];
1158 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
1159 next = next + 2 + next[1];
1161 memset(&iwe, 0, sizeof(iwe));
1162 iwe.cmd = IWEVGENIE;
1163 iwe.u.data.length = next - pos;
1164 *current_ev = iwe_stream_add_point(info, *current_ev,
1165 end_buf, &iwe, pos);
1171 memset(&iwe, 0, sizeof(iwe));
1172 iwe.cmd = IWEVGENIE;
1173 iwe.u.data.length = end - pos;
1174 *current_ev = iwe_stream_add_point(info, *current_ev,
1175 end_buf, &iwe, pos);
1179 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
1181 unsigned long end = jiffies;
1184 return jiffies_to_msecs(end - start);
1186 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
1190 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1191 struct cfg80211_internal_bss *bss, char *current_ev,
1194 struct iw_event iwe;
1196 u8 *ie = bss->pub.information_elements;
1197 int rem = bss->pub.len_information_elements, i, sig;
1198 bool ismesh = false;
1200 memset(&iwe, 0, sizeof(iwe));
1201 iwe.cmd = SIOCGIWAP;
1202 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1203 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1204 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1207 memset(&iwe, 0, sizeof(iwe));
1208 iwe.cmd = SIOCGIWFREQ;
1209 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1211 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1214 memset(&iwe, 0, sizeof(iwe));
1215 iwe.cmd = SIOCGIWFREQ;
1216 iwe.u.freq.m = bss->pub.channel->center_freq;
1218 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1221 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
1222 memset(&iwe, 0, sizeof(iwe));
1224 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
1225 IW_QUAL_NOISE_INVALID |
1226 IW_QUAL_QUAL_UPDATED;
1227 switch (wiphy->signal_type) {
1228 case CFG80211_SIGNAL_TYPE_MBM:
1229 sig = bss->pub.signal / 100;
1230 iwe.u.qual.level = sig;
1231 iwe.u.qual.updated |= IW_QUAL_DBM;
1232 if (sig < -110) /* rather bad */
1234 else if (sig > -40) /* perfect */
1236 /* will give a range of 0 .. 70 */
1237 iwe.u.qual.qual = sig + 110;
1239 case CFG80211_SIGNAL_TYPE_UNSPEC:
1240 iwe.u.qual.level = bss->pub.signal;
1241 /* will give range 0 .. 100 */
1242 iwe.u.qual.qual = bss->pub.signal;
1248 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1249 &iwe, IW_EV_QUAL_LEN);
1252 memset(&iwe, 0, sizeof(iwe));
1253 iwe.cmd = SIOCGIWENCODE;
1254 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
1255 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1257 iwe.u.data.flags = IW_ENCODE_DISABLED;
1258 iwe.u.data.length = 0;
1259 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1264 if (ie[1] > rem - 2)
1269 memset(&iwe, 0, sizeof(iwe));
1270 iwe.cmd = SIOCGIWESSID;
1271 iwe.u.data.length = ie[1];
1272 iwe.u.data.flags = 1;
1273 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1276 case WLAN_EID_MESH_ID:
1277 memset(&iwe, 0, sizeof(iwe));
1278 iwe.cmd = SIOCGIWESSID;
1279 iwe.u.data.length = ie[1];
1280 iwe.u.data.flags = 1;
1281 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1284 case WLAN_EID_MESH_CONFIG:
1286 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
1288 buf = kmalloc(50, GFP_ATOMIC);
1292 memset(&iwe, 0, sizeof(iwe));
1293 iwe.cmd = IWEVCUSTOM;
1294 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1296 iwe.u.data.length = strlen(buf);
1297 current_ev = iwe_stream_add_point(info, current_ev,
1300 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1302 iwe.u.data.length = strlen(buf);
1303 current_ev = iwe_stream_add_point(info, current_ev,
1306 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1308 iwe.u.data.length = strlen(buf);
1309 current_ev = iwe_stream_add_point(info, current_ev,
1312 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
1313 iwe.u.data.length = strlen(buf);
1314 current_ev = iwe_stream_add_point(info, current_ev,
1317 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1318 iwe.u.data.length = strlen(buf);
1319 current_ev = iwe_stream_add_point(info, current_ev,
1322 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1323 iwe.u.data.length = strlen(buf);
1324 current_ev = iwe_stream_add_point(info, current_ev,
1327 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
1328 iwe.u.data.length = strlen(buf);
1329 current_ev = iwe_stream_add_point(info, current_ev,
1334 case WLAN_EID_SUPP_RATES:
1335 case WLAN_EID_EXT_SUPP_RATES:
1336 /* display all supported rates in readable format */
1337 p = current_ev + iwe_stream_lcp_len(info);
1339 memset(&iwe, 0, sizeof(iwe));
1340 iwe.cmd = SIOCGIWRATE;
1341 /* Those two flags are ignored... */
1342 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
1344 for (i = 0; i < ie[1]; i++) {
1345 iwe.u.bitrate.value =
1346 ((ie[i + 2] & 0x7f) * 500000);
1347 p = iwe_stream_add_value(info, current_ev, p,
1348 end_buf, &iwe, IW_EV_PARAM_LEN);
1357 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
1359 memset(&iwe, 0, sizeof(iwe));
1360 iwe.cmd = SIOCGIWMODE;
1362 iwe.u.mode = IW_MODE_MESH;
1363 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
1364 iwe.u.mode = IW_MODE_MASTER;
1366 iwe.u.mode = IW_MODE_ADHOC;
1367 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
1368 &iwe, IW_EV_UINT_LEN);
1371 buf = kmalloc(30, GFP_ATOMIC);
1373 memset(&iwe, 0, sizeof(iwe));
1374 iwe.cmd = IWEVCUSTOM;
1375 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
1376 iwe.u.data.length = strlen(buf);
1377 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1379 memset(&iwe, 0, sizeof(iwe));
1380 iwe.cmd = IWEVCUSTOM;
1381 sprintf(buf, " Last beacon: %ums ago",
1382 elapsed_jiffies_msecs(bss->ts));
1383 iwe.u.data.length = strlen(buf);
1384 current_ev = iwe_stream_add_point(info, current_ev,
1385 end_buf, &iwe, buf);
1389 ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf);
1395 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
1396 struct iw_request_info *info,
1397 char *buf, size_t len)
1399 char *current_ev = buf;
1400 char *end_buf = buf + len;
1401 struct cfg80211_internal_bss *bss;
1403 spin_lock_bh(&dev->bss_lock);
1404 cfg80211_bss_expire(dev);
1406 list_for_each_entry(bss, &dev->bss_list, list) {
1407 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1408 spin_unlock_bh(&dev->bss_lock);
1411 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
1412 current_ev, end_buf);
1414 spin_unlock_bh(&dev->bss_lock);
1415 return current_ev - buf;
1419 int cfg80211_wext_giwscan(struct net_device *dev,
1420 struct iw_request_info *info,
1421 struct iw_point *data, char *extra)
1423 struct cfg80211_registered_device *rdev;
1426 if (!netif_running(dev))
1429 rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
1432 return PTR_ERR(rdev);
1434 if (rdev->scan_req) {
1439 res = ieee80211_scan_results(rdev, info, extra, data->length);
1447 cfg80211_unlock_rdev(rdev);
1450 EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);