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