2 * cfg80211 scan result handling
4 * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/netdevice.h>
9 #include <linux/wireless.h>
10 #include <linux/nl80211.h>
11 #include <linux/etherdevice.h>
13 #include <net/cfg80211.h>
14 #include <net/iw_handler.h>
18 #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
20 void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
22 struct net_device *dev;
23 #ifdef CONFIG_WIRELESS_EXT
24 union iwreq_data wrqu;
27 dev = dev_get_by_index(&init_net, request->ifidx);
31 WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
32 wiphy_to_dev(request->wiphy)->scan_req = NULL;
35 nl80211_send_scan_aborted(wiphy_to_dev(request->wiphy), dev);
37 nl80211_send_scan_done(wiphy_to_dev(request->wiphy), dev);
39 #ifdef CONFIG_WIRELESS_EXT
41 memset(&wrqu, 0, sizeof(wrqu));
43 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
52 EXPORT_SYMBOL(cfg80211_scan_done);
54 static void bss_release(struct kref *ref)
56 struct cfg80211_internal_bss *bss;
58 bss = container_of(ref, struct cfg80211_internal_bss, ref);
59 if (bss->pub.free_priv)
60 bss->pub.free_priv(&bss->pub);
64 /* must hold dev->bss_lock! */
65 void cfg80211_bss_age(struct cfg80211_registered_device *dev,
66 unsigned long age_secs)
68 struct cfg80211_internal_bss *bss;
69 unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
71 list_for_each_entry(bss, &dev->bss_list, list) {
72 bss->ts -= age_jiffies;
76 /* must hold dev->bss_lock! */
77 void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
79 struct cfg80211_internal_bss *bss, *tmp;
82 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
83 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
86 rb_erase(&bss->rbn, &dev->bss_tree);
87 kref_put(&bss->ref, bss_release);
92 dev->bss_generation++;
95 static u8 *find_ie(u8 num, u8 *ies, size_t len)
97 while (len > 2 && ies[0] != num) {
103 if (len < 2 + ies[1])
108 static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
110 const u8 *ie1 = find_ie(num, ies1, len1);
111 const u8 *ie2 = find_ie(num, ies2, len2);
119 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
120 if (r == 0 && ie1[1] != ie2[1])
121 return ie2[1] - ie1[1];
125 static bool is_bss(struct cfg80211_bss *a,
127 const u8 *ssid, size_t ssid_len)
131 if (bssid && compare_ether_addr(a->bssid, bssid))
137 ssidie = find_ie(WLAN_EID_SSID,
138 a->information_elements,
139 a->len_information_elements);
142 if (ssidie[1] != ssid_len)
144 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
147 static bool is_mesh(struct cfg80211_bss *a,
148 const u8 *meshid, size_t meshidlen,
153 if (!is_zero_ether_addr(a->bssid))
156 ie = find_ie(WLAN_EID_MESH_ID,
157 a->information_elements,
158 a->len_information_elements);
161 if (ie[1] != meshidlen)
163 if (memcmp(ie + 2, meshid, meshidlen))
166 ie = find_ie(WLAN_EID_MESH_CONFIG,
167 a->information_elements,
168 a->len_information_elements);
169 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
173 * Ignore mesh capability (last two bytes of the IE) when
174 * comparing since that may differ between stations taking
175 * part in the same mesh.
177 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
180 static int cmp_bss(struct cfg80211_bss *a,
181 struct cfg80211_bss *b)
185 if (a->channel != b->channel)
186 return b->channel->center_freq - a->channel->center_freq;
188 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
192 if (is_zero_ether_addr(a->bssid)) {
193 r = cmp_ies(WLAN_EID_MESH_ID,
194 a->information_elements,
195 a->len_information_elements,
196 b->information_elements,
197 b->len_information_elements);
200 return cmp_ies(WLAN_EID_MESH_CONFIG,
201 a->information_elements,
202 a->len_information_elements,
203 b->information_elements,
204 b->len_information_elements);
207 return cmp_ies(WLAN_EID_SSID,
208 a->information_elements,
209 a->len_information_elements,
210 b->information_elements,
211 b->len_information_elements);
214 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
215 struct ieee80211_channel *channel,
217 const u8 *ssid, size_t ssid_len,
218 u16 capa_mask, u16 capa_val)
220 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
221 struct cfg80211_internal_bss *bss, *res = NULL;
223 spin_lock_bh(&dev->bss_lock);
225 list_for_each_entry(bss, &dev->bss_list, list) {
226 if ((bss->pub.capability & capa_mask) != capa_val)
228 if (channel && bss->pub.channel != channel)
230 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
237 spin_unlock_bh(&dev->bss_lock);
242 EXPORT_SYMBOL(cfg80211_get_bss);
244 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
245 struct ieee80211_channel *channel,
246 const u8 *meshid, size_t meshidlen,
249 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
250 struct cfg80211_internal_bss *bss, *res = NULL;
252 spin_lock_bh(&dev->bss_lock);
254 list_for_each_entry(bss, &dev->bss_list, list) {
255 if (channel && bss->pub.channel != channel)
257 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
264 spin_unlock_bh(&dev->bss_lock);
269 EXPORT_SYMBOL(cfg80211_get_mesh);
272 static void rb_insert_bss(struct cfg80211_registered_device *dev,
273 struct cfg80211_internal_bss *bss)
275 struct rb_node **p = &dev->bss_tree.rb_node;
276 struct rb_node *parent = NULL;
277 struct cfg80211_internal_bss *tbss;
282 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
284 cmp = cmp_bss(&bss->pub, &tbss->pub);
287 /* will sort of leak this BSS */
297 rb_link_node(&bss->rbn, parent, p);
298 rb_insert_color(&bss->rbn, &dev->bss_tree);
301 static struct cfg80211_internal_bss *
302 rb_find_bss(struct cfg80211_registered_device *dev,
303 struct cfg80211_internal_bss *res)
305 struct rb_node *n = dev->bss_tree.rb_node;
306 struct cfg80211_internal_bss *bss;
310 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
311 r = cmp_bss(&res->pub, &bss->pub);
324 static struct cfg80211_internal_bss *
325 cfg80211_bss_update(struct cfg80211_registered_device *dev,
326 struct cfg80211_internal_bss *res,
329 struct cfg80211_internal_bss *found = NULL;
330 const u8 *meshid, *meshcfg;
333 * The reference to "res" is donated to this function.
336 if (WARN_ON(!res->pub.channel)) {
337 kref_put(&res->ref, bss_release);
343 if (is_zero_ether_addr(res->pub.bssid)) {
344 /* must be mesh, verify */
345 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
346 res->pub.len_information_elements);
347 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
348 res->pub.information_elements,
349 res->pub.len_information_elements);
350 if (!meshid || !meshcfg ||
351 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
353 kref_put(&res->ref, bss_release);
358 spin_lock_bh(&dev->bss_lock);
360 found = rb_find_bss(dev, res);
362 if (found && overwrite) {
363 list_replace(&found->list, &res->list);
364 rb_replace_node(&found->rbn, &res->rbn,
366 kref_put(&found->ref, bss_release);
369 kref_get(&found->ref);
370 found->pub.beacon_interval = res->pub.beacon_interval;
371 found->pub.tsf = res->pub.tsf;
372 found->pub.signal = res->pub.signal;
373 found->pub.capability = res->pub.capability;
375 kref_put(&res->ref, bss_release);
377 /* this "consumes" the reference */
378 list_add_tail(&res->list, &dev->bss_list);
379 rb_insert_bss(dev, res);
383 dev->bss_generation++;
384 spin_unlock_bh(&dev->bss_lock);
386 kref_get(&found->ref);
390 struct cfg80211_bss *
391 cfg80211_inform_bss_frame(struct wiphy *wiphy,
392 struct ieee80211_channel *channel,
393 struct ieee80211_mgmt *mgmt, size_t len,
394 s32 signal, gfp_t gfp)
396 struct cfg80211_internal_bss *res;
397 size_t ielen = len - offsetof(struct ieee80211_mgmt,
398 u.probe_resp.variable);
400 size_t privsz = wiphy->bss_priv_size;
402 if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
403 (signal < 0 || signal > 100)))
406 if (WARN_ON(!mgmt || !wiphy ||
407 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
410 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
414 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
415 res->pub.channel = channel;
416 res->pub.signal = signal;
417 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
418 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
419 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
420 /* point to after the private area */
421 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
422 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
423 res->pub.len_information_elements = ielen;
425 kref_init(&res->ref);
427 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
429 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
433 /* cfg80211_bss_update gives us a referenced result */
436 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
438 void cfg80211_put_bss(struct cfg80211_bss *pub)
440 struct cfg80211_internal_bss *bss;
445 bss = container_of(pub, struct cfg80211_internal_bss, pub);
446 kref_put(&bss->ref, bss_release);
448 EXPORT_SYMBOL(cfg80211_put_bss);
450 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
452 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
453 struct cfg80211_internal_bss *bss;
458 bss = container_of(pub, struct cfg80211_internal_bss, pub);
460 spin_lock_bh(&dev->bss_lock);
462 list_del(&bss->list);
463 rb_erase(&bss->rbn, &dev->bss_tree);
465 spin_unlock_bh(&dev->bss_lock);
467 kref_put(&bss->ref, bss_release);
469 EXPORT_SYMBOL(cfg80211_unlink_bss);
471 #ifdef CONFIG_WIRELESS_EXT
472 int cfg80211_wext_siwscan(struct net_device *dev,
473 struct iw_request_info *info,
474 union iwreq_data *wrqu, char *extra)
476 struct cfg80211_registered_device *rdev;
478 struct iw_scan_req *wreq = NULL;
479 struct cfg80211_scan_request *creq;
480 int i, err, n_channels = 0;
481 enum ieee80211_band band;
483 if (!netif_running(dev))
486 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
489 return PTR_ERR(rdev);
491 if (rdev->scan_req) {
496 wiphy = &rdev->wiphy;
498 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
499 if (wiphy->bands[band])
500 n_channels += wiphy->bands[band]->n_channels;
502 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
503 n_channels * sizeof(void *),
511 creq->ifidx = dev->ifindex;
512 creq->ssids = (void *)(creq + 1);
513 creq->channels = (void *)(creq->ssids + 1);
514 creq->n_channels = n_channels;
519 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
521 if (!wiphy->bands[band])
523 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
524 creq->channels[i] = &wiphy->bands[band]->channels[j];
529 /* translate scan request */
530 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
531 wreq = (struct iw_scan_req *)extra;
533 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
534 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
536 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
537 creq->ssids[0].ssid_len = wreq->essid_len;
539 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
543 rdev->scan_req = creq;
544 err = rdev->ops->scan(wiphy, dev, creq);
546 rdev->scan_req = NULL;
550 cfg80211_put_dev(rdev);
553 EXPORT_SYMBOL(cfg80211_wext_siwscan);
555 static void ieee80211_scan_add_ies(struct iw_request_info *info,
556 struct cfg80211_bss *bss,
557 char **current_ev, char *end_buf)
559 u8 *pos, *end, *next;
562 if (!bss->information_elements ||
563 !bss->len_information_elements)
567 * If needed, fragment the IEs buffer (at IE boundaries) into short
568 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
570 pos = bss->information_elements;
571 end = pos + bss->len_information_elements;
573 while (end - pos > IW_GENERIC_IE_MAX) {
574 next = pos + 2 + pos[1];
575 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
576 next = next + 2 + next[1];
578 memset(&iwe, 0, sizeof(iwe));
580 iwe.u.data.length = next - pos;
581 *current_ev = iwe_stream_add_point(info, *current_ev,
588 memset(&iwe, 0, sizeof(iwe));
590 iwe.u.data.length = end - pos;
591 *current_ev = iwe_stream_add_point(info, *current_ev,
596 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
598 unsigned long end = jiffies;
601 return jiffies_to_msecs(end - start);
603 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
607 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
608 struct cfg80211_internal_bss *bss, char *current_ev,
613 u8 *ie = bss->pub.information_elements;
614 int rem = bss->pub.len_information_elements, i, sig;
617 memset(&iwe, 0, sizeof(iwe));
619 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
620 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
621 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
624 memset(&iwe, 0, sizeof(iwe));
625 iwe.cmd = SIOCGIWFREQ;
626 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
628 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
631 memset(&iwe, 0, sizeof(iwe));
632 iwe.cmd = SIOCGIWFREQ;
633 iwe.u.freq.m = bss->pub.channel->center_freq;
635 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
638 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
639 memset(&iwe, 0, sizeof(iwe));
641 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
642 IW_QUAL_NOISE_INVALID |
643 IW_QUAL_QUAL_UPDATED;
644 switch (wiphy->signal_type) {
645 case CFG80211_SIGNAL_TYPE_MBM:
646 sig = bss->pub.signal / 100;
647 iwe.u.qual.level = sig;
648 iwe.u.qual.updated |= IW_QUAL_DBM;
649 if (sig < -110) /* rather bad */
651 else if (sig > -40) /* perfect */
653 /* will give a range of 0 .. 70 */
654 iwe.u.qual.qual = sig + 110;
656 case CFG80211_SIGNAL_TYPE_UNSPEC:
657 iwe.u.qual.level = bss->pub.signal;
658 /* will give range 0 .. 100 */
659 iwe.u.qual.qual = bss->pub.signal;
665 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
666 &iwe, IW_EV_QUAL_LEN);
669 memset(&iwe, 0, sizeof(iwe));
670 iwe.cmd = SIOCGIWENCODE;
671 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
672 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
674 iwe.u.data.flags = IW_ENCODE_DISABLED;
675 iwe.u.data.length = 0;
676 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
686 memset(&iwe, 0, sizeof(iwe));
687 iwe.cmd = SIOCGIWESSID;
688 iwe.u.data.length = ie[1];
689 iwe.u.data.flags = 1;
690 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
693 case WLAN_EID_MESH_ID:
694 memset(&iwe, 0, sizeof(iwe));
695 iwe.cmd = SIOCGIWESSID;
696 iwe.u.data.length = ie[1];
697 iwe.u.data.flags = 1;
698 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
701 case WLAN_EID_MESH_CONFIG:
703 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
705 buf = kmalloc(50, GFP_ATOMIC);
709 memset(&iwe, 0, sizeof(iwe));
710 iwe.cmd = IWEVCUSTOM;
711 sprintf(buf, "Mesh network (version %d)", cfg[0]);
712 iwe.u.data.length = strlen(buf);
713 current_ev = iwe_stream_add_point(info, current_ev,
716 sprintf(buf, "Path Selection Protocol ID: "
717 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
719 iwe.u.data.length = strlen(buf);
720 current_ev = iwe_stream_add_point(info, current_ev,
723 sprintf(buf, "Path Selection Metric ID: "
724 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
726 iwe.u.data.length = strlen(buf);
727 current_ev = iwe_stream_add_point(info, current_ev,
730 sprintf(buf, "Congestion Control Mode ID: "
731 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
733 iwe.u.data.length = strlen(buf);
734 current_ev = iwe_stream_add_point(info, current_ev,
737 sprintf(buf, "Channel Precedence: "
738 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
740 iwe.u.data.length = strlen(buf);
741 current_ev = iwe_stream_add_point(info, current_ev,
746 case WLAN_EID_SUPP_RATES:
747 case WLAN_EID_EXT_SUPP_RATES:
748 /* display all supported rates in readable format */
749 p = current_ev + iwe_stream_lcp_len(info);
751 memset(&iwe, 0, sizeof(iwe));
752 iwe.cmd = SIOCGIWRATE;
753 /* Those two flags are ignored... */
754 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
756 for (i = 0; i < ie[1]; i++) {
757 iwe.u.bitrate.value =
758 ((ie[i + 2] & 0x7f) * 500000);
759 p = iwe_stream_add_value(info, current_ev, p,
760 end_buf, &iwe, IW_EV_PARAM_LEN);
769 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
771 memset(&iwe, 0, sizeof(iwe));
772 iwe.cmd = SIOCGIWMODE;
774 iwe.u.mode = IW_MODE_MESH;
775 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
776 iwe.u.mode = IW_MODE_MASTER;
778 iwe.u.mode = IW_MODE_ADHOC;
779 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
780 &iwe, IW_EV_UINT_LEN);
783 buf = kmalloc(30, GFP_ATOMIC);
785 memset(&iwe, 0, sizeof(iwe));
786 iwe.cmd = IWEVCUSTOM;
787 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
788 iwe.u.data.length = strlen(buf);
789 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
791 memset(&iwe, 0, sizeof(iwe));
792 iwe.cmd = IWEVCUSTOM;
793 sprintf(buf, " Last beacon: %ums ago",
794 elapsed_jiffies_msecs(bss->ts));
795 iwe.u.data.length = strlen(buf);
796 current_ev = iwe_stream_add_point(info, current_ev,
801 ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf);
807 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
808 struct iw_request_info *info,
809 char *buf, size_t len)
811 char *current_ev = buf;
812 char *end_buf = buf + len;
813 struct cfg80211_internal_bss *bss;
815 spin_lock_bh(&dev->bss_lock);
816 cfg80211_bss_expire(dev);
818 list_for_each_entry(bss, &dev->bss_list, list) {
819 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
820 spin_unlock_bh(&dev->bss_lock);
823 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
824 current_ev, end_buf);
826 spin_unlock_bh(&dev->bss_lock);
827 return current_ev - buf;
831 int cfg80211_wext_giwscan(struct net_device *dev,
832 struct iw_request_info *info,
833 struct iw_point *data, char *extra)
835 struct cfg80211_registered_device *rdev;
838 if (!netif_running(dev))
841 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
844 return PTR_ERR(rdev);
846 if (rdev->scan_req) {
851 res = ieee80211_scan_results(rdev, info, extra, data->length);
859 cfg80211_put_dev(rdev);
862 EXPORT_SYMBOL(cfg80211_wext_giwscan);