a9f265149409c65e6b1aefdef21aa26834455950
[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, false);
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 if (!interface_add(iface, config))
124 goto error_free_config;
125 }
126 return;
127
128 error_free_config:
129 free(config);
130 error:
131 free(iface);
132 }
133
134 static void
135 config_parse_route(struct uci_section *s, bool v6)
136 {
137 void *route;
138
139 blob_buf_init(&b, 0);
140 route = blobmsg_open_array(&b, "route");
141 uci_to_blob(&b, s, &route_attr_list);
142 blobmsg_close_array(&b, route);
143 interface_ip_add_route(NULL, blob_data(b.head), v6);
144 }
145
146 static void
147 config_parse_neighbor(struct uci_section *s, bool v6)
148 {
149 void *neighbor;
150 blob_buf_init(&b,0);
151 neighbor = blobmsg_open_array(&b, "neighbor");
152 uci_to_blob(&b,s, &neighbor_attr_list);
153 blobmsg_close_array(&b, neighbor);
154 interface_ip_add_neighbor(NULL, blob_data(b.head), v6);
155 }
156
157 static void
158 config_parse_rule(struct uci_section *s, bool v6)
159 {
160 void *rule;
161
162 blob_buf_init(&b, 0);
163 rule = blobmsg_open_array(&b, "rule");
164 uci_to_blob(&b, s, &rule_attr_list);
165 blobmsg_close_array(&b, rule);
166 iprule_add(blob_data(b.head), v6);
167 }
168
169 static void
170 config_init_devices(void)
171 {
172 struct uci_element *e;
173
174 uci_foreach_element(&uci_network->sections, e) {
175 const struct uci_blob_param_list *params = NULL;
176 struct uci_section *s = uci_to_section(e);
177 struct device_type *devtype = NULL;
178 struct device *dev;
179 const char *type, *name;
180
181 if (strcmp(s->type, "device") != 0)
182 continue;
183
184 name = uci_lookup_option_string(uci_ctx, s, "name");
185 if (!name)
186 continue;
187
188 type = uci_lookup_option_string(uci_ctx, s, "type");
189 if (type)
190 devtype = device_type_get(type);
191
192 if (devtype)
193 params = devtype->config_params;
194 if (!params)
195 params = simple_device_type.config_params;
196
197 blob_buf_init(&b, 0);
198 uci_to_blob(&b, s, params);
199 if (devtype) {
200 dev = device_create(name, devtype, b.head);
201 if (!dev)
202 continue;
203 } else {
204 dev = device_get(name, 1);
205 if (!dev)
206 continue;
207
208 dev->current_config = true;
209 device_apply_config(dev, dev->type, b.head);
210 }
211 dev->default_config = false;
212 }
213 }
214
215 static void
216 config_parse_vlan(struct device *dev, struct uci_section *s)
217 {
218 enum {
219 BRVLAN_ATTR_VID,
220 BRVLAN_ATTR_LOCAL,
221 BRVLAN_ATTR_PORTS,
222 __BRVLAN_ATTR_MAX,
223 };
224 static const struct blobmsg_policy vlan_attrs[__BRVLAN_ATTR_MAX] = {
225 [BRVLAN_ATTR_VID] = { "vlan", BLOBMSG_TYPE_INT32 },
226 [BRVLAN_ATTR_LOCAL] = { "local", BLOBMSG_TYPE_BOOL },
227 [BRVLAN_ATTR_PORTS] = { "ports", BLOBMSG_TYPE_ARRAY },
228 };
229 static const struct uci_blob_param_info vlan_attr_info[__BRVLAN_ATTR_MAX] = {
230 [BRVLAN_ATTR_PORTS] = { .type = BLOBMSG_TYPE_STRING },
231 };
232 static const struct uci_blob_param_list vlan_attr_list = {
233 .n_params = __BRVLAN_ATTR_MAX,
234 .params = vlan_attrs,
235 .info = vlan_attr_info,
236 };
237 struct blob_attr *tb[__BRVLAN_ATTR_MAX];
238 struct blob_attr *cur;
239 struct bridge_vlan_port *port;
240 struct bridge_vlan *vlan;
241 unsigned int vid;
242 const char *val;
243 char *name_buf;
244 int name_len = 0;
245 int n_ports = 0;
246 int rem;
247
248 val = uci_lookup_option_string(uci_ctx, s, "vlan");
249 if (!val)
250 return;
251
252 blob_buf_init(&b, 0);
253 uci_to_blob(&b, s, &vlan_attr_list);
254 blobmsg_parse(vlan_attrs, __BRVLAN_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
255
256 if (!tb[BRVLAN_ATTR_VID])
257 return;
258
259 vid = blobmsg_get_u32(tb[BRVLAN_ATTR_VID]);
260 if (!vid || vid > 4095)
261 return;
262
263 blobmsg_for_each_attr(cur, tb[BRVLAN_ATTR_PORTS], rem) {
264 name_len += strlen(blobmsg_get_string(cur)) + 1;
265 n_ports++;
266 }
267
268 vlan = calloc(1, sizeof(*vlan) + n_ports * sizeof(*port) + name_len);
269 if (!vlan)
270 return;
271
272 vlan->vid = vid;
273 vlan->local = true;
274 if (tb[BRVLAN_ATTR_LOCAL])
275 vlan->local = blobmsg_get_bool(tb[BRVLAN_ATTR_LOCAL]);
276
277 vlan->n_ports = n_ports;
278 vlan->ports = port = (struct bridge_vlan_port *)&vlan[1];
279 name_buf = (char *)&port[n_ports];
280
281 blobmsg_for_each_attr(cur, tb[BRVLAN_ATTR_PORTS], rem) {
282 char *sep;
283
284 port->ifname = name_buf;
285 port->flags = BRVLAN_F_UNTAGGED;
286 strcpy(name_buf, blobmsg_get_string(cur));
287
288 sep = strchr(name_buf, ':');
289 if (sep) {
290 for (*sep = 0, sep++; *sep; sep++)
291 switch (*sep) {
292 case '*':
293 port->flags |= BRVLAN_F_PVID;
294 break;
295 case 't':
296 port->flags &= ~BRVLAN_F_UNTAGGED;
297 break;
298 }
299 }
300
301 name_buf += strlen(name_buf) + 1;
302 port++;
303 }
304
305 vlist_add(&dev->vlans, &vlan->node, &vlan->vid);
306 }
307
308
309 static void
310 config_init_vlans(void)
311 {
312 struct uci_element *e;
313 struct device *dev;
314
315 device_vlan_update(false);
316 uci_foreach_element(&uci_network->sections, e) {
317 struct uci_section *s = uci_to_section(e);
318 const char *name;
319
320 if (strcmp(s->type, "bridge-vlan") != 0)
321 continue;
322
323 name = uci_lookup_option_string(uci_ctx, s, "device");
324 if (!name)
325 continue;
326
327 dev = device_get(name, 0);
328 if (!dev || !dev->vlans.update)
329 continue;
330
331 config_parse_vlan(dev, s);
332 }
333 device_vlan_update(true);
334 }
335
336 static struct uci_package *
337 config_init_package(const char *config)
338 {
339 struct uci_context *ctx = uci_ctx;
340 struct uci_package *p = NULL;
341
342 if (!ctx) {
343 ctx = uci_alloc_context();
344 uci_ctx = ctx;
345
346 ctx->flags &= ~UCI_FLAG_STRICT;
347 if (config_path)
348 uci_set_confdir(ctx, config_path);
349
350 #ifdef DUMMY_MODE
351 uci_set_savedir(ctx, "./tmp");
352 #endif
353 } else {
354 p = uci_lookup_package(ctx, config);
355 if (p)
356 uci_unload(ctx, p);
357 }
358
359 if (uci_load(ctx, config, &p))
360 return NULL;
361
362 return p;
363 }
364
365 static void
366 config_init_interfaces(void)
367 {
368 struct uci_element *e;
369
370 uci_foreach_element(&uci_network->sections, e) {
371 struct uci_section *s = uci_to_section(e);
372
373 if (!strcmp(s->type, "interface"))
374 config_parse_interface(s, false);
375 }
376
377 uci_foreach_element(&uci_network->sections, e) {
378 struct uci_section *s = uci_to_section(e);
379
380 if (!strcmp(s->type, "alias"))
381 config_parse_interface(s, true);
382 }
383 }
384
385 static void
386 config_init_ip(void)
387 {
388 struct interface *iface;
389 struct uci_element *e;
390
391 vlist_for_each_element(&interfaces, iface, node)
392 interface_ip_update_start(&iface->config_ip);
393
394 uci_foreach_element(&uci_network->sections, e) {
395 struct uci_section *s = uci_to_section(e);
396
397 if (!strcmp(s->type, "route"))
398 config_parse_route(s, false);
399 else if (!strcmp(s->type, "route6"))
400 config_parse_route(s, true);
401 if (!strcmp(s->type, "neighbor"))
402 config_parse_neighbor(s, false);
403 else if (!strcmp(s->type, "neighbor6"))
404 config_parse_neighbor(s, true);
405 }
406
407 vlist_for_each_element(&interfaces, iface, node)
408 interface_ip_update_complete(&iface->config_ip);
409 }
410
411 static void
412 config_init_rules(void)
413 {
414 struct uci_element *e;
415
416 iprule_update_start();
417
418 uci_foreach_element(&uci_network->sections, e) {
419 struct uci_section *s = uci_to_section(e);
420
421 if (!strcmp(s->type, "rule"))
422 config_parse_rule(s, false);
423 else if (!strcmp(s->type, "rule6"))
424 config_parse_rule(s, true);
425 }
426
427 iprule_update_complete();
428 }
429
430 static void
431 config_init_globals(void)
432 {
433 struct uci_section *globals = uci_lookup_section(
434 uci_ctx, uci_network, "globals");
435 if (!globals)
436 return;
437
438 const char *ula_prefix = uci_lookup_option_string(
439 uci_ctx, globals, "ula_prefix");
440 interface_ip_set_ula_prefix(ula_prefix);
441 }
442
443 static void
444 config_parse_wireless_device(struct uci_section *s)
445 {
446 struct wireless_driver *drv;
447 const char *driver_name;
448
449 driver_name = uci_lookup_option_string(uci_ctx, s, "type");
450 if (!driver_name)
451 return;
452
453 drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
454 if (!drv)
455 return;
456
457 blob_buf_init(&b, 0);
458 uci_to_blob(&b, s, drv->device.config);
459 wireless_device_create(drv, s->e.name, b.head);
460 }
461
462 static struct wireless_interface*
463 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
464 {
465 char *name;
466
467 name = alloca(strlen(s->type) + 16);
468 sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
469
470 blob_buf_init(&b, 0);
471 uci_to_blob(&b, s, wdev->drv->interface.config);
472 return wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
473 }
474
475 static void
476 config_parse_wireless_vlan(struct wireless_device *wdev, char *vif, struct uci_section *s)
477 {
478 char *name;
479
480 name = alloca(strlen(s->type) + 16);
481 sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
482
483 blob_buf_init(&b, 0);
484 uci_to_blob(&b, s, wdev->drv->vlan.config);
485 wireless_vlan_create(wdev, vif, b.head, s->anonymous ? name : s->e.name);
486 }
487
488 static void
489 config_parse_wireless_station(struct wireless_device *wdev, char *vif, struct uci_section *s)
490 {
491 char *name;
492
493 name = alloca(strlen(s->type) + 16);
494 sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
495
496 blob_buf_init(&b, 0);
497 uci_to_blob(&b, s, wdev->drv->station.config);
498 wireless_station_create(wdev, vif, b.head, s->anonymous ? name : s->e.name);
499 }
500
501 static void
502 config_init_wireless(void)
503 {
504 struct wireless_device *wdev;
505 struct uci_element *e;
506 const char *dev_name;
507
508 if (!uci_wireless) {
509 DPRINTF("No wireless configuration found\n");
510 return;
511 }
512
513 vlist_update(&wireless_devices);
514
515 uci_foreach_element(&uci_wireless->sections, e) {
516 struct uci_section *s = uci_to_section(e);
517 if (strcmp(s->type, "wifi-device") != 0)
518 continue;
519
520 config_parse_wireless_device(s);
521 }
522
523 vlist_flush(&wireless_devices);
524
525 vlist_for_each_element(&wireless_devices, wdev, node) {
526 wdev->vif_idx = 0;
527 vlist_update(&wdev->interfaces);
528 wdev->vlan_idx = 0;
529 vlist_update(&wdev->vlans);
530 wdev->sta_idx = 0;
531 vlist_update(&wdev->stations);
532 }
533
534 uci_foreach_element(&uci_wireless->sections, e) {
535 struct uci_section *s = uci_to_section(e);
536 struct wireless_interface *vif;
537 struct uci_element *f;
538
539 if (strcmp(s->type, "wifi-iface") != 0)
540 continue;
541
542 dev_name = uci_lookup_option_string(uci_ctx, s, "device");
543 if (!dev_name)
544 continue;
545
546 wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
547 if (!wdev) {
548 DPRINTF("device %s not found!\n", dev_name);
549 continue;
550 }
551
552 vif = config_parse_wireless_interface(wdev, s);
553
554 if (!vif || s->anonymous)
555 continue;
556 uci_foreach_element(&uci_wireless->sections, f) {
557 struct uci_section *s = uci_to_section(f);
558 const char *vif_name;
559
560 if (strcmp(s->type, "wifi-vlan") != 0)
561 continue;
562
563 vif_name = uci_lookup_option_string(uci_ctx, s, "iface");
564 if (vif_name && strcmp(e->name, vif_name))
565 continue;
566 config_parse_wireless_vlan(wdev, vif->name, s);
567 }
568
569 uci_foreach_element(&uci_wireless->sections, f) {
570 struct uci_section *s = uci_to_section(f);
571 const char *vif_name;
572
573 if (strcmp(s->type, "wifi-station") != 0)
574 continue;
575
576 vif_name = uci_lookup_option_string(uci_ctx, s, "iface");
577 if (vif_name && strcmp(e->name, vif_name))
578 continue;
579 config_parse_wireless_station(wdev, vif->name, s);
580 }
581 }
582
583 vlist_for_each_element(&wireless_devices, wdev, node) {
584 vlist_flush(&wdev->interfaces);
585 vlist_flush(&wdev->vlans);
586 vlist_flush(&wdev->stations);
587 }
588 }
589
590 int
591 config_init_all(void)
592 {
593 int ret = 0;
594 char *err;
595
596 uci_network = config_init_package("network");
597 if (!uci_network) {
598 uci_get_errorstr(uci_ctx, &err, NULL);
599 netifd_log_message(L_CRIT, "Failed to load network config (%s)\n", err);
600 free(err);
601 return -1;
602 }
603
604 uci_wireless = config_init_package("wireless");
605 if (!uci_wireless && uci_ctx->err != UCI_ERR_NOTFOUND) {
606 uci_get_errorstr(uci_ctx, &err, NULL);
607 netifd_log_message(L_CRIT, "Failed to load wireless config (%s)\n", err);
608 free(err);
609 ret = -1;
610 }
611
612 vlist_update(&interfaces);
613 config_init = true;
614 device_lock();
615
616 device_reset_config();
617 config_init_devices();
618 config_init_interfaces();
619 config_init_vlans();
620 config_init_ip();
621 config_init_rules();
622 config_init_globals();
623 config_init_wireless();
624
625 config_init = false;
626 device_unlock();
627
628 device_reset_old();
629 device_init_pending();
630 vlist_flush(&interfaces);
631 device_free_unused(NULL);
632 interface_refresh_assignments(false);
633 interface_start_pending();
634 wireless_start_pending();
635
636 return ret;
637 }