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
, false))
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_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
464 struct device_neighbor
*neighbor
;
470 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
471 if (neighbor
->enabled
!= enabled
)
474 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
479 r
= blobmsg_open_table(&b
, NULL
);
481 if (neighbor
->flags
& DEVNEIGH_MAC
)
482 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
484 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
485 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
486 blobmsg_add_string_buffer(&b
);
489 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
491 if (neighbor
->router
)
492 blobmsg_add_u32(&b
, "router", neighbor
->router
);
494 blobmsg_close_table(&b
, r
);
499 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
501 struct device_route
*route
;
507 time_t now
= system_get_rtime();
508 vlist_for_each_element(&ip
->route
, route
, node
) {
509 if (route
->enabled
!= enabled
)
512 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
515 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
520 r
= blobmsg_open_table(&b
, NULL
);
522 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
523 inet_ntop(af
, &route
->addr
, buf
, buflen
);
524 blobmsg_add_string_buffer(&b
);
526 blobmsg_add_u32(&b
, "mask", route
->mask
);
528 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
529 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
530 blobmsg_add_string_buffer(&b
);
532 if (route
->flags
& DEVROUTE_TYPE
)
533 blobmsg_add_u32(&b
, "type", route
->type
);
535 if (route
->flags
& DEVROUTE_PROTO
)
536 blobmsg_add_u32(&b
, "proto", route
->proto
);
538 if (route
->flags
& DEVROUTE_MTU
)
539 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
541 if (route
->flags
& DEVROUTE_METRIC
)
542 blobmsg_add_u32(&b
, "metric", route
->metric
);
544 if (route
->flags
& DEVROUTE_TABLE
)
545 blobmsg_add_u32(&b
, "table", route
->table
);
547 if (route
->valid_until
)
548 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
550 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
551 inet_ntop(af
, &route
->source
, buf
, buflen
);
552 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
553 blobmsg_add_string_buffer(&b
);
555 blobmsg_close_table(&b
, r
);
561 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
563 struct device_prefix
*prefix
;
566 const int buflen
= INET6_ADDRSTRLEN
;
568 time_t now
= system_get_rtime();
569 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
570 a
= blobmsg_open_table(&b
, NULL
);
572 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
573 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
574 blobmsg_add_string_buffer(&b
);
576 blobmsg_add_u32(&b
, "mask", prefix
->length
);
578 if (prefix
->preferred_until
) {
579 int preferred
= prefix
->preferred_until
- now
;
582 blobmsg_add_u32(&b
, "preferred", preferred
);
585 if (prefix
->valid_until
)
586 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
588 blobmsg_add_string(&b
, "class", prefix
->pclass
);
590 c
= blobmsg_open_table(&b
, "assigned");
591 struct device_prefix_assignment
*assign
;
592 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
593 if (!assign
->name
[0])
596 struct in6_addr addr
= prefix
->addr
;
597 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
599 void *d
= blobmsg_open_table(&b
, assign
->name
);
601 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
602 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
603 blobmsg_add_string_buffer(&b
);
605 blobmsg_add_u32(&b
, "mask", assign
->length
);
607 blobmsg_close_table(&b
, d
);
609 blobmsg_close_table(&b
, c
);
611 blobmsg_close_table(&b
, a
);
617 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
621 const int buflen
= INET6_ADDRSTRLEN
;
622 time_t now
= system_get_rtime();
624 struct device_prefix
*prefix
;
625 list_for_each_entry(prefix
, &prefixes
, head
) {
626 struct device_prefix_assignment
*assign
;
627 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
628 if (strcmp(assign
->name
, iface
->name
))
631 struct in6_addr addr
= prefix
->addr
;
632 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
634 a
= blobmsg_open_table(&b
, NULL
);
636 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
637 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
638 blobmsg_add_string_buffer(&b
);
640 blobmsg_add_u32(&b
, "mask", assign
->length
);
642 if (prefix
->preferred_until
) {
643 int preferred
= prefix
->preferred_until
- now
;
646 blobmsg_add_u32(&b
, "preferred", preferred
);
649 if (prefix
->valid_until
)
650 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
652 void *c
= blobmsg_open_table(&b
, "local-address");
653 if (assign
->enabled
) {
654 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
655 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
656 blobmsg_add_string_buffer(&b
);
658 blobmsg_add_u32(&b
, "mask", assign
->length
);
660 blobmsg_close_table(&b
, c
);
662 blobmsg_close_table(&b
, a
);
668 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
670 struct dns_server
*dns
;
674 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
675 if (ip
->no_dns
== enabled
)
678 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
679 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
680 blobmsg_add_string_buffer(&b
);
685 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
687 struct dns_search_domain
*dns
;
689 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
690 if (ip
->no_dns
== enabled
)
693 blobmsg_add_string(&b
, NULL
, dns
->name
);
698 netifd_dump_status(struct interface
*iface
)
700 struct interface_data
*data
;
704 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
705 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
706 blobmsg_add_u8(&b
, "available", iface
->available
);
707 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
708 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
710 if (iface
->state
== IFS_UP
) {
711 time_t cur
= system_get_rtime();
712 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
713 if (iface
->l3_dev
.dev
)
714 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
717 if (iface
->proto_handler
)
718 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
720 dev
= iface
->main_dev
.dev
;
721 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
722 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
723 blobmsg_add_string(&b
, "device", dev
->ifname
);
725 if (iface
->state
== IFS_UP
) {
726 if (iface
->updated
) {
727 a
= blobmsg_open_array(&b
, "updated");
729 if (iface
->updated
& IUF_ADDRESS
)
730 blobmsg_add_string(&b
, NULL
, "addresses");
731 if (iface
->updated
& IUF_ROUTE
)
732 blobmsg_add_string(&b
, NULL
, "routes");
733 if (iface
->updated
& IUF_PREFIX
)
734 blobmsg_add_string(&b
, NULL
, "prefixes");
735 if (iface
->updated
& IUF_DATA
)
736 blobmsg_add_string(&b
, NULL
, "data");
738 blobmsg_close_array(&b
, a
);
742 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
744 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
745 blobmsg_add_u32(&b
, "metric", iface
->metric
);
746 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
747 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
748 if (iface
->assignment_weight
)
749 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
750 a
= blobmsg_open_array(&b
, "ipv4-address");
751 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
752 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
753 blobmsg_close_array(&b
, a
);
754 a
= blobmsg_open_array(&b
, "ipv6-address");
755 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
756 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
757 blobmsg_close_array(&b
, a
);
758 a
= blobmsg_open_array(&b
, "ipv6-prefix");
759 interface_ip_dump_prefix_list(&iface
->config_ip
);
760 interface_ip_dump_prefix_list(&iface
->proto_ip
);
761 blobmsg_close_array(&b
, a
);
762 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
763 interface_ip_dump_prefix_assignment_list(iface
);
764 blobmsg_close_array(&b
, a
);
765 a
= blobmsg_open_array(&b
, "route");
766 interface_ip_dump_route_list(&iface
->config_ip
, true);
767 interface_ip_dump_route_list(&iface
->proto_ip
, true);
768 blobmsg_close_array(&b
, a
);
769 a
= blobmsg_open_array(&b
, "dns-server");
770 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
771 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
772 blobmsg_close_array(&b
, a
);
773 a
= blobmsg_open_array(&b
, "dns-search");
774 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
775 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
776 blobmsg_close_array(&b
, a
);
777 a
= blobmsg_open_array(&b
, "neighbors");
778 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
779 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
780 blobmsg_close_array(&b
, a
);
782 inactive
= blobmsg_open_table(&b
, "inactive");
783 a
= blobmsg_open_array(&b
, "ipv4-address");
784 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
785 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
786 blobmsg_close_array(&b
, a
);
787 a
= blobmsg_open_array(&b
, "ipv6-address");
788 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
789 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
790 blobmsg_close_array(&b
, a
);
791 a
= blobmsg_open_array(&b
, "route");
792 interface_ip_dump_route_list(&iface
->config_ip
, false);
793 interface_ip_dump_route_list(&iface
->proto_ip
, false);
794 blobmsg_close_array(&b
, a
);
795 a
= blobmsg_open_array(&b
, "dns-server");
796 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
797 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
798 blobmsg_close_array(&b
, a
);
799 a
= blobmsg_open_array(&b
, "dns-search");
800 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
801 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
802 blobmsg_close_array(&b
, a
);
803 a
= blobmsg_open_array(&b
, "neighbors");
804 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
805 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
806 blobmsg_close_array(&b
, a
);
807 blobmsg_close_table(&b
, inactive
);
810 a
= blobmsg_open_table(&b
, "data");
811 avl_for_each_element(&iface
->data
, data
, node
)
812 blobmsg_add_blob(&b
, data
->data
);
814 blobmsg_close_table(&b
, a
);
816 if (!list_empty(&iface
->errors
))
817 netifd_add_interface_errors(&b
, iface
);
821 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
822 struct ubus_request_data
*req
, const char *method
,
823 struct blob_attr
*msg
)
825 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
827 blob_buf_init(&b
, 0);
828 netifd_dump_status(iface
);
829 ubus_send_reply(ctx
, req
, b
.head
);
836 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
837 struct ubus_request_data
*req
, const char *method
,
838 struct blob_attr
*msg
)
840 blob_buf_init(&b
, 0);
841 void *a
= blobmsg_open_array(&b
, "interface");
843 struct interface
*iface
;
844 vlist_for_each_element(&interfaces
, iface
, node
) {
845 void *i
= blobmsg_open_table(&b
, NULL
);
846 blobmsg_add_string(&b
, "interface", iface
->name
);
847 netifd_dump_status(iface
);
848 blobmsg_close_table(&b
, i
);
851 blobmsg_close_array(&b
, a
);
852 ubus_send_reply(ctx
, req
, b
.head
);
863 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
864 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
865 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
869 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
870 struct ubus_request_data
*req
, const char *method
,
871 struct blob_attr
*msg
)
873 struct blob_attr
*tb
[__DEV_LINK_MAX
];
874 struct blob_attr
*cur
;
875 struct interface
*iface
;
876 bool add
= !strncmp(method
, "add", 3);
877 bool link_ext
= true;
879 iface
= container_of(obj
, struct interface
, ubus
);
881 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
883 if (!tb
[DEV_LINK_NAME
])
884 return UBUS_STATUS_INVALID_ARGUMENT
;
886 cur
= tb
[DEV_LINK_EXT
];
888 link_ext
= blobmsg_get_bool(cur
);
890 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]), add
, link_ext
);
895 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
896 struct ubus_request_data
*req
, const char *method
,
897 struct blob_attr
*msg
)
899 struct interface
*iface
;
901 iface
= container_of(obj
, struct interface
, ubus
);
903 if (!iface
->proto
|| !iface
->proto
->notify
)
904 return UBUS_STATUS_NOT_SUPPORTED
;
906 return iface
->proto
->notify(iface
->proto
, msg
);
910 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
912 struct interface
*iface
;
914 iface
= container_of(timeout
, struct interface
, remove_timer
);
915 vlist_delete(&interfaces
, &iface
->node
);
919 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
920 struct ubus_request_data
*req
, const char *method
,
921 struct blob_attr
*msg
)
923 struct interface
*iface
;
925 iface
= container_of(obj
, struct interface
, ubus
);
926 if (iface
->remove_timer
.cb
)
927 return UBUS_STATUS_INVALID_ARGUMENT
;
929 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
930 uloop_timeout_set(&iface
->remove_timer
, 100);
935 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
936 struct ubus_request_data
*req
, const char *method
,
937 struct blob_attr
*msg
)
939 struct interface
*iface
;
941 const struct device_hotplug_ops
*ops
;
943 iface
= container_of(obj
, struct interface
, ubus
);
944 dev
= iface
->main_dev
.dev
;
948 ops
= dev
->hotplug_ops
;
952 return ops
->prepare(dev
);
956 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
957 struct ubus_request_data
*req
, const char *method
,
958 struct blob_attr
*msg
)
960 struct interface
*iface
;
962 iface
= container_of(obj
, struct interface
, ubus
);
964 return interface_parse_data(iface
, msg
);
967 static struct ubus_method iface_object_methods
[] = {
968 { .name
= "up", .handler
= netifd_handle_up
},
969 { .name
= "down", .handler
= netifd_handle_down
},
970 { .name
= "renew", .handler
= netifd_handle_renew
},
971 { .name
= "status", .handler
= netifd_handle_status
},
972 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
973 { .name
= "dump", .handler
= netifd_handle_dump
},
974 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
975 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
976 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
977 { .name
= "remove", .handler
= netifd_iface_remove
},
978 { .name
= "set_data", .handler
= netifd_handle_set_data
},
981 static struct ubus_object_type iface_object_type
=
982 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
985 static struct ubus_object iface_object
= {
986 .name
= "network.interface",
987 .type
= &iface_object_type
,
988 .n_methods
= ARRAY_SIZE(iface_object_methods
),
991 static void netifd_add_object(struct ubus_object
*obj
)
993 int ret
= ubus_add_object(ubus_ctx
, obj
);
996 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
999 static const struct blobmsg_policy iface_policy
= {
1000 .name
= "interface",
1001 .type
= BLOBMSG_TYPE_STRING
,
1005 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1006 struct ubus_request_data
*req
, const char *method
,
1007 struct blob_attr
*msg
)
1009 struct interface
*iface
;
1010 struct blob_attr
*tb
;
1013 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1015 return UBUS_STATUS_INVALID_ARGUMENT
;
1017 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1019 return UBUS_STATUS_NOT_FOUND
;
1021 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1024 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1027 cb
= iface_object_methods
[i
].handler
;
1028 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1031 return UBUS_STATUS_INVALID_ARGUMENT
;
1034 static void netifd_add_iface_object(void)
1036 struct ubus_method
*methods
;
1039 methods
= calloc(1, sizeof(iface_object_methods
));
1043 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1044 iface_object
.methods
= methods
;
1046 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1047 if (methods
[i
].handler
== netifd_handle_dump
)
1050 methods
[i
].handler
= netifd_handle_iface
;
1051 methods
[i
].policy
= &iface_policy
;
1052 methods
[i
].n_policy
= 1;
1054 netifd_add_object(&iface_object
);
1057 static struct wireless_device
*
1058 get_wdev(struct blob_attr
*msg
, int *ret
)
1060 struct blobmsg_policy wdev_policy
= {
1062 .type
= BLOBMSG_TYPE_STRING
,
1064 struct blob_attr
*dev_attr
;
1065 struct wireless_device
*wdev
= NULL
;
1068 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1070 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1074 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1076 *ret
= UBUS_STATUS_NOT_FOUND
;
1085 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1086 struct ubus_request_data
*req
, const char *method
,
1087 struct blob_attr
*msg
)
1089 struct wireless_device
*wdev
;
1092 wdev
= get_wdev(msg
, &ret
);
1093 if (ret
== UBUS_STATUS_NOT_FOUND
)
1097 wireless_device_set_up(wdev
);
1099 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1100 wireless_device_set_up(wdev
);
1107 netifd_handle_wdev_down(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
)
1119 wireless_device_set_down(wdev
);
1121 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1122 wireless_device_set_down(wdev
);
1129 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1130 struct ubus_request_data
*req
, const char *method
,
1131 struct blob_attr
*msg
)
1133 struct wireless_device
*wdev
;
1136 wdev
= get_wdev(msg
, &ret
);
1137 if (ret
== UBUS_STATUS_NOT_FOUND
)
1140 blob_buf_init(&b
, 0);
1142 wireless_device_status(wdev
, &b
);
1144 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1145 wireless_device_status(wdev
, &b
);
1147 ubus_send_reply(ctx
, req
, b
.head
);
1152 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1153 struct ubus_request_data
*req
, const char *method
,
1154 struct blob_attr
*msg
)
1156 struct wireless_device
*wdev
;
1159 wdev
= get_wdev(msg
, &ret
);
1160 if (ret
== UBUS_STATUS_NOT_FOUND
)
1163 blob_buf_init(&b
, 0);
1165 wireless_device_get_validate(wdev
, &b
);
1167 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1168 wireless_device_get_validate(wdev
, &b
);
1170 ubus_send_reply(ctx
, req
, b
.head
);
1175 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1176 struct ubus_request_data
*req
, const char *method
,
1177 struct blob_attr
*msg
)
1179 struct wireless_device
*wdev
;
1182 wdev
= get_wdev(msg
, &ret
);
1186 return wireless_device_notify(wdev
, msg
, req
);
1189 static struct ubus_method wireless_object_methods
[] = {
1190 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1191 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1192 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1193 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1194 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1197 static struct ubus_object_type wireless_object_type
=
1198 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1201 static struct ubus_object wireless_object
= {
1202 .name
= "network.wireless",
1203 .type
= &wireless_object_type
,
1204 .methods
= wireless_object_methods
,
1205 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1209 netifd_ubus_init(const char *path
)
1214 ubus_ctx
= ubus_connect(path
);
1218 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1219 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1220 netifd_ubus_add_fd();
1222 netifd_add_object(&main_object
);
1223 netifd_add_object(&dev_object
);
1224 netifd_add_object(&wireless_object
);
1225 netifd_add_iface_object();
1231 netifd_ubus_done(void)
1233 ubus_free(ubus_ctx
);
1237 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1239 blob_buf_init(&b
, 0);
1240 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1241 blobmsg_add_string(&b
, "interface", iface
->name
);
1242 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1246 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1248 const char *event
= (up
) ? "interface.update" : "interface.down";
1249 blob_buf_init(&b
, 0);
1250 blobmsg_add_string(&b
, "interface", iface
->name
);
1251 netifd_dump_status(iface
);
1252 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1253 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1257 netifd_ubus_add_interface(struct interface
*iface
)
1259 struct ubus_object
*obj
= &iface
->ubus
;
1262 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1266 obj
->type
= &iface_object_type
;
1267 obj
->methods
= iface_object_methods
;
1268 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1269 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1270 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1277 netifd_ubus_remove_interface(struct interface
*iface
)
1279 if (!iface
->ubus
.name
)
1282 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1283 free((void *) iface
->ubus
.name
);