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
);
111 static struct ubus_method main_object_methods
[] = {
112 { .name
= "restart", .handler
= netifd_handle_restart
},
113 { .name
= "reload", .handler
= netifd_handle_reload
},
114 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
115 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
118 static struct ubus_object_type main_object_type
=
119 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
121 static struct ubus_object main_object
= {
123 .type
= &main_object_type
,
124 .methods
= main_object_methods
,
125 .n_methods
= ARRAY_SIZE(main_object_methods
),
133 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
134 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
138 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
139 struct ubus_request_data
*req
, const char *method
,
140 struct blob_attr
*msg
)
142 struct device
*dev
= NULL
;
143 struct blob_attr
*tb
[__DEV_MAX
];
145 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
148 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
150 return UBUS_STATUS_INVALID_ARGUMENT
;
153 blob_buf_init(&b
, 0);
154 device_dump_status(&b
, dev
);
155 ubus_send_reply(ctx
, req
, b
.head
);
166 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
167 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
168 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
172 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
173 struct ubus_request_data
*req
, const char *method
,
174 struct blob_attr
*msg
)
176 struct device
*dev
= NULL
;
177 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
178 struct blob_attr
*cur
;
181 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
183 if (!tb
[ALIAS_ATTR_ALIAS
])
184 return UBUS_STATUS_INVALID_ARGUMENT
;
186 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
187 dev
= device_get(blobmsg_data(cur
), true);
189 return UBUS_STATUS_NOT_FOUND
;
192 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
193 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
196 if (!blobmsg_check_attr(cur
, NULL
))
199 alias_notify_device(blobmsg_data(cur
), dev
);
204 device_free_unused(dev
);
205 return UBUS_STATUS_INVALID_ARGUMENT
;
214 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
215 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
216 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
220 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
221 struct ubus_request_data
*req
, const char *method
,
222 struct blob_attr
*msg
)
224 struct device
*dev
= NULL
;
225 struct blob_attr
*tb
[__DEV_STATE_MAX
];
226 struct blob_attr
*cur
;
228 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
230 cur
= tb
[DEV_STATE_NAME
];
232 return UBUS_STATUS_INVALID_ARGUMENT
;
234 dev
= device_get(blobmsg_data(cur
), false);
236 return UBUS_STATUS_NOT_FOUND
;
238 cur
= tb
[DEV_STATE_DEFER
];
240 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
245 static struct ubus_method dev_object_methods
[] = {
246 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
247 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
248 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
251 static struct ubus_object_type dev_object_type
=
252 UBUS_OBJECT_TYPE("device", dev_object_methods
);
254 static struct ubus_object dev_object
= {
255 .name
= "network.device",
256 .type
= &dev_object_type
,
257 .methods
= dev_object_methods
,
258 .n_methods
= ARRAY_SIZE(dev_object_methods
),
262 netifd_ubus_add_fd(void)
265 system_fd_set_cloexec(ctx
->sock
.fd
);
269 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
271 static struct uloop_timeout retry
= {
272 .cb
= netifd_ubus_reconnect_timer
,
276 if (ubus_reconnect(ctx
, ubus_path
) != 0) {
277 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
278 uloop_timeout_set(&retry
, t
* 1000);
282 DPRINTF("reconnected to ubus, new id: %08x\n", ctx
->local_id
);
283 netifd_ubus_add_fd();
287 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
289 netifd_ubus_reconnect_timer(NULL
);
292 /* per-interface object */
295 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
296 struct ubus_request_data
*req
, const char *method
,
297 struct blob_attr
*msg
)
299 struct interface
*iface
;
301 iface
= container_of(obj
, struct interface
, ubus
);
302 interface_set_up(iface
);
308 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
309 struct ubus_request_data
*req
, const char *method
,
310 struct blob_attr
*msg
)
312 struct interface
*iface
;
314 iface
= container_of(obj
, struct interface
, ubus
);
315 interface_set_down(iface
);
321 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
323 struct interface_error
*error
;
327 e
= blobmsg_open_array(b
, "errors");
328 list_for_each_entry(error
, &iface
->errors
, list
) {
329 e2
= blobmsg_open_table(b
, NULL
);
331 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
332 blobmsg_add_string(b
, "code", error
->code
);
333 if (error
->data
[0]) {
334 e3
= blobmsg_open_array(b
, "data");
335 for (i
= 0; error
->data
[i
]; i
++)
336 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
337 blobmsg_close_array(b
, e3
);
340 blobmsg_close_table(b
, e2
);
342 blobmsg_close_array(b
, e
);
346 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
,
349 struct device_addr
*addr
;
355 time_t now
= system_get_rtime();
356 vlist_for_each_element(&ip
->addr
, addr
, node
) {
357 if (addr
->enabled
!= enabled
)
360 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
365 if (af
!= (v6
? AF_INET6
: AF_INET
))
368 a
= blobmsg_open_table(&b
, NULL
);
370 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
371 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
372 blobmsg_add_string_buffer(&b
);
374 blobmsg_add_u32(&b
, "mask", addr
->mask
);
376 if (addr
->preferred_until
) {
377 int preferred
= addr
->preferred_until
- now
;
380 blobmsg_add_u32(&b
, "preferred", preferred
);
383 if (addr
->valid_until
)
384 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
386 blobmsg_close_table(&b
, a
);
391 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
393 struct device_route
*route
;
399 time_t now
= system_get_rtime();
400 vlist_for_each_element(&ip
->route
, route
, node
) {
401 if (route
->enabled
!= enabled
)
404 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
409 r
= blobmsg_open_table(&b
, NULL
);
411 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
412 inet_ntop(af
, &route
->addr
, buf
, buflen
);
413 blobmsg_add_string_buffer(&b
);
415 blobmsg_add_u32(&b
, "mask", route
->mask
);
417 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
418 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
419 blobmsg_add_string_buffer(&b
);
421 if (route
->flags
& DEVROUTE_MTU
)
422 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
424 if (route
->flags
& DEVROUTE_METRIC
)
425 blobmsg_add_u32(&b
, "metric", route
->metric
);
427 if (route
->flags
& DEVROUTE_TABLE
)
428 blobmsg_add_u32(&b
, "table", route
->table
);
430 if (route
->valid_until
)
431 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
433 blobmsg_close_table(&b
, r
);
439 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
441 struct device_prefix
*prefix
;
444 const int buflen
= INET6_ADDRSTRLEN
;
446 time_t now
= system_get_rtime();
447 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
448 a
= blobmsg_open_table(&b
, NULL
);
450 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
451 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
452 blobmsg_add_string_buffer(&b
);
454 blobmsg_add_u32(&b
, "mask", prefix
->length
);
456 if (prefix
->preferred_until
) {
457 int preferred
= prefix
->preferred_until
- now
;
460 blobmsg_add_u32(&b
, "preferred", preferred
);
463 if (prefix
->valid_until
)
464 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
466 c
= blobmsg_open_table(&b
, "assigned");
467 struct device_prefix_assignment
*assign
;
468 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
469 if (!assign
->name
[0])
472 struct in6_addr addr
= prefix
->addr
;
473 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
475 void *d
= blobmsg_open_table(&b
, assign
->name
);
477 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
478 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
479 blobmsg_add_string_buffer(&b
);
481 blobmsg_add_u32(&b
, "mask", assign
->length
);
483 blobmsg_close_table(&b
, d
);
485 blobmsg_close_table(&b
, c
);
487 blobmsg_close_table(&b
, a
);
493 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
497 const int buflen
= INET6_ADDRSTRLEN
;
498 time_t now
= system_get_rtime();
500 struct device_prefix
*prefix
;
501 list_for_each_entry(prefix
, &prefixes
, head
) {
502 struct device_prefix_assignment
*assign
;
503 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
504 if (strcmp(assign
->name
, iface
->name
))
507 struct in6_addr addr
= prefix
->addr
;
508 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
510 a
= blobmsg_open_table(&b
, NULL
);
512 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
513 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
514 blobmsg_add_string_buffer(&b
);
516 blobmsg_add_u32(&b
, "mask", assign
->length
);
518 if (prefix
->preferred_until
) {
519 int preferred
= prefix
->preferred_until
- now
;
522 blobmsg_add_u32(&b
, "preferred", preferred
);
525 if (prefix
->valid_until
)
526 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
528 blobmsg_close_table(&b
, a
);
535 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
538 struct dns_server
*dns
;
542 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
543 if (ip
->no_dns
== enabled
)
546 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
547 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
548 blobmsg_add_string_buffer(&b
);
553 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
,
556 struct dns_search_domain
*dns
;
558 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
559 if (ip
->no_dns
== enabled
)
562 blobmsg_add_string(&b
, NULL
, dns
->name
);
567 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
568 struct ubus_request_data
*req
, const char *method
,
569 struct blob_attr
*msg
)
571 struct interface
*iface
;
572 struct interface_data
*data
;
576 iface
= container_of(obj
, struct interface
, ubus
);
578 blob_buf_init(&b
, 0);
579 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
580 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
581 blobmsg_add_u8(&b
, "available", iface
->available
);
582 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
584 if (iface
->state
== IFS_UP
) {
585 time_t cur
= system_get_rtime();
586 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
587 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
590 if (iface
->proto_handler
)
591 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
593 dev
= iface
->main_dev
.dev
;
594 if (dev
&& !dev
->hidden
&&
595 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
596 blobmsg_add_string(&b
, "device", dev
->ifname
);
598 if (iface
->state
== IFS_UP
) {
599 blobmsg_add_u32(&b
, "metric", iface
->metric
);
600 a
= blobmsg_open_array(&b
, "ipv4-address");
601 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
602 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
603 blobmsg_close_array(&b
, a
);
604 a
= blobmsg_open_array(&b
, "ipv6-address");
605 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
606 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
607 blobmsg_close_array(&b
, a
);
608 a
= blobmsg_open_array(&b
, "ipv6-prefix");
609 interface_ip_dump_prefix_list(&iface
->config_ip
);
610 interface_ip_dump_prefix_list(&iface
->proto_ip
);
611 blobmsg_close_array(&b
, a
);
612 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
613 interface_ip_dump_prefix_assignment_list(iface
);
614 blobmsg_close_array(&b
, a
);
615 a
= blobmsg_open_array(&b
, "route");
616 interface_ip_dump_route_list(&iface
->config_ip
, true);
617 interface_ip_dump_route_list(&iface
->proto_ip
, true);
618 blobmsg_close_array(&b
, a
);
619 a
= blobmsg_open_array(&b
, "dns-server");
620 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
621 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
622 blobmsg_close_array(&b
, a
);
623 a
= blobmsg_open_array(&b
, "dns-search");
624 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
625 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
626 blobmsg_close_array(&b
, a
);
628 inactive
= blobmsg_open_table(&b
, "inactive");
629 a
= blobmsg_open_array(&b
, "ipv4-address");
630 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
631 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
632 blobmsg_close_array(&b
, a
);
633 a
= blobmsg_open_array(&b
, "ipv6-address");
634 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
635 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
636 blobmsg_close_array(&b
, a
);
637 a
= blobmsg_open_array(&b
, "route");
638 interface_ip_dump_route_list(&iface
->config_ip
, false);
639 interface_ip_dump_route_list(&iface
->proto_ip
, false);
640 blobmsg_close_array(&b
, a
);
641 a
= blobmsg_open_array(&b
, "dns-server");
642 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
643 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
644 blobmsg_close_array(&b
, a
);
645 a
= blobmsg_open_array(&b
, "dns-search");
646 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
647 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
648 blobmsg_close_array(&b
, a
);
649 blobmsg_close_table(&b
, inactive
);
652 a
= blobmsg_open_table(&b
, "data");
653 avl_for_each_element(&iface
->data
, data
, node
)
654 blob_put(&b
, blob_id(data
->data
), blob_data(data
->data
), blob_len(data
->data
));
656 blobmsg_close_table(&b
, a
);
658 if (!list_is_empty(&iface
->errors
))
659 netifd_add_interface_errors(&b
, iface
);
661 ubus_send_reply(ctx
, req
, b
.head
);
667 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
668 struct ubus_request_data
*req
, const char *method
,
669 struct blob_attr
*msg
)
671 struct blob_attr
*tb
[__DEV_MAX
];
672 struct interface
*iface
;
674 bool add
= !strncmp(method
, "add", 3);
677 iface
= container_of(obj
, struct interface
, ubus
);
679 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
682 return UBUS_STATUS_INVALID_ARGUMENT
;
686 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
? 2 : 0);
688 ret
= UBUS_STATUS_NOT_FOUND
;
693 device_set_present(dev
, true);
694 if (iface
->device_config
)
695 device_set_config(dev
, &simple_device_type
, iface
->config
);
697 system_if_apply_settings(dev
, &dev
->settings
);
698 ret
= interface_add_link(iface
, dev
);
700 ret
= interface_remove_link(iface
, dev
);
711 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
712 struct ubus_request_data
*req
, const char *method
,
713 struct blob_attr
*msg
)
715 struct interface
*iface
;
717 iface
= container_of(obj
, struct interface
, ubus
);
719 if (!iface
->proto
|| !iface
->proto
->notify
)
720 return UBUS_STATUS_NOT_SUPPORTED
;
722 return iface
->proto
->notify(iface
->proto
, msg
);
726 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
728 struct interface
*iface
;
730 iface
= container_of(timeout
, struct interface
, remove_timer
);
731 vlist_delete(&interfaces
, &iface
->node
);
735 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
736 struct ubus_request_data
*req
, const char *method
,
737 struct blob_attr
*msg
)
739 struct interface
*iface
;
741 iface
= container_of(obj
, struct interface
, ubus
);
742 if (iface
->remove_timer
.cb
)
743 return UBUS_STATUS_INVALID_ARGUMENT
;
745 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
746 uloop_timeout_set(&iface
->remove_timer
, 100);
751 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
752 struct ubus_request_data
*req
, const char *method
,
753 struct blob_attr
*msg
)
755 struct interface
*iface
;
757 const struct device_hotplug_ops
*ops
;
759 iface
= container_of(obj
, struct interface
, ubus
);
760 dev
= iface
->main_dev
.dev
;
764 ops
= dev
->hotplug_ops
;
768 return ops
->prepare(dev
);
772 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
773 struct ubus_request_data
*req
, const char *method
,
774 struct blob_attr
*msg
)
776 struct interface
*iface
;
777 struct blob_attr
*cur
;
780 iface
= container_of(obj
, struct interface
, ubus
);
782 blob_for_each_attr(cur
, msg
, rem
) {
783 ret
= interface_add_data(iface
, cur
);
791 static struct ubus_method iface_object_methods
[] = {
792 { .name
= "up", .handler
= netifd_handle_up
},
793 { .name
= "down", .handler
= netifd_handle_down
},
794 { .name
= "status", .handler
= netifd_handle_status
},
795 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
796 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
797 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
798 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
799 { .name
= "remove", .handler
= netifd_iface_remove
},
800 { .name
= "set_data", .handler
= netifd_handle_set_data
},
803 static struct ubus_object_type iface_object_type
=
804 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
807 static struct ubus_object iface_object
= {
808 .name
= "network.interface",
809 .type
= &iface_object_type
,
810 .n_methods
= ARRAY_SIZE(iface_object_methods
),
813 static void netifd_add_object(struct ubus_object
*obj
)
815 int ret
= ubus_add_object(ctx
, obj
);
818 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
821 static const struct blobmsg_policy iface_policy
= {
823 .type
= BLOBMSG_TYPE_STRING
,
827 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
828 struct ubus_request_data
*req
, const char *method
,
829 struct blob_attr
*msg
)
831 struct interface
*iface
;
832 struct blob_attr
*tb
;
835 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
837 return UBUS_STATUS_INVALID_ARGUMENT
;
839 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
841 return UBUS_STATUS_NOT_FOUND
;
843 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
846 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
849 cb
= iface_object_methods
[i
].handler
;
850 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
853 return UBUS_STATUS_INVALID_ARGUMENT
;
856 static void netifd_add_iface_object(void)
858 struct ubus_method
*methods
;
861 methods
= calloc(1, sizeof(iface_object_methods
));
862 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
863 iface_object
.methods
= methods
;
865 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
866 methods
[i
].handler
= netifd_handle_iface
;
867 methods
[i
].policy
= &iface_policy
;
868 methods
[i
].n_policy
= 1;
870 netifd_add_object(&iface_object
);
874 netifd_ubus_init(const char *path
)
879 ctx
= ubus_connect(path
);
883 DPRINTF("connected as %08x\n", ctx
->local_id
);
884 ctx
->connection_lost
= netifd_ubus_connection_lost
;
885 netifd_ubus_add_fd();
887 netifd_add_object(&main_object
);
888 netifd_add_object(&dev_object
);
889 netifd_add_iface_object();
895 netifd_ubus_done(void)
901 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
903 blob_buf_init(&b
, 0);
904 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
905 blobmsg_add_string(&b
, "interface", iface
->name
);
906 ubus_send_event(ctx
, "network.interface", b
.head
);
910 netifd_ubus_add_interface(struct interface
*iface
)
912 struct ubus_object
*obj
= &iface
->ubus
;
915 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
919 obj
->type
= &iface_object_type
;
920 obj
->methods
= iface_object_methods
;
921 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
922 if (ubus_add_object(ctx
, &iface
->ubus
)) {
923 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
930 netifd_ubus_remove_interface(struct interface
*iface
)
932 if (!iface
->ubus
.name
)
935 ubus_remove_object(ctx
, &iface
->ubus
);
936 free((void *) iface
->ubus
.name
);