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 if (res->pub.capability & WLAN_CAPABILITY_ESS)
434 regulatory_hint_found_beacon(wiphy, channel, gfp);
436 /* cfg80211_bss_update gives us a referenced result */
439 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
441 void cfg80211_put_bss(struct cfg80211_bss *pub)
443 struct cfg80211_internal_bss *bss;
448 bss = container_of(pub, struct cfg80211_internal_bss, pub);
449 kref_put(&bss->ref, bss_release);
451 EXPORT_SYMBOL(cfg80211_put_bss);
453 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
455 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
456 struct cfg80211_internal_bss *bss;
461 bss = container_of(pub, struct cfg80211_internal_bss, pub);
463 spin_lock_bh(&dev->bss_lock);
465 list_del(&bss->list);
466 rb_erase(&bss->rbn, &dev->bss_tree);
468 spin_unlock_bh(&dev->bss_lock);
470 kref_put(&bss->ref, bss_release);
472 EXPORT_SYMBOL(cfg80211_unlink_bss);
474 #ifdef CONFIG_WIRELESS_EXT
475 int cfg80211_wext_siwscan(struct net_device *dev,
476 struct iw_request_info *info,
477 union iwreq_data *wrqu, char *extra)
479 struct cfg80211_registered_device *rdev;
481 struct iw_scan_req *wreq = NULL;
482 struct cfg80211_scan_request *creq;
483 int i, err, n_channels = 0;
484 enum ieee80211_band band;
486 if (!netif_running(dev))
489 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
492 return PTR_ERR(rdev);
494 if (rdev->scan_req) {
499 wiphy = &rdev->wiphy;
501 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
502 if (wiphy->bands[band])
503 n_channels += wiphy->bands[band]->n_channels;
505 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
506 n_channels * sizeof(void *),
514 creq->ifidx = dev->ifindex;
515 creq->ssids = (void *)(creq + 1);
516 creq->channels = (void *)(creq->ssids + 1);
517 creq->n_channels = n_channels;
522 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
524 if (!wiphy->bands[band])
526 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
527 creq->channels[i] = &wiphy->bands[band]->channels[j];
532 /* translate scan request */
533 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
534 wreq = (struct iw_scan_req *)extra;
536 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
537 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
539 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
540 creq->ssids[0].ssid_len = wreq->essid_len;
542 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
546 rdev->scan_req = creq;
547 err = rdev->ops->scan(wiphy, dev, creq);
549 rdev->scan_req = NULL;
553 cfg80211_put_dev(rdev);
556 EXPORT_SYMBOL(cfg80211_wext_siwscan);
558 static void ieee80211_scan_add_ies(struct iw_request_info *info,
559 struct cfg80211_bss *bss,
560 char **current_ev, char *end_buf)
562 u8 *pos, *end, *next;
565 if (!bss->information_elements ||
566 !bss->len_information_elements)
570 * If needed, fragment the IEs buffer (at IE boundaries) into short
571 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
573 pos = bss->information_elements;
574 end = pos + bss->len_information_elements;
576 while (end - pos > IW_GENERIC_IE_MAX) {
577 next = pos + 2 + pos[1];
578 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
579 next = next + 2 + next[1];
581 memset(&iwe, 0, sizeof(iwe));
583 iwe.u.data.length = next - pos;
584 *current_ev = iwe_stream_add_point(info, *current_ev,
591 memset(&iwe, 0, sizeof(iwe));
593 iwe.u.data.length = end - pos;
594 *current_ev = iwe_stream_add_point(info, *current_ev,
599 static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
601 unsigned long end = jiffies;
604 return jiffies_to_msecs(end - start);
606 return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
610 ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
611 struct cfg80211_internal_bss *bss, char *current_ev,
616 u8 *ie = bss->pub.information_elements;
617 int rem = bss->pub.len_information_elements, i, sig;
620 memset(&iwe, 0, sizeof(iwe));
622 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
623 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
624 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
627 memset(&iwe, 0, sizeof(iwe));
628 iwe.cmd = SIOCGIWFREQ;
629 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
631 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
634 memset(&iwe, 0, sizeof(iwe));
635 iwe.cmd = SIOCGIWFREQ;
636 iwe.u.freq.m = bss->pub.channel->center_freq;
638 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
641 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
642 memset(&iwe, 0, sizeof(iwe));
644 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
645 IW_QUAL_NOISE_INVALID |
646 IW_QUAL_QUAL_UPDATED;
647 switch (wiphy->signal_type) {
648 case CFG80211_SIGNAL_TYPE_MBM:
649 sig = bss->pub.signal / 100;
650 iwe.u.qual.level = sig;
651 iwe.u.qual.updated |= IW_QUAL_DBM;
652 if (sig < -110) /* rather bad */
654 else if (sig > -40) /* perfect */
656 /* will give a range of 0 .. 70 */
657 iwe.u.qual.qual = sig + 110;
659 case CFG80211_SIGNAL_TYPE_UNSPEC:
660 iwe.u.qual.level = bss->pub.signal;
661 /* will give range 0 .. 100 */
662 iwe.u.qual.qual = bss->pub.signal;
668 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
669 &iwe, IW_EV_QUAL_LEN);
672 memset(&iwe, 0, sizeof(iwe));
673 iwe.cmd = SIOCGIWENCODE;
674 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
675 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
677 iwe.u.data.flags = IW_ENCODE_DISABLED;
678 iwe.u.data.length = 0;
679 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
689 memset(&iwe, 0, sizeof(iwe));
690 iwe.cmd = SIOCGIWESSID;
691 iwe.u.data.length = ie[1];
692 iwe.u.data.flags = 1;
693 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
696 case WLAN_EID_MESH_ID:
697 memset(&iwe, 0, sizeof(iwe));
698 iwe.cmd = SIOCGIWESSID;
699 iwe.u.data.length = ie[1];
700 iwe.u.data.flags = 1;
701 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
704 case WLAN_EID_MESH_CONFIG:
706 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
708 buf = kmalloc(50, GFP_ATOMIC);
712 memset(&iwe, 0, sizeof(iwe));
713 iwe.cmd = IWEVCUSTOM;
714 sprintf(buf, "Mesh network (version %d)", cfg[0]);
715 iwe.u.data.length = strlen(buf);
716 current_ev = iwe_stream_add_point(info, current_ev,
719 sprintf(buf, "Path Selection Protocol ID: "
720 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
722 iwe.u.data.length = strlen(buf);
723 current_ev = iwe_stream_add_point(info, current_ev,
726 sprintf(buf, "Path Selection Metric ID: "
727 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
729 iwe.u.data.length = strlen(buf);
730 current_ev = iwe_stream_add_point(info, current_ev,
733 sprintf(buf, "Congestion Control Mode ID: "
734 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
736 iwe.u.data.length = strlen(buf);
737 current_ev = iwe_stream_add_point(info, current_ev,
740 sprintf(buf, "Channel Precedence: "
741 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
743 iwe.u.data.length = strlen(buf);
744 current_ev = iwe_stream_add_point(info, current_ev,
749 case WLAN_EID_SUPP_RATES:
750 case WLAN_EID_EXT_SUPP_RATES:
751 /* display all supported rates in readable format */
752 p = current_ev + iwe_stream_lcp_len(info);
754 memset(&iwe, 0, sizeof(iwe));
755 iwe.cmd = SIOCGIWRATE;
756 /* Those two flags are ignored... */
757 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
759 for (i = 0; i < ie[1]; i++) {
760 iwe.u.bitrate.value =
761 ((ie[i + 2] & 0x7f) * 500000);
762 p = iwe_stream_add_value(info, current_ev, p,
763 end_buf, &iwe, IW_EV_PARAM_LEN);
772 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
774 memset(&iwe, 0, sizeof(iwe));
775 iwe.cmd = SIOCGIWMODE;
777 iwe.u.mode = IW_MODE_MESH;
778 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
779 iwe.u.mode = IW_MODE_MASTER;
781 iwe.u.mode = IW_MODE_ADHOC;
782 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
783 &iwe, IW_EV_UINT_LEN);
786 buf = kmalloc(30, GFP_ATOMIC);
788 memset(&iwe, 0, sizeof(iwe));
789 iwe.cmd = IWEVCUSTOM;
790 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
791 iwe.u.data.length = strlen(buf);
792 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
794 memset(&iwe, 0, sizeof(iwe));
795 iwe.cmd = IWEVCUSTOM;
796 sprintf(buf, " Last beacon: %ums ago",
797 elapsed_jiffies_msecs(bss->ts));
798 iwe.u.data.length = strlen(buf);
799 current_ev = iwe_stream_add_point(info, current_ev,
804 ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf);
810 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
811 struct iw_request_info *info,
812 char *buf, size_t len)
814 char *current_ev = buf;
815 char *end_buf = buf + len;
816 struct cfg80211_internal_bss *bss;
818 spin_lock_bh(&dev->bss_lock);
819 cfg80211_bss_expire(dev);
821 list_for_each_entry(bss, &dev->bss_list, list) {
822 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
823 spin_unlock_bh(&dev->bss_lock);
826 current_ev = ieee80211_bss(&dev->wiphy, info, bss,
827 current_ev, end_buf);
829 spin_unlock_bh(&dev->bss_lock);
830 return current_ev - buf;
834 int cfg80211_wext_giwscan(struct net_device *dev,
835 struct iw_request_info *info,
836 struct iw_point *data, char *extra)
838 struct cfg80211_registered_device *rdev;
841 if (!netif_running(dev))
844 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
847 return PTR_ERR(rdev);
849 if (rdev->scan_req) {
854 res = ieee80211_scan_results(rdev, info, extra, data->length);
862 cfg80211_put_dev(rdev);
865 EXPORT_SYMBOL(cfg80211_wext_giwscan);