Add support for IPv6 and interface target-routes & dependencies
[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 struct blob_attr *
370 config_memdup(struct blob_attr *attr)
371 {
372         struct blob_attr *ret;
373         int size = blob_pad_len(attr);
374
375         ret = malloc(size);
376         if (!ret)
377                 return NULL;
378
379         memcpy(ret, attr, size);
380         return ret;
381 }
382
383 static struct uci_package *
384 config_init_package(const char *config)
385 {
386         struct uci_context *ctx = uci_ctx;
387         struct uci_package *p = NULL;
388
389         if (!ctx) {
390                 ctx = uci_alloc_context();
391                 uci_ctx = ctx;
392
393 #ifdef DUMMY_MODE
394                 uci_set_confdir(ctx, "./config");
395                 uci_set_savedir(ctx, "./tmp");
396 #endif
397         } else {
398                 p = uci_lookup_package(ctx, config);
399                 if (p)
400                         uci_unload(ctx, p);
401         }
402
403         if (uci_load(ctx, config, &p))
404                 return NULL;
405
406         return p;
407 }
408
409 static void
410 config_init_interfaces(void)
411 {
412         struct uci_element *e;
413
414         uci_foreach_element(&uci_network->sections, e) {
415                 struct uci_section *s = uci_to_section(e);
416
417                 if (!strcmp(s->type, "interface"))
418                         config_parse_interface(s, false);
419         }
420
421         uci_foreach_element(&uci_network->sections, e) {
422                 struct uci_section *s = uci_to_section(e);
423
424                 if (!strcmp(s->type, "alias"))
425                         config_parse_interface(s, true);
426         }
427 }
428
429 static void
430 config_init_routes(void)
431 {
432         struct interface *iface;
433         struct uci_element *e;
434
435         vlist_for_each_element(&interfaces, iface, node)
436                 interface_ip_update_start(&iface->config_ip);
437
438         uci_foreach_element(&uci_network->sections, e) {
439                 struct uci_section *s = uci_to_section(e);
440
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);
445         }
446
447         vlist_for_each_element(&interfaces, iface, node)
448                 interface_ip_update_complete(&iface->config_ip);
449 }
450
451 static void
452 config_init_rules(void)
453 {
454         struct uci_element *e;
455
456         iprule_update_start();
457
458         uci_foreach_element(&uci_network->sections, e) {
459                 struct uci_section *s = uci_to_section(e);
460
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);
465         }
466
467         iprule_update_complete();
468 }
469
470 static void
471 config_init_globals(void)
472 {
473         struct uci_section *globals = uci_lookup_section(
474                         uci_ctx, uci_network, "globals");
475         if (!globals)
476                 return;
477
478         const char *ula_prefix = uci_lookup_option_string(
479                         uci_ctx, globals, "ula_prefix");
480         interface_ip_set_ula_prefix(ula_prefix);
481 }
482
483 void
484 config_init_all(void)
485 {
486         uci_network = config_init_package("network");
487         if (!uci_network) {
488                 fprintf(stderr, "Failed to load network config\n");
489                 return;
490         }
491
492         vlist_update(&interfaces);
493         config_init = true;
494         device_lock();
495
496         device_reset_config();
497         config_init_devices();
498         config_init_interfaces();
499         config_init_routes();
500         config_init_rules();
501         config_init_globals();
502
503         config_init = false;
504         device_unlock();
505
506         device_reset_old();
507         device_init_pending();
508         vlist_flush(&interfaces);
509         device_free_unused(NULL);
510         interface_refresh_assignments(false);
511         interface_start_pending();
512 }