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);
62 /* must hold dev->bss_lock! */
63 void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
65 struct cfg80211_internal_bss *bss, *tmp;
68 list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
69 if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
72 rb_erase(&bss->rbn, &dev->bss_tree);
73 kref_put(&bss->ref, bss_release);
78 dev->bss_generation++;
81 static u8 *find_ie(u8 num, u8 *ies, size_t len)
83 while (len > 2 && ies[0] != num) {
94 static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
96 const u8 *ie1 = find_ie(num, ies1, len1);
97 const u8 *ie2 = find_ie(num, ies2, len2);
105 r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
106 if (r == 0 && ie1[1] != ie2[1])
107 return ie2[1] - ie1[1];
111 static bool is_bss(struct cfg80211_bss *a,
113 const u8 *ssid, size_t ssid_len)
117 if (compare_ether_addr(a->bssid, bssid))
120 ssidie = find_ie(WLAN_EID_SSID,
121 a->information_elements,
122 a->len_information_elements);
125 if (ssidie[1] != ssid_len)
127 return memcmp(ssidie + 2, ssid, ssid_len) == 0;
130 static bool is_mesh(struct cfg80211_bss *a,
131 const u8 *meshid, size_t meshidlen,
136 if (!is_zero_ether_addr(a->bssid))
139 ie = find_ie(WLAN_EID_MESH_ID,
140 a->information_elements,
141 a->len_information_elements);
144 if (ie[1] != meshidlen)
146 if (memcmp(ie + 2, meshid, meshidlen))
149 ie = find_ie(WLAN_EID_MESH_CONFIG,
150 a->information_elements,
151 a->len_information_elements);
152 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
156 * Ignore mesh capability (last two bytes of the IE) when
157 * comparing since that may differ between stations taking
158 * part in the same mesh.
160 return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
163 static int cmp_bss(struct cfg80211_bss *a,
164 struct cfg80211_bss *b)
168 if (a->channel != b->channel)
169 return b->channel->center_freq - a->channel->center_freq;
171 r = memcmp(a->bssid, b->bssid, ETH_ALEN);
175 if (is_zero_ether_addr(a->bssid)) {
176 r = cmp_ies(WLAN_EID_MESH_ID,
177 a->information_elements,
178 a->len_information_elements,
179 b->information_elements,
180 b->len_information_elements);
183 return cmp_ies(WLAN_EID_MESH_CONFIG,
184 a->information_elements,
185 a->len_information_elements,
186 b->information_elements,
187 b->len_information_elements);
190 return cmp_ies(WLAN_EID_SSID,
191 a->information_elements,
192 a->len_information_elements,
193 b->information_elements,
194 b->len_information_elements);
197 struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
198 struct ieee80211_channel *channel,
200 const u8 *ssid, size_t ssid_len)
202 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
203 struct cfg80211_internal_bss *bss, *res = NULL;
205 spin_lock_bh(&dev->bss_lock);
207 list_for_each_entry(bss, &dev->bss_list, list) {
208 if (channel && bss->pub.channel != channel)
210 if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
217 spin_unlock_bh(&dev->bss_lock);
222 EXPORT_SYMBOL(cfg80211_get_bss);
224 struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
225 struct ieee80211_channel *channel,
226 const u8 *meshid, size_t meshidlen,
229 struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
230 struct cfg80211_internal_bss *bss, *res = NULL;
232 spin_lock_bh(&dev->bss_lock);
234 list_for_each_entry(bss, &dev->bss_list, list) {
235 if (channel && bss->pub.channel != channel)
237 if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
244 spin_unlock_bh(&dev->bss_lock);
249 EXPORT_SYMBOL(cfg80211_get_mesh);
252 static void rb_insert_bss(struct cfg80211_registered_device *dev,
253 struct cfg80211_internal_bss *bss)
255 struct rb_node **p = &dev->bss_tree.rb_node;
256 struct rb_node *parent = NULL;
257 struct cfg80211_internal_bss *tbss;
262 tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
264 cmp = cmp_bss(&bss->pub, &tbss->pub);
267 /* will sort of leak this BSS */
277 rb_link_node(&bss->rbn, parent, p);
278 rb_insert_color(&bss->rbn, &dev->bss_tree);
281 static struct cfg80211_internal_bss *
282 rb_find_bss(struct cfg80211_registered_device *dev,
283 struct cfg80211_internal_bss *res)
285 struct rb_node *n = dev->bss_tree.rb_node;
286 struct cfg80211_internal_bss *bss;
290 bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
291 r = cmp_bss(&res->pub, &bss->pub);
304 static struct cfg80211_internal_bss *
305 cfg80211_bss_update(struct cfg80211_registered_device *dev,
306 struct cfg80211_internal_bss *res,
309 struct cfg80211_internal_bss *found = NULL;
310 const u8 *meshid, *meshcfg;
313 * The reference to "res" is donated to this function.
316 if (WARN_ON(!res->pub.channel)) {
317 kref_put(&res->ref, bss_release);
323 if (is_zero_ether_addr(res->pub.bssid)) {
324 /* must be mesh, verify */
325 meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
326 res->pub.len_information_elements);
327 meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
328 res->pub.information_elements,
329 res->pub.len_information_elements);
330 if (!meshid || !meshcfg ||
331 meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
333 kref_put(&res->ref, bss_release);
338 spin_lock_bh(&dev->bss_lock);
340 found = rb_find_bss(dev, res);
342 if (found && overwrite) {
343 list_replace(&found->list, &res->list);
344 rb_replace_node(&found->rbn, &res->rbn,
346 kref_put(&found->ref, bss_release);
349 kref_get(&found->ref);
350 found->pub.beacon_interval = res->pub.beacon_interval;
351 found->pub.tsf = res->pub.tsf;
352 found->pub.signal = res->pub.signal;
353 found->pub.signal_type = res->pub.signal_type;
354 found->pub.capability = res->pub.capability;
356 kref_put(&res->ref, bss_release);
358 /* this "consumes" the reference */
359 list_add_tail(&res->list, &dev->bss_list);
360 rb_insert_bss(dev, res);
364 dev->bss_generation++;
365 spin_unlock_bh(&dev->bss_lock);
367 kref_get(&found->ref);
371 struct cfg80211_bss *
372 cfg80211_inform_bss_frame(struct wiphy *wiphy,
373 struct ieee80211_channel *channel,
374 struct ieee80211_mgmt *mgmt, size_t len,
375 s32 signal, enum cfg80211_signal_type sigtype,
378 struct cfg80211_internal_bss *res;
379 size_t ielen = len - offsetof(struct ieee80211_mgmt,
380 u.probe_resp.variable);
382 size_t privsz = wiphy->bss_priv_size;
384 if (WARN_ON(sigtype == NL80211_BSS_SIGNAL_UNSPEC &&
385 (signal < 0 || signal > 100)))
388 if (WARN_ON(!mgmt || !wiphy ||
389 len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
392 res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
396 memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
397 res->pub.channel = channel;
398 res->pub.signal_type = sigtype;
399 res->pub.signal = signal;
400 res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
401 res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
402 res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
403 /* point to after the private area */
404 res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
405 memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
406 res->pub.len_information_elements = ielen;
408 kref_init(&res->ref);
410 overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
412 res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
416 /* cfg80211_bss_update gives us a referenced result */
419 EXPORT_SYMBOL(cfg80211_inform_bss_frame);
421 void cfg80211_put_bss(struct cfg80211_bss *pub)
423 struct cfg80211_internal_bss *bss;
428 bss = container_of(pub, struct cfg80211_internal_bss, pub);
429 kref_put(&bss->ref, bss_release);
431 EXPORT_SYMBOL(cfg80211_put_bss);
433 #ifdef CONFIG_WIRELESS_EXT
434 int cfg80211_wext_siwscan(struct net_device *dev,
435 struct iw_request_info *info,
436 union iwreq_data *wrqu, char *extra)
438 struct cfg80211_registered_device *rdev;
440 struct iw_scan_req *wreq = NULL;
441 struct cfg80211_scan_request *creq;
442 int i, err, n_channels = 0;
443 enum ieee80211_band band;
445 if (!netif_running(dev))
448 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
451 return PTR_ERR(rdev);
453 if (rdev->scan_req) {
458 wiphy = &rdev->wiphy;
460 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
461 if (wiphy->bands[band])
462 n_channels += wiphy->bands[band]->n_channels;
464 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
465 n_channels * sizeof(void *),
473 creq->ifidx = dev->ifindex;
474 creq->ssids = (void *)(creq + 1);
475 creq->channels = (void *)(creq->ssids + 1);
476 creq->n_channels = n_channels;
481 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
483 if (!wiphy->bands[band])
485 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
486 creq->channels[i] = &wiphy->bands[band]->channels[j];
491 /* translate scan request */
492 if (wrqu->data.length == sizeof(struct iw_scan_req)) {
493 wreq = (struct iw_scan_req *)extra;
495 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
496 if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
498 memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
499 creq->ssids[0].ssid_len = wreq->essid_len;
501 if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
505 rdev->scan_req = creq;
506 err = rdev->ops->scan(wiphy, dev, creq);
508 rdev->scan_req = NULL;
512 cfg80211_put_dev(rdev);
515 EXPORT_SYMBOL(cfg80211_wext_siwscan);
517 static void ieee80211_scan_add_ies(struct iw_request_info *info,
518 struct cfg80211_bss *bss,
519 char **current_ev, char *end_buf)
521 u8 *pos, *end, *next;
524 if (!bss->information_elements ||
525 !bss->len_information_elements)
529 * If needed, fragment the IEs buffer (at IE boundaries) into short
530 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
532 pos = bss->information_elements;
533 end = pos + bss->len_information_elements;
535 while (end - pos > IW_GENERIC_IE_MAX) {
536 next = pos + 2 + pos[1];
537 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
538 next = next + 2 + next[1];
540 memset(&iwe, 0, sizeof(iwe));
542 iwe.u.data.length = next - pos;
543 *current_ev = iwe_stream_add_point(info, *current_ev,
550 memset(&iwe, 0, sizeof(iwe));
552 iwe.u.data.length = end - pos;
553 *current_ev = iwe_stream_add_point(info, *current_ev,
560 ieee80211_bss(struct iw_request_info *info,
561 struct cfg80211_internal_bss *bss,
562 char *current_ev, char *end_buf)
566 u8 *ie = bss->pub.information_elements;
567 int rem = bss->pub.len_information_elements, i;
570 memset(&iwe, 0, sizeof(iwe));
572 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
573 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
574 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
577 memset(&iwe, 0, sizeof(iwe));
578 iwe.cmd = SIOCGIWFREQ;
579 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
581 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
584 memset(&iwe, 0, sizeof(iwe));
585 iwe.cmd = SIOCGIWFREQ;
586 iwe.u.freq.m = bss->pub.channel->center_freq;
588 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
591 if (bss->pub.signal_type != CFG80211_SIGNAL_TYPE_NONE) {
592 memset(&iwe, 0, sizeof(iwe));
594 iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
595 IW_QUAL_NOISE_INVALID |
596 IW_QUAL_QUAL_INVALID;
597 switch (bss->pub.signal_type) {
598 case CFG80211_SIGNAL_TYPE_MBM:
599 iwe.u.qual.level = bss->pub.signal / 100;
600 iwe.u.qual.updated |= IW_QUAL_DBM;
602 case CFG80211_SIGNAL_TYPE_UNSPEC:
603 iwe.u.qual.level = bss->pub.signal;
609 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
610 &iwe, IW_EV_QUAL_LEN);
613 memset(&iwe, 0, sizeof(iwe));
614 iwe.cmd = SIOCGIWENCODE;
615 if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
616 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
618 iwe.u.data.flags = IW_ENCODE_DISABLED;
619 iwe.u.data.length = 0;
620 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
630 memset(&iwe, 0, sizeof(iwe));
631 iwe.cmd = SIOCGIWESSID;
632 iwe.u.data.length = ie[1];
633 iwe.u.data.flags = 1;
634 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
637 case WLAN_EID_MESH_ID:
638 memset(&iwe, 0, sizeof(iwe));
639 iwe.cmd = SIOCGIWESSID;
640 iwe.u.data.length = ie[1];
641 iwe.u.data.flags = 1;
642 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
645 case WLAN_EID_MESH_CONFIG:
647 if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
649 buf = kmalloc(50, GFP_ATOMIC);
653 memset(&iwe, 0, sizeof(iwe));
654 iwe.cmd = IWEVCUSTOM;
655 sprintf(buf, "Mesh network (version %d)", cfg[0]);
656 iwe.u.data.length = strlen(buf);
657 current_ev = iwe_stream_add_point(info, current_ev,
660 sprintf(buf, "Path Selection Protocol ID: "
661 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
663 iwe.u.data.length = strlen(buf);
664 current_ev = iwe_stream_add_point(info, current_ev,
667 sprintf(buf, "Path Selection Metric ID: "
668 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
670 iwe.u.data.length = strlen(buf);
671 current_ev = iwe_stream_add_point(info, current_ev,
674 sprintf(buf, "Congestion Control Mode ID: "
675 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
677 iwe.u.data.length = strlen(buf);
678 current_ev = iwe_stream_add_point(info, current_ev,
681 sprintf(buf, "Channel Precedence: "
682 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
684 iwe.u.data.length = strlen(buf);
685 current_ev = iwe_stream_add_point(info, current_ev,
690 case WLAN_EID_SUPP_RATES:
691 case WLAN_EID_EXT_SUPP_RATES:
692 /* display all supported rates in readable format */
693 p = current_ev + iwe_stream_lcp_len(info);
695 memset(&iwe, 0, sizeof(iwe));
696 iwe.cmd = SIOCGIWRATE;
697 /* Those two flags are ignored... */
698 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
700 for (i = 0; i < ie[1]; i++) {
701 iwe.u.bitrate.value =
702 ((ie[i + 2] & 0x7f) * 500000);
703 p = iwe_stream_add_value(info, current_ev, p,
704 end_buf, &iwe, IW_EV_PARAM_LEN);
713 if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
715 memset(&iwe, 0, sizeof(iwe));
716 iwe.cmd = SIOCGIWMODE;
718 iwe.u.mode = IW_MODE_MESH;
719 else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
720 iwe.u.mode = IW_MODE_MASTER;
722 iwe.u.mode = IW_MODE_ADHOC;
723 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
724 &iwe, IW_EV_UINT_LEN);
727 buf = kmalloc(30, GFP_ATOMIC);
729 memset(&iwe, 0, sizeof(iwe));
730 iwe.cmd = IWEVCUSTOM;
731 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
732 iwe.u.data.length = strlen(buf);
733 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
735 memset(&iwe, 0, sizeof(iwe));
736 iwe.cmd = IWEVCUSTOM;
737 sprintf(buf, " Last beacon: %dms ago",
738 jiffies_to_msecs(jiffies - bss->ts));
739 iwe.u.data.length = strlen(buf);
740 current_ev = iwe_stream_add_point(info, current_ev,
745 ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf);
751 static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
752 struct iw_request_info *info,
753 char *buf, size_t len)
755 char *current_ev = buf;
756 char *end_buf = buf + len;
757 struct cfg80211_internal_bss *bss;
759 spin_lock_bh(&dev->bss_lock);
760 cfg80211_bss_expire(dev);
762 list_for_each_entry(bss, &dev->bss_list, list) {
763 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
764 spin_unlock_bh(&dev->bss_lock);
767 current_ev = ieee80211_bss(info, bss,
768 current_ev, end_buf);
770 spin_unlock_bh(&dev->bss_lock);
771 return current_ev - buf;
775 int cfg80211_wext_giwscan(struct net_device *dev,
776 struct iw_request_info *info,
777 struct iw_point *data, char *extra)
779 struct cfg80211_registered_device *rdev;
782 if (!netif_running(dev))
785 rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
788 return PTR_ERR(rdev);
790 if (rdev->scan_req) {
795 res = ieee80211_scan_results(rdev, info, extra, data->length);
803 cfg80211_put_dev(rdev);
806 EXPORT_SYMBOL(cfg80211_wext_giwscan);