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
)
56 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
57 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
58 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
62 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
63 struct ubus_request_data
*req
, const char *method
,
64 struct blob_attr
*msg
)
66 struct blob_attr
*tb
[__HR_MAX
];
67 struct interface
*iface
;
71 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
73 return UBUS_STATUS_INVALID_ARGUMENT
;
76 v6
= blobmsg_get_bool(tb
[HR_V6
]);
78 memset(&a
, 0, sizeof(a
));
79 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
80 return UBUS_STATUS_INVALID_ARGUMENT
;
83 iface
= interface_ip_add_target_route(&a
, v6
);
85 return UBUS_STATUS_NOT_FOUND
;
88 blobmsg_add_string(&b
, "interface", iface
->name
);
89 ubus_send_reply(ctx
, req
, b
.head
);
95 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
96 struct ubus_request_data
*req
, const char *method
,
97 struct blob_attr
*msg
)
100 proto_dump_handlers(&b
);
101 ubus_send_reply(ctx
, req
, b
.head
);
106 static struct ubus_method main_object_methods
[] = {
107 { .name
= "restart", .handler
= netifd_handle_restart
},
108 { .name
= "reload", .handler
= netifd_handle_reload
},
109 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
110 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
113 static struct ubus_object_type main_object_type
=
114 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
116 static struct ubus_object main_object
= {
118 .type
= &main_object_type
,
119 .methods
= main_object_methods
,
120 .n_methods
= ARRAY_SIZE(main_object_methods
),
128 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
129 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
133 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
134 struct ubus_request_data
*req
, const char *method
,
135 struct blob_attr
*msg
)
137 struct device
*dev
= NULL
;
138 struct blob_attr
*tb
[__DEV_MAX
];
140 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
143 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), false);
145 return UBUS_STATUS_INVALID_ARGUMENT
;
148 blob_buf_init(&b
, 0);
149 device_dump_status(&b
, dev
);
150 ubus_send_reply(ctx
, req
, b
.head
);
161 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
162 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
163 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
167 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
168 struct ubus_request_data
*req
, const char *method
,
169 struct blob_attr
*msg
)
171 struct device
*dev
= NULL
;
172 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
173 struct blob_attr
*cur
;
176 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
178 if (!tb
[ALIAS_ATTR_ALIAS
])
179 return UBUS_STATUS_INVALID_ARGUMENT
;
181 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
182 dev
= device_get(blobmsg_data(cur
), true);
184 return UBUS_STATUS_NOT_FOUND
;
187 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
188 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
191 if (!blobmsg_check_attr(cur
, NULL
))
194 alias_notify_device(blobmsg_data(cur
), dev
);
199 device_free_unused(dev
);
200 return UBUS_STATUS_INVALID_ARGUMENT
;
209 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
210 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
211 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
215 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
216 struct ubus_request_data
*req
, const char *method
,
217 struct blob_attr
*msg
)
219 struct device
*dev
= NULL
;
220 struct blob_attr
*tb
[__DEV_STATE_MAX
];
221 struct blob_attr
*cur
;
223 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
225 cur
= tb
[DEV_STATE_NAME
];
227 return UBUS_STATUS_INVALID_ARGUMENT
;
229 dev
= device_get(blobmsg_data(cur
), false);
231 return UBUS_STATUS_NOT_FOUND
;
233 cur
= tb
[DEV_STATE_DEFER
];
235 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
240 static struct ubus_method dev_object_methods
[] = {
241 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
242 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
243 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
246 static struct ubus_object_type dev_object_type
=
247 UBUS_OBJECT_TYPE("device", dev_object_methods
);
249 static struct ubus_object dev_object
= {
250 .name
= "network.device",
251 .type
= &dev_object_type
,
252 .methods
= dev_object_methods
,
253 .n_methods
= ARRAY_SIZE(dev_object_methods
),
257 netifd_ubus_add_fd(void)
260 system_fd_set_cloexec(ctx
->sock
.fd
);
264 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
266 static struct uloop_timeout retry
= {
267 .cb
= netifd_ubus_reconnect_timer
,
271 if (ubus_reconnect(ctx
, ubus_path
) != 0) {
272 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
273 uloop_timeout_set(&retry
, t
* 1000);
277 DPRINTF("reconnected to ubus, new id: %08x\n", ctx
->local_id
);
278 netifd_ubus_add_fd();
282 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
284 netifd_ubus_reconnect_timer(NULL
);
287 /* per-interface object */
290 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
291 struct ubus_request_data
*req
, const char *method
,
292 struct blob_attr
*msg
)
294 struct interface
*iface
;
296 iface
= container_of(obj
, struct interface
, ubus
);
297 interface_set_up(iface
);
303 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
304 struct ubus_request_data
*req
, const char *method
,
305 struct blob_attr
*msg
)
307 struct interface
*iface
;
309 iface
= container_of(obj
, struct interface
, ubus
);
310 interface_set_down(iface
);
316 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
318 struct interface_error
*error
;
322 e
= blobmsg_open_array(b
, "errors");
323 list_for_each_entry(error
, &iface
->errors
, list
) {
324 e2
= blobmsg_open_table(b
, NULL
);
326 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
327 blobmsg_add_string(b
, "code", error
->code
);
328 if (error
->data
[0]) {
329 e3
= blobmsg_open_array(b
, "data");
330 for (i
= 0; error
->data
[i
]; i
++)
331 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
332 blobmsg_close_array(b
, e3
);
335 blobmsg_close_table(b
, e2
);
337 blobmsg_close_array(b
, e
);
341 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
,
344 struct device_addr
*addr
;
350 time_t now
= system_get_rtime();
351 vlist_for_each_element(&ip
->addr
, addr
, node
) {
352 if (addr
->enabled
!= enabled
)
355 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
360 if (af
!= (v6
? AF_INET6
: AF_INET
))
363 a
= blobmsg_open_table(&b
, NULL
);
365 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
366 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
367 blobmsg_add_string_buffer(&b
);
369 blobmsg_add_u32(&b
, "mask", addr
->mask
);
371 if (addr
->preferred_until
) {
372 int preferred
= addr
->preferred_until
- now
;
375 blobmsg_add_u32(&b
, "preferred", preferred
);
378 if (addr
->valid_until
)
379 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
381 blobmsg_close_table(&b
, a
);
386 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
388 struct device_route
*route
;
394 time_t now
= system_get_rtime();
395 vlist_for_each_element(&ip
->route
, route
, node
) {
396 if (route
->enabled
!= enabled
)
399 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
404 r
= blobmsg_open_table(&b
, NULL
);
406 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
407 inet_ntop(af
, &route
->addr
, buf
, buflen
);
408 blobmsg_add_string_buffer(&b
);
410 blobmsg_add_u32(&b
, "mask", route
->mask
);
412 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
413 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
414 blobmsg_add_string_buffer(&b
);
416 if (route
->flags
& DEVROUTE_MTU
)
417 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
419 if (route
->flags
& DEVROUTE_METRIC
)
420 blobmsg_add_u32(&b
, "metric", route
->metric
);
422 if (route
->valid_until
)
423 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
425 blobmsg_close_table(&b
, r
);
431 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
433 struct device_prefix
*prefix
;
436 const int buflen
= INET6_ADDRSTRLEN
;
438 time_t now
= system_get_rtime();
439 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
440 a
= blobmsg_open_table(&b
, NULL
);
442 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
443 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
444 blobmsg_add_string_buffer(&b
);
446 blobmsg_add_u32(&b
, "mask", prefix
->length
);
448 if (prefix
->preferred_until
) {
449 int preferred
= prefix
->preferred_until
- now
;
452 blobmsg_add_u32(&b
, "preferred", preferred
);
455 if (prefix
->valid_until
)
456 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
458 c
= blobmsg_open_table(&b
, "assigned");
459 struct device_prefix_assignment
*assign
;
460 vlist_for_each_element(prefix
->assignments
, assign
, node
) {
461 void *d
= blobmsg_open_table(&b
, assign
->name
);
463 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
464 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
465 blobmsg_add_string_buffer(&b
);
467 blobmsg_add_u32(&b
, "mask", assign
->length
);
469 blobmsg_close_table(&b
, d
);
471 blobmsg_close_table(&b
, c
);
473 blobmsg_close_table(&b
, a
);
479 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
483 const int buflen
= INET6_ADDRSTRLEN
;
484 time_t now
= system_get_rtime();
486 struct device_prefix
*prefix
;
487 list_for_each_entry(prefix
, &prefixes
, head
) {
488 struct device_prefix_assignment
*assign
;
489 vlist_for_each_element(prefix
->assignments
, assign
, node
) {
490 if (strcmp(assign
->name
, iface
->name
))
493 a
= blobmsg_open_table(&b
, NULL
);
495 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
496 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
497 blobmsg_add_string_buffer(&b
);
499 blobmsg_add_u32(&b
, "mask", assign
->length
);
501 if (prefix
->preferred_until
) {
502 int preferred
= prefix
->preferred_until
- now
;
505 blobmsg_add_u32(&b
, "preferred", preferred
);
508 if (prefix
->valid_until
)
509 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
511 blobmsg_close_table(&b
, a
);
518 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
521 struct dns_server
*dns
;
525 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
526 if (ip
->no_dns
== enabled
)
529 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
530 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
531 blobmsg_add_string_buffer(&b
);
536 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
,
539 struct dns_search_domain
*dns
;
541 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
542 if (ip
->no_dns
== enabled
)
545 blobmsg_add_string(&b
, NULL
, dns
->name
);
550 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
551 struct ubus_request_data
*req
, const char *method
,
552 struct blob_attr
*msg
)
554 struct interface
*iface
;
555 struct interface_data
*data
;
559 iface
= container_of(obj
, struct interface
, ubus
);
561 blob_buf_init(&b
, 0);
562 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
563 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
564 blobmsg_add_u8(&b
, "available", iface
->available
);
565 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
567 if (iface
->state
== IFS_UP
) {
568 time_t cur
= system_get_rtime();
569 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
570 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
573 if (iface
->proto_handler
)
574 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
576 dev
= iface
->main_dev
.dev
;
577 if (dev
&& !dev
->hidden
&&
578 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
579 blobmsg_add_string(&b
, "device", dev
->ifname
);
581 if (iface
->state
== IFS_UP
) {
582 blobmsg_add_u32(&b
, "metric", iface
->metric
);
583 a
= blobmsg_open_array(&b
, "ipv4-address");
584 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
585 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
586 blobmsg_close_array(&b
, a
);
587 a
= blobmsg_open_array(&b
, "ipv6-address");
588 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
589 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
590 blobmsg_close_array(&b
, a
);
591 a
= blobmsg_open_array(&b
, "ipv6-prefix");
592 interface_ip_dump_prefix_list(&iface
->config_ip
);
593 interface_ip_dump_prefix_list(&iface
->proto_ip
);
594 blobmsg_close_array(&b
, a
);
595 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
596 interface_ip_dump_prefix_assignment_list(iface
);
597 blobmsg_close_array(&b
, a
);
598 a
= blobmsg_open_array(&b
, "route");
599 interface_ip_dump_route_list(&iface
->config_ip
, true);
600 interface_ip_dump_route_list(&iface
->proto_ip
, true);
601 blobmsg_close_array(&b
, a
);
602 a
= blobmsg_open_array(&b
, "dns-server");
603 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
604 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
605 blobmsg_close_array(&b
, a
);
606 a
= blobmsg_open_array(&b
, "dns-search");
607 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
608 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
609 blobmsg_close_array(&b
, a
);
611 inactive
= blobmsg_open_table(&b
, "inactive");
612 a
= blobmsg_open_array(&b
, "ipv4-address");
613 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
614 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
615 blobmsg_close_array(&b
, a
);
616 a
= blobmsg_open_array(&b
, "ipv6-address");
617 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
618 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
619 blobmsg_close_array(&b
, a
);
620 a
= blobmsg_open_array(&b
, "route");
621 interface_ip_dump_route_list(&iface
->config_ip
, false);
622 interface_ip_dump_route_list(&iface
->proto_ip
, false);
623 blobmsg_close_array(&b
, a
);
624 a
= blobmsg_open_array(&b
, "dns-server");
625 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
626 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
627 blobmsg_close_array(&b
, a
);
628 a
= blobmsg_open_array(&b
, "dns-search");
629 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
630 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
631 blobmsg_close_array(&b
, a
);
632 blobmsg_close_table(&b
, inactive
);
635 a
= blobmsg_open_table(&b
, "data");
636 avl_for_each_element(&iface
->data
, data
, node
)
637 blob_put(&b
, blob_id(data
->data
), blob_data(data
->data
), blob_len(data
->data
));
639 blobmsg_close_table(&b
, a
);
641 if (!list_is_empty(&iface
->errors
))
642 netifd_add_interface_errors(&b
, iface
);
644 ubus_send_reply(ctx
, req
, b
.head
);
650 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
651 struct ubus_request_data
*req
, const char *method
,
652 struct blob_attr
*msg
)
654 struct blob_attr
*tb
[__DEV_MAX
];
655 struct interface
*iface
;
657 bool add
= !strncmp(method
, "add", 3);
660 iface
= container_of(obj
, struct interface
, ubus
);
662 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
665 return UBUS_STATUS_INVALID_ARGUMENT
;
669 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
? 2 : 0);
671 ret
= UBUS_STATUS_NOT_FOUND
;
676 device_set_present(dev
, true);
677 if (iface
->device_config
)
678 device_set_config(dev
, &simple_device_type
, iface
->config
);
680 system_if_apply_settings(dev
, &dev
->settings
);
681 ret
= interface_add_link(iface
, dev
);
683 ret
= interface_remove_link(iface
, dev
);
694 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
695 struct ubus_request_data
*req
, const char *method
,
696 struct blob_attr
*msg
)
698 struct interface
*iface
;
700 iface
= container_of(obj
, struct interface
, ubus
);
702 if (!iface
->proto
|| !iface
->proto
->notify
)
703 return UBUS_STATUS_NOT_SUPPORTED
;
705 return iface
->proto
->notify(iface
->proto
, msg
);
709 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
711 struct interface
*iface
;
713 iface
= container_of(timeout
, struct interface
, remove_timer
);
714 vlist_delete(&interfaces
, &iface
->node
);
718 netifd_iface_remove(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
;
724 iface
= container_of(obj
, struct interface
, ubus
);
725 if (iface
->remove_timer
.cb
)
726 return UBUS_STATUS_INVALID_ARGUMENT
;
728 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
729 uloop_timeout_set(&iface
->remove_timer
, 100);
734 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
735 struct ubus_request_data
*req
, const char *method
,
736 struct blob_attr
*msg
)
738 struct interface
*iface
;
740 const struct device_hotplug_ops
*ops
;
742 iface
= container_of(obj
, struct interface
, ubus
);
743 dev
= iface
->main_dev
.dev
;
747 ops
= dev
->hotplug_ops
;
751 return ops
->prepare(dev
);
755 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
756 struct ubus_request_data
*req
, const char *method
,
757 struct blob_attr
*msg
)
759 struct interface
*iface
;
760 struct blob_attr
*cur
;
763 iface
= container_of(obj
, struct interface
, ubus
);
765 blob_for_each_attr(cur
, msg
, rem
) {
766 ret
= interface_add_data(iface
, cur
);
774 static struct ubus_method iface_object_methods
[] = {
775 { .name
= "up", .handler
= netifd_handle_up
},
776 { .name
= "down", .handler
= netifd_handle_down
},
777 { .name
= "status", .handler
= netifd_handle_status
},
778 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
779 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
780 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
781 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
782 { .name
= "remove", .handler
= netifd_iface_remove
},
783 { .name
= "set_data", .handler
= netifd_handle_set_data
},
786 static struct ubus_object_type iface_object_type
=
787 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
790 static struct ubus_object iface_object
= {
791 .name
= "network.interface",
792 .type
= &iface_object_type
,
793 .n_methods
= ARRAY_SIZE(iface_object_methods
),
796 static void netifd_add_object(struct ubus_object
*obj
)
798 int ret
= ubus_add_object(ctx
, obj
);
801 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
804 static const struct blobmsg_policy iface_policy
= {
806 .type
= BLOBMSG_TYPE_STRING
,
810 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
811 struct ubus_request_data
*req
, const char *method
,
812 struct blob_attr
*msg
)
814 struct interface
*iface
;
815 struct blob_attr
*tb
;
818 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
820 return UBUS_STATUS_INVALID_ARGUMENT
;
822 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
824 return UBUS_STATUS_NOT_FOUND
;
826 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
829 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
832 cb
= iface_object_methods
[i
].handler
;
833 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
836 return UBUS_STATUS_INVALID_ARGUMENT
;
839 static void netifd_add_iface_object(void)
841 struct ubus_method
*methods
;
844 methods
= calloc(1, sizeof(iface_object_methods
));
845 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
846 iface_object
.methods
= methods
;
848 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
849 methods
[i
].handler
= netifd_handle_iface
;
850 methods
[i
].policy
= &iface_policy
;
851 methods
[i
].n_policy
= 1;
853 netifd_add_object(&iface_object
);
857 netifd_ubus_init(const char *path
)
862 ctx
= ubus_connect(path
);
866 DPRINTF("connected as %08x\n", ctx
->local_id
);
867 ctx
->connection_lost
= netifd_ubus_connection_lost
;
868 netifd_ubus_add_fd();
870 netifd_add_object(&main_object
);
871 netifd_add_object(&dev_object
);
872 netifd_add_iface_object();
878 netifd_ubus_done(void)
884 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
886 blob_buf_init(&b
, 0);
887 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
888 blobmsg_add_string(&b
, "interface", iface
->name
);
889 ubus_send_event(ctx
, "network.interface", b
.head
);
893 netifd_ubus_add_interface(struct interface
*iface
)
895 struct ubus_object
*obj
= &iface
->ubus
;
898 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
902 obj
->type
= &iface_object_type
;
903 obj
->methods
= iface_object_methods
;
904 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
905 if (ubus_add_object(ctx
, &iface
->ubus
)) {
906 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
913 netifd_ubus_remove_interface(struct interface
*iface
)
915 if (!iface
->ubus
.name
)
918 ubus_remove_object(ctx
, &iface
->ubus
);
919 free((void *) iface
->ubus
.name
);