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"
27 struct ubus_context *ubus_ctx = NULL;
28 static struct blob_buf b;
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)
48 return UBUS_STATUS_UNKNOWN_ERROR;
50 return UBUS_STATUS_OK;
60 static const struct blobmsg_policy route_policy[__HR_MAX] = {
61 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
62 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
63 [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
67 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
68 struct ubus_request_data *req, const char *method,
69 struct blob_attr *msg)
71 struct blob_attr *tb[__HR_MAX];
72 struct interface *iface = NULL;
76 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
78 return UBUS_STATUS_INVALID_ARGUMENT;
81 v6 = blobmsg_get_bool(tb[HR_V6]);
84 iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
86 memset(&a, 0, sizeof(a));
87 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
88 return UBUS_STATUS_INVALID_ARGUMENT;
91 iface = interface_ip_add_target_route(&a, v6, iface);
93 return UBUS_STATUS_NOT_FOUND;
96 blobmsg_add_string(&b, "interface", iface->name);
97 ubus_send_reply(ctx, req, b.head);
103 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
104 struct ubus_request_data *req, const char *method,
105 struct blob_attr *msg)
107 blob_buf_init(&b, 0);
108 proto_dump_handlers(&b);
109 ubus_send_reply(ctx, req, b.head);
120 static const struct blobmsg_policy dynamic_policy[__DI_MAX] = {
121 [DI_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
125 netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
126 struct ubus_request_data *req, const char *method,
127 struct blob_attr *msg)
129 struct blob_attr *tb[__DI_MAX];
130 struct interface *iface;
131 struct blob_attr *config;
134 blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
137 return UBUS_STATUS_INVALID_ARGUMENT;
139 const char *name = blobmsg_get_string(tb[DI_NAME]);
141 iface = interface_alloc(name, msg);
143 return UBUS_STATUS_UNKNOWN_ERROR;
145 config = blob_memdup(msg);
149 interface_add(iface, config);
151 // need to look up the interface name again, in case of config update
152 // the pointer will have changed
153 iface = vlist_find(&interfaces, name, iface, node);
155 return UBUS_STATUS_UNKNOWN_ERROR;
157 // Set interface as dynamic
158 interface_set_dynamic(iface);
160 dev = iface->main_dev.dev;
161 if (!dev || !dev->default_config)
162 return UBUS_STATUS_UNKNOWN_ERROR;
164 return UBUS_STATUS_OK;
168 return UBUS_STATUS_UNKNOWN_ERROR;
171 static struct ubus_method main_object_methods[] = {
172 { .name = "restart", .handler = netifd_handle_restart },
173 { .name = "reload", .handler = netifd_handle_reload },
174 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
175 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
176 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
179 static struct ubus_object_type main_object_type =
180 UBUS_OBJECT_TYPE("netifd", main_object_methods);
182 static struct ubus_object main_object = {
184 .type = &main_object_type,
185 .methods = main_object_methods,
186 .n_methods = ARRAY_SIZE(main_object_methods),
194 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
195 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
199 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
200 struct ubus_request_data *req, const char *method,
201 struct blob_attr *msg)
203 struct device *dev = NULL;
204 struct blob_attr *tb[__DEV_MAX];
206 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
209 dev = device_find(blobmsg_data(tb[DEV_NAME]));
211 return UBUS_STATUS_INVALID_ARGUMENT;
214 blob_buf_init(&b, 0);
215 device_dump_status(&b, dev);
216 ubus_send_reply(ctx, req, b.head);
227 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
228 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
229 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
233 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
234 struct ubus_request_data *req, const char *method,
235 struct blob_attr *msg)
237 struct device *dev = NULL;
238 struct blob_attr *tb[__ALIAS_ATTR_MAX];
239 struct blob_attr *cur;
242 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
244 if (!tb[ALIAS_ATTR_ALIAS])
245 return UBUS_STATUS_INVALID_ARGUMENT;
247 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
248 dev = device_get(blobmsg_data(cur), true);
250 return UBUS_STATUS_NOT_FOUND;
253 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
254 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
257 if (!blobmsg_check_attr(cur, NULL))
260 alias_notify_device(blobmsg_data(cur), dev);
265 device_free_unused(dev);
266 return UBUS_STATUS_INVALID_ARGUMENT;
275 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
276 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
277 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
281 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
282 struct ubus_request_data *req, const char *method,
283 struct blob_attr *msg)
285 struct device *dev = NULL;
286 struct blob_attr *tb[__DEV_STATE_MAX];
287 struct blob_attr *cur;
289 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
291 cur = tb[DEV_STATE_NAME];
293 return UBUS_STATUS_INVALID_ARGUMENT;
295 dev = device_find(blobmsg_data(cur));
297 return UBUS_STATUS_NOT_FOUND;
299 cur = tb[DEV_STATE_DEFER];
301 device_set_deferred(dev, !!blobmsg_get_u8(cur));
306 static struct ubus_method dev_object_methods[] = {
307 UBUS_METHOD("status", netifd_dev_status, dev_policy),
308 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
309 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
312 static struct ubus_object_type dev_object_type =
313 UBUS_OBJECT_TYPE("device", dev_object_methods);
315 static struct ubus_object dev_object = {
316 .name = "network.device",
317 .type = &dev_object_type,
318 .methods = dev_object_methods,
319 .n_methods = ARRAY_SIZE(dev_object_methods),
323 netifd_ubus_add_fd(void)
325 ubus_add_uloop(ubus_ctx);
326 system_fd_set_cloexec(ubus_ctx->sock.fd);
330 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
332 static struct uloop_timeout retry = {
333 .cb = netifd_ubus_reconnect_timer,
337 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
338 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
339 uloop_timeout_set(&retry, t * 1000);
343 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
344 netifd_ubus_add_fd();
348 netifd_ubus_connection_lost(struct ubus_context *ctx)
350 netifd_ubus_reconnect_timer(NULL);
353 /* per-interface object */
356 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
357 struct ubus_request_data *req, const char *method,
358 struct blob_attr *msg)
360 struct interface *iface;
362 iface = container_of(obj, struct interface, ubus);
363 interface_set_up(iface);
369 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
370 struct ubus_request_data *req, const char *method,
371 struct blob_attr *msg)
373 struct interface *iface;
375 iface = container_of(obj, struct interface, ubus);
376 interface_set_down(iface);
382 netifd_handle_renew(struct ubus_context *ctx, struct ubus_object *obj,
383 struct ubus_request_data *req, const char *method,
384 struct blob_attr *msg)
386 struct interface *iface;
388 iface = container_of(obj, struct interface, ubus);
389 interface_renew(iface);
395 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
397 struct interface_error *error;
401 e = blobmsg_open_array(b, "errors");
402 list_for_each_entry(error, &iface->errors, list) {
403 e2 = blobmsg_open_table(b, NULL);
405 blobmsg_add_string(b, "subsystem", error->subsystem);
406 blobmsg_add_string(b, "code", error->code);
407 if (error->data[0]) {
408 e3 = blobmsg_open_array(b, "data");
409 for (i = 0; error->data[i]; i++)
410 blobmsg_add_string(b, NULL, error->data[i]);
411 blobmsg_close_array(b, e3);
414 blobmsg_close_table(b, e2);
416 blobmsg_close_array(b, e);
420 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6, bool enabled)
422 struct device_addr *addr;
428 time_t now = system_get_rtime();
429 vlist_for_each_element(&ip->addr, addr, node) {
430 if (addr->enabled != enabled)
433 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
438 if (af != (v6 ? AF_INET6 : AF_INET))
441 a = blobmsg_open_table(&b, NULL);
443 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
444 inet_ntop(af, &addr->addr, buf, buflen);
445 blobmsg_add_string_buffer(&b);
447 blobmsg_add_u32(&b, "mask", addr->mask);
449 if (addr->preferred_until) {
450 int preferred = addr->preferred_until - now;
453 blobmsg_add_u32(&b, "preferred", preferred);
456 if (addr->valid_until)
457 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
460 blobmsg_add_string(&b, "class", addr->pclass);
462 blobmsg_close_table(&b, a);
467 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
469 struct device_route *route;
475 time_t now = system_get_rtime();
476 vlist_for_each_element(&ip->route, route, node) {
477 if (route->enabled != enabled)
480 if ((ip->no_defaultroute == enabled) && !route->mask)
483 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
488 r = blobmsg_open_table(&b, NULL);
490 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
491 inet_ntop(af, &route->addr, buf, buflen);
492 blobmsg_add_string_buffer(&b);
494 blobmsg_add_u32(&b, "mask", route->mask);
496 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
497 inet_ntop(af, &route->nexthop, buf, buflen);
498 blobmsg_add_string_buffer(&b);
500 if (route->flags & DEVROUTE_TYPE)
501 blobmsg_add_u32(&b, "type", route->type);
503 if (route->flags & DEVROUTE_PROTO)
504 blobmsg_add_u32(&b, "proto", route->proto);
506 if (route->flags & DEVROUTE_MTU)
507 blobmsg_add_u32(&b, "mtu", route->mtu);
509 if (route->flags & DEVROUTE_METRIC)
510 blobmsg_add_u32(&b, "metric", route->metric);
512 if (route->flags & DEVROUTE_TABLE)
513 blobmsg_add_u32(&b, "table", route->table);
515 if (route->valid_until)
516 blobmsg_add_u32(&b, "valid", route->valid_until - now);
518 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
519 inet_ntop(af, &route->source, buf, buflen);
520 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
521 blobmsg_add_string_buffer(&b);
523 blobmsg_close_table(&b, r);
529 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
531 struct device_prefix *prefix;
534 const int buflen = INET6_ADDRSTRLEN;
536 time_t now = system_get_rtime();
537 vlist_for_each_element(&ip->prefix, prefix, node) {
538 a = blobmsg_open_table(&b, NULL);
540 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
541 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
542 blobmsg_add_string_buffer(&b);
544 blobmsg_add_u32(&b, "mask", prefix->length);
546 if (prefix->preferred_until) {
547 int preferred = prefix->preferred_until - now;
550 blobmsg_add_u32(&b, "preferred", preferred);
553 if (prefix->valid_until)
554 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
556 blobmsg_add_string(&b, "class", prefix->pclass);
558 c = blobmsg_open_table(&b, "assigned");
559 struct device_prefix_assignment *assign;
560 list_for_each_entry(assign, &prefix->assignments, head) {
561 if (!assign->name[0])
564 struct in6_addr addr = prefix->addr;
565 addr.s6_addr32[1] |= htonl(assign->assigned);
567 void *d = blobmsg_open_table(&b, assign->name);
569 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
570 inet_ntop(AF_INET6, &addr, buf, buflen);
571 blobmsg_add_string_buffer(&b);
573 blobmsg_add_u32(&b, "mask", assign->length);
575 blobmsg_close_table(&b, d);
577 blobmsg_close_table(&b, c);
579 blobmsg_close_table(&b, a);
585 interface_ip_dump_prefix_assignment_list(struct interface *iface)
589 const int buflen = INET6_ADDRSTRLEN;
590 time_t now = system_get_rtime();
592 struct device_prefix *prefix;
593 list_for_each_entry(prefix, &prefixes, head) {
594 struct device_prefix_assignment *assign;
595 list_for_each_entry(assign, &prefix->assignments, head) {
596 if (strcmp(assign->name, iface->name))
599 struct in6_addr addr = prefix->addr;
600 addr.s6_addr32[1] |= htonl(assign->assigned);
602 a = blobmsg_open_table(&b, NULL);
604 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
605 inet_ntop(AF_INET6, &addr, buf, buflen);
606 blobmsg_add_string_buffer(&b);
608 blobmsg_add_u32(&b, "mask", assign->length);
610 if (prefix->preferred_until) {
611 int preferred = prefix->preferred_until - now;
614 blobmsg_add_u32(&b, "preferred", preferred);
617 if (prefix->valid_until)
618 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
620 void *c = blobmsg_open_table(&b, "local-address");
621 if (assign->enabled) {
622 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
623 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
624 blobmsg_add_string_buffer(&b);
626 blobmsg_add_u32(&b, "mask", assign->length < 64 ? 64 : assign->length);
628 blobmsg_close_table(&b, c);
630 blobmsg_close_table(&b, a);
636 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip, bool enabled)
638 struct dns_server *dns;
642 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
643 if (ip->no_dns == enabled)
646 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
647 inet_ntop(dns->af, &dns->addr, buf, buflen);
648 blobmsg_add_string_buffer(&b);
653 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip, bool enabled)
655 struct dns_search_domain *dns;
657 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
658 if (ip->no_dns == enabled)
661 blobmsg_add_string(&b, NULL, dns->name);
666 netifd_dump_status(struct interface *iface)
668 struct interface_data *data;
672 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
673 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
674 blobmsg_add_u8(&b, "available", iface->available);
675 blobmsg_add_u8(&b, "autostart", iface->autostart);
676 blobmsg_add_u8(&b, "dynamic", iface->dynamic);
678 if (iface->state == IFS_UP) {
679 time_t cur = system_get_rtime();
680 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
681 if (iface->l3_dev.dev)
682 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
685 if (iface->proto_handler)
686 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
688 dev = iface->main_dev.dev;
689 if (dev && !dev->hidden && iface->proto_handler &&
690 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
691 blobmsg_add_string(&b, "device", dev->ifname);
693 if (iface->state == IFS_UP) {
694 if (iface->updated) {
695 a = blobmsg_open_array(&b, "updated");
697 if (iface->updated & IUF_ADDRESS)
698 blobmsg_add_string(&b, NULL, "addresses");
699 if (iface->updated & IUF_ROUTE)
700 blobmsg_add_string(&b, NULL, "routes");
701 if (iface->updated & IUF_PREFIX)
702 blobmsg_add_string(&b, NULL, "prefixes");
703 if (iface->updated & IUF_DATA)
704 blobmsg_add_string(&b, NULL, "data");
706 blobmsg_close_array(&b, a);
710 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
712 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
713 blobmsg_add_u32(&b, "metric", iface->metric);
714 blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
715 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
716 if (iface->assignment_weight)
717 blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
718 a = blobmsg_open_array(&b, "ipv4-address");
719 interface_ip_dump_address_list(&iface->config_ip, false, true);
720 interface_ip_dump_address_list(&iface->proto_ip, false, true);
721 blobmsg_close_array(&b, a);
722 a = blobmsg_open_array(&b, "ipv6-address");
723 interface_ip_dump_address_list(&iface->config_ip, true, true);
724 interface_ip_dump_address_list(&iface->proto_ip, true, true);
725 blobmsg_close_array(&b, a);
726 a = blobmsg_open_array(&b, "ipv6-prefix");
727 interface_ip_dump_prefix_list(&iface->config_ip);
728 interface_ip_dump_prefix_list(&iface->proto_ip);
729 blobmsg_close_array(&b, a);
730 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
731 interface_ip_dump_prefix_assignment_list(iface);
732 blobmsg_close_array(&b, a);
733 a = blobmsg_open_array(&b, "route");
734 interface_ip_dump_route_list(&iface->config_ip, true);
735 interface_ip_dump_route_list(&iface->proto_ip, true);
736 blobmsg_close_array(&b, a);
737 a = blobmsg_open_array(&b, "dns-server");
738 interface_ip_dump_dns_server_list(&iface->config_ip, true);
739 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
740 blobmsg_close_array(&b, a);
741 a = blobmsg_open_array(&b, "dns-search");
742 interface_ip_dump_dns_search_list(&iface->config_ip, true);
743 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
744 blobmsg_close_array(&b, a);
746 inactive = blobmsg_open_table(&b, "inactive");
747 a = blobmsg_open_array(&b, "ipv4-address");
748 interface_ip_dump_address_list(&iface->config_ip, false, false);
749 interface_ip_dump_address_list(&iface->proto_ip, false, false);
750 blobmsg_close_array(&b, a);
751 a = blobmsg_open_array(&b, "ipv6-address");
752 interface_ip_dump_address_list(&iface->config_ip, true, false);
753 interface_ip_dump_address_list(&iface->proto_ip, true, false);
754 blobmsg_close_array(&b, a);
755 a = blobmsg_open_array(&b, "route");
756 interface_ip_dump_route_list(&iface->config_ip, false);
757 interface_ip_dump_route_list(&iface->proto_ip, false);
758 blobmsg_close_array(&b, a);
759 a = blobmsg_open_array(&b, "dns-server");
760 interface_ip_dump_dns_server_list(&iface->config_ip, false);
761 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
762 blobmsg_close_array(&b, a);
763 a = blobmsg_open_array(&b, "dns-search");
764 interface_ip_dump_dns_search_list(&iface->config_ip, false);
765 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
766 blobmsg_close_array(&b, a);
767 blobmsg_close_table(&b, inactive);
770 a = blobmsg_open_table(&b, "data");
771 avl_for_each_element(&iface->data, data, node)
772 blobmsg_add_blob(&b, data->data);
774 blobmsg_close_table(&b, a);
776 if (!list_empty(&iface->errors))
777 netifd_add_interface_errors(&b, iface);
781 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
782 struct ubus_request_data *req, const char *method,
783 struct blob_attr *msg)
785 struct interface *iface = container_of(obj, struct interface, ubus);
787 blob_buf_init(&b, 0);
788 netifd_dump_status(iface);
789 ubus_send_reply(ctx, req, b.head);
796 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
797 struct ubus_request_data *req, const char *method,
798 struct blob_attr *msg)
800 blob_buf_init(&b, 0);
801 void *a = blobmsg_open_array(&b, "interface");
803 struct interface *iface;
804 vlist_for_each_element(&interfaces, iface, node) {
805 void *i = blobmsg_open_table(&b, NULL);
806 blobmsg_add_string(&b, "interface", iface->name);
807 netifd_dump_status(iface);
808 blobmsg_close_table(&b, i);
811 blobmsg_close_array(&b, a);
812 ubus_send_reply(ctx, req, b.head);
823 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
824 [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
825 [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
829 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
830 struct ubus_request_data *req, const char *method,
831 struct blob_attr *msg)
833 struct blob_attr *tb[__DEV_LINK_MAX];
834 struct blob_attr *cur;
835 struct interface *iface;
836 bool add = !strncmp(method, "add", 3);
837 bool link_ext = true;
839 iface = container_of(obj, struct interface, ubus);
841 blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
843 if (!tb[DEV_LINK_NAME])
844 return UBUS_STATUS_INVALID_ARGUMENT;
846 cur = tb[DEV_LINK_EXT];
848 link_ext = blobmsg_get_bool(cur);
850 return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
855 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
856 struct ubus_request_data *req, const char *method,
857 struct blob_attr *msg)
859 struct interface *iface;
861 iface = container_of(obj, struct interface, ubus);
863 if (!iface->proto || !iface->proto->notify)
864 return UBUS_STATUS_NOT_SUPPORTED;
866 return iface->proto->notify(iface->proto, msg);
870 netifd_iface_do_remove(struct uloop_timeout *timeout)
872 struct interface *iface;
874 iface = container_of(timeout, struct interface, remove_timer);
875 vlist_delete(&interfaces, &iface->node);
879 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
880 struct ubus_request_data *req, const char *method,
881 struct blob_attr *msg)
883 struct interface *iface;
885 iface = container_of(obj, struct interface, ubus);
886 if (iface->remove_timer.cb)
887 return UBUS_STATUS_INVALID_ARGUMENT;
889 iface->remove_timer.cb = netifd_iface_do_remove;
890 uloop_timeout_set(&iface->remove_timer, 100);
895 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
896 struct ubus_request_data *req, const char *method,
897 struct blob_attr *msg)
899 struct interface *iface;
901 const struct device_hotplug_ops *ops;
903 iface = container_of(obj, struct interface, ubus);
904 dev = iface->main_dev.dev;
908 ops = dev->hotplug_ops;
912 return ops->prepare(dev);
916 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
917 struct ubus_request_data *req, const char *method,
918 struct blob_attr *msg)
920 struct interface *iface;
922 iface = container_of(obj, struct interface, ubus);
924 return interface_parse_data(iface, msg);
927 static struct ubus_method iface_object_methods[] = {
928 { .name = "up", .handler = netifd_handle_up },
929 { .name = "down", .handler = netifd_handle_down },
930 { .name = "renew", .handler = netifd_handle_renew },
931 { .name = "status", .handler = netifd_handle_status },
932 { .name = "prepare", .handler = netifd_handle_iface_prepare },
933 { .name = "dump", .handler = netifd_handle_dump },
934 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
935 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
936 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
937 { .name = "remove", .handler = netifd_iface_remove },
938 { .name = "set_data", .handler = netifd_handle_set_data },
941 static struct ubus_object_type iface_object_type =
942 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
945 static struct ubus_object iface_object = {
946 .name = "network.interface",
947 .type = &iface_object_type,
948 .n_methods = ARRAY_SIZE(iface_object_methods),
951 static void netifd_add_object(struct ubus_object *obj)
953 int ret = ubus_add_object(ubus_ctx, obj);
956 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
959 static const struct blobmsg_policy iface_policy = {
961 .type = BLOBMSG_TYPE_STRING,
965 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
966 struct ubus_request_data *req, const char *method,
967 struct blob_attr *msg)
969 struct interface *iface;
970 struct blob_attr *tb;
973 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
975 return UBUS_STATUS_INVALID_ARGUMENT;
977 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
979 return UBUS_STATUS_NOT_FOUND;
981 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
984 if (strcmp(method, iface_object_methods[i].name) != 0)
987 cb = iface_object_methods[i].handler;
988 return cb(ctx, &iface->ubus, req, method, msg);
991 return UBUS_STATUS_INVALID_ARGUMENT;
994 static void netifd_add_iface_object(void)
996 struct ubus_method *methods;
999 methods = calloc(1, sizeof(iface_object_methods));
1003 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
1004 iface_object.methods = methods;
1006 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
1007 if (methods[i].handler == netifd_handle_dump)
1010 methods[i].handler = netifd_handle_iface;
1011 methods[i].policy = &iface_policy;
1012 methods[i].n_policy = 1;
1014 netifd_add_object(&iface_object);
1017 static struct wireless_device *
1018 get_wdev(struct blob_attr *msg, int *ret)
1020 struct blobmsg_policy wdev_policy = {
1022 .type = BLOBMSG_TYPE_STRING,
1024 struct blob_attr *dev_attr;
1025 struct wireless_device *wdev = NULL;
1028 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1030 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1034 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1036 *ret = UBUS_STATUS_NOT_FOUND;
1045 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1046 struct ubus_request_data *req, const char *method,
1047 struct blob_attr *msg)
1049 struct wireless_device *wdev;
1052 wdev = get_wdev(msg, &ret);
1053 if (ret == UBUS_STATUS_NOT_FOUND)
1057 wireless_device_set_up(wdev);
1059 vlist_for_each_element(&wireless_devices, wdev, node)
1060 wireless_device_set_up(wdev);
1067 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1068 struct ubus_request_data *req, const char *method,
1069 struct blob_attr *msg)
1071 struct wireless_device *wdev;
1074 wdev = get_wdev(msg, &ret);
1075 if (ret == UBUS_STATUS_NOT_FOUND)
1079 wireless_device_set_down(wdev);
1081 vlist_for_each_element(&wireless_devices, wdev, node)
1082 wireless_device_set_down(wdev);
1089 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1090 struct ubus_request_data *req, const char *method,
1091 struct blob_attr *msg)
1093 struct wireless_device *wdev;
1096 wdev = get_wdev(msg, &ret);
1097 if (ret == UBUS_STATUS_NOT_FOUND)
1100 blob_buf_init(&b, 0);
1102 wireless_device_status(wdev, &b);
1104 vlist_for_each_element(&wireless_devices, wdev, node)
1105 wireless_device_status(wdev, &b);
1107 ubus_send_reply(ctx, req, b.head);
1112 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1113 struct ubus_request_data *req, const char *method,
1114 struct blob_attr *msg)
1116 struct wireless_device *wdev;
1119 wdev = get_wdev(msg, &ret);
1120 if (ret == UBUS_STATUS_NOT_FOUND)
1123 blob_buf_init(&b, 0);
1125 wireless_device_get_validate(wdev, &b);
1127 vlist_for_each_element(&wireless_devices, wdev, node)
1128 wireless_device_get_validate(wdev, &b);
1130 ubus_send_reply(ctx, req, b.head);
1135 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1136 struct ubus_request_data *req, const char *method,
1137 struct blob_attr *msg)
1139 struct wireless_device *wdev;
1142 wdev = get_wdev(msg, &ret);
1146 return wireless_device_notify(wdev, msg, req);
1149 static struct ubus_method wireless_object_methods[] = {
1150 { .name = "up", .handler = netifd_handle_wdev_up },
1151 { .name = "down", .handler = netifd_handle_wdev_down },
1152 { .name = "status", .handler = netifd_handle_wdev_status },
1153 { .name = "notify", .handler = netifd_handle_wdev_notify },
1154 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1157 static struct ubus_object_type wireless_object_type =
1158 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1161 static struct ubus_object wireless_object = {
1162 .name = "network.wireless",
1163 .type = &wireless_object_type,
1164 .methods = wireless_object_methods,
1165 .n_methods = ARRAY_SIZE(wireless_object_methods),
1169 netifd_ubus_init(const char *path)
1174 ubus_ctx = ubus_connect(path);
1178 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1179 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1180 netifd_ubus_add_fd();
1182 netifd_add_object(&main_object);
1183 netifd_add_object(&dev_object);
1184 netifd_add_object(&wireless_object);
1185 netifd_add_iface_object();
1191 netifd_ubus_done(void)
1193 ubus_free(ubus_ctx);
1197 netifd_ubus_interface_event(struct interface *iface, bool up)
1199 blob_buf_init(&b, 0);
1200 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1201 blobmsg_add_string(&b, "interface", iface->name);
1202 ubus_send_event(ubus_ctx, "network.interface", b.head);
1206 netifd_ubus_interface_notify(struct interface *iface, bool up)
1208 const char *event = (up) ? "interface.update" : "interface.down";
1209 blob_buf_init(&b, 0);
1210 blobmsg_add_string(&b, "interface", iface->name);
1211 netifd_dump_status(iface);
1212 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1213 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1217 netifd_ubus_add_interface(struct interface *iface)
1219 struct ubus_object *obj = &iface->ubus;
1222 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1226 obj->type = &iface_object_type;
1227 obj->methods = iface_object_methods;
1228 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1229 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1230 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1237 netifd_ubus_remove_interface(struct interface *iface)
1239 if (!iface->ubus.name)
1242 ubus_remove_object(ubus_ctx, &iface->ubus);
1243 free((void *) iface->ubus.name);