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)
43 static const struct blobmsg_policy route_policy[__HR_MAX] = {
44 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
45 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
49 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
50 struct ubus_request_data *req, const char *method,
51 struct blob_attr *msg)
53 struct blob_attr *tb[__HR_MAX];
54 struct interface *iface;
58 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
60 return UBUS_STATUS_INVALID_ARGUMENT;
63 v6 = blobmsg_get_bool(tb[HR_V6]);
65 memset(&a, 0, sizeof(a));
66 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
67 return UBUS_STATUS_INVALID_ARGUMENT;
70 iface = interface_ip_add_target_route(&a, v6);
72 return UBUS_STATUS_NOT_FOUND;
75 blobmsg_add_string(&b, "interface", iface->name);
76 ubus_send_reply(ctx, req, b.head);
81 static struct ubus_method main_object_methods[] = {
82 { .name = "restart", .handler = netifd_handle_restart },
83 { .name = "reload", .handler = netifd_handle_reload },
84 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
87 static struct ubus_object_type main_object_type =
88 UBUS_OBJECT_TYPE("netifd", main_object_methods);
90 static struct ubus_object main_object = {
92 .type = &main_object_type,
93 .methods = main_object_methods,
94 .n_methods = ARRAY_SIZE(main_object_methods),
102 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
103 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
107 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
108 struct ubus_request_data *req, const char *method,
109 struct blob_attr *msg)
111 struct device *dev = NULL;
112 struct blob_attr *tb[__DEV_MAX];
114 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
117 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
119 return UBUS_STATUS_INVALID_ARGUMENT;
122 blob_buf_init(&b, 0);
123 device_dump_status(&b, dev);
124 ubus_send_reply(ctx, req, b.head);
135 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
136 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
137 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
141 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
142 struct ubus_request_data *req, const char *method,
143 struct blob_attr *msg)
145 struct device *dev = NULL;
146 struct blob_attr *tb[__ALIAS_ATTR_MAX];
147 struct blob_attr *cur;
150 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
152 if (!tb[ALIAS_ATTR_ALIAS])
153 return UBUS_STATUS_INVALID_ARGUMENT;
155 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
156 dev = device_get(blobmsg_data(cur), true);
158 return UBUS_STATUS_NOT_FOUND;
161 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
162 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
165 if (!blobmsg_check_attr(cur, NULL))
168 alias_notify_device(blobmsg_data(cur), dev);
173 device_free_unused(dev);
174 return UBUS_STATUS_INVALID_ARGUMENT;
177 static struct ubus_method dev_object_methods[] = {
178 UBUS_METHOD("status", netifd_dev_status, dev_policy),
179 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
182 static struct ubus_object_type dev_object_type =
183 UBUS_OBJECT_TYPE("device", dev_object_methods);
185 static struct ubus_object dev_object = {
186 .name = "network.device",
187 .type = &dev_object_type,
188 .methods = dev_object_methods,
189 .n_methods = ARRAY_SIZE(dev_object_methods),
193 netifd_ubus_init(const char *path)
197 ctx = ubus_connect(path);
201 DPRINTF("connected as %08x\n", ctx->local_id);
204 ubus_fd.fd = ctx->sock.fd;
205 netifd_fd_add(&ubus_fd);
207 ret = ubus_add_object(ctx, &main_object);
211 ret = ubus_add_object(ctx, &dev_object);
215 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
220 netifd_ubus_done(void)
226 /* per-interface object */
229 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
230 struct ubus_request_data *req, const char *method,
231 struct blob_attr *msg)
233 struct interface *iface;
235 iface = container_of(obj, struct interface, ubus);
236 interface_set_up(iface);
242 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
243 struct ubus_request_data *req, const char *method,
244 struct blob_attr *msg)
246 struct interface *iface;
248 iface = container_of(obj, struct interface, ubus);
249 interface_set_down(iface);
255 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
257 struct interface_error *error;
261 e = blobmsg_open_array(b, "errors");
262 list_for_each_entry(error, &iface->errors, list) {
263 e2 = blobmsg_open_table(b, NULL);
265 blobmsg_add_string(b, "subsystem", error->subsystem);
266 blobmsg_add_string(b, "code", error->code);
267 if (error->data[0]) {
268 e3 = blobmsg_open_array(b, "data");
269 for (i = 0; error->data[i]; i++)
270 blobmsg_add_string(b, NULL, error->data[i]);
271 blobmsg_close_array(b, e3);
274 blobmsg_close_table(b, e2);
276 blobmsg_close_array(b, e);
280 interface_ip_dump_address_list(struct interface_ip_settings *ip)
282 struct device_addr *addr;
288 vlist_for_each_element(&ip->addr, addr, node) {
289 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
294 a = blobmsg_open_table(&b, NULL);
296 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
297 inet_ntop(af, &addr->addr, buf, buflen);
298 blobmsg_add_string_buffer(&b);
300 blobmsg_add_u32(&b, "mask", addr->mask);
302 blobmsg_close_table(&b, a);
307 interface_ip_dump_route_list(struct interface_ip_settings *ip)
309 struct device_route *route;
315 vlist_for_each_element(&ip->route, route, node) {
316 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
321 r = blobmsg_open_table(&b, NULL);
323 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
324 inet_ntop(af, &route->addr, buf, buflen);
325 blobmsg_add_string_buffer(&b);
327 blobmsg_add_u32(&b, "mask", route->mask);
329 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
330 inet_ntop(af, &route->nexthop, buf, buflen);
331 blobmsg_add_string_buffer(&b);
333 blobmsg_close_table(&b, r);
338 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
339 struct ubus_request_data *req, const char *method,
340 struct blob_attr *msg)
342 struct interface *iface;
343 struct interface_data *data;
347 iface = container_of(obj, struct interface, ubus);
349 blob_buf_init(&b, 0);
350 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
351 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
352 blobmsg_add_u8(&b, "available", iface->available);
353 blobmsg_add_u8(&b, "autostart", iface->autostart);
355 if (iface->state == IFS_UP) {
356 time_t cur = system_get_rtime();
357 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
358 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
361 dev = iface->main_dev.dev;
362 if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
363 blobmsg_add_string(&b, "device", dev->ifname);
365 if (iface->state == IFS_UP) {
366 a = blobmsg_open_array(&b, "address");
367 interface_ip_dump_address_list(&iface->config_ip);
368 interface_ip_dump_address_list(&iface->proto_ip);
369 blobmsg_close_array(&b, a);
370 a = blobmsg_open_array(&b, "route");
371 interface_ip_dump_route_list(&iface->config_ip);
372 interface_ip_dump_route_list(&iface->proto_ip);
373 blobmsg_close_array(&b, a);
376 a = blobmsg_open_table(&b, "data");
377 avl_for_each_element(&iface->data, data, node)
378 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
380 blobmsg_close_table(&b, a);
382 if (!list_is_empty(&iface->errors))
383 netifd_add_interface_errors(&b, iface);
385 ubus_send_reply(ctx, req, b.head);
391 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
392 struct ubus_request_data *req, const char *method,
393 struct blob_attr *msg)
395 struct blob_attr *tb[__DEV_MAX];
396 struct interface *iface;
398 bool add = !strncmp(method, "add", 3);
401 iface = container_of(obj, struct interface, ubus);
403 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
406 return UBUS_STATUS_INVALID_ARGUMENT;
410 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
412 return UBUS_STATUS_NOT_FOUND;
415 return interface_add_link(iface, dev);
417 return interface_remove_link(iface, dev);
426 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
427 struct ubus_request_data *req, const char *method,
428 struct blob_attr *msg)
430 struct interface *iface;
432 iface = container_of(obj, struct interface, ubus);
434 if (!iface->proto || !iface->proto->notify)
435 return UBUS_STATUS_NOT_SUPPORTED;
437 return iface->proto->notify(iface->proto, msg);
441 netifd_iface_do_remove(struct uloop_timeout *timeout)
443 struct interface *iface;
445 iface = container_of(timeout, struct interface, remove_timer);
446 vlist_delete(&interfaces, &iface->node);
450 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
451 struct ubus_request_data *req, const char *method,
452 struct blob_attr *msg)
454 struct interface *iface;
456 iface = container_of(obj, struct interface, ubus);
457 if (iface->remove_timer.cb)
458 return UBUS_STATUS_INVALID_ARGUMENT;
460 iface->remove_timer.cb = netifd_iface_do_remove;
461 uloop_timeout_set(&iface->remove_timer, 100);
466 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
467 struct ubus_request_data *req, const char *method,
468 struct blob_attr *msg)
470 struct interface *iface;
472 const struct device_hotplug_ops *ops;
474 iface = container_of(obj, struct interface, ubus);
475 dev = iface->main_dev.dev;
479 ops = dev->hotplug_ops;
483 return ops->prepare(dev);
487 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
488 struct ubus_request_data *req, const char *method,
489 struct blob_attr *msg)
491 struct interface *iface;
492 struct blob_attr *cur;
495 iface = container_of(obj, struct interface, ubus);
497 blob_for_each_attr(cur, msg, rem) {
498 ret = interface_add_data(iface, cur);
506 static struct ubus_method iface_object_methods[] = {
507 { .name = "up", .handler = netifd_handle_up },
508 { .name = "down", .handler = netifd_handle_down },
509 { .name = "status", .handler = netifd_handle_status },
510 { .name = "prepare", .handler = netifd_handle_iface_prepare },
511 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
512 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
513 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
514 { .name = "remove", .handler = netifd_iface_remove },
515 { .name = "set_data", .handler = netifd_handle_set_data },
518 static struct ubus_object_type iface_object_type =
519 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
523 netifd_ubus_interface_event(struct interface *iface, bool up)
525 blob_buf_init(&b, 0);
526 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
527 blobmsg_add_string(&b, "interface", iface->name);
528 ubus_send_event(ctx, "network.interface", b.head);
532 netifd_ubus_add_interface(struct interface *iface)
534 struct ubus_object *obj = &iface->ubus;
537 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
542 obj->type = &iface_object_type;
543 obj->methods = iface_object_methods;
544 obj->n_methods = ARRAY_SIZE(iface_object_methods);
545 if (ubus_add_object(ctx, &iface->ubus)) {
546 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
553 netifd_ubus_remove_interface(struct interface *iface)
555 if (!iface->ubus.name)
558 ubus_remove_object(ctx, &iface->ubus);
559 free((void *) iface->ubus.name);