interface: fix jail ifdown and jails without jail_ifname
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 2522cfae28a151a07de45479045dfb32c6914309..15c826b427d69e5f2658bb88d5267dcb4da3e516 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -44,8 +44,10 @@ netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
                     struct ubus_request_data *req, const char *method,
                     struct blob_attr *msg)
 {
-       netifd_reload();
-       return 0;
+       if (netifd_reload())
+               return UBUS_STATUS_NOT_FOUND;
+
+       return UBUS_STATUS_OK;
 }
 
 enum {
@@ -127,7 +129,6 @@ netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
        struct blob_attr *tb[__DI_MAX];
        struct interface *iface;
        struct blob_attr *config;
-       struct device *dev;
 
        blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
 
@@ -136,7 +137,7 @@ netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
 
        const char *name = blobmsg_get_string(tb[DI_NAME]);
 
-       iface = interface_alloc(name, msg);
+       iface = interface_alloc(name, msg, true);
        if (!iface)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
@@ -144,50 +145,54 @@ netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
        if (!config)
                goto error;
 
-       interface_add(iface, config);
-
-       // need to look up the interface name again, in case of config update
-       // the pointer will have changed
-       iface = vlist_find(&interfaces, name, iface, node);
-       if (!iface)
-               return UBUS_STATUS_UNKNOWN_ERROR;
-
-       // Set interface as dynamic
-       interface_set_dynamic(iface);
-
-       dev = iface->main_dev.dev;
-       if (!dev || !dev->default_config)
-               return UBUS_STATUS_UNKNOWN_ERROR;
+       if (!interface_add(iface, config))
+               goto error_free_config;
 
        return UBUS_STATUS_OK;
 
+error_free_config:
+       free(config);
 error:
        free(iface);
        return UBUS_STATUS_UNKNOWN_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 },
+};
+
 static int
-netifd_del_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
-                     struct ubus_request_data *req, const char *method,
-                     struct blob_attr *msg)
+netifd_netns_updown(struct ubus_context *ctx, struct ubus_object *obj,
+                 struct ubus_request_data *req, const char *method,
+                 struct blob_attr *msg)
 {
-       struct blob_attr *tb[__DI_MAX];
-       struct interface *iface;
-
-       blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
+       struct blob_attr *tb[__NETNS_UPDOWN_MAX];
+       char *jail;
+       pid_t netns_pid;
+       bool start;
 
-       if (!tb[DI_NAME])
+       blobmsg_parse(netns_updown_policy, __NETNS_UPDOWN_MAX, tb, blob_data(msg), blob_len(msg));
+       if (!tb[NETNS_UPDOWN_JAIL] || !tb[NETNS_UPDOWN_PID])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       const char *name = blobmsg_get_string(tb[DI_NAME]);
-       iface = vlist_find(&interfaces, name, iface, node);
+       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 (!iface)
-               return UBUS_STATUS_NOT_FOUND;
-       else if (!iface->dynamic)
-               return UBUS_STATUS_INVALID_COMMAND;
+       if (start)
+               interface_start_jail(jail, netns_pid);
+       else
+               interface_stop_jail(jail, netns_pid);
 
-       vlist_delete(&interfaces, &iface->node);
        return UBUS_STATUS_OK;
 }
 
@@ -197,7 +202,7 @@ static struct ubus_method main_object_methods[] = {
        UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
        { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
        UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
-       UBUS_METHOD("del_dynamic", netifd_del_dynamic, dynamic_policy),
+       UBUS_METHOD("netns_updown", netifd_netns_updown, netns_updown_policy),
 };
 
 static struct ubus_object_type main_object_type =
@@ -230,7 +235,7 @@ netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
        blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
 
        if (tb[DEV_NAME]) {
-               dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
+               dev = device_find(blobmsg_data(tb[DEV_NAME]));
                if (!dev)
                        return UBUS_STATUS_INVALID_ARGUMENT;
        }
@@ -278,7 +283,7 @@ netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
                if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
                        goto error;
 
-               if (!blobmsg_check_attr(cur, NULL))
+               if (!blobmsg_check_attr(cur, false))
                        goto error;
 
                alias_notify_device(blobmsg_data(cur), dev);
@@ -316,7 +321,7 @@ netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
        if (!cur)
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       dev = device_get(blobmsg_data(cur), false);
+       dev = device_find(blobmsg_data(cur));
        if (!dev)
                return UBUS_STATUS_NOT_FOUND;
 
@@ -402,6 +407,19 @@ netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+static int
+netifd_handle_renew(struct ubus_context *ctx, struct ubus_object *obj,
+                  struct ubus_request_data *req, const char *method,
+                  struct blob_attr *msg)
+{
+       struct interface *iface;
+
+       iface = container_of(obj, struct interface, ubus);
+       interface_renew(iface);
+
+       return 0;
+}
+
 static void
 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
 {
@@ -428,8 +446,7 @@ netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
 }
 
 static void
-interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
-                               bool enabled)
+interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6, bool enabled)
 {
        struct device_addr *addr;
        char *buf;
@@ -458,6 +475,12 @@ interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
 
                blobmsg_add_u32(&b, "mask", addr->mask);
 
+               if (addr->point_to_point) {
+                       buf = blobmsg_alloc_string_buffer(&b, "ptpaddress", buflen);
+                       inet_ntop(af, &addr->point_to_point, buf, buflen);
+                       blobmsg_add_string_buffer(&b);
+               }
+
                if (addr->preferred_until) {
                        int preferred = addr->preferred_until - now;
                        if (preferred < 0)
@@ -475,6 +498,43 @@ interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
        }
 }
 
