knownnetworks.c (29373B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2016-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <sys/types.h> 29 #include <dirent.h> 30 #include <errno.h> 31 #include <stdio.h> 32 #include <unistd.h> 33 #include <limits.h> 34 #include <stdlib.h> 35 36 #include <ell/ell.h> 37 38 #include "ell/useful.h" 39 #include "src/iwd.h" 40 #include "src/module.h" 41 #include "src/storage.h" 42 #include "src/common.h" 43 #include "src/network.h" 44 #include "src/dbus.h" 45 #include "src/knownnetworks.h" 46 #include "src/scan.h" 47 #include "src/util.h" 48 #include "src/watchlist.h" 49 #include "src/band.h" 50 51 static struct l_queue *known_networks; 52 static size_t num_known_hidden_networks; 53 static struct l_dir_watch *storage_dir_watch; 54 static struct watchlist known_network_watches; 55 static struct l_settings *known_freqs; 56 57 void __network_config_parse(const struct l_settings *settings, 58 const char *full_path, 59 struct network_config *config) 60 { 61 bool b; 62 const char *value; 63 uint8_t new_addr[6]; 64 65 memset(config, 0, sizeof(struct network_config)); 66 67 config->connected_time = l_path_get_mtime(full_path); 68 69 /* If no entry, default to AutoConnectable=True */ 70 if (!l_settings_get_bool(settings, NET_AUTOCONNECT, &b)) 71 b = true; 72 73 config->is_autoconnectable = b; 74 75 if (!l_settings_get_bool(settings, NET_HIDDEN, &b)) 76 b = false; 77 78 config->is_hidden = b; 79 80 if (!l_settings_get_bool(settings, NET_ALWAYS_RANDOMIZE_ADDRESS, &b)) 81 b = false; 82 83 config->always_random_addr = b; 84 85 value = l_settings_get_value(settings, NET_ADDRESS_OVERRIDE); 86 if (value) { 87 if (util_string_to_address(value, new_addr) && 88 util_is_valid_sta_address(new_addr)) { 89 config->override_addr = true; 90 memcpy(config->sta_addr, new_addr, sizeof(new_addr)); 91 } else 92 l_warn("[%s].%s is not a valid MAC address", 93 NET_ADDRESS_OVERRIDE); 94 } 95 96 if (config->override_addr && config->always_random_addr) { 97 l_warn("Cannot use both [%s].%s and [%s].%s, using latter", 98 NET_ALWAYS_RANDOMIZE_ADDRESS, 99 NET_ADDRESS_OVERRIDE); 100 config->always_random_addr = false; 101 } 102 103 if (!l_settings_get_bool(settings, NET_TRANSITION_DISABLE, &b)) 104 b = false; 105 106 config->have_transition_disable = b; 107 if (config->have_transition_disable) { 108 unsigned int i; 109 char **modes = l_settings_get_string_list(settings, 110 NET_TRANSITION_DISABLE_MODES, ' '); 111 112 for (i = 0; modes && modes[i]; i++) { 113 if (!strcmp(modes[i], "personal")) 114 set_bit(&config->transition_disable, 0); 115 else if (!strcmp(modes[i], "enterprise")) 116 set_bit(&config->transition_disable, 2); 117 else if (!strcmp(modes[i], "open")) 118 set_bit(&config->transition_disable, 3); 119 else 120 l_warn("[%s].%s: Unrecognized value: %s", 121 NET_TRANSITION_DISABLE_MODES, 122 modes[i]); 123 } 124 125 l_strfreev(modes); 126 } 127 128 if (l_settings_has_key(settings, NET_USE_DEFAULT_ECC_GROUP)) { 129 if (l_settings_get_bool(settings, 130 NET_USE_DEFAULT_ECC_GROUP, &b)) { 131 config->ecc_group = b ? KNOWN_NETWORK_ECC_GROUP_DEFAULT 132 : KNOWN_NETWORK_ECC_GROUP_MOST_SECURE; 133 } else 134 l_warn("[%s].%s is not a boolean value", 135 NET_USE_DEFAULT_ECC_GROUP); 136 } else 137 config->ecc_group = KNOWN_NETWORK_ECC_GROUP_AUTO; 138 } 139 140 void __network_info_init(struct network_info *info, 141 const char *ssid, enum security security, 142 struct network_config *config) 143 { 144 if (ssid) 145 strcpy(info->ssid, ssid); 146 147 info->type = security; 148 149 memcpy(&info->config, config, sizeof(struct network_config)); 150 151 if (info->config.is_hidden) 152 num_known_hidden_networks++; 153 } 154 155 static void network_info_free(void *data) 156 { 157 struct network_info *network = data; 158 159 l_queue_destroy(network->known_frequencies, l_free); 160 161 network->ops->free(network); 162 } 163 164 static int connected_time_compare(const void *a, const void *b, void *user_data) 165 { 166 const struct network_info *ni_a = a; 167 const struct network_info *ni_b = b; 168 uint64_t ta = ni_a->config.connected_time; 169 uint64_t tb = ni_b->config.connected_time; 170 171 if (l_time_after(ta, tb)) 172 return -1; 173 else if (l_time_before(ta, tb)) 174 return 1; 175 176 return 0; 177 } 178 179 static const char *known_network_get_path(const struct network_info *network) 180 { 181 static char path[256]; 182 unsigned int pos = 0, i; 183 184 L_WARN_ON((pos = snprintf(path, sizeof(path), "%s/", 185 IWD_BASE_PATH)) >= (int) sizeof(path)); 186 187 for (i = 0; network->ssid[i] && pos < sizeof(path); i++) 188 pos += snprintf(path + pos, sizeof(path) - pos, "%02x", 189 (unsigned char)network->ssid[i]); 190 191 if (pos < sizeof(path)) 192 snprintf(path + pos, sizeof(path) - pos, "_%s", 193 security_to_str(network->type)); 194 path[sizeof(path) - 1] = '\0'; 195 196 return path; 197 } 198 199 /* 200 * Finds the position n of this network_info in the list of known networks 201 * sorted by connected_time. E.g. an offset of 0 means the most recently 202 * used network. Only networks with seen_count > 0 are considered. E.g. 203 * only networks that appear in scan results on at least one wifi card. 204 * 205 * Returns -ENOENT if the entry couldn't be found. 206 */ 207 int known_network_offset(const struct network_info *target) 208 { 209 const struct l_queue_entry *entry; 210 const struct network_info *info; 211 int n = 0; 212 213 for (entry = l_queue_get_entries(known_networks); entry; 214 entry = entry->next) { 215 info = entry->data; 216 if (target == info) 217 return n; 218 219 if (info->seen_count) 220 n += 1; 221 } 222 223 return -ENOENT; 224 } 225 226 static void known_network_register_dbus(struct network_info *network) 227 { 228 const char *path = known_network_get_path(network); 229 230 if (!l_dbus_object_add_interface(dbus_get_bus(), path, 231 IWD_KNOWN_NETWORK_INTERFACE, network)) 232 l_info("Unable to register %s interface", 233 IWD_KNOWN_NETWORK_INTERFACE); 234 235 if (!l_dbus_object_add_interface(dbus_get_bus(), path, 236 L_DBUS_INTERFACE_PROPERTIES, network)) 237 l_info("Unable to register %s interface", 238 L_DBUS_INTERFACE_PROPERTIES); 239 } 240 241 static void known_network_set_autoconnect(struct network_info *network, 242 bool autoconnect) 243 { 244 if (network->config.is_autoconnectable == autoconnect) 245 return; 246 247 network->config.is_autoconnectable = autoconnect; 248 249 #ifdef HAVE_DBUS 250 l_dbus_property_changed(dbus_get_bus(), known_network_get_path(network), 251 IWD_KNOWN_NETWORK_INTERFACE, "AutoConnect"); 252 #endif 253 } 254 255 static int known_network_touch(struct network_info *info) 256 { 257 return storage_network_touch(info->type, info->ssid); 258 } 259 260 static struct l_settings *known_network_open(struct network_info *info) 261 { 262 return storage_network_open(info->type, info->ssid); 263 } 264 265 static void known_network_sync(struct network_info *info, 266 struct l_settings *settings) 267 { 268 storage_network_sync(info->type, info->ssid, settings); 269 } 270 271 static void known_network_remove(struct network_info *info) 272 { 273 storage_network_remove(info->type, info->ssid); 274 } 275 276 static void known_network_free(struct network_info *info) 277 { 278 l_free(info); 279 } 280 281 static const char *known_network_get_name(const struct network_info *info) 282 { 283 return info->ssid; 284 } 285 286 static const char *known_network_get_type(const struct network_info *info) 287 { 288 return security_to_str(info->type); 289 } 290 291 static char *known_network_get_file_path(const struct network_info *info) 292 { 293 return storage_get_network_file_path(info->type, info->ssid); 294 } 295 296 static struct network_info_ops known_network_ops = { 297 .open = known_network_open, 298 .touch = known_network_touch, 299 .sync = known_network_sync, 300 .remove = known_network_remove, 301 .free = known_network_free, 302 .get_path = known_network_get_path, 303 .get_name = known_network_get_name, 304 .get_type = known_network_get_type, 305 .get_file_path = known_network_get_file_path, 306 }; 307 308 struct l_settings *network_info_open_settings(struct network_info *info) 309 { 310 return info->ops->open(info); 311 } 312 313 int network_info_touch(struct network_info *info) 314 { 315 return info->ops->touch(info); 316 } 317 318 const char *network_info_get_path(const struct network_info *info) 319 { 320 return info->ops->get_path(info); 321 } 322 323 const char *network_info_get_name(const struct network_info *info) 324 { 325 return info->ops->get_name(info); 326 } 327 328 const char *network_info_get_type(const struct network_info *info) 329 { 330 return info->ops->get_type(info); 331 } 332 333 const struct iovec *network_info_get_extra_ies(const struct network_info *info, 334 struct scan_bss *bss, 335 size_t *num_elems) 336 { 337 if (!info || !info->ops->get_extra_ies) 338 return NULL; 339 340 return info->ops->get_extra_ies(info, bss, num_elems); 341 } 342 343 const uint8_t *network_info_get_uuid(struct network_info *info) 344 { 345 char *file_path; 346 char *to_hash; 347 /* 348 * 16 bytes of randomness. Since we only care about a unique value there 349 * is no need to use any special pre-defined namespace. 350 */ 351 static const uint8_t nsid[16] = { 352 0xfd, 0x88, 0x6f, 0x1e, 0xdf, 0x02, 0xd7, 0x8b, 353 0xc4, 0x90, 0x30, 0x59, 0x73, 0x8a, 0x86, 0x0d 354 }; 355 356 if (info->has_uuid) 357 return info->uuid; 358 359 file_path = info->ops->get_file_path(info); 360 361 /* 362 * This will generate a UUID based on file path and mtime. This 363 * is done so we can get a different UUID if the network has 364 * been forgotten. 365 */ 366 to_hash = l_strdup_printf("%s_%" PRIu64, file_path, 367 info->config.connected_time); 368 l_uuid_v5(nsid, to_hash, strlen(to_hash), info->uuid); 369 l_free(to_hash); 370 l_free(file_path); 371 372 info->has_uuid = true; 373 374 return info->uuid; 375 } 376 377 void network_info_set_uuid(struct network_info *info, const uint8_t *uuid) 378 { 379 memcpy(info->uuid, uuid, 16); 380 info->has_uuid = true; 381 } 382 383 struct scan_freq_set *network_info_get_roam_frequencies( 384 const struct network_info *info, 385 uint32_t current_freq, 386 uint8_t max) 387 { 388 struct scan_freq_set *freqs; 389 const struct l_queue_entry *entry; 390 391 freqs = scan_freq_set_new(); 392 393 for (entry = l_queue_get_entries(info->known_frequencies); entry && max; 394 entry = entry->next) { 395 struct known_frequency *kn = entry->data; 396 397 if (kn->frequency == current_freq) 398 continue; 399 400 scan_freq_set_add(freqs, kn->frequency); 401 402 max--; 403 } 404 405 if (scan_freq_set_isempty(freqs)) { 406 scan_freq_set_free(freqs); 407 return NULL; 408 } 409 410 return freqs; 411 } 412 413 bool network_info_match_hessid(const struct network_info *info, 414 const uint8_t *hessid) 415 { 416 if (!info->ops->match_hessid) 417 return false; 418 419 return info->ops->match_hessid(info, hessid); 420 } 421 422 const uint8_t *network_info_match_roaming_consortium( 423 const struct network_info *info, 424 const uint8_t *rc, 425 size_t rc_len, 426 size_t *rc_len_out) 427 { 428 if (!info->ops->match_roaming_consortium) 429 return NULL; 430 431 return info->ops->match_roaming_consortium(info, rc, rc_len, 432 rc_len_out); 433 } 434 435 bool network_info_match_nai_realm(const struct network_info *info, 436 const char **nai_realms) 437 { 438 if (!info->ops->match_nai_realms) 439 return false; 440 441 return info->ops->match_nai_realms(info, nai_realms); 442 } 443 444 void known_network_set_connected_time(struct network_info *network, 445 uint64_t connected_time) 446 { 447 if (network->config.connected_time == connected_time) 448 return; 449 450 network->config.connected_time = connected_time; 451 452 #ifdef HAVE_DBUS 453 l_dbus_property_changed(dbus_get_bus(), 454 known_network_get_path(network), 455 IWD_KNOWN_NETWORK_INTERFACE, 456 "LastConnectedTime"); 457 #endif 458 459 l_queue_remove(known_networks, network); 460 l_queue_insert(known_networks, network, connected_time_compare, NULL); 461 } 462 463 void known_network_update(struct network_info *network, 464 struct network_config *new) 465 { 466 struct network_config *old = &network->config; 467 468 known_network_set_connected_time(network, new->connected_time); 469 470 if (old->is_hidden != new->is_hidden) { 471 if (old->is_hidden && !new->is_hidden) 472 num_known_hidden_networks--; 473 else if (!old->is_hidden && new->is_hidden) 474 num_known_hidden_networks++; 475 476 #ifdef HAVE_DBUS 477 l_dbus_property_changed(dbus_get_bus(), 478 known_network_get_path(network), 479 IWD_KNOWN_NETWORK_INTERFACE, 480 "Hidden"); 481 #endif 482 483 old->is_hidden = new->is_hidden; 484 } 485 486 known_network_set_autoconnect(network, new->is_autoconnectable); 487 488 memcpy(&network->config, new, sizeof(struct network_config)); 489 490 WATCHLIST_NOTIFY(&known_network_watches, 491 known_networks_watch_func_t, 492 KNOWN_NETWORKS_EVENT_UPDATED, network); 493 } 494 495 bool known_networks_foreach(known_networks_foreach_func_t function, 496 void *user_data) 497 { 498 const struct l_queue_entry *entry; 499 500 for (entry = l_queue_get_entries(known_networks); entry; 501 entry = entry->next) 502 if (!function(entry->data, user_data)) 503 break; 504 505 return !entry; 506 } 507 508 bool known_networks_has_hidden(void) 509 { 510 return num_known_hidden_networks ? true : false; 511 } 512 513 static bool network_info_match(const void *a, const void *b) 514 { 515 const struct network_info *ni_a = a; 516 const struct network_info *ni_b = b; 517 518 if (ni_a->type != ni_b->type) 519 return false; 520 521 if (strcmp(ni_a->ssid, ni_b->ssid)) 522 return false; 523 524 return true; 525 } 526 527 struct network_info *known_networks_find(const char *ssid, 528 enum security security) 529 { 530 struct network_info query; 531 532 query.type = security; 533 strcpy(query.ssid, ssid); 534 535 return l_queue_find(known_networks, network_info_match, &query); 536 } 537 538 static void known_network_append_frequencies(const struct network_info *info, 539 struct scan_freq_set *set, 540 uint8_t max) 541 { 542 const struct l_queue_entry *entry; 543 544 for (entry = l_queue_get_entries(info->known_frequencies); entry && max; 545 entry = entry->next, max--) { 546 const struct known_frequency *known_freq = entry->data; 547 548 scan_freq_set_add(set, known_freq->frequency); 549 } 550 } 551 552 struct scan_freq_set *known_networks_get_recent_frequencies( 553 uint8_t num_networks_tosearch, 554 uint8_t freqs_per_network) 555 { 556 /* 557 * This search function assumes that the known networks are always 558 * sorted by the last connection time with the most recent ones being on 559 * top. Therefore, we just need to get the top NUM of networks from the 560 * list. 561 */ 562 const struct l_queue_entry *network_entry; 563 struct scan_freq_set *set; 564 565 if (!num_networks_tosearch || !freqs_per_network) 566 return NULL; 567 568 set = scan_freq_set_new(); 569 570 for (network_entry = l_queue_get_entries(known_networks); 571 network_entry && num_networks_tosearch; 572 network_entry = network_entry->next, 573 num_networks_tosearch--) { 574 const struct network_info *network = network_entry->data; 575 576 known_network_append_frequencies(network, set, 577 freqs_per_network); 578 } 579 580 return set; 581 } 582 583 static bool known_frequency_match(const void *a, const void *b) 584 { 585 const struct known_frequency *known_freq = a; 586 const uint32_t *frequency = b; 587 588 return known_freq->frequency == *frequency; 589 } 590 591 /* 592 * Adds a frequency to the 'known' set of frequencies that this network 593 * operates on. The list is sorted according to most-recently seen 594 */ 595 int known_network_add_frequency(struct network_info *info, uint32_t frequency) 596 { 597 struct known_frequency *known_freq; 598 599 if (!info->known_frequencies) 600 info->known_frequencies = l_queue_new(); 601 602 known_freq = l_queue_remove_if(info->known_frequencies, 603 known_frequency_match, &frequency); 604 if (!known_freq) { 605 known_freq = l_new(struct known_frequency, 1); 606 known_freq->frequency = frequency; 607 } 608 609 l_queue_push_head(info->known_frequencies, known_freq); 610 611 return 0; 612 } 613 614 static struct l_dbus_message *known_network_forget(struct l_dbus *dbus, 615 struct l_dbus_message *message, 616 void *user_data) 617 { 618 struct network_info *network = user_data; 619 struct l_dbus_message *reply; 620 621 /* Other actions taken care of by the filesystem watch callback */ 622 network->ops->remove(network); 623 624 reply = l_dbus_message_new_method_return(message); 625 l_dbus_message_set_arguments(reply, ""); 626 627 return reply; 628 } 629 630 static bool known_network_property_get_name(struct l_dbus *dbus, 631 struct l_dbus_message *message, 632 struct l_dbus_message_builder *builder, 633 void *user_data) 634 { 635 struct network_info *network = user_data; 636 637 l_dbus_message_builder_append_basic(builder, 's', 638 network_info_get_name(network)); 639 640 return true; 641 } 642 643 static bool known_network_property_get_type(struct l_dbus *dbus, 644 struct l_dbus_message *message, 645 struct l_dbus_message_builder *builder, 646 void *user_data) 647 { 648 struct network_info *network = user_data; 649 650 l_dbus_message_builder_append_basic(builder, 's', 651 network_info_get_type(network)); 652 653 return true; 654 } 655 656 static bool known_network_property_get_hidden(struct l_dbus *dbus, 657 struct l_dbus_message *message, 658 struct l_dbus_message_builder *builder, 659 void *user_data) 660 { 661 struct network_info *network = user_data; 662 bool is_hidden = network->config.is_hidden; 663 664 l_dbus_message_builder_append_basic(builder, 'b', &is_hidden); 665 666 return true; 667 } 668 669 static bool known_network_property_get_autoconnect(struct l_dbus *dbus, 670 struct l_dbus_message *message, 671 struct l_dbus_message_builder *builder, 672 void *user_data) 673 { 674 struct network_info *network = user_data; 675 bool autoconnect = network->config.is_autoconnectable; 676 677 l_dbus_message_builder_append_basic(builder, 'b', &autoconnect); 678 679 return true; 680 } 681 682 static struct l_dbus_message *known_network_property_set_autoconnect( 683 struct l_dbus *dbus, 684 struct l_dbus_message *message, 685 struct l_dbus_message_iter *new_value, 686 l_dbus_property_complete_cb_t complete, 687 void *user_data) 688 { 689 struct network_info *network = user_data; 690 struct l_settings *settings; 691 bool autoconnect; 692 693 if (!l_dbus_message_iter_get_variant(new_value, "b", &autoconnect)) 694 return dbus_error_invalid_args(message); 695 696 if (network->config.is_autoconnectable == autoconnect) 697 return l_dbus_message_new_method_return(message); 698 699 settings = network->ops->open(network); 700 if (!settings) 701 return dbus_error_failed(message); 702 703 l_settings_set_bool(settings, NET_AUTOCONNECT, autoconnect); 704 705 network->ops->sync(network, settings); 706 l_settings_free(settings); 707 708 return l_dbus_message_new_method_return(message); 709 } 710 711 static bool known_network_property_get_last_connected(struct l_dbus *dbus, 712 struct l_dbus_message *message, 713 struct l_dbus_message_builder *builder, 714 void *user_data) 715 { 716 struct network_info *network = user_data; 717 char datestr[64]; 718 struct tm tm; 719 time_t seconds = l_time_to_secs(network->config.connected_time); 720 721 if (seconds == 0) 722 return false; 723 724 gmtime_r(&seconds, &tm); 725 726 if (!strftime(datestr, sizeof(datestr), "%FT%TZ", &tm)) 727 return false; 728 729 l_dbus_message_builder_append_basic(builder, 's', datestr); 730 731 return true; 732 } 733 734 static void setup_known_network_interface(struct l_dbus_interface *interface) 735 { 736 l_dbus_interface_method(interface, "Forget", 0, 737 known_network_forget, "", ""); 738 739 l_dbus_interface_property(interface, "Name", 0, "s", 740 known_network_property_get_name, NULL); 741 l_dbus_interface_property(interface, "Type", 0, "s", 742 known_network_property_get_type, NULL); 743 l_dbus_interface_property(interface, "Hidden", 0, "b", 744 known_network_property_get_hidden, 745 NULL); 746 l_dbus_interface_property(interface, "AutoConnect", 0, "b", 747 known_network_property_get_autoconnect, 748 known_network_property_set_autoconnect); 749 l_dbus_interface_property(interface, "LastConnectedTime", 0, "s", 750 known_network_property_get_last_connected, 751 NULL); 752 } 753 754 void known_networks_remove(struct network_info *network) 755 { 756 if (network->config.is_hidden) 757 num_known_hidden_networks--; 758 759 l_queue_remove(known_networks, network); 760 #ifdef HAVE_DBUS 761 l_dbus_unregister_object(dbus_get_bus(), 762 known_network_get_path(network)); 763 #endif 764 765 WATCHLIST_NOTIFY(&known_network_watches, 766 known_networks_watch_func_t, 767 KNOWN_NETWORKS_EVENT_REMOVED, network); 768 769 if (known_freqs && network->has_uuid) { 770 char uuid[37]; 771 772 l_uuid_to_string(network->uuid, uuid, sizeof(uuid)); 773 l_settings_remove_group(known_freqs, uuid); 774 storage_known_frequencies_sync(known_freqs); 775 } 776 777 network_info_free(network); 778 } 779 780 void known_networks_add(struct network_info *network) 781 { 782 l_queue_insert(known_networks, network, connected_time_compare, NULL); 783 #ifdef HAVE_DBUS 784 known_network_register_dbus(network); 785 #endif 786 787 WATCHLIST_NOTIFY(&known_network_watches, 788 known_networks_watch_func_t, 789 KNOWN_NETWORKS_EVENT_ADDED, network); 790 } 791 792 static void known_network_new(const char *ssid, enum security security, 793 struct network_config *config) 794 { 795 struct network_info *network; 796 797 network = l_new(struct network_info, 1); 798 __network_info_init(network, ssid, security, config); 799 network->ops = &known_network_ops; 800 known_networks_add(network); 801 } 802 803 static void known_networks_watch_cb(const char *filename, 804 enum l_dir_watch_event event, 805 void *user_data) 806 { 807 const char *ssid; 808 L_AUTO_FREE_VAR(char *, full_path) = NULL; 809 enum security security; 810 struct network_info *network_before; 811 struct l_settings *settings; 812 uint64_t connected_time; 813 814 /* 815 * Ignore notifications for the actual directory, we can't do 816 * anything about some of them anyway. Only react to 817 * notifications for files in the storage directory. 818 */ 819 if (!filename) 820 return; 821 822 ssid = storage_network_ssid_from_path(filename, &security); 823 if (!ssid) 824 return; 825 826 network_before = known_networks_find(ssid, security); 827 828 full_path = storage_get_network_file_path(security, ssid); 829 830 switch (event) { 831 case L_DIR_WATCH_EVENT_CREATED: 832 case L_DIR_WATCH_EVENT_REMOVED: 833 case L_DIR_WATCH_EVENT_MODIFIED: 834 /* 835 * For now treat all the operations the same. E.g. they may 836 * result in the removal of the network (file moved out, not 837 * readable or invalid) or the creation of a new network (file 838 * created, permissions granted, syntax fixed, etc.) 839 * so we always need to re-read the file. 840 */ 841 settings = storage_network_open(security, ssid); 842 843 if (settings) { 844 struct network_config config; 845 846 __network_config_parse(settings, full_path, &config); 847 848 if (network_before) 849 known_network_update(network_before, &config); 850 else 851 known_network_new(ssid, security, &config); 852 } else if (network_before) 853 known_networks_remove(network_before); 854 855 l_settings_free(settings); 856 857 break; 858 case L_DIR_WATCH_EVENT_ACCESSED: 859 break; 860 case L_DIR_WATCH_EVENT_ATTRIB: 861 if (network_before) { 862 connected_time = l_path_get_mtime(full_path); 863 known_network_set_connected_time(network_before, 864 connected_time); 865 } 866 867 break; 868 } 869 } 870 871 static void known_networks_watch_destroy(void *user_data) 872 { 873 storage_dir_watch = NULL; 874 } 875 876 static struct l_queue *known_frequencies_from_string(char *freq_set_str) 877 { 878 struct l_queue *known_frequencies; 879 struct known_frequency *known_freq; 880 uint16_t t; 881 882 if (!freq_set_str) 883 return NULL; 884 885 if (*freq_set_str == '\0') 886 return NULL; 887 888 known_frequencies = l_queue_new(); 889 890 while (*freq_set_str != '\0') { 891 errno = 0; 892 893 t = strtoul(freq_set_str, &freq_set_str, 10); 894 895 if (unlikely(errno == ERANGE || !t || 896 !band_freq_to_channel(t, NULL))) 897 goto error; 898 899 known_freq = l_new(struct known_frequency, 1); 900 known_freq->frequency = t; 901 902 l_queue_push_tail(known_frequencies, known_freq); 903 } 904 905 if (l_queue_isempty(known_frequencies)) 906 goto error; 907 908 return known_frequencies; 909 910 error: 911 l_queue_destroy(known_frequencies, l_free); 912 913 return NULL; 914 } 915 916 static void known_frequency_to_string(void *data, void *user_data) 917 { 918 struct known_frequency *known_freq = data; 919 struct l_string *str = user_data; 920 921 l_string_append_printf(str, " %u", known_freq->frequency); 922 } 923 924 static char *known_frequencies_to_string(struct l_queue *known_frequencies) 925 { 926 struct l_string *str; 927 928 str = l_string_new(100); 929 930 l_queue_foreach(known_frequencies, known_frequency_to_string, str); 931 932 return l_string_unwrap(str); 933 } 934 935 struct hotspot_search { 936 struct network_info *info; 937 const char *path; 938 }; 939 940 static bool match_hotspot_path(const struct network_info *info, void *user_data) 941 { 942 struct hotspot_search *search = user_data; 943 char *path; 944 945 if (!info->is_hotspot) 946 return true; 947 948 path = info->ops->get_file_path(info); 949 950 if (!strcmp(path, search->path)) { 951 l_free(path); 952 search->info = (struct network_info *)info; 953 return false; 954 } 955 956 l_free(path); 957 958 return true; 959 } 960 961 static struct network_info *find_network_info_from_path(const char *path) 962 { 963 enum security security; 964 struct hotspot_search search; 965 const char *ssid = storage_network_ssid_from_path(path, &security); 966 967 if (ssid) 968 return known_networks_find(ssid, security); 969 970 search.info = NULL; 971 search.path = path; 972 973 /* Try hotspot */ 974 known_networks_foreach(match_hotspot_path, &search); 975 976 return search.info; 977 } 978 979 static int known_network_frequencies_load(void) 980 { 981 char **groups; 982 struct l_queue *known_frequencies; 983 uint32_t i; 984 uint8_t uuid[16]; 985 986 known_freqs = storage_known_frequencies_load(); 987 if (!known_freqs) { 988 l_debug("No known frequency file found."); 989 return 0; 990 } 991 992 groups = l_settings_get_groups(known_freqs); 993 994 for (i = 0; groups[i]; i++) { 995 struct network_info *info; 996 char *freq_list; 997 const char *path = l_settings_get_value(known_freqs, groups[i], 998 "name"); 999 if (!path) 1000 goto invalid_entry; 1001 1002 info = find_network_info_from_path(path); 1003 if (!info) 1004 goto invalid_entry; 1005 1006 if (info->has_uuid) 1007 goto invalid_entry; 1008 1009 freq_list = l_settings_get_string(known_freqs, groups[i], 1010 "list"); 1011 if (!freq_list) 1012 goto invalid_entry; 1013 1014 known_frequencies = known_frequencies_from_string(freq_list); 1015 l_free(freq_list); 1016 1017 if (!known_frequencies) 1018 goto invalid_entry; 1019 1020 if (!l_uuid_from_string(groups[i], uuid)) { 1021 l_queue_destroy(known_frequencies, l_free); 1022 goto invalid_entry; 1023 } 1024 1025 network_info_set_uuid(info, uuid); 1026 info->known_frequencies = known_frequencies; 1027 1028 continue; 1029 1030 invalid_entry: 1031 l_settings_remove_group(known_freqs, groups[i]); 1032 } 1033 1034 l_strv_free(groups); 1035 1036 return 0; 1037 } 1038 1039 /* 1040 * Syncs a single network_info frequency to the global frequency file 1041 */ 1042 void known_network_frequency_sync(struct network_info *info) 1043 { 1044 char *freq_list_str; 1045 char *file_path; 1046 char group[37]; 1047 1048 if (!info->known_frequencies) 1049 return; 1050 1051 if (!known_freqs) 1052 known_freqs = l_settings_new(); 1053 1054 freq_list_str = known_frequencies_to_string(info->known_frequencies); 1055 1056 file_path = info->ops->get_file_path(info); 1057 1058 l_uuid_to_string(network_info_get_uuid(info), group, sizeof(group)); 1059 1060 l_settings_set_value(known_freqs, group, "name", file_path); 1061 l_settings_set_value(known_freqs, group, "list", freq_list_str); 1062 l_free(file_path); 1063 l_free(freq_list_str); 1064 1065 storage_known_frequencies_sync(known_freqs); 1066 } 1067 1068 uint32_t known_networks_watch_add(known_networks_watch_func_t func, 1069 void *user_data, 1070 known_networks_destroy_func_t destroy) 1071 { 1072 return watchlist_add(&known_network_watches, func, user_data, destroy); 1073 } 1074 1075 void known_networks_watch_remove(uint32_t id) 1076 { 1077 watchlist_remove(&known_network_watches, id); 1078 } 1079 1080 static int known_networks_init(void) 1081 { 1082 #ifdef HAVE_DBUS 1083 struct l_dbus *dbus = dbus_get_bus(); 1084 #endif 1085 DIR *dir; 1086 struct dirent *dirent; 1087 1088 L_AUTO_FREE_VAR(char *, storage_dir) = storage_get_path(NULL); 1089 1090 #ifdef HAVE_DBUS 1091 if (!l_dbus_register_interface(dbus, IWD_KNOWN_NETWORK_INTERFACE, 1092 setup_known_network_interface, 1093 NULL, false)) { 1094 l_info("Unable to register %s interface", 1095 IWD_KNOWN_NETWORK_INTERFACE); 1096 return -EPERM; 1097 } 1098 #endif 1099 1100 dir = opendir(storage_dir); 1101 if (!dir) { 1102 l_info("Unable to open %s: %s", storage_dir, strerror(errno)); 1103 #ifdef HAVE_DBUS 1104 l_dbus_unregister_interface(dbus, IWD_KNOWN_NETWORK_INTERFACE); 1105 #endif 1106 return -ENOENT; 1107 } 1108 1109 known_networks = l_queue_new(); 1110 1111 while ((dirent = readdir(dir))) { 1112 const char *ssid; 1113 enum security security; 1114 struct l_settings *settings; 1115 L_AUTO_FREE_VAR(char *, full_path) = NULL; 1116 1117 if (dirent->d_type == DT_UNKNOWN) { 1118 if (!storage_is_file(dirent->d_name)) 1119 continue; 1120 } else if (dirent->d_type != DT_REG && 1121 dirent->d_type != DT_LNK) { 1122 continue; 1123 } 1124 1125 ssid = storage_network_ssid_from_path(dirent->d_name, 1126 &security); 1127 if (!ssid) 1128 continue; 1129 1130 settings = storage_network_open(security, ssid); 1131 1132 full_path = storage_get_network_file_path(security, ssid); 1133 1134 if (settings) { 1135 struct network_config config; 1136 1137 __network_config_parse(settings, full_path, &config); 1138 known_network_new(ssid, security, &config); 1139 } 1140 1141 l_settings_free(settings); 1142 } 1143 1144 closedir(dir); 1145 1146 storage_dir_watch = l_dir_watch_new(storage_dir, 1147 known_networks_watch_cb, NULL, 1148 known_networks_watch_destroy); 1149 watchlist_init(&known_network_watches, NULL); 1150 1151 return 0; 1152 } 1153 1154 static void known_networks_exit(void) 1155 { 1156 #ifdef HAVE_DBUS 1157 struct l_dbus *dbus = dbus_get_bus(); 1158 #endif 1159 1160 l_dir_watch_destroy(storage_dir_watch); 1161 1162 l_queue_destroy(known_networks, network_info_free); 1163 known_networks = NULL; 1164 1165 #ifdef HAVE_DBUS 1166 l_dbus_unregister_interface(dbus, IWD_KNOWN_NETWORK_INTERFACE); 1167 #endif 1168 1169 watchlist_destroy(&known_network_watches); 1170 } 1171 1172 IWD_MODULE(known_networks, known_networks_init, known_networks_exit) 1173 1174 static void known_frequencies_exit(void) 1175 { 1176 l_settings_free(known_freqs); 1177 } 1178 1179 /* 1180 * Since the known frequency file should only be read in after all known 1181 * networks are loaded (including hotspots) we need to create another module 1182 * here which depends on hotspot. 1183 */ 1184 IWD_MODULE(known_frequencies, known_network_frequencies_load, known_frequencies_exit) 1185 IWD_MODULE_DEPENDS(known_frequencies, hotspot)