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);
165 return UBUS_STATUS_OK;
169 return UBUS_STATUS_UNKNOWN_ERROR;
172 static struct ubus_method main_object_methods[] = {
173 { .name = "restart", .handler = netifd_handle_restart },
174 { .name = "reload", .handler = netifd_handle_reload },
175 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
176 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
177 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
180 static struct ubus_object_type main_object_type =
181 UBUS_OBJECT_TYPE("netifd", main_object_methods);
183 static struct ubus_object main_object = {
185 .type = &main_object_type,
186 .methods = main_object_methods,
187 .n_methods = ARRAY_SIZE(main_object_methods),
195 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
196 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
200 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
201 struct ubus_request_data *req, const char *method,
202 struct blob_attr *msg)
204 struct device *dev = NULL;
205 struct blob_attr *tb[__DEV_MAX];
207 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
210 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
212 return UBUS_STATUS_INVALID_ARGUMENT;
215 blob_buf_init(&b, 0);
216 device_dump_status(&b, dev);
217 ubus_send_reply(ctx, req, b.head);
228 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
229 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
230 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
234 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
235 struct ubus_request_data *req, const char *method,
236 struct blob_attr *msg)
238 struct device *dev = NULL;
239 struct blob_attr *tb[__ALIAS_ATTR_MAX];
240 struct blob_attr *cur;
243 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
245 if (!tb[ALIAS_ATTR_ALIAS])
246 return UBUS_STATUS_INVALID_ARGUMENT;
248 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
249 dev = device_get(blobmsg_data(cur), true);
251 return UBUS_STATUS_NOT_FOUND;
254 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
255 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
258 if (!blobmsg_check_attr(cur, NULL))
261 alias_notify_device(blobmsg_data(cur), dev);
266 device_free_unused(dev);
267 return UBUS_STATUS_INVALID_ARGUMENT;
276 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
277 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
278 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
282 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
283 struct ubus_request_data *req, const char *method,
284 struct blob_attr *msg)
286 struct device *dev = NULL;
287 struct blob_attr *tb[__DEV_STATE_MAX];
288 struct blob_attr *cur;
290 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
292 cur = tb[DEV_STATE_NAME];
294 return UBUS_STATUS_INVALID_ARGUMENT;
296 dev = device_get(blobmsg_data(cur), false);
298 return UBUS_STATUS_NOT_FOUND;
300 cur = tb[DEV_STATE_DEFER];
302 device_set_deferred(dev, !!blobmsg_get_u8(cur));
307 static struct ubus_method dev_object_methods[] = {
308 UBUS_METHOD("status", netifd_dev_status, dev_policy),
309 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
310 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
313 static struct ubus_object_type dev_object_type =
314 UBUS_OBJECT_TYPE("device", dev_object_methods);
316 static struct ubus_object dev_object = {
317 .name = "network.device",
318 .type = &dev_object_type,
319 .methods = dev_object_methods,
320 .n_methods = ARRAY_SIZE(dev_object_methods),
324 netifd_ubus_add_fd(void)
326 ubus_add_uloop(ubus_ctx);
327 system_fd_set_cloexec(ubus_ctx->sock.fd);
331 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
333 static struct uloop_timeout retry = {
334 .cb = netifd_ubus_reconnect_timer,
338 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
339 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
340 uloop_timeout_set(&retry, t * 1000);
344 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
345 netifd_ubus_add_fd();
349 netifd_ubus_connection_lost(struct ubus_context *ctx)
351 netifd_ubus_reconnect_timer(NULL);
354 /* per-interface object */
357 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
358 struct ubus_request_data *req, const char *method,
359 struct blob_attr *msg)
361 struct interface *iface;
363 iface = container_of(obj, struct interface, ubus);
364 interface_set_up(iface);
370 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
371 struct ubus_request_data *req, const char *method,
372 struct blob_attr *msg)
374 struct interface *iface;
376 iface = container_of(obj, struct interface, ubus);
377 interface_set_down(iface);
383 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
385 struct interface_error *error;
389 e = blobmsg_open_array(b, "errors");
390 list_for_each_entry(error, &iface->errors, list) {
391 e2 = blobmsg_open_table(b, NULL);
393 blobmsg_add_string(b, "subsystem", error->subsystem);
394 blobmsg_add_string(b, "code", error->code);
395 if (error->data[0]) {
396 e3 = blobmsg_open_array(b, "data");
397 for (i = 0; error->data[i]; i++)
398 blobmsg_add_string(b, NULL, error->data[i]);
399 blobmsg_close_array(b, e3);
402 blobmsg_close_table(b, e2);
404 blobmsg_close_array(b, e);
408 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
411 struct device_addr *addr;
417 time_t now = system_get_rtime();
418 vlist_for_each_element(&ip->addr, addr, node) {
419 if (addr->enabled != enabled)
422 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
427 if (af != (v6 ? AF_INET6 : AF_INET))
430 a = blobmsg_open_table(&b, NULL);
432 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
433 inet_ntop(af, &addr->addr, buf, buflen);
434 blobmsg_add_string_buffer(&b);
436 blobmsg_add_u32(&b, "mask", addr->mask);
438 if (addr->preferred_until) {
439 int preferred = addr->preferred_until - now;
442 blobmsg_add_u32(&b, "preferred", preferred);
445 if (addr->valid_until)
446 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
449 blobmsg_add_string(&b, "class", addr->pclass);
451 blobmsg_close_table(&b, a);
456 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
458 struct device_route *route;
464 time_t now = system_get_rtime();
465 vlist_for_each_element(&ip->route, route, node) {
466 if (route->enabled != enabled)
469 if ((ip->no_defaultroute == enabled) && !route->mask)
472 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
477 r = blobmsg_open_table(&b, NULL);
479 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
480 inet_ntop(af, &route->addr, buf, buflen);
481 blobmsg_add_string_buffer(&b);
483 blobmsg_add_u32(&b, "mask", route->mask);
485 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
486 inet_ntop(af, &route->nexthop, buf, buflen);
487 blobmsg_add_string_buffer(&b);
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 blobmsg_close_table(&b, a);
610 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
613 struct dns_server *dns;
617 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
618 if (ip->no_dns == enabled)
621 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
622 inet_ntop(dns->af, &dns->addr, buf, buflen);
623 blobmsg_add_string_buffer(&b);
628 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
631 struct dns_search_domain *dns;
633 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
634 if (ip->no_dns == enabled)
637 blobmsg_add_string(&b, NULL, dns->name);
642 netifd_dump_status(struct interface *iface)
644 struct interface_data *data;
648 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
649 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
650 blobmsg_add_u8(&b, "available", iface->available);
651 blobmsg_add_u8(&b, "autostart", iface->autostart);
653 if (iface->state == IFS_UP) {
654 time_t cur = system_get_rtime();
655 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
656 if (iface->l3_dev.dev)
657 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
660 if (iface->proto_handler)
661 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
663 dev = iface->main_dev.dev;
664 if (dev && !dev->hidden &&
665 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
666 blobmsg_add_string(&b, "device", dev->ifname);
668 if (iface->state == IFS_UP) {
669 if (iface->updated) {
670 a = blobmsg_open_array(&b, "updated");
672 if (iface->updated & IUF_ADDRESS)
673 blobmsg_add_string(&b, NULL, "addresses");
674 if (iface->updated & IUF_ROUTE)
675 blobmsg_add_string(&b, NULL, "routes");
676 if (iface->updated & IUF_PREFIX)
677 blobmsg_add_string(&b, NULL, "prefixes");
678 if (iface->updated & IUF_DATA)
679 blobmsg_add_string(&b, NULL, "data");
681 blobmsg_close_array(&b, a);
685 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
687 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
688 blobmsg_add_u32(&b, "metric", iface->metric);
689 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
690 a = blobmsg_open_array(&b, "ipv4-address");
691 interface_ip_dump_address_list(&iface->config_ip, false, true);
692 interface_ip_dump_address_list(&iface->proto_ip, false, true);
693 blobmsg_close_array(&b, a);
694 a = blobmsg_open_array(&b, "ipv6-address");
695 interface_ip_dump_address_list(&iface->config_ip, true, true);
696 interface_ip_dump_address_list(&iface->proto_ip, true, true);
697 blobmsg_close_array(&b, a);
698 a = blobmsg_open_array(&b, "ipv6-prefix");
699 interface_ip_dump_prefix_list(&iface->config_ip);
700 interface_ip_dump_prefix_list(&iface->proto_ip);
701 blobmsg_close_array(&b, a);
702 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
703 interface_ip_dump_prefix_assignment_list(iface);
704 blobmsg_close_array(&b, a);
705 a = blobmsg_open_array(&b, "route");
706 interface_ip_dump_route_list(&iface->config_ip, true);
707 interface_ip_dump_route_list(&iface->proto_ip, true);
708 blobmsg_close_array(&b, a);
709 a = blobmsg_open_array(&b, "dns-server");
710 interface_ip_dump_dns_server_list(&iface->config_ip, true);
711 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
712 blobmsg_close_array(&b, a);
713 a = blobmsg_open_array(&b, "dns-search");
714 interface_ip_dump_dns_search_list(&iface->config_ip, true);
715 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
716 blobmsg_close_array(&b, a);
718 inactive = blobmsg_open_table(&b, "inactive");
719 a = blobmsg_open_array(&b, "ipv4-address");
720 interface_ip_dump_address_list(&iface->config_ip, false, false);
721 interface_ip_dump_address_list(&iface->proto_ip, false, false);
722 blobmsg_close_array(&b, a);
723 a = blobmsg_open_array(&b, "ipv6-address");
724 interface_ip_dump_address_list(&iface->config_ip, true, false);
725 interface_ip_dump_address_list(&iface->proto_ip, true, false);
726 blobmsg_close_array(&b, a);
727 a = blobmsg_open_array(&b, "route");
728 interface_ip_dump_route_list(&iface->config_ip, false);
729 interface_ip_dump_route_list(&iface->proto_ip, false);
730 blobmsg_close_array(&b, a);
731 a = blobmsg_open_array(&b, "dns-server");
732 interface_ip_dump_dns_server_list(&iface->config_ip, false);
733 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
734 blobmsg_close_array(&b, a);
735 a = blobmsg_open_array(&b, "dns-search");
736 interface_ip_dump_dns_search_list(&iface->config_ip, false);
737 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
738 blobmsg_close_array(&b, a);
739 blobmsg_close_table(&b, inactive);
742 a = blobmsg_open_table(&b, "data");
743 avl_for_each_element(&iface->data, data, node)
744 blobmsg_add_blob(&b, data->data);
746 blobmsg_close_table(&b, a);
748 if (!list_empty(&iface->errors))
749 netifd_add_interface_errors(&b, iface);
753 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
754 struct ubus_request_data *req, const char *method,
755 struct blob_attr *msg)
757 struct interface *iface = container_of(obj, struct interface, ubus);
759 blob_buf_init(&b, 0);
760 netifd_dump_status(iface);
761 ubus_send_reply(ctx, req, b.head);
768 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
769 struct ubus_request_data *req, const char *method,
770 struct blob_attr *msg)
772 blob_buf_init(&b, 0);
773 void *a = blobmsg_open_array(&b, "interface");
775 struct interface *iface;
776 vlist_for_each_element(&interfaces, iface, node) {
777 void *i = blobmsg_open_table(&b, NULL);
778 blobmsg_add_string(&b, "interface", iface->name);
779 netifd_dump_status(iface);
780 blobmsg_close_table(&b, i);
783 blobmsg_close_array(&b, a);
784 ubus_send_reply(ctx, req, b.head);
790 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
791 struct ubus_request_data *req, const char *method,
792 struct blob_attr *msg)
794 struct blob_attr *tb[__DEV_MAX];
795 struct interface *iface;
796 bool add = !strncmp(method, "add", 3);
798 iface = container_of(obj, struct interface, ubus);
800 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
803 return UBUS_STATUS_INVALID_ARGUMENT;
805 return interface_handle_link(iface, blobmsg_data(tb[DEV_NAME]), add);
810 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
811 struct ubus_request_data *req, const char *method,
812 struct blob_attr *msg)
814 struct interface *iface;
816 iface = container_of(obj, struct interface, ubus);
818 if (!iface->proto || !iface->proto->notify)
819 return UBUS_STATUS_NOT_SUPPORTED;
821 return iface->proto->notify(iface->proto, msg);
825 netifd_iface_do_remove(struct uloop_timeout *timeout)
827 struct interface *iface;
829 iface = container_of(timeout, struct interface, remove_timer);
830 vlist_delete(&interfaces, &iface->node);
834 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
835 struct ubus_request_data *req, const char *method,
836 struct blob_attr *msg)
838 struct interface *iface;
840 iface = container_of(obj, struct interface, ubus);
841 if (iface->remove_timer.cb)
842 return UBUS_STATUS_INVALID_ARGUMENT;
844 iface->remove_timer.cb = netifd_iface_do_remove;
845 uloop_timeout_set(&iface->remove_timer, 100);
850 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
851 struct ubus_request_data *req, const char *method,
852 struct blob_attr *msg)
854 struct interface *iface;
856 const struct device_hotplug_ops *ops;
858 iface = container_of(obj, struct interface, ubus);
859 dev = iface->main_dev.dev;
863 ops = dev->hotplug_ops;
867 return ops->prepare(dev);
871 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
872 struct ubus_request_data *req, const char *method,
873 struct blob_attr *msg)
875 struct interface *iface;
876 struct blob_attr *cur;
879 iface = container_of(obj, struct interface, ubus);
881 blob_for_each_attr(cur, msg, rem) {
882 ret = interface_add_data(iface, cur);
890 static struct ubus_method iface_object_methods[] = {
891 { .name = "up", .handler = netifd_handle_up },
892 { .name = "down", .handler = netifd_handle_down },
893 { .name = "status", .handler = netifd_handle_status },
894 { .name = "prepare", .handler = netifd_handle_iface_prepare },
895 { .name = "dump", .handler = netifd_handle_dump },
896 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
897 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
898 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
899 { .name = "remove", .handler = netifd_iface_remove },
900 { .name = "set_data", .handler = netifd_handle_set_data },
903 static struct ubus_object_type iface_object_type =
904 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
907 static struct ubus_object iface_object = {
908 .name = "network.interface",
909 .type = &iface_object_type,
910 .n_methods = ARRAY_SIZE(iface_object_methods),
913 static void netifd_add_object(struct ubus_object *obj)
915 int ret = ubus_add_object(ubus_ctx, obj);
918 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
921 static const struct blobmsg_policy iface_policy = {
923 .type = BLOBMSG_TYPE_STRING,
927 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
928 struct ubus_request_data *req, const char *method,
929 struct blob_attr *msg)
931 struct interface *iface;
932 struct blob_attr *tb;
935 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
937 return UBUS_STATUS_INVALID_ARGUMENT;
939 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
941 return UBUS_STATUS_NOT_FOUND;
943 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
946 if (strcmp(method, iface_object_methods[i].name) != 0)
949 cb = iface_object_methods[i].handler;
950 return cb(ctx, &iface->ubus, req, method, msg);
953 return UBUS_STATUS_INVALID_ARGUMENT;
956 static void netifd_add_iface_object(void)
958 struct ubus_method *methods;
961 methods = calloc(1, sizeof(iface_object_methods));
962 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
963 iface_object.methods = methods;
965 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
966 if (methods[i].handler == netifd_handle_dump)
969 methods[i].handler = netifd_handle_iface;
970 methods[i].policy = &iface_policy;
971 methods[i].n_policy = 1;
973 netifd_add_object(&iface_object);
976 static struct wireless_device *
977 get_wdev(struct blob_attr *msg, int *ret)
979 struct blobmsg_policy wdev_policy = {
981 .type = BLOBMSG_TYPE_STRING,
983 struct blob_attr *dev_attr;
984 struct wireless_device *wdev = NULL;
987 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
989 *ret = UBUS_STATUS_INVALID_ARGUMENT;
993 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
995 *ret = UBUS_STATUS_NOT_FOUND;
1004 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1005 struct ubus_request_data *req, const char *method,
1006 struct blob_attr *msg)
1008 struct wireless_device *wdev;
1011 wdev = get_wdev(msg, &ret);
1012 if (ret == UBUS_STATUS_NOT_FOUND)
1016 wireless_device_set_up(wdev);
1018 vlist_for_each_element(&wireless_devices, wdev, node)
1019 wireless_device_set_up(wdev);
1026 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1027 struct ubus_request_data *req, const char *method,
1028 struct blob_attr *msg)
1030 struct wireless_device *wdev;
1033 wdev = get_wdev(msg, &ret);
1034 if (ret == UBUS_STATUS_NOT_FOUND)
1038 wireless_device_set_down(wdev);
1040 vlist_for_each_element(&wireless_devices, wdev, node)
1041 wireless_device_set_down(wdev);
1048 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1049 struct ubus_request_data *req, const char *method,
1050 struct blob_attr *msg)
1052 struct wireless_device *wdev;
1055 wdev = get_wdev(msg, &ret);
1056 if (ret == UBUS_STATUS_NOT_FOUND)
1059 blob_buf_init(&b, 0);
1061 wireless_device_status(wdev, &b);
1063 vlist_for_each_element(&wireless_devices, wdev, node)
1064 wireless_device_status(wdev, &b);
1066 ubus_send_reply(ctx, req, b.head);
1071 netifd_handle_wdev_get_validate(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_get_validate(wdev, &b);
1086 vlist_for_each_element(&wireless_devices, wdev, node)
1087 wireless_device_get_validate(wdev, &b);
1089 ubus_send_reply(ctx, req, b.head);
1094 netifd_handle_wdev_notify(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);
1105 return wireless_device_notify(wdev, msg, req);
1108 static struct ubus_method wireless_object_methods[] = {
1109 { .name = "up", .handler = netifd_handle_wdev_up },
1110 { .name = "down", .handler = netifd_handle_wdev_down },
1111 { .name = "status", .handler = netifd_handle_wdev_status },
1112 { .name = "notify", .handler = netifd_handle_wdev_notify },
1113 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1116 static struct ubus_object_type wireless_object_type =
1117 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1120 static struct ubus_object wireless_object = {
1121 .name = "network.wireless",
1122 .type = &wireless_object_type,
1123 .methods = wireless_object_methods,
1124 .n_methods = ARRAY_SIZE(wireless_object_methods),
1128 netifd_ubus_init(const char *path)
1133 ubus_ctx = ubus_connect(path);
1137 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1138 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1139 netifd_ubus_add_fd();
1141 netifd_add_object(&main_object);
1142 netifd_add_object(&dev_object);
1143 netifd_add_object(&wireless_object);
1144 netifd_add_iface_object();
1150 netifd_ubus_done(void)
1152 ubus_free(ubus_ctx);
1156 netifd_ubus_interface_event(struct interface *iface, bool up)
1158 blob_buf_init(&b, 0);
1159 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1160 blobmsg_add_string(&b, "interface", iface->name);
1161 ubus_send_event(ubus_ctx, "network.interface", b.head);
1165 netifd_ubus_interface_notify(struct interface *iface, bool up)
1167 const char *event = (up) ? "update" : "down";
1168 blob_buf_init(&b, 0);
1169 blobmsg_add_string(&b, "interface", iface->name);
1170 netifd_dump_status(iface);
1171 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1172 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1176 netifd_ubus_add_interface(struct interface *iface)
1178 struct ubus_object *obj = &iface->ubus;
1181 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1185 obj->type = &iface_object_type;
1186 obj->methods = iface_object_methods;
1187 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1188 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1189 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1196 netifd_ubus_remove_interface(struct interface *iface)
1198 if (!iface->ubus.name)
1201 ubus_remove_object(ubus_ctx, &iface->ubus);
1202 free((void *) iface->ubus.name);