station.c (164119B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2018-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 <stdio.h> 29 #include <errno.h> 30 #include <time.h> 31 #include <sys/time.h> 32 #include <limits.h> 33 #include <linux/if_ether.h> 34 35 #include <ell/ell.h> 36 37 #include "ell/useful.h" 38 #include "src/util.h" 39 #include "src/iwd.h" 40 #include "src/module.h" 41 #include "src/common.h" 42 #include "src/watchlist.h" 43 #include "src/scan.h" 44 #include "src/netdev.h" 45 #include "src/dbus.h" 46 #include "src/wiphy.h" 47 #include "src/network.h" 48 #include "src/knownnetworks.h" 49 #include "src/ie.h" 50 #include "src/handshake.h" 51 #include "src/station.h" 52 #include "src/blacklist.h" 53 #include "src/mpdu.h" 54 #include "src/erp.h" 55 #include "src/netconfig.h" 56 #include "src/anqp.h" 57 #include "src/anqputil.h" 58 #include "src/diagnostic.h" 59 #include "src/frame-xchg.h" 60 #include "src/sysfs.h" 61 #include "src/band.h" 62 #include "src/ft.h" 63 #include "src/eap.h" 64 #include "src/eap-tls-common.h" 65 #include "src/storage.h" 66 #include "src/pmksa.h" 67 #include "src/vendor_quirks.h" 68 69 #define STATION_RECENT_NETWORK_LIMIT 5 70 #define STATION_RECENT_FREQS_LIMIT 5 71 72 static struct l_queue *station_list; 73 static uint32_t netdev_watch; 74 static uint32_t mfp_setting; 75 static uint32_t roam_retry_interval; 76 static bool anqp_disabled; 77 static bool supports_arp_evict_nocarrier; 78 static bool supports_ndisc_evict_nocarrier; 79 static bool supports_drop_gratuitous_arp; 80 static bool supports_drop_unsolicited_na; 81 static bool supports_ipv4_drop_unicast_in_l2_multicast; 82 static bool supports_ipv6_drop_unicast_in_l2_multicast; 83 static bool pmksa_disabled; 84 static struct watchlist event_watches; 85 static uint32_t known_networks_watch; 86 static uint32_t allowed_bands; 87 88 struct station { 89 enum station_state state; 90 struct watchlist state_watches; 91 struct scan_bss *connected_bss; 92 struct network *connected_network; 93 struct scan_bss *connect_pending_bss; 94 struct network *connect_pending_network; 95 struct l_queue *autoconnect_list; 96 struct l_queue *bss_list; 97 struct l_queue *hidden_bss_list_sorted; 98 struct l_hashmap *networks; 99 struct l_queue *networks_sorted; 100 struct l_dbus_message *connect_pending; 101 struct l_dbus_message *hidden_pending; 102 struct l_dbus_message *disconnect_pending; 103 struct l_dbus_message *scan_pending; 104 struct l_dbus_message *get_station_pending; 105 struct signal_agent *signal_agent; 106 uint32_t dbus_scan_id; 107 uint32_t quick_scan_id; 108 uint32_t hidden_network_scan_id; 109 struct l_queue *owe_hidden_scan_ids; 110 111 /* Roaming related members */ 112 struct l_timeout *roam_trigger_timeout; 113 uint32_t roam_scan_id; 114 uint8_t preauth_bssid[6]; 115 116 struct wiphy *wiphy; 117 struct netdev *netdev; 118 119 struct l_queue *anqp_pending; 120 121 struct netconfig *netconfig; 122 123 /* Set of frequencies to scan first when attempting a roam */ 124 struct scan_freq_set *roam_freqs; 125 struct l_queue *roam_bss_list; 126 127 /* Frequencies split into subsets by priority */ 128 struct scan_freq_set *scan_freqs_order[3]; 129 unsigned int dbus_scan_subset_idx; 130 131 uint32_t wiphy_watch; 132 133 struct wiphy_radio_work_item ft_work; 134 135 uint64_t last_roam_scan; 136 137 struct l_queue *affinities; 138 unsigned int affinity_watch; 139 char *affinity_client; 140 141 struct handshake_state *hs; 142 143 bool preparing_roam : 1; 144 bool roam_scan_full : 1; 145 bool signal_low : 1; 146 bool ap_directed_roaming : 1; 147 bool scanning : 1; 148 bool autoconnect : 1; 149 bool autoconnect_can_start : 1; 150 bool netconfig_after_roam : 1; 151 }; 152 153 struct anqp_entry { 154 struct station *station; 155 struct network *network; 156 uint32_t pending; 157 }; 158 159 /* 160 * Rather than sorting BSS's purely based on ranking a higher level grouping 161 * is used. The purpose of this higher order grouping is the consider the BSS's 162 * roam blacklist status. The roam blacklist is a "soft" blacklist in that we 163 * still should connect to these BSS's if they are the only "good" option. 164 * The question here is: what makes a BSS "good" vs "bad". 165 * 166 * For an initial (probably naive) approach here we can use the 167 * RoamThreshold[5G] which indicates the signal level that would not 168 * be of an acceptable connection quality. BSS can then be sorted either 169 * above or below this threshold. Within each of these groups a BSS may be 170 * blacklisted, meaning it should get sorted lower on the list compared to 171 * others within the same group. 172 * 173 * This sorting is achieved by extending rank to a uint32_t where the first 16 174 * bits are the standard rank calculated by scan.c. Above that bits can be 175 * reserved for this higher level grouping: 176 * 177 * Bit 16 indicates the BSS is not blacklisted 178 * Bit 17 indicates the BSS is above the critical signal threshold 179 */ 180 181 #define ABOVE_THRESHOLD_BIT 17 182 #define NOT_BLACKLISTED_BIT 16 183 184 static uint32_t evaluate_bss_group_rank(const uint8_t *addr, uint32_t freq, 185 int16_t signal_strength, uint16_t rank) 186 { 187 int signal = signal_strength / 100; 188 bool roam_blacklist; 189 bool good_signal; 190 uint32_t rank_out = (uint32_t) rank; 191 192 if (blacklist_contains_bss(addr, BLACKLIST_REASON_CONNECT_FAILED)) 193 return 0; 194 195 roam_blacklist = blacklist_contains_bss(addr, BLACKLIST_REASON_AP_BUSY); 196 good_signal = signal >= netdev_get_low_signal_threshold(freq); 197 198 if (good_signal) 199 set_bit(&rank_out, ABOVE_THRESHOLD_BIT); 200 201 if (!roam_blacklist) 202 set_bit(&rank_out, NOT_BLACKLISTED_BIT); 203 204 return rank_out; 205 } 206 207 /* 208 * Used as entries for the roam list since holding scan_bss pointers directly 209 * from station->bss_list is not 100% safe due to the possibility of the 210 * hardware scanning and overwriting station->bss_list. 211 */ 212 struct roam_bss { 213 uint8_t addr[6]; 214 uint32_t rank; 215 int32_t signal_strength; 216 bool ft_failed: 1; 217 }; 218 219 static struct roam_bss *roam_bss_from_scan_bss(const struct scan_bss *bss, 220 uint32_t rank) 221 { 222 struct roam_bss *rbss = l_new(struct roam_bss, 1); 223 224 memcpy(rbss->addr, bss->addr, 6); 225 rbss->rank = rank; 226 rbss->signal_strength = bss->signal_strength; 227 rbss->ft_failed = false; 228 229 return rbss; 230 } 231 232 static int roam_bss_rank_compare(const void *a, const void *b, void *user_data) 233 { 234 const struct roam_bss *new_bss = a, *bss = b; 235 236 if (bss->rank == new_bss->rank) 237 return (bss->signal_strength > 238 new_bss->signal_strength) ? 1 : -1; 239 240 return (bss->rank > new_bss->rank) ? 1 : -1; 241 } 242 243 struct wiphy *station_get_wiphy(struct station *station) 244 { 245 return station->wiphy; 246 } 247 248 struct netdev *station_get_netdev(struct station *station) 249 { 250 return station->netdev; 251 } 252 253 struct network *station_get_connected_network(struct station *station) 254 { 255 return station->connected_network; 256 } 257 258 bool station_is_busy(struct station *station) 259 { 260 return station->state != STATION_STATE_DISCONNECTED && 261 station->state != STATION_STATE_AUTOCONNECT_FULL && 262 station->state != STATION_STATE_AUTOCONNECT_QUICK; 263 } 264 265 static bool station_is_autoconnecting(struct station *station) 266 { 267 return station->state == STATION_STATE_AUTOCONNECT_FULL || 268 station->state == STATION_STATE_AUTOCONNECT_QUICK; 269 } 270 271 static bool station_is_roaming(struct station *station) 272 { 273 return station->state == STATION_STATE_ROAMING || 274 station->state == STATION_STATE_FT_ROAMING || 275 station->state == STATION_STATE_FW_ROAMING; 276 } 277 278 static bool station_debug_event(struct station *station, const char *name) 279 { 280 struct l_dbus_message *signal; 281 282 if (!iwd_is_developer_mode()) 283 return true; 284 285 l_debug("StationDebug.Event(%s)", name); 286 287 signal = l_dbus_message_new_signal(dbus_get_bus(), 288 netdev_get_path(station->netdev), 289 IWD_STATION_DEBUG_INTERFACE, "Event"); 290 291 l_dbus_message_set_arguments(signal, "sav", name, 0); 292 293 return l_dbus_send(dbus_get_bus(), signal) != 0; 294 } 295 296 static void station_property_set_scanning(struct station *station, 297 bool scanning) 298 { 299 if (station->scanning == scanning) 300 return; 301 302 station->scanning = scanning; 303 304 #ifdef HAVE_DBUS 305 l_dbus_property_changed(dbus_get_bus(), 306 netdev_get_path(station->netdev), 307 IWD_STATION_INTERFACE, "Scanning"); 308 #endif 309 } 310 311 static void station_enter_state(struct station *station, 312 enum station_state state); 313 314 static void network_add_foreach(struct network *network, void *user_data) 315 { 316 struct station *station = user_data; 317 318 l_queue_insert(station->autoconnect_list, network, 319 network_rank_compare, NULL); 320 } 321 322 static int station_autoconnect_next(struct station *station) 323 { 324 struct network *network; 325 int r; 326 327 if (!station->autoconnect_list) 328 return -ENOENT; 329 330 while ((network = l_queue_pop_head(station->autoconnect_list))) { 331 const char *ssid = network_get_ssid(network); 332 struct scan_bss *bss = network_bss_select(network, false); 333 334 l_debug("autoconnect: Trying SSID: %s", ssid); 335 336 if (!bss) { 337 l_debug("autoconnect: No suitable BSSes found"); 338 continue; 339 } 340 341 l_debug("autoconnect: '%s' freq: %u, rank: %u, strength: %i", 342 util_address_to_string(bss->addr), 343 bss->frequency, bss->rank, 344 bss->signal_strength); 345 346 r = network_autoconnect(network, bss); 347 if (!r) { 348 if (station->quick_scan_id) { 349 scan_cancel(netdev_get_wdev_id(station->netdev), 350 station->quick_scan_id); 351 station->quick_scan_id = 0; 352 station_property_set_scanning(station, false); 353 } 354 355 return 0; 356 } 357 358 l_debug("autoconnect: network_autoconnect: %s (%d)", 359 strerror(-r), r); 360 } 361 362 return -ENOENT; 363 } 364 365 static void station_autoconnect_start(struct station *station) 366 { 367 if (!station->autoconnect_can_start) 368 return; 369 370 if (!station_is_autoconnecting(station)) 371 return; 372 373 if (!l_queue_isempty(station->anqp_pending)) 374 return; 375 376 if (!l_queue_isempty(station->owe_hidden_scan_ids)) 377 return; 378 379 if (L_WARN_ON(station->autoconnect_list)) 380 l_queue_destroy(station->autoconnect_list, NULL); 381 382 l_debug(""); 383 384 station->autoconnect_list = l_queue_new(); 385 station_network_foreach(station, network_add_foreach, station); 386 station_autoconnect_next(station); 387 station->autoconnect_can_start = false; 388 } 389 390 static void bss_free(void *data) 391 { 392 struct scan_bss *bss = data; 393 394 scan_bss_free(bss); 395 } 396 397 static void network_free(void *data) 398 { 399 struct network *network = data; 400 401 network_remove(network, -ESHUTDOWN); 402 } 403 404 struct process_network_data { 405 struct station *station; 406 const struct scan_freq_set *freqs; 407 }; 408 409 static bool process_network(const void *key, void *data, void *user_data) 410 { 411 struct network *network = data; 412 struct process_network_data *process_data = user_data; 413 struct station *station = process_data->station; 414 415 if (!network_bss_list_isempty(network)) { 416 bool connected = network == station->connected_network; 417 418 /* Build the network list ordered by rank */ 419 network_rank_update(network, connected); 420 421 l_queue_insert(station->networks_sorted, network, 422 network_rank_compare, NULL); 423 424 network_update_known_frequencies(network); 425 426 return false; 427 } 428 429 /* Drop networks that have no more BSSs in range */ 430 l_debug("No remaining BSSs for SSID: %s -- Removing network", 431 network_get_ssid(network)); 432 network_remove(network, -ERANGE); 433 434 return true; 435 } 436 437 static const char *iwd_network_get_path(struct station *station, 438 const char *ssid, 439 enum security security) 440 { 441 static char path[256]; 442 unsigned int pos, i; 443 444 pos = snprintf(path, sizeof(path), "%s/", 445 netdev_get_path(station->netdev)); 446 447 for (i = 0; ssid[i] && pos < sizeof(path); i++) 448 pos += snprintf(path + pos, sizeof(path) - pos, "%02hhx", 449 ssid[i]); 450 451 snprintf(path + pos, sizeof(path) - pos, "_%s", 452 security_to_str(security)); 453 454 return path; 455 } 456 457 struct network *station_network_find(struct station *station, const char *ssid, 458 enum security security) 459 { 460 const char *path = iwd_network_get_path(station, ssid, security); 461 462 return l_hashmap_lookup(station->networks, path); 463 } 464 465 static int bss_signal_strength_compare(const void *a, const void *b, void *user) 466 { 467 const struct scan_bss *new_bss = a; 468 const struct scan_bss *bss = b; 469 470 return (bss->signal_strength > new_bss->signal_strength) ? 1 : -1; 471 } 472 473 static void station_print_scan_bss(const struct scan_bss *bss) 474 { 475 uint32_t kbps100 = DIV_ROUND_CLOSEST(bss->data_rate, 100000); 476 char optional[64] = {0}; 477 char *ptr = optional; 478 479 if (bss->have_snr) 480 ptr += sprintf(ptr, ", snr: %d", bss->snr); 481 482 if (bss->have_utilization) 483 ptr += sprintf(ptr, ", load: %u/255, clients: %u", 484 bss->utilization, bss->sta_count); 485 486 l_debug("Processing BSS '%s' with SSID: %s, freq: %u, rank: %u, " 487 "strength: %i, data_rate: %u.%u%s", 488 util_address_to_string(bss->addr), 489 util_ssid_to_utf8(bss->ssid_len, bss->ssid), 490 bss->frequency, bss->rank, bss->signal_strength, 491 kbps100 / 10, kbps100 % 10, 492 optional); 493 } 494 495 static const char *station_get_bss_path(struct station *station, 496 struct scan_bss *bss) 497 { 498 enum security security; 499 char ssid[33]; 500 const char *network_path; 501 502 memcpy(ssid, bss->ssid, bss->ssid_len); 503 ssid[bss->ssid_len] = '\0'; 504 505 if (scan_bss_get_security(bss, &security) < 0) 506 return NULL; 507 508 network_path = iwd_network_get_path(station, ssid, security); 509 if (!network_path) 510 return NULL; 511 512 return __network_path_append_bss(network_path, bss); 513 } 514 515 static bool match_bss_path(const void *data, const void *user_data) 516 { 517 const char *path1 = data; 518 const char *path2 = user_data; 519 520 return !strcmp(path1, path2); 521 } 522 523 static bool station_unregister_bss(struct station *station, 524 struct scan_bss *bss) 525 { 526 const char *path = station_get_bss_path(station, bss); 527 528 if (L_WARN_ON(!path)) 529 return false; 530 531 l_queue_remove_if(station->affinities, match_bss_path, path); 532 533 return l_dbus_unregister_object(dbus_get_bus(), path); 534 } 535 536 static bool station_register_bss(struct network *network, struct scan_bss *bss) 537 { 538 struct scan_bss *old; 539 const char *path = __network_path_append_bss(network_get_path(network), 540 bss); 541 542 if (L_WARN_ON(!path)) 543 return false; 544 545 /* 546 * If we find this path in the object tree update the data to the new 547 * scan_bss pointer, as this one will be freed soon. 548 */ 549 old = l_dbus_object_get_data(dbus_get_bus(), path, IWD_BSS_INTERFACE); 550 if (old) 551 return l_dbus_object_set_data(dbus_get_bus(), path, 552 IWD_BSS_INTERFACE, bss); 553 554 if (!l_dbus_object_add_interface(dbus_get_bus(), path, 555 IWD_BSS_INTERFACE, bss)) 556 return false; 557 558 if (!l_dbus_object_add_interface(dbus_get_bus(), path, 559 L_DBUS_INTERFACE_PROPERTIES, bss)) 560 return false; 561 562 return true; 563 } 564 565 /* 566 * Returns the network object the BSS was added to or NULL if ignored. 567 */ 568 static struct network *station_add_seen_bss(struct station *station, 569 struct scan_bss *bss) 570 { 571 struct network *network; 572 enum security security; 573 const char *path; 574 char ssid[SSID_MAX_SIZE + 1]; 575 576 station_print_scan_bss(bss); 577 578 if (util_ssid_is_hidden(bss->ssid_len, bss->ssid)) { 579 l_debug("BSS has hidden SSID"); 580 581 l_queue_insert(station->hidden_bss_list_sorted, bss, 582 bss_signal_strength_compare, NULL); 583 return NULL; 584 } 585 586 memcpy(ssid, bss->ssid, bss->ssid_len); 587 ssid[bss->ssid_len] = '\0'; 588 589 if (!(bss->capability & IE_BSS_CAP_ESS)) { 590 l_debug("Ignoring non-ESS BSS \"%s\"", ssid); 591 return NULL; 592 } 593 594 if (scan_bss_get_security(bss, &security) < 0) 595 return NULL; 596 597 /* Hidden OWE transition network */ 598 if (security == SECURITY_NONE && bss->rsne && bss->owe_trans) { 599 struct ie_owe_transition_info *info = bss->owe_trans; 600 /* 601 * WiFi Alliance OWE Specification v1.1 - Section 2.2.1: 602 * 603 * "2. An OWE AP shall use two different SSIDs, one for OWE 604 * and one for Open" 605 * 606 * "4. The OWE BSS shall include the OWE Transition Mode element 607 * in all Beacon and Probe Response frames to encapsulate 608 * the BSSID and SSID of the Open BSS" 609 * 610 * Meaning the hidden SSID should not match the SSID in the 611 * hidden network's OWE IE. Might as well restrict BSSID as well 612 * to be safe. 613 * 614 * In addition this SSID must be a valid utf8 string otherwise 615 * we could not look up the network. Note that this is not true 616 * for the open BSS IE, it can be non-utf8. 617 */ 618 if (!util_ssid_is_utf8(info->ssid_len, info->ssid)) 619 return NULL; 620 621 if (!memcmp(info->ssid, bss->ssid, bss->ssid_len)) 622 return NULL; 623 624 if (!memcmp(info->bssid, bss->addr, 6)) 625 return NULL; 626 627 memcpy(ssid, info->ssid, info->ssid_len); 628 ssid[info->ssid_len] = '\0'; 629 630 l_debug("Found hidden OWE network, using %s for network lookup", 631 ssid); 632 } 633 634 path = iwd_network_get_path(station, ssid, security); 635 636 network = l_hashmap_lookup(station->networks, path); 637 if (!network) { 638 network = network_create(station, ssid, security); 639 640 if (!network_register(network, path)) { 641 network_remove(network, -EINVAL); 642 return NULL; 643 } 644 645 l_hashmap_insert(station->networks, 646 network_get_path(network), network); 647 l_debug("Added new Network \"%s\" security %s", 648 network_get_ssid(network), security_to_str(security)); 649 } 650 651 network_bss_add(network, bss); 652 station_register_bss(network, bss); 653 654 return network; 655 } 656 657 static bool bss_match(const void *a, const void *b) 658 { 659 const struct scan_bss *bss_a = a; 660 const struct scan_bss *bss_b = b; 661 662 if (memcmp(bss_a->addr, bss_b->addr, sizeof(bss_a->addr))) 663 return false; 664 665 if (bss_a->ssid_len != bss_b->ssid_len) 666 return false; 667 668 return !memcmp(bss_a->ssid, bss_b->ssid, bss_a->ssid_len); 669 } 670 671 struct bss_expiration_data { 672 struct scan_bss *connected_bss; 673 uint64_t now; 674 const struct scan_freq_set *freqs; 675 struct station *station; 676 }; 677 678 #define SCAN_RESULT_BSS_RETENTION_TIME (30 * 1000000) 679 680 static bool bss_free_if_expired(void *data, void *user_data) 681 { 682 struct scan_bss *bss = data; 683 struct bss_expiration_data *expiration_data = user_data; 684 685 if (bss == expiration_data->connected_bss) 686 /* Do not expire the currently connected BSS. */ 687 return false; 688 689 /* Keep any BSSes that are not on the frequency list */ 690 if (!scan_freq_set_contains(expiration_data->freqs, bss->frequency)) 691 return false; 692 693 if (l_time_before(expiration_data->now, 694 bss->time_stamp + SCAN_RESULT_BSS_RETENTION_TIME)) 695 return false; 696 697 station_unregister_bss(expiration_data->station, bss); 698 699 scan_bss_free(bss); 700 701 return true; 702 } 703 704 static void station_bss_list_remove_expired_bsses(struct station *station, 705 const struct scan_freq_set *freqs) 706 { 707 struct bss_expiration_data data = { 708 .now = l_time_now(), 709 .connected_bss = station->connected_bss, 710 .freqs = freqs, 711 .station = station, 712 }; 713 714 l_queue_foreach_remove(station->bss_list, bss_free_if_expired, &data); 715 } 716 717 struct nai_search { 718 struct network *network; 719 const char **realms; 720 }; 721 722 static bool match_nai_realms(const struct network_info *info, void *user_data) 723 { 724 struct nai_search *search = user_data; 725 726 if (!network_info_match_nai_realm(info, search->realms)) 727 return false; 728 729 network_set_info(search->network, (struct network_info *) info); 730 731 return true; 732 } 733 734 static bool match_pending(const void *a, const void *b) 735 { 736 const struct anqp_entry *entry = a; 737 738 return entry->pending != 0; 739 } 740 741 static void remove_anqp(void *data) 742 { 743 struct anqp_entry *entry = data; 744 745 if (entry->pending) 746 anqp_cancel(entry->pending); 747 748 l_free(entry); 749 } 750 751 static bool anqp_entry_foreach(void *data, void *user_data) 752 { 753 struct anqp_entry *e = data; 754 755 WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, 756 STATION_EVENT_ANQP_FINISHED, e->network); 757 758 remove_anqp(e); 759 760 return true; 761 } 762 763 static void station_anqp_response_cb(enum anqp_result result, 764 const void *anqp, size_t anqp_len, 765 void *user_data) 766 { 767 struct anqp_entry *entry = user_data; 768 struct station *station = entry->station; 769 struct network *network = entry->network; 770 struct anqp_iter iter; 771 uint16_t id; 772 uint16_t len; 773 const void *data; 774 char **realms = NULL; 775 struct nai_search search; 776 777 l_debug(""); 778 779 if (result != ANQP_SUCCESS) { 780 /* TODO: try next BSS */ 781 goto request_done; 782 } 783 784 anqp_iter_init(&iter, anqp, anqp_len); 785 786 while (anqp_iter_next(&iter, &id, &len, &data)) { 787 switch (id) { 788 case ANQP_NAI_REALM: 789 if (realms) 790 break; 791 792 realms = anqp_parse_nai_realms(data, len); 793 if (!realms) 794 goto request_done; 795 796 break; 797 default: 798 continue; 799 } 800 } 801 802 if (!realms) 803 goto request_done; 804 805 search.network = network; 806 search.realms = (const char **)realms; 807 808 known_networks_foreach(match_nai_realms, &search); 809 810 l_strv_free(realms); 811 812 request_done: 813 entry->pending = 0; 814 815 /* Return if there are other pending requests */ 816 if (l_queue_find(station->anqp_pending, match_pending, NULL)) 817 return; 818 819 /* Notify all watchers now that every ANQP request has finished */ 820 l_queue_foreach_remove(station->anqp_pending, anqp_entry_foreach, NULL); 821 822 station_autoconnect_start(station); 823 } 824 825 static bool station_start_anqp(struct station *station, struct network *network, 826 struct scan_bss *bss) 827 { 828 uint8_t anqp[256]; 829 uint8_t *ptr = anqp; 830 struct anqp_entry *entry; 831 832 if (!bss->hs20_capable) 833 return false; 834 835 /* Network already has ANQP data/HESSID */ 836 if (network_get_info(network)) 837 return false; 838 839 if (anqp_disabled) { 840 l_debug("Not querying AP for ANQP data (disabled)"); 841 return false; 842 } 843 844 entry = l_new(struct anqp_entry, 1); 845 entry->station = station; 846 entry->network = network; 847 848 l_put_le16(ANQP_QUERY_LIST, ptr); 849 ptr += 2; 850 l_put_le16(2, ptr); 851 ptr += 2; 852 l_put_le16(ANQP_NAI_REALM, ptr); 853 ptr += 2; 854 l_put_le16(ANQP_VENDOR_SPECIFIC, ptr); 855 ptr += 2; 856 /* vendor length */ 857 l_put_le16(7, ptr); 858 ptr += 2; 859 *ptr++ = 0x50; 860 *ptr++ = 0x6f; 861 *ptr++ = 0x9a; 862 *ptr++ = 0x11; /* HS20 ANQP Element type */ 863 *ptr++ = ANQP_HS20_QUERY_LIST; 864 *ptr++ = 0; /* reserved */ 865 *ptr++ = ANQP_HS20_OSU_PROVIDERS_NAI_LIST; 866 867 /* 868 * TODO: Additional roaming consortiums can be queried if indicated 869 * by the roaming consortium IE. The IE contains up to the first 3, and 870 * these are checked in hs20_find_settings_file. 871 */ 872 873 entry->pending = anqp_request(netdev_get_wdev_id(station->netdev), 874 netdev_get_address(station->netdev), bss, anqp, 875 ptr - anqp, station_anqp_response_cb, 876 entry, NULL); 877 if (!entry->pending) { 878 l_free(entry); 879 return false; 880 } 881 882 l_queue_push_head(station->anqp_pending, entry); 883 884 WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, 885 STATION_EVENT_ANQP_STARTED, network); 886 return true; 887 } 888 889 static bool network_has_open_pair(struct network *network, struct scan_bss *owe) 890 { 891 const struct l_queue_entry *entry; 892 struct ie_owe_transition_info *owe_info = owe->owe_trans; 893 894 for (entry = network_bss_list_get_entries(network); entry; 895 entry = entry->next) { 896 struct scan_bss *open = entry->data; 897 struct ie_owe_transition_info *open_info = open->owe_trans; 898 899 /* AP does not advertise owe transition */ 900 if (!open_info) 901 continue; 902 903 /* 904 * Check if this is an Open/Hidden pair: 905 * 906 * Open SSID equals the SSID in OWE IE 907 * Open BSSID equals the BSSID in OWE IE 908 * 909 * OWE SSID equals the SSID in Open IE 910 * OWE BSSID equals the BSSID in Open IE 911 */ 912 if (open->ssid_len == owe_info->ssid_len && 913 open_info->ssid_len == owe->ssid_len && 914 !memcmp(open->ssid, owe_info->ssid, 915 open->ssid_len) && 916 !memcmp(open_info->ssid, owe->ssid, 917 owe->ssid_len) && 918 !memcmp(open->addr, owe_info->bssid, 6) && 919 !memcmp(open_info->bssid, owe->addr, 6)) 920 return true; 921 } 922 923 return false; 924 } 925 926 static bool station_owe_transition_results(int err, struct l_queue *bss_list, 927 const struct scan_freq_set *freqs, 928 void *userdata) 929 { 930 struct network *network = userdata; 931 struct station *station = network_get_station(network); 932 struct scan_bss *bss; 933 934 station_property_set_scanning(station, false); 935 936 if (err) 937 goto done; 938 939 while ((bss = l_queue_pop_head(bss_list))) { 940 /* 941 * Don't handle the open BSS, hidden BSS, BSS with no OWE 942 * Transition IE, or an IE with a non-utf8 SSID 943 */ 944 if (!bss->rsne || !bss->owe_trans || 945 util_ssid_is_hidden(bss->ssid_len, bss->ssid) || 946 !util_ssid_is_utf8(bss->owe_trans->ssid_len, 947 bss->owe_trans->ssid)) 948 goto free; 949 950 /* Check if we have an open BSS that matches */ 951 if (!network_has_open_pair(network, bss)) 952 goto free; 953 954 l_debug("Adding OWE transition network "MAC" to %s", 955 MAC_STR(bss->addr), network_get_ssid(network)); 956 957 l_queue_push_tail(station->bss_list, bss); 958 network_bss_add(network, bss); 959 station_register_bss(network, bss); 960 961 continue; 962 963 free: 964 scan_bss_free(bss); 965 } 966 967 network_update_known_frequencies(network); 968 969 l_queue_destroy(bss_list, NULL); 970 971 done: 972 l_queue_pop_head(station->owe_hidden_scan_ids); 973 974 WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, 975 STATION_EVENT_OWE_HIDDEN_FINISHED, network); 976 977 station_autoconnect_start(station); 978 979 return err == 0; 980 } 981 982 static void station_owe_transition_triggered(int err, void *user_data) 983 { 984 struct network *network = user_data; 985 struct station *station = network_get_station(network); 986 987 if (err < 0) { 988 l_debug("OWE transition scan trigger failed: %i", err); 989 990 l_queue_pop_head(station->owe_hidden_scan_ids); 991 992 WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, 993 STATION_EVENT_OWE_HIDDEN_FINISHED, network); 994 995 return; 996 } 997 998 l_debug("OWE transition scan triggered"); 999 1000 station_property_set_scanning(station, true); 1001 } 1002 1003 static void foreach_add_owe_scan(struct network *network, void *data) 1004 { 1005 struct station *station = data; 1006 const struct l_queue_entry *entry; 1007 struct l_queue *list = NULL; 1008 uint32_t id; 1009 1010 if (network_get_security(network) != SECURITY_NONE) 1011 return; 1012 1013 for (entry = network_bss_list_get_entries(network); entry; 1014 entry = entry->next) { 1015 struct scan_bss *open = entry->data; 1016 1017 if (!open->owe_trans) 1018 continue; 1019 1020 /* only want the open networks with WFA OWE IE */ 1021 if (open->rsne) 1022 continue; 1023 1024 /* BSS already in network object */ 1025 if (network_bss_find_by_addr(network, open->owe_trans->bssid)) 1026 continue; 1027 1028 if (!list) 1029 list = l_queue_new(); 1030 1031 l_queue_push_tail(list, open); 1032 } 1033 1034 if (!list) 1035 return; 1036 1037 id = scan_owe_hidden(netdev_get_wdev_id(station->netdev), list, 1038 station_owe_transition_triggered, 1039 station_owe_transition_results, network, NULL); 1040 1041 l_queue_destroy(list, NULL); 1042 1043 if (!id) 1044 return; 1045 1046 if (!station->owe_hidden_scan_ids) 1047 station->owe_hidden_scan_ids = l_queue_new(); 1048 1049 l_queue_push_tail(station->owe_hidden_scan_ids, L_UINT_TO_PTR(id)); 1050 1051 WATCHLIST_NOTIFY(&event_watches, station_event_watch_func_t, 1052 STATION_EVENT_OWE_HIDDEN_STARTED, network); 1053 } 1054 1055 static void station_process_owe_transition_networks(struct station *station) 1056 { 1057 station_network_foreach(station, foreach_add_owe_scan, station); 1058 } 1059 1060 static bool bss_free_if_ssid_not_utf8(void *data, void *user_data) 1061 { 1062 struct scan_bss *bss = data; 1063 1064 if (util_ssid_is_hidden(bss->ssid_len, bss->ssid)) 1065 return false; 1066 1067 if (util_ssid_is_utf8(bss->ssid_len, bss->ssid)) 1068 return false; 1069 1070 l_debug("Dropping scan_bss '%s', with non-utf8 SSID", 1071 util_address_to_string(bss->addr)); 1072 bss_free(bss); 1073 return true; 1074 } 1075 1076 /* 1077 * Used when scan results were obtained; either from scan running 1078 * inside station module or scans running in other state machines, e.g. wsc 1079 */ 1080 void station_set_scan_results(struct station *station, 1081 struct l_queue *new_bss_list, 1082 const struct scan_freq_set *freqs, 1083 bool trigger_autoconnect) 1084 { 1085 const struct l_queue_entry *bss_entry; 1086 struct network *network; 1087 struct process_network_data data; 1088 1089 l_queue_foreach_remove(new_bss_list, bss_free_if_ssid_not_utf8, NULL); 1090 1091 while ((network = l_queue_pop_head(station->networks_sorted))) 1092 network_bss_list_clear(network); 1093 1094 l_queue_clear(station->hidden_bss_list_sorted, NULL); 1095 1096 l_queue_destroy(station->autoconnect_list, NULL); 1097 station->autoconnect_list = NULL; 1098 1099 station_bss_list_remove_expired_bsses(station, freqs); 1100 1101 for (bss_entry = l_queue_get_entries(station->bss_list); bss_entry; 1102 bss_entry = bss_entry->next) { 1103 struct scan_bss *old_bss = bss_entry->data; 1104 struct scan_bss *new_bss; 1105 1106 new_bss = l_queue_find(new_bss_list, bss_match, old_bss); 1107 if (new_bss) { 1108 if (old_bss == station->connected_bss) 1109 station->connected_bss = new_bss; 1110 1111 scan_bss_free(old_bss); 1112 1113 continue; 1114 } 1115 1116 if (old_bss == station->connected_bss) { 1117 l_warn("Connected BSS not in scan results"); 1118 station->connected_bss->rank = 0; 1119 } 1120 1121 l_queue_push_tail(new_bss_list, old_bss); 1122 } 1123 1124 l_queue_destroy(station->bss_list, NULL); 1125 1126 for (bss_entry = l_queue_get_entries(new_bss_list); bss_entry; 1127 bss_entry = bss_entry->next) { 1128 struct scan_bss *bss = bss_entry->data; 1129 struct network *network = station_add_seen_bss(station, bss); 1130 1131 if (!network) 1132 continue; 1133 1134 /* Cached BSS entry, this should have been processed already */ 1135 if (!scan_freq_set_contains(freqs, bss->frequency)) 1136 continue; 1137 1138 station_register_bss(network, bss); 1139 1140 station_start_anqp(station, network, bss); 1141 } 1142 1143 station->bss_list = new_bss_list; 1144 1145 data.station = station; 1146 data.freqs = freqs; 1147 1148 l_hashmap_foreach_remove(station->networks, process_network, &data); 1149 1150 station->autoconnect_can_start = trigger_autoconnect; 1151 station_autoconnect_start(station); 1152 } 1153 1154 static void station_reconnect(struct station *station); 1155 1156 static void station_handshake_event(struct handshake_state *hs, 1157 enum handshake_event event, 1158 void *user_data, ...) 1159 { 1160 struct station *station = user_data; 1161 struct network *network = station->connected_network; 1162 va_list args; 1163 1164 va_start(args, user_data); 1165 1166 switch (event) { 1167 case HANDSHAKE_EVENT_STARTED: 1168 l_debug("Handshaking"); 1169 station_debug_event(station, "handshake-started"); 1170 break; 1171 case HANDSHAKE_EVENT_SETTING_KEYS: 1172 l_debug("Setting keys"); 1173 1174 /* If we got here, then our settings work. Update if needed */ 1175 network_sync_settings(network); 1176 break; 1177 case HANDSHAKE_EVENT_FAILED: 1178 netdev_handshake_failed(hs, va_arg(args, int)); 1179 break; 1180 case HANDSHAKE_EVENT_REKEY_FAILED: 1181 l_warn("Unable to securely rekey on this hw/kernel..."); 1182 station_reconnect(station); 1183 break; 1184 case HANDSHAKE_EVENT_TRANSITION_DISABLE: 1185 { 1186 const uint8_t *td = va_arg(args, const uint8_t *); 1187 size_t len = va_arg(args, size_t); 1188 1189 network_set_transition_disable(network, td, len); 1190 break; 1191 } 1192 case HANDSHAKE_EVENT_COMPLETE: 1193 case HANDSHAKE_EVENT_SETTING_KEYS_FAILED: 1194 case HANDSHAKE_EVENT_EAP_NOTIFY: 1195 case HANDSHAKE_EVENT_P2P_IP_REQUEST: 1196 case HANDSHAKE_EVENT_REKEY_COMPLETE: 1197 /* 1198 * currently we don't care about any other events. The 1199 * netdev_connect_cb will notify us when the connection is 1200 * complete. 1201 */ 1202 break; 1203 } 1204 1205 va_end(args); 1206 } 1207 1208 static int station_build_handshake_rsn(struct handshake_state *hs, 1209 struct wiphy *wiphy, 1210 struct network *network, 1211 struct scan_bss *bss) 1212 { 1213 struct netdev *netdev = netdev_find(hs->ifindex); 1214 const struct l_settings *settings = iwd_get_config(); 1215 enum security security = network_get_security(network); 1216 bool add_mde = false; 1217 struct erp_cache_entry *erp_cache = NULL; 1218 struct ie_rsn_info bss_info; 1219 uint8_t rsne_buf[256]; 1220 struct ie_rsn_info info; 1221 uint8_t *ap_ie; 1222 bool disable_ocv; 1223 enum band_freq band; 1224 struct pmksa *pmksa; 1225 1226 memset(&info, 0, sizeof(info)); 1227 1228 if (!band_freq_to_channel(bss->frequency, &band)) 1229 goto not_supported; 1230 1231 memset(&bss_info, 0, sizeof(bss_info)); 1232 scan_bss_get_rsn_info(bss, &bss_info); 1233 1234 if (bss_info.akm_suites & (IE_RSN_AKM_SUITE_FILS_SHA256 | 1235 IE_RSN_AKM_SUITE_FILS_SHA384)) 1236 hs->support_fils = true; 1237 1238 /* 1239 * If this network 8021x we might have a set of cached EAP keys. If so 1240 * wiphy may select FILS if supported by the AP. 1241 */ 1242 if (security == SECURITY_8021X && hs->support_fils) 1243 erp_cache = network_get_erp_cache(network); 1244 1245 info.akm_suites = wiphy_select_akm(wiphy, bss, security, 1246 &bss_info, erp_cache != NULL); 1247 1248 /* 1249 * Special case for OWE. With OWE we still need to build up the 1250 * handshake object with AKM/cipher info since OWE does the full 4-way 1251 * handshake. But if this is a non-OWE open network, we can skip this. 1252 */ 1253 if (security == SECURITY_NONE && 1254 !(info.akm_suites & IE_RSN_AKM_SUITE_OWE)) 1255 goto open_network; 1256 1257 if (!info.akm_suites) 1258 goto not_supported; 1259 1260 info.pairwise_ciphers = wiphy_select_cipher(wiphy, 1261 bss_info.pairwise_ciphers); 1262 info.group_cipher = wiphy_select_cipher(wiphy, 1263 bss_info.group_cipher); 1264 1265 if (!info.pairwise_ciphers || !info.group_cipher) 1266 goto not_supported; 1267 1268 /* Management frame protection is explicitly off for OSEN */ 1269 if (info.akm_suites & IE_RSN_AKM_SUITE_OSEN) { 1270 info.group_management_cipher = 1271 IE_RSN_CIPHER_SUITE_NO_GROUP_TRAFFIC; 1272 goto build_ie; 1273 } 1274 1275 switch (mfp_setting) { 1276 case 0: 1277 if (band != BAND_FREQ_6_GHZ) 1278 break; 1279 1280 l_error("MFP turned off by [General].ManagementFrameProtection," 1281 " 6GHz frequencies are disabled"); 1282 goto not_supported; 1283 case 1: 1284 info.group_management_cipher = 1285 wiphy_select_cipher(wiphy, 1286 bss_info.group_management_cipher); 1287 info.mfpc = info.group_management_cipher != 0; 1288 1289 if (band != BAND_FREQ_6_GHZ) 1290 break; 1291 1292 if (!info.mfpc) 1293 goto not_supported; 1294 1295 /* 1296 * 802.11ax Section 12.12.2 1297 * The STA shall use management frame protection 1298 * (MFPR=1) when using RSN. 1299 */ 1300 info.mfpr = true; 1301 1302 break; 1303 case 2: 1304 info.group_management_cipher = 1305 wiphy_select_cipher(wiphy, 1306 bss_info.group_management_cipher); 1307 1308 /* 1309 * MFP required on our side, but AP doesn't support MFP 1310 * or cipher mismatch 1311 */ 1312 if (info.group_management_cipher == 0) 1313 goto not_supported; 1314 1315 info.mfpc = true; 1316 info.mfpr = true; 1317 break; 1318 } 1319 1320 if (bss_info.mfpr && !info.mfpc) 1321 goto not_supported; 1322 1323 build_ie: 1324 if (!l_settings_get_bool(settings, "General", "DisableOCV", 1325 &disable_ocv)) 1326 disable_ocv = false; 1327 1328 /* 1329 * Obviously do not enable OCV if explicitly disabled or no AP support. 1330 * 1331 * Not obviously hostapd rejects OCV support if MFPC is not enabled. 1332 * This is not really specified by the spec, but we have to work around 1333 * this limitation. 1334 * 1335 * Another limitation is full mac cards. With limited testing it was 1336 * seen that they do not include the OCI in the 4-way handshake yet 1337 * still advertise the capability. Because of this OCV is disabled if 1338 * any offload features are detected (since IWD prefers to use offload). 1339 * 1340 * TODO: For now OCV is disabled if the network is FT capable. This is 1341 * being done until support in the kernel is added to 1342 * automatically include the OCI element for the association 1343 * request. 1344 */ 1345 info.ocvc = !disable_ocv && bss_info.ocvc && info.mfpc && 1346 !wiphy_can_offload(wiphy) && 1347 !IE_AKM_IS_FT(info.akm_suites); 1348 1349 /* 1350 * IEEE 802.11-2020 9.4.2.24.4 states extended key IDs can only be used 1351 * with CCMP/GCMP cipher suites. We also only enable support if the AP 1352 * also indicates support. 1353 */ 1354 if (wiphy_supports_ext_key_id(wiphy) && bss_info.extended_key_id && 1355 IE_CIPHER_IS_GCMP_CCMP(info.pairwise_ciphers)) 1356 info.extended_key_id = true; 1357 1358 if (IE_AKM_IS_SAE(info.akm_suites) && !pmksa_disabled) { 1359 pmksa = pmksa_cache_get(netdev_get_address(netdev), bss->addr, 1360 bss->ssid, bss->ssid_len, 1361 info.akm_suites); 1362 if (pmksa) { 1363 if (!L_WARN_ON(!handshake_state_set_pmksa(hs, pmksa))) { 1364 info.num_pmkids = 1; 1365 info.pmkids = hs->pmksa->pmkid; 1366 } 1367 } 1368 } 1369 1370 /* RSN takes priority */ 1371 if (bss->rsne) { 1372 ap_ie = bss->rsne; 1373 ie_build_rsne(&info, rsne_buf); 1374 } else if (bss->wpa) { 1375 ap_ie = bss->wpa; 1376 ie_build_wpa(&info, rsne_buf); 1377 } else if (bss->osen) { 1378 ap_ie = bss->osen; 1379 ie_build_osen(&info, rsne_buf); 1380 } else 1381 goto not_supported; 1382 1383 if (!handshake_state_set_authenticator_ie(hs, ap_ie)) 1384 goto not_supported; 1385 1386 if (!handshake_state_set_supplicant_ie(hs, rsne_buf)) 1387 goto not_supported; 1388 1389 if (IE_AKM_IS_FT(info.akm_suites)) 1390 add_mde = true; 1391 1392 /* 1393 * If FILS was chosen, the ERP cache has been verified to exist. Take 1394 * a reference now so it remains valid (in case of expiration) until 1395 * FILS starts. 1396 */ 1397 if (IE_AKM_IS_FILS(hs->akm_suite)) 1398 hs->erp_cache = erp_cache; 1399 else if (erp_cache) 1400 erp_cache_put(erp_cache); 1401 1402 open_network: 1403 if (security == SECURITY_NONE) 1404 /* Perform FT association if available */ 1405 add_mde = bss->mde_present; 1406 1407 if (add_mde) { 1408 uint8_t mde[5]; 1409 1410 /* The MDE advertised by the BSS must be passed verbatim */ 1411 mde[0] = IE_TYPE_MOBILITY_DOMAIN; 1412 mde[1] = 3; 1413 memcpy(mde + 2, bss->mde, 3); 1414 1415 handshake_state_set_mde(hs, mde); 1416 } 1417 1418 return 0; 1419 1420 not_supported: 1421 if (erp_cache) 1422 erp_cache_put(erp_cache); 1423 1424 return -ENOTSUP; 1425 } 1426 1427 static struct handshake_state *station_handshake_setup(struct station *station, 1428 struct network *network, 1429 struct scan_bss *bss) 1430 { 1431 struct wiphy *wiphy = station->wiphy; 1432 const struct network_info *info = network_get_info(network); 1433 struct handshake_state *hs; 1434 const struct iovec *vendor_ies; 1435 size_t iov_elems = 0; 1436 struct ie_fils_ip_addr_request_info fils_ip_req; 1437 1438 hs = netdev_handshake_state_new(station->netdev); 1439 1440 handshake_state_set_event_func(hs, station_handshake_event, station); 1441 1442 if (station_build_handshake_rsn(hs, wiphy, network, bss) < 0) 1443 goto not_supported; 1444 1445 handshake_state_set_authenticator_rsnxe(hs, bss->rsnxe); 1446 1447 if (network_handshake_setup(network, bss, hs) < 0) 1448 goto not_supported; 1449 1450 vendor_ies = network_info_get_extra_ies(info, bss, &iov_elems); 1451 handshake_state_set_vendor_ies(hs, vendor_ies, iov_elems); 1452 1453 handshake_state_set_vendor_quirks(hs, bss->vendor_quirks); 1454 1455 /* 1456 * It can't hurt to try the FILS IP Address Assignment independent of 1457 * which auth-proto is actually used. 1458 */ 1459 if (station->netconfig && netconfig_get_fils_ip_req(station->netconfig, 1460 &fils_ip_req)) { 1461 hs->fils_ip_req_ie = l_malloc(32); 1462 ie_build_fils_ip_addr_request(&fils_ip_req, hs->fils_ip_req_ie); 1463 } 1464 1465 return hs; 1466 1467 not_supported: 1468 handshake_state_unref(hs); 1469 return NULL; 1470 } 1471 1472 static bool new_scan_results(int err, struct l_queue *bss_list, 1473 const struct scan_freq_set *freqs, 1474 void *userdata) 1475 { 1476 struct station *station = userdata; 1477 1478 station_property_set_scanning(station, false); 1479 1480 if (err) 1481 return false; 1482 1483 station_set_scan_results(station, bss_list, freqs, false); 1484 1485 station_process_owe_transition_networks(station); 1486 1487 station->autoconnect_can_start = true; 1488 station_autoconnect_start(station); 1489 1490 return true; 1491 } 1492 1493 static void periodic_scan_trigger(int err, void *user_data) 1494 { 1495 struct station *station = user_data; 1496 1497 station_property_set_scanning(station, true); 1498 } 1499 1500 static void periodic_scan_stop(struct station *station) 1501 { 1502 uint64_t id = netdev_get_wdev_id(station->netdev); 1503 1504 if (scan_periodic_stop(id)) 1505 station_property_set_scanning(station, false); 1506 } 1507 1508 static bool station_needs_hidden_network_scan(struct station *station) 1509 { 1510 if (!known_networks_has_hidden()) 1511 return false; 1512 1513 if (station_is_autoconnecting(station)) 1514 return true; 1515 1516 return !l_queue_isempty(station->hidden_bss_list_sorted); 1517 } 1518 1519 static struct scan_freq_set *station_get_allowed_freqs(struct station *station) 1520 { 1521 const struct scan_freq_set *supported = 1522 wiphy_get_supported_freqs(station->wiphy); 1523 struct scan_freq_set *allowed = scan_freq_set_clone(supported, 1524 allowed_bands); 1525 1526 if (scan_freq_set_isempty(allowed)) { 1527 scan_freq_set_free(allowed); 1528 allowed = NULL; 1529 } 1530 1531 return allowed; 1532 } 1533 1534 static uint32_t station_scan_trigger(struct station *station, 1535 struct scan_freq_set *freqs, 1536 scan_trigger_func_t triggered, 1537 scan_notify_func_t notify, 1538 scan_destroy_func_t destroy) 1539 { 1540 uint64_t id = netdev_get_wdev_id(station->netdev); 1541 struct scan_parameters params; 1542 1543 memset(¶ms, 0, sizeof(params)); 1544 params.flush = true; 1545 params.freqs = freqs; 1546 1547 if (wiphy_can_randomize_mac_addr(station->wiphy) || 1548 station->connected_bss || 1549 station_needs_hidden_network_scan(station)) { 1550 /* If we're connected, HW cannot randomize our MAC */ 1551 if (!station->connected_bss) 1552 params.randomize_mac_addr_hint = true; 1553 1554 return scan_active_full(id, ¶ms, triggered, notify, 1555 station, destroy); 1556 } 1557 1558 return scan_passive_full(id, ¶ms, triggered, notify, 1559 station, destroy); 1560 } 1561 1562 static bool station_quick_scan_results(int err, struct l_queue *bss_list, 1563 const struct scan_freq_set *freqs, 1564 void *userdata) 1565 { 1566 struct station *station = userdata; 1567 1568 station_property_set_scanning(station, false); 1569 1570 if (err) 1571 goto done; 1572 1573 station_set_scan_results(station, bss_list, freqs, false); 1574 1575 station_process_owe_transition_networks(station); 1576 1577 station->autoconnect_can_start = true; 1578 station_autoconnect_start(station); 1579 1580 done: 1581 if (station->state == STATION_STATE_AUTOCONNECT_QUICK) 1582 /* 1583 * If we're still in AUTOCONNECT_QUICK state, then autoconnect 1584 * failed to find any candidates. Transition to AUTOCONNECT_FULL 1585 */ 1586 station_enter_state(station, STATION_STATE_AUTOCONNECT_FULL); 1587 1588 return err == 0; 1589 } 1590 1591 static void station_quick_scan_triggered(int err, void *user_data) 1592 { 1593 struct station *station = user_data; 1594 1595 if (err < 0) { 1596 l_debug("Quick scan trigger failed: %i", err); 1597 1598 station_enter_state(station, STATION_STATE_AUTOCONNECT_FULL); 1599 1600 return; 1601 } 1602 1603 l_debug("Quick scan triggered for %s", 1604 netdev_get_name(station->netdev)); 1605 1606 station_property_set_scanning(station, true); 1607 } 1608 1609 static void station_quick_scan_destroy(void *userdata) 1610 { 1611 struct station *station = userdata; 1612 1613 station->quick_scan_id = 0; 1614 } 1615 1616 static int station_quick_scan_trigger(struct station *station) 1617 { 1618 _auto_(scan_freq_set_free) struct scan_freq_set *known_freq_set = NULL; 1619 _auto_(scan_freq_set_free) struct scan_freq_set *allowed = NULL; 1620 bool known_6ghz; 1621 1622 if (wiphy_regdom_is_updating(station->wiphy)) { 1623 l_debug("regdom is updating, delaying quick scan"); 1624 return -EAGAIN; 1625 } 1626 1627 known_freq_set = known_networks_get_recent_frequencies( 1628 STATION_RECENT_NETWORK_LIMIT, 1629 STATION_RECENT_FREQS_LIMIT); 1630 if (!known_freq_set) 1631 return -ENODATA; 1632 1633 known_6ghz = scan_freq_set_get_bands(known_freq_set) & BAND_FREQ_6_GHZ; 1634 1635 /* 1636 * This means IWD has previously connected to a 6GHz AP before, but now 1637 * the regulatory domain disallows 6GHz likely caused by a reboot, the 1638 * firmware going down, or a regulatory update. The only way to 1639 * re-enable 6GHz is to get enough beacons via scanning for the firmware 1640 * to set the regulatory domain. A quick scan is very unlikely to do 1641 * this since its so limited, so return an error which will fall back to 1642 * full autoconnect. 1643 */ 1644 if (wiphy_band_is_disabled(station->wiphy, BAND_FREQ_6_GHZ) == 1 && 1645 wiphy_country_is_unknown(station->wiphy) && 1646 known_6ghz) 1647 return -ENOTSUP; 1648 1649 allowed = station_get_allowed_freqs(station); 1650 if (L_WARN_ON(!allowed)) 1651 return -ENOTSUP; 1652 1653 scan_freq_set_constrain(known_freq_set, allowed); 1654 1655 if (scan_freq_set_isempty(known_freq_set)) 1656 return -ENOTSUP; 1657 1658 station->quick_scan_id = station_scan_trigger(station, 1659 known_freq_set, 1660 station_quick_scan_triggered, 1661 station_quick_scan_results, 1662 station_quick_scan_destroy); 1663 if (!station->quick_scan_id) 1664 return -EIO; 1665 1666 return 0; 1667 } 1668 1669 static const char *station_state_to_string(enum station_state state) 1670 { 1671 switch (state) { 1672 case STATION_STATE_DISCONNECTED: 1673 return "disconnected"; 1674 case STATION_STATE_AUTOCONNECT_QUICK: 1675 return "autoconnect_quick"; 1676 case STATION_STATE_AUTOCONNECT_FULL: 1677 return "autoconnect_full"; 1678 case STATION_STATE_CONNECTING: 1679 return "connecting"; 1680 case STATION_STATE_CONNECTING_AUTO: 1681 return "connecting (auto)"; 1682 case STATION_STATE_CONNECTED: 1683 return "connected"; 1684 case STATION_STATE_DISCONNECTING: 1685 return "disconnecting"; 1686 case STATION_STATE_ROAMING: 1687 return "roaming"; 1688 case STATION_STATE_FT_ROAMING: 1689 return "ft-roaming"; 1690 case STATION_STATE_FW_ROAMING: 1691 return "fw-roaming"; 1692 case STATION_STATE_NETCONFIG: 1693 return "connecting (netconfig)"; 1694 } 1695 1696 return "invalid"; 1697 } 1698 1699 static void station_set_evict_nocarrier(struct station *station, bool value) 1700 { 1701 char *v = value ? "1" : "0"; 1702 1703 if (supports_arp_evict_nocarrier) 1704 sysfs_write_ipv4_setting(netdev_get_name(station->netdev), 1705 "arp_evict_nocarrier", v); 1706 1707 if (supports_ndisc_evict_nocarrier) 1708 sysfs_write_ipv6_setting(netdev_get_name(station->netdev), 1709 "ndisc_evict_nocarrier", v); 1710 } 1711 1712 /* 1713 * Handles dropping ARP (IPv4) and neighbor advertisements (IPv6) settings. 1714 */ 1715 static void station_set_drop_neighbor_discovery(struct station *station, 1716 bool value) 1717 { 1718 char *v = value ? "1" : "0"; 1719 1720 if (supports_drop_gratuitous_arp) 1721 sysfs_write_ipv4_setting(netdev_get_name(station->netdev), 1722 "drop_gratuitous_arp", v); 1723 1724 if (supports_drop_unsolicited_na) 1725 sysfs_write_ipv6_setting(netdev_get_name(station->netdev), 1726 "drop_unsolicited_na", v); 1727 } 1728 1729 static void station_set_drop_unicast_l2_multicast(struct station *station, 1730 bool value) 1731 { 1732 char *v = value ? "1" : "0"; 1733 1734 if (supports_ipv4_drop_unicast_in_l2_multicast) 1735 sysfs_write_ipv4_setting(netdev_get_name(station->netdev), 1736 "drop_unicast_in_l2_multicast", v); 1737 1738 if (supports_ipv6_drop_unicast_in_l2_multicast) 1739 sysfs_write_ipv6_setting(netdev_get_name(station->netdev), 1740 "drop_unicast_in_l2_multicast", v); 1741 } 1742 1743 static void station_signal_agent_notify(struct station *station); 1744 1745 static void station_enter_state(struct station *station, 1746 enum station_state state) 1747 { 1748 uint64_t id = netdev_get_wdev_id(station->netdev); 1749 #ifdef HAVE_DBUS 1750 struct l_dbus *dbus = dbus_get_bus(); 1751 #endif 1752 bool disconnected; 1753 int ret; 1754 1755 iwd_notice(IWD_NOTICE_STATE, "old: %s, new: %s", 1756 station_state_to_string(station->state), 1757 station_state_to_string(state)); 1758 1759 #ifdef HAVE_DBUS 1760 station_debug_event(station, station_state_to_string(state)); 1761 1762 disconnected = !station_is_busy(station); 1763 1764 if ((disconnected && state > STATION_STATE_AUTOCONNECT_FULL) || 1765 (!disconnected && state != station->state)) 1766 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 1767 IWD_STATION_INTERFACE, "State"); 1768 #endif 1769 1770 station->state = state; 1771 1772 switch (state) { 1773 case STATION_STATE_AUTOCONNECT_QUICK: 1774 ret = station_quick_scan_trigger(station); 1775 if (ret == 0 || ret == -EAGAIN) 1776 break; 1777 1778 station->state = STATION_STATE_AUTOCONNECT_FULL; 1779 /* Fall through */ 1780 case STATION_STATE_AUTOCONNECT_FULL: 1781 scan_periodic_start(id, periodic_scan_trigger, 1782 new_scan_results, station); 1783 break; 1784 case STATION_STATE_CONNECTING: 1785 case STATION_STATE_CONNECTING_AUTO: 1786 /* Refresh the ordered network list */ 1787 network_rank_update(station->connected_network, true); 1788 l_queue_remove(station->networks_sorted, 1789 station->connected_network); 1790 l_queue_insert(station->networks_sorted, 1791 station->connected_network, 1792 network_rank_compare, NULL); 1793 1794 #ifdef HAVE_DBUS 1795 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 1796 IWD_STATION_INTERFACE, "ConnectedNetwork"); 1797 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 1798 IWD_STATION_INTERFACE, "ConnectedAccessPoint"); 1799 l_dbus_property_changed(dbus, 1800 network_get_path(station->connected_network), 1801 IWD_NETWORK_INTERFACE, "Connected"); 1802 #endif 1803 1804 if (station->signal_agent) 1805 station_signal_agent_notify(station); 1806 1807 periodic_scan_stop(station); 1808 break; 1809 case STATION_STATE_CONNECTED: 1810 network_clear_blacklist(station->connected_network); 1811 #ifdef HAVE_DBUS 1812 if (station->connect_pending) { 1813 struct l_dbus_message *reply = 1814 l_dbus_message_new_method_return( 1815 station->connect_pending); 1816 dbus_pending_reply(&station->connect_pending, reply); 1817 } 1818 1819 l_dbus_object_add_interface(dbus, 1820 netdev_get_path(station->netdev), 1821 IWD_STATION_DIAGNOSTIC_INTERFACE, 1822 station); 1823 #endif 1824 periodic_scan_stop(station); 1825 1826 station_set_evict_nocarrier(station, true); 1827 1828 /* 1829 * Hotspot Specification 2.0 - Section 6.5 1830 * 1831 * " - Shall drop all received {gratuitous ARP, unsolicited 1832 * Neighbor Advertisement} messages when the Proxy ARP field 1833 * is set to 1 in the Extended Capabilities element of the 1834 * serving AP. 1835 * 1836 * - When the serving AP transmits frames containing an HS2.0 1837 * Indication element in which the value of the DGAF Disable 1838 * bit subfield is set to 0, the mobile device should 1839 * discard all received unicast IP packets that were 1840 * decrypted using the GTK" 1841 */ 1842 if (station->connected_bss->proxy_arp) 1843 station_set_drop_neighbor_discovery(station, true); 1844 if (station->connected_bss->hs20_dgaf_disable) 1845 station_set_drop_unicast_l2_multicast(station, true); 1846 1847 break; 1848 case STATION_STATE_DISCONNECTED: 1849 periodic_scan_stop(station); 1850 1851 station_set_evict_nocarrier(station, true); 1852 station_set_drop_neighbor_discovery(station, false); 1853 station_set_drop_unicast_l2_multicast(station, false); 1854 1855 if (station->affinity_watch) { 1856 l_dbus_remove_watch(dbus_get_bus(), 1857 station->affinity_watch); 1858 station->affinity_watch = 0; 1859 } 1860 1861 if (station->hs) { 1862 handshake_state_unref(station->hs); 1863 station->hs = NULL; 1864 } 1865 1866 break; 1867 case STATION_STATE_DISCONNECTING: 1868 case STATION_STATE_NETCONFIG: 1869 break; 1870 case STATION_STATE_ROAMING: 1871 case STATION_STATE_FT_ROAMING: 1872 case STATION_STATE_FW_ROAMING: 1873 #ifdef HAVE_DBUS 1874 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 1875 IWD_STATION_INTERFACE, "ConnectedAccessPoint"); 1876 #endif 1877 1878 if (station->affinity_watch) { 1879 l_dbus_remove_watch(dbus_get_bus(), 1880 station->affinity_watch); 1881 station->affinity_watch = 0; 1882 } 1883 1884 station_set_evict_nocarrier(station, false); 1885 break; 1886 } 1887 1888 WATCHLIST_NOTIFY(&station->state_watches, 1889 station_state_watch_func_t, station->state); 1890 } 1891 1892 enum station_state station_get_state(struct station *station) 1893 { 1894 return station->state; 1895 } 1896 1897 uint32_t station_add_state_watch(struct station *station, 1898 station_state_watch_func_t func, 1899 void *user_data, 1900 station_destroy_func_t destroy) 1901 { 1902 return watchlist_add(&station->state_watches, func, user_data, destroy); 1903 } 1904 1905 bool station_remove_state_watch(struct station *station, uint32_t id) 1906 { 1907 return watchlist_remove(&station->state_watches, id); 1908 } 1909 1910 uint32_t station_add_event_watch(station_event_watch_func_t func, 1911 void *user_data, 1912 station_destroy_func_t destroy) 1913 { 1914 return watchlist_add(&event_watches, func, user_data, destroy); 1915 } 1916 1917 void station_remove_event_watch(uint32_t id) 1918 { 1919 watchlist_remove(&event_watches, id); 1920 } 1921 1922 bool station_set_autoconnect(struct station *station, bool autoconnect) 1923 { 1924 if (station->autoconnect == autoconnect) 1925 return true; 1926 1927 station->autoconnect = autoconnect; 1928 1929 if (station->state == STATION_STATE_DISCONNECTED && autoconnect) 1930 station_enter_state(station, STATION_STATE_AUTOCONNECT_QUICK); 1931 1932 if (station_is_autoconnecting(station) && !autoconnect) 1933 station_enter_state(station, STATION_STATE_DISCONNECTED); 1934 1935 if (iwd_is_developer_mode()) 1936 l_dbus_property_changed(dbus_get_bus(), 1937 netdev_get_path(station->netdev), 1938 IWD_STATION_DEBUG_INTERFACE, "AutoConnect"); 1939 1940 return true; 1941 } 1942 1943 bool station_get_autoconnect(struct station *station) 1944 { 1945 return station->autoconnect; 1946 } 1947 1948 static void station_roam_state_clear(struct station *station) 1949 { 1950 l_debug("%u", netdev_get_ifindex(station->netdev)); 1951 1952 l_timeout_remove(station->roam_trigger_timeout); 1953 station->roam_trigger_timeout = NULL; 1954 station->preparing_roam = false; 1955 station->roam_scan_full = false; 1956 station->signal_low = false; 1957 station->netconfig_after_roam = false; 1958 station->last_roam_scan = 0; 1959 1960 if (station->roam_scan_id) 1961 scan_cancel(netdev_get_wdev_id(station->netdev), 1962 station->roam_scan_id); 1963 1964 if (station->roam_freqs) { 1965 scan_freq_set_free(station->roam_freqs); 1966 station->roam_freqs = NULL; 1967 } 1968 1969 l_queue_clear(station->roam_bss_list, l_free); 1970 1971 ft_clear_authentications(netdev_get_ifindex(station->netdev)); 1972 1973 if (station->ft_work.id) 1974 wiphy_radio_work_done(station->wiphy, station->ft_work.id); 1975 } 1976 1977 static void station_reset_connection_state(struct station *station) 1978 { 1979 struct network *network = station->connected_network; 1980 #ifdef HAVE_DBUS 1981 struct l_dbus *dbus = dbus_get_bus(); 1982 #endif 1983 1984 l_debug("%u", netdev_get_ifindex(station->netdev)); 1985 1986 if (!network) 1987 return; 1988 1989 station_roam_state_clear(station); 1990 1991 if (station->netconfig) 1992 netconfig_reset(station->netconfig); 1993 1994 /* Refresh the ordered network list */ 1995 network_rank_update(station->connected_network, false); 1996 l_queue_remove(station->networks_sorted, station->connected_network); 1997 l_queue_insert(station->networks_sorted, station->connected_network, 1998 network_rank_compare, NULL); 1999 2000 station->connected_bss = NULL; 2001 station->connected_network = NULL; 2002 2003 #ifdef HAVE_DBUS 2004 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 2005 IWD_STATION_INTERFACE, "ConnectedNetwork"); 2006 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 2007 IWD_STATION_INTERFACE, "ConnectedAccessPoint"); 2008 l_dbus_property_changed(dbus, network_get_path(network), 2009 IWD_NETWORK_INTERFACE, "Connected"); 2010 l_dbus_object_remove_interface(dbus, netdev_get_path(station->netdev), 2011 IWD_STATION_DIAGNOSTIC_INTERFACE); 2012 #endif 2013 2014 /* 2015 * Perform this step last since calling network_disconnected() might 2016 * result in the removal of the network (for example if provisioning 2017 * a new hidden network fails with an incorrect password). 2018 */ 2019 if (station->state == STATION_STATE_CONNECTED || 2020 station->state == STATION_STATE_CONNECTING || 2021 station->state == STATION_STATE_CONNECTING_AUTO || 2022 station->state == STATION_STATE_NETCONFIG || 2023 station_is_roaming(station)) 2024 network_disconnected(network); 2025 } 2026 2027 static void station_disassociated(struct station *station) 2028 { 2029 l_debug("%u", netdev_get_ifindex(station->netdev)); 2030 2031 station_reset_connection_state(station); 2032 2033 station_enter_state(station, STATION_STATE_DISCONNECTED); 2034 2035 if (station->autoconnect) 2036 station_enter_state(station, STATION_STATE_AUTOCONNECT_QUICK); 2037 } 2038 2039 static void station_roam_timeout_rearm(struct station *station, int seconds); 2040 static int station_roam_scan(struct station *station, 2041 struct scan_freq_set *freq_set); 2042 2043 static bool is_freq_band_supported(struct station *station, uint32_t freq, 2044 enum band_freq band) 2045 { 2046 const struct band_freq_attrs *attr; 2047 2048 if (!(band & wiphy_get_supported_bands(station->wiphy))) 2049 return false; 2050 2051 /* Skip if frequency is not supported or disabled */ 2052 attr = wiphy_get_frequency_info(station->wiphy, freq); 2053 if (!attr || attr->disabled) 2054 return false; 2055 2056 return true; 2057 } 2058 2059 /* 2060 * Some Cisco APs report all operating class values as 0 in the Neighbor 2061 * Report Responses. Historically we could be very sure what band the 2062 * channel was associated with since there was no overlap between 2.4 2063 * and 5GHz. With the addition of 6GHz this becomes more difficult as 2064 * the 6GHz channel range overlaps both 2.4 and 5ghz. 2065 * 2066 * To cover all 3 bands, calculate a frequency for each and see what sticks. 2067 */ 2068 static void station_parse_zero_oper_class(struct station *station, 2069 struct ie_neighbor_report_info *info, 2070 struct scan_freq_set *freq_set) 2071 { 2072 uint32_t freq_2g; 2073 uint32_t freq_5g; 2074 uint32_t freq_6g; 2075 2076 freq_2g = band_channel_to_freq(info->channel_num, BAND_FREQ_2_4_GHZ); 2077 freq_5g = band_channel_to_freq(info->channel_num, BAND_FREQ_5_GHZ); 2078 freq_6g = band_channel_to_freq(info->channel_num, BAND_FREQ_6_GHZ); 2079 2080 if (!freq_2g && !freq_5g && !freq_6g) { 2081 l_debug("Ignored: 0 oper class with an unusual " 2082 "channel number %u", info->channel_num); 2083 return; 2084 } 2085 2086 if (freq_2g && is_freq_band_supported(station, freq_2g, 2087 BAND_FREQ_2_4_GHZ)) { 2088 l_debug("adding frequency %u from invalid oper class", freq_2g); 2089 scan_freq_set_add(freq_set, freq_2g); 2090 } 2091 2092 if (freq_5g && is_freq_band_supported(station, freq_5g, 2093 BAND_FREQ_5_GHZ)) { 2094 l_debug("adding frequency %u from invalid oper class", freq_5g); 2095 scan_freq_set_add(freq_set, freq_5g); 2096 } 2097 2098 if (freq_6g && is_freq_band_supported(station, freq_6g, 2099 BAND_FREQ_6_GHZ)) { 2100 l_debug("adding frequency %u from invalid oper class", freq_6g); 2101 scan_freq_set_add(freq_set, freq_6g); 2102 } 2103 } 2104 2105 static void station_add_neighbor_report_freqs(struct station *station, 2106 struct ie_neighbor_report_info *info, 2107 struct scan_freq_set *freq_set) 2108 { 2109 enum band_freq band; 2110 uint32_t freq; 2111 2112 const uint8_t *country = station->connected_bss->cc_present ? 2113 station->connected_bss->cc : NULL; 2114 2115 if (info->oper_class == 0) { 2116 station_parse_zero_oper_class(station, info, freq_set); 2117 return; 2118 } else { 2119 band = band_oper_class_to_band(country, info->oper_class); 2120 if (!band) { 2121 l_debug("Ignored: unsupported oper class"); 2122 2123 return; 2124 } 2125 } 2126 2127 freq = band_channel_to_freq(info->channel_num, band); 2128 if (!freq) { 2129 l_debug("Ignored: unsupported channel"); 2130 2131 return; 2132 } 2133 2134 /* Skip if the band/frequency is not supported */ 2135 if (!is_freq_band_supported(station, freq, band)) 2136 return; 2137 2138 scan_freq_set_add(freq_set, freq); 2139 } 2140 2141 static void parse_neighbor_report(struct station *station, 2142 const uint8_t *reports, 2143 size_t reports_len, 2144 struct scan_freq_set **set) 2145 { 2146 struct ie_tlv_iter iter; 2147 struct scan_freq_set *freq_set_md, *freq_set_no_md; 2148 uint32_t current_freq = station->connected_bss->frequency; 2149 struct handshake_state *hs = netdev_get_handshake(station->netdev); 2150 2151 freq_set_md = scan_freq_set_new(); 2152 freq_set_no_md = scan_freq_set_new(); 2153 2154 ie_tlv_iter_init(&iter, reports, reports_len); 2155 2156 /* First see if any of the reports contain the MD bit set */ 2157 while (ie_tlv_iter_next(&iter)) { 2158 struct ie_neighbor_report_info info; 2159 2160 if (ie_tlv_iter_get_tag(&iter) != IE_TYPE_NEIGHBOR_REPORT) 2161 continue; 2162 2163 if (ie_parse_neighbor_report(&iter, &info) < 0) 2164 continue; 2165 2166 l_debug("Neighbor report received for %s: ch %i " 2167 "(oper class %i), %s", 2168 util_address_to_string(info.addr), 2169 (int) info.channel_num, (int) info.oper_class, 2170 info.md ? "MD set" : "MD not set"); 2171 2172 if (!memcmp(info.addr, 2173 station->connected_bss->addr, ETH_ALEN)) { 2174 /* 2175 * If this report is for the current AP, don't add 2176 * it to any of the lists yet. We will need to scan 2177 * its channel because it may still be the best ranked 2178 * or the only visible AP. 2179 */ 2180 continue; 2181 } 2182 2183 station_add_neighbor_report_freqs(station, &info, 2184 info.md && hs->mde ? 2185 freq_set_md : freq_set_no_md); 2186 } 2187 2188 /* 2189 * If there are neighbor reports with the MD bit set then the bit 2190 * is probably valid so scan only the frequencies of the neighbors 2191 * with that bit set, which will allow us to use Fast Transition. 2192 * Some APs, such as those based on hostapd do not set the MD bit 2193 * even if the neighbor is within the MD. 2194 * 2195 * In any case we only select the frequencies here and will check 2196 * the IEs in the scan results as the authoritative information 2197 * on whether we can use Fast Transition, and rank BSSes based on 2198 * that. 2199 * 2200 * TODO: possibly save the neighbors from outside the MD and if 2201 * none of the ones in the MD end up working, try a non-FT 2202 * transition to those neighbors. We should be using a 2203 * blacklisting mechanism (for both initial connection and 2204 * transitions) so that cound_md would not count the 2205 * BSSes already used and when it goes down to 0 we'd 2206 * automatically fall back to the non-FT candidates and then to 2207 * full scan. 2208 */ 2209 if (!scan_freq_set_isempty(freq_set_md)) { 2210 scan_freq_set_add(freq_set_md, current_freq); 2211 *set = freq_set_md; 2212 scan_freq_set_free(freq_set_no_md); 2213 } else if (!scan_freq_set_isempty(freq_set_no_md)) { 2214 scan_freq_set_add(freq_set_no_md, current_freq); 2215 *set = freq_set_no_md; 2216 scan_freq_set_free(freq_set_md); 2217 } else { 2218 scan_freq_set_free(freq_set_no_md); 2219 scan_freq_set_free(freq_set_md); 2220 *set = NULL; 2221 } 2222 } 2223 2224 static void station_early_neighbor_report_cb(struct netdev *netdev, int err, 2225 const uint8_t *reports, 2226 size_t reports_len, 2227 void *user_data) 2228 { 2229 struct station *station = user_data; 2230 2231 if (err == -ENODEV) 2232 return; 2233 2234 l_debug("ifindex: %u, error: %d(%s)", 2235 netdev_get_ifindex(station->netdev), 2236 err, err < 0 ? strerror(-err) : ""); 2237 2238 if (!reports || err) 2239 return; 2240 2241 parse_neighbor_report(station, reports, reports_len, 2242 &station->roam_freqs); 2243 } 2244 2245 static bool station_try_next_bss(struct station *station) 2246 { 2247 struct scan_bss *next; 2248 int ret; 2249 2250 next = network_bss_select(station->connected_network, false); 2251 2252 if (!next) 2253 return false; 2254 2255 ret = __station_connect_network(station, station->connected_network, 2256 next, station->state); 2257 if (ret < 0) 2258 return false; 2259 2260 l_debug("Attempting to connect to next BSS "MAC, MAC_STR(next->addr)); 2261 2262 return true; 2263 } 2264 2265 static bool station_can_fast_transition(struct station *station, 2266 struct handshake_state *hs, 2267 struct scan_bss *bss) 2268 { 2269 uint16_t mdid; 2270 2271 if (!hs->mde) 2272 return false; 2273 2274 if (ie_parse_mobility_domain_from_data(hs->mde, hs->mde[1] + 2, 2275 &mdid, NULL, NULL) < 0) 2276 return false; 2277 2278 if (!(bss->mde_present && l_get_le16(bss->mde) == mdid)) 2279 return false; 2280 2281 if (hs->supplicant_ie != NULL) { 2282 struct ie_rsn_info rsn_info; 2283 2284 if (!IE_AKM_IS_FT(hs->akm_suite)) 2285 return false; 2286 2287 if (scan_bss_get_rsn_info(bss, &rsn_info) < 0) 2288 return false; 2289 2290 if (!IE_AKM_IS_FT(rsn_info.akm_suites)) 2291 return false; 2292 } 2293 2294 /* 2295 * FT-over-Air in its current form relies on CMD_REMAIN_ON_CHANNEL. Some 2296 * drivers do not support this so only allow over-DS if this is the case 2297 */ 2298 if (!(hs->mde[4] & 1) && 2299 !wiphy_supports_cmd_offchannel(station->wiphy)) { 2300 l_debug("FT-over-Air needs offchannel, using reassociation"); 2301 return false; 2302 } 2303 2304 return true; 2305 } 2306 2307 static void station_disconnect_on_netconfig_failed(struct netdev *netdev, 2308 bool success, 2309 void *user_data) 2310 { 2311 struct station *station = user_data; 2312 2313 if (station_try_next_bss(station)) 2314 return; 2315 2316 if (station->connect_pending) { 2317 struct l_dbus_message *reply = dbus_error_failed( 2318 station->connect_pending); 2319 2320 dbus_pending_reply(&station->connect_pending, reply); 2321 } 2322 2323 station_reset_connection_state(station); 2324 2325 station_enter_state(station, STATION_STATE_DISCONNECTED); 2326 station_enter_state(station, STATION_STATE_AUTOCONNECT_FULL); 2327 } 2328 2329 static void station_netconfig_event_handler(enum netconfig_event event, 2330 void *user_data) 2331 { 2332 struct station *station = user_data; 2333 2334 switch (event) { 2335 case NETCONFIG_EVENT_CONNECTED: 2336 station_enter_state(station, STATION_STATE_CONNECTED); 2337 break; 2338 case NETCONFIG_EVENT_FAILED: 2339 station_debug_event(station, "netconfig-failed"); 2340 2341 netconfig_reset(station->netconfig); 2342 2343 if (station->state == STATION_STATE_NETCONFIG) 2344 network_connect_failed(station->connected_network, 2345 false); 2346 2347 network_blacklist_add(station->connected_network, 2348 station->connected_bss); 2349 2350 netdev_disconnect(station->netdev, 2351 station_disconnect_on_netconfig_failed, 2352 station); 2353 break; 2354 default: 2355 l_error("station: Unsupported netconfig event: %d.", event); 2356 break; 2357 } 2358 } 2359 2360 static bool netconfig_after_roam(struct station *station) 2361 { 2362 const struct network *network = station_get_connected_network(station); 2363 2364 /* Netconfig was reset which frees all settings, reload now */ 2365 if (!netconfig_load_settings(station->netconfig, 2366 network_get_settings(network))) 2367 return false; 2368 2369 if (!netconfig_configure(station->netconfig, 2370 station_netconfig_event_handler, 2371 station)) 2372 return false; 2373 2374 station_enter_state(station, STATION_STATE_NETCONFIG); 2375 2376 return true; 2377 } 2378 2379 static void station_roamed(struct station *station) 2380 { 2381 station->roam_scan_full = false; 2382 2383 /* 2384 * Schedule another roaming attempt in case the signal continues to 2385 * remain low. A subsequent high signal notification will cancel it. 2386 */ 2387 if (station->signal_low) 2388 station_roam_timeout_rearm(station, roam_retry_interval); 2389 2390 if (station->netconfig) 2391 netconfig_reconfigure(station->netconfig, 2392 !supports_arp_evict_nocarrier); 2393 2394 if (station->roam_freqs) { 2395 scan_freq_set_free(station->roam_freqs); 2396 station->roam_freqs = NULL; 2397 } 2398 2399 if (station->connected_bss->cap_rm_neighbor_report) { 2400 if (netdev_neighbor_report_req(station->netdev, 2401 station_early_neighbor_report_cb) < 0) 2402 l_warn("Could not request neighbor report"); 2403 } 2404 2405 l_queue_clear(station->roam_bss_list, l_free); 2406 2407 /* Re-enable netconfig if it never finished on the last BSS */ 2408 if (station->netconfig_after_roam) { 2409 station->netconfig_after_roam = false; 2410 L_WARN_ON(!netconfig_after_roam(station)); 2411 } else 2412 station_enter_state(station, STATION_STATE_CONNECTED); 2413 } 2414 2415 static void station_roam_retry(struct station *station) 2416 { 2417 /* 2418 * If we're still connected to the old BSS, only clear preparing_roam 2419 * and reattempt in 60 seconds if signal level is still low at that 2420 * time. 2421 */ 2422 station->preparing_roam = false; 2423 station->roam_scan_full = false; 2424 station->ap_directed_roaming = false; 2425 2426 if (station->roam_freqs) { 2427 scan_freq_set_free(station->roam_freqs); 2428 station->roam_freqs = NULL; 2429 } 2430 2431 if (station->signal_low) 2432 station_roam_timeout_rearm(station, roam_retry_interval); 2433 } 2434 2435 static void station_roam_failed(struct station *station) 2436 { 2437 l_debug("%u", netdev_get_ifindex(station->netdev)); 2438 2439 l_queue_clear(station->roam_bss_list, l_free); 2440 2441 /* 2442 * If we attempted a reassociation or a fast transition, and ended up 2443 * here then we are now disconnected. 2444 */ 2445 if (station_is_roaming(station)) { 2446 station_disassociated(station); 2447 return; 2448 } 2449 2450 /* Re-enable netconfig if needed, even on a failed roam */ 2451 if (station->netconfig_after_roam) { 2452 station->netconfig_after_roam = false; 2453 L_WARN_ON(!netconfig_after_roam(station)); 2454 } 2455 2456 /* 2457 * We were told by the AP to roam, but failed. Try ourselves or 2458 * wait for the AP to tell us to roam again 2459 */ 2460 if (station->ap_directed_roaming) { 2461 /* 2462 * The candidate list from the AP (or neighbor report) found 2463 * no BSS's. Force a full scan 2464 */ 2465 if (!station->roam_scan_full) 2466 goto full_scan; 2467 2468 goto delayed_retry; 2469 } 2470 2471 /* 2472 * If we tried a limited scan, failed and the signal is still low, 2473 * repeat with a full scan right away 2474 */ 2475 if (station->signal_low && !station->roam_scan_full) { 2476 /* 2477 * Since we're re-using roam_scan_id, explicitly cancel 2478 * the scan here, so that the destroy callback is not called 2479 * after the return of this function 2480 */ 2481 full_scan: 2482 scan_cancel(netdev_get_wdev_id(station->netdev), 2483 station->roam_scan_id); 2484 2485 if (!station_roam_scan(station, NULL)) 2486 return; 2487 } 2488 2489 delayed_retry: 2490 station_roam_retry(station); 2491 } 2492 2493 static void station_reassociate_cb(struct netdev *netdev, 2494 enum netdev_result result, 2495 void *event_data, 2496 void *user_data) 2497 { 2498 struct station *station = user_data; 2499 2500 l_debug("%u, result: %d", netdev_get_ifindex(station->netdev), result); 2501 2502 if (station->state != STATION_STATE_ROAMING && 2503 station->state != STATION_STATE_FT_ROAMING) 2504 return; 2505 2506 if (result == NETDEV_RESULT_OK) 2507 station_roamed(station); 2508 else 2509 station_roam_failed(station); 2510 } 2511 2512 static void station_netdev_event(struct netdev *netdev, enum netdev_event event, 2513 void *event_data, void *user_data); 2514 2515 static int station_transition_reassociate(struct station *station, 2516 struct scan_bss *bss, 2517 struct handshake_state *new_hs) 2518 { 2519 int ret; 2520 2521 ret = netdev_reassociate(station->netdev, bss, station->connected_bss, 2522 new_hs, station_netdev_event, 2523 station_reassociate_cb, station); 2524 if (ret < 0) 2525 return ret; 2526 2527 l_debug(""); 2528 2529 station->connected_bss = bss; 2530 station->preparing_roam = false; 2531 station_enter_state(station, STATION_STATE_ROAMING); 2532 2533 return 0; 2534 } 2535 2536 static bool bss_match_bssid(const void *a, const void *b) 2537 { 2538 const struct scan_bss *bss = a; 2539 const uint8_t *bssid = b; 2540 2541 return !memcmp(bss->addr, bssid, sizeof(bss->addr)); 2542 } 2543 2544 static void station_preauthenticate_cb(struct netdev *netdev, 2545 enum netdev_result result, 2546 const uint8_t *pmk, void *user_data) 2547 { 2548 struct station *station = user_data; 2549 struct network *connected = station->connected_network; 2550 struct scan_bss *bss; 2551 struct handshake_state *new_hs; 2552 2553 l_debug("%u, result: %d", netdev_get_ifindex(station->netdev), result); 2554 2555 if (!station->preparing_roam || result == NETDEV_RESULT_ABORTED) 2556 return; 2557 2558 bss = network_bss_find_by_addr(station->connected_network, 2559 station->preauth_bssid); 2560 if (!bss) { 2561 l_error("Roam target BSS not found"); 2562 station_roam_failed(station); 2563 return; 2564 } 2565 2566 new_hs = station_handshake_setup(station, connected, bss); 2567 if (!new_hs) { 2568 l_error("station_handshake_setup failed"); 2569 2570 station_roam_failed(station); 2571 return; 2572 } 2573 2574 if (result == NETDEV_RESULT_OK) { 2575 uint8_t pmkid[16]; 2576 uint8_t rsne_buf[300]; 2577 struct ie_rsn_info rsn_info; 2578 2579 handshake_state_set_pmk(new_hs, pmk, 32); 2580 handshake_state_set_authenticator_address(new_hs, 2581 station->preauth_bssid); 2582 handshake_state_set_supplicant_address(new_hs, 2583 netdev_get_address(station->netdev)); 2584 2585 /* 2586 * Rebuild the RSNE to include the negotiated PMKID. Note 2587 * supplicant_ie can't be a WPA IE here, including because 2588 * the WPA IE doesn't have a capabilities field and 2589 * target_rsne->preauthentication would have been false in 2590 * station_transition_start. 2591 */ 2592 ie_parse_rsne_from_data(new_hs->supplicant_ie, 2593 new_hs->supplicant_ie[1] + 2, 2594 &rsn_info); 2595 2596 /* 2597 * IEEE 802.11 Section 12.7.1.3: 2598 * 2599 * "When the PMKID is calculated for the PMKSA as part of 2600 * preauthentication, the AKM has not yet been negotiated. 2601 * In this case, the HMAC-SHA-1 based derivation is used for 2602 * the PMKID calculation." 2603 */ 2604 handshake_state_get_pmkid(new_hs, pmkid, L_CHECKSUM_SHA1); 2605 2606 rsn_info.num_pmkids = 1; 2607 rsn_info.pmkids = pmkid; 2608 2609 ie_build_rsne(&rsn_info, rsne_buf); 2610 handshake_state_set_supplicant_ie(new_hs, rsne_buf); 2611 } 2612 2613 if (station_transition_reassociate(station, bss, new_hs) < 0) { 2614 handshake_state_unref(new_hs); 2615 station_roam_failed(station); 2616 } 2617 2618 handshake_state_unref(station->hs); 2619 station->hs = handshake_state_ref(new_hs); 2620 } 2621 2622 static void station_transition_start(struct station *station); 2623 2624 static bool station_ft_work_ready(struct wiphy_radio_work_item *item) 2625 { 2626 struct station *station = l_container_of(item, struct station, ft_work); 2627 _auto_(l_free) struct roam_bss *rbss = l_queue_pop_head( 2628 station->roam_bss_list); 2629 struct scan_bss *bss; 2630 int ret; 2631 2632 /* Very unlikely, but the BSS could have gone away */ 2633 bss = network_bss_find_by_addr(station->connected_network, rbss->addr); 2634 if (!bss) 2635 goto try_next; 2636 2637 ret = ft_handshake_setup(netdev_get_ifindex(station->netdev), 2638 bss->addr); 2639 switch (ret) { 2640 case MMPDU_STATUS_CODE_INVALID_PMKID: 2641 /* 2642 * Re-insert removing FT from the ranking (scan_bss does not 2643 * take into account FT, so we can use that rank directly). 2644 * If the BSS is still the best reassociation will be used, 2645 * otherwise we'll try more FT candidates that are better ranked 2646 */ 2647 rbss->rank = bss->rank; 2648 rbss->ft_failed = true; 2649 2650 l_debug("Re-inserting BSS "MAC" using reassociation, rank: %u", 2651 MAC_STR(rbss->addr), rbss->rank); 2652 2653 l_queue_insert(station->roam_bss_list, rbss, 2654 roam_bss_rank_compare, NULL); 2655 2656 station_debug_event(station, "ft-fallback-to-reassoc"); 2657 iwd_notice(IWD_NOTICE_FT_ROAM_FAILED, "status: %d", 2658 MMPDU_STATUS_CODE_INVALID_PMKID); 2659 2660 station_transition_start(station); 2661 l_steal_ptr(rbss); 2662 break; 2663 case -ENOENT: 2664 station_debug_event(station, "ft-roam-failed"); 2665 iwd_notice(IWD_NOTICE_FT_ROAM_FAILED, 2666 "status: authentication timeout"); 2667 try_next: 2668 station_transition_start(station); 2669 break; 2670 case 0: 2671 ret = netdev_ft_reassociate(station->netdev, bss, 2672 station->connected_bss, 2673 station_netdev_event, 2674 station_reassociate_cb, station); 2675 if (ret < 0) 2676 goto disassociate; 2677 2678 station->connected_bss = bss; 2679 station->preparing_roam = false; 2680 station_enter_state(station, STATION_STATE_FT_ROAMING); 2681 2682 break; 2683 case -EINVAL: 2684 /* 2685 * Likely an impossible situation, but since ft_handshake_setup 2686 * rederived the handshake keys we can't do anything but 2687 * disconnect. 2688 */ 2689 disassociate: 2690 station_disassociated(station); 2691 break; 2692 default: 2693 if (ret > 0) { 2694 iwd_notice(IWD_NOTICE_FT_ROAM_FAILED, "status: %d", ret); 2695 goto try_next; 2696 } 2697 2698 station_roam_failed(station); 2699 break; 2700 } 2701 2702 return true; 2703 } 2704 2705 static const struct wiphy_radio_work_item_ops ft_work_ops = { 2706 .do_work = station_ft_work_ready, 2707 }; 2708 2709 static bool station_fast_transition(struct station *station, 2710 struct scan_bss *bss) 2711 { 2712 struct handshake_state *hs = netdev_get_handshake(station->netdev); 2713 struct network *connected = station->connected_network; 2714 const struct network_info *info = network_get_info(connected); 2715 const struct iovec *vendor_ies; 2716 size_t iov_elems = 0; 2717 2718 /* Rebuild handshake RSN for target AP */ 2719 if (station_build_handshake_rsn(hs, station->wiphy, 2720 station->connected_network, bss) < 0) 2721 return false; 2722 2723 /* Reset the vendor_ies in case they're different */ 2724 vendor_ies = network_info_get_extra_ies(info, bss, &iov_elems); 2725 handshake_state_set_vendor_ies(hs, vendor_ies, iov_elems); 2726 2727 if (station->roam_trigger_timeout) { 2728 l_timeout_remove(station->roam_trigger_timeout); 2729 station->roam_trigger_timeout = NULL; 2730 } 2731 2732 /* Both ft_action/ft_authenticate will gate the associate work item */ 2733 if ((hs->mde[4] & 1)) { 2734 ft_action(netdev_get_ifindex(station->netdev), 2735 station->connected_bss->frequency, bss); 2736 goto done; 2737 } 2738 2739 station_debug_event(station, "ft-authenticating"); 2740 2741 if (station->connected_bss->frequency == bss->frequency) { 2742 ft_authenticate_onchannel(netdev_get_ifindex(station->netdev), 2743 bss); 2744 goto done; 2745 } 2746 2747 ft_authenticate(netdev_get_ifindex(station->netdev), bss); 2748 2749 done: 2750 if (station->ft_work.id) 2751 wiphy_radio_work_reschedule(station->wiphy, &station->ft_work); 2752 else 2753 wiphy_radio_work_insert(station->wiphy, &station->ft_work, 2754 WIPHY_WORK_PRIORITY_CONNECT, 2755 &ft_work_ops); 2756 2757 return true; 2758 } 2759 2760 static bool station_try_next_transition(struct station *station, 2761 struct scan_bss *bss, 2762 bool no_ft) 2763 { 2764 struct handshake_state *hs = netdev_get_handshake(station->netdev); 2765 struct network *connected = station->connected_network; 2766 enum security security = network_get_security(connected); 2767 struct handshake_state *new_hs; 2768 struct ie_rsn_info cur_rsne, target_rsne; 2769 const char *vendor_quirks = vendor_quirks_to_string(bss->vendor_quirks); 2770 2771 iwd_notice(IWD_NOTICE_ROAM_INFO, "bss: "MAC", signal: %d, load: %d/255", 2772 MAC_STR(bss->addr), 2773 bss->signal_strength / 100, 2774 bss->utilization); 2775 if (vendor_quirks) 2776 l_debug("vendor quirks for "MAC": %s", 2777 MAC_STR(bss->addr), vendor_quirks); 2778 2779 /* Reset AP roam flag, at this point the roaming behaves the same */ 2780 station->ap_directed_roaming = false; 2781 2782 /* Can we use Fast Transition? */ 2783 if (station_can_fast_transition(station, hs, bss) && !no_ft) 2784 return station_fast_transition(station, bss); 2785 2786 /* Non-FT transition */ 2787 2788 /* 2789 * FT not available, we can try preauthentication if available. 2790 * 802.11-2012 section 11.5.9.2: 2791 * "A STA shall not use preauthentication within the same mobility 2792 * domain if AKM suite type 00-0F-AC:3 or 00-0F-AC:4 is used in 2793 * the current association." 2794 */ 2795 if (security == SECURITY_8021X && 2796 scan_bss_get_rsn_info(station->connected_bss, 2797 &cur_rsne) >= 0 && 2798 scan_bss_get_rsn_info(bss, &target_rsne) >= 0 && 2799 cur_rsne.preauthentication && 2800 target_rsne.preauthentication) { 2801 /* 2802 * Both the current and the target AP support 2803 * pre-authentication and we're using 8021x authentication so 2804 * attempt to pre-authenticate and reassociate afterwards. 2805 * If the pre-authentication fails or times out we simply 2806 * won't supply any PMKID when reassociating. 2807 * Remain in the preparing_roam state. 2808 */ 2809 memcpy(station->preauth_bssid, bss->addr, ETH_ALEN); 2810 2811 if (netdev_preauthenticate(station->netdev, bss, 2812 station_preauthenticate_cb, 2813 station) >= 0) 2814 return true; 2815 } 2816 2817 new_hs = station_handshake_setup(station, connected, bss); 2818 if (!new_hs) { 2819 l_error("station_handshake_setup failed in reassociation"); 2820 return false; 2821 } 2822 2823 if (station_transition_reassociate(station, bss, new_hs) < 0) { 2824 handshake_state_unref(new_hs); 2825 return false; 2826 } 2827 2828 handshake_state_unref(station->hs); 2829 station->hs = handshake_state_ref(new_hs); 2830 2831 return true; 2832 } 2833 2834 static void station_transition_start(struct station *station) 2835 { 2836 struct roam_bss *rbss; 2837 bool roaming = false; 2838 bool connected = (station->state == STATION_STATE_CONNECTED); 2839 2840 /* 2841 * For each failed attempt pop the BSS leaving the head of the queue 2842 * with the current roam candidate. 2843 */ 2844 while ((rbss = l_queue_peek_head(station->roam_bss_list))) { 2845 struct scan_bss *bss = network_bss_find_by_addr( 2846 station->connected_network, rbss->addr); 2847 if (L_WARN_ON(!bss)) 2848 continue; 2849 2850 roaming = station_try_next_transition(station, bss, 2851 rbss->ft_failed); 2852 if (roaming) 2853 break; 2854 2855 l_queue_pop_head(station->roam_bss_list); 2856 l_free(rbss); 2857 } 2858 2859 if (!roaming) { 2860 station_roam_failed(station); 2861 return; 2862 } 2863 2864 /* 2865 * Netconfig could potentially be running and not completed yet. We 2866 * still should roam in this case but need to restart netconfig once the 2867 * roam is finished. 2868 */ 2869 if (station->netconfig && !connected) { 2870 netconfig_reset(station->netconfig); 2871 station->netconfig_after_roam = true; 2872 } 2873 } 2874 2875 static void station_roam_scan_triggered(int err, void *user_data) 2876 { 2877 struct station *station = user_data; 2878 2879 if (err) { 2880 station_roam_failed(station); 2881 return; 2882 } 2883 2884 station_debug_event(station, "roam-scan-triggered"); 2885 iwd_notice(IWD_NOTICE_ROAM_SCAN); 2886 2887 /* 2888 * Do not update the Scanning property as we won't be updating the 2889 * list of networks. 2890 */ 2891 } 2892 2893 static void station_update_roam_bss(struct station *station, 2894 struct scan_bss *bss) 2895 { 2896 struct network *network = station->connected_network; 2897 struct scan_bss *old = 2898 l_queue_remove_if(station->bss_list, bss_match, bss); 2899 2900 network_bss_update(network, bss); 2901 station_register_bss(network, bss); 2902 l_queue_push_tail(station->bss_list, bss); 2903 2904 if (old) 2905 scan_bss_free(old); 2906 } 2907 2908 static bool station_roam_scan_notify(int err, struct l_queue *bss_list, 2909 const struct scan_freq_set *freqs, 2910 void *userdata) 2911 { 2912 struct station *station = userdata; 2913 struct network *network = station->connected_network; 2914 struct handshake_state *hs = netdev_get_handshake(station->netdev); 2915 struct scan_bss *current_bss = station->connected_bss; 2916 struct scan_bss *bss; 2917 uint32_t cur_bss_group_rank = 0; 2918 static const double RANK_FT_FACTOR = 1.3; 2919 uint16_t mdid; 2920 enum security orig_security, security; 2921 2922 if (err) { 2923 station_roam_failed(station); 2924 return false; 2925 } 2926 2927 /* 2928 * Do not call station_set_scan_results because this may have been 2929 * a partial scan. We could at most update the current networks' BSS 2930 * list in its station->networks entry. 2931 */ 2932 2933 orig_security = network_get_security(network); 2934 2935 if (hs->mde) 2936 ie_parse_mobility_domain_from_data(hs->mde, hs->mde[1] + 2, 2937 &mdid, NULL, NULL); 2938 2939 /* 2940 * Find the current BSS rank, use the updated result if it exists. If 2941 * this is an AP roam keep the current rank as zero to force the roam 2942 * to occur. 2943 */ 2944 bss = l_queue_find(bss_list, bss_match_bssid, current_bss->addr); 2945 if (bss && !station->ap_directed_roaming) { 2946 double cur_bss_rank = bss->rank; 2947 2948 if (hs->mde && bss->mde_present && l_get_le16(bss->mde) == mdid) 2949 cur_bss_rank *= RANK_FT_FACTOR; 2950 2951 cur_bss_group_rank = evaluate_bss_group_rank(bss->addr, 2952 bss->frequency, 2953 bss->signal_strength, 2954 (uint16_t) cur_bss_rank); 2955 } 2956 2957 /* 2958 * BSSes in the bss_list come already ranked with their initial 2959 * association preference rank value. We only need to add preference 2960 * for BSSes that are within the FT Mobility Domain so as to favor 2961 * Fast Roaming, if it is supported. 2962 */ 2963 l_debug("Current BSS '%s' with SSID: %s", 2964 util_address_to_string(current_bss->addr), 2965 util_ssid_to_utf8(current_bss->ssid_len, current_bss->ssid)); 2966 2967 /* 2968 * Reverse now so the known frequency list gets updated in the correct 2969 * order (via network_bss_update). 2970 */ 2971 l_queue_reverse(bss_list); 2972 2973 while ((bss = l_queue_pop_head(bss_list))) { 2974 double rank; 2975 struct roam_bss *rbss; 2976 uint32_t group_rank; 2977 2978 station_print_scan_bss(bss); 2979 2980 /* Skip the BSS we are connected to */ 2981 if (!memcmp(bss->addr, station->connected_bss->addr, 6)) 2982 goto next; 2983 2984 /* Skip result if it is not part of the ESS */ 2985 if (bss->ssid_len != hs->ssid_len || 2986 memcmp(bss->ssid, hs->ssid, hs->ssid_len)) 2987 goto next; 2988 2989 if (scan_bss_get_security(bss, &security) < 0) 2990 goto next; 2991 2992 if (security != orig_security) 2993 goto next; 2994 2995 if (network_can_connect_bss(network, bss) < 0) 2996 goto next; 2997 2998 if (blacklist_contains_bss(bss->addr, 2999 BLACKLIST_REASON_CONNECT_FAILED)) 3000 goto next; 3001 3002 rank = bss->rank; 3003 3004 if (hs->mde && bss->mde_present && l_get_le16(bss->mde) == mdid) 3005 rank *= RANK_FT_FACTOR; 3006 3007 group_rank = evaluate_bss_group_rank(bss->addr, bss->frequency, 3008 bss->signal_strength, 3009 (uint16_t) rank); 3010 3011 if (group_rank <= cur_bss_group_rank) 3012 goto next; 3013 3014 /* 3015 * We need to update/add any potential roam candidate so 3016 * station/network know it exists. 3017 */ 3018 station_update_roam_bss(station, bss); 3019 3020 rbss = roam_bss_from_scan_bss(bss, group_rank); 3021 3022 l_queue_insert(station->roam_bss_list, rbss, 3023 roam_bss_rank_compare, NULL); 3024 3025 continue; 3026 3027 next: 3028 scan_bss_free(bss); 3029 } 3030 3031 l_queue_destroy(bss_list, NULL); 3032 3033 /* See if we have anywhere to roam to */ 3034 if (l_queue_isempty(station->roam_bss_list)) { 3035 station_debug_event(station, "no-roam-candidates"); 3036 goto fail; 3037 } 3038 3039 station_transition_start(station); 3040 3041 return true; 3042 3043 fail: 3044 station_roam_failed(station); 3045 3046 return true; 3047 } 3048 3049 static void station_roam_scan_destroy(void *userdata) 3050 { 3051 struct station *station = userdata; 3052 3053 station->roam_scan_id = 0; 3054 } 3055 3056 static int station_roam_scan(struct station *station, 3057 struct scan_freq_set *freq_set) 3058 { 3059 struct scan_parameters params = { .freqs = freq_set, .flush = true }; 3060 _auto_(scan_freq_set_free) struct scan_freq_set *allowed = 3061 station_get_allowed_freqs(station); 3062 3063 if (L_WARN_ON(!allowed)) 3064 return -ENOTSUP; 3065 3066 l_debug("ifindex: %u", netdev_get_ifindex(station->netdev)); 3067 3068 if (station->connected_network) { 3069 const char *ssid = network_get_ssid(station->connected_network); 3070 /* Use direct probe request */ 3071 params.ssid = (const uint8_t *)ssid; 3072 params.ssid_len = strlen(ssid); 3073 } 3074 3075 if (!freq_set) { 3076 station->roam_scan_full = true; 3077 params.freqs = allowed; 3078 station_debug_event(station, "full-roam-scan"); 3079 } else 3080 scan_freq_set_constrain(freq_set, allowed); 3081 3082 if (L_WARN_ON(scan_freq_set_isempty(params.freqs))) 3083 return -ENOTSUP; 3084 3085 station->last_roam_scan = l_time_now(); 3086 3087 station->roam_scan_id = 3088 scan_active_full(netdev_get_wdev_id(station->netdev), ¶ms, 3089 station_roam_scan_triggered, 3090 station_roam_scan_notify, station, 3091 station_roam_scan_destroy); 3092 3093 if (!station->roam_scan_id) 3094 return -EIO; 3095 3096 return 0; 3097 } 3098 3099 static int station_roam_scan_known_freqs(struct station *station) 3100 { 3101 const struct network_info *info = network_get_info( 3102 station->connected_network); 3103 struct scan_freq_set *freqs = network_info_get_roam_frequencies(info, 3104 station->connected_bss->frequency, 3105 STATION_RECENT_FREQS_LIMIT); 3106 int r = -ENODATA; 3107 3108 if (!freqs) 3109 return r; 3110 3111 if (!wiphy_constrain_freq_set(station->wiphy, freqs)) 3112 goto free_set; 3113 3114 r = station_roam_scan(station, freqs); 3115 3116 free_set: 3117 scan_freq_set_free(freqs); 3118 return r; 3119 } 3120 3121 static void station_neighbor_report_cb(struct netdev *netdev, int err, 3122 const uint8_t *reports, 3123 size_t reports_len, void *user_data) 3124 { 3125 struct station *station = user_data; 3126 struct scan_freq_set *freq_set; 3127 int r; 3128 3129 if (err == -ENODEV) 3130 return; 3131 3132 l_debug("ifindex: %u, error: %d(%s)", 3133 netdev_get_ifindex(station->netdev), 3134 err, err < 0 ? strerror(-err) : ""); 3135 3136 /* 3137 * Check if we're still attempting to roam -- if dbus Disconnect 3138 * had been called in the meantime we just abort the attempt. 3139 */ 3140 if (!station->preparing_roam || err == -ENODEV) 3141 return; 3142 3143 if (!reports || err) { 3144 r = station_roam_scan_known_freqs(station); 3145 3146 if (r == -ENODATA) 3147 l_debug("no neighbor report results or known freqs"); 3148 3149 if (r < 0) 3150 station_roam_failed(station); 3151 3152 return; 3153 } 3154 3155 parse_neighbor_report(station, reports, reports_len, &freq_set); 3156 3157 r = station_roam_scan(station, freq_set); 3158 3159 if (freq_set) 3160 scan_freq_set_free(freq_set); 3161 3162 if (r < 0) 3163 station_roam_failed(station); 3164 } 3165 3166 static void station_start_roam(struct station *station) 3167 { 3168 int r; 3169 3170 station->preparing_roam = true; 3171 3172 /* 3173 * If current BSS supports Neighbor Reports, narrow the scan down 3174 * to channels occupied by known neighbors in the ESS. If no neighbor 3175 * report was obtained upon connection, request one now. This isn't 3176 * 100% reliable as the neighbor lists are not required to be 3177 * complete or current. It is likely still better than doing a 3178 * full scan. 10.11.10.1: "A neighbor report may not be exhaustive 3179 * either by choice, or due to the fact that there may be neighbor 3180 * APs not known to the AP." 3181 */ 3182 if (station->roam_freqs) { 3183 if (station_roam_scan(station, station->roam_freqs) == 0) { 3184 l_debug("Using cached neighbor report for roam"); 3185 return; 3186 } 3187 } else if (station->connected_bss->cap_rm_neighbor_report) { 3188 if (netdev_neighbor_report_req(station->netdev, 3189 station_neighbor_report_cb) == 0) { 3190 l_debug("Requesting neighbor report for roam"); 3191 return; 3192 } 3193 } 3194 3195 r = station_roam_scan_known_freqs(station); 3196 if (r == -ENODATA) 3197 l_debug("No neighbor report or known frequencies, roam failed"); 3198 3199 if (r < 0) 3200 station_roam_failed(station); 3201 } 3202 3203 static bool station_cannot_roam(struct station *station) 3204 { 3205 const struct l_settings *config = iwd_get_config(); 3206 bool disabled; 3207 3208 /* 3209 * Disable roaming with hardware that can roam automatically. Note this 3210 * is now required for recent kernels which have CQM event support on 3211 * this type of hardware (e.g. brcmfmac). 3212 */ 3213 if (wiphy_supports_firmware_roam(station->wiphy)) 3214 return true; 3215 3216 if (!l_settings_get_bool(config, "Scan", "DisableRoamingScan", 3217 &disabled)) 3218 disabled = false; 3219 3220 return disabled || station->preparing_roam || 3221 station->state == STATION_STATE_ROAMING || 3222 station->state == STATION_STATE_FT_ROAMING || 3223 station->ft_work.id; 3224 } 3225 3226 static void station_roam_trigger_cb(struct l_timeout *timeout, void *user_data) 3227 { 3228 struct station *station = user_data; 3229 3230 l_debug("%u", netdev_get_ifindex(station->netdev)); 3231 3232 l_timeout_remove(station->roam_trigger_timeout); 3233 station->roam_trigger_timeout = NULL; 3234 3235 if (station_cannot_roam(station)) 3236 return; 3237 3238 station_start_roam(station); 3239 } 3240 3241 static void station_roam_timeout_rearm(struct station *station, int seconds) 3242 { 3243 uint64_t remaining; 3244 3245 if (!station->roam_trigger_timeout) 3246 goto new_timeout; 3247 3248 /* If we can't get the remaining time just create a new timer */ 3249 if (L_WARN_ON(!l_timeout_remaining(station->roam_trigger_timeout, 3250 &remaining))) { 3251 l_timeout_remove(station->roam_trigger_timeout); 3252 goto new_timeout; 3253 } 3254 3255 /* Our current timeout is less than the rearm, keep current */ 3256 if (l_time_before(remaining, seconds * L_USEC_PER_SEC)) { 3257 l_debug("Keeping current roam timeout of %" PRIx64 " seconds", 3258 l_time_to_secs(remaining)); 3259 return; 3260 } 3261 3262 l_debug("Rescheduling roam timeout from %" PRIx64" to %d seconds", 3263 l_time_to_secs(remaining), seconds); 3264 l_timeout_modify(station->roam_trigger_timeout, seconds); 3265 3266 return; 3267 3268 new_timeout: 3269 l_debug("Arming new roam timer for %d seconds", seconds); 3270 3271 station->roam_trigger_timeout = 3272 l_timeout_create(seconds, station_roam_trigger_cb, 3273 station, NULL); 3274 } 3275 3276 #define WNM_REQUEST_MODE_PREFERRED_CANDIDATE_LIST (1 << 0) 3277 #define WNM_REQUEST_MODE_DISASSOCIATION_IMMINENT (1 << 2) 3278 #define WNM_REQUEST_MODE_TERMINATION_IMMINENT (1 << 3) 3279 #define WNM_REQUEST_MODE_ESS_DISASSOCIATION_IMMINENT (1 << 4) 3280 3281 static void station_ap_directed_roam(struct station *station, 3282 const struct mmpdu_header *hdr, 3283 const void *body, size_t body_len) 3284 { 3285 uint32_t pos = 0; 3286 uint8_t req_mode; 3287 uint16_t dtimer; 3288 uint8_t valid_interval; 3289 bool can_roam = !station_cannot_roam(station); 3290 bool ignore_candidates = 3291 station->connected_bss->vendor_quirks.ignore_bss_tm_candidates; 3292 3293 l_debug("ifindex: %u", netdev_get_ifindex(station->netdev)); 3294 3295 if (station->state != STATION_STATE_CONNECTED) { 3296 l_debug("roam: unexpected AP directed roam -- ignore"); 3297 return; 3298 } 3299 3300 /* 3301 * Sanitize the frame to check that it is from our current AP. 3302 * 3303 * 802.11-2020 Section 9.3.3.1 about Address2: 3304 * "If the STA is an AP with dot11MultiBSSDImplemented set to false, 3305 * then this address is the BSSID." 3306 * 3307 * Address3: 3308 * "If the STA is an AP or PCP, the Address 3 field is the same as the 3309 * Address 2 field." 3310 * 3311 * For now check that Address2 & Address3 is the same as the connected 3312 * BSS address. 3313 */ 3314 if (memcmp(hdr->address_2, station->connected_bss, ETH_ALEN) || 3315 memcmp(hdr->address_2, hdr->address_3, ETH_ALEN)) { 3316 l_debug("roam: AP directed roam not from our AP -- ignore"); 3317 return; 3318 } 3319 3320 if (body_len < 7) 3321 goto format_error; 3322 3323 /* 3324 * First two bytes are checked by the frame watch (WNM category and 3325 * WNM action). The third is the dialog token which is not relevant 3326 * because we did not send a BSS transition query -- so skip these 3327 * first three bytes. 3328 */ 3329 pos += 3; 3330 3331 req_mode = l_get_u8(body + pos); 3332 pos++; 3333 3334 /* 3335 * TODO: Disassociation timer and validity interval are currently not 3336 * used since the BSS transition request is being handled immediately. 3337 */ 3338 dtimer = l_get_le16(body + pos); 3339 pos += 2; 3340 valid_interval = l_get_u8(body + pos); 3341 pos++; 3342 3343 l_debug("roam: BSS transition received from AP: " MAC", " 3344 "Disassociation Time: %u, " 3345 "Validity interval: %u, Address3: " MAC, 3346 MAC_STR(hdr->address_2), 3347 dtimer, valid_interval, 3348 MAC_STR(hdr->address_3)); 3349 3350 /* 3351 * The ap_directed_roaming flag forces IWD to roam if there are any 3352 * candidates, even if they are worse than the current BSS. This isn't 3353 * always a good idea since we may be associated to the best BSS. Where 3354 * this does matter is if the AP indicates its going down or will be 3355 * disassociating us. If either of these bits are set, set the 3356 * ap_directed_roaming flag. Otherwise still try roaming but don't 3357 * treat it any different than a normal roam. 3358 * 3359 * The only exception here is if we are in the middle of roaming 3360 * (can_roam == false) since we cannot reliably know if the roam scan 3361 * included frequencies from potential candidates in this request, 3362 * forcing a roam in this case might result in unintended behavior. 3363 */ 3364 if (can_roam && req_mode & (WNM_REQUEST_MODE_DISASSOCIATION_IMMINENT | 3365 WNM_REQUEST_MODE_TERMINATION_IMMINENT | 3366 WNM_REQUEST_MODE_ESS_DISASSOCIATION_IMMINENT)) 3367 station->ap_directed_roaming = true; 3368 3369 if (req_mode & WNM_REQUEST_MODE_TERMINATION_IMMINENT) { 3370 if (pos + 12 > body_len) 3371 goto format_error; 3372 3373 pos += 12; 3374 } 3375 3376 if (req_mode & WNM_REQUEST_MODE_ESS_DISASSOCIATION_IMMINENT) { 3377 uint8_t url_len; 3378 3379 if (pos + 1 > body_len) 3380 goto format_error; 3381 3382 url_len = l_get_u8(body + pos); 3383 pos++; 3384 3385 if (pos + url_len > body_len) 3386 goto format_error; 3387 3388 pos += url_len; 3389 } 3390 3391 blacklist_add_bss(station->connected_bss->addr, 3392 BLACKLIST_REASON_AP_BUSY); 3393 station_debug_event(station, "ap-roam-blacklist-added"); 3394 3395 /* 3396 * Validating the frame and blacklisting should still be done even if 3397 * we are mid-roam. Its important to track the BSS requesting the 3398 * transition so when the current roam completes IWD will be less likely 3399 * to roam back to the current BSS. 3400 */ 3401 if (!can_roam) 3402 return; 3403 3404 station->preparing_roam = true; 3405 3406 l_timeout_remove(station->roam_trigger_timeout); 3407 station->roam_trigger_timeout = NULL; 3408 3409 if ((req_mode & WNM_REQUEST_MODE_PREFERRED_CANDIDATE_LIST) && 3410 !ignore_candidates) { 3411 l_debug("roam: AP sent a preferred candidate list"); 3412 station_neighbor_report_cb(station->netdev, 0, body + pos, 3413 body_len - pos, station); 3414 } else { 3415 if (station->connected_bss->cap_rm_neighbor_report) { 3416 if (!netdev_neighbor_report_req(station->netdev, 3417 station_neighbor_report_cb)) 3418 return; 3419 3420 l_warn("failed to request neighbor report!"); 3421 } 3422 3423 l_debug("full scan after BSS transition request"); 3424 if (station_roam_scan(station, NULL) < 0) 3425 station_roam_failed(station); 3426 } 3427 3428 return; 3429 3430 format_error: 3431 l_debug("bad AP roam frame formatting"); 3432 } 3433 3434 static void station_low_rssi(struct station *station) 3435 { 3436 if (station->signal_low) 3437 return; 3438 3439 station->signal_low = true; 3440 3441 if (station_cannot_roam(station)) 3442 return; 3443 3444 /* Set a 5-second initial timeout */ 3445 station_roam_timeout_rearm(station, 5); 3446 } 3447 3448 static void station_ok_rssi(struct station *station) 3449 { 3450 l_timeout_remove(station->roam_trigger_timeout); 3451 station->roam_trigger_timeout = NULL; 3452 3453 station->signal_low = false; 3454 } 3455 3456 static void station_event_roamed(struct station *station, struct scan_bss *new) 3457 { 3458 struct scan_bss *stale; 3459 3460 network_bss_update(station->connected_network, new); 3461 station_register_bss(station->connected_network, new); 3462 3463 /* Remove new BSS if it exists in past scan results */ 3464 stale = l_queue_remove_if(station->bss_list, bss_match, new); 3465 if (stale) 3466 scan_bss_free(stale); 3467 3468 station->connected_bss = new; 3469 3470 l_queue_insert(station->bss_list, new, scan_bss_rank_compare, NULL); 3471 3472 station_roamed(station); 3473 } 3474 3475 static void station_event_channel_switched(struct station *station, 3476 const uint32_t freq) 3477 { 3478 struct network *network = station->connected_network; 3479 3480 station->connected_bss->frequency = freq; 3481 3482 network_bss_update(network, station->connected_bss); 3483 } 3484 3485 static bool station_retry_owe_default_group(struct station *station) 3486 { 3487 /* 3488 * Shouldn't ever get here with classic open networks so its safe to 3489 * assume if the security is none this is an OWE network. 3490 */ 3491 if (network_get_security(station->connected_network) != SECURITY_NONE) 3492 return false; 3493 3494 /* If we already forced group 19, allow the BSS to be blacklisted */ 3495 if (network_get_force_default_ecc_group(station->connected_network)) 3496 return false; 3497 3498 l_warn("Failed to connect to OWE BSS "MAC" possibly because the AP is " 3499 "incorrectly deriving the PTK, this AP should be fixed. " 3500 "Retrying with group 19 as a workaround", 3501 MAC_STR(station->connected_bss->addr)); 3502 3503 network_set_force_default_ecc_group(station->connected_network); 3504 3505 return true; 3506 } 3507 3508 static bool station_retry_with_reason(struct station *station, 3509 uint16_t reason_code) 3510 { 3511 iwd_notice(IWD_NOTICE_CONNECT_FAILED, "reason: %u", reason_code); 3512 3513 /* 3514 * We don't want to cause a retry and blacklist if the password was 3515 * incorrect. Otherwise we would just continue to fail. 3516 * 3517 * Other reason codes can be added here if its decided we want to 3518 * fail in those cases. 3519 */ 3520 switch (reason_code) { 3521 case MMPDU_REASON_CODE_PREV_AUTH_NOT_VALID: 3522 if (station_retry_owe_default_group(station)) 3523 goto try_next; 3524 /* fall through */ 3525 case MMPDU_REASON_CODE_IEEE8021X_FAILED: 3526 return false; 3527 default: 3528 break; 3529 } 3530 3531 blacklist_add_bss(station->connected_bss->addr, 3532 BLACKLIST_REASON_CONNECT_FAILED); 3533 3534 /* 3535 * Network blacklist the BSS as well, since the timeout blacklist could 3536 * be disabled 3537 */ 3538 network_blacklist_add(station->connected_network, 3539 station->connected_bss); 3540 3541 try_next: 3542 return station_try_next_bss(station); 3543 } 3544 3545 static bool station_pmksa_fallback(struct station *station, uint16_t status) 3546 { 3547 /* 3548 * IEEE 802.11-2020 12.6.10.3 Cached PMKSAs and RSNA key management 3549 * 3550 * "If the Authenticator does not have a PMKSA for the PMKIDs in the 3551 * (re)association request or the AKM does not match, its behavior 3552 * depends on how the PMKSA was established. If SAE authentication was 3553 * used to establish the PMKSA, then the AP shall reject (re)association 3554 * by sending a (Re)Association Response frame with status code 3555 * STATUS_INVALID_PMKID. Note that this allows the non-AP STA to fall 3556 * back to full SAE authentication to establish another PMKSA" 3557 */ 3558 if (status != MMPDU_STATUS_CODE_INVALID_PMKID) 3559 return false; 3560 3561 if (L_WARN_ON(!station->hs)) 3562 return false; 3563 3564 if (!IE_AKM_IS_SAE(station->hs->akm_suite) || !station->hs->have_pmksa) 3565 return false; 3566 3567 /* 3568 * Remove the PMKSA from the handshake and return true to re-try the 3569 * same BSS without PMKSA. 3570 */ 3571 handshake_state_remove_pmksa(station->hs); 3572 3573 station_debug_event(station, "pmksa-invalid-pmkid"); 3574 3575 return true; 3576 } 3577 3578 static bool station_retry_with_status(struct station *station, 3579 uint16_t status_code) 3580 { 3581 /* If PMKSA failed don't blacklist so we can retry this BSS */ 3582 if (station_pmksa_fallback(station, status_code)) 3583 goto try_next; 3584 3585 switch (status_code) { 3586 /* 3587 * Certain Auth/Assoc failures should not cause a timeout blacklist. 3588 * In these cases we want to only temporarily blacklist the BSS until 3589 * the connection is complete. 3590 */ 3591 case MMPDU_STATUS_CODE_DENIED_UNSUFFICIENT_BANDWIDTH: 3592 case MMPDU_STATUS_CODE_DENIED_POOR_CHAN_CONDITIONS: 3593 /* 3594 * TODO: The WITH_SUGG_BSS_TRANS case should also include a neighbor 3595 * report IE in the frame. This would allow us to target a 3596 * specific BSS on our next attempt. There is currently no way to 3597 * obtain that IE, but this should be done in the future. 3598 */ 3599 case MMPDU_STATUS_CODE_REJECTED_WITH_SUGG_BSS_TRANS: 3600 break; 3601 /* 3602 * If a BSS is indicating its unable to handle more connections we will 3603 * blacklist this the same way we do for BSS's issuing BSS-TM requests 3604 * thereby avoiding roams to this BSS for the configured timeout. 3605 */ 3606 case MMPDU_STATUS_CODE_DENIED_NO_MORE_STAS: 3607 blacklist_add_bss(station->connected_bss->addr, 3608 BLACKLIST_REASON_AP_BUSY); 3609 break; 3610 /* For any other status codes, blacklist the BSS */ 3611 default: 3612 blacklist_add_bss(station->connected_bss->addr, 3613 BLACKLIST_REASON_CONNECT_FAILED); 3614 break; 3615 } 3616 3617 /* 3618 * Unconditionally network blacklist the BSS if we are retrying. This 3619 * will allow network_bss_select to traverse the BSS list and ignore 3620 * BSS's which have previously failed 3621 */ 3622 network_blacklist_add(station->connected_network, 3623 station->connected_bss); 3624 3625 try_next: 3626 iwd_notice(IWD_NOTICE_CONNECT_FAILED, "status: %u", status_code); 3627 3628 return station_try_next_bss(station); 3629 } 3630 3631 static void station_connect_ok(struct station *station) 3632 { 3633 struct handshake_state *hs = netdev_get_handshake(station->netdev); 3634 3635 l_debug(""); 3636 3637 /* 3638 * Get a neighbor report now so future roams can avoid waiting for 3639 * a report at that time 3640 */ 3641 if (station->connected_bss->cap_rm_neighbor_report) { 3642 if (netdev_neighbor_report_req(station->netdev, 3643 station_early_neighbor_report_cb) < 0) 3644 l_warn("Could not request neighbor report"); 3645 } 3646 3647 network_connected(station->connected_network); 3648 3649 if (station->netconfig) { 3650 if (hs->fils_ip_req_ie && hs->fils_ip_resp_ie) { 3651 struct ie_fils_ip_addr_response_info info; 3652 struct ie_tlv_iter iter; 3653 int r; 3654 3655 ie_tlv_iter_init(&iter, hs->fils_ip_resp_ie, 3656 hs->fils_ip_resp_ie[1] + 2); 3657 if (!L_WARN_ON(unlikely(!ie_tlv_iter_next(&iter)))) 3658 r = ie_parse_fils_ip_addr_response(&iter, 3659 &info); 3660 else 3661 r = -ENOMSG; 3662 3663 if (r != 0) 3664 l_debug("Error parsing the FILS IP Address " 3665 "Assignment response: %s (%i)", 3666 strerror(-r), -r); 3667 else if (info.response_pending && 3668 info.response_timeout) 3669 l_debug("FILS IP Address Assignment response " 3670 "is pending (unsupported)"); 3671 else if (info.response_pending) 3672 l_debug("FILS IP Address Assignment failed"); 3673 else { 3674 l_debug("FILS IP Address Assignment response " 3675 "OK"); 3676 netconfig_handle_fils_ip_resp( 3677 station->netconfig, 3678 &info); 3679 } 3680 } 3681 3682 if (L_WARN_ON(!netconfig_configure(station->netconfig, 3683 station_netconfig_event_handler, 3684 station))) 3685 return; 3686 3687 station_enter_state(station, STATION_STATE_NETCONFIG); 3688 } else 3689 station_enter_state(station, STATION_STATE_CONNECTED); 3690 } 3691 3692 static void station_connect_cb(struct netdev *netdev, enum netdev_result result, 3693 void *event_data, void *user_data) 3694 { 3695 struct station *station = user_data; 3696 bool continue_autoconnect; 3697 uint16_t reason = MMPDU_REASON_CODE_UNSPECIFIED; 3698 3699 l_debug("%u, result: %d", netdev_get_ifindex(station->netdev), result); 3700 3701 switch (result) { 3702 case NETDEV_RESULT_OK: 3703 blacklist_remove_bss(station->connected_bss->addr, 3704 BLACKLIST_REASON_CONNECT_FAILED); 3705 station_connect_ok(station); 3706 return; 3707 case NETDEV_RESULT_DISCONNECTED: 3708 reason = l_get_u16(event_data); 3709 3710 iwd_notice(IWD_NOTICE_DISCONNECT_INFO, "reason: %u", reason); 3711 3712 /* Disconnected while connecting */ 3713 network_blacklist_add(station->connected_network, 3714 station->connected_bss); 3715 if (station_try_next_bss(station)) 3716 return; 3717 3718 break; 3719 case NETDEV_RESULT_HANDSHAKE_FAILED: 3720 reason = l_get_u16(event_data); 3721 3722 /* reason code in this case */ 3723 if (station_retry_with_reason(station, reason)) 3724 return; 3725 3726 break; 3727 case NETDEV_RESULT_AUTHENTICATION_FAILED: 3728 case NETDEV_RESULT_ASSOCIATION_FAILED: 3729 /* status code in this case */ 3730 if (station_retry_with_status(station, l_get_u16(event_data))) 3731 return; 3732 3733 break; 3734 default: 3735 break; 3736 } 3737 3738 if (station->connect_pending) { 3739 struct l_dbus_message *reply; 3740 3741 if (result == NETDEV_RESULT_ABORTED) 3742 reply = dbus_error_aborted(station->connect_pending); 3743 else 3744 reply = dbus_error_failed(station->connect_pending); 3745 3746 dbus_pending_reply(&station->connect_pending, reply); 3747 } 3748 3749 if (result == NETDEV_RESULT_ABORTED) 3750 return; 3751 3752 continue_autoconnect = station->state == STATION_STATE_CONNECTING_AUTO; 3753 3754 if (station->state == STATION_STATE_CONNECTING) { 3755 /* 3756 * Either a handshake failure (generated internally) or a 4-way 3757 * handshake timeout should trigger the PSK to be asked for 3758 * again. Set during_eapol accordingly. 3759 */ 3760 bool during_eapol = result == NETDEV_RESULT_HANDSHAKE_FAILED || 3761 reason == MMPDU_REASON_CODE_4WAY_HANDSHAKE_TIMEOUT; 3762 network_connect_failed(station->connected_network, 3763 during_eapol); 3764 } 3765 3766 station_reset_connection_state(station); 3767 station_enter_state(station, STATION_STATE_DISCONNECTED); 3768 3769 if (continue_autoconnect) { 3770 if (station_autoconnect_next(station) < 0) { 3771 l_debug("Nothing left on autoconnect list"); 3772 station_enter_state(station, 3773 STATION_STATE_AUTOCONNECT_FULL); 3774 } 3775 3776 return; 3777 } 3778 3779 if (station->autoconnect) 3780 station_enter_state(station, STATION_STATE_AUTOCONNECT_QUICK); 3781 } 3782 3783 static void station_disconnect_event(struct station *station, void *event_data) 3784 { 3785 l_debug("%u", netdev_get_ifindex(station->netdev)); 3786 3787 /* 3788 * If we're connecting, AP deauthenticated us, most likely because 3789 * we provided the wrong password or otherwise failed authentication 3790 * during the handshaking phase. Connection failures should be handled 3791 * within station_connect_cb/station_reassociate_cb apart from netconfig 3792 * and FW roams. 3793 */ 3794 switch (station->state) { 3795 case STATION_STATE_CONNECTED: 3796 case STATION_STATE_FW_ROAMING: 3797 case STATION_STATE_NETCONFIG: 3798 iwd_notice(IWD_NOTICE_DISCONNECT_INFO, "reason: %u", 3799 l_get_u16(event_data)); 3800 station_disassociated(station); 3801 return; 3802 default: 3803 break; 3804 } 3805 3806 l_warn("Unexpected disconnect event"); 3807 } 3808 3809 #define STATION_PKT_LOSS_THRESHOLD 10 3810 #define LOSS_ROAM_RATE_LIMIT 2 3811 3812 static void station_packets_lost(struct station *station, uint32_t num_pkts) 3813 { 3814 uint64_t elapsed; 3815 3816 l_debug("Packets lost event: %u", num_pkts); 3817 3818 if (num_pkts < STATION_PKT_LOSS_THRESHOLD) 3819 return; 3820 3821 if (station_cannot_roam(station)) 3822 return; 3823 3824 station_debug_event(station, "packet-loss-roam"); 3825 3826 elapsed = l_time_diff(station->last_roam_scan, l_time_now()); 3827 3828 /* 3829 * If we just issued a roam scan, delay the roam to avoid constant 3830 * scanning. 3831 */ 3832 if (LOSS_ROAM_RATE_LIMIT > l_time_to_secs(elapsed)) { 3833 l_debug("Too many roam attempts in %u second timeframe, " 3834 "delaying roam", LOSS_ROAM_RATE_LIMIT); 3835 3836 station_roam_timeout_rearm(station, LOSS_ROAM_RATE_LIMIT); 3837 3838 return; 3839 } 3840 3841 if (station->roam_trigger_timeout) { 3842 l_debug("canceling roam timer to roam immediately"); 3843 l_timeout_remove(station->roam_trigger_timeout); 3844 station->roam_trigger_timeout = NULL; 3845 } 3846 3847 station_start_roam(station); 3848 } 3849 3850 static void station_beacon_lost(struct station *station) 3851 { 3852 l_debug("Beacon lost event"); 3853 3854 if (station_cannot_roam(station)) 3855 return; 3856 3857 station_debug_event(station, "beacon-loss-roam"); 3858 3859 station_roam_timeout_rearm(station, LOSS_ROAM_RATE_LIMIT); 3860 } 3861 3862 static void station_event_roaming(struct station *station) 3863 { 3864 if (station->netconfig && station->state != STATION_STATE_CONNECTED) { 3865 netconfig_reset(station->netconfig); 3866 station->netconfig_after_roam = true; 3867 } 3868 3869 station_enter_state(station, STATION_STATE_FW_ROAMING); 3870 } 3871 3872 static void station_ecc_group_retry(struct station *station) 3873 { 3874 struct network *network = station_get_connected_network(station); 3875 3876 if (L_WARN_ON(!network)) 3877 return; 3878 3879 station_debug_event(station, "ecc-group-rejected"); 3880 3881 network_set_force_default_ecc_group(network); 3882 } 3883 3884 static void station_netdev_event(struct netdev *netdev, enum netdev_event event, 3885 void *event_data, void *user_data) 3886 { 3887 struct station *station = user_data; 3888 3889 switch (event) { 3890 case NETDEV_EVENT_AUTHENTICATING: 3891 station_debug_event(station, "authenticating"); 3892 break; 3893 case NETDEV_EVENT_ASSOCIATING: 3894 station_debug_event(station, "associating"); 3895 break; 3896 case NETDEV_EVENT_DISCONNECT_BY_AP: 3897 case NETDEV_EVENT_DISCONNECT_BY_SME: 3898 station_disconnect_event(station, event_data); 3899 break; 3900 case NETDEV_EVENT_RSSI_THRESHOLD_LOW: 3901 station_low_rssi(station); 3902 break; 3903 case NETDEV_EVENT_RSSI_THRESHOLD_HIGH: 3904 station_ok_rssi(station); 3905 break; 3906 case NETDEV_EVENT_RSSI_LEVEL_NOTIFY: 3907 if (station->signal_agent) 3908 station_signal_agent_notify(station); 3909 break; 3910 case NETDEV_EVENT_ROAMING: 3911 station_event_roaming(station); 3912 break; 3913 case NETDEV_EVENT_ROAMED: 3914 station_event_roamed(station, (struct scan_bss *) event_data); 3915 break; 3916 case NETDEV_EVENT_CHANNEL_SWITCHED: 3917 station_event_channel_switched(station, l_get_u32(event_data)); 3918 break; 3919 case NETDEV_EVENT_PACKET_LOSS_NOTIFY: 3920 station_packets_lost(station, l_get_u32(event_data)); 3921 break; 3922 case NETDEV_EVENT_BEACON_LOSS_NOTIFY: 3923 station_beacon_lost(station); 3924 break; 3925 case NETDEV_EVENT_ECC_GROUP_RETRY: 3926 station_ecc_group_retry(station); 3927 break; 3928 } 3929 } 3930 3931 int __station_connect_network(struct station *station, struct network *network, 3932 struct scan_bss *bss, enum station_state state) 3933 { 3934 struct handshake_state *hs; 3935 int r; 3936 const char *vendor_quirks = vendor_quirks_to_string(bss->vendor_quirks); 3937 3938 /* 3939 * If we already have a handshake_state ref this is due to a retry, 3940 * unref that now 3941 */ 3942 if (station->hs) { 3943 handshake_state_unref(station->hs); 3944 station->hs = NULL; 3945 } 3946 3947 if (station->netconfig && !netconfig_load_settings( 3948 station->netconfig, 3949 network_get_settings(network))) 3950 return -EINVAL; 3951 3952 hs = station_handshake_setup(station, network, bss); 3953 if (!hs) 3954 return -ENOTSUP; 3955 3956 r = netdev_connect(station->netdev, bss, hs, 3957 station_netdev_event, 3958 station_connect_cb, station); 3959 if (r < 0) { 3960 handshake_state_unref(hs); 3961 return r; 3962 } 3963 3964 iwd_notice(IWD_NOTICE_CONNECT_INFO, "ssid: %s, bss: "MAC", " 3965 "signal: %d, load: %d/255", 3966 network_get_ssid(network), 3967 MAC_STR(bss->addr), 3968 bss->signal_strength / 100, 3969 bss->utilization); 3970 3971 if (vendor_quirks) 3972 l_debug("vendor quirks for "MAC": %s", 3973 MAC_STR(bss->addr), vendor_quirks); 3974 3975 station->connected_bss = bss; 3976 station->connected_network = network; 3977 station->hs = handshake_state_ref(hs); 3978 3979 if (station->state != state) 3980 station_enter_state(station, state); 3981 3982 return 0; 3983 } 3984 3985 static void station_disconnect_onconnect_cb(struct netdev *netdev, bool success, 3986 void *user_data) 3987 { 3988 struct station *station = user_data; 3989 int err; 3990 3991 station_enter_state(station, STATION_STATE_DISCONNECTED); 3992 3993 err = __station_connect_network(station, 3994 station->connect_pending_network, 3995 station->connect_pending_bss, 3996 STATION_STATE_CONNECTING); 3997 3998 station->connect_pending_network = NULL; 3999 station->connect_pending_bss = NULL; 4000 4001 if (err < 0) { 4002 #ifdef HAVE_DBUS 4003 dbus_pending_reply(&station->connect_pending, 4004 dbus_error_from_errno(err, 4005 station->connect_pending)); 4006 #endif 4007 return; 4008 } 4009 } 4010 4011 static void station_disconnect_onconnect(struct station *station, 4012 struct network *network, 4013 struct scan_bss *bss, 4014 struct l_dbus_message *message) 4015 { 4016 if (netdev_disconnect(station->netdev, station_disconnect_onconnect_cb, 4017 station) < 0) { 4018 #ifdef HAVE_DBUS 4019 l_dbus_send(dbus_get_bus(), 4020 dbus_error_from_errno(-EIO, message)); 4021 #endif 4022 return; 4023 } 4024 4025 station_reset_connection_state(station); 4026 4027 station_enter_state(station, STATION_STATE_DISCONNECTING); 4028 4029 station->connect_pending_network = network; 4030 station->connect_pending_bss = bss; 4031 4032 station->connect_pending = l_dbus_message_ref(message); 4033 } 4034 4035 void station_connect_network(struct station *station, struct network *network, 4036 struct scan_bss *bss, 4037 struct l_dbus_message *message) 4038 { 4039 #ifdef HAVE_DBUS 4040 struct l_dbus *dbus = dbus_get_bus(); 4041 #endif 4042 int err; 4043 4044 /* 4045 * If a hidden scan is not completed, station_is_busy would not 4046 * indicate anything is going on so we need to cancel the scan and 4047 * fail the connection now. 4048 */ 4049 if (station->hidden_network_scan_id) { 4050 scan_cancel(netdev_get_wdev_id(station->netdev), 4051 station->hidden_network_scan_id); 4052 4053 #ifdef HAVE_DBUS 4054 dbus_pending_reply(&station->hidden_pending, 4055 dbus_error_failed(station->hidden_pending)); 4056 #endif 4057 } 4058 4059 if (station->quick_scan_id) { 4060 scan_cancel(netdev_get_wdev_id(station->netdev), 4061 station->quick_scan_id); 4062 station->quick_scan_id = 0; 4063 station_property_set_scanning(station, false); 4064 } 4065 4066 if (station_is_busy(station)) { 4067 station_disconnect_onconnect(station, network, bss, message); 4068 4069 return; 4070 } 4071 4072 err = __station_connect_network(station, network, bss, 4073 STATION_STATE_CONNECTING); 4074 if (err < 0) 4075 goto error; 4076 4077 station->connect_pending = l_dbus_message_ref(message); 4078 4079 station_set_autoconnect(station, true); 4080 4081 return; 4082 4083 error: 4084 #ifdef HAVE_DBUS 4085 l_dbus_send(dbus, dbus_error_from_errno(err, message)); 4086 #else 4087 return; 4088 #endif 4089 } 4090 4091 static void station_hidden_network_scan_triggered(int err, void *user_data) 4092 { 4093 struct station *station = user_data; 4094 4095 l_debug(""); 4096 4097 if (!err) 4098 return; 4099 4100 dbus_pending_reply(&station->hidden_pending, 4101 dbus_error_failed(station->hidden_pending)); 4102 } 4103 4104 static bool station_hidden_network_scan_results(int err, 4105 struct l_queue *bss_list, 4106 const struct scan_freq_set *freqs, 4107 void *userdata) 4108 { 4109 struct station *station = userdata; 4110 struct network *network_psk; 4111 struct network *network_open; 4112 const char *ssid; 4113 uint8_t ssid_len; 4114 struct l_dbus_message *msg; 4115 struct l_dbus_message *error; 4116 struct scan_bss *bss; 4117 4118 l_debug(""); 4119 4120 msg = station->hidden_pending; 4121 station->hidden_pending = NULL; 4122 /* Zero this now so station_connect_network knows the scan is done */ 4123 station->hidden_network_scan_id = 0; 4124 4125 if (err) { 4126 dbus_pending_reply(&msg, dbus_error_failed(msg)); 4127 return false; 4128 } 4129 4130 if (!l_dbus_message_get_arguments(msg, "s", &ssid)) { 4131 dbus_pending_reply(&msg, dbus_error_invalid_args(msg)); 4132 return false; 4133 } 4134 4135 ssid_len = strlen(ssid); 4136 4137 while ((bss = l_queue_pop_head(bss_list))) { 4138 if (bss->ssid_len != ssid_len || 4139 memcmp(bss->ssid, ssid, ssid_len)) 4140 goto next; 4141 4142 if (bss->owe_trans) 4143 goto next; 4144 4145 /* 4146 * Override time_stamp so that this entry is removed on 4147 * the next scan 4148 */ 4149 bss->time_stamp = 0; 4150 4151 if (station_add_seen_bss(station, bss)) { 4152 l_queue_push_tail(station->bss_list, bss); 4153 4154 continue; 4155 } 4156 4157 next: 4158 scan_bss_free(bss); 4159 } 4160 4161 l_queue_destroy(bss_list, NULL); 4162 4163 network_psk = station_network_find(station, ssid, SECURITY_PSK); 4164 network_open = station_network_find(station, ssid, SECURITY_NONE); 4165 4166 if (!network_psk && !network_open) { 4167 dbus_pending_reply(&msg, dbus_error_not_found(msg)); 4168 return true; 4169 } 4170 4171 if (network_psk && network_open) { 4172 station_hide_network(station, network_psk); 4173 station_hide_network(station, network_open); 4174 dbus_pending_reply(&msg, dbus_error_service_set_overlap(msg)); 4175 return true; 4176 } 4177 4178 network_update_known_frequencies(network_psk ?: network_open); 4179 4180 error = network_connect_new_hidden_network(network_psk ?: network_open, 4181 msg); 4182 4183 if (error) 4184 dbus_pending_reply(&msg, error); 4185 else 4186 l_dbus_message_unref(msg); 4187 4188 return true; 4189 } 4190 4191 static void station_hidden_network_scan_destroy(void *userdata) 4192 { 4193 struct station *station = userdata; 4194 4195 station->hidden_network_scan_id = 0; 4196 } 4197 4198 static struct l_dbus_message *station_dbus_connect_hidden_network( 4199 struct l_dbus *dbus, 4200 struct l_dbus_message *message, 4201 void *user_data) 4202 { 4203 struct station *station = user_data; 4204 uint64_t id = netdev_get_wdev_id(station->netdev); 4205 struct scan_parameters params = { 4206 .flush = true, 4207 .randomize_mac_addr_hint = false, 4208 }; 4209 const char *ssid; 4210 struct network *network; 4211 _auto_(scan_freq_set_free) struct scan_freq_set *allowed = NULL; 4212 4213 l_debug(""); 4214 4215 if (station->hidden_pending) 4216 return dbus_error_busy(message); 4217 4218 if (!l_dbus_message_get_arguments(message, "s", &ssid)) 4219 return dbus_error_invalid_args(message); 4220 4221 if (strlen(ssid) > SSID_MAX_SIZE) 4222 return dbus_error_invalid_args(message); 4223 4224 if (known_networks_find(ssid, SECURITY_PSK) || 4225 known_networks_find(ssid, SECURITY_NONE)) 4226 return dbus_error_already_provisioned(message); 4227 4228 network = station_network_find(station, ssid, SECURITY_PSK); 4229 if (!network) 4230 network = station_network_find(station, ssid, SECURITY_NONE); 4231 4232 /* 4233 * This checks for a corner case where the hidden network was already 4234 * found and is in our scan results, but the initial connection failed. 4235 * For example, the password was given incorrectly. In this case the 4236 * entry will also be found on the hidden bss list. 4237 */ 4238 if (network) { 4239 const struct l_queue_entry *entry = 4240 l_queue_get_entries(station->hidden_bss_list_sorted); 4241 struct scan_bss *target = network_bss_select(network, true); 4242 4243 /* Treat OWE transition networks special */ 4244 if (target->owe_trans) 4245 goto not_hidden; 4246 4247 for (; entry; entry = entry->next) { 4248 struct scan_bss *bss = entry->data; 4249 4250 if (!scan_bss_addr_eq(target, bss)) 4251 continue; 4252 4253 /* We can skip the scan and try to connect right away */ 4254 return network_connect_new_hidden_network(network, 4255 message); 4256 } 4257 4258 not_hidden: 4259 return dbus_error_not_hidden(message); 4260 } 4261 4262 allowed = station_get_allowed_freqs(station); 4263 if (L_WARN_ON(!allowed)) 4264 return dbus_error_not_supported(message); 4265 4266 params.freqs = allowed; 4267 params.ssid = (const uint8_t *)ssid; 4268 params.ssid_len = strlen(ssid); 4269 4270 /* HW cannot randomize our MAC if connected */ 4271 if (!station->connected_bss) 4272 params.randomize_mac_addr_hint = true; 4273 4274 station->hidden_network_scan_id = scan_active_full(id, ¶ms, 4275 station_hidden_network_scan_triggered, 4276 station_hidden_network_scan_results, 4277 station, station_hidden_network_scan_destroy); 4278 if (!station->hidden_network_scan_id) 4279 return dbus_error_failed(message); 4280 4281 station->hidden_pending = l_dbus_message_ref(message); 4282 4283 return NULL; 4284 } 4285 4286 static void station_disconnect_reconnect_cb(struct netdev *netdev, bool success, 4287 void *user_data) 4288 { 4289 struct station *station = user_data; 4290 4291 if (__station_connect_network(station, station->connected_network, 4292 station->connected_bss, 4293 STATION_STATE_ROAMING) < 0) 4294 station_disassociated(station); 4295 } 4296 4297 static void station_reconnect(struct station *station) 4298 { 4299 /* 4300 * Rather than doing 4 or so state changes, lets just go into 4301 * roaming for the duration of this reconnect. 4302 */ 4303 station_enter_state(station, STATION_STATE_ROAMING); 4304 4305 netdev_disconnect(station->netdev, station_disconnect_reconnect_cb, 4306 station); 4307 } 4308 4309 static void station_disconnect_cb(struct netdev *netdev, bool success, 4310 void *user_data) 4311 { 4312 struct station *station = user_data; 4313 4314 l_debug("%u, success: %d", 4315 netdev_get_ifindex(station->netdev), success); 4316 4317 #ifdef HAVE_DBUS 4318 if (station->disconnect_pending) { 4319 struct l_dbus_message *reply; 4320 4321 if (success) { 4322 reply = l_dbus_message_new_method_return( 4323 station->disconnect_pending); 4324 l_dbus_message_set_arguments(reply, ""); 4325 } else 4326 reply = dbus_error_failed(station->disconnect_pending); 4327 4328 dbus_pending_reply(&station->disconnect_pending, reply); 4329 } 4330 #endif 4331 4332 station_enter_state(station, STATION_STATE_DISCONNECTED); 4333 4334 if (station->autoconnect) 4335 station_enter_state(station, STATION_STATE_AUTOCONNECT_QUICK); 4336 } 4337 4338 int station_disconnect(struct station *station) 4339 { 4340 if (station->state == STATION_STATE_DISCONNECTING) 4341 return -EBUSY; 4342 4343 if (!station->connected_bss) 4344 return -ENOTCONN; 4345 4346 /* 4347 * If the disconnect somehow fails we won't know if we're still 4348 * connected so we may as well indicate now that we're no longer 4349 * connected. 4350 */ 4351 station_reset_connection_state(station); 4352 4353 station_enter_state(station, STATION_STATE_DISCONNECTING); 4354 4355 if (netdev_disconnect(station->netdev, 4356 station_disconnect_cb, station) < 0) 4357 return -EIO; 4358 4359 return 0; 4360 } 4361 4362 static struct l_dbus_message *station_dbus_disconnect(struct l_dbus *dbus, 4363 struct l_dbus_message *message, 4364 void *user_data) 4365 { 4366 struct station *station = user_data; 4367 int result; 4368 4369 l_debug(""); 4370 4371 /* 4372 * Disconnect was triggered by the user, don't autoconnect. Wait for 4373 * the user's explicit instructions to scan and connect to the network 4374 */ 4375 station_set_autoconnect(station, false); 4376 4377 if (station->hidden_network_scan_id) { 4378 scan_cancel(netdev_get_wdev_id(station->netdev), 4379 station->hidden_network_scan_id); 4380 dbus_pending_reply(&station->hidden_pending, 4381 dbus_error_aborted(station->hidden_pending)); 4382 4383 return l_dbus_message_new_method_return(message); 4384 } 4385 4386 if (!station_is_busy(station)) 4387 return l_dbus_message_new_method_return(message); 4388 4389 result = station_disconnect(station); 4390 if (result < 0) 4391 return dbus_error_from_errno(result, message); 4392 4393 station->disconnect_pending = l_dbus_message_ref(message); 4394 4395 return NULL; 4396 } 4397 4398 static struct l_dbus_message *station_dbus_get_networks(struct l_dbus *dbus, 4399 struct l_dbus_message *message, 4400 void *user_data) 4401 { 4402 struct station *station = user_data; 4403 struct l_dbus_message *reply = 4404 l_dbus_message_new_method_return(message); 4405 struct l_dbus_message_builder *builder = 4406 l_dbus_message_builder_new(reply); 4407 struct l_queue *sorted = station->networks_sorted; 4408 const struct l_queue_entry *entry; 4409 4410 l_dbus_message_builder_enter_array(builder, "(on)"); 4411 4412 for (entry = l_queue_get_entries(sorted); entry; entry = entry->next) { 4413 const struct network *network = entry->data; 4414 int16_t signal_strength = network_get_signal_strength(network); 4415 4416 l_dbus_message_builder_enter_struct(builder, "on"); 4417 l_dbus_message_builder_append_basic(builder, 'o', 4418 network_get_path(network)); 4419 l_dbus_message_builder_append_basic(builder, 'n', 4420 &signal_strength); 4421 l_dbus_message_builder_leave_struct(builder); 4422 } 4423 4424 l_dbus_message_builder_leave_array(builder); 4425 4426 l_dbus_message_builder_finalize(builder); 4427 l_dbus_message_builder_destroy(builder); 4428 4429 return reply; 4430 } 4431 4432 static struct l_dbus_message *station_dbus_get_hidden_access_points( 4433 struct l_dbus *dbus, 4434 struct l_dbus_message *message, 4435 void *user_data) 4436 { 4437 struct station *station = user_data; 4438 struct l_dbus_message *reply = 4439 l_dbus_message_new_method_return(message); 4440 struct l_dbus_message_builder *builder = 4441 l_dbus_message_builder_new(reply); 4442 const struct l_queue_entry *entry; 4443 4444 l_dbus_message_builder_enter_array(builder, "(sns)"); 4445 4446 for (entry = l_queue_get_entries(station->hidden_bss_list_sorted); 4447 entry; entry = entry->next) { 4448 struct scan_bss *bss = entry->data; 4449 int16_t signal_strength = bss->signal_strength; 4450 enum security security; 4451 4452 if (scan_bss_get_security(bss, &security) < 0) 4453 continue; 4454 4455 l_dbus_message_builder_enter_struct(builder, "sns"); 4456 l_dbus_message_builder_append_basic(builder, 's', 4457 util_address_to_string(bss->addr)); 4458 l_dbus_message_builder_append_basic(builder, 'n', 4459 &signal_strength); 4460 l_dbus_message_builder_append_basic(builder, 's', 4461 security_to_str(security)); 4462 l_dbus_message_builder_leave_struct(builder); 4463 } 4464 4465 l_dbus_message_builder_leave_array(builder); 4466 4467 l_dbus_message_builder_finalize(builder); 4468 l_dbus_message_builder_destroy(builder); 4469 4470 return reply; 4471 } 4472 4473 static void station_dbus_scan_done(struct station *station, 4474 bool try_autoconnect) 4475 { 4476 station->dbus_scan_id = 0; 4477 station_property_set_scanning(station, false); 4478 4479 station_process_owe_transition_networks(station); 4480 4481 if (try_autoconnect) { 4482 station->autoconnect_can_start = true; 4483 station_autoconnect_start(station); 4484 } 4485 } 4486 4487 static void station_dbus_scan_triggered(int err, void *user_data) 4488 { 4489 struct station *station = user_data; 4490 struct l_dbus_message *reply; 4491 4492 l_debug("station_scan_triggered: %i", err); 4493 4494 if (err < 0) { 4495 if (station->scan_pending) { 4496 reply = dbus_error_from_errno(err, 4497 station->scan_pending); 4498 dbus_pending_reply(&station->scan_pending, reply); 4499 } 4500 4501 station_dbus_scan_done(station, true); 4502 return; 4503 } 4504 4505 l_debug("Scan triggered for %s subset %i", 4506 netdev_get_name(station->netdev), 4507 station->dbus_scan_subset_idx); 4508 4509 if (station->scan_pending) { 4510 reply = l_dbus_message_new_method_return(station->scan_pending); 4511 l_dbus_message_set_arguments(reply, ""); 4512 dbus_pending_reply(&station->scan_pending, reply); 4513 } 4514 4515 station_property_set_scanning(station, true); 4516 } 4517 4518 static bool station_dbus_scan_subset(struct station *station); 4519 4520 static bool station_dbus_scan_results(int err, struct l_queue *bss_list, 4521 const struct scan_freq_set *freqs, 4522 void *userdata) 4523 { 4524 struct station *station = userdata; 4525 unsigned int next_idx = station->dbus_scan_subset_idx + 1; 4526 bool last_subset; 4527 4528 if (err) { 4529 station_dbus_scan_done(station, true); 4530 return false; 4531 } 4532 4533 last_subset = next_idx >= L_ARRAY_SIZE(station->scan_freqs_order) || 4534 station->scan_freqs_order[next_idx] == NULL; 4535 station->dbus_scan_subset_idx = next_idx; 4536 4537 station_set_scan_results(station, bss_list, freqs, false); 4538 4539 if (last_subset || !station_dbus_scan_subset(station)) 4540 station_dbus_scan_done(station, true); 4541 4542 return true; 4543 } 4544 4545 static bool station_dbus_scan_subset(struct station *station) 4546 { 4547 unsigned int idx = station->dbus_scan_subset_idx; 4548 4549 station->dbus_scan_id = station_scan_trigger(station, 4550 station->scan_freqs_order[idx], 4551 station_dbus_scan_triggered, 4552 station_dbus_scan_results, 4553 NULL); 4554 4555 return station->dbus_scan_id != 0; 4556 } 4557 4558 static struct l_dbus_message *station_dbus_scan(struct l_dbus *dbus, 4559 struct l_dbus_message *message, 4560 void *user_data) 4561 { 4562 struct station *station = user_data; 4563 4564 l_debug("Scan called from DBus"); 4565 4566 if (station->dbus_scan_id) 4567 return dbus_error_busy(message); 4568 4569 if (L_IN_SET(station->state, STATION_STATE_CONNECTING, 4570 STATION_STATE_CONNECTING_AUTO, 4571 STATION_STATE_NETCONFIG) || 4572 station_is_roaming(station)) 4573 return dbus_error_busy(message); 4574 4575 station->dbus_scan_subset_idx = 0; 4576 4577 if (!station_dbus_scan_subset(station)) 4578 return dbus_error_failed(message); 4579 4580 station->scan_pending = l_dbus_message_ref(message); 4581 4582 return NULL; 4583 } 4584 4585 struct signal_agent { 4586 char *owner; 4587 char *path; 4588 unsigned int disconnect_watch; 4589 }; 4590 4591 static void station_signal_agent_notify(struct station *station) 4592 { 4593 struct signal_agent *agent = station->signal_agent; 4594 struct netdev *netdev = station->netdev; 4595 const char *device_path = netdev_get_path(netdev); 4596 uint8_t level = netdev_get_rssi_level_idx(netdev); 4597 4598 struct l_dbus_message *msg; 4599 4600 msg = l_dbus_message_new_method_call(dbus_get_bus(), 4601 agent->owner, agent->path, 4602 IWD_SIGNAL_AGENT_INTERFACE, 4603 "Changed"); 4604 l_dbus_message_set_arguments(msg, "oy", device_path, level); 4605 l_dbus_message_set_no_reply(msg, true); 4606 4607 l_dbus_send(dbus_get_bus(), msg); 4608 } 4609 4610 static void station_signal_agent_release(struct signal_agent *agent, 4611 const char *device_path) 4612 { 4613 struct l_dbus_message *msg; 4614 4615 msg = l_dbus_message_new_method_call(dbus_get_bus(), 4616 agent->owner, agent->path, 4617 IWD_SIGNAL_AGENT_INTERFACE, 4618 "Release"); 4619 l_dbus_message_set_arguments(msg, "o", device_path); 4620 l_dbus_message_set_no_reply(msg, true); 4621 4622 l_dbus_send(dbus_get_bus(), msg); 4623 } 4624 4625 static void signal_agent_free(void *data) 4626 { 4627 struct signal_agent *agent = data; 4628 4629 l_free(agent->owner); 4630 l_free(agent->path); 4631 l_dbus_remove_watch(dbus_get_bus(), agent->disconnect_watch); 4632 l_free(agent); 4633 } 4634 4635 static void signal_agent_disconnect(struct l_dbus *dbus, void *user_data) 4636 { 4637 struct station *station = user_data; 4638 4639 l_debug("signal_agent %s disconnected", station->signal_agent->owner); 4640 4641 l_idle_oneshot(signal_agent_free, station->signal_agent, NULL); 4642 station->signal_agent = NULL; 4643 4644 netdev_set_rssi_report_levels(station->netdev, NULL, 0); 4645 } 4646 4647 static struct l_dbus_message *station_dbus_signal_agent_register( 4648 struct l_dbus *dbus, 4649 struct l_dbus_message *message, 4650 void *user_data) 4651 { 4652 struct station *station = user_data; 4653 const char *path, *sender; 4654 struct l_dbus_message *reply; 4655 struct l_dbus_message_iter level_iter; 4656 int8_t levels[16]; 4657 int err; 4658 int16_t val; 4659 size_t count = 0; 4660 4661 if (station->signal_agent) 4662 return dbus_error_already_exists(message); 4663 4664 l_debug("signal agent register called"); 4665 4666 if (!l_dbus_message_get_arguments(message, "oan", &path, &level_iter)) 4667 return dbus_error_invalid_args(message); 4668 4669 while (l_dbus_message_iter_next_entry(&level_iter, &val)) { 4670 if (count >= L_ARRAY_SIZE(levels) || val > 127 || val < -127) 4671 return dbus_error_invalid_args(message); 4672 4673 levels[count++] = val; 4674 } 4675 4676 if (count < 1) 4677 return dbus_error_invalid_args(message); 4678 4679 err = netdev_set_rssi_report_levels(station->netdev, levels, count); 4680 if (err == -ENOTSUP) 4681 return dbus_error_not_supported(message); 4682 else if (err < 0) 4683 return dbus_error_failed(message); 4684 4685 sender = l_dbus_message_get_sender(message); 4686 4687 station->signal_agent = l_new(struct signal_agent, 1); 4688 station->signal_agent->owner = l_strdup(sender); 4689 station->signal_agent->path = l_strdup(path); 4690 station->signal_agent->disconnect_watch = 4691 l_dbus_add_disconnect_watch(dbus, sender, 4692 signal_agent_disconnect, 4693 station, NULL); 4694 4695 l_debug("agent %s path %s", sender, path); 4696 4697 reply = l_dbus_message_new_method_return(message); 4698 l_dbus_send(dbus, reply); 4699 4700 if (station->connected_network) 4701 station_signal_agent_notify(station); 4702 4703 return NULL; 4704 } 4705 4706 static struct l_dbus_message *station_dbus_signal_agent_unregister( 4707 struct l_dbus *dbus, 4708 struct l_dbus_message *message, 4709 void *user_data) 4710 { 4711 struct station *station = user_data; 4712 const char *path, *sender; 4713 4714 if (!station->signal_agent) 4715 return dbus_error_failed(message); 4716 4717 l_debug("signal agent unregister"); 4718 4719 if (!l_dbus_message_get_arguments(message, "o", &path)) 4720 return dbus_error_invalid_args(message); 4721 4722 if (strcmp(station->signal_agent->path, path)) 4723 return dbus_error_not_found(message); 4724 4725 sender = l_dbus_message_get_sender(message); 4726 4727 if (strcmp(station->signal_agent->owner, sender)) 4728 return dbus_error_not_found(message); 4729 4730 signal_agent_free(station->signal_agent); 4731 station->signal_agent = NULL; 4732 4733 netdev_set_rssi_report_levels(station->netdev, NULL, 0); 4734 4735 return l_dbus_message_new_method_return(message); 4736 } 4737 4738 static bool station_property_get_connected_network(struct l_dbus *dbus, 4739 struct l_dbus_message *message, 4740 struct l_dbus_message_builder *builder, 4741 void *user_data) 4742 { 4743 struct station *station = user_data; 4744 4745 if (!station->connected_network) 4746 return false; 4747 4748 l_dbus_message_builder_append_basic(builder, 'o', 4749 network_get_path(station->connected_network)); 4750 4751 return true; 4752 } 4753 4754 static bool station_property_get_connected_ap(struct l_dbus *dbus, 4755 struct l_dbus_message *message, 4756 struct l_dbus_message_builder *builder, 4757 void *user_data) 4758 { 4759 struct station *station = user_data; 4760 4761 if (!station->connected_network) 4762 return false; 4763 4764 l_dbus_message_builder_append_basic(builder, 'o', 4765 network_bss_get_path(station->connected_network, 4766 station->connected_bss)); 4767 4768 return true; 4769 } 4770 4771 static bool station_property_get_scanning(struct l_dbus *dbus, 4772 struct l_dbus_message *message, 4773 struct l_dbus_message_builder *builder, 4774 void *user_data) 4775 { 4776 struct station *station = user_data; 4777 bool scanning = station->scanning; 4778 4779 l_dbus_message_builder_append_basic(builder, 'b', &scanning); 4780 4781 return true; 4782 } 4783 4784 static bool station_property_get_state(struct l_dbus *dbus, 4785 struct l_dbus_message *message, 4786 struct l_dbus_message_builder *builder, 4787 void *user_data) 4788 { 4789 struct station *station = user_data; 4790 const char *statestr = "invalid"; 4791 4792 switch (station->state) { 4793 case STATION_STATE_AUTOCONNECT_QUICK: 4794 case STATION_STATE_AUTOCONNECT_FULL: 4795 case STATION_STATE_DISCONNECTED: 4796 statestr = "disconnected"; 4797 break; 4798 case STATION_STATE_CONNECTING: 4799 case STATION_STATE_CONNECTING_AUTO: 4800 case STATION_STATE_NETCONFIG: 4801 statestr = "connecting"; 4802 break; 4803 case STATION_STATE_CONNECTED: 4804 statestr = "connected"; 4805 break; 4806 case STATION_STATE_DISCONNECTING: 4807 statestr = "disconnecting"; 4808 break; 4809 case STATION_STATE_ROAMING: 4810 case STATION_STATE_FT_ROAMING: 4811 case STATION_STATE_FW_ROAMING: 4812 /* 4813 * Stay in a connecting state if roaming before netconfig 4814 * has finished 4815 */ 4816 if (station->netconfig_after_roam) 4817 statestr = "connecting"; 4818 else 4819 statestr = "roaming"; 4820 break; 4821 } 4822 4823 l_dbus_message_builder_append_basic(builder, 's', statestr); 4824 return true; 4825 } 4826 4827 static bool station_property_get_affinities(struct l_dbus *dbus, 4828 struct l_dbus_message *message, 4829 struct l_dbus_message_builder *builder, 4830 void *user_data) 4831 { 4832 struct station *station = user_data; 4833 const struct l_queue_entry *e; 4834 4835 if (!station->connected_network) 4836 return false; 4837 4838 l_dbus_message_builder_enter_array(builder, "o"); 4839 4840 for (e = l_queue_get_entries(station->affinities); e; e = e->next) { 4841 const char *path = e->data; 4842 4843 l_dbus_message_builder_append_basic(builder, 'o', path); 4844 } 4845 4846 l_dbus_message_builder_leave_array(builder); 4847 4848 return true; 4849 } 4850 4851 static void station_affinity_disconnected_cb(struct l_dbus *dbus, 4852 void *user_data) 4853 { 4854 struct station *station = user_data; 4855 4856 l_dbus_remove_watch(dbus_get_bus(), station->affinity_watch); 4857 4858 l_debug("client that set affinity has disconnected"); 4859 4860 /* The client who set the affinity disconnected, raise the threshold */ 4861 netdev_raise_signal_threshold(station->netdev); 4862 } 4863 4864 static void station_affinity_watch_destroy(void *user_data) 4865 { 4866 struct station *station = user_data; 4867 bool empty = l_queue_length(station->affinities) == 0; 4868 4869 station->affinity_watch = 0; 4870 4871 l_free(station->affinity_client); 4872 station->affinity_client = NULL; 4873 4874 l_queue_clear(station->affinities, l_free); 4875 4876 if (!empty) 4877 l_dbus_property_changed(dbus_get_bus(), 4878 netdev_get_path(station->netdev), 4879 IWD_STATION_INTERFACE, "Affinities"); 4880 } 4881 4882 static struct l_dbus_message *station_property_set_affinities( 4883 struct l_dbus *dbus, 4884 struct l_dbus_message *message, 4885 struct l_dbus_message_iter *new_value, 4886 l_dbus_property_complete_cb_t complete, 4887 void *user_data) 4888 { 4889 struct station *station = user_data; 4890 struct l_dbus_message_iter array; 4891 const char *sender = l_dbus_message_get_sender(message); 4892 char *old_path = l_queue_peek_head(station->affinities); 4893 const char *new_path; 4894 struct scan_bss *new_bss = NULL; 4895 struct scan_bss *old_bss = NULL; 4896 bool lower_threshold = false; 4897 4898 if (!station->connected_network) 4899 return dbus_error_not_connected(message); 4900 4901 if (wiphy_supports_firmware_roam(station->wiphy)) 4902 return dbus_error_not_supported(message); 4903 4904 if (station->affinity_watch && 4905 strcmp(station->affinity_client, sender)) { 4906 l_warn("Only one client may manage Affinities property"); 4907 return dbus_error_permission_denied(message); 4908 } 4909 4910 if (!l_dbus_message_iter_get_variant(new_value, "ao", &array)) 4911 return dbus_error_invalid_args(message); 4912 4913 /* Get first entry, or if an empty array set the path to NULL */ 4914 if (!l_dbus_message_iter_next_entry(&array, &new_path)) 4915 new_path = NULL; 4916 4917 /* 4918 * Only allowing single values for now. If there is more than a single 4919 * value, fail 4920 */ 4921 if (new_path && l_dbus_message_iter_next_entry(&array, &new_path)) 4922 return dbus_error_invalid_args(message); 4923 4924 old_path = l_queue_peek_head(station->affinities); 4925 if (old_path) 4926 old_bss = l_dbus_object_get_data(dbus_get_bus(), 4927 old_path, IWD_BSS_INTERFACE); 4928 if (new_path) 4929 new_bss = l_dbus_object_get_data(dbus, 4930 new_path, IWD_BSS_INTERFACE); 4931 4932 /* Either the same path, or both arrays are empty */ 4933 if (old_bss == new_bss) { 4934 complete(dbus, message, NULL); 4935 return NULL; 4936 } 4937 4938 /* 4939 * TODO: For now only allow the affinities array to contain a single 4940 * value, the connected BSS path. Any other values will be 4941 * rejected. This could change in the future. 4942 */ 4943 if (new_bss && new_bss != station->connected_bss) 4944 return dbus_error_invalid_args(message); 4945 4946 l_queue_clear(station->affinities, l_free); 4947 4948 /* 4949 * Adding a new BSS, create a watch for this DBus client so if it 4950 * disconnects we can clear the affinities list 4951 */ 4952 if (new_path) { 4953 l_queue_push_head(station->affinities, l_strdup(new_path)); 4954 4955 lower_threshold = true; 4956 4957 if (!station->affinity_watch) { 4958 station->affinity_client = l_strdup(sender); 4959 station->affinity_watch = l_dbus_add_disconnect_watch( 4960 dbus, sender, 4961 station_affinity_disconnected_cb, 4962 station, 4963 station_affinity_watch_destroy); 4964 } 4965 /* The list was cleared, remove the watch */ 4966 } else if (station->affinity_watch) 4967 l_dbus_remove_watch(dbus, station->affinity_watch); 4968 4969 /* 4970 * If affinity was set to the current BSS, lower the roam threshold. If 4971 * the connected BSS was not in the list raise the signal threshold. 4972 * The threshold may already be raised, in which case netdev will detect 4973 * this and do nothing. 4974 */ 4975 if (lower_threshold) 4976 netdev_lower_signal_threshold(station->netdev); 4977 else 4978 netdev_raise_signal_threshold(station->netdev); 4979 4980 complete(dbus, message, NULL); 4981 4982 l_dbus_property_changed(dbus, netdev_get_path(station->netdev), 4983 IWD_STATION_INTERFACE, "Affinities"); 4984 4985 return NULL; 4986 } 4987 4988 void station_foreach(station_foreach_func_t func, void *user_data) 4989 { 4990 const struct l_queue_entry *entry; 4991 4992 for (entry = l_queue_get_entries(station_list); entry; 4993 entry = entry->next) { 4994 struct station *station = entry->data; 4995 4996 func(station, user_data); 4997 } 4998 } 4999 5000 struct station *station_find(uint32_t ifindex) 5001 { 5002 const struct l_queue_entry *entry; 5003 5004 for (entry = l_queue_get_entries(station_list); entry; 5005 entry = entry->next) { 5006 struct station *station = entry->data; 5007 5008 if (netdev_get_ifindex(station->netdev) == ifindex) 5009 return station; 5010 } 5011 5012 return NULL; 5013 } 5014 5015 struct network_foreach_data { 5016 station_network_foreach_func_t func; 5017 void *user_data; 5018 }; 5019 5020 static void network_foreach(const void *key, void *value, void *user_data) 5021 { 5022 struct network_foreach_data *data = user_data; 5023 struct network *network = value; 5024 5025 data->func(network, data->user_data); 5026 } 5027 5028 void station_network_foreach(struct station *station, 5029 station_network_foreach_func_t func, 5030 void *user_data) 5031 { 5032 struct network_foreach_data data = { 5033 .func = func, 5034 .user_data = user_data, 5035 }; 5036 5037 l_hashmap_foreach(station->networks, network_foreach, &data); 5038 } 5039 5040 void station_foreach_network(struct station *station, 5041 void (*func)(struct network *, void *), 5042 void *user_data) 5043 { 5044 const struct l_queue_entry *entry; 5045 5046 for (entry = l_queue_get_entries(station->networks_sorted); entry; 5047 entry = entry->next) 5048 func(entry->data, user_data); 5049 } 5050 5051 struct l_queue *station_get_bss_list(struct station *station) 5052 { 5053 return station->bss_list; 5054 } 5055 5056 struct scan_bss *station_get_connected_bss(struct station *station) 5057 { 5058 return station->connected_bss; 5059 } 5060 5061 int station_hide_network(struct station *station, struct network *network) 5062 { 5063 const char *path = network_get_path(network); 5064 struct scan_bss *bss; 5065 5066 l_debug("%s", path); 5067 5068 if (station->connected_network == network) 5069 return -EBUSY; 5070 5071 if (!l_hashmap_lookup(station->networks, path)) 5072 return -ENOENT; 5073 5074 l_queue_remove(station->networks_sorted, network); 5075 l_hashmap_remove(station->networks, path); 5076 5077 while ((bss = network_bss_list_pop(network))) { 5078 memset(bss->ssid, 0, bss->ssid_len); 5079 l_queue_remove_if(station->hidden_bss_list_sorted, 5080 bss_match_bssid, bss->addr); 5081 l_queue_insert(station->hidden_bss_list_sorted, bss, 5082 bss_signal_strength_compare, NULL); 5083 } 5084 5085 network_remove(network, -ESRCH); 5086 5087 return 0; 5088 } 5089 5090 static void station_add_2_4ghz_freq(uint32_t freq, void *user_data) 5091 { 5092 struct scan_freq_set *set = user_data; 5093 5094 /* exclude social channels added in initial scan request */ 5095 if (freq < 3000 && freq != 2412 && freq != 2437 && freq != 2462) 5096 scan_freq_set_add(set, freq); 5097 } 5098 5099 static void station_fill_scan_freq_subsets(struct station *station) 5100 { 5101 const struct scan_freq_set *supported = 5102 wiphy_get_supported_freqs(station->wiphy); 5103 unsigned int subset_idx = 0; 5104 5105 /* 5106 * Scan the 2.4GHz "social channels" first, 5GHz second, if supported, 5107 * all other 2.4GHz channels last. To be refined as needed. 5108 */ 5109 if (allowed_bands & BAND_FREQ_2_4_GHZ) { 5110 station->scan_freqs_order[subset_idx] = scan_freq_set_new(); 5111 scan_freq_set_add(station->scan_freqs_order[subset_idx], 2412); 5112 scan_freq_set_add(station->scan_freqs_order[subset_idx], 2437); 5113 scan_freq_set_add(station->scan_freqs_order[subset_idx], 2462); 5114 subset_idx++; 5115 } 5116 5117 /* 5118 * TODO: It may might sense to split up 5 and 6ghz into separate subsets 5119 * since the channel set is so large. 5120 */ 5121 if (allowed_bands & (BAND_FREQ_5_GHZ | BAND_FREQ_6_GHZ)) { 5122 uint32_t mask = allowed_bands & 5123 (BAND_FREQ_5_GHZ | BAND_FREQ_6_GHZ); 5124 struct scan_freq_set *set = scan_freq_set_clone(supported, 5125 mask); 5126 5127 /* 5/6ghz didn't add any frequencies */ 5128 if (scan_freq_set_isempty(set)) { 5129 scan_freq_set_free(set); 5130 } else 5131 station->scan_freqs_order[subset_idx++] = set; 5132 } 5133 5134 /* Add remaining 2.4ghz channels to subset */ 5135 if (allowed_bands & BAND_FREQ_2_4_GHZ) { 5136 station->scan_freqs_order[subset_idx] = scan_freq_set_new(); 5137 scan_freq_set_foreach(supported, station_add_2_4ghz_freq, 5138 station->scan_freqs_order[subset_idx]); 5139 } 5140 5141 /* 5142 * This has the unintended consequence of allowing DBus scans to 5143 * scan the entire spectrum rather than cause IWD to be completely 5144 * non-functional. Rather than prevent DBus scans from working at all 5145 * print a warning here. 5146 */ 5147 if (station->scan_freqs_order[0] == NULL) 5148 l_warn("All supported bands were disabled by user! IWD will not" 5149 " function as expected"); 5150 5151 } 5152 5153 static void station_wiphy_watch(struct wiphy *wiphy, 5154 enum wiphy_state_watch_event event, 5155 void *user_data) 5156 { 5157 struct station *station = user_data; 5158 int ret; 5159 5160 if (event != WIPHY_STATE_WATCH_EVENT_REGDOM_DONE) 5161 return; 5162 5163 /* 5164 * The only state that requires special handling is for 5165 * quick scans since the previous quick scan was delayed until 5166 * the regulatory domain updated. Try again in case 6GHz is now 5167 * unlocked (unlikely), or advance to full autoconnect. Just in 5168 * case this update came during a quick scan, ignore it. 5169 */ 5170 if (station->state != STATION_STATE_AUTOCONNECT_QUICK || 5171 station->quick_scan_id) 5172 return; 5173 5174 ret = station_quick_scan_trigger(station); 5175 if (!ret) 5176 return; 5177 5178 L_WARN_ON(ret == -EAGAIN); 5179 station_enter_state(station, STATION_STATE_AUTOCONNECT_FULL); 5180 } 5181 5182 static struct station *station_create(struct netdev *netdev) 5183 { 5184 struct station *station; 5185 #ifdef HAVE_DBUS 5186 struct l_dbus *dbus = dbus_get_bus(); 5187 #endif 5188 bool autoconnect = true; 5189 5190 station = l_new(struct station, 1); 5191 watchlist_init(&station->state_watches, NULL); 5192 5193 station->bss_list = l_queue_new(); 5194 station->hidden_bss_list_sorted = l_queue_new(); 5195 station->networks = l_hashmap_new(); 5196 l_hashmap_set_hash_function(station->networks, l_str_hash); 5197 l_hashmap_set_compare_function(station->networks, 5198 (l_hashmap_compare_func_t) strcmp); 5199 station->networks_sorted = l_queue_new(); 5200 5201 station->wiphy = netdev_get_wiphy(netdev); 5202 station->netdev = netdev; 5203 5204 station->wiphy_watch = wiphy_state_watch_add(station->wiphy, 5205 station_wiphy_watch, 5206 station, NULL); 5207 5208 l_queue_push_head(station_list, station); 5209 5210 #ifdef HAVE_DBUS 5211 l_dbus_object_add_interface(dbus, netdev_get_path(netdev), 5212 IWD_STATION_INTERFACE, station); 5213 #endif 5214 5215 if (netconfig_enabled()) 5216 station->netconfig = netconfig_new(netdev_get_ifindex(netdev)); 5217 5218 station->anqp_pending = l_queue_new(); 5219 5220 station_fill_scan_freq_subsets(station); 5221 5222 if (iwd_is_developer_mode()) { 5223 #ifdef HAVE_DBUS 5224 l_dbus_object_add_interface(dbus, 5225 netdev_get_path(station->netdev), 5226 IWD_STATION_DEBUG_INTERFACE, 5227 station); 5228 #endif 5229 autoconnect = false; 5230 } 5231 5232 station_set_autoconnect(station, autoconnect); 5233 5234 station->roam_bss_list = l_queue_new(); 5235 station->affinities = l_queue_new(); 5236 5237 return station; 5238 } 5239 5240 static void station_free(struct station *station) 5241 { 5242 l_debug(""); 5243 5244 if (!l_queue_remove(station_list, station)) 5245 return; 5246 5247 l_dbus_object_remove_interface(dbus_get_bus(), 5248 netdev_get_path(station->netdev), 5249 IWD_STATION_DIAGNOSTIC_INTERFACE); 5250 if (iwd_is_developer_mode()) 5251 l_dbus_object_remove_interface(dbus_get_bus(), 5252 netdev_get_path(station->netdev), 5253 IWD_STATION_DEBUG_INTERFACE); 5254 5255 if (station->netconfig) { 5256 netconfig_destroy(station->netconfig); 5257 station->netconfig = NULL; 5258 } 5259 5260 periodic_scan_stop(station); 5261 5262 if (station->signal_agent) { 5263 station_signal_agent_release(station->signal_agent, 5264 netdev_get_path(station->netdev)); 5265 signal_agent_free(station->signal_agent); 5266 } 5267 5268 if (station->connect_pending) 5269 dbus_pending_reply(&station->connect_pending, 5270 dbus_error_aborted(station->connect_pending)); 5271 5272 if (station->hidden_pending) 5273 dbus_pending_reply(&station->hidden_pending, 5274 dbus_error_aborted(station->hidden_pending)); 5275 5276 if (station->disconnect_pending) 5277 dbus_pending_reply(&station->disconnect_pending, 5278 dbus_error_aborted(station->disconnect_pending)); 5279 5280 if (station->scan_pending) 5281 dbus_pending_reply(&station->scan_pending, 5282 dbus_error_aborted(station->scan_pending)); 5283 5284 if (station->dbus_scan_id) 5285 scan_cancel(netdev_get_wdev_id(station->netdev), 5286 station->dbus_scan_id); 5287 5288 if (station->quick_scan_id) 5289 scan_cancel(netdev_get_wdev_id(station->netdev), 5290 station->quick_scan_id); 5291 5292 if (station->hidden_network_scan_id) 5293 scan_cancel(netdev_get_wdev_id(station->netdev), 5294 station->hidden_network_scan_id); 5295 5296 if (station->owe_hidden_scan_ids) { 5297 void *ptr; 5298 5299 while ((ptr = l_queue_pop_head(station->owe_hidden_scan_ids))) 5300 scan_cancel(netdev_get_wdev_id(station->netdev), 5301 L_PTR_TO_UINT(ptr)); 5302 5303 l_queue_destroy(station->owe_hidden_scan_ids, NULL); 5304 } 5305 5306 if (station->hs) { 5307 handshake_state_unref(station->hs); 5308 station->hs = NULL; 5309 } 5310 5311 station_roam_state_clear(station); 5312 5313 l_queue_destroy(station->networks_sorted, NULL); 5314 l_hashmap_destroy(station->networks, network_free); 5315 l_queue_destroy(station->bss_list, bss_free); 5316 l_queue_destroy(station->hidden_bss_list_sorted, NULL); 5317 l_queue_destroy(station->autoconnect_list, NULL); 5318 5319 watchlist_destroy(&station->state_watches); 5320 5321 l_queue_destroy(station->anqp_pending, remove_anqp); 5322 5323 scan_freq_set_free(station->scan_freqs_order[0]); 5324 scan_freq_set_free(station->scan_freqs_order[1]); 5325 5326 if (station->scan_freqs_order[2]) 5327 scan_freq_set_free(station->scan_freqs_order[2]); 5328 5329 wiphy_state_watch_remove(station->wiphy, station->wiphy_watch); 5330 5331 l_queue_destroy(station->roam_bss_list, l_free); 5332 5333 if (station->affinity_watch) 5334 l_dbus_remove_watch(dbus_get_bus(), station->affinity_watch); 5335 5336 l_queue_destroy(station->affinities, l_free); 5337 5338 l_free(station); 5339 } 5340 5341 static void station_setup_interface(struct l_dbus_interface *interface) 5342 { 5343 l_dbus_interface_method(interface, "ConnectHiddenNetwork", 0, 5344 station_dbus_connect_hidden_network, 5345 "", "s", "name"); 5346 l_dbus_interface_method(interface, "Disconnect", 0, 5347 station_dbus_disconnect, "", ""); 5348 l_dbus_interface_method(interface, "GetOrderedNetworks", 0, 5349 station_dbus_get_networks, "a(on)", "", 5350 "networks"); 5351 l_dbus_interface_method(interface, "GetHiddenAccessPoints", 0, 5352 station_dbus_get_hidden_access_points, 5353 "a(sns)", "", 5354 "accesspoints"); 5355 l_dbus_interface_method(interface, "Scan", 0, 5356 station_dbus_scan, "", ""); 5357 l_dbus_interface_method(interface, "RegisterSignalLevelAgent", 0, 5358 station_dbus_signal_agent_register, 5359 "", "oan", "path", "levels"); 5360 l_dbus_interface_method(interface, "UnregisterSignalLevelAgent", 0, 5361 station_dbus_signal_agent_unregister, 5362 "", "o", "path"); 5363 5364 l_dbus_interface_property(interface, "ConnectedNetwork", 0, "o", 5365 station_property_get_connected_network, 5366 NULL); 5367 l_dbus_interface_property(interface, "ConnectedAccessPoint", 0, "o", 5368 station_property_get_connected_ap, 5369 NULL); 5370 l_dbus_interface_property(interface, "Scanning", 0, "b", 5371 station_property_get_scanning, NULL); 5372 l_dbus_interface_property(interface, "State", 0, "s", 5373 station_property_get_state, NULL); 5374 l_dbus_interface_property(interface, "Affinities", 0, "ao", 5375 station_property_get_affinities, 5376 station_property_set_affinities); 5377 } 5378 5379 static void station_destroy_interface(void *user_data) 5380 { 5381 struct station *station = user_data; 5382 5383 station_free(station); 5384 } 5385 5386 static void station_get_diagnostic_cb( 5387 const struct diagnostic_station_info *info, 5388 void *user_data) 5389 { 5390 struct station *station = user_data; 5391 struct l_dbus_message *reply; 5392 struct l_dbus_message_builder *builder; 5393 struct handshake_state *hs = netdev_get_handshake(station->netdev); 5394 uint16_t channel_num; 5395 5396 if (!info) { 5397 reply = dbus_error_aborted(station->get_station_pending); 5398 goto done; 5399 } 5400 5401 channel_num = band_freq_to_channel(station->connected_bss->frequency, 5402 NULL); 5403 5404 reply = l_dbus_message_new_method_return(station->get_station_pending); 5405 5406 builder = l_dbus_message_builder_new(reply); 5407 5408 l_dbus_message_builder_enter_array(builder, "{sv}"); 5409 5410 dbus_append_dict_basic(builder, "ConnectedBss", 's', 5411 util_address_to_string(info->addr)); 5412 dbus_append_dict_basic(builder, "Frequency", 'u', 5413 &station->connected_bss->frequency); 5414 5415 if (channel_num != 0) 5416 dbus_append_dict_basic(builder, "Channel", 'q', &channel_num); 5417 5418 dbus_append_dict_basic(builder, "Security", 's', 5419 diagnostic_akm_suite_to_security(hs->akm_suite, 5420 hs->wpa_ie)); 5421 5422 if (hs->pairwise_cipher) { 5423 const char *str; 5424 5425 if (hs->pairwise_cipher == 5426 IE_RSN_CIPHER_SUITE_USE_GROUP_CIPHER) 5427 str = ie_rsn_cipher_suite_to_string(hs->group_cipher); 5428 else 5429 str = ie_rsn_cipher_suite_to_string( 5430 hs->pairwise_cipher); 5431 5432 if (str) 5433 dbus_append_dict_basic(builder, "PairwiseCipher", 5434 's', str); 5435 } 5436 5437 diagnostic_info_to_dict(info, builder); 5438 5439 l_dbus_message_builder_leave_array(builder); 5440 l_dbus_message_builder_finalize(builder); 5441 l_dbus_message_builder_destroy(builder); 5442 5443 done: 5444 dbus_pending_reply(&station->get_station_pending, reply); 5445 } 5446 5447 static void station_get_diagnostic_destroy(void *user_data) 5448 { 5449 struct station *station = user_data; 5450 struct l_dbus_message *reply; 5451 5452 if (station->get_station_pending) { 5453 reply = dbus_error_aborted(station->get_station_pending); 5454 dbus_pending_reply(&station->get_station_pending, reply); 5455 } 5456 } 5457 5458 static struct l_dbus_message *station_get_diagnostics(struct l_dbus *dbus, 5459 struct l_dbus_message *message, 5460 void *user_data) 5461 { 5462 struct station *station = user_data; 5463 int ret; 5464 5465 if (station->get_station_pending) 5466 return dbus_error_busy(message); 5467 5468 ret = netdev_get_current_station(station->netdev, 5469 station_get_diagnostic_cb, station, 5470 station_get_diagnostic_destroy); 5471 if (ret < 0) 5472 return dbus_error_from_errno(ret, message); 5473 5474 station->get_station_pending = l_dbus_message_ref(message); 5475 5476 return NULL; 5477 } 5478 5479 struct station_roam_data { 5480 struct station *station; 5481 struct l_dbus_message *pending; 5482 uint8_t bssid[6]; 5483 }; 5484 5485 static void station_force_roam_scan_triggered(int err, void *user_data) 5486 { 5487 struct station_roam_data *data = user_data; 5488 struct station *station = data->station; 5489 5490 if (err) 5491 station_roam_failed(station); 5492 } 5493 5494 static bool station_force_roam_scan_notify(int err, struct l_queue *bss_list, 5495 const struct scan_freq_set *freqs, 5496 void *user_data) 5497 { 5498 struct station_roam_data *data = user_data; 5499 struct station *station = data->station; 5500 struct scan_bss *target; 5501 struct l_dbus_message *reply; 5502 5503 if (err) { 5504 reply = dbus_error_from_errno(err, data->pending); 5505 goto reply; 5506 } 5507 5508 target = l_queue_remove_if(bss_list, bss_match_bssid, data->bssid); 5509 if (!target) { 5510 reply = dbus_error_not_found(data->pending); 5511 goto reply; 5512 } 5513 5514 l_debug("Attempting forced roam to BSS "MAC, MAC_STR(target->addr)); 5515 5516 /* The various roam routines expect this to be set from scanning */ 5517 station->preparing_roam = true; 5518 l_queue_push_tail(station->roam_bss_list, 5519 roam_bss_from_scan_bss(target, target->rank)); 5520 5521 station_update_roam_bss(station, target); 5522 5523 station_transition_start(station); 5524 5525 reply = l_dbus_message_new_method_return(data->pending); 5526 5527 reply: 5528 dbus_pending_reply(&data->pending, reply); 5529 5530 return false; 5531 } 5532 5533 static void station_force_roam_scan_destroy(void *user_data) 5534 { 5535 struct station_roam_data *data = user_data; 5536 5537 data->station->roam_scan_id = 0; 5538 5539 if (data->pending) 5540 l_dbus_message_unref(data->pending); 5541 5542 l_free(data); 5543 } 5544 5545 static struct l_dbus_message *station_force_roam(struct l_dbus *dbus, 5546 struct l_dbus_message *message, 5547 void *user_data) 5548 { 5549 struct station *station = user_data; 5550 struct scan_bss *target; 5551 struct l_dbus_message_iter iter; 5552 uint8_t *mac; 5553 uint32_t mac_len; 5554 struct scan_parameters params = { 0 }; 5555 struct scan_freq_set *freqs = NULL; 5556 struct station_roam_data *data; 5557 5558 if (!l_dbus_message_get_arguments(message, "ay", &iter)) 5559 goto invalid_args; 5560 5561 if (!l_dbus_message_iter_get_fixed_array(&iter, &mac, &mac_len)) 5562 goto invalid_args; 5563 5564 if (mac_len != 6) 5565 return dbus_error_invalid_args(message); 5566 5567 if (!station->connected_network) 5568 return dbus_error_not_connected(message); 5569 5570 target = network_bss_find_by_addr(station->connected_network, mac); 5571 if (!target) 5572 goto full_scan; 5573 5574 if (target && target == station->connected_bss) 5575 return dbus_error_already_exists(message); 5576 5577 if (station->connected_bss->ssid_len != target->ssid_len) 5578 goto invalid_args; 5579 5580 if (memcmp(station->connected_bss->ssid, target->ssid, 5581 target->ssid_len)) 5582 goto invalid_args; 5583 5584 /* 5585 * Always scan before a roam to ensure the kernel has the BSS in its 5586 * cache. If we already see the BSS only scan that frequency 5587 */ 5588 freqs = scan_freq_set_new(); 5589 scan_freq_set_add(freqs, target->frequency); 5590 5591 params.freqs = freqs; 5592 5593 full_scan: 5594 params.flush = true; 5595 5596 data = l_new(struct station_roam_data, 1); 5597 data->station = station; 5598 data->pending = l_dbus_message_ref(message); 5599 memcpy(data->bssid, mac, 6); 5600 5601 station->roam_scan_id = scan_active_full( 5602 netdev_get_wdev_id(station->netdev), 5603 ¶ms, 5604 station_force_roam_scan_triggered, 5605 station_force_roam_scan_notify, data, 5606 station_force_roam_scan_destroy); 5607 5608 if (freqs) 5609 scan_freq_set_free(freqs); 5610 5611 if (!station->roam_scan_id) { 5612 l_free(data); 5613 return dbus_error_failed(message); 5614 } 5615 5616 if (freqs) 5617 l_debug("Scanning on %u for BSS "MAC, target->frequency, 5618 MAC_STR(mac)); 5619 else 5620 l_debug("Full scan for BSS "MAC, MAC_STR(mac)); 5621 5622 return NULL; 5623 5624 invalid_args: 5625 return dbus_error_invalid_args(message); 5626 } 5627 5628 static struct network *station_find_network_from_bss(struct station *station, 5629 struct scan_bss *bss) 5630 { 5631 enum security security; 5632 char ssid[33]; 5633 5634 memcpy(ssid, bss->ssid, bss->ssid_len); 5635 ssid[bss->ssid_len] = '\0'; 5636 5637 if (scan_bss_get_security(bss, &security) < 0) 5638 return NULL; 5639 5640 return station_network_find(station, ssid, security); 5641 } 5642 5643 static void station_setup_diagnostic_interface( 5644 struct l_dbus_interface *interface) 5645 { 5646 l_dbus_interface_method(interface, "GetDiagnostics", 0, 5647 station_get_diagnostics, "a{sv}", "", 5648 "diagnostics"); 5649 } 5650 5651 static void station_destroy_diagnostic_interface(void *user_data) 5652 { 5653 } 5654 5655 static struct l_dbus_message *station_force_connect_bssid(struct l_dbus *dbus, 5656 struct l_dbus_message *message, 5657 void *user_data) 5658 { 5659 struct station *station = user_data; 5660 struct l_queue *bss_list; 5661 struct scan_bss *target; 5662 struct network *network; 5663 struct l_dbus_message_iter iter; 5664 uint8_t *mac; 5665 uint32_t mac_len; 5666 5667 if (!l_dbus_message_get_arguments(message, "ay", &iter)) 5668 goto invalid_args; 5669 5670 if (!l_dbus_message_iter_get_fixed_array(&iter, &mac, &mac_len)) 5671 goto invalid_args; 5672 5673 if (mac_len != 6) 5674 return dbus_error_invalid_args(message); 5675 5676 bss_list = station_get_bss_list(station); 5677 5678 target = l_queue_find(bss_list, bss_match_bssid, mac); 5679 if (!target) 5680 return dbus_error_invalid_args(message); 5681 5682 if (util_ssid_is_hidden(target->ssid_len, target->ssid)) 5683 return dbus_error_not_found(message); 5684 5685 network = station_find_network_from_bss(station, target); 5686 if (!network) 5687 return dbus_error_invalid_args(message); 5688 5689 l_debug("Attempting forced connection to BSS "MAC, MAC_STR(mac)); 5690 5691 return __network_connect(network, target, message); 5692 5693 invalid_args: 5694 return dbus_error_invalid_args(message); 5695 } 5696 5697 static void station_debug_scan_triggered(int err, void *user_data) 5698 { 5699 struct station *station = user_data; 5700 struct l_dbus_message *reply; 5701 5702 if (err < 0) { 5703 if (station->scan_pending) { 5704 reply = dbus_error_from_errno(err, 5705 station->scan_pending); 5706 dbus_pending_reply(&station->scan_pending, reply); 5707 } 5708 5709 station_dbus_scan_done(station, false); 5710 return; 5711 } 5712 5713 l_debug("debug scan triggered for %s", 5714 netdev_get_name(station->netdev)); 5715 5716 if (station->scan_pending) { 5717 reply = l_dbus_message_new_method_return(station->scan_pending); 5718 l_dbus_message_set_arguments(reply, ""); 5719 dbus_pending_reply(&station->scan_pending, reply); 5720 } 5721 5722 station_property_set_scanning(station, true); 5723 } 5724 5725 static bool station_debug_scan_results(int err, struct l_queue *bss_list, 5726 const struct scan_freq_set *freqs, 5727 void *userdata) 5728 { 5729 struct station *station = userdata; 5730 5731 if (err) { 5732 station_dbus_scan_done(station, false); 5733 return false; 5734 } 5735 5736 station_set_scan_results(station, bss_list, freqs, false); 5737 station_dbus_scan_done(station, false); 5738 5739 return true; 5740 } 5741 5742 static struct l_dbus_message *station_debug_scan(struct l_dbus *dbus, 5743 struct l_dbus_message *message, 5744 void *user_data) 5745 { 5746 struct station *station = user_data; 5747 struct l_dbus_message_iter iter; 5748 uint16_t *freqs; 5749 uint32_t freqs_len; 5750 struct scan_freq_set *freq_set; 5751 unsigned int i; 5752 5753 if (station->dbus_scan_id) 5754 return dbus_error_busy(message); 5755 5756 if (L_IN_SET(station->state, STATION_STATE_CONNECTING, 5757 STATION_STATE_CONNECTING_AUTO, 5758 STATION_STATE_NETCONFIG) || 5759 station_is_roaming(station)) 5760 return dbus_error_busy(message); 5761 5762 if (!l_dbus_message_get_arguments(message, "aq", &iter)) 5763 goto invalid_args; 5764 5765 if (!l_dbus_message_iter_get_fixed_array(&iter, &freqs, &freqs_len)) 5766 goto invalid_args; 5767 5768 freq_set = scan_freq_set_new(); 5769 5770 for (i = 0; i < freqs_len; i++) { 5771 if (scan_freq_set_contains(freq_set, (uint32_t)freqs[i])) 5772 continue; 5773 5774 if (!scan_freq_set_add(freq_set, (uint32_t)freqs[i])) { 5775 scan_freq_set_free(freq_set); 5776 goto invalid_args; 5777 } 5778 5779 l_debug("added frequency %u", freqs[i]); 5780 } 5781 5782 station->dbus_scan_id = station_scan_trigger(station, freq_set, 5783 station_debug_scan_triggered, 5784 station_debug_scan_results, 5785 NULL); 5786 5787 scan_freq_set_free(freq_set); 5788 5789 if (!station->dbus_scan_id) 5790 goto failed; 5791 5792 station->scan_pending = l_dbus_message_ref(message); 5793 5794 return NULL; 5795 5796 failed: 5797 return dbus_error_failed(message); 5798 invalid_args: 5799 return dbus_error_invalid_args(message); 5800 } 5801 5802 static bool station_property_get_autoconnect(struct l_dbus *dbus, 5803 struct l_dbus_message *message, 5804 struct l_dbus_message_builder *builder, 5805 void *user_data) 5806 { 5807 struct station *station = user_data; 5808 bool autoconnect; 5809 5810 autoconnect = station->autoconnect; 5811 5812 l_dbus_message_builder_append_basic(builder, 'b', &autoconnect); 5813 5814 return true; 5815 } 5816 5817 static struct l_dbus_message *station_property_set_autoconnect( 5818 struct l_dbus *dbus, 5819 struct l_dbus_message *message, 5820 struct l_dbus_message_iter *new_value, 5821 l_dbus_property_complete_cb_t complete, 5822 void *user_data) 5823 { 5824 struct station *station = user_data; 5825 bool autoconnect; 5826 5827 if (!l_dbus_message_iter_get_variant(new_value, "b", &autoconnect)) 5828 return dbus_error_invalid_args(message); 5829 5830 l_debug("Setting autoconnect %s", autoconnect ? "true" : "false"); 5831 5832 station_set_autoconnect(station, autoconnect); 5833 5834 return l_dbus_message_new_method_return(message); 5835 } 5836 5837 static void station_append_byte_array(struct l_dbus_message_builder *builder, 5838 const char *name, 5839 const uint8_t *bytes, size_t len) 5840 { 5841 size_t i; 5842 5843 l_dbus_message_builder_enter_dict(builder, "sv"); 5844 l_dbus_message_builder_append_basic(builder, 's', name); 5845 l_dbus_message_builder_enter_variant(builder, "ay"); 5846 l_dbus_message_builder_enter_array(builder, "y"); 5847 5848 for (i = 0; i < len; i++) 5849 l_dbus_message_builder_append_basic(builder, 'y', &bytes[i]); 5850 5851 l_dbus_message_builder_leave_array(builder); 5852 l_dbus_message_builder_leave_variant(builder); 5853 l_dbus_message_builder_leave_dict(builder); 5854 } 5855 5856 static void station_append_bss_list(struct l_dbus_message_builder *builder, 5857 const struct l_queue_entry *entry) 5858 { 5859 for (; entry; entry = entry->next) { 5860 struct scan_bss *bss = entry->data; 5861 int32_t rssi = bss->signal_strength / 100; 5862 5863 l_dbus_message_builder_enter_array(builder, "{sv}"); 5864 5865 dbus_append_dict_basic(builder, "Frequency", 'u', 5866 &bss->frequency); 5867 dbus_append_dict_basic(builder, "RSSI", 'i', 5868 &rssi); 5869 dbus_append_dict_basic(builder, "Rank", 'q', &bss->rank); 5870 5871 dbus_append_dict_basic(builder, "Address", 's', 5872 util_address_to_string(bss->addr)); 5873 5874 station_append_byte_array(builder, "MDE", bss->mde, 3); 5875 5876 l_dbus_message_builder_leave_array(builder); 5877 } 5878 } 5879 5880 static struct l_dbus_message *station_debug_get_networks(struct l_dbus *dbus, 5881 struct l_dbus_message *message, 5882 void *user_data) 5883 { 5884 struct station *station = user_data; 5885 struct l_dbus_message *reply = 5886 l_dbus_message_new_method_return(message); 5887 struct l_dbus_message_builder *builder = 5888 l_dbus_message_builder_new(reply); 5889 const struct l_queue_entry *entry; 5890 5891 l_dbus_message_builder_enter_array(builder, "{oaa{sv}}"); 5892 5893 if (l_queue_isempty(station->networks_sorted)) 5894 goto done; 5895 5896 for (entry = l_queue_get_entries(station->networks_sorted); entry; 5897 entry = entry->next) { 5898 const struct network *network = entry->data; 5899 5900 l_dbus_message_builder_enter_dict(builder, "oaa{sv}"); 5901 l_dbus_message_builder_append_basic(builder, 'o', 5902 network_get_path(network)); 5903 l_dbus_message_builder_enter_array(builder, "a{sv}"); 5904 5905 station_append_bss_list(builder, 5906 network_bss_list_get_entries(network)); 5907 5908 l_dbus_message_builder_leave_array(builder); 5909 l_dbus_message_builder_leave_dict(builder); 5910 } 5911 5912 done: 5913 l_dbus_message_builder_leave_array(builder); 5914 5915 l_dbus_message_builder_finalize(builder); 5916 l_dbus_message_builder_destroy(builder); 5917 5918 return reply; 5919 } 5920 5921 static void station_setup_debug_interface( 5922 struct l_dbus_interface *interface) 5923 { 5924 l_dbus_interface_method(interface, "ConnectBssid", 0, 5925 station_force_connect_bssid, "", "ay", 5926 "mac"); 5927 l_dbus_interface_method(interface, "Roam", 0, 5928 station_force_roam, "", "ay", "mac"); 5929 5930 l_dbus_interface_method(interface, "Scan", 0, 5931 station_debug_scan, "", "aq", 5932 "frequencies"); 5933 l_dbus_interface_method(interface, "GetNetworks", 0, 5934 station_debug_get_networks, "a{oaa{sv}}", "", 5935 "networks"); 5936 5937 l_dbus_interface_signal(interface, "Event", 0, "sav", "name", "data"); 5938 5939 l_dbus_interface_property(interface, "AutoConnect", 0, "b", 5940 station_property_get_autoconnect, 5941 station_property_set_autoconnect); 5942 } 5943 5944 static void ap_roam_frame_event(const struct mmpdu_header *hdr, 5945 const void *body, size_t body_len, 5946 int rssi, void *user_data) 5947 { 5948 uint32_t ifindex = L_PTR_TO_UINT(user_data); 5949 struct station *station = station_find(ifindex); 5950 5951 if (!station) 5952 return; 5953 5954 station_ap_directed_roam(station, hdr, body, body_len); 5955 } 5956 5957 static void add_frame_watches(struct netdev *netdev) 5958 { 5959 static const uint8_t action_ap_roam_prefix[2] = { 0x0a, 0x07 }; 5960 5961 /* 5962 * register for AP roam transition watch 5963 */ 5964 frame_watch_add(netdev_get_wdev_id(netdev), 0, 0x00d0, 5965 action_ap_roam_prefix, sizeof(action_ap_roam_prefix), 5966 false, ap_roam_frame_event, 5967 L_UINT_TO_PTR(netdev_get_ifindex(netdev)), NULL); 5968 } 5969 5970 static void station_netdev_watch(struct netdev *netdev, 5971 enum netdev_watch_event event, void *userdata) 5972 { 5973 switch (event) { 5974 case NETDEV_WATCH_EVENT_NEW: 5975 if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION) { 5976 add_frame_watches(netdev); 5977 5978 if (netdev_get_is_up(netdev)) 5979 station_create(netdev); 5980 } 5981 break; 5982 case NETDEV_WATCH_EVENT_UP: 5983 if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION) 5984 station_create(netdev); 5985 5986 break; 5987 #ifdef HAVE_DBUS 5988 case NETDEV_WATCH_EVENT_DOWN: 5989 case NETDEV_WATCH_EVENT_DEL: 5990 l_dbus_object_remove_interface(dbus_get_bus(), 5991 netdev_get_path(netdev), 5992 IWD_STATION_INTERFACE); 5993 break; 5994 #endif 5995 case NETDEV_WATCH_EVENT_IFTYPE_CHANGE: 5996 if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION) 5997 add_frame_watches(netdev); 5998 5999 break; 6000 default: 6001 break; 6002 } 6003 } 6004 6005 static void station_known_networks_changed(enum known_networks_event event, 6006 const struct network_info *info, 6007 void *user_data) 6008 { 6009 _auto_(l_free) char *network_id = NULL; 6010 6011 if (event != KNOWN_NETWORKS_EVENT_REMOVED) 6012 return; 6013 6014 if (info->type != SECURITY_8021X) 6015 return; 6016 6017 network_id = l_util_hexstring(info->ssid, strlen(info->ssid)); 6018 eap_tls_forget_peer(network_id); 6019 } 6020 6021 static int station_init(void) 6022 { 6023 if (scan_get_band_rank_modifier(BAND_FREQ_2_4_GHZ)) 6024 allowed_bands |= BAND_FREQ_2_4_GHZ; 6025 if (scan_get_band_rank_modifier(BAND_FREQ_5_GHZ)) 6026 allowed_bands |= BAND_FREQ_5_GHZ; 6027 if (scan_get_band_rank_modifier(BAND_FREQ_6_GHZ)) 6028 allowed_bands |= BAND_FREQ_6_GHZ; 6029 6030 if (!(allowed_bands & (BAND_FREQ_2_4_GHZ | BAND_FREQ_5_GHZ))) { 6031 l_error("At least 2.4GHz and 5GHz bands must be enabled for " 6032 "IWD to start, check [Rank].BandModifier* setting"); 6033 return -ENOTSUP; 6034 } 6035 6036 station_list = l_queue_new(); 6037 netdev_watch = netdev_watch_add(station_netdev_watch, NULL, NULL); 6038 #ifdef HAVE_DBUS 6039 l_dbus_register_interface(dbus_get_bus(), IWD_STATION_INTERFACE, 6040 station_setup_interface, 6041 station_destroy_interface, false); 6042 l_dbus_register_interface(dbus_get_bus(), 6043 IWD_STATION_DIAGNOSTIC_INTERFACE, 6044 station_setup_diagnostic_interface, 6045 station_destroy_diagnostic_interface, 6046 false); 6047 if (iwd_is_developer_mode()) 6048 l_dbus_register_interface(dbus_get_bus(), 6049 IWD_STATION_DEBUG_INTERFACE, 6050 station_setup_debug_interface, 6051 NULL, 6052 false); 6053 #endif 6054 6055 if (!l_settings_get_uint(iwd_get_config(), "General", 6056 "ManagementFrameProtection", 6057 &mfp_setting)) 6058 mfp_setting = 1; 6059 6060 if (mfp_setting > 2) { 6061 l_error("Invalid [General].ManagementFrameProtection value: %d," 6062 " using default of 1", mfp_setting); 6063 mfp_setting = 1; 6064 } 6065 6066 if (!l_settings_get_uint(iwd_get_config(), "General", 6067 "RoamRetryInterval", 6068 &roam_retry_interval)) 6069 roam_retry_interval = 60; 6070 6071 if (roam_retry_interval > INT_MAX) 6072 roam_retry_interval = INT_MAX; 6073 6074 if (!l_settings_get_bool(iwd_get_config(), "General", "DisableANQP", 6075 &anqp_disabled)) 6076 anqp_disabled = true; 6077 6078 if (!l_settings_get_bool(iwd_get_config(), "General", "DisablePMKSA", 6079 &pmksa_disabled)) 6080 pmksa_disabled = false; 6081 6082 if (!netconfig_enabled()) 6083 l_info("station: Network configuration is disabled."); 6084 6085 supports_arp_evict_nocarrier = sysfs_supports_ipv4_setting("all", 6086 "arp_evict_nocarrier"); 6087 supports_ndisc_evict_nocarrier = sysfs_supports_ipv6_setting("all", 6088 "ndisc_evict_nocarrier"); 6089 supports_drop_gratuitous_arp = sysfs_supports_ipv4_setting("all", 6090 "drop_gratuitous_arp"); 6091 supports_drop_unsolicited_na = sysfs_supports_ipv6_setting("all", 6092 "drop_unsolicited_na"); 6093 supports_ipv4_drop_unicast_in_l2_multicast = 6094 sysfs_supports_ipv4_setting("all", 6095 "drop_unicast_in_l2_multicast"); 6096 supports_ipv6_drop_unicast_in_l2_multicast = 6097 sysfs_supports_ipv6_setting("all", 6098 "drop_unicast_in_l2_multicast"); 6099 6100 watchlist_init(&event_watches, NULL); 6101 6102 eap_tls_set_session_cache_ops(storage_eap_tls_cache_load, 6103 storage_eap_tls_cache_sync); 6104 known_networks_watch = known_networks_watch_add( 6105 station_known_networks_changed, 6106 NULL, NULL); 6107 6108 return 0; 6109 } 6110 6111 static void station_exit(void) 6112 { 6113 #ifdef HAVE_DBUS 6114 l_dbus_unregister_interface(dbus_get_bus(), 6115 IWD_STATION_DIAGNOSTIC_INTERFACE); 6116 if (iwd_is_developer_mode()) 6117 l_dbus_unregister_interface(dbus_get_bus(), 6118 IWD_STATION_DEBUG_INTERFACE); 6119 l_dbus_unregister_interface(dbus_get_bus(), IWD_STATION_INTERFACE); 6120 #endif 6121 netdev_watch_remove(netdev_watch); 6122 l_queue_destroy(station_list, NULL); 6123 station_list = NULL; 6124 watchlist_destroy(&event_watches); 6125 known_networks_watch_remove(known_networks_watch); 6126 known_networks_watch = 0; 6127 } 6128 6129 IWD_MODULE(station, station_init, station_exit) 6130 IWD_MODULE_DEPENDS(station, known_networks) 6131 IWD_MODULE_DEPENDS(station, netdev); 6132 IWD_MODULE_DEPENDS(station, netconfig); 6133 IWD_MODULE_DEPENDS(station, frame_xchg); 6134 IWD_MODULE_DEPENDS(station, wiphy);