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"
28 bool config_init = false;
30 static struct uci_context *uci_ctx;
31 static struct uci_package *uci_network;
32 static struct uci_package *uci_wireless;
33 static struct blob_buf b;
36 config_parse_bridge_interface(struct uci_section *s)
40 name = alloca(strlen(s->e.name) + 4);
41 sprintf(name, "br-%s", s->e.name);
42 blobmsg_add_string(&b, "name", name);
44 uci_to_blob(&b, s, bridge_device_type.config_params);
45 if (!device_create(name, &bridge_device_type, b.head)) {
46 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
51 blobmsg_add_string(&b, "ifname", name);
56 config_parse_interface(struct uci_section *s, bool alias)
58 struct interface *iface;
59 const char *type = NULL, *disabled;
60 struct blob_attr *config;
64 disabled = uci_lookup_option_string(uci_ctx, s, "disabled");
65 if (disabled && !strcmp(disabled, "1"))
71 type = uci_lookup_option_string(uci_ctx, s, "type");
72 if (type && !strcmp(type, "bridge")) {
73 if (config_parse_bridge_interface(s))
79 uci_to_blob(&b, s, &interface_attr_list);
81 iface = interface_alloc(s->e.name, b.head);
85 if (iface->proto_handler && iface->proto_handler->config_params)
86 uci_to_blob(&b, s, iface->proto_handler->config_params);
88 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
89 iface->device_config = true;
91 config = blob_memdup(b.head);
96 if (!interface_add_alias(iface, config))
97 goto error_free_config;
99 interface_add(iface, config);
103 * need to look up the interface name again, in case of config update,
104 * the pointer will have changed
106 iface = vlist_find(&interfaces, s->e.name, iface, node);
110 dev = iface->main_dev.dev;
111 if (!dev || !dev->default_config)
114 blob_buf_init(&b, 0);
115 uci_to_blob(&b, s, dev->type->config_params);
116 if (blob_len(b.head) == 0)
119 device_set_config(dev, dev->type, b.head);
128 config_parse_route(struct uci_section *s, bool v6)
132 blob_buf_init(&b, 0);
133 route = blobmsg_open_array(&b, "route");
134 uci_to_blob(&b, s, &route_attr_list);
135 blobmsg_close_array(&b, route);
136 interface_ip_add_route(NULL, blob_data(b.head), v6);
140 config_parse_rule(struct uci_section *s, bool v6)
144 blob_buf_init(&b, 0);
145 rule = blobmsg_open_array(&b, "rule");
146 uci_to_blob(&b, s, &rule_attr_list);
147 blobmsg_close_array(&b, rule);
148 iprule_add(blob_data(b.head), v6);
152 config_init_devices(void)
154 struct uci_element *e;
156 uci_foreach_element(&uci_network->sections, e) {
157 struct uci_section *s = uci_to_section(e);
158 const struct device_type *devtype = NULL;
159 const char *type, *name;
161 if (strcmp(s->type, "device") != 0)
164 name = uci_lookup_option_string(uci_ctx, s, "name");
168 type = uci_lookup_option_string(uci_ctx, s, "type");
170 if (!strcmp(type, "8021ad"))
171 devtype = &vlandev_device_type;
172 else if (!strcmp(type, "8021q"))
173 devtype = &vlandev_device_type;
174 else if (!strcmp(type, "bridge"))
175 devtype = &bridge_device_type;
176 else if (!strcmp(type, "macvlan"))
177 devtype = &macvlan_device_type;
178 else if (!strcmp(type, "tunnel"))
179 devtype = &tunnel_device_type;
183 devtype = &simple_device_type;
185 blob_buf_init(&b, 0);
186 uci_to_blob(&b, s, devtype->config_params);
187 device_create(name, devtype, b.head);
191 static struct uci_package *
192 config_init_package(const char *config)
194 struct uci_context *ctx = uci_ctx;
195 struct uci_package *p = NULL;
198 ctx = uci_alloc_context();
201 ctx->flags &= ~UCI_FLAG_STRICT;
203 uci_set_confdir(ctx, config_path);
206 uci_set_savedir(ctx, "./tmp");
209 p = uci_lookup_package(ctx, config);
214 if (uci_load(ctx, config, &p))
221 config_init_interfaces(void)
223 struct uci_element *e;
225 uci_foreach_element(&uci_network->sections, e) {
226 struct uci_section *s = uci_to_section(e);
228 if (!strcmp(s->type, "interface"))
229 config_parse_interface(s, false);
232 uci_foreach_element(&uci_network->sections, e) {
233 struct uci_section *s = uci_to_section(e);
235 if (!strcmp(s->type, "alias"))
236 config_parse_interface(s, true);
241 config_init_routes(void)
243 struct interface *iface;
244 struct uci_element *e;
246 vlist_for_each_element(&interfaces, iface, node)
247 interface_ip_update_start(&iface->config_ip);
249 uci_foreach_element(&uci_network->sections, e) {
250 struct uci_section *s = uci_to_section(e);
252 if (!strcmp(s->type, "route"))
253 config_parse_route(s, false);
254 else if (!strcmp(s->type, "route6"))
255 config_parse_route(s, true);
258 vlist_for_each_element(&interfaces, iface, node)
259 interface_ip_update_complete(&iface->config_ip);
263 config_init_rules(void)
265 struct uci_element *e;
267 iprule_update_start();
269 uci_foreach_element(&uci_network->sections, e) {
270 struct uci_section *s = uci_to_section(e);
272 if (!strcmp(s->type, "rule"))
273 config_parse_rule(s, false);
274 else if (!strcmp(s->type, "rule6"))
275 config_parse_rule(s, true);
278 iprule_update_complete();
282 config_init_globals(void)
284 struct uci_section *globals = uci_lookup_section(
285 uci_ctx, uci_network, "globals");
289 const char *ula_prefix = uci_lookup_option_string(
290 uci_ctx, globals, "ula_prefix");
291 interface_ip_set_ula_prefix(ula_prefix);
295 config_parse_wireless_device(struct uci_section *s)
297 struct wireless_driver *drv;
298 const char *driver_name;
300 driver_name = uci_lookup_option_string(uci_ctx, s, "type");
304 drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
308 blob_buf_init(&b, 0);
309 uci_to_blob(&b, s, drv->device.config);
310 wireless_device_create(drv, s->e.name, b.head);
314 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
316 blob_buf_init(&b, 0);
317 uci_to_blob(&b, s, wdev->drv->interface.config);
318 wireless_interface_create(wdev, b.head, s->e.name);
322 config_init_wireless(void)
324 struct wireless_device *wdev;
325 struct uci_element *e;
326 const char *dev_name;
329 DPRINTF("No wireless configuration found\n");
333 vlist_update(&wireless_devices);
335 uci_foreach_element(&uci_wireless->sections, e) {
336 struct uci_section *s = uci_to_section(e);
337 if (strcmp(s->type, "wifi-device") != 0)
340 config_parse_wireless_device(s);
343 vlist_flush(&wireless_devices);
345 vlist_for_each_element(&wireless_devices, wdev, node) {
347 vlist_update(&wdev->interfaces);
350 uci_foreach_element(&uci_wireless->sections, e) {
351 struct uci_section *s = uci_to_section(e);
353 if (strcmp(s->type, "wifi-iface") != 0)
356 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
360 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
362 DPRINTF("device %s not found!\n", dev_name);
366 config_parse_wireless_interface(wdev, s);
369 vlist_for_each_element(&wireless_devices, wdev, node)
370 vlist_flush(&wdev->interfaces);
374 config_init_all(void)
376 uci_network = config_init_package("network");
378 fprintf(stderr, "Failed to load network config\n");
382 uci_wireless = config_init_package("wireless");
384 vlist_update(&interfaces);
388 device_reset_config();
389 config_init_devices();
390 config_init_interfaces();
391 config_init_routes();
393 config_init_globals();
394 config_init_wireless();
400 device_init_pending();
401 vlist_flush(&interfaces);
402 device_free_unused(NULL);
403 interface_refresh_assignments(false);
404 interface_start_pending();
405 wireless_start_pending();