2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
22 #include "interface.h"
28 BRIDGE_ATTR_FORWARD_DELAY,
30 BRIDGE_ATTR_IGMP_SNOOP,
31 BRIDGE_ATTR_AGEING_TIME,
32 BRIDGE_ATTR_HELLO_TIME,
34 BRIDGE_ATTR_BRIDGE_EMPTY,
35 BRIDGE_ATTR_MULTICAST_QUERIER,
37 BRIDGE_ATTR_ROBUSTNESS,
38 BRIDGE_ATTR_QUERY_INTERVAL,
39 BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL,
40 BRIDGE_ATTR_LAST_MEMBER_INTERVAL,
44 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
45 [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
46 [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
47 [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
48 [BRIDGE_ATTR_PRIORITY] = { "priority", BLOBMSG_TYPE_INT32 },
49 [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
50 [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
51 [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
52 [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
53 [BRIDGE_ATTR_BRIDGE_EMPTY] = { "bridge_empty", BLOBMSG_TYPE_BOOL },
54 [BRIDGE_ATTR_MULTICAST_QUERIER] = { "multicast_querier", BLOBMSG_TYPE_BOOL },
55 [BRIDGE_ATTR_HASH_MAX] = { "hash_max", BLOBMSG_TYPE_INT32 },
56 [BRIDGE_ATTR_ROBUSTNESS] = { "robustness", BLOBMSG_TYPE_INT32 },
57 [BRIDGE_ATTR_QUERY_INTERVAL] = { "query_interval", BLOBMSG_TYPE_INT32 },
58 [BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL] = { "query_response_interval", BLOBMSG_TYPE_INT32 },
59 [BRIDGE_ATTR_LAST_MEMBER_INTERVAL] = { "last_member_interval", BLOBMSG_TYPE_INT32 },
62 static const struct uci_blob_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
63 [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
66 static const struct uci_blob_param_list bridge_attr_list = {
67 .n_params = __BRIDGE_ATTR_MAX,
68 .params = bridge_attrs,
69 .info = bridge_attr_info,
72 .next = { &device_attr_list },
75 static struct device *bridge_create(const char *name, struct device_type *devtype,
76 struct blob_attr *attr);
77 static void bridge_config_init(struct device *dev);
78 static void bridge_free(struct device *dev);
79 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
81 bridge_reload(struct device *dev, struct blob_attr *attr);
83 struct device_type bridge_device_type = {
85 .config_params = &bridge_attr_list,
87 .create = bridge_create,
88 .config_init = bridge_config_init,
89 .reload = bridge_reload,
91 .dump_info = bridge_dump_info,
96 device_state_cb set_state;
98 struct blob_attr *config_data;
99 struct bridge_config config;
100 struct blob_attr *ifnames;
104 struct uloop_timeout retry;
105 struct bridge_member *primary_port;
106 struct vlist_tree members;
111 struct bridge_member {
112 struct vlist_node node;
113 struct bridge_state *bst;
114 struct device_user dev;
120 bridge_reset_primary(struct bridge_state *bst)
122 struct bridge_member *bm;
124 if (!bst->primary_port &&
125 (bst->dev.settings.flags & DEV_OPT_MACADDR))
128 bst->primary_port = NULL;
129 bst->dev.settings.flags &= ~DEV_OPT_MACADDR;
130 vlist_for_each_element(&bst->members, bm, node) {
136 bst->primary_port = bm;
137 if (bm->dev.dev->settings.flags & DEV_OPT_MACADDR)
138 macaddr = bm->dev.dev->settings.macaddr;
140 macaddr = bm->dev.dev->orig_settings.macaddr;
141 memcpy(bst->dev.settings.macaddr, macaddr, 6);
142 bst->dev.settings.flags |= DEV_OPT_MACADDR;
148 bridge_disable_member(struct bridge_member *bm)
150 struct bridge_state *bst = bm->bst;
155 system_bridge_delif(&bst->dev, bm->dev.dev);
156 device_release(&bm->dev);
158 device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
164 bridge_enable_interface(struct bridge_state *bst)
171 ret = system_bridge_addbr(&bst->dev, &bst->config);
180 bridge_disable_interface(struct bridge_state *bst)
185 system_bridge_delbr(&bst->dev);
190 bridge_enable_member(struct bridge_member *bm)
192 struct bridge_state *bst = bm->bst;
198 ret = bridge_enable_interface(bst);
202 /* Disable IPv6 for bridge members */
203 if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
204 bm->dev.dev->settings.ipv6 = 0;
205 bm->dev.dev->settings.flags |= DEV_OPT_IPV6;
208 ret = device_claim(&bm->dev);
212 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
214 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
218 device_set_present(&bst->dev, true);
219 device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
227 device_release(&bm->dev);
233 bridge_remove_member(struct bridge_member *bm)
235 struct bridge_state *bst = bm->bst;
240 if (bm == bst->primary_port)
241 bridge_reset_primary(bst);
244 bridge_disable_member(bm);
247 bm->bst->n_present--;
249 if (bst->config.bridge_empty)
252 bst->force_active = false;
253 if (bst->n_present == 0)
254 device_set_present(&bst->dev, false);
258 bridge_free_member(struct bridge_member *bm)
260 struct device *dev = bm->dev.dev;
262 bridge_remove_member(bm);
263 device_remove_user(&bm->dev);
266 * When reloading the config and moving a device from one bridge to
267 * another, the other bridge may have tried to claim this device
268 * before it was removed here.
269 * Ensure that claiming the device is retried by toggling its present
273 device_set_present(dev, false);
274 device_set_present(dev, true);
281 bridge_check_retry(struct bridge_state *bst)
286 uloop_timeout_set(&bst->retry, 100);
290 bridge_member_cb(struct device_user *dev, enum device_event ev)
292 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
293 struct bridge_state *bst = bm->bst;
297 assert(!bm->present);
302 if (bst->n_present == 1)
303 device_set_present(&bst->dev, true);
304 if (bst->dev.active && !bridge_enable_member(bm)) {
306 * Adding a bridge member can overwrite the bridge mtu
307 * in the kernel, apply the bridge settings in case the
310 system_if_apply_settings(&bst->dev, &bst->dev.settings,
311 DEV_OPT_MTU | DEV_OPT_MTU6);
315 case DEV_EVENT_REMOVE:
317 vlist_delete(&bst->members, &bm->node);
322 bridge_remove_member(bm);
331 bridge_set_down(struct bridge_state *bst)
333 struct bridge_member *bm;
335 bst->set_state(&bst->dev, false);
337 vlist_for_each_element(&bst->members, bm, node)
338 bridge_disable_member(bm);
340 bridge_disable_interface(bst);
346 bridge_set_up(struct bridge_state *bst)
348 struct bridge_member *bm;
351 if (!bst->n_present) {
352 if (!bst->force_active)
355 ret = bridge_enable_interface(bst);
361 vlist_for_each_element(&bst->members, bm, node)
362 bridge_enable_member(bm);
363 bridge_check_retry(bst);
365 if (!bst->force_active && !bst->n_present) {
366 /* initialization of all member interfaces failed */
367 bridge_disable_interface(bst);
368 device_set_present(&bst->dev, false);
372 bridge_reset_primary(bst);
373 ret = bst->set_state(&bst->dev, true);
375 bridge_set_down(bst);
381 bridge_set_state(struct device *dev, bool up)
383 struct bridge_state *bst;
385 bst = container_of(dev, struct bridge_state, dev);
388 return bridge_set_up(bst);
390 return bridge_set_down(bst);
393 static struct bridge_member *
394 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
396 struct bridge_member *bm;
398 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
403 bm->dev.cb = bridge_member_cb;
404 bm->dev.hotplug = hotplug;
405 strcpy(bm->name, dev->ifname);
407 vlist_add(&bst->members, &bm->node, bm->name);
408 // Need to look up the bridge member again as the above
409 // created pointer will be freed in case the bridge member
411 bm = vlist_find(&bst->members, dev->ifname, bm, node);
413 bm->node.version = -1;
419 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
420 struct vlist_node *node_old)
422 struct bridge_member *bm;
426 bm = container_of(node_new, struct bridge_member, node);
435 device_add_user(&bm->dev, dev);
440 bm = container_of(node_old, struct bridge_member, node);
441 bridge_free_member(bm);
447 bridge_add_member(struct bridge_state *bst, const char *name)
451 dev = device_get(name, true);
455 bridge_create_member(bst, dev, false);
459 bridge_hotplug_add(struct device *dev, struct device *member)
461 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
463 bridge_create_member(bst, member, true);
469 bridge_hotplug_del(struct device *dev, struct device *member)
471 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
472 struct bridge_member *bm;
474 bm = vlist_find(&bst->members, member->ifname, bm, node);
476 return UBUS_STATUS_NOT_FOUND;
478 vlist_delete(&bst->members, &bm->node);
483 bridge_hotplug_prepare(struct device *dev)
485 struct bridge_state *bst;
487 bst = container_of(dev, struct bridge_state, dev);
488 bst->force_active = true;
489 device_set_present(&bst->dev, true);
494 static const struct device_hotplug_ops bridge_ops = {
495 .prepare = bridge_hotplug_prepare,
496 .add = bridge_hotplug_add,
497 .del = bridge_hotplug_del
501 bridge_free(struct device *dev)
503 struct bridge_state *bst;
505 bst = container_of(dev, struct bridge_state, dev);
506 vlist_flush_all(&bst->members);
507 free(bst->config_data);
512 bridge_dump_info(struct device *dev, struct blob_buf *b)
514 struct bridge_state *bst;
515 struct bridge_member *bm;
518 bst = container_of(dev, struct bridge_state, dev);
520 system_if_dump_info(dev, b);
521 list = blobmsg_open_array(b, "bridge-members");
523 vlist_for_each_element(&bst->members, bm, node)
524 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
526 blobmsg_close_array(b, list);
530 bridge_config_init(struct device *dev)
532 struct bridge_state *bst;
533 struct blob_attr *cur;
536 bst = container_of(dev, struct bridge_state, dev);
538 if (bst->config.bridge_empty) {
539 bst->force_active = true;
540 device_set_present(&bst->dev, true);
544 vlist_update(&bst->members);
546 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
547 bridge_add_member(bst, blobmsg_data(cur));
550 vlist_flush(&bst->members);
551 bridge_check_retry(bst);
555 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
557 struct bridge_config *cfg = &bst->config;
558 struct blob_attr *cur;
562 cfg->forward_delay = 2;
563 cfg->igmp_snoop = true;
564 cfg->multicast_querier = true;
566 cfg->query_interval = 12500;
567 cfg->query_response_interval = 1000;
568 cfg->last_member_interval = 100;
570 cfg->bridge_empty = false;
571 cfg->priority = 0x7FFF;
573 if ((cur = tb[BRIDGE_ATTR_STP]))
574 cfg->stp = blobmsg_get_bool(cur);
576 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
577 cfg->forward_delay = blobmsg_get_u32(cur);
579 if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
580 cfg->priority = blobmsg_get_u32(cur);
582 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
583 cfg->multicast_querier = cfg->igmp_snoop = blobmsg_get_bool(cur);
585 if ((cur = tb[BRIDGE_ATTR_MULTICAST_QUERIER]))
586 cfg->multicast_querier = blobmsg_get_bool(cur);
588 if ((cur = tb[BRIDGE_ATTR_HASH_MAX]))
589 cfg->hash_max = blobmsg_get_u32(cur);
591 if ((cur = tb[BRIDGE_ATTR_ROBUSTNESS])) {
592 cfg->robustness = blobmsg_get_u32(cur);
593 cfg->flags |= BRIDGE_OPT_ROBUSTNESS;
596 if ((cur = tb[BRIDGE_ATTR_QUERY_INTERVAL])) {
597 cfg->query_interval = blobmsg_get_u32(cur);
598 cfg->flags |= BRIDGE_OPT_QUERY_INTERVAL;
601 if ((cur = tb[BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL])) {
602 cfg->query_response_interval = blobmsg_get_u32(cur);
603 cfg->flags |= BRIDGE_OPT_QUERY_RESPONSE_INTERVAL;
606 if ((cur = tb[BRIDGE_ATTR_LAST_MEMBER_INTERVAL])) {
607 cfg->last_member_interval = blobmsg_get_u32(cur);
608 cfg->flags |= BRIDGE_OPT_LAST_MEMBER_INTERVAL;
611 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
612 cfg->ageing_time = blobmsg_get_u32(cur);
613 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
616 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
617 cfg->hello_time = blobmsg_get_u32(cur);
618 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
621 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
622 cfg->max_age = blobmsg_get_u32(cur);
623 cfg->flags |= BRIDGE_OPT_MAX_AGE;
626 if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
627 cfg->bridge_empty = blobmsg_get_bool(cur);
631 bridge_reload(struct device *dev, struct blob_attr *attr)
633 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
634 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
635 enum dev_change_type ret = DEV_CONFIG_APPLIED;
637 struct bridge_state *bst;
639 BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
640 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
642 bst = container_of(dev, struct bridge_state, dev);
643 attr = blob_memdup(attr);
645 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
646 blob_data(attr), blob_len(attr));
647 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
648 blob_data(attr), blob_len(attr));
650 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
651 device_init_settings(dev, tb_dev);
652 bridge_apply_settings(bst, tb_br);
654 if (bst->config_data) {
655 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
656 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
658 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
659 blob_data(bst->config_data), blob_len(bst->config_data));
662 uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
664 ret = DEV_CONFIG_RESTART;
666 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
667 blob_data(bst->config_data), blob_len(bst->config_data));
670 uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
671 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
672 ret = DEV_CONFIG_RESTART;
674 bridge_config_init(dev);
677 free(bst->config_data);
678 bst->config_data = attr;
683 bridge_retry_members(struct uloop_timeout *timeout)
685 struct bridge_state *bst = container_of(timeout, struct bridge_state, retry);
686 struct bridge_member *bm;
689 vlist_for_each_element(&bst->members, bm, node) {
693 if (!bm->dev.dev->present)
698 bridge_enable_member(bm);
702 static struct device *
703 bridge_create(const char *name, struct device_type *devtype,
704 struct blob_attr *attr)
706 struct bridge_state *bst;
707 struct device *dev = NULL;
709 bst = calloc(1, sizeof(*bst));
714 device_init(dev, devtype, name);
715 dev->config_pending = true;
716 bst->retry.cb = bridge_retry_members;
718 bst->set_state = dev->set_state;
719 dev->set_state = bridge_set_state;
721 dev->hotplug_ops = &bridge_ops;
723 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
724 bst->members.keep_old = true;
725 bridge_reload(dev, attr);