add -Wmissing-declarations to cflags
[project/netifd.git] / bridge.c
index c3b02bd..1f1a220 100644 (file)
--- a/bridge.c
+++ b/bridge.c
 enum {
        BRIDGE_ATTR_IFNAME,
        BRIDGE_ATTR_STP,
+       BRIDGE_ATTR_FORWARD_DELAY,
+       BRIDGE_ATTR_IGMP_SNOOP,
+       BRIDGE_ATTR_AGEING_TIME,
+       BRIDGE_ATTR_HELLO_TIME,
+       BRIDGE_ATTR_MAX_AGE,
        __BRIDGE_ATTR_MAX
 };
 
 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
        [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
        [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
+       [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
 };
 
 static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
@@ -33,10 +43,10 @@ static const struct config_param_list bridge_attr_list = {
        .next = { &device_attr_list },
 };
 
-static struct device *bridge_create(struct blob_attr *attr);
+static struct device *bridge_create(const char *name, struct blob_attr *attr);
 static void bridge_config_init(struct device *dev);
 static void bridge_free(struct device *dev);
-static void bridge_dump_status(struct device *dev, struct blob_buf *b);
+static void bridge_dump_info(struct device *dev, struct blob_buf *b);
 
 const struct device_type bridge_device_type = {
        .name = "Bridge",
@@ -45,13 +55,14 @@ const struct device_type bridge_device_type = {
        .create = bridge_create,
        .config_init = bridge_config_init,
        .free = bridge_free,
-       .dump_status = bridge_dump_status,
+       .dump_info = bridge_dump_info,
 };
 
 struct bridge_state {
        struct device dev;
        device_state_cb set_state;
 
+       struct bridge_config config;
        struct blob_attr *ifnames;
        bool active;
 
@@ -66,6 +77,8 @@ struct bridge_member {
        bool present;
 };
 
+static void bridge_free_member(struct bridge_member *bm);
+
 static int
 bridge_disable_member(struct bridge_member *bm)
 {
@@ -136,6 +149,8 @@ bridge_member_cb(struct device_user *dev, enum device_event ev)
                if (bst->n_present == 0)
                        device_set_present(&bst->dev, false);
 
+               if (dev->hotplug)
+                       bridge_free_member(bm);
                break;
        default:
                return;
@@ -166,7 +181,7 @@ bridge_set_up(struct bridge_state *bst)
        if (!bst->n_present)
                return -ENOENT;
 
-       ret = system_bridge_addbr(&bst->dev);
+       ret = system_bridge_addbr(&bst->dev, &bst->config);
        if (ret < 0)
                goto out;
 
@@ -202,7 +217,7 @@ bridge_set_state(struct device *dev, bool up)
 }
 
 static struct bridge_member *
-bridge_create_member(struct bridge_state *bst, struct device *dev)
+bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
 {
        struct bridge_member *bm;
 
@@ -210,6 +225,7 @@ bridge_create_member(struct bridge_state *bst, struct device *dev)
        bm->bst = bst;
        bm->dev.cb = bridge_member_cb;
        device_add_user(&bm->dev, dev);
+       bm->dev.hotplug = hotplug;
 
        list_add_tail(&bm->list, &bst->members);
 
@@ -243,7 +259,7 @@ bridge_add_member(struct bridge_state *bst, const char *name)
        if (!dev)
                return;
 
-       bridge_create_member(bst, dev);
+       bridge_create_member(bst, dev, false);
 }
 
 static int
@@ -251,7 +267,7 @@ bridge_hotplug_add(struct device *dev, struct device *member)
 {
        struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
 
-       bridge_create_member(bst, member);
+       bridge_create_member(bst, member, true);
 
        return 0;
 }
@@ -294,7 +310,7 @@ bridge_free(struct device *dev)
 }
 
 static void
-bridge_dump_status(struct device *dev, struct blob_buf *b)
+bridge_dump_info(struct device *dev, struct blob_buf *b)
 {
        struct bridge_state *bst;
        struct bridge_member *bm;
@@ -322,28 +338,58 @@ bridge_config_init(struct device *dev)
        }
 }
 
+static void
+bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
+{
+       struct bridge_config *cfg = &bst->config;
+       struct blob_attr *cur;
+
+       /* defaults */
+       cfg->stp = true;
+       cfg->forward_delay = 1;
+       cfg->igmp_snoop = true;
+
+       if ((cur = tb[BRIDGE_ATTR_STP]))
+               cfg->stp = blobmsg_get_bool(cur);
+
+       if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
+               cfg->forward_delay = blobmsg_get_u32(cur);
+
+       if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
+               cfg->igmp_snoop = blobmsg_get_bool(cur);
+
+       if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
+               cfg->ageing_time = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_AGEING_TIME;
+       }
+
+       if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
+               cfg->hello_time = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_HELLO_TIME;
+       }
+
+       if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
+               cfg->max_age = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_MAX_AGE;
+       }
+}
+
 static struct device *
-bridge_create(struct blob_attr *attr)
+bridge_create(const char *name, struct blob_attr *attr)
 {
        struct blob_attr *tb_dev[__DEV_ATTR_MAX];
        struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
        struct bridge_state *bst;
        struct device *dev = NULL;
-       const char *name;
 
        blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
                blob_data(attr), blob_len(attr));
        blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
                blob_data(attr), blob_len(attr));
 
-       if (!tb_dev[DEV_ATTR_NAME])
-               return NULL;
-
        if (!tb_br[BRIDGE_ATTR_IFNAME])
                return NULL;
 
-       name = blobmsg_data(tb_dev[DEV_ATTR_NAME]);
-
        bst = calloc(1, sizeof(*bst));
        if (!bst)
                return NULL;
@@ -353,6 +399,7 @@ bridge_create(struct blob_attr *attr)
        device_init_settings(dev, tb_dev);
        dev->config_pending = true;
        bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
+       bridge_apply_settings(bst, tb_br);
 
        bst->set_state = dev->set_state;
        dev->set_state = bridge_set_state;