bridge: rework config change pvid handling
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 85d834dd7946aa2728dca570fa2692f00e447906..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;
 
@@ -159,14 +165,12 @@ error:
 
 enum {
        NETNS_UPDOWN_JAIL,
-       NETNS_UPDOWN_PID,
        NETNS_UPDOWN_START,
        __NETNS_UPDOWN_MAX
 };
 
 static const struct blobmsg_policy netns_updown_policy[__NETNS_UPDOWN_MAX] = {
        [NETNS_UPDOWN_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
-       [NETNS_UPDOWN_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
        [NETNS_UPDOWN_START] = { .name = "start", .type = BLOBMSG_TYPE_BOOL },
 };
 
@@ -175,23 +179,26 @@ netifd_netns_updown(struct ubus_context *ctx, struct ubus_object *obj,
                  struct ubus_request_data *req, const char *method,
                  struct blob_attr *msg)
 {
+       int target_netns_fd = ubus_request_get_caller_fd(req);
        struct blob_attr *tb[__NETNS_UPDOWN_MAX];
-       char *jail;
-       pid_t netns_pid;
        bool start;
 
-       blobmsg_parse(netns_updown_policy, __NETNS_UPDOWN_MAX, tb, blob_data(msg), blob_len(msg));
-       if (!tb[NETNS_UPDOWN_JAIL] || !tb[NETNS_UPDOWN_PID])
+       if (target_netns_fd < 0)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
+       blobmsg_parse(netns_updown_policy, __NETNS_UPDOWN_MAX, tb, blob_data(msg), blob_len(msg));
+
        start = tb[NETNS_UPDOWN_START] && blobmsg_get_bool(tb[NETNS_UPDOWN_START]);
-       jail = blobmsg_get_string(tb[NETNS_UPDOWN_JAIL]);
-       netns_pid = blobmsg_get_u32(tb[NETNS_UPDOWN_PID]);
 
-       if (start)
-               interface_start_jail(jail, netns_pid);
-       else
-               interface_stop_jail(jail, netns_pid);
+       if (start) {
+               if (!tb[NETNS_UPDOWN_JAIL])
+                       return UBUS_STATUS_INVALID_ARGUMENT;
+
+               interface_start_jail(target_netns_fd, blobmsg_get_string(tb[NETNS_UPDOWN_JAIL]));
+       } else {
+               interface_stop_jail(target_netns_fd);
+       }
+       close(target_netns_fd);
 
        return UBUS_STATUS_OK;
 }
@@ -266,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));
 
@@ -291,19 +298,23 @@ netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 
 error:
-       device_free_unused(dev);
+       device_free_unused();
        return UBUS_STATUS_INVALID_ARGUMENT;
 }
 
 enum {
        DEV_STATE_NAME,
        DEV_STATE_DEFER,
+       DEV_STATE_AUTH_STATUS,
+       DEV_STATE_AUTH_VLANS,
        __DEV_STATE_MAX,
 };
 
 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
@@ -314,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));
 
@@ -329,6 +341,56 @@ netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
        if (cur)
                device_set_deferred(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;
+}
+
+#ifdef DUMMY_MODE
+enum {
+    DEV_HOTPLUG_ATTR_NAME,
+    DEV_HOTPLUG_ATTR_ADD,
+    __DEV_HOTPLUG_ATTR_MAX,
+};
+
+static const struct blobmsg_policy dev_hotplug_policy[__DEV_HOTPLUG_ATTR_MAX] = {
+    [DEV_HOTPLUG_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
+    [DEV_HOTPLUG_ATTR_ADD] = { "add", BLOBMSG_TYPE_BOOL },
+};
+
+static int
+netifd_handle_dev_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
+                         struct ubus_request_data *req, const char *method,
+                         struct blob_attr *msg)
+{
+       struct blob_attr *tb[__DEV_HOTPLUG_ATTR_MAX];
+       const char *name;
+
+       blobmsg_parse(dev_hotplug_policy, __DEV_HOTPLUG_ATTR_MAX, tb,
+                     blob_data(msg), blob_len(msg));
+
+       if (!tb[DEV_HOTPLUG_ATTR_NAME] || !tb[DEV_HOTPLUG_ATTR_ADD])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       name = blobmsg_get_string(tb[DEV_HOTPLUG_ATTR_NAME]);
+       device_hotplug_event(name, blobmsg_get_bool(tb[DEV_HOTPLUG_ATTR_ADD]));
+
+       return 0;
+}
+#endif
+
+static int
+netifd_handle_stp_init(struct ubus_context *ctx, struct ubus_object *obj,
+                      struct ubus_request_data *req, const char *method,
+                      struct blob_attr *msg)
+{
+       device_stp_init();
+
        return 0;
 }
 
