manager.c (23389B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 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 <stdlib.h> 28 #include <stdio.h> 29 #include <errno.h> 30 #include <linux/if_ether.h> 31 #include <fnmatch.h> 32 33 #include <ell/ell.h> 34 35 #include "linux/nl80211.h" 36 37 #include "src/nl80211util.h" 38 #include "src/iwd.h" 39 #include "src/module.h" 40 #include "src/netdev.h" 41 #include "src/wiphy.h" 42 #include "src/util.h" 43 #include "src/common.h" 44 #include "src/nl80211cmd.h" 45 #include "src/p2p.h" 46 47 static struct l_genl_family *nl80211 = NULL; 48 static char **whitelist_filter; 49 static char **blacklist_filter; 50 static bool randomize; 51 static bool use_default; 52 static unsigned int config_watch; 53 54 struct wiphy_setup_state { 55 uint32_t id; 56 struct wiphy *wiphy; 57 unsigned int pending_cmd_count; 58 bool aborted; 59 bool retry; 60 61 /* 62 * Data we may need if the driver does not seem to support interface 63 * manipulation and we fall back to using the driver-created default 64 * interface. 65 */ 66 bool use_default; 67 struct l_queue *default_interfaces; 68 }; 69 70 static struct l_queue *pending_wiphys; 71 72 static void wiphy_setup_state_free(void *data) 73 { 74 struct wiphy_setup_state *state = data; 75 76 l_queue_destroy(state->default_interfaces, 77 (l_queue_destroy_func_t) l_genl_msg_unref); 78 79 L_WARN_ON(state->pending_cmd_count); 80 l_free(state); 81 } 82 83 static void wiphy_setup_state_destroy(struct wiphy_setup_state *state) 84 { 85 l_queue_remove(pending_wiphys, state); 86 wiphy_setup_state_free(state); 87 } 88 89 static bool manager_use_default(struct wiphy_setup_state *state) 90 { 91 uint8_t addr_buf[6]; 92 uint8_t *addr = NULL; 93 const struct l_queue_entry *entry; 94 95 l_debug(""); 96 97 if (!state->default_interfaces) { 98 l_error("No default interface for wiphy %u", 99 (unsigned int) state->id); 100 state->retry = true; 101 return false; 102 } 103 104 entry = l_queue_get_entries(state->default_interfaces); 105 while (entry) { 106 struct l_genl_msg *msg = entry->data; 107 108 if (randomize) { 109 wiphy_generate_random_address(state->wiphy, addr_buf); 110 addr = addr_buf; 111 } 112 113 netdev_create_from_genl(msg, addr); 114 entry = entry->next; 115 } 116 117 return true; 118 } 119 120 static void manager_new_station_interface_cb(struct l_genl_msg *msg, 121 void *user_data) 122 { 123 struct wiphy_setup_state *state = user_data; 124 uint8_t addr_buf[6]; 125 uint8_t *addr = NULL; 126 int error; 127 128 l_debug(""); 129 130 if (state->aborted) 131 return; 132 133 error = l_genl_msg_get_error(msg); 134 if (error < 0) { 135 l_error("NEW_INTERFACE failed: %s", 136 strerror(-l_genl_msg_get_error(msg))); 137 138 /* 139 * If we receive an EBUSY most likely the wiphy is still 140 * initializing, the default interface has not been created 141 * yet and the wiphy needs some time. Retry when we 142 * receive a NEW_INTERFACE event. 143 */ 144 if (error == -EBUSY) { 145 state->retry = true; 146 return; 147 } 148 149 /* 150 * Nothing we can do to use this wiphy since by now we 151 * will have successfully deleted any default interface 152 * there may have been. 153 */ 154 return; 155 } 156 157 if (randomize && !wiphy_has_feature(state->wiphy, 158 NL80211_FEATURE_MAC_ON_CREATE)) { 159 wiphy_generate_random_address(state->wiphy, addr_buf); 160 addr = addr_buf; 161 } 162 163 netdev_create_from_genl(msg, addr); 164 } 165 166 static void manager_new_p2p_interface_cb(struct l_genl_msg *msg, 167 void *user_data) 168 { 169 struct wiphy_setup_state *state = user_data; 170 171 l_debug(""); 172 173 if (state->aborted) 174 return; 175 176 if (l_genl_msg_get_error(msg) < 0) { 177 l_error("NEW_INTERFACE failed for p2p-device: %s", 178 strerror(-l_genl_msg_get_error(msg))); 179 return; 180 } 181 182 p2p_device_update_from_genl(msg, true); 183 } 184 185 static void manager_new_interface_done(void *user_data) 186 { 187 struct wiphy_setup_state *state = user_data; 188 189 state->pending_cmd_count--; 190 191 if (!state->pending_cmd_count && !state->retry) 192 wiphy_setup_state_destroy(state); 193 } 194 195 static void manager_create_interfaces(struct wiphy_setup_state *state) 196 { 197 struct l_genl_msg *msg; 198 char ifname[10]; 199 uint32_t iftype; 200 unsigned int cmd_id; 201 202 if (state->aborted) 203 return; 204 205 if (state->use_default) { 206 manager_use_default(state); 207 208 /* 209 * Some drivers don't let us touch the default interface 210 * but still allow us to create/destroy P2P interfaces, so 211 * give it a chance. 212 */ 213 goto try_create_p2p; 214 } 215 216 /* 217 * Current policy: we maintain one netdev per wiphy for station, 218 * AP and Ad-Hoc modes, one optional p2p-device and zero or more 219 * p2p-GOs or p2p-clients. The P2P-client/GO interfaces will be 220 * created on request. 221 */ 222 223 /* To be improved */ 224 snprintf(ifname, sizeof(ifname), "wlan%i", (int) state->id); 225 l_debug("creating %s", ifname); 226 iftype = NL80211_IFTYPE_STATION; 227 228 msg = l_genl_msg_new(NL80211_CMD_NEW_INTERFACE); 229 l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY, 4, &state->id); 230 l_genl_msg_append_attr(msg, NL80211_ATTR_IFTYPE, 4, &iftype); 231 l_genl_msg_append_attr(msg, NL80211_ATTR_IFNAME, 232 strlen(ifname) + 1, ifname); 233 l_genl_msg_append_attr(msg, NL80211_ATTR_4ADDR, 1, "\0"); 234 l_genl_msg_append_attr(msg, NL80211_ATTR_SOCKET_OWNER, 0, ""); 235 236 if (randomize && wiphy_has_feature(state->wiphy, 237 NL80211_FEATURE_MAC_ON_CREATE)) { 238 uint8_t random_addr[6]; 239 240 wiphy_generate_random_address(state->wiphy, random_addr); 241 l_debug("Creating interface on phy: %s with random addr: "MAC, 242 wiphy_get_name(state->wiphy), 243 MAC_STR(random_addr)); 244 l_genl_msg_append_attr(msg, NL80211_ATTR_MAC, 6, random_addr); 245 } 246 247 cmd_id = l_genl_family_send(nl80211, msg, 248 manager_new_station_interface_cb, state, 249 manager_new_interface_done); 250 251 if (!cmd_id) { 252 l_error("Error sending NEW_INTERFACE for %s", ifname); 253 return; 254 } 255 256 state->pending_cmd_count++; 257 258 try_create_p2p: 259 /* 260 * Require the MAC on create feature so we can send our desired 261 * interface address during GO Negotiation before actually creating 262 * the local Client/GO interface. Could be worked around if needed. 263 */ 264 if (!wiphy_supports_iftype(state->wiphy, NL80211_IFTYPE_P2P_DEVICE) || 265 !wiphy_supports_iftype(state->wiphy, 266 NL80211_IFTYPE_P2P_CLIENT) || 267 !wiphy_has_feature(state->wiphy, 268 NL80211_FEATURE_MAC_ON_CREATE)) 269 return; 270 271 /* 272 * Use wlan%i-p2p for now. We might want to use 273 * <default_interface's_name>-p2p here (in case state->use_default 274 * is true) but the risk is that we'd go over the interface name 275 * length limit. 276 */ 277 snprintf(ifname, sizeof(ifname), "wlan%i-p2p", (int) state->id); 278 l_debug("creating %s", ifname); 279 iftype = NL80211_IFTYPE_P2P_DEVICE; 280 281 msg = l_genl_msg_new(NL80211_CMD_NEW_INTERFACE); 282 l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY, 4, &state->id); 283 l_genl_msg_append_attr(msg, NL80211_ATTR_IFTYPE, 4, &iftype); 284 l_genl_msg_append_attr(msg, NL80211_ATTR_IFNAME, 285 strlen(ifname) + 1, ifname); 286 l_genl_msg_append_attr(msg, NL80211_ATTR_4ADDR, 1, "\0"); 287 l_genl_msg_append_attr(msg, NL80211_ATTR_SOCKET_OWNER, 0, ""); 288 cmd_id = l_genl_family_send(nl80211, msg, 289 manager_new_p2p_interface_cb, state, 290 manager_new_interface_done); 291 292 if (!cmd_id) { 293 l_error("Error sending NEW_INTERFACE for %s", ifname); 294 return; 295 } 296 297 state->pending_cmd_count++; 298 } 299 300 static bool manager_wiphy_check_setup_done(struct wiphy_setup_state *state) 301 { 302 if (state->pending_cmd_count || state->retry) 303 return false; 304 305 manager_create_interfaces(state); 306 307 return !state->pending_cmd_count && !state->retry; 308 } 309 310 static void manager_setup_cmd_done(void *user_data) 311 { 312 struct wiphy_setup_state *state = user_data; 313 314 state->pending_cmd_count--; 315 316 if (manager_wiphy_check_setup_done(state)) 317 wiphy_setup_state_destroy(state); 318 } 319 320 static void manager_del_interface_cb(struct l_genl_msg *msg, void *user_data) 321 { 322 struct wiphy_setup_state *state = user_data; 323 int err; 324 325 l_debug(""); 326 327 if (state->aborted) 328 return; 329 330 err = l_genl_msg_get_error(msg); 331 332 if (err == -ENODEV) 333 return; 334 else if (err < 0) { 335 l_error("DEL_INTERFACE failed: %s", 336 strerror(-l_genl_msg_get_error(msg))); 337 state->use_default = true; 338 } 339 } 340 341 static void manager_get_interface_cb(struct l_genl_msg *msg, void *user_data) 342 { 343 struct wiphy_setup_state *state = user_data; 344 uint32_t wiphy; 345 uint32_t ifindex; 346 uint32_t iftype; 347 uint64_t wdev; 348 const char *ifname; 349 struct l_genl_msg *del_msg; 350 unsigned int cmd_id; 351 char *pattern; 352 unsigned int i; 353 bool whitelisted = false, blacklisted = false; 354 355 l_debug(""); 356 357 if (state->aborted) 358 return; 359 360 if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, &wdev, 361 NL80211_ATTR_WIPHY, &wiphy, 362 NL80211_ATTR_IFTYPE, &iftype, 363 NL80211_ATTR_UNSPEC) < 0) 364 return; 365 366 if (wiphy != state->id) { 367 l_debug("Wiphy attribute mismatch, wanted: %u, got %u", 368 state->id, wiphy); 369 return; 370 } 371 372 if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex, 373 NL80211_ATTR_IFNAME, &ifname, 374 NL80211_ATTR_UNSPEC) < 0) 375 goto delete_interface; 376 377 if (whitelist_filter) { 378 for (i = 0; (pattern = whitelist_filter[i]); i++) { 379 if (fnmatch(pattern, ifname, 0) != 0) 380 continue; 381 382 whitelisted = true; 383 break; 384 } 385 } 386 387 if (blacklist_filter) { 388 for (i = 0; (pattern = blacklist_filter[i]); i++) { 389 if (fnmatch(pattern, ifname, 0) != 0) 390 continue; 391 392 blacklisted = true; 393 break; 394 } 395 } 396 397 /* 398 * If this interface is usable as our default netdev in case the 399 * driver does not support interface manipulation, save the message 400 * just in case. 401 */ 402 if ((iftype == NL80211_IFTYPE_ADHOC || 403 iftype == NL80211_IFTYPE_STATION || 404 iftype == NL80211_IFTYPE_AP) && 405 (!whitelist_filter || whitelisted) && 406 !blacklisted) { 407 if (!state->default_interfaces) 408 state->default_interfaces = l_queue_new(); 409 410 l_queue_push_head(state->default_interfaces, 411 l_genl_msg_ref(msg)); 412 } 413 414 delete_interface: 415 if (state->use_default) 416 return; 417 418 del_msg = l_genl_msg_new(NL80211_CMD_DEL_INTERFACE); 419 l_genl_msg_append_attr(del_msg, NL80211_ATTR_WDEV, 8, &wdev); 420 l_genl_msg_append_attr(del_msg, NL80211_ATTR_WIPHY, 4, &state->id); 421 cmd_id = l_genl_family_send(nl80211, del_msg, 422 manager_del_interface_cb, state, 423 manager_setup_cmd_done); 424 425 if (!cmd_id) { 426 l_error("Sending DEL_INTERFACE for wdev: %" PRIu64" failed", 427 wdev); 428 state->use_default = true; 429 return; 430 } 431 432 l_debug(""); 433 state->pending_cmd_count++; 434 } 435 436 static bool manager_wiphy_state_match(const void *a, const void *b) 437 { 438 const struct wiphy_setup_state *state = a; 439 uint32_t id = L_PTR_TO_UINT(b); 440 441 return (state->id == id); 442 } 443 444 static struct wiphy_setup_state *manager_find_pending(uint32_t id) 445 { 446 return l_queue_find(pending_wiphys, manager_wiphy_state_match, 447 L_UINT_TO_PTR(id)); 448 } 449 450 static uint32_t manager_parse_wiphy_id(struct l_genl_msg *msg) 451 { 452 uint32_t wiphy; 453 454 if (nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &wiphy, 455 NL80211_ATTR_UNSPEC) < 0) 456 return -1; 457 458 return wiphy; 459 } 460 461 static void manager_del_wiphy_event(struct l_genl_msg *msg) 462 { 463 struct wiphy_setup_state *state; 464 struct wiphy *wiphy; 465 uint32_t id; 466 467 id = manager_parse_wiphy_id(msg); 468 469 state = manager_find_pending(id); 470 if (state) { 471 if (state->pending_cmd_count) 472 state->aborted = true; 473 else 474 wiphy_setup_state_destroy(state); 475 } 476 477 wiphy = wiphy_find(id); 478 if (wiphy) 479 wiphy_destroy(wiphy); 480 } 481 482 static void manager_interface_dump_callback(struct l_genl_msg *msg, 483 void *user_data) 484 { 485 struct wiphy_setup_state *state; 486 487 l_debug(""); 488 489 state = manager_find_pending(manager_parse_wiphy_id(msg)); 490 if (!state) 491 return; 492 493 manager_get_interface_cb(msg, state); 494 } 495 496 static void manager_filtered_interface_dump_done(void *user_data) 497 { 498 struct wiphy_setup_state *state = user_data; 499 500 l_debug(""); 501 502 if (!manager_wiphy_check_setup_done(state)) 503 return; 504 505 /* We decided to keep the the default interface(s) */ 506 l_debug("Wiphy setup complete: %s", wiphy_get_name(state->wiphy)); 507 wiphy_setup_state_destroy(state); 508 } 509 510 static bool manager_check_create_interfaces(void *data, void *user_data) 511 { 512 struct wiphy_setup_state *state = data; 513 514 if (!manager_wiphy_check_setup_done(state)) 515 return false; 516 517 wiphy_setup_state_free(state); 518 return true; 519 } 520 521 static void manager_interface_dump_done(void *user_data) 522 { 523 l_debug(""); 524 525 l_queue_foreach_remove(pending_wiphys, 526 manager_check_create_interfaces, NULL); 527 } 528 529 /* We are dumping multiple wiphys for the very first time */ 530 static void manager_wiphy_dump_callback(struct l_genl_msg *msg, void *user_data) 531 { 532 uint32_t id; 533 const char *name; 534 struct wiphy *wiphy; 535 struct wiphy_setup_state *state; 536 537 if (nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &id, 538 NL80211_ATTR_WIPHY_NAME, &name, 539 NL80211_ATTR_UNSPEC) < 0) 540 return; 541 542 /* 543 * A Wiphy split dump can generate many (6+) NEW_WIPHY messages 544 * We need to parse attributes from all of them, but only perform 545 * initialization steps once for each new wiphy detected 546 */ 547 wiphy = wiphy_find(id); 548 if (wiphy) 549 goto done; 550 551 wiphy = wiphy_create(id, name); 552 if (!wiphy || wiphy_is_blacklisted(wiphy)) 553 return; 554 555 state = l_new(struct wiphy_setup_state, 1); 556 state->id = id; 557 state->wiphy = wiphy; 558 559 l_debug("New wiphy %s added (%d)", name, id); 560 561 l_queue_push_tail(pending_wiphys, state); 562 563 done: 564 wiphy_update_from_genl(wiphy, msg); 565 } 566 567 /* We are dumping a single wiphy, due to a NEW_WIPHY event */ 568 static void manager_wiphy_filtered_dump_callback(struct l_genl_msg *msg, 569 void *user_data) 570 { 571 struct wiphy_setup_state *state = user_data; 572 573 wiphy_update_from_genl(state->wiphy, msg); 574 } 575 576 static void manager_filtered_wiphy_dump_done(void *user_data) 577 { 578 struct wiphy_setup_state *state = user_data; 579 580 l_debug(""); 581 582 wiphy_create_complete(state->wiphy); 583 state->use_default = use_default; 584 585 /* 586 * If whitelist/blacklist were given only try to use existing 587 * interfaces same as when the driver does not support 588 * NEW_INTERFACE or DEL_INTERFACE, otherwise the interface 589 * names will become meaningless after we've created our own 590 * interface(s). Optimally phy name white/blacklists should 591 * be used. 592 */ 593 if (whitelist_filter || blacklist_filter) 594 state->use_default = true; 595 596 if (!state->use_default) 597 state->use_default = wiphy_uses_default_if(state->wiphy); 598 599 if (state->use_default) 600 l_info("Wiphy %s will only use the default interface", 601 wiphy_get_name(state->wiphy)); 602 } 603 604 static void manager_wiphy_dump_done(void *user_data) 605 { 606 const struct l_queue_entry *e; 607 608 l_debug(""); 609 610 for (e = l_queue_get_entries(pending_wiphys); e; e = e->next) 611 manager_filtered_wiphy_dump_done(e->data); 612 } 613 614 static int manager_wiphy_filtered_dump(uint32_t wiphy_id, 615 l_genl_msg_func_t cb, 616 struct wiphy_setup_state *state) 617 { 618 struct l_genl_msg *msg; 619 unsigned int wiphy_cmd_id; 620 unsigned int iface_cmd_id; 621 l_genl_destroy_func_t wiphy_done = manager_wiphy_dump_done; 622 l_genl_destroy_func_t interface_done = manager_interface_dump_done; 623 624 /* If state is provided, don't try to process other pending states */ 625 if (state) { 626 wiphy_done = manager_filtered_wiphy_dump_done; 627 interface_done = manager_filtered_interface_dump_done; 628 } 629 630 /* 631 * Until fixed, a NEW_WIPHY event will not include all the information 632 * that may be available, but a dump will. Because of this we do both 633 * GET_WIPHY/GET_INTERFACE, same as we would during initialization. 634 */ 635 msg = l_genl_msg_new_sized(NL80211_CMD_GET_WIPHY, 128); 636 l_genl_msg_append_attr(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP, 0, NULL); 637 l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY, 4, &wiphy_id); 638 639 wiphy_cmd_id = l_genl_family_dump(nl80211, msg, cb, state, wiphy_done); 640 if (!wiphy_cmd_id) { 641 l_error("Could not dump wiphy %u", wiphy_id); 642 l_genl_msg_unref(msg); 643 return -EIO; 644 } 645 646 /* 647 * As the first step after new wiphy is detected we will query 648 * the initial interface setup, delete the default interfaces 649 * and create interfaces for our own use with NL80211_ATTR_SOCKET_OWNER 650 * on them. After that if new interfaces are created outside of 651 * IWD, or removed outside of IWD, we don't touch them and will 652 * try to minimally adapt to handle the removals correctly. It's 653 * a very unlikely situation in any case but it wouldn't make 654 * sense to try to continually enforce our setup fighting against 655 * some other process, and it wouldn't make sense to try to 656 * manage and use additional interfaces beyond the one or two 657 * we need for our operations. 658 */ 659 msg = l_genl_msg_new(NL80211_CMD_GET_INTERFACE); 660 l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY, 4, &wiphy_id); 661 662 iface_cmd_id = l_genl_family_dump(nl80211, msg, 663 manager_interface_dump_callback, 664 state, interface_done); 665 if (!iface_cmd_id) { 666 l_error("Could not dump interface for wiphy %u", wiphy_id); 667 l_genl_family_cancel(nl80211, wiphy_cmd_id); 668 l_genl_msg_unref(msg); 669 return -EIO; 670 } 671 672 return 0; 673 } 674 675 static void manager_config_notify(struct l_genl_msg *msg, void *user_data) 676 { 677 uint8_t cmd; 678 uint32_t wiphy_id; 679 struct wiphy_setup_state *state; 680 681 if (!pending_wiphys) 682 return; 683 684 cmd = l_genl_msg_get_command(msg); 685 686 l_debug("Notification of command %s(%u)", 687 nl80211cmd_to_string(cmd), cmd); 688 689 switch (cmd) { 690 case NL80211_CMD_NEW_WIPHY: 691 { 692 const char *name; 693 struct wiphy *wiphy; 694 695 if (nl80211_parse_attrs(msg, NL80211_ATTR_WIPHY, &wiphy_id, 696 NL80211_ATTR_WIPHY_NAME, &name, 697 NL80211_ATTR_UNSPEC) < 0) 698 return; 699 700 /* 701 * NEW_WIPHY events are sent in three cases: 702 * 1. New wiphy is detected 703 * 2. Wiphy is moved to a new namespace 704 * 3. Wiphy is renamed 705 * 706 * Take care of case 3 here without re-parsing the entire 707 * wiphy structure, potentially causing leaks, etc. 708 */ 709 wiphy = wiphy_find(wiphy_id); 710 if (wiphy) { 711 wiphy_update_name(wiphy, name); 712 return; 713 } 714 715 wiphy = wiphy_create(wiphy_id, name); 716 if (!wiphy || wiphy_is_blacklisted(wiphy)) 717 return; 718 719 state = l_new(struct wiphy_setup_state, 1); 720 state->id = wiphy_id; 721 state->wiphy = wiphy; 722 723 if (manager_wiphy_filtered_dump(wiphy_id, 724 manager_wiphy_filtered_dump_callback, 725 state) < 0) { 726 wiphy_setup_state_free(state); 727 return; 728 } 729 730 l_queue_push_tail(pending_wiphys, state); 731 return; 732 } 733 734 case NL80211_CMD_DEL_WIPHY: 735 manager_del_wiphy_event(msg); 736 return; 737 738 case NL80211_CMD_NEW_INTERFACE: 739 /* 740 * Interfaces are normally dumped on the NEW_WIPHY events and 741 * we have nothing to do here. But check if by any chance 742 * we've queried this wiphy and it was still busy initialising, 743 * in that case retry the setup now that an interface, likely 744 * the initial default one, has been added. 745 */ 746 wiphy_id = manager_parse_wiphy_id(msg); 747 state = manager_find_pending(wiphy_id); 748 749 if (state && state->retry) { 750 state->retry = false; 751 l_debug("Retrying setup of wiphy %u", state->id); 752 753 manager_get_interface_cb(msg, state); 754 755 if (manager_wiphy_check_setup_done(state)) 756 wiphy_setup_state_destroy(state); 757 758 return; 759 } 760 761 if (!wiphy_find(wiphy_id)) { 762 l_warn("Received a NEW_INTERFACE for a wiphy id" 763 " that isn't tracked. This is most likely a" 764 " kernel bug where NEW_WIPHY events that are" 765 " too large are dropped on the floor." 766 " Attempting a workaround..."); 767 manager_wiphy_filtered_dump(wiphy_id, 768 manager_wiphy_dump_callback, 769 NULL); 770 return; 771 } 772 773 return; 774 775 case NL80211_CMD_DEL_INTERFACE: 776 { 777 uint32_t ifindex; 778 779 if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex, 780 NL80211_ATTR_UNSPEC) < 0) { 781 uint64_t wdev_id; 782 struct p2p_device *p2p_device; 783 784 if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, 785 &wdev_id, 786 NL80211_ATTR_UNSPEC) < 0) 787 return; 788 789 p2p_device = p2p_device_find(wdev_id); 790 if (!p2p_device) 791 return; 792 793 p2p_device_destroy(p2p_device); 794 } else { 795 struct netdev *netdev = netdev_find(ifindex); 796 797 if (!netdev) 798 return; 799 800 netdev_destroy(netdev); 801 } 802 803 return; 804 } 805 } 806 } 807 808 static void manager_set_reg_cb(struct l_genl_msg *msg, void *user_data) 809 { 810 int err = l_genl_msg_get_error(msg); 811 812 if (err < 0) 813 l_error("Failed to set country (%d)", err); 814 } 815 816 static int manager_init(void) 817 { 818 struct l_genl *genl = iwd_get_genl(); 819 const struct l_settings *config = iwd_get_config(); 820 struct l_genl_msg *msg; 821 unsigned int wiphy_dump; 822 unsigned int interface_dump; 823 const char *randomize_str; 824 const char *if_whitelist = iwd_get_iface_whitelist(); 825 const char *if_blacklist = iwd_get_iface_blacklist(); 826 const char *cc; 827 828 nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME); 829 830 if (if_whitelist) 831 whitelist_filter = l_strsplit(if_whitelist, ','); 832 833 if (if_blacklist) 834 blacklist_filter = l_strsplit(if_blacklist, ','); 835 836 pending_wiphys = l_queue_new(); 837 838 config_watch = l_genl_family_register(nl80211, "config", 839 manager_config_notify, 840 NULL, NULL); 841 if (!config_watch) { 842 l_error("Registering for config notifications failed"); 843 goto error; 844 } 845 846 msg = l_genl_msg_new_sized(NL80211_CMD_GET_WIPHY, 128); 847 l_genl_msg_append_attr(msg, NL80211_ATTR_SPLIT_WIPHY_DUMP, 0, NULL); 848 wiphy_dump = l_genl_family_dump(nl80211, msg, 849 manager_wiphy_dump_callback, 850 NULL, 851 manager_wiphy_dump_done); 852 if (!wiphy_dump) { 853 l_error("Initial wiphy information dump failed"); 854 l_genl_msg_unref(msg); 855 goto error; 856 } 857 858 msg = l_genl_msg_new(NL80211_CMD_GET_INTERFACE); 859 interface_dump = l_genl_family_dump(nl80211, msg, 860 manager_interface_dump_callback, 861 NULL, 862 manager_interface_dump_done); 863 if (!interface_dump) { 864 l_error("Initial interface information dump failed"); 865 l_genl_msg_unref(msg); 866 l_genl_family_cancel(nl80211, wiphy_dump); 867 goto error; 868 } 869 870 cc = l_settings_get_value(config, "General", "Country"); 871 if (cc) { 872 if (strlen(cc) != 2 || !l_ascii_isalpha(cc[0]) || 873 !l_ascii_isalpha(cc[1])) { 874 l_warn("[General].Country=%s is invalid. Country will " 875 "not be set", cc); 876 } else { 877 msg = l_genl_msg_new(NL80211_CMD_REQ_SET_REG); 878 l_genl_msg_append_attr(msg, NL80211_ATTR_REG_ALPHA2, 879 2, cc); 880 if (!l_genl_family_send(nl80211, msg, 881 manager_set_reg_cb, 882 NULL, NULL)) 883 l_warn("Failed to set country"); 884 } 885 } 886 887 randomize_str = l_settings_get_value(config, "General", 888 "AddressRandomization"); 889 if (randomize_str) { 890 if (!strcmp(randomize_str, "once")) 891 randomize = true; 892 else if (!strcmp(randomize_str, "disabled")) 893 randomize = false; 894 } 895 896 if (!l_settings_get_bool(config, "General", 897 "UseDefaultInterface", &use_default)) 898 use_default = false; 899 else 900 l_warn("[General].UseDefaultInterface is deprecated, please " 901 "use [DriverQuirks].DefaultInterface instead"); 902 903 return 0; 904 905 error: 906 l_queue_destroy(pending_wiphys, NULL); 907 pending_wiphys = NULL; 908 909 l_genl_family_free(nl80211); 910 nl80211 = NULL; 911 912 return -EIO; 913 } 914 915 static void manager_exit(void) 916 { 917 l_strfreev(whitelist_filter); 918 l_strfreev(blacklist_filter); 919 920 l_genl_family_unregister(nl80211, config_watch); 921 922 l_queue_destroy(pending_wiphys, wiphy_setup_state_free); 923 pending_wiphys = NULL; 924 925 l_genl_family_free(nl80211); 926 nl80211 = NULL; 927 randomize = false; 928 } 929 930 IWD_MODULE(manager, manager_init, manager_exit);