6 static struct ubus_context *ctx = NULL;
7 static struct blob_buf b;
17 static const struct blobmsg_policy dev_policy[] = {
18 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
19 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
22 static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
23 struct ubus_request_data *req, const char *method,
24 struct blob_attr *msg)
27 struct blob_attr *tb[DEV_LAST];
28 bool add = !strncmp(method, "add", 3);
30 blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
33 return UBUS_STATUS_INVALID_ARGUMENT;
35 dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
37 return UBUS_STATUS_NOT_FOUND;
39 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
40 set_device_present(dev, add);
42 check_device_state(dev);
47 static struct ubus_method main_object_methods[] = {
48 UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
49 UBUS_METHOD("del_device", netifd_handle_device, dev_policy),
52 static struct ubus_object_type main_object_type =
53 UBUS_OBJECT_TYPE("netifd", main_object_methods);
55 static struct ubus_object main_object = {
56 .name = "network.interface",
57 .type = &main_object_type,
58 .methods = main_object_methods,
59 .n_methods = ARRAY_SIZE(main_object_methods),
62 int netifd_ubus_init(const char *path)
66 ctx = ubus_connect(path);
70 DPRINTF("connected as %08x\n", ctx->local_id);
74 ret = ubus_add_object(ctx, &main_object);
76 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
81 void netifd_ubus_done(void)
87 /* per-interface object */
89 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
90 struct ubus_request_data *req, const char *method,
91 struct blob_attr *msg)
93 struct interface *iface;
95 iface = container_of(obj, struct interface, ubus);
96 set_interface_up(iface);
101 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
102 struct ubus_request_data *req, const char *method,
103 struct blob_attr *msg)
105 struct interface *iface;
107 iface = container_of(obj, struct interface, ubus);
108 set_interface_down(iface);
113 static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
114 struct ubus_request_data *req, const char *method,
115 struct blob_attr *msg)
117 struct interface *iface;
119 iface = container_of(obj, struct interface, ubus);
121 blob_buf_init(&b, 0);
122 blobmsg_add_u8(&b, "up", iface->up);
123 blobmsg_add_u8(&b, "active", iface->active);
124 blobmsg_add_u8(&b, "autostart", iface->autostart);
125 if (iface->main_dev.dev) {
126 struct device *dev = iface->main_dev.dev;
130 /* use a different field for virtual devices */
136 devinfo = blobmsg_open_table(&b, field);
137 blobmsg_add_string(&b, "name", dev->ifname);
139 if (dev->type->dump_status)
140 dev->type->dump_status(dev, &b);
142 blobmsg_close_table(&b, devinfo);
145 ubus_send_reply(ctx, req, b.head);
151 static struct ubus_method iface_object_methods[] = {
152 { .name = "up", .handler = netifd_handle_up },
153 { .name = "down", .handler = netifd_handle_down },
154 { .name = "status", .handler = netifd_handle_status },
157 static struct ubus_object_type iface_object_type =
158 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
161 void netifd_ubus_add_interface(struct interface *iface)
163 struct ubus_object *obj = &iface->ubus;
166 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
170 sprintf(name, "%s.%s", main_object.name, iface->name);
172 obj->type = &iface_object_type;
173 obj->methods = iface_object_methods;
174 obj->n_methods = ARRAY_SIZE(iface_object_methods);
175 if (ubus_add_object(ctx, &iface->ubus)) {
176 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
182 void netifd_ubus_remove_interface(struct interface *iface)
184 if (!iface->ubus.name)
187 ubus_remove_object(ctx, &iface->ubus);
188 free((void *) iface->ubus.name);