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 blobmsg_add_string(&b
, "class", prefix
->pclass
);
468 c
= blobmsg_open_table(&b
, "assigned");
469 struct device_prefix_assignment
*assign
;
470 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
471 if (!assign
->name
[0])
474 struct in6_addr addr
= prefix
->addr
;
475 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
477 void *d
= blobmsg_open_table(&b
, assign
->name
);
479 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
480 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
481 blobmsg_add_string_buffer(&b
);
483 blobmsg_add_u32(&b
, "mask", assign
->length
);
485 blobmsg_close_table(&b
, d
);
487 blobmsg_close_table(&b
, c
);
489 blobmsg_close_table(&b
, a
);
495 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
499 const int buflen
= INET6_ADDRSTRLEN
;
500 time_t now
= system_get_rtime();
502 struct device_prefix
*prefix
;
503 list_for_each_entry(prefix
, &prefixes
, head
) {
504 struct device_prefix_assignment
*assign
;
505 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
506 if (strcmp(assign
->name
, iface
->name
))
509 struct in6_addr addr
= prefix
->addr
;
510 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
512 a
= blobmsg_open_table(&b
, NULL
);
514 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
515 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
516 blobmsg_add_string_buffer(&b
);
518 blobmsg_add_u32(&b
, "mask", assign
->length
);
520 if (prefix
->preferred_until
) {
521 int preferred
= prefix
->preferred_until
- now
;
524 blobmsg_add_u32(&b
, "preferred", preferred
);
527 if (prefix
->valid_until
)
528 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
530 blobmsg_close_table(&b
, a
);
537 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
,
540 struct dns_server
*dns
;
544 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
545 if (ip
->no_dns
== enabled
)
548 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
549 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
550 blobmsg_add_string_buffer(&b
);
555 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
,
558 struct dns_search_domain
*dns
;
560 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
561 if (ip
->no_dns
== enabled
)
564 blobmsg_add_string(&b
, NULL
, dns
->name
);
569 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
570 struct ubus_request_data
*req
, const char *method
,
571 struct blob_attr
*msg
)
573 struct interface
*iface
;
574 struct interface_data
*data
;
578 iface
= container_of(obj
, struct interface
, ubus
);
580 blob_buf_init(&b
, 0);
581 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
582 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
583 blobmsg_add_u8(&b
, "available", iface
->available
);
584 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
586 if (iface
->state
== IFS_UP
) {
587 time_t cur
= system_get_rtime();
588 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
589 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
592 if (iface
->proto_handler
)
593 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
595 dev
= iface
->main_dev
.dev
;
596 if (dev
&& !dev
->hidden
&&
597 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
598 blobmsg_add_string(&b
, "device", dev
->ifname
);
600 if (iface
->state
== IFS_UP
) {
601 blobmsg_add_u32(&b
, "metric", iface
->metric
);
602 a
= blobmsg_open_array(&b
, "ipv4-address");
603 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
604 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
605 blobmsg_close_array(&b
, a
);
606 a
= blobmsg_open_array(&b
, "ipv6-address");
607 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
608 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
609 blobmsg_close_array(&b
, a
);
610 a
= blobmsg_open_array(&b
, "ipv6-prefix");
611 interface_ip_dump_prefix_list(&iface
->config_ip
);
612 interface_ip_dump_prefix_list(&iface
->proto_ip
);
613 blobmsg_close_array(&b
, a
);
614 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
615 interface_ip_dump_prefix_assignment_list(iface
);
616 blobmsg_close_array(&b
, a
);
617 a
= blobmsg_open_array(&b
, "route");
618 interface_ip_dump_route_list(&iface
->config_ip
, true);
619 interface_ip_dump_route_list(&iface
->proto_ip
, true);
620 blobmsg_close_array(&b
, a
);
621 a
= blobmsg_open_array(&b
, "dns-server");
622 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
623 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
624 blobmsg_close_array(&b
, a
);
625 a
= blobmsg_open_array(&b
, "dns-search");
626 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
627 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
628 blobmsg_close_array(&b
, a
);
630 inactive
= blobmsg_open_table(&b
, "inactive");
631 a
= blobmsg_open_array(&b
, "ipv4-address");
632 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
633 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
634 blobmsg_close_array(&b
, a
);
635 a
= blobmsg_open_array(&b
, "ipv6-address");
636 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
637 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
638 blobmsg_close_array(&b
, a
);
639 a
= blobmsg_open_array(&b
, "route");
640 interface_ip_dump_route_list(&iface
->config_ip
, false);
641 interface_ip_dump_route_list(&iface
->proto_ip
, false);
642 blobmsg_close_array(&b
, a
);
643 a
= blobmsg_open_array(&b
, "dns-server");
644 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
645 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
646 blobmsg_close_array(&b
, a
);
647 a
= blobmsg_open_array(&b
, "dns-search");
648 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
649 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
650 blobmsg_close_array(&b
, a
);
651 blobmsg_close_table(&b
, inactive
);
654 a
= blobmsg_open_table(&b
, "data");
655 avl_for_each_element(&iface
->data
, data
, node
)
656 blob_put(&b
, blob_id(data
->data
), blob_data(data
->data
), blob_len(data
->data
));
658 blobmsg_close_table(&b
, a
);
660 if (!list_is_empty(&iface
->errors
))
661 netifd_add_interface_errors(&b
, iface
);
663 ubus_send_reply(ctx
, req
, b
.head
);
669 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
670 struct ubus_request_data
*req
, const char *method
,
671 struct blob_attr
*msg
)
673 struct blob_attr
*tb
[__DEV_MAX
];
674 struct interface
*iface
;
676 bool add
= !strncmp(method
, "add", 3);
679 iface
= container_of(obj
, struct interface
, ubus
);
681 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
684 return UBUS_STATUS_INVALID_ARGUMENT
;
688 dev
= device_get(blobmsg_data(tb
[DEV_NAME
]), add
? 2 : 0);
690 ret
= UBUS_STATUS_NOT_FOUND
;
695 device_set_present(dev
, true);
696 if (iface
->device_config
)
697 device_set_config(dev
, &simple_device_type
, iface
->config
);
699 system_if_apply_settings(dev
, &dev
->settings
);
700 ret
= interface_add_link(iface
, dev
);
702 ret
= interface_remove_link(iface
, dev
);
713 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
714 struct ubus_request_data
*req
, const char *method
,
715 struct blob_attr
*msg
)
717 struct interface
*iface
;
719 iface
= container_of(obj
, struct interface
, ubus
);
721 if (!iface
->proto
|| !iface
->proto
->notify
)
722 return UBUS_STATUS_NOT_SUPPORTED
;
724 return iface
->proto
->notify(iface
->proto
, msg
);
728 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
730 struct interface
*iface
;
732 iface
= container_of(timeout
, struct interface
, remove_timer
);
733 vlist_delete(&interfaces
, &iface
->node
);
737 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
738 struct ubus_request_data
*req
, const char *method
,
739 struct blob_attr
*msg
)
741 struct interface
*iface
;
743 iface
= container_of(obj
, struct interface
, ubus
);
744 if (iface
->remove_timer
.cb
)
745 return UBUS_STATUS_INVALID_ARGUMENT
;
747 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
748 uloop_timeout_set(&iface
->remove_timer
, 100);
753 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
754 struct ubus_request_data
*req
, const char *method
,
755 struct blob_attr
*msg
)
757 struct interface
*iface
;
759 const struct device_hotplug_ops
*ops
;
761 iface
= container_of(obj
, struct interface
, ubus
);
762 dev
= iface
->main_dev
.dev
;
766 ops
= dev
->hotplug_ops
;
770 return ops
->prepare(dev
);
774 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
775 struct ubus_request_data
*req
, const char *method
,
776 struct blob_attr
*msg
)
778 struct interface
*iface
;
779 struct blob_attr
*cur
;
782 iface
= container_of(obj
, struct interface
, ubus
);
784 blob_for_each_attr(cur
, msg
, rem
) {
785 ret
= interface_add_data(iface
, cur
);
793 static struct ubus_method iface_object_methods
[] = {
794 { .name
= "up", .handler
= netifd_handle_up
},
795 { .name
= "down", .handler
= netifd_handle_down
},
796 { .name
= "status", .handler
= netifd_handle_status
},
797 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
798 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_policy
),
799 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_policy
),
800 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
801 { .name
= "remove", .handler
= netifd_iface_remove
},
802 { .name
= "set_data", .handler
= netifd_handle_set_data
},
805 static struct ubus_object_type iface_object_type
=
806 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
809 static struct ubus_object iface_object
= {
810 .name
= "network.interface",
811 .type
= &iface_object_type
,
812 .n_methods
= ARRAY_SIZE(iface_object_methods
),
815 static void netifd_add_object(struct ubus_object
*obj
)
817 int ret
= ubus_add_object(ctx
, obj
);
820 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
823 static const struct blobmsg_policy iface_policy
= {
825 .type
= BLOBMSG_TYPE_STRING
,
829 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
830 struct ubus_request_data
*req
, const char *method
,
831 struct blob_attr
*msg
)
833 struct interface
*iface
;
834 struct blob_attr
*tb
;
837 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
839 return UBUS_STATUS_INVALID_ARGUMENT
;
841 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
843 return UBUS_STATUS_NOT_FOUND
;
845 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
848 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
851 cb
= iface_object_methods
[i
].handler
;
852 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
855 return UBUS_STATUS_INVALID_ARGUMENT
;
858 static void netifd_add_iface_object(void)
860 struct ubus_method
*methods
;
863 methods
= calloc(1, sizeof(iface_object_methods
));
864 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
865 iface_object
.methods
= methods
;
867 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
868 methods
[i
].handler
= netifd_handle_iface
;
869 methods
[i
].policy
= &iface_policy
;
870 methods
[i
].n_policy
= 1;
872 netifd_add_object(&iface_object
);
876 netifd_ubus_init(const char *path
)
881 ctx
= ubus_connect(path
);
885 DPRINTF("connected as %08x\n", ctx
->local_id
);
886 ctx
->connection_lost
= netifd_ubus_connection_lost
;
887 netifd_ubus_add_fd();
889 netifd_add_object(&main_object
);
890 netifd_add_object(&dev_object
);
891 netifd_add_iface_object();
897 netifd_ubus_done(void)
903 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
905 blob_buf_init(&b
, 0);
906 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
907 blobmsg_add_string(&b
, "interface", iface
->name
);
908 ubus_send_event(ctx
, "network.interface", b
.head
);
912 netifd_ubus_add_interface(struct interface
*iface
)
914 struct ubus_object
*obj
= &iface
->ubus
;
917 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
921 obj
->type
= &iface_object_type
;
922 obj
->methods
= iface_object_methods
;
923 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
924 if (ubus_add_object(ctx
, &iface
->ubus
)) {
925 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
932 netifd_ubus_remove_interface(struct interface
*iface
)
934 if (!iface
->ubus
.name
)
937 ubus_remove_object(ctx
, &iface
->ubus
);
938 free((void *) iface
->ubus
.name
);