2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
21 #include <libubox/blobmsg_json.h>
24 #include "interface.h"
25 #include "interface-ip.h"
31 bool config_init
= false;
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
;
40 config_section_idx(struct uci_section
*s
)
42 struct uci_element
*e
;
45 uci_foreach_element(&uci_wireless
->sections
, e
) {
46 struct uci_section
*cur
= uci_to_section(e
);
51 if (!strcmp(cur
->type
, s
->type
))
59 config_bridge_has_vlans(const char *br_name
)
61 struct uci_element
*e
;
63 uci_foreach_element(&uci_network
->sections
, e
) {
64 struct uci_section
*s
= uci_to_section(e
);
67 if (strcmp(s
->type
, "bridge-vlan") != 0)
70 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
74 if (!strcmp(name
, br_name
))
82 config_fixup_bridge_var(struct uci_section
*s
, const char *name
, const char *val
)
84 struct uci_ptr ptr
= {
91 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
95 uci_set(uci_ctx
, &ptr
);
99 * config_fixup_bridge_ports - translate deprecated configs
101 * Old configs used "ifname" option for specifying bridge ports. For backward
102 * compatibility translate it into the new "ports" option.
104 static void config_fixup_bridge_ports(struct uci_section
*s
)
106 struct uci_ptr ptr
= {
112 if (uci_lookup_option(uci_ctx
, s
, "ports"))
115 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
120 uci_rename(uci_ctx
, &ptr
);
124 config_fixup_bridge_vlan_filtering(struct uci_section
*s
, const char *name
)
126 bool has_vlans
= config_bridge_has_vlans(name
);
128 config_fixup_bridge_var(s
, "__has_vlans", has_vlans
? "1" : "0");
133 config_fixup_bridge_var(s
, "vlan_filtering", "1");
137 config_parse_bridge_interface(struct uci_section
*s
, struct device_type
*devtype
)
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
);
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
);
153 blob_buf_init(&b
, 0);
154 blobmsg_add_string(&b
, "ifname", name
);
159 config_parse_interface(struct uci_section
*s
, bool alias
)
161 struct interface
*iface
;
162 const char *type
= NULL
, *disabled
;
163 struct blob_attr
*config
;
165 struct device_type
*devtype
= NULL
;
167 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
168 if (disabled
&& !strcmp(disabled
, "1"))
171 blob_buf_init(&b
, 0);
174 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
177 devtype
= device_type_get(type
);
179 if (devtype
&& devtype
->bridge_capability
) {
180 if (config_parse_bridge_interface(s
, devtype
))
186 uci_to_blob(&b
, s
, &interface_attr_list
);
188 iface
= interface_alloc(s
->e
.name
, b
.head
, false);
192 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
193 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
195 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
196 iface
->device_config
= true;
198 config
= blob_memdup(b
.head
);
203 if (!interface_add_alias(iface
, config
))
204 goto error_free_config
;
206 if (!interface_add(iface
, config
))
207 goto error_free_config
;
218 config_parse_route(struct uci_section
*s
, bool v6
)
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
);
230 config_parse_neighbor(struct uci_section
*s
, bool v6
)
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
);
241 config_parse_rule(struct uci_section
*s
, bool v6
)
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
);
253 config_init_devices(bool bridge
)
255 struct uci_element
*e
;
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
;
262 const char *type
, *name
;
264 if (strcmp(s
->type
, "device") != 0)
267 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
271 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
273 devtype
= device_type_get(type
);
275 if (bridge
!= (devtype
&& devtype
->bridge_capability
))
279 params
= devtype
->config_params
;
281 params
= simple_device_type
.config_params
;
283 if (devtype
&& devtype
->bridge_capability
) {
284 config_fixup_bridge_ports(s
);
285 config_fixup_bridge_vlan_filtering(s
, name
);
288 blob_buf_init(&b
, 0);
289 uci_to_blob(&b
, s
, params
);
291 dev
= device_create(name
, devtype
, b
.head
);
295 dev
= device_get(name
, 1);
299 dev
->current_config
= true;
300 device_apply_config(dev
, dev
->type
, b
.head
);
302 dev
->default_config
= false;
307 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
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
},
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
},
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
,
331 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
332 struct blob_attr
*cur
;
333 struct bridge_vlan_port
*port
;
334 struct bridge_vlan
*vlan
;
342 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
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
));
350 if (!tb
[BRVLAN_ATTR_VID
])
353 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
354 if (!vid
|| vid
> 4095)
357 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
358 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
362 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
368 if (tb
[BRVLAN_ATTR_LOCAL
])
369 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
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
];
376 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
379 port
->ifname
= name_buf
;
380 port
->flags
= BRVLAN_F_UNTAGGED
;
381 strcpy(name_buf
, blobmsg_get_string(cur
));
383 sep
= strchr(name_buf
, ':');
385 for (*sep
= 0, sep
++; *sep
; sep
++)
388 port
->flags
|= BRVLAN_F_PVID
;
391 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
396 name_buf
+= strlen(name_buf
) + 1;
400 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_ALIAS
], rem
)
401 kvlist_set(&dev
->vlan_aliases
, blobmsg_get_string(cur
), &vid
);
403 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
408 config_init_vlans(void)
410 struct uci_element
*e
;
413 device_vlan_update(false);
414 uci_foreach_element(&uci_network
->sections
, e
) {
415 struct uci_section
*s
= uci_to_section(e
);
418 if (strcmp(s
->type
, "bridge-vlan") != 0)
421 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
425 dev
= device_get(name
, 0);
426 if (!dev
|| !dev
->vlans
.update
)
429 config_parse_vlan(dev
, s
);
431 device_vlan_update(true);
434 static struct uci_package
*
435 config_init_package(const char *config
)
437 struct uci_context
*ctx
= uci_ctx
;
438 struct uci_package
*p
= NULL
;
441 ctx
= uci_alloc_context();
444 ctx
->flags
&= ~UCI_FLAG_STRICT
;
446 uci_set_confdir(ctx
, config_path
);
449 uci_set_savedir(ctx
, "./tmp");
452 p
= uci_lookup_package(ctx
, config
);
457 if (uci_load(ctx
, config
, &p
))
464 config_init_interfaces(void)
466 struct uci_element
*e
;
468 uci_foreach_element(&uci_network
->sections
, e
) {
469 struct uci_section
*s
= uci_to_section(e
);
471 if (!strcmp(s
->type
, "interface"))
472 config_parse_interface(s
, false);
475 uci_foreach_element(&uci_network
->sections
, e
) {
476 struct uci_section
*s
= uci_to_section(e
);
478 if (!strcmp(s
->type
, "alias"))
479 config_parse_interface(s
, true);
486 struct interface
*iface
;
487 struct uci_element
*e
;
489 vlist_for_each_element(&interfaces
, iface
, node
)
490 interface_ip_update_start(&iface
->config_ip
);
492 uci_foreach_element(&uci_network
->sections
, e
) {
493 struct uci_section
*s
= uci_to_section(e
);
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);
505 vlist_for_each_element(&interfaces
, iface
, node
)
506 interface_ip_update_complete(&iface
->config_ip
);
510 config_init_rules(void)
512 struct uci_element
*e
;
514 iprule_update_start();
516 uci_foreach_element(&uci_network
->sections
, e
) {
517 struct uci_section
*s
= uci_to_section(e
);
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);
525 iprule_update_complete();
529 config_init_globals(void)
531 struct uci_section
*globals
= uci_lookup_section(
532 uci_ctx
, uci_network
, "globals");
536 const char *ula_prefix
= uci_lookup_option_string(
537 uci_ctx
, globals
, "ula_prefix");
538 interface_ip_set_ula_prefix(ula_prefix
);
542 config_parse_wireless_device(struct uci_section
*s
)
544 struct wireless_driver
*drv
;
545 const char *driver_name
;
547 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
551 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
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
);
561 config_parse_wireless_vlan(struct wireless_interface
*vif
, struct uci_section
*s
)
565 name
= alloca(strlen(s
->type
) + 16);
566 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
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
);
574 config_parse_wireless_station(struct wireless_interface
*vif
, struct uci_section
*s
)
578 name
= alloca(strlen(s
->type
) + 16);
579 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
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
);
587 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
589 struct wireless_interface
*vif
;
590 struct uci_element
*f
;
593 name
= alloca(strlen(s
->type
) + 16);
594 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
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
);
602 vif
->vlan_idx
= vif
->sta_idx
= 0;
603 vlist_update(&vif
->vlans
);
604 vlist_update(&vif
->stations
);
609 uci_foreach_element(&uci_wireless
->sections
, f
) {
610 struct uci_section
*cur
= uci_to_section(f
);
611 const char *vif_name
;
613 if (strcmp(cur
->type
, "wifi-vlan") != 0)
616 vif_name
= uci_lookup_option_string(uci_ctx
, cur
, "iface");
617 if (vif_name
&& strcmp(s
->e
.name
, vif_name
))
619 config_parse_wireless_vlan(vif
, cur
);
622 uci_foreach_element(&uci_wireless
->sections
, f
) {
623 struct uci_section
*cur
= uci_to_section(f
);
624 const char *vif_name
;
626 if (strcmp(cur
->type
, "wifi-station") != 0)
629 vif_name
= uci_lookup_option_string(uci_ctx
, cur
, "iface");
630 if (vif_name
&& strcmp(s
->e
.name
, vif_name
))
632 config_parse_wireless_station(vif
, cur
);
636 vlist_flush(&vif
->vlans
);
637 vlist_flush(&vif
->stations
);
641 config_init_wireless(void)
643 struct wireless_device
*wdev
;
644 struct uci_element
*e
;
645 const char *dev_name
;
648 DPRINTF("No wireless configuration found\n");
652 vlist_update(&wireless_devices
);
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)
659 config_parse_wireless_device(s
);
662 vlist_flush(&wireless_devices
);
664 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
666 vlist_update(&wdev
->interfaces
);
669 uci_foreach_element(&uci_wireless
->sections
, e
) {
670 struct uci_section
*s
= uci_to_section(e
);
672 if (strcmp(s
->type
, "wifi-iface") != 0)
675 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
679 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
681 DPRINTF("device %s not found!\n", dev_name
);
685 config_parse_wireless_interface(wdev
, s
);
688 vlist_for_each_element(&wireless_devices
, wdev
, node
)
689 vlist_flush(&wdev
->interfaces
);
693 static struct blob_attr
*
694 config_find_blobmsg_attr(struct blob_attr
*attr
, const char *name
, int type
)
696 struct blobmsg_policy policy
= { .name
= name
, .type
= type
};
697 struct blob_attr
*cur
;
699 blobmsg_parse(&policy
, 1, &cur
, blobmsg_data(attr
), blobmsg_len(attr
));
704 struct ether_addr
*config_get_default_macaddr(const char *ifname
)
706 struct blob_attr
*cur
;
711 cur
= config_find_blobmsg_attr(board_netdevs
, ifname
, BLOBMSG_TYPE_TABLE
);
715 cur
= config_find_blobmsg_attr(cur
, "macaddr", BLOBMSG_TYPE_STRING
);
719 return ether_aton(blobmsg_get_string(cur
));
723 config_init_board(void)
725 struct blob_attr
*cur
;
727 blob_buf_init(&b
, 0);
729 if (!blobmsg_add_json_from_file(&b
, DEFAULT_BOARD_JSON
))
733 board_netdevs
= NULL
;
735 cur
= config_find_blobmsg_attr(b
.head
, "network-device",
740 board_netdevs
= blob_memdup(cur
);
744 config_init_all(void)
749 uci_network
= config_init_package("network");
751 uci_get_errorstr(uci_ctx
, &err
, NULL
);
752 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
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
);
767 vlist_update(&interfaces
);
770 device_reset_config();
771 config_init_devices(true);
773 config_init_devices(false);
774 config_init_interfaces();
777 config_init_globals();
778 config_init_wireless();
783 device_init_pending();
784 vlist_flush(&interfaces
);
785 interface_refresh_assignments(false);
786 interface_start_pending();
787 wireless_start_pending(0);