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;
33 static bool uci_attr_to_blob(struct blob_buf *b, const char *str,
34 const char *name, enum blobmsg_type type)
40 case BLOBMSG_TYPE_STRING:
41 blobmsg_add_string(b, name, str);
43 case BLOBMSG_TYPE_BOOL:
44 if (!strcmp(str, "true") || !strcmp(str, "1"))
46 else if (!strcmp(str, "false") || !strcmp(str, "0"))
51 blobmsg_add_u8(b, name, intval);
53 case BLOBMSG_TYPE_INT32:
54 intval = strtol(str, &err, 0);
58 blobmsg_add_u32(b, name, intval);
66 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
67 enum blobmsg_type type)
69 struct uci_element *e;
70 char *str, *next, *word;
72 if (o->type == UCI_TYPE_LIST) {
73 uci_foreach_element(&o->v.list, e) {
74 uci_attr_to_blob(b, e->name, NULL, type);
79 str = strdup(o->v.string);
82 while ((word = strsep(&next, " \t")) != NULL) {
86 uci_attr_to_blob(b, word, NULL, type);
92 static int __uci_to_blob(struct blob_buf *b, struct uci_section *s,
93 const struct config_param_list *p)
95 const struct blobmsg_policy *attr = NULL;
96 struct uci_element *e;
101 uci_foreach_element(&s->options, e) {
102 for (i = 0; i < p->n_params; i++) {
103 attr = &p->params[i];
104 if (!strcmp(attr->name, e->name))
108 if (i == p->n_params)
111 o = uci_to_option(e);
113 if (attr->type == BLOBMSG_TYPE_ARRAY) {
117 array = blobmsg_open_array(b, attr->name);
118 uci_array_to_blob(b, o, p->info[i].type);
119 blobmsg_close_array(b, array);
124 if (o->type == UCI_TYPE_LIST)
127 ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
133 static int uci_to_blob(struct blob_buf *b, struct uci_section *s,
134 const struct config_param_list *p)
139 ret += __uci_to_blob(b, s, p);
140 for (i = 0; i < p->n_next; i++)
141 ret += uci_to_blob(b, s, p->next[i]);
147 config_parse_bridge_interface(struct uci_section *s)
151 name = alloca(strlen(s->e.name) + 4);
152 sprintf(name, "br-%s", s->e.name);
153 blobmsg_add_string(&b, "name", name);
155 uci_to_blob(&b, s, bridge_device_type.config_params);
156 if (!device_create(name, &bridge_device_type, b.head)) {
157 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
161 blob_buf_init(&b, 0);
162 blobmsg_add_string(&b, "ifname", name);
167 config_parse_interface(struct uci_section *s, bool alias)
169 struct interface *iface;
170 const char *type = NULL;
171 struct blob_attr *config;
175 blob_buf_init(&b, 0);
178 type = uci_lookup_option_string(uci_ctx, s, "type");
179 if (type && !strcmp(type, "bridge")) {
180 if (config_parse_bridge_interface(s))
186 uci_to_blob(&b, s, &interface_attr_list);
187 iface = calloc(1, sizeof(*iface));
191 interface_init(iface, s->e.name, b.head);
193 if (iface->proto_handler && iface->proto_handler->config_params)
194 uci_to_blob(&b, s, iface->proto_handler->config_params);
196 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
197 iface->device_config = true;
199 config = malloc(blob_pad_len(b.head));
203 memcpy(config, b.head, blob_pad_len(b.head));
206 if (!interface_add_alias(iface, config))
207 goto error_free_config;
209 interface_add(iface, config);
213 * need to look up the interface name again, in case of config update,
214 * the pointer will have changed
216 iface = vlist_find(&interfaces, s->e.name, iface, node);
220 dev = iface->main_dev.dev;
221 if (!dev || !dev->default_config)
224 blob_buf_init(&b, 0);
225 uci_to_blob(&b, s, dev->type->config_params);
226 if (blob_len(b.head) == 0)
229 device_set_config(dev, dev->type, b.head);
238 config_parse_route(struct uci_section *s, bool v6)
242 blob_buf_init(&b, 0);
243 route = blobmsg_open_array(&b, "route");
244 uci_to_blob(&b, s, &route_attr_list);
245 blobmsg_close_array(&b, route);
246 interface_ip_add_route(NULL, blob_data(b.head), v6);
250 config_parse_rule(struct uci_section *s, bool v6)
254 blob_buf_init(&b, 0);
255 rule = blobmsg_open_array(&b, "rule");
256 uci_to_blob(&b, s, &rule_attr_list);
257 blobmsg_close_array(&b, rule);
258 iprule_add(blob_data(b.head), v6);
262 config_init_devices(void)
264 struct uci_element *e;
266 uci_foreach_element(&uci_network->sections, e) {
267 struct uci_section *s = uci_to_section(e);
268 const struct device_type *devtype = NULL;
269 const char *type, *name;
271 if (strcmp(s->type, "device") != 0)
274 name = uci_lookup_option_string(uci_ctx, s, "name");
278 type = uci_lookup_option_string(uci_ctx, s, "type");
280 if (!strcmp(type, "bridge"))
281 devtype = &bridge_device_type;
282 else if (!strcmp(type, "tunnel"))
283 devtype = &tunnel_device_type;
287 devtype = &simple_device_type;
289 blob_buf_init(&b, 0);
290 uci_to_blob(&b, s, devtype->config_params);
291 device_create(name, devtype, b.head);
296 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
297 const struct config_param_list *config, unsigned long *diff)
302 for (i = 0; i < config->n_params; i++) {
303 if (!tb1[i] && !tb2[i])
306 if (!!tb1[i] != !!tb2[i])
309 if (blob_len(tb1[i]) != blob_len(tb2[i]))
312 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
330 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
331 const struct config_param_list *config)
333 struct blob_attr **tb1, **tb2;
341 tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
342 blobmsg_parse(config->params, config->n_params, tb1,
343 blob_data(c1), blob_len(c1));
345 tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
346 blobmsg_parse(config->params, config->n_params, tb2,
347 blob_data(c2), blob_len(c2));
349 return !config_diff(tb1, tb2, config, NULL);
353 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
354 const struct config_param_list *config)
358 if (!__config_check_equal(c1, c2, config))
361 for (i = 0; i < config->n_next; i++) {
362 if (!__config_check_equal(c1, c2, config->next[i]))
370 config_memdup(struct blob_attr *attr)
372 struct blob_attr *ret;
373 int size = blob_pad_len(attr);
379 memcpy(ret, attr, size);
383 static struct uci_package *
384 config_init_package(const char *config)
386 struct uci_context *ctx = uci_ctx;
387 struct uci_package *p = NULL;
390 ctx = uci_alloc_context();
394 uci_set_confdir(ctx, "./config");
395 uci_set_savedir(ctx, "./tmp");
398 p = uci_lookup_package(ctx, config);
403 if (uci_load(ctx, config, &p))
410 config_init_interfaces(void)
412 struct uci_element *e;
414 uci_foreach_element(&uci_network->sections, e) {
415 struct uci_section *s = uci_to_section(e);
417 if (!strcmp(s->type, "interface"))
418 config_parse_interface(s, false);
421 uci_foreach_element(&uci_network->sections, e) {
422 struct uci_section *s = uci_to_section(e);
424 if (!strcmp(s->type, "alias"))
425 config_parse_interface(s, true);
430 config_init_routes(void)
432 struct interface *iface;
433 struct uci_element *e;
435 vlist_for_each_element(&interfaces, iface, node)
436 interface_ip_update_start(&iface->config_ip);
438 uci_foreach_element(&uci_network->sections, e) {
439 struct uci_section *s = uci_to_section(e);
441 if (!strcmp(s->type, "route"))
442 config_parse_route(s, false);
443 else if (!strcmp(s->type, "route6"))
444 config_parse_route(s, true);
447 vlist_for_each_element(&interfaces, iface, node)
448 interface_ip_update_complete(&iface->config_ip);
452 config_init_rules(void)
454 struct uci_element *e;
456 iprule_update_start();
458 uci_foreach_element(&uci_network->sections, e) {
459 struct uci_section *s = uci_to_section(e);
461 if (!strcmp(s->type, "rule"))
462 config_parse_rule(s, false);
463 else if (!strcmp(s->type, "rule6"))
464 config_parse_rule(s, true);
467 iprule_update_complete();
471 config_init_globals(void)
473 struct uci_section *globals = uci_lookup_section(
474 uci_ctx, uci_network, "globals");
478 const char *ula_prefix = uci_lookup_option_string(
479 uci_ctx, globals, "ula_prefix");
480 interface_ip_set_ula_prefix(ula_prefix);
484 config_init_all(void)
486 uci_network = config_init_package("network");
488 fprintf(stderr, "Failed to load network config\n");
492 vlist_update(&interfaces);
496 device_reset_config();
497 config_init_devices();
498 config_init_interfaces();
499 config_init_routes();
501 config_init_globals();
507 device_init_pending();
508 vlist_flush(&interfaces);
509 device_free_unused(NULL);
510 interface_refresh_assignments(false);
511 interface_start_pending();