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