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;
177 netifd_del_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
178 struct ubus_request_data *req, const char *method,
179 struct blob_attr *msg)
181 struct blob_attr *tb[__DI_MAX];
182 struct interface *iface;
184 blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
187 return UBUS_STATUS_INVALID_ARGUMENT;
189 const char *name = blobmsg_get_string(tb[DI_NAME]);
190 iface = vlist_find(&interfaces, name, iface, node);
193 return UBUS_STATUS_NOT_FOUND;
194 else if (!iface->dynamic)
195 return UBUS_STATUS_INVALID_COMMAND;
197 vlist_delete(&interfaces, &iface->node);
198 return UBUS_STATUS_OK;
201 static struct ubus_method main_object_methods[] = {
202 { .name = "restart", .handler = netifd_handle_restart },
203 { .name = "reload", .handler = netifd_handle_reload },
204 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
205 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
206 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
207 UBUS_METHOD("del_dynamic", netifd_del_dynamic, dynamic_policy),
210 static struct ubus_object_type main_object_type =
211 UBUS_OBJECT_TYPE("netifd", main_object_methods);
213 static struct ubus_object main_object = {
215 .type = &main_object_type,
216 .methods = main_object_methods,
217 .n_methods = ARRAY_SIZE(main_object_methods),
225 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
226 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
230 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
231 struct ubus_request_data *req, const char *method,
232 struct blob_attr *msg)
234 struct device *dev = NULL;
235 struct blob_attr *tb[__DEV_MAX];
237 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
240 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
242 return UBUS_STATUS_INVALID_ARGUMENT;
245 blob_buf_init(&b, 0);
246 device_dump_status(&b, dev);
247 ubus_send_reply(ctx, req, b.head);
258 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
259 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
260 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
264 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
265 struct ubus_request_data *req, const char *method,
266 struct blob_attr *msg)
268 struct device *dev = NULL;
269 struct blob_attr *tb[__ALIAS_ATTR_MAX];
270 struct blob_attr *cur;
273 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
275 if (!tb[ALIAS_ATTR_ALIAS])
276 return UBUS_STATUS_INVALID_ARGUMENT;
278 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
279 dev = device_get(blobmsg_data(cur), true);
281 return UBUS_STATUS_NOT_FOUND;
284 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
285 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
288 if (!blobmsg_check_attr(cur, NULL))
291 alias_notify_device(blobmsg_data(cur), dev);
296 device_free_unused(dev);
297 return UBUS_STATUS_INVALID_ARGUMENT;
306 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
307 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
308 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
312 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
313 struct ubus_request_data *req, const char *method,
314 struct blob_attr *msg)
316 struct device *dev = NULL;
317 struct blob_attr *tb[__DEV_STATE_MAX];
318 struct blob_attr *cur;
320 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
322 cur = tb[DEV_STATE_NAME];
324 return UBUS_STATUS_INVALID_ARGUMENT;
326 dev = device_get(blobmsg_data(cur), false);
328 return UBUS_STATUS_NOT_FOUND;
330 cur = tb[DEV_STATE_DEFER];
332 device_set_deferred(dev, !!blobmsg_get_u8(cur));
337 static struct ubus_method dev_object_methods[] = {
338 UBUS_METHOD("status", netifd_dev_status, dev_policy),
339 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
340 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
343 static struct ubus_object_type dev_object_type =
344 UBUS_OBJECT_TYPE("device", dev_object_methods);
346 static struct ubus_object dev_object = {
347 .name = "network.device",
348 .type = &dev_object_type,
349 .methods = dev_object_methods,
350 .n_methods = ARRAY_SIZE(dev_object_methods),
354 netifd_ubus_add_fd(void)
356 ubus_add_uloop(ubus_ctx);
357 system_fd_set_cloexec(ubus_ctx->sock.fd);
361 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
363 static struct uloop_timeout retry = {
364 .cb = netifd_ubus_reconnect_timer,
368 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
369 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
370 uloop_timeout_set(&retry, t * 1000);
374 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
375 netifd_ubus_add_fd();
379 netifd_ubus_connection_lost(struct ubus_context *ctx)
381 netifd_ubus_reconnect_timer(NULL);
384 /* per-interface object */
387 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
388 struct ubus_request_data *req, const char *method,
389 struct blob_attr *msg)
391 struct interface *iface;
393 iface = container_of(obj, struct interface, ubus);
394 interface_set_up(iface);
400 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
401 struct ubus_request_data *req, const char *method,
402 struct blob_attr *msg)
404 struct interface *iface;
406 iface = container_of(obj, struct interface, ubus);
407 interface_set_down(iface);
413 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
415 struct interface_error *error;
419 e = blobmsg_open_array(b, "errors");
420 list_for_each_entry(error, &iface->errors, list) {
421 e2 = blobmsg_open_table(b, NULL);
423 blobmsg_add_string(b, "subsystem", error->subsystem);
424 blobmsg_add_string(b, "code", error->code);
425 if (error->data[0]) {
426 e3 = blobmsg_open_array(b, "data");
427 for (i = 0; error->data[i]; i++)
428 blobmsg_add_string(b, NULL, error->data[i]);
429 blobmsg_close_array(b, e3);
432 blobmsg_close_table(b, e2);
434 blobmsg_close_array(b, e);
438 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
441 struct device_addr *addr;
447 time_t now = system_get_rtime();
448 vlist_for_each_element(&ip->addr, addr, node) {
449 if (addr->enabled != enabled)
452 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
457 if (af != (v6 ? AF_INET6 : AF_INET))
460 a = blobmsg_open_table(&b, NULL);
462 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
463 inet_ntop(af, &addr->addr, buf, buflen);
464 blobmsg_add_string_buffer(&b);
466 blobmsg_add_u32(&b, "mask", addr->mask);
468 if (addr->preferred_until) {
469 int preferred = addr->preferred_until - now;
472 blobmsg_add_u32(&b, "preferred", preferred);
475 if (addr->valid_until)
476 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
479 blobmsg_add_string(&b, "class", addr->pclass);
481 blobmsg_close_table(&b, a);
486 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
488 struct device_route *route;
494 time_t now = system_get_rtime();
495 vlist_for_each_element(&ip->route, route, node) {
496 if (route->enabled != enabled)
499 if ((ip->no_defaultroute == enabled) && !route->mask)
502 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
507 r = blobmsg_open_table(&b, NULL);
509 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
510 inet_ntop(af, &route->addr, buf, buflen);
511 blobmsg_add_string_buffer(&b);
513 blobmsg_add_u32(&b, "mask", route->mask);
515 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
516 inet_ntop(af, &route->nexthop, buf, buflen);
517 blobmsg_add_string_buffer(&b);
519 if (route->flags & DEVROUTE_TYPE)
520 blobmsg_add_u32(&b, "type", route->type);
522 if (route->flags & DEVROUTE_MTU)
523 blobmsg_add_u32(&b, "mtu", route->mtu);
525 if (route->flags & DEVROUTE_METRIC)
526 blobmsg_add_u32(&b, "metric", route->metric);
528 if (route->flags & DEVROUTE_TABLE)
529 blobmsg_add_u32(&b, "table", route->table);
531 if (route->valid_until)
532 blobmsg_add_u32(&b, "valid", route->valid_until - now);
534 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
535 inet_ntop(af, &route->source, buf, buflen);
536 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
537 blobmsg_add_string_buffer(&b);
539 blobmsg_close_table(&b, r);
545 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
547 struct device_prefix *prefix;
550 const int buflen = INET6_ADDRSTRLEN;
552 time_t now = system_get_rtime();
553 vlist_for_each_element(&ip->prefix, prefix, node) {
554 a = blobmsg_open_table(&b, NULL);
556 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
557 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
558 blobmsg_add_string_buffer(&b);
560 blobmsg_add_u32(&b, "mask", prefix->length);
562 if (prefix->preferred_until) {
563 int preferred = prefix->preferred_until - now;
566 blobmsg_add_u32(&b, "preferred", preferred);
569 if (prefix->valid_until)
570 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
572 blobmsg_add_string(&b, "class", prefix->pclass);
574 c = blobmsg_open_table(&b, "assigned");
575 struct device_prefix_assignment *assign;
576 list_for_each_entry(assign, &prefix->assignments, head) {
577 if (!assign->name[0])
580 struct in6_addr addr = prefix->addr;
581 addr.s6_addr32[1] |= htonl(assign->assigned);
583 void *d = blobmsg_open_table(&b, assign->name);
585 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
586 inet_ntop(AF_INET6, &addr, buf, buflen);
587 blobmsg_add_string_buffer(&b);
589 blobmsg_add_u32(&b, "mask", assign->length);
591 blobmsg_close_table(&b, d);
593 blobmsg_close_table(&b, c);
595 blobmsg_close_table(&b, a);
601 interface_ip_dump_prefix_assignment_list(struct interface *iface)
605 const int buflen = INET6_ADDRSTRLEN;
606 time_t now = system_get_rtime();
608 struct device_prefix *prefix;
609 list_for_each_entry(prefix, &prefixes, head) {
610 struct device_prefix_assignment *assign;
611 list_for_each_entry(assign, &prefix->assignments, head) {
612 if (strcmp(assign->name, iface->name))
615 struct in6_addr addr = prefix->addr;
616 addr.s6_addr32[1] |= htonl(assign->assigned);
618 a = blobmsg_open_table(&b, NULL);
620 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
621 inet_ntop(AF_INET6, &addr, buf, buflen);
622 blobmsg_add_string_buffer(&b);
624 blobmsg_add_u32(&b, "mask", assign->length);
626 if (prefix->preferred_until) {
627 int preferred = prefix->preferred_until - now;
630 blobmsg_add_u32(&b, "preferred", preferred);
633 if (prefix->valid_until)
634 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
636 blobmsg_close_table(&b, a);
643 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
646 struct dns_server *dns;
650 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
651 if (ip->no_dns == enabled)
654 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
655 inet_ntop(dns->af, &dns->addr, buf, buflen);
656 blobmsg_add_string_buffer(&b);
661 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
664 struct dns_search_domain *dns;
666 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
667 if (ip->no_dns == enabled)
670 blobmsg_add_string(&b, NULL, dns->name);
675 netifd_dump_status(struct interface *iface)
677 struct interface_data *data;
681 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
682 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
683 blobmsg_add_u8(&b, "available", iface->available);
684 blobmsg_add_u8(&b, "autostart", iface->autostart);
686 if (iface->state == IFS_UP) {
687 time_t cur = system_get_rtime();
688 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
689 if (iface->l3_dev.dev)
690 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
693 if (iface->proto_handler)
694 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
696 dev = iface->main_dev.dev;
697 if (dev && !dev->hidden &&
698 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
699 blobmsg_add_string(&b, "device", dev->ifname);
701 if (iface->state == IFS_UP) {
702 if (iface->updated) {
703 a = blobmsg_open_array(&b, "updated");
705 if (iface->updated & IUF_ADDRESS)
706 blobmsg_add_string(&b, NULL, "addresses");
707 if (iface->updated & IUF_ROUTE)
708 blobmsg_add_string(&b, NULL, "routes");
709 if (iface->updated & IUF_PREFIX)
710 blobmsg_add_string(&b, NULL, "prefixes");
711 if (iface->updated & IUF_DATA)
712 blobmsg_add_string(&b, NULL, "data");
714 blobmsg_close_array(&b, a);
718 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
720 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
721 blobmsg_add_u32(&b, "metric", iface->metric);
722 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
723 a = blobmsg_open_array(&b, "ipv4-address");
724 interface_ip_dump_address_list(&iface->config_ip, false, true);
725 interface_ip_dump_address_list(&iface->proto_ip, false, true);
726 blobmsg_close_array(&b, a);
727 a = blobmsg_open_array(&b, "ipv6-address");
728 interface_ip_dump_address_list(&iface->config_ip, true, true);
729 interface_ip_dump_address_list(&iface->proto_ip, true, true);
730 blobmsg_close_array(&b, a);
731 a = blobmsg_open_array(&b, "ipv6-prefix");
732 interface_ip_dump_prefix_list(&iface->config_ip);
733 interface_ip_dump_prefix_list(&iface->proto_ip);
734 blobmsg_close_array(&b, a);
735 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
736 interface_ip_dump_prefix_assignment_list(iface);
737 blobmsg_close_array(&b, a);
738 a = blobmsg_open_array(&b, "route");
739 interface_ip_dump_route_list(&iface->config_ip, true);
740 interface_ip_dump_route_list(&iface->proto_ip, true);
741 blobmsg_close_array(&b, a);
742 a = blobmsg_open_array(&b, "dns-server");
743 interface_ip_dump_dns_server_list(&iface->config_ip, true);
744 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
745 blobmsg_close_array(&b, a);
746 a = blobmsg_open_array(&b, "dns-search");
747 interface_ip_dump_dns_search_list(&iface->config_ip, true);
748 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
749 blobmsg_close_array(&b, a);
751 inactive = blobmsg_open_table(&b, "inactive");
752 a = blobmsg_open_array(&b, "ipv4-address");
753 interface_ip_dump_address_list(&iface->config_ip, false, false);
754 interface_ip_dump_address_list(&iface->proto_ip, false, false);
755 blobmsg_close_array(&b, a);
756 a = blobmsg_open_array(&b, "ipv6-address");
757 interface_ip_dump_address_list(&iface->config_ip, true, false);
758 interface_ip_dump_address_list(&iface->proto_ip, true, false);
759 blobmsg_close_array(&b, a);
760 a = blobmsg_open_array(&b, "route");
761 interface_ip_dump_route_list(&iface->config_ip, false);
762 interface_ip_dump_route_list(&iface->proto_ip, false);
763 blobmsg_close_array(&b, a);
764 a = blobmsg_open_array(&b, "dns-server");
765 interface_ip_dump_dns_server_list(&iface->config_ip, false);
766 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
767 blobmsg_close_array(&b, a);
768 a = blobmsg_open_array(&b, "dns-search");
769 interface_ip_dump_dns_search_list(&iface->config_ip, false);
770 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
771 blobmsg_close_array(&b, a);
772 blobmsg_close_table(&b, inactive);
775 a = blobmsg_open_table(&b, "data");
776 avl_for_each_element(&iface->data, data, node)
777 blobmsg_add_blob(&b, data->data);
779 blobmsg_close_table(&b, a);
781 if (!list_empty(&iface->errors))
782 netifd_add_interface_errors(&b, iface);
786 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
787 struct ubus_request_data *req, const char *method,
788 struct blob_attr *msg)
790 struct interface *iface = container_of(obj, struct interface, ubus);
792 blob_buf_init(&b, 0);
793 netifd_dump_status(iface);
794 ubus_send_reply(ctx, req, b.head);
801 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
802 struct ubus_request_data *req, const char *method,
803 struct blob_attr *msg)
805 blob_buf_init(&b, 0);
806 void *a = blobmsg_open_array(&b, "interface");
808 struct interface *iface;
809 vlist_for_each_element(&interfaces, iface, node) {
810 void *i = blobmsg_open_table(&b, NULL);
811 blobmsg_add_string(&b, "interface", iface->name);
812 netifd_dump_status(iface);
813 blobmsg_close_table(&b, i);
816 blobmsg_close_array(&b, a);
817 ubus_send_reply(ctx, req, b.head);
823 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
824 struct ubus_request_data *req, const char *method,
825 struct blob_attr *msg)
827 struct blob_attr *tb[__DEV_MAX];
828 struct interface *iface;
829 bool add = !strncmp(method, "add", 3);
831 iface = container_of(obj, struct interface, ubus);
833 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
836 return UBUS_STATUS_INVALID_ARGUMENT;
838 return interface_handle_link(iface, blobmsg_data(tb[DEV_NAME]), add);
843 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
844 struct ubus_request_data *req, const char *method,
845 struct blob_attr *msg)
847 struct interface *iface;
849 iface = container_of(obj, struct interface, ubus);
851 if (!iface->proto || !iface->proto->notify)
852 return UBUS_STATUS_NOT_SUPPORTED;
854 return iface->proto->notify(iface->proto, msg);
858 netifd_iface_do_remove(struct uloop_timeout *timeout)
860 struct interface *iface;
862 iface = container_of(timeout, struct interface, remove_timer);
863 vlist_delete(&interfaces, &iface->node);
867 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
868 struct ubus_request_data *req, const char *method,
869 struct blob_attr *msg)
871 struct interface *iface;
873 iface = container_of(obj, struct interface, ubus);
874 if (iface->remove_timer.cb)
875 return UBUS_STATUS_INVALID_ARGUMENT;
877 iface->remove_timer.cb = netifd_iface_do_remove;
878 uloop_timeout_set(&iface->remove_timer, 100);
883 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
884 struct ubus_request_data *req, const char *method,
885 struct blob_attr *msg)
887 struct interface *iface;
889 const struct device_hotplug_ops *ops;
891 iface = container_of(obj, struct interface, ubus);
892 dev = iface->main_dev.dev;
896 ops = dev->hotplug_ops;
900 return ops->prepare(dev);
904 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
905 struct ubus_request_data *req, const char *method,
906 struct blob_attr *msg)
908 struct interface *iface;
909 struct blob_attr *cur;
912 iface = container_of(obj, struct interface, ubus);
914 blob_for_each_attr(cur, msg, rem) {
915 ret = interface_add_data(iface, cur);
923 static struct ubus_method iface_object_methods[] = {
924 { .name = "up", .handler = netifd_handle_up },
925 { .name = "down", .handler = netifd_handle_down },
926 { .name = "status", .handler = netifd_handle_status },
927 { .name = "prepare", .handler = netifd_handle_iface_prepare },
928 { .name = "dump", .handler = netifd_handle_dump },
929 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
930 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
931 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
932 { .name = "remove", .handler = netifd_iface_remove },
933 { .name = "set_data", .handler = netifd_handle_set_data },
936 static struct ubus_object_type iface_object_type =
937 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
940 static struct ubus_object iface_object = {
941 .name = "network.interface",
942 .type = &iface_object_type,
943 .n_methods = ARRAY_SIZE(iface_object_methods),
946 static void netifd_add_object(struct ubus_object *obj)
948 int ret = ubus_add_object(ubus_ctx, obj);
951 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
954 static const struct blobmsg_policy iface_policy = {
956 .type = BLOBMSG_TYPE_STRING,
960 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
961 struct ubus_request_data *req, const char *method,
962 struct blob_attr *msg)
964 struct interface *iface;
965 struct blob_attr *tb;
968 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
970 return UBUS_STATUS_INVALID_ARGUMENT;
972 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
974 return UBUS_STATUS_NOT_FOUND;
976 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
979 if (strcmp(method, iface_object_methods[i].name) != 0)
982 cb = iface_object_methods[i].handler;
983 return cb(ctx, &iface->ubus, req, method, msg);
986 return UBUS_STATUS_INVALID_ARGUMENT;
989 static void netifd_add_iface_object(void)
991 struct ubus_method *methods;
994 methods = calloc(1, sizeof(iface_object_methods));
995 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
996 iface_object.methods = methods;
998 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
999 if (methods[i].handler == netifd_handle_dump)
1002 methods[i].handler = netifd_handle_iface;
1003 methods[i].policy = &iface_policy;
1004 methods[i].n_policy = 1;
1006 netifd_add_object(&iface_object);
1009 static struct wireless_device *
1010 get_wdev(struct blob_attr *msg, int *ret)
1012 struct blobmsg_policy wdev_policy = {
1014 .type = BLOBMSG_TYPE_STRING,
1016 struct blob_attr *dev_attr;
1017 struct wireless_device *wdev = NULL;
1020 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1022 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1026 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1028 *ret = UBUS_STATUS_NOT_FOUND;
1037 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1038 struct ubus_request_data *req, const char *method,
1039 struct blob_attr *msg)
1041 struct wireless_device *wdev;
1044 wdev = get_wdev(msg, &ret);
1045 if (ret == UBUS_STATUS_NOT_FOUND)
1049 wireless_device_set_up(wdev);
1051 vlist_for_each_element(&wireless_devices, wdev, node)
1052 wireless_device_set_up(wdev);
1059 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1060 struct ubus_request_data *req, const char *method,
1061 struct blob_attr *msg)
1063 struct wireless_device *wdev;
1066 wdev = get_wdev(msg, &ret);
1067 if (ret == UBUS_STATUS_NOT_FOUND)
1071 wireless_device_set_down(wdev);
1073 vlist_for_each_element(&wireless_devices, wdev, node)
1074 wireless_device_set_down(wdev);
1081 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1082 struct ubus_request_data *req, const char *method,
1083 struct blob_attr *msg)
1085 struct wireless_device *wdev;
1088 wdev = get_wdev(msg, &ret);
1089 if (ret == UBUS_STATUS_NOT_FOUND)
1092 blob_buf_init(&b, 0);
1094 wireless_device_status(wdev, &b);
1096 vlist_for_each_element(&wireless_devices, wdev, node)
1097 wireless_device_status(wdev, &b);
1099 ubus_send_reply(ctx, req, b.head);
1104 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1105 struct ubus_request_data *req, const char *method,
1106 struct blob_attr *msg)
1108 struct wireless_device *wdev;
1111 wdev = get_wdev(msg, &ret);
1112 if (ret == UBUS_STATUS_NOT_FOUND)
1115 blob_buf_init(&b, 0);
1117 wireless_device_get_validate(wdev, &b);
1119 vlist_for_each_element(&wireless_devices, wdev, node)
1120 wireless_device_get_validate(wdev, &b);
1122 ubus_send_reply(ctx, req, b.head);
1127 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1128 struct ubus_request_data *req, const char *method,
1129 struct blob_attr *msg)
1131 struct wireless_device *wdev;
1134 wdev = get_wdev(msg, &ret);
1138 return wireless_device_notify(wdev, msg, req);
1141 static struct ubus_method wireless_object_methods[] = {
1142 { .name = "up", .handler = netifd_handle_wdev_up },
1143 { .name = "down", .handler = netifd_handle_wdev_down },
1144 { .name = "status", .handler = netifd_handle_wdev_status },
1145 { .name = "notify", .handler = netifd_handle_wdev_notify },
1146 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1149 static struct ubus_object_type wireless_object_type =
1150 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1153 static struct ubus_object wireless_object = {
1154 .name = "network.wireless",
1155 .type = &wireless_object_type,
1156 .methods = wireless_object_methods,
1157 .n_methods = ARRAY_SIZE(wireless_object_methods),
1161 netifd_ubus_init(const char *path)
1166 ubus_ctx = ubus_connect(path);
1170 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1171 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1172 netifd_ubus_add_fd();
1174 netifd_add_object(&main_object);
1175 netifd_add_object(&dev_object);
1176 netifd_add_object(&wireless_object);
1177 netifd_add_iface_object();
1183 netifd_ubus_done(void)
1185 ubus_free(ubus_ctx);
1189 netifd_ubus_interface_event(struct interface *iface, bool up)
1191 blob_buf_init(&b, 0);
1192 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1193 blobmsg_add_string(&b, "interface", iface->name);
1194 ubus_send_event(ubus_ctx, "network.interface", b.head);
1198 netifd_ubus_interface_notify(struct interface *iface, bool up)
1200 const char *event = (up) ? "interface.update" : "interface.down";
1201 blob_buf_init(&b, 0);
1202 blobmsg_add_string(&b, "interface", iface->name);
1203 netifd_dump_status(iface);
1204 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1205 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1209 netifd_ubus_add_interface(struct interface *iface)
1211 struct ubus_object *obj = &iface->ubus;
1214 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1218 obj->type = &iface_object_type;
1219 obj->methods = iface_object_methods;
1220 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1221 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1222 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1229 netifd_ubus_remove_interface(struct interface *iface)
1231 if (!iface->ubus.name)
1234 ubus_remove_object(ubus_ctx, &iface->ubus);
1235 free((void *) iface->ubus.name);