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 const char *ubus_path;
33 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
34 struct ubus_request_data *req, const char *method,
35 struct blob_attr *msg)
42 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
43 struct ubus_request_data *req, const char *method,
44 struct blob_attr *msg)
56 static const struct blobmsg_policy route_policy[__HR_MAX] = {
57 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
58 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
62 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
63 struct ubus_request_data *req, const char *method,
64 struct blob_attr *msg)
66 struct blob_attr *tb[__HR_MAX];
67 struct interface *iface;
71 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
73 return UBUS_STATUS_INVALID_ARGUMENT;
76 v6 = blobmsg_get_bool(tb[HR_V6]);
78 memset(&a, 0, sizeof(a));
79 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
80 return UBUS_STATUS_INVALID_ARGUMENT;
83 iface = interface_ip_add_target_route(&a, v6);
85 return UBUS_STATUS_NOT_FOUND;
88 blobmsg_add_string(&b, "interface", iface->name);
89 ubus_send_reply(ctx, req, b.head);
95 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
96 struct ubus_request_data *req, const char *method,
97 struct blob_attr *msg)
100 proto_dump_handlers(&b);
101 ubus_send_reply(ctx, req, b.head);
106 static struct ubus_method main_object_methods[] = {
107 { .name = "restart", .handler = netifd_handle_restart },
108 { .name = "reload", .handler = netifd_handle_reload },
109 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
110 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
113 static struct ubus_object_type main_object_type =
114 UBUS_OBJECT_TYPE("netifd", main_object_methods);
116 static struct ubus_object main_object = {
118 .type = &main_object_type,
119 .methods = main_object_methods,
120 .n_methods = ARRAY_SIZE(main_object_methods),
128 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
129 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
133 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
134 struct ubus_request_data *req, const char *method,
135 struct blob_attr *msg)
137 struct device *dev = NULL;
138 struct blob_attr *tb[__DEV_MAX];
140 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
143 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
145 return UBUS_STATUS_INVALID_ARGUMENT;
148 blob_buf_init(&b, 0);
149 device_dump_status(&b, dev);
150 ubus_send_reply(ctx, req, b.head);
161 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
162 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
163 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
167 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
168 struct ubus_request_data *req, const char *method,
169 struct blob_attr *msg)
171 struct device *dev = NULL;
172 struct blob_attr *tb[__ALIAS_ATTR_MAX];
173 struct blob_attr *cur;
176 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
178 if (!tb[ALIAS_ATTR_ALIAS])
179 return UBUS_STATUS_INVALID_ARGUMENT;
181 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
182 dev = device_get(blobmsg_data(cur), true);
184 return UBUS_STATUS_NOT_FOUND;
187 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
188 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
191 if (!blobmsg_check_attr(cur, NULL))
194 alias_notify_device(blobmsg_data(cur), dev);
199 device_free_unused(dev);
200 return UBUS_STATUS_INVALID_ARGUMENT;
209 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
210 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
211 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
215 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
216 struct ubus_request_data *req, const char *method,
217 struct blob_attr *msg)
219 struct device *dev = NULL;
220 struct blob_attr *tb[__DEV_STATE_MAX];
221 struct blob_attr *cur;
223 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
225 cur = tb[DEV_STATE_NAME];
227 return UBUS_STATUS_INVALID_ARGUMENT;
229 dev = device_get(blobmsg_data(cur), false);
231 return UBUS_STATUS_NOT_FOUND;
233 cur = tb[DEV_STATE_DEFER];
235 device_set_deferred(dev, !!blobmsg_get_u8(cur));
240 static struct ubus_method dev_object_methods[] = {
241 UBUS_METHOD("status", netifd_dev_status, dev_policy),
242 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
243 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
246 static struct ubus_object_type dev_object_type =
247 UBUS_OBJECT_TYPE("device", dev_object_methods);
249 static struct ubus_object dev_object = {
250 .name = "network.device",
251 .type = &dev_object_type,
252 .methods = dev_object_methods,
253 .n_methods = ARRAY_SIZE(dev_object_methods),
257 netifd_ubus_add_fd(void)
260 system_fd_set_cloexec(ctx->sock.fd);
264 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
266 static struct uloop_timeout retry = {
267 .cb = netifd_ubus_reconnect_timer,
271 if (ubus_reconnect(ctx, ubus_path) != 0) {
272 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
273 uloop_timeout_set(&retry, t * 1000);
277 DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
278 netifd_ubus_add_fd();
282 netifd_ubus_connection_lost(struct ubus_context *ctx)
284 netifd_ubus_reconnect_timer(NULL);
288 netifd_ubus_init(const char *path)
295 ctx = ubus_connect(path);
299 DPRINTF("connected as %08x\n", ctx->local_id);
300 ctx->connection_lost = netifd_ubus_connection_lost;
301 netifd_ubus_add_fd();
303 ret = ubus_add_object(ctx, &main_object);
307 ret = ubus_add_object(ctx, &dev_object);
311 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
316 netifd_ubus_done(void)
322 /* per-interface object */
325 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
326 struct ubus_request_data *req, const char *method,
327 struct blob_attr *msg)
329 struct interface *iface;
331 iface = container_of(obj, struct interface, ubus);
332 interface_set_up(iface);
338 netifd_handle_down(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;
344 iface = container_of(obj, struct interface, ubus);
345 interface_set_down(iface);
351 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
353 struct interface_error *error;
357 e = blobmsg_open_array(b, "errors");
358 list_for_each_entry(error, &iface->errors, list) {
359 e2 = blobmsg_open_table(b, NULL);
361 blobmsg_add_string(b, "subsystem", error->subsystem);
362 blobmsg_add_string(b, "code", error->code);
363 if (error->data[0]) {
364 e3 = blobmsg_open_array(b, "data");
365 for (i = 0; error->data[i]; i++)
366 blobmsg_add_string(b, NULL, error->data[i]);
367 blobmsg_close_array(b, e3);
370 blobmsg_close_table(b, e2);
372 blobmsg_close_array(b, e);
376 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
379 struct device_addr *addr;
385 vlist_for_each_element(&ip->addr, addr, node) {
386 if (addr->enabled != enabled)
389 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
394 if (af != (v6 ? AF_INET6 : AF_INET))
397 a = blobmsg_open_table(&b, NULL);
399 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
400 inet_ntop(af, &addr->addr, buf, buflen);
401 blobmsg_add_string_buffer(&b);
403 blobmsg_add_u32(&b, "mask", addr->mask);
405 blobmsg_close_table(&b, a);
410 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
412 struct device_route *route;
418 vlist_for_each_element(&ip->route, route, node) {
419 if (route->enabled != enabled)
422 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
427 r = blobmsg_open_table(&b, NULL);
429 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
430 inet_ntop(af, &route->addr, buf, buflen);
431 blobmsg_add_string_buffer(&b);
433 blobmsg_add_u32(&b, "mask", route->mask);
435 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
436 inet_ntop(af, &route->nexthop, buf, buflen);
437 blobmsg_add_string_buffer(&b);
439 if (route->flags & DEVROUTE_MTU)
440 blobmsg_add_u32(&b, "mtu", route->mtu);
442 if (route->flags & DEVROUTE_METRIC)
443 blobmsg_add_u32(&b, "metric", route->metric);
445 blobmsg_close_table(&b, r);
451 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
453 struct device_prefix *prefix;
456 const int buflen = INET6_ADDRSTRLEN;
458 vlist_for_each_element(&ip->prefix, prefix, node) {
459 a = blobmsg_open_table(&b, NULL);
461 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
462 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
463 blobmsg_add_string_buffer(&b);
465 blobmsg_add_u32(&b, "mask", prefix->length);
467 time_t now = system_get_rtime();
468 if (prefix->preferred_until) {
469 int preferred = prefix->preferred_until - now;
472 blobmsg_add_u32(&b, "preferred", preferred);
475 if (prefix->valid_until) {
476 int valid = prefix->valid_until - now;
479 blobmsg_add_u32(&b, "valid", valid);
482 c = blobmsg_open_table(&b, "assigned");
483 struct device_prefix_assignment *assign;
484 vlist_for_each_element(prefix->assignments, assign, node) {
485 void *d = blobmsg_open_table(&b, assign->name);
487 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
488 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
489 blobmsg_add_string_buffer(&b);
491 blobmsg_add_u32(&b, "mask", assign->length);
493 blobmsg_close_table(&b, d);
495 blobmsg_close_table(&b, c);
497 blobmsg_close_table(&b, a);
503 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
506 struct dns_server *dns;
510 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
511 if (ip->no_dns == enabled)
514 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
515 inet_ntop(dns->af, &dns->addr, buf, buflen);
516 blobmsg_add_string_buffer(&b);
521 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
524 struct dns_search_domain *dns;
526 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
527 if (ip->no_dns == enabled)
530 blobmsg_add_string(&b, NULL, dns->name);
535 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
536 struct ubus_request_data *req, const char *method,
537 struct blob_attr *msg)
539 struct interface *iface;
540 struct interface_data *data;
544 iface = container_of(obj, struct interface, ubus);
546 blob_buf_init(&b, 0);
547 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
548 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
549 blobmsg_add_u8(&b, "available", iface->available);
550 blobmsg_add_u8(&b, "autostart", iface->autostart);
552 if (iface->state == IFS_UP) {
553 time_t cur = system_get_rtime();
554 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
555 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
558 if (iface->proto_handler)
559 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
561 dev = iface->main_dev.dev;
562 if (dev && !dev->hidden &&
563 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
564 blobmsg_add_string(&b, "device", dev->ifname);
566 if (iface->state == IFS_UP) {
567 blobmsg_add_u32(&b, "metric", iface->metric);
568 a = blobmsg_open_array(&b, "ipv4-address");
569 interface_ip_dump_address_list(&iface->config_ip, false, true);
570 interface_ip_dump_address_list(&iface->proto_ip, false, true);
571 blobmsg_close_array(&b, a);
572 a = blobmsg_open_array(&b, "ipv6-address");
573 interface_ip_dump_address_list(&iface->config_ip, true, true);
574 interface_ip_dump_address_list(&iface->proto_ip, true, true);
575 blobmsg_close_array(&b, a);
576 a = blobmsg_open_array(&b, "ipv6-prefix");
577 interface_ip_dump_prefix_list(&iface->config_ip);
578 interface_ip_dump_prefix_list(&iface->proto_ip);
579 blobmsg_close_array(&b, a);
580 a = blobmsg_open_array(&b, "route");
581 interface_ip_dump_route_list(&iface->config_ip, true);
582 interface_ip_dump_route_list(&iface->proto_ip, true);
583 blobmsg_close_array(&b, a);
584 a = blobmsg_open_array(&b, "dns-server");
585 interface_ip_dump_dns_server_list(&iface->config_ip, true);
586 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
587 blobmsg_close_array(&b, a);
588 a = blobmsg_open_array(&b, "dns-search");
589 interface_ip_dump_dns_search_list(&iface->config_ip, true);
590 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
591 blobmsg_close_array(&b, a);
593 inactive = blobmsg_open_table(&b, "inactive");
594 a = blobmsg_open_array(&b, "ipv4-address");
595 interface_ip_dump_address_list(&iface->config_ip, false, false);
596 interface_ip_dump_address_list(&iface->proto_ip, false, false);
597 blobmsg_close_array(&b, a);
598 a = blobmsg_open_array(&b, "ipv6-address");
599 interface_ip_dump_address_list(&iface->config_ip, true, false);
600 interface_ip_dump_address_list(&iface->proto_ip, true, false);
601 blobmsg_close_array(&b, a);
602 a = blobmsg_open_array(&b, "route");
603 interface_ip_dump_route_list(&iface->config_ip, false);
604 interface_ip_dump_route_list(&iface->proto_ip, false);
605 blobmsg_close_array(&b, a);
606 a = blobmsg_open_array(&b, "dns-server");
607 interface_ip_dump_dns_server_list(&iface->config_ip, false);
608 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
609 blobmsg_close_array(&b, a);
610 a = blobmsg_open_array(&b, "dns-search");
611 interface_ip_dump_dns_search_list(&iface->config_ip, false);
612 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
613 blobmsg_close_array(&b, a);
614 blobmsg_close_table(&b, inactive);
617 a = blobmsg_open_table(&b, "data");
618 avl_for_each_element(&iface->data, data, node)
619 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
621 blobmsg_close_table(&b, a);
623 if (!list_is_empty(&iface->errors))
624 netifd_add_interface_errors(&b, iface);
626 ubus_send_reply(ctx, req, b.head);
632 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
633 struct ubus_request_data *req, const char *method,
634 struct blob_attr *msg)
636 struct blob_attr *tb[__DEV_MAX];
637 struct interface *iface;
639 bool add = !strncmp(method, "add", 3);
642 iface = container_of(obj, struct interface, ubus);
644 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
647 return UBUS_STATUS_INVALID_ARGUMENT;
651 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
653 ret = UBUS_STATUS_NOT_FOUND;
658 device_set_present(dev, true);
659 if (iface->device_config)
660 device_set_config(dev, &simple_device_type, iface->config);
662 system_if_apply_settings(dev, &dev->settings);
663 ret = interface_add_link(iface, dev);
665 ret = interface_remove_link(iface, dev);
676 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
677 struct ubus_request_data *req, const char *method,
678 struct blob_attr *msg)
680 struct interface *iface;
682 iface = container_of(obj, struct interface, ubus);
684 if (!iface->proto || !iface->proto->notify)
685 return UBUS_STATUS_NOT_SUPPORTED;
687 return iface->proto->notify(iface->proto, msg);
691 netifd_iface_do_remove(struct uloop_timeout *timeout)
693 struct interface *iface;
695 iface = container_of(timeout, struct interface, remove_timer);
696 vlist_delete(&interfaces, &iface->node);
700 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
701 struct ubus_request_data *req, const char *method,
702 struct blob_attr *msg)
704 struct interface *iface;
706 iface = container_of(obj, struct interface, ubus);
707 if (iface->remove_timer.cb)
708 return UBUS_STATUS_INVALID_ARGUMENT;
710 iface->remove_timer.cb = netifd_iface_do_remove;
711 uloop_timeout_set(&iface->remove_timer, 100);
716 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
717 struct ubus_request_data *req, const char *method,
718 struct blob_attr *msg)
720 struct interface *iface;
722 const struct device_hotplug_ops *ops;
724 iface = container_of(obj, struct interface, ubus);
725 dev = iface->main_dev.dev;
729 ops = dev->hotplug_ops;
733 return ops->prepare(dev);
737 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
738 struct ubus_request_data *req, const char *method,
739 struct blob_attr *msg)
741 struct interface *iface;
742 struct blob_attr *cur;
745 iface = container_of(obj, struct interface, ubus);
747 blob_for_each_attr(cur, msg, rem) {
748 ret = interface_add_data(iface, cur);
756 static struct ubus_method iface_object_methods[] = {
757 { .name = "up", .handler = netifd_handle_up },
758 { .name = "down", .handler = netifd_handle_down },
759 { .name = "status", .handler = netifd_handle_status },
760 { .name = "prepare", .handler = netifd_handle_iface_prepare },
761 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
762 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
763 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
764 { .name = "remove", .handler = netifd_iface_remove },
765 { .name = "set_data", .handler = netifd_handle_set_data },
768 static struct ubus_object_type iface_object_type =
769 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
773 netifd_ubus_interface_event(struct interface *iface, bool up)
775 blob_buf_init(&b, 0);
776 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
777 blobmsg_add_string(&b, "interface", iface->name);
778 ubus_send_event(ctx, "network.interface", b.head);
782 netifd_ubus_add_interface(struct interface *iface)
784 struct ubus_object *obj = &iface->ubus;
787 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
791 obj->type = &iface_object_type;
792 obj->methods = iface_object_methods;
793 obj->n_methods = ARRAY_SIZE(iface_object_methods);
794 if (ubus_add_object(ctx, &iface->ubus)) {
795 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
802 netifd_ubus_remove_interface(struct interface *iface)
804 if (!iface->ubus.name)
807 ubus_remove_object(ctx, &iface->ubus);
808 free((void *) iface->ubus.name);