2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <arpa/inet.h>
21 #include "interface.h"
27 struct ubus_context
*ubus_ctx
= NULL
;
28 static struct blob_buf b
;
29 static const char *ubus_path
;
34 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
35 struct ubus_request_data
*req
, const char *method
,
36 struct blob_attr
*msg
)
43 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
44 struct ubus_request_data
*req
, const char *method
,
45 struct blob_attr
*msg
)
58 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
59 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
60 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
61 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
65 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
66 struct ubus_request_data
*req
, const char *method
,
67 struct blob_attr
*msg
)
69 struct blob_attr
*tb
[__HR_MAX
];
70 struct interface
*iface
= NULL
;
74 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
76 return UBUS_STATUS_INVALID_ARGUMENT
;
79 v6
= blobmsg_get_bool(tb
[HR_V6
]);
82 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
84 memset(&a
, 0, sizeof(a
));
85 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
86 return UBUS_STATUS_INVALID_ARGUMENT
;
89 iface
= interface_ip_add_target_route(&a
, v6
, iface
);
91 return UBUS_STATUS_NOT_FOUND
;
94 blobmsg_add_string(&b
, "interface", iface
->name
);
95 ubus_send_reply(ctx
, req
, b
.head
);
101 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
102 struct ubus_request_data
*req
, const char *method
,
103 struct blob_attr
*msg
)
105 blob_buf_init(&b
, 0);
106 proto_dump_handlers(&b
);
107 ubus_send_reply(ctx
, req
, b
.head
);
118 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
119 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
123 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
124 struct ubus_request_data
*req
, const char *method
,
125 struct blob_attr
*msg
)
127 struct blob_attr
*tb
[__DI_MAX
];
128 struct interface
*iface
;
129 struct blob_attr
*config
;
132 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
135 return UBUS_STATUS_INVALID_ARGUMENT
;
137 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
139 iface
= interface_alloc(name
, msg
);
141 return UBUS_STATUS_UNKNOWN_ERROR
;
143 config
= blob_memdup(msg
);
147 interface_add(iface
, config
);
149 // need to look up the interface name again, in case of config update
150 // the pointer will have changed
151 iface
= vlist_find(&interfaces
, name
, iface
, node
);
153 return UBUS_STATUS_UNKNOWN_ERROR
;
155 // Set interface as dynamic
156 interface_set_dynamic(iface
);
158 dev
= iface
->main_dev
.dev
;
159 if (!dev
|| !dev
->default_config
)
160 return UBUS_STATUS_UNKNOWN_ERROR
;
162 return UBUS_STATUS_OK
;
166 return UBUS_STATUS_UNKNOWN_ERROR
;
170 netifd_del_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
171 struct ubus_request_data
*req
, const char *method
,
172 struct blob_attr
*msg
)
174 struct blob_attr
*tb
[__DI_MAX
];
175 struct interface
*iface
;
177 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
180 return UBUS_STATUS_INVALID_ARGUMENT
;
182 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
183 iface
= vlist_find(&interfaces
, name
, iface
, node
);
186 return UBUS_STATUS_NOT_FOUND
;
187 else if (!iface
->dynamic
)
188 return UBUS_STATUS_INVALID_COMMAND
;
190 vlist_delete(&interfaces
, &iface
->node
);
191 return UBUS_STATUS_OK
;
194 static struct ubus_method main_object_methods
[] = {
195 { .name
= "restart", .handler
= netifd_handle_restart
},
196 { .name
= "reload", .handler
= netifd_handle_reload
},
197 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
198 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
199 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
200 UBUS_METHOD("del_dynamic", netifd_del_dynamic
, dynamic_policy
),
203 static struct ubus_object_type main_object_type
=
204 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
206 static struct ubus_object main_object
= {
208 .type
= &main_object_type
,
209 .methods
= main_object_methods
,
210 .n_methods
= ARRAY_SIZE(main_object_methods
),
218 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
219 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
223 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
224 struct ubus_request_data
*req
, const char *method
,
225 struct blob_attr
*msg
)
227 struct device
*dev
= NULL
;
228 struct blob_attr
*tb
[__DEV_MAX
];
230 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
233 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
235 return UBUS_STATUS_INVALID_ARGUMENT
;
238 blob_buf_init(&b
, 0);
239 device_dump_status(&b
, dev
);
240 ubus_send_reply(ctx
, req
, b
.head
);
251 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
252 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
253 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
257 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
258 struct ubus_request_data
*req
, const char *method
,
259 struct blob_attr
*msg
)
261 struct device
*dev
= NULL
;
262 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
263 struct blob_attr
*cur
;
266 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
268 if (!tb
[ALIAS_ATTR_ALIAS
])
269 return UBUS_STATUS_INVALID_ARGUMENT
;
271 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
272 dev
= device_get(blobmsg_data(cur
), true);
274 return UBUS_STATUS_NOT_FOUND
;
277 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
278 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
281 if (!blobmsg_check_attr(cur
, NULL
))
284 alias_notify_device(blobmsg_data(cur
), dev
);
289 device_free_unused(dev
);
290 return UBUS_STATUS_INVALID_ARGUMENT
;
299 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
300 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
301 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
305 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
306 struct ubus_request_data
*req
, const char *method
,
307 struct blob_attr
*msg
)
309 struct device
*dev
= NULL
;
310 struct blob_attr
*tb
[__DEV_STATE_MAX
];
311 struct blob_attr
*cur
;
313 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
315 cur
= tb
[DEV_STATE_NAME
];
317 return UBUS_STATUS_INVALID_ARGUMENT
;
319 dev
= device_get(blobmsg_data(cur
), false);
321 return UBUS_STATUS_NOT_FOUND
;
323 cur
= tb
[DEV_STATE_DEFER
];
325 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
330 static struct ubus_method dev_object_methods
[] = {
331 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
332 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
333 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
336 static struct ubus_object_type dev_object_type
=
337 UBUS_OBJECT_TYPE("device", dev_object_methods
);
339 static struct ubus_object dev_object
= {
340 .name
= "network.device",
341 .type
= &dev_object_type
,
342 .methods
= dev_object_methods
,
343 .n_methods
= ARRAY_SIZE(dev_object_methods
),
347 netifd_ubus_add_fd(void)
349 ubus_add_uloop(ubus_ctx
);
350 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
354 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
356 static struct uloop_timeout retry
= {
357 .cb
= netifd_ubus_reconnect_timer
,
361 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
362 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
363 uloop_timeout_set(&retry
, t
* 1000);
367 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
368 netifd_ubus_add_fd();
372 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
374 netifd_ubus_reconnect_timer(NULL
);
377 /* per-interface object */
380 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
381 struct ubus_request_data
*req
, const char *method
,
382 struct blob_attr
*msg
)
384 struct interface
*iface
;
386 iface
= container_of(obj
, struct interface
, ubus
);
387 interface_set_up(iface
);
393 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
394 struct ubus_request_data
*req
, const char *method
,
395 struct blob_attr
*msg
)
397 struct interface
*iface
;
399 iface
= container_of(obj
, struct interface
, ubus
);
400 interface_set_down(iface
);
406 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
408 struct interface_error
*error
;
412 e
= blobmsg_open_array(b
, "errors");
413 list_for_each_entry(error
, &iface
->errors
, list
) {
414 e2
= blobmsg_open_table(b
, NULL
);
416 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
417 blobmsg_add_string(b
, "code", error
->code
);
418 if (error
->data
[0]) {
419 e3
= blobmsg_open_array(b
, "data");
420 for (i
= 0; error
->data
[i
]; i
++)
421 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
422 blobmsg_close_array(b
, e3
);
425 blobmsg_close_table(b
, e2
);
427 blobmsg_close_array(b
, e
);
431 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
,
434 struct device_addr
*addr
;
440 time_t now
= system_get_rtime();
441 vlist_for_each_element(&ip
->addr
, addr
, node
) {
442 if (addr
->enabled
!= enabled
)
445 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
450 if (af
!= (v6
? AF_INET6
: AF_INET
))
453 a
= blobmsg_open_table(&b
, NULL
);
455 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
456 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
457 blobmsg_add_string_buffer(&b
);
459 blobmsg_add_u32(&b
, "mask", addr
->mask
);
461 if (addr
->preferred_until
) {
462 int preferred
= addr
->preferred_until
- now
;
465 blobmsg_add_u32(&b
, "preferred", preferred
);
468 if (addr
->valid_until
)
469 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
472 blobmsg_add_string(&b
, "class", addr
->pclass
);
474 blobmsg_close_table(&b
, a
);
479 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
481 struct device_route
*route
;
487 time_t now
= system_get_rtime();
488 vlist_for_each_element(&ip
->route
, route
, node
) {
489 if (route
->enabled
!= enabled
)
492 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
495 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
500 r
= blobmsg_open_table(&b
, NULL
);
502 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
503 inet_ntop(af
, &route
->addr
, buf
, buflen
);
504 blobmsg_add_string_buffer(&b
);
506 blobmsg_add_u32(&b
, "mask", route
->mask
);
508 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
509 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
510 blobmsg_add_string_buffer(&b
);
512 if (route
->flags
& DEVROUTE_TYPE
)
513 blobmsg_add_u32(&b
, "type", route
->type
);
515 if (route
->flags
& DEVROUTE_MTU
)
516 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
518 if (route
->flags
& DEVROUTE_METRIC
)
519 blobmsg_add_u32(&b
, "metric", route
->metric
);
521 if (route
->flags
& DEVROUTE_TABLE
)
522 blobmsg_add_u32(&b
, "table", route
->table
);
524 if (route
->valid_until
)
525 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
527 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
528 inet_ntop(af
, &route
->source
, buf
, buflen
);
529 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
530 blobmsg_add_string_buffer(&b
);
532 blobmsg_close_table(&b
, r
);
538 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
540 struct device_prefix
*prefix
;
543 const int buflen
= INET6_ADDRSTRLEN
;
545 time_t now
= system_get_rtime();
546 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
547 a
= blobmsg_open_table(&b
, NULL
);
549 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
550 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
551 blobmsg_add_string_buffer(&b
);
553 blobmsg_add_u32(&b
, "mask", prefix
->length
);
555 if (prefix
->preferred_until
) {
556 int preferred
= prefix
->preferred_until
- now
;
559 blobmsg_add_u32(&b
, "preferred", preferred
);
562 if (prefix
->valid_until
)
563 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
565 blobmsg_add_string(&b
, "class", prefix
->pclass
);
567 c
= blobmsg_open_table(&b
, "assigned");
568 struct device_prefix_assignment
*assign
;
569 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
570 if (!assign
->name
[0])
573 struct in6_addr addr
= prefix
->addr
;
574 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
576 void *d
= blobmsg_open_table(&b
, assign
->name
);
578 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
579 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
580 blobmsg_add_string_buffer(&b
);
582 blobmsg_add_u32(&b
, "mask", assign
->length
);
584 blobmsg_close_table(&b
, d
);
586 blobmsg_close_table(&b
, c
);
588 blobmsg_close_table(&b
, a
);
594 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
598 const int buflen
= INET6_ADDRSTRLEN
;
599 time_t now
= system_get_rtime();
601 struct device_prefix
*prefix
;
602 list_for_each_entry(prefix
, &prefixes
, head
) {
603 struct device_prefix_assignment
*assign
;
604 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
605 if (strcmp(assign
->name
, iface
->name
))
608 struct in6_addr addr
= prefix
->addr
;
609 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
611 a
= blobmsg_open_table(&b
, NULL
);
613 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
614 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
615 blobmsg_add_string_buffer(&b
);
617 blobmsg_add_u32(&b
, "mask", assign
->length
);
619 if (prefix
->preferred_until
) {
620 int preferred
= prefix
->preferred_until
- now
;
623 blobmsg_add_u32(&b
, "preferred", preferred
);
626 if (prefix
->valid_until
)
627 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
629 blobmsg_close_table(&b
, a
);
636 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
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
,
657 struct dns_search_domain
*dns
;
659 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
660 if (ip
->no_dns
== enabled
)
663 blobmsg_add_string(&b
, NULL
, dns
->name
);
668 netifd_dump_status(struct interface
*iface
)
670 struct interface_data
*data
;
674 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
675 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
676 blobmsg_add_u8(&b
, "available", iface
->available
);
677 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
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
&&
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_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
716 a
= blobmsg_open_array(&b
, "ipv4-address");
717 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
718 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
719 blobmsg_close_array(&b
, a
);
720 a
= blobmsg_open_array(&b
, "ipv6-address");
721 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
722 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
723 blobmsg_close_array(&b
, a
);
724 a
= blobmsg_open_array(&b
, "ipv6-prefix");
725 interface_ip_dump_prefix_list(&iface
->config_ip
);
726 interface_ip_dump_prefix_list(&iface
->proto_ip
);
727 blobmsg_close_array(&b
, a
);
728 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
729 interface_ip_dump_prefix_assignment_list(iface
);
730 blobmsg_close_array(&b
, a
);
731 a
= blobmsg_open_array(&b
, "route");
732 interface_ip_dump_route_list(&iface
->config_ip
, true);
733 interface_ip_dump_route_list(&iface
->proto_ip
, true);
734 blobmsg_close_array(&b
, a
);
735 a
= blobmsg_open_array(&b
, "dns-server");
736 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
737 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
738 blobmsg_close_array(&b
, a
);
739 a
= blobmsg_open_array(&b
, "dns-search");
740 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
741 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
742 blobmsg_close_array(&b
, a
);
744 inactive
= blobmsg_open_table(&b
, "inactive");
745 a
= blobmsg_open_array(&b
, "ipv4-address");
746 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
747 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
748 blobmsg_close_array(&b
, a
);
749 a
= blobmsg_open_array(&b
, "ipv6-address");
750 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
751 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
752 blobmsg_close_array(&b
, a
);
753 a
= blobmsg_open_array(&b
, "route");
754 interface_ip_dump_route_list(&iface
->config_ip
, false);
755 interface_ip_dump_route_list(&iface
->proto_ip
, false);
756 blobmsg_close_array(&b
, a
);
757 a
= blobmsg_open_array(&b
, "dns-server");
758 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
759 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
760 blobmsg_close_array(&b
, a
);
761 a
= blobmsg_open_array(&b
, "dns-search");
762 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
763 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
764 blobmsg_close_array(&b
, a
);
765 blobmsg_close_table(&b
, inactive
);
768 a
= blobmsg_open_table(&b
, "data");
769 avl_for_each_element(&iface
->data
, data
, node
)
770 blobmsg_add_blob(&b
, data
->data
);
772 blobmsg_close_table(&b
, a
);
774 if (!list_empty(&iface
->errors
))
775 netifd_add_interface_errors(&b
, iface
);
779 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
780 struct ubus_request_data
*req
, const char *method
,
781 struct blob_attr
*msg
)
783 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
785 blob_buf_init(&b
, 0);
786 netifd_dump_status(iface
);
787 ubus_send_reply(ctx
, req
, b
.head
);
794 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
795 struct ubus_request_data
*req
, const char *method
,
796 struct blob_attr
*msg
)
798 blob_buf_init(&b
, 0);
799 void *a
= blobmsg_open_array(&b
, "interface");
801 struct interface
*iface
;
802 vlist_for_each_element(&interfaces
, iface
, node
) {
803 void *i
= blobmsg_open_table(&b
, NULL
);
804 blobmsg_add_string(&b
, "interface", iface
->name
);
805 netifd_dump_status(iface
);
806 blobmsg_close_table(&b
, i
);
809 blobmsg_close_array(&b
, a
);
810 ubus_send_reply(ctx
, req
, b
.head
);
821 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
822 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
823 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
827 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
828 struct ubus_request_data
*req
, const char *method
,
829 struct blob_attr
*msg
)
831 struct blob_attr
*tb
[__DEV_LINK_MAX
];
832 struct blob_attr
*cur
;
833 struct interface
*iface
;
834 bool add
= !strncmp(method
, "add", 3);
835 bool link_ext
= true;
837 iface
= container_of(obj
, struct interface
, ubus
);
839 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
841 if (!tb
[DEV_LINK_NAME
])
842 return UBUS_STATUS_INVALID_ARGUMENT
;
844 cur
= tb
[DEV_LINK_EXT
];
846 link_ext
= !!blobmsg_get_u8(cur
);
848 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]), add
, link_ext
);
853 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
854 struct ubus_request_data
*req
, const char *method
,
855 struct blob_attr
*msg
)
857 struct interface
*iface
;
859 iface
= container_of(obj
, struct interface
, ubus
);
861 if (!iface
->proto
|| !iface
->proto
->notify
)
862 return UBUS_STATUS_NOT_SUPPORTED
;
864 return iface
->proto
->notify(iface
->proto
, msg
);
868 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
870 struct interface
*iface
;
872 iface
= container_of(timeout
, struct interface
, remove_timer
);
873 vlist_delete(&interfaces
, &iface
->node
);
877 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
878 struct ubus_request_data
*req
, const char *method
,
879 struct blob_attr
*msg
)
881 struct interface
*iface
;
883 iface
= container_of(obj
, struct interface
, ubus
);
884 if (iface
->remove_timer
.cb
)
885 return UBUS_STATUS_INVALID_ARGUMENT
;
887 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
888 uloop_timeout_set(&iface
->remove_timer
, 100);
893 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
894 struct ubus_request_data
*req
, const char *method
,
895 struct blob_attr
*msg
)
897 struct interface
*iface
;
899 const struct device_hotplug_ops
*ops
;
901 iface
= container_of(obj
, struct interface
, ubus
);
902 dev
= iface
->main_dev
.dev
;
906 ops
= dev
->hotplug_ops
;
910 return ops
->prepare(dev
);
914 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
915 struct ubus_request_data
*req
, const char *method
,
916 struct blob_attr
*msg
)
918 struct interface
*iface
;
919 struct blob_attr
*cur
;
922 iface
= container_of(obj
, struct interface
, ubus
);
924 blob_for_each_attr(cur
, msg
, rem
) {
925 ret
= interface_add_data(iface
, cur
);
933 static struct ubus_method iface_object_methods
[] = {
934 { .name
= "up", .handler
= netifd_handle_up
},
935 { .name
= "down", .handler
= netifd_handle_down
},
936 { .name
= "status", .handler
= netifd_handle_status
},
937 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
938 { .name
= "dump", .handler
= netifd_handle_dump
},
939 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
940 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
941 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
942 { .name
= "remove", .handler
= netifd_iface_remove
},
943 { .name
= "set_data", .handler
= netifd_handle_set_data
},
946 static struct ubus_object_type iface_object_type
=
947 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
950 static struct ubus_object iface_object
= {
951 .name
= "network.interface",
952 .type
= &iface_object_type
,
953 .n_methods
= ARRAY_SIZE(iface_object_methods
),
956 static void netifd_add_object(struct ubus_object
*obj
)
958 int ret
= ubus_add_object(ubus_ctx
, obj
);
961 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
964 static const struct blobmsg_policy iface_policy
= {
966 .type
= BLOBMSG_TYPE_STRING
,
970 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
971 struct ubus_request_data
*req
, const char *method
,
972 struct blob_attr
*msg
)
974 struct interface
*iface
;
975 struct blob_attr
*tb
;
978 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
980 return UBUS_STATUS_INVALID_ARGUMENT
;
982 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
984 return UBUS_STATUS_NOT_FOUND
;
986 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
989 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
992 cb
= iface_object_methods
[i
].handler
;
993 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
996 return UBUS_STATUS_INVALID_ARGUMENT
;
999 static void netifd_add_iface_object(void)
1001 struct ubus_method
*methods
;
1004 methods
= calloc(1, sizeof(iface_object_methods
));
1005 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1006 iface_object
.methods
= methods
;
1008 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1009 if (methods
[i
].handler
== netifd_handle_dump
)
1012 methods
[i
].handler
= netifd_handle_iface
;
1013 methods
[i
].policy
= &iface_policy
;
1014 methods
[i
].n_policy
= 1;
1016 netifd_add_object(&iface_object
);
1019 static struct wireless_device
*
1020 get_wdev(struct blob_attr
*msg
, int *ret
)
1022 struct blobmsg_policy wdev_policy
= {
1024 .type
= BLOBMSG_TYPE_STRING
,
1026 struct blob_attr
*dev_attr
;
1027 struct wireless_device
*wdev
= NULL
;
1030 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1032 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1036 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1038 *ret
= UBUS_STATUS_NOT_FOUND
;
1047 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1048 struct ubus_request_data
*req
, const char *method
,
1049 struct blob_attr
*msg
)
1051 struct wireless_device
*wdev
;
1054 wdev
= get_wdev(msg
, &ret
);
1055 if (ret
== UBUS_STATUS_NOT_FOUND
)
1059 wireless_device_set_up(wdev
);
1061 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1062 wireless_device_set_up(wdev
);
1069 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1070 struct ubus_request_data
*req
, const char *method
,
1071 struct blob_attr
*msg
)
1073 struct wireless_device
*wdev
;
1076 wdev
= get_wdev(msg
, &ret
);
1077 if (ret
== UBUS_STATUS_NOT_FOUND
)
1081 wireless_device_set_down(wdev
);
1083 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1084 wireless_device_set_down(wdev
);
1091 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1092 struct ubus_request_data
*req
, const char *method
,
1093 struct blob_attr
*msg
)
1095 struct wireless_device
*wdev
;
1098 wdev
= get_wdev(msg
, &ret
);
1099 if (ret
== UBUS_STATUS_NOT_FOUND
)
1102 blob_buf_init(&b
, 0);
1104 wireless_device_status(wdev
, &b
);
1106 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1107 wireless_device_status(wdev
, &b
);
1109 ubus_send_reply(ctx
, req
, b
.head
);
1114 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1115 struct ubus_request_data
*req
, const char *method
,
1116 struct blob_attr
*msg
)
1118 struct wireless_device
*wdev
;
1121 wdev
= get_wdev(msg
, &ret
);
1122 if (ret
== UBUS_STATUS_NOT_FOUND
)
1125 blob_buf_init(&b
, 0);
1127 wireless_device_get_validate(wdev
, &b
);
1129 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1130 wireless_device_get_validate(wdev
, &b
);
1132 ubus_send_reply(ctx
, req
, b
.head
);
1137 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1138 struct ubus_request_data
*req
, const char *method
,
1139 struct blob_attr
*msg
)
1141 struct wireless_device
*wdev
;
1144 wdev
= get_wdev(msg
, &ret
);
1148 return wireless_device_notify(wdev
, msg
, req
);
1151 static struct ubus_method wireless_object_methods
[] = {
1152 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1153 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1154 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1155 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1156 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1159 static struct ubus_object_type wireless_object_type
=
1160 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1163 static struct ubus_object wireless_object
= {
1164 .name
= "network.wireless",
1165 .type
= &wireless_object_type
,
1166 .methods
= wireless_object_methods
,
1167 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1171 netifd_ubus_init(const char *path
)
1176 ubus_ctx
= ubus_connect(path
);
1180 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1181 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1182 netifd_ubus_add_fd();
1184 netifd_add_object(&main_object
);
1185 netifd_add_object(&dev_object
);
1186 netifd_add_object(&wireless_object
);
1187 netifd_add_iface_object();
1193 netifd_ubus_done(void)
1195 ubus_free(ubus_ctx
);
1199 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1201 blob_buf_init(&b
, 0);
1202 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1203 blobmsg_add_string(&b
, "interface", iface
->name
);
1204 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1208 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1210 const char *event
= (up
) ? "interface.update" : "interface.down";
1211 blob_buf_init(&b
, 0);
1212 blobmsg_add_string(&b
, "interface", iface
->name
);
1213 netifd_dump_status(iface
);
1214 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1215 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1219 netifd_ubus_add_interface(struct interface
*iface
)
1221 struct ubus_object
*obj
= &iface
->ubus
;
1224 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1228 obj
->type
= &iface_object_type
;
1229 obj
->methods
= iface_object_methods
;
1230 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1231 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1232 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1239 netifd_ubus_remove_interface(struct interface
*iface
)
1241 if (!iface
->ubus
.name
)
1244 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1245 free((void *) iface
->ubus
.name
);