proto-shell: remove the device user before issuing the protocol down event
[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                 DPRINTF("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
151         DPRINTF("Create interface '%s'\n", s->e.name);
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 static void
181 config_init_devices(void)
182 {
183         struct uci_element *e;
184
185         uci_foreach_element(&uci_network->sections, e) {
186                 struct uci_section *s = uci_to_section(e);
187                 const struct device_type *devtype;
188                 const char *type, *name;
189
190                 if (strcmp(s->type, "device") != 0)
191                         continue;
192
193                 name = uci_lookup_option_string(uci_ctx, s, "name");
194                 if (!name)
195                         continue;
196
197                 type = uci_lookup_option_string(uci_ctx, s, "type");
198                 if (type && !strcmp(type, "bridge"))
199                         devtype = &bridge_device_type;
200                 else
201                         devtype = &simple_device_type;
202
203                 blob_buf_init(&b, 0);
204                 uci_to_blob(&b, s, devtype->config_params);
205                 device_create(name, devtype, b.head);
206         }
207 }
208
209 bool
210 config_diff(struct blob_attr **tb1, struct blob_attr **tb2,
211             const struct config_param_list *config, unsigned long *diff)
212 {
213         bool ret = false;
214         int i;
215
216         for (i = 0; i < config->n_params; i++) {
217                 if (!tb1[i] && !tb2[i])
218                         continue;
219
220                 if (!!tb1[i] != !!tb2[i])
221                         goto mark;
222
223                 if (blob_len(tb1[i]) != blob_len(tb2[i]))
224                         goto mark;
225
226                 if (memcmp(tb1[i], tb2[i], blob_pad_len(tb1[i])) != 0)
227                         goto mark;
228
229                 continue;
230
231 mark:
232                 ret = true;
233                 if (diff)
234                         set_bit(diff, i);
235                 else
236                         return ret;
237         }
238
239         return ret;
240 }
241
242
243 static bool
244 __config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
245                      const struct config_param_list *config)
246 {
247         struct blob_attr **tb1, **tb2;
248
249         tb1 = alloca(config->n_params * sizeof(struct blob_attr *));
250         blobmsg_parse(config->params, config->n_params, tb1,
251                 blob_data(c1), blob_len(c1));
252
253         tb2 = alloca(config->n_params * sizeof(struct blob_attr *));
254         blobmsg_parse(config->params, config->n_params, tb2,
255                 blob_data(c2), blob_len(c2));
256
257         return !config_diff(tb1, tb2, config, NULL);
258 }
259
260 bool
261 config_check_equal(struct blob_attr *c1, struct blob_attr *c2,
262                    const struct config_param_list *config)
263 {
264         int i;
265
266         if (!__config_check_equal(c1, c2, config))
267                 return false;
268
269         for (i = 0; i < config->n_next; i++) {
270                 if (!__config_check_equal(c1, c2, config->next[i]))
271                         return false;
272         }
273
274         return true;
275 }
276
277 struct blob_attr *
278 config_memdup(struct blob_attr *attr)
279 {
280         struct blob_attr *ret;
281         int size = blob_pad_len(attr);
282
283         ret = malloc(size);
284         if (!ret)
285                 return NULL;
286
287         memcpy(ret, attr, size);
288         return ret;
289 }
290
291 static struct uci_package *
292 config_init_package(const char *config)
293 {
294         struct uci_context *ctx = uci_ctx;
295         struct uci_package *p = NULL;
296
297         if (!ctx) {
298                 ctx = uci_alloc_context();
299                 uci_ctx = ctx;
300
301 #ifdef DUMMY_MODE
302                 uci_set_confdir(ctx, "./config");
303                 uci_set_savedir(ctx, "./tmp");
304 #endif
305         } else {
306                 p = uci_lookup_package(ctx, config);
307                 if (p)
308                         uci_unload(ctx, p);
309         }
310
311         if (uci_load(ctx, "network", &p))
312                 return NULL;
313
314         return p;
315 }
316
317 void
318 config_init_interfaces(const char *name)
319 {
320         struct uci_context *ctx;
321         struct uci_package *p = NULL;
322         struct uci_element *e;
323
324         p = config_init_package("network");
325         ctx = uci_ctx;
326         if (!p) {
327                 fprintf(stderr, "Failed to load network config\n");
328                 return;
329         }
330
331         uci_network = p;
332         config_init = true;
333
334         config_init_devices();
335
336         uci_foreach_element(&p->sections, e) {
337                 struct uci_section *s = uci_to_section(e);
338
339                 if (name && strcmp(s->e.name, name) != 0)
340                         continue;
341
342                 if (!strcmp(s->type, "interface"))
343                         config_parse_interface(s);
344         }
345         device_free_unused(NULL);
346         config_init = false;
347
348         interface_start_pending();
349 }