7 static struct ubus_context *ctx = NULL;
8 static struct blob_buf b;
16 __DEV_MAX_NOFORCE = __DEV_MAX - 1,
19 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
20 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
21 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
25 netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
26 struct ubus_request_data *req, const char *method,
27 struct blob_attr *msg)
30 struct blob_attr *tb[__DEV_MAX];
31 bool add = !strncmp(method, "add", 3);
33 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
36 return UBUS_STATUS_INVALID_ARGUMENT;
38 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
40 return UBUS_STATUS_NOT_FOUND;
42 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
43 device_set_present(dev, add);
45 check_device_state(dev);
51 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
52 struct ubus_request_data *req, const char *method,
53 struct blob_attr *msg)
59 static struct ubus_method main_object_methods[] = {
60 UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
61 UBUS_METHOD("remove_device", netifd_handle_device, dev_policy),
62 { .name = "restart", .handler = netifd_handle_restart },
65 static struct ubus_object_type main_object_type =
66 UBUS_OBJECT_TYPE("netifd", main_object_methods);
68 static struct ubus_object main_object = {
69 .name = "network.interface",
70 .type = &main_object_type,
71 .methods = main_object_methods,
72 .n_methods = ARRAY_SIZE(main_object_methods),
76 netifd_ubus_init(const char *path)
80 ctx = ubus_connect(path);
84 DPRINTF("connected as %08x\n", ctx->local_id);
88 ret = ubus_add_object(ctx, &main_object);
90 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
96 netifd_ubus_done(void)
102 /* per-interface object */
105 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
106 struct ubus_request_data *req, const char *method,
107 struct blob_attr *msg)
109 struct interface *iface;
111 iface = container_of(obj, struct interface, ubus);
112 interface_set_up(iface);
118 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
119 struct ubus_request_data *req, const char *method,
120 struct blob_attr *msg)
122 struct interface *iface;
124 iface = container_of(obj, struct interface, ubus);
125 interface_set_down(iface);
131 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
133 struct interface_error *error;
137 e = blobmsg_open_array(b, "errors");
138 list_for_each_entry(error, &iface->errors, list) {
139 e2 = blobmsg_open_table(b, NULL);
141 blobmsg_add_string(b, "subsystem", error->subsystem);
142 blobmsg_add_string(b, "code", error->code);
143 if (error->data[0]) {
144 e3 = blobmsg_open_array(b, "data");
145 for (i = 0; error->data[i]; i++)
146 blobmsg_add_string(b, NULL, error->data[i]);
147 blobmsg_close_array(b, e3);
150 blobmsg_close_table(b, e2);
152 blobmsg_close_array(b, e);
156 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
157 struct ubus_request_data *req, const char *method,
158 struct blob_attr *msg)
160 struct interface *iface;
162 iface = container_of(obj, struct interface, ubus);
164 blob_buf_init(&b, 0);
165 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
166 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
167 blobmsg_add_u8(&b, "available", iface->available);
168 blobmsg_add_u8(&b, "autostart", iface->autostart);
169 if (iface->main_dev.dev) {
170 struct device *dev = iface->main_dev.dev;
174 /* use a different field for virtual devices */
180 devinfo = blobmsg_open_table(&b, field);
181 blobmsg_add_string(&b, "name", dev->ifname);
183 if (dev->type->dump_status)
184 dev->type->dump_status(dev, &b);
186 blobmsg_close_table(&b, devinfo);
189 if (!list_is_empty(&iface->errors))
190 netifd_add_interface_errors(&b, iface);
192 ubus_send_reply(ctx, req, b.head);
198 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
199 struct ubus_request_data *req, const char *method,
200 struct blob_attr *msg)
202 struct interface *iface;
203 struct device *dev, *main_dev;
204 struct blob_attr *tb[__DEV_MAX];
205 bool add = !strncmp(method, "add", 3);
208 iface = container_of(obj, struct interface, ubus);
210 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
213 return UBUS_STATUS_INVALID_ARGUMENT;
215 main_dev = iface->main_dev.dev;
217 return UBUS_STATUS_NOT_FOUND;
219 if (!main_dev->hotplug_ops)
220 return UBUS_STATUS_NOT_SUPPORTED;
222 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
224 return UBUS_STATUS_NOT_FOUND;
226 if (main_dev != dev) {
228 ret = main_dev->hotplug_ops->add(main_dev, dev);
230 ret = main_dev->hotplug_ops->del(main_dev, dev);
232 ret = UBUS_STATUS_UNKNOWN_ERROR;
234 ret = UBUS_STATUS_INVALID_ARGUMENT;
238 device_free_unused(dev);
244 static struct ubus_method iface_object_methods[] = {
245 { .name = "up", .handler = netifd_handle_up },
246 { .name = "down", .handler = netifd_handle_down },
247 { .name = "status", .handler = netifd_handle_status },
248 { .name = "add_device", .handler = netifd_iface_handle_device,
249 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
250 { .name = "remove_device", .handler = netifd_iface_handle_device,
251 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
254 static struct ubus_object_type iface_object_type =
255 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
259 netifd_ubus_add_interface(struct interface *iface)
261 struct ubus_object *obj = &iface->ubus;
264 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
268 sprintf(name, "%s.%s", main_object.name, iface->name);
270 obj->type = &iface_object_type;
271 obj->methods = iface_object_methods;
272 obj->n_methods = ARRAY_SIZE(iface_object_methods);
273 if (ubus_add_object(ctx, &iface->ubus)) {
274 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
281 netifd_ubus_remove_interface(struct interface *iface)
283 if (!iface->ubus.name)
286 ubus_remove_object(ctx, &iface->ubus);
287 free((void *) iface->ubus.name);