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> |
| 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> |
| 12 | #include <net/arp.h> |
| 13 | #include <net/cfg80211.h> |
| 14 | #include <net/iw_handler.h> |
| 15 | #include "core.h" |
| 16 | #include "nl80211.h" |
| 17 | |
| 18 | #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ) |
| 19 | |
| 20 | void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted) |
| 21 | { |
| 22 | struct net_device *dev; |
| 23 | #ifdef CONFIG_WIRELESS_EXT |
| 24 | union iwreq_data wrqu; |
| 25 | #endif |
| 26 | |
| 27 | dev = dev_get_by_index(&init_net, request->ifidx); |
| 28 | if (!dev) |
| 29 | goto out; |
| 30 | |
| 31 | WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); |
| 32 | wiphy_to_dev(request->wiphy)->scan_req = NULL; |
| 33 | |
| 34 | if (aborted) |
| 35 | nl80211_send_scan_aborted(wiphy_to_dev(request->wiphy), dev); |
| 36 | else |
| 37 | nl80211_send_scan_done(wiphy_to_dev(request->wiphy), dev); |
| 38 | |
| 39 | #ifdef CONFIG_WIRELESS_EXT |
| 40 | if (!aborted) { |
| 41 | memset(&wrqu, 0, sizeof(wrqu)); |
| 42 | |
| 43 | wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | dev_put(dev); |
| 48 | |
| 49 | out: |
| 50 | kfree(request); |
| 51 | } |
| 52 | EXPORT_SYMBOL(cfg80211_scan_done); |
| 53 | |
| 54 | static void bss_release(struct kref *ref) |
| 55 | { |
| 56 | struct cfg80211_internal_bss *bss; |
| 57 | |
| 58 | bss = container_of(ref, struct cfg80211_internal_bss, ref); |
Johannes Berg | 78c1c7e | 2009-02-10 21:25:57 +0100 | [diff] [blame] | 59 | if (bss->pub.free_priv) |
| 60 | bss->pub.free_priv(&bss->pub); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 61 | kfree(bss); |
| 62 | } |
| 63 | |
| 64 | /* must hold dev->bss_lock! */ |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame] | 65 | void cfg80211_bss_age(struct cfg80211_registered_device *dev, |
| 66 | unsigned long age_secs) |
| 67 | { |
| 68 | struct cfg80211_internal_bss *bss; |
| 69 | unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC); |
| 70 | |
| 71 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 72 | bss->ts -= age_jiffies; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* must hold dev->bss_lock! */ |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 77 | void cfg80211_bss_expire(struct cfg80211_registered_device *dev) |
| 78 | { |
| 79 | struct cfg80211_internal_bss *bss, *tmp; |
| 80 | bool expired = false; |
| 81 | |
| 82 | list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) { |
| 83 | if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE)) |
| 84 | continue; |
| 85 | list_del(&bss->list); |
| 86 | rb_erase(&bss->rbn, &dev->bss_tree); |
| 87 | kref_put(&bss->ref, bss_release); |
| 88 | expired = true; |
| 89 | } |
| 90 | |
| 91 | if (expired) |
| 92 | dev->bss_generation++; |
| 93 | } |
| 94 | |
| 95 | static u8 *find_ie(u8 num, u8 *ies, size_t len) |
| 96 | { |
| 97 | while (len > 2 && ies[0] != num) { |
| 98 | len -= ies[1] + 2; |
| 99 | ies += ies[1] + 2; |
| 100 | } |
| 101 | if (len < 2) |
| 102 | return NULL; |
| 103 | if (len < 2 + ies[1]) |
| 104 | return NULL; |
| 105 | return ies; |
| 106 | } |
| 107 | |
| 108 | static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2) |
| 109 | { |
| 110 | const u8 *ie1 = find_ie(num, ies1, len1); |
| 111 | const u8 *ie2 = find_ie(num, ies2, len2); |
| 112 | int r; |
| 113 | |
| 114 | if (!ie1 && !ie2) |
| 115 | return 0; |
| 116 | if (!ie1) |
| 117 | return -1; |
| 118 | |
| 119 | r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1])); |
| 120 | if (r == 0 && ie1[1] != ie2[1]) |
| 121 | return ie2[1] - ie1[1]; |
| 122 | return r; |
| 123 | } |
| 124 | |
| 125 | static bool is_bss(struct cfg80211_bss *a, |
| 126 | const u8 *bssid, |
| 127 | const u8 *ssid, size_t ssid_len) |
| 128 | { |
| 129 | const u8 *ssidie; |
| 130 | |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 131 | if (bssid && compare_ether_addr(a->bssid, bssid)) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 132 | return false; |
| 133 | |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 134 | if (!ssid) |
| 135 | return true; |
| 136 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 137 | ssidie = find_ie(WLAN_EID_SSID, |
| 138 | a->information_elements, |
| 139 | a->len_information_elements); |
| 140 | if (!ssidie) |
| 141 | return false; |
| 142 | if (ssidie[1] != ssid_len) |
| 143 | return false; |
| 144 | return memcmp(ssidie + 2, ssid, ssid_len) == 0; |
| 145 | } |
| 146 | |
| 147 | static bool is_mesh(struct cfg80211_bss *a, |
| 148 | const u8 *meshid, size_t meshidlen, |
| 149 | const u8 *meshcfg) |
| 150 | { |
| 151 | const u8 *ie; |
| 152 | |
| 153 | if (!is_zero_ether_addr(a->bssid)) |
| 154 | return false; |
| 155 | |
| 156 | ie = find_ie(WLAN_EID_MESH_ID, |
| 157 | a->information_elements, |
| 158 | a->len_information_elements); |
| 159 | if (!ie) |
| 160 | return false; |
| 161 | if (ie[1] != meshidlen) |
| 162 | return false; |
| 163 | if (memcmp(ie + 2, meshid, meshidlen)) |
| 164 | return false; |
| 165 | |
| 166 | ie = find_ie(WLAN_EID_MESH_CONFIG, |
| 167 | a->information_elements, |
| 168 | a->len_information_elements); |
| 169 | if (ie[1] != IEEE80211_MESH_CONFIG_LEN) |
| 170 | return false; |
| 171 | |
| 172 | /* |
| 173 | * Ignore mesh capability (last two bytes of the IE) when |
| 174 | * comparing since that may differ between stations taking |
| 175 | * part in the same mesh. |
| 176 | */ |
| 177 | return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0; |
| 178 | } |
| 179 | |
| 180 | static int cmp_bss(struct cfg80211_bss *a, |
| 181 | struct cfg80211_bss *b) |
| 182 | { |
| 183 | int r; |
| 184 | |
| 185 | if (a->channel != b->channel) |
| 186 | return b->channel->center_freq - a->channel->center_freq; |
| 187 | |
| 188 | r = memcmp(a->bssid, b->bssid, ETH_ALEN); |
| 189 | if (r) |
| 190 | return r; |
| 191 | |
| 192 | if (is_zero_ether_addr(a->bssid)) { |
| 193 | r = cmp_ies(WLAN_EID_MESH_ID, |
| 194 | a->information_elements, |
| 195 | a->len_information_elements, |
| 196 | b->information_elements, |
| 197 | b->len_information_elements); |
| 198 | if (r) |
| 199 | return r; |
| 200 | return cmp_ies(WLAN_EID_MESH_CONFIG, |
| 201 | a->information_elements, |
| 202 | a->len_information_elements, |
| 203 | b->information_elements, |
| 204 | b->len_information_elements); |
| 205 | } |
| 206 | |
| 207 | return cmp_ies(WLAN_EID_SSID, |
| 208 | a->information_elements, |
| 209 | a->len_information_elements, |
| 210 | b->information_elements, |
| 211 | b->len_information_elements); |
| 212 | } |
| 213 | |
| 214 | struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy, |
| 215 | struct ieee80211_channel *channel, |
| 216 | const u8 *bssid, |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 217 | const u8 *ssid, size_t ssid_len, |
| 218 | u16 capa_mask, u16 capa_val) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 219 | { |
| 220 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 221 | struct cfg80211_internal_bss *bss, *res = NULL; |
| 222 | |
| 223 | spin_lock_bh(&dev->bss_lock); |
| 224 | |
| 225 | list_for_each_entry(bss, &dev->bss_list, list) { |
Johannes Berg | 79420f0 | 2009-02-10 21:25:59 +0100 | [diff] [blame] | 226 | if ((bss->pub.capability & capa_mask) != capa_val) |
| 227 | continue; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 228 | if (channel && bss->pub.channel != channel) |
| 229 | continue; |
| 230 | if (is_bss(&bss->pub, bssid, ssid, ssid_len)) { |
| 231 | res = bss; |
| 232 | kref_get(&res->ref); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | spin_unlock_bh(&dev->bss_lock); |
| 238 | if (!res) |
| 239 | return NULL; |
| 240 | return &res->pub; |
| 241 | } |
| 242 | EXPORT_SYMBOL(cfg80211_get_bss); |
| 243 | |
| 244 | struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy, |
| 245 | struct ieee80211_channel *channel, |
| 246 | const u8 *meshid, size_t meshidlen, |
| 247 | const u8 *meshcfg) |
| 248 | { |
| 249 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 250 | struct cfg80211_internal_bss *bss, *res = NULL; |
| 251 | |
| 252 | spin_lock_bh(&dev->bss_lock); |
| 253 | |
| 254 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 255 | if (channel && bss->pub.channel != channel) |
| 256 | continue; |
| 257 | if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) { |
| 258 | res = bss; |
| 259 | kref_get(&res->ref); |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | spin_unlock_bh(&dev->bss_lock); |
| 265 | if (!res) |
| 266 | return NULL; |
| 267 | return &res->pub; |
| 268 | } |
| 269 | EXPORT_SYMBOL(cfg80211_get_mesh); |
| 270 | |
| 271 | |
| 272 | static void rb_insert_bss(struct cfg80211_registered_device *dev, |
| 273 | struct cfg80211_internal_bss *bss) |
| 274 | { |
| 275 | struct rb_node **p = &dev->bss_tree.rb_node; |
| 276 | struct rb_node *parent = NULL; |
| 277 | struct cfg80211_internal_bss *tbss; |
| 278 | int cmp; |
| 279 | |
| 280 | while (*p) { |
| 281 | parent = *p; |
| 282 | tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn); |
| 283 | |
| 284 | cmp = cmp_bss(&bss->pub, &tbss->pub); |
| 285 | |
| 286 | if (WARN_ON(!cmp)) { |
| 287 | /* will sort of leak this BSS */ |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | if (cmp < 0) |
| 292 | p = &(*p)->rb_left; |
| 293 | else |
| 294 | p = &(*p)->rb_right; |
| 295 | } |
| 296 | |
| 297 | rb_link_node(&bss->rbn, parent, p); |
| 298 | rb_insert_color(&bss->rbn, &dev->bss_tree); |
| 299 | } |
| 300 | |
| 301 | static struct cfg80211_internal_bss * |
| 302 | rb_find_bss(struct cfg80211_registered_device *dev, |
| 303 | struct cfg80211_internal_bss *res) |
| 304 | { |
| 305 | struct rb_node *n = dev->bss_tree.rb_node; |
| 306 | struct cfg80211_internal_bss *bss; |
| 307 | int r; |
| 308 | |
| 309 | while (n) { |
| 310 | bss = rb_entry(n, struct cfg80211_internal_bss, rbn); |
| 311 | r = cmp_bss(&res->pub, &bss->pub); |
| 312 | |
| 313 | if (r == 0) |
| 314 | return bss; |
| 315 | else if (r < 0) |
| 316 | n = n->rb_left; |
| 317 | else |
| 318 | n = n->rb_right; |
| 319 | } |
| 320 | |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | static struct cfg80211_internal_bss * |
| 325 | cfg80211_bss_update(struct cfg80211_registered_device *dev, |
| 326 | struct cfg80211_internal_bss *res, |
| 327 | bool overwrite) |
| 328 | { |
| 329 | struct cfg80211_internal_bss *found = NULL; |
| 330 | const u8 *meshid, *meshcfg; |
| 331 | |
| 332 | /* |
| 333 | * The reference to "res" is donated to this function. |
| 334 | */ |
| 335 | |
| 336 | if (WARN_ON(!res->pub.channel)) { |
| 337 | kref_put(&res->ref, bss_release); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | res->ts = jiffies; |
| 342 | |
| 343 | if (is_zero_ether_addr(res->pub.bssid)) { |
| 344 | /* must be mesh, verify */ |
| 345 | meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements, |
| 346 | res->pub.len_information_elements); |
| 347 | meshcfg = find_ie(WLAN_EID_MESH_CONFIG, |
| 348 | res->pub.information_elements, |
| 349 | res->pub.len_information_elements); |
| 350 | if (!meshid || !meshcfg || |
| 351 | meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) { |
| 352 | /* bogus mesh */ |
| 353 | kref_put(&res->ref, bss_release); |
| 354 | return NULL; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | spin_lock_bh(&dev->bss_lock); |
| 359 | |
| 360 | found = rb_find_bss(dev, res); |
| 361 | |
| 362 | if (found && overwrite) { |
| 363 | list_replace(&found->list, &res->list); |
| 364 | rb_replace_node(&found->rbn, &res->rbn, |
| 365 | &dev->bss_tree); |
| 366 | kref_put(&found->ref, bss_release); |
| 367 | found = res; |
| 368 | } else if (found) { |
| 369 | kref_get(&found->ref); |
| 370 | found->pub.beacon_interval = res->pub.beacon_interval; |
| 371 | found->pub.tsf = res->pub.tsf; |
| 372 | found->pub.signal = res->pub.signal; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 373 | found->pub.capability = res->pub.capability; |
| 374 | found->ts = res->ts; |
| 375 | kref_put(&res->ref, bss_release); |
| 376 | } else { |
| 377 | /* this "consumes" the reference */ |
| 378 | list_add_tail(&res->list, &dev->bss_list); |
| 379 | rb_insert_bss(dev, res); |
| 380 | found = res; |
| 381 | } |
| 382 | |
| 383 | dev->bss_generation++; |
| 384 | spin_unlock_bh(&dev->bss_lock); |
| 385 | |
| 386 | kref_get(&found->ref); |
| 387 | return found; |
| 388 | } |
| 389 | |
| 390 | struct cfg80211_bss * |
| 391 | cfg80211_inform_bss_frame(struct wiphy *wiphy, |
| 392 | struct ieee80211_channel *channel, |
| 393 | struct ieee80211_mgmt *mgmt, size_t len, |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame^] | 394 | s32 signal, gfp_t gfp) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 395 | { |
| 396 | struct cfg80211_internal_bss *res; |
| 397 | size_t ielen = len - offsetof(struct ieee80211_mgmt, |
| 398 | u.probe_resp.variable); |
| 399 | bool overwrite; |
| 400 | size_t privsz = wiphy->bss_priv_size; |
| 401 | |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame^] | 402 | if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC && |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 403 | (signal < 0 || signal > 100))) |
| 404 | return NULL; |
| 405 | |
| 406 | if (WARN_ON(!mgmt || !wiphy || |
| 407 | len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable))) |
| 408 | return NULL; |
| 409 | |
| 410 | res = kzalloc(sizeof(*res) + privsz + ielen, gfp); |
| 411 | if (!res) |
| 412 | return NULL; |
| 413 | |
| 414 | memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN); |
| 415 | res->pub.channel = channel; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 416 | res->pub.signal = signal; |
| 417 | res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); |
| 418 | res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int); |
| 419 | res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info); |
| 420 | /* point to after the private area */ |
| 421 | res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz; |
| 422 | memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen); |
| 423 | res->pub.len_information_elements = ielen; |
| 424 | |
| 425 | kref_init(&res->ref); |
| 426 | |
| 427 | overwrite = ieee80211_is_probe_resp(mgmt->frame_control); |
| 428 | |
| 429 | res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite); |
| 430 | if (!res) |
| 431 | return NULL; |
| 432 | |
| 433 | /* cfg80211_bss_update gives us a referenced result */ |
| 434 | return &res->pub; |
| 435 | } |
| 436 | EXPORT_SYMBOL(cfg80211_inform_bss_frame); |
| 437 | |
| 438 | void cfg80211_put_bss(struct cfg80211_bss *pub) |
| 439 | { |
| 440 | struct cfg80211_internal_bss *bss; |
| 441 | |
| 442 | if (!pub) |
| 443 | return; |
| 444 | |
| 445 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 446 | kref_put(&bss->ref, bss_release); |
| 447 | } |
| 448 | EXPORT_SYMBOL(cfg80211_put_bss); |
| 449 | |
Johannes Berg | d491af1 | 2009-02-10 21:25:58 +0100 | [diff] [blame] | 450 | void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) |
| 451 | { |
| 452 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 453 | struct cfg80211_internal_bss *bss; |
| 454 | |
| 455 | if (WARN_ON(!pub)) |
| 456 | return; |
| 457 | |
| 458 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 459 | |
| 460 | spin_lock_bh(&dev->bss_lock); |
| 461 | |
| 462 | list_del(&bss->list); |
| 463 | rb_erase(&bss->rbn, &dev->bss_tree); |
| 464 | |
| 465 | spin_unlock_bh(&dev->bss_lock); |
| 466 | |
| 467 | kref_put(&bss->ref, bss_release); |
| 468 | } |
| 469 | EXPORT_SYMBOL(cfg80211_unlink_bss); |
| 470 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 471 | #ifdef CONFIG_WIRELESS_EXT |
| 472 | int cfg80211_wext_siwscan(struct net_device *dev, |
| 473 | struct iw_request_info *info, |
| 474 | union iwreq_data *wrqu, char *extra) |
| 475 | { |
| 476 | struct cfg80211_registered_device *rdev; |
| 477 | struct wiphy *wiphy; |
| 478 | struct iw_scan_req *wreq = NULL; |
| 479 | struct cfg80211_scan_request *creq; |
| 480 | int i, err, n_channels = 0; |
| 481 | enum ieee80211_band band; |
| 482 | |
| 483 | if (!netif_running(dev)) |
| 484 | return -ENETDOWN; |
| 485 | |
| 486 | rdev = cfg80211_get_dev_from_ifindex(dev->ifindex); |
| 487 | |
| 488 | if (IS_ERR(rdev)) |
| 489 | return PTR_ERR(rdev); |
| 490 | |
| 491 | if (rdev->scan_req) { |
| 492 | err = -EBUSY; |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | wiphy = &rdev->wiphy; |
| 497 | |
| 498 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
| 499 | if (wiphy->bands[band]) |
| 500 | n_channels += wiphy->bands[band]->n_channels; |
| 501 | |
| 502 | creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) + |
| 503 | n_channels * sizeof(void *), |
| 504 | GFP_ATOMIC); |
| 505 | if (!creq) { |
| 506 | err = -ENOMEM; |
| 507 | goto out; |
| 508 | } |
| 509 | |
| 510 | creq->wiphy = wiphy; |
| 511 | creq->ifidx = dev->ifindex; |
| 512 | creq->ssids = (void *)(creq + 1); |
| 513 | creq->channels = (void *)(creq->ssids + 1); |
| 514 | creq->n_channels = n_channels; |
| 515 | creq->n_ssids = 1; |
| 516 | |
| 517 | /* all channels */ |
| 518 | i = 0; |
| 519 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 520 | int j; |
| 521 | if (!wiphy->bands[band]) |
| 522 | continue; |
| 523 | for (j = 0; j < wiphy->bands[band]->n_channels; j++) { |
| 524 | creq->channels[i] = &wiphy->bands[band]->channels[j]; |
| 525 | i++; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /* translate scan request */ |
| 530 | if (wrqu->data.length == sizeof(struct iw_scan_req)) { |
| 531 | wreq = (struct iw_scan_req *)extra; |
| 532 | |
| 533 | if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { |
| 534 | if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) |
| 535 | return -EINVAL; |
| 536 | memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len); |
| 537 | creq->ssids[0].ssid_len = wreq->essid_len; |
| 538 | } |
| 539 | if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) |
| 540 | creq->n_ssids = 0; |
| 541 | } |
| 542 | |
| 543 | rdev->scan_req = creq; |
| 544 | err = rdev->ops->scan(wiphy, dev, creq); |
| 545 | if (err) { |
| 546 | rdev->scan_req = NULL; |
| 547 | kfree(creq); |
| 548 | } |
| 549 | out: |
| 550 | cfg80211_put_dev(rdev); |
| 551 | return err; |
| 552 | } |
| 553 | EXPORT_SYMBOL(cfg80211_wext_siwscan); |
| 554 | |
| 555 | static void ieee80211_scan_add_ies(struct iw_request_info *info, |
| 556 | struct cfg80211_bss *bss, |
| 557 | char **current_ev, char *end_buf) |
| 558 | { |
| 559 | u8 *pos, *end, *next; |
| 560 | struct iw_event iwe; |
| 561 | |
| 562 | if (!bss->information_elements || |
| 563 | !bss->len_information_elements) |
| 564 | return; |
| 565 | |
| 566 | /* |
| 567 | * If needed, fragment the IEs buffer (at IE boundaries) into short |
| 568 | * enough fragments to fit into IW_GENERIC_IE_MAX octet messages. |
| 569 | */ |
| 570 | pos = bss->information_elements; |
| 571 | end = pos + bss->len_information_elements; |
| 572 | |
| 573 | while (end - pos > IW_GENERIC_IE_MAX) { |
| 574 | next = pos + 2 + pos[1]; |
| 575 | while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX) |
| 576 | next = next + 2 + next[1]; |
| 577 | |
| 578 | memset(&iwe, 0, sizeof(iwe)); |
| 579 | iwe.cmd = IWEVGENIE; |
| 580 | iwe.u.data.length = next - pos; |
| 581 | *current_ev = iwe_stream_add_point(info, *current_ev, |
| 582 | end_buf, &iwe, pos); |
| 583 | |
| 584 | pos = next; |
| 585 | } |
| 586 | |
| 587 | if (end > pos) { |
| 588 | memset(&iwe, 0, sizeof(iwe)); |
| 589 | iwe.cmd = IWEVGENIE; |
| 590 | iwe.u.data.length = end - pos; |
| 591 | *current_ev = iwe_stream_add_point(info, *current_ev, |
| 592 | end_buf, &iwe, pos); |
| 593 | } |
| 594 | } |
| 595 | |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame] | 596 | static inline unsigned int elapsed_jiffies_msecs(unsigned long start) |
| 597 | { |
| 598 | unsigned long end = jiffies; |
| 599 | |
| 600 | if (end >= start) |
| 601 | return jiffies_to_msecs(end - start); |
| 602 | |
| 603 | return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1); |
| 604 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 605 | |
| 606 | static char * |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame^] | 607 | ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info, |
| 608 | struct cfg80211_internal_bss *bss, char *current_ev, |
| 609 | char *end_buf) |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 610 | { |
| 611 | struct iw_event iwe; |
| 612 | u8 *buf, *cfg, *p; |
| 613 | u8 *ie = bss->pub.information_elements; |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 614 | int rem = bss->pub.len_information_elements, i, sig; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 615 | bool ismesh = false; |
| 616 | |
| 617 | memset(&iwe, 0, sizeof(iwe)); |
| 618 | iwe.cmd = SIOCGIWAP; |
| 619 | iwe.u.ap_addr.sa_family = ARPHRD_ETHER; |
| 620 | memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); |
| 621 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 622 | IW_EV_ADDR_LEN); |
| 623 | |
| 624 | memset(&iwe, 0, sizeof(iwe)); |
| 625 | iwe.cmd = SIOCGIWFREQ; |
| 626 | iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); |
| 627 | iwe.u.freq.e = 0; |
| 628 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 629 | IW_EV_FREQ_LEN); |
| 630 | |
| 631 | memset(&iwe, 0, sizeof(iwe)); |
| 632 | iwe.cmd = SIOCGIWFREQ; |
| 633 | iwe.u.freq.m = bss->pub.channel->center_freq; |
| 634 | iwe.u.freq.e = 6; |
| 635 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 636 | IW_EV_FREQ_LEN); |
| 637 | |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame^] | 638 | if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 639 | memset(&iwe, 0, sizeof(iwe)); |
| 640 | iwe.cmd = IWEVQUAL; |
| 641 | iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED | |
| 642 | IW_QUAL_NOISE_INVALID | |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 643 | IW_QUAL_QUAL_UPDATED; |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame^] | 644 | switch (wiphy->signal_type) { |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 645 | case CFG80211_SIGNAL_TYPE_MBM: |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 646 | sig = bss->pub.signal / 100; |
| 647 | iwe.u.qual.level = sig; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 648 | iwe.u.qual.updated |= IW_QUAL_DBM; |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 649 | if (sig < -110) /* rather bad */ |
| 650 | sig = -110; |
| 651 | else if (sig > -40) /* perfect */ |
| 652 | sig = -40; |
| 653 | /* will give a range of 0 .. 70 */ |
| 654 | iwe.u.qual.qual = sig + 110; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 655 | break; |
| 656 | case CFG80211_SIGNAL_TYPE_UNSPEC: |
| 657 | iwe.u.qual.level = bss->pub.signal; |
Johannes Berg | a77b855 | 2009-02-18 18:27:22 +0100 | [diff] [blame] | 658 | /* will give range 0 .. 100 */ |
| 659 | iwe.u.qual.qual = bss->pub.signal; |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 660 | break; |
| 661 | default: |
| 662 | /* not reached */ |
| 663 | break; |
| 664 | } |
| 665 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, |
| 666 | &iwe, IW_EV_QUAL_LEN); |
| 667 | } |
| 668 | |
| 669 | memset(&iwe, 0, sizeof(iwe)); |
| 670 | iwe.cmd = SIOCGIWENCODE; |
| 671 | if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY) |
| 672 | iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; |
| 673 | else |
| 674 | iwe.u.data.flags = IW_ENCODE_DISABLED; |
| 675 | iwe.u.data.length = 0; |
| 676 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 677 | &iwe, ""); |
| 678 | |
| 679 | while (rem >= 2) { |
| 680 | /* invalid data */ |
| 681 | if (ie[1] > rem - 2) |
| 682 | break; |
| 683 | |
| 684 | switch (ie[0]) { |
| 685 | case WLAN_EID_SSID: |
| 686 | memset(&iwe, 0, sizeof(iwe)); |
| 687 | iwe.cmd = SIOCGIWESSID; |
| 688 | iwe.u.data.length = ie[1]; |
| 689 | iwe.u.data.flags = 1; |
| 690 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 691 | &iwe, ie + 2); |
| 692 | break; |
| 693 | case WLAN_EID_MESH_ID: |
| 694 | memset(&iwe, 0, sizeof(iwe)); |
| 695 | iwe.cmd = SIOCGIWESSID; |
| 696 | iwe.u.data.length = ie[1]; |
| 697 | iwe.u.data.flags = 1; |
| 698 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 699 | &iwe, ie + 2); |
| 700 | break; |
| 701 | case WLAN_EID_MESH_CONFIG: |
| 702 | ismesh = true; |
| 703 | if (ie[1] != IEEE80211_MESH_CONFIG_LEN) |
| 704 | break; |
| 705 | buf = kmalloc(50, GFP_ATOMIC); |
| 706 | if (!buf) |
| 707 | break; |
| 708 | cfg = ie + 2; |
| 709 | memset(&iwe, 0, sizeof(iwe)); |
| 710 | iwe.cmd = IWEVCUSTOM; |
| 711 | sprintf(buf, "Mesh network (version %d)", cfg[0]); |
| 712 | iwe.u.data.length = strlen(buf); |
| 713 | current_ev = iwe_stream_add_point(info, current_ev, |
| 714 | end_buf, |
| 715 | &iwe, buf); |
| 716 | sprintf(buf, "Path Selection Protocol ID: " |
| 717 | "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3], |
| 718 | cfg[4]); |
| 719 | iwe.u.data.length = strlen(buf); |
| 720 | current_ev = iwe_stream_add_point(info, current_ev, |
| 721 | end_buf, |
| 722 | &iwe, buf); |
| 723 | sprintf(buf, "Path Selection Metric ID: " |
| 724 | "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7], |
| 725 | cfg[8]); |
| 726 | iwe.u.data.length = strlen(buf); |
| 727 | current_ev = iwe_stream_add_point(info, current_ev, |
| 728 | end_buf, |
| 729 | &iwe, buf); |
| 730 | sprintf(buf, "Congestion Control Mode ID: " |
| 731 | "0x%02X%02X%02X%02X", cfg[9], cfg[10], |
| 732 | cfg[11], cfg[12]); |
| 733 | iwe.u.data.length = strlen(buf); |
| 734 | current_ev = iwe_stream_add_point(info, current_ev, |
| 735 | end_buf, |
| 736 | &iwe, buf); |
| 737 | sprintf(buf, "Channel Precedence: " |
| 738 | "0x%02X%02X%02X%02X", cfg[13], cfg[14], |
| 739 | cfg[15], cfg[16]); |
| 740 | iwe.u.data.length = strlen(buf); |
| 741 | current_ev = iwe_stream_add_point(info, current_ev, |
| 742 | end_buf, |
| 743 | &iwe, buf); |
| 744 | kfree(buf); |
| 745 | break; |
| 746 | case WLAN_EID_SUPP_RATES: |
| 747 | case WLAN_EID_EXT_SUPP_RATES: |
| 748 | /* display all supported rates in readable format */ |
| 749 | p = current_ev + iwe_stream_lcp_len(info); |
| 750 | |
| 751 | memset(&iwe, 0, sizeof(iwe)); |
| 752 | iwe.cmd = SIOCGIWRATE; |
| 753 | /* Those two flags are ignored... */ |
| 754 | iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; |
| 755 | |
| 756 | for (i = 0; i < ie[1]; i++) { |
| 757 | iwe.u.bitrate.value = |
| 758 | ((ie[i + 2] & 0x7f) * 500000); |
| 759 | p = iwe_stream_add_value(info, current_ev, p, |
| 760 | end_buf, &iwe, IW_EV_PARAM_LEN); |
| 761 | } |
| 762 | current_ev = p; |
| 763 | break; |
| 764 | } |
| 765 | rem -= ie[1] + 2; |
| 766 | ie += ie[1] + 2; |
| 767 | } |
| 768 | |
| 769 | if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) |
| 770 | || ismesh) { |
| 771 | memset(&iwe, 0, sizeof(iwe)); |
| 772 | iwe.cmd = SIOCGIWMODE; |
| 773 | if (ismesh) |
| 774 | iwe.u.mode = IW_MODE_MESH; |
| 775 | else if (bss->pub.capability & WLAN_CAPABILITY_ESS) |
| 776 | iwe.u.mode = IW_MODE_MASTER; |
| 777 | else |
| 778 | iwe.u.mode = IW_MODE_ADHOC; |
| 779 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, |
| 780 | &iwe, IW_EV_UINT_LEN); |
| 781 | } |
| 782 | |
| 783 | buf = kmalloc(30, GFP_ATOMIC); |
| 784 | if (buf) { |
| 785 | memset(&iwe, 0, sizeof(iwe)); |
| 786 | iwe.cmd = IWEVCUSTOM; |
| 787 | sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf)); |
| 788 | iwe.u.data.length = strlen(buf); |
| 789 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 790 | &iwe, buf); |
| 791 | memset(&iwe, 0, sizeof(iwe)); |
| 792 | iwe.cmd = IWEVCUSTOM; |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame] | 793 | sprintf(buf, " Last beacon: %ums ago", |
| 794 | elapsed_jiffies_msecs(bss->ts)); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 795 | iwe.u.data.length = strlen(buf); |
| 796 | current_ev = iwe_stream_add_point(info, current_ev, |
| 797 | end_buf, &iwe, buf); |
| 798 | kfree(buf); |
| 799 | } |
| 800 | |
| 801 | ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf); |
| 802 | |
| 803 | return current_ev; |
| 804 | } |
| 805 | |
| 806 | |
| 807 | static int ieee80211_scan_results(struct cfg80211_registered_device *dev, |
| 808 | struct iw_request_info *info, |
| 809 | char *buf, size_t len) |
| 810 | { |
| 811 | char *current_ev = buf; |
| 812 | char *end_buf = buf + len; |
| 813 | struct cfg80211_internal_bss *bss; |
| 814 | |
| 815 | spin_lock_bh(&dev->bss_lock); |
| 816 | cfg80211_bss_expire(dev); |
| 817 | |
| 818 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 819 | if (buf + len - current_ev <= IW_EV_ADDR_LEN) { |
| 820 | spin_unlock_bh(&dev->bss_lock); |
| 821 | return -E2BIG; |
| 822 | } |
Johannes Berg | 77965c9 | 2009-02-18 18:45:06 +0100 | [diff] [blame^] | 823 | current_ev = ieee80211_bss(&dev->wiphy, info, bss, |
| 824 | current_ev, end_buf); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 825 | } |
| 826 | spin_unlock_bh(&dev->bss_lock); |
| 827 | return current_ev - buf; |
| 828 | } |
| 829 | |
| 830 | |
| 831 | int cfg80211_wext_giwscan(struct net_device *dev, |
| 832 | struct iw_request_info *info, |
| 833 | struct iw_point *data, char *extra) |
| 834 | { |
| 835 | struct cfg80211_registered_device *rdev; |
| 836 | int res; |
| 837 | |
| 838 | if (!netif_running(dev)) |
| 839 | return -ENETDOWN; |
| 840 | |
| 841 | rdev = cfg80211_get_dev_from_ifindex(dev->ifindex); |
| 842 | |
| 843 | if (IS_ERR(rdev)) |
| 844 | return PTR_ERR(rdev); |
| 845 | |
| 846 | if (rdev->scan_req) { |
| 847 | res = -EAGAIN; |
| 848 | goto out; |
| 849 | } |
| 850 | |
| 851 | res = ieee80211_scan_results(rdev, info, extra, data->length); |
| 852 | data->length = 0; |
| 853 | if (res >= 0) { |
| 854 | data->length = res; |
| 855 | res = 0; |
| 856 | } |
| 857 | |
| 858 | out: |
| 859 | cfg80211_put_dev(rdev); |
| 860 | return res; |
| 861 | } |
| 862 | EXPORT_SYMBOL(cfg80211_wext_giwscan); |
| 863 | #endif |