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_expire(struct cfg80211_registered_device *dev)
67 struct cfg80211_internal_bss *bss, *tmp;
70 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
71 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
74 rb_erase(&bss->rbn, &dev->bss_tree);
75 kref_put(&bss->ref, bss_release);
80 dev->bss_generation++;
83 static u8 *find_ie(u8 num, u8 *ies, size_t len)
85 while (len > 2 && ies[0] != num) {
96 static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
98 const u8 *ie1 = find_ie(num, ies1, len1);
99 const u8 *ie2 = find_ie(num, ies2, len2);
107 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
108 if (r == 0 && ie1[1] != ie2[1])
109 return ie2[1] - ie1[1];
113 static bool is_bss(struct cfg80211_bss *a,
115 const u8 *ssid, size_t ssid_len)
119 if (bssid && compare_ether_addr(a->bssid, bssid))
125 ssidie = find_ie(WLAN_EID_SSID,
126 a->information_elements,
127 a->len_information_elements);
130 if (ssidie[1] != ssid_len)
132 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
135 static bool is_mesh(struct cfg80211_bss *a,
136 const u8 *meshid, size_t meshidlen,
141 if (!is_zero_ether_addr(a->bssid))
144 ie = find_ie(WLAN_EID_MESH_ID,
145 a->information_elements,
146 a->len_information_elements);
149 if (ie[1] != meshidlen)
151 if (memcmp(ie + 2, meshid, meshidlen))
154 ie = find_ie(WLAN_EID_MESH_CONFIG,
155 a->information_elements,
156 a->len_information_elements);
157 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
161 * Ignore mesh capability (last two bytes of the IE) when
162 * comparing since that may differ between stations taking
163 * part in the same mesh.
165 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
168 static int cmp_bss(struct cfg80211_bss *a,
169 struct cfg80211_bss *b)
173 if (a->channel != b->channel)
174 return b->channel->center_freq - a->channel->center_freq;
176 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
180 if (is_zero_ether_addr(a->bssid)) {
181 r = cmp_ies(WLAN_EID_MESH_ID,
182 a->information_elements,
183 a->len_information_elements,
184 b->information_elements,
185 b->len_information_elements);
188 return cmp_ies(WLAN_EID_MESH_CONFIG,
189 a->information_elements,
190 a->len_information_elements,
191 b->information_elements,
192 b->len_information_elements);
195 return cmp_ies(WLAN_EID_SSID,
196 a->information_elements,
197 a->len_information_elements,
198 b->information_elements,
199 b->len_information_elements);
202 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
203 struct ieee80211_channel *channel,
205 const u8 *ssid, size_t ssid_len,
206 u16 capa_mask, u16 capa_val)
208 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
209 struct cfg80211_internal_bss *bss, *res = NULL;
211 spin_lock_bh(&dev->bss_lock);
213 list_for_each_entry(bss, &dev->bss_list, list) {
214 if ((bss->pub.capability & capa_mask) != capa_val)
216 if (channel && bss->pub.channel != channel)
218 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
225 spin_unlock_bh(&dev->bss_lock);
230 EXPORT_SYMBOL(cfg80211_get_bss);
232 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
233 struct ieee80211_channel *channel,
234 const u8 *meshid, size_t meshidlen,
237 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
238 struct cfg80211_internal_bss *bss, *res = NULL;
240 spin_lock_bh(&dev->bss_lock);
242 list_for_each_entry(bss, &dev->bss_list, list) {
243 if (channel && bss->pub.channel != channel)
245 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
252 spin_unlock_bh(&dev->bss_lock);
257 EXPORT_SYMBOL(cfg80211_get_mesh);
260 static void rb_insert_bss(struct cfg80211_registered_device *dev,
261 struct cfg80211_internal_bss *bss)
263 struct rb_node **p = &dev->bss_tree.rb_node;
264 struct rb_node *parent = NULL;
265 struct cfg80211_internal_bss *tbss;
270 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
272 cmp = cmp_bss(&bss->pub, &tbss->pub);
275 /* will sort of leak this BSS */
285 rb_link_node(&bss->rbn, parent, p);
286 rb_insert_color(&bss->rbn, &dev->bss_tree);
289 static struct cfg80211_internal_bss *
290 rb_find_bss(struct cfg80211_registered_device *dev,
291 struct cfg80211_internal_bss *res)
293 struct rb_node *n = dev->bss_tree.rb_node;
294 struct cfg80211_internal_bss *bss;
298 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
299 r = cmp_bss(&res->pub, &bss->pub);
312 static struct cfg80211_internal_bss *
313 cfg80211_bss_update(struct cfg80211_registered_device *dev,
314 struct cfg80211_internal_bss *res,
317 struct cfg80211_internal_bss *found = NULL;
318 const u8 *meshid, *meshcfg;
321 * The reference to "res" is donated to this function.
324 if (WARN_ON(!res->pub.channel)) {
325 kref_put(&res->ref, bss_release);
331 if (is_zero_ether_addr(res->pub.bssid)) {
332 /* must be mesh, verify */
333 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
334 res->pub.len_information_elements);
335 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
336 res->pub.information_elements,
337 res->pub.len_information_elements);
338 if (!meshid || !meshcfg ||
339 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
341 kref_put(&res->ref, bss_release);
346 spin_lock_bh(&dev->bss_lock);
348 found = rb_find_bss(dev, res);
350 if (found && overwrite) {
351 list_replace(&found->list, &res->list);
352 rb_replace_node(&found->rbn, &res->rbn,
354 kref_put(&found->ref, bss_release);
357 kref_get(&found->ref);
358 found->pub.beacon_interval = res->pub.beacon_interval;
359 found->pub.tsf = res->pub.tsf;
360 found->pub.signal = res->pub.signal;
361 found->pub.signal_type = res->pub.signal_type;
362 found->pub.capability = res->pub.capability;
364 kref_put(&res->ref, bss_release);
366 /* this "consumes" the reference */
367 list_add_tail(&res->list, &dev->bss_list);
368 rb_insert_bss(dev, res);
372 dev->bss_generation++;
373 spin_unlock_bh(&dev->bss_lock);
375 kref_get(&found->ref);
379 struct cfg80211_bss *
380 cfg80211_inform_bss_frame(struct wiphy *wiphy,
381 struct ieee80211_channel *channel,
382 struct ieee80211_mgmt *mgmt, size_t len,
383 s32 signal, enum cfg80211_signal_type sigtype,
386 struct cfg80211_internal_bss *res;
387 size_t ielen = len - offsetof(struct ieee80211_mgmt,
388 u.probe_resp.variable);
390 size_t privsz = wiphy->bss_priv_size;
392 if (WARN_ON(sigtype == NL80211_BSS_SIGNAL_UNSPEC &&
393 (signal < 0 || signal > 100)))
396 if (WARN_ON(!mgmt || !wiphy ||
397 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
400 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
404 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
405 res->pub.channel = channel;
406 res->pub.signal_type = sigtype;
407 res->pub.signal = signal;
408 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
409 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
410 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
411 /* point to after the private area */
412 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
413 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
414 res->pub.len_information_elements = ielen;
416 kref_init(&res->ref);
418 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
420 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
424 /* cfg80211_bss_update gives us a referenced result */
427 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
429 void cfg80211_put_bss(struct cfg80211_bss *pub)
431 struct cfg80211_internal_bss *bss;
436 bss = container_of(pub, struct cfg80211_internal_bss, pub);
437 kref_put(&bss->ref, bss_release);
439 EXPORT_SYMBOL(cfg80211_put_bss);
441 void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
443 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
444 struct cfg80211_internal_bss *bss;
449 bss = container_of(pub, struct cfg80211_internal_bss, pub);
451 spin_lock_bh(&dev->bss_lock);
453 list_del(&bss->list);
454 rb_erase(&bss->rbn, &dev->bss_tree);
456 spin_unlock_bh(&dev->bss_lock);
458 kref_put(&bss->ref, bss_release);
460 EXPORT_SYMBOL(cfg80211_unlink_bss);
462 #ifdef CONFIG_WIRELESS_EXT
463 int cfg80211_wext_siwscan(struct net_device *dev,
464 struct iw_request_info *info,
465 union iwreq_data *wrqu, char *extra)
467 struct cfg80211_registered_device *rdev;
469 struct iw_scan_req *wreq = NULL;
470 struct cfg80211_scan_request *creq;
471 int i, err, n_channels = 0;
472 enum ieee80211_band band;
474 if (!netif_running(dev))
477 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
480 return PTR_ERR(rdev);
482 if (rdev->scan_req) {
487 wiphy = &rdev->wiphy;
489 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
490 if (wiphy->bands[band])
491 n_channels += wiphy->bands[band]->n_channels;
493 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
494 n_channels * sizeof(void *),
502 creq->ifidx = dev->ifindex;
503 creq->ssids = (void *)(creq + 1);
504 creq->channels = (void *)(creq->ssids + 1);
505 creq->n_channels = n_channels;
510 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
512 if (!wiphy->bands[band])
514 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
515 creq->channels[i] = &wiphy->bands[band]->channels[j];
520 /* translate scan request */
521 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
522 wreq = (struct iw_scan_req *)extra;
524 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
525 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
527 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
528 creq->ssids[0].ssid_len = wreq->essid_len;
530 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
534 rdev->scan_req = creq;
535 err = rdev->ops->scan(wiphy, dev, creq);
537 rdev->scan_req = NULL;
541 cfg80211_put_dev(rdev);
544 EXPORT_SYMBOL(cfg80211_wext_siwscan);
546 static void ieee80211_scan_add_ies(struct iw_request_info *info,
547 struct cfg80211_bss *bss,
548 char **current_ev, char *end_buf)
550 u8 *pos, *end, *next;
553 if (!bss->information_elements ||
554 !bss->len_information_elements)
558 * If needed, fragment the IEs buffer (at IE boundaries) into short
559 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
561 pos = bss->information_elements;
562 end = pos + bss->len_information_elements;
564 while (end - pos > IW_GENERIC_IE_MAX) {
565 next = pos + 2 + pos[1];
566 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
567 next = next + 2 + next[1];
569 memset(&iwe, 0, sizeof(iwe));
571 iwe.u.data.length = next - pos;
572 *current_ev = iwe_stream_add_point(info, *current_ev,
579 memset(&iwe, 0, sizeof(iwe));
581 iwe.u.data.length = end - pos;
582 *current_ev = iwe_stream_add_point(info, *current_ev,
589 ieee80211_bss(struct iw_request_info *info,
590 struct cfg80211_internal_bss *bss,
591 char *current_ev, char *end_buf)
595 u8 *ie = bss->pub.information_elements;
596 int rem = bss->pub.len_information_elements, i;
599 memset(&iwe, 0, sizeof(iwe));
601 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
602 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
603 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
606 memset(&iwe, 0, sizeof(iwe));
607 iwe.cmd = SIOCGIWFREQ;
608 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
610 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
613 memset(&iwe, 0, sizeof(iwe));
614 iwe.cmd = SIOCGIWFREQ;
615 iwe.u.freq.m = bss->pub.channel->center_freq;
617 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
620 if (bss->pub.signal_type != CFG80211_SIGNAL_TYPE_NONE) {
621 memset(&iwe, 0, sizeof(iwe));
623 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
624 IW_QUAL_NOISE_INVALID |
625 IW_QUAL_QUAL_INVALID;
626 switch (bss->pub.signal_type) {
627 case CFG80211_SIGNAL_TYPE_MBM:
628 iwe.u.qual.level = bss->pub.signal / 100;
629 iwe.u.qual.updated |= IW_QUAL_DBM;
631 case CFG80211_SIGNAL_TYPE_UNSPEC:
632 iwe.u.qual.level = bss->pub.signal;
638 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
639 &iwe, IW_EV_QUAL_LEN);
642 memset(&iwe, 0, sizeof(iwe));
643 iwe.cmd = SIOCGIWENCODE;
644 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
645 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
647 iwe.u.data.flags = IW_ENCODE_DISABLED;
648 iwe.u.data.length = 0;
649 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
659 memset(&iwe, 0, sizeof(iwe));
660 iwe.cmd = SIOCGIWESSID;
661 iwe.u.data.length = ie[1];
662 iwe.u.data.flags = 1;
663 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
666 case WLAN_EID_MESH_ID:
667 memset(&iwe, 0, sizeof(iwe));
668 iwe.cmd = SIOCGIWESSID;
669 iwe.u.data.length = ie[1];
670 iwe.u.data.flags = 1;
671 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
674 case WLAN_EID_MESH_CONFIG:
676 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
678 buf = kmalloc(50, GFP_ATOMIC);
682 memset(&iwe, 0, sizeof(iwe));
683 iwe.cmd = IWEVCUSTOM;
684 sprintf(buf, "Mesh network (version %d)", cfg[0]);
685 iwe.u.data.length = strlen(buf);
686 current_ev = iwe_stream_add_point(info, current_ev,
689 sprintf(buf, "Path Selection Protocol ID: "
690 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
692 iwe.u.data.length = strlen(buf);
693 current_ev = iwe_stream_add_point(info, current_ev,
696 sprintf(buf, "Path Selection Metric ID: "
697 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
699 iwe.u.data.length = strlen(buf);
700 current_ev = iwe_stream_add_point(info, current_ev,
703 sprintf(buf, "Congestion Control Mode ID: "
704 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
706 iwe.u.data.length = strlen(buf);
707 current_ev = iwe_stream_add_point(info, current_ev,
710 sprintf(buf, "Channel Precedence: "
711 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
713 iwe.u.data.length = strlen(buf);
714 current_ev = iwe_stream_add_point(info, current_ev,
719 case WLAN_EID_SUPP_RATES:
720 case WLAN_EID_EXT_SUPP_RATES:
721 /* display all supported rates in readable format */
722 p = current_ev + iwe_stream_lcp_len(info);
724 memset(&iwe, 0, sizeof(iwe));
725 iwe.cmd = SIOCGIWRATE;
726 /* Those two flags are ignored... */
727 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
729 for (i = 0; i < ie[1]; i++) {
730 iwe.u.bitrate.value =
731 ((ie[i + 2] & 0x7f) * 500000);
732 p = iwe_stream_add_value(info, current_ev, p,
733 end_buf, &iwe, IW_EV_PARAM_LEN);
742 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
744 memset(&iwe, 0, sizeof(iwe));
745 iwe.cmd = SIOCGIWMODE;
747 iwe.u.mode = IW_MODE_MESH;
748 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
749 iwe.u.mode = IW_MODE_MASTER;
751 iwe.u.mode = IW_MODE_ADHOC;
752 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
753 &iwe, IW_EV_UINT_LEN);
756 buf = kmalloc(30, GFP_ATOMIC);
758 memset(&iwe, 0, sizeof(iwe));
759 iwe.cmd = IWEVCUSTOM;
760 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
761 iwe.u.data.length = strlen(buf);
762 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
764 memset(&iwe, 0, sizeof(iwe));
765 iwe.cmd = IWEVCUSTOM;
766 sprintf(buf, " Last beacon: %dms ago",
767 jiffies_to_msecs(jiffies - bss->ts));
768 iwe.u.data.length = strlen(buf);
769 current_ev = iwe_stream_add_point(info, current_ev,
774 ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf);
780 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
781 struct iw_request_info *info,
782 char *buf, size_t len)
784 char *current_ev = buf;
785 char *end_buf = buf + len;
786 struct cfg80211_internal_bss *bss;
788 spin_lock_bh(&dev->bss_lock);
789 cfg80211_bss_expire(dev);
791 list_for_each_entry(bss, &dev->bss_list, list) {
792 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
793 spin_unlock_bh(&dev->bss_lock);
796 current_ev = ieee80211_bss(info, bss,
797 current_ev, end_buf);
799 spin_unlock_bh(&dev->bss_lock);
800 return current_ev - buf;
804 int cfg80211_wext_giwscan(struct net_device *dev,
805 struct iw_request_info *info,
806 struct iw_point *data, char *extra)
808 struct cfg80211_registered_device *rdev;
811 if (!netif_running(dev))
814 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
817 return PTR_ERR(rdev);
819 if (rdev->scan_req) {
824 res = ieee80211_scan_results(rdev, info, extra, data->length);
832 cfg80211_put_dev(rdev);
835 EXPORT_SYMBOL(cfg80211_wext_giwscan);