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