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; |
| 373 | found->pub.signal_type = res->pub.signal_type; |
| 374 | found->pub.capability = res->pub.capability; |
| 375 | found->ts = res->ts; |
| 376 | kref_put(&res->ref, bss_release); |
| 377 | } else { |
| 378 | /* this "consumes" the reference */ |
| 379 | list_add_tail(&res->list, &dev->bss_list); |
| 380 | rb_insert_bss(dev, res); |
| 381 | found = res; |
| 382 | } |
| 383 | |
| 384 | dev->bss_generation++; |
| 385 | spin_unlock_bh(&dev->bss_lock); |
| 386 | |
| 387 | kref_get(&found->ref); |
| 388 | return found; |
| 389 | } |
| 390 | |
| 391 | struct cfg80211_bss * |
| 392 | cfg80211_inform_bss_frame(struct wiphy *wiphy, |
| 393 | struct ieee80211_channel *channel, |
| 394 | struct ieee80211_mgmt *mgmt, size_t len, |
| 395 | s32 signal, enum cfg80211_signal_type sigtype, |
| 396 | gfp_t gfp) |
| 397 | { |
| 398 | struct cfg80211_internal_bss *res; |
| 399 | size_t ielen = len - offsetof(struct ieee80211_mgmt, |
| 400 | u.probe_resp.variable); |
| 401 | bool overwrite; |
| 402 | size_t privsz = wiphy->bss_priv_size; |
| 403 | |
| 404 | if (WARN_ON(sigtype == NL80211_BSS_SIGNAL_UNSPEC && |
| 405 | (signal < 0 || signal > 100))) |
| 406 | return NULL; |
| 407 | |
| 408 | if (WARN_ON(!mgmt || !wiphy || |
| 409 | len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable))) |
| 410 | return NULL; |
| 411 | |
| 412 | res = kzalloc(sizeof(*res) + privsz + ielen, gfp); |
| 413 | if (!res) |
| 414 | return NULL; |
| 415 | |
| 416 | memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN); |
| 417 | res->pub.channel = channel; |
| 418 | res->pub.signal_type = sigtype; |
| 419 | res->pub.signal = signal; |
| 420 | res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp); |
| 421 | res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int); |
| 422 | res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info); |
| 423 | /* point to after the private area */ |
| 424 | res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz; |
| 425 | memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen); |
| 426 | res->pub.len_information_elements = ielen; |
| 427 | |
| 428 | kref_init(&res->ref); |
| 429 | |
| 430 | overwrite = ieee80211_is_probe_resp(mgmt->frame_control); |
| 431 | |
| 432 | res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite); |
| 433 | if (!res) |
| 434 | return NULL; |
| 435 | |
| 436 | /* cfg80211_bss_update gives us a referenced result */ |
| 437 | return &res->pub; |
| 438 | } |
| 439 | EXPORT_SYMBOL(cfg80211_inform_bss_frame); |
| 440 | |
| 441 | void cfg80211_put_bss(struct cfg80211_bss *pub) |
| 442 | { |
| 443 | struct cfg80211_internal_bss *bss; |
| 444 | |
| 445 | if (!pub) |
| 446 | return; |
| 447 | |
| 448 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 449 | kref_put(&bss->ref, bss_release); |
| 450 | } |
| 451 | EXPORT_SYMBOL(cfg80211_put_bss); |
| 452 | |
Johannes Berg | d491af1 | 2009-02-10 21:25:58 +0100 | [diff] [blame] | 453 | void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub) |
| 454 | { |
| 455 | struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy); |
| 456 | struct cfg80211_internal_bss *bss; |
| 457 | |
| 458 | if (WARN_ON(!pub)) |
| 459 | return; |
| 460 | |
| 461 | bss = container_of(pub, struct cfg80211_internal_bss, pub); |
| 462 | |
| 463 | spin_lock_bh(&dev->bss_lock); |
| 464 | |
| 465 | list_del(&bss->list); |
| 466 | rb_erase(&bss->rbn, &dev->bss_tree); |
| 467 | |
| 468 | spin_unlock_bh(&dev->bss_lock); |
| 469 | |
| 470 | kref_put(&bss->ref, bss_release); |
| 471 | } |
| 472 | EXPORT_SYMBOL(cfg80211_unlink_bss); |
| 473 | |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 474 | #ifdef CONFIG_WIRELESS_EXT |
| 475 | int cfg80211_wext_siwscan(struct net_device *dev, |
| 476 | struct iw_request_info *info, |
| 477 | union iwreq_data *wrqu, char *extra) |
| 478 | { |
| 479 | struct cfg80211_registered_device *rdev; |
| 480 | struct wiphy *wiphy; |
| 481 | struct iw_scan_req *wreq = NULL; |
| 482 | struct cfg80211_scan_request *creq; |
| 483 | int i, err, n_channels = 0; |
| 484 | enum ieee80211_band band; |
| 485 | |
| 486 | if (!netif_running(dev)) |
| 487 | return -ENETDOWN; |
| 488 | |
| 489 | rdev = cfg80211_get_dev_from_ifindex(dev->ifindex); |
| 490 | |
| 491 | if (IS_ERR(rdev)) |
| 492 | return PTR_ERR(rdev); |
| 493 | |
| 494 | if (rdev->scan_req) { |
| 495 | err = -EBUSY; |
| 496 | goto out; |
| 497 | } |
| 498 | |
| 499 | wiphy = &rdev->wiphy; |
| 500 | |
| 501 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
| 502 | if (wiphy->bands[band]) |
| 503 | n_channels += wiphy->bands[band]->n_channels; |
| 504 | |
| 505 | creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) + |
| 506 | n_channels * sizeof(void *), |
| 507 | GFP_ATOMIC); |
| 508 | if (!creq) { |
| 509 | err = -ENOMEM; |
| 510 | goto out; |
| 511 | } |
| 512 | |
| 513 | creq->wiphy = wiphy; |
| 514 | creq->ifidx = dev->ifindex; |
| 515 | creq->ssids = (void *)(creq + 1); |
| 516 | creq->channels = (void *)(creq->ssids + 1); |
| 517 | creq->n_channels = n_channels; |
| 518 | creq->n_ssids = 1; |
| 519 | |
| 520 | /* all channels */ |
| 521 | i = 0; |
| 522 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 523 | int j; |
| 524 | if (!wiphy->bands[band]) |
| 525 | continue; |
| 526 | for (j = 0; j < wiphy->bands[band]->n_channels; j++) { |
| 527 | creq->channels[i] = &wiphy->bands[band]->channels[j]; |
| 528 | i++; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* translate scan request */ |
| 533 | if (wrqu->data.length == sizeof(struct iw_scan_req)) { |
| 534 | wreq = (struct iw_scan_req *)extra; |
| 535 | |
| 536 | if (wrqu->data.flags & IW_SCAN_THIS_ESSID) { |
| 537 | if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) |
| 538 | return -EINVAL; |
| 539 | memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len); |
| 540 | creq->ssids[0].ssid_len = wreq->essid_len; |
| 541 | } |
| 542 | if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE) |
| 543 | creq->n_ssids = 0; |
| 544 | } |
| 545 | |
| 546 | rdev->scan_req = creq; |
| 547 | err = rdev->ops->scan(wiphy, dev, creq); |
| 548 | if (err) { |
| 549 | rdev->scan_req = NULL; |
| 550 | kfree(creq); |
| 551 | } |
| 552 | out: |
| 553 | cfg80211_put_dev(rdev); |
| 554 | return err; |
| 555 | } |
| 556 | EXPORT_SYMBOL(cfg80211_wext_siwscan); |
| 557 | |
| 558 | static void ieee80211_scan_add_ies(struct iw_request_info *info, |
| 559 | struct cfg80211_bss *bss, |
| 560 | char **current_ev, char *end_buf) |
| 561 | { |
| 562 | u8 *pos, *end, *next; |
| 563 | struct iw_event iwe; |
| 564 | |
| 565 | if (!bss->information_elements || |
| 566 | !bss->len_information_elements) |
| 567 | return; |
| 568 | |
| 569 | /* |
| 570 | * If needed, fragment the IEs buffer (at IE boundaries) into short |
| 571 | * enough fragments to fit into IW_GENERIC_IE_MAX octet messages. |
| 572 | */ |
| 573 | pos = bss->information_elements; |
| 574 | end = pos + bss->len_information_elements; |
| 575 | |
| 576 | while (end - pos > IW_GENERIC_IE_MAX) { |
| 577 | next = pos + 2 + pos[1]; |
| 578 | while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX) |
| 579 | next = next + 2 + next[1]; |
| 580 | |
| 581 | memset(&iwe, 0, sizeof(iwe)); |
| 582 | iwe.cmd = IWEVGENIE; |
| 583 | iwe.u.data.length = next - pos; |
| 584 | *current_ev = iwe_stream_add_point(info, *current_ev, |
| 585 | end_buf, &iwe, pos); |
| 586 | |
| 587 | pos = next; |
| 588 | } |
| 589 | |
| 590 | if (end > pos) { |
| 591 | memset(&iwe, 0, sizeof(iwe)); |
| 592 | iwe.cmd = IWEVGENIE; |
| 593 | iwe.u.data.length = end - pos; |
| 594 | *current_ev = iwe_stream_add_point(info, *current_ev, |
| 595 | end_buf, &iwe, pos); |
| 596 | } |
| 597 | } |
| 598 | |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame^] | 599 | static inline unsigned int elapsed_jiffies_msecs(unsigned long start) |
| 600 | { |
| 601 | unsigned long end = jiffies; |
| 602 | |
| 603 | if (end >= start) |
| 604 | return jiffies_to_msecs(end - start); |
| 605 | |
| 606 | return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1); |
| 607 | } |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 608 | |
| 609 | static char * |
| 610 | ieee80211_bss(struct iw_request_info *info, |
| 611 | struct cfg80211_internal_bss *bss, |
| 612 | char *current_ev, char *end_buf) |
| 613 | { |
| 614 | struct iw_event iwe; |
| 615 | u8 *buf, *cfg, *p; |
| 616 | u8 *ie = bss->pub.information_elements; |
| 617 | int rem = bss->pub.len_information_elements, i; |
| 618 | bool ismesh = false; |
| 619 | |
| 620 | memset(&iwe, 0, sizeof(iwe)); |
| 621 | iwe.cmd = SIOCGIWAP; |
| 622 | iwe.u.ap_addr.sa_family = ARPHRD_ETHER; |
| 623 | memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); |
| 624 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 625 | IW_EV_ADDR_LEN); |
| 626 | |
| 627 | memset(&iwe, 0, sizeof(iwe)); |
| 628 | iwe.cmd = SIOCGIWFREQ; |
| 629 | iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); |
| 630 | iwe.u.freq.e = 0; |
| 631 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 632 | IW_EV_FREQ_LEN); |
| 633 | |
| 634 | memset(&iwe, 0, sizeof(iwe)); |
| 635 | iwe.cmd = SIOCGIWFREQ; |
| 636 | iwe.u.freq.m = bss->pub.channel->center_freq; |
| 637 | iwe.u.freq.e = 6; |
| 638 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, |
| 639 | IW_EV_FREQ_LEN); |
| 640 | |
| 641 | if (bss->pub.signal_type != CFG80211_SIGNAL_TYPE_NONE) { |
| 642 | memset(&iwe, 0, sizeof(iwe)); |
| 643 | iwe.cmd = IWEVQUAL; |
| 644 | iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED | |
| 645 | IW_QUAL_NOISE_INVALID | |
| 646 | IW_QUAL_QUAL_INVALID; |
| 647 | switch (bss->pub.signal_type) { |
| 648 | case CFG80211_SIGNAL_TYPE_MBM: |
| 649 | iwe.u.qual.level = bss->pub.signal / 100; |
| 650 | iwe.u.qual.updated |= IW_QUAL_DBM; |
| 651 | break; |
| 652 | case CFG80211_SIGNAL_TYPE_UNSPEC: |
| 653 | iwe.u.qual.level = bss->pub.signal; |
| 654 | break; |
| 655 | default: |
| 656 | /* not reached */ |
| 657 | break; |
| 658 | } |
| 659 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, |
| 660 | &iwe, IW_EV_QUAL_LEN); |
| 661 | } |
| 662 | |
| 663 | memset(&iwe, 0, sizeof(iwe)); |
| 664 | iwe.cmd = SIOCGIWENCODE; |
| 665 | if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY) |
| 666 | iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; |
| 667 | else |
| 668 | iwe.u.data.flags = IW_ENCODE_DISABLED; |
| 669 | iwe.u.data.length = 0; |
| 670 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 671 | &iwe, ""); |
| 672 | |
| 673 | while (rem >= 2) { |
| 674 | /* invalid data */ |
| 675 | if (ie[1] > rem - 2) |
| 676 | break; |
| 677 | |
| 678 | switch (ie[0]) { |
| 679 | case WLAN_EID_SSID: |
| 680 | memset(&iwe, 0, sizeof(iwe)); |
| 681 | iwe.cmd = SIOCGIWESSID; |
| 682 | iwe.u.data.length = ie[1]; |
| 683 | iwe.u.data.flags = 1; |
| 684 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 685 | &iwe, ie + 2); |
| 686 | break; |
| 687 | case WLAN_EID_MESH_ID: |
| 688 | memset(&iwe, 0, sizeof(iwe)); |
| 689 | iwe.cmd = SIOCGIWESSID; |
| 690 | iwe.u.data.length = ie[1]; |
| 691 | iwe.u.data.flags = 1; |
| 692 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 693 | &iwe, ie + 2); |
| 694 | break; |
| 695 | case WLAN_EID_MESH_CONFIG: |
| 696 | ismesh = true; |
| 697 | if (ie[1] != IEEE80211_MESH_CONFIG_LEN) |
| 698 | break; |
| 699 | buf = kmalloc(50, GFP_ATOMIC); |
| 700 | if (!buf) |
| 701 | break; |
| 702 | cfg = ie + 2; |
| 703 | memset(&iwe, 0, sizeof(iwe)); |
| 704 | iwe.cmd = IWEVCUSTOM; |
| 705 | sprintf(buf, "Mesh network (version %d)", cfg[0]); |
| 706 | iwe.u.data.length = strlen(buf); |
| 707 | current_ev = iwe_stream_add_point(info, current_ev, |
| 708 | end_buf, |
| 709 | &iwe, buf); |
| 710 | sprintf(buf, "Path Selection Protocol ID: " |
| 711 | "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3], |
| 712 | cfg[4]); |
| 713 | iwe.u.data.length = strlen(buf); |
| 714 | current_ev = iwe_stream_add_point(info, current_ev, |
| 715 | end_buf, |
| 716 | &iwe, buf); |
| 717 | sprintf(buf, "Path Selection Metric ID: " |
| 718 | "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7], |
| 719 | cfg[8]); |
| 720 | iwe.u.data.length = strlen(buf); |
| 721 | current_ev = iwe_stream_add_point(info, current_ev, |
| 722 | end_buf, |
| 723 | &iwe, buf); |
| 724 | sprintf(buf, "Congestion Control Mode ID: " |
| 725 | "0x%02X%02X%02X%02X", cfg[9], cfg[10], |
| 726 | cfg[11], cfg[12]); |
| 727 | iwe.u.data.length = strlen(buf); |
| 728 | current_ev = iwe_stream_add_point(info, current_ev, |
| 729 | end_buf, |
| 730 | &iwe, buf); |
| 731 | sprintf(buf, "Channel Precedence: " |
| 732 | "0x%02X%02X%02X%02X", cfg[13], cfg[14], |
| 733 | cfg[15], cfg[16]); |
| 734 | iwe.u.data.length = strlen(buf); |
| 735 | current_ev = iwe_stream_add_point(info, current_ev, |
| 736 | end_buf, |
| 737 | &iwe, buf); |
| 738 | kfree(buf); |
| 739 | break; |
| 740 | case WLAN_EID_SUPP_RATES: |
| 741 | case WLAN_EID_EXT_SUPP_RATES: |
| 742 | /* display all supported rates in readable format */ |
| 743 | p = current_ev + iwe_stream_lcp_len(info); |
| 744 | |
| 745 | memset(&iwe, 0, sizeof(iwe)); |
| 746 | iwe.cmd = SIOCGIWRATE; |
| 747 | /* Those two flags are ignored... */ |
| 748 | iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; |
| 749 | |
| 750 | for (i = 0; i < ie[1]; i++) { |
| 751 | iwe.u.bitrate.value = |
| 752 | ((ie[i + 2] & 0x7f) * 500000); |
| 753 | p = iwe_stream_add_value(info, current_ev, p, |
| 754 | end_buf, &iwe, IW_EV_PARAM_LEN); |
| 755 | } |
| 756 | current_ev = p; |
| 757 | break; |
| 758 | } |
| 759 | rem -= ie[1] + 2; |
| 760 | ie += ie[1] + 2; |
| 761 | } |
| 762 | |
| 763 | if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) |
| 764 | || ismesh) { |
| 765 | memset(&iwe, 0, sizeof(iwe)); |
| 766 | iwe.cmd = SIOCGIWMODE; |
| 767 | if (ismesh) |
| 768 | iwe.u.mode = IW_MODE_MESH; |
| 769 | else if (bss->pub.capability & WLAN_CAPABILITY_ESS) |
| 770 | iwe.u.mode = IW_MODE_MASTER; |
| 771 | else |
| 772 | iwe.u.mode = IW_MODE_ADHOC; |
| 773 | current_ev = iwe_stream_add_event(info, current_ev, end_buf, |
| 774 | &iwe, IW_EV_UINT_LEN); |
| 775 | } |
| 776 | |
| 777 | buf = kmalloc(30, GFP_ATOMIC); |
| 778 | if (buf) { |
| 779 | memset(&iwe, 0, sizeof(iwe)); |
| 780 | iwe.cmd = IWEVCUSTOM; |
| 781 | sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf)); |
| 782 | iwe.u.data.length = strlen(buf); |
| 783 | current_ev = iwe_stream_add_point(info, current_ev, end_buf, |
| 784 | &iwe, buf); |
| 785 | memset(&iwe, 0, sizeof(iwe)); |
| 786 | iwe.cmd = IWEVCUSTOM; |
Dan Williams | cb3a8ee | 2009-02-11 17:14:43 -0500 | [diff] [blame^] | 787 | sprintf(buf, " Last beacon: %ums ago", |
| 788 | elapsed_jiffies_msecs(bss->ts)); |
Johannes Berg | 2a51931 | 2009-02-10 21:25:55 +0100 | [diff] [blame] | 789 | iwe.u.data.length = strlen(buf); |
| 790 | current_ev = iwe_stream_add_point(info, current_ev, |
| 791 | end_buf, &iwe, buf); |
| 792 | kfree(buf); |
| 793 | } |
| 794 | |
| 795 | ieee80211_scan_add_ies(info, &bss->pub, ¤t_ev, end_buf); |
| 796 | |
| 797 | return current_ev; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | static int ieee80211_scan_results(struct cfg80211_registered_device *dev, |
| 802 | struct iw_request_info *info, |
| 803 | char *buf, size_t len) |
| 804 | { |
| 805 | char *current_ev = buf; |
| 806 | char *end_buf = buf + len; |
| 807 | struct cfg80211_internal_bss *bss; |
| 808 | |
| 809 | spin_lock_bh(&dev->bss_lock); |
| 810 | cfg80211_bss_expire(dev); |
| 811 | |
| 812 | list_for_each_entry(bss, &dev->bss_list, list) { |
| 813 | if (buf + len - current_ev <= IW_EV_ADDR_LEN) { |
| 814 | spin_unlock_bh(&dev->bss_lock); |
| 815 | return -E2BIG; |
| 816 | } |
| 817 | current_ev = ieee80211_bss(info, bss, |
| 818 | current_ev, end_buf); |
| 819 | } |
| 820 | spin_unlock_bh(&dev->bss_lock); |
| 821 | return current_ev - buf; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | int cfg80211_wext_giwscan(struct net_device *dev, |
| 826 | struct iw_request_info *info, |
| 827 | struct iw_point *data, char *extra) |
| 828 | { |
| 829 | struct cfg80211_registered_device *rdev; |
| 830 | int res; |
| 831 | |
| 832 | if (!netif_running(dev)) |
| 833 | return -ENETDOWN; |
| 834 | |
| 835 | rdev = cfg80211_get_dev_from_ifindex(dev->ifindex); |
| 836 | |
| 837 | if (IS_ERR(rdev)) |
| 838 | return PTR_ERR(rdev); |
| 839 | |
| 840 | if (rdev->scan_req) { |
| 841 | res = -EAGAIN; |
| 842 | goto out; |
| 843 | } |
| 844 | |
| 845 | res = ieee80211_scan_results(rdev, info, extra, data->length); |
| 846 | data->length = 0; |
| 847 | if (res >= 0) { |
| 848 | data->length = res; |
| 849 | res = 0; |
| 850 | } |
| 851 | |
| 852 | out: |
| 853 | cfg80211_put_dev(rdev); |
| 854 | return res; |
| 855 | } |
| 856 | EXPORT_SYMBOL(cfg80211_wext_giwscan); |
| 857 | #endif |