13 static struct ubus_context *ctx = NULL;
14 static struct blob_buf b;
15 static struct netifd_fd ubus_fd;
20 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
21 struct ubus_request_data *req, const char *method,
22 struct blob_attr *msg)
29 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
30 struct ubus_request_data *req, const char *method,
31 struct blob_attr *msg)
37 static struct ubus_method main_object_methods[] = {
38 { .name = "restart", .handler = netifd_handle_restart },
39 { .name = "reload", .handler = netifd_handle_reload },
42 static struct ubus_object_type main_object_type =
43 UBUS_OBJECT_TYPE("netifd", main_object_methods);
45 static struct ubus_object main_object = {
47 .type = &main_object_type,
48 .methods = main_object_methods,
49 .n_methods = ARRAY_SIZE(main_object_methods),
57 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
58 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
62 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
63 struct ubus_request_data *req, const char *method,
64 struct blob_attr *msg)
66 struct device *dev = NULL;
67 struct blob_attr *tb[__DEV_MAX];
69 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
72 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
74 return UBUS_STATUS_INVALID_ARGUMENT;
78 device_dump_status(&b, dev);
79 ubus_send_reply(ctx, req, b.head);
90 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
91 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
92 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
96 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
97 struct ubus_request_data *req, const char *method,
98 struct blob_attr *msg)
100 struct device *dev = NULL;
101 struct blob_attr *tb[__ALIAS_ATTR_MAX];
102 struct blob_attr *cur;
105 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
107 if (!tb[ALIAS_ATTR_ALIAS])
108 return UBUS_STATUS_INVALID_ARGUMENT;
110 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
111 dev = device_get(blobmsg_data(cur), true);
113 return UBUS_STATUS_NOT_FOUND;
116 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
117 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
120 if (!blobmsg_check_attr(cur, NULL))
123 alias_notify_device(blobmsg_data(cur), dev);
128 device_free_unused(dev);
129 return UBUS_STATUS_INVALID_ARGUMENT;
132 static struct ubus_method dev_object_methods[] = {
133 UBUS_METHOD("status", netifd_dev_status, dev_policy),
134 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
137 static struct ubus_object_type dev_object_type =
138 UBUS_OBJECT_TYPE("device", dev_object_methods);
140 static struct ubus_object dev_object = {
141 .name = "network.device",
142 .type = &dev_object_type,
143 .methods = dev_object_methods,
144 .n_methods = ARRAY_SIZE(dev_object_methods),
148 netifd_ubus_init(const char *path)
152 ctx = ubus_connect(path);
156 DPRINTF("connected as %08x\n", ctx->local_id);
159 ubus_fd.fd = ctx->sock.fd;
160 netifd_fd_add(&ubus_fd);
162 ret = ubus_add_object(ctx, &main_object);
166 ret = ubus_add_object(ctx, &dev_object);
170 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
175 netifd_ubus_done(void)
181 /* per-interface object */
184 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
185 struct ubus_request_data *req, const char *method,
186 struct blob_attr *msg)
188 struct interface *iface;
190 iface = container_of(obj, struct interface, ubus);
191 interface_set_up(iface);
197 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
198 struct ubus_request_data *req, const char *method,
199 struct blob_attr *msg)
201 struct interface *iface;
203 iface = container_of(obj, struct interface, ubus);
204 interface_set_down(iface);
210 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
212 struct interface_error *error;
216 e = blobmsg_open_array(b, "errors");
217 list_for_each_entry(error, &iface->errors, list) {
218 e2 = blobmsg_open_table(b, NULL);
220 blobmsg_add_string(b, "subsystem", error->subsystem);
221 blobmsg_add_string(b, "code", error->code);
222 if (error->data[0]) {
223 e3 = blobmsg_open_array(b, "data");
224 for (i = 0; error->data[i]; i++)
225 blobmsg_add_string(b, NULL, error->data[i]);
226 blobmsg_close_array(b, e3);
229 blobmsg_close_table(b, e2);
231 blobmsg_close_array(b, e);
235 interface_ip_dump_address_list(struct interface_ip_settings *ip)
237 struct device_addr *addr;
243 vlist_for_each_element(&ip->addr, addr, node) {
244 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
249 a = blobmsg_open_table(&b, NULL);
251 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
252 inet_ntop(af, &addr->addr, buf, buflen);
253 blobmsg_add_string_buffer(&b);
255 blobmsg_add_u32(&b, "mask", addr->mask);
257 blobmsg_close_table(&b, a);
262 interface_ip_dump_route_list(struct interface_ip_settings *ip)
264 struct device_route *route;
270 vlist_for_each_element(&ip->route, route, node) {
271 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
276 r = blobmsg_open_table(&b, NULL);
278 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
279 inet_ntop(af, &route->addr, buf, buflen);
280 blobmsg_add_string_buffer(&b);
282 blobmsg_add_u32(&b, "mask", route->mask);
284 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
285 inet_ntop(af, &route->nexthop, buf, buflen);
286 blobmsg_add_string_buffer(&b);
288 blobmsg_close_table(&b, r);
293 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
294 struct ubus_request_data *req, const char *method,
295 struct blob_attr *msg)
297 struct interface *iface;
298 struct interface_data *data;
302 iface = container_of(obj, struct interface, ubus);
304 blob_buf_init(&b, 0);
305 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
306 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
307 blobmsg_add_u8(&b, "available", iface->available);
308 blobmsg_add_u8(&b, "autostart", iface->autostart);
310 if (iface->state == IFS_UP) {
311 time_t cur = system_get_rtime();
312 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
313 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
316 dev = iface->main_dev.dev;
317 if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
318 blobmsg_add_string(&b, "device", dev->ifname);
320 if (iface->state == IFS_UP) {
321 a = blobmsg_open_array(&b, "address");
322 interface_ip_dump_address_list(&iface->config_ip);
323 interface_ip_dump_address_list(&iface->proto_ip);
324 blobmsg_close_array(&b, a);
325 a = blobmsg_open_array(&b, "route");
326 interface_ip_dump_route_list(&iface->config_ip);
327 interface_ip_dump_route_list(&iface->proto_ip);
328 blobmsg_close_array(&b, a);
331 a = blobmsg_open_table(&b, "data");
332 avl_for_each_element(&iface->data, data, node)
333 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
335 blobmsg_close_table(&b, a);
337 if (!list_is_empty(&iface->errors))
338 netifd_add_interface_errors(&b, iface);
340 ubus_send_reply(ctx, req, b.head);
346 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
347 struct ubus_request_data *req, const char *method,
348 struct blob_attr *msg)
350 struct blob_attr *tb[__DEV_MAX];
351 struct interface *iface;
353 bool add = !strncmp(method, "add", 3);
356 iface = container_of(obj, struct interface, ubus);
358 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
361 return UBUS_STATUS_INVALID_ARGUMENT;
365 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
367 return UBUS_STATUS_NOT_FOUND;
370 return interface_add_link(iface, dev);
372 return interface_remove_link(iface, dev);
381 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
382 struct ubus_request_data *req, const char *method,
383 struct blob_attr *msg)
385 struct interface *iface;
387 iface = container_of(obj, struct interface, ubus);
389 if (!iface->proto || !iface->proto->notify)
390 return UBUS_STATUS_NOT_SUPPORTED;
392 return iface->proto->notify(iface->proto, msg);
396 netifd_iface_do_remove(struct uloop_timeout *timeout)
398 struct interface *iface;
400 iface = container_of(timeout, struct interface, remove_timer);
401 vlist_delete(&interfaces, &iface->node);
405 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
406 struct ubus_request_data *req, const char *method,
407 struct blob_attr *msg)
409 struct interface *iface;
411 iface = container_of(obj, struct interface, ubus);
412 if (iface->remove_timer.cb)
413 return UBUS_STATUS_INVALID_ARGUMENT;
415 iface->remove_timer.cb = netifd_iface_do_remove;
416 uloop_timeout_set(&iface->remove_timer, 100);
421 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
422 struct ubus_request_data *req, const char *method,
423 struct blob_attr *msg)
425 struct interface *iface;
427 const struct device_hotplug_ops *ops;
429 iface = container_of(obj, struct interface, ubus);
430 dev = iface->main_dev.dev;
434 ops = dev->hotplug_ops;
438 return ops->prepare(dev);
442 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
443 struct ubus_request_data *req, const char *method,
444 struct blob_attr *msg)
446 struct interface *iface;
447 struct blob_attr *cur;
450 iface = container_of(obj, struct interface, ubus);
452 blob_for_each_attr(cur, msg, rem) {
453 ret = interface_add_data(iface, cur);
461 static struct ubus_method iface_object_methods[] = {
462 { .name = "up", .handler = netifd_handle_up },
463 { .name = "down", .handler = netifd_handle_down },
464 { .name = "status", .handler = netifd_handle_status },
465 { .name = "prepare", .handler = netifd_handle_iface_prepare },
466 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
467 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
468 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
469 { .name = "remove", .handler = netifd_iface_remove },
470 { .name = "set_data", .handler = netifd_handle_set_data },
473 static struct ubus_object_type iface_object_type =
474 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
478 netifd_ubus_interface_event(struct interface *iface, bool up)
480 blob_buf_init(&b, 0);
481 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
482 blobmsg_add_string(&b, "interface", iface->name);
483 ubus_send_event(ctx, "network.interface", b.head);
487 netifd_ubus_add_interface(struct interface *iface)
489 struct ubus_object *obj = &iface->ubus;
492 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
497 obj->type = &iface_object_type;
498 obj->methods = iface_object_methods;
499 obj->n_methods = ARRAY_SIZE(iface_object_methods);
500 if (ubus_add_object(ctx, &iface->ubus)) {
501 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
508 netifd_ubus_remove_interface(struct interface *iface)
510 if (!iface->ubus.name)
513 ubus_remove_object(ctx, &iface->ubus);
514 free((void *) iface->ubus.name);