zero-initialize the ifreq when setting interfaces up or down
[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 void 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;
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;
56
57                 blobmsg_add_u32(b, name, intval);
58                 break;
59         default:
60                 break;
61         }
62 }
63
64 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
65                               enum blobmsg_type type)
66 {
67         struct uci_element *e;
68         char *str, *next, *word;
69
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);
73                 }
74                 return;
75         }
76
77         str = strdup(o->v.string);
78         next = str;
79
80         while ((word = strsep(&next, " \t")) != NULL) {
81                 if (!*word)
82                         continue;
83
84                 uci_attr_to_blob(b, word, NULL, type);
85         }
86
87         free(str);
88 }
89
90 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
91                           const struct config_param_list *p)
92 {
93         const struct blobmsg_policy *attr = NULL;
94         struct uci_element *e;
95         struct uci_option *o;
96         void *array;
97         int i;
98
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))
103                                 break;
104                 }
105
106                 if (i == p->n_params)
107                         continue;
108
109                 o = uci_to_option(e);
110
111                 if (attr->type == BLOBMSG_TYPE_ARRAY) {
112                         if (!p->info)
113                                 continue;
114
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);
118                         continue;
119                 }
120
121                 if (o->type == UCI_TYPE_LIST)
122                         continue;
123
124                 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
125         }
126 }
127
128 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
129                         const struct config_param_list *p)
130 {
131         int i;
132
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]);
136 }
137
138 static int
139 config_parse_bridge_interface(struct uci_section *s)
140 {
141         char *name;
142
143         name = alloca(strlen(s->e.name) + 4);
144         sprintf(name, "br-%s", s->e.name);
145         blobmsg_add_string(&b, "name", name);
146
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);
150                 return -EINVAL;
151         }
152
153         blob_buf_init(&b, 0);
154         blobmsg_add_string(&b, "ifname", name);
155         return 0;
156 }
157
158 static void
159 config_parse_interface(struct uci_section *s, bool alias)
160 {
161         struct interface *iface;
162         const char *type = NULL;
163         struct blob_attr *config;
164         struct device *dev;
165
166         blob_buf_init(&b, 0);
167
168         if (!alias)
169                 type = uci_lookup_option_string(uci_ctx, s, "type");
170         if (type && !strcmp(type, "bridge"))
171                 if (config_parse_bridge_interface(s))
172                         return;
173
174         uci_to_blob(&b, s, &interface_attr_list);
175         iface = calloc(1, sizeof(*iface));
176         if (!iface)
177                 return;
178
179         interface_init(iface, s->e.name, b.head);
180
181         if (iface->proto_handler && iface->proto_handler->config_params)
182                 uci_to_blob(&b, s, iface->proto_handler->config_params);
183
184         config = malloc(blob_pad_len(b.head));
185         if (!config)
186                 goto error;
187
188         memcpy(config, b.head, blob_pad_len(b.head));
189
190         if (alias) {
191                 if (!interface_add_alias(iface, config))
192                         goto error_free_config;
193         } else {
194                 interface_add(iface, config);
195         }
196
197         /*
198          * need to look up the interface name again, in case of config update,
199          * the pointer will have changed
200          */
201         iface = vlist_find(&interfaces, s->e.name, iface, node);
202         if (!iface)
203                 return;
204
205         dev = iface->main_dev.dev;
206         if (!dev || !dev->default_config)
207                 return;
208
209         blob_buf_init(&b, 0);
210         uci_to_blob(&b, s, dev->type->config_params);
211         if (blob_len(b.head) == 0)
212                 return;
213
214         device_set_config(dev, dev->type, b.head);
215         return;
216 error_free_config:
217         free(config);
218 error:
219         free(iface);
220 }
221
222 static void
223 config_parse_route(struct uci_section *s, bool v6)
224 {
225         void *route;
226
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);
232 }
233
234 static void
235 config_init_devices(void)
236 {
237         struct uci_element *e;
238
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;
243
244                 if (strcmp(s->type, "device") != 0)
245                         continue;
246
247                 name = uci_lookup_option_string(uci_ctx, s, "name");
248                 if (!name)
249                         continue;
250
251                 type = uci_lookup_option_string(uci_ctx, s, "type");
252                 if (type) {
253                         if (!strcmp(type, "bridge"))
254                                 devtype = &bridge_device_type;
255                         else if (!strcmp(type, "tunnel"))
256                                 devtype = &tunnel_device_type;
257                 }
258
259                 if (!devtype)
260                         devtype = &simple_device_type;
261
262                 blob_buf_init(&b, 0);
263                 uci_to_blob(&b, s, devtype->config_params);
264                 device_create(name, devtype, b.head);
265         }
266 }
267
268 bool
269 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
270             const struct config_param_list *config, unsigned long *diff)
271 {
272         bool ret = false;
273         int i;
274
275         for (i = 0; i < config->n_params; i++) {
276                 if (!tb1[i] && !tb2[i])
277                         continue;
278
279                 if (!!tb1[i] != !!tb2[i])
280                         goto mark;
281
282                 if (blob_len(tb1[i]) != blob_len(tb2[i]))
283                         goto mark;
284
285                 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
286                         goto mark;
287
288                 continue;
289
290 mark:
291                 ret = true;
292                 if (diff)
293                         set_bit(diff, i);
294                 else
295                         return ret;
296         }
297
298         return ret;
299 }
300
301
302 static bool
303 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
304                      const struct config_param_list *config)
305 {
306         struct blob_attr **tb1, **tb2;
307
308         if (!!c1 ^ !!c2)
309                 return false;
310
311         if (!c1 && !c2)
312                 return true;
313
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));
317
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));
321
322         return !config_diff(tb1, tb2, config, NULL);
323 }
324
325 bool
326 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
327                    const struct config_param_list *config)
328 {
329         int i;
330
331         if (!__config_check_equal(c1, c2, config))
332                 return false;
333
334         for (i = 0; i < config->n_next; i++) {
335                 if (!__config_check_equal(c1, c2, config->next[i]))
336                         return false;
337         }
338
339         return true;
340 }
341
342 struct blob_attr *
343 config_memdup(struct blob_attr *attr)
344 {
345         struct blob_attr *ret;
346         int size = blob_pad_len(attr);
347
348         ret = malloc(size);
349         if (!ret)
350                 return NULL;
351
352         memcpy(ret, attr, size);
353         return ret;
354 }
355
356 static struct uci_package *
357 config_init_package(const char *config)
358 {
359         struct uci_context *ctx = uci_ctx;
360         struct uci_package *p = NULL;
361
362         if (!ctx) {
363                 ctx = uci_alloc_context();
364                 uci_ctx = ctx;
365
366 #ifdef DUMMY_MODE
367                 uci_set_confdir(ctx, "./config");
368                 uci_set_savedir(ctx, "./tmp");
369 #endif
370         } else {
371                 p = uci_lookup_package(ctx, config);
372                 if (p)
373                         uci_unload(ctx, p);
374         }
375
376         if (uci_load(ctx, config, &p))
377                 return NULL;
378
379         return p;
380 }
381
382 static void
383 config_init_interfaces(void)
384 {
385         struct uci_element *e;
386
387         uci_foreach_element(&uci_network->sections, e) {
388                 struct uci_section *s = uci_to_section(e);
389
390                 if (!strcmp(s->type, "interface"))
391                         config_parse_interface(s, false);
392         }
393
394         uci_foreach_element(&uci_network->sections, e) {
395                 struct uci_section *s = uci_to_section(e);
396
397                 if (!strcmp(s->type, "alias"))
398                         config_parse_interface(s, true);
399         }
400 }
401
402 static void
403 config_init_routes(void)
404 {
405         struct interface *iface;
406         struct uci_element *e;
407
408         vlist_for_each_element(&interfaces, iface, node)
409                 interface_ip_update_start(&iface->config_ip);
410
411         uci_foreach_element(&uci_network->sections, e) {
412                 struct uci_section *s = uci_to_section(e);
413
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);
418         }
419
420         vlist_for_each_element(&interfaces, iface, node)
421                 interface_ip_update_complete(&iface->config_ip);
422 }
423
424 void
425 config_init_all(void)
426 {
427         uci_network = config_init_package("network");
428         if (!uci_network) {
429                 fprintf(stderr, "Failed to load network config\n");
430                 return;
431         }
432
433         vlist_update(&interfaces);
434         config_init = true;
435         device_lock();
436
437         device_reset_config();
438         config_init_devices();
439         config_init_interfaces();
440         config_init_routes();
441
442         config_init = false;
443         device_unlock();
444
445         device_reset_old();
446         device_init_pending();
447         device_free_unused(NULL);
448         vlist_flush(&interfaces);
449         interface_start_pending();
450 }