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_section_idx(struct uci_section *s)
38 struct uci_element *e;
41 uci_foreach_element(&uci_wireless->sections, e) {
42 struct uci_section *cur = uci_to_section(e);
47 if (!strcmp(cur->type, s->type))
55 config_parse_bridge_interface(struct uci_section *s)
59 name = alloca(strlen(s->e.name) + 4);
60 sprintf(name, "br-%s", s->e.name);
61 blobmsg_add_string(&b, "name", name);
63 uci_to_blob(&b, s, bridge_device_type.config_params);
64 if (!device_create(name, &bridge_device_type, b.head)) {
65 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
70 blobmsg_add_string(&b, "ifname", name);
75 config_parse_interface(struct uci_section *s, bool alias)
77 struct interface *iface;
78 const char *type = NULL, *disabled;
79 struct blob_attr *config;
83 disabled = uci_lookup_option_string(uci_ctx, s, "disabled");
84 if (disabled && !strcmp(disabled, "1"))
90 type = uci_lookup_option_string(uci_ctx, s, "type");
91 if (type && !strcmp(type, "bridge")) {
92 if (config_parse_bridge_interface(s))
98 uci_to_blob(&b, s, &interface_attr_list);
100 iface = interface_alloc(s->e.name, b.head);
104 if (iface->proto_handler && iface->proto_handler->config_params)
105 uci_to_blob(&b, s, iface->proto_handler->config_params);
107 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
108 iface->device_config = true;
110 config = blob_memdup(b.head);
115 if (!interface_add_alias(iface, config))
116 goto error_free_config;
118 interface_add(iface, config);
122 * need to look up the interface name again, in case of config update,
123 * the pointer will have changed
125 iface = vlist_find(&interfaces, s->e.name, iface, node);
129 dev = iface->main_dev.dev;
130 if (!dev || !dev->default_config)
133 blob_buf_init(&b, 0);
134 uci_to_blob(&b, s, dev->type->config_params);
135 if (blob_len(b.head) == 0)
138 device_set_config(dev, dev->type, b.head);
147 config_parse_route(struct uci_section *s, bool v6)
151 blob_buf_init(&b, 0);
152 route = blobmsg_open_array(&b, "route");
153 uci_to_blob(&b, s, &route_attr_list);
154 blobmsg_close_array(&b, route);
155 interface_ip_add_route(NULL, blob_data(b.head), v6);
159 config_parse_rule(struct uci_section *s, bool v6)
163 blob_buf_init(&b, 0);
164 rule = blobmsg_open_array(&b, "rule");
165 uci_to_blob(&b, s, &rule_attr_list);
166 blobmsg_close_array(&b, rule);
167 iprule_add(blob_data(b.head), v6);
171 config_init_devices(void)
173 struct uci_element *e;
175 uci_foreach_element(&uci_network->sections, e) {
176 struct uci_section *s = uci_to_section(e);
177 const struct device_type *devtype = NULL;
178 const char *type, *name;
180 if (strcmp(s->type, "device") != 0)
183 name = uci_lookup_option_string(uci_ctx, s, "name");
187 type = uci_lookup_option_string(uci_ctx, s, "type");
189 if (!strcmp(type, "8021ad"))
190 devtype = &vlandev_device_type;
191 else if (!strcmp(type, "8021q"))
192 devtype = &vlandev_device_type;
193 else if (!strcmp(type, "bridge"))
194 devtype = &bridge_device_type;
195 else if (!strcmp(type, "macvlan"))
196 devtype = &macvlan_device_type;
197 else if (!strcmp(type, "tunnel"))
198 devtype = &tunnel_device_type;
202 devtype = &simple_device_type;
204 blob_buf_init(&b, 0);
205 uci_to_blob(&b, s, devtype->config_params);
206 device_create(name, devtype, b.head);
210 static struct uci_package *
211 config_init_package(const char *config)
213 struct uci_context *ctx = uci_ctx;
214 struct uci_package *p = NULL;
217 ctx = uci_alloc_context();
220 ctx->flags &= ~UCI_FLAG_STRICT;
222 uci_set_confdir(ctx, config_path);
225 uci_set_savedir(ctx, "./tmp");
228 p = uci_lookup_package(ctx, config);
233 if (uci_load(ctx, config, &p))
240 config_init_interfaces(void)
242 struct uci_element *e;
244 uci_foreach_element(&uci_network->sections, e) {
245 struct uci_section *s = uci_to_section(e);
247 if (!strcmp(s->type, "interface"))
248 config_parse_interface(s, false);
251 uci_foreach_element(&uci_network->sections, e) {
252 struct uci_section *s = uci_to_section(e);
254 if (!strcmp(s->type, "alias"))
255 config_parse_interface(s, true);
260 config_init_routes(void)
262 struct interface *iface;
263 struct uci_element *e;
265 vlist_for_each_element(&interfaces, iface, node)
266 interface_ip_update_start(&iface->config_ip);
268 uci_foreach_element(&uci_network->sections, e) {
269 struct uci_section *s = uci_to_section(e);
271 if (!strcmp(s->type, "route"))
272 config_parse_route(s, false);
273 else if (!strcmp(s->type, "route6"))
274 config_parse_route(s, true);
277 vlist_for_each_element(&interfaces, iface, node)
278 interface_ip_update_complete(&iface->config_ip);
282 config_init_rules(void)
284 struct uci_element *e;
286 iprule_update_start();
288 uci_foreach_element(&uci_network->sections, e) {
289 struct uci_section *s = uci_to_section(e);
291 if (!strcmp(s->type, "rule"))
292 config_parse_rule(s, false);
293 else if (!strcmp(s->type, "rule6"))
294 config_parse_rule(s, true);
297 iprule_update_complete();
301 config_init_globals(void)
303 struct uci_section *globals = uci_lookup_section(
304 uci_ctx, uci_network, "globals");
308 const char *ula_prefix = uci_lookup_option_string(
309 uci_ctx, globals, "ula_prefix");
310 interface_ip_set_ula_prefix(ula_prefix);
314 config_parse_wireless_device(struct uci_section *s)
316 struct wireless_driver *drv;
317 const char *driver_name;
319 driver_name = uci_lookup_option_string(uci_ctx, s, "type");
323 drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
327 blob_buf_init(&b, 0);
328 uci_to_blob(&b, s, drv->device.config);
329 wireless_device_create(drv, s->e.name, b.head);
333 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
338 name = alloca(strlen(s->type) + 16);
339 asprintf(&name, "@%s[%d]", s->type, config_section_idx(s));
342 blob_buf_init(&b, 0);
343 uci_to_blob(&b, s, wdev->drv->interface.config);
344 wireless_interface_create(wdev, b.head, name ? name : s->e.name);
348 config_init_wireless(void)
350 struct wireless_device *wdev;
351 struct uci_element *e;
352 const char *dev_name;
355 DPRINTF("No wireless configuration found\n");
359 vlist_update(&wireless_devices);
361 uci_foreach_element(&uci_wireless->sections, e) {
362 struct uci_section *s = uci_to_section(e);
363 if (strcmp(s->type, "wifi-device") != 0)
366 config_parse_wireless_device(s);
369 vlist_flush(&wireless_devices);
371 vlist_for_each_element(&wireless_devices, wdev, node) {
373 vlist_update(&wdev->interfaces);
376 uci_foreach_element(&uci_wireless->sections, e) {
377 struct uci_section *s = uci_to_section(e);
379 if (strcmp(s->type, "wifi-iface") != 0)
382 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
386 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
388 DPRINTF("device %s not found!\n", dev_name);
392 config_parse_wireless_interface(wdev, s);
395 vlist_for_each_element(&wireless_devices, wdev, node)
396 vlist_flush(&wdev->interfaces);
400 config_init_all(void)
402 uci_network = config_init_package("network");
404 fprintf(stderr, "Failed to load network config\n");
408 uci_wireless = config_init_package("wireless");
410 vlist_update(&interfaces);
414 device_reset_config();
415 config_init_devices();
416 config_init_interfaces();
417 config_init_routes();
419 config_init_globals();
420 config_init_wireless();
426 device_init_pending();
427 vlist_flush(&interfaces);
428 device_free_unused(NULL);
429 interface_refresh_assignments(false);
430 interface_start_pending();
431 wireless_start_pending();