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 "config.h" 29 #include "ubus.h" 30 #include "system.h" 31 #include "ucode.h" 32 33 bool config_init = false; 34 35 static struct uci_context *uci_ctx; 36 static struct uci_package *uci_network; 37 static struct blob_attr *board_netdevs; 38 static struct blob_buf b; 39 static LIST_HEAD(config_vlans); 40 static LIST_HEAD(config_ifaces); 41 static KVLIST(config_procd_devices, kvlist_blob_len); 42 43 struct vlan_config_entry { 44 struct list_head list; 45 struct blob_attr *data; 46 char *dev_name; 47 char name[]; 48 }; 49 50 static bool 51 config_bridge_has_vlans(const char *br_name) 52 { 53 struct vlan_config_entry *e; 54 55 list_for_each_entry(e, &config_vlans, list) 56 if (!strcmp(e->dev_name, br_name)) 57 return true; 58 59 return false; 60 } 61 62 static bool 63 config_vlans_has_name(struct blob_attr *vlans, const char *name) 64 { 65 struct blob_attr *cur; 66 size_t rem; 67 68 blobmsg_for_each_attr(cur, vlans, rem) 69 if (!strcmp(blobmsg_name(cur), name)) 70 return true; 71 72 return false; 73 } 74 75 /* 76 * Entries in inline_vlans replace bridge-vlan entries with the same name, and 77 * come last so that they also win on a duplicate vid. 78 */ 79 static void 80 config_bridge_add_vlans(struct blob_buf *buf, struct device_type *devtype, 81 const char *br_name, struct blob_attr *inline_vlans) 82 { 83 struct vlan_config_entry *e; 84 struct blob_attr *cur; 85 size_t rem; 86 void *c; 87 88 if (!devtype->bridge_vlans || !config_bridge_has_vlans(br_name)) 89 return; 90 91 c = blobmsg_open_table(buf, "vlans"); 92 list_for_each_entry(e, &config_vlans, list) { 93 if (strcmp(e->dev_name, br_name) != 0 || 94 config_vlans_has_name(inline_vlans, e->name)) 95 continue; 96 97 blobmsg_add_field(buf, BLOBMSG_TYPE_TABLE, e->name, 98 blobmsg_data(e->data), blobmsg_data_len(e->data)); 99 } 100 101 blobmsg_for_each_attr(cur, inline_vlans, rem) 102 blobmsg_add_blob(buf, cur); 103 blobmsg_close_table(buf, c); 104 } 105 106 /** 107 * config_fixup_bridge_ports - translate deprecated configs 108 * 109 * Old configs used "ifname" option for specifying bridge ports. For backward 110 * compatibility translate it into the new "ports" option. 111 */ 112 static void config_fixup_bridge_ports(struct uci_section *s) 113 { 114 struct uci_ptr ptr = { 115 .p = s->package, 116 .s = s, 117 .option = "ifname", 118 }; 119 120 if (uci_lookup_option(uci_ctx, s, "ports")) 121 return; 122 123 uci_lookup_ptr(uci_ctx, &ptr, NULL, false); 124 if (!ptr.o) 125 return; 126 127 ptr.value = "ports"; 128 uci_rename(uci_ctx, &ptr); 129 } 130 131 static struct device * 132 config_device_add(const char *name, struct device_type *devtype, 133 struct blob_attr *config) 134 { 135 struct device *dev; 136 137 if (devtype) { 138 dev = device_create(name, devtype, config); 139 if (!dev) 140 return NULL; 141 142 dev->from_config = true; 143 } else { 144 dev = device_get(name, 1); 145 if (!dev) 146 return NULL; 147 148 dev->current_config = true; 149 device_apply_config(dev, dev->type, config); 150 } 151 152 dev->default_config = false; 153 device_set_bridge_config(dev, config); 154 155 return dev; 156 } 157 158 static int 159 config_parse_bridge_interface(struct uci_section *s, struct device_type *devtype) 160 { 161 char *name; 162 163 name = alloca(strlen(s->e.name) + strlen(devtype->name_prefix) + 2); 164 sprintf(name, "%s-%s", devtype->name_prefix, s->e.name); 165 blobmsg_add_string(&b, "name", name); 166 167 config_fixup_bridge_ports(s); 168 uci_to_blob(&b, s, devtype->config_params); 169 config_bridge_add_vlans(&b, devtype, name, NULL); 170 if (!config_device_add(name, devtype, b.head)) { 171 D(INTERFACE, "Failed to create '%s' device for interface '%s'", 172 devtype->name, s->e.name); 173 } 174 175 blob_buf_init(&b, 0); 176 blobmsg_add_string(&b, "ifname", name); 177 return 0; 178 } 179 180 static void 181 config_parse_interface(struct uci_section *s, bool alias) 182 { 183 struct interface *iface; 184 const char *type = NULL, *disabled; 185 struct blob_attr *config; 186 bool bridge = false; 187 struct device_type *devtype = NULL; 188 189 disabled = uci_lookup_option_string(uci_ctx, s, "disabled"); 190 if (disabled && (!strcmp(disabled, "1") || !strcmp(disabled, "true"))) 191 return; 192 193 blob_buf_init(&b, 0); 194 195 if (!alias) 196 type = uci_lookup_option_string(uci_ctx, s, "type"); 197 198 if (type) 199 devtype = device_type_get(type); 200 201 if (devtype && devtype->bridge_capability) { 202 if (config_parse_bridge_interface(s, devtype)) 203 return; 204 205 bridge = true; 206 } 207 208 uci_to_blob(&b, s, &interface_attr_list); 209 210 iface = interface_alloc(s->e.name, b.head, false); 211 if (!iface) 212 return; 213 214 if (iface->proto_handler) { 215 if (iface->proto_handler->config_load) 216 iface->proto_handler->config_load(iface->proto_handler, s, &b); 217 else if (iface->proto_handler->config_params) 218 uci_to_blob(&b, s, iface->proto_handler->config_params); 219 } 220 221 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params)) 222 iface->device_config = true; 223 224 config = blob_memdup(b.head); 225 if (!config) 226 goto error; 227 228 if (alias) { 229 if (!interface_add_alias(iface, config)) 230 goto error_free_config; 231 } else { 232 if (!interface_add(iface, config)) 233 goto error_free_config; 234 } 235 return; 236 237 error_free_config: 238 free(config); 239 error: 240 interface_free(iface); 241 } 242 243 static void 244 config_parse_route(struct uci_section *s, bool v6) 245 { 246 void *route; 247 248 blob_buf_init(&b, 0); 249 route = blobmsg_open_array(&b, "route"); 250 uci_to_blob(&b, s, &route_attr_list); 251 blobmsg_close_array(&b, route); 252 interface_ip_add_route(NULL, blob_data(b.head), v6); 253 } 254 255 static void 256 config_parse_neighbor(struct uci_section *s, bool v6) 257 { 258 void *neighbor; 259 blob_buf_init(&b,0); 260 neighbor = blobmsg_open_array(&b, "neighbor"); 261 uci_to_blob(&b,s, &neighbor_attr_list); 262 blobmsg_close_array(&b, neighbor); 263 interface_ip_add_neighbor(NULL, blob_data(b.head), v6); 264 } 265 266 static void 267 config_parse_rule(struct uci_section *s, bool v6) 268 { 269 void *rule; 270 271 blob_buf_init(&b, 0); 272 rule = blobmsg_open_array(&b, "rule"); 273 uci_to_blob(&b, s, &rule_attr_list); 274 blobmsg_close_array(&b, rule); 275 iprule_add(blob_data(b.head), v6); 276 } 277 278 static void 279 config_insert_vlan_entry(const char *name, const char *dev_name, struct blob_attr *data) 280 { 281 struct vlan_config_entry *e, *tmp; 282 struct blob_attr *attrbuf; 283 char *dev_name_buf; 284 285 list_for_each_entry_safe(e, tmp, &config_vlans, list) { 286 if (strcmp(e->name, name) != 0 || 287 strcmp(e->dev_name, dev_name) != 0) 288 continue; 289 290 list_del(&e->list); 291 free(e); 292 } 293 294 e = calloc_a(sizeof(*e) + strlen(name) + 1, 295 &attrbuf, blob_pad_len(data), 296 &dev_name_buf, strlen(dev_name) + 1); 297 e->data = memcpy(attrbuf, data, blob_pad_len(data)); 298 e->dev_name = strcpy(dev_name_buf, dev_name); 299 strcpy(e->name, name); 300 list_add_tail(&e->list, &config_vlans); 301 } 302 303 static void 304 config_procd_device_store(struct blob_attr *data) 305 { 306 kvlist_set(&config_procd_devices, blobmsg_name(data), data); 307 } 308 309 enum { 310 PROCD_DEV_ATTR_TYPE, 311 PROCD_DEV_ATTR_VLANS, 312 __PROCD_DEV_ATTR_MAX 313 }; 314 315 static const struct blobmsg_policy procd_dev_attrs[__PROCD_DEV_ATTR_MAX] = { 316 [PROCD_DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING }, 317 [PROCD_DEV_ATTR_VLANS] = { "vlans", BLOBMSG_TYPE_TABLE }, 318 }; 319 320 static bool 321 config_procd_device_type(struct blob_attr *data, bool bridge, 322 struct device_type **devtype) 323 { 324 struct blob_attr *tb[__PROCD_DEV_ATTR_MAX], *vlans; 325 326 blobmsg_parse_attr(procd_dev_attrs, __PROCD_DEV_ATTR_MAX, tb, data); 327 328 *devtype = NULL; 329 if (tb[PROCD_DEV_ATTR_TYPE]) { 330 *devtype = device_type_get(blobmsg_get_string(tb[PROCD_DEV_ATTR_TYPE])); 331 if (!*devtype) 332 return false; 333 } 334 335 if (bridge != (*devtype && (*devtype)->bridge_capability)) 336 return false; 337 338 vlans = tb[PROCD_DEV_ATTR_VLANS]; 339 if (bridge && (*devtype)->bridge_vlans && vlans && 340 blobmsg_check_array(vlans, BLOBMSG_TYPE_TABLE) < 0) 341 return false; 342 343 return true; 344 } 345 346 static void 347 config_procd_bridge_add(const char *name, struct device_type *devtype, 348 struct blob_attr *data) 349 { 350 struct blob_attr *tb[__PROCD_DEV_ATTR_MAX], *cur; 351 size_t rem; 352 353 if (devtype->bridge_vlans && config_bridge_has_vlans(name)) { 354 blobmsg_parse_attr(procd_dev_attrs, __PROCD_DEV_ATTR_MAX, tb, data); 355 blob_buf_init(&b, 0); 356 blobmsg_for_each_attr(cur, data, rem) 357 if (strcmp(blobmsg_name(cur), "vlans") != 0) 358 blobmsg_add_blob(&b, cur); 359 config_bridge_add_vlans(&b, devtype, name, tb[PROCD_DEV_ATTR_VLANS]); 360 data = b.head; 361 } 362 363 config_device_add(name, devtype, data); 364 } 365 366 /* 367 * A procd entry replaces the uci section of the same name, so the section is 368 * not added first, which would apply its config to the device in between. 369 * An entry without a type does not name the device to create, so a typed 370 * section still creates it. 371 */ 372 static void 373 config_init_devices(bool bridge) 374 { 375 struct device_type *procd_devtype; 376 struct uci_element *e; 377 struct blob_attr *data; 378 const char *procd_name; 379 380 uci_foreach_element(&uci_network->sections, e) { 381 const struct uci_blob_param_list *params = NULL; 382 struct uci_section *s = uci_to_section(e); 383 struct device_type *devtype = NULL; 384 const char *type, *name; 385 386 if (strcmp(s->type, "device") != 0) 387 continue; 388 389 name = uci_lookup_option_string(uci_ctx, s, "name"); 390 if (!name) 391 continue; 392 393 type = uci_lookup_option_string(uci_ctx, s, "type"); 394 if (type) 395 devtype = device_type_get(type); 396 397 if (bridge != (devtype && devtype->bridge_capability)) 398 continue; 399 400 data = kvlist_get(&config_procd_devices, name); 401 if (data && config_procd_device_type(data, bridge, &procd_devtype) && 402 (procd_devtype || !devtype)) 403 continue; 404 405 if (devtype) 406 params = devtype->config_params; 407 if (!params) 408 params = simple_device_type.config_params; 409 410 if (bridge) 411 config_fixup_bridge_ports(s); 412 413 blob_buf_init(&b, 0); 414 uci_to_blob(&b, s, params); 415 uci_to_blob(&b, s, &device_bridge_attr_list); 416 if (bridge) 417 config_bridge_add_vlans(&b, devtype, name, NULL); 418 config_device_add(name, devtype, b.head); 419 } 420 421 kvlist_for_each(&config_procd_devices, procd_name, data) { 422 if (!config_procd_device_type(data, bridge, &procd_devtype)) 423 continue; 424 425 if (bridge) 426 config_procd_bridge_add(procd_name, procd_devtype, data); 427 else 428 config_device_add(procd_name, procd_devtype, data); 429 } 430 } 431 432 static void 433 config_load_vlan(const char *dev_name, struct uci_section *s) 434 { 435 blob_buf_init(&b, 0); 436 uci_to_blob(&b, s, &bridge_vlan_attr_list); 437 config_insert_vlan_entry(s->e.name, dev_name, b.head); 438 } 439 440 static void 441 config_procd_vlan_cb(struct blob_attr *data) 442 { 443 static const struct blobmsg_policy policy = 444 { "device", BLOBMSG_TYPE_STRING }; 445 struct blob_attr *attr; 446 447 blobmsg_parse_attr(&policy, 1, &attr, data); 448 if (!attr) 449 return; 450 451 config_insert_vlan_entry(blobmsg_name(data), blobmsg_get_string(attr), data); 452 } 453 454 static void 455 config_load_vlans(void) 456 { 457 struct uci_element *e; 458 459 uci_foreach_element(&uci_network->sections, e) { 460 struct uci_section *s = uci_to_section(e); 461 const char *name; 462 463 if (strcmp(s->type, "bridge-vlan") != 0) 464 continue; 465 466 name = uci_lookup_option_string(uci_ctx, s, "device"); 467 if (!name) 468 continue; 469 470 config_load_vlan(name, s); 471 } 472 473 netifd_ubus_get_procd_data("bridge-vlan", config_procd_vlan_cb); 474 } 475 476 static void 477 config_free_vlans(void) 478 { 479 struct vlan_config_entry *e, *tmp; 480 481 list_for_each_entry_safe(e, tmp, &config_vlans, list) { 482 list_del(&e->list); 483 free(e); 484 } 485 } 486 487 static struct uci_package * 488 config_init_package(const char *config) 489 { 490 struct uci_context *ctx = uci_ctx; 491 struct uci_package *p = NULL; 492 493 if (!ctx) { 494 ctx = uci_alloc_context(); 495 uci_ctx = ctx; 496 497 ctx->flags &= ~UCI_FLAG_STRICT; 498 if (config_path) 499 uci_set_confdir(ctx, config_path); 500 501 #ifdef DUMMY_MODE 502 uci_set_savedir(ctx, "./tmp"); 503 #endif 504 } else { 505 p = uci_lookup_package(ctx, config); 506 if (p) 507 uci_unload(ctx, p); 508 } 509 510 if (uci_load(ctx, config, &p)) 511 return NULL; 512 513 return p; 514 } 515 516 static void 517 config_procd_interface_cb(struct blob_attr *data) 518 { 519 struct interface *iface; 520 const char *name = blobmsg_name(data); 521 522 iface = interface_alloc(name, data, false); 523 if (!iface) 524 return; 525 526 data = blob_memdup(data); 527 if (!data) { 528 interface_free(iface); 529 return; 530 } 531 532 iface->config = data; 533 list_add(&iface->node.avl.list, &config_ifaces); 534 } 535 536 static void 537 config_init_interfaces(void) 538 { 539 struct interface *iface; 540 struct uci_element *e; 541 542 uci_foreach_element(&uci_network->sections, e) { 543 struct uci_section *s = uci_to_section(e); 544 545 if (!strcmp(s->type, "interface")) 546 config_parse_interface(s, false); 547 } 548 549 uci_foreach_element(&uci_network->sections, e) { 550 struct uci_section *s = uci_to_section(e); 551 552 if (!strcmp(s->type, "alias")) 553 config_parse_interface(s, true); 554 } 555 556 netifd_ubus_get_procd_data("network-interface", config_procd_interface_cb); 557 while (!list_empty(&config_ifaces)) { 558 iface = list_first_entry(&config_ifaces, struct interface, node.avl.list); 559 list_del(&iface->node.avl.list); 560 561 if (!interface_add(iface, iface->config)) 562 interface_free(iface); 563 } 564 } 565 566 static void 567 config_init_ip(void) 568 { 569 struct interface *iface; 570 struct uci_element *e; 571 572 vlist_for_each_element(&interfaces, iface, node) 573 interface_ip_update_start(&iface->config_ip); 574 575 uci_foreach_element(&uci_network->sections, e) { 576 struct uci_section *s = uci_to_section(e); 577 578 if (!strcmp(s->type, "route")) 579 config_parse_route(s, false); 580 else if (!strcmp(s->type, "route6")) 581 config_parse_route(s, true); 582 if (!strcmp(s->type, "neighbor")) 583 config_parse_neighbor(s, false); 584 else if (!strcmp(s->type, "neighbor6")) 585 config_parse_neighbor(s, true); 586 } 587 588 vlist_for_each_element(&interfaces, iface, node) 589 interface_ip_update_complete(&iface->config_ip); 590 } 591 592 static void 593 config_init_rules(void) 594 { 595 struct uci_element *e; 596 597 iprule_update_start(); 598 599 uci_foreach_element(&uci_network->sections, e) { 600 struct uci_section *s = uci_to_section(e); 601 602 if (!strcmp(s->type, "rule")) 603 config_parse_rule(s, false); 604 else if (!strcmp(s->type, "rule6")) 605 config_parse_rule(s, true); 606 } 607 608 iprule_update_complete(); 609 } 610 611 static void 612 config_init_globals(void) 613 { 614 struct uci_section *globals = uci_lookup_section( 615 uci_ctx, uci_network, "globals"); 616 if (!globals) 617 return; 618 619 const char *ula_prefix = uci_lookup_option_string( 620 uci_ctx, globals, "ula_prefix"); 621 interface_ip_set_ula_prefix(ula_prefix); 622 623 const char *tcp_l3mdev = uci_lookup_option_string( 624 uci_ctx, globals, "tcp_l3mdev"); 625 if (tcp_l3mdev) 626 system_tcp_l3mdev(!strcmp(tcp_l3mdev, "1")); 627 628 const char *udp_l3mdev = uci_lookup_option_string( 629 uci_ctx, globals, "udp_l3mdev"); 630 if (udp_l3mdev) 631 system_udp_l3mdev(!strcmp(udp_l3mdev, "1")); 632 } 633 634 static struct blob_attr * 635 config_find_blobmsg_attr(struct blob_attr *attr, const char *name, int type) 636 { 637 struct blobmsg_policy policy = { .name = name, .type = type }; 638 struct blob_attr *cur; 639 640 blobmsg_parse_attr(&policy, 1, &cur, attr); 641 642 return cur; 643 } 644 645 struct ether_addr *config_get_default_macaddr(const char *ifname) 646 { 647 struct blob_attr *cur; 648 649 if (!board_netdevs) 650 return NULL; 651 652 cur = config_find_blobmsg_attr(board_netdevs, ifname, BLOBMSG_TYPE_TABLE); 653 if (!cur) 654 return NULL; 655 656 cur = config_find_blobmsg_attr(cur, "macaddr", BLOBMSG_TYPE_STRING); 657 if (!cur) 658 return NULL; 659 660 return ether_aton(blobmsg_get_string(cur)); 661 } 662 663 int config_get_default_gro(const char *ifname) 664 { 665 struct blob_attr *cur; 666 667 if (!board_netdevs) 668 return -1; 669 670 cur = config_find_blobmsg_attr(board_netdevs, ifname, BLOBMSG_TYPE_TABLE); 671 if (!cur) 672 return -1; 673 674 cur = config_find_blobmsg_attr(cur, "gro", BLOBMSG_TYPE_BOOL); 675 if (!cur) 676 return -1; 677 678 return blobmsg_get_bool(cur); 679 } 680 681 int config_get_default_pse(const char *ifname) 682 { 683 struct blob_attr *cur; 684 685 if (!board_netdevs) 686 return -1; 687 688 cur = config_find_blobmsg_attr(board_netdevs, ifname, BLOBMSG_TYPE_TABLE); 689 if (!cur) 690 return -1; 691 692 cur = config_find_blobmsg_attr(cur, "pse", BLOBMSG_TYPE_BOOL); 693 if (!cur) 694 return -1; 695 696 return blobmsg_get_bool(cur); 697 } 698 699 const char *config_get_default_conduit(const char *ifname) 700 { 701 struct blob_attr *cur; 702 703 if (!board_netdevs) 704 return NULL; 705 706 cur = config_find_blobmsg_attr(board_netdevs, ifname, BLOBMSG_TYPE_TABLE); 707 if (!cur) 708 return NULL; 709 710 cur = config_find_blobmsg_attr(cur, "conduit", BLOBMSG_TYPE_STRING); 711 if (!cur) 712 return NULL; 713 714 return blobmsg_get_string(cur); 715 } 716 717 static void 718 config_init_board(void) 719 { 720 struct blob_attr *cur; 721 722 blob_buf_init(&b, 0); 723 724 if (!blobmsg_add_json_from_file(&b, DEFAULT_BOARD_JSON)) 725 return; 726 727 free(board_netdevs); 728 board_netdevs = NULL; 729 730 cur = config_find_blobmsg_attr(b.head, "network_device", 731 BLOBMSG_TYPE_TABLE); 732 if (!cur) 733 return; 734 735 board_netdevs = blob_memdup(cur); 736 } 737 738 int 739 config_init_all(void) 740 { 741 int ret = 0; 742 char *err; 743 744 uci_network = config_init_package("network"); 745 if (!uci_network) { 746 uci_get_errorstr(uci_ctx, &err, NULL); 747 netifd_log_message(L_CRIT, "Failed to load network config (%s)\n", err); 748 free(err); 749 return -1; 750 } 751 752 config_init_board(); 753 754 vlist_update(&interfaces); 755 config_init = true; 756 757 device_reset_config(); 758 config_load_vlans(); 759 netifd_ubus_get_procd_data("network-device", config_procd_device_store); 760 config_init_devices(true); 761 config_init_devices(false); 762 kvlist_free(&config_procd_devices); 763 netifd_ucode_config_load(false); 764 config_init_interfaces(); 765 config_free_vlans(); 766 config_init_ip(); 767 config_init_rules(); 768 config_init_globals(); 769 770 config_init = false; 771 772 device_reset_old(); 773 device_init_pending(); 774 vlist_flush(&interfaces); 775 /* a recreated bridge lost its hotplug members without any claim */ 776 interface_relink_hotplug(); 777 interface_refresh_assignments(false); 778 interface_start_pending(); 779 netifd_ucode_config_load(true); 780 781 return ret; 782 } 783
This page was automatically generated by LXR 0.3.1. • OpenWrt