main.c (15387B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <errno.h> 30 #include <getopt.h> 31 #include <signal.h> 32 #include <fcntl.h> 33 #include <unistd.h> 34 #include <sys/stat.h> 35 #include <sys/mman.h> 36 #include <linux/rtnetlink.h> 37 #include <ell/ell.h> 38 39 #include <ell/useful.h> 40 41 #include "linux/nl80211.h" 42 43 #include "src/iwd.h" 44 #include "src/module.h" 45 #include "src/wiphy.h" 46 #include "src/dbus.h" 47 #include "src/eap.h" 48 #include "src/eapol.h" 49 #include "src/rfkill.h" 50 #include "src/storage.h" 51 #include "src/anqp.h" 52 #include "src/netconfig.h" 53 #include "src/crypto.h" 54 #include "src/control.h" 55 56 #include "src/backtrace.h" 57 58 static struct l_genl *genl; 59 static struct l_netlink *rtnl; 60 static struct l_settings *iwd_config; 61 static struct l_timeout *timeout; 62 static const char *interfaces; 63 static const char *nointerfaces; 64 static const char *phys; 65 static const char *nophys; 66 static const char *debugopt; 67 static const char *logger; 68 static bool developeropt; 69 static bool terminating; 70 static bool nl80211_complete; 71 72 static void main_loop_quit(struct l_timeout *timeout, void *user_data) 73 { 74 l_main_quit(); 75 } 76 77 static void iwd_shutdown(void) 78 { 79 if (terminating) 80 return; 81 82 terminating = true; 83 84 if (!nl80211_complete) { 85 l_main_quit(); 86 return; 87 } 88 89 #ifdef HAVE_DBUS 90 dbus_shutdown(); 91 #endif 92 netdev_shutdown(); 93 94 timeout = l_timeout_create(1, main_loop_quit, NULL, NULL); 95 } 96 97 static void signal_handler(uint32_t signo, void *user_data) 98 { 99 switch (signo) { 100 case SIGINT: 101 case SIGTERM: 102 l_info("Terminate"); 103 iwd_shutdown(); 104 break; 105 } 106 } 107 108 const struct l_settings *iwd_get_config(void) 109 { 110 return iwd_config; 111 } 112 113 struct l_genl *iwd_get_genl(void) 114 { 115 return genl; 116 } 117 118 struct l_netlink *iwd_get_rtnl(void) 119 { 120 return rtnl; 121 } 122 123 const char *iwd_get_iface_whitelist(void) 124 { 125 return interfaces; 126 } 127 128 const char *iwd_get_iface_blacklist(void) 129 { 130 return nointerfaces; 131 } 132 133 const char *iwd_get_phy_whitelist(void) 134 { 135 return phys; 136 } 137 138 const char *iwd_get_phy_blacklist(void) 139 { 140 return nophys; 141 } 142 143 bool iwd_is_developer_mode(void) 144 { 145 return developeropt; 146 } 147 148 static void usage(void) 149 { 150 printf("iwd - Wireless daemon\n" 151 "Usage:\n"); 152 printf("\tiwd [options]\n"); 153 printf("Options:\n" 154 "\t-E, --developer Enable developer mode\n" 155 "\t-i, --interfaces Interfaces to manage\n" 156 "\t-I, --nointerfaces Interfaces to ignore\n" 157 "\t-p, --phys Phys to manage\n" 158 "\t-P, --nophys Phys to ignore\n" 159 "\t-d, --debug Enable debug output\n" 160 "\t-l, --logger Override default stderr logging\n" 161 "\t-v, --version Show version\n" 162 "\t-h, --help Show help options\n"); 163 } 164 165 static const struct option main_options[] = { 166 { "developer", no_argument, NULL, 'E' }, 167 { "version", no_argument, NULL, 'v' }, 168 { "interfaces", required_argument, NULL, 'i' }, 169 { "nointerfaces", required_argument, NULL, 'I' }, 170 { "phys", required_argument, NULL, 'p' }, 171 { "nophys", required_argument, NULL, 'P' }, 172 { "logger", required_argument, NULL, 'l' }, 173 { "debug", optional_argument, NULL, 'd' }, 174 { "help", no_argument, NULL, 'h' }, 175 { } 176 }; 177 178 static void do_debug(const char *str, void *user_data) 179 { 180 const char *prefix = user_data; 181 182 l_info("%s%s", prefix, str); 183 } 184 185 static void nl80211_appeared(const struct l_genl_family_info *info, 186 void *user_data) 187 { 188 l_debug("Found nl80211 interface"); 189 190 nl80211_complete = true; 191 192 if (iwd_modules_init() < 0) { 193 l_main_quit(); 194 return; 195 } 196 } 197 198 #ifdef HAVE_DBUS 199 static void request_name_callback(struct l_dbus *dbus, bool success, 200 bool queued, void *user_data) 201 { 202 if (!success) { 203 l_error("Name request failed"); 204 l_main_quit(); 205 } 206 207 /* TODO: Always request nl80211 for now, ignoring auto-loading */ 208 l_genl_request_family(genl, NL80211_GENL_NAME, nl80211_appeared, 209 NULL, NULL); 210 } 211 212 static struct l_dbus_message *iwd_dbus_get_info(struct l_dbus *dbus, 213 struct l_dbus_message *message, 214 void *user_data) 215 { 216 struct l_dbus_message *reply = 217 l_dbus_message_new_method_return(message); 218 _auto_(l_free) char *storage_dir = storage_get_path(NULL); 219 220 l_dbus_message_set_arguments(reply, "a{sv}", 3, 221 "NetworkConfigurationEnabled", 222 "b", netconfig_enabled(), 223 "StateDirectory", "s", storage_dir, 224 "Version", "s", VERSION); 225 226 return reply; 227 } 228 229 static void iwd_setup_deamon_interface(struct l_dbus_interface *interface) 230 { 231 l_dbus_interface_method(interface, "GetInfo", 0, iwd_dbus_get_info, 232 "a{sv}", "", "info"); 233 } 234 235 static void dbus_ready(void *user_data) 236 { 237 struct l_dbus *dbus = user_data; 238 239 l_dbus_register_interface(dbus, IWD_DAEMON_INTERFACE, 240 iwd_setup_deamon_interface, 241 NULL, false); 242 243 if (!l_dbus_object_manager_enable(dbus, "/")) 244 l_warn("Unable to register the ObjectManager"); 245 246 if (!l_dbus_object_add_interface(dbus, IWD_BASE_PATH, 247 IWD_DAEMON_INTERFACE, 248 NULL) || 249 !l_dbus_object_add_interface(dbus, IWD_BASE_PATH, 250 L_DBUS_INTERFACE_PROPERTIES, 251 NULL)) 252 l_info("Unable to add %s and/or %s at %s", 253 IWD_DAEMON_INTERFACE, L_DBUS_INTERFACE_PROPERTIES, 254 IWD_BASE_PATH); 255 256 l_dbus_name_acquire(dbus, "net.connman.iwd", false, false, false, 257 request_name_callback, NULL); 258 } 259 260 static void dbus_disconnected(void *user_data) 261 { 262 l_info("D-Bus disconnected, quitting..."); 263 iwd_shutdown(); 264 } 265 #endif 266 267 static void print_koption(const void *key, void *value, void *user_data) 268 { 269 l_info("\t%s", (const char *) key); 270 } 271 272 #define ADD_MISSING(opt) \ 273 l_hashmap_replace(options, opt, &r, NULL) 274 275 #define ADD_OPTIONAL(opt) \ 276 l_hashmap_replace(optional, opt, &r, NULL) 277 278 static int check_crypto(void) 279 { 280 int r = 0; 281 struct l_hashmap *options = l_hashmap_string_new(); 282 struct l_hashmap *optional = l_hashmap_string_new(); 283 284 if (!l_checksum_is_supported(L_CHECKSUM_SHA1, true)) { 285 r = -ENOTSUP; 286 l_error("No HMAC(SHA1) support found"); 287 ADD_MISSING("CONFIG_CRYPTO_USER_API_HASH"); 288 ADD_MISSING("CONFIG_CRYPTO_SHA1"); 289 ADD_MISSING("CONFIG_CRYPTO_HMAC"); 290 ADD_OPTIONAL("CONFIG_CRYPTO_SHA1_SSSE3"); 291 } 292 293 if (!l_checksum_is_supported(L_CHECKSUM_MD5, true)) { 294 r = -ENOTSUP; 295 l_error("No HMAC(MD5) support found"); 296 ADD_MISSING("CONFIG_CRYPTO_USER_API_HASH"); 297 ADD_MISSING("CONFIG_CRYPTO_MD5"); 298 ADD_MISSING("CONFIG_CRYPTO_HMAC"); 299 } 300 301 if (!l_checksum_cmac_aes_supported()) { 302 r = -ENOTSUP; 303 l_error("No CMAC(AES) support found"); 304 ADD_MISSING("CONFIG_CRYPTO_USER_API_HASH"); 305 ADD_MISSING("CONFIG_CRYPTO_AES"); 306 ADD_MISSING("CONFIG_CRYPTO_CMAC"); 307 ADD_OPTIONAL("CONFIG_CRYPTO_AES_X86_64"); 308 ADD_OPTIONAL("CONFIG_CRYPTO_AES_NI_INTEL"); 309 } 310 311 if (!l_checksum_is_supported(L_CHECKSUM_SHA256, true)) { 312 r = -ENOTSUP; 313 l_error("No HMAC(SHA256) support not found"); 314 ADD_MISSING("CONFIG_CRYPTO_USER_API_HASH"); 315 ADD_MISSING("CONFIG_CRYPTO_HMAC"); 316 ADD_MISSING("CONFIG_CRYPTO_SHA256"); 317 ADD_OPTIONAL("CONFIG_CRYPTO_SHA256_SSSE3"); 318 } 319 320 if (!l_checksum_is_supported(L_CHECKSUM_SHA512, true)) { 321 l_warn("No HMAC(SHA512) support found, " 322 "certain TLS connections might fail"); 323 ADD_MISSING("CONFIG_CRYPTO_SHA512"); 324 ADD_OPTIONAL("CONFIG_CRYPTO_SHA512_SSSE3"); 325 } 326 327 if (!l_cipher_is_supported(L_CIPHER_DES) || 328 !l_cipher_is_supported(L_CIPHER_DES3_EDE_CBC)) { 329 r = -ENOTSUP; 330 l_error("DES support not found"); 331 ADD_MISSING("CONFIG_CRYPTO_USER_API_SKCIPHER"); 332 ADD_MISSING("CONFIG_CRYPTO_DES"); 333 ADD_MISSING("CONFIG_CRYPTO_ECB"); 334 ADD_OPTIONAL("CONFIG_CRYPTO_DES3_EDE_X86_64"); 335 } 336 337 if (!l_cipher_is_supported(L_CIPHER_AES)) { 338 r = -ENOTSUP; 339 l_error("AES support not found"); 340 ADD_MISSING("CONFIG_CRYPTO_USER_API_SKCIPHER"); 341 ADD_MISSING("CONFIG_CRYPTO_AES"); 342 ADD_MISSING("CONFIG_CRYPTO_ECB"); 343 ADD_OPTIONAL("CONFIG_CRYPTO_AES_X86_64"); 344 ADD_OPTIONAL("CONFIG_CRYPTO_AES_NI_INTEL"); 345 } 346 347 if (!l_cipher_is_supported(L_CIPHER_DES3_EDE_CBC)) { 348 l_warn("No CBC(DES3_EDE) support found, " 349 "certain TLS connections might fail"); 350 ADD_MISSING("CONFIG_CRYPTO_DES"); 351 ADD_MISSING("CONFIG_CRYPTO_CBC"); 352 ADD_OPTIONAL("CONFIG_CRYPTO_DES3_EDE_X86_64"); 353 } 354 355 if (!l_cipher_is_supported(L_CIPHER_AES_CBC)) { 356 l_warn("No CBC(AES) support found, " 357 "WPS will not be available"); 358 ADD_MISSING("CONFIG_CRYPTO_CBC"); 359 } 360 361 if (!l_key_is_supported(L_KEY_FEATURE_DH)) { 362 l_warn("No Diffie-Hellman support found, " 363 "WPS will not be available"); 364 ADD_MISSING("CONFIG_KEY_DH_OPERATIONS"); 365 } 366 367 if (!l_key_is_supported(L_KEY_FEATURE_RESTRICT)) { 368 l_warn("No keyring restrictions support found."); 369 ADD_MISSING("CONFIG_KEYS"); 370 } 371 372 if (!l_key_is_supported(L_KEY_FEATURE_CRYPTO)) { 373 l_warn("No asymmetric key support found."); 374 l_warn("TLS based WPA-Enterprise authentication methods will" 375 " not function."); 376 l_warn("Kernel 4.20+ is required for this feature."); 377 ADD_MISSING("CONFIG_ASYMMETRIC_KEY_TYPE"); 378 ADD_MISSING("CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE"); 379 ADD_MISSING("CONFIG_X509_CERTIFICATE_PARSER"); 380 ADD_MISSING("CONFIG_PKCS7_MESSAGE_PARSER"); 381 ADD_MISSING("CONFIG_PKCS8_PRIVATE_KEY_PARSER"); 382 } 383 384 if (l_hashmap_isempty(options)) 385 goto done; 386 387 l_info("The following options are missing in the kernel:"); 388 389 if (l_hashmap_remove(options, "CONFIG_CRYPTO_USER_API_HASH")) 390 l_info("\tCONFIG_CRYPTO_USER_API_HASH"); 391 392 if (l_hashmap_remove(options, "CONFIG_CRYPTO_USER_API_SKCIPHER")) 393 l_info("\tCONFIG_CRYPTO_USER_API_SKCIPHER"); 394 395 l_hashmap_foreach(options, print_koption, NULL); 396 397 if (!l_hashmap_isempty(optional)) { 398 l_info("The following optimized implementations might be " 399 "available:"); 400 l_hashmap_foreach(optional, print_koption, NULL); 401 } 402 403 done: 404 l_hashmap_destroy(options, NULL); 405 l_hashmap_destroy(optional, NULL); 406 407 return r; 408 } 409 410 /* 411 * Initialize a systemd encryption key for encrypting/decrypting credentials. 412 */ 413 static bool setup_system_key(void) 414 { 415 int fd; 416 struct stat st; 417 const char *cred_dir; 418 void *key = NULL; 419 _auto_(l_free) char *path = NULL; 420 _auto_(l_free) char *key_id = NULL; 421 bool r = false; 422 423 key_id = l_settings_get_string(iwd_config, "General", 424 "SystemdEncrypt"); 425 if (!key_id) 426 return true; 427 428 cred_dir = getenv("CREDENTIALS_DIRECTORY"); 429 if (!cred_dir) { 430 l_warn("SystemdEncrypt enabled but CREDENTIALS_DIRECTORY not " 431 "set, check iwd.service file"); 432 return false; 433 } 434 435 path = l_strdup_printf("%s/%s", cred_dir, key_id); 436 437 fd = open(path, O_RDONLY, 0); 438 if (fd < 0) { 439 l_warn("SystemdEncrypt: Cannot open secret: %s (%d)", 440 strerror(errno), errno); 441 return false; 442 } 443 444 if (fstat(fd, &st) < 0 || st.st_size == 0) 445 goto close_fd; 446 447 key = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); 448 if (key == MAP_FAILED) { 449 l_warn("SystemdEncrypt: can't mmap secret: %s (%d)", 450 strerror(errno), errno); 451 goto close_fd; 452 } 453 454 if (mlock(key, st.st_size) < 0) { 455 l_warn("SystemdEncrypt: Failed to mlock secrets file"); 456 goto unmap; 457 } 458 459 r = storage_init(key, st.st_size); 460 munlock(key, st.st_size); 461 462 unmap: 463 munmap(key, st.st_size); 464 close_fd: 465 close(fd); 466 467 return r; 468 } 469 470 int main(int argc, char *argv[]) 471 { 472 int exit_status; 473 struct l_dbus *dbus; 474 const char *config_dir; 475 const char *control_path; 476 char **config_dirs; 477 int i, err; 478 479 for (;;) { 480 int opt; 481 482 opt = getopt_long(argc, argv, "Ei:I:p:P:d::vhl:", 483 main_options, NULL); 484 if (opt < 0) 485 break; 486 487 switch (opt) { 488 case 'E': 489 developeropt = true; 490 break; 491 case 'i': 492 interfaces = optarg; 493 break; 494 case 'I': 495 nointerfaces = optarg; 496 break; 497 case 'p': 498 phys = optarg; 499 break; 500 case 'P': 501 nophys = optarg; 502 break; 503 case 'd': 504 if (optarg) 505 debugopt = optarg; 506 else if (argv[optind] && argv[optind][0] != '-') 507 debugopt = argv[optind++]; 508 else 509 debugopt = "*"; 510 break; 511 case 'l': 512 logger = optarg; 513 break; 514 case 'v': 515 printf("%s\n", VERSION); 516 return EXIT_SUCCESS; 517 case 'h': 518 usage(); 519 return EXIT_SUCCESS; 520 default: 521 return EXIT_FAILURE; 522 } 523 } 524 525 if (argc - optind > 0) { 526 fprintf(stderr, "Invalid command line parameters\n"); 527 return EXIT_FAILURE; 528 } 529 530 if (logger && !strcmp(logger, "syslog")) 531 l_log_set_syslog(); 532 else if (logger && !strcmp(logger, "journal")) 533 l_log_set_journal(); 534 else 535 l_log_set_stderr(); 536 537 l_log_set_ident("iwd"); 538 539 if (check_crypto() < 0) 540 return EXIT_FAILURE; 541 542 if (!l_main_init()) 543 return EXIT_FAILURE; 544 545 if (debugopt) 546 l_debug_enable(debugopt); 547 548 #ifdef HAVE_BACKTRACE 549 __iwd_backtrace_init(); 550 #endif 551 552 l_info("Wireless daemon version %s", VERSION); 553 554 config_dir = getenv("CONFIGURATION_DIRECTORY"); 555 if (!config_dir) 556 config_dir = DAEMON_CONFIGDIR; 557 558 l_debug("Using configuration directory %s", config_dir); 559 560 iwd_config = l_settings_new(); 561 562 config_dirs = l_strsplit(config_dir, ':'); 563 564 for (i = 0; config_dirs[i]; i++) { 565 L_AUTO_FREE_VAR(char *, path) = 566 l_strdup_printf("%s/%s", config_dirs[i], "main.conf"); 567 568 if (!l_settings_load_from_file(iwd_config, path)) 569 continue; 570 571 l_info("Loaded configuration from %s", path); 572 break; 573 } 574 575 l_strv_free(config_dirs); 576 577 __eapol_set_config(iwd_config); 578 __eap_set_config(iwd_config); 579 580 exit_status = EXIT_FAILURE; 581 582 if (!storage_create_dirs()) 583 goto failed_dirs; 584 585 genl = l_genl_new(); 586 if (!genl) { 587 l_error("Failed to open generic netlink socket"); 588 goto failed_genl; 589 } 590 591 if (getenv("IWD_GENL_DEBUG")) 592 l_genl_set_debug(genl, do_debug, "[GENL] ", NULL); 593 594 rtnl = l_netlink_new(NETLINK_ROUTE); 595 if (!rtnl) { 596 l_error("Failed to open route netlink socket"); 597 goto failed_rtnl; 598 } 599 600 if (getenv("IWD_RTNL_DEBUG")) 601 l_netlink_set_debug(rtnl, do_debug, "[RTNL] ", NULL); 602 603 #ifdef HAVE_DBUS 604 dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS); 605 if (!dbus) { 606 l_error("Failed to initialize D-Bus"); 607 goto failed_dbus; 608 } 609 610 l_dbus_set_ready_handler(dbus, dbus_ready, dbus, NULL); 611 l_dbus_set_disconnect_handler(dbus, dbus_disconnected, NULL, NULL); 612 dbus_init(dbus); 613 #else 614 l_genl_request_family(genl, NL80211_GENL_NAME, nl80211_appeared, 615 NULL, NULL); 616 #endif 617 618 if (!setup_system_key()) 619 goto failed_storage; 620 621 control_path = getenv("IWD_CONTROL_SOCKET"); 622 if (!control_path) 623 control_path = CONTROL_SOCKET_PATH; 624 err = control_init(control_path); 625 if (err < 0) { 626 l_error("Unable to listen on %s: %s", control_path, strerror(-err)); 627 goto failed_storage; 628 } 629 630 exit_status = l_main_run_with_signal(signal_handler, NULL); 631 632 control_exit(); 633 iwd_modules_exit(); 634 storage_exit(); 635 636 failed_storage: 637 #ifdef HAVE_DBUS 638 dbus_exit(); 639 l_dbus_destroy(dbus); 640 #else 641 (void)NULL; 642 #endif 643 644 failed_dbus: 645 l_netlink_destroy(rtnl); 646 647 failed_rtnl: 648 l_genl_unref(genl); 649 650 failed_genl: 651 storage_cleanup_dirs(); 652 653 failed_dirs: 654 l_settings_free(iwd_config); 655 l_timeout_remove(timeout); 656 l_main_exit(); 657 658 return exit_status; 659 }