wireless: add support for defining wifi interfaces via procd service data
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 9098c662cc4144f5bf9eec6f86a334016c7832e2..f8662c2a7b78be1f3a817affdd4a8e17124d5d9c 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,8 +836,8 @@ netifd_dump_status(struct interface *iface)
        if (iface->jail)
                blobmsg_add_string(&b, "jail", iface->jail);
 
-       if (iface->jail_ifname)
-               blobmsg_add_string(&b, "jail_ifname", iface->jail_ifname);
+       if (iface->jail_device)
+               blobmsg_add_string(&b, "jail_device", iface->jail_device);
 
        if (iface->state == IFS_UP) {
                if (iface->updated) {
@@ -854,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);
 
@@ -1065,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)
@@ -1091,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)
@@ -1295,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();
 
@@ -1311,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);
 }
 
@@ -1354,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;
        }
@@ -1369,3 +1445,45 @@ netifd_ubus_remove_interface(struct interface *iface)
        ubus_remove_object(ubus_ctx, &iface->ubus);
        free((void *) iface->ubus.name);
 }
+
+static void
+netifd_ubus_data_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       struct blob_attr *srv, *in, *t, *data;
+       procd_data_cb cb = req->priv;
+       int rem, rem2, rem3, rem4;
+
+       blobmsg_for_each_attr(srv, msg, rem) {
+               if (!blobmsg_check_attr(srv, true) ||
+                   blobmsg_type(srv) != BLOBMSG_TYPE_TABLE)
+                       continue;
+               blobmsg_for_each_attr(in, srv, rem2) {
+                       if (!blobmsg_check_attr(in , true) ||
+                               blobmsg_type(in) != BLOBMSG_TYPE_TABLE)
+                               continue;
+                       blobmsg_for_each_attr(t, in, rem3) {
+                               if (!blobmsg_check_attr(t, true) ||
+                                       blobmsg_type(t) != BLOBMSG_TYPE_TABLE)
+                                       continue;
+                               blobmsg_for_each_attr(data, t, rem4) {
+                                       if (!blobmsg_check_attr(t, true) ||
+                                               blobmsg_type(t) != BLOBMSG_TYPE_TABLE)
+                                               continue;
+                                       cb(data);
+                               }
+                       }
+               }
+       }
+}
+
+void netifd_ubus_get_procd_data(const char *type, procd_data_cb cb)
+{
+       uint32_t id;
+
+       if (ubus_lookup_id(ubus_ctx, "service", &id))
+               return;
+
+       blob_buf_init(&b, 0);
+       blobmsg_add_string(&b, "type", type);
+       ubus_invoke(ubus_ctx, id, "get_data", b.head, netifd_ubus_data_cb, cb, 30000);
+}