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 void 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
);
64 static void uci_array_to_blob(struct blob_buf
*b
, struct uci_option
*o
,
65 enum blobmsg_type type
)
67 struct uci_element
*e
;
68 char *str
, *next
, *word
;
70 if (o
->type
== UCI_TYPE_LIST
) {
71 uci_foreach_element(&o
->v
.list
, e
) {
72 uci_attr_to_blob(b
, e
->name
, NULL
, type
);
77 str
= strdup(o
->v
.string
);
80 while ((word
= strsep(&next
, " \t")) != NULL
) {
84 uci_attr_to_blob(b
, word
, NULL
, type
);
90 static void __uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
91 const struct config_param_list
*p
)
93 const struct blobmsg_policy
*attr
= NULL
;
94 struct uci_element
*e
;
99 uci_foreach_element(&s
->options
, e
) {
100 for (i
= 0; i
< p
->n_params
; i
++) {
101 attr
= &p
->params
[i
];
102 if (!strcmp(attr
->name
, e
->name
))
106 if (i
== p
->n_params
)
109 o
= uci_to_option(e
);
111 if (attr
->type
== BLOBMSG_TYPE_ARRAY
) {
115 array
= blobmsg_open_array(b
, attr
->name
);
116 uci_array_to_blob(b
, o
, p
->info
[i
].type
);
117 blobmsg_close_array(b
, array
);
121 if (o
->type
== UCI_TYPE_LIST
)
124 uci_attr_to_blob(b
, o
->v
.string
, attr
->name
, attr
->type
);
128 static void uci_to_blob(struct blob_buf
*b
, struct uci_section
*s
,
129 const struct config_param_list
*p
)
133 __uci_to_blob(b
, s
, p
);
134 for (i
= 0; i
< p
->n_next
; i
++)
135 uci_to_blob(b
, s
, p
->next
[i
]);
139 config_parse_bridge_interface(struct uci_section
*s
)
143 name
= alloca(strlen(s
->e
.name
) + 4);
144 sprintf(name
, "br-%s", s
->e
.name
);
145 blobmsg_add_string(&b
, "name", name
);
147 uci_to_blob(&b
, s
, bridge_device_type
.config_params
);
148 if (!device_create(name
, &bridge_device_type
, b
.head
)) {
149 D(INTERFACE
, "Failed to create bridge for interface '%s'\n", s
->e
.name
);
153 blob_buf_init(&b
, 0);
154 blobmsg_add_string(&b
, "ifname", name
);
159 config_parse_interface(struct uci_section
*s
, bool alias
)
161 struct interface
*iface
;
162 const char *type
= NULL
;
163 struct blob_attr
*config
;
166 blob_buf_init(&b
, 0);
169 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
170 if (type
&& !strcmp(type
, "bridge"))
171 if (config_parse_bridge_interface(s
))
174 uci_to_blob(&b
, s
, &interface_attr_list
);
175 iface
= calloc(1, sizeof(*iface
));
179 interface_init(iface
, s
->e
.name
, b
.head
);
181 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
182 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
184 config
= malloc(blob_pad_len(b
.head
));
188 memcpy(config
, b
.head
, blob_pad_len(b
.head
));
191 if (!interface_add_alias(iface
, config
))
192 goto error_free_config
;
194 interface_add(iface
, config
);
198 * need to look up the interface name again, in case of config update,
199 * the pointer will have changed
201 iface
= vlist_find(&interfaces
, s
->e
.name
, iface
, node
);
205 dev
= iface
->main_dev
.dev
;
206 if (!dev
|| !dev
->default_config
)
209 blob_buf_init(&b
, 0);
210 uci_to_blob(&b
, s
, dev
->type
->config_params
);
211 if (blob_len(b
.head
) == 0)
214 device_set_config(dev
, dev
->type
, b
.head
);
223 config_parse_route(struct uci_section
*s
, bool v6
)
227 blob_buf_init(&b
, 0);
228 route
= blobmsg_open_array(&b
, "route");
229 uci_to_blob(&b
, s
, &route_attr_list
);
230 blobmsg_close_array(&b
, route
);
231 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
235 config_init_devices(void)
237 struct uci_element
*e
;
239 uci_foreach_element(&uci_network
->sections
, e
) {
240 struct uci_section
*s
= uci_to_section(e
);
241 const struct device_type
*devtype
= NULL
;
242 const char *type
, *name
;
244 if (strcmp(s
->type
, "device") != 0)
247 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
251 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
253 if (!strcmp(type
, "bridge"))
254 devtype
= &bridge_device_type
;
255 else if (!strcmp(type
, "tunnel"))
256 devtype
= &tunnel_device_type
;
260 devtype
= &simple_device_type
;
262 blob_buf_init(&b
, 0);
263 uci_to_blob(&b
, s
, devtype
->config_params
);
264 device_create(name
, devtype
, b
.head
);
269 config_diff(struct blob_attr
**tb1
, struct blob_attr
**tb2
,
270 const struct config_param_list
*config
, unsigned long *diff
)
275 for (i
= 0; i
< config
->n_params
; i
++) {
276 if (!tb1
[i
] && !tb2
[i
])
279 if (!!tb1
[i
] != !!tb2
[i
])
282 if (blob_len(tb1
[i
]) != blob_len(tb2
[i
]))
285 if (memcmp(tb1
[i
], tb2
[i
], blob_raw_len(tb1
[i
])) != 0)
303 __config_check_equal(struct blob_attr
*c1
, struct blob_attr
*c2
,
304 const struct config_param_list
*config
)
306 struct blob_attr
**tb1
, **tb2
;
314 tb1
= alloca(config
->n_params
* sizeof(struct blob_attr
*));
315 blobmsg_parse(config
->params
, config
->n_params
, tb1
,
316 blob_data(c1
), blob_len(c1
));
318 tb2
= alloca(config
->n_params
* sizeof(struct blob_attr
*));
319 blobmsg_parse(config
->params
, config
->n_params
, tb2
,
320 blob_data(c2
), blob_len(c2
));
322 return !config_diff(tb1
, tb2
, config
, NULL
);
326 config_check_equal(struct blob_attr
*c1
, struct blob_attr
*c2
,
327 const struct config_param_list
*config
)
331 if (!__config_check_equal(c1
, c2
, config
))
334 for (i
= 0; i
< config
->n_next
; i
++) {
335 if (!__config_check_equal(c1
, c2
, config
->next
[i
]))
343 config_memdup(struct blob_attr
*attr
)
345 struct blob_attr
*ret
;
346 int size
= blob_pad_len(attr
);
352 memcpy(ret
, attr
, size
);
356 static struct uci_package
*
357 config_init_package(const char *config
)
359 struct uci_context
*ctx
= uci_ctx
;
360 struct uci_package
*p
= NULL
;
363 ctx
= uci_alloc_context();
367 uci_set_confdir(ctx
, "./config");
368 uci_set_savedir(ctx
, "./tmp");
371 p
= uci_lookup_package(ctx
, config
);
376 if (uci_load(ctx
, config
, &p
))
383 config_init_interfaces(void)
385 struct uci_element
*e
;
387 uci_foreach_element(&uci_network
->sections
, e
) {
388 struct uci_section
*s
= uci_to_section(e
);
390 if (!strcmp(s
->type
, "interface"))
391 config_parse_interface(s
, false);
394 uci_foreach_element(&uci_network
->sections
, e
) {
395 struct uci_section
*s
= uci_to_section(e
);
397 if (!strcmp(s
->type
, "alias"))
398 config_parse_interface(s
, true);
403 config_init_routes(void)
405 struct interface
*iface
;
406 struct uci_element
*e
;
408 vlist_for_each_element(&interfaces
, iface
, node
)
409 interface_ip_update_start(&iface
->config_ip
);
411 uci_foreach_element(&uci_network
->sections
, e
) {
412 struct uci_section
*s
= uci_to_section(e
);
414 if (!strcmp(s
->type
, "route"))
415 config_parse_route(s
, false);
416 else if (!strcmp(s
->type
, "route6"))
417 config_parse_route(s
, true);
420 vlist_for_each_element(&interfaces
, iface
, node
)
421 interface_ip_update_complete(&iface
->config_ip
);
425 config_init_all(void)
427 uci_network
= config_init_package("network");
429 fprintf(stderr
, "Failed to load network config\n");
433 vlist_update(&interfaces
);
437 device_reset_config();
438 config_init_devices();
439 config_init_interfaces();
440 config_init_routes();
446 device_init_pending();
447 device_free_unused(NULL
);
448 vlist_flush(&interfaces
);
449 interface_start_pending();