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 bridge_member *primary_port;
92 struct vlist_tree members;
96 struct bridge_member {
97 struct vlist_node node;
98 struct bridge_state *bst;
99 struct device_user dev;
105 bridge_reset_primary(struct bridge_state *bst)
107 struct bridge_member *bm;
109 if (!bst->primary_port &&
110 (bst->dev.settings.flags & DEV_OPT_MACADDR))
113 bst->primary_port = NULL;
114 bst->dev.settings.flags &= ~DEV_OPT_MACADDR;
115 vlist_for_each_element(&bst->members, bm, node) {
121 bst->primary_port = bm;
122 if (bm->dev.dev->settings.flags & DEV_OPT_MACADDR)
123 macaddr = bm->dev.dev->settings.macaddr;
125 macaddr = bm->dev.dev->orig_settings.macaddr;
126 memcpy(bst->dev.settings.macaddr, macaddr, 6);
127 bst->dev.settings.flags |= DEV_OPT_MACADDR;
133 bridge_disable_member(struct bridge_member *bm)
135 struct bridge_state *bst = bm->bst;
140 system_bridge_delif(&bst->dev, bm->dev.dev);
141 device_release(&bm->dev);
143 device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
149 bridge_enable_member(struct bridge_member *bm)
151 struct bridge_state *bst = bm->bst;
157 /* Disable IPv6 for bridge members */
158 if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
159 bm->dev.dev->settings.ipv6 = 0;
160 bm->dev.dev->settings.flags |= DEV_OPT_IPV6;
163 ret = device_claim(&bm->dev);
167 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
169 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
173 device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
184 bridge_remove_member(struct bridge_member *bm)
186 struct bridge_state *bst = bm->bst;
191 if (bm == bst->primary_port)
192 bridge_reset_primary(bst);
195 bridge_disable_member(bm);
198 bm->bst->n_present--;
200 if (bst->config.bridge_empty)
203 bst->force_active = false;
204 if (bst->n_present == 0)
205 device_set_present(&bst->dev, false);
209 bridge_free_member(struct bridge_member *bm)
211 struct device *dev = bm->dev.dev;
213 bridge_remove_member(bm);
214 device_remove_user(&bm->dev);
217 * When reloading the config and moving a device from one bridge to
218 * another, the other bridge may have tried to claim this device
219 * before it was removed here.
220 * Ensure that claiming the device is retried by toggling its present
224 device_set_present(dev, false);
225 device_set_present(dev, true);
232 bridge_member_cb(struct device_user *dev, enum device_event ev)
234 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
235 struct bridge_state *bst = bm->bst;
239 assert(!bm->present);
244 if (bst->n_present == 1)
245 device_set_present(&bst->dev, true);
246 if (bst->dev.active && !bridge_enable_member(bm)) {
248 * Adding a bridge member can overwrite the bridge mtu
249 * in the kernel, apply the bridge settings in case the
252 system_if_apply_settings(&bst->dev, &bst->dev.settings,
257 case DEV_EVENT_REMOVE:
259 vlist_delete(&bst->members, &bm->node);
264 bridge_remove_member(bm);
273 bridge_set_down(struct bridge_state *bst)
275 struct bridge_member *bm;
277 bst->set_state(&bst->dev, false);
279 vlist_for_each_element(&bst->members, bm, node)
280 bridge_disable_member(bm);
282 system_bridge_delbr(&bst->dev);
288 bridge_set_up(struct bridge_state *bst)
290 struct bridge_member *bm;
293 if (!bst->force_active && !bst->n_present)
296 ret = system_bridge_addbr(&bst->dev, &bst->config);
300 vlist_for_each_element(&bst->members, bm, node)
301 bridge_enable_member(bm);
303 if (!bst->force_active && !bst->n_present) {
304 /* initialization of all member interfaces failed */
305 system_bridge_delbr(&bst->dev);
306 device_set_present(&bst->dev, false);
310 bridge_reset_primary(bst);
311 ret = bst->set_state(&bst->dev, true);
313 bridge_set_down(bst);
320 bridge_set_state(struct device *dev, bool up)
322 struct bridge_state *bst;
324 bst = container_of(dev, struct bridge_state, dev);
327 return bridge_set_up(bst);
329 return bridge_set_down(bst);
332 static struct bridge_member *
333 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
335 struct bridge_member *bm;
337 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
342 bm->dev.cb = bridge_member_cb;
343 bm->dev.hotplug = hotplug;
344 strcpy(bm->name, dev->ifname);
346 vlist_add(&bst->members, &bm->node, bm->name);
347 // Need to look up the bridge member again as the above
348 // created pointer will be freed in case the bridge member
350 bm = vlist_find(&bst->members, dev->ifname, bm, node);
352 bm->node.version = -1;
358 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
359 struct vlist_node *node_old)
361 struct bridge_member *bm;
365 bm = container_of(node_new, struct bridge_member, node);
374 device_add_user(&bm->dev, dev);
379 bm = container_of(node_old, struct bridge_member, node);
380 bridge_free_member(bm);
386 bridge_add_member(struct bridge_state *bst, const char *name)
390 dev = device_get(name, true);
394 bridge_create_member(bst, dev, false);
398 bridge_hotplug_add(struct device *dev, struct device *member)
400 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
402 bridge_create_member(bst, member, true);
408 bridge_hotplug_del(struct device *dev, struct device *member)
410 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
411 struct bridge_member *bm;
413 bm = vlist_find(&bst->members, member->ifname, bm, node);
415 return UBUS_STATUS_NOT_FOUND;
417 vlist_delete(&bst->members, &bm->node);
422 bridge_hotplug_prepare(struct device *dev)
424 struct bridge_state *bst;
426 bst = container_of(dev, struct bridge_state, dev);
427 bst->force_active = true;
428 device_set_present(&bst->dev, true);
433 static const struct device_hotplug_ops bridge_ops = {
434 .prepare = bridge_hotplug_prepare,
435 .add = bridge_hotplug_add,
436 .del = bridge_hotplug_del
440 bridge_free(struct device *dev)
442 struct bridge_state *bst;
444 bst = container_of(dev, struct bridge_state, dev);
445 vlist_flush_all(&bst->members);
450 bridge_dump_info(struct device *dev, struct blob_buf *b)
452 struct bridge_state *bst;
453 struct bridge_member *bm;
456 bst = container_of(dev, struct bridge_state, dev);
458 system_if_dump_info(dev, b);
459 list = blobmsg_open_array(b, "bridge-members");
461 vlist_for_each_element(&bst->members, bm, node)
462 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
464 blobmsg_close_array(b, list);
468 bridge_config_init(struct device *dev)
470 struct bridge_state *bst;
471 struct blob_attr *cur;
474 bst = container_of(dev, struct bridge_state, dev);
476 if (bst->config.bridge_empty) {
477 bst->force_active = true;
478 device_set_present(&bst->dev, true);
481 vlist_update(&bst->members);
483 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
484 bridge_add_member(bst, blobmsg_data(cur));
487 vlist_flush(&bst->members);
491 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
493 struct bridge_config *cfg = &bst->config;
494 struct blob_attr *cur;
498 cfg->forward_delay = 2;
499 cfg->igmp_snoop = true;
500 cfg->bridge_empty = false;
501 cfg->priority = 0x7FFF;
503 if ((cur = tb[BRIDGE_ATTR_STP]))
504 cfg->stp = blobmsg_get_bool(cur);
506 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
507 cfg->forward_delay = blobmsg_get_u32(cur);
509 if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
510 cfg->priority = blobmsg_get_u32(cur);
512 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
513 cfg->igmp_snoop = blobmsg_get_bool(cur);
515 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
516 cfg->ageing_time = blobmsg_get_u32(cur);
517 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
520 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
521 cfg->hello_time = blobmsg_get_u32(cur);
522 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
525 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
526 cfg->max_age = blobmsg_get_u32(cur);
527 cfg->flags |= BRIDGE_OPT_MAX_AGE;
530 if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
531 cfg->bridge_empty = blobmsg_get_bool(cur);
535 bridge_reload(struct device *dev, struct blob_attr *attr)
537 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
538 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
539 enum dev_change_type ret = DEV_CONFIG_APPLIED;
541 struct bridge_state *bst;
543 BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
544 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
546 bst = container_of(dev, struct bridge_state, dev);
548 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
549 blob_data(attr), blob_len(attr));
550 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
551 blob_data(attr), blob_len(attr));
553 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
554 device_init_settings(dev, tb_dev);
555 bridge_apply_settings(bst, tb_br);
557 if (bst->config_data) {
558 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
559 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
561 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
562 blob_data(bst->config_data), blob_len(bst->config_data));
565 uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
566 if (diff & ~(1 << DEV_ATTR_IFNAME))
567 ret = DEV_CONFIG_RESTART;
569 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
570 blob_data(bst->config_data), blob_len(bst->config_data));
573 uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
574 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
575 ret = DEV_CONFIG_RESTART;
577 bridge_config_init(dev);
580 bst->config_data = attr;
584 static struct device *
585 bridge_create(const char *name, struct blob_attr *attr)
587 struct bridge_state *bst;
588 struct device *dev = NULL;
590 bst = calloc(1, sizeof(*bst));
595 device_init(dev, &bridge_device_type, name);
596 dev->config_pending = true;
598 bst->set_state = dev->set_state;
599 dev->set_state = bridge_set_state;
601 dev->hotplug_ops = &bridge_ops;
603 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
604 bst->members.keep_old = true;
605 bridge_reload(dev, attr);