@@ -336,6 +398,10 @@ static struct ubus_method dev_object_methods[] = {
        UBUS_METHOD("status", netifd_dev_status, dev_policy),
        UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
        UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
+#ifdef DUMMY_MODE
+       UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug, dev_hotplug_policy),
+#endif
+       UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init)
 };
 
 static struct ubus_object_type dev_object_type =
@@ -355,6 +421,11 @@ netifd_ubus_add_fd(void)
        system_fd_set_cloexec(ubus_ctx->sock.fd);
 }
 
+void netifd_ubus_device_notify(const char *event, struct blob_attr *data, int timeout)
+{
+       ubus_notify(ubus_ctx, &dev_object, event, data, timeout);
+}
+
 static void
 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
 {
@@ -364,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();
 }
 
@@ -765,6 +836,9 @@ netifd_dump_status(struct interface *iface)
        if (iface->jail)
                blobmsg_add_string(&b, "jail", iface->jail);
 
+       if (iface->jail_device)
+               blobmsg_add_string(&b, "jail_device", iface->jail_device);
+
        if (iface->state == IFS_UP) {
                if (iface->updated) {
                        a = blobmsg_open_array(&b, "updated");
@@ -851,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);
 
@@ -900,12 +977,14 @@ netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
 enum {
        DEV_LINK_NAME,
        DEV_LINK_EXT,
+       DEV_LINK_VLAN,
        __DEV_LINK_MAX,
 };
 
 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
        [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
        [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_LINK_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_ARRAY },
 };
 
 static int
@@ -930,7 +1009,8 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
        if (cur)
                link_ext = blobmsg_get_bool(cur);
 
-       return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
+       return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]),
+                                    tb[DEV_LINK_VLAN], add, link_ext);
 }
 
 
@@ -980,19 +1060,27 @@ netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
                            struct blob_attr *msg)
 {
        struct interface *iface;
-       struct device *dev;
+       struct device *dev, *bridge_dev = NULL;
        const struct device_hotplug_ops *ops;
 
        iface = container_of(obj, struct interface, ubus);
        dev = iface->main_dev.dev;
        if (!dev)
-               return 0;
+               goto out;
 
        ops = dev->hotplug_ops;
        if (!ops)
-               return 0;
+               goto out;
+
+       ops->prepare(dev, &bridge_dev);
 
-       return ops->prepare(dev);
+out:
+       blob_buf_init(&b, 0);
+       if (bridge_dev)
+               blobmsg_add_string(&b, "bridge", bridge_dev->ifname);
+       ubus_send_reply(ctx, req, b.head);
+
+       return 0;
 }
 
 static int
@@ -1051,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)
@@ -1077,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)
@@ -1271,17 +1359,23 @@ static struct ubus_object wireless_object = {
        .n_methods = ARRAY_SIZE(wireless_object_methods),
 };
 
+int
+netifd_extdev_invoke(uint32_t id, const char *method, struct blob_attr *msg,
+         ubus_data_handler_t data_cb, void *data)
+{
+       return ubus_invoke(ubus_ctx, id, method, msg, data_cb, data, 3000);
+}
+
 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();
 
@@ -1290,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);
 }
 
@@ -1333,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;
        }