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