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
)
48 return UBUS_STATUS_NOT_FOUND
;
50 return UBUS_STATUS_OK
;
60 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
61 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
62 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
63 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
67 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
68 struct ubus_request_data
*req
, const char *method
,
69 struct blob_attr
*msg
)
71 struct blob_attr
*tb
[__HR_MAX
];
72 struct interface
*iface
= NULL
;
76 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
78 return UBUS_STATUS_INVALID_ARGUMENT
;
81 v6
= blobmsg_get_bool(tb
[HR_V6
]);
84 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
86 memset(&a
, 0, sizeof(a
));
87 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
88 return UBUS_STATUS_INVALID_ARGUMENT
;
91 iface
= interface_ip_add_target_route(&a
, v6
, iface
);
93 return UBUS_STATUS_NOT_FOUND
;
96 blobmsg_add_string(&b
, "interface", iface
->name
);
97 ubus_send_reply(ctx
, req
, b
.head
);
103 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
104 struct ubus_request_data
*req
, const char *method
,
105 struct blob_attr
*msg
)
107 blob_buf_init(&b
, 0);
108 proto_dump_handlers(&b
);
109 ubus_send_reply(ctx
, req
, b
.head
);
120 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
121 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
125 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
126 struct ubus_request_data
*req
, const char *method
,
127 struct blob_attr
*msg
)
129 struct blob_attr
*tb
[__DI_MAX
];
130 struct interface
*iface
;
131 struct blob_attr
*config
;
133 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
136 return UBUS_STATUS_INVALID_ARGUMENT
;
138 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
140 iface
= interface_alloc(name
, msg
, true);
142 return UBUS_STATUS_UNKNOWN_ERROR
;
144 config
= blob_memdup(msg
);
148 if (!interface_add(iface
, config
))
149 goto error_free_config
;
151 return UBUS_STATUS_OK
;
157 return UBUS_STATUS_UNKNOWN_ERROR
;
160 static struct ubus_method main_object_methods
[] = {
161 { .name
= "restart", .handler
= netifd_handle_restart
},
162 { .name
= "reload", .handler
= netifd_handle_reload
},
163 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
164 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
165 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
168 static struct ubus_object_type main_object_type
=
169 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
171 static struct ubus_object main_object
= {
173 .type
= &main_object_type
,
174 .methods
= main_object_methods
,
175 .n_methods
= ARRAY_SIZE(main_object_methods
),
183 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
184 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
188 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
189 struct ubus_request_data
*req
, const char *method
,
190 struct blob_attr
*msg
)
192 struct device
*dev
= NULL
;
193 struct blob_attr
*tb
[__DEV_MAX
];
195 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
198 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
200 return UBUS_STATUS_INVALID_ARGUMENT
;
203 blob_buf_init(&b
, 0);
204 device_dump_status(&b
, dev
);
205 ubus_send_reply(ctx
, req
, b
.head
);
216 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
217 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
218 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
222 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
223 struct ubus_request_data
*req
, const char *method
,
224 struct blob_attr
*msg
)
226 struct device
*dev
= NULL
;
227 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
228 struct blob_attr
*cur
;
231 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
233 if (!tb
[ALIAS_ATTR_ALIAS
])
234 return UBUS_STATUS_INVALID_ARGUMENT
;
236 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
237 dev
= device_get(blobmsg_data(cur
), true);
239 return UBUS_STATUS_NOT_FOUND
;
242 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
243 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
246 if (!blobmsg_check_attr(cur
, NULL
))
249 alias_notify_device(blobmsg_data(cur
), dev
);
254 device_free_unused(dev
);
255 return UBUS_STATUS_INVALID_ARGUMENT
;
264 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
265 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
266 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
270 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
271 struct ubus_request_data
*req
, const char *method
,
272 struct blob_attr
*msg
)
274 struct device
*dev
= NULL
;
275 struct blob_attr
*tb
[__DEV_STATE_MAX
];
276 struct blob_attr
*cur
;
278 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
280 cur
= tb
[DEV_STATE_NAME
];
282 return UBUS_STATUS_INVALID_ARGUMENT
;
284 dev
= device_find(blobmsg_data(cur
));
286 return UBUS_STATUS_NOT_FOUND
;
288 cur
= tb
[DEV_STATE_DEFER
];
290 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
295 static struct ubus_method dev_object_methods
[] = {
296 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
297 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
298 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
301 static struct ubus_object_type dev_object_type
=
302 UBUS_OBJECT_TYPE("device", dev_object_methods
);
304 static struct ubus_object dev_object
= {
305 .name
= "network.device",
306 .type
= &dev_object_type
,
307 .methods
= dev_object_methods
,
308 .n_methods
= ARRAY_SIZE(dev_object_methods
),
312 netifd_ubus_add_fd(void)
314 ubus_add_uloop(ubus_ctx
);
315 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
319 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
321 static struct uloop_timeout retry
= {
322 .cb
= netifd_ubus_reconnect_timer
,
326 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
327 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
328 uloop_timeout_set(&retry
, t
* 1000);
332 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
333 netifd_ubus_add_fd();
337 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
339 netifd_ubus_reconnect_timer(NULL
);
342 /* per-interface object */
345 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
346 struct ubus_request_data
*req
, const char *method
,
347 struct blob_attr
*msg
)
349 struct interface
*iface
;
351 iface
= container_of(obj
, struct interface
, ubus
);
352 interface_set_up(iface
);
358 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
359 struct ubus_request_data
*req
, const char *method
,
360 struct blob_attr
*msg
)
362 struct interface
*iface
;
364 iface
= container_of(obj
, struct interface
, ubus
);
365 interface_set_down(iface
);
371 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
372 struct ubus_request_data
*req
, const char *method
,
373 struct blob_attr
*msg
)
375 struct interface
*iface
;
377 iface
= container_of(obj
, struct interface
, ubus
);
378 interface_renew(iface
);
384 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
386 struct interface_error
*error
;
390 e
= blobmsg_open_array(b
, "errors");
391 list_for_each_entry(error
, &iface
->errors
, list
) {
392 e2
= blobmsg_open_table(b
, NULL
);
394 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
395 blobmsg_add_string(b
, "code", error
->code
);
396 if (error
->data
[0]) {
397 e3
= blobmsg_open_array(b
, "data");
398 for (i
= 0; error
->data
[i
]; i
++)
399 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
400 blobmsg_close_array(b
, e3
);
403 blobmsg_close_table(b
, e2
);
405 blobmsg_close_array(b
, e
);
409 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
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
->point_to_point
) {
439 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
440 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
441 blobmsg_add_string_buffer(&b
);
444 if (addr
->preferred_until
) {
445 int preferred
= addr
->preferred_until
- now
;
448 blobmsg_add_u32(&b
, "preferred", preferred
);
451 if (addr
->valid_until
)
452 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
455 blobmsg_add_string(&b
, "class", addr
->pclass
);
457 blobmsg_close_table(&b
, a
);
462 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
464 struct device_route
*route
;
470 time_t now
= system_get_rtime();
471 vlist_for_each_element(&ip
->route
, route
, node
) {
472 if (route
->enabled
!= enabled
)
475 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
478 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
483 r
= blobmsg_open_table(&b
, NULL
);
485 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
486 inet_ntop(af
, &route
->addr
, buf
, buflen
);
487 blobmsg_add_string_buffer(&b
);
489 blobmsg_add_u32(&b
, "mask", route
->mask
);
491 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
492 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
493 blobmsg_add_string_buffer(&b
);
495 if (route
->flags
& DEVROUTE_TYPE
)
496 blobmsg_add_u32(&b
, "type", route
->type
);
498 if (route
->flags
& DEVROUTE_PROTO
)
499 blobmsg_add_u32(&b
, "proto", route
->proto
);
501 if (route
->flags
& DEVROUTE_MTU
)
502 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
504 if (route
->flags
& DEVROUTE_METRIC
)
505 blobmsg_add_u32(&b
, "metric", route
->metric
);
507 if (route
->flags
& DEVROUTE_TABLE
)
508 blobmsg_add_u32(&b
, "table", route
->table
);
510 if (route
->valid_until
)
511 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
513 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
514 inet_ntop(af
, &route
->source
, buf
, buflen
);
515 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
516 blobmsg_add_string_buffer(&b
);
518 blobmsg_close_table(&b
, r
);
524 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
526 struct device_prefix
*prefix
;
529 const int buflen
= INET6_ADDRSTRLEN
;
531 time_t now
= system_get_rtime();
532 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
533 a
= blobmsg_open_table(&b
, NULL
);
535 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
536 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
537 blobmsg_add_string_buffer(&b
);
539 blobmsg_add_u32(&b
, "mask", prefix
->length
);
541 if (prefix
->preferred_until
) {
542 int preferred
= prefix
->preferred_until
- now
;
545 blobmsg_add_u32(&b
, "preferred", preferred
);
548 if (prefix
->valid_until
)
549 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
551 blobmsg_add_string(&b
, "class", prefix
->pclass
);
553 c
= blobmsg_open_table(&b
, "assigned");
554 struct device_prefix_assignment
*assign
;
555 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
556 if (!assign
->name
[0])
559 struct in6_addr addr
= prefix
->addr
;
560 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
562 void *d
= blobmsg_open_table(&b
, assign
->name
);
564 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
565 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
566 blobmsg_add_string_buffer(&b
);
568 blobmsg_add_u32(&b
, "mask", assign
->length
);
570 blobmsg_close_table(&b
, d
);
572 blobmsg_close_table(&b
, c
);
574 blobmsg_close_table(&b
, a
);
580 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
584 const int buflen
= INET6_ADDRSTRLEN
;
585 time_t now
= system_get_rtime();
587 struct device_prefix
*prefix
;
588 list_for_each_entry(prefix
, &prefixes
, head
) {
589 struct device_prefix_assignment
*assign
;
590 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
591 if (strcmp(assign
->name
, iface
->name
))
594 struct in6_addr addr
= prefix
->addr
;
595 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
597 a
= blobmsg_open_table(&b
, NULL
);
599 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
600 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
601 blobmsg_add_string_buffer(&b
);
603 blobmsg_add_u32(&b
, "mask", assign
->length
);
605 if (prefix
->preferred_until
) {
606 int preferred
= prefix
->preferred_until
- now
;
609 blobmsg_add_u32(&b
, "preferred", preferred
);
612 if (prefix
->valid_until
)
613 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
615 void *c
= blobmsg_open_table(&b
, "local-address");
616 if (assign
->enabled
) {
617 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
618 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
619 blobmsg_add_string_buffer(&b
);
621 blobmsg_add_u32(&b
, "mask", assign
->length
);
623 blobmsg_close_table(&b
, c
);
625 blobmsg_close_table(&b
, a
);
631 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
633 struct dns_server
*dns
;
637 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
638 if (ip
->no_dns
== enabled
)
641 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
642 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
643 blobmsg_add_string_buffer(&b
);
648 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
650 struct dns_search_domain
*dns
;
652 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
653 if (ip
->no_dns
== enabled
)
656 blobmsg_add_string(&b
, NULL
, dns
->name
);
661 netifd_dump_status(struct interface
*iface
)
663 struct interface_data
*data
;
667 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
668 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
669 blobmsg_add_u8(&b
, "available", iface
->available
);
670 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
671 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
673 if (iface
->state
== IFS_UP
) {
674 time_t cur
= system_get_rtime();
675 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
676 if (iface
->l3_dev
.dev
)
677 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
680 if (iface
->proto_handler
)
681 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
683 dev
= iface
->main_dev
.dev
;
684 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
685 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
686 blobmsg_add_string(&b
, "device", dev
->ifname
);
688 if (iface
->state
== IFS_UP
) {
689 if (iface
->updated
) {
690 a
= blobmsg_open_array(&b
, "updated");
692 if (iface
->updated
& IUF_ADDRESS
)
693 blobmsg_add_string(&b
, NULL
, "addresses");
694 if (iface
->updated
& IUF_ROUTE
)
695 blobmsg_add_string(&b
, NULL
, "routes");
696 if (iface
->updated
& IUF_PREFIX
)
697 blobmsg_add_string(&b
, NULL
, "prefixes");
698 if (iface
->updated
& IUF_DATA
)
699 blobmsg_add_string(&b
, NULL
, "data");
701 blobmsg_close_array(&b
, a
);
705 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
707 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
708 blobmsg_add_u32(&b
, "metric", iface
->metric
);
709 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
710 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
711 if (iface
->assignment_weight
)
712 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
713 a
= blobmsg_open_array(&b
, "ipv4-address");
714 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
715 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
716 blobmsg_close_array(&b
, a
);
717 a
= blobmsg_open_array(&b
, "ipv6-address");
718 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
719 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
720 blobmsg_close_array(&b
, a
);
721 a
= blobmsg_open_array(&b
, "ipv6-prefix");
722 interface_ip_dump_prefix_list(&iface
->config_ip
);
723 interface_ip_dump_prefix_list(&iface
->proto_ip
);
724 blobmsg_close_array(&b
, a
);
725 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
726 interface_ip_dump_prefix_assignment_list(iface
);
727 blobmsg_close_array(&b
, a
);
728 a
= blobmsg_open_array(&b
, "route");
729 interface_ip_dump_route_list(&iface
->config_ip
, true);
730 interface_ip_dump_route_list(&iface
->proto_ip
, true);
731 blobmsg_close_array(&b
, a
);
732 a
= blobmsg_open_array(&b
, "dns-server");
733 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
734 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
735 blobmsg_close_array(&b
, a
);
736 a
= blobmsg_open_array(&b
, "dns-search");
737 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
738 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
739 blobmsg_close_array(&b
, a
);
741 inactive
= blobmsg_open_table(&b
, "inactive");
742 a
= blobmsg_open_array(&b
, "ipv4-address");
743 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
744 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
745 blobmsg_close_array(&b
, a
);
746 a
= blobmsg_open_array(&b
, "ipv6-address");
747 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
748 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
749 blobmsg_close_array(&b
, a
);
750 a
= blobmsg_open_array(&b
, "route");
751 interface_ip_dump_route_list(&iface
->config_ip
, false);
752 interface_ip_dump_route_list(&iface
->proto_ip
, false);
753 blobmsg_close_array(&b
, a
);
754 a
= blobmsg_open_array(&b
, "dns-server");
755 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
756 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
757 blobmsg_close_array(&b
, a
);
758 a
= blobmsg_open_array(&b
, "dns-search");
759 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
760 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
761 blobmsg_close_array(&b
, a
);
762 blobmsg_close_table(&b
, inactive
);
765 a
= blobmsg_open_table(&b
, "data");
766 avl_for_each_element(&iface
->data
, data
, node
)
767 blobmsg_add_blob(&b
, data
->data
);
769 blobmsg_close_table(&b
, a
);
771 if (!list_empty(&iface
->errors
))
772 netifd_add_interface_errors(&b
, iface
);
776 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
777 struct ubus_request_data
*req
, const char *method
,
778 struct blob_attr
*msg
)
780 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
782 blob_buf_init(&b
, 0);
783 netifd_dump_status(iface
);
784 ubus_send_reply(ctx
, req
, b
.head
);
791 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
792 struct ubus_request_data
*req
, const char *method
,
793 struct blob_attr
*msg
)
795 blob_buf_init(&b
, 0);
796 void *a
= blobmsg_open_array(&b
, "interface");
798 struct interface
*iface
;
799 vlist_for_each_element(&interfaces
, iface
, node
) {
800 void *i
= blobmsg_open_table(&b
, NULL
);
801 blobmsg_add_string(&b
, "interface", iface
->name
);
802 netifd_dump_status(iface
);
803 blobmsg_close_table(&b
, i
);
806 blobmsg_close_array(&b
, a
);
807 ubus_send_reply(ctx
, req
, b
.head
);
818 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
819 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
820 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
824 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
825 struct ubus_request_data
*req
, const char *method
,
826 struct blob_attr
*msg
)
828 struct blob_attr
*tb
[__DEV_LINK_MAX
];
829 struct blob_attr
*cur
;
830 struct interface
*iface
;
831 bool add
= !strncmp(method
, "add", 3);
832 bool link_ext
= true;
834 iface
= container_of(obj
, struct interface
, ubus
);
836 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
838 if (!tb
[DEV_LINK_NAME
])
839 return UBUS_STATUS_INVALID_ARGUMENT
;
841 cur
= tb
[DEV_LINK_EXT
];
843 link_ext
= blobmsg_get_bool(cur
);
845 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]), add
, link_ext
);
850 netifd_iface_notify_proto(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 iface
= container_of(obj
, struct interface
, ubus
);
858 if (!iface
->proto
|| !iface
->proto
->notify
)
859 return UBUS_STATUS_NOT_SUPPORTED
;
861 return iface
->proto
->notify(iface
->proto
, msg
);
865 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
867 struct interface
*iface
;
869 iface
= container_of(timeout
, struct interface
, remove_timer
);
870 vlist_delete(&interfaces
, &iface
->node
);
874 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
875 struct ubus_request_data
*req
, const char *method
,
876 struct blob_attr
*msg
)
878 struct interface
*iface
;
880 iface
= container_of(obj
, struct interface
, ubus
);
881 if (iface
->remove_timer
.cb
)
882 return UBUS_STATUS_INVALID_ARGUMENT
;
884 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
885 uloop_timeout_set(&iface
->remove_timer
, 100);
890 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
891 struct ubus_request_data
*req
, const char *method
,
892 struct blob_attr
*msg
)
894 struct interface
*iface
;
896 const struct device_hotplug_ops
*ops
;
898 iface
= container_of(obj
, struct interface
, ubus
);
899 dev
= iface
->main_dev
.dev
;
903 ops
= dev
->hotplug_ops
;
907 return ops
->prepare(dev
);
911 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
912 struct ubus_request_data
*req
, const char *method
,
913 struct blob_attr
*msg
)
915 struct interface
*iface
;
917 iface
= container_of(obj
, struct interface
, ubus
);
919 return interface_parse_data(iface
, msg
);
922 static struct ubus_method iface_object_methods
[] = {
923 { .name
= "up", .handler
= netifd_handle_up
},
924 { .name
= "down", .handler
= netifd_handle_down
},
925 { .name
= "renew", .handler
= netifd_handle_renew
},
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_link_policy
),
930 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_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
));
998 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
999 iface_object
.methods
= methods
;
1001 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1002 if (methods
[i
].handler
== netifd_handle_dump
)
1005 methods
[i
].handler
= netifd_handle_iface
;
1006 methods
[i
].policy
= &iface_policy
;
1007 methods
[i
].n_policy
= 1;
1009 netifd_add_object(&iface_object
);
1012 static struct wireless_device
*
1013 get_wdev(struct blob_attr
*msg
, int *ret
)
1015 struct blobmsg_policy wdev_policy
= {
1017 .type
= BLOBMSG_TYPE_STRING
,
1019 struct blob_attr
*dev_attr
;
1020 struct wireless_device
*wdev
= NULL
;
1023 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1025 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1029 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1031 *ret
= UBUS_STATUS_NOT_FOUND
;
1040 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1041 struct ubus_request_data
*req
, const char *method
,
1042 struct blob_attr
*msg
)
1044 struct wireless_device
*wdev
;
1047 wdev
= get_wdev(msg
, &ret
);
1048 if (ret
== UBUS_STATUS_NOT_FOUND
)
1052 wireless_device_set_up(wdev
);
1054 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1055 wireless_device_set_up(wdev
);
1062 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1063 struct ubus_request_data
*req
, const char *method
,
1064 struct blob_attr
*msg
)
1066 struct wireless_device
*wdev
;
1069 wdev
= get_wdev(msg
, &ret
);
1070 if (ret
== UBUS_STATUS_NOT_FOUND
)
1074 wireless_device_set_down(wdev
);
1076 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1077 wireless_device_set_down(wdev
);
1084 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1085 struct ubus_request_data
*req
, const char *method
,
1086 struct blob_attr
*msg
)
1088 struct wireless_device
*wdev
;
1091 wdev
= get_wdev(msg
, &ret
);
1092 if (ret
== UBUS_STATUS_NOT_FOUND
)
1095 blob_buf_init(&b
, 0);
1097 wireless_device_status(wdev
, &b
);
1099 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1100 wireless_device_status(wdev
, &b
);
1102 ubus_send_reply(ctx
, req
, b
.head
);
1107 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1108 struct ubus_request_data
*req
, const char *method
,
1109 struct blob_attr
*msg
)
1111 struct wireless_device
*wdev
;
1114 wdev
= get_wdev(msg
, &ret
);
1115 if (ret
== UBUS_STATUS_NOT_FOUND
)
1118 blob_buf_init(&b
, 0);
1120 wireless_device_get_validate(wdev
, &b
);
1122 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1123 wireless_device_get_validate(wdev
, &b
);
1125 ubus_send_reply(ctx
, req
, b
.head
);
1130 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1131 struct ubus_request_data
*req
, const char *method
,
1132 struct blob_attr
*msg
)
1134 struct wireless_device
*wdev
;
1137 wdev
= get_wdev(msg
, &ret
);
1141 return wireless_device_notify(wdev
, msg
, req
);
1144 static struct ubus_method wireless_object_methods
[] = {
1145 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1146 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1147 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1148 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1149 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1152 static struct ubus_object_type wireless_object_type
=
1153 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1156 static struct ubus_object wireless_object
= {
1157 .name
= "network.wireless",
1158 .type
= &wireless_object_type
,
1159 .methods
= wireless_object_methods
,
1160 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1164 netifd_ubus_init(const char *path
)
1169 ubus_ctx
= ubus_connect(path
);
1173 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1174 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1175 netifd_ubus_add_fd();
1177 netifd_add_object(&main_object
);
1178 netifd_add_object(&dev_object
);
1179 netifd_add_object(&wireless_object
);
1180 netifd_add_iface_object();
1186 netifd_ubus_done(void)
1188 ubus_free(ubus_ctx
);
1192 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1194 blob_buf_init(&b
, 0);
1195 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1196 blobmsg_add_string(&b
, "interface", iface
->name
);
1197 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1201 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1203 const char *event
= (up
) ? "interface.update" : "interface.down";
1204 blob_buf_init(&b
, 0);
1205 blobmsg_add_string(&b
, "interface", iface
->name
);
1206 netifd_dump_status(iface
);
1207 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1208 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1212 netifd_ubus_add_interface(struct interface
*iface
)
1214 struct ubus_object
*obj
= &iface
->ubus
;
1217 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1221 obj
->type
= &iface_object_type
;
1222 obj
->methods
= iface_object_methods
;
1223 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1224 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1225 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1232 netifd_ubus_remove_interface(struct interface
*iface
)
1234 if (!iface
->ubus
.name
)
1237 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1238 free((void *) iface
->ubus
.name
);