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"
26 static struct ubus_context
*ctx
= NULL
;
27 static struct blob_buf b
;
28 static struct netifd_fd ubus_fd
;
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
)
57 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
58 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
59 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
63 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
64 struct ubus_request_data
*req
, const char *method
,
65 struct blob_attr
*msg
)
67 struct blob_attr
*tb
[__HR_MAX
];
68 struct interface
*iface
;
72 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
74 return UBUS_STATUS_INVALID_ARGUMENT
;
77 v6
= blobmsg_get_bool(tb
[HR_V6
]);
79 memset(&a
, 0, sizeof(a
));
80 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
81 return UBUS_STATUS_INVALID_ARGUMENT
;
84 iface
= interface_ip_add_target_route(&a
, v6
);
86 return UBUS_STATUS_NOT_FOUND
;
89 blobmsg_add_string(&b
, "interface", iface
->name
);
90 ubus_send_reply(ctx
, req
, b
.head
);
96 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
97 struct ubus_request_data
*req
, const char *method
,
98 struct blob_attr
*msg
)
100 blob_buf_init(&b
, 0);
101 proto_dump_handlers(&b
);
102 ubus_send_reply(ctx
, req
, b
.head
);
107 static struct ubus_method main_object_methods
[] = {
108 { .name
= "restart", .handler
= netifd_handle_restart
},
109 { .name
= "reload", .handler
= netifd_handle_reload
},
110 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
111 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
114 static struct ubus_object_type main_object_type
=
115 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
117 static struct ubus_object main_object
= {
119 .type
= &main_object_type
,
120 .methods
= main_object_methods
,
121 .n_methods
= ARRAY_SIZE(main_object_methods
),
129 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
130 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
134 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
135 struct ubus_request_data
*req
, const char *method
,
136 struct blob_attr
*msg
)
138 struct device
*dev
= NULL
;
139 struct blob_attr
*tb
[__DEV_MAX
];
141 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
144 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
146 return UBUS_STATUS_INVALID_ARGUMENT
;
149 blob_buf_init(&b
, 0);
150 device_dump_status(&b
, dev
);
151 ubus_send_reply(ctx
, req
, b
.head
);
162 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
163 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
164 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
168 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
169 struct ubus_request_data
*req
, const char *method
,
170 struct blob_attr
*msg
)
172 struct device
*dev
= NULL
;
173 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
174 struct blob_attr
*cur
;
177 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
179 if (!tb
[ALIAS_ATTR_ALIAS
])
180 return UBUS_STATUS_INVALID_ARGUMENT
;
182 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
183 dev
= device_get(blobmsg_data(cur
), true);
185 return UBUS_STATUS_NOT_FOUND
;
188 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
189 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
192 if (!blobmsg_check_attr(cur
, NULL
))
195 alias_notify_device(blobmsg_data(cur
), dev
);
200 device_free_unused(dev
);
201 return UBUS_STATUS_INVALID_ARGUMENT
;
210 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
211 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
212 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
216 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
217 struct ubus_request_data
*req
, const char *method
,
218 struct blob_attr
*msg
)
220 struct device
*dev
= NULL
;
221 struct blob_attr
*tb
[__DEV_STATE_MAX
];
222 struct blob_attr
*cur
;
224 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
226 cur
= tb
[DEV_STATE_NAME
];
228 return UBUS_STATUS_INVALID_ARGUMENT
;
230 dev
= device_get(blobmsg_data(cur
), false);
232 return UBUS_STATUS_NOT_FOUND
;
234 cur
= tb
[DEV_STATE_DEFER
];
236 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
241 static struct ubus_method dev_object_methods
[] = {
242 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
243 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
244 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
247 static struct ubus_object_type dev_object_type
=
248 UBUS_OBJECT_TYPE("device", dev_object_methods
);
250 static struct ubus_object dev_object
= {
251 .name
= "network.device",
252 .type
= &dev_object_type
,
253 .methods
= dev_object_methods
,
254 .n_methods
= ARRAY_SIZE(dev_object_methods
),
258 netifd_ubus_add_fd(void)
261 ubus_fd
.fd
= ctx
->sock
.fd
;
262 netifd_fd_add(&ubus_fd
);
266 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
268 static struct uloop_timeout retry
= {
269 .cb
= netifd_ubus_reconnect_timer
,
273 if (ubus_reconnect(ctx
, ubus_path
) != 0) {
274 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
275 uloop_timeout_set(&retry
, t
* 1000);
279 DPRINTF("reconnected to ubus, new id: %08x\n", ctx
->local_id
);
280 netifd_ubus_add_fd();
284 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
286 netifd_fd_delete(&ubus_fd
);
287 netifd_ubus_reconnect_timer(NULL
);
291 netifd_ubus_init(const char *path
)
298 ctx
= ubus_connect(path
);
302 DPRINTF("connected as %08x\n", ctx
->local_id
);
303 ctx
->connection_lost
= netifd_ubus_connection_lost
;
304 netifd_ubus_add_fd();
306 ret
= ubus_add_object(ctx
, &main_object
);
310 ret
= ubus_add_object(ctx
, &dev_object
);
314 fprintf(stderr
, "Failed to publish object: %s\n", ubus_strerror(ret
));
319 netifd_ubus_done(void)
325 /* per-interface object */
328 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
329 struct ubus_request_data
*req
, const char *method
,
330 struct blob_attr
*msg
)
332 struct interface
*iface
;
334 iface
= container_of(obj
, struct interface
, ubus
);
335 interface_set_up(iface
);
341 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
342 struct ubus_request_data
*req
, const char *method
,
343 struct blob_attr
*msg
)
345 struct interface
*iface
;
347 iface
= container_of(obj
, struct interface
, ubus
);
348 interface_set_down(iface
);
354 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
356 struct interface_error
*error
;
360 e
= blobmsg_open_array(b
, "errors");
361 list_for_each_entry(error
, &iface
->errors
, list
) {
362 e2
= blobmsg_open_table(b
, NULL
);
364 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
365 blobmsg_add_string(b
, "code", error
->code
);
366 if (error
->data
[0]) {
367 e3
= blobmsg_open_array(b
, "data");
368 for (i
= 0; error
->data
[i
]; i
++)
369 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
370 blobmsg_close_array(b
, e3
);
373 blobmsg_close_table(b
, e2
);
375 blobmsg_close_array(b
, e
);
379 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
,
382 struct device_addr
*addr
;
388 vlist_for_each_element(&ip
->addr
, addr
, node
) {
389 if (addr
->enabled
!= enabled
)
392 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
397 if (af
!= (v6
? AF_INET6
: AF_INET
))
400 a
= blobmsg_open_table(&b
, NULL
);
402 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
403 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
404 blobmsg_add_string_buffer(&b
);
406 blobmsg_add_u32(&b
, "mask", addr
->mask
);
408 blobmsg_close_table(&b
, a
);
413 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
415 struct device_route
*route
;
421 vlist_for_each_element(&ip
->route
, route
, node
) {
422 if (route
->enabled
!= enabled
)
425 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
430 r
= blobmsg_open_table(&b
, NULL
);
432 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
433 inet_ntop(af
, &route
->addr
, buf
, buflen
);
434 blobmsg_add_string_buffer(&b
);
436 blobmsg_add_u32(&b
, "mask", route
->mask
);
438 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
439 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
440 blobmsg_add_string_buffer(&b
);
442 if (route
->flags
& DEVROUTE_MTU
)
443 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
445 if (route
->flags
& DEVROUTE_METRIC
)
446 blobmsg_add_u32(&b
, "metric", route
->metric
);
448 blobmsg_add_u8(&b
, "enabled", route
->enabled
);
450 blobmsg_close_table(&b
, r
);
455 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
458 struct dns_server
*dns
;
462 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
463 if (ip
->no_dns
== enabled
)
466 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
467 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
468 blobmsg_add_string_buffer(&b
);
473 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
,
476 struct dns_search_domain
*dns
;
478 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
479 if (ip
->no_dns
== enabled
)
482 blobmsg_add_string(&b
, NULL
, dns
->name
);
487 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
488 struct ubus_request_data
*req
, const char *method
,
489 struct blob_attr
*msg
)
491 struct interface
*iface
;
492 struct interface_data
*data
;
496 iface
= container_of(obj
, struct interface
, ubus
);
498 blob_buf_init(&b
, 0);
499 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
500 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
501 blobmsg_add_u8(&b
, "available", iface
->available
);
502 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
504 if (iface
->state
== IFS_UP
) {
505 time_t cur
= system_get_rtime();
506 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
507 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
510 if (iface
->proto_handler
)
511 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
513 dev
= iface
->main_dev
.dev
;
514 if (dev
&& !dev
->hidden
&&
515 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
516 blobmsg_add_string(&b
, "device", dev
->ifname
);
518 if (iface
->state
== IFS_UP
) {
519 blobmsg_add_u32(&b
, "metric", iface
->metric
);
520 a
= blobmsg_open_array(&b
, "ipv4-address");
521 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
522 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
523 blobmsg_close_array(&b
, a
);
524 a
= blobmsg_open_array(&b
, "ipv6-address");
525 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
526 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
527 blobmsg_close_array(&b
, a
);
528 a
= blobmsg_open_array(&b
, "route");
529 interface_ip_dump_route_list(&iface
->config_ip
, true);
530 interface_ip_dump_route_list(&iface
->proto_ip
, true);
531 blobmsg_close_array(&b
, a
);
532 a
= blobmsg_open_array(&b
, "dns-server");
533 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
534 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
535 blobmsg_close_array(&b
, a
);
536 a
= blobmsg_open_array(&b
, "dns-search");
537 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
538 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
539 blobmsg_close_array(&b
, a
);
541 inactive
= blobmsg_open_table(&b
, "inactive");
542 a
= blobmsg_open_array(&b
, "ipv4-address");
543 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
544 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
545 blobmsg_close_array(&b
, a
);
546 a
= blobmsg_open_array(&b
, "ipv6-address");
547 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
548 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
549 blobmsg_close_array(&b
, a
);
550 a
= blobmsg_open_array(&b
, "route");
551 interface_ip_dump_route_list(&iface
->config_ip
, false);
552 interface_ip_dump_route_list(&iface
->proto_ip
, false);
553 blobmsg_close_array(&b
, a
);
554 a
= blobmsg_open_array(&b
, "dns-server");
555 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
556 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
557 blobmsg_close_array(&b
, a
);
558 a
= blobmsg_open_array(&b
, "dns-search");
559 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
560 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
561 blobmsg_close_array(&b
, a
);
562 blobmsg_close_table(&b
, inactive
);
565 a
= blobmsg_open_table(&b
, "data");
566 avl_for_each_element(&iface
->data
, data
, node
)
567 blob_put(&b
, blob_id(data
->data
), blob_data(data
->data
), blob_len(data
->data
));
569 blobmsg_close_table(&b
, a
);
571 if (!list_is_empty(&iface
->errors
))
572 netifd_add_interface_errors(&b
, iface
);
574 ubus_send_reply(ctx
, req
, b
.head
);
580 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
581 struct ubus_request_data
*req
, const char *method
,
582 struct blob_attr
*msg
)
584 struct blob_attr
*tb
[__DEV_MAX
];
585 struct interface
*iface
;
587 bool add
= !strncmp(method
, "add", 3);
590 iface
= container_of(obj
, struct interface
, ubus
);
592 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
595 return UBUS_STATUS_INVALID_ARGUMENT
;
599 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
? 2 : 0);
601 return UBUS_STATUS_NOT_FOUND
;
604 device_set_present(dev
, true);
605 if (iface
->device_config
)
606 device_set_config(dev
, &simple_device_type
, iface
->config
);
608 system_if_apply_settings(dev
, &dev
->settings
);
609 ret
= interface_add_link(iface
, dev
);
611 ret
= interface_remove_link(iface
, dev
);
621 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
622 struct ubus_request_data
*req
, const char *method
,
623 struct blob_attr
*msg
)
625 struct interface
*iface
;
627 iface
= container_of(obj
, struct interface
, ubus
);
629 if (!iface
->proto
|| !iface
->proto
->notify
)
630 return UBUS_STATUS_NOT_SUPPORTED
;
632 return iface
->proto
->notify(iface
->proto
, msg
);
636 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
638 struct interface
*iface
;
640 iface
= container_of(timeout
, struct interface
, remove_timer
);
641 vlist_delete(&interfaces
, &iface
->node
);
645 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
646 struct ubus_request_data
*req
, const char *method
,
647 struct blob_attr
*msg
)
649 struct interface
*iface
;
651 iface
= container_of(obj
, struct interface
, ubus
);
652 if (iface
->remove_timer
.cb
)
653 return UBUS_STATUS_INVALID_ARGUMENT
;
655 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
656 uloop_timeout_set(&iface
->remove_timer
, 100);
661 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
662 struct ubus_request_data
*req
, const char *method
,
663 struct blob_attr
*msg
)
665 struct interface
*iface
;
667 const struct device_hotplug_ops
*ops
;
669 iface
= container_of(obj
, struct interface
, ubus
);
670 dev
= iface
->main_dev
.dev
;
674 ops
= dev
->hotplug_ops
;
678 return ops
->prepare(dev
);
682 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
683 struct ubus_request_data
*req
, const char *method
,
684 struct blob_attr
*msg
)
686 struct interface
*iface
;
687 struct blob_attr
*cur
;
690 iface
= container_of(obj
, struct interface
, ubus
);
692 blob_for_each_attr(cur
, msg
, rem
) {
693 ret
= interface_add_data(iface
, cur
);
701 static struct ubus_method iface_object_methods
[] = {
702 { .name
= "up", .handler
= netifd_handle_up
},
703 { .name
= "down", .handler
= netifd_handle_down
},
704 { .name
= "status", .handler
= netifd_handle_status
},
705 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
706 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
707 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
708 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
709 { .name
= "remove", .handler
= netifd_iface_remove
},
710 { .name
= "set_data", .handler
= netifd_handle_set_data
},
713 static struct ubus_object_type iface_object_type
=
714 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
718 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
720 blob_buf_init(&b
, 0);
721 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
722 blobmsg_add_string(&b
, "interface", iface
->name
);
723 ubus_send_event(ctx
, "network.interface", b
.head
);
727 netifd_ubus_add_interface(struct interface
*iface
)
729 struct ubus_object
*obj
= &iface
->ubus
;
732 asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
);
737 obj
->type
= &iface_object_type
;
738 obj
->methods
= iface_object_methods
;
739 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
740 if (ubus_add_object(ctx
, &iface
->ubus
)) {
741 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
748 netifd_ubus_remove_interface(struct interface
*iface
)
750 if (!iface
->ubus
.name
)
753 ubus_remove_object(ctx
, &iface
->ubus
);
754 free((void *) iface
->ubus
.name
);