network.c (56387B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <sys/types.h> 29 #include <stdio.h> 30 #include <errno.h> 31 #include <limits.h> 32 #include <alloca.h> 33 #include <linux/if_ether.h> 34 35 #include <ell/ell.h> 36 37 #include "ell/useful.h" 38 39 #include "src/missing.h" 40 #include "src/module.h" 41 #include "src/ie.h" 42 #include "src/crypto.h" 43 #include "src/iwd.h" 44 #include "src/common.h" 45 #include "src/storage.h" 46 #include "src/scan.h" 47 #include "src/dbus.h" 48 #include "src/agent.h" 49 #include "src/netdev.h" 50 #include "src/wiphy.h" 51 #include "src/station.h" 52 #include "src/eap.h" 53 #include "src/knownnetworks.h" 54 #include "src/network.h" 55 #include "src/blacklist.h" 56 #include "src/util.h" 57 #include "src/erp.h" 58 #include "src/handshake.h" 59 #include "src/band.h" 60 #include "src/util.h" 61 62 #define SAE_PT_SETTING "SAE-PT-Group%u" 63 64 static uint32_t known_networks_watch; 65 static uint32_t event_watch; 66 67 struct network { 68 char ssid[SSID_MAX_SIZE + 1]; 69 enum security security; 70 char *object_path; 71 struct station *station; 72 struct network_info *info; 73 unsigned char *psk; 74 char *passphrase; 75 char *password_identifier; 76 struct l_ecc_point *sae_pt_19; /* SAE PT for Group 19 */ 77 struct l_ecc_point *sae_pt_20; /* SAE PT for Group 20 */ 78 unsigned int agent_request; 79 struct l_queue *bss_list; 80 struct l_settings *settings; 81 struct l_queue *secrets; 82 struct l_queue *blacklist; /* temporary blacklist for BSS's */ 83 uint8_t hessid[6]; 84 char **nai_realms; 85 uint8_t *rc_ie; 86 bool sync_settings:1; /* should settings be synced on connect? */ 87 bool ask_passphrase:1; /* Whether we should force-ask agent */ 88 bool is_hs20:1; 89 bool anqp_pending:1; /* Set if there is a pending ANQP request */ 90 bool owe_hidden_pending:1; 91 bool provisioning_hidden:1; 92 uint8_t transition_disable; /* Temporary cache until info is set */ 93 bool have_transition_disable:1; 94 bool force_default_ecc_group:1; 95 int rank; 96 /* Holds DBus Connect() message if it comes in before ANQP finishes */ 97 struct l_dbus_message *connect_after_anqp; 98 struct l_dbus_message *connect_after_owe_hidden; 99 }; 100 101 static bool network_settings_load(struct network *network) 102 { 103 if (network->settings) 104 return true; 105 106 if (network->info) 107 network->settings = network_info_open_settings(network->info); 108 109 return network->settings != NULL; 110 } 111 112 static void network_reset_psk(struct network *network) 113 { 114 if (network->psk) 115 explicit_bzero(network->psk, 32); 116 117 l_free(network->psk); 118 network->psk = NULL; 119 } 120 121 static void network_reset_passphrase(struct network *network) 122 { 123 if (network->passphrase) { 124 explicit_bzero(network->passphrase, 125 strlen(network->passphrase)); 126 l_free(network->passphrase); 127 network->passphrase = NULL; 128 } 129 130 if (network->password_identifier) { 131 explicit_bzero(network->password_identifier, 132 strlen(network->password_identifier)); 133 l_free(network->password_identifier); 134 network->password_identifier = NULL; 135 } 136 137 if (network->sae_pt_19) { 138 l_ecc_point_free(network->sae_pt_19); 139 network->sae_pt_19 = NULL; 140 } 141 142 if (network->sae_pt_20) { 143 l_ecc_point_free(network->sae_pt_20); 144 network->sae_pt_20 = NULL; 145 } 146 } 147 148 static void network_settings_close(struct network *network) 149 { 150 if (!network->settings) 151 return; 152 153 network_reset_psk(network); 154 network_reset_passphrase(network); 155 156 l_settings_free(network->settings); 157 network->settings = NULL; 158 } 159 160 static bool network_secret_check_cacheable(void *data, void *user_data) 161 { 162 struct eap_secret_info *secret = data; 163 164 if (secret->cache_policy == EAP_CACHE_NEVER) { 165 eap_secret_info_free(secret); 166 return true; 167 } 168 169 return false; 170 } 171 172 void network_clear_blacklist(struct network *network) 173 { 174 l_queue_clear(network->blacklist, NULL); 175 } 176 177 void network_connected(struct network *network) 178 { 179 enum security security = network_get_security(network); 180 const char *ssid = network_get_ssid(network); 181 int err; 182 183 if (!network->info) { 184 /* 185 * This is an open network seen for the first time: 186 * 187 * Write a settings file to keep track of the 188 * last connected time. This will also make iwd autoconnect 189 * to this network in the future. 190 */ 191 if (!network->settings) 192 network->settings = l_settings_new(); 193 194 storage_network_sync(security, ssid, network->settings); 195 } else { 196 err = network_info_touch(network->info); 197 if (err < 0) 198 l_error("Error %i touching network config", err); 199 200 /* Syncs frequencies of already known network*/ 201 known_network_frequency_sync(network->info); 202 } 203 204 l_queue_foreach_remove(network->secrets, 205 network_secret_check_cacheable, network); 206 207 network->provisioning_hidden = false; 208 } 209 210 void network_disconnected(struct network *network) 211 { 212 network_settings_close(network); 213 214 network_clear_blacklist(network); 215 216 if (network->provisioning_hidden) 217 station_hide_network(network->station, network); 218 } 219 220 bool network_rankmod(const struct network *network, double *rankmod) 221 { 222 struct network_info *info = network->info; 223 int n; 224 225 /* 226 * Current policy is that only networks successfully connected 227 * to at least once are autoconnectable. Known Networks that 228 * we have never connected to are not. 229 */ 230 if (!info || !info->config.connected_time) 231 return false; 232 233 n = known_network_offset(network->info); 234 if (n < 0) 235 return false; 236 237 *rankmod = util_exponential_decay(n); 238 239 return true; 240 } 241 242 struct network *network_create(struct station *station, const char *ssid, 243 enum security security) 244 { 245 struct network *network; 246 247 network = l_new(struct network, 1); 248 network->station = station; 249 strcpy(network->ssid, ssid); 250 network->security = security; 251 252 network->info = known_networks_find(ssid, security); 253 if (network->info) { 254 network->info->seen_count++; 255 if (network->info->config.ecc_group == 256 KNOWN_NETWORK_ECC_GROUP_DEFAULT) 257 network->force_default_ecc_group = true; 258 } 259 260 network->bss_list = l_queue_new(); 261 network->blacklist = l_queue_new(); 262 263 return network; 264 } 265 266 const char *network_get_ssid(const struct network *network) 267 { 268 return network->ssid; 269 } 270 271 const char *network_get_path(const struct network *network) 272 { 273 return network->object_path; 274 } 275 276 enum security network_get_security(const struct network *network) 277 { 278 return network->security; 279 } 280 281 static const uint8_t *network_get_psk(struct network *network) 282 { 283 int r; 284 285 if (network->psk) 286 return network->psk; 287 288 network->psk = l_malloc(32); 289 290 if ((r = crypto_psk_from_passphrase(network->passphrase, 291 (unsigned char *)network->ssid, 292 strlen(network->ssid), 293 network->psk)) < 0) { 294 l_free(network->psk); 295 network->psk = NULL; 296 l_error("PSK generation failed: %s.", strerror(-r)); 297 } else 298 network->sync_settings = true; 299 300 return network->psk; 301 } 302 303 static struct l_ecc_point *network_generate_sae_pt(struct network *network, 304 unsigned int group) 305 { 306 struct l_ecc_point *pt; 307 308 l_debug("Generating PT for Group %u", group); 309 310 pt = crypto_derive_sae_pt_ecc(group, network->ssid, 311 network->passphrase, 312 network->password_identifier); 313 if (!pt) 314 l_warn("SAE PT generation for Group %u failed", group); 315 316 return pt; 317 } 318 319 static bool __network_set_passphrase(struct network *network, 320 const char *passphrase) 321 { 322 if (!passphrase || !crypto_passphrase_is_valid(passphrase)) 323 return false; 324 325 network_reset_passphrase(network); 326 network->passphrase = l_strdup(passphrase); 327 328 network->sae_pt_19 = network_generate_sae_pt(network, 19); 329 network->sae_pt_20 = network_generate_sae_pt(network, 20); 330 331 network->sync_settings = true; 332 333 return true; 334 } 335 336 bool network_set_passphrase(struct network *network, const char *passphrase) 337 { 338 if (network_get_security(network) != SECURITY_PSK) 339 return false; 340 341 if (!network_settings_load(network)) 342 network->settings = l_settings_new(); 343 344 return __network_set_passphrase(network, passphrase); 345 } 346 347 bool network_set_psk(struct network *network, const uint8_t *psk) 348 { 349 if (network_get_security(network) != SECURITY_PSK) 350 return false; 351 352 if (!network_settings_load(network)) 353 network->settings = l_settings_new(); 354 355 network_reset_psk(network); 356 network->psk = l_memdup(psk, 32); 357 return true; 358 } 359 360 int network_set_transition_disable(struct network *network, 361 const uint8_t *td, size_t len) 362 { 363 struct network_info *info = network->info; 364 /* We only recognize bits 0, 2, 3 */ 365 uint8_t supported_bitmask = 0x0d; 366 367 if (len < 1) 368 return -EBADMSG; 369 370 network->have_transition_disable = true; 371 network->transition_disable = td[0] & supported_bitmask; 372 373 if (info && info->config.have_transition_disable && 374 info->config.transition_disable == 375 network->transition_disable) 376 return 0; 377 378 network->sync_settings = true; 379 380 return 0; 381 } 382 383 int network_get_signal_strength(const struct network *network) 384 { 385 struct scan_bss *best_bss = l_queue_peek_head(network->bss_list); 386 387 return best_bss->signal_strength; 388 } 389 390 struct l_settings *network_get_settings(const struct network *network) 391 { 392 return network->settings; 393 } 394 395 struct station *network_get_station(const struct network *network) 396 { 397 return network->station; 398 } 399 400 static bool network_set_8021x_secrets(struct network *network) 401 { 402 const struct l_queue_entry *entry; 403 404 if (!network->settings) 405 return false; 406 407 for (entry = l_queue_get_entries(network->secrets); entry; 408 entry = entry->next) { 409 struct eap_secret_info *secret = entry->data; 410 char *setting; 411 412 switch (secret->type) { 413 case EAP_SECRET_LOCAL_PKEY_PASSPHRASE: 414 case EAP_SECRET_REMOTE_PASSWORD: 415 if (!l_settings_set_string(network->settings, 416 "Security", secret->id, 417 secret->value)) 418 return false; 419 break; 420 421 case EAP_SECRET_REMOTE_USER_PASSWORD: 422 if (!l_settings_set_string(network->settings, 423 "Security", secret->id, 424 secret->value)) 425 return false; 426 427 if (secret->id2) 428 setting = secret->id2; 429 else { 430 setting = alloca(strlen(secret->id) + 10); 431 sprintf(setting, "%s-Password", secret->id); 432 } 433 434 if (!l_settings_set_string(network->settings, 435 "Security", setting, 436 secret->value + 1 + 437 strlen(secret->value))) 438 return false; 439 440 break; 441 } 442 } 443 444 return true; 445 } 446 447 static int network_set_handshake_secrets_psk(struct network *network, 448 struct handshake_state *hs) 449 { 450 /* SAE will generate/set the PMK */ 451 if (IE_AKM_IS_SAE(hs->akm_suite)) { 452 if (!network->passphrase) 453 return -ENOKEY; 454 455 handshake_state_set_passphrase(hs, network->passphrase); 456 457 if (network->password_identifier) 458 handshake_state_set_password_identifier(hs, 459 network->password_identifier); 460 461 if (ie_rsnxe_capable(hs->authenticator_rsnxe, 462 IE_RSNX_SAE_H2E)) { 463 l_debug("Authenticator is SAE H2E capable"); 464 handshake_state_add_ecc_sae_pt(hs, network->sae_pt_19); 465 handshake_state_add_ecc_sae_pt(hs, network->sae_pt_20); 466 } 467 } else { 468 const uint8_t *psk = network_get_psk(network); 469 470 if (!psk) 471 return -ENOKEY; 472 473 handshake_state_set_pmk(hs, psk, 32); 474 } 475 476 return 0; 477 } 478 479 int network_handshake_setup(struct network *network, struct scan_bss *bss, 480 struct handshake_state *hs) 481 { 482 struct station *station = network->station; 483 struct wiphy *wiphy = station_get_wiphy(station); 484 struct l_settings *settings = network->settings; 485 const struct l_settings *config = iwd_get_config(); 486 struct network_info *info = network->info; 487 uint32_t eapol_proto_version; 488 uint8_t new_addr[ETH_ALEN]; 489 int r; 490 const char *str; 491 492 switch (network->security) { 493 case SECURITY_PSK: 494 /* Check the BSS password ID settings match our configuration */ 495 if (bss->sae_pw_id_exclusive && !network->password_identifier) { 496 l_error("[Security].PasswordIdentifier is not set but " 497 "BSS requires SAE password identifiers"); 498 return -ENOKEY; 499 } 500 501 if (!bss->sae_pw_id_used && network->password_identifier) { 502 l_error("[Security].PasswordIdentifier set but BSS " 503 "does not not use password identifiers"); 504 return -ENOKEY; 505 } 506 507 r = network_set_handshake_secrets_psk(network, hs); 508 if (r < 0) 509 return r; 510 511 break; 512 case SECURITY_8021X: 513 handshake_state_set_8021x_config(hs, settings); 514 break; 515 case SECURITY_NONE: 516 break; 517 case SECURITY_WEP: 518 return -ENOTSUP; 519 } 520 521 handshake_state_set_ssid(hs, bss->ssid, bss->ssid_len); 522 523 if (settings && l_settings_get_uint(settings, "EAPoL", 524 "ProtocolVersion", 525 &eapol_proto_version)) { 526 if (eapol_proto_version > 3) { 527 l_warn("Invalid ProtocolVersion value - should be 0-3"); 528 eapol_proto_version = 0; 529 } 530 531 if (eapol_proto_version) 532 l_debug("Overriding EAPoL protocol version to: %u", 533 eapol_proto_version); 534 535 handshake_state_set_protocol_version(hs, eapol_proto_version); 536 } 537 538 hs->force_default_ecc_group = network->force_default_ecc_group; 539 540 /* 541 * The randomization options in the provisioning file are dependent on 542 * main.conf: [General].AddressRandomization=network. Any other value 543 * should disqualify the three network-specific settings below. 544 */ 545 str = l_settings_get_value(config, "General", "AddressRandomization"); 546 if (!(str && !strcmp(str, "network"))) 547 return 0; 548 549 /* 550 * We have three possible options here: 551 * 1. per-network MAC generation (default, no option in network config) 552 * 2. per-network full MAC randomization 553 * 3. per-network MAC override 554 */ 555 if (info && info->config.override_addr) 556 handshake_state_set_supplicant_address(hs, 557 info->config.sta_addr); 558 else if (info && info->config.always_random_addr) { 559 wiphy_generate_random_address(wiphy, new_addr); 560 handshake_state_set_supplicant_address(hs, new_addr); 561 } 562 563 return 0; 564 } 565 566 static int network_settings_load_pt_ecc(struct network *network, 567 unsigned int group, 568 struct l_ecc_point **out_pt) 569 { 570 _auto_(l_free) char *key = l_strdup_printf(SAE_PT_SETTING, group); 571 size_t pt_len; 572 _auto_(l_free) uint8_t *pt = l_settings_get_bytes(network->settings, 573 "Security", key, &pt_len); 574 const struct l_ecc_curve *curve = l_ecc_curve_from_ike_group((group)); 575 576 if (!curve) 577 return -EINVAL; 578 579 if (!pt) 580 goto generate; 581 582 if (pt_len != l_ecc_curve_get_scalar_bytes(curve) * 2) 583 goto bad_format; 584 585 *out_pt = l_ecc_point_from_data(curve, L_ECC_POINT_TYPE_FULL, 586 pt, pt_len); 587 if (*out_pt) 588 return 0; 589 590 bad_format: 591 l_error("%s profile: invalid %s format", network->ssid, key); 592 593 generate: 594 if (!network->passphrase) 595 return -ENOKEY; 596 597 *out_pt = network_generate_sae_pt(network, group); 598 if (*out_pt) 599 return 1; 600 601 return -EIO; 602 } 603 604 static inline bool __bss_is_sae(const struct scan_bss *bss, 605 const struct ie_rsn_info *rsn) 606 { 607 if (rsn->akm_suites & IE_RSN_AKM_SUITE_SAE_SHA256) 608 return true; 609 610 return false; 611 } 612 613 static bool bss_is_sae(const struct scan_bss *bss) 614 { 615 struct ie_rsn_info rsn; 616 617 memset(&rsn, 0, sizeof(rsn)); 618 scan_bss_get_rsn_info(bss, &rsn); 619 620 return __bss_is_sae(bss, &rsn); 621 } 622 623 static int network_load_psk(struct network *network, struct scan_bss *bss) 624 { 625 /* 626 * A legacy psk file may only contain the PreSharedKey entry. For SAE 627 * networks the raw Passphrase is required. So in this case where 628 * the psk is found but no Passphrase, we ask the agent. The psk file 629 * will then be re-written to contain the raw passphrase. 630 */ 631 bool is_sae = bss_is_sae(bss); 632 const char *ssid = network_get_ssid(network); 633 enum security security = network_get_security(network); 634 size_t psk_len; 635 _auto_(l_free) uint8_t *psk = 636 l_settings_get_bytes(network->settings, "Security", 637 "PreSharedKey", &psk_len); 638 _auto_(l_free) char *passphrase = 639 l_settings_get_string(network->settings, 640 "Security", "Passphrase"); 641 _auto_(l_free) char *password_id = 642 l_settings_get_string(network->settings, "Security", 643 "PasswordIdentifier"); 644 _auto_(l_free) char *path = 645 storage_get_network_file_path(security, ssid); 646 647 if (psk && psk_len != 32) { 648 l_error("%s: invalid PreSharedKey format", path); 649 l_free(psk); 650 psk = NULL; 651 psk_len = 0; 652 } 653 654 /* PSK can be generated from the passphrase but not the other way */ 655 if (!psk || is_sae) { 656 if (!passphrase) 657 return -ENOKEY; 658 659 if (!crypto_passphrase_is_valid(passphrase)) { 660 l_error("%s: invalid Passphrase format", path); 661 return -ENOKEY; 662 } 663 } 664 665 network_reset_passphrase(network); 666 network_reset_psk(network); 667 network->passphrase = l_steal_ptr(passphrase); 668 network->password_identifier = l_steal_ptr(password_id); 669 670 if (network_settings_load_pt_ecc(network, 19, &network->sae_pt_19) > 0) 671 network->sync_settings = true; 672 673 if (network_settings_load_pt_ecc(network, 20, &network->sae_pt_20) > 0) 674 network->sync_settings = true; 675 676 network->psk = l_steal_ptr(psk); 677 678 return 0; 679 } 680 681 static void network_settings_save_sae_pt_ecc(struct l_settings *settings, 682 struct l_ecc_point *pt) 683 { 684 const struct l_ecc_curve *curve = l_ecc_point_get_curve(pt); 685 unsigned int group = l_ecc_curve_get_ike_group(curve); 686 _auto_(l_free) char *key = l_strdup_printf(SAE_PT_SETTING, group); 687 uint8_t buf[256]; 688 ssize_t len; 689 690 len = l_ecc_point_get_data(pt, buf, sizeof(buf)); 691 if (len < 0) { 692 l_warn("Unable to serialize '%s'", key); 693 return; 694 } 695 696 l_settings_set_bytes(settings, "Security", key, buf, len); 697 } 698 699 static void network_settings_save(struct network *network, 700 struct l_settings *settings) 701 { 702 if (network->have_transition_disable) { 703 char *modes[4]; 704 unsigned int i = 0; 705 706 l_settings_set_bool(settings, NET_TRANSITION_DISABLE, true); 707 708 if (test_bit(&network->transition_disable, 0)) 709 modes[i++] = "personal"; 710 711 if (test_bit(&network->transition_disable, 2)) 712 modes[i++] = "enterprise"; 713 714 if (test_bit(&network->transition_disable, 3)) 715 modes[i++] = "open"; 716 717 modes[i] = NULL; 718 719 l_settings_set_string_list(settings, 720 NET_TRANSITION_DISABLE_MODES, 721 modes, ' '); 722 } 723 724 if (network->security != SECURITY_PSK) 725 return; 726 727 /* We only update the [Security] bits here, wipe the group first */ 728 l_settings_remove_group(settings, "Security"); 729 730 if (network->psk) 731 l_settings_set_bytes(settings, "Security", "PreSharedKey", 732 network->psk, 32); 733 734 if (network->passphrase) 735 l_settings_set_string(settings, "Security", "Passphrase", 736 network->passphrase); 737 738 if (network->password_identifier) 739 l_settings_set_string(settings, "Security", 740 "PasswordIdentifier", 741 network->password_identifier); 742 743 if (network->sae_pt_19) 744 network_settings_save_sae_pt_ecc(settings, network->sae_pt_19); 745 746 if (network->sae_pt_20) 747 network_settings_save_sae_pt_ecc(settings, network->sae_pt_20); 748 } 749 750 void network_sync_settings(struct network *network) 751 { 752 struct network_info *info = network->info; 753 754 if (!network->sync_settings) 755 return; 756 757 l_debug(""); 758 759 network->sync_settings = false; 760 761 /* 762 * Re-open the settings from Disk, in case they were updated 763 * since we last opened them. 764 */ 765 if (network->info) { 766 struct l_settings *fs_settings = info->ops->open(info); 767 768 if (L_WARN_ON(!fs_settings)) 769 return; 770 771 network_settings_save(network, fs_settings); 772 info->ops->sync(info, fs_settings); 773 l_settings_free(fs_settings); 774 return; 775 } 776 777 network_settings_save(network, network->settings); 778 storage_network_sync(network->security, network->ssid, 779 network->settings); 780 } 781 782 const struct network_info *network_get_info(const struct network *network) 783 { 784 return network->info; 785 } 786 787 void network_set_info(struct network *network, struct network_info *info) 788 { 789 if (info) { 790 network->info = info; 791 network->info->seen_count++; 792 793 network_update_known_frequencies(network); 794 } else { 795 network->info->seen_count--; 796 network->info = NULL; 797 } 798 799 #ifdef HAVE_DBUS 800 l_dbus_property_changed(dbus_get_bus(), network_get_path(network), 801 IWD_NETWORK_INTERFACE, "KnownNetwork"); 802 #endif 803 } 804 805 void network_set_force_default_ecc_group(struct network *network) 806 { 807 /* No network info, likely a failed OWE connection */ 808 if (!network->info) { 809 network->force_default_ecc_group = true; 810 return; 811 } 812 813 /* Profile explicitly wants to try the most secure group */ 814 if (network->info->config.ecc_group == 815 KNOWN_NETWORK_ECC_GROUP_MOST_SECURE) 816 return; 817 818 l_debug("Forcing default group for %s", network->ssid); 819 820 network->force_default_ecc_group = true; 821 network->info->config.ecc_group = KNOWN_NETWORK_ECC_GROUP_DEFAULT; 822 } 823 824 bool network_get_force_default_ecc_group(struct network *network) 825 { 826 if (!network->info) 827 return network->force_default_ecc_group; 828 829 if (network->info->config.ecc_group == KNOWN_NETWORK_ECC_GROUP_DEFAULT) 830 return true; 831 832 return false; 833 } 834 835 int network_can_connect_bss(struct network *network, const struct scan_bss *bss) 836 { 837 struct station *station = network->station; 838 struct wiphy *wiphy = station_get_wiphy(station); 839 enum security security = network_get_security(network); 840 struct network_info *info = network->info; 841 struct network_config *config = info ? &info->config : NULL; 842 bool can_transition_disable = wiphy_can_transition_disable(wiphy); 843 struct ie_rsn_info rsn; 844 enum band_freq band; 845 int ret; 846 847 switch (security) { 848 case SECURITY_NONE: 849 case SECURITY_PSK: 850 case SECURITY_8021X: 851 break; 852 default: 853 return -ENOSYS; 854 } 855 856 if (!band_freq_to_channel(bss->frequency, &band)) 857 return -ENOTSUP; 858 859 memset(&rsn, 0, sizeof(rsn)); 860 ret = scan_bss_get_rsn_info(bss, &rsn); 861 if (ret < 0) { 862 /* 863 * WPA3 Specification Version 3, Section 8 864 * Transition Disable implies PMF, no TKIP, yet 865 * Bit 3 is specified as 'Open system authentication without 866 * encryption'. 867 * 868 * We assume the spec means us to check bit 3 here 869 */ 870 if (ret == -ENOENT && security == SECURITY_NONE) { 871 /* 872 * 802.11ax 12.12.2 - STA shall not use Open System 873 * authentication without encryption 874 */ 875 if (band == BAND_FREQ_6_GHZ) 876 return -EPERM; 877 878 if (!config) 879 return 0; 880 881 if (!config->have_transition_disable || 882 !test_bit(&config->transition_disable, 883 3)) 884 return 0; 885 886 if (!can_transition_disable) { 887 l_debug("HW not capable of Transition Disable"); 888 return 0; 889 } 890 } 891 892 return ret; 893 } 894 895 if (IE_AKM_IS_OWE(rsn.akm_suites) && wiphy_owe_disabled(wiphy)) 896 return -EPERM; 897 898 if (!config || !config->have_transition_disable) { 899 if (band == BAND_FREQ_6_GHZ) 900 goto mfp_no_tkip; 901 902 goto no_transition_disable; 903 } 904 905 if (!can_transition_disable) { 906 if (band == BAND_FREQ_6_GHZ) 907 return -EPERM; 908 909 l_debug("HW not capable of Transition Disable, skip"); 910 goto no_transition_disable; 911 } 912 913 /* WPA3-Personal */ 914 if (test_bit(&config->transition_disable, 0)) { 915 rsn.akm_suites &= ~IE_RSN_AKM_SUITE_PSK; 916 rsn.akm_suites &= ~IE_RSN_AKM_SUITE_PSK_SHA256; 917 rsn.akm_suites &= ~IE_RSN_AKM_SUITE_FT_USING_PSK; 918 } 919 920 /* WPA3-Enterprise */ 921 if (test_bit(&config->transition_disable, 2)) 922 rsn.akm_suites &= ~IE_RSN_AKM_SUITE_8021X; 923 924 /* Enhanced Open */ 925 if (test_bit(&config->transition_disable, 3)) { 926 if (!(rsn.akm_suites & IE_RSN_AKM_SUITE_OWE)) 927 return -EPERM; 928 } 929 930 mfp_no_tkip: 931 /* 932 * WPA3 Specification, v3, Section 8: 933 * - Disable use of WEP and TKIP 934 * - Disallow association without negotiation of PMF 935 */ 936 rsn.pairwise_ciphers &= ~IE_RSN_CIPHER_SUITE_TKIP; 937 938 if (!rsn.group_management_cipher) 939 return -EPERM; 940 941 rsn.mfpr = true; 942 943 /* 802.11ax Section 12.12.2 */ 944 if (band == BAND_FREQ_6_GHZ) { 945 /* STA shall not use the following cipher suite selectors */ 946 rsn.pairwise_ciphers &= ~IE_RSN_CIPHER_SUITE_USE_GROUP_CIPHER; 947 948 /* Basically the STA must use OWE, SAE, or 8021x */ 949 if (!IE_AKM_IS_SAE(rsn.akm_suites) && 950 !IE_AKM_IS_8021X(rsn.akm_suites) && 951 (!(rsn.akm_suites & IE_RSN_AKM_SUITE_OWE))) 952 return -EPERM; 953 } 954 955 no_transition_disable: 956 if (!wiphy_select_cipher(wiphy, rsn.pairwise_ciphers)) 957 return -ENOTSUP; 958 959 if (!wiphy_select_cipher(wiphy, rsn.group_cipher)) 960 return -ENOTSUP; 961 962 if (rsn.mfpr && !wiphy_select_cipher(wiphy, 963 rsn.group_management_cipher)) 964 return -EPERM; 965 966 if (!wiphy_select_akm(wiphy, bss, security, &rsn, false)) 967 return -ENOTSUP; 968 969 return 0; 970 } 971 972 int network_autoconnect(struct network *network, struct scan_bss *bss) 973 { 974 struct station *station = network->station; 975 enum security security = network_get_security(network); 976 struct network_info *info = network->info; 977 struct network_config *config; 978 int ret; 979 980 /* already waiting for an agent request, connect in progress */ 981 if (network->agent_request) 982 return -EALREADY; 983 984 if (network->ask_passphrase) 985 return -ENOKEY; 986 987 if (!info) 988 return -ENOENT; 989 990 config = &info->config; 991 992 if (!config->is_autoconnectable) 993 return -EPERM; 994 995 if (!network_settings_load(network)) 996 return -ENOKEY; 997 998 switch (security) { 999 case SECURITY_PSK: 1000 ret = network_load_psk(network, bss); 1001 if (ret < 0) 1002 goto close_settings; 1003 1004 break; 1005 case SECURITY_8021X: 1006 { 1007 struct l_queue *missing_secrets = NULL; 1008 1009 ret = eap_check_settings(network->settings, network->secrets, 1010 "EAP-", true, &missing_secrets); 1011 if (ret < 0) 1012 goto close_settings; 1013 1014 ret = -ENOKEY; 1015 if (!l_queue_isempty(missing_secrets)) { 1016 l_queue_destroy(missing_secrets, eap_secret_info_free); 1017 goto close_settings; 1018 } 1019 1020 if (!network_set_8021x_secrets(network)) 1021 goto close_settings; 1022 1023 break; 1024 } 1025 1026 case SECURITY_NONE: 1027 break; 1028 default: 1029 return -ENOTSUP; 1030 } 1031 1032 return __station_connect_network(station, network, bss, 1033 STATION_STATE_CONNECTING_AUTO); 1034 1035 close_settings: 1036 network_settings_close(network); 1037 return ret; 1038 } 1039 1040 void network_connect_failed(struct network *network, bool in_handshake) 1041 { 1042 /* 1043 * Connection failed during the handshake phase. If PSK try asking 1044 * for the passphrase once more 1045 */ 1046 if (network_get_security(network) == SECURITY_PSK && in_handshake) { 1047 network->sync_settings = false; 1048 network->ask_passphrase = true; 1049 } 1050 1051 l_queue_destroy(network->secrets, eap_secret_info_free); 1052 network->secrets = NULL; 1053 } 1054 1055 static bool hotspot_info_matches(struct network *network, 1056 const struct network_info *info) 1057 { 1058 struct scan_bss *bss; 1059 1060 if (!network->is_hs20 || !info->is_hotspot) 1061 return false; 1062 1063 bss = network_bss_select(network, true); 1064 1065 if (network_info_match_hessid(info, bss->hessid)) 1066 return true; 1067 1068 if (network_info_match_roaming_consortium(info, bss->rc_ie, 1069 bss->rc_ie[1] + 2, 1070 NULL)) 1071 return true; 1072 1073 return false; 1074 } 1075 1076 static bool match_hotspot_network(const struct network_info *info, 1077 void *user_data) 1078 { 1079 struct network *network = user_data; 1080 1081 if (!hotspot_info_matches(network, info)) 1082 return false; 1083 1084 network_set_info(network, (struct network_info *) info); 1085 1086 return true; 1087 } 1088 1089 bool network_update_known_frequencies(struct network *network) 1090 { 1091 const struct l_queue_entry *e; 1092 struct l_queue *reversed; 1093 1094 if (!network->info) 1095 return false; 1096 1097 reversed = l_queue_new(); 1098 1099 for (e = l_queue_get_entries(network->bss_list); e; e = e->next) { 1100 struct scan_bss *bss = e->data; 1101 1102 l_queue_push_head(reversed, bss); 1103 } 1104 1105 for (e = l_queue_get_entries(reversed); e; e = e->next) { 1106 struct scan_bss *bss = e->data; 1107 1108 known_network_add_frequency(network->info, bss->frequency); 1109 } 1110 1111 l_queue_destroy(reversed, NULL); 1112 1113 return true; 1114 } 1115 1116 const char *__network_path_append_bss(const char *network_path, 1117 const struct scan_bss *bss) 1118 { 1119 static char path[256]; 1120 1121 snprintf(path, sizeof(path), "%s/%02x%02x%02x%02x%02x%02x", 1122 network_path, MAC_STR(bss->addr)); 1123 1124 return path; 1125 } 1126 1127 const char *network_bss_get_path(const struct network *network, 1128 const struct scan_bss *bss) 1129 { 1130 return __network_path_append_bss(network->object_path, bss); 1131 } 1132 1133 void network_bss_list_clear(struct network *network) 1134 { 1135 l_queue_destroy(network->bss_list, NULL); 1136 network->bss_list = l_queue_new(); 1137 } 1138 1139 bool network_bss_add(struct network *network, struct scan_bss *bss) 1140 { 1141 if (!l_queue_insert(network->bss_list, bss, scan_bss_rank_compare, 1142 NULL)) 1143 return false; 1144 1145 #ifdef HAVE_DBUS 1146 l_dbus_property_changed(dbus_get_bus(), network->object_path, 1147 IWD_NETWORK_INTERFACE, "ExtendedServiceSet"); 1148 #endif 1149 1150 /* Done if BSS is not HS20 or we already have network_info set */ 1151 if (!bss->hs20_capable) 1152 return true; 1153 1154 network->is_hs20 = true; 1155 1156 if (network->info) 1157 return true; 1158 1159 /* Set the network_info to a matching hotspot entry, if found */ 1160 known_networks_foreach(match_hotspot_network, network); 1161 1162 return true; 1163 } 1164 1165 static bool match_addr(const void *a, const void *b) 1166 { 1167 const struct scan_bss *bss = a; 1168 1169 return memcmp(bss->addr, b, 6) == 0; 1170 } 1171 1172 /* 1173 * Replaces an old scan_bss (if exists) in the bss list with a new bss object. 1174 * Note this BSS is *not* freed and must be by the caller. scan_bss objects are 1175 * shared between network/station but station technically owns them. 1176 */ 1177 bool network_bss_update(struct network *network, struct scan_bss *bss) 1178 { 1179 l_queue_remove_if(network->bss_list, match_addr, bss->addr); 1180 1181 l_queue_insert(network->bss_list, bss, scan_bss_rank_compare, NULL); 1182 1183 /* Sync frequency for already known networks */ 1184 if (network->info) { 1185 known_network_add_frequency(network->info, bss->frequency); 1186 known_network_frequency_sync(network->info); 1187 } 1188 1189 return true; 1190 } 1191 1192 bool network_bss_list_isempty(struct network *network) 1193 { 1194 return l_queue_isempty(network->bss_list); 1195 } 1196 1197 struct scan_bss *network_bss_list_pop(struct network *network) 1198 { 1199 return l_queue_pop_head(network->bss_list); 1200 } 1201 1202 struct scan_bss *network_bss_find_by_addr(struct network *network, 1203 const uint8_t *addr) 1204 { 1205 return l_queue_find(network->bss_list, match_addr, addr); 1206 } 1207 1208 static bool match_bss(const void *a, const void *b) 1209 { 1210 return a == b; 1211 } 1212 1213 struct erp_cache_entry *network_get_erp_cache(struct network *network) 1214 { 1215 struct erp_cache_entry *cache; 1216 struct l_settings *settings; 1217 char *check_id; 1218 const char *identity; 1219 bool ret; 1220 1221 settings = network_get_settings(network); 1222 if (!settings) 1223 return NULL; 1224 1225 check_id = l_settings_get_string(settings, "Security", "EAP-Identity"); 1226 if (!check_id) 1227 return NULL; 1228 1229 cache = erp_cache_get(network_get_ssid(network)); 1230 if (!cache) { 1231 l_free(check_id); 1232 return NULL; 1233 } 1234 1235 identity = erp_cache_entry_get_identity(cache); 1236 1237 ret = strcmp(check_id, identity) == 0; 1238 1239 l_free(check_id); 1240 1241 /* 1242 * The settings file must have change out from under us. In this 1243 * case we want to remove the ERP entry because it is no longer 1244 * valid. 1245 */ 1246 if (!ret) { 1247 erp_cache_put(cache); 1248 erp_cache_remove(identity); 1249 return NULL; 1250 } 1251 1252 return cache; 1253 } 1254 1255 const struct l_queue_entry *network_bss_list_get_entries( 1256 const struct network *network) 1257 { 1258 return l_queue_get_entries(network->bss_list); 1259 } 1260 1261 struct scan_bss *network_bss_select(struct network *network, 1262 bool fallback_to_blacklist) 1263 { 1264 struct l_queue *bss_list = network->bss_list; 1265 const struct l_queue_entry *bss_entry; 1266 struct scan_bss *candidate = NULL; 1267 bool skipped_open = false; 1268 1269 for (bss_entry = l_queue_get_entries(bss_list); bss_entry; 1270 bss_entry = bss_entry->next) { 1271 struct scan_bss *bss = bss_entry->data; 1272 int ret = network_can_connect_bss(network, bss); 1273 1274 if (ret == -ENOSYS) 1275 return NULL; 1276 else if (ret < 0) 1277 continue; 1278 1279 /* 1280 * We only want to record the first (best) candidate. In case 1281 * all our BSS's are blacklisted but we still want to connect 1282 * we want to hold only this first candidate 1283 */ 1284 if (!candidate) 1285 candidate = bss; 1286 1287 /* check if temporarily blacklisted */ 1288 if (l_queue_find(network->blacklist, match_bss, bss)) 1289 continue; 1290 1291 if (blacklist_contains_bss(bss->addr, 1292 BLACKLIST_REASON_CONNECT_FAILED)) 1293 continue; 1294 1295 /* OWE Transition BSS */ 1296 if (bss->owe_trans) { 1297 /* Don't want to connect to the Open BSS if possible */ 1298 if (!bss->rsne) { 1299 skipped_open = true; 1300 continue; 1301 } 1302 1303 /* Candidate is not OWE, set this as new candidate */ 1304 if (!(candidate->owe_trans && candidate->rsne)) 1305 candidate = bss; 1306 } 1307 1308 return bss; 1309 } 1310 1311 /* 1312 * No BSS was found, but if we are falling back to blacklisted BSS's we 1313 * can just use the first connectable candidate found above. 1314 */ 1315 if (fallback_to_blacklist || skipped_open) 1316 return candidate; 1317 1318 return NULL; 1319 } 1320 1321 static void passphrase_callback(enum agent_result result, 1322 const char *passphrase, 1323 struct l_dbus_message *message, 1324 void *user_data) 1325 { 1326 struct network *network = user_data; 1327 struct station *station = network->station; 1328 struct scan_bss *bss; 1329 1330 l_debug("result %d", result); 1331 1332 network->agent_request = 0; 1333 1334 /* 1335 * agent will release its reference to message after invoking this 1336 * callback. So if we want this message, we need to take a reference 1337 * to it 1338 */ 1339 l_dbus_message_ref(message); 1340 1341 if (result != AGENT_RESULT_OK) { 1342 dbus_pending_reply(&message, dbus_error_aborted(message)); 1343 goto err; 1344 } 1345 1346 bss = network_bss_select(network, true); 1347 1348 /* Did all good BSSes go away while we waited */ 1349 if (!bss) { 1350 dbus_pending_reply(&message, dbus_error_failed(message)); 1351 goto err; 1352 } 1353 1354 network_reset_psk(network); 1355 1356 if (!__network_set_passphrase(network, passphrase)) { 1357 dbus_pending_reply(&message, 1358 dbus_error_invalid_format(message)); 1359 goto err; 1360 } 1361 1362 station_connect_network(station, network, bss, message); 1363 l_dbus_message_unref(message); 1364 return; 1365 1366 err: 1367 network_settings_close(network); 1368 1369 if (network->provisioning_hidden) 1370 station_hide_network(station, network); 1371 } 1372 1373 static struct l_dbus_message *network_connect_psk(struct network *network, 1374 struct scan_bss *bss, 1375 struct l_dbus_message *message) 1376 { 1377 struct station *station = network->station; 1378 1379 if (!network_settings_load(network)) { 1380 network->settings = l_settings_new(); 1381 network->ask_passphrase = true; 1382 } else if (!network->ask_passphrase) 1383 network->ask_passphrase = 1384 network_load_psk(network, bss) < 0; 1385 1386 l_debug("ask_passphrase: %s", 1387 network->ask_passphrase ? "true" : "false"); 1388 1389 if (network->ask_passphrase) { 1390 network->ask_passphrase = false; 1391 1392 network->agent_request = 1393 agent_request_passphrase(network->object_path, 1394 passphrase_callback, 1395 message, network, NULL); 1396 1397 if (!network->agent_request) 1398 return dbus_error_no_agent(message); 1399 } else 1400 station_connect_network(station, network, bss, message); 1401 1402 return NULL; 1403 } 1404 1405 struct eap_secret_request { 1406 struct network *network; 1407 struct eap_secret_info *secret; 1408 struct l_queue *pending_secrets; 1409 void (*callback)(enum agent_result result, 1410 struct l_dbus_message *message, 1411 struct eap_secret_request *req); 1412 }; 1413 1414 static void eap_secret_request_free(void *data) 1415 { 1416 struct eap_secret_request *req = data; 1417 1418 eap_secret_info_free(req->secret); 1419 l_queue_destroy(req->pending_secrets, eap_secret_info_free); 1420 l_free(req); 1421 } 1422 1423 static bool eap_secret_info_match_local(const void *a, const void *b) 1424 { 1425 const struct eap_secret_info *info = a; 1426 1427 return info->type == EAP_SECRET_LOCAL_PKEY_PASSPHRASE; 1428 } 1429 1430 static void eap_password_callback(enum agent_result result, const char *value, 1431 struct l_dbus_message *message, 1432 void *user_data) 1433 { 1434 struct eap_secret_request *req = user_data; 1435 1436 req->network->agent_request = 0; 1437 1438 if (value) { 1439 if (strlen(value) < IWD_MAX_PASSWORD_LEN) 1440 req->secret->value = l_strdup(value); 1441 else { 1442 l_error("EAP password too long"); 1443 result = AGENT_RESULT_FAILED; 1444 } 1445 } 1446 1447 req->callback(result, message, req); 1448 } 1449 1450 static void eap_user_password_callback(enum agent_result result, 1451 const char *user, const char *passwd, 1452 struct l_dbus_message *message, 1453 void *user_data) 1454 { 1455 struct eap_secret_request *req = user_data; 1456 1457 req->network->agent_request = 0; 1458 1459 if (user && passwd) { 1460 size_t len1 = strlen(user) + 1; 1461 size_t len2 = strlen(passwd) + 1; 1462 1463 if (len2 > IWD_MAX_PASSWORD_LEN) { 1464 l_error("EAP password too long"); 1465 result = AGENT_RESULT_FAILED; 1466 goto done; 1467 } 1468 1469 req->secret->value = l_malloc(len1 + len2); 1470 memcpy(req->secret->value, user, len1); 1471 memcpy(req->secret->value + len1, passwd, len2); 1472 } 1473 1474 done: 1475 req->callback(result, message, req); 1476 } 1477 1478 static bool eap_send_agent_req(struct network *network, 1479 struct l_queue *pending_secrets, 1480 struct l_dbus_message *message, 1481 void *callback) 1482 { 1483 struct eap_secret_request *req; 1484 struct eap_secret_info *info; 1485 1486 /* 1487 * Request the locally-verifiable data first, i.e. 1488 * the private key encryption passphrases so that we don't bother 1489 * asking for any other data if these passphrases turn out to 1490 * be wrong. 1491 */ 1492 info = l_queue_remove_if(pending_secrets, eap_secret_info_match_local, 1493 NULL); 1494 1495 if (!info) 1496 info = l_queue_pop_head(pending_secrets); 1497 1498 req = l_new(struct eap_secret_request, 1); 1499 req->network = network; 1500 req->secret = info; 1501 req->pending_secrets = pending_secrets; 1502 req->callback = callback; 1503 1504 switch (info->type) { 1505 case EAP_SECRET_LOCAL_PKEY_PASSPHRASE: 1506 network->agent_request = agent_request_pkey_passphrase( 1507 network->object_path, 1508 eap_password_callback, 1509 message, req, 1510 eap_secret_request_free); 1511 break; 1512 case EAP_SECRET_REMOTE_PASSWORD: 1513 network->agent_request = agent_request_user_password( 1514 network->object_path, 1515 info->parameter, 1516 eap_password_callback, 1517 message, req, 1518 eap_secret_request_free); 1519 break; 1520 case EAP_SECRET_REMOTE_USER_PASSWORD: 1521 network->agent_request = agent_request_user_name_password( 1522 network->object_path, 1523 eap_user_password_callback, 1524 message, req, 1525 eap_secret_request_free); 1526 break; 1527 } 1528 1529 if (network->agent_request) 1530 return true; 1531 1532 eap_secret_request_free(req); 1533 return false; 1534 } 1535 1536 static struct l_dbus_message *network_connect_8021x(struct network *network, 1537 struct scan_bss *bss, 1538 struct l_dbus_message *message); 1539 1540 static void eap_secret_done(enum agent_result result, 1541 struct l_dbus_message *message, 1542 struct eap_secret_request *req) 1543 { 1544 struct network *network = req->network; 1545 struct eap_secret_info *secret = req->secret; 1546 struct l_queue *pending = req->pending_secrets; 1547 struct scan_bss *bss; 1548 1549 l_debug("result %d", result); 1550 1551 /* 1552 * Agent will release its reference to message after invoking this 1553 * callback. So if we want this message, we need to take a reference 1554 * to it. 1555 */ 1556 l_dbus_message_ref(message); 1557 1558 if (result != AGENT_RESULT_OK) { 1559 dbus_pending_reply(&message, dbus_error_aborted(message)); 1560 goto err; 1561 } 1562 1563 bss = network_bss_select(network, true); 1564 1565 /* Did all good BSSes go away while we waited */ 1566 if (!bss) { 1567 dbus_pending_reply(&message, dbus_error_failed(message)); 1568 goto err; 1569 } 1570 1571 if (!network->secrets) 1572 network->secrets = l_queue_new(); 1573 1574 l_queue_push_tail(network->secrets, secret); 1575 1576 req->secret = NULL; 1577 1578 /* 1579 * If we have any other missing secrets in the queue, send the 1580 * next request immediately unless we've just received a passphrase 1581 * for a local private key. In that case we will first call 1582 * network_connect_8021x to have it validate the new passphrase. 1583 */ 1584 if (secret->type == EAP_SECRET_LOCAL_PKEY_PASSPHRASE || 1585 l_queue_isempty(req->pending_secrets)) { 1586 struct l_dbus_message *reply; 1587 1588 reply = network_connect_8021x(network, bss, message); 1589 if (reply) 1590 dbus_pending_reply(&message, reply); 1591 else 1592 l_dbus_message_unref(message); 1593 1594 return; 1595 } 1596 1597 req->pending_secrets = NULL; 1598 1599 if (eap_send_agent_req(network, pending, message, 1600 eap_secret_done)) { 1601 l_dbus_message_unref(message); 1602 return; 1603 } 1604 1605 dbus_pending_reply(&message, dbus_error_no_agent(message)); 1606 err: 1607 network_settings_close(network); 1608 } 1609 1610 static struct l_dbus_message *network_connect_8021x(struct network *network, 1611 struct scan_bss *bss, 1612 struct l_dbus_message *message) 1613 { 1614 struct station *station = network->station; 1615 int r; 1616 struct l_queue *missing_secrets = NULL; 1617 struct l_dbus_message *reply; 1618 1619 l_debug(""); 1620 1621 r = eap_check_settings(network->settings, network->secrets, "EAP-", 1622 true, &missing_secrets); 1623 if (r) { 1624 #ifdef HAVE_DBUS 1625 if (r == -EUNATCH) 1626 reply = dbus_error_not_available(message); 1627 else if (r == -ENOTSUP) 1628 reply = dbus_error_not_supported(message); 1629 else if (r == -EACCES) 1630 reply = dbus_error_failed(message); 1631 else 1632 reply = dbus_error_not_configured(message); 1633 #endif 1634 1635 goto error; 1636 } 1637 1638 l_debug("supplied %u secrets, %u more needed for EAP", 1639 l_queue_length(network->secrets), 1640 l_queue_length(missing_secrets)); 1641 1642 if (l_queue_isempty(missing_secrets)) { 1643 if (!network_set_8021x_secrets(network)) { 1644 #ifdef HAVE_DBUS 1645 reply = dbus_error_failed(message); 1646 #endif 1647 1648 goto error; 1649 } 1650 1651 station_connect_network(station, network, bss, message); 1652 1653 return NULL; 1654 } 1655 1656 if (eap_send_agent_req(network, missing_secrets, message, 1657 eap_secret_done)) 1658 return NULL; 1659 1660 #ifdef HAVE_DBUS 1661 reply = dbus_error_no_agent(message); 1662 #endif 1663 1664 error: 1665 network_settings_close(network); 1666 1667 l_queue_destroy(network->secrets, eap_secret_info_free); 1668 network->secrets = NULL; 1669 1670 return reply; 1671 } 1672 1673 struct l_dbus_message *__network_connect(struct network *network, 1674 struct scan_bss *bss, 1675 struct l_dbus_message *message) 1676 { 1677 struct station *station = network->station; 1678 1679 switch (network_get_security(network)) { 1680 case SECURITY_PSK: 1681 return network_connect_psk(network, bss, message); 1682 case SECURITY_NONE: 1683 if (network->connect_after_owe_hidden) 1684 return dbus_error_busy(message); 1685 1686 /* Save message and connect after OWE hidden scan is done */ 1687 if (network->owe_hidden_pending) { 1688 network->connect_after_owe_hidden = 1689 l_dbus_message_ref(message); 1690 l_debug("Pending OWE hidden scan, delaying connect"); 1691 return NULL; 1692 } 1693 1694 station_connect_network(station, network, bss, message); 1695 return NULL; 1696 case SECURITY_8021X: 1697 if (network->connect_after_anqp) 1698 return dbus_error_busy(message); 1699 1700 /* 1701 * If there is an ongoing ANQP request we must wait for that to 1702 * finish. Save the message and wait for the ANQP watch to 1703 * fire 1704 */ 1705 if (network->anqp_pending) { 1706 network->connect_after_anqp = 1707 l_dbus_message_ref(message); 1708 l_debug("Pending ANQP request, delaying connect to %s", 1709 network->ssid); 1710 return NULL; 1711 } 1712 1713 if (!network_settings_load(network)) 1714 return dbus_error_not_configured(message); 1715 1716 return network_connect_8021x(network, bss, message); 1717 default: 1718 return dbus_error_not_supported(message); 1719 } 1720 } 1721 1722 static struct l_dbus_message *network_connect(struct l_dbus *dbus, 1723 struct l_dbus_message *message, 1724 void *user_data) 1725 { 1726 struct network *network = user_data; 1727 struct station *station = network->station; 1728 struct scan_bss *bss; 1729 1730 l_debug(""); 1731 1732 if (network == station_get_connected_network(station)) 1733 /* 1734 * The requested network is already connected, return success. 1735 */ 1736 return l_dbus_message_new_method_return(message); 1737 1738 if (network->agent_request) 1739 return dbus_error_busy(message); 1740 1741 /* 1742 * Select the best BSS to use at this time. If we have to query the 1743 * agent this may not be the final choice because BSS visibility can 1744 * change while we wait for the agent. 1745 */ 1746 bss = network_bss_select(network, true); 1747 1748 /* None of the BSSes is compatible with our stack */ 1749 if (!bss) 1750 return dbus_error_not_supported(message); 1751 1752 return __network_connect(network, bss, message); 1753 } 1754 1755 /* 1756 * Returns an error message in case an error occurs. Otherwise this function 1757 * returns NULL and takes a reference to message. Callers should unref 1758 * their copy in this case 1759 */ 1760 struct l_dbus_message *network_connect_new_hidden_network( 1761 struct network *network, 1762 struct l_dbus_message *message) 1763 { 1764 struct station *station = network->station; 1765 struct scan_bss *bss; 1766 1767 l_debug(""); 1768 1769 if (network->agent_request) 1770 return dbus_error_busy(message); 1771 1772 /* 1773 * This is not a Known Network. If connection succeeds, either 1774 * network_sync_settings or network_connected will save this network 1775 * as hidden and trigger an update to the hidden networks count. 1776 */ 1777 1778 bss = network_bss_select(network, true); 1779 /* This should never happened for the hidden networks. */ 1780 if (!bss) 1781 return dbus_error_not_supported(message); 1782 1783 network->settings = l_settings_new(); 1784 l_settings_set_bool(network->settings, NET_HIDDEN, true); 1785 1786 switch (network_get_security(network)) { 1787 case SECURITY_PSK: 1788 network->provisioning_hidden = true; 1789 return network_connect_psk(network, bss, message); 1790 case SECURITY_NONE: 1791 network->provisioning_hidden = true; 1792 station_connect_network(station, network, bss, message); 1793 return NULL; 1794 default: 1795 break; 1796 } 1797 1798 return dbus_error_not_supported(message); 1799 } 1800 1801 void network_blacklist_add(struct network *network, struct scan_bss *bss) 1802 { 1803 l_queue_push_head(network->blacklist, bss); 1804 } 1805 1806 static bool network_property_get_name(struct l_dbus *dbus, 1807 struct l_dbus_message *message, 1808 struct l_dbus_message_builder *builder, 1809 void *user_data) 1810 { 1811 struct network *network = user_data; 1812 1813 l_dbus_message_builder_append_basic(builder, 's', 1814 network_get_ssid(network)); 1815 return true; 1816 } 1817 1818 static bool network_property_is_connected(struct l_dbus *dbus, 1819 struct l_dbus_message *message, 1820 struct l_dbus_message_builder *builder, 1821 void *user_data) 1822 { 1823 struct network *network = user_data; 1824 struct station *station = network->station; 1825 bool connected; 1826 1827 connected = station_get_connected_network(station) == network; 1828 l_dbus_message_builder_append_basic(builder, 'b', &connected); 1829 return true; 1830 } 1831 1832 static bool network_property_get_device(struct l_dbus *dbus, 1833 struct l_dbus_message *message, 1834 struct l_dbus_message_builder *builder, 1835 void *user_data) 1836 { 1837 struct network *network = user_data; 1838 struct station *station = network->station; 1839 struct netdev *netdev = station_get_netdev(station); 1840 1841 l_dbus_message_builder_append_basic(builder, 'o', 1842 netdev_get_path(netdev)); 1843 1844 return true; 1845 } 1846 1847 static bool network_property_get_type(struct l_dbus *dbus, 1848 struct l_dbus_message *message, 1849 struct l_dbus_message_builder *builder, 1850 void *user_data) 1851 1852 { 1853 struct network *network = user_data; 1854 enum security security = network_get_security(network); 1855 1856 l_dbus_message_builder_append_basic(builder, 's', 1857 security_to_str(security)); 1858 1859 return true; 1860 } 1861 1862 static bool network_property_get_known_network(struct l_dbus *dbus, 1863 struct l_dbus_message *message, 1864 struct l_dbus_message_builder *builder, 1865 void *user_data) 1866 { 1867 struct network *network = user_data; 1868 1869 if (!network->info) 1870 return false; 1871 1872 l_dbus_message_builder_append_basic(builder, 'o', 1873 network_info_get_path(network->info)); 1874 1875 return true; 1876 } 1877 1878 static bool network_property_get_extended_service_set(struct l_dbus *dbus, 1879 struct l_dbus_message *message, 1880 struct l_dbus_message_builder *builder, 1881 void *user_data) 1882 { 1883 struct network *network = user_data; 1884 const struct l_queue_entry *e; 1885 1886 l_dbus_message_builder_enter_array(builder, "o"); 1887 1888 for (e = l_queue_get_entries(network->bss_list); e; e = e->next) { 1889 struct scan_bss *bss = e->data; 1890 const char *path = network_bss_get_path(network, bss); 1891 1892 l_dbus_message_builder_append_basic(builder, 'o', path); 1893 } 1894 1895 l_dbus_message_builder_leave_array(builder); 1896 1897 return true; 1898 } 1899 1900 bool network_register(struct network *network, const char *path) 1901 { 1902 #ifdef HAVE_DBUS 1903 if (!l_dbus_object_add_interface(dbus_get_bus(), path, 1904 IWD_NETWORK_INTERFACE, network)) { 1905 l_info("Unable to register %s interface", 1906 IWD_NETWORK_INTERFACE); 1907 return false; 1908 } 1909 1910 if (!l_dbus_object_add_interface(dbus_get_bus(), path, 1911 L_DBUS_INTERFACE_PROPERTIES, network)) 1912 l_info("Unable to register %s interface", 1913 L_DBUS_INTERFACE_PROPERTIES); 1914 #endif 1915 1916 network->object_path = l_strdup(path); 1917 1918 return true; 1919 } 1920 1921 static void network_unregister(struct network *network, int reason) 1922 { 1923 #ifdef HAVE_DBUS 1924 struct l_dbus *dbus = dbus_get_bus(); 1925 #endif 1926 1927 if (network->connect_after_anqp) 1928 dbus_pending_reply(&network->connect_after_anqp, 1929 dbus_error_aborted(network->connect_after_anqp)); 1930 1931 if (network->connect_after_owe_hidden) 1932 dbus_pending_reply(&network->connect_after_owe_hidden, 1933 dbus_error_aborted(network->connect_after_owe_hidden)); 1934 1935 agent_request_cancel(network->agent_request, reason); 1936 network_settings_close(network); 1937 1938 #ifdef HAVE_DBUS 1939 l_dbus_unregister_object(dbus, network->object_path); 1940 #endif 1941 1942 l_free(network->object_path); 1943 network->object_path = NULL; 1944 } 1945 1946 void network_remove(struct network *network, int reason) 1947 { 1948 if (network->object_path) 1949 network_unregister(network, reason); 1950 1951 l_queue_destroy(network->secrets, eap_secret_info_free); 1952 network->secrets = NULL; 1953 1954 if (network->info) 1955 network->info->seen_count -= 1; 1956 1957 l_queue_destroy(network->bss_list, NULL); 1958 l_queue_destroy(network->blacklist, NULL); 1959 1960 if (network->nai_realms) 1961 l_strv_free(network->nai_realms); 1962 1963 if (network->rc_ie) 1964 l_free(network->rc_ie); 1965 1966 l_free(network); 1967 } 1968 1969 int network_rank_compare(const void *a, const void *b, void *user) 1970 { 1971 const struct network *new_network = a; 1972 const struct network *network = b; 1973 1974 return (network->rank > new_network->rank) ? 1 : -1; 1975 } 1976 1977 void network_rank_update(struct network *network, bool connected) 1978 { 1979 static const double RANK_RSNE_FACTOR = 1.2; 1980 static const double RANK_WPA_FACTOR = 1.0; 1981 static const double RANK_OPEN_FACTOR = 0.5; 1982 static const double RANK_NO_PRIVACY_FACTOR = 0.5; 1983 /* 1984 * Theoretically there may be difference between the BSS selection 1985 * here and in network_bss_select but those should be rare cases. 1986 */ 1987 struct scan_bss *best_bss = l_queue_peek_head(network->bss_list); 1988 struct network_info *info = network->info; 1989 1990 /* 1991 * The rank should separate networks into four groups that use 1992 * non-overlapping ranges for: 1993 * - current connected network, 1994 * - other networks we've connected to before, 1995 * - networks with preprovisioned settings file that we haven't 1996 * used yet, 1997 * - other networks. 1998 * 1999 * Within the 2nd group the last connection time is the main factor, 2000 * for the other two groups it's the BSS rank - mainly signal strength. 2001 */ 2002 if (connected) { 2003 network->rank = INT_MAX; 2004 return; 2005 } 2006 2007 if (!info) { /* Not known, assign negative rank */ 2008 network->rank = (int) best_bss->rank - USHRT_MAX; 2009 return; 2010 } 2011 2012 if (info->config.connected_time != 0) { 2013 int n = known_network_offset(info); 2014 2015 L_WARN_ON(n < 0); 2016 2017 network->rank = 2018 util_exponential_decay(n) * best_bss->rank + USHRT_MAX; 2019 } else 2020 network->rank = best_bss->rank; 2021 2022 /* 2023 * Prefer RSNE first, WPA second. Open networks are much less 2024 * desirable. 2025 */ 2026 if (best_bss->rsne) 2027 network->rank *= RANK_RSNE_FACTOR; 2028 else if (best_bss->wpa) 2029 network->rank *= RANK_WPA_FACTOR; 2030 else 2031 network->rank *= RANK_OPEN_FACTOR; 2032 2033 /* We prefer networks with CAP PRIVACY */ 2034 if (!(best_bss->capability & IE_BSS_CAP_PRIVACY)) 2035 network->rank *= RANK_NO_PRIVACY_FACTOR; 2036 } 2037 2038 static void network_unset_hotspot(struct network *network, void *user_data) 2039 { 2040 struct network_info *info = user_data; 2041 2042 if (network->info != info) 2043 return; 2044 2045 network_set_info(network, NULL); 2046 2047 l_queue_destroy(network->secrets, eap_secret_info_free); 2048 network->secrets = NULL; 2049 } 2050 2051 static void emit_known_network_removed(struct station *station, void *user_data) 2052 { 2053 struct network_info *info = user_data; 2054 bool was_hidden = info->config.is_hidden; 2055 struct network *connected_network; 2056 struct network *network = NULL; 2057 2058 /* Clear network info, as this network is no longer known */ 2059 if (info->is_hotspot) 2060 station_network_foreach(station, network_unset_hotspot, info); 2061 else { 2062 network = station_network_find(station, info->ssid, info->type); 2063 if (!network) 2064 return; 2065 2066 network_set_info(network, NULL); 2067 2068 l_queue_destroy(network->secrets, eap_secret_info_free); 2069 network->secrets = NULL; 2070 } 2071 2072 connected_network = station_get_connected_network(station); 2073 if (connected_network && connected_network->info == NULL) 2074 station_disconnect(station); 2075 2076 if (network && was_hidden) 2077 station_hide_network(station, network); 2078 } 2079 2080 static void network_update_hotspot(struct network *network, void *user_data) 2081 { 2082 struct network_info *info = user_data; 2083 2084 match_hotspot_network(info, network); 2085 } 2086 2087 static void match_known_network(struct station *station, void *user_data) 2088 { 2089 struct network_info *info = user_data; 2090 struct network *network; 2091 2092 if (!info->is_hotspot) { 2093 network = station_network_find(station, info->ssid, info->type); 2094 if (!network) 2095 return; 2096 2097 network_set_info(network, info); 2098 return; 2099 } 2100 2101 /* This is a new hotspot network */ 2102 station_network_foreach(station, network_update_hotspot, info); 2103 } 2104 2105 static void known_networks_changed(enum known_networks_event event, 2106 const struct network_info *info, 2107 void *user_data) 2108 { 2109 switch (event) { 2110 case KNOWN_NETWORKS_EVENT_ADDED: 2111 station_foreach(match_known_network, (void *) info); 2112 2113 /* Syncs frequencies of newly known network */ 2114 known_network_frequency_sync((struct network_info *)info); 2115 break; 2116 case KNOWN_NETWORKS_EVENT_REMOVED: 2117 station_foreach(emit_known_network_removed, (void *) info); 2118 break; 2119 case KNOWN_NETWORKS_EVENT_UPDATED: 2120 break; 2121 } 2122 } 2123 2124 static void event_watch_changed(enum station_event state, 2125 struct network *network, void *user_data) 2126 { 2127 struct l_dbus_message *reply; 2128 2129 switch (state) { 2130 case STATION_EVENT_ANQP_STARTED: 2131 network->anqp_pending = true; 2132 break; 2133 case STATION_EVENT_ANQP_FINISHED: 2134 network->anqp_pending = false; 2135 2136 if (!network->connect_after_anqp) 2137 return; 2138 2139 l_debug("ANQP complete, resuming connect to %s", network->ssid); 2140 2141 if (!network_settings_load(network)) { 2142 #ifdef HAVE_DBUS 2143 reply = dbus_error_not_configured( 2144 network->connect_after_anqp); 2145 dbus_pending_reply(&network->connect_after_anqp, reply); 2146 #endif 2147 return; 2148 } 2149 2150 reply = network_connect_8021x(network, 2151 network_bss_select(network, true), 2152 network->connect_after_anqp); 2153 2154 #ifdef HAVE_DBUS 2155 if (reply) 2156 l_dbus_send(dbus_get_bus(), reply); 2157 2158 l_dbus_message_unref(network->connect_after_anqp); 2159 #endif 2160 network->connect_after_anqp = NULL; 2161 2162 break; 2163 case STATION_EVENT_OWE_HIDDEN_STARTED: 2164 network->owe_hidden_pending = true; 2165 break; 2166 case STATION_EVENT_OWE_HIDDEN_FINISHED: 2167 network->owe_hidden_pending = false; 2168 2169 if (!network->connect_after_owe_hidden) 2170 return; 2171 2172 station_connect_network(network->station, network, 2173 network_bss_select(network, true), 2174 network->connect_after_owe_hidden); 2175 2176 l_dbus_message_unref(network->connect_after_owe_hidden); 2177 network->connect_after_owe_hidden = NULL; 2178 2179 break; 2180 } 2181 } 2182 2183 static void setup_network_interface(struct l_dbus_interface *interface) 2184 { 2185 l_dbus_interface_method(interface, "Connect", 0, 2186 network_connect, 2187 "", ""); 2188 2189 l_dbus_interface_property(interface, "Name", 0, "s", 2190 network_property_get_name, NULL); 2191 2192 l_dbus_interface_property(interface, "Connected", 0, "b", 2193 network_property_is_connected, 2194 NULL); 2195 2196 l_dbus_interface_property(interface, "Device", 0, "o", 2197 network_property_get_device, NULL); 2198 2199 l_dbus_interface_property(interface, "Type", 0, "s", 2200 network_property_get_type, NULL); 2201 2202 l_dbus_interface_property(interface, "KnownNetwork", 0, "o", 2203 network_property_get_known_network, NULL); 2204 2205 l_dbus_interface_property(interface, "ExtendedServiceSet", 0, "ao", 2206 network_property_get_extended_service_set, NULL); 2207 } 2208 2209 static bool network_bss_property_get_address(struct l_dbus *dbus, 2210 struct l_dbus_message *message, 2211 struct l_dbus_message_builder *builder, 2212 void *user_data) 2213 { 2214 struct scan_bss *bss = user_data; 2215 2216 l_dbus_message_builder_append_basic(builder, 's', 2217 util_address_to_string(bss->addr)); 2218 return true; 2219 } 2220 2221 static void setup_bss_interface(struct l_dbus_interface *interface) 2222 { 2223 l_dbus_interface_property(interface, "Address", 0, "s", 2224 network_bss_property_get_address, NULL); 2225 } 2226 2227 static int network_init(void) 2228 { 2229 #ifdef HAVE_DBUS 2230 if (!l_dbus_register_interface(dbus_get_bus(), IWD_NETWORK_INTERFACE, 2231 setup_network_interface, NULL, false)) 2232 l_error("Unable to register %s interface", 2233 IWD_NETWORK_INTERFACE); 2234 #endif 2235 2236 if (!l_dbus_register_interface(dbus_get_bus(), IWD_BSS_INTERFACE, 2237 setup_bss_interface, NULL, false)) 2238 l_error("Unable to register %s interface", 2239 IWD_BSS_INTERFACE); 2240 2241 known_networks_watch = 2242 known_networks_watch_add(known_networks_changed, NULL, NULL); 2243 2244 event_watch = station_add_event_watch(event_watch_changed, NULL, NULL); 2245 2246 return 0; 2247 } 2248 2249 static void network_exit(void) 2250 { 2251 known_networks_watch_remove(known_networks_watch); 2252 known_networks_watch = 0; 2253 2254 station_remove_event_watch(event_watch); 2255 event_watch = 0; 2256 2257 #ifdef HAVE_DBUS 2258 l_dbus_unregister_interface(dbus_get_bus(), IWD_NETWORK_INTERFACE); 2259 l_dbus_unregister_interface(dbus_get_bus(), IWD_BSS_INTERFACE); 2260 #endif 2261 } 2262 2263 IWD_MODULE(network, network_init, network_exit) 2264 IWD_MODULE_DEPENDS(network, known_networks)