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"
26 static struct ubus_context
*ctx
= NULL
;
27 static struct blob_buf b
;
28 static const char *ubus_path
;
33 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
34 struct ubus_request_data
*req
, const char *method
,
35 struct blob_attr
*msg
)
42 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
43 struct ubus_request_data
*req
, const char *method
,
44 struct blob_attr
*msg
)
57 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
58 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
59 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
60 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
64 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
65 struct ubus_request_data
*req
, const char *method
,
66 struct blob_attr
*msg
)
68 struct blob_attr
*tb
[__HR_MAX
];
69 struct interface
*iface
= NULL
;
73 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
75 return UBUS_STATUS_INVALID_ARGUMENT
;
78 v6
= blobmsg_get_bool(tb
[HR_V6
]);
81 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
83 memset(&a
, 0, sizeof(a
));
84 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
85 return UBUS_STATUS_INVALID_ARGUMENT
;
88 iface
= interface_ip_add_target_route(&a
, v6
, iface
);
90 return UBUS_STATUS_NOT_FOUND
;
93 blobmsg_add_string(&b
, "interface", iface
->name
);
94 ubus_send_reply(ctx
, req
, b
.head
);
100 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
101 struct ubus_request_data
*req
, const char *method
,
102 struct blob_attr
*msg
)
104 blob_buf_init(&b
, 0);
105 proto_dump_handlers(&b
);
106 ubus_send_reply(ctx
, req
, b
.head
);
117 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
118 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
122 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
123 struct ubus_request_data
*req
, const char *method
,
124 struct blob_attr
*msg
)
126 struct blob_attr
*tb
[__DI_MAX
];
127 struct interface
*iface
;
128 struct blob_attr
*config
;
131 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
134 return UBUS_STATUS_INVALID_ARGUMENT
;
136 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
138 iface
= calloc(1, sizeof(*iface
));
140 return UBUS_STATUS_UNKNOWN_ERROR
;
142 interface_init(iface
, name
, msg
, true);
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 iface
= vlist_find(&interfaces
, name
, iface
, node
);
154 return UBUS_STATUS_UNKNOWN_ERROR
;
156 dev
= iface
->main_dev
.dev
;
157 if (!dev
|| !dev
->default_config
)
158 return UBUS_STATUS_UNKNOWN_ERROR
;
160 device_set_config(dev
, dev
->type
, msg
);
161 return UBUS_STATUS_OK
;
165 return UBUS_STATUS_UNKNOWN_ERROR
;
168 static struct ubus_method main_object_methods
[] = {
169 { .name
= "restart", .handler
= netifd_handle_restart
},
170 { .name
= "reload", .handler
= netifd_handle_reload
},
171 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
172 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
173 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
176 static struct ubus_object_type main_object_type
=
177 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
179 static struct ubus_object main_object
= {
181 .type
= &main_object_type
,
182 .methods
= main_object_methods
,
183 .n_methods
= ARRAY_SIZE(main_object_methods
),
191 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
192 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
196 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
197 struct ubus_request_data
*req
, const char *method
,
198 struct blob_attr
*msg
)
200 struct device
*dev
= NULL
;
201 struct blob_attr
*tb
[__DEV_MAX
];
203 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
206 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
208 return UBUS_STATUS_INVALID_ARGUMENT
;
211 blob_buf_init(&b
, 0);
212 device_dump_status(&b
, dev
);
213 ubus_send_reply(ctx
, req
, b
.head
);
224 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
225 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
226 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
230 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
231 struct ubus_request_data
*req
, const char *method
,
232 struct blob_attr
*msg
)
234 struct device
*dev
= NULL
;
235 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
236 struct blob_attr
*cur
;
239 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
241 if (!tb
[ALIAS_ATTR_ALIAS
])
242 return UBUS_STATUS_INVALID_ARGUMENT
;
244 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
245 dev
= device_get(blobmsg_data(cur
), true);
247 return UBUS_STATUS_NOT_FOUND
;
250 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
251 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
254 if (!blobmsg_check_attr(cur
, NULL
))
257 alias_notify_device(blobmsg_data(cur
), dev
);
262 device_free_unused(dev
);
263 return UBUS_STATUS_INVALID_ARGUMENT
;
272 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
273 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
274 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
278 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
279 struct ubus_request_data
*req
, const char *method
,
280 struct blob_attr
*msg
)
282 struct device
*dev
= NULL
;
283 struct blob_attr
*tb
[__DEV_STATE_MAX
];
284 struct blob_attr
*cur
;
286 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
288 cur
= tb
[DEV_STATE_NAME
];
290 return UBUS_STATUS_INVALID_ARGUMENT
;
292 dev
= device_get(blobmsg_data(cur
), false);
294 return UBUS_STATUS_NOT_FOUND
;
296 cur
= tb
[DEV_STATE_DEFER
];
298 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
303 static struct ubus_method dev_object_methods
[] = {
304 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
305 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
306 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
309 static struct ubus_object_type dev_object_type
=
310 UBUS_OBJECT_TYPE("device", dev_object_methods
);
312 static struct ubus_object dev_object
= {
313 .name
= "network.device",
314 .type
= &dev_object_type
,
315 .methods
= dev_object_methods
,
316 .n_methods
= ARRAY_SIZE(dev_object_methods
),
320 netifd_ubus_add_fd(void)
323 system_fd_set_cloexec(ctx
->sock
.fd
);
327 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
329 static struct uloop_timeout retry
= {
330 .cb
= netifd_ubus_reconnect_timer
,
334 if (ubus_reconnect(ctx
, ubus_path
) != 0) {
335 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
336 uloop_timeout_set(&retry
, t
* 1000);
340 DPRINTF("reconnected to ubus, new id: %08x\n", ctx
->local_id
);
341 netifd_ubus_add_fd();
345 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
347 netifd_ubus_reconnect_timer(NULL
);
350 /* per-interface object */
353 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
354 struct ubus_request_data
*req
, const char *method
,
355 struct blob_attr
*msg
)
357 struct interface
*iface
;
359 iface
= container_of(obj
, struct interface
, ubus
);
360 interface_set_up(iface
);
366 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
367 struct ubus_request_data
*req
, const char *method
,
368 struct blob_attr
*msg
)
370 struct interface
*iface
;
372 iface
= container_of(obj
, struct interface
, ubus
);
373 interface_set_down(iface
);
379 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
381 struct interface_error
*error
;
385 e
= blobmsg_open_array(b
, "errors");
386 list_for_each_entry(error
, &iface
->errors
, list
) {
387 e2
= blobmsg_open_table(b
, NULL
);
389 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
390 blobmsg_add_string(b
, "code", error
->code
);
391 if (error
->data
[0]) {
392 e3
= blobmsg_open_array(b
, "data");
393 for (i
= 0; error
->data
[i
]; i
++)
394 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
395 blobmsg_close_array(b
, e3
);
398 blobmsg_close_table(b
, e2
);
400 blobmsg_close_array(b
, e
);
404 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
,
407 struct device_addr
*addr
;
413 time_t now
= system_get_rtime();
414 vlist_for_each_element(&ip
->addr
, addr
, node
) {
415 if (addr
->enabled
!= enabled
)
418 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
423 if (af
!= (v6
? AF_INET6
: AF_INET
))
426 a
= blobmsg_open_table(&b
, NULL
);
428 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
429 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
430 blobmsg_add_string_buffer(&b
);
432 blobmsg_add_u32(&b
, "mask", addr
->mask
);
434 if (addr
->preferred_until
) {
435 int preferred
= addr
->preferred_until
- now
;
438 blobmsg_add_u32(&b
, "preferred", preferred
);
441 if (addr
->valid_until
)
442 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
444 blobmsg_close_table(&b
, a
);
449 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
451 struct device_route
*route
;
457 time_t now
= system_get_rtime();
458 vlist_for_each_element(&ip
->route
, route
, node
) {
459 if (route
->enabled
!= enabled
)
462 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
467 r
= blobmsg_open_table(&b
, NULL
);
469 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
470 inet_ntop(af
, &route
->addr
, buf
, buflen
);
471 blobmsg_add_string_buffer(&b
);
473 blobmsg_add_u32(&b
, "mask", route
->mask
);
475 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
476 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
477 blobmsg_add_string_buffer(&b
);
479 if (route
->flags
& DEVROUTE_MTU
)
480 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
482 if (route
->flags
& DEVROUTE_METRIC
)
483 blobmsg_add_u32(&b
, "metric", route
->metric
);
485 if (route
->flags
& DEVROUTE_TABLE
)
486 blobmsg_add_u32(&b
, "table", route
->table
);
488 if (route
->valid_until
)
489 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
491 blobmsg_close_table(&b
, r
);
497 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
499 struct device_prefix
*prefix
;
502 const int buflen
= INET6_ADDRSTRLEN
;
504 time_t now
= system_get_rtime();
505 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
506 a
= blobmsg_open_table(&b
, NULL
);
508 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
509 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
510 blobmsg_add_string_buffer(&b
);
512 blobmsg_add_u32(&b
, "mask", prefix
->length
);
514 if (prefix
->preferred_until
) {
515 int preferred
= prefix
->preferred_until
- now
;
518 blobmsg_add_u32(&b
, "preferred", preferred
);
521 if (prefix
->valid_until
)
522 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
524 blobmsg_add_string(&b
, "class", prefix
->pclass
);
526 c
= blobmsg_open_table(&b
, "assigned");
527 struct device_prefix_assignment
*assign
;
528 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
529 if (!assign
->name
[0])
532 struct in6_addr addr
= prefix
->addr
;
533 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
535 void *d
= blobmsg_open_table(&b
, assign
->name
);
537 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
538 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
539 blobmsg_add_string_buffer(&b
);
541 blobmsg_add_u32(&b
, "mask", assign
->length
);
543 blobmsg_close_table(&b
, d
);
545 blobmsg_close_table(&b
, c
);
547 blobmsg_close_table(&b
, a
);
553 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
557 const int buflen
= INET6_ADDRSTRLEN
;
558 time_t now
= system_get_rtime();
560 struct device_prefix
*prefix
;
561 list_for_each_entry(prefix
, &prefixes
, head
) {
562 struct device_prefix_assignment
*assign
;
563 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
564 if (strcmp(assign
->name
, iface
->name
))
567 struct in6_addr addr
= prefix
->addr
;
568 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
570 a
= blobmsg_open_table(&b
, NULL
);
572 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
573 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
574 blobmsg_add_string_buffer(&b
);
576 blobmsg_add_u32(&b
, "mask", assign
->length
);
578 if (prefix
->preferred_until
) {
579 int preferred
= prefix
->preferred_until
- now
;
582 blobmsg_add_u32(&b
, "preferred", preferred
);
585 if (prefix
->valid_until
)
586 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
588 blobmsg_close_table(&b
, a
);
595 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
598 struct dns_server
*dns
;
602 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
603 if (ip
->no_dns
== enabled
)
606 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
607 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
608 blobmsg_add_string_buffer(&b
);
613 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
,
616 struct dns_search_domain
*dns
;
618 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
619 if (ip
->no_dns
== enabled
)
622 blobmsg_add_string(&b
, NULL
, dns
->name
);
627 netifd_dump_status(struct interface
*iface
)
629 struct interface_data
*data
;
633 blobmsg_add_string(&b
, "name", iface
->name
);
634 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
635 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
636 blobmsg_add_u8(&b
, "available", iface
->available
);
637 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
639 if (iface
->state
== IFS_UP
) {
640 time_t cur
= system_get_rtime();
641 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
642 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
645 if (iface
->proto_handler
)
646 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
648 dev
= iface
->main_dev
.dev
;
649 if (dev
&& !dev
->hidden
&&
650 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
651 blobmsg_add_string(&b
, "device", dev
->ifname
);
653 if (iface
->state
== IFS_UP
) {
654 blobmsg_add_u32(&b
, "metric", iface
->metric
);
655 a
= blobmsg_open_array(&b
, "ipv4-address");
656 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
657 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
658 blobmsg_close_array(&b
, a
);
659 a
= blobmsg_open_array(&b
, "ipv6-address");
660 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
661 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
662 blobmsg_close_array(&b
, a
);
663 a
= blobmsg_open_array(&b
, "ipv6-prefix");
664 interface_ip_dump_prefix_list(&iface
->config_ip
);
665 interface_ip_dump_prefix_list(&iface
->proto_ip
);
666 blobmsg_close_array(&b
, a
);
667 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
668 interface_ip_dump_prefix_assignment_list(iface
);
669 blobmsg_close_array(&b
, a
);
670 a
= blobmsg_open_array(&b
, "route");
671 interface_ip_dump_route_list(&iface
->config_ip
, true);
672 interface_ip_dump_route_list(&iface
->proto_ip
, true);
673 blobmsg_close_array(&b
, a
);
674 a
= blobmsg_open_array(&b
, "dns-server");
675 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
676 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
677 blobmsg_close_array(&b
, a
);
678 a
= blobmsg_open_array(&b
, "dns-search");
679 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
680 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
681 blobmsg_close_array(&b
, a
);
683 inactive
= blobmsg_open_table(&b
, "inactive");
684 a
= blobmsg_open_array(&b
, "ipv4-address");
685 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
686 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
687 blobmsg_close_array(&b
, a
);
688 a
= blobmsg_open_array(&b
, "ipv6-address");
689 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
690 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
691 blobmsg_close_array(&b
, a
);
692 a
= blobmsg_open_array(&b
, "route");
693 interface_ip_dump_route_list(&iface
->config_ip
, false);
694 interface_ip_dump_route_list(&iface
->proto_ip
, false);
695 blobmsg_close_array(&b
, a
);
696 a
= blobmsg_open_array(&b
, "dns-server");
697 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
698 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
699 blobmsg_close_array(&b
, a
);
700 a
= blobmsg_open_array(&b
, "dns-search");
701 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
702 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
703 blobmsg_close_array(&b
, a
);
704 blobmsg_close_table(&b
, inactive
);
707 a
= blobmsg_open_table(&b
, "data");
708 avl_for_each_element(&iface
->data
, data
, node
)
709 blob_put(&b
, blob_id(data
->data
), blob_data(data
->data
), blob_len(data
->data
));
711 blobmsg_close_table(&b
, a
);
713 if (!list_is_empty(&iface
->errors
))
714 netifd_add_interface_errors(&b
, iface
);
718 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
719 struct ubus_request_data
*req
, const char *method
,
720 struct blob_attr
*msg
)
722 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
724 blob_buf_init(&b
, 0);
725 netifd_dump_status(iface
);
726 ubus_send_reply(ctx
, req
, b
.head
);
732 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
733 struct ubus_request_data
*req
, const char *method
,
734 struct blob_attr
*msg
)
736 blob_buf_init(&b
, 0);
737 void *a
= blobmsg_open_array(&b
, "interface");
739 struct interface
*iface
;
740 vlist_for_each_element(&interfaces
, iface
, node
) {
741 void *i
= blobmsg_open_table(&b
, NULL
);
742 netifd_dump_status(iface
);
743 blobmsg_close_table(&b
, i
);
746 blobmsg_close_array(&b
, a
);
747 ubus_send_reply(ctx
, req
, b
.head
);
753 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
754 struct ubus_request_data
*req
, const char *method
,
755 struct blob_attr
*msg
)
757 struct blob_attr
*tb
[__DEV_MAX
];
758 struct interface
*iface
;
760 bool add
= !strncmp(method
, "add", 3);
763 iface
= container_of(obj
, struct interface
, ubus
);
765 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
768 return UBUS_STATUS_INVALID_ARGUMENT
;
772 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
? 2 : 0);
774 ret
= UBUS_STATUS_NOT_FOUND
;
779 device_set_present(dev
, true);
780 if (iface
->device_config
)
781 device_set_config(dev
, &simple_device_type
, iface
->config
);
783 system_if_apply_settings(dev
, &dev
->settings
);
784 ret
= interface_add_link(iface
, dev
);
786 ret
= interface_remove_link(iface
, dev
);
797 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
798 struct ubus_request_data
*req
, const char *method
,
799 struct blob_attr
*msg
)
801 struct interface
*iface
;
803 iface
= container_of(obj
, struct interface
, ubus
);
805 if (!iface
->proto
|| !iface
->proto
->notify
)
806 return UBUS_STATUS_NOT_SUPPORTED
;
808 return iface
->proto
->notify(iface
->proto
, msg
);
812 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
814 struct interface
*iface
;
816 iface
= container_of(timeout
, struct interface
, remove_timer
);
817 vlist_delete(&interfaces
, &iface
->node
);
821 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
822 struct ubus_request_data
*req
, const char *method
,
823 struct blob_attr
*msg
)
825 struct interface
*iface
;
827 iface
= container_of(obj
, struct interface
, ubus
);
828 if (iface
->remove_timer
.cb
)
829 return UBUS_STATUS_INVALID_ARGUMENT
;
831 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
832 uloop_timeout_set(&iface
->remove_timer
, 100);
837 netifd_handle_iface_prepare(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 const struct device_hotplug_ops
*ops
;
845 iface
= container_of(obj
, struct interface
, ubus
);
846 dev
= iface
->main_dev
.dev
;
850 ops
= dev
->hotplug_ops
;
854 return ops
->prepare(dev
);
858 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
859 struct ubus_request_data
*req
, const char *method
,
860 struct blob_attr
*msg
)
862 struct interface
*iface
;
863 struct blob_attr
*cur
;
866 iface
= container_of(obj
, struct interface
, ubus
);
868 blob_for_each_attr(cur
, msg
, rem
) {
869 ret
= interface_add_data(iface
, cur
);
877 static struct ubus_method iface_object_methods
[] = {
878 { .name
= "up", .handler
= netifd_handle_up
},
879 { .name
= "down", .handler
= netifd_handle_down
},
880 { .name
= "status", .handler
= netifd_handle_status
},
881 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
882 { .name
= "dump", .handler
= netifd_handle_dump
},
883 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
884 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
885 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
886 { .name
= "remove", .handler
= netifd_iface_remove
},
887 { .name
= "set_data", .handler
= netifd_handle_set_data
},
890 static struct ubus_object_type iface_object_type
=
891 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
894 static struct ubus_object iface_object
= {
895 .name
= "network.interface",
896 .type
= &iface_object_type
,
897 .n_methods
= ARRAY_SIZE(iface_object_methods
),
900 static void netifd_add_object(struct ubus_object
*obj
)
902 int ret
= ubus_add_object(ctx
, obj
);
905 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
908 static const struct blobmsg_policy iface_policy
= {
910 .type
= BLOBMSG_TYPE_STRING
,
914 netifd_handle_iface(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
*tb
;
922 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
924 return UBUS_STATUS_INVALID_ARGUMENT
;
926 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
928 return UBUS_STATUS_NOT_FOUND
;
930 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
933 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
936 cb
= iface_object_methods
[i
].handler
;
937 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
940 return UBUS_STATUS_INVALID_ARGUMENT
;
943 static void netifd_add_iface_object(void)
945 struct ubus_method
*methods
;
948 methods
= calloc(1, sizeof(iface_object_methods
));
949 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
950 iface_object
.methods
= methods
;
952 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
953 if (methods
[i
].handler
== netifd_handle_dump
)
956 methods
[i
].handler
= netifd_handle_iface
;
957 methods
[i
].policy
= &iface_policy
;
958 methods
[i
].n_policy
= 1;
960 netifd_add_object(&iface_object
);
964 netifd_ubus_init(const char *path
)
969 ctx
= ubus_connect(path
);
973 DPRINTF("connected as %08x\n", ctx
->local_id
);
974 ctx
->connection_lost
= netifd_ubus_connection_lost
;
975 netifd_ubus_add_fd();
977 netifd_add_object(&main_object
);
978 netifd_add_object(&dev_object
);
979 netifd_add_iface_object();
985 netifd_ubus_done(void)
991 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
993 blob_buf_init(&b
, 0);
994 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
995 blobmsg_add_string(&b
, "interface", iface
->name
);
996 ubus_send_event(ctx
, "network.interface", b
.head
);
1000 netifd_ubus_add_interface(struct interface
*iface
)
1002 struct ubus_object
*obj
= &iface
->ubus
;
1005 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1009 obj
->type
= &iface_object_type
;
1010 obj
->methods
= iface_object_methods
;
1011 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1012 if (ubus_add_object(ctx
, &iface
->ubus
)) {
1013 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1020 netifd_ubus_remove_interface(struct interface
*iface
)
1022 if (!iface
->ubus
.name
)
1025 ubus_remove_object(ctx
, &iface
->ubus
);
1026 free((void *) iface
->ubus
.name
);