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
);
142 return UBUS_STATUS_UNKNOWN_ERROR
;
144 config
= blob_memdup(msg
);
148 interface_add(iface
, config
);
150 // need to look up the interface name again, in case of config update
151 // the pointer will have changed
152 iface
= vlist_find(&interfaces
, name
, iface
, node
);
154 return UBUS_STATUS_UNKNOWN_ERROR
;
156 // Set interface as dynamic
157 interface_set_dynamic(iface
);
159 return UBUS_STATUS_OK
;
163 return UBUS_STATUS_UNKNOWN_ERROR
;
166 static struct ubus_method main_object_methods
[] = {
167 { .name
= "restart", .handler
= netifd_handle_restart
},
168 { .name
= "reload", .handler
= netifd_handle_reload
},
169 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
170 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
171 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
174 static struct ubus_object_type main_object_type
=
175 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
177 static struct ubus_object main_object
= {
179 .type
= &main_object_type
,
180 .methods
= main_object_methods
,
181 .n_methods
= ARRAY_SIZE(main_object_methods
),
189 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
190 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
194 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
195 struct ubus_request_data
*req
, const char *method
,
196 struct blob_attr
*msg
)
198 struct device
*dev
= NULL
;
199 struct blob_attr
*tb
[__DEV_MAX
];
201 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
204 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
206 return UBUS_STATUS_INVALID_ARGUMENT
;
209 blob_buf_init(&b
, 0);
210 device_dump_status(&b
, dev
);
211 ubus_send_reply(ctx
, req
, b
.head
);
222 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
223 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
224 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
228 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
229 struct ubus_request_data
*req
, const char *method
,
230 struct blob_attr
*msg
)
232 struct device
*dev
= NULL
;
233 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
234 struct blob_attr
*cur
;
237 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
239 if (!tb
[ALIAS_ATTR_ALIAS
])
240 return UBUS_STATUS_INVALID_ARGUMENT
;
242 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
243 dev
= device_get(blobmsg_data(cur
), true);
245 return UBUS_STATUS_NOT_FOUND
;
248 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
249 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
252 if (!blobmsg_check_attr(cur
, NULL
))
255 alias_notify_device(blobmsg_data(cur
), dev
);
260 device_free_unused(dev
);
261 return UBUS_STATUS_INVALID_ARGUMENT
;
270 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
271 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
272 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
276 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
277 struct ubus_request_data
*req
, const char *method
,
278 struct blob_attr
*msg
)
280 struct device
*dev
= NULL
;
281 struct blob_attr
*tb
[__DEV_STATE_MAX
];
282 struct blob_attr
*cur
;
284 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
286 cur
= tb
[DEV_STATE_NAME
];
288 return UBUS_STATUS_INVALID_ARGUMENT
;
290 dev
= device_find(blobmsg_data(cur
));
292 return UBUS_STATUS_NOT_FOUND
;
294 cur
= tb
[DEV_STATE_DEFER
];
296 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
301 static struct ubus_method dev_object_methods
[] = {
302 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
303 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
304 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
307 static struct ubus_object_type dev_object_type
=
308 UBUS_OBJECT_TYPE("device", dev_object_methods
);
310 static struct ubus_object dev_object
= {
311 .name
= "network.device",
312 .type
= &dev_object_type
,
313 .methods
= dev_object_methods
,
314 .n_methods
= ARRAY_SIZE(dev_object_methods
),
318 netifd_ubus_add_fd(void)
320 ubus_add_uloop(ubus_ctx
);
321 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
325 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
327 static struct uloop_timeout retry
= {
328 .cb
= netifd_ubus_reconnect_timer
,
332 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
333 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
334 uloop_timeout_set(&retry
, t
* 1000);
338 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
339 netifd_ubus_add_fd();
343 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
345 netifd_ubus_reconnect_timer(NULL
);
348 /* per-interface object */
351 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
352 struct ubus_request_data
*req
, const char *method
,
353 struct blob_attr
*msg
)
355 struct interface
*iface
;
357 iface
= container_of(obj
, struct interface
, ubus
);
358 interface_set_up(iface
);
364 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
365 struct ubus_request_data
*req
, const char *method
,
366 struct blob_attr
*msg
)
368 struct interface
*iface
;
370 iface
= container_of(obj
, struct interface
, ubus
);
371 interface_set_down(iface
);
377 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
378 struct ubus_request_data
*req
, const char *method
,
379 struct blob_attr
*msg
)
381 struct interface
*iface
;
383 iface
= container_of(obj
, struct interface
, ubus
);
384 interface_renew(iface
);
390 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
392 struct interface_error
*error
;
396 e
= blobmsg_open_array(b
, "errors");
397 list_for_each_entry(error
, &iface
->errors
, list
) {
398 e2
= blobmsg_open_table(b
, NULL
);
400 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
401 blobmsg_add_string(b
, "code", error
->code
);
402 if (error
->data
[0]) {
403 e3
= blobmsg_open_array(b
, "data");
404 for (i
= 0; error
->data
[i
]; i
++)
405 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
406 blobmsg_close_array(b
, e3
);
409 blobmsg_close_table(b
, e2
);
411 blobmsg_close_array(b
, e
);
415 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
417 struct device_addr
*addr
;
423 time_t now
= system_get_rtime();
424 vlist_for_each_element(&ip
->addr
, addr
, node
) {
425 if (addr
->enabled
!= enabled
)
428 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
433 if (af
!= (v6
? AF_INET6
: AF_INET
))
436 a
= blobmsg_open_table(&b
, NULL
);
438 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
439 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
440 blobmsg_add_string_buffer(&b
);
442 blobmsg_add_u32(&b
, "mask", addr
->mask
);
444 if (addr
->point_to_point
) {
445 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
446 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
447 blobmsg_add_string_buffer(&b
);
450 if (addr
->preferred_until
) {
451 int preferred
= addr
->preferred_until
- now
;
454 blobmsg_add_u32(&b
, "preferred", preferred
);
457 if (addr
->valid_until
)
458 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
461 blobmsg_add_string(&b
, "class", addr
->pclass
);
463 blobmsg_close_table(&b
, a
);
468 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
470 struct device_route
*route
;
476 time_t now
= system_get_rtime();
477 vlist_for_each_element(&ip
->route
, route
, node
) {
478 if (route
->enabled
!= enabled
)
481 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
484 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
489 r
= blobmsg_open_table(&b
, NULL
);
491 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
492 inet_ntop(af
, &route
->addr
, buf
, buflen
);
493 blobmsg_add_string_buffer(&b
);
495 blobmsg_add_u32(&b
, "mask", route
->mask
);
497 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
498 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
499 blobmsg_add_string_buffer(&b
);
501 if (route
->flags
& DEVROUTE_TYPE
)
502 blobmsg_add_u32(&b
, "type", route
->type
);
504 if (route
->flags
& DEVROUTE_PROTO
)
505 blobmsg_add_u32(&b
, "proto", route
->proto
);
507 if (route
->flags
& DEVROUTE_MTU
)
508 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
510 if (route
->flags
& DEVROUTE_METRIC
)
511 blobmsg_add_u32(&b
, "metric", route
->metric
);
513 if (route
->flags
& DEVROUTE_TABLE
)
514 blobmsg_add_u32(&b
, "table", route
->table
);
516 if (route
->valid_until
)
517 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
519 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
520 inet_ntop(af
, &route
->source
, buf
, buflen
);
521 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
522 blobmsg_add_string_buffer(&b
);
524 blobmsg_close_table(&b
, r
);
530 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
532 struct device_prefix
*prefix
;
535 const int buflen
= INET6_ADDRSTRLEN
;
537 time_t now
= system_get_rtime();
538 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
539 a
= blobmsg_open_table(&b
, NULL
);
541 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
542 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
543 blobmsg_add_string_buffer(&b
);
545 blobmsg_add_u32(&b
, "mask", prefix
->length
);
547 if (prefix
->preferred_until
) {
548 int preferred
= prefix
->preferred_until
- now
;
551 blobmsg_add_u32(&b
, "preferred", preferred
);
554 if (prefix
->valid_until
)
555 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
557 blobmsg_add_string(&b
, "class", prefix
->pclass
);
559 c
= blobmsg_open_table(&b
, "assigned");
560 struct device_prefix_assignment
*assign
;
561 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
562 if (!assign
->name
[0])
565 struct in6_addr addr
= prefix
->addr
;
566 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
568 void *d
= blobmsg_open_table(&b
, assign
->name
);
570 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
571 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
572 blobmsg_add_string_buffer(&b
);
574 blobmsg_add_u32(&b
, "mask", assign
->length
);
576 blobmsg_close_table(&b
, d
);
578 blobmsg_close_table(&b
, c
);
580 blobmsg_close_table(&b
, a
);
586 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
590 const int buflen
= INET6_ADDRSTRLEN
;
591 time_t now
= system_get_rtime();
593 struct device_prefix
*prefix
;
594 list_for_each_entry(prefix
, &prefixes
, head
) {
595 struct device_prefix_assignment
*assign
;
596 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
597 if (strcmp(assign
->name
, iface
->name
))
600 struct in6_addr addr
= prefix
->addr
;
601 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
603 a
= blobmsg_open_table(&b
, NULL
);
605 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
606 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
607 blobmsg_add_string_buffer(&b
);
609 blobmsg_add_u32(&b
, "mask", assign
->length
);
611 if (prefix
->preferred_until
) {
612 int preferred
= prefix
->preferred_until
- now
;
615 blobmsg_add_u32(&b
, "preferred", preferred
);
618 if (prefix
->valid_until
)
619 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
621 void *c
= blobmsg_open_table(&b
, "local-address");
622 if (assign
->enabled
) {
623 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
624 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
625 blobmsg_add_string_buffer(&b
);
627 blobmsg_add_u32(&b
, "mask", assign
->length
);
629 blobmsg_close_table(&b
, c
);
631 blobmsg_close_table(&b
, a
);
637 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
639 struct dns_server
*dns
;
643 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
644 if (ip
->no_dns
== enabled
)
647 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
648 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
649 blobmsg_add_string_buffer(&b
);
654 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
656 struct dns_search_domain
*dns
;
658 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
659 if (ip
->no_dns
== enabled
)
662 blobmsg_add_string(&b
, NULL
, dns
->name
);
667 netifd_dump_status(struct interface
*iface
)
669 struct interface_data
*data
;
673 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
674 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
675 blobmsg_add_u8(&b
, "available", iface
->available
);
676 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
677 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
679 if (iface
->state
== IFS_UP
) {
680 time_t cur
= system_get_rtime();
681 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
682 if (iface
->l3_dev
.dev
)
683 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
686 if (iface
->proto_handler
)
687 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
689 dev
= iface
->main_dev
.dev
;
690 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
691 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
692 blobmsg_add_string(&b
, "device", dev
->ifname
);
694 if (iface
->state
== IFS_UP
) {
695 if (iface
->updated
) {
696 a
= blobmsg_open_array(&b
, "updated");
698 if (iface
->updated
& IUF_ADDRESS
)
699 blobmsg_add_string(&b
, NULL
, "addresses");
700 if (iface
->updated
& IUF_ROUTE
)
701 blobmsg_add_string(&b
, NULL
, "routes");
702 if (iface
->updated
& IUF_PREFIX
)
703 blobmsg_add_string(&b
, NULL
, "prefixes");
704 if (iface
->updated
& IUF_DATA
)
705 blobmsg_add_string(&b
, NULL
, "data");
707 blobmsg_close_array(&b
, a
);
711 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
713 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
714 blobmsg_add_u32(&b
, "metric", iface
->metric
);
715 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
716 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
717 if (iface
->assignment_weight
)
718 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
719 a
= blobmsg_open_array(&b
, "ipv4-address");
720 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
721 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
722 blobmsg_close_array(&b
, a
);
723 a
= blobmsg_open_array(&b
, "ipv6-address");
724 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
725 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
726 blobmsg_close_array(&b
, a
);
727 a
= blobmsg_open_array(&b
, "ipv6-prefix");
728 interface_ip_dump_prefix_list(&iface
->config_ip
);
729 interface_ip_dump_prefix_list(&iface
->proto_ip
);
730 blobmsg_close_array(&b
, a
);
731 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
732 interface_ip_dump_prefix_assignment_list(iface
);
733 blobmsg_close_array(&b
, a
);
734 a
= blobmsg_open_array(&b
, "route");
735 interface_ip_dump_route_list(&iface
->config_ip
, true);
736 interface_ip_dump_route_list(&iface
->proto_ip
, true);
737 blobmsg_close_array(&b
, a
);
738 a
= blobmsg_open_array(&b
, "dns-server");
739 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
740 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
741 blobmsg_close_array(&b
, a
);
742 a
= blobmsg_open_array(&b
, "dns-search");
743 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
744 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
745 blobmsg_close_array(&b
, a
);
747 inactive
= blobmsg_open_table(&b
, "inactive");
748 a
= blobmsg_open_array(&b
, "ipv4-address");
749 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
750 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
751 blobmsg_close_array(&b
, a
);
752 a
= blobmsg_open_array(&b
, "ipv6-address");
753 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
754 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
755 blobmsg_close_array(&b
, a
);
756 a
= blobmsg_open_array(&b
, "route");
757 interface_ip_dump_route_list(&iface
->config_ip
, false);
758 interface_ip_dump_route_list(&iface
->proto_ip
, false);
759 blobmsg_close_array(&b
, a
);
760 a
= blobmsg_open_array(&b
, "dns-server");
761 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
762 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
763 blobmsg_close_array(&b
, a
);
764 a
= blobmsg_open_array(&b
, "dns-search");
765 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
766 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
767 blobmsg_close_array(&b
, a
);
768 blobmsg_close_table(&b
, inactive
);
771 a
= blobmsg_open_table(&b
, "data");
772 avl_for_each_element(&iface
->data
, data
, node
)
773 blobmsg_add_blob(&b
, data
->data
);
775 blobmsg_close_table(&b
, a
);
777 if (!list_empty(&iface
->errors
))
778 netifd_add_interface_errors(&b
, iface
);
782 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
783 struct ubus_request_data
*req
, const char *method
,
784 struct blob_attr
*msg
)
786 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
788 blob_buf_init(&b
, 0);
789 netifd_dump_status(iface
);
790 ubus_send_reply(ctx
, req
, b
.head
);
797 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
798 struct ubus_request_data
*req
, const char *method
,
799 struct blob_attr
*msg
)
801 blob_buf_init(&b
, 0);
802 void *a
= blobmsg_open_array(&b
, "interface");
804 struct interface
*iface
;
805 vlist_for_each_element(&interfaces
, iface
, node
) {
806 void *i
= blobmsg_open_table(&b
, NULL
);
807 blobmsg_add_string(&b
, "interface", iface
->name
);
808 netifd_dump_status(iface
);
809 blobmsg_close_table(&b
, i
);
812 blobmsg_close_array(&b
, a
);
813 ubus_send_reply(ctx
, req
, b
.head
);
824 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
825 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
826 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
830 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
831 struct ubus_request_data
*req
, const char *method
,
832 struct blob_attr
*msg
)
834 struct blob_attr
*tb
[__DEV_LINK_MAX
];
835 struct blob_attr
*cur
;
836 struct interface
*iface
;
837 bool add
= !strncmp(method
, "add", 3);
838 bool link_ext
= true;
840 iface
= container_of(obj
, struct interface
, ubus
);
842 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
844 if (!tb
[DEV_LINK_NAME
])
845 return UBUS_STATUS_INVALID_ARGUMENT
;
847 cur
= tb
[DEV_LINK_EXT
];
849 link_ext
= blobmsg_get_bool(cur
);
851 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]), add
, link_ext
);
856 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
857 struct ubus_request_data
*req
, const char *method
,
858 struct blob_attr
*msg
)
860 struct interface
*iface
;
862 iface
= container_of(obj
, struct interface
, ubus
);
864 if (!iface
->proto
|| !iface
->proto
->notify
)
865 return UBUS_STATUS_NOT_SUPPORTED
;
867 return iface
->proto
->notify(iface
->proto
, msg
);
871 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
873 struct interface
*iface
;
875 iface
= container_of(timeout
, struct interface
, remove_timer
);
876 vlist_delete(&interfaces
, &iface
->node
);
880 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
881 struct ubus_request_data
*req
, const char *method
,
882 struct blob_attr
*msg
)
884 struct interface
*iface
;
886 iface
= container_of(obj
, struct interface
, ubus
);
887 if (iface
->remove_timer
.cb
)
888 return UBUS_STATUS_INVALID_ARGUMENT
;
890 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
891 uloop_timeout_set(&iface
->remove_timer
, 100);
896 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
897 struct ubus_request_data
*req
, const char *method
,
898 struct blob_attr
*msg
)
900 struct interface
*iface
;
902 const struct device_hotplug_ops
*ops
;
904 iface
= container_of(obj
, struct interface
, ubus
);
905 dev
= iface
->main_dev
.dev
;
909 ops
= dev
->hotplug_ops
;
913 return ops
->prepare(dev
);
917 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
918 struct ubus_request_data
*req
, const char *method
,
919 struct blob_attr
*msg
)
921 struct interface
*iface
;
923 iface
= container_of(obj
, struct interface
, ubus
);
925 return interface_parse_data(iface
, msg
);
928 static struct ubus_method iface_object_methods
[] = {
929 { .name
= "up", .handler
= netifd_handle_up
},
930 { .name
= "down", .handler
= netifd_handle_down
},
931 { .name
= "renew", .handler
= netifd_handle_renew
},
932 { .name
= "status", .handler
= netifd_handle_status
},
933 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
934 { .name
= "dump", .handler
= netifd_handle_dump
},
935 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
936 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
937 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
938 { .name
= "remove", .handler
= netifd_iface_remove
},
939 { .name
= "set_data", .handler
= netifd_handle_set_data
},
942 static struct ubus_object_type iface_object_type
=
943 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
946 static struct ubus_object iface_object
= {
947 .name
= "network.interface",
948 .type
= &iface_object_type
,
949 .n_methods
= ARRAY_SIZE(iface_object_methods
),
952 static void netifd_add_object(struct ubus_object
*obj
)
954 int ret
= ubus_add_object(ubus_ctx
, obj
);
957 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
960 static const struct blobmsg_policy iface_policy
= {
962 .type
= BLOBMSG_TYPE_STRING
,
966 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
967 struct ubus_request_data
*req
, const char *method
,
968 struct blob_attr
*msg
)
970 struct interface
*iface
;
971 struct blob_attr
*tb
;
974 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
976 return UBUS_STATUS_INVALID_ARGUMENT
;
978 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
980 return UBUS_STATUS_NOT_FOUND
;
982 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
985 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
988 cb
= iface_object_methods
[i
].handler
;
989 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
992 return UBUS_STATUS_INVALID_ARGUMENT
;
995 static void netifd_add_iface_object(void)
997 struct ubus_method
*methods
;
1000 methods
= calloc(1, sizeof(iface_object_methods
));
1004 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1005 iface_object
.methods
= methods
;
1007 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1008 if (methods
[i
].handler
== netifd_handle_dump
)
1011 methods
[i
].handler
= netifd_handle_iface
;
1012 methods
[i
].policy
= &iface_policy
;
1013 methods
[i
].n_policy
= 1;
1015 netifd_add_object(&iface_object
);
1018 static struct wireless_device
*
1019 get_wdev(struct blob_attr
*msg
, int *ret
)
1021 struct blobmsg_policy wdev_policy
= {
1023 .type
= BLOBMSG_TYPE_STRING
,
1025 struct blob_attr
*dev_attr
;
1026 struct wireless_device
*wdev
= NULL
;
1029 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1031 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1035 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1037 *ret
= UBUS_STATUS_NOT_FOUND
;
1046 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1047 struct ubus_request_data
*req
, const char *method
,
1048 struct blob_attr
*msg
)
1050 struct wireless_device
*wdev
;
1053 wdev
= get_wdev(msg
, &ret
);
1054 if (ret
== UBUS_STATUS_NOT_FOUND
)
1058 wireless_device_set_up(wdev
);
1060 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1061 wireless_device_set_up(wdev
);
1068 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1069 struct ubus_request_data
*req
, const char *method
,
1070 struct blob_attr
*msg
)
1072 struct wireless_device
*wdev
;
1075 wdev
= get_wdev(msg
, &ret
);
1076 if (ret
== UBUS_STATUS_NOT_FOUND
)
1080 wireless_device_set_down(wdev
);
1082 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1083 wireless_device_set_down(wdev
);
1090 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1091 struct ubus_request_data
*req
, const char *method
,
1092 struct blob_attr
*msg
)
1094 struct wireless_device
*wdev
;
1097 wdev
= get_wdev(msg
, &ret
);
1098 if (ret
== UBUS_STATUS_NOT_FOUND
)
1101 blob_buf_init(&b
, 0);
1103 wireless_device_status(wdev
, &b
);
1105 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1106 wireless_device_status(wdev
, &b
);
1108 ubus_send_reply(ctx
, req
, b
.head
);
1113 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1114 struct ubus_request_data
*req
, const char *method
,
1115 struct blob_attr
*msg
)
1117 struct wireless_device
*wdev
;
1120 wdev
= get_wdev(msg
, &ret
);
1121 if (ret
== UBUS_STATUS_NOT_FOUND
)
1124 blob_buf_init(&b
, 0);
1126 wireless_device_get_validate(wdev
, &b
);
1128 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1129 wireless_device_get_validate(wdev
, &b
);
1131 ubus_send_reply(ctx
, req
, b
.head
);
1136 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1137 struct ubus_request_data
*req
, const char *method
,
1138 struct blob_attr
*msg
)
1140 struct wireless_device
*wdev
;
1143 wdev
= get_wdev(msg
, &ret
);
1147 return wireless_device_notify(wdev
, msg
, req
);
1150 static struct ubus_method wireless_object_methods
[] = {
1151 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1152 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1153 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1154 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1155 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1158 static struct ubus_object_type wireless_object_type
=
1159 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1162 static struct ubus_object wireless_object
= {
1163 .name
= "network.wireless",
1164 .type
= &wireless_object_type
,
1165 .methods
= wireless_object_methods
,
1166 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1170 netifd_ubus_init(const char *path
)
1175 ubus_ctx
= ubus_connect(path
);
1179 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1180 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1181 netifd_ubus_add_fd();
1183 netifd_add_object(&main_object
);
1184 netifd_add_object(&dev_object
);
1185 netifd_add_object(&wireless_object
);
1186 netifd_add_iface_object();
1192 netifd_ubus_done(void)
1194 ubus_free(ubus_ctx
);
1198 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1200 blob_buf_init(&b
, 0);
1201 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1202 blobmsg_add_string(&b
, "interface", iface
->name
);
1203 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1207 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1209 const char *event
= (up
) ? "interface.update" : "interface.down";
1210 blob_buf_init(&b
, 0);
1211 blobmsg_add_string(&b
, "interface", iface
->name
);
1212 netifd_dump_status(iface
);
1213 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1214 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1218 netifd_ubus_add_interface(struct interface
*iface
)
1220 struct ubus_object
*obj
= &iface
->ubus
;
1223 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1227 obj
->type
= &iface_object_type
;
1228 obj
->methods
= iface_object_methods
;
1229 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1230 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1231 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1238 netifd_ubus_remove_interface(struct interface
*iface
)
1240 if (!iface
->ubus
.name
)
1243 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1244 free((void *) iface
->ubus
.name
);