device: force changes to be applied via state change if necessary
[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 #define _GNU_SOURCE
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include <uci.h>
20
21 #include "netifd.h"
22 #include "interface.h"
23 #include "interface-ip.h"
24 #include "iprule.h"
25 #include "proto.h"
26 #include "wireless.h"
27 #include "config.h"
28
29 bool config_init = false;
30
31 static struct uci_context *uci_ctx;
32 static struct uci_package *uci_network;
33 static struct uci_package *uci_wireless;
34 static struct blob_buf b;
35
36 static int
37 config_section_idx(struct uci_section *s)
38 {
39         struct uci_element *e;
40         int idx = 0;
41
42         uci_foreach_element(&uci_wireless->sections, e) {
43                 struct uci_section *cur = uci_to_section(e);
44
45                 if (s == cur)
46                         return idx;
47
48                 if (!strcmp(cur->type, s->type))
49                         idx++;
50         }
51
52         return -1;
53 }
54
55 static int
56 config_parse_bridge_interface(struct uci_section *s)
57 {
58         char *name;
59
60         name = alloca(strlen(s->e.name) + 4);
61         sprintf(name, "br-%s", s->e.name);
62         blobmsg_add_string(&b, "name", name);
63
64         uci_to_blob(&b, s, bridge_device_type.config_params);
65         if (!device_create(name, &bridge_device_type, b.head)) {
66                 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
67                 return -EINVAL;
68         }
69
70         blob_buf_init(&b, 0);
71         blobmsg_add_string(&b, "ifname", name);
72         return 0;
73 }
74
75 static void
76 config_parse_interface(struct uci_section *s, bool alias)
77 {
78         struct interface *iface;
79         const char *type = NULL, *disabled;
80         struct blob_attr *config;
81         struct device *dev;
82         bool bridge = false;
83
84         disabled = uci_lookup_option_string(uci_ctx, s, "disabled");
85         if (disabled && !strcmp(disabled, "1"))
86                 return;
87
88         blob_buf_init(&b, 0);
89
90         if (!alias)
91                 type = uci_lookup_option_string(uci_ctx, s, "type");
92         if (type && !strcmp(type, "bridge")) {
93                 if (config_parse_bridge_interface(s))
94                         return;
95
96                 bridge = true;
97         }
98
99         uci_to_blob(&b, s, &interface_attr_list);
100
101         iface = interface_alloc(s->e.name, b.head);
102         if (!iface)
103                 return;
104
105         if (iface->proto_handler && iface->proto_handler->config_params)
106                 uci_to_blob(&b, s, iface->proto_handler->config_params);
107
108         if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params) > 1)
109                 iface->device_config = true;
110
111         config = blob_memdup(b.head);
112         if (!config)
113                 goto error;
114
115         if (alias) {
116                 if (!interface_add_alias(iface, config))
117                         goto error_free_config;
118         } else {
119                 interface_add(iface, config);
120         }
121
122         /*
123          * need to look up the interface name again, in case of config update,
124          * the pointer will have changed
125          */
126         iface = vlist_find(&interfaces, s->e.name, iface, node);
127         if (!iface)
128                 return;
129
130         dev = iface->main_dev.dev;
131         if (!dev || !dev->default_config)
132                 return;
133
134         blob_buf_init(&b, 0);
135         uci_to_blob(&b, s, dev->type->config_params);
136         if (blob_len(b.head) == 0)
137                 return;
138
139         if (iface->device_config && dev->default_config)
140                 device_set_config(dev, dev->type, b.head);
141         return;
142 error_free_config:
143         free(config);
144 error:
145         free(iface);
146 }
147
148 static void
149 config_parse_route(struct uci_section *s, bool v6)
150 {
151         void *route;
152
153         blob_buf_init(&b, 0);
154         route = blobmsg_open_array(&b, "route");
155         uci_to_blob(&b, s, &route_attr_list);
156         blobmsg_close_array(&b, route);
157         interface_ip_add_route(NULL, blob_data(b.head), v6);
158 }
159
160 static void
161 config_parse_rule(struct uci_section *s, bool v6)
162 {
163         void *rule;
164
165         blob_buf_init(&b, 0);
166         rule = blobmsg_open_array(&b, "rule");
167         uci_to_blob(&b, s, &rule_attr_list);
168         blobmsg_close_array(&b, rule);
169         iprule_add(blob_data(b.head), v6);
170 }
171
172 static void
173 config_init_devices(void)
174 {
175         struct uci_element *e;
176
177         uci_foreach_element(&uci_network->sections, e) {
178                 const struct uci_blob_param_list *params = NULL;
179                 struct uci_section *s = uci_to_section(e);
180                 const struct device_type *devtype = NULL;
181                 struct device *dev;
182                 const char *type, *name;
183
184                 if (strcmp(s->type, "device") != 0)
185                         continue;
186
187                 name = uci_lookup_option_string(uci_ctx, s, "name");
188                 if (!name)
189                         continue;
190
191                 type = uci_lookup_option_string(uci_ctx, s, "type");
192                 if (type) {
193                         if (!strcmp(type, "8021ad"))
194                                 devtype = &vlandev_device_type;
195                         else if (!strcmp(type, "8021q"))
196                                 devtype = &vlandev_device_type;
197                         else if (!strcmp(type, "bridge"))
198                                 devtype = &bridge_device_type;
199                         else if (!strcmp(type, "macvlan"))
200                                 devtype = &macvlan_device_type;
201                         else if (!strcmp(type, "tunnel"))
202                                 devtype = &tunnel_device_type;
203                 }
204
205                 if (devtype)
206                         params = devtype->config_params;
207                 if (!params)
208                         params = simple_device_type.config_params;
209
210                 blob_buf_init(&b, 0);
211                 uci_to_blob(&b, s, params);
212                 if (devtype) {
213                         dev = device_create(name, devtype, b.head);
214                         if (!dev)
215                                 continue;
216                 } else {
217                         dev = device_get(name, 1);
218                         if (!dev)
219                                 continue;
220
221                         device_apply_config(dev, dev->type, b.head);
222                 }
223                 dev->default_config = false;
224         }
225 }
226
227 static struct uci_package *
228 config_init_package(const char *config)
229 {
230         struct uci_context *ctx = uci_ctx;
231         struct uci_package *p = NULL;
232
233         if (!ctx) {
234                 ctx = uci_alloc_context();
235                 uci_ctx = ctx;
236
237                 ctx->flags &= ~UCI_FLAG_STRICT;
238                 if (config_path)
239                         uci_set_confdir(ctx, config_path);
240
241 #ifdef DUMMY_MODE
242                 uci_set_savedir(ctx, "./tmp");
243 #endif
244         } else {
245                 p = uci_lookup_package(ctx, config);
246                 if (p)
247                         uci_unload(ctx, p);
248         }
249
250         if (uci_load(ctx, config, &p))
251                 return NULL;
252
253         return p;
254 }
255
256 static void
257 config_init_interfaces(void)
258 {
259         struct uci_element *e;
260
261         uci_foreach_element(&uci_network->sections, e) {
262                 struct uci_section *s = uci_to_section(e);
263
264                 if (!strcmp(s->type, "interface"))
265                         config_parse_interface(s, false);
266         }
267
268         uci_foreach_element(&uci_network->sections, e) {
269                 struct uci_section *s = uci_to_section(e);
270
271                 if (!strcmp(s->type, "alias"))
272                         config_parse_interface(s, true);
273         }
274 }
275
276 static void
277 config_init_routes(void)
278 {
279         struct interface *iface;
280         struct uci_element *e;
281
282         vlist_for_each_element(&interfaces, iface, node)
283                 interface_ip_update_start(&iface->config_ip);
284
285         uci_foreach_element(&uci_network->sections, e) {
286                 struct uci_section *s = uci_to_section(e);
287
288                 if (!strcmp(s->type, "route"))
289                         config_parse_route(s, false);
290                 else if (!strcmp(s->type, "route6"))
291                         config_parse_route(s, true);
292         }
293
294         vlist_for_each_element(&interfaces, iface, node)
295                 interface_ip_update_complete(&iface->config_ip);
296 }
297
298 static void
299 config_init_rules(void)
300 {
301         struct uci_element *e;
302
303         iprule_update_start();
304
305         uci_foreach_element(&uci_network->sections, e) {
306                 struct uci_section *s = uci_to_section(e);
307
308                 if (!strcmp(s->type, "rule"))
309                         config_parse_rule(s, false);
310                 else if (!strcmp(s->type, "rule6"))
311                         config_parse_rule(s, true);
312         }
313
314         iprule_update_complete();
315 }
316
317 static void
318 config_init_globals(void)
319 {
320         struct uci_section *globals = uci_lookup_section(
321                         uci_ctx, uci_network, "globals");
322         if (!globals)
323                 return;
324
325         const char *ula_prefix = uci_lookup_option_string(
326                         uci_ctx, globals, "ula_prefix");
327         interface_ip_set_ula_prefix(ula_prefix);
328 }
329
330 static void
331 config_parse_wireless_device(struct uci_section *s)
332 {
333         struct wireless_driver *drv;
334         const char *driver_name;
335
336         driver_name = uci_lookup_option_string(uci_ctx, s, "type");
337         if (!driver_name)
338                 return;
339
340         drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
341         if (!drv)
342                 return;
343
344         blob_buf_init(&b, 0);
345         uci_to_blob(&b, s, drv->device.config);
346         wireless_device_create(drv, s->e.name, b.head);
347 }
348
349 static void
350 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
351 {
352         char *name;
353
354         name = alloca(strlen(s->type) + 16);
355         sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
356
357         blob_buf_init(&b, 0);
358         uci_to_blob(&b, s, wdev->drv->interface.config);
359         wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
360 }
361
362 static void
363 config_init_wireless(void)
364 {
365         struct wireless_device *wdev;
366         struct uci_element *e;
367         const char *dev_name;
368
369         if (!uci_wireless) {
370                 DPRINTF("No wireless configuration found\n");
371                 return;
372         }
373
374         vlist_update(&wireless_devices);
375
376         uci_foreach_element(&uci_wireless->sections, e) {
377                 struct uci_section *s = uci_to_section(e);
378                 if (strcmp(s->type, "wifi-device") != 0)
379                         continue;
380
381                 config_parse_wireless_device(s);
382         }
383
384         vlist_flush(&wireless_devices);
385
386         vlist_for_each_element(&wireless_devices, wdev, node) {
387                 wdev->vif_idx = 0;
388                 vlist_update(&wdev->interfaces);
389         }
390
391         uci_foreach_element(&uci_wireless->sections, e) {
392                 struct uci_section *s = uci_to_section(e);
393
394                 if (strcmp(s->type, "wifi-iface") != 0)
395                         continue;
396
397                 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
398                 if (!dev_name)
399                         continue;
400
401                 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
402                 if (!wdev) {
403                         DPRINTF("device %s not found!\n", dev_name);
404                         continue;
405                 }
406
407                 config_parse_wireless_interface(wdev, s);
408         }
409
410         vlist_for_each_element(&wireless_devices, wdev, node)
411                 vlist_flush(&wdev->interfaces);
412 }
413
414 void
415 config_init_all(void)
416 {
417         uci_network = config_init_package("network");
418         if (!uci_network) {
419                 fprintf(stderr, "Failed to load network config\n");
420                 return;
421         }
422
423         uci_wireless = config_init_package("wireless");
424
425         vlist_update(&interfaces);
426         config_init = true;
427         device_lock();
428
429         device_reset_config();
430         config_init_devices();
431         config_init_interfaces();
432         config_init_routes();
433         config_init_rules();
434         config_init_globals();
435         config_init_wireless();
436
437         config_init = false;
438         device_unlock();
439
440         device_reset_old();
441         device_init_pending();
442         vlist_flush(&interfaces);
443         device_free_unused(NULL);
444         interface_refresh_assignments(false);
445         interface_start_pending();
446         wireless_start_pending();
447 }