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);
147 bridge_enable_member(struct bridge_member *bm)
149 struct bridge_state *bst = bm->bst;
155 ret = device_claim(&bm->dev);
159 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
161 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
174 bridge_remove_member(struct bridge_member *bm)
176 struct bridge_state *bst = bm->bst;
181 if (bm == bst->primary_port);
182 bridge_reset_primary(bst);
185 bridge_disable_member(bm);
188 bm->bst->n_present--;
190 if (bst->config.bridge_empty)
193 bst->force_active = false;
194 if (bst->n_present == 0)
195 device_set_present(&bst->dev, false);
199 bridge_free_member(struct bridge_member *bm)
201 struct device *dev = bm->dev.dev;
203 bridge_remove_member(bm);
204 device_remove_user(&bm->dev);
207 * When reloading the config and moving a device from one bridge to
208 * another, the other bridge may have tried to claim this device
209 * before it was removed here.
210 * Ensure that claiming the device is retried by toggling its present
214 device_set_present(dev, false);
215 device_set_present(dev, true);
222 bridge_member_cb(struct device_user *dev, enum device_event ev)
224 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
225 struct bridge_state *bst = bm->bst;
229 assert(!bm->present);
235 bridge_enable_member(bm);
236 else if (bst->n_present == 1)
237 device_set_present(&bst->dev, true);
240 case DEV_EVENT_REMOVE:
242 vlist_delete(&bst->members, &bm->node);
247 bridge_remove_member(bm);
256 bridge_set_down(struct bridge_state *bst)
258 struct bridge_member *bm;
260 bst->set_state(&bst->dev, false);
262 vlist_for_each_element(&bst->members, bm, node)
263 bridge_disable_member(bm);
265 system_bridge_delbr(&bst->dev);
271 bridge_set_up(struct bridge_state *bst)
273 struct bridge_member *bm;
276 if (!bst->force_active && !bst->n_present)
279 ret = system_bridge_addbr(&bst->dev, &bst->config);
283 vlist_for_each_element(&bst->members, bm, node)
284 bridge_enable_member(bm);
286 if (!bst->force_active && !bst->n_present) {
287 /* initialization of all member interfaces failed */
288 system_bridge_delbr(&bst->dev);
289 device_set_present(&bst->dev, false);
293 bridge_reset_primary(bst);
294 ret = bst->set_state(&bst->dev, true);
296 bridge_set_down(bst);
303 bridge_set_state(struct device *dev, bool up)
305 struct bridge_state *bst;
307 bst = container_of(dev, struct bridge_state, dev);
310 return bridge_set_up(bst);
312 return bridge_set_down(bst);
315 static struct bridge_member *
316 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
318 struct bridge_member *bm;
320 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
322 bm->dev.cb = bridge_member_cb;
323 bm->dev.hotplug = hotplug;
324 strcpy(bm->name, dev->ifname);
326 vlist_add(&bst->members, &bm->node, bm->name);
328 bm->node.version = -1;
334 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
335 struct vlist_node *node_old)
337 struct bridge_member *bm;
341 bm = container_of(node_new, struct bridge_member, node);
350 device_add_user(&bm->dev, dev);
355 bm = container_of(node_old, struct bridge_member, node);
356 bridge_free_member(bm);
362 bridge_add_member(struct bridge_state *bst, const char *name)
366 dev = device_get(name, true);
370 bridge_create_member(bst, dev, false);
374 bridge_hotplug_add(struct device *dev, struct device *member)
376 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
378 bridge_create_member(bst, member, true);
384 bridge_hotplug_del(struct device *dev, struct device *member)
386 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
387 struct bridge_member *bm;
389 bm = vlist_find(&bst->members, member->ifname, bm, node);
391 return UBUS_STATUS_NOT_FOUND;
393 vlist_delete(&bst->members, &bm->node);
398 bridge_hotplug_prepare(struct device *dev)
400 struct bridge_state *bst;
402 bst = container_of(dev, struct bridge_state, dev);
403 bst->force_active = true;
404 device_set_present(&bst->dev, true);
409 static const struct device_hotplug_ops bridge_ops = {
410 .prepare = bridge_hotplug_prepare,
411 .add = bridge_hotplug_add,
412 .del = bridge_hotplug_del
416 bridge_free(struct device *dev)
418 struct bridge_state *bst;
420 bst = container_of(dev, struct bridge_state, dev);
421 vlist_flush_all(&bst->members);
426 bridge_dump_info(struct device *dev, struct blob_buf *b)
428 struct bridge_state *bst;
429 struct bridge_member *bm;
432 bst = container_of(dev, struct bridge_state, dev);
434 system_if_dump_info(dev, b);
435 list = blobmsg_open_array(b, "bridge-members");
437 vlist_for_each_element(&bst->members, bm, node)
438 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
440 blobmsg_close_array(b, list);
444 bridge_config_init(struct device *dev)
446 struct bridge_state *bst;
447 struct blob_attr *cur;
450 bst = container_of(dev, struct bridge_state, dev);
452 if (bst->config.bridge_empty) {
453 bst->force_active = true;
454 device_set_present(&bst->dev, true);
457 vlist_update(&bst->members);
459 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
460 bridge_add_member(bst, blobmsg_data(cur));
463 vlist_flush(&bst->members);
467 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
469 struct bridge_config *cfg = &bst->config;
470 struct blob_attr *cur;
474 cfg->forward_delay = 2;
475 cfg->igmp_snoop = false;
476 cfg->bridge_empty = false;
477 cfg->priority = 0x7FFF;
479 if ((cur = tb[BRIDGE_ATTR_STP]))
480 cfg->stp = blobmsg_get_bool(cur);
482 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
483 cfg->forward_delay = blobmsg_get_u32(cur);
485 if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
486 cfg->priority = blobmsg_get_u32(cur);
488 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
489 cfg->igmp_snoop = blobmsg_get_bool(cur);
491 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
492 cfg->ageing_time = blobmsg_get_u32(cur);
493 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
496 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
497 cfg->hello_time = blobmsg_get_u32(cur);
498 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
501 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
502 cfg->max_age = blobmsg_get_u32(cur);
503 cfg->flags |= BRIDGE_OPT_MAX_AGE;
506 if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
507 cfg->bridge_empty = blobmsg_get_bool(cur);
511 bridge_reload(struct device *dev, struct blob_attr *attr)
513 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
514 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
515 enum dev_change_type ret = DEV_CONFIG_APPLIED;
517 struct bridge_state *bst;
519 BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
520 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
522 bst = container_of(dev, struct bridge_state, dev);
524 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
525 blob_data(attr), blob_len(attr));
526 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
527 blob_data(attr), blob_len(attr));
529 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
530 device_init_settings(dev, tb_dev);
531 bridge_apply_settings(bst, tb_br);
533 if (bst->config_data) {
534 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
535 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
537 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
538 blob_data(bst->config_data), blob_len(bst->config_data));
541 uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
542 if (diff & ~(1 << DEV_ATTR_IFNAME))
543 ret = DEV_CONFIG_RESTART;
545 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
546 blob_data(bst->config_data), blob_len(bst->config_data));
549 uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
550 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
551 ret = DEV_CONFIG_RESTART;
553 bridge_config_init(dev);
556 bst->config_data = attr;
560 static struct device *
561 bridge_create(const char *name, struct blob_attr *attr)
563 struct bridge_state *bst;
564 struct device *dev = NULL;
566 bst = calloc(1, sizeof(*bst));
571 device_init(dev, &bridge_device_type, name);
572 dev->config_pending = true;
574 bst->set_state = dev->set_state;
575 dev->set_state = bridge_set_state;
577 dev->hotplug_ops = &bridge_ops;
579 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
580 bst->members.keep_old = true;
581 bridge_reload(dev, attr);