12 static struct ubus_context *ctx = NULL;
13 static struct blob_buf b;
14 static struct netifd_fd ubus_fd;
19 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
20 struct ubus_request_data *req, const char *method,
21 struct blob_attr *msg)
28 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
29 struct ubus_request_data *req, const char *method,
30 struct blob_attr *msg)
36 static struct ubus_method main_object_methods[] = {
37 { .name = "restart", .handler = netifd_handle_restart },
38 { .name = "reload", .handler = netifd_handle_reload },
41 static struct ubus_object_type main_object_type =
42 UBUS_OBJECT_TYPE("netifd", main_object_methods);
44 static struct ubus_object main_object = {
46 .type = &main_object_type,
47 .methods = main_object_methods,
48 .n_methods = ARRAY_SIZE(main_object_methods),
56 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
57 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
61 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
62 struct ubus_request_data *req, const char *method,
63 struct blob_attr *msg)
65 struct device *dev = NULL;
66 struct blob_attr *tb[__DEV_MAX];
68 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
71 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
73 return UBUS_STATUS_INVALID_ARGUMENT;
77 device_dump_status(&b, dev);
78 ubus_send_reply(ctx, req, b.head);
83 static struct ubus_method dev_object_methods[] = {
84 UBUS_METHOD("status", netifd_dev_status, dev_policy)
87 static struct ubus_object_type dev_object_type =
88 UBUS_OBJECT_TYPE("device", dev_object_methods);
90 static struct ubus_object dev_object = {
91 .name = "network.device",
92 .type = &dev_object_type,
93 .methods = dev_object_methods,
94 .n_methods = ARRAY_SIZE(dev_object_methods),
98 netifd_ubus_init(const char *path)
102 ctx = ubus_connect(path);
106 DPRINTF("connected as %08x\n", ctx->local_id);
109 ubus_fd.fd = ctx->sock.fd;
110 netifd_fd_add(&ubus_fd);
112 ret = ubus_add_object(ctx, &main_object);
116 ret = ubus_add_object(ctx, &dev_object);
120 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
125 netifd_ubus_done(void)
131 /* per-interface object */
134 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method,
136 struct blob_attr *msg)
138 struct interface *iface;
140 iface = container_of(obj, struct interface, ubus);
141 interface_set_up(iface);
147 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
148 struct ubus_request_data *req, const char *method,
149 struct blob_attr *msg)
151 struct interface *iface;
153 iface = container_of(obj, struct interface, ubus);
154 interface_set_down(iface);
160 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
162 struct interface_error *error;
166 e = blobmsg_open_array(b, "errors");
167 list_for_each_entry(error, &iface->errors, list) {
168 e2 = blobmsg_open_table(b, NULL);
170 blobmsg_add_string(b, "subsystem", error->subsystem);
171 blobmsg_add_string(b, "code", error->code);
172 if (error->data[0]) {
173 e3 = blobmsg_open_array(b, "data");
174 for (i = 0; error->data[i]; i++)
175 blobmsg_add_string(b, NULL, error->data[i]);
176 blobmsg_close_array(b, e3);
179 blobmsg_close_table(b, e2);
181 blobmsg_close_array(b, e);
185 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
186 struct ubus_request_data *req, const char *method,
187 struct blob_attr *msg)
189 struct interface *iface;
191 iface = container_of(obj, struct interface, ubus);
193 blob_buf_init(&b, 0);
194 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
195 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
196 blobmsg_add_u8(&b, "available", iface->available);
197 blobmsg_add_u8(&b, "autostart", iface->autostart);
199 if (iface->state == IFS_UP) {
200 time_t cur = system_get_rtime();
201 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
204 if (iface->main_dev.dev) {
205 struct device *dev = iface->main_dev.dev;
209 /* use a different field for virtual devices */
215 devinfo = blobmsg_open_table(&b, field);
216 blobmsg_add_string(&b, "name", dev->ifname);
218 blobmsg_close_table(&b, devinfo);
221 if (!list_is_empty(&iface->errors))
222 netifd_add_interface_errors(&b, iface);
224 ubus_send_reply(ctx, req, b.head);
230 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
231 struct ubus_request_data *req, const char *method,
232 struct blob_attr *msg)
234 struct interface *iface;
235 struct device *dev, *main_dev;
236 struct blob_attr *tb[__DEV_MAX];
237 bool add = !strncmp(method, "add", 3);
240 iface = container_of(obj, struct interface, ubus);
242 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
245 return UBUS_STATUS_INVALID_ARGUMENT;
247 main_dev = iface->main_dev.dev;
249 return UBUS_STATUS_NOT_FOUND;
251 if (!main_dev->hotplug_ops)
252 return UBUS_STATUS_NOT_SUPPORTED;
254 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
256 return UBUS_STATUS_NOT_FOUND;
258 if (main_dev != dev) {
260 ret = main_dev->hotplug_ops->add(main_dev, dev);
262 ret = main_dev->hotplug_ops->del(main_dev, dev);
264 ret = UBUS_STATUS_UNKNOWN_ERROR;
266 ret = UBUS_STATUS_INVALID_ARGUMENT;
270 device_free_unused(dev);
277 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
278 struct ubus_request_data *req, const char *method,
279 struct blob_attr *msg)
281 struct interface *iface;
283 iface = container_of(obj, struct interface, ubus);
285 if (!iface->proto || !iface->proto->notify)
286 return UBUS_STATUS_NOT_SUPPORTED;
288 return iface->proto->notify(iface->proto, msg);
292 netifd_iface_do_remove(struct uloop_timeout *timeout)
294 struct interface *iface;
296 iface = container_of(timeout, struct interface, remove_timer);
297 vlist_delete(&interfaces, &iface->node);
301 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
302 struct ubus_request_data *req, const char *method,
303 struct blob_attr *msg)
305 struct interface *iface;
307 iface = container_of(obj, struct interface, ubus);
308 if (iface->remove_timer.cb)
309 return UBUS_STATUS_INVALID_ARGUMENT;
311 iface->remove_timer.cb = netifd_iface_do_remove;
312 uloop_timeout_set(&iface->remove_timer, 100);
316 static struct ubus_method iface_object_methods[] = {
317 { .name = "up", .handler = netifd_handle_up },
318 { .name = "down", .handler = netifd_handle_down },
319 { .name = "status", .handler = netifd_handle_status },
320 { .name = "add_device", .handler = netifd_iface_handle_device,
321 .policy = dev_policy, .n_policy = __DEV_MAX },
322 { .name = "remove_device", .handler = netifd_iface_handle_device,
323 .policy = dev_policy, .n_policy = __DEV_MAX },
324 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
325 { .name = "remove", .handler = netifd_iface_remove }
328 static struct ubus_object_type iface_object_type =
329 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
333 netifd_ubus_interface_event(struct interface *iface, bool up)
335 blob_buf_init(&b, 0);
336 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
337 blobmsg_add_string(&b, "interface", iface->name);
338 ubus_send_event(ctx, "network.interface", b.head);
342 netifd_ubus_add_interface(struct interface *iface)
344 struct ubus_object *obj = &iface->ubus;
347 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
352 obj->type = &iface_object_type;
353 obj->methods = iface_object_methods;
354 obj->n_methods = ARRAY_SIZE(iface_object_methods);
355 if (ubus_add_object(ctx, &iface->ubus)) {
356 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
363 netifd_ubus_remove_interface(struct interface *iface)
365 if (!iface->ubus.name)
368 ubus_remove_object(ctx, &iface->ubus);
369 free((void *) iface->ubus.name);