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,
38 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
39 [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
40 [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
41 [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
42 [BRIDGE_ATTR_PRIORITY] = { "priority", BLOBMSG_TYPE_INT32 },
43 [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
44 [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
45 [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
46 [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
47 [BRIDGE_ATTR_BRIDGE_EMPTY] = { "bridge_empty", BLOBMSG_TYPE_BOOL },
50 static const struct uci_blob_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
51 [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
54 static const struct uci_blob_param_list bridge_attr_list = {
55 .n_params = __BRIDGE_ATTR_MAX,
56 .params = bridge_attrs,
57 .info = bridge_attr_info,
60 .next = { &device_attr_list },
63 static struct device *bridge_create(const char *name, struct blob_attr *attr);
64 static void bridge_config_init(struct device *dev);
65 static void bridge_free(struct device *dev);
66 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
68 bridge_reload(struct device *dev, struct blob_attr *attr);
70 const struct device_type bridge_device_type = {
72 .config_params = &bridge_attr_list,
74 .create = bridge_create,
75 .config_init = bridge_config_init,
76 .reload = bridge_reload,
78 .dump_info = bridge_dump_info,
83 device_state_cb set_state;
85 struct blob_attr *config_data;
86 struct bridge_config config;
87 struct blob_attr *ifnames;
91 struct uloop_timeout retry;
92 struct bridge_member *primary_port;
93 struct vlist_tree members;
98 struct bridge_member {
99 struct vlist_node node;
100 struct bridge_state *bst;
101 struct device_user dev;
107 bridge_reset_primary(struct bridge_state *bst)
109 struct bridge_member *bm;
111 if (!bst->primary_port &&
112 (bst->dev.settings.flags & DEV_OPT_MACADDR))
115 bst->primary_port = NULL;
116 bst->dev.settings.flags &= ~DEV_OPT_MACADDR;
117 vlist_for_each_element(&bst->members, bm, node) {
123 bst->primary_port = bm;
124 if (bm->dev.dev->settings.flags & DEV_OPT_MACADDR)
125 macaddr = bm->dev.dev->settings.macaddr;
127 macaddr = bm->dev.dev->orig_settings.macaddr;
128 memcpy(bst->dev.settings.macaddr, macaddr, 6);
129 bst->dev.settings.flags |= DEV_OPT_MACADDR;
135 bridge_disable_member(struct bridge_member *bm)
137 struct bridge_state *bst = bm->bst;
142 system_bridge_delif(&bst->dev, bm->dev.dev);
143 device_release(&bm->dev);
145 device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
151 bridge_enable_interface(struct bridge_state *bst)
158 ret = system_bridge_addbr(&bst->dev, &bst->config);
167 bridge_disable_interface(struct bridge_state *bst)
172 system_bridge_delbr(&bst->dev);
177 bridge_enable_member(struct bridge_member *bm)
179 struct bridge_state *bst = bm->bst;
185 ret = bridge_enable_interface(bst);
189 /* Disable IPv6 for bridge members */
190 if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
191 bm->dev.dev->settings.ipv6 = 0;
192 bm->dev.dev->settings.flags |= DEV_OPT_IPV6;
195 ret = device_claim(&bm->dev);
199 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
201 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
205 device_set_present(&bst->dev, true);
206 device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
214 device_release(&bm->dev);
220 bridge_remove_member(struct bridge_member *bm)
222 struct bridge_state *bst = bm->bst;
227 if (bm == bst->primary_port)
228 bridge_reset_primary(bst);
231 bridge_disable_member(bm);
234 bm->bst->n_present--;
236 if (bst->config.bridge_empty)
239 bst->force_active = false;
240 if (bst->n_present == 0)
241 device_set_present(&bst->dev, false);
245 bridge_free_member(struct bridge_member *bm)
247 struct device *dev = bm->dev.dev;
249 bridge_remove_member(bm);
250 device_remove_user(&bm->dev);
253 * When reloading the config and moving a device from one bridge to
254 * another, the other bridge may have tried to claim this device
255 * before it was removed here.
256 * Ensure that claiming the device is retried by toggling its present
260 device_set_present(dev, false);
261 device_set_present(dev, true);
268 bridge_check_retry(struct bridge_state *bst)
273 uloop_timeout_set(&bst->retry, 100);
277 bridge_member_cb(struct device_user *dev, enum device_event ev)
279 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
280 struct bridge_state *bst = bm->bst;
284 assert(!bm->present);
289 if (bst->n_present == 1)
290 device_set_present(&bst->dev, true);
291 if (bst->dev.active && !bridge_enable_member(bm)) {
293 * Adding a bridge member can overwrite the bridge mtu
294 * in the kernel, apply the bridge settings in case the
297 system_if_apply_settings(&bst->dev, &bst->dev.settings,
302 case DEV_EVENT_REMOVE:
304 vlist_delete(&bst->members, &bm->node);
309 bridge_remove_member(bm);
318 bridge_set_down(struct bridge_state *bst)
320 struct bridge_member *bm;
322 bst->set_state(&bst->dev, false);
324 vlist_for_each_element(&bst->members, bm, node)
325 bridge_disable_member(bm);
327 bridge_disable_interface(bst);
333 bridge_set_up(struct bridge_state *bst)
335 struct bridge_member *bm;
338 if (!bst->n_present) {
339 if (!bst->force_active)
342 ret = bridge_enable_interface(bst);
348 vlist_for_each_element(&bst->members, bm, node)
349 bridge_enable_member(bm);
350 bridge_check_retry(bst);
352 if (!bst->force_active && !bst->n_present) {
353 /* initialization of all member interfaces failed */
354 bridge_disable_interface(bst);
355 device_set_present(&bst->dev, false);
359 bridge_reset_primary(bst);
360 ret = bst->set_state(&bst->dev, true);
362 bridge_set_down(bst);
368 bridge_set_state(struct device *dev, bool up)
370 struct bridge_state *bst;
372 bst = container_of(dev, struct bridge_state, dev);
375 return bridge_set_up(bst);
377 return bridge_set_down(bst);
380 static struct bridge_member *
381 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
383 struct bridge_member *bm;
385 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
390 bm->dev.cb = bridge_member_cb;
391 bm->dev.hotplug = hotplug;
392 strcpy(bm->name, dev->ifname);
394 vlist_add(&bst->members, &bm->node, bm->name);
395 // Need to look up the bridge member again as the above
396 // created pointer will be freed in case the bridge member
398 bm = vlist_find(&bst->members, dev->ifname, bm, node);
400 bm->node.version = -1;
406 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
407 struct vlist_node *node_old)
409 struct bridge_member *bm;
413 bm = container_of(node_new, struct bridge_member, node);
422 device_add_user(&bm->dev, dev);
427 bm = container_of(node_old, struct bridge_member, node);
428 bridge_free_member(bm);
434 bridge_add_member(struct bridge_state *bst, const char *name)
438 dev = device_get(name, true);
442 bridge_create_member(bst, dev, false);
446 bridge_hotplug_add(struct device *dev, struct device *member)
448 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
450 bridge_create_member(bst, member, true);
456 bridge_hotplug_del(struct device *dev, struct device *member)
458 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
459 struct bridge_member *bm;
461 bm = vlist_find(&bst->members, member->ifname, bm, node);
463 return UBUS_STATUS_NOT_FOUND;
465 vlist_delete(&bst->members, &bm->node);
470 bridge_hotplug_prepare(struct device *dev)
472 struct bridge_state *bst;
474 bst = container_of(dev, struct bridge_state, dev);
475 bst->force_active = true;
476 device_set_present(&bst->dev, true);
481 static const struct device_hotplug_ops bridge_ops = {
482 .prepare = bridge_hotplug_prepare,
483 .add = bridge_hotplug_add,
484 .del = bridge_hotplug_del
488 bridge_free(struct device *dev)
490 struct bridge_state *bst;
492 bst = container_of(dev, struct bridge_state, dev);
493 vlist_flush_all(&bst->members);
494 free(bst->config_data);
499 bridge_dump_info(struct device *dev, struct blob_buf *b)
501 struct bridge_state *bst;
502 struct bridge_member *bm;
505 bst = container_of(dev, struct bridge_state, dev);
507 system_if_dump_info(dev, b);
508 list = blobmsg_open_array(b, "bridge-members");
510 vlist_for_each_element(&bst->members, bm, node)
511 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
513 blobmsg_close_array(b, list);
517 bridge_config_init(struct device *dev)
519 struct bridge_state *bst;
520 struct blob_attr *cur;
523 bst = container_of(dev, struct bridge_state, dev);
525 if (bst->config.bridge_empty) {
526 bst->force_active = true;
527 device_set_present(&bst->dev, true);
531 vlist_update(&bst->members);
533 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
534 bridge_add_member(bst, blobmsg_data(cur));
537 vlist_flush(&bst->members);
538 bridge_check_retry(bst);
542 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
544 struct bridge_config *cfg = &bst->config;
545 struct blob_attr *cur;
549 cfg->forward_delay = 2;
550 cfg->igmp_snoop = true;
551 cfg->bridge_empty = false;
552 cfg->priority = 0x7FFF;
554 if ((cur = tb[BRIDGE_ATTR_STP]))
555 cfg->stp = blobmsg_get_bool(cur);
557 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
558 cfg->forward_delay = blobmsg_get_u32(cur);
560 if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
561 cfg->priority = blobmsg_get_u32(cur);
563 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
564 cfg->igmp_snoop = blobmsg_get_bool(cur);
566 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
567 cfg->ageing_time = blobmsg_get_u32(cur);
568 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
571 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
572 cfg->hello_time = blobmsg_get_u32(cur);
573 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
576 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
577 cfg->max_age = blobmsg_get_u32(cur);
578 cfg->flags |= BRIDGE_OPT_MAX_AGE;
581 if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
582 cfg->bridge_empty = blobmsg_get_bool(cur);
586 bridge_reload(struct device *dev, struct blob_attr *attr)
588 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
589 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
590 enum dev_change_type ret = DEV_CONFIG_APPLIED;
592 struct bridge_state *bst;
594 BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
595 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
597 bst = container_of(dev, struct bridge_state, dev);
598 attr = blob_memdup(attr);
600 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
601 blob_data(attr), blob_len(attr));
602 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
603 blob_data(attr), blob_len(attr));
605 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
606 device_init_settings(dev, tb_dev);
607 bridge_apply_settings(bst, tb_br);
609 if (bst->config_data) {
610 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
611 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
613 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
614 blob_data(bst->config_data), blob_len(bst->config_data));
617 uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
618 if (diff & ~(1 << DEV_ATTR_IFNAME))
619 ret = DEV_CONFIG_RESTART;
621 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
622 blob_data(bst->config_data), blob_len(bst->config_data));
625 uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
626 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
627 ret = DEV_CONFIG_RESTART;
629 bridge_config_init(dev);
632 free(bst->config_data);
633 bst->config_data = attr;
638 bridge_retry_members(struct uloop_timeout *timeout)
640 struct bridge_state *bst = container_of(timeout, struct bridge_state, retry);
641 struct bridge_member *bm;
644 vlist_for_each_element(&bst->members, bm, node) {
648 if (!bm->dev.dev->present)
653 bridge_enable_member(bm);
657 static struct device *
658 bridge_create(const char *name, struct blob_attr *attr)
660 struct bridge_state *bst;
661 struct device *dev = NULL;
663 bst = calloc(1, sizeof(*bst));
668 device_init(dev, &bridge_device_type, name);
669 dev->config_pending = true;
670 bst->retry.cb = bridge_retry_members;
672 bst->set_state = dev->set_state;
673 dev->set_state = bridge_set_state;
675 dev->hotplug_ops = &bridge_ops;
677 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
678 bst->members.keep_old = true;
679 bridge_reload(dev, attr);