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 iface->device_config = true;
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 device_set_config(dev, dev->type, msg);
166 if (iface->state != IFS_SETUP && iface->state != IFS_UP)
167 vlist_delete(&interfaces, &iface->node);
169 return UBUS_STATUS_OK;
173 return UBUS_STATUS_UNKNOWN_ERROR;
176 static struct ubus_method main_object_methods[] = {
177 { .name = "restart", .handler = netifd_handle_restart },
178 { .name = "reload", .handler = netifd_handle_reload },
179 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
180 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
181 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
184 static struct ubus_object_type main_object_type =
185 UBUS_OBJECT_TYPE("netifd", main_object_methods);
187 static struct ubus_object main_object = {
189 .type = &main_object_type,
190 .methods = main_object_methods,
191 .n_methods = ARRAY_SIZE(main_object_methods),
199 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
200 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
204 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
205 struct ubus_request_data *req, const char *method,
206 struct blob_attr *msg)
208 struct device *dev = NULL;
209 struct blob_attr *tb[__DEV_MAX];
211 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
214 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
216 return UBUS_STATUS_INVALID_ARGUMENT;
219 blob_buf_init(&b, 0);
220 device_dump_status(&b, dev);
221 ubus_send_reply(ctx, req, b.head);
232 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
233 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
234 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
238 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
239 struct ubus_request_data *req, const char *method,
240 struct blob_attr *msg)
242 struct device *dev = NULL;
243 struct blob_attr *tb[__ALIAS_ATTR_MAX];
244 struct blob_attr *cur;
247 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
249 if (!tb[ALIAS_ATTR_ALIAS])
250 return UBUS_STATUS_INVALID_ARGUMENT;
252 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
253 dev = device_get(blobmsg_data(cur), true);
255 return UBUS_STATUS_NOT_FOUND;
258 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
259 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
262 if (!blobmsg_check_attr(cur, NULL))
265 alias_notify_device(blobmsg_data(cur), dev);
270 device_free_unused(dev);
271 return UBUS_STATUS_INVALID_ARGUMENT;
280 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
281 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
282 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
286 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
287 struct ubus_request_data *req, const char *method,
288 struct blob_attr *msg)
290 struct device *dev = NULL;
291 struct blob_attr *tb[__DEV_STATE_MAX];
292 struct blob_attr *cur;
294 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
296 cur = tb[DEV_STATE_NAME];
298 return UBUS_STATUS_INVALID_ARGUMENT;
300 dev = device_get(blobmsg_data(cur), false);
302 return UBUS_STATUS_NOT_FOUND;
304 cur = tb[DEV_STATE_DEFER];
306 device_set_deferred(dev, !!blobmsg_get_u8(cur));
311 static struct ubus_method dev_object_methods[] = {
312 UBUS_METHOD("status", netifd_dev_status, dev_policy),
313 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
314 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
317 static struct ubus_object_type dev_object_type =
318 UBUS_OBJECT_TYPE("device", dev_object_methods);
320 static struct ubus_object dev_object = {
321 .name = "network.device",
322 .type = &dev_object_type,
323 .methods = dev_object_methods,
324 .n_methods = ARRAY_SIZE(dev_object_methods),
328 netifd_ubus_add_fd(void)
330 ubus_add_uloop(ubus_ctx);
331 system_fd_set_cloexec(ubus_ctx->sock.fd);
335 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
337 static struct uloop_timeout retry = {
338 .cb = netifd_ubus_reconnect_timer,
342 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
343 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
344 uloop_timeout_set(&retry, t * 1000);
348 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
349 netifd_ubus_add_fd();
353 netifd_ubus_connection_lost(struct ubus_context *ctx)
355 netifd_ubus_reconnect_timer(NULL);
358 /* per-interface object */
361 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
362 struct ubus_request_data *req, const char *method,
363 struct blob_attr *msg)
365 struct interface *iface;
367 iface = container_of(obj, struct interface, ubus);
368 interface_set_up(iface);
374 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
375 struct ubus_request_data *req, const char *method,
376 struct blob_attr *msg)
378 struct interface *iface;
380 iface = container_of(obj, struct interface, ubus);
381 interface_set_down(iface);
387 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
389 struct interface_error *error;
393 e = blobmsg_open_array(b, "errors");
394 list_for_each_entry(error, &iface->errors, list) {
395 e2 = blobmsg_open_table(b, NULL);
397 blobmsg_add_string(b, "subsystem", error->subsystem);
398 blobmsg_add_string(b, "code", error->code);
399 if (error->data[0]) {
400 e3 = blobmsg_open_array(b, "data");
401 for (i = 0; error->data[i]; i++)
402 blobmsg_add_string(b, NULL, error->data[i]);
403 blobmsg_close_array(b, e3);
406 blobmsg_close_table(b, e2);
408 blobmsg_close_array(b, e);
412 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
415 struct device_addr *addr;
421 time_t now = system_get_rtime();
422 vlist_for_each_element(&ip->addr, addr, node) {
423 if (addr->enabled != enabled)
426 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
431 if (af != (v6 ? AF_INET6 : AF_INET))
434 a = blobmsg_open_table(&b, NULL);
436 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
437 inet_ntop(af, &addr->addr, buf, buflen);
438 blobmsg_add_string_buffer(&b);
440 blobmsg_add_u32(&b, "mask", addr->mask);
442 if (addr->preferred_until) {
443 int preferred = addr->preferred_until - now;
446 blobmsg_add_u32(&b, "preferred", preferred);
449 if (addr->valid_until)
450 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
453 blobmsg_add_string(&b, "class", addr->pclass);
455 blobmsg_close_table(&b, a);
460 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
462 struct device_route *route;
468 time_t now = system_get_rtime();
469 vlist_for_each_element(&ip->route, route, node) {
470 if (route->enabled != enabled)
473 if ((ip->no_defaultroute == enabled) && !route->mask)
476 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
481 r = blobmsg_open_table(&b, NULL);
483 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
484 inet_ntop(af, &route->addr, buf, buflen);
485 blobmsg_add_string_buffer(&b);
487 blobmsg_add_u32(&b, "mask", route->mask);
489 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
490 inet_ntop(af, &route->nexthop, buf, buflen);
491 blobmsg_add_string_buffer(&b);
493 if (route->flags & DEVROUTE_TYPE)
494 blobmsg_add_u32(&b, "type", route->type);
496 if (route->flags & DEVROUTE_MTU)
497 blobmsg_add_u32(&b, "mtu", route->mtu);
499 if (route->flags & DEVROUTE_METRIC)
500 blobmsg_add_u32(&b, "metric", route->metric);
502 if (route->flags & DEVROUTE_TABLE)
503 blobmsg_add_u32(&b, "table", route->table);
505 if (route->valid_until)
506 blobmsg_add_u32(&b, "valid", route->valid_until - now);
508 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
509 inet_ntop(af, &route->source, buf, buflen);
510 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
511 blobmsg_add_string_buffer(&b);
513 blobmsg_close_table(&b, r);
519 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
521 struct device_prefix *prefix;
524 const int buflen = INET6_ADDRSTRLEN;
526 time_t now = system_get_rtime();
527 vlist_for_each_element(&ip->prefix, prefix, node) {
528 a = blobmsg_open_table(&b, NULL);
530 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
531 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
532 blobmsg_add_string_buffer(&b);
534 blobmsg_add_u32(&b, "mask", prefix->length);
536 if (prefix->preferred_until) {
537 int preferred = prefix->preferred_until - now;
540 blobmsg_add_u32(&b, "preferred", preferred);
543 if (prefix->valid_until)
544 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
546 blobmsg_add_string(&b, "class", prefix->pclass);
548 c = blobmsg_open_table(&b, "assigned");
549 struct device_prefix_assignment *assign;
550 list_for_each_entry(assign, &prefix->assignments, head) {
551 if (!assign->name[0])
554 struct in6_addr addr = prefix->addr;
555 addr.s6_addr32[1] |= htonl(assign->assigned);
557 void *d = blobmsg_open_table(&b, assign->name);
559 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
560 inet_ntop(AF_INET6, &addr, buf, buflen);
561 blobmsg_add_string_buffer(&b);
563 blobmsg_add_u32(&b, "mask", assign->length);
565 blobmsg_close_table(&b, d);
567 blobmsg_close_table(&b, c);
569 blobmsg_close_table(&b, a);
575 interface_ip_dump_prefix_assignment_list(struct interface *iface)
579 const int buflen = INET6_ADDRSTRLEN;
580 time_t now = system_get_rtime();
582 struct device_prefix *prefix;
583 list_for_each_entry(prefix, &prefixes, head) {
584 struct device_prefix_assignment *assign;
585 list_for_each_entry(assign, &prefix->assignments, head) {
586 if (strcmp(assign->name, iface->name))
589 struct in6_addr addr = prefix->addr;
590 addr.s6_addr32[1] |= htonl(assign->assigned);
592 a = blobmsg_open_table(&b, NULL);
594 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
595 inet_ntop(AF_INET6, &addr, buf, buflen);
596 blobmsg_add_string_buffer(&b);
598 blobmsg_add_u32(&b, "mask", assign->length);
600 if (prefix->preferred_until) {
601 int preferred = prefix->preferred_until - now;
604 blobmsg_add_u32(&b, "preferred", preferred);
607 if (prefix->valid_until)
608 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
610 blobmsg_close_table(&b, a);
617 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
620 struct dns_server *dns;
624 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
625 if (ip->no_dns == enabled)
628 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
629 inet_ntop(dns->af, &dns->addr, buf, buflen);
630 blobmsg_add_string_buffer(&b);
635 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
638 struct dns_search_domain *dns;
640 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
641 if (ip->no_dns == enabled)
644 blobmsg_add_string(&b, NULL, dns->name);
649 netifd_dump_status(struct interface *iface)
651 struct interface_data *data;
655 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
656 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
657 blobmsg_add_u8(&b, "available", iface->available);
658 blobmsg_add_u8(&b, "autostart", iface->autostart);
660 if (iface->state == IFS_UP) {
661 time_t cur = system_get_rtime();
662 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
663 if (iface->l3_dev.dev)
664 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
667 if (iface->proto_handler)
668 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
670 dev = iface->main_dev.dev;
671 if (dev && !dev->hidden &&
672 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
673 blobmsg_add_string(&b, "device", dev->ifname);
675 if (iface->state == IFS_UP) {
676 if (iface->updated) {
677 a = blobmsg_open_array(&b, "updated");
679 if (iface->updated & IUF_ADDRESS)
680 blobmsg_add_string(&b, NULL, "addresses");
681 if (iface->updated & IUF_ROUTE)
682 blobmsg_add_string(&b, NULL, "routes");
683 if (iface->updated & IUF_PREFIX)
684 blobmsg_add_string(&b, NULL, "prefixes");
685 if (iface->updated & IUF_DATA)
686 blobmsg_add_string(&b, NULL, "data");
688 blobmsg_close_array(&b, a);
692 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
694 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
695 blobmsg_add_u32(&b, "metric", iface->metric);
696 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
697 a = blobmsg_open_array(&b, "ipv4-address");
698 interface_ip_dump_address_list(&iface->config_ip, false, true);
699 interface_ip_dump_address_list(&iface->proto_ip, false, true);
700 blobmsg_close_array(&b, a);
701 a = blobmsg_open_array(&b, "ipv6-address");
702 interface_ip_dump_address_list(&iface->config_ip, true, true);
703 interface_ip_dump_address_list(&iface->proto_ip, true, true);
704 blobmsg_close_array(&b, a);
705 a = blobmsg_open_array(&b, "ipv6-prefix");
706 interface_ip_dump_prefix_list(&iface->config_ip);
707 interface_ip_dump_prefix_list(&iface->proto_ip);
708 blobmsg_close_array(&b, a);
709 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
710 interface_ip_dump_prefix_assignment_list(iface);
711 blobmsg_close_array(&b, a);
712 a = blobmsg_open_array(&b, "route");
713 interface_ip_dump_route_list(&iface->config_ip, true);
714 interface_ip_dump_route_list(&iface->proto_ip, true);
715 blobmsg_close_array(&b, a);
716 a = blobmsg_open_array(&b, "dns-server");
717 interface_ip_dump_dns_server_list(&iface->config_ip, true);
718 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
719 blobmsg_close_array(&b, a);
720 a = blobmsg_open_array(&b, "dns-search");
721 interface_ip_dump_dns_search_list(&iface->config_ip, true);
722 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
723 blobmsg_close_array(&b, a);
725 inactive = blobmsg_open_table(&b, "inactive");
726 a = blobmsg_open_array(&b, "ipv4-address");
727 interface_ip_dump_address_list(&iface->config_ip, false, false);
728 interface_ip_dump_address_list(&iface->proto_ip, false, false);
729 blobmsg_close_array(&b, a);
730 a = blobmsg_open_array(&b, "ipv6-address");
731 interface_ip_dump_address_list(&iface->config_ip, true, false);
732 interface_ip_dump_address_list(&iface->proto_ip, true, false);
733 blobmsg_close_array(&b, a);
734 a = blobmsg_open_array(&b, "route");
735 interface_ip_dump_route_list(&iface->config_ip, false);
736 interface_ip_dump_route_list(&iface->proto_ip, false);
737 blobmsg_close_array(&b, a);
738 a = blobmsg_open_array(&b, "dns-server");
739 interface_ip_dump_dns_server_list(&iface->config_ip, false);
740 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
741 blobmsg_close_array(&b, a);
742 a = blobmsg_open_array(&b, "dns-search");
743 interface_ip_dump_dns_search_list(&iface->config_ip, false);
744 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
745 blobmsg_close_array(&b, a);
746 blobmsg_close_table(&b, inactive);
749 a = blobmsg_open_table(&b, "data");
750 avl_for_each_element(&iface->data, data, node)
751 blobmsg_add_blob(&b, data->data);
753 blobmsg_close_table(&b, a);
755 if (!list_empty(&iface->errors))
756 netifd_add_interface_errors(&b, iface);
760 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
761 struct ubus_request_data *req, const char *method,
762 struct blob_attr *msg)
764 struct interface *iface = container_of(obj, struct interface, ubus);
766 blob_buf_init(&b, 0);
767 netifd_dump_status(iface);
768 ubus_send_reply(ctx, req, b.head);
775 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
776 struct ubus_request_data *req, const char *method,
777 struct blob_attr *msg)
779 blob_buf_init(&b, 0);
780 void *a = blobmsg_open_array(&b, "interface");
782 struct interface *iface;
783 vlist_for_each_element(&interfaces, iface, node) {
784 void *i = blobmsg_open_table(&b, NULL);
785 blobmsg_add_string(&b, "interface", iface->name);
786 netifd_dump_status(iface);
787 blobmsg_close_table(&b, i);
790 blobmsg_close_array(&b, a);
791 ubus_send_reply(ctx, req, b.head);
797 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
798 struct ubus_request_data *req, const char *method,
799 struct blob_attr *msg)
801 struct blob_attr *tb[__DEV_MAX];
802 struct interface *iface;
803 bool add = !strncmp(method, "add", 3);
805 iface = container_of(obj, struct interface, ubus);
807 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
810 return UBUS_STATUS_INVALID_ARGUMENT;
812 return interface_handle_link(iface, blobmsg_data(tb[DEV_NAME]), add);
817 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
818 struct ubus_request_data *req, const char *method,
819 struct blob_attr *msg)
821 struct interface *iface;
823 iface = container_of(obj, struct interface, ubus);
825 if (!iface->proto || !iface->proto->notify)
826 return UBUS_STATUS_NOT_SUPPORTED;
828 return iface->proto->notify(iface->proto, msg);
832 netifd_iface_do_remove(struct uloop_timeout *timeout)
834 struct interface *iface;
836 iface = container_of(timeout, struct interface, remove_timer);
837 vlist_delete(&interfaces, &iface->node);
841 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
842 struct ubus_request_data *req, const char *method,
843 struct blob_attr *msg)
845 struct interface *iface;
847 iface = container_of(obj, struct interface, ubus);
848 if (iface->remove_timer.cb)
849 return UBUS_STATUS_INVALID_ARGUMENT;
851 iface->remove_timer.cb = netifd_iface_do_remove;
852 uloop_timeout_set(&iface->remove_timer, 100);
857 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
858 struct ubus_request_data *req, const char *method,
859 struct blob_attr *msg)
861 struct interface *iface;
863 const struct device_hotplug_ops *ops;
865 iface = container_of(obj, struct interface, ubus);
866 dev = iface->main_dev.dev;
870 ops = dev->hotplug_ops;
874 return ops->prepare(dev);
878 netifd_handle_set_data(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;
883 struct blob_attr *cur;
886 iface = container_of(obj, struct interface, ubus);
888 blob_for_each_attr(cur, msg, rem) {
889 ret = interface_add_data(iface, cur);
897 static struct ubus_method iface_object_methods[] = {
898 { .name = "up", .handler = netifd_handle_up },
899 { .name = "down", .handler = netifd_handle_down },
900 { .name = "status", .handler = netifd_handle_status },
901 { .name = "prepare", .handler = netifd_handle_iface_prepare },
902 { .name = "dump", .handler = netifd_handle_dump },
903 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
904 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
905 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
906 { .name = "remove", .handler = netifd_iface_remove },
907 { .name = "set_data", .handler = netifd_handle_set_data },
910 static struct ubus_object_type iface_object_type =
911 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
914 static struct ubus_object iface_object = {
915 .name = "network.interface",
916 .type = &iface_object_type,
917 .n_methods = ARRAY_SIZE(iface_object_methods),
920 static void netifd_add_object(struct ubus_object *obj)
922 int ret = ubus_add_object(ubus_ctx, obj);
925 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
928 static const struct blobmsg_policy iface_policy = {
930 .type = BLOBMSG_TYPE_STRING,
934 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
935 struct ubus_request_data *req, const char *method,
936 struct blob_attr *msg)
938 struct interface *iface;
939 struct blob_attr *tb;
942 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
944 return UBUS_STATUS_INVALID_ARGUMENT;
946 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
948 return UBUS_STATUS_NOT_FOUND;
950 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
953 if (strcmp(method, iface_object_methods[i].name) != 0)
956 cb = iface_object_methods[i].handler;
957 return cb(ctx, &iface->ubus, req, method, msg);
960 return UBUS_STATUS_INVALID_ARGUMENT;
963 static void netifd_add_iface_object(void)
965 struct ubus_method *methods;
968 methods = calloc(1, sizeof(iface_object_methods));
969 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
970 iface_object.methods = methods;
972 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
973 if (methods[i].handler == netifd_handle_dump)
976 methods[i].handler = netifd_handle_iface;
977 methods[i].policy = &iface_policy;
978 methods[i].n_policy = 1;
980 netifd_add_object(&iface_object);
983 static struct wireless_device *
984 get_wdev(struct blob_attr *msg, int *ret)
986 struct blobmsg_policy wdev_policy = {
988 .type = BLOBMSG_TYPE_STRING,
990 struct blob_attr *dev_attr;
991 struct wireless_device *wdev = NULL;
994 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
996 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1000 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1002 *ret = UBUS_STATUS_NOT_FOUND;
1011 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1012 struct ubus_request_data *req, const char *method,
1013 struct blob_attr *msg)
1015 struct wireless_device *wdev;
1018 wdev = get_wdev(msg, &ret);
1019 if (ret == UBUS_STATUS_NOT_FOUND)
1023 wireless_device_set_up(wdev);
1025 vlist_for_each_element(&wireless_devices, wdev, node)
1026 wireless_device_set_up(wdev);
1033 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1034 struct ubus_request_data *req, const char *method,
1035 struct blob_attr *msg)
1037 struct wireless_device *wdev;
1040 wdev = get_wdev(msg, &ret);
1041 if (ret == UBUS_STATUS_NOT_FOUND)
1045 wireless_device_set_down(wdev);
1047 vlist_for_each_element(&wireless_devices, wdev, node)
1048 wireless_device_set_down(wdev);
1055 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1056 struct ubus_request_data *req, const char *method,
1057 struct blob_attr *msg)
1059 struct wireless_device *wdev;
1062 wdev = get_wdev(msg, &ret);
1063 if (ret == UBUS_STATUS_NOT_FOUND)
1066 blob_buf_init(&b, 0);
1068 wireless_device_status(wdev, &b);
1070 vlist_for_each_element(&wireless_devices, wdev, node)
1071 wireless_device_status(wdev, &b);
1073 ubus_send_reply(ctx, req, b.head);
1078 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1079 struct ubus_request_data *req, const char *method,
1080 struct blob_attr *msg)
1082 struct wireless_device *wdev;
1085 wdev = get_wdev(msg, &ret);
1086 if (ret == UBUS_STATUS_NOT_FOUND)
1089 blob_buf_init(&b, 0);
1091 wireless_device_get_validate(wdev, &b);
1093 vlist_for_each_element(&wireless_devices, wdev, node)
1094 wireless_device_get_validate(wdev, &b);
1096 ubus_send_reply(ctx, req, b.head);
1101 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1102 struct ubus_request_data *req, const char *method,
1103 struct blob_attr *msg)
1105 struct wireless_device *wdev;
1108 wdev = get_wdev(msg, &ret);
1112 return wireless_device_notify(wdev, msg, req);
1115 static struct ubus_method wireless_object_methods[] = {
1116 { .name = "up", .handler = netifd_handle_wdev_up },
1117 { .name = "down", .handler = netifd_handle_wdev_down },
1118 { .name = "status", .handler = netifd_handle_wdev_status },
1119 { .name = "notify", .handler = netifd_handle_wdev_notify },
1120 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1123 static struct ubus_object_type wireless_object_type =
1124 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1127 static struct ubus_object wireless_object = {
1128 .name = "network.wireless",
1129 .type = &wireless_object_type,
1130 .methods = wireless_object_methods,
1131 .n_methods = ARRAY_SIZE(wireless_object_methods),
1135 netifd_ubus_init(const char *path)
1140 ubus_ctx = ubus_connect(path);
1144 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1145 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1146 netifd_ubus_add_fd();
1148 netifd_add_object(&main_object);
1149 netifd_add_object(&dev_object);
1150 netifd_add_object(&wireless_object);
1151 netifd_add_iface_object();
1157 netifd_ubus_done(void)
1159 ubus_free(ubus_ctx);
1163 netifd_ubus_interface_event(struct interface *iface, bool up)
1165 blob_buf_init(&b, 0);
1166 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1167 blobmsg_add_string(&b, "interface", iface->name);
1168 ubus_send_event(ubus_ctx, "network.interface", b.head);
1172 netifd_ubus_interface_notify(struct interface *iface, bool up)
1174 const char *event = (up) ? "interface.update" : "interface.down";
1175 blob_buf_init(&b, 0);
1176 blobmsg_add_string(&b, "interface", iface->name);
1177 netifd_dump_status(iface);
1178 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1179 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1183 netifd_ubus_add_interface(struct interface *iface)
1185 struct ubus_object *obj = &iface->ubus;
1188 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1192 obj->type = &iface_object_type;
1193 obj->methods = iface_object_methods;
1194 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1195 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1196 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1203 netifd_ubus_remove_interface(struct interface *iface)
1205 if (!iface->ubus.name)
1208 ubus_remove_object(ubus_ctx, &iface->ubus);
1209 free((void *) iface->ubus.name);