add copyright headers
[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)
160 {
161         struct interface *iface;
162         const char *type;
163         struct blob_attr *config;
164         struct device *dev;
165
166         blob_buf_init(&b, 0);
167
168         type = uci_lookup_option_string(uci_ctx, s, "type");
169         if (type && !strcmp(type, "bridge"))
170                 if (config_parse_bridge_interface(s))
171                         return;
172
173         uci_to_blob(&b, s, &interface_attr_list);
174         iface = calloc(1, sizeof(*iface));
175         if (!iface)
176                 return;
177
178         interface_init(iface, s->e.name, b.head);
179
180         if (iface->proto_handler && iface->proto_handler->config_params)
181                 uci_to_blob(&b, s, iface->proto_handler->config_params);
182
183         config = malloc(blob_pad_len(b.head));
184         if (!config) {
185                 free(iface);
186                 return;
187         }
188
189         memcpy(config, b.head, blob_pad_len(b.head));
190         interface_add(iface, config);
191
192         /*
193          * need to look up the interface name again, in case of config update,
194          * the pointer will have changed
195          */
196         iface = vlist_find(&interfaces, s->e.name, iface, node);
197         if (!iface)
198                 return;
199
200         dev = iface->main_dev.dev;
201         if (!dev || !dev->default_config)
202                 return;
203
204         blob_buf_init(&b, 0);
205         uci_to_blob(&b, s, dev->type->config_params);
206         if (blob_len(b.head) == 0)
207                 return;
208
209         device_set_config(dev, dev->type, b.head);
210 }
211
212 static void
213 config_parse_route(struct uci_section *s, bool v6)
214 {
215         void *route;
216
217         blob_buf_init(&b, 0);
218         route = blobmsg_open_array(&b, "route");
219         uci_to_blob(&b, s, &route_attr_list);
220         blobmsg_close_array(&b, route);
221         interface_ip_add_route(NULL, blob_data(b.head), v6);
222 }
223
224 static void
225 config_init_devices(void)
226 {
227         struct uci_element *e;
228
229         uci_foreach_element(&uci_network->sections, e) {
230                 struct uci_section *s = uci_to_section(e);
231                 const struct device_type *devtype = NULL;
232                 const char *type, *name;
233
234                 if (strcmp(s->type, "device") != 0)
235                         continue;
236
237                 name = uci_lookup_option_string(uci_ctx, s, "name");
238                 if (!name)
239                         continue;
240
241                 type = uci_lookup_option_string(uci_ctx, s, "type");
242                 if (type) {
243                         if (!strcmp(type, "bridge"))
244                                 devtype = &bridge_device_type;
245                         else if (!strcmp(type, "tunnel"))
246                                 devtype = &tunnel_device_type;
247                 }
248
249                 if (!devtype)
250                         devtype = &simple_device_type;
251
252                 blob_buf_init(&b, 0);
253                 uci_to_blob(&b, s, devtype->config_params);
254                 device_create(name, devtype, b.head);
255         }
256 }
257
258 bool
259 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
260             const struct config_param_list *config, unsigned long *diff)
261 {
262         bool ret = false;
263         int i;
264
265         for (i = 0; i < config->n_params; i++) {
266                 if (!tb1[i] && !tb2[i])
267                         continue;
268
269                 if (!!tb1[i] != !!tb2[i])
270                         goto mark;
271
272                 if (blob_len(tb1[i]) != blob_len(tb2[i]))
273                         goto mark;
274
275                 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
276                         goto mark;
277
278                 continue;
279
280 mark:
281                 ret = true;
282                 if (diff)
283                         set_bit(diff, i);
284                 else
285                         return ret;
286         }
287
288         return ret;
289 }
290
291
292 static bool
293 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
294                      const struct config_param_list *config)
295 {
296         struct blob_attr **tb1, **tb2;
297
298         if (!!c1 ^ !!c2)
299                 return false;
300
301         if (!c1 && !c2)
302                 return true;
303
304         tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
305         blobmsg_parse(config->params, config->n_params, tb1,
306                 blob_data(c1), blob_len(c1));
307
308         tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
309         blobmsg_parse(config->params, config->n_params, tb2,
310                 blob_data(c2), blob_len(c2));
311
312         return !config_diff(tb1, tb2, config, NULL);
313 }
314
315 bool
316 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
317                    const struct config_param_list *config)
318 {
319         int i;
320
321         if (!__config_check_equal(c1, c2, config))
322                 return false;
323
324         for (i = 0; i < config->n_next; i++) {
325                 if (!__config_check_equal(c1, c2, config->next[i]))
326                         return false;
327         }
328
329         return true;
330 }
331
332 struct blob_attr *
333 config_memdup(struct blob_attr *attr)
334 {
335         struct blob_attr *ret;
336         int size = blob_pad_len(attr);
337
338         ret = malloc(size);
339         if (!ret)
340                 return NULL;
341
342         memcpy(ret, attr, size);
343         return ret;
344 }
345
346 static struct uci_package *
347 config_init_package(const char *config)
348 {
349         struct uci_context *ctx = uci_ctx;
350         struct uci_package *p = NULL;
351
352         if (!ctx) {
353                 ctx = uci_alloc_context();
354                 uci_ctx = ctx;
355
356 #ifdef DUMMY_MODE
357                 uci_set_confdir(ctx, "./config");
358                 uci_set_savedir(ctx, "./tmp");
359 #endif
360         } else {
361                 p = uci_lookup_package(ctx, config);
362                 if (p)
363                         uci_unload(ctx, p);
364         }
365
366         if (uci_load(ctx, config, &p))
367                 return NULL;
368
369         return p;
370 }
371
372 static void
373 config_init_interfaces(void)
374 {
375         struct uci_element *e;
376
377         uci_foreach_element(&uci_network->sections, e) {
378                 struct uci_section *s = uci_to_section(e);
379
380                 if (!strcmp(s->type, "interface"))
381                         config_parse_interface(s);
382         }
383 }
384
385 static void
386 config_init_routes(void)
387 {
388         struct interface *iface;
389         struct uci_element *e;
390
391         vlist_for_each_element(&interfaces, iface, node)
392                 interface_ip_update_start(&iface->config_ip);
393
394         uci_foreach_element(&uci_network->sections, e) {
395                 struct uci_section *s = uci_to_section(e);
396
397                 if (!strcmp(s->type, "route"))
398                         config_parse_route(s, false);
399                 else if (!strcmp(s->type, "route6"))
400                         config_parse_route(s, true);
401         }
402
403         vlist_for_each_element(&interfaces, iface, node)
404                 interface_ip_update_complete(&iface->config_ip);
405 }
406
407 void
408 config_init_all(void)
409 {
410         uci_network = config_init_package("network");
411         if (!uci_network) {
412                 fprintf(stderr, "Failed to load network config\n");
413                 return;
414         }
415
416         vlist_update(&interfaces);
417         config_init = true;
418         device_lock();
419
420         device_reset_config();
421         config_init_devices();
422         config_init_interfaces();
423         config_init_routes();
424
425         config_init = false;
426         device_unlock();
427
428         device_reset_old();
429         device_init_pending();
430         device_free_unused(NULL);
431         vlist_flush(&interfaces);
432         interface_start_pending();
433 }