1 /* 2 * netifd - network interface daemon 3 * Copyright (C) 2012 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 #define _GNU_SOURCE 15 #include <string.h> 16 #include <stdlib.h> 17 #include <stdio.h> 18 19 #include <uci.h> 20 21 #include <libubox/blobmsg_json.h> 22 23 #include "netifd.h" 24 #include "interface.h" 25 #include "interface-ip.h" 26 #include "iprule.h" 27 #include "proto.h" 28 #include "wireless.h" 29 #include "config.h" 30 31 bool config_init = false; 32 33 static struct uci_context *uci_ctx; 34 static struct uci_package *uci_network; 35 static struct uci_package *uci_wireless; 36 static struct blob_attr *board_netdevs; 37 static struct blob_buf b; 38 39 static int 40 config_section_idx(struct uci_section *s) 41 { 42 struct uci_element *e; 43 int idx = 0; 44 45 uci_foreach_element(&uci_wireless->sections, e) { 46 struct uci_section *cur = uci_to_section(e); 47 48 if (s == cur) 49 return idx; 50 51 if (!strcmp(cur->type, s->type)) 52 idx++; 53 } 54 55 return -1; 56 } 57 58 static bool 59 config_bridge_has_vlans(const char *br_name) 60 { 61 struct uci_element *e; 62 63 uci_foreach_element(&uci_network->sections, e) { 64 struct uci_section *s = uci_to_section(e); 65 const char *name; 66 67 if (strcmp(s->type, "bridge-vlan") != 0) 68 continue; 69 70 name = uci_lookup_option_string(uci_ctx, s, "device"); 71 if (!name) 72 continue; 73 74 if (!strcmp(name, br_name)) 75 return true; 76 } 77 78 return false; 79 } 80 81 static void 82 config_fixup_bridge_var(struct uci_section *s, const char *name, const char *val) 83 { 84 struct uci_ptr ptr = { 85 .p = s->package, 86 .s = s, 87 .option = name, 88 .value = val, 89 }; 90 91 uci_lookup_ptr(uci_ctx, &ptr, NULL, false); 92 if (ptr.o) 93 return; 94 95 uci_set(uci_ctx, &ptr); 96 } 97 98 /** 99 * config_fixup_bridge_ports - translate deprecated configs 100 * 101 * Old configs used "ifname" option for specifying bridge ports. For backward 102 * compatibility translate it into the new "ports" option. 103 */ 104 static void config_fixup_bridge_ports(struct uci_section *s) 105 { 106 struct uci_ptr ptr = { 107 .p = s->package, 108 .s = s, 109 .option = "ifname", 110 }; 111 112 if (uci_lookup_option(uci_ctx, s, "ports")) 113 return; 114 115 uci_lookup_ptr(uci_ctx, &ptr, NULL, false); 116 if (!ptr.o) 117 return; 118 119 ptr.value = "ports"; 120 uci_rename(uci_ctx, &ptr); 121 } 122 123 static void 124 config_fixup_bridge_vlan_filtering(struct uci_section *s, const char *name) 125 { 126 bool has_vlans = config_bridge_has_vlans(name); 127 128 config_fixup_bridge_var(s, "__has_vlans", has_vlans ? "1" : ""); 129 130 if (!has_vlans) 131 return; 132 133 config_fixup_bridge_var(s, "vlan_filtering", "1"); 134 } 135 136 static int 137 config_parse_bridge_interface(struct uci_section *s, struct device_type *devtype) 138 { 139 char *name; 140 141 name = alloca(strlen(s->e.name) + strlen(devtype->name_prefix) + 2); 142 sprintf(name, "%s-%s", devtype->name_prefix, s->e.name); 143 blobmsg_add_string(&b, "name", name); 144 145 config_fixup_bridge_ports(s); 146 config_fixup_bridge_vlan_filtering(s, name); 147 uci_to_blob(&b, s, devtype->config_params); 148 if (!device_create(name, devtype, b.head)) { 149 D(INTERFACE, "Failed to create '%s' device for interface '%s'\n", 150 devtype->name, s->e.name); 151 } 152 153 blob_buf_init(&b, 0); 154 blobmsg_add_string(&b, "ifname", name); 155 return 0; 156 } 157 158 static void 159 config_parse_interface(struct uci_section *s, bool alias) 160 { 161 struct interface *iface; 162 const char *type = NULL, *disabled; 163 struct blob_attr *config; 164 bool bridge = false; 165 struct device_type *devtype = NULL; 166 167 disabled = uci_lookup_option_string(uci_ctx, s, "disabled"); 168 if (disabled && !strcmp(disabled, "1")) 169 return; 170 171 blob_buf_init(&b, 0); 172 173 if (!alias) 174 type = uci_lookup_option_string(uci_ctx, s, "type"); 175 176 if (type) 177 devtype = device_type_get(type); 178 179 if (devtype && devtype->bridge_capability) { 180 if (config_parse_bridge_interface(s, devtype)) 181 return; 182 183 bridge = true; 184 } 185 186 uci_to_blob(&b, s, &interface_attr_list); 187 188 iface = interface_alloc(s->e.name, b.head, false); 189 if (!iface) 190 return; 191 192 if (iface->proto_handler && iface->proto_handler->config_params) 193 uci_to_blob(&b, s, iface->proto_handler->config_params); 194 195 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params)) 196 iface->device_config = true; 197 198 config = blob_memdup(b.head); 199 if (!config) 200 goto error; 201 202 if (alias) { 203 if (!interface_add_alias(iface, config)) 204 goto error_free_config; 205 } else { 206 if (!interface_add(iface, config)) 207 goto error_free_config; 208 } 209 return; 210 211 error_free_config: 212 free(config); 213 error: 214 free(iface); 215 } 216 217 static void 218 config_parse_route(struct uci_section *s, bool v6) 219 { 220 void *route; 221 222 blob_buf_init(&b, 0); 223 route = blobmsg_open_array(&b, "route"); 224 uci_to_blob(&b, s, &route_attr_list); 225 blobmsg_close_array(&b, route); 226 interface_ip_add_route(NULL, blob_data(b.head), v6); 227 } 228 229 static void 230 config_parse_neighbor(struct uci_section *s, bool v6) 231 { 232 void *neighbor; 233 blob_buf_init(&b,0); 234 neighbor = blobmsg_open_array(&b, "neighbor"); 235 uci_to_blob(&b,s, &neighbor_attr_list); 236 blobmsg_close_array(&b, neighbor); 237 interface_ip_add_neighbor(NULL, blob_data(b.head), v6); 238 } 239 240 static void 241 config_parse_rule(struct uci_section *s, bool v6) 242 { 243 void *rule; 244 245 blob_buf_init(&b, 0); 246 rule = blobmsg_open_array(&b, "rule"); 247 uci_to_blob(&b, s, &rule_attr_list); 248 blobmsg_close_array(&b, rule); 249 iprule_add(blob_data(b.head), v6); 250 } 251 252 static void 253 config_init_devices(bool bridge) 254 { 255 struct uci_element *e; 256 257 uci_foreach_element(&uci_network->sections, e) { 258 const struct uci_blob_param_list *params = NULL; 259 struct uci_section *s = uci_to_section(e); 260 struct device_type *devtype = NULL; 261 struct device *dev; 262 const char *type, *name; 263 264 if (strcmp(s->type, "device") != 0) 265 continue; 266 267 name = uci_lookup_option_string(uci_ctx, s, "name"); 268 if (!name) 269 continue; 270 271 type = uci_lookup_option_string(uci_ctx, s, "type"); 272 if (type) 273 devtype = device_type_get(type); 274 275 if (bridge != (devtype && devtype->bridge_capability)) 276 continue; 277 278 if (devtype) 279 params = devtype->config_params; 280 if (!params) 281 params = simple_device_type.config_params; 282 283 if (devtype && devtype->bridge_capability) { 284 config_fixup_bridge_ports(s); 285 config_fixup_bridge_vlan_filtering(s, name); 286 } 287 288 blob_buf_init(&b, 0); 289 uci_to_blob(&b, s, params); 290 if (devtype) { 291 dev = device_create(name, devtype, b.head); 292 if (!dev) 293 continue; 294 } else { 295 dev = device_get(name, 1); 296 if (!dev) 297 continue; 298 299 dev->current_config = true; 300 device_apply_config(dev, dev->type, b.head); 301 } 302 dev->default_config = false; 303 } 304 } 305 306 static void 307 config_parse_vlan(struct device *dev, struct uci_section *s) 308 { 309 enum { 310 BRVLAN_ATTR_VID, 311 BRVLAN_ATTR_LOCAL, 312 BRVLAN_ATTR_PORTS, 313 BRVLAN_ATTR_ALIAS, 314 __BRVLAN_ATTR_MAX, 315 }; 316 static const struct blobmsg_policy vlan_attrs[__BRVLAN_ATTR_MAX] = { 317 [BRVLAN_ATTR_VID] = { "vlan", BLOBMSG_TYPE_INT32 }, 318 [BRVLAN_ATTR_LOCAL] = { "local", BLOBMSG_TYPE_BOOL }, 319 [BRVLAN_ATTR_PORTS] = { "ports", BLOBMSG_TYPE_ARRAY }, 320 [BRVLAN_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY }, 321 }; 322 static const struct uci_blob_param_info vlan_attr_info[__BRVLAN_ATTR_MAX] = { 323 [BRVLAN_ATTR_PORTS] = { .type = BLOBMSG_TYPE_STRING }, 324 [BRVLAN_ATTR_ALIAS] = { .type = BLOBMSG_TYPE_STRING }, 325 }; 326 static const struct uci_blob_param_list vlan_attr_list = { 327 .n_params = __BRVLAN_ATTR_MAX, 328 .params = vlan_attrs, 329 .info = vlan_attr_info, 330 }; 331 struct blob_attr *tb[__BRVLAN_ATTR_MAX]; 332 struct blob_attr *cur; 333 struct bridge_vlan_port *port; 334 struct bridge_vlan *vlan; 335 unsigned int vid; 336 const char *val; 337 char *name_buf; 338 int name_len = 0; 339 int n_ports = 0; 340 size_t rem; 341 342 val = uci_lookup_option_string(uci_ctx, s, "vlan"); 343 if (!val) 344 return; 345 346 blob_buf_init(&b, 0); 347 uci_to_blob(&b, s, &vlan_attr_list); 348 blobmsg_parse(vlan_attrs, __BRVLAN_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head)); 349 350 if (!tb[BRVLAN_ATTR_VID]) 351 return; 352 353 vid = blobmsg_get_u32(tb[BRVLAN_ATTR_VID]); 354 if (!vid || vid > 4095) 355 return; 356 357 blobmsg_for_each_attr(cur, tb[BRVLAN_ATTR_PORTS], rem) { 358 name_len += strlen(blobmsg_get_string(cur)) + 1; 359 n_ports++; 360 } 361 362 vlan = calloc(1, sizeof(*vlan) + n_ports * sizeof(*port) + name_len); 363 if (!vlan) 364 return; 365 366 vlan->vid = vid; 367 vlan->local = true; 368 if (tb[BRVLAN_ATTR_LOCAL]) 369 vlan->local = blobmsg_get_bool(tb[BRVLAN_ATTR_LOCAL]); 370 371 vlan->n_ports = n_ports; 372 vlan->ports = port = (struct bridge_vlan_port *)&vlan[1]; 373 INIT_LIST_HEAD(&vlan->hotplug_ports); 374 name_buf = (char *)&port[n_ports]; 375 376 blobmsg_for_each_attr(cur, tb[BRVLAN_ATTR_PORTS], rem) { 377 char *sep; 378 379 port->ifname = name_buf; 380 port->flags = BRVLAN_F_UNTAGGED; 381 strcpy(name_buf, blobmsg_get_string(cur)); 382 383 sep = strchr(name_buf, ':'); 384 if (sep) { 385 for (*sep = 0, sep++; *sep; sep++) 386 switch (*sep) { 387 case '*': 388 port->flags |= BRVLAN_F_PVID; 389 break; 390 case 't': 391 port->flags &= ~BRVLAN_F_UNTAGGED; 392 break; 393 } 394 } 395 396 name_buf += strlen(name_buf) + 1; 397 port++; 398 } 399 400 blobmsg_for_each_attr(cur, tb[BRVLAN_ATTR_ALIAS], rem) 401 kvlist_set(&dev->vlan_aliases, blobmsg_get_string(cur), &vid); 402 403 vlist_add(&dev->vlans, &vlan->node, &vlan->vid); 404 } 405 406 407 static void 408 config_init_vlans(void) 409 { 410 struct uci_element *e; 411 struct device *dev; 412 413 device_vlan_update(false); 414 uci_foreach_element(&uci_network->sections, e) { 415 struct uci_section *s = uci_to_section(e); 416 const char *name; 417 418 if (strcmp(s->type, "bridge-vlan") != 0) 419 continue; 420 421 name = uci_lookup_option_string(uci_ctx, s, "device"); 422 if (!name) 423 continue; 424 425 dev = device_get(name, 0); 426 if (!dev || !dev->vlans.update) 427 continue; 428 429 config_parse_vlan(dev, s); 430 } 431 device_vlan_update(true); 432 } 433 434 static struct uci_package * 435 config_init_package(const char *config) 436 { 437 struct uci_context *ctx = uci_ctx; 438 struct uci_package *p = NULL; 439 440 if (!ctx) { 441 ctx = uci_alloc_context(); 442 uci_ctx = ctx; 443 444 ctx->flags &= ~UCI_FLAG_STRICT; 445 if (config_path) 446 uci_set_confdir(ctx, config_path); 447 448 #ifdef DUMMY_MODE 449 uci_set_savedir(ctx, "./tmp"); 450 #endif 451 } else { 452 p = uci_lookup_package(ctx, config); 453 if (p) 454 uci_unload(ctx, p); 455 } 456 457 if (uci_load(ctx, config, &p)) 458 return NULL; 459 460 return p; 461 } 462 463 static void 464 config_init_interfaces(void) 465 { 466 struct uci_element *e; 467 468 uci_foreach_element(&uci_network->sections, e) { 469 struct uci_section *s = uci_to_section(e); 470 471 if (!strcmp(s->type, "interface")) 472 config_parse_interface(s, false); 473 } 474 475 uci_foreach_element(&uci_network->sections, e) { 476 struct uci_section *s = uci_to_section(e); 477 478 if (!strcmp(s->type, "alias")) 479 config_parse_interface(s, true); 480 } 481 } 482 483 static void 484 config_init_ip(void) 485 { 486 struct interface *iface; 487 struct uci_element *e; 488 489 vlist_for_each_element(&interfaces, iface, node) 490 interface_ip_update_start(&iface->config_ip); 491 492 uci_foreach_element(&uci_network->sections, e) { 493 struct uci_section *s = uci_to_section(e); 494 495 if (!strcmp(s->type, "route")) 496 config_parse_route(s, false); 497 else if (!strcmp(s->type, "route6")) 498 config_parse_route(s, true); 499 if (!strcmp(s->type, "neighbor")) 500 config_parse_neighbor(s, false); 501 else if (!strcmp(s->type, "neighbor6")) 502 config_parse_neighbor(s, true); 503 } 504 505 vlist_for_each_element(&interfaces, iface, node) 506 interface_ip_update_complete(&iface->config_ip); 507 } 508 509 static void 510 config_init_rules(void) 511 { 512 struct uci_element *e; 513 514 iprule_update_start(); 515 516 uci_foreach_element(&uci_network->sections, e) { 517 struct uci_section *s = uci_to_section(e); 518 519 if (!strcmp(s->type, "rule")) 520 config_parse_rule(s, false); 521 else if (!strcmp(s->type, "rule6")) 522 config_parse_rule(s, true); 523 } 524 525 iprule_update_complete(); 526 } 527 528 static void 529 config_init_globals(void) 530 { 531 struct uci_section *globals = uci_lookup_section( 532 uci_ctx, uci_network, "globals"); 533 if (!globals) 534 return; 535 536 const char *ula_prefix = uci_lookup_option_string( 537 uci_ctx, globals, "ula_prefix"); 538 interface_ip_set_ula_prefix(ula_prefix); 539 } 540 541 static void 542 config_parse_wireless_device(struct uci_section *s) 543 { 544 struct wireless_driver *drv; 545 const char *driver_name; 546 547 driver_name = uci_lookup_option_string(uci_ctx, s, "type"); 548 if (!driver_name) 549 return; 550 551 drv = avl_find_element(&wireless_drivers, driver_name, drv, node); 552 if (!drv) 553 return; 554 555 blob_buf_init(&b, 0); 556 uci_to_blob(&b, s, drv->device.config); 557 wireless_device_create(drv, s->e.name, b.head); 558 } 559 560 static void 561 config_parse_wireless_vlan(struct wireless_interface *vif, struct uci_section *s) 562 { 563 char *name; 564 565 name = alloca(strlen(s->type) + 16); 566 sprintf(name, "@%s[%d]", s->type, config_section_idx(s)); 567 568 blob_buf_init(&b, 0); 569 uci_to_blob(&b, s, vif->wdev->drv->vlan.config); 570 wireless_vlan_create(vif, b.head, s->anonymous ? name : s->e.name); 571 } 572 573 static void 574 config_parse_wireless_station(struct wireless_interface *vif, struct uci_section *s) 575 { 576 char *name; 577 578 name = alloca(strlen(s->type) + 16); 579 sprintf(name, "@%s[%d]", s->type, config_section_idx(s)); 580 581 blob_buf_init(&b, 0); 582 uci_to_blob(&b, s, vif->wdev->drv->station.config); 583 wireless_station_create(vif, b.head, s->anonymous ? name : s->e.name); 584 } 585 586 static void 587 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s) 588 { 589 struct wireless_interface *vif; 590 struct uci_element *f; 591 char *name; 592 593 name = alloca(strlen(s->type) + 16); 594 sprintf(name, "@%s[%d]", s->type, config_section_idx(s)); 595 596 blob_buf_init(&b, 0); 597 uci_to_blob(&b, s, wdev->drv->interface.config); 598 vif = wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name); 599 if (!vif) 600 return; 601 602 vif->vlan_idx = vif->sta_idx = 0; 603 vlist_update(&vif->vlans); 604 vlist_update(&vif->stations); 605 606 if (s->anonymous) 607 goto out; 608 609 uci_foreach_element(&uci_wireless->sections, f) { 610 struct uci_section *cur = uci_to_section(f); 611 const char *vif_name; 612 613 if (strcmp(cur->type, "wifi-vlan") != 0) 614 continue; 615 616 vif_name = uci_lookup_option_string(uci_ctx, cur, "iface"); 617 if (vif_name && strcmp(s->e.name, vif_name)) 618 continue; 619 config_parse_wireless_vlan(vif, cur); 620 } 621 622 uci_foreach_element(&uci_wireless->sections, f) { 623 struct uci_section *cur = uci_to_section(f); 624 const char *vif_name; 625 626 if (strcmp(cur->type, "wifi-station") != 0) 627 continue; 628 629 vif_name = uci_lookup_option_string(uci_ctx, cur, "iface"); 630 if (vif_name && strcmp(s->e.name, vif_name)) 631 continue; 632 config_parse_wireless_station(vif, cur); 633 } 634 635 out: 636 vlist_flush(&vif->vlans); 637 vlist_flush(&vif->stations); 638 } 639 640 static void 641 config_init_wireless(void) 642 { 643 struct wireless_device *wdev; 644 struct uci_element *e; 645 const char *dev_name; 646 647 if (!uci_wireless) { 648 DPRINTF("No wireless configuration found\n"); 649 return; 650 } 651 652 vlist_update(&wireless_devices); 653 654 uci_foreach_element(&uci_wireless->sections, e) { 655 struct uci_section *s = uci_to_section(e); 656 if (strcmp(s->type, "wifi-device") != 0) 657 continue; 658 659 config_parse_wireless_device(s); 660 } 661 662 vlist_flush(&wireless_devices); 663 664 vlist_for_each_element(&wireless_devices, wdev, node) { 665 wdev->vif_idx = 0; 666 vlist_update(&wdev->interfaces); 667 } 668 669 uci_foreach_element(&uci_wireless->sections, e) { 670 struct uci_section *s = uci_to_section(e); 671 672 if (strcmp(s->type, "wifi-iface") != 0) 673 continue; 674 675 dev_name = uci_lookup_option_string(uci_ctx, s, "device"); 676 if (!dev_name) 677 continue; 678 679 wdev = vlist_find(&wireless_devices, dev_name, wdev, node); 680 if (!wdev) { 681 DPRINTF("device %s not found!\n", dev_name); 682 continue; 683 } 684 685 config_parse_wireless_interface(wdev, s); 686 } 687 688 vlist_for_each_element(&wireless_devices, wdev, node) 689 vlist_flush(&wdev->interfaces); 690 } 691 692 693 static struct blob_attr * 694 config_find_blobmsg_attr(struct blob_attr *attr, const char *name, int type) 695 { 696 struct blobmsg_policy policy = { .name = name, .type = type }; 697 struct blob_attr *cur; 698 699 blobmsg_parse(&policy, 1, &cur, blobmsg_data(attr), blobmsg_len(attr)); 700 701 return cur; 702 } 703 704 struct ether_addr *config_get_default_macaddr(const char *ifname) 705 { 706 struct blob_attr *cur; 707 708 if (!board_netdevs) 709 return NULL; 710 711 cur = config_find_blobmsg_attr(board_netdevs, ifname, BLOBMSG_TYPE_TABLE); 712 if (!cur) 713 return NULL; 714 715 cur = config_find_blobmsg_attr(cur, "macaddr", BLOBMSG_TYPE_STRING); 716 if (!cur) 717 return NULL; 718 719 return ether_aton(blobmsg_get_string(cur)); 720 } 721 722 static void 723 config_init_board(void) 724 { 725 struct blob_attr *cur; 726 727 blob_buf_init(&b, 0); 728 729 if (!blobmsg_add_json_from_file(&b, DEFAULT_BOARD_JSON)) 730 return; 731 732 free(board_netdevs); 733 board_netdevs = NULL; 734 735 cur = config_find_blobmsg_attr(b.head, "network-device", 736 BLOBMSG_TYPE_TABLE); 737 if (!cur) 738 return; 739 740 board_netdevs = blob_memdup(cur); 741 } 742 743 int 744 config_init_all(void) 745 { 746 int ret = 0; 747 char *err; 748 749 uci_network = config_init_package("network"); 750 if (!uci_network) { 751 uci_get_errorstr(uci_ctx, &err, NULL); 752 netifd_log_message(L_CRIT, "Failed to load network config (%s)\n", err); 753 free(err); 754 return -1; 755 } 756 757 uci_wireless = config_init_package("wireless"); 758 if (!uci_wireless && uci_ctx->err != UCI_ERR_NOTFOUND) { 759 uci_get_errorstr(uci_ctx, &err, NULL); 760 netifd_log_message(L_CRIT, "Failed to load wireless config (%s)\n", err); 761 free(err); 762 ret = -1; 763 } 764 765 config_init_board(); 766 767 vlist_update(&interfaces); 768 config_init = true; 769 770 device_reset_config(); 771 config_init_devices(true); 772 config_init_vlans(); 773 config_init_devices(false); 774 config_init_interfaces(); 775 config_init_ip(); 776 config_init_rules(); 777 config_init_globals(); 778 config_init_wireless(); 779 780 config_init = false; 781 782 device_reset_old(); 783 device_init_pending(); 784 vlist_flush(&interfaces); 785 interface_refresh_assignments(false); 786 interface_start_pending(); 787 wireless_start_pending(0); 788 789 return ret; 790 } 791
This page was automatically generated by LXR 0.3.1. • OpenWrt