+static void
+interface_ip_dump_neighbor_list(struct interface_ip_settings *ip, bool enabled)
+{
+       struct device_neighbor *neighbor;
+       int buflen = 128;
+       char *buf;
+       void *r;
+       int af;
+
+       vlist_for_each_element(&ip->neighbor, neighbor, node) {
+               if (neighbor->enabled != enabled)
+                       continue;
+
+               if ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
+                       af = AF_INET;
+               else
+                       af = AF_INET6;
+
+               r = blobmsg_open_table(&b, NULL);
+
+               if (neighbor->flags & DEVNEIGH_MAC)
+                       blobmsg_add_string(&b, "mac", format_macaddr(neighbor->macaddr));
+
+               buf = blobmsg_alloc_string_buffer(&b , "address", buflen);
+               inet_ntop(af, &neighbor->addr, buf, buflen);
+               blobmsg_add_string_buffer(&b);
+
+               if (neighbor->proxy)
+                       blobmsg_add_u32(&b, "proxy", neighbor->proxy);
+
+               if (neighbor->router)
+                       blobmsg_add_u32(&b, "router", neighbor->router);
+
+               blobmsg_close_table(&b, r);
+       }
+}
+
 static void
 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
 {
@@ -512,6 +572,9 @@ interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
                if (route->flags & DEVROUTE_TYPE)
                        blobmsg_add_u32(&b, "type", route->type);
 
+               if (route->flags & DEVROUTE_PROTO)
+                       blobmsg_add_u32(&b, "proto", route->proto);
+
                if (route->flags & DEVROUTE_MTU)
                        blobmsg_add_u32(&b, "mtu", route->mtu);
 
@@ -626,15 +689,23 @@ interface_ip_dump_prefix_assignment_list(struct interface *iface)
                        if (prefix->valid_until)
                                blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
 
+                       void *c = blobmsg_open_table(&b, "local-address");
+                       if (assign->enabled) {
+                               buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
+                               inet_ntop(AF_INET6, &assign->addr, buf, buflen);
+                               blobmsg_add_string_buffer(&b);
+
+                               blobmsg_add_u32(&b, "mask", assign->length);
+                       }
+                       blobmsg_close_table(&b, c);
+
                        blobmsg_close_table(&b, a);
                }
        }
 }
 
-
 static void
-interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
-                                  bool enabled)
+interface_ip_dump_dns_server_list(struct interface_ip_settings *ip, bool enabled)
 {
        struct dns_server *dns;
        int buflen = 128;
@@ -651,8 +722,7 @@ interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
 }
 
 static void
-interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
-                                  bool enabled)
+interface_ip_dump_dns_search_list(struct interface_ip_settings *ip, bool enabled)
 {
        struct dns_search_domain *dns;
 
@@ -675,6 +745,7 @@ netifd_dump_status(struct interface *iface)
        blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
        blobmsg_add_u8(&b, "available", iface->available);
        blobmsg_add_u8(&b, "autostart", iface->autostart);
+       blobmsg_add_u8(&b, "dynamic", iface->dynamic);
 
        if (iface->state == IFS_UP) {
                time_t cur = system_get_rtime();
@@ -687,10 +758,16 @@ netifd_dump_status(struct interface *iface)
                blobmsg_add_string(&b, "proto", iface->proto_handler->name);
 
        dev = iface->main_dev.dev;
-       if (dev && !dev->hidden &&
+       if (dev && !dev->hidden && iface->proto_handler &&
            !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
                blobmsg_add_string(&b, "device", dev->ifname);
 
+       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->state == IFS_UP) {
                if (iface->updated) {
                        a = blobmsg_open_array(&b, "updated");
@@ -710,9 +787,12 @@ netifd_dump_status(struct interface *iface)
                if (iface->ip4table)
                        blobmsg_add_u32(&b, "ip4table", iface->ip4table);
                if (iface->ip6table)
-                       blobmsg_add_u32(&b, "ip6table", iface->ip6table);                 
+                       blobmsg_add_u32(&b, "ip6table", iface->ip6table);
                blobmsg_add_u32(&b, "metric", iface->metric);
+               blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
                blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
+               if (iface->assignment_weight)
+                       blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
                a = blobmsg_open_array(&b, "ipv4-address");
                interface_ip_dump_address_list(&iface->config_ip, false, true);
                interface_ip_dump_address_list(&iface->proto_ip, false, true);
@@ -740,6 +820,10 @@ netifd_dump_status(struct interface *iface)
                interface_ip_dump_dns_search_list(&iface->config_ip, true);
                interface_ip_dump_dns_search_list(&iface->proto_ip, true);
                blobmsg_close_array(&b, a);
+               a = blobmsg_open_array(&b, "neighbors");
+               interface_ip_dump_neighbor_list(&iface->config_ip, true);
+               interface_ip_dump_neighbor_list(&iface->proto_ip, true);
+               blobmsg_close_array(&b, a);
 
                inactive = blobmsg_open_table(&b, "inactive");
                a = blobmsg_open_array(&b, "ipv4-address");
@@ -762,6 +846,10 @@ netifd_dump_status(struct interface *iface)
                interface_ip_dump_dns_search_list(&iface->config_ip, false);
                interface_ip_dump_dns_search_list(&iface->proto_ip, false);
                blobmsg_close_array(&b, a);
+               a = blobmsg_open_array(&b, "neighbors");
+               interface_ip_dump_neighbor_list(&iface->config_ip, false);
+               interface_ip_dump_neighbor_list(&iface->proto_ip, false);
+               blobmsg_close_array(&b, a);
                blobmsg_close_table(&b, inactive);
        }
 
@@ -812,23 +900,40 @@ netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+enum {
+       DEV_LINK_NAME,
+       DEV_LINK_EXT,
+       __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 },
+};
+
 static int
 netifd_iface_handle_device(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_MAX];
+       struct blob_attr *tb[__DEV_LINK_MAX];
+       struct blob_attr *cur;
        struct interface *iface;
        bool add = !strncmp(method, "add", 3);
+       bool link_ext = true;
 
        iface = container_of(obj, struct interface, ubus);
 
-       blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
+       blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
 
-       if (!tb[DEV_NAME])
+       if (!tb[DEV_LINK_NAME])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       return interface_handle_link(iface, blobmsg_data(tb[DEV_NAME]), add);
+       cur = tb[DEV_LINK_EXT];
+       if (cur)
+               link_ext = blobmsg_get_bool(cur);
+
+       return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
 }
 
 
@@ -899,28 +1004,21 @@ netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
                       struct blob_attr *msg)
 {
        struct interface *iface;
-       struct blob_attr *cur;
-       int rem, ret;
 
        iface = container_of(obj, struct interface, ubus);
 
-       blob_for_each_attr(cur, msg, rem) {
-               ret = interface_add_data(iface, cur);
-               if (ret)
-                       return ret;
-       }
-
-       return 0;
+       return interface_parse_data(iface, msg);
 }
 
 static struct ubus_method iface_object_methods[] = {
        { .name = "up", .handler = netifd_handle_up },
        { .name = "down", .handler = netifd_handle_down },
+       { .name = "renew", .handler = netifd_handle_renew },
        { .name = "status", .handler = netifd_handle_status },
        { .name = "prepare", .handler = netifd_handle_iface_prepare },
        { .name = "dump", .handler = netifd_handle_dump },
-       UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
-       UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
+       UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
+       UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
        { .name = "notify_proto", .handler = netifd_iface_notify_proto },
        { .name = "remove", .handler = netifd_iface_remove },
        { .name = "set_data", .handler = netifd_handle_set_data },
@@ -985,6 +1083,9 @@ static void netifd_add_iface_object(void)
        int i;
 
        methods = calloc(1, sizeof(iface_object_methods));
+       if (!methods)
+               return;
+
        memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
        iface_object.methods = methods;
 
@@ -1026,6 +1127,28 @@ get_wdev(struct blob_attr *msg, int *ret)
        return wdev;
 }
 
+static int
+netifd_handle_wdev_reconf(struct ubus_context *ctx, struct ubus_object *obj,
+                         struct ubus_request_data *req, const char *method,
+                         struct blob_attr *msg)
+{
+       struct wireless_device *wdev;
+       int ret;
+
+       wdev = get_wdev(msg, &ret);
+       if (ret == UBUS_STATUS_NOT_FOUND)
+               return ret;
+
+       if (wdev) {
+               wireless_device_reconf(wdev);
+       } else {
+               vlist_for_each_element(&wireless_devices, wdev, node)
+                       wireless_device_reconf(wdev);
+       }
+
+       return 0;
+}
+
 static int
 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
                      struct ubus_request_data *req, const char *method,
@@ -1134,6 +1257,7 @@ netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
 static struct ubus_method wireless_object_methods[] = {
        { .name = "up", .handler = netifd_handle_wdev_up },
        { .name = "down", .handler = netifd_handle_wdev_down },
+       { .name = "reconf", .handler = netifd_handle_wdev_reconf },
        { .name = "status", .handler = netifd_handle_wdev_status },
        { .name = "notify", .handler = netifd_handle_wdev_notify },
        { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },