device: apply config changes from device sections
[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))
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         device_set_config(dev, dev->type, b.head);
140         return;
141 error_free_config:
142         free(config);
143 error:
144         free(iface);
145 }
146
147 static void
148 config_parse_route(struct uci_section *s, bool v6)
149 {
150         void *route;
151
152         blob_buf_init(&b, 0);
153         route = blobmsg_open_array(&b, "route");
154         uci_to_blob(&b, s, &route_attr_list);
155         blobmsg_close_array(&b, route);
156         interface_ip_add_route(NULL, blob_data(b.head), v6);
157 }
158
159 static void
160 config_parse_rule(struct uci_section *s, bool v6)
161 {
162         void *rule;
163
164         blob_buf_init(&b, 0);
165         rule = blobmsg_open_array(&b, "rule");
166         uci_to_blob(&b, s, &rule_attr_list);
167         blobmsg_close_array(&b, rule);
168         iprule_add(blob_data(b.head), v6);
169 }
170
171 static void
172 config_init_devices(void)
173 {
174         struct uci_element *e;
175
176         uci_foreach_element(&uci_network->sections, e) {
177                 const struct uci_blob_param_list *params = NULL;
178                 struct uci_section *s = uci_to_section(e);
179                 const struct device_type *devtype = NULL;
180                 struct device *dev;
181                 const char *type, *name;
182
183                 if (strcmp(s->type, "device") != 0)
184                         continue;
185
186                 name = uci_lookup_option_string(uci_ctx, s, "name");
187                 if (!name)
188                         continue;
189
190                 type = uci_lookup_option_string(uci_ctx, s, "type");
191                 if (type) {
192                         if (!strcmp(type, "8021ad"))
193                                 devtype = &vlandev_device_type;
194                         else if (!strcmp(type, "8021q"))
195                                 devtype = &vlandev_device_type;
196                         else if (!strcmp(type, "bridge"))
197                                 devtype = &bridge_device_type;
198                         else if (!strcmp(type, "macvlan"))
199                                 devtype = &macvlan_device_type;
200                         else if (!strcmp(type, "tunnel"))
201                                 devtype = &tunnel_device_type;
202                 }
203
204                 if (devtype)
205                         params = devtype->config_params;
206                 if (!params)
207                         params = simple_device_type.config_params;
208
209                 blob_buf_init(&b, 0);
210                 uci_to_blob(&b, s, params);
211                 if (devtype) {
212                         dev = device_create(name, devtype, b.head);
213                         if (!dev)
214                                 continue;
215                 } else {
216                         dev = device_get(name, 1);
217                         if (!dev)
218                                 continue;
219
220                         device_apply_config(dev, dev->type, blob_memdup(b.head));
221                 }
222                 dev->default_config = false;
223         }
224 }
225
226 static struct uci_package *
227 config_init_package(const char *config)
228 {
229         struct uci_context *ctx = uci_ctx;
230         struct uci_package *p = NULL;
231
232         if (!ctx) {
233                 ctx = uci_alloc_context();
234                 uci_ctx = ctx;
235
236                 ctx->flags &= ~UCI_FLAG_STRICT;
237                 if (config_path)
238                         uci_set_confdir(ctx, config_path);
239
240 #ifdef DUMMY_MODE
241                 uci_set_savedir(ctx, "./tmp");
242 #endif
243         } else {
244                 p = uci_lookup_package(ctx, config);
245                 if (p)
246                         uci_unload(ctx, p);
247         }
248
249         if (uci_load(ctx, config, &p))
250                 return NULL;
251
252         return p;
253 }
254
255 static void
256 config_init_interfaces(void)
257 {
258         struct uci_element *e;
259
260         uci_foreach_element(&uci_network->sections, e) {
261                 struct uci_section *s = uci_to_section(e);
262
263                 if (!strcmp(s->type, "interface"))
264                         config_parse_interface(s, false);
265         }
266
267         uci_foreach_element(&uci_network->sections, e) {
268                 struct uci_section *s = uci_to_section(e);
269
270                 if (!strcmp(s->type, "alias"))
271                         config_parse_interface(s, true);
272         }
273 }
274
275 static void
276 config_init_routes(void)
277 {
278         struct interface *iface;
279         struct uci_element *e;
280
281         vlist_for_each_element(&interfaces, iface, node)
282                 interface_ip_update_start(&iface->config_ip);
283
284         uci_foreach_element(&uci_network->sections, e) {
285                 struct uci_section *s = uci_to_section(e);
286
287                 if (!strcmp(s->type, "route"))
288                         config_parse_route(s, false);
289                 else if (!strcmp(s->type, "route6"))
290                         config_parse_route(s, true);
291         }
292
293         vlist_for_each_element(&interfaces, iface, node)
294                 interface_ip_update_complete(&iface->config_ip);
295 }
296
297 static void
298 config_init_rules(void)
299 {
300         struct uci_element *e;
301
302         iprule_update_start();
303
304         uci_foreach_element(&uci_network->sections, e) {
305                 struct uci_section *s = uci_to_section(e);
306
307                 if (!strcmp(s->type, "rule"))
308                         config_parse_rule(s, false);
309                 else if (!strcmp(s->type, "rule6"))
310                         config_parse_rule(s, true);
311         }
312
313         iprule_update_complete();
314 }
315
316 static void
317 config_init_globals(void)
318 {
319         struct uci_section *globals = uci_lookup_section(
320                         uci_ctx, uci_network, "globals");
321         if (!globals)
322                 return;
323
324         const char *ula_prefix = uci_lookup_option_string(
325                         uci_ctx, globals, "ula_prefix");
326         interface_ip_set_ula_prefix(ula_prefix);
327 }
328
329 static void
330 config_parse_wireless_device(struct uci_section *s)
331 {
332         struct wireless_driver *drv;
333         const char *driver_name;
334
335         driver_name = uci_lookup_option_string(uci_ctx, s, "type");
336         if (!driver_name)
337                 return;
338
339         drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
340         if (!drv)
341                 return;
342
343         blob_buf_init(&b, 0);
344         uci_to_blob(&b, s, drv->device.config);
345         wireless_device_create(drv, s->e.name, b.head);
346 }
347
348 static void
349 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
350 {
351         char *name;
352
353         name = alloca(strlen(s->type) + 16);
354         sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
355
356         blob_buf_init(&b, 0);
357         uci_to_blob(&b, s, wdev->drv->interface.config);
358         wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
359 }
360
361 static void
362 config_init_wireless(void)
363 {
364         struct wireless_device *wdev;
365         struct uci_element *e;
366         const char *dev_name;
367
368         if (!uci_wireless) {
369                 DPRINTF("No wireless configuration found\n");
370                 return;
371         }
372
373         vlist_update(&wireless_devices);
374
375         uci_foreach_element(&uci_wireless->sections, e) {
376                 struct uci_section *s = uci_to_section(e);
377                 if (strcmp(s->type, "wifi-device") != 0)
378                         continue;
379
380                 config_parse_wireless_device(s);
381         }
382
383         vlist_flush(&wireless_devices);
384
385         vlist_for_each_element(&wireless_devices, wdev, node) {
386                 wdev->vif_idx = 0;
387                 vlist_update(&wdev->interfaces);
388         }
389
390         uci_foreach_element(&uci_wireless->sections, e) {
391                 struct uci_section *s = uci_to_section(e);
392
393                 if (strcmp(s->type, "wifi-iface") != 0)
394                         continue;
395
396                 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
397                 if (!dev_name)
398                         continue;
399
400                 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
401                 if (!wdev) {
402                         DPRINTF("device %s not found!\n", dev_name);
403                         continue;
404                 }
405
406                 config_parse_wireless_interface(wdev, s);
407         }
408
409         vlist_for_each_element(&wireless_devices, wdev, node)
410                 vlist_flush(&wdev->interfaces);
411 }
412
413 void
414 config_init_all(void)
415 {
416         uci_network = config_init_package("network");
417         if (!uci_network) {
418                 fprintf(stderr, "Failed to load network config\n");
419                 return;
420         }
421
422         uci_wireless = config_init_package("wireless");
423
424         vlist_update(&interfaces);
425         config_init = true;
426         device_lock();
427
428         device_reset_config();
429         config_init_devices();
430         config_init_interfaces();
431         config_init_routes();
432         config_init_rules();
433         config_init_globals();
434         config_init_wireless();
435
436         config_init = false;
437         device_unlock();
438
439         device_reset_old();
440         device_init_pending();
441         vlist_flush(&interfaces);
442         device_free_unused(NULL);
443         interface_refresh_assignments(false);
444         interface_start_pending();
445         wireless_start_pending();
446 }