9 static struct ubus_context *ctx = NULL;
10 static struct blob_buf b;
18 __DEV_MAX_NOFORCE = __DEV_MAX - 1,
21 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
22 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
23 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
27 netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
28 struct ubus_request_data *req, const char *method,
29 struct blob_attr *msg)
32 struct blob_attr *tb[__DEV_MAX];
33 bool add = !strncmp(method, "add", 3);
35 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
38 return UBUS_STATUS_INVALID_ARGUMENT;
40 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
42 return UBUS_STATUS_NOT_FOUND;
44 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
45 device_set_present(dev, add);
47 device_check_state(dev);
53 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
54 struct ubus_request_data *req, const char *method,
55 struct blob_attr *msg)
62 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
63 struct ubus_request_data *req, const char *method,
64 struct blob_attr *msg)
70 static struct ubus_method main_object_methods[] = {
71 UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
72 UBUS_METHOD("remove_device", netifd_handle_device, dev_policy),
73 { .name = "restart", .handler = netifd_handle_restart },
74 { .name = "reload", .handler = netifd_handle_reload },
77 static struct ubus_object_type main_object_type =
78 UBUS_OBJECT_TYPE("netifd", main_object_methods);
80 static struct ubus_object main_object = {
82 .type = &main_object_type,
83 .methods = main_object_methods,
84 .n_methods = ARRAY_SIZE(main_object_methods),
88 netifd_ubus_init(const char *path)
92 ctx = ubus_connect(path);
96 DPRINTF("connected as %08x\n", ctx->local_id);
100 ret = ubus_add_object(ctx, &main_object);
102 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
108 netifd_ubus_done(void)
114 /* per-interface object */
117 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
118 struct ubus_request_data *req, const char *method,
119 struct blob_attr *msg)
121 struct interface *iface;
123 iface = container_of(obj, struct interface, ubus);
124 interface_set_up(iface);
130 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
131 struct ubus_request_data *req, const char *method,
132 struct blob_attr *msg)
134 struct interface *iface;
136 iface = container_of(obj, struct interface, ubus);
137 interface_set_down(iface);
143 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
145 struct interface_error *error;
149 e = blobmsg_open_array(b, "errors");
150 list_for_each_entry(error, &iface->errors, list) {
151 e2 = blobmsg_open_table(b, NULL);
153 blobmsg_add_string(b, "subsystem", error->subsystem);
154 blobmsg_add_string(b, "code", error->code);
155 if (error->data[0]) {
156 e3 = blobmsg_open_array(b, "data");
157 for (i = 0; error->data[i]; i++)
158 blobmsg_add_string(b, NULL, error->data[i]);
159 blobmsg_close_array(b, e3);
162 blobmsg_close_table(b, e2);
164 blobmsg_close_array(b, e);
168 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
169 struct ubus_request_data *req, const char *method,
170 struct blob_attr *msg)
172 struct interface *iface;
174 iface = container_of(obj, struct interface, ubus);
176 blob_buf_init(&b, 0);
177 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
178 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
179 blobmsg_add_u8(&b, "available", iface->available);
180 blobmsg_add_u8(&b, "autostart", iface->autostart);
182 if (iface->state == IFS_UP) {
183 time_t cur = system_get_rtime();
184 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
187 if (iface->main_dev.dev) {
188 struct device *dev = iface->main_dev.dev;
192 /* use a different field for virtual devices */
198 devinfo = blobmsg_open_table(&b, field);
199 blobmsg_add_string(&b, "name", dev->ifname);
201 if (dev->type->dump_status)
202 dev->type->dump_status(dev, &b);
204 blobmsg_close_table(&b, devinfo);
207 if (!list_is_empty(&iface->errors))
208 netifd_add_interface_errors(&b, iface);
210 ubus_send_reply(ctx, req, b.head);
216 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
217 struct ubus_request_data *req, const char *method,
218 struct blob_attr *msg)
220 struct interface *iface;
221 struct device *dev, *main_dev;
222 struct blob_attr *tb[__DEV_MAX];
223 bool add = !strncmp(method, "add", 3);
226 iface = container_of(obj, struct interface, ubus);
228 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
231 return UBUS_STATUS_INVALID_ARGUMENT;
233 main_dev = iface->main_dev.dev;
235 return UBUS_STATUS_NOT_FOUND;
237 if (!main_dev->hotplug_ops)
238 return UBUS_STATUS_NOT_SUPPORTED;
240 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
242 return UBUS_STATUS_NOT_FOUND;
244 if (main_dev != dev) {
246 ret = main_dev->hotplug_ops->add(main_dev, dev);
248 ret = main_dev->hotplug_ops->del(main_dev, dev);
250 ret = UBUS_STATUS_UNKNOWN_ERROR;
252 ret = UBUS_STATUS_INVALID_ARGUMENT;
256 device_free_unused(dev);
263 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
264 struct ubus_request_data *req, const char *method,
265 struct blob_attr *msg)
267 struct interface *iface;
269 iface = container_of(obj, struct interface, ubus);
271 if (!iface->proto || !iface->proto->notify)
272 return UBUS_STATUS_NOT_SUPPORTED;
274 return iface->proto->notify(iface->proto, msg);
278 netifd_iface_do_remove(struct uloop_timeout *timeout)
280 struct interface *iface;
282 iface = container_of(timeout, struct interface, remove_timer);
283 vlist_delete(&interfaces, &iface->node);
287 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
288 struct ubus_request_data *req, const char *method,
289 struct blob_attr *msg)
291 struct interface *iface;
293 iface = container_of(obj, struct interface, ubus);
294 if (iface->remove_timer.cb)
295 return UBUS_STATUS_INVALID_ARGUMENT;
297 iface->remove_timer.cb = netifd_iface_do_remove;
298 uloop_timeout_set(&iface->remove_timer, 100);
302 static struct ubus_method iface_object_methods[] = {
303 { .name = "up", .handler = netifd_handle_up },
304 { .name = "down", .handler = netifd_handle_down },
305 { .name = "status", .handler = netifd_handle_status },
306 { .name = "add_device", .handler = netifd_iface_handle_device,
307 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
308 { .name = "remove_device", .handler = netifd_iface_handle_device,
309 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
310 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
311 { .name = "remove", .handler = netifd_iface_remove }
314 static struct ubus_object_type iface_object_type =
315 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
319 netifd_ubus_interface_event(struct interface *iface, bool up)
321 blob_buf_init(&b, 0);
322 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
323 blobmsg_add_string(&b, "interface", iface->name);
324 ubus_send_event(ctx, "network.interface", b.head);
328 netifd_ubus_add_interface(struct interface *iface)
330 struct ubus_object *obj = &iface->ubus;
333 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
337 sprintf(name, "%s.%s", main_object.name, iface->name);
339 obj->type = &iface_object_type;
340 obj->methods = iface_object_methods;
341 obj->n_methods = ARRAY_SIZE(iface_object_methods);
342 if (ubus_add_object(ctx, &iface->ubus)) {
343 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
350 netifd_ubus_remove_interface(struct interface *iface)
352 if (!iface->ubus.name)
355 ubus_remove_object(ctx, &iface->ubus);
356 free((void *) iface->ubus.name);