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