add -Wmissing-declarations to cflags
[project/netifd.git] / config.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <uci.h>
6
7 #include "netifd.h"
8 #include "interface.h"
9 #include "proto.h"
10 #include "config.h"
11
12 bool config_init = false;
13
14 static struct uci_context *uci_ctx;
15 static struct uci_package *uci_network;
16 static struct blob_buf b;
17
18 static void uci_attr_to_blob(struct blob_buf *b, const char *str,
19                              const char *name, enum blobmsg_type type)
20 {
21         char *err;
22         int intval;
23
24         switch (type) {
25         case BLOBMSG_TYPE_STRING:
26                 blobmsg_add_string(b, name, str);
27                 break;
28         case BLOBMSG_TYPE_BOOL:
29                 if (!strcmp(str, "true") || !strcmp(str, "1"))
30                         intval = 1;
31                 else if (!strcmp(str, "false") || !strcmp(str, "0"))
32                         intval = 0;
33                 else
34                         return;
35
36                 blobmsg_add_u8(b, name, intval);
37                 break;
38         case BLOBMSG_TYPE_INT32:
39                 intval = strtol(str, &err, 0);
40                 if (*err)
41                         return;
42
43                 blobmsg_add_u32(b, name, intval);
44                 break;
45         default:
46                 break;
47         }
48 }
49
50 static void uci_array_to_blob(struct blob_buf *b, struct uci_option *o,
51                               enum blobmsg_type type)
52 {
53         struct uci_element *e;
54         char *str, *next, *word;
55
56         if (o->type == UCI_TYPE_LIST) {
57                 uci_foreach_element(&o->v.list, e) {
58                         uci_attr_to_blob(b, e->name, NULL, type);
59                 }
60                 return;
61         }
62
63         str = strdup(o->v.string);
64         next = str;
65
66         while ((word = strsep(&next, " \t")) != NULL) {
67                 if (!*word)
68                         continue;
69
70                 uci_attr_to_blob(b, word, NULL, type);
71         }
72
73         free(str);
74 }
75
76 static void __uci_to_blob(struct blob_buf *b, struct uci_section *s,
77                           const struct config_param_list *p)
78 {
79         const struct blobmsg_policy *attr = NULL;
80         struct uci_element *e;
81         struct uci_option *o;
82         void *array;
83         int i;
84
85         uci_foreach_element(&s->options, e) {
86                 for (i = 0; i < p->n_params; i++) {
87                         attr = &p->params[i];
88                         if (!strcmp(attr->name, e->name))
89                                 break;
90                 }
91
92                 if (i == p->n_params)
93                         continue;
94
95                 o = uci_to_option(e);
96
97                 if (attr->type == BLOBMSG_TYPE_ARRAY) {
98                         if (!p->info)
99                                 continue;
100
101                         array = blobmsg_open_array(b, attr->name);
102                         uci_array_to_blob(b, o, p->info[i].type);
103                         blobmsg_close_array(b, array);
104                         continue;
105                 }
106
107                 if (o->type == UCI_TYPE_LIST)
108                         continue;
109
110                 uci_attr_to_blob(b, o->v.string, attr->name, attr->type);
111         }
112 }
113
114 static void uci_to_blob(struct blob_buf *b, struct uci_section *s,
115                         const struct config_param_list *p)
116 {
117         int i;
118
119         __uci_to_blob(b, s, p);
120         for (i = 0; i < p->n_next; i++)
121                 uci_to_blob(b, s, p->next[i]);
122 }
123
124 static int
125 config_parse_bridge_interface(struct uci_section *s)
126 {
127         char *name;
128
129         name = alloca(strlen(s->e.name) + 4);
130         sprintf(name, "br-%s", s->e.name);
131         blobmsg_add_string(&b, "name", name);
132
133         uci_to_blob(&b, s, bridge_device_type.config_params);
134         if (!device_create(name, &bridge_device_type, b.head)) {
135                 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
136                 return -EINVAL;
137         }
138
139         blob_buf_init(&b, 0);
140         blobmsg_add_string(&b, "ifname", name);
141         return 0;
142 }
143
144 static void
145 config_parse_interface(struct uci_section *s)
146 {
147         struct interface *iface;
148         const char *type;
149         struct blob_attr *config;
150         struct device *dev;
151
152         blob_buf_init(&b, 0);
153
154         type = uci_lookup_option_string(uci_ctx, s, "type");
155         if (type && !strcmp(type, "bridge"))
156                 if (config_parse_bridge_interface(s))
157                         return;
158
159         uci_to_blob(&b, s, &interface_attr_list);
160         iface = calloc(1, sizeof(*iface));
161         if (!iface)
162                 return;
163
164         interface_init(iface, s->e.name, b.head);
165
166         if (iface->proto_handler && iface->proto_handler->config_params)
167                 uci_to_blob(&b, s, iface->proto_handler->config_params);
168
169         config = malloc(blob_pad_len(b.head));
170         if (!config) {
171                 free(iface);
172                 return;
173         }
174
175         memcpy(config, b.head, blob_pad_len(b.head));
176         interface_add(iface, config);
177
178         dev = iface->main_dev.dev;
179         if (!dev || !dev->default_config)
180                 return;
181
182         blob_buf_init(&b, 0);
183         uci_to_blob(&b, s, dev->type->config_params);
184         if (blob_len(b.head) == 0)
185                 return;
186
187         device_set_config(dev, dev->type, b.head);
188 }
189
190 static void
191 config_init_devices(void)
192 {
193         struct uci_element *e;
194
195         uci_foreach_element(&uci_network->sections, e) {
196                 struct uci_section *s = uci_to_section(e);
197                 const struct device_type *devtype;
198                 const char *type, *name;
199
200                 if (strcmp(s->type, "device") != 0)
201                         continue;
202
203                 name = uci_lookup_option_string(uci_ctx, s, "name");
204                 if (!name)
205                         continue;
206
207                 type = uci_lookup_option_string(uci_ctx, s, "type");
208                 if (type && !strcmp(type, "bridge"))
209                         devtype = &bridge_device_type;
210                 else
211                         devtype = &simple_device_type;
212
213                 blob_buf_init(&b, 0);
214                 uci_to_blob(&b, s, devtype->config_params);
215                 device_create(name, devtype, b.head);
216         }
217 }
218
219 bool
220 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
221             const struct config_param_list *config, unsigned long *diff)
222 {
223         bool ret = false;
224         int i;
225
226         for (i = 0; i < config->n_params; i++) {
227                 if (!tb1[i] && !tb2[i])
228                         continue;
229
230                 if (!!tb1[i] != !!tb2[i])
231                         goto mark;
232
233                 if (blob_len(tb1[i]) != blob_len(tb2[i]))
234                         goto mark;
235
236                 if (memcmp(tb1[i], tb2[i], blob_raw_len(tb1[i])) != 0)
237                         goto mark;
238
239                 continue;
240
241 mark:
242                 ret = true;
243                 if (diff)
244                         set_bit(diff, i);
245                 else
246                         return ret;
247         }
248
249         return ret;
250 }
251
252
253 static bool
254 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
255                      const struct config_param_list *config)
256 {
257         struct blob_attr **tb1, **tb2;
258
259         if (!!c1 ^ !!c2)
260                 return false;
261
262         if (!c1 && !c2)
263                 return true;
264
265         tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
266         blobmsg_parse(config->params, config->n_params, tb1,
267                 blob_data(c1), blob_len(c1));
268
269         tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
270         blobmsg_parse(config->params, config->n_params, tb2,
271                 blob_data(c2), blob_len(c2));
272
273         return !config_diff(tb1, tb2, config, NULL);
274 }
275
276 bool
277 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
278                    const struct config_param_list *config)
279 {
280         int i;
281
282         if (!__config_check_equal(c1, c2, config))
283                 return false;
284
285         for (i = 0; i < config->n_next; i++) {
286                 if (!__config_check_equal(c1, c2, config->next[i]))
287                         return false;
288         }
289
290         return true;
291 }
292
293 struct blob_attr *
294 config_memdup(struct blob_attr *attr)
295 {
296         struct blob_attr *ret;
297         int size = blob_pad_len(attr);
298
299         ret = malloc(size);
300         if (!ret)
301                 return NULL;
302
303         memcpy(ret, attr, size);
304         return ret;
305 }
306
307 static struct uci_package *
308 config_init_package(const char *config)
309 {
310         struct uci_context *ctx = uci_ctx;
311         struct uci_package *p = NULL;
312
313         if (!ctx) {
314                 ctx = uci_alloc_context();
315                 uci_ctx = ctx;
316
317 #ifdef DUMMY_MODE
318                 uci_set_confdir(ctx, "./config");
319                 uci_set_savedir(ctx, "./tmp");
320 #endif
321         } else {
322                 p = uci_lookup_package(ctx, config);
323                 if (p)
324                         uci_unload(ctx, p);
325         }
326
327         if (uci_load(ctx, "network", &p))
328                 return NULL;
329
330         return p;
331 }
332
333 void
334 config_init_all(void)
335 {
336         struct uci_package *p = NULL;
337         struct uci_element *e;
338
339         p = config_init_package("network");
340         if (!p) {
341                 fprintf(stderr, "Failed to load network config\n");
342                 return;
343         }
344
345         uci_network = p;
346         config_init = true;
347         device_lock();
348
349         device_reset_config();
350         config_init_devices();
351
352         uci_foreach_element(&p->sections, e) {
353                 struct uci_section *s = uci_to_section(e);
354
355                 if (!strcmp(s->type, "interface"))
356                         config_parse_interface(s);
357         }
358
359         config_init = false;
360         device_unlock();
361
362         device_reset_old();
363         device_init_pending();
364         device_free_unused(NULL);
365         interface_start_pending();
366 }