9 static struct ubus_context *ctx = NULL;
10 static struct blob_buf b;
15 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
16 struct ubus_request_data *req, const char *method,
17 struct blob_attr *msg)
24 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
25 struct ubus_request_data *req, const char *method,
26 struct blob_attr *msg)
32 static struct ubus_method main_object_methods[] = {
33 { .name = "restart", .handler = netifd_handle_restart },
34 { .name = "reload", .handler = netifd_handle_reload },
37 static struct ubus_object_type main_object_type =
38 UBUS_OBJECT_TYPE("netifd", main_object_methods);
40 static struct ubus_object main_object = {
42 .type = &main_object_type,
43 .methods = main_object_methods,
44 .n_methods = ARRAY_SIZE(main_object_methods),
52 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
53 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
57 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
58 struct ubus_request_data *req, const char *method,
59 struct blob_attr *msg)
61 struct device *dev = NULL;
62 struct blob_attr *tb[__DEV_MAX];
64 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
67 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
69 return UBUS_STATUS_INVALID_ARGUMENT;
73 device_dump_status(&b, dev);
74 ubus_send_reply(ctx, req, b.head);
79 static struct ubus_method dev_object_methods[] = {
80 UBUS_METHOD("status", netifd_dev_status, dev_policy)
83 static struct ubus_object_type dev_object_type =
84 UBUS_OBJECT_TYPE("device", dev_object_methods);
86 static struct ubus_object dev_object = {
87 .name = "network.device",
88 .type = &dev_object_type,
89 .methods = dev_object_methods,
90 .n_methods = ARRAY_SIZE(dev_object_methods),
94 netifd_ubus_init(const char *path)
98 ctx = ubus_connect(path);
102 DPRINTF("connected as %08x\n", ctx->local_id);
106 ret = ubus_add_object(ctx, &main_object);
110 ret = ubus_add_object(ctx, &dev_object);
114 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
119 netifd_ubus_done(void)
125 /* per-interface object */
128 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
129 struct ubus_request_data *req, const char *method,
130 struct blob_attr *msg)
132 struct interface *iface;
134 iface = container_of(obj, struct interface, ubus);
135 interface_set_up(iface);
141 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
142 struct ubus_request_data *req, const char *method,
143 struct blob_attr *msg)
145 struct interface *iface;
147 iface = container_of(obj, struct interface, ubus);
148 interface_set_down(iface);
154 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
156 struct interface_error *error;
160 e = blobmsg_open_array(b, "errors");
161 list_for_each_entry(error, &iface->errors, list) {
162 e2 = blobmsg_open_table(b, NULL);
164 blobmsg_add_string(b, "subsystem", error->subsystem);
165 blobmsg_add_string(b, "code", error->code);
166 if (error->data[0]) {
167 e3 = blobmsg_open_array(b, "data");
168 for (i = 0; error->data[i]; i++)
169 blobmsg_add_string(b, NULL, error->data[i]);
170 blobmsg_close_array(b, e3);
173 blobmsg_close_table(b, e2);
175 blobmsg_close_array(b, e);
179 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
180 struct ubus_request_data *req, const char *method,
181 struct blob_attr *msg)
183 struct interface *iface;
185 iface = container_of(obj, struct interface, ubus);
187 blob_buf_init(&b, 0);
188 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
189 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
190 blobmsg_add_u8(&b, "available", iface->available);
191 blobmsg_add_u8(&b, "autostart", iface->autostart);
193 if (iface->state == IFS_UP) {
194 time_t cur = system_get_rtime();
195 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
198 if (iface->main_dev.dev) {
199 struct device *dev = iface->main_dev.dev;
203 /* use a different field for virtual devices */
209 devinfo = blobmsg_open_table(&b, field);
210 blobmsg_add_string(&b, "name", dev->ifname);
212 blobmsg_close_table(&b, devinfo);
215 if (!list_is_empty(&iface->errors))
216 netifd_add_interface_errors(&b, iface);
218 ubus_send_reply(ctx, req, b.head);
224 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
225 struct ubus_request_data *req, const char *method,
226 struct blob_attr *msg)
228 struct interface *iface;
229 struct device *dev, *main_dev;
230 struct blob_attr *tb[__DEV_MAX];
231 bool add = !strncmp(method, "add", 3);
234 iface = container_of(obj, struct interface, ubus);
236 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
239 return UBUS_STATUS_INVALID_ARGUMENT;
241 main_dev = iface->main_dev.dev;
243 return UBUS_STATUS_NOT_FOUND;
245 if (!main_dev->hotplug_ops)
246 return UBUS_STATUS_NOT_SUPPORTED;
248 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
250 return UBUS_STATUS_NOT_FOUND;
252 if (main_dev != dev) {
254 ret = main_dev->hotplug_ops->add(main_dev, dev);
256 ret = main_dev->hotplug_ops->del(main_dev, dev);
258 ret = UBUS_STATUS_UNKNOWN_ERROR;
260 ret = UBUS_STATUS_INVALID_ARGUMENT;
264 device_free_unused(dev);
271 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
272 struct ubus_request_data *req, const char *method,
273 struct blob_attr *msg)
275 struct interface *iface;
277 iface = container_of(obj, struct interface, ubus);
279 if (!iface->proto || !iface->proto->notify)
280 return UBUS_STATUS_NOT_SUPPORTED;
282 return iface->proto->notify(iface->proto, msg);
286 netifd_iface_do_remove(struct uloop_timeout *timeout)
288 struct interface *iface;
290 iface = container_of(timeout, struct interface, remove_timer);
291 vlist_delete(&interfaces, &iface->node);
295 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
296 struct ubus_request_data *req, const char *method,
297 struct blob_attr *msg)
299 struct interface *iface;
301 iface = container_of(obj, struct interface, ubus);
302 if (iface->remove_timer.cb)
303 return UBUS_STATUS_INVALID_ARGUMENT;
305 iface->remove_timer.cb = netifd_iface_do_remove;
306 uloop_timeout_set(&iface->remove_timer, 100);
310 static struct ubus_method iface_object_methods[] = {
311 { .name = "up", .handler = netifd_handle_up },
312 { .name = "down", .handler = netifd_handle_down },
313 { .name = "status", .handler = netifd_handle_status },
314 { .name = "add_device", .handler = netifd_iface_handle_device,
315 .policy = dev_policy, .n_policy = __DEV_MAX },
316 { .name = "remove_device", .handler = netifd_iface_handle_device,
317 .policy = dev_policy, .n_policy = __DEV_MAX },
318 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
319 { .name = "remove", .handler = netifd_iface_remove }
322 static struct ubus_object_type iface_object_type =
323 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
327 netifd_ubus_interface_event(struct interface *iface, bool up)
329 blob_buf_init(&b, 0);
330 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
331 blobmsg_add_string(&b, "interface", iface->name);
332 ubus_send_event(ctx, "network.interface", b.head);
336 netifd_ubus_add_interface(struct interface *iface)
338 struct ubus_object *obj = &iface->ubus;
341 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
345 sprintf(name, "%s.%s", main_object.name, iface->name);
347 obj->type = &iface_object_type;
348 obj->methods = iface_object_methods;
349 obj->n_methods = ARRAY_SIZE(iface_object_methods);
350 if (ubus_add_object(ctx, &iface->ubus)) {
351 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
358 netifd_ubus_remove_interface(struct interface *iface)
360 if (!iface->ubus.name)
363 ubus_remove_object(ctx, &iface->ubus);
364 free((void *) iface->ubus.name);