Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * cfg80211 scan result handling |
| 3 | * |
| 4 | * Copyright 2008 Johannes Berg <johannes@sipsolutions.net> |
| 5 | */ |
| 6 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 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> |
| 13 | #include <net/arp.h> |
| 14 | #include <net/cfg80211.h> |
Johannes Berg | 262eb9b2 | 2011-07-13 10:39:09 +0200 | [diff] [blame] | 15 | #include <net/cfg80211-wext.h> |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 16 | #include <net/iw_handler.h> |
| 17 | #include "core.h" |
| 18 | #include "nl80211.h" |
Johannes Berg | a9a1162 | 2009-07-27 12:01:53 +0200 | [diff] [blame] | 19 | #include "wext-compat.h" |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 20 | |
Rajkumar Manoharan | f9616e0 | 2012-04-13 16:38:40 +0530 | [diff] [blame] | 21 | #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 22 | |
Amitkumar Karwar | e8e27c6 | 2012-10-11 21:03:33 -0700 | [diff] [blame] | 23 | static void bss_release(struct kref *ref) |
| 24 | { |
| 25 | struct cfg80211_internal_bss *bss; |
| 26 | |
| 27 | bss = container_of(ref, struct cfg80211_internal_bss, ref); |
| 28 | if (bss->pub.free_priv) |
| 29 | bss->pub.free_priv(&bss->pub); |
| 30 | |
| 31 | if (bss->beacon_ies_allocated) |
| 32 | kfree(bss->pub.beacon_ies); |
| 33 | if (bss->proberesp_ies_allocated) |
| 34 | kfree(bss->pub.proberesp_ies); |
| 35 | |
| 36 | BUG_ON(atomic_read(&bss->hold)); |
| 37 | |
| 38 | kfree(bss); |
| 39 | } |
| 40 | |
| 41 | /* must hold dev->bss_lock! */ |
| 42 | static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev, |
| 43 | struct cfg80211_internal_bss *bss) |
| 44 | { |
| 45 | list_del_init(&bss->list); |
| 46 | rb_erase(&bss->rbn, &dev->bss_tree); |
| 47 | kref_put(&bss->ref, bss_release); |
| 48 | } |
| 49 | |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 50 | /* must hold dev->bss_lock! */ |
| 51 | static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev, |
| 52 | unsigned long expire_time) |
| 53 | { |
| 54 | struct cfg80211_internal_bss *bss, *tmp; |
| 55 | bool expired = false; |
| 56 | |
| 57 | list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) { |
| 58 | if (atomic_read(&bss->hold)) |
| 59 | continue; |
| 60 | if (!time_after(expire_time, bss->ts)) |
| 61 | continue; |
| 62 | |
| 63 | __cfg80211_unlink_bss(dev, bss); |
| 64 | expired = true; |
| 65 | } |
| 66 | |
| 67 | if (expired) |
| 68 | dev->bss_generation++; |
| 69 | } |
| 70 | |
Johannes Berg | 01a0ac4 | 2009-08-20 21:36:16 +0200 | [diff] [blame] | 71 | void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 72 | { |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 73 | struct cfg80211_scan_request *request; |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 74 | struct wireless_dev *wdev; |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 75 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 76 | union iwreq_data wrqu; |
| 77 | #endif |
| 78 | |
Johannes Berg | 01a0ac4 | 2009-08-20 21:36:16 +0200 | [diff] [blame] | 79 | ASSERT_RDEV_LOCK(rdev); |
| 80 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 81 | request = rdev->scan_req; |
| 82 | |
Johannes Berg | 01a0ac4 | 2009-08-20 21:36:16 +0200 | [diff] [blame] | 83 | if (!request) |
| 84 | return; |
| 85 | |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 86 | wdev = request->wdev; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 87 | |
Johannes Berg | 6829c87 | 2009-07-02 09:13:27 +0200 | [diff] [blame] | 88 | /* |
| 89 | * This must be before sending the other events! |
| 90 | * Otherwise, wpa_supplicant gets completely confused with |
| 91 | * wext events. |
| 92 | */ |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 93 | if (wdev->netdev) |
| 94 | cfg80211_sme_scan_done(wdev->netdev); |
Johannes Berg | 6829c87 | 2009-07-02 09:13:27 +0200 | [diff] [blame] | 95 | |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 96 | if (request->aborted) { |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 97 | nl80211_send_scan_aborted(rdev, wdev); |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 98 | } else { |
| 99 | if (request->flags & NL80211_SCAN_FLAG_FLUSH) { |
| 100 | /* flush entries from previous scans */ |
| 101 | spin_lock_bh(&rdev->bss_lock); |
| 102 | __cfg80211_bss_expire(rdev, request->scan_start); |
| 103 | spin_unlock_bh(&rdev->bss_lock); |
| 104 | } |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 105 | nl80211_send_scan_done(rdev, wdev); |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 106 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 107 | |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 108 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 109 | if (wdev->netdev && !request->aborted) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 110 | memset(&wrqu, 0, sizeof(wrqu)); |
| 111 | |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 112 | wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 113 | } |
| 114 | #endif |
| 115 | |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 116 | if (wdev->netdev) |
| 117 | dev_put(wdev->netdev); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 118 | |
Johannes Berg | 36e6fea | 2009-08-12 22:21:21 +0200 | [diff] [blame] | 119 | rdev->scan_req = NULL; |
Johannes Berg | 01a0ac4 | 2009-08-20 21:36:16 +0200 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * OK. If this is invoked with "leak" then we can't |
| 123 | * free this ... but we've cleaned it up anyway. The |
| 124 | * driver failed to call the scan_done callback, so |
| 125 | * all bets are off, it might still be trying to use |
| 126 | * the scan request or not ... if it accesses the dev |
| 127 | * in there (it shouldn't anyway) then it may crash. |
| 128 | */ |
| 129 | if (!leak) |
| 130 | kfree(request); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 131 | } |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 132 | |
Johannes Berg | 36e6fea | 2009-08-12 22:21:21 +0200 | [diff] [blame] | 133 | void __cfg80211_scan_done(struct work_struct *wk) |
| 134 | { |
| 135 | struct cfg80211_registered_device *rdev; |
| 136 | |
| 137 | rdev = container_of(wk, struct cfg80211_registered_device, |
| 138 | scan_done_wk); |
| 139 | |
| 140 | cfg80211_lock_rdev(rdev); |
Johannes Berg | 01a0ac4 | 2009-08-20 21:36:16 +0200 | [diff] [blame] | 141 | ___cfg80211_scan_done(rdev, false); |
Johannes Berg | 36e6fea | 2009-08-12 22:21:21 +0200 | [diff] [blame] | 142 | cfg80211_unlock_rdev(rdev); |
| 143 | } |
| 144 | |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 145 | void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted) |
| 146 | { |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 147 | WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); |
| 148 | |
| 149 | request->aborted = aborted; |
Alban Browaeys | e60d744 | 2009-11-25 15:13:00 +0100 | [diff] [blame] | 150 | queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk); |
Johannes Berg | 667503d | 2009-07-07 03:56:11 +0200 | [diff] [blame] | 151 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 152 | EXPORT_SYMBOL(cfg80211_scan_done); |
| 153 | |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 154 | void __cfg80211_sched_scan_results(struct work_struct *wk) |
| 155 | { |
| 156 | struct cfg80211_registered_device *rdev; |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 157 | struct cfg80211_sched_scan_request *request; |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 158 | |
| 159 | rdev = container_of(wk, struct cfg80211_registered_device, |
| 160 | sched_scan_results_wk); |
| 161 | |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 162 | request = rdev->sched_scan_req; |
| 163 | |
Luciano Coelho | c10841c | 2011-06-30 08:32:41 +0300 | [diff] [blame] | 164 | mutex_lock(&rdev->sched_scan_mtx); |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 165 | |
| 166 | /* we don't have sched_scan_req anymore if the scan is stopping */ |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 167 | if (request) { |
| 168 | if (request->flags & NL80211_SCAN_FLAG_FLUSH) { |
| 169 | /* flush entries from previous scans */ |
| 170 | spin_lock_bh(&rdev->bss_lock); |
| 171 | __cfg80211_bss_expire(rdev, request->scan_start); |
| 172 | spin_unlock_bh(&rdev->bss_lock); |
| 173 | request->scan_start = |
| 174 | jiffies + msecs_to_jiffies(request->interval); |
| 175 | } |
| 176 | nl80211_send_sched_scan_results(rdev, request->dev); |
| 177 | } |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 178 | |
Luciano Coelho | c10841c | 2011-06-30 08:32:41 +0300 | [diff] [blame] | 179 | mutex_unlock(&rdev->sched_scan_mtx); |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void cfg80211_sched_scan_results(struct wiphy *wiphy) |
| 183 | { |
| 184 | /* ignore if we're not scanning */ |
| 185 | if (wiphy_to_dev(wiphy)->sched_scan_req) |
| 186 | queue_work(cfg80211_wq, |
| 187 | &wiphy_to_dev(wiphy)->sched_scan_results_wk); |
| 188 | } |
| 189 | EXPORT_SYMBOL(cfg80211_sched_scan_results); |
| 190 | |
Luciano Coelho | 85a9994 | 2011-05-12 16:28:29 +0300 | [diff] [blame] | 191 | void cfg80211_sched_scan_stopped(struct wiphy *wiphy) |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 192 | { |
Luciano Coelho | 85a9994 | 2011-05-12 16:28:29 +0300 | [diff] [blame] | 193 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 194 | |
Luciano Coelho | c10841c | 2011-06-30 08:32:41 +0300 | [diff] [blame] | 195 | mutex_lock(&rdev->sched_scan_mtx); |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 196 | __cfg80211_stop_sched_scan(rdev, true); |
Luciano Coelho | c10841c | 2011-06-30 08:32:41 +0300 | [diff] [blame] | 197 | mutex_unlock(&rdev->sched_scan_mtx); |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 198 | } |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 199 | EXPORT_SYMBOL(cfg80211_sched_scan_stopped); |
| 200 | |
| 201 | int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev, |
| 202 | bool driver_initiated) |
| 203 | { |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 204 | struct net_device *dev; |
| 205 | |
Luciano Coelho | c10841c | 2011-06-30 08:32:41 +0300 | [diff] [blame] | 206 | lockdep_assert_held(&rdev->sched_scan_mtx); |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 207 | |
| 208 | if (!rdev->sched_scan_req) |
Luciano Coelho | 1a84ff7 | 2011-07-08 11:16:16 +0300 | [diff] [blame] | 209 | return -ENOENT; |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 210 | |
| 211 | dev = rdev->sched_scan_req->dev; |
| 212 | |
Luciano Coelho | 85a9994 | 2011-05-12 16:28:29 +0300 | [diff] [blame] | 213 | if (!driver_initiated) { |
Jesper Juhl | 3b4670f | 2011-06-29 22:49:33 +0200 | [diff] [blame] | 214 | int err = rdev->ops->sched_scan_stop(&rdev->wiphy, dev); |
Luciano Coelho | 85a9994 | 2011-05-12 16:28:29 +0300 | [diff] [blame] | 215 | if (err) |
| 216 | return err; |
| 217 | } |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 218 | |
| 219 | nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED); |
| 220 | |
| 221 | kfree(rdev->sched_scan_req); |
| 222 | rdev->sched_scan_req = NULL; |
| 223 | |
Jesper Juhl | 3b4670f | 2011-06-29 22:49:33 +0200 | [diff] [blame] | 224 | return 0; |
Luciano Coelho | 807f8a8 | 2011-05-11 17:09:35 +0300 | [diff] [blame] | 225 | } |
| 226 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 227 | /* must hold dev->bss_lock! */ |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame] | 228 | void cfg80211_bss_age(struct cfg80211_registered_device *dev, |
| 229 | unsigned long age_secs) |
| 230 | { |
| 231 | struct cfg80211_internal_bss *bss; |
| 232 | unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC); |
| 233 | |
| 234 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 235 | bss->ts -= age_jiffies; |
| 236 | } |
| 237 | } |
| 238 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 239 | void cfg80211_bss_expire(struct cfg80211_registered_device *dev) |
| 240 | { |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 241 | __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 244 | const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 245 | { |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 246 | while (len > 2 && ies[0] != eid) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 247 | len -= ies[1] + 2; |
| 248 | ies += ies[1] + 2; |
| 249 | } |
| 250 | if (len < 2) |
| 251 | return NULL; |
| 252 | if (len < 2 + ies[1]) |
| 253 | return NULL; |
| 254 | return ies; |
| 255 | } |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 256 | EXPORT_SYMBOL(cfg80211_find_ie); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 257 | |
Eliad Peller | 0c28ec5 | 2011-09-15 11:53:01 +0300 | [diff] [blame] | 258 | const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type, |
| 259 | const u8 *ies, int len) |
| 260 | { |
| 261 | struct ieee80211_vendor_ie *ie; |
| 262 | const u8 *pos = ies, *end = ies + len; |
| 263 | int ie_oui; |
| 264 | |
| 265 | while (pos < end) { |
| 266 | pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos, |
| 267 | end - pos); |
| 268 | if (!pos) |
| 269 | return NULL; |
| 270 | |
| 271 | if (end - pos < sizeof(*ie)) |
| 272 | return NULL; |
| 273 | |
| 274 | ie = (struct ieee80211_vendor_ie *)pos; |
| 275 | ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2]; |
| 276 | if (ie_oui == oui && ie->oui_type == oui_type) |
| 277 | return pos; |
| 278 | |
| 279 | pos += 2 + ie->len; |
| 280 | } |
| 281 | return NULL; |
| 282 | } |
| 283 | EXPORT_SYMBOL(cfg80211_find_vendor_ie); |
| 284 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 285 | static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2) |
| 286 | { |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 287 | const u8 *ie1 = cfg80211_find_ie(num, ies1, len1); |
| 288 | const u8 *ie2 = cfg80211_find_ie(num, ies2, len2); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 289 | |
Johannes Berg | 3b6ef63 | 2011-11-04 11:01:46 +0100 | [diff] [blame] | 290 | /* equal if both missing */ |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 291 | if (!ie1 && !ie2) |
| 292 | return 0; |
Johannes Berg | 3b6ef63 | 2011-11-04 11:01:46 +0100 | [diff] [blame] | 293 | /* sort missing IE before (left of) present IE */ |
| 294 | if (!ie1) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 295 | return -1; |
Johannes Berg | 3b6ef63 | 2011-11-04 11:01:46 +0100 | [diff] [blame] | 296 | if (!ie2) |
| 297 | return 1; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 298 | |
Johannes Berg | 3b6ef63 | 2011-11-04 11:01:46 +0100 | [diff] [blame] | 299 | /* sort by length first, then by contents */ |
| 300 | if (ie1[1] != ie2[1]) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 301 | return ie2[1] - ie1[1]; |
Johannes Berg | 3b6ef63 | 2011-11-04 11:01:46 +0100 | [diff] [blame] | 302 | return memcmp(ie1 + 2, ie2 + 2, ie1[1]); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static bool is_bss(struct cfg80211_bss *a, |
| 306 | const u8 *bssid, |
| 307 | const u8 *ssid, size_t ssid_len) |
| 308 | { |
| 309 | const u8 *ssidie; |
| 310 | |
Joe Perches | ac422d3 | 2012-05-08 18:56:55 +0000 | [diff] [blame] | 311 | if (bssid && !ether_addr_equal(a->bssid, bssid)) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 312 | return false; |
| 313 | |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 314 | if (!ssid) |
| 315 | return true; |
| 316 | |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 317 | ssidie = cfg80211_find_ie(WLAN_EID_SSID, |
| 318 | a->information_elements, |
| 319 | a->len_information_elements); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 320 | if (!ssidie) |
| 321 | return false; |
| 322 | if (ssidie[1] != ssid_len) |
| 323 | return false; |
| 324 | return memcmp(ssidie + 2, ssid, ssid_len) == 0; |
| 325 | } |
| 326 | |
Eliad Peller | 333ba73 | 2011-05-29 15:53:20 +0300 | [diff] [blame] | 327 | static bool is_mesh_bss(struct cfg80211_bss *a) |
| 328 | { |
| 329 | const u8 *ie; |
| 330 | |
| 331 | if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability)) |
| 332 | return false; |
| 333 | |
| 334 | ie = cfg80211_find_ie(WLAN_EID_MESH_ID, |
| 335 | a->information_elements, |
| 336 | a->len_information_elements); |
| 337 | if (!ie) |
| 338 | return false; |
| 339 | |
| 340 | ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, |
| 341 | a->information_elements, |
| 342 | a->len_information_elements); |
| 343 | if (!ie) |
| 344 | return false; |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 349 | static bool is_mesh(struct cfg80211_bss *a, |
| 350 | const u8 *meshid, size_t meshidlen, |
| 351 | const u8 *meshcfg) |
| 352 | { |
| 353 | const u8 *ie; |
| 354 | |
Eliad Peller | 333ba73 | 2011-05-29 15:53:20 +0300 | [diff] [blame] | 355 | if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability)) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 356 | return false; |
| 357 | |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 358 | ie = cfg80211_find_ie(WLAN_EID_MESH_ID, |
| 359 | a->information_elements, |
| 360 | a->len_information_elements); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 361 | if (!ie) |
| 362 | return false; |
| 363 | if (ie[1] != meshidlen) |
| 364 | return false; |
| 365 | if (memcmp(ie + 2, meshid, meshidlen)) |
| 366 | return false; |
| 367 | |
Johannes Berg | c21dbf9 | 2010-01-26 14:15:46 +0100 | [diff] [blame] | 368 | ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, |
| 369 | a->information_elements, |
| 370 | a->len_information_elements); |
Johannes Berg | cd3468b | 2009-07-29 22:07:44 +0200 | [diff] [blame] | 371 | if (!ie) |
| 372 | return false; |
Rui Paulo | 136cfa2 | 2009-11-18 18:40:00 +0000 | [diff] [blame] | 373 | if (ie[1] != sizeof(struct ieee80211_meshconf_ie)) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 374 | return false; |
| 375 | |
| 376 | /* |
| 377 | * Ignore mesh capability (last two bytes of the IE) when |
| 378 | * comparing since that may differ between stations taking |
| 379 | * part in the same mesh. |
| 380 | */ |
Rui Paulo | 136cfa2 | 2009-11-18 18:40:00 +0000 | [diff] [blame] | 381 | return memcmp(ie + 2, meshcfg, |
| 382 | sizeof(struct ieee80211_meshconf_ie) - 2) == 0; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Dmitry Tarnyagin | dd9dfb9 | 2011-11-04 17:12:07 +0100 | [diff] [blame] | 385 | static int cmp_bss_core(struct cfg80211_bss *a, |
| 386 | struct cfg80211_bss *b) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 387 | { |
| 388 | int r; |
| 389 | |
| 390 | if (a->channel != b->channel) |
| 391 | return b->channel->center_freq - a->channel->center_freq; |
| 392 | |
Eliad Peller | 333ba73 | 2011-05-29 15:53:20 +0300 | [diff] [blame] | 393 | if (is_mesh_bss(a) && is_mesh_bss(b)) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 394 | r = cmp_ies(WLAN_EID_MESH_ID, |
| 395 | a->information_elements, |
| 396 | a->len_information_elements, |
| 397 | b->information_elements, |
| 398 | b->len_information_elements); |
| 399 | if (r) |
| 400 | return r; |
| 401 | return cmp_ies(WLAN_EID_MESH_CONFIG, |
| 402 | a->information_elements, |
| 403 | a->len_information_elements, |
| 404 | b->information_elements, |
| 405 | b->len_information_elements); |
| 406 | } |
| 407 | |
Emmanuel Grumbach | ef9456a | 2012-04-30 10:23:36 +0300 | [diff] [blame] | 408 | /* |
| 409 | * we can't use compare_ether_addr here since we need a < > operator. |
| 410 | * The binary return value of compare_ether_addr isn't enough |
| 411 | */ |
| 412 | return memcmp(a->bssid, b->bssid, sizeof(a->bssid)); |
Dmitry Tarnyagin | dd9dfb9 | 2011-11-04 17:12:07 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static int cmp_bss(struct cfg80211_bss *a, |
| 416 | struct cfg80211_bss *b) |
| 417 | { |
| 418 | int r; |
| 419 | |
| 420 | r = cmp_bss_core(a, b); |
Javier Cardona | 0a35d36 | 2011-05-04 10:24:56 -0700 | [diff] [blame] | 421 | if (r) |
| 422 | return r; |
| 423 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 424 | return cmp_ies(WLAN_EID_SSID, |
| 425 | a->information_elements, |
| 426 | a->len_information_elements, |
| 427 | b->information_elements, |
| 428 | b->len_information_elements); |
| 429 | } |
| 430 | |
Dmitry Tarnyagin | dd9dfb9 | 2011-11-04 17:12:07 +0100 | [diff] [blame] | 431 | static int cmp_hidden_bss(struct cfg80211_bss *a, |
| 432 | struct cfg80211_bss *b) |
| 433 | { |
| 434 | const u8 *ie1; |
| 435 | const u8 *ie2; |
| 436 | int i; |
| 437 | int r; |
| 438 | |
| 439 | r = cmp_bss_core(a, b); |
| 440 | if (r) |
| 441 | return r; |
| 442 | |
| 443 | ie1 = cfg80211_find_ie(WLAN_EID_SSID, |
| 444 | a->information_elements, |
| 445 | a->len_information_elements); |
| 446 | ie2 = cfg80211_find_ie(WLAN_EID_SSID, |
| 447 | b->information_elements, |
| 448 | b->len_information_elements); |
| 449 | |
| 450 | /* Key comparator must use same algorithm in any rb-tree |
| 451 | * search function (order is important), otherwise ordering |
| 452 | * of items in the tree is broken and search gives incorrect |
| 453 | * results. This code uses same order as cmp_ies() does. */ |
| 454 | |
| 455 | /* sort missing IE before (left of) present IE */ |
| 456 | if (!ie1) |
| 457 | return -1; |
| 458 | if (!ie2) |
| 459 | return 1; |
| 460 | |
| 461 | /* zero-size SSID is used as an indication of the hidden bss */ |
| 462 | if (!ie2[1]) |
| 463 | return 0; |
| 464 | |
| 465 | /* sort by length first, then by contents */ |
| 466 | if (ie1[1] != ie2[1]) |
| 467 | return ie2[1] - ie1[1]; |
| 468 | |
| 469 | /* zeroed SSID ie is another indication of a hidden bss */ |
| 470 | for (i = 0; i < ie2[1]; i++) |
| 471 | if (ie2[i + 2]) |
| 472 | return -1; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 477 | struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy, |
| 478 | struct ieee80211_channel *channel, |
| 479 | const u8 *bssid, |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 480 | const u8 *ssid, size_t ssid_len, |
| 481 | u16 capa_mask, u16 capa_val) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 482 | { |
| 483 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 484 | struct cfg80211_internal_bss *bss, *res = NULL; |
Johannes Berg | ccb6c13 | 2010-07-13 10:55:38 +0200 | [diff] [blame] | 485 | unsigned long now = jiffies; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 486 | |
| 487 | spin_lock_bh(&dev->bss_lock); |
| 488 | |
| 489 | list_for_each_entry(bss, &dev->bss_list, list) { |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 490 | if ((bss->pub.capability & capa_mask) != capa_val) |
| 491 | continue; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 492 | if (channel && bss->pub.channel != channel) |
| 493 | continue; |
Johannes Berg | ccb6c13 | 2010-07-13 10:55:38 +0200 | [diff] [blame] | 494 | /* Don't get expired BSS structs */ |
| 495 | if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) && |
| 496 | !atomic_read(&bss->hold)) |
| 497 | continue; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 498 | if (is_bss(&bss->pub, bssid, ssid, ssid_len)) { |
| 499 | res = bss; |
| 500 | kref_get(&res->ref); |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | spin_unlock_bh(&dev->bss_lock); |
| 506 | if (!res) |
| 507 | return NULL; |
| 508 | return &res->pub; |
| 509 | } |
| 510 | EXPORT_SYMBOL(cfg80211_get_bss); |
| 511 | |
| 512 | struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy, |
| 513 | struct ieee80211_channel *channel, |
| 514 | const u8 *meshid, size_t meshidlen, |
| 515 | const u8 *meshcfg) |
| 516 | { |
| 517 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 518 | struct cfg80211_internal_bss *bss, *res = NULL; |
| 519 | |
| 520 | spin_lock_bh(&dev->bss_lock); |
| 521 | |
| 522 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 523 | if (channel && bss->pub.channel != channel) |
| 524 | continue; |
| 525 | if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) { |
| 526 | res = bss; |
| 527 | kref_get(&res->ref); |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | spin_unlock_bh(&dev->bss_lock); |
| 533 | if (!res) |
| 534 | return NULL; |
| 535 | return &res->pub; |
| 536 | } |
| 537 | EXPORT_SYMBOL(cfg80211_get_mesh); |
| 538 | |
| 539 | |
| 540 | static void rb_insert_bss(struct cfg80211_registered_device *dev, |
| 541 | struct cfg80211_internal_bss *bss) |
| 542 | { |
| 543 | struct rb_node **p = &dev->bss_tree.rb_node; |
| 544 | struct rb_node *parent = NULL; |
| 545 | struct cfg80211_internal_bss *tbss; |
| 546 | int cmp; |
| 547 | |
| 548 | while (*p) { |
| 549 | parent = *p; |
| 550 | tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn); |
| 551 | |
| 552 | cmp = cmp_bss(&bss->pub, &tbss->pub); |
| 553 | |
| 554 | if (WARN_ON(!cmp)) { |
| 555 | /* will sort of leak this BSS */ |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | if (cmp < 0) |
| 560 | p = &(*p)->rb_left; |
| 561 | else |
| 562 | p = &(*p)->rb_right; |
| 563 | } |
| 564 | |
| 565 | rb_link_node(&bss->rbn, parent, p); |
| 566 | rb_insert_color(&bss->rbn, &dev->bss_tree); |
| 567 | } |
| 568 | |
| 569 | static struct cfg80211_internal_bss * |
| 570 | rb_find_bss(struct cfg80211_registered_device *dev, |
| 571 | struct cfg80211_internal_bss *res) |
| 572 | { |
| 573 | struct rb_node *n = dev->bss_tree.rb_node; |
| 574 | struct cfg80211_internal_bss *bss; |
| 575 | int r; |
| 576 | |
| 577 | while (n) { |
| 578 | bss = rb_entry(n, struct cfg80211_internal_bss, rbn); |
| 579 | r = cmp_bss(&res->pub, &bss->pub); |
| 580 | |
| 581 | if (r == 0) |
| 582 | return bss; |
| 583 | else if (r < 0) |
| 584 | n = n->rb_left; |
| 585 | else |
| 586 | n = n->rb_right; |
| 587 | } |
| 588 | |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | static struct cfg80211_internal_bss * |
Dmitry Tarnyagin | dd9dfb9 | 2011-11-04 17:12:07 +0100 | [diff] [blame] | 593 | rb_find_hidden_bss(struct cfg80211_registered_device *dev, |
| 594 | struct cfg80211_internal_bss *res) |
| 595 | { |
| 596 | struct rb_node *n = dev->bss_tree.rb_node; |
| 597 | struct cfg80211_internal_bss *bss; |
| 598 | int r; |
| 599 | |
| 600 | while (n) { |
| 601 | bss = rb_entry(n, struct cfg80211_internal_bss, rbn); |
| 602 | r = cmp_hidden_bss(&res->pub, &bss->pub); |
| 603 | |
| 604 | if (r == 0) |
| 605 | return bss; |
| 606 | else if (r < 0) |
| 607 | n = n->rb_left; |
| 608 | else |
| 609 | n = n->rb_right; |
| 610 | } |
| 611 | |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | static void |
| 616 | copy_hidden_ies(struct cfg80211_internal_bss *res, |
| 617 | struct cfg80211_internal_bss *hidden) |
| 618 | { |
| 619 | if (unlikely(res->pub.beacon_ies)) |
| 620 | return; |
| 621 | if (WARN_ON(!hidden->pub.beacon_ies)) |
| 622 | return; |
| 623 | |
| 624 | res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC); |
| 625 | if (unlikely(!res->pub.beacon_ies)) |
| 626 | return; |
| 627 | |
| 628 | res->beacon_ies_allocated = true; |
| 629 | res->pub.len_beacon_ies = hidden->pub.len_beacon_ies; |
| 630 | memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies, |
| 631 | res->pub.len_beacon_ies); |
| 632 | } |
| 633 | |
| 634 | static struct cfg80211_internal_bss * |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 635 | cfg80211_bss_update(struct cfg80211_registered_device *dev, |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 636 | struct cfg80211_internal_bss *res) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 637 | { |
| 638 | struct cfg80211_internal_bss *found = NULL; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * The reference to "res" is donated to this function. |
| 642 | */ |
| 643 | |
| 644 | if (WARN_ON(!res->pub.channel)) { |
| 645 | kref_put(&res->ref, bss_release); |
| 646 | return NULL; |
| 647 | } |
| 648 | |
| 649 | res->ts = jiffies; |
| 650 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 651 | spin_lock_bh(&dev->bss_lock); |
| 652 | |
| 653 | found = rb_find_bss(dev, res); |
| 654 | |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 655 | if (found) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 656 | found->pub.beacon_interval = res->pub.beacon_interval; |
| 657 | found->pub.tsf = res->pub.tsf; |
| 658 | found->pub.signal = res->pub.signal; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 659 | found->pub.capability = res->pub.capability; |
| 660 | found->ts = res->ts; |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 661 | |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 662 | /* Update IEs */ |
| 663 | if (res->pub.proberesp_ies) { |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 664 | size_t used = dev->wiphy.bss_priv_size + sizeof(*res); |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 665 | size_t ielen = res->pub.len_proberesp_ies; |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 666 | |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 667 | if (found->pub.proberesp_ies && |
| 668 | !found->proberesp_ies_allocated && |
| 669 | ksize(found) >= used + ielen) { |
| 670 | memcpy(found->pub.proberesp_ies, |
| 671 | res->pub.proberesp_ies, ielen); |
| 672 | found->pub.len_proberesp_ies = ielen; |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 673 | } else { |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 674 | u8 *ies = found->pub.proberesp_ies; |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 675 | |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 676 | if (found->proberesp_ies_allocated) |
Michael Buesch | 273de92 | 2009-04-25 22:28:55 +0200 | [diff] [blame] | 677 | ies = krealloc(ies, ielen, GFP_ATOMIC); |
| 678 | else |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 679 | ies = kmalloc(ielen, GFP_ATOMIC); |
| 680 | |
| 681 | if (ies) { |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 682 | memcpy(ies, res->pub.proberesp_ies, |
| 683 | ielen); |
| 684 | found->proberesp_ies_allocated = true; |
| 685 | found->pub.proberesp_ies = ies; |
| 686 | found->pub.len_proberesp_ies = ielen; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /* Override possible earlier Beacon frame IEs */ |
| 691 | found->pub.information_elements = |
| 692 | found->pub.proberesp_ies; |
| 693 | found->pub.len_information_elements = |
| 694 | found->pub.len_proberesp_ies; |
| 695 | } |
| 696 | if (res->pub.beacon_ies) { |
| 697 | size_t used = dev->wiphy.bss_priv_size + sizeof(*res); |
| 698 | size_t ielen = res->pub.len_beacon_ies; |
Sven Neumann | 01123e2 | 2010-12-09 15:05:24 +0100 | [diff] [blame] | 699 | bool information_elements_is_beacon_ies = |
| 700 | (found->pub.information_elements == |
| 701 | found->pub.beacon_ies); |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 702 | |
| 703 | if (found->pub.beacon_ies && |
| 704 | !found->beacon_ies_allocated && |
| 705 | ksize(found) >= used + ielen) { |
| 706 | memcpy(found->pub.beacon_ies, |
| 707 | res->pub.beacon_ies, ielen); |
| 708 | found->pub.len_beacon_ies = ielen; |
| 709 | } else { |
| 710 | u8 *ies = found->pub.beacon_ies; |
| 711 | |
| 712 | if (found->beacon_ies_allocated) |
| 713 | ies = krealloc(ies, ielen, GFP_ATOMIC); |
| 714 | else |
| 715 | ies = kmalloc(ielen, GFP_ATOMIC); |
| 716 | |
| 717 | if (ies) { |
| 718 | memcpy(ies, res->pub.beacon_ies, |
| 719 | ielen); |
| 720 | found->beacon_ies_allocated = true; |
| 721 | found->pub.beacon_ies = ies; |
| 722 | found->pub.len_beacon_ies = ielen; |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 723 | } |
| 724 | } |
Sven Neumann | 01123e2 | 2010-12-09 15:05:24 +0100 | [diff] [blame] | 725 | |
| 726 | /* Override IEs if they were from a beacon before */ |
| 727 | if (information_elements_is_beacon_ies) { |
| 728 | found->pub.information_elements = |
| 729 | found->pub.beacon_ies; |
| 730 | found->pub.len_information_elements = |
| 731 | found->pub.len_beacon_ies; |
| 732 | } |
Johannes Berg | cd1658f | 2009-04-16 15:00:58 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 735 | kref_put(&res->ref, bss_release); |
| 736 | } else { |
Dmitry Tarnyagin | dd9dfb9 | 2011-11-04 17:12:07 +0100 | [diff] [blame] | 737 | struct cfg80211_internal_bss *hidden; |
| 738 | |
| 739 | /* First check if the beacon is a probe response from |
| 740 | * a hidden bss. If so, copy beacon ies (with nullified |
| 741 | * ssid) into the probe response bss entry (with real ssid). |
| 742 | * It is required basically for PSM implementation |
| 743 | * (probe responses do not contain tim ie) */ |
| 744 | |
| 745 | /* TODO: The code is not trying to update existing probe |
| 746 | * response bss entries when beacon ies are |
| 747 | * getting changed. */ |
| 748 | hidden = rb_find_hidden_bss(dev, res); |
| 749 | if (hidden) |
| 750 | copy_hidden_ies(res, hidden); |
| 751 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 752 | /* this "consumes" the reference */ |
| 753 | list_add_tail(&res->list, &dev->bss_list); |
| 754 | rb_insert_bss(dev, res); |
| 755 | found = res; |
| 756 | } |
| 757 | |
| 758 | dev->bss_generation++; |
| 759 | spin_unlock_bh(&dev->bss_lock); |
| 760 | |
| 761 | kref_get(&found->ref); |
| 762 | return found; |
| 763 | } |
| 764 | |
Jussi Kivilinna | 06aa7af | 2009-03-26 23:40:09 +0200 | [diff] [blame] | 765 | struct cfg80211_bss* |
| 766 | cfg80211_inform_bss(struct wiphy *wiphy, |
| 767 | struct ieee80211_channel *channel, |
Johannes Berg | 7b8bcff | 2012-03-13 13:57:04 +0100 | [diff] [blame] | 768 | const u8 *bssid, u64 tsf, u16 capability, |
| 769 | u16 beacon_interval, const u8 *ie, size_t ielen, |
Jussi Kivilinna | 06aa7af | 2009-03-26 23:40:09 +0200 | [diff] [blame] | 770 | s32 signal, gfp_t gfp) |
| 771 | { |
| 772 | struct cfg80211_internal_bss *res; |
| 773 | size_t privsz; |
| 774 | |
| 775 | if (WARN_ON(!wiphy)) |
| 776 | return NULL; |
| 777 | |
| 778 | privsz = wiphy->bss_priv_size; |
| 779 | |
Sujith | 22fe88d | 2010-05-13 10:34:08 +0530 | [diff] [blame] | 780 | if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC && |
Jussi Kivilinna | 06aa7af | 2009-03-26 23:40:09 +0200 | [diff] [blame] | 781 | (signal < 0 || signal > 100))) |
| 782 | return NULL; |
| 783 | |
| 784 | res = kzalloc(sizeof(*res) + privsz + ielen, gfp); |
| 785 | if (!res) |
| 786 | return NULL; |
| 787 | |
| 788 | memcpy(res->pub.bssid, bssid, ETH_ALEN); |
| 789 | res->pub.channel = channel; |
| 790 | res->pub.signal = signal; |
Johannes Berg | 7b8bcff | 2012-03-13 13:57:04 +0100 | [diff] [blame] | 791 | res->pub.tsf = tsf; |
Jussi Kivilinna | 06aa7af | 2009-03-26 23:40:09 +0200 | [diff] [blame] | 792 | res->pub.beacon_interval = beacon_interval; |
| 793 | res->pub.capability = capability; |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 794 | /* |
| 795 | * Since we do not know here whether the IEs are from a Beacon or Probe |
| 796 | * Response frame, we need to pick one of the options and only use it |
| 797 | * with the driver that does not provide the full Beacon/Probe Response |
| 798 | * frame. Use Beacon frame pointer to avoid indicating that this should |
| 799 | * override the information_elements pointer should we have received an |
| 800 | * earlier indication of Probe Response data. |
| 801 | * |
| 802 | * The initial buffer for the IEs is allocated with the BSS entry and |
| 803 | * is located after the private area. |
| 804 | */ |
| 805 | res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz; |
| 806 | memcpy(res->pub.beacon_ies, ie, ielen); |
| 807 | res->pub.len_beacon_ies = ielen; |
| 808 | res->pub.information_elements = res->pub.beacon_ies; |
| 809 | res->pub.len_information_elements = res->pub.len_beacon_ies; |
Jussi Kivilinna | 06aa7af | 2009-03-26 23:40:09 +0200 | [diff] [blame] | 810 | |
| 811 | kref_init(&res->ref); |
| 812 | |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 813 | res = cfg80211_bss_update(wiphy_to_dev(wiphy), res); |
Jussi Kivilinna | 06aa7af | 2009-03-26 23:40:09 +0200 | [diff] [blame] | 814 | if (!res) |
| 815 | return NULL; |
| 816 | |
| 817 | if (res->pub.capability & WLAN_CAPABILITY_ESS) |
| 818 | regulatory_hint_found_beacon(wiphy, channel, gfp); |
| 819 | |
| 820 | /* cfg80211_bss_update gives us a referenced result */ |
| 821 | return &res->pub; |
| 822 | } |
| 823 | EXPORT_SYMBOL(cfg80211_inform_bss); |
| 824 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 825 | struct cfg80211_bss * |
| 826 | cfg80211_inform_bss_frame(struct wiphy *wiphy, |
| 827 | struct ieee80211_channel *channel, |
| 828 | struct ieee80211_mgmt *mgmt, size_t len, |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 829 | s32 signal, gfp_t gfp) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 830 | { |
| 831 | struct cfg80211_internal_bss *res; |
| 832 | size_t ielen = len - offsetof(struct ieee80211_mgmt, |
| 833 | u.probe_resp.variable); |
Mariusz Kozlowski | bef9bac | 2011-03-26 19:26:55 +0100 | [diff] [blame] | 834 | size_t privsz; |
| 835 | |
| 836 | if (WARN_ON(!mgmt)) |
| 837 | return NULL; |
| 838 | |
| 839 | if (WARN_ON(!wiphy)) |
| 840 | return NULL; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 841 | |
Sujith | 22fe88d | 2010-05-13 10:34:08 +0530 | [diff] [blame] | 842 | if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC && |
Hila Gonen | 768be59 | 2012-08-26 11:00:28 +0300 | [diff] [blame] | 843 | (signal < 0 || signal > 100))) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 844 | return NULL; |
| 845 | |
Mariusz Kozlowski | bef9bac | 2011-03-26 19:26:55 +0100 | [diff] [blame] | 846 | if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable))) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 847 | return NULL; |
| 848 | |
Mariusz Kozlowski | bef9bac | 2011-03-26 19:26:55 +0100 | [diff] [blame] | 849 | privsz = wiphy->bss_priv_size; |
| 850 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 851 | res = kzalloc(sizeof(*res) + privsz + ielen, gfp); |
| 852 | if (!res) |
| 853 | return NULL; |
| 854 | |
| 855 | memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN); |
| 856 | res->pub.channel = channel; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 857 | res->pub.signal = signal; |
| 858 | res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); |
| 859 | res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int); |
| 860 | res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info); |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 861 | /* |
| 862 | * The initial buffer for the IEs is allocated with the BSS entry and |
| 863 | * is located after the private area. |
| 864 | */ |
| 865 | if (ieee80211_is_probe_resp(mgmt->frame_control)) { |
| 866 | res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz; |
| 867 | memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable, |
| 868 | ielen); |
| 869 | res->pub.len_proberesp_ies = ielen; |
| 870 | res->pub.information_elements = res->pub.proberesp_ies; |
| 871 | res->pub.len_information_elements = res->pub.len_proberesp_ies; |
| 872 | } else { |
| 873 | res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz; |
| 874 | memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen); |
| 875 | res->pub.len_beacon_ies = ielen; |
| 876 | res->pub.information_elements = res->pub.beacon_ies; |
| 877 | res->pub.len_information_elements = res->pub.len_beacon_ies; |
| 878 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 879 | |
| 880 | kref_init(&res->ref); |
| 881 | |
Jouni Malinen | 34a6edd | 2010-01-06 16:19:24 +0200 | [diff] [blame] | 882 | res = cfg80211_bss_update(wiphy_to_dev(wiphy), res); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 883 | if (!res) |
| 884 | return NULL; |
| 885 | |
Luis R. Rodriguez | e38f8a7 | 2009-02-21 00:20:39 -0500 | [diff] [blame] | 886 | if (res->pub.capability & WLAN_CAPABILITY_ESS) |
| 887 | regulatory_hint_found_beacon(wiphy, channel, gfp); |
| 888 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 889 | /* cfg80211_bss_update gives us a referenced result */ |
| 890 | return &res->pub; |
| 891 | } |
| 892 | EXPORT_SYMBOL(cfg80211_inform_bss_frame); |
| 893 | |
Johannes Berg | 4c0c0b7 | 2012-01-20 13:55:26 +0100 | [diff] [blame] | 894 | void cfg80211_ref_bss(struct cfg80211_bss *pub) |
| 895 | { |
| 896 | struct cfg80211_internal_bss *bss; |
| 897 | |
| 898 | if (!pub) |
| 899 | return; |
| 900 | |
| 901 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 902 | kref_get(&bss->ref); |
| 903 | } |
| 904 | EXPORT_SYMBOL(cfg80211_ref_bss); |
| 905 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 906 | void cfg80211_put_bss(struct cfg80211_bss *pub) |
| 907 | { |
| 908 | struct cfg80211_internal_bss *bss; |
| 909 | |
| 910 | if (!pub) |
| 911 | return; |
| 912 | |
| 913 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 914 | kref_put(&bss->ref, bss_release); |
| 915 | } |
| 916 | EXPORT_SYMBOL(cfg80211_put_bss); |
| 917 | |
Johannes Berg | d491af1 | 2009-02-10 21:25:58 +0100 | [diff] [blame] | 918 | void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) |
| 919 | { |
| 920 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 921 | struct cfg80211_internal_bss *bss; |
| 922 | |
| 923 | if (WARN_ON(!pub)) |
| 924 | return; |
| 925 | |
| 926 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 927 | |
| 928 | spin_lock_bh(&dev->bss_lock); |
Johannes Berg | 3207390 | 2010-10-06 21:18:04 +0200 | [diff] [blame] | 929 | if (!list_empty(&bss->list)) { |
Juuso Oikarinen | 2b78ac9 | 2011-03-28 14:32:32 +0300 | [diff] [blame] | 930 | __cfg80211_unlink_bss(dev, bss); |
Johannes Berg | 3207390 | 2010-10-06 21:18:04 +0200 | [diff] [blame] | 931 | dev->bss_generation++; |
Johannes Berg | 3207390 | 2010-10-06 21:18:04 +0200 | [diff] [blame] | 932 | } |
Johannes Berg | d491af1 | 2009-02-10 21:25:58 +0100 | [diff] [blame] | 933 | spin_unlock_bh(&dev->bss_lock); |
Johannes Berg | d491af1 | 2009-02-10 21:25:58 +0100 | [diff] [blame] | 934 | } |
| 935 | EXPORT_SYMBOL(cfg80211_unlink_bss); |
| 936 | |
Johannes Berg | 3d23e34 | 2009-09-29 23:27:28 +0200 | [diff] [blame] | 937 | #ifdef CONFIG_CFG80211_WEXT |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 938 | int cfg80211_wext_siwscan(struct net_device *dev, |
| 939 | struct iw_request_info *info, |
| 940 | union iwreq_data *wrqu, char *extra) |
| 941 | { |
| 942 | struct cfg80211_registered_device *rdev; |
| 943 | struct wiphy *wiphy; |
| 944 | struct iw_scan_req *wreq = NULL; |
Johannes Berg | 65486c8 | 2009-12-23 15:33:35 +0100 | [diff] [blame] | 945 | struct cfg80211_scan_request *creq = NULL; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 946 | int i, err, n_channels = 0; |
| 947 | enum ieee80211_band band; |
| 948 | |
| 949 | if (!netif_running(dev)) |
| 950 | return -ENETDOWN; |
| 951 | |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 952 | if (wrqu->data.length == sizeof(struct iw_scan_req)) |
| 953 | wreq = (struct iw_scan_req *)extra; |
| 954 | |
Johannes Berg | 463d018 | 2009-07-14 00:33:35 +0200 | [diff] [blame] | 955 | rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 956 | |
| 957 | if (IS_ERR(rdev)) |
| 958 | return PTR_ERR(rdev); |
| 959 | |
| 960 | if (rdev->scan_req) { |
| 961 | err = -EBUSY; |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | wiphy = &rdev->wiphy; |
| 966 | |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 967 | /* Determine number of channels, needed to allocate creq */ |
| 968 | if (wreq && wreq->num_channels) |
| 969 | n_channels = wreq->num_channels; |
| 970 | else { |
| 971 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
| 972 | if (wiphy->bands[band]) |
| 973 | n_channels += wiphy->bands[band]->n_channels; |
| 974 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 975 | |
| 976 | creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) + |
| 977 | n_channels * sizeof(void *), |
| 978 | GFP_ATOMIC); |
| 979 | if (!creq) { |
| 980 | err = -ENOMEM; |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | creq->wiphy = wiphy; |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 985 | creq->wdev = dev->ieee80211_ptr; |
Johannes Berg | 5ba6353 | 2009-08-07 17:54:07 +0200 | [diff] [blame] | 986 | /* SSIDs come after channels */ |
| 987 | creq->ssids = (void *)&creq->channels[n_channels]; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 988 | creq->n_channels = n_channels; |
| 989 | creq->n_ssids = 1; |
Sam Leffler | 15d6030 | 2012-10-11 21:03:34 -0700 | [diff] [blame] | 990 | creq->scan_start = jiffies; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 991 | |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 992 | /* translate "Scan on frequencies" request */ |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 993 | i = 0; |
| 994 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 995 | int j; |
Johannes Berg | 584991d | 2009-11-02 13:32:03 +0100 | [diff] [blame] | 996 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 997 | if (!wiphy->bands[band]) |
| 998 | continue; |
Johannes Berg | 584991d | 2009-11-02 13:32:03 +0100 | [diff] [blame] | 999 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1000 | for (j = 0; j < wiphy->bands[band]->n_channels; j++) { |
Johannes Berg | 584991d | 2009-11-02 13:32:03 +0100 | [diff] [blame] | 1001 | /* ignore disabled channels */ |
| 1002 | if (wiphy->bands[band]->channels[j].flags & |
| 1003 | IEEE80211_CHAN_DISABLED) |
| 1004 | continue; |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 1005 | |
| 1006 | /* If we have a wireless request structure and the |
| 1007 | * wireless request specifies frequencies, then search |
| 1008 | * for the matching hardware channel. |
| 1009 | */ |
| 1010 | if (wreq && wreq->num_channels) { |
| 1011 | int k; |
| 1012 | int wiphy_freq = wiphy->bands[band]->channels[j].center_freq; |
| 1013 | for (k = 0; k < wreq->num_channels; k++) { |
Holger Schurig | a4e7b73 | 2009-09-11 10:13:53 +0200 | [diff] [blame] | 1014 | int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]); |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 1015 | if (wext_freq == wiphy_freq) |
| 1016 | goto wext_freq_found; |
| 1017 | } |
| 1018 | goto wext_freq_not_found; |
| 1019 | } |
| 1020 | |
| 1021 | wext_freq_found: |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1022 | creq->channels[i] = &wiphy->bands[band]->channels[j]; |
| 1023 | i++; |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 1024 | wext_freq_not_found: ; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1025 | } |
| 1026 | } |
Holger Schurig | 8862dc5 | 2009-09-11 10:13:55 +0200 | [diff] [blame] | 1027 | /* No channels found? */ |
| 1028 | if (!i) { |
| 1029 | err = -EINVAL; |
| 1030 | goto out; |
| 1031 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1032 | |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 1033 | /* Set real number of channels specified in creq->channels[] */ |
| 1034 | creq->n_channels = i; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1035 | |
Holger Schurig | b2e3abd | 2009-09-09 13:09:54 +0200 | [diff] [blame] | 1036 | /* translate "Scan for SSID" request */ |
| 1037 | if (wreq) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1038 | if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { |
Johannes Berg | 65486c8 | 2009-12-23 15:33:35 +0100 | [diff] [blame] | 1039 | if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) { |
| 1040 | err = -EINVAL; |
| 1041 | goto out; |
| 1042 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1043 | memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len); |
| 1044 | creq->ssids[0].ssid_len = wreq->essid_len; |
| 1045 | } |
| 1046 | if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) |
| 1047 | creq->n_ssids = 0; |
| 1048 | } |
| 1049 | |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1050 | for (i = 0; i < IEEE80211_NUM_BANDS; i++) |
Johannes Berg | a401d2b | 2011-07-20 00:52:16 +0200 | [diff] [blame] | 1051 | if (wiphy->bands[i]) |
| 1052 | creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1; |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1053 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1054 | rdev->scan_req = creq; |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 1055 | err = rdev->ops->scan(wiphy, creq); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1056 | if (err) { |
| 1057 | rdev->scan_req = NULL; |
Johannes Berg | 65486c8 | 2009-12-23 15:33:35 +0100 | [diff] [blame] | 1058 | /* creq will be freed below */ |
Johannes Berg | 463d018 | 2009-07-14 00:33:35 +0200 | [diff] [blame] | 1059 | } else { |
Johannes Berg | fd01428 | 2012-06-18 19:17:03 +0200 | [diff] [blame] | 1060 | nl80211_send_scan_start(rdev, dev->ieee80211_ptr); |
Johannes Berg | 65486c8 | 2009-12-23 15:33:35 +0100 | [diff] [blame] | 1061 | /* creq now owned by driver */ |
| 1062 | creq = NULL; |
Johannes Berg | 463d018 | 2009-07-14 00:33:35 +0200 | [diff] [blame] | 1063 | dev_hold(dev); |
| 1064 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1065 | out: |
Johannes Berg | 65486c8 | 2009-12-23 15:33:35 +0100 | [diff] [blame] | 1066 | kfree(creq); |
Johannes Berg | 4d0c8ae | 2009-07-07 03:56:09 +0200 | [diff] [blame] | 1067 | cfg80211_unlock_rdev(rdev); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1068 | return err; |
| 1069 | } |
Johannes Berg | ba44cb7 | 2009-04-20 18:49:39 +0200 | [diff] [blame] | 1070 | EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1071 | |
| 1072 | static void ieee80211_scan_add_ies(struct iw_request_info *info, |
| 1073 | struct cfg80211_bss *bss, |
| 1074 | char **current_ev, char *end_buf) |
| 1075 | { |
| 1076 | u8 *pos, *end, *next; |
| 1077 | struct iw_event iwe; |
| 1078 | |
| 1079 | if (!bss->information_elements || |
| 1080 | !bss->len_information_elements) |
| 1081 | return; |
| 1082 | |
| 1083 | /* |
| 1084 | * If needed, fragment the IEs buffer (at IE boundaries) into short |
| 1085 | * enough fragments to fit into IW_GENERIC_IE_MAX octet messages. |
| 1086 | */ |
| 1087 | pos = bss->information_elements; |
| 1088 | end = pos + bss->len_information_elements; |
| 1089 | |
| 1090 | while (end - pos > IW_GENERIC_IE_MAX) { |
| 1091 | next = pos + 2 + pos[1]; |
| 1092 | while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX) |
| 1093 | next = next + 2 + next[1]; |
| 1094 | |
| 1095 | memset(&iwe, 0, sizeof(iwe)); |
| 1096 | iwe.cmd = IWEVGENIE; |
| 1097 | iwe.u.data.length = next - pos; |
| 1098 | *current_ev = iwe_stream_add_point(info, *current_ev, |
| 1099 | end_buf, &iwe, pos); |
| 1100 | |
| 1101 | pos = next; |
| 1102 | } |
| 1103 | |
| 1104 | if (end > pos) { |
| 1105 | memset(&iwe, 0, sizeof(iwe)); |
| 1106 | iwe.cmd = IWEVGENIE; |
| 1107 | iwe.u.data.length = end - pos; |
| 1108 | *current_ev = iwe_stream_add_point(info, *current_ev, |
| 1109 | end_buf, &iwe, pos); |
| 1110 | } |
| 1111 | } |
| 1112 | |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame] | 1113 | static inline unsigned int elapsed_jiffies_msecs(unsigned long start) |
| 1114 | { |
| 1115 | unsigned long end = jiffies; |
| 1116 | |
| 1117 | if (end >= start) |
| 1118 | return jiffies_to_msecs(end - start); |
| 1119 | |
| 1120 | return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1); |
| 1121 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1122 | |
| 1123 | static char * |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 1124 | ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info, |
| 1125 | struct cfg80211_internal_bss *bss, char *current_ev, |
| 1126 | char *end_buf) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1127 | { |
| 1128 | struct iw_event iwe; |
| 1129 | u8 *buf, *cfg, *p; |
| 1130 | u8 *ie = bss->pub.information_elements; |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 1131 | int rem = bss->pub.len_information_elements, i, sig; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1132 | bool ismesh = false; |
| 1133 | |
| 1134 | memset(&iwe, 0, sizeof(iwe)); |
| 1135 | iwe.cmd = SIOCGIWAP; |
| 1136 | iwe.u.ap_addr.sa_family = ARPHRD_ETHER; |
| 1137 | memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); |
| 1138 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 1139 | IW_EV_ADDR_LEN); |
| 1140 | |
| 1141 | memset(&iwe, 0, sizeof(iwe)); |
| 1142 | iwe.cmd = SIOCGIWFREQ; |
| 1143 | iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); |
| 1144 | iwe.u.freq.e = 0; |
| 1145 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 1146 | IW_EV_FREQ_LEN); |
| 1147 | |
| 1148 | memset(&iwe, 0, sizeof(iwe)); |
| 1149 | iwe.cmd = SIOCGIWFREQ; |
| 1150 | iwe.u.freq.m = bss->pub.channel->center_freq; |
| 1151 | iwe.u.freq.e = 6; |
| 1152 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 1153 | IW_EV_FREQ_LEN); |
| 1154 | |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 1155 | if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1156 | memset(&iwe, 0, sizeof(iwe)); |
| 1157 | iwe.cmd = IWEVQUAL; |
| 1158 | iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED | |
| 1159 | IW_QUAL_NOISE_INVALID | |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 1160 | IW_QUAL_QUAL_UPDATED; |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 1161 | switch (wiphy->signal_type) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1162 | case CFG80211_SIGNAL_TYPE_MBM: |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 1163 | sig = bss->pub.signal / 100; |
| 1164 | iwe.u.qual.level = sig; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1165 | iwe.u.qual.updated |= IW_QUAL_DBM; |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 1166 | if (sig < -110) /* rather bad */ |
| 1167 | sig = -110; |
| 1168 | else if (sig > -40) /* perfect */ |
| 1169 | sig = -40; |
| 1170 | /* will give a range of 0 .. 70 */ |
| 1171 | iwe.u.qual.qual = sig + 110; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1172 | break; |
| 1173 | case CFG80211_SIGNAL_TYPE_UNSPEC: |
| 1174 | iwe.u.qual.level = bss->pub.signal; |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 1175 | /* will give range 0 .. 100 */ |
| 1176 | iwe.u.qual.qual = bss->pub.signal; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1177 | break; |
| 1178 | default: |
| 1179 | /* not reached */ |
| 1180 | break; |
| 1181 | } |
| 1182 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, |
| 1183 | &iwe, IW_EV_QUAL_LEN); |
| 1184 | } |
| 1185 | |
| 1186 | memset(&iwe, 0, sizeof(iwe)); |
| 1187 | iwe.cmd = SIOCGIWENCODE; |
| 1188 | if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY) |
| 1189 | iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; |
| 1190 | else |
| 1191 | iwe.u.data.flags = IW_ENCODE_DISABLED; |
| 1192 | iwe.u.data.length = 0; |
| 1193 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 1194 | &iwe, ""); |
| 1195 | |
| 1196 | while (rem >= 2) { |
| 1197 | /* invalid data */ |
| 1198 | if (ie[1] > rem - 2) |
| 1199 | break; |
| 1200 | |
| 1201 | switch (ie[0]) { |
| 1202 | case WLAN_EID_SSID: |
| 1203 | memset(&iwe, 0, sizeof(iwe)); |
| 1204 | iwe.cmd = SIOCGIWESSID; |
| 1205 | iwe.u.data.length = ie[1]; |
| 1206 | iwe.u.data.flags = 1; |
| 1207 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 1208 | &iwe, ie + 2); |
| 1209 | break; |
| 1210 | case WLAN_EID_MESH_ID: |
| 1211 | memset(&iwe, 0, sizeof(iwe)); |
| 1212 | iwe.cmd = SIOCGIWESSID; |
| 1213 | iwe.u.data.length = ie[1]; |
| 1214 | iwe.u.data.flags = 1; |
| 1215 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 1216 | &iwe, ie + 2); |
| 1217 | break; |
| 1218 | case WLAN_EID_MESH_CONFIG: |
| 1219 | ismesh = true; |
Rui Paulo | 136cfa2 | 2009-11-18 18:40:00 +0000 | [diff] [blame] | 1220 | if (ie[1] != sizeof(struct ieee80211_meshconf_ie)) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1221 | break; |
| 1222 | buf = kmalloc(50, GFP_ATOMIC); |
| 1223 | if (!buf) |
| 1224 | break; |
| 1225 | cfg = ie + 2; |
| 1226 | memset(&iwe, 0, sizeof(iwe)); |
| 1227 | iwe.cmd = IWEVCUSTOM; |
Rui Paulo | 76aa5e7 | 2009-11-18 18:22:59 +0000 | [diff] [blame] | 1228 | sprintf(buf, "Mesh Network Path Selection Protocol ID: " |
| 1229 | "0x%02X", cfg[0]); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1230 | iwe.u.data.length = strlen(buf); |
| 1231 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1232 | end_buf, |
| 1233 | &iwe, buf); |
Rui Paulo | 76aa5e7 | 2009-11-18 18:22:59 +0000 | [diff] [blame] | 1234 | sprintf(buf, "Path Selection Metric ID: 0x%02X", |
| 1235 | cfg[1]); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1236 | iwe.u.data.length = strlen(buf); |
| 1237 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1238 | end_buf, |
| 1239 | &iwe, buf); |
Rui Paulo | 76aa5e7 | 2009-11-18 18:22:59 +0000 | [diff] [blame] | 1240 | sprintf(buf, "Congestion Control Mode ID: 0x%02X", |
| 1241 | cfg[2]); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1242 | iwe.u.data.length = strlen(buf); |
| 1243 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1244 | end_buf, |
| 1245 | &iwe, buf); |
Rui Paulo | 76aa5e7 | 2009-11-18 18:22:59 +0000 | [diff] [blame] | 1246 | sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1247 | iwe.u.data.length = strlen(buf); |
| 1248 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1249 | end_buf, |
| 1250 | &iwe, buf); |
Rui Paulo | 76aa5e7 | 2009-11-18 18:22:59 +0000 | [diff] [blame] | 1251 | sprintf(buf, "Authentication ID: 0x%02X", cfg[4]); |
| 1252 | iwe.u.data.length = strlen(buf); |
| 1253 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1254 | end_buf, |
| 1255 | &iwe, buf); |
| 1256 | sprintf(buf, "Formation Info: 0x%02X", cfg[5]); |
| 1257 | iwe.u.data.length = strlen(buf); |
| 1258 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1259 | end_buf, |
| 1260 | &iwe, buf); |
| 1261 | sprintf(buf, "Capabilities: 0x%02X", cfg[6]); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1262 | iwe.u.data.length = strlen(buf); |
| 1263 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1264 | end_buf, |
| 1265 | &iwe, buf); |
| 1266 | kfree(buf); |
| 1267 | break; |
| 1268 | case WLAN_EID_SUPP_RATES: |
| 1269 | case WLAN_EID_EXT_SUPP_RATES: |
| 1270 | /* display all supported rates in readable format */ |
| 1271 | p = current_ev + iwe_stream_lcp_len(info); |
| 1272 | |
| 1273 | memset(&iwe, 0, sizeof(iwe)); |
| 1274 | iwe.cmd = SIOCGIWRATE; |
| 1275 | /* Those two flags are ignored... */ |
| 1276 | iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; |
| 1277 | |
| 1278 | for (i = 0; i < ie[1]; i++) { |
| 1279 | iwe.u.bitrate.value = |
| 1280 | ((ie[i + 2] & 0x7f) * 500000); |
| 1281 | p = iwe_stream_add_value(info, current_ev, p, |
| 1282 | end_buf, &iwe, IW_EV_PARAM_LEN); |
| 1283 | } |
| 1284 | current_ev = p; |
| 1285 | break; |
| 1286 | } |
| 1287 | rem -= ie[1] + 2; |
| 1288 | ie += ie[1] + 2; |
| 1289 | } |
| 1290 | |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1291 | if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) || |
| 1292 | ismesh) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1293 | memset(&iwe, 0, sizeof(iwe)); |
| 1294 | iwe.cmd = SIOCGIWMODE; |
| 1295 | if (ismesh) |
| 1296 | iwe.u.mode = IW_MODE_MESH; |
| 1297 | else if (bss->pub.capability & WLAN_CAPABILITY_ESS) |
| 1298 | iwe.u.mode = IW_MODE_MASTER; |
| 1299 | else |
| 1300 | iwe.u.mode = IW_MODE_ADHOC; |
| 1301 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, |
| 1302 | &iwe, IW_EV_UINT_LEN); |
| 1303 | } |
| 1304 | |
| 1305 | buf = kmalloc(30, GFP_ATOMIC); |
| 1306 | if (buf) { |
| 1307 | memset(&iwe, 0, sizeof(iwe)); |
| 1308 | iwe.cmd = IWEVCUSTOM; |
| 1309 | sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf)); |
| 1310 | iwe.u.data.length = strlen(buf); |
| 1311 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 1312 | &iwe, buf); |
| 1313 | memset(&iwe, 0, sizeof(iwe)); |
| 1314 | iwe.cmd = IWEVCUSTOM; |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame] | 1315 | sprintf(buf, " Last beacon: %ums ago", |
| 1316 | elapsed_jiffies_msecs(bss->ts)); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1317 | iwe.u.data.length = strlen(buf); |
| 1318 | current_ev = iwe_stream_add_point(info, current_ev, |
| 1319 | end_buf, &iwe, buf); |
| 1320 | kfree(buf); |
| 1321 | } |
| 1322 | |
| 1323 | ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf); |
| 1324 | |
| 1325 | return current_ev; |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | static int ieee80211_scan_results(struct cfg80211_registered_device *dev, |
| 1330 | struct iw_request_info *info, |
| 1331 | char *buf, size_t len) |
| 1332 | { |
| 1333 | char *current_ev = buf; |
| 1334 | char *end_buf = buf + len; |
| 1335 | struct cfg80211_internal_bss *bss; |
| 1336 | |
| 1337 | spin_lock_bh(&dev->bss_lock); |
| 1338 | cfg80211_bss_expire(dev); |
| 1339 | |
| 1340 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 1341 | if (buf + len - current_ev <= IW_EV_ADDR_LEN) { |
| 1342 | spin_unlock_bh(&dev->bss_lock); |
| 1343 | return -E2BIG; |
| 1344 | } |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame] | 1345 | current_ev = ieee80211_bss(&dev->wiphy, info, bss, |
| 1346 | current_ev, end_buf); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1347 | } |
| 1348 | spin_unlock_bh(&dev->bss_lock); |
| 1349 | return current_ev - buf; |
| 1350 | } |
| 1351 | |
| 1352 | |
| 1353 | int cfg80211_wext_giwscan(struct net_device *dev, |
| 1354 | struct iw_request_info *info, |
| 1355 | struct iw_point *data, char *extra) |
| 1356 | { |
| 1357 | struct cfg80211_registered_device *rdev; |
| 1358 | int res; |
| 1359 | |
| 1360 | if (!netif_running(dev)) |
| 1361 | return -ENETDOWN; |
| 1362 | |
Johannes Berg | 463d018 | 2009-07-14 00:33:35 +0200 | [diff] [blame] | 1363 | rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1364 | |
| 1365 | if (IS_ERR(rdev)) |
| 1366 | return PTR_ERR(rdev); |
| 1367 | |
| 1368 | if (rdev->scan_req) { |
| 1369 | res = -EAGAIN; |
| 1370 | goto out; |
| 1371 | } |
| 1372 | |
| 1373 | res = ieee80211_scan_results(rdev, info, extra, data->length); |
| 1374 | data->length = 0; |
| 1375 | if (res >= 0) { |
| 1376 | data->length = res; |
| 1377 | res = 0; |
| 1378 | } |
| 1379 | |
| 1380 | out: |
Johannes Berg | 4d0c8ae | 2009-07-07 03:56:09 +0200 | [diff] [blame] | 1381 | cfg80211_unlock_rdev(rdev); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1382 | return res; |
| 1383 | } |
Johannes Berg | ba44cb7 | 2009-04-20 18:49:39 +0200 | [diff] [blame] | 1384 | EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 1385 | #endif |