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)
58 static const struct blobmsg_policy route_policy[__HR_MAX] = {
59 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
60 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
61 [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
65 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
66 struct ubus_request_data *req, const char *method,
67 struct blob_attr *msg)
69 struct blob_attr *tb[__HR_MAX];
70 struct interface *iface = NULL;
74 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
76 return UBUS_STATUS_INVALID_ARGUMENT;
79 v6 = blobmsg_get_bool(tb[HR_V6]);
82 iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
84 memset(&a, 0, sizeof(a));
85 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
86 return UBUS_STATUS_INVALID_ARGUMENT;
89 iface = interface_ip_add_target_route(&a, v6, iface);
91 return UBUS_STATUS_NOT_FOUND;
94 blobmsg_add_string(&b, "interface", iface->name);
95 ubus_send_reply(ctx, req, b.head);
101 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
102 struct ubus_request_data *req, const char *method,
103 struct blob_attr *msg)
105 blob_buf_init(&b, 0);
106 proto_dump_handlers(&b);
107 ubus_send_reply(ctx, req, b.head);
118 static const struct blobmsg_policy dynamic_policy[__DI_MAX] = {
119 [DI_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
123 netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
124 struct ubus_request_data *req, const char *method,
125 struct blob_attr *msg)
127 struct blob_attr *tb[__DI_MAX];
128 struct interface *iface;
129 struct blob_attr *config;
132 blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
135 return UBUS_STATUS_INVALID_ARGUMENT;
137 const char *name = blobmsg_get_string(tb[DI_NAME]);
139 iface = interface_alloc(name, msg);
141 return UBUS_STATUS_UNKNOWN_ERROR;
143 config = blob_memdup(msg);
147 interface_add(iface, config);
149 // need to look up the interface name again, in case of config update
150 // the pointer will have changed
151 iface = vlist_find(&interfaces, name, iface, node);
153 return UBUS_STATUS_UNKNOWN_ERROR;
155 // Set interface as dynamic
156 interface_set_dynamic(iface);
158 dev = iface->main_dev.dev;
159 if (!dev || !dev->default_config)
160 return UBUS_STATUS_UNKNOWN_ERROR;
162 return UBUS_STATUS_OK;
166 return UBUS_STATUS_UNKNOWN_ERROR;
169 static struct ubus_method main_object_methods[] = {
170 { .name = "restart", .handler = netifd_handle_restart },
171 { .name = "reload", .handler = netifd_handle_reload },
172 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
173 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
174 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
177 static struct ubus_object_type main_object_type =
178 UBUS_OBJECT_TYPE("netifd", main_object_methods);
180 static struct ubus_object main_object = {
182 .type = &main_object_type,
183 .methods = main_object_methods,
184 .n_methods = ARRAY_SIZE(main_object_methods),
192 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
193 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
197 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
198 struct ubus_request_data *req, const char *method,
199 struct blob_attr *msg)
201 struct device *dev = NULL;
202 struct blob_attr *tb[__DEV_MAX];
204 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
207 dev = device_find(blobmsg_data(tb[DEV_NAME]));
209 return UBUS_STATUS_INVALID_ARGUMENT;
212 blob_buf_init(&b, 0);
213 device_dump_status(&b, dev);
214 ubus_send_reply(ctx, req, b.head);
225 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
226 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
227 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
231 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
232 struct ubus_request_data *req, const char *method,
233 struct blob_attr *msg)
235 struct device *dev = NULL;
236 struct blob_attr *tb[__ALIAS_ATTR_MAX];
237 struct blob_attr *cur;
240 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
242 if (!tb[ALIAS_ATTR_ALIAS])
243 return UBUS_STATUS_INVALID_ARGUMENT;
245 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
246 dev = device_get(blobmsg_data(cur), true);
248 return UBUS_STATUS_NOT_FOUND;
251 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
252 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
255 if (!blobmsg_check_attr(cur, NULL))
258 alias_notify_device(blobmsg_data(cur), dev);
263 device_free_unused(dev);
264 return UBUS_STATUS_INVALID_ARGUMENT;
273 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
274 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
275 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
279 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
280 struct ubus_request_data *req, const char *method,
281 struct blob_attr *msg)
283 struct device *dev = NULL;
284 struct blob_attr *tb[__DEV_STATE_MAX];
285 struct blob_attr *cur;
287 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
289 cur = tb[DEV_STATE_NAME];
291 return UBUS_STATUS_INVALID_ARGUMENT;
293 dev = device_find(blobmsg_data(cur));
295 return UBUS_STATUS_NOT_FOUND;
297 cur = tb[DEV_STATE_DEFER];
299 device_set_deferred(dev, !!blobmsg_get_u8(cur));
304 static struct ubus_method dev_object_methods[] = {
305 UBUS_METHOD("status", netifd_dev_status, dev_policy),
306 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
307 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
310 static struct ubus_object_type dev_object_type =
311 UBUS_OBJECT_TYPE("device", dev_object_methods);
313 static struct ubus_object dev_object = {
314 .name = "network.device",
315 .type = &dev_object_type,
316 .methods = dev_object_methods,
317 .n_methods = ARRAY_SIZE(dev_object_methods),
321 netifd_ubus_add_fd(void)
323 ubus_add_uloop(ubus_ctx);
324 system_fd_set_cloexec(ubus_ctx->sock.fd);
328 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
330 static struct uloop_timeout retry = {
331 .cb = netifd_ubus_reconnect_timer,
335 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
336 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
337 uloop_timeout_set(&retry, t * 1000);
341 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
342 netifd_ubus_add_fd();
346 netifd_ubus_connection_lost(struct ubus_context *ctx)
348 netifd_ubus_reconnect_timer(NULL);
351 /* per-interface object */
354 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
355 struct ubus_request_data *req, const char *method,
356 struct blob_attr *msg)
358 struct interface *iface;
360 iface = container_of(obj, struct interface, ubus);
361 interface_set_up(iface);
367 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
368 struct ubus_request_data *req, const char *method,
369 struct blob_attr *msg)
371 struct interface *iface;
373 iface = container_of(obj, struct interface, ubus);
374 interface_set_down(iface);
380 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
382 struct interface_error *error;
386 e = blobmsg_open_array(b, "errors");
387 list_for_each_entry(error, &iface->errors, list) {
388 e2 = blobmsg_open_table(b, NULL);
390 blobmsg_add_string(b, "subsystem", error->subsystem);
391 blobmsg_add_string(b, "code", error->code);
392 if (error->data[0]) {
393 e3 = blobmsg_open_array(b, "data");
394 for (i = 0; error->data[i]; i++)
395 blobmsg_add_string(b, NULL, error->data[i]);
396 blobmsg_close_array(b, e3);
399 blobmsg_close_table(b, e2);
401 blobmsg_close_array(b, e);
405 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
408 struct device_addr *addr;
414 time_t now = system_get_rtime();
415 vlist_for_each_element(&ip->addr, addr, node) {
416 if (addr->enabled != enabled)
419 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
424 if (af != (v6 ? AF_INET6 : AF_INET))
427 a = blobmsg_open_table(&b, NULL);
429 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
430 inet_ntop(af, &addr->addr, buf, buflen);
431 blobmsg_add_string_buffer(&b);
433 blobmsg_add_u32(&b, "mask", addr->mask);
435 if (addr->preferred_until) {
436 int preferred = addr->preferred_until - now;
439 blobmsg_add_u32(&b, "preferred", preferred);
442 if (addr->valid_until)
443 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
446 blobmsg_add_string(&b, "class", addr->pclass);
448 blobmsg_close_table(&b, a);
453 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
455 struct device_route *route;
461 time_t now = system_get_rtime();
462 vlist_for_each_element(&ip->route, route, node) {
463 if (route->enabled != enabled)
466 if ((ip->no_defaultroute == enabled) && !route->mask)
469 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
474 r = blobmsg_open_table(&b, NULL);
476 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
477 inet_ntop(af, &route->addr, buf, buflen);
478 blobmsg_add_string_buffer(&b);
480 blobmsg_add_u32(&b, "mask", route->mask);
482 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
483 inet_ntop(af, &route->nexthop, buf, buflen);
484 blobmsg_add_string_buffer(&b);
486 if (route->flags & DEVROUTE_TYPE)
487 blobmsg_add_u32(&b, "type", route->type);
489 if (route->flags & DEVROUTE_MTU)
490 blobmsg_add_u32(&b, "mtu", route->mtu);
492 if (route->flags & DEVROUTE_METRIC)
493 blobmsg_add_u32(&b, "metric", route->metric);
495 if (route->flags & DEVROUTE_TABLE)
496 blobmsg_add_u32(&b, "table", route->table);
498 if (route->valid_until)
499 blobmsg_add_u32(&b, "valid", route->valid_until - now);
501 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
502 inet_ntop(af, &route->source, buf, buflen);
503 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
504 blobmsg_add_string_buffer(&b);
506 blobmsg_close_table(&b, r);
512 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
514 struct device_prefix *prefix;
517 const int buflen = INET6_ADDRSTRLEN;
519 time_t now = system_get_rtime();
520 vlist_for_each_element(&ip->prefix, prefix, node) {
521 a = blobmsg_open_table(&b, NULL);
523 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
524 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
525 blobmsg_add_string_buffer(&b);
527 blobmsg_add_u32(&b, "mask", prefix->length);
529 if (prefix->preferred_until) {
530 int preferred = prefix->preferred_until - now;
533 blobmsg_add_u32(&b, "preferred", preferred);
536 if (prefix->valid_until)
537 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
539 blobmsg_add_string(&b, "class", prefix->pclass);
541 c = blobmsg_open_table(&b, "assigned");
542 struct device_prefix_assignment *assign;
543 list_for_each_entry(assign, &prefix->assignments, head) {
544 if (!assign->name[0])
547 struct in6_addr addr = prefix->addr;
548 addr.s6_addr32[1] |= htonl(assign->assigned);
550 void *d = blobmsg_open_table(&b, assign->name);
552 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
553 inet_ntop(AF_INET6, &addr, buf, buflen);
554 blobmsg_add_string_buffer(&b);
556 blobmsg_add_u32(&b, "mask", assign->length);
558 blobmsg_close_table(&b, d);
560 blobmsg_close_table(&b, c);
562 blobmsg_close_table(&b, a);
568 interface_ip_dump_prefix_assignment_list(struct interface *iface)
572 const int buflen = INET6_ADDRSTRLEN;
573 time_t now = system_get_rtime();
575 struct device_prefix *prefix;
576 list_for_each_entry(prefix, &prefixes, head) {
577 struct device_prefix_assignment *assign;
578 list_for_each_entry(assign, &prefix->assignments, head) {
579 if (strcmp(assign->name, iface->name))
582 struct in6_addr addr = prefix->addr;
583 addr.s6_addr32[1] |= htonl(assign->assigned);
585 a = blobmsg_open_table(&b, NULL);
587 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
588 inet_ntop(AF_INET6, &addr, buf, buflen);
589 blobmsg_add_string_buffer(&b);
591 blobmsg_add_u32(&b, "mask", assign->length);
593 if (prefix->preferred_until) {
594 int preferred = prefix->preferred_until - now;
597 blobmsg_add_u32(&b, "preferred", preferred);
600 if (prefix->valid_until)
601 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
603 void *c = blobmsg_open_table(&b, "local-address");
604 if (assign->enabled) {
605 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
606 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
607 blobmsg_add_string_buffer(&b);
609 blobmsg_add_u32(&b, "mask", assign->length < 64 ? 64 : assign->length);
611 blobmsg_close_table(&b, c);
613 blobmsg_close_table(&b, a);
619 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
622 struct dns_server *dns;
626 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
627 if (ip->no_dns == enabled)
630 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
631 inet_ntop(dns->af, &dns->addr, buf, buflen);
632 blobmsg_add_string_buffer(&b);
637 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
640 struct dns_search_domain *dns;
642 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
643 if (ip->no_dns == enabled)
646 blobmsg_add_string(&b, NULL, dns->name);
651 netifd_dump_status(struct interface *iface)
653 struct interface_data *data;
657 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
658 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
659 blobmsg_add_u8(&b, "available", iface->available);
660 blobmsg_add_u8(&b, "autostart", iface->autostart);
661 blobmsg_add_u8(&b, "dynamic", iface->dynamic);
663 if (iface->state == IFS_UP) {
664 time_t cur = system_get_rtime();
665 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
666 if (iface->l3_dev.dev)
667 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
670 if (iface->proto_handler)
671 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
673 dev = iface->main_dev.dev;
674 if (dev && !dev->hidden && iface->proto_handler &&
675 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
676 blobmsg_add_string(&b, "device", dev->ifname);
678 if (iface->state == IFS_UP) {
679 if (iface->updated) {
680 a = blobmsg_open_array(&b, "updated");
682 if (iface->updated & IUF_ADDRESS)
683 blobmsg_add_string(&b, NULL, "addresses");
684 if (iface->updated & IUF_ROUTE)
685 blobmsg_add_string(&b, NULL, "routes");
686 if (iface->updated & IUF_PREFIX)
687 blobmsg_add_string(&b, NULL, "prefixes");
688 if (iface->updated & IUF_DATA)
689 blobmsg_add_string(&b, NULL, "data");
691 blobmsg_close_array(&b, a);
695 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
697 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
698 blobmsg_add_u32(&b, "metric", iface->metric);
699 blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
700 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
701 a = blobmsg_open_array(&b, "ipv4-address");
702 interface_ip_dump_address_list(&iface->config_ip, false, true);
703 interface_ip_dump_address_list(&iface->proto_ip, false, true);
704 blobmsg_close_array(&b, a);
705 a = blobmsg_open_array(&b, "ipv6-address");
706 interface_ip_dump_address_list(&iface->config_ip, true, true);
707 interface_ip_dump_address_list(&iface->proto_ip, true, true);
708 blobmsg_close_array(&b, a);
709 a = blobmsg_open_array(&b, "ipv6-prefix");
710 interface_ip_dump_prefix_list(&iface->config_ip);
711 interface_ip_dump_prefix_list(&iface->proto_ip);
712 blobmsg_close_array(&b, a);
713 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
714 interface_ip_dump_prefix_assignment_list(iface);
715 blobmsg_close_array(&b, a);
716 a = blobmsg_open_array(&b, "route");
717 interface_ip_dump_route_list(&iface->config_ip, true);
718 interface_ip_dump_route_list(&iface->proto_ip, true);
719 blobmsg_close_array(&b, a);
720 a = blobmsg_open_array(&b, "dns-server");
721 interface_ip_dump_dns_server_list(&iface->config_ip, true);
722 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
723 blobmsg_close_array(&b, a);
724 a = blobmsg_open_array(&b, "dns-search");
725 interface_ip_dump_dns_search_list(&iface->config_ip, true);
726 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
727 blobmsg_close_array(&b, a);
729 inactive = blobmsg_open_table(&b, "inactive");
730 a = blobmsg_open_array(&b, "ipv4-address");
731 interface_ip_dump_address_list(&iface->config_ip, false, false);
732 interface_ip_dump_address_list(&iface->proto_ip, false, false);
733 blobmsg_close_array(&b, a);
734 a = blobmsg_open_array(&b, "ipv6-address");
735 interface_ip_dump_address_list(&iface->config_ip, true, false);
736 interface_ip_dump_address_list(&iface->proto_ip, true, false);
737 blobmsg_close_array(&b, a);
738 a = blobmsg_open_array(&b, "route");
739 interface_ip_dump_route_list(&iface->config_ip, false);
740 interface_ip_dump_route_list(&iface->proto_ip, false);
741 blobmsg_close_array(&b, a);
742 a = blobmsg_open_array(&b, "dns-server");
743 interface_ip_dump_dns_server_list(&iface->config_ip, false);
744 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
745 blobmsg_close_array(&b, a);
746 a = blobmsg_open_array(&b, "dns-search");
747 interface_ip_dump_dns_search_list(&iface->config_ip, false);
748 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
749 blobmsg_close_array(&b, a);
750 blobmsg_close_table(&b, inactive);
753 a = blobmsg_open_table(&b, "data");
754 avl_for_each_element(&iface->data, data, node)
755 blobmsg_add_blob(&b, data->data);
757 blobmsg_close_table(&b, a);
759 if (!list_empty(&iface->errors))
760 netifd_add_interface_errors(&b, iface);
764 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
765 struct ubus_request_data *req, const char *method,
766 struct blob_attr *msg)
768 struct interface *iface = container_of(obj, struct interface, ubus);
770 blob_buf_init(&b, 0);
771 netifd_dump_status(iface);
772 ubus_send_reply(ctx, req, b.head);
779 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
780 struct ubus_request_data *req, const char *method,
781 struct blob_attr *msg)
783 blob_buf_init(&b, 0);
784 void *a = blobmsg_open_array(&b, "interface");
786 struct interface *iface;
787 vlist_for_each_element(&interfaces, iface, node) {
788 void *i = blobmsg_open_table(&b, NULL);
789 blobmsg_add_string(&b, "interface", iface->name);
790 netifd_dump_status(iface);
791 blobmsg_close_table(&b, i);
794 blobmsg_close_array(&b, a);
795 ubus_send_reply(ctx, req, b.head);
806 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
807 [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
808 [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
812 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
813 struct ubus_request_data *req, const char *method,
814 struct blob_attr *msg)
816 struct blob_attr *tb[__DEV_LINK_MAX];
817 struct blob_attr *cur;
818 struct interface *iface;
819 bool add = !strncmp(method, "add", 3);
820 bool link_ext = true;
822 iface = container_of(obj, struct interface, ubus);
824 blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
826 if (!tb[DEV_LINK_NAME])
827 return UBUS_STATUS_INVALID_ARGUMENT;
829 cur = tb[DEV_LINK_EXT];
831 link_ext = blobmsg_get_bool(cur);
833 return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
838 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
839 struct ubus_request_data *req, const char *method,
840 struct blob_attr *msg)
842 struct interface *iface;
844 iface = container_of(obj, struct interface, ubus);
846 if (!iface->proto || !iface->proto->notify)
847 return UBUS_STATUS_NOT_SUPPORTED;
849 return iface->proto->notify(iface->proto, msg);
853 netifd_iface_do_remove(struct uloop_timeout *timeout)
855 struct interface *iface;
857 iface = container_of(timeout, struct interface, remove_timer);
858 vlist_delete(&interfaces, &iface->node);
862 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
863 struct ubus_request_data *req, const char *method,
864 struct blob_attr *msg)
866 struct interface *iface;
868 iface = container_of(obj, struct interface, ubus);
869 if (iface->remove_timer.cb)
870 return UBUS_STATUS_INVALID_ARGUMENT;
872 iface->remove_timer.cb = netifd_iface_do_remove;
873 uloop_timeout_set(&iface->remove_timer, 100);
878 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
879 struct ubus_request_data *req, const char *method,
880 struct blob_attr *msg)
882 struct interface *iface;
884 const struct device_hotplug_ops *ops;
886 iface = container_of(obj, struct interface, ubus);
887 dev = iface->main_dev.dev;
891 ops = dev->hotplug_ops;
895 return ops->prepare(dev);
899 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
900 struct ubus_request_data *req, const char *method,
901 struct blob_attr *msg)
903 struct interface *iface;
905 iface = container_of(obj, struct interface, ubus);
907 return interface_parse_data(iface, msg);
910 static struct ubus_method iface_object_methods[] = {
911 { .name = "up", .handler = netifd_handle_up },
912 { .name = "down", .handler = netifd_handle_down },
913 { .name = "status", .handler = netifd_handle_status },
914 { .name = "prepare", .handler = netifd_handle_iface_prepare },
915 { .name = "dump", .handler = netifd_handle_dump },
916 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
917 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
918 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
919 { .name = "remove", .handler = netifd_iface_remove },
920 { .name = "set_data", .handler = netifd_handle_set_data },
923 static struct ubus_object_type iface_object_type =
924 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
927 static struct ubus_object iface_object = {
928 .name = "network.interface",
929 .type = &iface_object_type,
930 .n_methods = ARRAY_SIZE(iface_object_methods),
933 static void netifd_add_object(struct ubus_object *obj)
935 int ret = ubus_add_object(ubus_ctx, obj);
938 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
941 static const struct blobmsg_policy iface_policy = {
943 .type = BLOBMSG_TYPE_STRING,
947 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
948 struct ubus_request_data *req, const char *method,
949 struct blob_attr *msg)
951 struct interface *iface;
952 struct blob_attr *tb;
955 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
957 return UBUS_STATUS_INVALID_ARGUMENT;
959 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
961 return UBUS_STATUS_NOT_FOUND;
963 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
966 if (strcmp(method, iface_object_methods[i].name) != 0)
969 cb = iface_object_methods[i].handler;
970 return cb(ctx, &iface->ubus, req, method, msg);
973 return UBUS_STATUS_INVALID_ARGUMENT;
976 static void netifd_add_iface_object(void)
978 struct ubus_method *methods;
981 methods = calloc(1, sizeof(iface_object_methods));
985 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
986 iface_object.methods = methods;
988 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
989 if (methods[i].handler == netifd_handle_dump)
992 methods[i].handler = netifd_handle_iface;
993 methods[i].policy = &iface_policy;
994 methods[i].n_policy = 1;
996 netifd_add_object(&iface_object);
999 static struct wireless_device *
1000 get_wdev(struct blob_attr *msg, int *ret)
1002 struct blobmsg_policy wdev_policy = {
1004 .type = BLOBMSG_TYPE_STRING,
1006 struct blob_attr *dev_attr;
1007 struct wireless_device *wdev = NULL;
1010 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1012 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1016 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1018 *ret = UBUS_STATUS_NOT_FOUND;
1027 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1028 struct ubus_request_data *req, const char *method,
1029 struct blob_attr *msg)
1031 struct wireless_device *wdev;
1034 wdev = get_wdev(msg, &ret);
1035 if (ret == UBUS_STATUS_NOT_FOUND)
1039 wireless_device_set_up(wdev);
1041 vlist_for_each_element(&wireless_devices, wdev, node)
1042 wireless_device_set_up(wdev);
1049 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1050 struct ubus_request_data *req, const char *method,
1051 struct blob_attr *msg)
1053 struct wireless_device *wdev;
1056 wdev = get_wdev(msg, &ret);
1057 if (ret == UBUS_STATUS_NOT_FOUND)
1061 wireless_device_set_down(wdev);
1063 vlist_for_each_element(&wireless_devices, wdev, node)
1064 wireless_device_set_down(wdev);
1071 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1072 struct ubus_request_data *req, const char *method,
1073 struct blob_attr *msg)
1075 struct wireless_device *wdev;
1078 wdev = get_wdev(msg, &ret);
1079 if (ret == UBUS_STATUS_NOT_FOUND)
1082 blob_buf_init(&b, 0);
1084 wireless_device_status(wdev, &b);
1086 vlist_for_each_element(&wireless_devices, wdev, node)
1087 wireless_device_status(wdev, &b);
1089 ubus_send_reply(ctx, req, b.head);
1094 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1095 struct ubus_request_data *req, const char *method,
1096 struct blob_attr *msg)
1098 struct wireless_device *wdev;
1101 wdev = get_wdev(msg, &ret);
1102 if (ret == UBUS_STATUS_NOT_FOUND)
1105 blob_buf_init(&b, 0);
1107 wireless_device_get_validate(wdev, &b);
1109 vlist_for_each_element(&wireless_devices, wdev, node)
1110 wireless_device_get_validate(wdev, &b);
1112 ubus_send_reply(ctx, req, b.head);
1117 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1118 struct ubus_request_data *req, const char *method,
1119 struct blob_attr *msg)
1121 struct wireless_device *wdev;
1124 wdev = get_wdev(msg, &ret);
1128 return wireless_device_notify(wdev, msg, req);
1131 static struct ubus_method wireless_object_methods[] = {
1132 { .name = "up", .handler = netifd_handle_wdev_up },
1133 { .name = "down", .handler = netifd_handle_wdev_down },
1134 { .name = "status", .handler = netifd_handle_wdev_status },
1135 { .name = "notify", .handler = netifd_handle_wdev_notify },
1136 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1139 static struct ubus_object_type wireless_object_type =
1140 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1143 static struct ubus_object wireless_object = {
1144 .name = "network.wireless",
1145 .type = &wireless_object_type,
1146 .methods = wireless_object_methods,
1147 .n_methods = ARRAY_SIZE(wireless_object_methods),
1151 netifd_ubus_init(const char *path)
1156 ubus_ctx = ubus_connect(path);
1160 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1161 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1162 netifd_ubus_add_fd();
1164 netifd_add_object(&main_object);
1165 netifd_add_object(&dev_object);
1166 netifd_add_object(&wireless_object);
1167 netifd_add_iface_object();
1173 netifd_ubus_done(void)
1175 ubus_free(ubus_ctx);
1179 netifd_ubus_interface_event(struct interface *iface, bool up)
1181 blob_buf_init(&b, 0);
1182 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1183 blobmsg_add_string(&b, "interface", iface->name);
1184 ubus_send_event(ubus_ctx, "network.interface", b.head);
1188 netifd_ubus_interface_notify(struct interface *iface, bool up)
1190 const char *event = (up) ? "interface.update" : "interface.down";
1191 blob_buf_init(&b, 0);
1192 blobmsg_add_string(&b, "interface", iface->name);
1193 netifd_dump_status(iface);
1194 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1195 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1199 netifd_ubus_add_interface(struct interface *iface)
1201 struct ubus_object *obj = &iface->ubus;
1204 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1208 obj->type = &iface_object_type;
1209 obj->methods = iface_object_methods;
1210 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1211 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1212 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1219 netifd_ubus_remove_interface(struct interface *iface)
1221 if (!iface->ubus.name)
1224 ubus_remove_object(ubus_ctx, &iface->ubus);
1225 free((void *) iface->ubus.name);