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.
22 #include "interface.h"
23 #include "interface-ip.h"
29 bool config_init = false;
31 static struct uci_context *uci_ctx;
32 static struct uci_package *uci_network;
33 static struct uci_package *uci_wireless;
34 static struct blob_buf b;
37 config_section_idx(struct uci_section *s)
39 struct uci_element *e;
42 uci_foreach_element(&uci_wireless->sections, e) {
43 struct uci_section *cur = uci_to_section(e);
48 if (!strcmp(cur->type, s->type))
56 config_parse_bridge_interface(struct uci_section *s)
60 name = alloca(strlen(s->e.name) + 4);
61 sprintf(name, "br-%s", s->e.name);
62 blobmsg_add_string(&b, "name", name);
64 uci_to_blob(&b, s, bridge_device_type.config_params);
65 if (!device_create(name, &bridge_device_type, b.head)) {
66 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
71 blobmsg_add_string(&b, "ifname", name);
76 config_parse_interface(struct uci_section *s, bool alias)
78 struct interface *iface;
79 const char *type = NULL, *disabled;
80 struct blob_attr *config;
84 disabled = uci_lookup_option_string(uci_ctx, s, "disabled");
85 if (disabled && !strcmp(disabled, "1"))
91 type = uci_lookup_option_string(uci_ctx, s, "type");
92 if (type && !strcmp(type, "bridge")) {
93 if (config_parse_bridge_interface(s))
99 uci_to_blob(&b, s, &interface_attr_list);
101 iface = interface_alloc(s->e.name, b.head);
105 if (iface->proto_handler && iface->proto_handler->config_params)
106 uci_to_blob(&b, s, iface->proto_handler->config_params);
108 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
109 iface->device_config = true;
111 config = blob_memdup(b.head);
116 if (!interface_add_alias(iface, config))
117 goto error_free_config;
119 interface_add(iface, config);
123 * need to look up the interface name again, in case of config update,
124 * the pointer will have changed
126 iface = vlist_find(&interfaces, s->e.name, iface, node);
130 dev = iface->main_dev.dev;
131 if (!dev || !dev->default_config)
134 blob_buf_init(&b, 0);
135 uci_to_blob(&b, s, dev->type->config_params);
136 if (blob_len(b.head) == 0)
139 if (iface->device_config || dev->settings.flags)
140 device_apply_config(dev, dev->type, b.head);
149 config_parse_route(struct uci_section *s, bool v6)
153 blob_buf_init(&b, 0);
154 route = blobmsg_open_array(&b, "route");
155 uci_to_blob(&b, s, &route_attr_list);
156 blobmsg_close_array(&b, route);
157 interface_ip_add_route(NULL, blob_data(b.head), v6);
161 config_parse_rule(struct uci_section *s, bool v6)
165 blob_buf_init(&b, 0);
166 rule = blobmsg_open_array(&b, "rule");
167 uci_to_blob(&b, s, &rule_attr_list);
168 blobmsg_close_array(&b, rule);
169 iprule_add(blob_data(b.head), v6);
173 config_init_devices(void)
175 struct uci_element *e;
177 uci_foreach_element(&uci_network->sections, e) {
178 const struct uci_blob_param_list *params = NULL;
179 struct uci_section *s = uci_to_section(e);
180 const struct device_type *devtype = NULL;
182 const char *type, *name;
184 if (strcmp(s->type, "device") != 0)
187 name = uci_lookup_option_string(uci_ctx, s, "name");
191 type = uci_lookup_option_string(uci_ctx, s, "type");
193 if (!strcmp(type, "8021ad"))
194 devtype = &vlandev_device_type;
195 else if (!strcmp(type, "8021q"))
196 devtype = &vlandev_device_type;
197 else if (!strcmp(type, "bridge"))
198 devtype = &bridge_device_type;
199 else if (!strcmp(type, "macvlan"))
200 devtype = &macvlan_device_type;
201 else if (!strcmp(type, "tunnel"))
202 devtype = &tunnel_device_type;
206 params = devtype->config_params;
208 params = simple_device_type.config_params;
210 blob_buf_init(&b, 0);
211 uci_to_blob(&b, s, params);
213 dev = device_create(name, devtype, b.head);
217 dev = device_get(name, 1);
221 dev->current_config = true;
222 device_apply_config(dev, dev->type, b.head);
224 dev->default_config = false;
228 static struct uci_package *
229 config_init_package(const char *config)
231 struct uci_context *ctx = uci_ctx;
232 struct uci_package *p = NULL;
235 ctx = uci_alloc_context();
238 ctx->flags &= ~UCI_FLAG_STRICT;
240 uci_set_confdir(ctx, config_path);
243 uci_set_savedir(ctx, "./tmp");
246 p = uci_lookup_package(ctx, config);
251 if (uci_load(ctx, config, &p))
258 config_init_interfaces(void)
260 struct uci_element *e;
262 uci_foreach_element(&uci_network->sections, e) {
263 struct uci_section *s = uci_to_section(e);
265 if (!strcmp(s->type, "interface"))
266 config_parse_interface(s, false);
269 uci_foreach_element(&uci_network->sections, e) {
270 struct uci_section *s = uci_to_section(e);
272 if (!strcmp(s->type, "alias"))
273 config_parse_interface(s, true);
278 config_init_routes(void)
280 struct interface *iface;
281 struct uci_element *e;
283 vlist_for_each_element(&interfaces, iface, node)
284 interface_ip_update_start(&iface->config_ip);
286 uci_foreach_element(&uci_network->sections, e) {
287 struct uci_section *s = uci_to_section(e);
289 if (!strcmp(s->type, "route"))
290 config_parse_route(s, false);
291 else if (!strcmp(s->type, "route6"))
292 config_parse_route(s, true);
295 vlist_for_each_element(&interfaces, iface, node)
296 interface_ip_update_complete(&iface->config_ip);
300 config_init_rules(void)
302 struct uci_element *e;
304 iprule_update_start();
306 uci_foreach_element(&uci_network->sections, e) {
307 struct uci_section *s = uci_to_section(e);
309 if (!strcmp(s->type, "rule"))
310 config_parse_rule(s, false);
311 else if (!strcmp(s->type, "rule6"))
312 config_parse_rule(s, true);
315 iprule_update_complete();
319 config_init_globals(void)
321 struct uci_section *globals = uci_lookup_section(
322 uci_ctx, uci_network, "globals");
326 const char *ula_prefix = uci_lookup_option_string(
327 uci_ctx, globals, "ula_prefix");
328 interface_ip_set_ula_prefix(ula_prefix);
332 config_parse_wireless_device(struct uci_section *s)
334 struct wireless_driver *drv;
335 const char *driver_name;
337 driver_name = uci_lookup_option_string(uci_ctx, s, "type");
341 drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
345 blob_buf_init(&b, 0);
346 uci_to_blob(&b, s, drv->device.config);
347 wireless_device_create(drv, s->e.name, b.head);
351 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
355 name = alloca(strlen(s->type) + 16);
356 sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
358 blob_buf_init(&b, 0);
359 uci_to_blob(&b, s, wdev->drv->interface.config);
360 wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
364 config_init_wireless(void)
366 struct wireless_device *wdev;
367 struct uci_element *e;
368 const char *dev_name;
371 DPRINTF("No wireless configuration found\n");
375 vlist_update(&wireless_devices);
377 uci_foreach_element(&uci_wireless->sections, e) {
378 struct uci_section *s = uci_to_section(e);
379 if (strcmp(s->type, "wifi-device") != 0)
382 config_parse_wireless_device(s);
385 vlist_flush(&wireless_devices);
387 vlist_for_each_element(&wireless_devices, wdev, node) {
389 vlist_update(&wdev->interfaces);
392 uci_foreach_element(&uci_wireless->sections, e) {
393 struct uci_section *s = uci_to_section(e);
395 if (strcmp(s->type, "wifi-iface") != 0)
398 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
402 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
404 DPRINTF("device %s not found!\n", dev_name);
408 config_parse_wireless_interface(wdev, s);
411 vlist_for_each_element(&wireless_devices, wdev, node)
412 vlist_flush(&wdev->interfaces);
416 config_init_all(void)
418 uci_network = config_init_package("network");
420 fprintf(stderr, "Failed to load network config\n");
424 uci_wireless = config_init_package("wireless");
426 vlist_update(&interfaces);
430 device_reset_config();
431 config_init_devices();
432 config_init_interfaces();
433 config_init_routes();
435 config_init_globals();
436 config_init_wireless();
442 device_init_pending();
443 vlist_flush(&interfaces);
444 device_free_unused(NULL);
445 interface_refresh_assignments(false);
446 interface_start_pending();
447 wireless_start_pending();