X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;ds=sidebyside;f=ubus.c;h=d34cc87c107c3bd727487376d4234c9199b85864;hb=3cac4ab6ac9c26d5360b1826b85aac4845414423;hp=a9b0057769cec817cee370042a84f2443b7c3044;hpb=ccca61c97d460d73f29750abdf38cea20ac440f3;p=project%2Fnetifd.git diff --git a/ubus.c b/ubus.c index a9b0057..d34cc87 100644 --- a/ubus.c +++ b/ubus.c @@ -1,5 +1,6 @@ #define _GNU_SOURCE +#include #include #include @@ -230,6 +231,28 @@ netifd_add_interface_errors(struct blob_buf *b, struct interface *iface) blobmsg_close_array(b, e); } +static void +interface_ip_dump_address_list(struct interface_ip_settings *ip) +{ + struct device_addr *addr; + static char *buf; + int buflen = 128; + int af; + + vlist_for_each_element(&ip->addr, addr, node) { + if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) + af = AF_INET; + else + af = AF_INET6; + + buf = blobmsg_alloc_string_buffer(&b, NULL, buflen); + inet_ntop(af, &addr->addr, buf, buflen - 5); + buf += strlen(buf); + sprintf(buf, "/%d", addr->mask); + blobmsg_add_string_buffer(&b); + } +} + static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, @@ -237,6 +260,7 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj, { struct interface *iface; struct device *dev; + void *a; iface = container_of(obj, struct interface, ubus); @@ -256,6 +280,13 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj, if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) blobmsg_add_string(&b, "device", dev->ifname); + if (iface->state == IFS_UP) { + a = blobmsg_open_array(&b, "address"); + interface_ip_dump_address_list(&iface->config_ip); + interface_ip_dump_address_list(&iface->proto_ip); + blobmsg_close_array(&b, a); + } + if (!list_is_empty(&iface->errors)) netifd_add_interface_errors(&b, iface); @@ -269,11 +300,10 @@ 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 interface *iface; - struct device *dev, *main_dev = NULL; struct blob_attr *tb[__DEV_MAX]; + struct interface *iface; + struct device *dev; bool add = !strncmp(method, "add", 3); - const char *devname; int ret; iface = container_of(obj, struct interface, ubus); @@ -283,53 +313,18 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj, if (!tb[DEV_NAME]) return UBUS_STATUS_INVALID_ARGUMENT; - devname = blobmsg_data(tb[DEV_NAME]); - dev = iface->main_dev.dev; - if (iface->hotplug_dev && dev && !add) { - if (strcmp(dev->ifname, devname) != 0) - return UBUS_STATUS_INVALID_ARGUMENT; - } + device_lock(); - if (iface->hotplug_dev) { - if (iface->main_dev.dev) { - interface_set_available(iface, false); - device_remove_user(&iface->main_dev); - } - } else - main_dev = iface->main_dev.dev; - - dev = device_get(blobmsg_data(tb[DEV_NAME]), add); - if (!dev) + dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0); + if (add && !dev) return UBUS_STATUS_NOT_FOUND; - if (!main_dev) { - if (add) { - device_add_user(&iface->main_dev, dev); - iface->hotplug_dev = true; - } - ret = 0; - goto out; - } - - if (!main_dev->hotplug_ops) { - ret = UBUS_STATUS_NOT_SUPPORTED; - goto out; - } - - if (main_dev != dev) { - if (add) - ret = main_dev->hotplug_ops->add(main_dev, dev); - else - ret = main_dev->hotplug_ops->del(main_dev, dev); - if (ret) - ret = UBUS_STATUS_UNKNOWN_ERROR; - } else { - ret = UBUS_STATUS_INVALID_ARGUMENT; - } - -out: if (add) - device_free_unused(dev); + return interface_add_link(iface, dev); + else + return interface_remove_link(iface, dev); + + device_unlock(); return ret; } @@ -375,10 +370,32 @@ netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj, return 0; } +static int +netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct interface *iface; + struct device *dev; + const struct device_hotplug_ops *ops; + + iface = container_of(obj, struct interface, ubus); + dev = iface->main_dev.dev; + if (!dev) + return 0; + + ops = dev->hotplug_ops; + if (!ops) + return 0; + + return ops->prepare(dev); +} + static struct ubus_method iface_object_methods[] = { { .name = "up", .handler = netifd_handle_up }, { .name = "down", .handler = netifd_handle_down }, { .name = "status", .handler = netifd_handle_status }, + { .name = "prepare", .handler = netifd_handle_iface_prepare }, UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ), UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ), { .name = "notify_proto", .handler = netifd_iface_notify_proto },