9 #include "interface-ip.h"
13 bool config_init = false;
15 static struct uci_context *uci_ctx;
16 static struct uci_package *uci_network;
17 static struct blob_buf b;
19 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
20 const char *name, enum blobmsg_type type)
26 case BLOBMSG_TYPE_STRING:
27 blobmsg_add_string(b, name, str);
29 case BLOBMSG_TYPE_BOOL:
30 if (!strcmp(str, "true") || !strcmp(str, "1"))
32 else if (!strcmp(str, "false") || !strcmp(str, "0"))
37 blobmsg_add_u8(b, name, intval);
39 case BLOBMSG_TYPE_INT32:
40 intval = strtol(str, &err, 0);
44 blobmsg_add_u32(b, name, intval);
51 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
52 enum blobmsg_type type)
54 struct uci_element *e;
55 char *str, *next, *word;
57 if (o->type == UCI_TYPE_LIST) {
58 uci_foreach_element(&o->v.list, e) {
59 uci_attr_to_blob(b, e->name, NULL, type);
64 str = strdup(o->v.string);
67 while ((word = strsep(&next, " \t")) != NULL) {
71 uci_attr_to_blob(b, word, NULL, type);
77 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
78 const struct config_param_list *p)
80 const struct blobmsg_policy *attr = NULL;
81 struct uci_element *e;
86 uci_foreach_element(&s->options, e) {
87 for (i = 0; i < p->n_params; i++) {
89 if (!strcmp(attr->name, e->name))
98 if (attr->type == BLOBMSG_TYPE_ARRAY) {
102 array = blobmsg_open_array(b, attr->name);
103 uci_array_to_blob(b, o, p->info[i].type);
104 blobmsg_close_array(b, array);
108 if (o->type == UCI_TYPE_LIST)
111 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
115 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
116 const struct config_param_list *p)
120 __uci_to_blob(b, s, p);
121 for (i = 0; i < p->n_next; i++)
122 uci_to_blob(b, s, p->next[i]);
126 config_parse_bridge_interface(struct uci_section *s)
130 name = alloca(strlen(s->e.name) + 4);
131 sprintf(name, "br-%s", s->e.name);
132 blobmsg_add_string(&b, "name", name);
134 uci_to_blob(&b, s, bridge_device_type.config_params);
135 if (!device_create(name, &bridge_device_type, b.head)) {
136 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
140 blob_buf_init(&b, 0);
141 blobmsg_add_string(&b, "ifname", name);
146 config_parse_interface(struct uci_section *s)
148 struct interface *iface;
150 struct blob_attr *config;
153 blob_buf_init(&b, 0);
155 type = uci_lookup_option_string(uci_ctx, s, "type");
156 if (type && !strcmp(type, "bridge"))
157 if (config_parse_bridge_interface(s))
160 uci_to_blob(&b, s, &interface_attr_list);
161 iface = calloc(1, sizeof(*iface));
165 interface_init(iface, s->e.name, b.head);
167 if (iface->proto_handler && iface->proto_handler->config_params)
168 uci_to_blob(&b, s, iface->proto_handler->config_params);
170 config = malloc(blob_pad_len(b.head));
176 memcpy(config, b.head, blob_pad_len(b.head));
177 interface_add(iface, config);
180 * need to look up the interface name again, in case of config update,
181 * the pointer will have changed
183 iface = vlist_find(&interfaces, s->e.name, iface, node);
187 dev = iface->main_dev.dev;
188 if (!dev || !dev->default_config)
191 blob_buf_init(&b, 0);
192 uci_to_blob(&b, s, dev->type->config_params);
193 if (blob_len(b.head) == 0)
196 device_set_config(dev, dev->type, b.head);
200 config_parse_route(struct uci_section *s, bool v6)
204 blob_buf_init(&b, 0);
205 route = blobmsg_open_array(&b, "route");
206 uci_to_blob(&b, s, &route_attr_list);
207 blobmsg_close_array(&b, route);
208 interface_ip_add_route(NULL, blob_data(b.head), v6);
212 config_init_devices(void)
214 struct uci_element *e;
216 uci_foreach_element(&uci_network->sections, e) {
217 struct uci_section *s = uci_to_section(e);
218 const struct device_type *devtype;
219 const char *type, *name;
221 if (strcmp(s->type, "device") != 0)
224 name = uci_lookup_option_string(uci_ctx, s, "name");
228 type = uci_lookup_option_string(uci_ctx, s, "type");
229 if (type && !strcmp(type, "bridge"))
230 devtype = &bridge_device_type;
232 devtype = &simple_device_type;
234 blob_buf_init(&b, 0);
235 uci_to_blob(&b, s, devtype->config_params);
236 device_create(name, devtype, b.head);
241 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
242 const struct config_param_list *config, unsigned long *diff)
247 for (i = 0; i < config->n_params; i++) {
248 if (!tb1[i] && !tb2[i])
251 if (!!tb1[i] != !!tb2[i])
254 if (blob_len(tb1[i]) != blob_len(tb2[i]))
257 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
275 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
276 const struct config_param_list *config)
278 struct blob_attr **tb1, **tb2;
286 tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
287 blobmsg_parse(config->params, config->n_params, tb1,
288 blob_data(c1), blob_len(c1));
290 tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
291 blobmsg_parse(config->params, config->n_params, tb2,
292 blob_data(c2), blob_len(c2));
294 return !config_diff(tb1, tb2, config, NULL);
298 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
299 const struct config_param_list *config)
303 if (!__config_check_equal(c1, c2, config))
306 for (i = 0; i < config->n_next; i++) {
307 if (!__config_check_equal(c1, c2, config->next[i]))
315 config_memdup(struct blob_attr *attr)
317 struct blob_attr *ret;
318 int size = blob_pad_len(attr);
324 memcpy(ret, attr, size);
328 static struct uci_package *
329 config_init_package(const char *config)
331 struct uci_context *ctx = uci_ctx;
332 struct uci_package *p = NULL;
335 ctx = uci_alloc_context();
339 uci_set_confdir(ctx, "./config");
340 uci_set_savedir(ctx, "./tmp");
343 p = uci_lookup_package(ctx, config);
348 if (uci_load(ctx, config, &p))
355 config_init_interfaces(void)
357 struct uci_element *e;
359 uci_foreach_element(&uci_network->sections, e) {
360 struct uci_section *s = uci_to_section(e);
362 if (!strcmp(s->type, "interface"))
363 config_parse_interface(s);
368 config_init_routes(void)
370 struct interface *iface;
371 struct uci_element *e;
373 vlist_for_each_element(&interfaces, iface, node)
374 interface_ip_update_start(&iface->config_ip);
376 uci_foreach_element(&uci_network->sections, e) {
377 struct uci_section *s = uci_to_section(e);
379 if (!strcmp(s->type, "route"))
380 config_parse_route(s, false);
381 else if (!strcmp(s->type, "route6"))
382 config_parse_route(s, true);
385 vlist_for_each_element(&interfaces, iface, node)
386 interface_ip_update_complete(&iface->config_ip);
390 config_init_all(void)
392 uci_network = config_init_package("network");
394 fprintf(stderr, "Failed to load network config\n");
398 vlist_update(&interfaces);
402 device_reset_config();
403 config_init_devices();
404 config_init_interfaces();
405 config_init_routes();
411 device_init_pending();
412 device_free_unused(NULL);
413 vlist_flush(&interfaces);
414 interface_start_pending();