proto-shell: make handler dump code more generic
[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 "config.h"
26
27 bool config_init = false;
28
29 static struct uci_context *uci_ctx;
30 static struct uci_package *uci_network;
31 static struct blob_buf b;
32
33 static int
34 config_parse_bridge_interface(struct uci_section *s)
35 {
36         char *name;
37
38         name = alloca(strlen(s->e.name) + 4);
39         sprintf(name, "br-%s", s->e.name);
40         blobmsg_add_string(&b, "name", name);
41
42         uci_to_blob(&b, s, bridge_device_type.config_params);
43         if (!device_create(name, &bridge_device_type, b.head)) {
44                 D(INTERFACE, "Failed to create bridge for interface '%s'\n", s->e.name);
45                 return -EINVAL;
46         }
47
48         blob_buf_init(&b, 0);
49         blobmsg_add_string(&b, "ifname", name);
50         return 0;
51 }
52
53 static void
54 config_parse_interface(struct uci_section *s, bool alias)
55 {
56         struct interface *iface;
57         const char *type = NULL;
58         struct blob_attr *config;
59         struct device *dev;
60         bool bridge = false;
61
62         blob_buf_init(&b, 0);
63
64         if (!alias)
65                 type = uci_lookup_option_string(uci_ctx, s, "type");
66         if (type && !strcmp(type, "bridge")) {
67                 if (config_parse_bridge_interface(s))
68                         return;
69
70                 bridge = true;
71         }
72
73         uci_to_blob(&b, s, &interface_attr_list);
74
75         iface = interface_alloc(s->e.name, b.head);
76         if (!iface)
77                 return;
78
79         if (iface->proto_handler && iface->proto_handler->config_params)
80                 uci_to_blob(&b, s, iface->proto_handler->config_params);
81
82         if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
83                 iface->device_config = true;
84
85         config = blob_memdup(b.head);
86         if (!config)
87                 goto error;
88
89         if (alias) {
90                 if (!interface_add_alias(iface, config))
91                         goto error_free_config;
92         } else {
93                 interface_add(iface, config);
94         }
95
96         /*
97          * need to look up the interface name again, in case of config update,
98          * the pointer will have changed
99          */
100         iface = vlist_find(&interfaces, s->e.name, iface, node);
101         if (!iface)
102                 return;
103
104         dev = iface->main_dev.dev;
105         if (!dev || !dev->default_config)
106                 return;
107
108         blob_buf_init(&b, 0);
109         uci_to_blob(&b, s, dev->type->config_params);
110         if (blob_len(b.head) == 0)
111                 return;
112
113         device_set_config(dev, dev->type, b.head);
114         return;
115 error_free_config:
116         free(config);
117 error:
118         free(iface);
119 }
120
121 static void
122 config_parse_route(struct uci_section *s, bool v6)
123 {
124         void *route;
125
126         blob_buf_init(&b, 0);
127         route = blobmsg_open_array(&b, "route");
128         uci_to_blob(&b, s, &route_attr_list);
129         blobmsg_close_array(&b, route);
130         interface_ip_add_route(NULL, blob_data(b.head), v6);
131 }
132
133 static void
134 config_parse_rule(struct uci_section *s, bool v6)
135 {
136         void *rule;
137
138         blob_buf_init(&b, 0);
139         rule = blobmsg_open_array(&b, "rule");
140         uci_to_blob(&b, s, &rule_attr_list);
141         blobmsg_close_array(&b, rule);
142         iprule_add(blob_data(b.head), v6);
143 }
144
145 static void
146 config_init_devices(void)
147 {
148         struct uci_element *e;
149
150         uci_foreach_element(&uci_network->sections, e) {
151                 struct uci_section *s = uci_to_section(e);
152                 const struct device_type *devtype = NULL;
153                 const char *type, *name;
154
155                 if (strcmp(s->type, "device") != 0)
156                         continue;
157
158                 name = uci_lookup_option_string(uci_ctx, s, "name");
159                 if (!name)
160                         continue;
161
162                 type = uci_lookup_option_string(uci_ctx, s, "type");
163                 if (type) {
164                         if (!strcmp(type, "bridge"))
165                                 devtype = &bridge_device_type;
166                         else if (!strcmp(type, "tunnel"))
167                                 devtype = &tunnel_device_type;
168                         else if (!strcmp(type, "macvlan"))
169                                 devtype = &macvlan_device_type;
170                 }
171
172                 if (!devtype)
173                         devtype = &simple_device_type;
174
175                 blob_buf_init(&b, 0);
176                 uci_to_blob(&b, s, devtype->config_params);
177                 device_create(name, devtype, b.head);
178         }
179 }
180
181 static struct uci_package *
182 config_init_package(const char *config)
183 {
184         struct uci_context *ctx = uci_ctx;
185         struct uci_package *p = NULL;
186
187         if (!ctx) {
188                 ctx = uci_alloc_context();
189                 uci_ctx = ctx;
190
191 #ifdef DUMMY_MODE
192                 uci_set_confdir(ctx, "./config");
193                 uci_set_savedir(ctx, "./tmp");
194 #endif
195         } else {
196                 p = uci_lookup_package(ctx, config);
197                 if (p)
198                         uci_unload(ctx, p);
199         }
200
201         if (uci_load(ctx, config, &p))
202                 return NULL;
203
204         return p;
205 }
206
207 static void
208 config_init_interfaces(void)
209 {
210         struct uci_element *e;
211
212         uci_foreach_element(&uci_network->sections, e) {
213                 struct uci_section *s = uci_to_section(e);
214
215                 if (!strcmp(s->type, "interface"))
216                         config_parse_interface(s, false);
217         }
218
219         uci_foreach_element(&uci_network->sections, e) {
220                 struct uci_section *s = uci_to_section(e);
221
222                 if (!strcmp(s->type, "alias"))
223                         config_parse_interface(s, true);
224         }
225 }
226
227 static void
228 config_init_routes(void)
229 {
230         struct interface *iface;
231         struct uci_element *e;
232
233         vlist_for_each_element(&interfaces, iface, node)
234                 interface_ip_update_start(&iface->config_ip);
235
236         uci_foreach_element(&uci_network->sections, e) {
237                 struct uci_section *s = uci_to_section(e);
238
239                 if (!strcmp(s->type, "route"))
240                         config_parse_route(s, false);
241                 else if (!strcmp(s->type, "route6"))
242                         config_parse_route(s, true);
243         }
244
245         vlist_for_each_element(&interfaces, iface, node)
246                 interface_ip_update_complete(&iface->config_ip);
247 }
248
249 static void
250 config_init_rules(void)
251 {
252         struct uci_element *e;
253
254         iprule_update_start();
255
256         uci_foreach_element(&uci_network->sections, e) {
257                 struct uci_section *s = uci_to_section(e);
258
259                 if (!strcmp(s->type, "rule"))
260                         config_parse_rule(s, false);
261                 else if (!strcmp(s->type, "rule6"))
262                         config_parse_rule(s, true);
263         }
264
265         iprule_update_complete();
266 }
267
268 static void
269 config_init_globals(void)
270 {
271         struct uci_section *globals = uci_lookup_section(
272                         uci_ctx, uci_network, "globals");
273         if (!globals)
274                 return;
275
276         const char *ula_prefix = uci_lookup_option_string(
277                         uci_ctx, globals, "ula_prefix");
278         interface_ip_set_ula_prefix(ula_prefix);
279 }
280
281 void
282 config_init_all(void)
283 {
284         uci_network = config_init_package("network");
285         if (!uci_network) {
286                 fprintf(stderr, "Failed to load network config\n");
287                 return;
288         }
289
290         vlist_update(&interfaces);
291         config_init = true;
292         device_lock();
293
294         device_reset_config();
295         config_init_devices();
296         config_init_interfaces();
297         config_init_routes();
298         config_init_rules();
299         config_init_globals();
300
301         config_init = false;
302         device_unlock();
303
304         device_reset_old();
305         device_init_pending();
306         vlist_flush(&interfaces);
307         device_free_unused(NULL);
308         interface_refresh_assignments(false);
309         interface_start_pending();
310 }