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