2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <arpa/inet.h>
21 #include "interface.h"
26 static struct ubus_context *ctx = NULL;
27 static struct blob_buf b;
28 static struct netifd_fd ubus_fd;
29 static const char *ubus_path;
34 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
35 struct ubus_request_data *req, const char *method,
36 struct blob_attr *msg)
43 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
44 struct ubus_request_data *req, const char *method,
45 struct blob_attr *msg)
57 static const struct blobmsg_policy route_policy[__HR_MAX] = {
58 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
59 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
63 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
64 struct ubus_request_data *req, const char *method,
65 struct blob_attr *msg)
67 struct blob_attr *tb[__HR_MAX];
68 struct interface *iface;
72 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
74 return UBUS_STATUS_INVALID_ARGUMENT;
77 v6 = blobmsg_get_bool(tb[HR_V6]);
79 memset(&a, 0, sizeof(a));
80 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
81 return UBUS_STATUS_INVALID_ARGUMENT;
84 iface = interface_ip_add_target_route(&a, v6);
86 return UBUS_STATUS_NOT_FOUND;
89 blobmsg_add_string(&b, "interface", iface->name);
90 ubus_send_reply(ctx, req, b.head);
95 static struct ubus_method main_object_methods[] = {
96 { .name = "restart", .handler = netifd_handle_restart },
97 { .name = "reload", .handler = netifd_handle_reload },
98 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
101 static struct ubus_object_type main_object_type =
102 UBUS_OBJECT_TYPE("netifd", main_object_methods);
104 static struct ubus_object main_object = {
106 .type = &main_object_type,
107 .methods = main_object_methods,
108 .n_methods = ARRAY_SIZE(main_object_methods),
116 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
117 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
121 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
122 struct ubus_request_data *req, const char *method,
123 struct blob_attr *msg)
125 struct device *dev = NULL;
126 struct blob_attr *tb[__DEV_MAX];
128 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
131 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
133 return UBUS_STATUS_INVALID_ARGUMENT;
136 blob_buf_init(&b, 0);
137 device_dump_status(&b, dev);
138 ubus_send_reply(ctx, req, b.head);
149 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
150 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
151 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
155 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
156 struct ubus_request_data *req, const char *method,
157 struct blob_attr *msg)
159 struct device *dev = NULL;
160 struct blob_attr *tb[__ALIAS_ATTR_MAX];
161 struct blob_attr *cur;
164 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
166 if (!tb[ALIAS_ATTR_ALIAS])
167 return UBUS_STATUS_INVALID_ARGUMENT;
169 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
170 dev = device_get(blobmsg_data(cur), true);
172 return UBUS_STATUS_NOT_FOUND;
175 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
176 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
179 if (!blobmsg_check_attr(cur, NULL))
182 alias_notify_device(blobmsg_data(cur), dev);
187 device_free_unused(dev);
188 return UBUS_STATUS_INVALID_ARGUMENT;
191 static struct ubus_method dev_object_methods[] = {
192 UBUS_METHOD("status", netifd_dev_status, dev_policy),
193 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
196 static struct ubus_object_type dev_object_type =
197 UBUS_OBJECT_TYPE("device", dev_object_methods);
199 static struct ubus_object dev_object = {
200 .name = "network.device",
201 .type = &dev_object_type,
202 .methods = dev_object_methods,
203 .n_methods = ARRAY_SIZE(dev_object_methods),
207 netifd_ubus_add_fd(void)
210 ubus_fd.fd = ctx->sock.fd;
211 netifd_fd_add(&ubus_fd);
215 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
217 static struct uloop_timeout retry = {
218 .cb = netifd_ubus_reconnect_timer,
222 if (ubus_reconnect(ctx, ubus_path) != 0) {
223 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
224 uloop_timeout_set(&retry, t * 1000);
228 DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
229 netifd_ubus_add_fd();
233 netifd_ubus_connection_lost(struct ubus_context *ctx)
235 netifd_fd_delete(&ubus_fd);
236 netifd_ubus_reconnect_timer(NULL);
240 netifd_ubus_init(const char *path)
247 ctx = ubus_connect(path);
251 DPRINTF("connected as %08x\n", ctx->local_id);
252 ctx->connection_lost = netifd_ubus_connection_lost;
253 netifd_ubus_add_fd();
255 ret = ubus_add_object(ctx, &main_object);
259 ret = ubus_add_object(ctx, &dev_object);
263 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
268 netifd_ubus_done(void)
274 /* per-interface object */
277 netifd_handle_up(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);
284 interface_set_up(iface);
290 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
291 struct ubus_request_data *req, const char *method,
292 struct blob_attr *msg)
294 struct interface *iface;
296 iface = container_of(obj, struct interface, ubus);
297 interface_set_down(iface);
303 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
305 struct interface_error *error;
309 e = blobmsg_open_array(b, "errors");
310 list_for_each_entry(error, &iface->errors, list) {
311 e2 = blobmsg_open_table(b, NULL);
313 blobmsg_add_string(b, "subsystem", error->subsystem);
314 blobmsg_add_string(b, "code", error->code);
315 if (error->data[0]) {
316 e3 = blobmsg_open_array(b, "data");
317 for (i = 0; error->data[i]; i++)
318 blobmsg_add_string(b, NULL, error->data[i]);
319 blobmsg_close_array(b, e3);
322 blobmsg_close_table(b, e2);
324 blobmsg_close_array(b, e);
328 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6)
330 struct device_addr *addr;
336 vlist_for_each_element(&ip->addr, addr, node) {
337 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
342 if (af != (v6 ? AF_INET6 : AF_INET))
345 a = blobmsg_open_table(&b, NULL);
347 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
348 inet_ntop(af, &addr->addr, buf, buflen);
349 blobmsg_add_string_buffer(&b);
351 blobmsg_add_u32(&b, "mask", addr->mask);
353 blobmsg_close_table(&b, a);
358 interface_ip_dump_route_list(struct interface_ip_settings *ip)
360 struct device_route *route;
366 vlist_for_each_element(&ip->route, route, node) {
367 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
372 r = blobmsg_open_table(&b, NULL);
374 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
375 inet_ntop(af, &route->addr, buf, buflen);
376 blobmsg_add_string_buffer(&b);
378 blobmsg_add_u32(&b, "mask", route->mask);
380 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
381 inet_ntop(af, &route->nexthop, buf, buflen);
382 blobmsg_add_string_buffer(&b);
384 blobmsg_close_table(&b, r);
389 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip)
391 struct dns_server *dns;
395 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
396 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
397 inet_ntop(dns->af, &dns->addr, buf, buflen);
398 blobmsg_add_string_buffer(&b);
403 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip)
405 struct dns_search_domain *dns;
407 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
408 blobmsg_add_string(&b, NULL, dns->name);
413 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
414 struct ubus_request_data *req, const char *method,
415 struct blob_attr *msg)
417 struct interface *iface;
418 struct interface_data *data;
422 iface = container_of(obj, struct interface, ubus);
424 blob_buf_init(&b, 0);
425 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
426 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
427 blobmsg_add_u8(&b, "available", iface->available);
428 blobmsg_add_u8(&b, "autostart", iface->autostart);
430 if (iface->state == IFS_UP) {
431 time_t cur = system_get_rtime();
432 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
433 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
436 dev = iface->main_dev.dev;
437 if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
438 blobmsg_add_string(&b, "device", dev->ifname);
440 if (iface->state == IFS_UP) {
441 a = blobmsg_open_array(&b, "ipv4-address");
442 interface_ip_dump_address_list(&iface->config_ip, false);
443 interface_ip_dump_address_list(&iface->proto_ip, false);
444 blobmsg_close_array(&b, a);
445 a = blobmsg_open_array(&b, "ipv6-address");
446 interface_ip_dump_address_list(&iface->config_ip, true);
447 interface_ip_dump_address_list(&iface->proto_ip, true);
448 blobmsg_close_array(&b, a);
449 a = blobmsg_open_array(&b, "route");
450 interface_ip_dump_route_list(&iface->config_ip);
451 interface_ip_dump_route_list(&iface->proto_ip);
452 blobmsg_close_array(&b, a);
453 a = blobmsg_open_array(&b, "dns-server");
454 interface_ip_dump_dns_server_list(&iface->config_ip);
455 interface_ip_dump_dns_server_list(&iface->proto_ip);
456 blobmsg_close_array(&b, a);
457 a = blobmsg_open_array(&b, "dns-search");
458 interface_ip_dump_dns_search_list(&iface->config_ip);
459 interface_ip_dump_dns_search_list(&iface->proto_ip);
460 blobmsg_close_array(&b, a);
463 a = blobmsg_open_table(&b, "data");
464 avl_for_each_element(&iface->data, data, node)
465 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
467 blobmsg_close_table(&b, a);
469 if (!list_is_empty(&iface->errors))
470 netifd_add_interface_errors(&b, iface);
472 ubus_send_reply(ctx, req, b.head);
478 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
479 struct ubus_request_data *req, const char *method,
480 struct blob_attr *msg)
482 struct blob_attr *tb[__DEV_MAX];
483 struct interface *iface;
485 bool add = !strncmp(method, "add", 3);
488 iface = container_of(obj, struct interface, ubus);
490 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
493 return UBUS_STATUS_INVALID_ARGUMENT;
497 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
499 return UBUS_STATUS_NOT_FOUND;
502 return interface_add_link(iface, dev);
504 return interface_remove_link(iface, dev);
513 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
514 struct ubus_request_data *req, const char *method,
515 struct blob_attr *msg)
517 struct interface *iface;
519 iface = container_of(obj, struct interface, ubus);
521 if (!iface->proto || !iface->proto->notify)
522 return UBUS_STATUS_NOT_SUPPORTED;
524 return iface->proto->notify(iface->proto, msg);
528 netifd_iface_do_remove(struct uloop_timeout *timeout)
530 struct interface *iface;
532 iface = container_of(timeout, struct interface, remove_timer);
533 vlist_delete(&interfaces, &iface->node);
537 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
538 struct ubus_request_data *req, const char *method,
539 struct blob_attr *msg)
541 struct interface *iface;
543 iface = container_of(obj, struct interface, ubus);
544 if (iface->remove_timer.cb)
545 return UBUS_STATUS_INVALID_ARGUMENT;
547 iface->remove_timer.cb = netifd_iface_do_remove;
548 uloop_timeout_set(&iface->remove_timer, 100);
553 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
554 struct ubus_request_data *req, const char *method,
555 struct blob_attr *msg)
557 struct interface *iface;
559 const struct device_hotplug_ops *ops;
561 iface = container_of(obj, struct interface, ubus);
562 dev = iface->main_dev.dev;
566 ops = dev->hotplug_ops;
570 return ops->prepare(dev);
574 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
575 struct ubus_request_data *req, const char *method,
576 struct blob_attr *msg)
578 struct interface *iface;
579 struct blob_attr *cur;
582 iface = container_of(obj, struct interface, ubus);
584 blob_for_each_attr(cur, msg, rem) {
585 ret = interface_add_data(iface, cur);
593 static struct ubus_method iface_object_methods[] = {
594 { .name = "up", .handler = netifd_handle_up },
595 { .name = "down", .handler = netifd_handle_down },
596 { .name = "status", .handler = netifd_handle_status },
597 { .name = "prepare", .handler = netifd_handle_iface_prepare },
598 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
599 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
600 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
601 { .name = "remove", .handler = netifd_iface_remove },
602 { .name = "set_data", .handler = netifd_handle_set_data },
605 static struct ubus_object_type iface_object_type =
606 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
610 netifd_ubus_interface_event(struct interface *iface, bool up)
612 blob_buf_init(&b, 0);
613 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
614 blobmsg_add_string(&b, "interface", iface->name);
615 ubus_send_event(ctx, "network.interface", b.head);
619 netifd_ubus_add_interface(struct interface *iface)
621 struct ubus_object *obj = &iface->ubus;
624 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
629 obj->type = &iface_object_type;
630 obj->methods = iface_object_methods;
631 obj->n_methods = ARRAY_SIZE(iface_object_methods);
632 if (ubus_add_object(ctx, &iface->ubus)) {
633 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
640 netifd_ubus_remove_interface(struct interface *iface)
642 if (!iface->ubus.name)
645 ubus_remove_object(ctx, &iface->ubus);
646 free((void *) iface->ubus.name);