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 "interface.h"
22 #include "interface-ip.h"
27 bool config_init = false;
29 static struct uci_context *uci_ctx;
30 static struct uci_package *uci_network;
31 static struct blob_buf b;
34 config_parse_bridge_interface(struct uci_section *s)
38 name = alloca(strlen(s->e.name) + 4);
39 sprintf(name, "br-%s", s->e.name);
40 blobmsg_add_string(&b, "name", name);
42 uci_to_blob(&b, s, bridge_device_type.config_params);
43 if (!device_create(name, &bridge_device_type, b.head)) {
44 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
49 blobmsg_add_string(&b, "ifname", name);
54 config_parse_interface(struct uci_section *s, bool alias)
56 struct interface *iface;
57 const char *type = NULL;
58 struct blob_attr *config;
65 type = uci_lookup_option_string(uci_ctx, s, "type");
66 if (type && !strcmp(type, "bridge")) {
67 if (config_parse_bridge_interface(s))
73 uci_to_blob(&b, s, &interface_attr_list);
75 iface = interface_alloc(s->e.name, b.head);
79 if (iface->proto_handler && iface->proto_handler->config_params)
80 uci_to_blob(&b, s, iface->proto_handler->config_params);
82 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
83 iface->device_config = true;
85 config = blob_memdup(b.head);
90 if (!interface_add_alias(iface, config))
91 goto error_free_config;
93 interface_add(iface, config);
97 * need to look up the interface name again, in case of config update,
98 * the pointer will have changed
100 iface = vlist_find(&interfaces, s->e.name, iface, node);
104 dev = iface->main_dev.dev;
105 if (!dev || !dev->default_config)
108 blob_buf_init(&b, 0);
109 uci_to_blob(&b, s, dev->type->config_params);
110 if (blob_len(b.head) == 0)
113 device_set_config(dev, dev->type, b.head);
122 config_parse_route(struct uci_section *s, bool v6)
126 blob_buf_init(&b, 0);
127 route = blobmsg_open_array(&b, "route");
128 uci_to_blob(&b, s, &route_attr_list);
129 blobmsg_close_array(&b, route);
130 interface_ip_add_route(NULL, blob_data(b.head), v6);
134 config_parse_rule(struct uci_section *s, bool v6)
138 blob_buf_init(&b, 0);
139 rule = blobmsg_open_array(&b, "rule");
140 uci_to_blob(&b, s, &rule_attr_list);
141 blobmsg_close_array(&b, rule);
142 iprule_add(blob_data(b.head), v6);
146 config_init_devices(void)
148 struct uci_element *e;
150 uci_foreach_element(&uci_network->sections, e) {
151 struct uci_section *s = uci_to_section(e);
152 const struct device_type *devtype = NULL;
153 const char *type, *name;
155 if (strcmp(s->type, "device") != 0)
158 name = uci_lookup_option_string(uci_ctx, s, "name");
162 type = uci_lookup_option_string(uci_ctx, s, "type");
164 if (!strcmp(type, "bridge"))
165 devtype = &bridge_device_type;
166 else if (!strcmp(type, "tunnel"))
167 devtype = &tunnel_device_type;
168 else if (!strcmp(type, "macvlan"))
169 devtype = &macvlan_device_type;
173 devtype = &simple_device_type;
175 blob_buf_init(&b, 0);
176 uci_to_blob(&b, s, devtype->config_params);
177 device_create(name, devtype, b.head);
181 static struct uci_package *
182 config_init_package(const char *config)
184 struct uci_context *ctx = uci_ctx;
185 struct uci_package *p = NULL;
188 ctx = uci_alloc_context();
192 uci_set_confdir(ctx, "./config");
193 uci_set_savedir(ctx, "./tmp");
196 p = uci_lookup_package(ctx, config);
201 if (uci_load(ctx, config, &p))
208 config_init_interfaces(void)
210 struct uci_element *e;
212 uci_foreach_element(&uci_network->sections, e) {
213 struct uci_section *s = uci_to_section(e);
215 if (!strcmp(s->type, "interface"))
216 config_parse_interface(s, false);
219 uci_foreach_element(&uci_network->sections, e) {
220 struct uci_section *s = uci_to_section(e);
222 if (!strcmp(s->type, "alias"))
223 config_parse_interface(s, true);
228 config_init_routes(void)
230 struct interface *iface;
231 struct uci_element *e;
233 vlist_for_each_element(&interfaces, iface, node)
234 interface_ip_update_start(&iface->config_ip);
236 uci_foreach_element(&uci_network->sections, e) {
237 struct uci_section *s = uci_to_section(e);
239 if (!strcmp(s->type, "route"))
240 config_parse_route(s, false);
241 else if (!strcmp(s->type, "route6"))
242 config_parse_route(s, true);
245 vlist_for_each_element(&interfaces, iface, node)
246 interface_ip_update_complete(&iface->config_ip);
250 config_init_rules(void)
252 struct uci_element *e;
254 iprule_update_start();
256 uci_foreach_element(&uci_network->sections, e) {
257 struct uci_section *s = uci_to_section(e);
259 if (!strcmp(s->type, "rule"))
260 config_parse_rule(s, false);
261 else if (!strcmp(s->type, "rule6"))
262 config_parse_rule(s, true);
265 iprule_update_complete();
269 config_init_globals(void)
271 struct uci_section *globals = uci_lookup_section(
272 uci_ctx, uci_network, "globals");
276 const char *ula_prefix = uci_lookup_option_string(
277 uci_ctx, globals, "ula_prefix");
278 interface_ip_set_ula_prefix(ula_prefix);
282 config_init_all(void)
284 uci_network = config_init_package("network");
286 fprintf(stderr, "Failed to load network config\n");
290 vlist_update(&interfaces);
294 device_reset_config();
295 config_init_devices();
296 config_init_interfaces();
297 config_init_routes();
299 config_init_globals();
305 device_init_pending();
306 vlist_flush(&interfaces);
307 device_free_unused(NULL);
308 interface_refresh_assignments(false);
309 interface_start_pending();