1 /* 2 * netifd - network interface daemon 3 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 7 * as published by the Free Software Foundation 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 /* The wireless configuration is projected on the following objects 16 * 17 * 1. wireless device 18 * 2. wireless interface 19 * 3. wireless vlan 20 * 4. wireless station 21 * 22 * A wireless device is a phy or simplified a wireless card. 23 * A wireless interface is a SSID on a phy. 24 * A wireless vlan can be assigned to a wireless interface. A wireless interface can 25 * have multiple vlans. 26 * A wireless station is a client connected to an wireless interface. 27 */ 28 29 #include <signal.h> 30 #include "netifd.h" 31 #include "wireless.h" 32 #include "handler.h" 33 #include "ubus.h" 34 35 #define WIRELESS_SETUP_RETRY 3 36 37 struct vlist_tree wireless_devices; 38 struct avl_tree wireless_drivers; 39 static struct blob_buf b; 40 static int drv_fd; 41 static LIST_HEAD(handlers); 42 static bool handler_pending; 43 44 enum { 45 WDEV_ATTR_DISABLED, 46 WDEV_ATTR_RECONF, 47 WDEV_ATTR_SERIALIZE, 48 __WDEV_ATTR_MAX, 49 }; 50 51 static const struct blobmsg_policy wdev_policy[__WDEV_ATTR_MAX] = { 52 [WDEV_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL }, 53 [WDEV_ATTR_RECONF] = { .name = "reconf", .type = BLOBMSG_TYPE_BOOL }, 54 [WDEV_ATTR_SERIALIZE] = { .name = "serialize", .type = BLOBMSG_TYPE_BOOL }, 55 }; 56 57 static const struct uci_blob_param_list wdev_param = { 58 .n_params = ARRAY_SIZE(wdev_policy), 59 .params = wdev_policy, 60 }; 61 62 enum { 63 VIF_ATTR_DISABLED, 64 VIF_ATTR_NETWORK, 65 VIF_ATTR_NETWORK_VLAN, 66 VIF_ATTR_BRIDGE_ISOLATE, 67 VIF_ATTR_ISOLATE, 68 VIF_ATTR_MODE, 69 VIF_ATTR_PROXYARP, 70 VIF_ATTR_MCAST_TO_UCAST, 71 __VIF_ATTR_MAX, 72 }; 73 74 static const struct blobmsg_policy vif_policy[__VIF_ATTR_MAX] = { 75 [VIF_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL }, 76 [VIF_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY }, 77 [VIF_ATTR_NETWORK_VLAN] = { .name = "network_vlan", .type = BLOBMSG_TYPE_ARRAY }, 78 [VIF_ATTR_BRIDGE_ISOLATE] = { .name = "bridge_isolate", .type = BLOBMSG_TYPE_BOOL }, 79 [VIF_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL }, 80 [VIF_ATTR_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING }, 81 [VIF_ATTR_PROXYARP] = { .name = "proxy_arp", .type = BLOBMSG_TYPE_BOOL }, 82 [VIF_ATTR_MCAST_TO_UCAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL }, 83 }; 84 85 static const struct uci_blob_param_list vif_param = { 86 .n_params = ARRAY_SIZE(vif_policy), 87 .params = vif_policy, 88 }; 89 90 enum { 91 VLAN_ATTR_DISABLED, 92 VLAN_ATTR_NETWORK, 93 VLAN_ATTR_NETWORK_VLAN, 94 VLAN_ATTR_BRIDGE_ISOLATE, 95 VLAN_ATTR_ISOLATE, 96 VLAN_ATTR_MCAST_TO_UCAST, 97 __VLAN_ATTR_MAX, 98 }; 99 100 static const struct blobmsg_policy vlan_policy[__VLAN_ATTR_MAX] = { 101 [VLAN_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL }, 102 [VLAN_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_ARRAY }, 103 [VLAN_ATTR_NETWORK_VLAN] = { .name = "network_vlan", .type = BLOBMSG_TYPE_ARRAY }, 104 [VLAN_ATTR_BRIDGE_ISOLATE] = { .name = "bridge_isolate", .type = BLOBMSG_TYPE_BOOL }, 105 [VLAN_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL }, 106 [VLAN_ATTR_MCAST_TO_UCAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL }, 107 }; 108 109 static const struct uci_blob_param_list vlan_param = { 110 .n_params = ARRAY_SIZE(vlan_policy), 111 .params = vlan_policy, 112 }; 113 114 enum { 115 STA_ATTR_DISABLED, 116 __STA_ATTR_MAX, 117 }; 118 119 static const struct blobmsg_policy sta_policy[__STA_ATTR_MAX] = { 120 [STA_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL }, 121 }; 122 123 static const struct uci_blob_param_list station_param = { 124 .n_params = ARRAY_SIZE(sta_policy), 125 .params = sta_policy, 126 }; 127 128 static void 129 wireless_handler_stop(struct wireless_device *wdev) 130 { 131 if (wdev->handler_pending) { 132 wdev->handler_pending = false; 133 list_del(&wdev->handler); 134 } 135 } 136 137 static void 138 put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name) 139 { 140 void *c = blobmsg_open_table(buf, name); 141 blob_put_raw(buf, blobmsg_data(attr), blobmsg_len(attr)); 142 blobmsg_close_table(buf, c); 143 } 144 145 static void 146 vif_config_add_bridge(struct blob_buf *buf, struct blob_attr *networks, bool prepare) 147 { 148 struct interface *iface; 149 struct device *dev = NULL, *orig_dev; 150 struct blob_attr *cur; 151 const char *network; 152 size_t rem; 153 154 if (!networks) 155 return; 156 157 blobmsg_for_each_attr(cur, networks, rem) { 158 network = blobmsg_data(cur); 159 160 iface = vlist_find(&interfaces, network, iface, node); 161 if (!iface) 162 continue; 163 164 dev = iface->main_dev.dev; 165 if (!dev) 166 return; 167 168 if (!dev->hotplug_ops) 169 return; 170 } 171 172 if (!dev) 173 return; 174 175 orig_dev = dev; 176 if (dev->hotplug_ops && dev->hotplug_ops->prepare) 177 dev->hotplug_ops->prepare(dev, &dev); 178 179 if (!dev || !dev->type->bridge_capability) 180 return; 181 182 blobmsg_add_string(buf, "bridge", dev->ifname); 183 blobmsg_add_string(buf, "bridge-ifname", orig_dev->ifname); 184 185 if (dev->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST) 186 blobmsg_add_u8(buf, "multicast_to_unicast", 187 dev->settings.multicast_to_unicast); 188 } 189 190 static void 191 prepare_config(struct wireless_device *wdev, struct blob_buf *buf, bool up) 192 { 193 struct wireless_interface *vif; 194 struct wireless_vlan *vlan; 195 struct wireless_station *sta; 196 void *l, *i, *j, *k; 197 198 blob_buf_init(&b, 0); 199 put_container(&b, wdev->config, "config"); 200 if (wdev->data) 201 blobmsg_add_blob(&b, wdev->data); 202 203 l = blobmsg_open_table(&b, "interfaces"); 204 vlist_for_each_element(&wdev->interfaces, vif, node) { 205 if (vif->disabled) 206 continue; 207 208 i = blobmsg_open_table(&b, vif->name); 209 vif_config_add_bridge(&b, vif->network, up); 210 put_container(&b, vif->config, "config"); 211 if (vif->data) 212 blobmsg_add_blob(&b, vif->data); 213 214 j = blobmsg_open_table(&b, "vlans"); 215 vlist_for_each_element(&vif->vlans, vlan, node) { 216 k = blobmsg_open_table(&b, vlan->name); 217 vif_config_add_bridge(&b, vlan->network, up); 218 put_container(&b, vlan->config, "config"); 219 if (vlan->data) 220 blobmsg_add_blob(&b, vlan->data); 221 blobmsg_close_table(&b, k); 222 } 223 blobmsg_close_table(&b, j); 224 225 j = blobmsg_open_table(&b, "stas"); 226 vlist_for_each_element(&vif->stations, sta, node) { 227 k = blobmsg_open_table(&b, sta->name); 228 put_container(&b, sta->config, "config"); 229 if (sta->data) 230 blobmsg_add_blob(&b, sta->data); 231 blobmsg_close_table(&b, k); 232 } 233 blobmsg_close_table(&b, j); 234 blobmsg_close_table(&b, i); 235 } 236 blobmsg_close_table(&b, l); 237 238 } 239 240 static bool 241 wireless_process_check(struct wireless_process *proc) 242 { 243 return check_pid_path(proc->pid, proc->exe); 244 } 245 246 static void 247 wireless_complete_kill_request(struct wireless_device *wdev) 248 { 249 if (!wdev->kill_request) 250 return; 251 252 ubus_complete_deferred_request(ubus_ctx, wdev->kill_request, 0); 253 free(wdev->kill_request); 254 wdev->kill_request = NULL; 255 } 256 257 static void 258 wireless_process_free(struct wireless_device *wdev, struct wireless_process *proc) 259 { 260 D(WIRELESS, "Wireless device '%s' free pid %d", wdev->name, proc->pid); 261 list_del(&proc->list); 262 free(proc); 263 264 if (list_empty(&wdev->script_proc)) 265 wireless_complete_kill_request(wdev); 266 } 267 268 static void 269 wireless_close_script_proc_fd(struct wireless_device *wdev) 270 { 271 if (wdev->script_proc_fd.fd < 0) 272 return; 273 274 uloop_fd_delete(&wdev->script_proc_fd); 275 close(wdev->script_proc_fd.fd); 276 wdev->script_proc_fd.fd = -1; 277 } 278 279 static void 280 wireless_process_kill_all(struct wireless_device *wdev, int signal, bool free) 281 { 282 struct wireless_process *proc, *tmp; 283 284 list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) { 285 bool check = wireless_process_check(proc); 286 287 if (check && !proc->keep) { 288 D(WIRELESS, "Wireless device '%s' kill pid %d", wdev->name, proc->pid); 289 kill(proc->pid, signal); 290 } 291 292 if (free || !check) 293 wireless_process_free(wdev, proc); 294 } 295 296 if (free) 297 wireless_close_script_proc_fd(wdev); 298 } 299 300 static void 301 wireless_device_free_state(struct wireless_device *wdev) 302 { 303 struct wireless_interface *vif; 304 struct wireless_vlan *vlan; 305 struct wireless_station *sta; 306 307 wireless_handler_stop(wdev); 308 uloop_timeout_cancel(&wdev->script_check); 309 uloop_timeout_cancel(&wdev->timeout); 310 wireless_complete_kill_request(wdev); 311 free(wdev->data); 312 wdev->data = NULL; 313 vlist_for_each_element(&wdev->interfaces, vif, node) { 314 free(vif->data); 315 vif->data = NULL; 316 vif->ifname = NULL; 317 vlist_for_each_element(&vif->vlans, vlan, node) { 318 free(vlan->data); 319 vlan->data = NULL; 320 vlan->ifname = NULL; 321 } 322 vlist_for_each_element(&vif->stations, sta, node) { 323 free(sta->data); 324 sta->data = NULL; 325 } 326 } 327 } 328 329 static void wireless_device_set_mcast_to_unicast(struct device *dev, int val) 330 { 331 if (val < 0) { 332 dev->settings.flags &= ~DEV_OPT_MULTICAST_TO_UNICAST; 333 return; 334 } 335 336 dev->settings.multicast_to_unicast = !!val; 337 dev->settings.flags |= DEV_OPT_MULTICAST_TO_UNICAST; 338 } 339 340 static void wireless_interface_handle_link(struct wireless_interface *vif, const char *ifname, bool up) 341 { 342 struct interface *iface; 343 struct blob_attr *cur; 344 const char *network; 345 struct device *dev; 346 size_t rem; 347 348 if (!vif->network || !vif->ifname) 349 return; 350 351 if (!ifname) 352 ifname = vif->ifname; 353 354 if (!up) 355 goto out; 356 357 dev = __device_get(ifname, 2, false); 358 if (!dev) 359 goto out; 360 361 dev->wireless = true; 362 dev->settings.flags |= DEV_OPT_ISOLATE; 363 dev->settings.isolate = vif->bridge_isolate; 364 365 if (strcmp(ifname, vif->ifname) != 0) 366 goto out; 367 368 dev->wireless_isolate = vif->isolate; 369 dev->wireless_proxyarp = vif->proxyarp; 370 dev->wireless_ap = vif->ap_mode; 371 wireless_device_set_mcast_to_unicast(dev, vif->multicast_to_unicast); 372 dev->bpdu_filter = dev->wireless_ap; 373 374 out: 375 blobmsg_for_each_attr(cur, vif->network, rem) { 376 network = blobmsg_data(cur); 377 378 iface = vlist_find(&interfaces, network, iface, node); 379 if (!iface) 380 continue; 381 382 interface_handle_link(iface, ifname, vif->network_vlan, up, true); 383 } 384 } 385 386 static void wireless_vlan_handle_link(struct wireless_vlan *vlan, bool up) 387 { 388 struct interface *iface; 389 struct blob_attr *cur; 390 const char *network; 391 size_t rem; 392 393 if (!vlan->network || !vlan->ifname) 394 return; 395 396 if (up) { 397 struct device *dev = device_get(vlan->ifname, 2); 398 if (dev) { 399 dev->wireless_isolate = vlan->isolate; 400 dev->wireless = true; 401 dev->wireless_ap = true; 402 dev->bpdu_filter = true; 403 dev->settings.flags |= DEV_OPT_ISOLATE; 404 dev->settings.isolate = vlan->bridge_isolate; 405 wireless_device_set_mcast_to_unicast(dev, vlan->multicast_to_unicast); 406 } 407 } 408 409 blobmsg_for_each_attr(cur, vlan->network, rem) { 410 network = blobmsg_data(cur); 411 412 iface = vlist_find(&interfaces, network, iface, node); 413 if (!iface) 414 continue; 415 416 interface_handle_link(iface, vlan->ifname, vlan->network_vlan, up, true); 417 } 418 } 419 420 static void 421 wireless_device_setup_cancel(struct wireless_device *wdev) 422 { 423 if (wdev->cancel) 424 return; 425 426 wireless_handler_stop(wdev); 427 D(WIRELESS, "Cancel wireless device '%s' setup", wdev->name); 428 wdev->cancel = true; 429 uloop_timeout_set(&wdev->timeout, 10 * 1000); 430 } 431 432 static void 433 wireless_device_run_handler(struct wireless_device *wdev, bool up) 434 { 435 const char *action = up ? "setup" : "teardown"; 436 const char *argv[6]; 437 char *config; 438 int i = 0; 439 int fds[2] = { -1, -1 }; 440 441 wireless_handler_stop(wdev); 442 443 if (handler_pending && wdev->serialize) { 444 wdev->handler_action = up; 445 wdev->handler_pending = true; 446 list_add_tail(&wdev->handler, &handlers); 447 return; 448 } 449 if (wdev->serialize) 450 handler_pending = true; 451 452 D(WIRELESS, "Wireless device '%s' run %s handler", wdev->name, action); 453 if (!up && wdev->prev_config) { 454 config = blobmsg_format_json(wdev->prev_config, true); 455 free(wdev->prev_config); 456 wdev->prev_config = NULL; 457 } else { 458 prepare_config(wdev, &b, up); 459 free(wdev->prev_config); 460 wdev->prev_config = up ? blob_memdup(b.head) : NULL; 461 config = blobmsg_format_json(b.head, true); 462 } 463 464 argv[i++] = wdev->drv->script; 465 argv[i++] = wdev->drv->name; 466 argv[i++] = action; 467 argv[i++] = wdev->name; 468 argv[i++] = config; 469 argv[i] = NULL; 470 471 if (up && pipe(fds) == 0) { 472 if (wdev->script_proc_fd.fd >= 0) 473 wireless_close_script_proc_fd(wdev); 474 475 wdev->script_proc_fd.fd = fds[0]; 476 uloop_fd_add(&wdev->script_proc_fd, 477 ULOOP_READ | ULOOP_EDGE_TRIGGER); 478 } 479 480 netifd_start_process(argv, NULL, &wdev->script_task); 481 482 if (fds[1] >= 0) 483 close(fds[1]); 484 485 free(config); 486 } 487 488 static void 489 wireless_handler_next(void) 490 { 491 struct wireless_device *wdev; 492 493 if (handler_pending) 494 return; 495 if (list_empty(&handlers)) 496 return; 497 wdev = list_first_entry(&handlers, struct wireless_device, handler); 498 list_del(&wdev->handler); 499 wdev->handler_pending = false; 500 wireless_device_run_handler(wdev, wdev->handler_action); 501 } 502 503 static void 504 __wireless_device_set_up(struct wireless_device *wdev, int force) 505 { 506 if (wdev->disabled) 507 return; 508 509 if (wdev->retry_setup_failed) 510 return; 511 512 if (!wdev->autostart) 513 return; 514 515 if ((!force && wdev->state != IFS_DOWN) || config_init) 516 return; 517 518 wdev->state = IFS_SETUP; 519 wireless_device_run_handler(wdev, true); 520 } 521 522 static void 523 wireless_device_free(struct wireless_device *wdev) 524 { 525 wireless_handler_stop(wdev); 526 vlist_flush_all(&wdev->interfaces); 527 avl_delete(&wireless_devices.avl, &wdev->node.avl); 528 free(wdev->config); 529 free(wdev->prev_config); 530 free(wdev); 531 } 532 533 static void 534 wdev_handle_config_change(struct wireless_device *wdev) 535 { 536 enum interface_config_state state = wdev->config_state; 537 538 switch(state) { 539 case IFC_RELOAD: 540 wdev->retry = WIRELESS_SETUP_RETRY; 541 wdev->retry_setup_failed = false; 542 fallthrough; 543 case IFC_NORMAL: 544 __wireless_device_set_up(wdev, 0); 545 546 wdev->config_state = IFC_NORMAL; 547 break; 548 case IFC_REMOVE: 549 wireless_device_free(wdev); 550 break; 551 } 552 } 553 554 static void 555 wireless_device_mark_down(struct wireless_device *wdev) 556 { 557 struct wireless_interface *vif; 558 struct wireless_vlan *vlan; 559 560 netifd_log_message(L_NOTICE, "Wireless device '%s' is now down\n", wdev->name); 561 562 563 vlist_for_each_element(&wdev->interfaces, vif, node) { 564 wireless_interface_handle_link(vif, NULL, false); 565 vlist_for_each_element(&vif->vlans, vlan, node) 566 wireless_vlan_handle_link(vlan, false); 567 } 568 569 wireless_process_kill_all(wdev, SIGTERM, true); 570 571 wdev->cancel = false; 572 wdev->state = IFS_DOWN; 573 wireless_device_free_state(wdev); 574 wdev_handle_config_change(wdev); 575 } 576 577 /* timeout callback to protect the tear down */ 578 static void 579 wireless_device_setup_timeout(struct uloop_timeout *timeout) 580 { 581 struct wireless_device *wdev = container_of(timeout, struct wireless_device, timeout); 582 583 if (wdev->handler_pending) { 584 wdev->handler_pending = false; 585 list_del(&wdev->handler); 586 } 587 netifd_kill_process(&wdev->script_task); 588 wdev->script_task.cb(&wdev->script_task, -1); 589 wireless_device_mark_down(wdev); 590 } 591 592 void 593 wireless_device_set_up(struct wireless_device *wdev) 594 { 595 wdev->retry = WIRELESS_SETUP_RETRY; 596 wdev->autostart = true; 597 __wireless_device_set_up(wdev, 0); 598 } 599 600 void 601 wireless_device_reconf(struct wireless_device *wdev) 602 { 603 wdev->retry = WIRELESS_SETUP_RETRY; 604 wdev->autostart = true; 605 __wireless_device_set_up(wdev, wdev->reconf && (wdev->state == IFS_UP)); 606 } 607 608 static void 609 __wireless_device_set_down(struct wireless_device *wdev) 610 { 611 if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN) 612 return; 613 614 if (wdev->script_task.uloop.pending) { 615 wireless_device_setup_cancel(wdev); 616 return; 617 } 618 619 wdev->state = IFS_TEARDOWN; 620 wireless_device_run_handler(wdev, false); 621 } 622 623 /* ubus callback network.wireless.notify, command = up */ 624 static void 625 wireless_device_mark_up(struct wireless_device *wdev) 626 { 627 struct wireless_interface *vif; 628 struct wireless_vlan *vlan; 629 630 if (wdev->cancel) { 631 wdev->cancel = false; 632 __wireless_device_set_down(wdev); 633 return; 634 } 635 636 netifd_log_message(L_NOTICE, "Wireless device '%s' is now up\n", wdev->name); 637 wdev->retry = WIRELESS_SETUP_RETRY; 638 wdev->state = IFS_UP; 639 vlist_for_each_element(&wdev->interfaces, vif, node) { 640 wireless_interface_handle_link(vif, NULL, true); 641 vlist_for_each_element(&vif->vlans, vlan, node) 642 wireless_vlan_handle_link(vlan, true); 643 } 644 } 645 646 static void 647 wireless_device_retry_setup(struct wireless_device *wdev) 648 { 649 if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN || wdev->cancel) 650 return; 651 652 netifd_log_message(wdev->retry ? L_WARNING : L_CRIT, 653 "Wireless device '%s' setup failed, retry=%d\n", 654 wdev->name, wdev->retry); 655 if (--wdev->retry < 0) 656 wdev->retry_setup_failed = true; 657 658 __wireless_device_set_down(wdev); 659 } 660 661 static void 662 wireless_device_script_task_cb(struct netifd_process *proc, int ret) 663 { 664 struct wireless_device *wdev = container_of(proc, struct wireless_device, script_task); 665 666 switch (wdev->state) { 667 case IFS_SETUP: 668 wireless_device_retry_setup(wdev); 669 break; 670 case IFS_TEARDOWN: 671 wireless_device_mark_down(wdev); 672 break; 673 default: 674 break; 675 } 676 677 if (wdev->serialize) { 678 handler_pending = false; 679 wireless_handler_next(); 680 } 681 } 682 683 void 684 wireless_device_set_down(struct wireless_device *wdev) 685 { 686 wdev->retry_setup_failed = false; 687 wdev->autostart = false; 688 __wireless_device_set_down(wdev); 689 } 690 691 static void 692 wdev_set_config_state(struct wireless_device *wdev, enum interface_config_state s) 693 { 694 if (wdev->config_state != IFC_NORMAL) 695 return; 696 697 wdev->config_update = false; 698 if (!wdev->disabled && s == IFC_RELOAD && wdev->reconf && wdev->state == IFS_UP) { 699 wireless_device_reconf(wdev); 700 return; 701 } 702 703 wdev->config_state = s; 704 if (wdev->state == IFS_DOWN) 705 wdev_handle_config_change(wdev); 706 else 707 __wireless_device_set_down(wdev); 708 } 709 710 static void 711 wdev_prepare_prev_config(struct wireless_device *wdev) 712 { 713 prepare_config(wdev, &b, false); 714 free(wdev->prev_config); 715 wdev->prev_config = blob_memdup(b.head); 716 } 717 718 static void 719 wdev_change_config(struct wireless_device *wdev, struct wireless_device *wd_new) 720 { 721 struct blob_attr *new_config = wd_new->config; 722 bool disabled = wd_new->disabled; 723 724 wdev->reconf = wd_new->reconf; 725 wdev->serialize = wd_new->serialize; 726 free(wd_new); 727 728 if (blob_attr_equal(wdev->config, new_config) && wdev->disabled == disabled) 729 return; 730 731 D(WIRELESS, "Update configuration of wireless device '%s'", wdev->name); 732 free(wdev->config); 733 wdev->config = blob_memdup(new_config); 734 wdev->disabled = disabled; 735 wdev->config_update = true; 736 } 737 738 static void 739 wdev_create(struct wireless_device *wdev) 740 { 741 wdev->retry = WIRELESS_SETUP_RETRY; 742 wdev->config = blob_memdup(wdev->config); 743 } 744 745 /* vlist update call for wireless device list */ 746 static void 747 wdev_update(struct vlist_tree *tree, struct vlist_node *node_new, 748 struct vlist_node *node_old) 749 { 750 struct wireless_device *wd_old = container_of(node_old, struct wireless_device, node); 751 struct wireless_device *wd_new = container_of(node_new, struct wireless_device, node); 752 753 if (wd_old && wd_new) { 754 D(WIRELESS, "Update wireless device '%s'", wd_old->name); 755 wdev_change_config(wd_old, wd_new); 756 } else if (wd_old) { 757 D(WIRELESS, "Delete wireless device '%s'", wd_old->name); 758 wdev_set_config_state(wd_old, IFC_REMOVE); 759 } else if (wd_new) { 760 D(WIRELESS, "Create wireless device '%s'", wd_new->name); 761 wdev_create(wd_new); 762 } 763 } 764 765 /* wireless netifd script handler */ 766 static void 767 wireless_add_handler(const char *script, const char *name, json_object *obj) 768 { 769 struct wireless_driver *drv; 770 char *name_str, *script_str; 771 json_object *dev_config_obj, *iface_config_obj, *vlan_config_obj, *station_config_obj; 772 struct uci_blob_param_list *dev_config, *iface_config, *vlan_config, *station_config; 773 774 dev_config_obj = json_get_field(obj, "device", json_type_array); 775 iface_config_obj = json_get_field(obj, "iface", json_type_array); 776 vlan_config_obj = json_get_field(obj, "vlan", json_type_array); 777 station_config_obj = json_get_field(obj, "station", json_type_array); 778 779 if (!dev_config_obj || !iface_config_obj || !vlan_config_obj || !station_config_obj) 780 return; 781 782 drv = calloc_a(sizeof(*drv), 783 &dev_config, sizeof(*dev_config) + sizeof(void *), 784 &iface_config, sizeof(*iface_config) + sizeof(void *), 785 &vlan_config, sizeof(*vlan_config) + sizeof(void *), 786 &station_config, sizeof(*station_config) + sizeof(void *), 787 &name_str, strlen(name) + 1, 788 &script_str, strlen(script) + 1); 789 790 drv->name = strcpy(name_str, name); 791 drv->script = strcpy(script_str, script); 792 793 dev_config->n_next = 1; 794 dev_config->next[0] = &wdev_param; 795 drv->device.config = dev_config; 796 797 iface_config->n_next = 1; 798 iface_config->next[0] = &vif_param; 799 drv->interface.config = iface_config; 800 801 vlan_config->n_next = 1; 802 vlan_config->next[0] = &vlan_param; 803 drv->vlan.config = vlan_config; 804 805 station_config->n_next = 1; 806 station_config->next[0] = &station_param; 807 drv->station.config = station_config; 808 809 drv->device.buf = netifd_handler_parse_config(drv->device.config, dev_config_obj); 810 drv->interface.buf = netifd_handler_parse_config(drv->interface.config, iface_config_obj); 811 drv->vlan.buf = netifd_handler_parse_config(drv->vlan.config, vlan_config_obj); 812 drv->station.buf = netifd_handler_parse_config(drv->station.config, station_config_obj); 813 814 drv->node.key = drv->name; 815 avl_insert(&wireless_drivers, &drv->node); 816 D(WIRELESS, "Add handler for script %s: %s", script, name); 817 } 818 819 void wireless_init(void) 820 { 821 vlist_init(&wireless_devices, avl_strcmp, wdev_update); 822 wireless_devices.keep_old = true; 823 wireless_devices.no_delete = true; 824 825 avl_init(&wireless_drivers, avl_strcmp, false, NULL); 826 drv_fd = netifd_open_subdir("wireless"); 827 if (drv_fd < 0) 828 return; 829 830 netifd_init_script_handlers(drv_fd, wireless_add_handler); 831 } 832 833 /* parse blob config into the wireless interface object */ 834 static void 835 wireless_interface_init_config(struct wireless_interface *vif) 836 { 837 struct blob_attr *tb[__VIF_ATTR_MAX]; 838 struct blob_attr *cur; 839 840 vif->network = NULL; 841 blobmsg_parse_attr(vif_policy, __VIF_ATTR_MAX, tb, vif->config); 842 843 if ((cur = tb[VIF_ATTR_NETWORK])) 844 vif->network = cur; 845 846 if ((cur = tb[VIF_ATTR_NETWORK_VLAN])) 847 vif->network_vlan = cur; 848 849 cur = tb[VIF_ATTR_MODE]; 850 vif->ap_mode = cur && !strcmp(blobmsg_get_string(cur), "ap"); 851 852 cur = tb[VIF_ATTR_BRIDGE_ISOLATE]; 853 vif->bridge_isolate = cur && blobmsg_get_bool(cur); 854 855 cur = tb[VIF_ATTR_ISOLATE]; 856 vif->isolate = cur && blobmsg_get_bool(cur); 857 858 cur = tb[VIF_ATTR_PROXYARP]; 859 vif->proxyarp = vif->ap_mode && cur && blobmsg_get_bool(cur); 860 861 cur = tb[VIF_ATTR_MCAST_TO_UCAST]; 862 vif->multicast_to_unicast = cur ? blobmsg_get_bool(cur) : -1; 863 } 864 865 static void 866 vif_free(struct wireless_interface *vif) 867 { 868 vlist_flush_all(&vif->vlans); 869 vlist_flush_all(&vif->stations); 870 free((void *) vif->section); 871 free(vif->config); 872 free(vif); 873 } 874 875 /* vlist update call for wireless interface list */ 876 static void 877 vif_update(struct vlist_tree *tree, struct vlist_node *node_new, 878 struct vlist_node *node_old) 879 { 880 struct wireless_interface *vif_old = container_of(node_old, struct wireless_interface, node); 881 struct wireless_interface *vif_new = container_of(node_new, struct wireless_interface, node); 882 struct wireless_device *wdev; 883 884 if (vif_old) 885 wdev = vif_old->wdev; 886 else 887 wdev = vif_new->wdev; 888 889 if (vif_old && vif_new) { 890 free((void *) vif_old->section); 891 vif_old->section = strdup(vif_new->section); 892 if (blob_attr_equal(vif_old->config, vif_new->config)) { 893 free(vif_new); 894 return; 895 } 896 897 D(WIRELESS, "Update wireless interface %s on device %s", vif_new->name, wdev->name); 898 wireless_interface_handle_link(vif_old, NULL, false); 899 free(vif_old->config); 900 vif_old->config = blob_memdup(vif_new->config); 901 wireless_interface_init_config(vif_old); 902 free(vif_new); 903 } else if (vif_new) { 904 D(WIRELESS, "Create new wireless interface %s on device %s", vif_new->name, wdev->name); 905 vif_new->section = strdup(vif_new->section); 906 vif_new->config = blob_memdup(vif_new->config); 907 wireless_interface_init_config(vif_new); 908 } else if (vif_old) { 909 D(WIRELESS, "Delete wireless interface %s on device %s", vif_old->name, wdev->name); 910 wireless_interface_handle_link(vif_old, NULL, false); 911 vif_free(vif_old); 912 } 913 914 wdev->config_update = true; 915 } 916 917 /* parse blob config into the vlan object */ 918 static void 919 wireless_vlan_init_config(struct wireless_vlan *vlan) 920 { 921 struct blob_attr *tb[__VLAN_ATTR_MAX]; 922 struct blob_attr *cur; 923 924 vlan->network = NULL; 925 blobmsg_parse_attr(vlan_policy, __VLAN_ATTR_MAX, tb, vlan->config); 926 927 if ((cur = tb[VLAN_ATTR_NETWORK])) 928 vlan->network = cur; 929 930 if ((cur = tb[VLAN_ATTR_NETWORK_VLAN])) 931 vlan->network_vlan = cur; 932 933 cur = tb[VLAN_ATTR_BRIDGE_ISOLATE]; 934 vlan->bridge_isolate = cur && blobmsg_get_bool(cur); 935 936 cur = tb[VLAN_ATTR_ISOLATE]; 937 vlan->isolate = cur && blobmsg_get_bool(cur); 938 939 cur = tb[VLAN_ATTR_MCAST_TO_UCAST]; 940 vlan->multicast_to_unicast = cur ? blobmsg_get_bool(cur) : -1; 941 } 942 943 /* vlist update call for vlan list */ 944 static void 945 vlan_update(struct vlist_tree *tree, struct vlist_node *node_new, 946 struct vlist_node *node_old) 947 { 948 struct wireless_vlan *vlan_old = container_of_safe(node_old, struct wireless_vlan, node); 949 struct wireless_vlan *vlan_new = container_of_safe(node_new, struct wireless_vlan, node); 950 struct wireless_interface *vif = container_of(tree, struct wireless_interface, vlans); 951 struct wireless_device *wdev = vif->wdev; 952 953 if (vlan_old && vlan_new) { 954 free((void *) vlan_old->section); 955 vlan_old->section = strdup(vlan_new->section); 956 if (blob_attr_equal(vlan_old->config, vlan_new->config)) { 957 free(vlan_new); 958 return; 959 } 960 961 D(WIRELESS, "Update wireless vlan %s on device %s", vlan_new->name, wdev->name); 962 wireless_vlan_handle_link(vlan_old, false); 963 free(vlan_old->config); 964 vlan_old->config = blob_memdup(vlan_new->config); 965 vlan_old->isolate = vlan_new->isolate; 966 wireless_vlan_init_config(vlan_old); 967 free(vlan_new); 968 } else if (vlan_new) { 969 D(WIRELESS, "Create new wireless vlan %s on device %s", vlan_new->name, wdev->name); 970 vlan_new->section = strdup(vlan_new->section); 971 vlan_new->config = blob_memdup(vlan_new->config); 972 wireless_vlan_init_config(vlan_new); 973 } else if (vlan_old) { 974 D(WIRELESS, "Delete wireless vlan %s on device %s", vlan_old->name, wdev->name); 975 wireless_vlan_handle_link(vlan_old, false); 976 free((void *) vlan_old->section); 977 free(vlan_old->config); 978 free(vlan_old); 979 } 980 981 wdev->config_update = true; 982 } 983 984 /* vlist update call for station list */ 985 static void 986 station_update(struct vlist_tree *tree, struct vlist_node *node_new, 987 struct vlist_node *node_old) 988 { 989 struct wireless_station *sta_old = container_of_safe(node_old, struct wireless_station, node); 990 struct wireless_station *sta_new = container_of_safe(node_new, struct wireless_station, node); 991 struct wireless_interface *vif = container_of(tree, struct wireless_interface, stations); 992 struct wireless_device *wdev = vif->wdev; 993 994 if (sta_old && sta_new) { 995 free((void *) sta_old->section); 996 sta_old->section = strdup(sta_new->section); 997 if (blob_attr_equal(sta_old->config, sta_new->config)) { 998 free(sta_new); 999 return; 1000 } 1001 1002 D(WIRELESS, "Update wireless station %s on device %s", sta_new->name, wdev->name); 1003 free(sta_old->config); 1004 sta_old->config = blob_memdup(sta_new->config); 1005 free(sta_new); 1006 } else if (sta_new) { 1007 D(WIRELESS, "Create new wireless station %s on device %s", sta_new->name, wdev->name); 1008 sta_new->section = strdup(sta_new->section); 1009 sta_new->config = blob_memdup(sta_new->config); 1010 } else if (sta_old) { 1011 D(WIRELESS, "Delete wireless station %s on device %s", sta_old->name, wdev->name); 1012 free((void *) sta_old->section); 1013 free(sta_old->config); 1014 free(sta_old); 1015 } 1016 1017 wdev->config_update = true; 1018 } 1019 1020 static void 1021 wireless_proc_poll_fd(struct uloop_fd *fd, unsigned int events) 1022 { 1023 struct wireless_device *wdev = container_of(fd, struct wireless_device, script_proc_fd); 1024 char buf[128]; 1025 1026 while (1) { 1027 int b = read(fd->fd, buf, sizeof(buf)); 1028 if (b < 0) { 1029 if (errno == EINTR) 1030 continue; 1031 1032 if (errno == EAGAIN) 1033 return; 1034 1035 goto done; 1036 } 1037 1038 if (!b) 1039 goto done; 1040 } 1041 1042 done: 1043 uloop_timeout_set(&wdev->script_check, 0); 1044 wireless_close_script_proc_fd(wdev); 1045 } 1046 1047 /* watchdog and garbage collector for wireless processes. 1048 * It cleans up terminated processes. If a process is a requirement for the wireless device, it retries the setup */ 1049 static void 1050 wireless_device_check_script_tasks(struct uloop_timeout *timeout) 1051 { 1052 struct wireless_device *wdev = container_of(timeout, struct wireless_device, script_check); 1053 struct wireless_process *proc, *tmp; 1054 bool restart = false; 1055 1056 list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) { 1057 if (wireless_process_check(proc)) 1058 continue; 1059 1060 D(WIRELESS, "Wireless device '%s' pid %d has terminated", wdev->name, proc->pid); 1061 if (proc->required) 1062 restart = true; 1063 1064 wireless_process_free(wdev, proc); 1065 } 1066 1067 if (restart) 1068 wireless_device_retry_setup(wdev); 1069 else 1070 uloop_timeout_set(&wdev->script_check, 1000); 1071 } 1072 1073 /* creates a wireless device object. Called by config */ 1074 void 1075 wireless_device_create(struct wireless_driver *drv, const char *name, struct blob_attr *data) 1076 { 1077 struct wireless_device *wdev; 1078 char *name_buf; 1079 struct blob_attr *tb[__WDEV_ATTR_MAX]; 1080 struct blob_attr *cur; 1081 1082 blobmsg_parse_attr(wdev_policy, __WDEV_ATTR_MAX, tb, data); 1083 1084 wdev = calloc_a(sizeof(*wdev), &name_buf, strlen(name) + 1); 1085 1086 cur = tb[WDEV_ATTR_DISABLED]; 1087 wdev->disabled = cur && blobmsg_get_bool(cur); 1088 1089 wdev->drv = drv; 1090 wdev->state = IFS_DOWN; 1091 wdev->config_state = IFC_NORMAL; 1092 wdev->name = strcpy(name_buf, name); 1093 wdev->config = data; 1094 wdev->handler_pending = false; 1095 1096 cur = tb[WDEV_ATTR_SERIALIZE]; 1097 wdev->serialize = cur && blobmsg_get_bool(cur); 1098 1099 cur = tb[WDEV_ATTR_RECONF]; 1100 wdev->reconf = !cur || blobmsg_get_bool(cur); 1101 1102 wdev->retry_setup_failed = false; 1103 wdev->autostart = true; 1104 INIT_LIST_HEAD(&wdev->script_proc); 1105 vlist_init(&wdev->interfaces, avl_strcmp, vif_update); 1106 wdev->interfaces.keep_old = true; 1107 1108 wdev->timeout.cb = wireless_device_setup_timeout; 1109 wdev->script_task.cb = wireless_device_script_task_cb; 1110 wdev->script_task.dir_fd = drv_fd; 1111 wdev->script_task.log_prefix = wdev->name; 1112 1113 wdev->script_proc_fd.fd = -1; 1114 wdev->script_proc_fd.cb = wireless_proc_poll_fd; 1115 1116 wdev->script_check.cb = wireless_device_check_script_tasks; 1117 1118 vlist_add(&wireless_devices, &wdev->node, wdev->name); 1119 } 1120 1121 /* creates a wireless station object. Called by config */ 1122 void 1123 wireless_station_create(struct wireless_interface *vif, struct blob_attr *data, const char *section) 1124 { 1125 struct wireless_station *sta; 1126 struct blob_attr *tb[__STA_ATTR_MAX]; 1127 struct blob_attr *cur; 1128 char *name_buf; 1129 char name[8]; 1130 1131 blobmsg_parse_attr(sta_policy, __STA_ATTR_MAX, tb, data); 1132 1133 cur = tb[STA_ATTR_DISABLED]; 1134 if (cur && blobmsg_get_bool(cur)) 1135 return; 1136 1137 sprintf(name, "%d", vif->sta_idx++); 1138 1139 sta = calloc_a(sizeof(*sta), 1140 &name_buf, strlen(name) + 1); 1141 sta->name = strcpy(name_buf, name); 1142 sta->config = data; 1143 sta->section = section; 1144 1145 vlist_add(&vif->stations, &sta->node, sta->name); 1146 } 1147 1148 /* ubus callback network.wireless.status, runs for every interface, encode the station */ 1149 static void 1150 wireless_station_status(struct wireless_station *sta, struct blob_buf *b) 1151 { 1152 void *i; 1153 1154 i = blobmsg_open_table(b, NULL); 1155 if (sta->section) 1156 blobmsg_add_string(b, "section", sta->section); 1157 put_container(b, sta->config, "config"); 1158 blobmsg_close_table(b, i); 1159 } 1160 1161 /* create a vlan object. Called by config */ 1162 void 1163 wireless_vlan_create(struct wireless_interface *vif, struct blob_attr *data, const char *section) 1164 { 1165 struct wireless_vlan *vlan; 1166 struct blob_attr *tb[__VLAN_ATTR_MAX]; 1167 struct blob_attr *cur; 1168 char *name_buf; 1169 char name[8]; 1170 1171 blobmsg_parse_attr(vlan_policy, __VLAN_ATTR_MAX, tb, data); 1172 1173 cur = tb[VLAN_ATTR_DISABLED]; 1174 if (cur && blobmsg_get_bool(cur)) 1175 return; 1176 1177 sprintf(name, "%d", vif->vlan_idx++); 1178 1179 vlan = calloc_a(sizeof(*vlan), &name_buf, strlen(name) + 1); 1180 vlan->name = strcpy(name_buf, name); 1181 vlan->config = data; 1182 vlan->section = section; 1183 1184 vlist_add(&vif->vlans, &vlan->node, vlan->name); 1185 } 1186 1187 /* ubus callback network.wireless.status, runs for every interface, encode the vlan informations */ 1188 static void 1189 wireless_vlan_status(struct wireless_vlan *vlan, struct blob_buf *b) 1190 { 1191 void *i; 1192 1193 i = blobmsg_open_table(b, NULL); 1194 if (vlan->section) 1195 blobmsg_add_string(b, "section", vlan->section); 1196 if (vlan->ifname) 1197 blobmsg_add_string(b, "ifname", vlan->ifname); 1198 put_container(b, vlan->config, "config"); 1199 blobmsg_close_table(b, i); 1200 } 1201 1202 /* create a wireless interface object. Called by config */ 1203 struct wireless_interface* wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data, const char *section) 1204 { 1205 struct wireless_interface *vif; 1206 struct blob_attr *tb[__VIF_ATTR_MAX]; 1207 struct blob_attr *cur; 1208 char *name_buf; 1209 char name[8]; 1210 1211 blobmsg_parse_attr(vif_policy, __VIF_ATTR_MAX, tb, data); 1212 1213 cur = tb[VIF_ATTR_DISABLED]; 1214 if (cur && blobmsg_get_bool(cur)) 1215 return NULL; 1216 1217 sprintf(name, "%d", wdev->vif_idx++); 1218 1219 vif = calloc_a(sizeof(*vif), 1220 &name_buf, strlen(name) + 1); 1221 vif->name = strcpy(name_buf, name); 1222 vif->wdev = wdev; 1223 vif->config = data; 1224 vif->section = section; 1225 vif->isolate = false; 1226 1227 vlist_init(&vif->vlans, avl_strcmp, vlan_update); 1228 vif->vlans.keep_old = true; 1229 1230 vlist_init(&vif->stations, avl_strcmp, station_update); 1231 vif->stations.keep_old = true; 1232 1233 vlist_add(&wdev->interfaces, &vif->node, vif->name); 1234 1235 vif = vlist_find(&wdev->interfaces, name, vif, node); 1236 if (!vif) 1237 return NULL; 1238 1239 vif->vlan_idx = vif->sta_idx = 0; 1240 vlist_update(&vif->vlans); 1241 vlist_update(&vif->stations); 1242 1243 return vif; 1244 } 1245 1246 /* ubus callback network.wireless.status, runs for every interface */ 1247 static void 1248 wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b) 1249 { 1250 struct wireless_vlan *vlan; 1251 struct wireless_station *sta; 1252 void *i, *j; 1253 1254 i = blobmsg_open_table(b, NULL); 1255 if (iface->section) 1256 blobmsg_add_string(b, "section", iface->section); 1257 if (iface->ifname) 1258 blobmsg_add_string(b, "ifname", iface->ifname); 1259 put_container(b, iface->config, "config"); 1260 j = blobmsg_open_array(b, "vlans"); 1261 vlist_for_each_element(&iface->vlans, vlan, node) 1262 wireless_vlan_status(vlan, b); 1263 blobmsg_close_array(b, j); 1264 j = blobmsg_open_array(b, "stations"); 1265 vlist_for_each_element(&iface->stations, sta, node) 1266 wireless_station_status(sta, b); 1267 blobmsg_close_array(b, j); 1268 blobmsg_close_table(b, i); 1269 } 1270 1271 /* ubus callback network.wireless.status */ 1272 void 1273 wireless_device_status(struct wireless_device *wdev, struct blob_buf *b) 1274 { 1275 struct wireless_interface *iface; 1276 void *c, *i; 1277 1278 c = blobmsg_open_table(b, wdev->name); 1279 blobmsg_add_u8(b, "up", wdev->state == IFS_UP); 1280 blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN); 1281 blobmsg_add_u8(b, "autostart", wdev->autostart); 1282 blobmsg_add_u8(b, "disabled", wdev->disabled); 1283 blobmsg_add_u8(b, "retry_setup_failed", wdev->retry_setup_failed); 1284 put_container(b, wdev->config, "config"); 1285 1286 i = blobmsg_open_array(b, "interfaces"); 1287 vlist_for_each_element(&wdev->interfaces, iface, node) 1288 wireless_interface_status(iface, b); 1289 blobmsg_close_array(b, i); 1290 blobmsg_close_table(b, c); 1291 } 1292 1293 /* ubus callback network.wireless.get_validate */ 1294 void 1295 wireless_device_get_validate(struct wireless_device *wdev, struct blob_buf *b) 1296 { 1297 struct uci_blob_param_list *p; 1298 void *c, *d; 1299 int i; 1300 1301 c = blobmsg_open_table(b, wdev->name); 1302 1303 d = blobmsg_open_table(b, "device"); 1304 p = wdev->drv->device.config; 1305 for (i = 0; i < p->n_params; i++) 1306 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i)); 1307 blobmsg_close_table(b, d); 1308 1309 d = blobmsg_open_table(b, "interface"); 1310 p = wdev->drv->interface.config; 1311 for (i = 0; i < p->n_params; i++) 1312 blobmsg_add_string(b, p->params[i].name, uci_get_validate_string(p, i)); 1313 blobmsg_close_table(b, d); 1314 1315 blobmsg_close_table(b, c); 1316 } 1317 1318 /* ubus callback network.wireless.notify, command = set data, for vif */ 1319 static void 1320 wireless_interface_set_data(struct wireless_interface *vif) 1321 { 1322 enum { 1323 VIF_DATA_IFNAME, 1324 __VIF_DATA_MAX, 1325 }; 1326 static const struct blobmsg_policy data_policy[__VIF_DATA_MAX] = { 1327 [VIF_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING }, 1328 }; 1329 struct blob_attr *tb[__VIF_DATA_MAX]; 1330 struct blob_attr *cur; 1331 1332 blobmsg_parse_attr(data_policy, __VIF_DATA_MAX, tb, vif->data); 1333 1334 if ((cur = tb[VIF_DATA_IFNAME])) 1335 vif->ifname = blobmsg_data(cur); 1336 } 1337 1338 /* ubus callback network.wireless.notify, command = set data, for vlan */ 1339 static void 1340 wireless_vlan_set_data(struct wireless_vlan *vlan) 1341 { 1342 enum { 1343 VLAN_DATA_IFNAME, 1344 __VLAN_DATA_MAX, 1345 }; 1346 static const struct blobmsg_policy data_policy[__VLAN_DATA_MAX] = { 1347 [VLAN_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING }, 1348 }; 1349 struct blob_attr *tb[__VLAN_DATA_MAX]; 1350 struct blob_attr *cur; 1351 1352 blobmsg_parse_attr(data_policy, __VLAN_DATA_MAX, tb, vlan->data); 1353 1354 if ((cur = tb[VLAN_DATA_IFNAME])) 1355 vlan->ifname = blobmsg_data(cur); 1356 } 1357 1358 /* ubus callback network.wireless.notify, command = process add */ 1359 static int 1360 wireless_device_add_process(struct wireless_device *wdev, struct blob_attr *data) 1361 { 1362 enum { 1363 PROC_ATTR_PID, 1364 PROC_ATTR_EXE, 1365 PROC_ATTR_REQUIRED, 1366 PROC_ATTR_KEEP, 1367 __PROC_ATTR_MAX 1368 }; 1369 static const struct blobmsg_policy proc_policy[__PROC_ATTR_MAX] = { 1370 [PROC_ATTR_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 }, 1371 [PROC_ATTR_EXE] = { .name = "exe", .type = BLOBMSG_TYPE_STRING }, 1372 [PROC_ATTR_REQUIRED] = { .name = "required", .type = BLOBMSG_TYPE_BOOL }, 1373 [PROC_ATTR_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL }, 1374 }; 1375 struct blob_attr *tb[__PROC_ATTR_MAX]; 1376 struct wireless_process *proc; 1377 char *name; 1378 int pid; 1379 1380 if (!data) 1381 return UBUS_STATUS_INVALID_ARGUMENT; 1382 1383 blobmsg_parse_attr(proc_policy, __PROC_ATTR_MAX, tb, data); 1384 if (!tb[PROC_ATTR_PID] || !tb[PROC_ATTR_EXE]) 1385 return UBUS_STATUS_INVALID_ARGUMENT; 1386 1387 pid = blobmsg_get_u32(tb[PROC_ATTR_PID]); 1388 if (pid < 2) 1389 return UBUS_STATUS_INVALID_ARGUMENT; 1390 1391 proc = calloc_a(sizeof(*proc), 1392 &name, strlen(blobmsg_data(tb[PROC_ATTR_EXE])) + 1); 1393 1394 proc->pid = pid; 1395 proc->exe = strcpy(name, blobmsg_data(tb[PROC_ATTR_EXE])); 1396 1397 if (tb[PROC_ATTR_REQUIRED]) 1398 proc->required = blobmsg_get_bool(tb[PROC_ATTR_REQUIRED]); 1399 1400 if (tb[PROC_ATTR_KEEP]) 1401 proc->keep = blobmsg_get_bool(tb[PROC_ATTR_KEEP]); 1402 1403 D(WIRELESS, "Wireless device '%s' add pid %d", wdev->name, proc->pid); 1404 list_add(&proc->list, &wdev->script_proc); 1405 uloop_timeout_set(&wdev->script_check, 0); 1406 1407 return 0; 1408 } 1409 1410 /* ubus callback network.wireless.notify, command = process kill all */ 1411 static int 1412 wireless_device_process_kill_all(struct wireless_device *wdev, struct blob_attr *data, 1413 struct ubus_request_data *req) 1414 { 1415 enum { 1416 KILL_ATTR_SIGNAL, 1417 KILL_ATTR_IMMEDIATE, 1418 __KILL_ATTR_MAX 1419 }; 1420 static const struct blobmsg_policy kill_policy[__KILL_ATTR_MAX] = { 1421 [KILL_ATTR_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 }, 1422 [KILL_ATTR_IMMEDIATE] = { .name = "immediate", .type = BLOBMSG_TYPE_BOOL }, 1423 }; 1424 struct blob_attr *tb[__KILL_ATTR_MAX]; 1425 struct blob_attr *cur; 1426 bool immediate = false; 1427 int signal = SIGTERM; 1428 1429 blobmsg_parse_attr(kill_policy, __KILL_ATTR_MAX, tb, data); 1430 1431 if ((cur = tb[KILL_ATTR_SIGNAL])) 1432 signal = blobmsg_get_u32(cur); 1433 1434 if ((cur = tb[KILL_ATTR_IMMEDIATE])) 1435 immediate = blobmsg_get_bool(cur); 1436 1437 if (wdev->state != IFS_TEARDOWN || wdev->kill_request) 1438 return UBUS_STATUS_PERMISSION_DENIED; 1439 1440 wireless_process_kill_all(wdev, signal, immediate); 1441 1442 if (list_empty(&wdev->script_proc)) 1443 return 0; 1444 1445 wdev->kill_request = calloc(1, sizeof(*wdev->kill_request)); 1446 ubus_defer_request(ubus_ctx, req, wdev->kill_request); 1447 1448 return 0; 1449 } 1450 1451 /* ubus callback network.wireless.notify, command = set_retry */ 1452 static int 1453 wireless_device_set_retry(struct wireless_device *wdev, struct blob_attr *data) 1454 { 1455 static const struct blobmsg_policy retry_policy = { 1456 .name = "retry", .type = BLOBMSG_TYPE_INT32 1457 }; 1458 struct blob_attr *val; 1459 1460 blobmsg_parse_attr(&retry_policy, 1, &val, data); 1461 if (val) 1462 wdev->retry = blobmsg_get_u32(val); 1463 else 1464 wdev->retry = WIRELESS_SETUP_RETRY; 1465 __wireless_device_set_up(wdev, 0); 1466 netifd_log_message(L_NOTICE, "Wireless device '%s' set retry=%d\n", wdev->name, wdev->retry); 1467 return 0; 1468 } 1469 1470 enum { 1471 NOTIFY_CMD_UP = 0, 1472 NOTIFY_CMD_SET_DATA = 1, 1473 NOTIFY_CMD_PROCESS_ADD = 2, 1474 NOTIFY_CMD_PROCESS_KILL_ALL = 3, 1475 NOTIFY_CMD_SET_RETRY = 4, 1476 }; 1477 1478 /* ubus callback network.wireless.notify */ 1479 int 1480 wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data, 1481 struct ubus_request_data *req) 1482 { 1483 enum { 1484 NOTIFY_ATTR_COMMAND, 1485 NOTIFY_ATTR_VIF, 1486 NOTIFY_ATTR_VLAN, 1487 NOTIFY_ATTR_DATA, 1488 __NOTIFY_MAX, 1489 }; 1490 static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = { 1491 [NOTIFY_ATTR_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_INT32 }, 1492 [NOTIFY_ATTR_VIF] = { .name = "interface", .type = BLOBMSG_TYPE_STRING }, 1493 [NOTIFY_ATTR_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_STRING }, 1494 [NOTIFY_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE }, 1495 }; 1496 struct wireless_interface *vif = NULL; 1497 struct wireless_vlan *vlan = NULL; 1498 struct blob_attr *tb[__NOTIFY_MAX]; 1499 struct blob_attr *cur, **pdata; 1500 1501 blobmsg_parse_attr(notify_policy, __NOTIFY_MAX, tb, data); 1502 1503 if (!tb[NOTIFY_ATTR_COMMAND]) 1504 return UBUS_STATUS_INVALID_ARGUMENT; 1505 1506 if ((cur = tb[NOTIFY_ATTR_VIF]) != NULL) { 1507 vif = vlist_find(&wdev->interfaces, blobmsg_data(cur), vif, node); 1508 if (!vif) 1509 return UBUS_STATUS_NOT_FOUND; 1510 } 1511 1512 if ((cur = tb[NOTIFY_ATTR_VLAN]) != NULL) { 1513 if (!vif) 1514 return UBUS_STATUS_NOT_FOUND; 1515 vlan = vlist_find(&vif->vlans, blobmsg_data(cur), vlan, node); 1516 if (!vlan) 1517 return UBUS_STATUS_NOT_FOUND; 1518 } 1519 1520 cur = tb[NOTIFY_ATTR_DATA]; 1521 if (!cur) 1522 return UBUS_STATUS_INVALID_ARGUMENT; 1523 1524 switch (blobmsg_get_u32(tb[NOTIFY_ATTR_COMMAND])) { 1525 case NOTIFY_CMD_UP: 1526 if (vif || vlan) 1527 return UBUS_STATUS_INVALID_ARGUMENT; 1528 1529 if (wdev->state != IFS_SETUP) 1530 return UBUS_STATUS_PERMISSION_DENIED; 1531 1532 wireless_device_mark_up(wdev); 1533 break; 1534 case NOTIFY_CMD_SET_DATA: 1535 if (vlan) 1536 pdata = &vlan->data; 1537 else if (vif) 1538 pdata = &vif->data; 1539 else 1540 pdata = &wdev->data; 1541 1542 free(*pdata); 1543 *pdata = blob_memdup(cur); 1544 if (vlan) 1545 wireless_vlan_set_data(vlan); 1546 else if (vif) 1547 wireless_interface_set_data(vif); 1548 wdev_prepare_prev_config(wdev); 1549 break; 1550 case NOTIFY_CMD_PROCESS_ADD: 1551 return wireless_device_add_process(wdev, cur); 1552 case NOTIFY_CMD_PROCESS_KILL_ALL: 1553 return wireless_device_process_kill_all(wdev, cur, req); 1554 case NOTIFY_CMD_SET_RETRY: 1555 return wireless_device_set_retry(wdev, cur); 1556 default: 1557 return UBUS_STATUS_INVALID_ARGUMENT; 1558 } 1559 1560 return 0; 1561 } 1562 1563 static void 1564 wdev_check_network_enabled(struct wireless_device *wdev) 1565 { 1566 struct wireless_interface *vif; 1567 struct interface *iface; 1568 struct blob_attr *cur; 1569 size_t rem; 1570 1571 vlist_for_each_element(&wdev->interfaces, vif, node) { 1572 int enabled = -1; 1573 1574 blobmsg_for_each_attr(cur, vif->network, rem) { 1575 iface = vlist_find(&interfaces, blobmsg_get_string(cur), iface, node); 1576 if (!iface) 1577 continue; 1578 1579 if (iface->autostart) { 1580 enabled = 1; 1581 break; 1582 } 1583 if (enabled != 1) 1584 enabled = 0; 1585 } 1586 1587 if (vif->disabled == !enabled) 1588 continue; 1589 1590 vif->disabled = !enabled; 1591 wdev->config_update = true; 1592 } 1593 } 1594 1595 static void 1596 __wireless_start_pending(struct uloop_timeout *t) 1597 { 1598 struct wireless_device *wdev; 1599 1600 vlist_for_each_element(&wireless_devices, wdev, node) { 1601 wdev_check_network_enabled(wdev); 1602 if (wdev->config_update) 1603 wdev_set_config_state(wdev, IFC_RELOAD); 1604 __wireless_device_set_up(wdev, 0); 1605 } 1606 } 1607 1608 void wireless_start_pending(int timeout) 1609 { 1610 static struct uloop_timeout timer = { 1611 .cb = __wireless_start_pending 1612 }; 1613 1614 if (timeout) { 1615 uloop_timeout_set(&timer, timeout); 1616 return; 1617 } 1618 1619 uloop_timeout_cancel(&timer); 1620 timer.cb(&timer); 1621 } 1622 1623 void wireless_check_network_enabled(void) 1624 { 1625 struct wireless_device *wdev; 1626 1627 vlist_for_each_element(&wireless_devices, wdev, node) { 1628 wdev_check_network_enabled(wdev); 1629 1630 if (wdev->config_update) 1631 wireless_start_pending(1000); 1632 } 1633 } 1634 1635 void wireless_device_hotplug_event(const char *name, bool add) 1636 { 1637 struct wireless_interface *vif; 1638 struct wireless_device *wdev; 1639 const char *s; 1640 size_t len; 1641 1642 s = strstr(name, ".sta"); 1643 if (s) { 1644 if (strchr(s + 4, '.')) 1645 return; 1646 1647 len = s - name; 1648 } else { 1649 len = strlen(name); 1650 } 1651 1652 vlist_for_each_element(&wireless_devices, wdev, node) { 1653 vlist_for_each_element(&wdev->interfaces, vif, node) { 1654 if (!vif->ifname) 1655 continue; 1656 1657 if (strlen(vif->ifname) != len || 1658 strncmp(vif->ifname, name, len) != 0) 1659 continue; 1660 1661 wireless_interface_handle_link(vif, name, add); 1662 } 1663 } 1664 } 1665
This page was automatically generated by LXR 0.3.1. • OpenWrt