2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <arpa/inet.h>
21 #include "interface.h"
27 struct ubus_context
*ubus_ctx
= NULL
;
28 static struct blob_buf b
;
29 static const char *ubus_path
;
34 netifd_handle_restart(struct ubus_context
*ctx
, struct ubus_object
*obj
,
35 struct ubus_request_data
*req
, const char *method
,
36 struct blob_attr
*msg
)
43 netifd_handle_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
44 struct ubus_request_data
*req
, const char *method
,
45 struct blob_attr
*msg
)
48 return UBUS_STATUS_NOT_FOUND
;
50 return UBUS_STATUS_OK
;
60 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
61 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
62 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
63 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
67 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
68 struct ubus_request_data
*req
, const char *method
,
69 struct blob_attr
*msg
)
71 struct blob_attr
*tb
[__HR_MAX
];
72 struct interface
*iface
= NULL
;
76 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
78 return UBUS_STATUS_INVALID_ARGUMENT
;
81 v6
= blobmsg_get_bool(tb
[HR_V6
]);
84 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
86 memset(&a
, 0, sizeof(a
));
87 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
88 return UBUS_STATUS_INVALID_ARGUMENT
;
91 iface
= interface_ip_add_target_route(&a
, v6
, iface
);
93 return UBUS_STATUS_NOT_FOUND
;
96 blobmsg_add_string(&b
, "interface", iface
->name
);
97 ubus_send_reply(ctx
, req
, b
.head
);
103 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
104 struct ubus_request_data
*req
, const char *method
,
105 struct blob_attr
*msg
)
107 blob_buf_init(&b
, 0);
108 proto_dump_handlers(&b
);
109 ubus_send_reply(ctx
, req
, b
.head
);
120 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
121 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
125 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
126 struct ubus_request_data
*req
, const char *method
,
127 struct blob_attr
*msg
)
129 struct blob_attr
*tb
[__DI_MAX
];
130 struct interface
*iface
;
131 struct blob_attr
*config
;
133 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
136 return UBUS_STATUS_INVALID_ARGUMENT
;
138 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
140 iface
= interface_alloc(name
, msg
, true);
142 return UBUS_STATUS_UNKNOWN_ERROR
;
144 config
= blob_memdup(msg
);
148 if (!interface_add(iface
, config
))
149 goto error_free_config
;
151 return UBUS_STATUS_OK
;
157 return UBUS_STATUS_UNKNOWN_ERROR
;
167 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
168 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
169 [NETNS_UPDOWN_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
170 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
174 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
175 struct ubus_request_data
*req
, const char *method
,
176 struct blob_attr
*msg
)
178 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
183 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
184 if (!tb
[NETNS_UPDOWN_JAIL
] || !tb
[NETNS_UPDOWN_PID
])
185 return UBUS_STATUS_INVALID_ARGUMENT
;
187 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
188 jail
= blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]);
189 netns_pid
= blobmsg_get_u32(tb
[NETNS_UPDOWN_PID
]);
192 interface_start_jail(jail
, netns_pid
);
194 interface_stop_jail(jail
, netns_pid
);
196 return UBUS_STATUS_OK
;
199 static struct ubus_method main_object_methods
[] = {
200 { .name
= "restart", .handler
= netifd_handle_restart
},
201 { .name
= "reload", .handler
= netifd_handle_reload
},
202 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
203 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
204 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
205 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
208 static struct ubus_object_type main_object_type
=
209 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
211 static struct ubus_object main_object
= {
213 .type
= &main_object_type
,
214 .methods
= main_object_methods
,
215 .n_methods
= ARRAY_SIZE(main_object_methods
),
223 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
224 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
228 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
229 struct ubus_request_data
*req
, const char *method
,
230 struct blob_attr
*msg
)
232 struct device
*dev
= NULL
;
233 struct blob_attr
*tb
[__DEV_MAX
];
235 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
238 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
240 return UBUS_STATUS_INVALID_ARGUMENT
;
243 blob_buf_init(&b
, 0);
244 device_dump_status(&b
, dev
);
245 ubus_send_reply(ctx
, req
, b
.head
);
256 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
257 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
258 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
262 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
263 struct ubus_request_data
*req
, const char *method
,
264 struct blob_attr
*msg
)
266 struct device
*dev
= NULL
;
267 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
268 struct blob_attr
*cur
;
271 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
273 if (!tb
[ALIAS_ATTR_ALIAS
])
274 return UBUS_STATUS_INVALID_ARGUMENT
;
276 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
277 dev
= device_get(blobmsg_data(cur
), true);
279 return UBUS_STATUS_NOT_FOUND
;
282 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
283 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
286 if (!blobmsg_check_attr(cur
, false))
289 alias_notify_device(blobmsg_data(cur
), dev
);
294 device_free_unused();
295 return UBUS_STATUS_INVALID_ARGUMENT
;
301 DEV_STATE_AUTH_STATUS
,
305 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
306 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
307 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
308 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
312 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
313 struct ubus_request_data
*req
, const char *method
,
314 struct blob_attr
*msg
)
316 struct device
*dev
= NULL
;
317 struct blob_attr
*tb
[__DEV_STATE_MAX
];
318 struct blob_attr
*cur
;
320 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
322 cur
= tb
[DEV_STATE_NAME
];
324 return UBUS_STATUS_INVALID_ARGUMENT
;
326 dev
= device_find(blobmsg_data(cur
));
328 return UBUS_STATUS_NOT_FOUND
;
330 cur
= tb
[DEV_STATE_DEFER
];
332 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
334 cur
= tb
[DEV_STATE_AUTH_STATUS
];
336 device_set_auth_status(dev
, !!blobmsg_get_u8(cur
));
343 DEV_HOTPLUG_ATTR_NAME
,
344 DEV_HOTPLUG_ATTR_ADD
,
345 __DEV_HOTPLUG_ATTR_MAX
,
348 static const struct blobmsg_policy dev_hotplug_policy
[__DEV_HOTPLUG_ATTR_MAX
] = {
349 [DEV_HOTPLUG_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
350 [DEV_HOTPLUG_ATTR_ADD
] = { "add", BLOBMSG_TYPE_BOOL
},
354 netifd_handle_dev_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
355 struct ubus_request_data
*req
, const char *method
,
356 struct blob_attr
*msg
)
358 struct blob_attr
*tb
[__DEV_HOTPLUG_ATTR_MAX
];
361 blobmsg_parse(dev_hotplug_policy
, __DEV_HOTPLUG_ATTR_MAX
, tb
,
362 blob_data(msg
), blob_len(msg
));
364 if (!tb
[DEV_HOTPLUG_ATTR_NAME
] || !tb
[DEV_HOTPLUG_ATTR_ADD
])
365 return UBUS_STATUS_INVALID_ARGUMENT
;
367 name
= blobmsg_get_string(tb
[DEV_HOTPLUG_ATTR_NAME
]);
368 device_hotplug_event(name
, blobmsg_get_bool(tb
[DEV_HOTPLUG_ATTR_ADD
]));
375 netifd_handle_stp_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
376 struct ubus_request_data
*req
, const char *method
,
377 struct blob_attr
*msg
)
384 static struct ubus_method dev_object_methods
[] = {
385 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
386 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
387 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
389 UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug
, dev_hotplug_policy
),
391 UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init
)
394 static struct ubus_object_type dev_object_type
=
395 UBUS_OBJECT_TYPE("device", dev_object_methods
);
397 static struct ubus_object dev_object
= {
398 .name
= "network.device",
399 .type
= &dev_object_type
,
400 .methods
= dev_object_methods
,
401 .n_methods
= ARRAY_SIZE(dev_object_methods
),
405 netifd_ubus_add_fd(void)
407 ubus_add_uloop(ubus_ctx
);
408 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
411 void netifd_ubus_device_notify(const char *event
, struct blob_attr
*data
, int timeout
)
413 ubus_notify(ubus_ctx
, &dev_object
, event
, data
, timeout
);
417 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
419 static struct uloop_timeout retry
= {
420 .cb
= netifd_ubus_reconnect_timer
,
424 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
425 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
426 uloop_timeout_set(&retry
, t
* 1000);
430 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
431 netifd_ubus_add_fd();
435 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
437 netifd_ubus_reconnect_timer(NULL
);
440 /* per-interface object */
443 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
444 struct ubus_request_data
*req
, const char *method
,
445 struct blob_attr
*msg
)
447 struct interface
*iface
;
449 iface
= container_of(obj
, struct interface
, ubus
);
450 interface_set_up(iface
);
456 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
457 struct ubus_request_data
*req
, const char *method
,
458 struct blob_attr
*msg
)
460 struct interface
*iface
;
462 iface
= container_of(obj
, struct interface
, ubus
);
463 interface_set_down(iface
);
469 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
470 struct ubus_request_data
*req
, const char *method
,
471 struct blob_attr
*msg
)
473 struct interface
*iface
;
475 iface
= container_of(obj
, struct interface
, ubus
);
476 interface_renew(iface
);
482 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
484 struct interface_error
*error
;
488 e
= blobmsg_open_array(b
, "errors");
489 list_for_each_entry(error
, &iface
->errors
, list
) {
490 e2
= blobmsg_open_table(b
, NULL
);
492 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
493 blobmsg_add_string(b
, "code", error
->code
);
494 if (error
->data
[0]) {
495 e3
= blobmsg_open_array(b
, "data");
496 for (i
= 0; error
->data
[i
]; i
++)
497 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
498 blobmsg_close_array(b
, e3
);
501 blobmsg_close_table(b
, e2
);
503 blobmsg_close_array(b
, e
);
507 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
509 struct device_addr
*addr
;
515 time_t now
= system_get_rtime();
516 vlist_for_each_element(&ip
->addr
, addr
, node
) {
517 if (addr
->enabled
!= enabled
)
520 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
525 if (af
!= (v6
? AF_INET6
: AF_INET
))
528 a
= blobmsg_open_table(&b
, NULL
);
530 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
531 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
532 blobmsg_add_string_buffer(&b
);
534 blobmsg_add_u32(&b
, "mask", addr
->mask
);
536 if (addr
->point_to_point
) {
537 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
538 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
539 blobmsg_add_string_buffer(&b
);
542 if (addr
->preferred_until
) {
543 int preferred
= addr
->preferred_until
- now
;
546 blobmsg_add_u32(&b
, "preferred", preferred
);
549 if (addr
->valid_until
)
550 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
553 blobmsg_add_string(&b
, "class", addr
->pclass
);
555 blobmsg_close_table(&b
, a
);
560 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
562 struct device_neighbor
*neighbor
;
568 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
569 if (neighbor
->enabled
!= enabled
)
572 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
577 r
= blobmsg_open_table(&b
, NULL
);
579 if (neighbor
->flags
& DEVNEIGH_MAC
)
580 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
582 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
583 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
584 blobmsg_add_string_buffer(&b
);
587 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
589 if (neighbor
->router
)
590 blobmsg_add_u32(&b
, "router", neighbor
->router
);
592 blobmsg_close_table(&b
, r
);
597 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
599 struct device_route
*route
;
605 time_t now
= system_get_rtime();
606 vlist_for_each_element(&ip
->route
, route
, node
) {
607 if (route
->enabled
!= enabled
)
610 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
613 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
618 r
= blobmsg_open_table(&b
, NULL
);
620 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
621 inet_ntop(af
, &route
->addr
, buf
, buflen
);
622 blobmsg_add_string_buffer(&b
);
624 blobmsg_add_u32(&b
, "mask", route
->mask
);
626 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
627 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
628 blobmsg_add_string_buffer(&b
);
630 if (route
->flags
& DEVROUTE_TYPE
)
631 blobmsg_add_u32(&b
, "type", route
->type
);
633 if (route
->flags
& DEVROUTE_PROTO
)
634 blobmsg_add_u32(&b
, "proto", route
->proto
);
636 if (route
->flags
& DEVROUTE_MTU
)
637 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
639 if (route
->flags
& DEVROUTE_METRIC
)
640 blobmsg_add_u32(&b
, "metric", route
->metric
);
642 if (route
->flags
& DEVROUTE_TABLE
)
643 blobmsg_add_u32(&b
, "table", route
->table
);
645 if (route
->valid_until
)
646 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
648 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
649 inet_ntop(af
, &route
->source
, buf
, buflen
);
650 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
651 blobmsg_add_string_buffer(&b
);
653 blobmsg_close_table(&b
, r
);
659 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
661 struct device_prefix
*prefix
;
664 const int buflen
= INET6_ADDRSTRLEN
;
666 time_t now
= system_get_rtime();
667 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
668 a
= blobmsg_open_table(&b
, NULL
);
670 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
671 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
672 blobmsg_add_string_buffer(&b
);
674 blobmsg_add_u32(&b
, "mask", prefix
->length
);
676 if (prefix
->preferred_until
) {
677 int preferred
= prefix
->preferred_until
- now
;
680 blobmsg_add_u32(&b
, "preferred", preferred
);
683 if (prefix
->valid_until
)
684 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
686 blobmsg_add_string(&b
, "class", prefix
->pclass
);
688 c
= blobmsg_open_table(&b
, "assigned");
689 struct device_prefix_assignment
*assign
;
690 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
691 if (!assign
->name
[0])
694 struct in6_addr addr
= prefix
->addr
;
695 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
697 void *d
= blobmsg_open_table(&b
, assign
->name
);
699 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
700 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
701 blobmsg_add_string_buffer(&b
);
703 blobmsg_add_u32(&b
, "mask", assign
->length
);
705 blobmsg_close_table(&b
, d
);
707 blobmsg_close_table(&b
, c
);
709 blobmsg_close_table(&b
, a
);
715 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
719 const int buflen
= INET6_ADDRSTRLEN
;
720 time_t now
= system_get_rtime();
722 struct device_prefix
*prefix
;
723 list_for_each_entry(prefix
, &prefixes
, head
) {
724 struct device_prefix_assignment
*assign
;
725 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
726 if (strcmp(assign
->name
, iface
->name
))
729 struct in6_addr addr
= prefix
->addr
;
730 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
732 a
= blobmsg_open_table(&b
, NULL
);
734 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
735 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
736 blobmsg_add_string_buffer(&b
);
738 blobmsg_add_u32(&b
, "mask", assign
->length
);
740 if (prefix
->preferred_until
) {
741 int preferred
= prefix
->preferred_until
- now
;
744 blobmsg_add_u32(&b
, "preferred", preferred
);
747 if (prefix
->valid_until
)
748 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
750 void *c
= blobmsg_open_table(&b
, "local-address");
751 if (assign
->enabled
) {
752 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
753 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
754 blobmsg_add_string_buffer(&b
);
756 blobmsg_add_u32(&b
, "mask", assign
->length
);
758 blobmsg_close_table(&b
, c
);
760 blobmsg_close_table(&b
, a
);
766 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
768 struct dns_server
*dns
;
772 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
773 if (ip
->no_dns
== enabled
)
776 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
777 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
778 blobmsg_add_string_buffer(&b
);
783 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
785 struct dns_search_domain
*dns
;
787 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
788 if (ip
->no_dns
== enabled
)
791 blobmsg_add_string(&b
, NULL
, dns
->name
);
796 netifd_dump_status(struct interface
*iface
)
798 struct interface_data
*data
;
802 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
803 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
804 blobmsg_add_u8(&b
, "available", iface
->available
);
805 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
806 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
808 if (iface
->state
== IFS_UP
) {
809 time_t cur
= system_get_rtime();
810 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
811 if (iface
->l3_dev
.dev
)
812 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
815 if (iface
->proto_handler
)
816 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
818 dev
= iface
->main_dev
.dev
;
819 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
820 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
821 blobmsg_add_string(&b
, "device", dev
->ifname
);
824 blobmsg_add_string(&b
, "jail", iface
->jail
);
826 if (iface
->jail_ifname
)
827 blobmsg_add_string(&b
, "jail_ifname", iface
->jail_ifname
);
829 if (iface
->state
== IFS_UP
) {
830 if (iface
->updated
) {
831 a
= blobmsg_open_array(&b
, "updated");
833 if (iface
->updated
& IUF_ADDRESS
)
834 blobmsg_add_string(&b
, NULL
, "addresses");
835 if (iface
->updated
& IUF_ROUTE
)
836 blobmsg_add_string(&b
, NULL
, "routes");
837 if (iface
->updated
& IUF_PREFIX
)
838 blobmsg_add_string(&b
, NULL
, "prefixes");
839 if (iface
->updated
& IUF_DATA
)
840 blobmsg_add_string(&b
, NULL
, "data");
842 blobmsg_close_array(&b
, a
);
846 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
848 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
849 blobmsg_add_u32(&b
, "metric", iface
->metric
);
850 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
851 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
852 if (iface
->assignment_weight
)
853 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
854 a
= blobmsg_open_array(&b
, "ipv4-address");
855 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
856 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
857 blobmsg_close_array(&b
, a
);
858 a
= blobmsg_open_array(&b
, "ipv6-address");
859 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
860 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
861 blobmsg_close_array(&b
, a
);
862 a
= blobmsg_open_array(&b
, "ipv6-prefix");
863 interface_ip_dump_prefix_list(&iface
->config_ip
);
864 interface_ip_dump_prefix_list(&iface
->proto_ip
);
865 blobmsg_close_array(&b
, a
);
866 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
867 interface_ip_dump_prefix_assignment_list(iface
);
868 blobmsg_close_array(&b
, a
);
869 a
= blobmsg_open_array(&b
, "route");
870 interface_ip_dump_route_list(&iface
->config_ip
, true);
871 interface_ip_dump_route_list(&iface
->proto_ip
, true);
872 blobmsg_close_array(&b
, a
);
873 a
= blobmsg_open_array(&b
, "dns-server");
874 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
875 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
876 blobmsg_close_array(&b
, a
);
877 a
= blobmsg_open_array(&b
, "dns-search");
878 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
879 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
880 blobmsg_close_array(&b
, a
);
881 a
= blobmsg_open_array(&b
, "neighbors");
882 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
883 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
884 blobmsg_close_array(&b
, a
);
886 inactive
= blobmsg_open_table(&b
, "inactive");
887 a
= blobmsg_open_array(&b
, "ipv4-address");
888 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
889 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
890 blobmsg_close_array(&b
, a
);
891 a
= blobmsg_open_array(&b
, "ipv6-address");
892 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
893 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
894 blobmsg_close_array(&b
, a
);
895 a
= blobmsg_open_array(&b
, "route");
896 interface_ip_dump_route_list(&iface
->config_ip
, false);
897 interface_ip_dump_route_list(&iface
->proto_ip
, false);
898 blobmsg_close_array(&b
, a
);
899 a
= blobmsg_open_array(&b
, "dns-server");
900 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
901 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
902 blobmsg_close_array(&b
, a
);
903 a
= blobmsg_open_array(&b
, "dns-search");
904 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
905 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
906 blobmsg_close_array(&b
, a
);
907 a
= blobmsg_open_array(&b
, "neighbors");
908 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
909 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
910 blobmsg_close_array(&b
, a
);
911 blobmsg_close_table(&b
, inactive
);
914 a
= blobmsg_open_table(&b
, "data");
915 avl_for_each_element(&iface
->data
, data
, node
)
916 blobmsg_add_blob(&b
, data
->data
);
918 blobmsg_close_table(&b
, a
);
920 if (!list_empty(&iface
->errors
))
921 netifd_add_interface_errors(&b
, iface
);
925 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
926 struct ubus_request_data
*req
, const char *method
,
927 struct blob_attr
*msg
)
929 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
931 blob_buf_init(&b
, 0);
932 netifd_dump_status(iface
);
933 ubus_send_reply(ctx
, req
, b
.head
);
940 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
941 struct ubus_request_data
*req
, const char *method
,
942 struct blob_attr
*msg
)
944 blob_buf_init(&b
, 0);
945 void *a
= blobmsg_open_array(&b
, "interface");
947 struct interface
*iface
;
948 vlist_for_each_element(&interfaces
, iface
, node
) {
949 void *i
= blobmsg_open_table(&b
, NULL
);
950 blobmsg_add_string(&b
, "interface", iface
->name
);
951 netifd_dump_status(iface
);
952 blobmsg_close_table(&b
, i
);
955 blobmsg_close_array(&b
, a
);
956 ubus_send_reply(ctx
, req
, b
.head
);
968 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
969 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
970 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
971 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
975 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
976 struct ubus_request_data
*req
, const char *method
,
977 struct blob_attr
*msg
)
979 struct blob_attr
*tb
[__DEV_LINK_MAX
];
980 struct blob_attr
*cur
;
981 struct interface
*iface
;
982 bool add
= !strncmp(method
, "add", 3);
983 bool link_ext
= true;
985 iface
= container_of(obj
, struct interface
, ubus
);
987 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
989 if (!tb
[DEV_LINK_NAME
])
990 return UBUS_STATUS_INVALID_ARGUMENT
;
992 cur
= tb
[DEV_LINK_EXT
];
994 link_ext
= blobmsg_get_bool(cur
);
996 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
997 tb
[DEV_LINK_VLAN
], add
, link_ext
);
1002 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1003 struct ubus_request_data
*req
, const char *method
,
1004 struct blob_attr
*msg
)
1006 struct interface
*iface
;
1008 iface
= container_of(obj
, struct interface
, ubus
);
1010 if (!iface
->proto
|| !iface
->proto
->notify
)
1011 return UBUS_STATUS_NOT_SUPPORTED
;
1013 return iface
->proto
->notify(iface
->proto
, msg
);
1017 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
1019 struct interface
*iface
;
1021 iface
= container_of(timeout
, struct interface
, remove_timer
);
1022 vlist_delete(&interfaces
, &iface
->node
);
1026 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1027 struct ubus_request_data
*req
, const char *method
,
1028 struct blob_attr
*msg
)
1030 struct interface
*iface
;
1032 iface
= container_of(obj
, struct interface
, ubus
);
1033 if (iface
->remove_timer
.cb
)
1034 return UBUS_STATUS_INVALID_ARGUMENT
;
1036 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
1037 uloop_timeout_set(&iface
->remove_timer
, 100);
1042 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1043 struct ubus_request_data
*req
, const char *method
,
1044 struct blob_attr
*msg
)
1046 struct interface
*iface
;
1047 struct device
*dev
, *bridge_dev
= NULL
;
1048 const struct device_hotplug_ops
*ops
;
1050 iface
= container_of(obj
, struct interface
, ubus
);
1051 dev
= iface
->main_dev
.dev
;
1055 ops
= dev
->hotplug_ops
;
1059 ops
->prepare(dev
, &bridge_dev
);
1062 blob_buf_init(&b
, 0);
1064 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1065 ubus_send_reply(ctx
, req
, b
.head
);
1071 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1072 struct ubus_request_data
*req
, const char *method
,
1073 struct blob_attr
*msg
)
1075 struct interface
*iface
;
1077 iface
= container_of(obj
, struct interface
, ubus
);
1079 return interface_parse_data(iface
, msg
);
1082 static struct ubus_method iface_object_methods
[] = {
1083 { .name
= "up", .handler
= netifd_handle_up
},
1084 { .name
= "down", .handler
= netifd_handle_down
},
1085 { .name
= "renew", .handler
= netifd_handle_renew
},
1086 { .name
= "status", .handler
= netifd_handle_status
},
1087 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1088 { .name
= "dump", .handler
= netifd_handle_dump
},
1089 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1090 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1091 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1092 { .name
= "remove", .handler
= netifd_iface_remove
},
1093 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1096 static struct ubus_object_type iface_object_type
=
1097 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1100 static struct ubus_object iface_object
= {
1101 .name
= "network.interface",
1102 .type
= &iface_object_type
,
1103 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1106 static void netifd_add_object(struct ubus_object
*obj
)
1108 int ret
= ubus_add_object(ubus_ctx
, obj
);
1111 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1114 static const struct blobmsg_policy iface_policy
= {
1115 .name
= "interface",
1116 .type
= BLOBMSG_TYPE_STRING
,
1120 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1121 struct ubus_request_data
*req
, const char *method
,
1122 struct blob_attr
*msg
)
1124 struct interface
*iface
;
1125 struct blob_attr
*tb
;
1128 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1130 return UBUS_STATUS_INVALID_ARGUMENT
;
1132 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1134 return UBUS_STATUS_NOT_FOUND
;
1136 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1139 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1142 cb
= iface_object_methods
[i
].handler
;
1143 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1146 return UBUS_STATUS_INVALID_ARGUMENT
;
1149 static void netifd_add_iface_object(void)
1151 struct ubus_method
*methods
;
1154 methods
= calloc(1, sizeof(iface_object_methods
));
1158 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1159 iface_object
.methods
= methods
;
1161 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1162 if (methods
[i
].handler
== netifd_handle_dump
)
1165 methods
[i
].handler
= netifd_handle_iface
;
1166 methods
[i
].policy
= &iface_policy
;
1167 methods
[i
].n_policy
= 1;
1169 netifd_add_object(&iface_object
);
1172 static struct wireless_device
*
1173 get_wdev(struct blob_attr
*msg
, int *ret
)
1175 struct blobmsg_policy wdev_policy
= {
1177 .type
= BLOBMSG_TYPE_STRING
,
1179 struct blob_attr
*dev_attr
;
1180 struct wireless_device
*wdev
= NULL
;
1183 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1185 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1189 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1191 *ret
= UBUS_STATUS_NOT_FOUND
;
1200 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1201 struct ubus_request_data
*req
, const char *method
,
1202 struct blob_attr
*msg
)
1204 struct wireless_device
*wdev
;
1207 wdev
= get_wdev(msg
, &ret
);
1208 if (ret
== UBUS_STATUS_NOT_FOUND
)
1212 wireless_device_reconf(wdev
);
1214 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1215 wireless_device_reconf(wdev
);
1222 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1223 struct ubus_request_data
*req
, const char *method
,
1224 struct blob_attr
*msg
)
1226 struct wireless_device
*wdev
;
1229 wdev
= get_wdev(msg
, &ret
);
1230 if (ret
== UBUS_STATUS_NOT_FOUND
)
1234 wireless_device_set_up(wdev
);
1236 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1237 wireless_device_set_up(wdev
);
1244 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1245 struct ubus_request_data
*req
, const char *method
,
1246 struct blob_attr
*msg
)
1248 struct wireless_device
*wdev
;
1251 wdev
= get_wdev(msg
, &ret
);
1252 if (ret
== UBUS_STATUS_NOT_FOUND
)
1256 wireless_device_set_down(wdev
);
1258 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1259 wireless_device_set_down(wdev
);
1266 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1267 struct ubus_request_data
*req
, const char *method
,
1268 struct blob_attr
*msg
)
1270 struct wireless_device
*wdev
;
1273 wdev
= get_wdev(msg
, &ret
);
1274 if (ret
== UBUS_STATUS_NOT_FOUND
)
1277 blob_buf_init(&b
, 0);
1279 wireless_device_status(wdev
, &b
);
1281 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1282 wireless_device_status(wdev
, &b
);
1284 ubus_send_reply(ctx
, req
, b
.head
);
1289 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1290 struct ubus_request_data
*req
, const char *method
,
1291 struct blob_attr
*msg
)
1293 struct wireless_device
*wdev
;
1296 wdev
= get_wdev(msg
, &ret
);
1297 if (ret
== UBUS_STATUS_NOT_FOUND
)
1300 blob_buf_init(&b
, 0);
1302 wireless_device_get_validate(wdev
, &b
);
1304 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1305 wireless_device_get_validate(wdev
, &b
);
1307 ubus_send_reply(ctx
, req
, b
.head
);
1312 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1313 struct ubus_request_data
*req
, const char *method
,
1314 struct blob_attr
*msg
)
1316 struct wireless_device
*wdev
;
1319 wdev
= get_wdev(msg
, &ret
);
1323 return wireless_device_notify(wdev
, msg
, req
);
1326 static struct ubus_method wireless_object_methods
[] = {
1327 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1328 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1329 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1330 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1331 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1332 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1335 static struct ubus_object_type wireless_object_type
=
1336 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1339 static struct ubus_object wireless_object
= {
1340 .name
= "network.wireless",
1341 .type
= &wireless_object_type
,
1342 .methods
= wireless_object_methods
,
1343 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1347 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1348 ubus_data_handler_t data_cb
, void *data
)
1350 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1354 netifd_ubus_init(const char *path
)
1359 ubus_ctx
= ubus_connect(path
);
1363 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1364 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1365 netifd_ubus_add_fd();
1367 netifd_add_object(&main_object
);
1368 netifd_add_object(&dev_object
);
1369 netifd_add_object(&wireless_object
);
1370 netifd_add_iface_object();
1376 netifd_ubus_done(void)
1378 ubus_free(ubus_ctx
);
1382 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1384 blob_buf_init(&b
, 0);
1385 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1386 blobmsg_add_string(&b
, "interface", iface
->name
);
1387 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1391 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1393 const char *event
= (up
) ? "interface.update" : "interface.down";
1394 blob_buf_init(&b
, 0);
1395 blobmsg_add_string(&b
, "interface", iface
->name
);
1396 netifd_dump_status(iface
);
1397 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1398 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1402 netifd_ubus_add_interface(struct interface
*iface
)
1404 struct ubus_object
*obj
= &iface
->ubus
;
1407 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1411 obj
->type
= &iface_object_type
;
1412 obj
->methods
= iface_object_methods
;
1413 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1414 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1415 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1422 netifd_ubus_remove_interface(struct interface
*iface
)
1424 if (!iface
->ubus
.name
)
1427 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1428 free((void *) iface
->ubus
.name
);