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