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"
26 bool config_init = false;
28 static struct uci_context *uci_ctx;
29 static struct uci_package *uci_network;
30 static struct blob_buf b;
32 static bool uci_attr_to_blob(struct blob_buf *b, const char *str,
33 const char *name, enum blobmsg_type type)
39 case BLOBMSG_TYPE_STRING:
40 blobmsg_add_string(b, name, str);
42 case BLOBMSG_TYPE_BOOL:
43 if (!strcmp(str, "true") || !strcmp(str, "1"))
45 else if (!strcmp(str, "false") || !strcmp(str, "0"))
50 blobmsg_add_u8(b, name, intval);
52 case BLOBMSG_TYPE_INT32:
53 intval = strtol(str, &err, 0);
57 blobmsg_add_u32(b, name, intval);
65 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
66 enum blobmsg_type type)
68 struct uci_element *e;
69 char *str, *next, *word;
71 if (o->type == UCI_TYPE_LIST) {
72 uci_foreach_element(&o->v.list, e) {
73 uci_attr_to_blob(b, e->name, NULL, type);
78 str = strdup(o->v.string);
81 while ((word = strsep(&next, " \t")) != NULL) {
85 uci_attr_to_blob(b, word, NULL, type);
91 static int __uci_to_blob(struct blob_buf *b, struct uci_section *s,
92 const struct config_param_list *p)
94 const struct blobmsg_policy *attr = NULL;
95 struct uci_element *e;
100 uci_foreach_element(&s->options, e) {
101 for (i = 0; i < p->n_params; i++) {
102 attr = &p->params[i];
103 if (!strcmp(attr->name, e->name))
107 if (i == p->n_params)
110 o = uci_to_option(e);
112 if (attr->type == BLOBMSG_TYPE_ARRAY) {
116 array = blobmsg_open_array(b, attr->name);
117 uci_array_to_blob(b, o, p->info[i].type);
118 blobmsg_close_array(b, array);
123 if (o->type == UCI_TYPE_LIST)
126 ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
132 static int uci_to_blob(struct blob_buf *b, struct uci_section *s,
133 const struct config_param_list *p)
138 ret += __uci_to_blob(b, s, p);
139 for (i = 0; i < p->n_next; i++)
140 ret += uci_to_blob(b, s, p->next[i]);
146 config_parse_bridge_interface(struct uci_section *s)
150 name = alloca(strlen(s->e.name) + 4);
151 sprintf(name, "br-%s", s->e.name);
152 blobmsg_add_string(&b, "name", name);
154 uci_to_blob(&b, s, bridge_device_type.config_params);
155 if (!device_create(name, &bridge_device_type, b.head)) {
156 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
160 blob_buf_init(&b, 0);
161 blobmsg_add_string(&b, "ifname", name);
166 config_parse_interface(struct uci_section *s, bool alias)
168 struct interface *iface;
169 const char *type = NULL;
170 struct blob_attr *config;
174 blob_buf_init(&b, 0);
177 type = uci_lookup_option_string(uci_ctx, s, "type");
178 if (type && !strcmp(type, "bridge")) {
179 if (config_parse_bridge_interface(s))
185 uci_to_blob(&b, s, &interface_attr_list);
186 iface = calloc(1, sizeof(*iface));
190 interface_init(iface, s->e.name, b.head);
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 = malloc(blob_pad_len(b.head));
202 memcpy(config, b.head, blob_pad_len(b.head));
205 if (!interface_add_alias(iface, config))
206 goto error_free_config;
208 interface_add(iface, config);
212 * need to look up the interface name again, in case of config update,
213 * the pointer will have changed
215 iface = vlist_find(&interfaces, s->e.name, iface, node);
219 dev = iface->main_dev.dev;
220 if (!dev || !dev->default_config)
223 blob_buf_init(&b, 0);
224 uci_to_blob(&b, s, dev->type->config_params);
225 if (blob_len(b.head) == 0)
228 device_set_config(dev, dev->type, b.head);
237 config_parse_route(struct uci_section *s, bool v6)
241 blob_buf_init(&b, 0);
242 route = blobmsg_open_array(&b, "route");
243 uci_to_blob(&b, s, &route_attr_list);
244 blobmsg_close_array(&b, route);
245 interface_ip_add_route(NULL, blob_data(b.head), v6);
249 config_init_devices(void)
251 struct uci_element *e;
253 uci_foreach_element(&uci_network->sections, e) {
254 struct uci_section *s = uci_to_section(e);
255 const struct device_type *devtype = NULL;
256 const char *type, *name;
258 if (strcmp(s->type, "device") != 0)
261 name = uci_lookup_option_string(uci_ctx, s, "name");
265 type = uci_lookup_option_string(uci_ctx, s, "type");
267 if (!strcmp(type, "bridge"))
268 devtype = &bridge_device_type;
269 else if (!strcmp(type, "tunnel"))
270 devtype = &tunnel_device_type;
274 devtype = &simple_device_type;
276 blob_buf_init(&b, 0);
277 uci_to_blob(&b, s, devtype->config_params);
278 device_create(name, devtype, b.head);
283 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
284 const struct config_param_list *config, unsigned long *diff)
289 for (i = 0; i < config->n_params; i++) {
290 if (!tb1[i] && !tb2[i])
293 if (!!tb1[i] != !!tb2[i])
296 if (blob_len(tb1[i]) != blob_len(tb2[i]))
299 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
317 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
318 const struct config_param_list *config)
320 struct blob_attr **tb1, **tb2;
328 tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
329 blobmsg_parse(config->params, config->n_params, tb1,
330 blob_data(c1), blob_len(c1));
332 tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
333 blobmsg_parse(config->params, config->n_params, tb2,
334 blob_data(c2), blob_len(c2));
336 return !config_diff(tb1, tb2, config, NULL);
340 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
341 const struct config_param_list *config)
345 if (!__config_check_equal(c1, c2, config))
348 for (i = 0; i < config->n_next; i++) {
349 if (!__config_check_equal(c1, c2, config->next[i]))
357 config_memdup(struct blob_attr *attr)
359 struct blob_attr *ret;
360 int size = blob_pad_len(attr);
366 memcpy(ret, attr, size);
370 static struct uci_package *
371 config_init_package(const char *config)
373 struct uci_context *ctx = uci_ctx;
374 struct uci_package *p = NULL;
377 ctx = uci_alloc_context();
381 uci_set_confdir(ctx, "./config");
382 uci_set_savedir(ctx, "./tmp");
385 p = uci_lookup_package(ctx, config);
390 if (uci_load(ctx, config, &p))
397 config_init_interfaces(void)
399 struct uci_element *e;
401 uci_foreach_element(&uci_network->sections, e) {
402 struct uci_section *s = uci_to_section(e);
404 if (!strcmp(s->type, "interface"))
405 config_parse_interface(s, false);
408 uci_foreach_element(&uci_network->sections, e) {
409 struct uci_section *s = uci_to_section(e);
411 if (!strcmp(s->type, "alias"))
412 config_parse_interface(s, true);
417 config_init_routes(void)
419 struct interface *iface;
420 struct uci_element *e;
422 vlist_for_each_element(&interfaces, iface, node)
423 interface_ip_update_start(&iface->config_ip);
425 uci_foreach_element(&uci_network->sections, e) {
426 struct uci_section *s = uci_to_section(e);
428 if (!strcmp(s->type, "route"))
429 config_parse_route(s, false);
430 else if (!strcmp(s->type, "route6"))
431 config_parse_route(s, true);
434 vlist_for_each_element(&interfaces, iface, node)
435 interface_ip_update_complete(&iface->config_ip);
439 config_init_globals(void)
441 struct uci_section *globals = uci_lookup_section(
442 uci_ctx, uci_network, "globals");
446 const char *ula_prefix = uci_lookup_option_string(
447 uci_ctx, globals, "ula_prefix");
448 interface_ip_set_ula_prefix(ula_prefix);
452 config_init_all(void)
454 uci_network = config_init_package("network");
456 fprintf(stderr, "Failed to load network config\n");
460 vlist_update(&interfaces);
464 device_reset_config();
465 config_init_devices();
466 config_init_interfaces();
467 config_init_routes();
468 config_init_globals();
474 device_init_pending();
475 vlist_flush(&interfaces);
476 device_free_unused(NULL);
477 interface_start_pending();