1 /* 2 * SME code for cfg80211 3 * both driver SME event handling and the SME implementation 4 * (for nl80211's connect() and wext) 5 * 6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net> 7 * Copyright (C) 2009 Intel Corporation. All rights reserved. 8 * Copyright 2017 Intel Deutschland GmbH 9 */ 10 11 #include <linux/etherdevice.h> 12 #include <linux/if_arp.h> 13 #include <linux/slab.h> 14 #include <linux/workqueue.h> 15 #include <linux/wireless.h> 16 #include <linux/export.h> 17 #include <net/iw_handler.h> 18 #include <net/cfg80211.h> 19 #include <net/rtnetlink.h> 20 #include "nl80211.h" 21 #include "reg.h" 22 #include "rdev-ops.h" 23 24 /* 25 * Software SME in cfg80211, using auth/assoc/deauth calls to the 26 * driver. This is is for implementing nl80211's connect/disconnect 27 * and wireless extensions (if configured.) 28 */ 29 30 struct cfg80211_conn { 31 struct cfg80211_connect_params params; 32 /* these are sub-states of the _CONNECTING sme_state */ 33 enum { 34 CFG80211_CONN_SCANNING, 35 CFG80211_CONN_SCAN_AGAIN, 36 CFG80211_CONN_AUTHENTICATE_NEXT, 37 CFG80211_CONN_AUTHENTICATING, 38 CFG80211_CONN_AUTH_FAILED_TIMEOUT, 39 CFG80211_CONN_ASSOCIATE_NEXT, 40 CFG80211_CONN_ASSOCIATING, 41 CFG80211_CONN_ASSOC_FAILED, 42 CFG80211_CONN_ASSOC_FAILED_TIMEOUT, 43 CFG80211_CONN_DEAUTH, 44 CFG80211_CONN_ABANDON, 45 CFG80211_CONN_CONNECTED, 46 } state; 47 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN]; 48 const u8 *ie; 49 size_t ie_len; 50 bool auto_auth, prev_bssid_valid; 51 }; 52 53 static void cfg80211_sme_free(struct wireless_dev *wdev) 54 { 55 if (!wdev->conn) 56 return; 57 58 kfree(wdev->conn->ie); 59 kfree(wdev->conn); 60 wdev->conn = NULL; 61 } 62 63 static int cfg80211_conn_scan(struct wireless_dev *wdev) 64 { 65 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 66 struct cfg80211_scan_request *request; 67 int n_channels, err; 68 69 ASSERT_RTNL(); 70 ASSERT_WDEV_LOCK(wdev); 71 72 if (rdev->scan_req || rdev->scan_msg) 73 return -EBUSY; 74 75 if (wdev->conn->params.channel) 76 n_channels = 1; 77 else 78 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy); 79 80 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) + 81 sizeof(request->channels[0]) * n_channels, 82 GFP_KERNEL); 83 if (!request) 84 return -ENOMEM; 85 86 if (wdev->conn->params.channel) { 87 enum nl80211_band band = wdev->conn->params.channel->band; 88 struct ieee80211_supported_band *sband = 89 wdev->wiphy->bands[band]; 90 91 if (!sband) { 92 kfree(request); 93 return -EINVAL; 94 } 95 request->channels[0] = wdev->conn->params.channel; 96 request->rates[band] = (1 << sband->n_bitrates) - 1; 97 } else { 98 int i = 0, j; 99 enum nl80211_band band; 100 struct ieee80211_supported_band *bands; 101 struct ieee80211_channel *channel; 102 103 for (band = 0; band < NUM_NL80211_BANDS; band++) { 104 bands = wdev->wiphy->bands[band]; 105 if (!bands) 106 continue; 107 for (j = 0; j < bands->n_channels; j++) { 108 channel = &bands->channels[j]; 109 if (channel->flags & IEEE80211_CHAN_DISABLED) 110 continue; 111 request->channels[i++] = channel; 112 } 113 request->rates[band] = (1 << bands->n_bitrates) - 1; 114 } 115 n_channels = i; 116 } 117 request->n_channels = n_channels; 118 request->ssids = (void *)&request->channels[n_channels]; 119 request->n_ssids = 1; 120 121 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid, 122 wdev->conn->params.ssid_len); 123 request->ssids[0].ssid_len = wdev->conn->params.ssid_len; 124 125 eth_broadcast_addr(request->bssid); 126 127 request->wdev = wdev; 128 request->wiphy = &rdev->wiphy; 129 request->scan_start = jiffies; 130 131 rdev->scan_req = request; 132 133 err = rdev_scan(rdev, request); 134 if (!err) { 135 wdev->conn->state = CFG80211_CONN_SCANNING; 136 nl80211_send_scan_start(rdev, wdev); 137 dev_hold(wdev->netdev); 138 } else { 139 rdev->scan_req = NULL; 140 kfree(request); 141 } 142 return err; 143 } 144 145 static int cfg80211_conn_do_work(struct wireless_dev *wdev, 146 enum nl80211_timeout_reason *treason) 147 { 148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 149 struct cfg80211_connect_params *params; 150 struct cfg80211_assoc_request req = {}; 151 int err; 152 153 ASSERT_WDEV_LOCK(wdev); 154 155 if (!wdev->conn) 156 return 0; 157 158 params = &wdev->conn->params; 159 160 switch (wdev->conn->state) { 161 case CFG80211_CONN_SCANNING: 162 /* didn't find it during scan ... */ 163 return -ENOENT; 164 case CFG80211_CONN_SCAN_AGAIN: 165 return cfg80211_conn_scan(wdev); 166 case CFG80211_CONN_AUTHENTICATE_NEXT: 167 if (WARN_ON(!rdev->ops->auth)) 168 return -EOPNOTSUPP; 169 wdev->conn->state = CFG80211_CONN_AUTHENTICATING; 170 return cfg80211_mlme_auth(rdev, wdev->netdev, 171 params->channel, params->auth_type, 172 params->bssid, 173 params->ssid, params->ssid_len, 174 NULL, 0, 175 params->key, params->key_len, 176 params->key_idx, NULL, 0); 177 case CFG80211_CONN_AUTH_FAILED_TIMEOUT: 178 *treason = NL80211_TIMEOUT_AUTH; 179 return -ENOTCONN; 180 case CFG80211_CONN_ASSOCIATE_NEXT: 181 if (WARN_ON(!rdev->ops->assoc)) 182 return -EOPNOTSUPP; 183 wdev->conn->state = CFG80211_CONN_ASSOCIATING; 184 if (wdev->conn->prev_bssid_valid) 185 req.prev_bssid = wdev->conn->prev_bssid; 186 req.ie = params->ie; 187 req.ie_len = params->ie_len; 188 req.use_mfp = params->mfp != NL80211_MFP_NO; 189 req.crypto = params->crypto; 190 req.flags = params->flags; 191 req.ht_capa = params->ht_capa; 192 req.ht_capa_mask = params->ht_capa_mask; 193 req.vht_capa = params->vht_capa; 194 req.vht_capa_mask = params->vht_capa_mask; 195 196 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel, 197 params->bssid, params->ssid, 198 params->ssid_len, &req); 199 if (err) 200 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid, 201 NULL, 0, 202 WLAN_REASON_DEAUTH_LEAVING, 203 false); 204 return err; 205 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT: 206 *treason = NL80211_TIMEOUT_ASSOC; 207 /* fall through */ 208 case CFG80211_CONN_ASSOC_FAILED: 209 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid, 210 NULL, 0, 211 WLAN_REASON_DEAUTH_LEAVING, false); 212 return -ENOTCONN; 213 case CFG80211_CONN_DEAUTH: 214 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid, 215 NULL, 0, 216 WLAN_REASON_DEAUTH_LEAVING, false); 217 /* fall through */ 218 case CFG80211_CONN_ABANDON: 219 /* free directly, disconnected event already sent */ 220 cfg80211_sme_free(wdev); 221 return 0; 222 default: 223 return 0; 224 } 225 } 226 227 void cfg80211_conn_work(struct work_struct *work) 228 { 229 struct cfg80211_registered_device *rdev = 230 container_of(work, struct cfg80211_registered_device, conn_work); 231 struct wireless_dev *wdev; 232 u8 bssid_buf[ETH_ALEN], *bssid = NULL; 233 enum nl80211_timeout_reason treason; 234 235 rtnl_lock(); 236 237 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 238 if (!wdev->netdev) 239 continue; 240 241 wdev_lock(wdev); 242 if (!netif_running(wdev->netdev)) { 243 wdev_unlock(wdev); 244 continue; 245 } 246 if (!wdev->conn || 247 wdev->conn->state == CFG80211_CONN_CONNECTED) { 248 wdev_unlock(wdev); 249 continue; 250 } 251 if (wdev->conn->params.bssid) { 252 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN); 253 bssid = bssid_buf; 254 } 255 treason = NL80211_TIMEOUT_UNSPECIFIED; 256 if (cfg80211_conn_do_work(wdev, &treason)) { 257 struct cfg80211_connect_resp_params cr; 258 259 memset(&cr, 0, sizeof(cr)); 260 cr.status = -1; 261 cr.bssid = bssid; 262 cr.timeout_reason = treason; 263 __cfg80211_connect_result(wdev->netdev, &cr, false); 264 } 265 wdev_unlock(wdev); 266 } 267 268 rtnl_unlock(); 269 } 270 271 /* Returned bss is reference counted and must be cleaned up appropriately. */ 272 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev) 273 { 274 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 275 struct cfg80211_bss *bss; 276 277 ASSERT_WDEV_LOCK(wdev); 278 279 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel, 280 wdev->conn->params.bssid, 281 wdev->conn->params.ssid, 282 wdev->conn->params.ssid_len, 283 wdev->conn_bss_type, 284 IEEE80211_PRIVACY(wdev->conn->params.privacy)); 285 if (!bss) 286 return NULL; 287 288 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN); 289 wdev->conn->params.bssid = wdev->conn->bssid; 290 wdev->conn->params.channel = bss->channel; 291 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT; 292 schedule_work(&rdev->conn_work); 293 294 return bss; 295 } 296 297 static void __cfg80211_sme_scan_done(struct net_device *dev) 298 { 299 struct wireless_dev *wdev = dev->ieee80211_ptr; 300 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 301 struct cfg80211_bss *bss; 302 303 ASSERT_WDEV_LOCK(wdev); 304 305 if (!wdev->conn) 306 return; 307 308 if (wdev->conn->state != CFG80211_CONN_SCANNING && 309 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN) 310 return; 311 312 bss = cfg80211_get_conn_bss(wdev); 313 if (bss) 314 cfg80211_put_bss(&rdev->wiphy, bss); 315 else 316 schedule_work(&rdev->conn_work); 317 } 318 319 void cfg80211_sme_scan_done(struct net_device *dev) 320 { 321 struct wireless_dev *wdev = dev->ieee80211_ptr; 322 323 wdev_lock(wdev); 324 __cfg80211_sme_scan_done(dev); 325 wdev_unlock(wdev); 326 } 327 328 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len) 329 { 330 struct wiphy *wiphy = wdev->wiphy; 331 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 332 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf; 333 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code); 334 335 ASSERT_WDEV_LOCK(wdev); 336 337 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED) 338 return; 339 340 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG && 341 wdev->conn->auto_auth && 342 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) { 343 /* select automatically between only open, shared, leap */ 344 switch (wdev->conn->params.auth_type) { 345 case NL80211_AUTHTYPE_OPEN_SYSTEM: 346 if (wdev->connect_keys) 347 wdev->conn->params.auth_type = 348 NL80211_AUTHTYPE_SHARED_KEY; 349 else 350 wdev->conn->params.auth_type = 351 NL80211_AUTHTYPE_NETWORK_EAP; 352 break; 353 case NL80211_AUTHTYPE_SHARED_KEY: 354 wdev->conn->params.auth_type = 355 NL80211_AUTHTYPE_NETWORK_EAP; 356 break; 357 default: 358 /* huh? */ 359 wdev->conn->params.auth_type = 360 NL80211_AUTHTYPE_OPEN_SYSTEM; 361 break; 362 } 363 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT; 364 schedule_work(&rdev->conn_work); 365 } else if (status_code != WLAN_STATUS_SUCCESS) { 366 struct cfg80211_connect_resp_params cr; 367 368 memset(&cr, 0, sizeof(cr)); 369 cr.status = status_code; 370 cr.bssid = mgmt->bssid; 371 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED; 372 __cfg80211_connect_result(wdev->netdev, &cr, false); 373 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) { 374 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT; 375 schedule_work(&rdev->conn_work); 376 } 377 } 378 379 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status) 380 { 381 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 382 383 if (!wdev->conn) 384 return false; 385 386 if (status == WLAN_STATUS_SUCCESS) { 387 wdev->conn->state = CFG80211_CONN_CONNECTED; 388 return false; 389 } 390 391 if (wdev->conn->prev_bssid_valid) { 392 /* 393 * Some stupid APs don't accept reassoc, so we 394 * need to fall back to trying regular assoc; 395 * return true so no event is sent to userspace. 396 */ 397 wdev->conn->prev_bssid_valid = false; 398 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT; 399 schedule_work(&rdev->conn_work); 400 return true; 401 } 402 403 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED; 404 schedule_work(&rdev->conn_work); 405 return false; 406 } 407 408 void cfg80211_sme_deauth(struct wireless_dev *wdev) 409 { 410 cfg80211_sme_free(wdev); 411 } 412 413 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev) 414 { 415 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 416 417 if (!wdev->conn) 418 return; 419 420 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT; 421 schedule_work(&rdev->conn_work); 422 } 423 424 void cfg80211_sme_disassoc(struct wireless_dev *wdev) 425 { 426 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 427 428 if (!wdev->conn) 429 return; 430 431 wdev->conn->state = CFG80211_CONN_DEAUTH; 432 schedule_work(&rdev->conn_work); 433 } 434 435 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev) 436 { 437 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 438 439 if (!wdev->conn) 440 return; 441 442 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT; 443 schedule_work(&rdev->conn_work); 444 } 445 446 void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev) 447 { 448 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 449 450 if (!wdev->conn) 451 return; 452 453 wdev->conn->state = CFG80211_CONN_ABANDON; 454 schedule_work(&rdev->conn_work); 455 } 456 457 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev, 458 const u8 *ies, size_t ies_len, 459 const u8 **out_ies, size_t *out_ies_len) 460 { 461 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 462 u8 *buf; 463 size_t offs; 464 465 if (!rdev->wiphy.extended_capabilities_len || 466 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) { 467 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL); 468 if (!*out_ies) 469 return -ENOMEM; 470 *out_ies_len = ies_len; 471 return 0; 472 } 473 474 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2, 475 GFP_KERNEL); 476 if (!buf) 477 return -ENOMEM; 478 479 if (ies_len) { 480 static const u8 before_extcapa[] = { 481 /* not listing IEs expected to be created by driver */ 482 WLAN_EID_RSN, 483 WLAN_EID_QOS_CAPA, 484 WLAN_EID_RRM_ENABLED_CAPABILITIES, 485 WLAN_EID_MOBILITY_DOMAIN, 486 WLAN_EID_SUPPORTED_REGULATORY_CLASSES, 487 WLAN_EID_BSS_COEX_2040, 488 }; 489 490 offs = ieee80211_ie_split(ies, ies_len, before_extcapa, 491 ARRAY_SIZE(before_extcapa), 0); 492 memcpy(buf, ies, offs); 493 /* leave a whole for extended capabilities IE */ 494 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2, 495 ies + offs, ies_len - offs); 496 } else { 497 offs = 0; 498 } 499 500 /* place extended capabilities IE (with only driver capabilities) */ 501 buf[offs] = WLAN_EID_EXT_CAPABILITY; 502 buf[offs + 1] = rdev->wiphy.extended_capabilities_len; 503 memcpy(buf + offs + 2, 504 rdev->wiphy.extended_capabilities, 505 rdev->wiphy.extended_capabilities_len); 506 507 *out_ies = buf; 508 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2; 509 510 return 0; 511 } 512 513 static int cfg80211_sme_connect(struct wireless_dev *wdev, 514 struct cfg80211_connect_params *connect, 515 const u8 *prev_bssid) 516 { 517 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 518 struct cfg80211_bss *bss; 519 int err; 520 521 if (!rdev->ops->auth || !rdev->ops->assoc) 522 return -EOPNOTSUPP; 523 524 if (wdev->current_bss) { 525 if (!prev_bssid) 526 return -EALREADY; 527 if (prev_bssid && 528 !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid)) 529 return -ENOTCONN; 530 cfg80211_unhold_bss(wdev->current_bss); 531 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); 532 wdev->current_bss = NULL; 533 534 cfg80211_sme_free(wdev); 535 } 536 537 if (WARN_ON(wdev->conn)) 538 return -EINPROGRESS; 539 540 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL); 541 if (!wdev->conn) 542 return -ENOMEM; 543 544 /* 545 * Copy all parameters, and treat explicitly IEs, BSSID, SSID. 546 */ 547 memcpy(&wdev->conn->params, connect, sizeof(*connect)); 548 if (connect->bssid) { 549 wdev->conn->params.bssid = wdev->conn->bssid; 550 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN); 551 } 552 553 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len, 554 &wdev->conn->ie, 555 &wdev->conn->params.ie_len)) { 556 kfree(wdev->conn); 557 wdev->conn = NULL; 558 return -ENOMEM; 559 } 560 wdev->conn->params.ie = wdev->conn->ie; 561 562 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) { 563 wdev->conn->auto_auth = true; 564 /* start with open system ... should mostly work */ 565 wdev->conn->params.auth_type = 566 NL80211_AUTHTYPE_OPEN_SYSTEM; 567 } else { 568 wdev->conn->auto_auth = false; 569 } 570 571 wdev->conn->params.ssid = wdev->ssid; 572 wdev->conn->params.ssid_len = wdev->ssid_len; 573 574 /* see if we have the bss already */ 575 bss = cfg80211_get_conn_bss(wdev); 576 577 if (prev_bssid) { 578 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN); 579 wdev->conn->prev_bssid_valid = true; 580 } 581 582 /* we're good if we have a matching bss struct */ 583 if (bss) { 584 enum nl80211_timeout_reason treason; 585 586 err = cfg80211_conn_do_work(wdev, &treason); 587 cfg80211_put_bss(wdev->wiphy, bss); 588 } else { 589 /* otherwise we'll need to scan for the AP first */ 590 err = cfg80211_conn_scan(wdev); 591 592 /* 593 * If we can't scan right now, then we need to scan again 594 * after the current scan finished, since the parameters 595 * changed (unless we find a good AP anyway). 596 */ 597 if (err == -EBUSY) { 598 err = 0; 599 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN; 600 } 601 } 602 603 if (err) 604 cfg80211_sme_free(wdev); 605 606 return err; 607 } 608 609 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason) 610 { 611 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 612 int err; 613 614 if (!wdev->conn) 615 return 0; 616 617 if (!rdev->ops->deauth) 618 return -EOPNOTSUPP; 619 620 if (wdev->conn->state == CFG80211_CONN_SCANNING || 621 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) { 622 err = 0; 623 goto out; 624 } 625 626 /* wdev->conn->params.bssid must be set if > SCANNING */ 627 err = cfg80211_mlme_deauth(rdev, wdev->netdev, 628 wdev->conn->params.bssid, 629 NULL, 0, reason, false); 630 out: 631 cfg80211_sme_free(wdev); 632 return err; 633 } 634 635 /* 636 * code shared for in-device and software SME 637 */ 638 639 static bool cfg80211_is_all_idle(void) 640 { 641 struct cfg80211_registered_device *rdev; 642 struct wireless_dev *wdev; 643 bool is_all_idle = true; 644 645 /* 646 * All devices must be idle as otherwise if you are actively 647 * scanning some new beacon hints could be learned and would 648 * count as new regulatory hints. 649 */ 650 list_for_each_entry(rdev, &cfg80211_rdev_list, list) { 651 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) { 652 wdev_lock(wdev); 653 if (wdev->conn || wdev->current_bss) 654 is_all_idle = false; 655 wdev_unlock(wdev); 656 } 657 } 658 659 return is_all_idle; 660 } 661 662 static void disconnect_work(struct work_struct *work) 663 { 664 rtnl_lock(); 665 if (cfg80211_is_all_idle()) 666 regulatory_hint_disconnect(); 667 rtnl_unlock(); 668 } 669 670 static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work); 671 672 673 /* 674 * API calls for drivers implementing connect/disconnect and 675 * SME event handling 676 */ 677 678 /* This method must consume bss one way or another */ 679 void __cfg80211_connect_result(struct net_device *dev, 680 struct cfg80211_connect_resp_params *cr, 681 bool wextev) 682 { 683 struct wireless_dev *wdev = dev->ieee80211_ptr; 684 const u8 *country_ie; 685 #ifdef CONFIG_CFG80211_WEXT 686 union iwreq_data wrqu; 687 #endif 688 689 ASSERT_WDEV_LOCK(wdev); 690 691 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION && 692 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) { 693 cfg80211_put_bss(wdev->wiphy, cr->bss); 694 return; 695 } 696 697 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr, 698 GFP_KERNEL); 699 700 #ifdef CONFIG_CFG80211_WEXT 701 if (wextev) { 702 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) { 703 memset(&wrqu, 0, sizeof(wrqu)); 704 wrqu.data.length = cr->req_ie_len; 705 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, 706 cr->req_ie); 707 } 708 709 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) { 710 memset(&wrqu, 0, sizeof(wrqu)); 711 wrqu.data.length = cr->resp_ie_len; 712 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, 713 cr->resp_ie); 714 } 715 716 memset(&wrqu, 0, sizeof(wrqu)); 717 wrqu.ap_addr.sa_family = ARPHRD_ETHER; 718 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) { 719 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN); 720 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN); 721 wdev->wext.prev_bssid_valid = true; 722 } 723 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); 724 } 725 #endif 726 727 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) { 728 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect); 729 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid, 730 wdev->ssid, wdev->ssid_len, 731 wdev->conn_bss_type, 732 IEEE80211_PRIVACY_ANY); 733 if (cr->bss) 734 cfg80211_hold_bss(bss_from_pub(cr->bss)); 735 } 736 737 if (wdev->current_bss) { 738 cfg80211_unhold_bss(wdev->current_bss); 739 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); 740 wdev->current_bss = NULL; 741 } 742 743 if (cr->status != WLAN_STATUS_SUCCESS) { 744 kzfree(wdev->connect_keys); 745 wdev->connect_keys = NULL; 746 wdev->ssid_len = 0; 747 wdev->conn_owner_nlportid = 0; 748 if (cr->bss) { 749 cfg80211_unhold_bss(bss_from_pub(cr->bss)); 750 cfg80211_put_bss(wdev->wiphy, cr->bss); 751 } 752 cfg80211_sme_free(wdev); 753 return; 754 } 755 756 if (WARN_ON(!cr->bss)) 757 return; 758 759 wdev->current_bss = bss_from_pub(cr->bss); 760 761 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP)) 762 cfg80211_upload_connect_keys(wdev); 763 764 rcu_read_lock(); 765 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY); 766 if (!country_ie) { 767 rcu_read_unlock(); 768 return; 769 } 770 771 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC); 772 rcu_read_unlock(); 773 774 if (!country_ie) 775 return; 776 777 /* 778 * ieee80211_bss_get_ie() ensures we can access: 779 * - country_ie + 2, the start of the country ie data, and 780 * - and country_ie[1] which is the IE length 781 */ 782 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band, 783 country_ie + 2, country_ie[1]); 784 kfree(country_ie); 785 } 786 787 /* Consumes bss object one way or another */ 788 void cfg80211_connect_done(struct net_device *dev, 789 struct cfg80211_connect_resp_params *params, 790 gfp_t gfp) 791 { 792 struct wireless_dev *wdev = dev->ieee80211_ptr; 793 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 794 struct cfg80211_event *ev; 795 unsigned long flags; 796 u8 *next; 797 798 if (params->bss) { 799 /* Make sure the bss entry provided by the driver is valid. */ 800 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss); 801 802 if (WARN_ON(list_empty(&ibss->list))) { 803 cfg80211_put_bss(wdev->wiphy, params->bss); 804 return; 805 } 806 } 807 808 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) + 809 params->req_ie_len + params->resp_ie_len + 810 params->fils_kek_len + params->pmk_len + 811 (params->pmkid ? WLAN_PMKID_LEN : 0), gfp); 812 if (!ev) { 813 cfg80211_put_bss(wdev->wiphy, params->bss); 814 return; 815 } 816 817 ev->type = EVENT_CONNECT_RESULT; 818 next = ((u8 *)ev) + sizeof(*ev); 819 if (params->bssid) { 820 ev->cr.bssid = next; 821 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN); 822 next += ETH_ALEN; 823 } 824 if (params->req_ie_len) { 825 ev->cr.req_ie = next; 826 ev->cr.req_ie_len = params->req_ie_len; 827 memcpy((void *)ev->cr.req_ie, params->req_ie, 828 params->req_ie_len); 829 next += params->req_ie_len; 830 } 831 if (params->resp_ie_len) { 832 ev->cr.resp_ie = next; 833 ev->cr.resp_ie_len = params->resp_ie_len; 834 memcpy((void *)ev->cr.resp_ie, params->resp_ie, 835 params->resp_ie_len); 836 next += params->resp_ie_len; 837 } 838 if (params->fils_kek_len) { 839 ev->cr.fils_kek = next; 840 ev->cr.fils_kek_len = params->fils_kek_len; 841 memcpy((void *)ev->cr.fils_kek, params->fils_kek, 842 params->fils_kek_len); 843 next += params->fils_kek_len; 844 } 845 if (params->pmk_len) { 846 ev->cr.pmk = next; 847 ev->cr.pmk_len = params->pmk_len; 848 memcpy((void *)ev->cr.pmk, params->pmk, params->pmk_len); 849 next += params->pmk_len; 850 } 851 if (params->pmkid) { 852 ev->cr.pmkid = next; 853 memcpy((void *)ev->cr.pmkid, params->pmkid, WLAN_PMKID_LEN); 854 next += WLAN_PMKID_LEN; 855 } 856 ev->cr.update_erp_next_seq_num = params->update_erp_next_seq_num; 857 if (params->update_erp_next_seq_num) 858 ev->cr.fils_erp_next_seq_num = params->fils_erp_next_seq_num; 859 if (params->bss) 860 cfg80211_hold_bss(bss_from_pub(params->bss)); 861 ev->cr.bss = params->bss; 862 ev->cr.status = params->status; 863 ev->cr.timeout_reason = params->timeout_reason; 864 865 spin_lock_irqsave(&wdev->event_lock, flags); 866 list_add_tail(&ev->list, &wdev->event_list); 867 spin_unlock_irqrestore(&wdev->event_lock, flags); 868 queue_work(cfg80211_wq, &rdev->event_work); 869 } 870 EXPORT_SYMBOL(cfg80211_connect_done); 871 872 /* Consumes bss object one way or another */ 873 void __cfg80211_roamed(struct wireless_dev *wdev, 874 struct cfg80211_roam_info *info) 875 { 876 #ifdef CONFIG_CFG80211_WEXT 877 union iwreq_data wrqu; 878 #endif 879 ASSERT_WDEV_LOCK(wdev); 880 881 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION && 882 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) 883 goto out; 884 885 if (WARN_ON(!wdev->current_bss)) 886 goto out; 887 888 cfg80211_unhold_bss(wdev->current_bss); 889 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); 890 wdev->current_bss = NULL; 891 892 if (WARN_ON(!info->bss)) 893 return; 894 895 cfg80211_hold_bss(bss_from_pub(info->bss)); 896 wdev->current_bss = bss_from_pub(info->bss); 897 898 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy), 899 wdev->netdev, info, GFP_KERNEL); 900 901 #ifdef CONFIG_CFG80211_WEXT 902 if (info->req_ie) { 903 memset(&wrqu, 0, sizeof(wrqu)); 904 wrqu.data.length = info->req_ie_len; 905 wireless_send_event(wdev->netdev, IWEVASSOCREQIE, 906 &wrqu, info->req_ie); 907 } 908 909 if (info->resp_ie) { 910 memset(&wrqu, 0, sizeof(wrqu)); 911 wrqu.data.length = info->resp_ie_len; 912 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE, 913 &wrqu, info->resp_ie); 914 } 915 916 memset(&wrqu, 0, sizeof(wrqu)); 917 wrqu.ap_addr.sa_family = ARPHRD_ETHER; 918 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN); 919 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN); 920 wdev->wext.prev_bssid_valid = true; 921 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL); 922 #endif 923 924 return; 925 out: 926 cfg80211_put_bss(wdev->wiphy, info->bss); 927 } 928 929 /* Consumes info->bss object one way or another */ 930 void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info, 931 gfp_t gfp) 932 { 933 struct wireless_dev *wdev = dev->ieee80211_ptr; 934 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 935 struct cfg80211_event *ev; 936 unsigned long flags; 937 938 if (!info->bss) { 939 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel, 940 info->bssid, wdev->ssid, 941 wdev->ssid_len, 942 wdev->conn_bss_type, 943 IEEE80211_PRIVACY_ANY); 944 } 945 946 if (WARN_ON(!info->bss)) 947 return; 948 949 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len, gfp); 950 if (!ev) { 951 cfg80211_put_bss(wdev->wiphy, info->bss); 952 return; 953 } 954 955 ev->type = EVENT_ROAMED; 956 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev); 957 ev->rm.req_ie_len = info->req_ie_len; 958 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len); 959 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + info->req_ie_len; 960 ev->rm.resp_ie_len = info->resp_ie_len; 961 memcpy((void *)ev->rm.resp_ie, info->resp_ie, info->resp_ie_len); 962 ev->rm.bss = info->bss; 963 964 spin_lock_irqsave(&wdev->event_lock, flags); 965 list_add_tail(&ev->list, &wdev->event_list); 966 spin_unlock_irqrestore(&wdev->event_lock, flags); 967 queue_work(cfg80211_wq, &rdev->event_work); 968 } 969 EXPORT_SYMBOL(cfg80211_roamed); 970 971 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie, 972 size_t ie_len, u16 reason, bool from_ap) 973 { 974 struct wireless_dev *wdev = dev->ieee80211_ptr; 975 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 976 int i; 977 #ifdef CONFIG_CFG80211_WEXT 978 union iwreq_data wrqu; 979 #endif 980 981 ASSERT_WDEV_LOCK(wdev); 982 983 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION && 984 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) 985 return; 986 987 if (wdev->current_bss) { 988 cfg80211_unhold_bss(wdev->current_bss); 989 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); 990 } 991 992 wdev->current_bss = NULL; 993 wdev->ssid_len = 0; 994 wdev->conn_owner_nlportid = 0; 995 996 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap); 997 998 /* stop critical protocol if supported */ 999 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) { 1000 rdev->crit_proto_nlportid = 0; 1001 rdev_crit_proto_stop(rdev, wdev); 1002 } 1003 1004 /* 1005 * Delete all the keys ... pairwise keys can't really 1006 * exist any more anyway, but default keys might. 1007 */ 1008 if (rdev->ops->del_key) 1009 for (i = 0; i < 6; i++) 1010 rdev_del_key(rdev, dev, i, false, NULL); 1011 1012 rdev_set_qos_map(rdev, dev, NULL); 1013 1014 #ifdef CONFIG_CFG80211_WEXT 1015 memset(&wrqu, 0, sizeof(wrqu)); 1016 wrqu.ap_addr.sa_family = ARPHRD_ETHER; 1017 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); 1018 wdev->wext.connect.ssid_len = 0; 1019 #endif 1020 1021 schedule_work(&cfg80211_disconnect_work); 1022 } 1023 1024 void cfg80211_disconnected(struct net_device *dev, u16 reason, 1025 const u8 *ie, size_t ie_len, 1026 bool locally_generated, gfp_t gfp) 1027 { 1028 struct wireless_dev *wdev = dev->ieee80211_ptr; 1029 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 1030 struct cfg80211_event *ev; 1031 unsigned long flags; 1032 1033 ev = kzalloc(sizeof(*ev) + ie_len, gfp); 1034 if (!ev) 1035 return; 1036 1037 ev->type = EVENT_DISCONNECTED; 1038 ev->dc.ie = ((u8 *)ev) + sizeof(*ev); 1039 ev->dc.ie_len = ie_len; 1040 memcpy((void *)ev->dc.ie, ie, ie_len); 1041 ev->dc.reason = reason; 1042 ev->dc.locally_generated = locally_generated; 1043 1044 spin_lock_irqsave(&wdev->event_lock, flags); 1045 list_add_tail(&ev->list, &wdev->event_list); 1046 spin_unlock_irqrestore(&wdev->event_lock, flags); 1047 queue_work(cfg80211_wq, &rdev->event_work); 1048 } 1049 EXPORT_SYMBOL(cfg80211_disconnected); 1050 1051 /* 1052 * API calls for nl80211/wext compatibility code 1053 */ 1054 int cfg80211_connect(struct cfg80211_registered_device *rdev, 1055 struct net_device *dev, 1056 struct cfg80211_connect_params *connect, 1057 struct cfg80211_cached_keys *connkeys, 1058 const u8 *prev_bssid) 1059 { 1060 struct wireless_dev *wdev = dev->ieee80211_ptr; 1061 int err; 1062 1063 ASSERT_WDEV_LOCK(wdev); 1064 1065 if (WARN_ON(wdev->connect_keys)) { 1066 kzfree(wdev->connect_keys); 1067 wdev->connect_keys = NULL; 1068 } 1069 1070 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask, 1071 rdev->wiphy.ht_capa_mod_mask); 1072 1073 if (connkeys && connkeys->def >= 0) { 1074 int idx; 1075 u32 cipher; 1076 1077 idx = connkeys->def; 1078 cipher = connkeys->params[idx].cipher; 1079 /* If given a WEP key we may need it for shared key auth */ 1080 if (cipher == WLAN_CIPHER_SUITE_WEP40 || 1081 cipher == WLAN_CIPHER_SUITE_WEP104) { 1082 connect->key_idx = idx; 1083 connect->key = connkeys->params[idx].key; 1084 connect->key_len = connkeys->params[idx].key_len; 1085 1086 /* 1087 * If ciphers are not set (e.g. when going through 1088 * iwconfig), we have to set them appropriately here. 1089 */ 1090 if (connect->crypto.cipher_group == 0) 1091 connect->crypto.cipher_group = cipher; 1092 1093 if (connect->crypto.n_ciphers_pairwise == 0) { 1094 connect->crypto.n_ciphers_pairwise = 1; 1095 connect->crypto.ciphers_pairwise[0] = cipher; 1096 } 1097 } 1098 1099 connect->crypto.wep_keys = connkeys->params; 1100 connect->crypto.wep_tx_key = connkeys->def; 1101 } else { 1102 if (WARN_ON(connkeys)) 1103 return -EINVAL; 1104 } 1105 1106 wdev->connect_keys = connkeys; 1107 memcpy(wdev->ssid, connect->ssid, connect->ssid_len); 1108 wdev->ssid_len = connect->ssid_len; 1109 1110 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS : 1111 IEEE80211_BSS_TYPE_ESS; 1112 1113 if (!rdev->ops->connect) 1114 err = cfg80211_sme_connect(wdev, connect, prev_bssid); 1115 else 1116 err = rdev_connect(rdev, dev, connect); 1117 1118 if (err) { 1119 wdev->connect_keys = NULL; 1120 wdev->ssid_len = 0; 1121 return err; 1122 } 1123 1124 return 0; 1125 } 1126 1127 int cfg80211_disconnect(struct cfg80211_registered_device *rdev, 1128 struct net_device *dev, u16 reason, bool wextev) 1129 { 1130 struct wireless_dev *wdev = dev->ieee80211_ptr; 1131 int err = 0; 1132 1133 ASSERT_WDEV_LOCK(wdev); 1134 1135 kzfree(wdev->connect_keys); 1136 wdev->connect_keys = NULL; 1137 1138 wdev->conn_owner_nlportid = 0; 1139 1140 if (wdev->conn) 1141 err = cfg80211_sme_disconnect(wdev, reason); 1142 else if (!rdev->ops->disconnect) 1143 cfg80211_mlme_down(rdev, dev); 1144 else if (wdev->ssid_len) 1145 err = rdev_disconnect(rdev, dev, reason); 1146 1147 return err; 1148 } 1149 1150 /* 1151 * Used to clean up after the connection / connection attempt owner socket 1152 * disconnects 1153 */ 1154 void cfg80211_autodisconnect_wk(struct work_struct *work) 1155 { 1156 struct wireless_dev *wdev = 1157 container_of(work, struct wireless_dev, disconnect_wk); 1158 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 1159 1160 wdev_lock(wdev); 1161 1162 if (wdev->conn_owner_nlportid) { 1163 /* 1164 * Use disconnect_bssid if still connecting and ops->disconnect 1165 * not implemented. Otherwise we can use cfg80211_disconnect. 1166 */ 1167 if (rdev->ops->disconnect || wdev->current_bss) 1168 cfg80211_disconnect(rdev, wdev->netdev, 1169 WLAN_REASON_DEAUTH_LEAVING, true); 1170 else 1171 cfg80211_mlme_deauth(rdev, wdev->netdev, 1172 wdev->disconnect_bssid, NULL, 0, 1173 WLAN_REASON_DEAUTH_LEAVING, false); 1174 } 1175 1176 wdev_unlock(wdev); 1177 } 1178