config: replace config_memdup with blob_memdup from libubox
[project/netifd.git] / config.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include <uci.h>
19
20 #include "netifd.h"
21 #include "interface.h"
22 #include "interface-ip.h"
23 #include "iprule.h"
24 #include "proto.h"
25 #include "config.h"
26
27 bool config_init = false;
28
29 static struct uci_context *uci_ctx;
30 static struct uci_package *uci_network;
31 static struct blob_buf b;
32
33 static bool uci_attr_to_blob(struct blob_buf *b, const char *str,
34 const char *name, enum blobmsg_type type)
35 {
36 char *err;
37 int intval;
38
39 switch (type) {
40 case BLOBMSG_TYPE_STRING:
41 blobmsg_add_string(b, name, str);
42 break;
43 case BLOBMSG_TYPE_BOOL:
44 if (!strcmp(str, "true") || !strcmp(str, "1"))
45 intval = 1;
46 else if (!strcmp(str, "false") || !strcmp(str, "0"))
47 intval = 0;
48 else
49 return false;
50
51 blobmsg_add_u8(b, name, intval);
52 break;
53 case BLOBMSG_TYPE_INT32:
54 intval = strtol(str, &err, 0);
55 if (*err)
56 return false;
57
58 blobmsg_add_u32(b, name, intval);
59 break;
60 default:
61 return false;
62 }
63 return true;
64 }
65
66 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
67 enum blobmsg_type type)
68 {
69 struct uci_element *e;
70 char *str, *next, *word;
71
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);
75 }
76 return;
77 }
78
79 str = strdup(o->v.string);
80 next = str;
81
82 while ((word = strsep(&next, " \t")) != NULL) {
83 if (!*word)
84 continue;
85
86 uci_attr_to_blob(b, word, NULL, type);
87 }
88
89 free(str);
90 }
91
92 static int __uci_to_blob(struct blob_buf *b, struct uci_section *s,
93 const struct config_param_list *p)
94 {
95 const struct blobmsg_policy *attr = NULL;
96 struct uci_element *e;
97 struct uci_option *o;
98 void *array;
99 int i, ret = 0;
100
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))
105 break;
106 }
107
108 if (i == p->n_params)
109 continue;
110
111 o = uci_to_option(e);
112
113 if (attr->type == BLOBMSG_TYPE_ARRAY) {
114 if (!p->info)
115 continue;
116
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);
120 ret++;
121 continue;
122 }
123
124 if (o->type == UCI_TYPE_LIST)
125 continue;
126
127 ret += uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
128 }
129
130 return ret;
131 }
132
133 static int uci_to_blob(struct blob_buf *b, struct uci_section *s,
134 const struct config_param_list *p)
135 {
136 int ret = 0;
137 int i;
138
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]);
142
143 return ret;
144 }
145
146 static int
147 config_parse_bridge_interface(struct uci_section *s)
148 {
149 char *name;
150
151 name = alloca(strlen(s->e.name) + 4);
152 sprintf(name, "br-%s", s->e.name);
153 blobmsg_add_string(&b, "name", name);
154
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);
158 return -EINVAL;
159 }
160
161 blob_buf_init(&b, 0);
162 blobmsg_add_string(&b, "ifname", name);
163 return 0;
164 }
165
166 static void
167 config_parse_interface(struct uci_section *s, bool alias)
168 {
169 struct interface *iface;
170 const char *type = NULL;
171 struct blob_attr *config;
172 struct device *dev;
173 bool bridge = false;
174
175 blob_buf_init(&b, 0);
176
177 if (!alias)
178 type = uci_lookup_option_string(uci_ctx, s, "type");
179 if (type && !strcmp(type, "bridge")) {
180 if (config_parse_bridge_interface(s))
181 return;
182
183 bridge = true;
184 }
185
186 uci_to_blob(&b, s, &interface_attr_list);
187 iface = calloc(1, sizeof(*iface));
188 if (!iface)
189 return;
190
191 interface_init(iface, s->e.name, b.head);
192
193 if (iface->proto_handler && iface->proto_handler->config_params)
194 uci_to_blob(&b, s, iface->proto_handler->config_params);
195
196 if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
197 iface->device_config = true;
198
199 config = malloc(blob_pad_len(b.head));
200 if (!config)
201 goto error;
202
203 memcpy(config, b.head, blob_pad_len(b.head));
204
205 if (alias) {
206 if (!interface_add_alias(iface, config))
207 goto error_free_config;
208 } else {
209 interface_add(iface, config);
210 }
211
212 /*
213 * need to look up the interface name again, in case of config update,
214 * the pointer will have changed
215 */
216 iface = vlist_find(&interfaces, s->e.name, iface, node);
217 if (!iface)
218 return;
219
220 dev = iface->main_dev.dev;
221 if (!dev || !dev->default_config)
222 return;
223
224 blob_buf_init(&b, 0);
225 uci_to_blob(&b, s, dev->type->config_params);
226 if (blob_len(b.head) == 0)
227 return;
228
229 device_set_config(dev, dev->type, b.head);
230 return;
231 error_free_config:
232 free(config);
233 error:
234 free(iface);
235 }
236
237 static void
238 config_parse_route(struct uci_section *s, bool v6)
239 {
240 void *route;
241
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);
247 }
248
249 static void
250 config_parse_rule(struct uci_section *s, bool v6)
251 {
252 void *rule;
253
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);
259 }
260
261 static void
262 config_init_devices(void)
263 {
264 struct uci_element *e;
265
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;
270
271 if (strcmp(s->type, "device") != 0)
272 continue;
273
274 name = uci_lookup_option_string(uci_ctx, s, "name");
275 if (!name)
276 continue;
277
278 type = uci_lookup_option_string(uci_ctx, s, "type");
279 if (type) {
280 if (!strcmp(type, "bridge"))
281 devtype = &bridge_device_type;
282 else if (!strcmp(type, "tunnel"))
283 devtype = &tunnel_device_type;
284 }
285
286 if (!devtype)
287 devtype = &simple_device_type;
288
289 blob_buf_init(&b, 0);
290 uci_to_blob(&b, s, devtype->config_params);
291 device_create(name, devtype, b.head);
292 }
293 }
294
295 bool
296 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
297 const struct config_param_list *config, unsigned long *diff)
298 {
299 bool ret = false;
300 int i;
301
302 for (i = 0; i < config->n_params; i++) {
303 if (!tb1[i] && !tb2[i])
304 continue;
305
306 if (!!tb1[i] != !!tb2[i])
307 goto mark;
308
309 if (blob_len(tb1[i]) != blob_len(tb2[i]))
310 goto mark;
311
312 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
313 goto mark;
314
315 continue;
316
317 mark:
318 ret = true;
319 if (diff)
320 set_bit(diff, i);
321 else
322 return ret;
323 }
324
325 return ret;
326 }
327
328
329 static bool
330 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
331 const struct config_param_list *config)
332 {
333 struct blob_attr **tb1, **tb2;
334
335 if (!!c1 ^ !!c2)
336 return false;
337
338 if (!c1 && !c2)
339 return true;
340
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));
344
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));
348
349 return !config_diff(tb1, tb2, config, NULL);
350 }
351
352 bool
353 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
354 const struct config_param_list *config)
355 {
356 int i;
357
358 if (!__config_check_equal(c1, c2, config))
359 return false;
360
361 for (i = 0; i < config->n_next; i++) {
362 if (!__config_check_equal(c1, c2, config->next[i]))
363 return false;
364 }
365
366 return true;
367 }
368
369 static struct uci_package *
370 config_init_package(const char *config)
371 {
372 struct uci_context *ctx = uci_ctx;
373 struct uci_package *p = NULL;
374
375 if (!ctx) {
376 ctx = uci_alloc_context();
377 uci_ctx = ctx;
378
379 #ifdef DUMMY_MODE
380 uci_set_confdir(ctx, "./config");
381 uci_set_savedir(ctx, "./tmp");
382 #endif
383 } else {
384 p = uci_lookup_package(ctx, config);
385 if (p)
386 uci_unload(ctx, p);
387 }
388
389 if (uci_load(ctx, config, &p))
390 return NULL;
391
392 return p;
393 }
394
395 static void
396 config_init_interfaces(void)
397 {
398 struct uci_element *e;
399
400 uci_foreach_element(&uci_network->sections, e) {
401 struct uci_section *s = uci_to_section(e);
402
403 if (!strcmp(s->type, "interface"))
404 config_parse_interface(s, false);
405 }
406
407 uci_foreach_element(&uci_network->sections, e) {
408 struct uci_section *s = uci_to_section(e);
409
410 if (!strcmp(s->type, "alias"))
411 config_parse_interface(s, true);
412 }
413 }
414
415 static void
416 config_init_routes(void)
417 {
418 struct interface *iface;
419 struct uci_element *e;
420
421 vlist_for_each_element(&interfaces, iface, node)
422 interface_ip_update_start(&iface->config_ip);
423
424 uci_foreach_element(&uci_network->sections, e) {
425 struct uci_section *s = uci_to_section(e);
426
427 if (!strcmp(s->type, "route"))
428 config_parse_route(s, false);
429 else if (!strcmp(s->type, "route6"))
430 config_parse_route(s, true);
431 }
432
433 vlist_for_each_element(&interfaces, iface, node)
434 interface_ip_update_complete(&iface->config_ip);
435 }
436
437 static void
438 config_init_rules(void)
439 {
440 struct uci_element *e;
441
442 iprule_update_start();
443
444 uci_foreach_element(&uci_network->sections, e) {
445 struct uci_section *s = uci_to_section(e);
446
447 if (!strcmp(s->type, "rule"))
448 config_parse_rule(s, false);
449 else if (!strcmp(s->type, "rule6"))
450 config_parse_rule(s, true);
451 }
452
453 iprule_update_complete();
454 }
455
456 static void
457 config_init_globals(void)
458 {
459 struct uci_section *globals = uci_lookup_section(
460 uci_ctx, uci_network, "globals");
461 if (!globals)
462 return;
463
464 const char *ula_prefix = uci_lookup_option_string(
465 uci_ctx, globals, "ula_prefix");
466 interface_ip_set_ula_prefix(ula_prefix);
467 }
468
469 void
470 config_init_all(void)
471 {
472 uci_network = config_init_package("network");
473 if (!uci_network) {
474 fprintf(stderr, "Failed to load network config\n");
475 return;
476 }
477
478 vlist_update(&interfaces);
479 config_init = true;
480 device_lock();
481
482 device_reset_config();
483 config_init_devices();
484 config_init_interfaces();
485 config_init_routes();
486 config_init_rules();
487 config_init_globals();
488
489 config_init = false;
490 device_unlock();
491
492 device_reset_old();
493 device_init_pending();
494 vlist_flush(&interfaces);
495 device_free_unused(NULL);
496 interface_refresh_assignments(false);
497 interface_start_pending();
498 }