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