lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Some IBSS support code for cfg80211. |
| 3 | * |
| 4 | * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/etherdevice.h> |
| 8 | #include <linux/if_arp.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/export.h> |
| 11 | #include <net/cfg80211.h> |
| 12 | #include "wext-compat.h" |
| 13 | #include "nl80211.h" |
| 14 | |
| 15 | |
| 16 | void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid) |
| 17 | { |
| 18 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 19 | struct cfg80211_bss *bss; |
| 20 | #ifdef CONFIG_CFG80211_WEXT |
| 21 | union iwreq_data wrqu; |
| 22 | #endif |
| 23 | |
| 24 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 25 | return; |
| 26 | |
| 27 | if (!wdev->ssid_len) |
| 28 | return; |
| 29 | |
| 30 | bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid, |
| 31 | wdev->ssid, wdev->ssid_len, |
| 32 | WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS); |
| 33 | |
| 34 | if (WARN_ON(!bss)) |
| 35 | return; |
| 36 | |
| 37 | if (wdev->current_bss) { |
| 38 | cfg80211_unhold_bss(wdev->current_bss); |
| 39 | cfg80211_put_bss(&wdev->current_bss->pub); |
| 40 | } |
| 41 | |
| 42 | cfg80211_hold_bss(bss_from_pub(bss)); |
| 43 | wdev->current_bss = bss_from_pub(bss); |
| 44 | |
| 45 | cfg80211_upload_connect_keys(wdev); |
| 46 | |
| 47 | nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid, |
| 48 | GFP_KERNEL); |
| 49 | #ifdef CONFIG_CFG80211_WEXT |
| 50 | memset(&wrqu, 0, sizeof(wrqu)); |
| 51 | memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN); |
| 52 | wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp) |
| 57 | { |
| 58 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 59 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 60 | struct cfg80211_event *ev; |
| 61 | unsigned long flags; |
| 62 | |
| 63 | CFG80211_DEV_WARN_ON(!wdev->ssid_len); |
| 64 | |
| 65 | ev = kzalloc(sizeof(*ev), gfp); |
| 66 | if (!ev) |
| 67 | return; |
| 68 | |
| 69 | ev->type = EVENT_IBSS_JOINED; |
| 70 | memcpy(ev->cr.bssid, bssid, ETH_ALEN); |
| 71 | |
| 72 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 73 | list_add_tail(&ev->list, &wdev->event_list); |
| 74 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 75 | queue_work(cfg80211_wq, &rdev->event_work); |
| 76 | } |
| 77 | EXPORT_SYMBOL(cfg80211_ibss_joined); |
| 78 | |
| 79 | int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev, |
| 80 | struct net_device *dev, |
| 81 | struct cfg80211_ibss_params *params, |
| 82 | struct cfg80211_cached_keys *connkeys) |
| 83 | { |
| 84 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 85 | int err; |
| 86 | |
| 87 | ASSERT_WDEV_LOCK(wdev); |
| 88 | |
| 89 | if (wdev->ssid_len) |
| 90 | return -EALREADY; |
| 91 | |
| 92 | if (!params->basic_rates) { |
| 93 | /* |
| 94 | * If no rates were explicitly configured, |
| 95 | * use the mandatory rate set for 11b or |
| 96 | * 11a for maximum compatibility. |
| 97 | */ |
| 98 | struct ieee80211_supported_band *sband = |
| 99 | rdev->wiphy.bands[params->channel->band]; |
| 100 | int j; |
| 101 | u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ? |
| 102 | IEEE80211_RATE_MANDATORY_A : |
| 103 | IEEE80211_RATE_MANDATORY_B; |
| 104 | |
| 105 | for (j = 0; j < sband->n_bitrates; j++) { |
| 106 | if (sband->bitrates[j].flags & flag) |
| 107 | params->basic_rates |= BIT(j); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (WARN_ON(wdev->connect_keys)) |
| 112 | kfree(wdev->connect_keys); |
| 113 | wdev->connect_keys = connkeys; |
| 114 | |
| 115 | #ifdef CONFIG_CFG80211_WEXT |
| 116 | wdev->wext.ibss.channel = params->channel; |
| 117 | #endif |
| 118 | err = rdev->ops->join_ibss(&rdev->wiphy, dev, params); |
| 119 | if (err) { |
| 120 | wdev->connect_keys = NULL; |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | memcpy(wdev->ssid, params->ssid, params->ssid_len); |
| 125 | wdev->ssid_len = params->ssid_len; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int cfg80211_join_ibss(struct cfg80211_registered_device *rdev, |
| 131 | struct net_device *dev, |
| 132 | struct cfg80211_ibss_params *params, |
| 133 | struct cfg80211_cached_keys *connkeys) |
| 134 | { |
| 135 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 136 | int err; |
| 137 | |
| 138 | mutex_lock(&rdev->devlist_mtx); |
| 139 | wdev_lock(wdev); |
| 140 | err = __cfg80211_join_ibss(rdev, dev, params, connkeys); |
| 141 | wdev_unlock(wdev); |
| 142 | mutex_unlock(&rdev->devlist_mtx); |
| 143 | |
| 144 | return err; |
| 145 | } |
| 146 | |
| 147 | static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext) |
| 148 | { |
| 149 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 150 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 151 | int i; |
| 152 | |
| 153 | ASSERT_WDEV_LOCK(wdev); |
| 154 | |
| 155 | kfree(wdev->connect_keys); |
| 156 | wdev->connect_keys = NULL; |
| 157 | |
| 158 | /* |
| 159 | * Delete all the keys ... pairwise keys can't really |
| 160 | * exist any more anyway, but default keys might. |
| 161 | */ |
| 162 | if (rdev->ops->del_key) |
| 163 | for (i = 0; i < 6; i++) |
| 164 | rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL); |
| 165 | |
| 166 | if (wdev->current_bss) { |
| 167 | cfg80211_unhold_bss(wdev->current_bss); |
| 168 | cfg80211_put_bss(&wdev->current_bss->pub); |
| 169 | } |
| 170 | |
| 171 | wdev->current_bss = NULL; |
| 172 | wdev->ssid_len = 0; |
| 173 | #ifdef CONFIG_CFG80211_WEXT |
| 174 | if (!nowext) |
| 175 | wdev->wext.ibss.ssid_len = 0; |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | void cfg80211_clear_ibss(struct net_device *dev, bool nowext) |
| 180 | { |
| 181 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 182 | |
| 183 | wdev_lock(wdev); |
| 184 | __cfg80211_clear_ibss(dev, nowext); |
| 185 | wdev_unlock(wdev); |
| 186 | } |
| 187 | |
| 188 | int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev, |
| 189 | struct net_device *dev, bool nowext) |
| 190 | { |
| 191 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 192 | int err; |
| 193 | |
| 194 | ASSERT_WDEV_LOCK(wdev); |
| 195 | |
| 196 | if (!wdev->ssid_len) |
| 197 | return -ENOLINK; |
| 198 | |
| 199 | err = rdev->ops->leave_ibss(&rdev->wiphy, dev); |
| 200 | |
| 201 | if (err) |
| 202 | return err; |
| 203 | |
| 204 | __cfg80211_clear_ibss(dev, nowext); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev, |
| 210 | struct net_device *dev, bool nowext) |
| 211 | { |
| 212 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 213 | int err; |
| 214 | |
| 215 | wdev_lock(wdev); |
| 216 | err = __cfg80211_leave_ibss(rdev, dev, nowext); |
| 217 | wdev_unlock(wdev); |
| 218 | |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | #ifdef CONFIG_CFG80211_WEXT |
| 223 | int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev, |
| 224 | struct wireless_dev *wdev) |
| 225 | { |
| 226 | struct cfg80211_cached_keys *ck = NULL; |
| 227 | enum ieee80211_band band; |
| 228 | int i, err; |
| 229 | |
| 230 | ASSERT_WDEV_LOCK(wdev); |
| 231 | |
| 232 | if (!wdev->wext.ibss.beacon_interval) |
| 233 | wdev->wext.ibss.beacon_interval = 100; |
| 234 | |
| 235 | /* try to find an IBSS channel if none requested ... */ |
| 236 | if (!wdev->wext.ibss.channel) { |
| 237 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 238 | struct ieee80211_supported_band *sband; |
| 239 | struct ieee80211_channel *chan; |
| 240 | |
| 241 | sband = rdev->wiphy.bands[band]; |
| 242 | if (!sband) |
| 243 | continue; |
| 244 | |
| 245 | for (i = 0; i < sband->n_channels; i++) { |
| 246 | chan = &sband->channels[i]; |
| 247 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) |
| 248 | continue; |
| 249 | if (chan->flags & IEEE80211_CHAN_DISABLED) |
| 250 | continue; |
| 251 | wdev->wext.ibss.channel = chan; |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (wdev->wext.ibss.channel) |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | if (!wdev->wext.ibss.channel) |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | |
| 263 | /* don't join -- SSID is not there */ |
| 264 | if (!wdev->wext.ibss.ssid_len) |
| 265 | return 0; |
| 266 | |
| 267 | if (!netif_running(wdev->netdev)) |
| 268 | return 0; |
| 269 | |
| 270 | if (wdev->wext.keys) { |
| 271 | wdev->wext.keys->def = wdev->wext.default_key; |
| 272 | wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key; |
| 273 | } |
| 274 | |
| 275 | wdev->wext.ibss.privacy = wdev->wext.default_key != -1; |
| 276 | |
| 277 | if (wdev->wext.keys) { |
| 278 | ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL); |
| 279 | if (!ck) |
| 280 | return -ENOMEM; |
| 281 | for (i = 0; i < 6; i++) |
| 282 | ck->params[i].key = ck->data[i]; |
| 283 | } |
| 284 | err = __cfg80211_join_ibss(rdev, wdev->netdev, |
| 285 | &wdev->wext.ibss, ck); |
| 286 | if (err) |
| 287 | kfree(ck); |
| 288 | |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | int cfg80211_ibss_wext_siwfreq(struct net_device *dev, |
| 293 | struct iw_request_info *info, |
| 294 | struct iw_freq *wextfreq, char *extra) |
| 295 | { |
| 296 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 297 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 298 | struct ieee80211_channel *chan = NULL; |
| 299 | int err, freq; |
| 300 | |
| 301 | /* call only for ibss! */ |
| 302 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | if (!rdev->ops->join_ibss) |
| 306 | return -EOPNOTSUPP; |
| 307 | |
| 308 | freq = cfg80211_wext_freq(wdev->wiphy, wextfreq); |
| 309 | if (freq < 0) |
| 310 | return freq; |
| 311 | |
| 312 | if (freq) { |
| 313 | chan = ieee80211_get_channel(wdev->wiphy, freq); |
| 314 | if (!chan) |
| 315 | return -EINVAL; |
| 316 | if (chan->flags & IEEE80211_CHAN_NO_IBSS || |
| 317 | chan->flags & IEEE80211_CHAN_DISABLED) |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | |
| 321 | if (wdev->wext.ibss.channel == chan) |
| 322 | return 0; |
| 323 | |
| 324 | wdev_lock(wdev); |
| 325 | err = 0; |
| 326 | if (wdev->ssid_len) |
| 327 | err = __cfg80211_leave_ibss(rdev, dev, true); |
| 328 | wdev_unlock(wdev); |
| 329 | |
| 330 | if (err) |
| 331 | return err; |
| 332 | |
| 333 | if (chan) { |
| 334 | wdev->wext.ibss.channel = chan; |
| 335 | wdev->wext.ibss.channel_fixed = true; |
| 336 | } else { |
| 337 | /* cfg80211_ibss_wext_join will pick one if needed */ |
| 338 | wdev->wext.ibss.channel_fixed = false; |
| 339 | } |
| 340 | |
| 341 | mutex_lock(&rdev->devlist_mtx); |
| 342 | wdev_lock(wdev); |
| 343 | err = cfg80211_ibss_wext_join(rdev, wdev); |
| 344 | wdev_unlock(wdev); |
| 345 | mutex_unlock(&rdev->devlist_mtx); |
| 346 | |
| 347 | return err; |
| 348 | } |
| 349 | |
| 350 | int cfg80211_ibss_wext_giwfreq(struct net_device *dev, |
| 351 | struct iw_request_info *info, |
| 352 | struct iw_freq *freq, char *extra) |
| 353 | { |
| 354 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 355 | struct ieee80211_channel *chan = NULL; |
| 356 | |
| 357 | /* call only for ibss! */ |
| 358 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | wdev_lock(wdev); |
| 362 | if (wdev->current_bss) |
| 363 | chan = wdev->current_bss->pub.channel; |
| 364 | else if (wdev->wext.ibss.channel) |
| 365 | chan = wdev->wext.ibss.channel; |
| 366 | wdev_unlock(wdev); |
| 367 | |
| 368 | if (chan) { |
| 369 | freq->m = chan->center_freq; |
| 370 | freq->e = 6; |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* no channel if not joining */ |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | |
| 378 | int cfg80211_ibss_wext_siwessid(struct net_device *dev, |
| 379 | struct iw_request_info *info, |
| 380 | struct iw_point *data, char *ssid) |
| 381 | { |
| 382 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 383 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 384 | size_t len = data->length; |
| 385 | int err; |
| 386 | |
| 387 | /* call only for ibss! */ |
| 388 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | if (!rdev->ops->join_ibss) |
| 392 | return -EOPNOTSUPP; |
| 393 | |
| 394 | wdev_lock(wdev); |
| 395 | err = 0; |
| 396 | if (wdev->ssid_len) |
| 397 | err = __cfg80211_leave_ibss(rdev, dev, true); |
| 398 | wdev_unlock(wdev); |
| 399 | |
| 400 | if (err) |
| 401 | return err; |
| 402 | |
| 403 | /* iwconfig uses nul termination in SSID.. */ |
| 404 | if (len > 0 && ssid[len - 1] == '\0') |
| 405 | len--; |
| 406 | |
| 407 | wdev->wext.ibss.ssid = wdev->ssid; |
| 408 | memcpy(wdev->wext.ibss.ssid, ssid, len); |
| 409 | wdev->wext.ibss.ssid_len = len; |
| 410 | |
| 411 | mutex_lock(&rdev->devlist_mtx); |
| 412 | wdev_lock(wdev); |
| 413 | err = cfg80211_ibss_wext_join(rdev, wdev); |
| 414 | wdev_unlock(wdev); |
| 415 | mutex_unlock(&rdev->devlist_mtx); |
| 416 | |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | int cfg80211_ibss_wext_giwessid(struct net_device *dev, |
| 421 | struct iw_request_info *info, |
| 422 | struct iw_point *data, char *ssid) |
| 423 | { |
| 424 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 425 | |
| 426 | /* call only for ibss! */ |
| 427 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 428 | return -EINVAL; |
| 429 | |
| 430 | data->flags = 0; |
| 431 | |
| 432 | wdev_lock(wdev); |
| 433 | if (wdev->ssid_len) { |
| 434 | data->flags = 1; |
| 435 | data->length = wdev->ssid_len; |
| 436 | memcpy(ssid, wdev->ssid, data->length); |
| 437 | } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) { |
| 438 | data->flags = 1; |
| 439 | data->length = wdev->wext.ibss.ssid_len; |
| 440 | memcpy(ssid, wdev->wext.ibss.ssid, data->length); |
| 441 | } |
| 442 | wdev_unlock(wdev); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | int cfg80211_ibss_wext_siwap(struct net_device *dev, |
| 448 | struct iw_request_info *info, |
| 449 | struct sockaddr *ap_addr, char *extra) |
| 450 | { |
| 451 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 452 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 453 | u8 *bssid = ap_addr->sa_data; |
| 454 | int err; |
| 455 | |
| 456 | /* call only for ibss! */ |
| 457 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | if (!rdev->ops->join_ibss) |
| 461 | return -EOPNOTSUPP; |
| 462 | |
| 463 | if (ap_addr->sa_family != ARPHRD_ETHER) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | /* automatic mode */ |
| 467 | if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) |
| 468 | bssid = NULL; |
| 469 | |
| 470 | /* both automatic */ |
| 471 | if (!bssid && !wdev->wext.ibss.bssid) |
| 472 | return 0; |
| 473 | |
| 474 | /* fixed already - and no change */ |
| 475 | if (wdev->wext.ibss.bssid && bssid && |
| 476 | compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0) |
| 477 | return 0; |
| 478 | |
| 479 | wdev_lock(wdev); |
| 480 | err = 0; |
| 481 | if (wdev->ssid_len) |
| 482 | err = __cfg80211_leave_ibss(rdev, dev, true); |
| 483 | wdev_unlock(wdev); |
| 484 | |
| 485 | if (err) |
| 486 | return err; |
| 487 | |
| 488 | if (bssid) { |
| 489 | memcpy(wdev->wext.bssid, bssid, ETH_ALEN); |
| 490 | wdev->wext.ibss.bssid = wdev->wext.bssid; |
| 491 | } else |
| 492 | wdev->wext.ibss.bssid = NULL; |
| 493 | |
| 494 | mutex_lock(&rdev->devlist_mtx); |
| 495 | wdev_lock(wdev); |
| 496 | err = cfg80211_ibss_wext_join(rdev, wdev); |
| 497 | wdev_unlock(wdev); |
| 498 | mutex_unlock(&rdev->devlist_mtx); |
| 499 | |
| 500 | return err; |
| 501 | } |
| 502 | |
| 503 | int cfg80211_ibss_wext_giwap(struct net_device *dev, |
| 504 | struct iw_request_info *info, |
| 505 | struct sockaddr *ap_addr, char *extra) |
| 506 | { |
| 507 | struct wireless_dev *wdev = dev->ieee80211_ptr; |
| 508 | |
| 509 | /* call only for ibss! */ |
| 510 | if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC)) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | ap_addr->sa_family = ARPHRD_ETHER; |
| 514 | |
| 515 | wdev_lock(wdev); |
| 516 | if (wdev->current_bss) |
| 517 | memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN); |
| 518 | else if (wdev->wext.ibss.bssid) |
| 519 | memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN); |
| 520 | else |
| 521 | memset(ap_addr->sa_data, 0, ETH_ALEN); |
| 522 | |
| 523 | wdev_unlock(wdev); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | #endif |