Don't delete / readd IP addresses when only their lifetime has changed
[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                 }
170
171                 if (!devtype)
172                         devtype = &simple_device_type;
173
174                 blob_buf_init(&b, 0);
175                 uci_to_blob(&b, s, devtype->config_params);
176                 device_create(name, devtype, b.head);
177         }
178 }
179
180 static struct uci_package *
181 config_init_package(const char *config)
182 {
183         struct uci_context *ctx = uci_ctx;
184         struct uci_package *p = NULL;
185
186         if (!ctx) {
187                 ctx = uci_alloc_context();
188                 uci_ctx = ctx;
189
190 #ifdef DUMMY_MODE
191                 uci_set_confdir(ctx, "./config");
192                 uci_set_savedir(ctx, "./tmp");
193 #endif
194         } else {
195                 p = uci_lookup_package(ctx, config);
196                 if (p)
197                         uci_unload(ctx, p);
198         }
199
200         if (uci_load(ctx, config, &p))
201                 return NULL;
202
203         return p;
204 }
205
206 static void
207 config_init_interfaces(void)
208 {
209         struct uci_element *e;
210
211         uci_foreach_element(&uci_network->sections, e) {
212                 struct uci_section *s = uci_to_section(e);
213
214                 if (!strcmp(s->type, "interface"))
215                         config_parse_interface(s, false);
216         }
217
218         uci_foreach_element(&uci_network->sections, e) {
219                 struct uci_section *s = uci_to_section(e);
220
221                 if (!strcmp(s->type, "alias"))
222                         config_parse_interface(s, true);
223         }
224 }
225
226 static void
227 config_init_routes(void)
228 {
229         struct interface *iface;
230         struct uci_element *e;
231
232         vlist_for_each_element(&interfaces, iface, node)
233                 interface_ip_update_start(&iface->config_ip);
234
235         uci_foreach_element(&uci_network->sections, e) {
236                 struct uci_section *s = uci_to_section(e);
237
238                 if (!strcmp(s->type, "route"))
239                         config_parse_route(s, false);
240                 else if (!strcmp(s->type, "route6"))
241                         config_parse_route(s, true);
242         }
243
244         vlist_for_each_element(&interfaces, iface, node)
245                 interface_ip_update_complete(&iface->config_ip);
246 }
247
248 static void
249 config_init_rules(void)
250 {
251         struct uci_element *e;
252
253         iprule_update_start();
254
255         uci_foreach_element(&uci_network->sections, e) {
256                 struct uci_section *s = uci_to_section(e);
257
258                 if (!strcmp(s->type, "rule"))
259                         config_parse_rule(s, false);
260                 else if (!strcmp(s->type, "rule6"))
261                         config_parse_rule(s, true);
262         }
263
264         iprule_update_complete();
265 }
266
267 static void
268 config_init_globals(void)
269 {
270         struct uci_section *globals = uci_lookup_section(
271                         uci_ctx, uci_network, "globals");
272         if (!globals)
273                 return;
274
275         const char *ula_prefix = uci_lookup_option_string(
276                         uci_ctx, globals, "ula_prefix");
277         interface_ip_set_ula_prefix(ula_prefix);
278 }
279
280 void
281 config_init_all(void)
282 {
283         uci_network = config_init_package("network");
284         if (!uci_network) {
285                 fprintf(stderr, "Failed to load network config\n");
286                 return;
287         }
288
289         vlist_update(&interfaces);
290         config_init = true;
291         device_lock();
292
293         device_reset_config();
294         config_init_devices();
295         config_init_interfaces();
296         config_init_routes();
297         config_init_rules();
298         config_init_globals();
299
300         config_init = false;
301         device_unlock();
302
303         device_reset_old();
304         device_init_pending();
305         vlist_flush(&interfaces);
306         device_free_unused(NULL);
307         interface_refresh_assignments(false);
308         interface_start_pending();
309 }