bridge: rework config change pvid handling
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index a38617928b415c7540338684a1402785d83dd416..3d24dc7ad729b5e5f94aa316b57901dbd9af1508 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -27,6 +27,7 @@
 struct ubus_context *ubus_ctx = NULL;
 static struct blob_buf b;
 static const char *ubus_path;
+static struct udebug_ubus udebug;
 
 /* global object */
 
@@ -54,6 +55,7 @@ enum {
        HR_TARGET,
        HR_V6,
        HR_INTERFACE,
+       HR_EXCLUDE,
        __HR_MAX
 };
 
@@ -61,6 +63,7 @@ static const struct blobmsg_policy route_policy[__HR_MAX] = {
        [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
        [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
        [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
+       [HR_EXCLUDE] = { .name = "exclude", .type = BLOBMSG_TYPE_BOOL },
 };
 
 static int
@@ -72,6 +75,7 @@ netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
        struct interface *iface = NULL;
        union if_addr a;
        bool v6 = false;
+       bool exclude = false;
 
        blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
        if (!tb[HR_TARGET])
@@ -80,6 +84,9 @@ netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
        if (tb[HR_V6])
                v6 = blobmsg_get_bool(tb[HR_V6]);
 
+       if (tb[HR_EXCLUDE])
+               exclude = blobmsg_get_bool(tb[HR_EXCLUDE]);
+
        if (tb[HR_INTERFACE])
                iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
 
@@ -87,8 +94,7 @@ netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
        if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-
-       iface = interface_ip_add_target_route(&a, v6, iface);
+       iface = interface_ip_add_target_route(&a, v6, iface, exclude);
        if (!iface)
                return UBUS_STATUS_NOT_FOUND;
 
@@ -267,7 +273,7 @@ netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
        struct device *dev = NULL;
        struct blob_attr *tb[__ALIAS_ATTR_MAX];
        struct blob_attr *cur;
-       int rem;
+       size_t rem;
 
        blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
 
@@ -300,6 +306,7 @@ enum {
        DEV_STATE_NAME,
        DEV_STATE_DEFER,
        DEV_STATE_AUTH_STATUS,
+       DEV_STATE_AUTH_VLANS,
        __DEV_STATE_MAX,
 };
 
@@ -307,6 +314,7 @@ static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
        [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
        [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
        [DEV_STATE_AUTH_STATUS] = { .name = "auth_status", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_STATE_AUTH_VLANS] = { .name = "auth_vlans", BLOBMSG_TYPE_ARRAY },
 };
 
 static int
@@ -317,6 +325,7 @@ netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
        struct device *dev = NULL;
        struct blob_attr *tb[__DEV_STATE_MAX];
        struct blob_attr *cur;
+       bool auth_status;
 
        blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
 
@@ -332,9 +341,12 @@ netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
        if (cur)
                device_set_deferred(dev, !!blobmsg_get_u8(cur));
 
-       cur = tb[DEV_STATE_AUTH_STATUS];
-       if (cur)
-               device_set_auth_status(dev, !!blobmsg_get_u8(cur));
+       if ((cur = tb[DEV_STATE_AUTH_STATUS]) != NULL)
+               auth_status = blobmsg_get_bool(cur);
+       else
+               auth_status = dev->auth_status;
+       if (tb[DEV_STATE_AUTH_STATUS] || tb[DEV_STATE_AUTH_VLANS])
+               device_set_auth_status(dev, auth_status, tb[DEV_STATE_AUTH_VLANS]);
 
        return 0;
 }
@@ -423,12 +435,12 @@ netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
        int t = 2;
 
        if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
-               DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
+               D(SYSTEM, "failed to reconnect, trying again in %d seconds", t);
                uloop_timeout_set(&retry, t * 1000);
                return;
        }
 
-       DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
+       D(SYSTEM, "reconnected to ubus, new id: %08x", ubus_ctx->local_id);
        netifd_ubus_add_fd();
 }
 
@@ -913,6 +925,9 @@ netifd_dump_status(struct interface *iface)
        }
 
        a = blobmsg_open_table(&b, "data");
+
+       if (iface->zone)
+               blobmsg_add_string(&b, "zone", iface->zone);
        avl_for_each_element(&iface->data, data, node)
                blobmsg_add_blob(&b, data->data);
 
@@ -1124,7 +1139,7 @@ netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct interface *iface;
        struct blob_attr *tb;
-       int i;
+       size_t i;
 
        blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
        if (!tb)
@@ -1150,7 +1165,7 @@ netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
 static void netifd_add_iface_object(void)
 {
        struct ubus_method *methods;
-       int i;
+       size_t i;
 
        methods = calloc(1, sizeof(iface_object_methods));
        if (!methods)
@@ -1354,14 +1369,13 @@ netifd_extdev_invoke(uint32_t id, const char *method, struct blob_attr *msg,
 int
 netifd_ubus_init(const char *path)
 {
-       uloop_init();
        ubus_path = path;
 
        ubus_ctx = ubus_connect(path);
        if (!ubus_ctx)
                return -EIO;
 
-       DPRINTF("connected as %08x\n", ubus_ctx->local_id);
+       D(SYSTEM, "connected as %08x", ubus_ctx->local_id);
        ubus_ctx->connection_lost = netifd_ubus_connection_lost;
        netifd_ubus_add_fd();
 
@@ -1370,12 +1384,15 @@ netifd_ubus_init(const char *path)
        netifd_add_object(&wireless_object);
        netifd_add_iface_object();
 
+       udebug_ubus_init(&udebug, ubus_ctx, "netifd", netifd_udebug_config);
+
        return 0;
 }
 
 void
 netifd_ubus_done(void)
 {
+       udebug_ubus_free(&udebug);
        ubus_free(ubus_ctx);
 }
 
@@ -1413,7 +1430,7 @@ netifd_ubus_add_interface(struct interface *iface)
        obj->methods = iface_object_methods;
        obj->n_methods = ARRAY_SIZE(iface_object_methods);
        if (ubus_add_object(ubus_ctx, &iface->ubus)) {
-               DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
+               D(SYSTEM, "failed to publish ubus object for interface '%s'", iface->name);
                free(name);
                obj->name = NULL;
        }