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 iface
->device_config
= true;
145 config
= blob_memdup(msg
);
149 interface_add(iface
, config
);
151 // need to look up the interface name again, in case of config update
152 // the pointer will have changed
153 iface
= vlist_find(&interfaces
, name
, iface
, node
);
155 return UBUS_STATUS_UNKNOWN_ERROR
;
157 // Set interface as dynamic
158 interface_set_dynamic(iface
);
160 dev
= iface
->main_dev
.dev
;
161 if (!dev
|| !dev
->default_config
)
162 return UBUS_STATUS_UNKNOWN_ERROR
;
164 device_set_config(dev
, dev
->type
, msg
);
165 return UBUS_STATUS_OK
;
169 return UBUS_STATUS_UNKNOWN_ERROR
;
172 static struct ubus_method main_object_methods
[] = {
173 { .name
= "restart", .handler
= netifd_handle_restart
},
174 { .name
= "reload", .handler
= netifd_handle_reload
},
175 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
176 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
177 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
180 static struct ubus_object_type main_object_type
=
181 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
183 static struct ubus_object main_object
= {
185 .type
= &main_object_type
,
186 .methods
= main_object_methods
,
187 .n_methods
= ARRAY_SIZE(main_object_methods
),
195 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
196 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
200 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
201 struct ubus_request_data
*req
, const char *method
,
202 struct blob_attr
*msg
)
204 struct device
*dev
= NULL
;
205 struct blob_attr
*tb
[__DEV_MAX
];
207 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
210 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
212 return UBUS_STATUS_INVALID_ARGUMENT
;
215 blob_buf_init(&b
, 0);
216 device_dump_status(&b
, dev
);
217 ubus_send_reply(ctx
, req
, b
.head
);
228 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
229 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
230 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
234 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
235 struct ubus_request_data
*req
, const char *method
,
236 struct blob_attr
*msg
)
238 struct device
*dev
= NULL
;
239 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
240 struct blob_attr
*cur
;
243 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
245 if (!tb
[ALIAS_ATTR_ALIAS
])
246 return UBUS_STATUS_INVALID_ARGUMENT
;
248 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
249 dev
= device_get(blobmsg_data(cur
), true);
251 return UBUS_STATUS_NOT_FOUND
;
254 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
255 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
258 if (!blobmsg_check_attr(cur
, NULL
))
261 alias_notify_device(blobmsg_data(cur
), dev
);
266 device_free_unused(dev
);
267 return UBUS_STATUS_INVALID_ARGUMENT
;
276 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
277 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
278 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
282 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
283 struct ubus_request_data
*req
, const char *method
,
284 struct blob_attr
*msg
)
286 struct device
*dev
= NULL
;
287 struct blob_attr
*tb
[__DEV_STATE_MAX
];
288 struct blob_attr
*cur
;
290 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
292 cur
= tb
[DEV_STATE_NAME
];
294 return UBUS_STATUS_INVALID_ARGUMENT
;
296 dev
= device_get(blobmsg_data(cur
), false);
298 return UBUS_STATUS_NOT_FOUND
;
300 cur
= tb
[DEV_STATE_DEFER
];
302 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
307 static struct ubus_method dev_object_methods
[] = {
308 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
309 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
310 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
313 static struct ubus_object_type dev_object_type
=
314 UBUS_OBJECT_TYPE("device", dev_object_methods
);
316 static struct ubus_object dev_object
= {
317 .name
= "network.device",
318 .type
= &dev_object_type
,
319 .methods
= dev_object_methods
,
320 .n_methods
= ARRAY_SIZE(dev_object_methods
),
324 netifd_ubus_add_fd(void)
326 ubus_add_uloop(ubus_ctx
);
327 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
331 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
333 static struct uloop_timeout retry
= {
334 .cb
= netifd_ubus_reconnect_timer
,
338 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
339 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
340 uloop_timeout_set(&retry
, t
* 1000);
344 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
345 netifd_ubus_add_fd();
349 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
351 netifd_ubus_reconnect_timer(NULL
);
354 /* per-interface object */
357 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
358 struct ubus_request_data
*req
, const char *method
,
359 struct blob_attr
*msg
)
361 struct interface
*iface
;
363 iface
= container_of(obj
, struct interface
, ubus
);
364 interface_set_up(iface
);
370 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
371 struct ubus_request_data
*req
, const char *method
,
372 struct blob_attr
*msg
)
374 struct interface
*iface
;
376 iface
= container_of(obj
, struct interface
, ubus
);
377 interface_set_down(iface
);
383 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
385 struct interface_error
*error
;
389 e
= blobmsg_open_array(b
, "errors");
390 list_for_each_entry(error
, &iface
->errors
, list
) {
391 e2
= blobmsg_open_table(b
, NULL
);
393 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
394 blobmsg_add_string(b
, "code", error
->code
);
395 if (error
->data
[0]) {
396 e3
= blobmsg_open_array(b
, "data");
397 for (i
= 0; error
->data
[i
]; i
++)
398 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
399 blobmsg_close_array(b
, e3
);
402 blobmsg_close_table(b
, e2
);
404 blobmsg_close_array(b
, e
);
408 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
,
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
->preferred_until
) {
439 int preferred
= addr
->preferred_until
- now
;
442 blobmsg_add_u32(&b
, "preferred", preferred
);
445 if (addr
->valid_until
)
446 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
449 blobmsg_add_string(&b
, "class", addr
->pclass
);
451 blobmsg_close_table(&b
, a
);
456 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
458 struct device_route
*route
;
464 time_t now
= system_get_rtime();
465 vlist_for_each_element(&ip
->route
, route
, node
) {
466 if (route
->enabled
!= enabled
)
469 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
472 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
477 r
= blobmsg_open_table(&b
, NULL
);
479 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
480 inet_ntop(af
, &route
->addr
, buf
, buflen
);
481 blobmsg_add_string_buffer(&b
);
483 blobmsg_add_u32(&b
, "mask", route
->mask
);
485 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
486 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
487 blobmsg_add_string_buffer(&b
);
489 if (route
->flags
& DEVROUTE_TYPE
)
490 blobmsg_add_u32(&b
, "type", route
->type
);
492 if (route
->flags
& DEVROUTE_MTU
)
493 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
495 if (route
->flags
& DEVROUTE_METRIC
)
496 blobmsg_add_u32(&b
, "metric", route
->metric
);
498 if (route
->flags
& DEVROUTE_TABLE
)
499 blobmsg_add_u32(&b
, "table", route
->table
);
501 if (route
->valid_until
)
502 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
504 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
505 inet_ntop(af
, &route
->source
, buf
, buflen
);
506 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
507 blobmsg_add_string_buffer(&b
);
509 blobmsg_close_table(&b
, r
);
515 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
517 struct device_prefix
*prefix
;
520 const int buflen
= INET6_ADDRSTRLEN
;
522 time_t now
= system_get_rtime();
523 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
524 a
= blobmsg_open_table(&b
, NULL
);
526 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
527 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
528 blobmsg_add_string_buffer(&b
);
530 blobmsg_add_u32(&b
, "mask", prefix
->length
);
532 if (prefix
->preferred_until
) {
533 int preferred
= prefix
->preferred_until
- now
;
536 blobmsg_add_u32(&b
, "preferred", preferred
);
539 if (prefix
->valid_until
)
540 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
542 blobmsg_add_string(&b
, "class", prefix
->pclass
);
544 c
= blobmsg_open_table(&b
, "assigned");
545 struct device_prefix_assignment
*assign
;
546 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
547 if (!assign
->name
[0])
550 struct in6_addr addr
= prefix
->addr
;
551 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
553 void *d
= blobmsg_open_table(&b
, assign
->name
);
555 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
556 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
557 blobmsg_add_string_buffer(&b
);
559 blobmsg_add_u32(&b
, "mask", assign
->length
);
561 blobmsg_close_table(&b
, d
);
563 blobmsg_close_table(&b
, c
);
565 blobmsg_close_table(&b
, a
);
571 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
575 const int buflen
= INET6_ADDRSTRLEN
;
576 time_t now
= system_get_rtime();
578 struct device_prefix
*prefix
;
579 list_for_each_entry(prefix
, &prefixes
, head
) {
580 struct device_prefix_assignment
*assign
;
581 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
582 if (strcmp(assign
->name
, iface
->name
))
585 struct in6_addr addr
= prefix
->addr
;
586 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
588 a
= blobmsg_open_table(&b
, NULL
);
590 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
591 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
592 blobmsg_add_string_buffer(&b
);
594 blobmsg_add_u32(&b
, "mask", assign
->length
);
596 if (prefix
->preferred_until
) {
597 int preferred
= prefix
->preferred_until
- now
;
600 blobmsg_add_u32(&b
, "preferred", preferred
);
603 if (prefix
->valid_until
)
604 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
606 blobmsg_close_table(&b
, a
);
613 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
616 struct dns_server
*dns
;
620 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
621 if (ip
->no_dns
== enabled
)
624 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
625 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
626 blobmsg_add_string_buffer(&b
);
631 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
,
634 struct dns_search_domain
*dns
;
636 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
637 if (ip
->no_dns
== enabled
)
640 blobmsg_add_string(&b
, NULL
, dns
->name
);
645 netifd_dump_status(struct interface
*iface
)
647 struct interface_data
*data
;
651 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
652 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
653 blobmsg_add_u8(&b
, "available", iface
->available
);
654 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
656 if (iface
->state
== IFS_UP
) {
657 time_t cur
= system_get_rtime();
658 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
659 if (iface
->l3_dev
.dev
)
660 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
663 if (iface
->proto_handler
)
664 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
666 dev
= iface
->main_dev
.dev
;
667 if (dev
&& !dev
->hidden
&&
668 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
669 blobmsg_add_string(&b
, "device", dev
->ifname
);
671 if (iface
->state
== IFS_UP
) {
672 if (iface
->updated
) {
673 a
= blobmsg_open_array(&b
, "updated");
675 if (iface
->updated
& IUF_ADDRESS
)
676 blobmsg_add_string(&b
, NULL
, "addresses");
677 if (iface
->updated
& IUF_ROUTE
)
678 blobmsg_add_string(&b
, NULL
, "routes");
679 if (iface
->updated
& IUF_PREFIX
)
680 blobmsg_add_string(&b
, NULL
, "prefixes");
681 if (iface
->updated
& IUF_DATA
)
682 blobmsg_add_string(&b
, NULL
, "data");
684 blobmsg_close_array(&b
, a
);
688 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
690 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
691 blobmsg_add_u32(&b
, "metric", iface
->metric
);
692 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
693 a
= blobmsg_open_array(&b
, "ipv4-address");
694 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
695 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
696 blobmsg_close_array(&b
, a
);
697 a
= blobmsg_open_array(&b
, "ipv6-address");
698 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
699 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
700 blobmsg_close_array(&b
, a
);
701 a
= blobmsg_open_array(&b
, "ipv6-prefix");
702 interface_ip_dump_prefix_list(&iface
->config_ip
);
703 interface_ip_dump_prefix_list(&iface
->proto_ip
);
704 blobmsg_close_array(&b
, a
);
705 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
706 interface_ip_dump_prefix_assignment_list(iface
);
707 blobmsg_close_array(&b
, a
);
708 a
= blobmsg_open_array(&b
, "route");
709 interface_ip_dump_route_list(&iface
->config_ip
, true);
710 interface_ip_dump_route_list(&iface
->proto_ip
, true);
711 blobmsg_close_array(&b
, a
);
712 a
= blobmsg_open_array(&b
, "dns-server");
713 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
714 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
715 blobmsg_close_array(&b
, a
);
716 a
= blobmsg_open_array(&b
, "dns-search");
717 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
718 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
719 blobmsg_close_array(&b
, a
);
721 inactive
= blobmsg_open_table(&b
, "inactive");
722 a
= blobmsg_open_array(&b
, "ipv4-address");
723 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
724 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
725 blobmsg_close_array(&b
, a
);
726 a
= blobmsg_open_array(&b
, "ipv6-address");
727 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
728 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
729 blobmsg_close_array(&b
, a
);
730 a
= blobmsg_open_array(&b
, "route");
731 interface_ip_dump_route_list(&iface
->config_ip
, false);
732 interface_ip_dump_route_list(&iface
->proto_ip
, false);
733 blobmsg_close_array(&b
, a
);
734 a
= blobmsg_open_array(&b
, "dns-server");
735 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
736 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
737 blobmsg_close_array(&b
, a
);
738 a
= blobmsg_open_array(&b
, "dns-search");
739 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
740 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
741 blobmsg_close_array(&b
, a
);
742 blobmsg_close_table(&b
, inactive
);
745 a
= blobmsg_open_table(&b
, "data");
746 avl_for_each_element(&iface
->data
, data
, node
)
747 blobmsg_add_blob(&b
, data
->data
);
749 blobmsg_close_table(&b
, a
);
751 if (!list_empty(&iface
->errors
))
752 netifd_add_interface_errors(&b
, iface
);
756 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
757 struct ubus_request_data
*req
, const char *method
,
758 struct blob_attr
*msg
)
760 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
762 blob_buf_init(&b
, 0);
763 netifd_dump_status(iface
);
764 ubus_send_reply(ctx
, req
, b
.head
);
771 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
772 struct ubus_request_data
*req
, const char *method
,
773 struct blob_attr
*msg
)
775 blob_buf_init(&b
, 0);
776 void *a
= blobmsg_open_array(&b
, "interface");
778 struct interface
*iface
;
779 vlist_for_each_element(&interfaces
, iface
, node
) {
780 void *i
= blobmsg_open_table(&b
, NULL
);
781 blobmsg_add_string(&b
, "interface", iface
->name
);
782 netifd_dump_status(iface
);
783 blobmsg_close_table(&b
, i
);
786 blobmsg_close_array(&b
, a
);
787 ubus_send_reply(ctx
, req
, b
.head
);
793 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
794 struct ubus_request_data
*req
, const char *method
,
795 struct blob_attr
*msg
)
797 struct blob_attr
*tb
[__DEV_MAX
];
798 struct interface
*iface
;
799 bool add
= !strncmp(method
, "add", 3);
801 iface
= container_of(obj
, struct interface
, ubus
);
803 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
806 return UBUS_STATUS_INVALID_ARGUMENT
;
808 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_NAME
]), add
);
813 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
814 struct ubus_request_data
*req
, const char *method
,
815 struct blob_attr
*msg
)
817 struct interface
*iface
;
819 iface
= container_of(obj
, struct interface
, ubus
);
821 if (!iface
->proto
|| !iface
->proto
->notify
)
822 return UBUS_STATUS_NOT_SUPPORTED
;
824 return iface
->proto
->notify(iface
->proto
, msg
);
828 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
830 struct interface
*iface
;
832 iface
= container_of(timeout
, struct interface
, remove_timer
);
833 vlist_delete(&interfaces
, &iface
->node
);
837 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
838 struct ubus_request_data
*req
, const char *method
,
839 struct blob_attr
*msg
)
841 struct interface
*iface
;
843 iface
= container_of(obj
, struct interface
, ubus
);
844 if (iface
->remove_timer
.cb
)
845 return UBUS_STATUS_INVALID_ARGUMENT
;
847 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
848 uloop_timeout_set(&iface
->remove_timer
, 100);
853 netifd_handle_iface_prepare(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 const struct device_hotplug_ops
*ops
;
861 iface
= container_of(obj
, struct interface
, ubus
);
862 dev
= iface
->main_dev
.dev
;
866 ops
= dev
->hotplug_ops
;
870 return ops
->prepare(dev
);
874 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
875 struct ubus_request_data
*req
, const char *method
,
876 struct blob_attr
*msg
)
878 struct interface
*iface
;
879 struct blob_attr
*cur
;
882 iface
= container_of(obj
, struct interface
, ubus
);
884 blob_for_each_attr(cur
, msg
, rem
) {
885 ret
= interface_add_data(iface
, cur
);
893 static struct ubus_method iface_object_methods
[] = {
894 { .name
= "up", .handler
= netifd_handle_up
},
895 { .name
= "down", .handler
= netifd_handle_down
},
896 { .name
= "status", .handler
= netifd_handle_status
},
897 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
898 { .name
= "dump", .handler
= netifd_handle_dump
},
899 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
900 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
901 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
902 { .name
= "remove", .handler
= netifd_iface_remove
},
903 { .name
= "set_data", .handler
= netifd_handle_set_data
},
906 static struct ubus_object_type iface_object_type
=
907 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
910 static struct ubus_object iface_object
= {
911 .name
= "network.interface",
912 .type
= &iface_object_type
,
913 .n_methods
= ARRAY_SIZE(iface_object_methods
),
916 static void netifd_add_object(struct ubus_object
*obj
)
918 int ret
= ubus_add_object(ubus_ctx
, obj
);
921 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
924 static const struct blobmsg_policy iface_policy
= {
926 .type
= BLOBMSG_TYPE_STRING
,
930 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
931 struct ubus_request_data
*req
, const char *method
,
932 struct blob_attr
*msg
)
934 struct interface
*iface
;
935 struct blob_attr
*tb
;
938 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
940 return UBUS_STATUS_INVALID_ARGUMENT
;
942 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
944 return UBUS_STATUS_NOT_FOUND
;
946 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
949 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
952 cb
= iface_object_methods
[i
].handler
;
953 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
956 return UBUS_STATUS_INVALID_ARGUMENT
;
959 static void netifd_add_iface_object(void)
961 struct ubus_method
*methods
;
964 methods
= calloc(1, sizeof(iface_object_methods
));
965 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
966 iface_object
.methods
= methods
;
968 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
969 if (methods
[i
].handler
== netifd_handle_dump
)
972 methods
[i
].handler
= netifd_handle_iface
;
973 methods
[i
].policy
= &iface_policy
;
974 methods
[i
].n_policy
= 1;
976 netifd_add_object(&iface_object
);
979 static struct wireless_device
*
980 get_wdev(struct blob_attr
*msg
, int *ret
)
982 struct blobmsg_policy wdev_policy
= {
984 .type
= BLOBMSG_TYPE_STRING
,
986 struct blob_attr
*dev_attr
;
987 struct wireless_device
*wdev
= NULL
;
990 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
992 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
996 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
998 *ret
= UBUS_STATUS_NOT_FOUND
;
1007 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1008 struct ubus_request_data
*req
, const char *method
,
1009 struct blob_attr
*msg
)
1011 struct wireless_device
*wdev
;
1014 wdev
= get_wdev(msg
, &ret
);
1015 if (ret
== UBUS_STATUS_NOT_FOUND
)
1019 wireless_device_set_up(wdev
);
1021 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1022 wireless_device_set_up(wdev
);
1029 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1030 struct ubus_request_data
*req
, const char *method
,
1031 struct blob_attr
*msg
)
1033 struct wireless_device
*wdev
;
1036 wdev
= get_wdev(msg
, &ret
);
1037 if (ret
== UBUS_STATUS_NOT_FOUND
)
1041 wireless_device_set_down(wdev
);
1043 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1044 wireless_device_set_down(wdev
);
1051 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1052 struct ubus_request_data
*req
, const char *method
,
1053 struct blob_attr
*msg
)
1055 struct wireless_device
*wdev
;
1058 wdev
= get_wdev(msg
, &ret
);
1059 if (ret
== UBUS_STATUS_NOT_FOUND
)
1062 blob_buf_init(&b
, 0);
1064 wireless_device_status(wdev
, &b
);
1066 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1067 wireless_device_status(wdev
, &b
);
1069 ubus_send_reply(ctx
, req
, b
.head
);
1074 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1075 struct ubus_request_data
*req
, const char *method
,
1076 struct blob_attr
*msg
)
1078 struct wireless_device
*wdev
;
1081 wdev
= get_wdev(msg
, &ret
);
1082 if (ret
== UBUS_STATUS_NOT_FOUND
)
1085 blob_buf_init(&b
, 0);
1087 wireless_device_get_validate(wdev
, &b
);
1089 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1090 wireless_device_get_validate(wdev
, &b
);
1092 ubus_send_reply(ctx
, req
, b
.head
);
1097 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1098 struct ubus_request_data
*req
, const char *method
,
1099 struct blob_attr
*msg
)
1101 struct wireless_device
*wdev
;
1104 wdev
= get_wdev(msg
, &ret
);
1108 return wireless_device_notify(wdev
, msg
, req
);
1111 static struct ubus_method wireless_object_methods
[] = {
1112 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1113 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1114 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1115 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1116 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1119 static struct ubus_object_type wireless_object_type
=
1120 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1123 static struct ubus_object wireless_object
= {
1124 .name
= "network.wireless",
1125 .type
= &wireless_object_type
,
1126 .methods
= wireless_object_methods
,
1127 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1131 netifd_ubus_init(const char *path
)
1136 ubus_ctx
= ubus_connect(path
);
1140 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1141 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1142 netifd_ubus_add_fd();
1144 netifd_add_object(&main_object
);
1145 netifd_add_object(&dev_object
);
1146 netifd_add_object(&wireless_object
);
1147 netifd_add_iface_object();
1153 netifd_ubus_done(void)
1155 ubus_free(ubus_ctx
);
1159 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1161 blob_buf_init(&b
, 0);
1162 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1163 blobmsg_add_string(&b
, "interface", iface
->name
);
1164 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1168 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1170 const char *event
= (up
) ? "interface.update" : "interface.down";
1171 blob_buf_init(&b
, 0);
1172 blobmsg_add_string(&b
, "interface", iface
->name
);
1173 netifd_dump_status(iface
);
1174 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1175 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1179 netifd_ubus_add_interface(struct interface
*iface
)
1181 struct ubus_object
*obj
= &iface
->ubus
;
1184 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1188 obj
->type
= &iface_object_type
;
1189 obj
->methods
= iface_object_methods
;
1190 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1191 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1192 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1199 netifd_ubus_remove_interface(struct interface
*iface
)
1201 if (!iface
->ubus
.name
)
1204 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1205 free((void *) iface
->ubus
.name
);