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,
37 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
38 [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
39 [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
40 [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
41 [BRIDGE_ATTR_PRIORITY] = { "priority", BLOBMSG_TYPE_INT32 },
42 [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
43 [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
44 [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
45 [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
48 static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
49 [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
52 static const struct config_param_list bridge_attr_list = {
53 .n_params = __BRIDGE_ATTR_MAX,
54 .params = bridge_attrs,
55 .info = bridge_attr_info,
58 .next = { &device_attr_list },
61 static struct device *bridge_create(const char *name, struct blob_attr *attr);
62 static void bridge_config_init(struct device *dev);
63 static void bridge_free(struct device *dev);
64 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
66 bridge_reload(struct device *dev, struct blob_attr *attr);
68 const struct device_type bridge_device_type = {
70 .config_params = &bridge_attr_list,
72 .create = bridge_create,
73 .config_init = bridge_config_init,
74 .reload = bridge_reload,
76 .dump_info = bridge_dump_info,
81 device_state_cb set_state;
83 struct blob_attr *config_data;
84 struct bridge_config config;
85 struct blob_attr *ifnames;
89 struct bridge_member *primary_port;
90 struct vlist_tree members;
94 struct bridge_member {
95 struct vlist_node node;
96 struct bridge_state *bst;
97 struct device_user dev;
103 bridge_reset_primary(struct bridge_state *bst)
105 struct bridge_member *bm;
107 if (!bst->primary_port &&
108 (bst->dev.settings.flags & DEV_OPT_MACADDR))
111 bst->primary_port = NULL;
112 bst->dev.settings.flags &= ~DEV_OPT_MACADDR;
113 vlist_for_each_element(&bst->members, bm, node) {
119 bst->primary_port = bm;
120 if (bm->dev.dev->settings.flags & DEV_OPT_MACADDR)
121 macaddr = bm->dev.dev->settings.macaddr;
123 macaddr = bm->dev.dev->orig_settings.macaddr;
124 memcpy(bst->dev.settings.macaddr, macaddr, 6);
125 bst->dev.settings.flags |= DEV_OPT_MACADDR;
131 bridge_disable_member(struct bridge_member *bm)
133 struct bridge_state *bst = bm->bst;
138 system_bridge_delif(&bst->dev, bm->dev.dev);
139 device_release(&bm->dev);
145 bridge_enable_member(struct bridge_member *bm)
147 struct bridge_state *bst = bm->bst;
153 ret = device_claim(&bm->dev);
157 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
159 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
172 bridge_remove_member(struct bridge_member *bm)
174 struct bridge_state *bst = bm->bst;
179 if (bm == bst->primary_port);
180 bridge_reset_primary(bst);
183 bridge_disable_member(bm);
186 bm->bst->n_present--;
188 bst->force_active = false;
189 if (bst->n_present == 0)
190 device_set_present(&bst->dev, false);
194 bridge_free_member(struct bridge_member *bm)
196 struct device *dev = bm->dev.dev;
198 bridge_remove_member(bm);
199 device_remove_user(&bm->dev);
202 * When reloading the config and moving a device from one bridge to
203 * another, the other bridge may have tried to claim this device
204 * before it was removed here.
205 * Ensure that claiming the device is retried by toggling its present
209 device_set_present(dev, false);
210 device_set_present(dev, true);
217 bridge_member_cb(struct device_user *dev, enum device_event ev)
219 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
220 struct bridge_state *bst = bm->bst;
224 assert(!bm->present);
230 bridge_enable_member(bm);
231 else if (bst->n_present == 1)
232 device_set_present(&bst->dev, true);
235 case DEV_EVENT_REMOVE:
237 vlist_delete(&bst->members, &bm->node);
242 bridge_remove_member(bm);
251 bridge_set_down(struct bridge_state *bst)
253 struct bridge_member *bm;
255 bst->set_state(&bst->dev, false);
257 vlist_for_each_element(&bst->members, bm, node)
258 bridge_disable_member(bm);
260 system_bridge_delbr(&bst->dev);
266 bridge_set_up(struct bridge_state *bst)
268 struct bridge_member *bm;
271 if (!bst->force_active && !bst->n_present)
274 ret = system_bridge_addbr(&bst->dev, &bst->config);
278 vlist_for_each_element(&bst->members, bm, node)
279 bridge_enable_member(bm);
281 if (!bst->force_active && !bst->n_present) {
282 /* initialization of all member interfaces failed */
283 system_bridge_delbr(&bst->dev);
284 device_set_present(&bst->dev, false);
288 bridge_reset_primary(bst);
289 ret = bst->set_state(&bst->dev, true);
291 bridge_set_down(bst);
298 bridge_set_state(struct device *dev, bool up)
300 struct bridge_state *bst;
302 bst = container_of(dev, struct bridge_state, dev);
305 return bridge_set_up(bst);
307 return bridge_set_down(bst);
310 static struct bridge_member *
311 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
313 struct bridge_member *bm;
315 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
317 bm->dev.cb = bridge_member_cb;
318 bm->dev.hotplug = hotplug;
319 strcpy(bm->name, dev->ifname);
321 vlist_add(&bst->members, &bm->node, bm->name);
323 bm->node.version = -1;
329 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
330 struct vlist_node *node_old)
332 struct bridge_member *bm;
336 bm = container_of(node_new, struct bridge_member, node);
345 device_add_user(&bm->dev, dev);
350 bm = container_of(node_old, struct bridge_member, node);
351 bridge_free_member(bm);
357 bridge_add_member(struct bridge_state *bst, const char *name)
361 dev = device_get(name, true);
365 bridge_create_member(bst, dev, false);
369 bridge_hotplug_add(struct device *dev, struct device *member)
371 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
373 bridge_create_member(bst, member, true);
379 bridge_hotplug_del(struct device *dev, struct device *member)
381 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
382 struct bridge_member *bm;
384 bm = vlist_find(&bst->members, member->ifname, bm, node);
386 return UBUS_STATUS_NOT_FOUND;
388 vlist_delete(&bst->members, &bm->node);
393 bridge_hotplug_prepare(struct device *dev)
395 struct bridge_state *bst;
397 bst = container_of(dev, struct bridge_state, dev);
398 bst->force_active = true;
399 device_set_present(&bst->dev, true);
404 static const struct device_hotplug_ops bridge_ops = {
405 .prepare = bridge_hotplug_prepare,
406 .add = bridge_hotplug_add,
407 .del = bridge_hotplug_del
411 bridge_free(struct device *dev)
413 struct bridge_state *bst;
415 bst = container_of(dev, struct bridge_state, dev);
416 vlist_flush_all(&bst->members);
421 bridge_dump_info(struct device *dev, struct blob_buf *b)
423 struct bridge_state *bst;
424 struct bridge_member *bm;
427 bst = container_of(dev, struct bridge_state, dev);
429 system_if_dump_info(dev, b);
430 list = blobmsg_open_array(b, "bridge-members");
432 vlist_for_each_element(&bst->members, bm, node)
433 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
435 blobmsg_close_array(b, list);
439 bridge_config_init(struct device *dev)
441 struct bridge_state *bst;
442 struct blob_attr *cur;
445 bst = container_of(dev, struct bridge_state, dev);
450 vlist_update(&bst->members);
451 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
452 bridge_add_member(bst, blobmsg_data(cur));
454 vlist_flush(&bst->members);
458 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
460 struct bridge_config *cfg = &bst->config;
461 struct blob_attr *cur;
465 cfg->forward_delay = 2;
466 cfg->igmp_snoop = false;
467 cfg->priority = 0x7FFF;
469 if ((cur = tb[BRIDGE_ATTR_STP]))
470 cfg->stp = blobmsg_get_bool(cur);
472 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
473 cfg->forward_delay = blobmsg_get_u32(cur);
475 if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
476 cfg->priority = blobmsg_get_u32(cur);
478 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
479 cfg->igmp_snoop = blobmsg_get_bool(cur);
481 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
482 cfg->ageing_time = blobmsg_get_u32(cur);
483 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
486 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
487 cfg->hello_time = blobmsg_get_u32(cur);
488 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
491 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
492 cfg->max_age = blobmsg_get_u32(cur);
493 cfg->flags |= BRIDGE_OPT_MAX_AGE;
498 bridge_reload(struct device *dev, struct blob_attr *attr)
500 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
501 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
502 enum dev_change_type ret = DEV_CONFIG_APPLIED;
504 struct bridge_state *bst;
506 BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
507 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
509 bst = container_of(dev, struct bridge_state, dev);
511 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
512 blob_data(attr), blob_len(attr));
513 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
514 blob_data(attr), blob_len(attr));
516 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
517 device_init_settings(dev, tb_dev);
518 bridge_apply_settings(bst, tb_br);
520 if (bst->config_data) {
521 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
522 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
524 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
525 blob_data(bst->config_data), blob_len(bst->config_data));
528 config_diff(tb_dev, otb_dev, &device_attr_list, &diff);
529 if (diff & ~(1 << DEV_ATTR_IFNAME))
530 ret = DEV_CONFIG_RESTART;
532 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
533 blob_data(bst->config_data), blob_len(bst->config_data));
536 config_diff(tb_br, otb_br, &bridge_attr_list, &diff);
537 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
538 ret = DEV_CONFIG_RESTART;
540 bridge_config_init(dev);
543 bst->config_data = attr;
547 static struct device *
548 bridge_create(const char *name, struct blob_attr *attr)
550 struct bridge_state *bst;
551 struct device *dev = NULL;
553 bst = calloc(1, sizeof(*bst));
558 device_init(dev, &bridge_device_type, name);
559 dev->config_pending = true;
561 bst->set_state = dev->set_state;
562 dev->set_state = bridge_set_state;
564 dev->hotplug_ops = &bridge_ops;
566 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
567 bst->members.keep_old = true;
568 bridge_reload(dev, attr);