fix cleaning up interface state for reload
[project/netifd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 422caca..e549d9c 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -25,7 +25,6 @@
 
 static struct ubus_context *ctx = NULL;
 static struct blob_buf b;
-static struct netifd_fd ubus_fd;
 static const char *ubus_path;
 
 /* global object */
@@ -258,8 +257,7 @@ static void
 netifd_ubus_add_fd(void)
 {
        ubus_add_uloop(ctx);
-       ubus_fd.fd = ctx->sock.fd;
-       netifd_fd_add(&ubus_fd);
+       system_fd_set_cloexec(ctx->sock.fd);
 }
 
 static void
@@ -283,7 +281,6 @@ netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
 static void
 netifd_ubus_connection_lost(struct ubus_context *ctx)
 {
-       netifd_fd_delete(&ubus_fd);
        netifd_ubus_reconnect_timer(NULL);
 }
 
@@ -445,12 +442,63 @@ interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
                if (route->flags & DEVROUTE_METRIC)
                        blobmsg_add_u32(&b, "metric", route->metric);
 
-               blobmsg_add_u8(&b, "enabled", route->enabled);
-
                blobmsg_close_table(&b, r);
        }
 }
 
+
+static void
+interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
+{
+       struct device_prefix *prefix;
+       char *buf;
+       void *a, *c;
+       const int buflen = INET6_ADDRSTRLEN;
+
+       vlist_for_each_element(&ip->prefix, prefix, node) {
+               a = blobmsg_open_table(&b, NULL);
+
+               buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
+               inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
+               blobmsg_add_string_buffer(&b);
+
+               blobmsg_add_u32(&b, "mask", prefix->length);
+
+               time_t now = system_get_rtime();
+               if (prefix->preferred_until) {
+                       int preferred = prefix->preferred_until - now;
+                       if (preferred < 0)
+                               preferred = 0;
+                       blobmsg_add_u32(&b, "preferred", preferred);
+               }
+
+               if (prefix->valid_until) {
+                       int valid = prefix->valid_until - now;
+                       if (valid < 0)
+                               valid = 0;
+                       blobmsg_add_u32(&b, "valid", valid);
+               }
+
+               c = blobmsg_open_table(&b, "assigned");
+               struct device_prefix_assignment *assign;
+               vlist_for_each_element(prefix->assignments, assign, node) {
+                       void *d = blobmsg_open_table(&b, assign->name);
+
+                       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, d);
+               }
+               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)
@@ -525,6 +573,10 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
                interface_ip_dump_address_list(&iface->config_ip, true, true);
                interface_ip_dump_address_list(&iface->proto_ip, true, true);
                blobmsg_close_array(&b, a);
+               a = blobmsg_open_array(&b, "ipv6-prefix");
+               interface_ip_dump_prefix_list(&iface->config_ip);
+               interface_ip_dump_prefix_list(&iface->proto_ip);
+               blobmsg_close_array(&b, a);
                a = blobmsg_open_array(&b, "route");
                interface_ip_dump_route_list(&iface->config_ip, true);
                interface_ip_dump_route_list(&iface->proto_ip, true);
@@ -597,8 +649,10 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
        device_lock();
 
        dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
-       if (add && !dev)
-               return UBUS_STATUS_NOT_FOUND;
+       if (!dev) {
+               ret = UBUS_STATUS_NOT_FOUND;
+               goto out;
+       }
 
        if (add) {
                device_set_present(dev, true);
@@ -611,6 +665,7 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
                ret = interface_remove_link(iface, dev);
        }
 
+out:
        device_unlock();
 
        return ret;
@@ -729,8 +784,7 @@ netifd_ubus_add_interface(struct interface *iface)
        struct ubus_object *obj = &iface->ubus;
        char *name = NULL;
 
-       asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
-       if (!name)
+       if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
                return;
 
        obj->name = name;