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