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
;
166 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
167 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
168 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
172 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
173 struct ubus_request_data
*req
, const char *method
,
174 struct blob_attr
*msg
)
176 int target_netns_fd
= ubus_request_get_caller_fd(req
);
177 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
180 if (target_netns_fd
< 0)
181 return UBUS_STATUS_INVALID_ARGUMENT
;
183 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
185 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
188 if (!tb
[NETNS_UPDOWN_JAIL
])
189 return UBUS_STATUS_INVALID_ARGUMENT
;
191 interface_start_jail(target_netns_fd
, blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]));
193 interface_stop_jail(target_netns_fd
);
195 close(target_netns_fd
);
197 return UBUS_STATUS_OK
;
200 static struct ubus_method main_object_methods
[] = {
201 { .name
= "restart", .handler
= netifd_handle_restart
},
202 { .name
= "reload", .handler
= netifd_handle_reload
},
203 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
204 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
205 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
206 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
209 static struct ubus_object_type main_object_type
=
210 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
212 static struct ubus_object main_object
= {
214 .type
= &main_object_type
,
215 .methods
= main_object_methods
,
216 .n_methods
= ARRAY_SIZE(main_object_methods
),
224 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
225 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
229 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
230 struct ubus_request_data
*req
, const char *method
,
231 struct blob_attr
*msg
)
233 struct device
*dev
= NULL
;
234 struct blob_attr
*tb
[__DEV_MAX
];
236 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
239 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
241 return UBUS_STATUS_INVALID_ARGUMENT
;
244 blob_buf_init(&b
, 0);
245 device_dump_status(&b
, dev
);
246 ubus_send_reply(ctx
, req
, b
.head
);
257 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
258 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
259 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
263 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
264 struct ubus_request_data
*req
, const char *method
,
265 struct blob_attr
*msg
)
267 struct device
*dev
= NULL
;
268 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
269 struct blob_attr
*cur
;
272 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
274 if (!tb
[ALIAS_ATTR_ALIAS
])
275 return UBUS_STATUS_INVALID_ARGUMENT
;
277 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
278 dev
= device_get(blobmsg_data(cur
), true);
280 return UBUS_STATUS_NOT_FOUND
;
283 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
284 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
287 if (!blobmsg_check_attr(cur
, false))
290 alias_notify_device(blobmsg_data(cur
), dev
);
295 device_free_unused();
296 return UBUS_STATUS_INVALID_ARGUMENT
;
302 DEV_STATE_AUTH_STATUS
,
306 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
307 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
308 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
309 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
313 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
314 struct ubus_request_data
*req
, const char *method
,
315 struct blob_attr
*msg
)
317 struct device
*dev
= NULL
;
318 struct blob_attr
*tb
[__DEV_STATE_MAX
];
319 struct blob_attr
*cur
;
321 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
323 cur
= tb
[DEV_STATE_NAME
];
325 return UBUS_STATUS_INVALID_ARGUMENT
;
327 dev
= device_find(blobmsg_data(cur
));
329 return UBUS_STATUS_NOT_FOUND
;
331 cur
= tb
[DEV_STATE_DEFER
];
333 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
335 cur
= tb
[DEV_STATE_AUTH_STATUS
];
337 device_set_auth_status(dev
, !!blobmsg_get_u8(cur
));
344 DEV_HOTPLUG_ATTR_NAME
,
345 DEV_HOTPLUG_ATTR_ADD
,
346 __DEV_HOTPLUG_ATTR_MAX
,
349 static const struct blobmsg_policy dev_hotplug_policy
[__DEV_HOTPLUG_ATTR_MAX
] = {
350 [DEV_HOTPLUG_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
351 [DEV_HOTPLUG_ATTR_ADD
] = { "add", BLOBMSG_TYPE_BOOL
},
355 netifd_handle_dev_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
356 struct ubus_request_data
*req
, const char *method
,
357 struct blob_attr
*msg
)
359 struct blob_attr
*tb
[__DEV_HOTPLUG_ATTR_MAX
];
362 blobmsg_parse(dev_hotplug_policy
, __DEV_HOTPLUG_ATTR_MAX
, tb
,
363 blob_data(msg
), blob_len(msg
));
365 if (!tb
[DEV_HOTPLUG_ATTR_NAME
] || !tb
[DEV_HOTPLUG_ATTR_ADD
])
366 return UBUS_STATUS_INVALID_ARGUMENT
;
368 name
= blobmsg_get_string(tb
[DEV_HOTPLUG_ATTR_NAME
]);
369 device_hotplug_event(name
, blobmsg_get_bool(tb
[DEV_HOTPLUG_ATTR_ADD
]));
376 netifd_handle_stp_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
377 struct ubus_request_data
*req
, const char *method
,
378 struct blob_attr
*msg
)
385 static struct ubus_method dev_object_methods
[] = {
386 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
387 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
388 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
390 UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug
, dev_hotplug_policy
),
392 UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init
)
395 static struct ubus_object_type dev_object_type
=
396 UBUS_OBJECT_TYPE("device", dev_object_methods
);
398 static struct ubus_object dev_object
= {
399 .name
= "network.device",
400 .type
= &dev_object_type
,
401 .methods
= dev_object_methods
,
402 .n_methods
= ARRAY_SIZE(dev_object_methods
),
406 netifd_ubus_add_fd(void)
408 ubus_add_uloop(ubus_ctx
);
409 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
412 void netifd_ubus_device_notify(const char *event
, struct blob_attr
*data
, int timeout
)
414 ubus_notify(ubus_ctx
, &dev_object
, event
, data
, timeout
);
418 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
420 static struct uloop_timeout retry
= {
421 .cb
= netifd_ubus_reconnect_timer
,
425 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
426 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
427 uloop_timeout_set(&retry
, t
* 1000);
431 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
432 netifd_ubus_add_fd();
436 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
438 netifd_ubus_reconnect_timer(NULL
);
441 /* per-interface object */
444 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
445 struct ubus_request_data
*req
, const char *method
,
446 struct blob_attr
*msg
)
448 struct interface
*iface
;
450 iface
= container_of(obj
, struct interface
, ubus
);
451 interface_set_up(iface
);
457 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
458 struct ubus_request_data
*req
, const char *method
,
459 struct blob_attr
*msg
)
461 struct interface
*iface
;
463 iface
= container_of(obj
, struct interface
, ubus
);
464 interface_set_down(iface
);
470 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
471 struct ubus_request_data
*req
, const char *method
,
472 struct blob_attr
*msg
)
474 struct interface
*iface
;
476 iface
= container_of(obj
, struct interface
, ubus
);
477 interface_renew(iface
);
483 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
485 struct interface_error
*error
;
489 e
= blobmsg_open_array(b
, "errors");
490 list_for_each_entry(error
, &iface
->errors
, list
) {
491 e2
= blobmsg_open_table(b
, NULL
);
493 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
494 blobmsg_add_string(b
, "code", error
->code
);
495 if (error
->data
[0]) {
496 e3
= blobmsg_open_array(b
, "data");
497 for (i
= 0; error
->data
[i
]; i
++)
498 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
499 blobmsg_close_array(b
, e3
);
502 blobmsg_close_table(b
, e2
);
504 blobmsg_close_array(b
, e
);
508 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
510 struct device_addr
*addr
;
516 time_t now
= system_get_rtime();
517 vlist_for_each_element(&ip
->addr
, addr
, node
) {
518 if (addr
->enabled
!= enabled
)
521 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
526 if (af
!= (v6
? AF_INET6
: AF_INET
))
529 a
= blobmsg_open_table(&b
, NULL
);
531 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
532 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
533 blobmsg_add_string_buffer(&b
);
535 blobmsg_add_u32(&b
, "mask", addr
->mask
);
537 if (addr
->point_to_point
) {
538 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
539 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
540 blobmsg_add_string_buffer(&b
);
543 if (addr
->preferred_until
) {
544 int preferred
= addr
->preferred_until
- now
;
547 blobmsg_add_u32(&b
, "preferred", preferred
);
550 if (addr
->valid_until
)
551 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
554 blobmsg_add_string(&b
, "class", addr
->pclass
);
556 blobmsg_close_table(&b
, a
);
561 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
563 struct device_neighbor
*neighbor
;
569 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
570 if (neighbor
->enabled
!= enabled
)
573 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
578 r
= blobmsg_open_table(&b
, NULL
);
580 if (neighbor
->flags
& DEVNEIGH_MAC
)
581 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
583 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
584 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
585 blobmsg_add_string_buffer(&b
);
588 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
590 if (neighbor
->router
)
591 blobmsg_add_u32(&b
, "router", neighbor
->router
);
593 blobmsg_close_table(&b
, r
);
598 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
600 struct device_route
*route
;
606 time_t now
= system_get_rtime();
607 vlist_for_each_element(&ip
->route
, route
, node
) {
608 if (route
->enabled
!= enabled
)
611 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
614 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
619 r
= blobmsg_open_table(&b
, NULL
);
621 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
622 inet_ntop(af
, &route
->addr
, buf
, buflen
);
623 blobmsg_add_string_buffer(&b
);
625 blobmsg_add_u32(&b
, "mask", route
->mask
);
627 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
628 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
629 blobmsg_add_string_buffer(&b
);
631 if (route
->flags
& DEVROUTE_TYPE
)
632 blobmsg_add_u32(&b
, "type", route
->type
);
634 if (route
->flags
& DEVROUTE_PROTO
)
635 blobmsg_add_u32(&b
, "proto", route
->proto
);
637 if (route
->flags
& DEVROUTE_MTU
)
638 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
640 if (route
->flags
& DEVROUTE_METRIC
)
641 blobmsg_add_u32(&b
, "metric", route
->metric
);
643 if (route
->flags
& DEVROUTE_TABLE
)
644 blobmsg_add_u32(&b
, "table", route
->table
);
646 if (route
->valid_until
)
647 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
649 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
650 inet_ntop(af
, &route
->source
, buf
, buflen
);
651 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
652 blobmsg_add_string_buffer(&b
);
654 blobmsg_close_table(&b
, r
);
660 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
662 struct device_prefix
*prefix
;
665 const int buflen
= INET6_ADDRSTRLEN
;
667 time_t now
= system_get_rtime();
668 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
669 a
= blobmsg_open_table(&b
, NULL
);
671 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
672 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
673 blobmsg_add_string_buffer(&b
);
675 blobmsg_add_u32(&b
, "mask", prefix
->length
);
677 if (prefix
->preferred_until
) {
678 int preferred
= prefix
->preferred_until
- now
;
681 blobmsg_add_u32(&b
, "preferred", preferred
);
684 if (prefix
->valid_until
)
685 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
687 blobmsg_add_string(&b
, "class", prefix
->pclass
);
689 c
= blobmsg_open_table(&b
, "assigned");
690 struct device_prefix_assignment
*assign
;
691 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
692 if (!assign
->name
[0])
695 struct in6_addr addr
= prefix
->addr
;
696 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
698 void *d
= blobmsg_open_table(&b
, assign
->name
);
700 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
701 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
702 blobmsg_add_string_buffer(&b
);
704 blobmsg_add_u32(&b
, "mask", assign
->length
);
706 blobmsg_close_table(&b
, d
);
708 blobmsg_close_table(&b
, c
);
710 blobmsg_close_table(&b
, a
);
716 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
720 const int buflen
= INET6_ADDRSTRLEN
;
721 time_t now
= system_get_rtime();
723 struct device_prefix
*prefix
;
724 list_for_each_entry(prefix
, &prefixes
, head
) {
725 struct device_prefix_assignment
*assign
;
726 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
727 if (strcmp(assign
->name
, iface
->name
))
730 struct in6_addr addr
= prefix
->addr
;
731 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
733 a
= blobmsg_open_table(&b
, NULL
);
735 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
736 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
737 blobmsg_add_string_buffer(&b
);
739 blobmsg_add_u32(&b
, "mask", assign
->length
);
741 if (prefix
->preferred_until
) {
742 int preferred
= prefix
->preferred_until
- now
;
745 blobmsg_add_u32(&b
, "preferred", preferred
);
748 if (prefix
->valid_until
)
749 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
751 void *c
= blobmsg_open_table(&b
, "local-address");
752 if (assign
->enabled
) {
753 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
754 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
755 blobmsg_add_string_buffer(&b
);
757 blobmsg_add_u32(&b
, "mask", assign
->length
);
759 blobmsg_close_table(&b
, c
);
761 blobmsg_close_table(&b
, a
);
767 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
769 struct dns_server
*dns
;
773 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
774 if (ip
->no_dns
== enabled
)
777 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
778 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
779 blobmsg_add_string_buffer(&b
);
784 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
786 struct dns_search_domain
*dns
;
788 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
789 if (ip
->no_dns
== enabled
)
792 blobmsg_add_string(&b
, NULL
, dns
->name
);
797 netifd_dump_status(struct interface
*iface
)
799 struct interface_data
*data
;
803 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
804 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
805 blobmsg_add_u8(&b
, "available", iface
->available
);
806 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
807 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
809 if (iface
->state
== IFS_UP
) {
810 time_t cur
= system_get_rtime();
811 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
812 if (iface
->l3_dev
.dev
)
813 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
816 if (iface
->proto_handler
)
817 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
819 dev
= iface
->main_dev
.dev
;
820 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
821 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
822 blobmsg_add_string(&b
, "device", dev
->ifname
);
825 blobmsg_add_string(&b
, "jail", iface
->jail
);
827 if (iface
->jail_device
)
828 blobmsg_add_string(&b
, "jail_device", iface
->jail_device
);
830 if (iface
->state
== IFS_UP
) {
831 if (iface
->updated
) {
832 a
= blobmsg_open_array(&b
, "updated");
834 if (iface
->updated
& IUF_ADDRESS
)
835 blobmsg_add_string(&b
, NULL
, "addresses");
836 if (iface
->updated
& IUF_ROUTE
)
837 blobmsg_add_string(&b
, NULL
, "routes");
838 if (iface
->updated
& IUF_PREFIX
)
839 blobmsg_add_string(&b
, NULL
, "prefixes");
840 if (iface
->updated
& IUF_DATA
)
841 blobmsg_add_string(&b
, NULL
, "data");
843 blobmsg_close_array(&b
, a
);
847 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
849 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
850 blobmsg_add_u32(&b
, "metric", iface
->metric
);
851 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
852 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
853 if (iface
->assignment_weight
)
854 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
855 a
= blobmsg_open_array(&b
, "ipv4-address");
856 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
857 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
858 blobmsg_close_array(&b
, a
);
859 a
= blobmsg_open_array(&b
, "ipv6-address");
860 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
861 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
862 blobmsg_close_array(&b
, a
);
863 a
= blobmsg_open_array(&b
, "ipv6-prefix");
864 interface_ip_dump_prefix_list(&iface
->config_ip
);
865 interface_ip_dump_prefix_list(&iface
->proto_ip
);
866 blobmsg_close_array(&b
, a
);
867 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
868 interface_ip_dump_prefix_assignment_list(iface
);
869 blobmsg_close_array(&b
, a
);
870 a
= blobmsg_open_array(&b
, "route");
871 interface_ip_dump_route_list(&iface
->config_ip
, true);
872 interface_ip_dump_route_list(&iface
->proto_ip
, true);
873 blobmsg_close_array(&b
, a
);
874 a
= blobmsg_open_array(&b
, "dns-server");
875 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
876 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
877 blobmsg_close_array(&b
, a
);
878 a
= blobmsg_open_array(&b
, "dns-search");
879 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
880 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
881 blobmsg_close_array(&b
, a
);
882 a
= blobmsg_open_array(&b
, "neighbors");
883 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
884 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
885 blobmsg_close_array(&b
, a
);
887 inactive
= blobmsg_open_table(&b
, "inactive");
888 a
= blobmsg_open_array(&b
, "ipv4-address");
889 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
890 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
891 blobmsg_close_array(&b
, a
);
892 a
= blobmsg_open_array(&b
, "ipv6-address");
893 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
894 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
895 blobmsg_close_array(&b
, a
);
896 a
= blobmsg_open_array(&b
, "route");
897 interface_ip_dump_route_list(&iface
->config_ip
, false);
898 interface_ip_dump_route_list(&iface
->proto_ip
, false);
899 blobmsg_close_array(&b
, a
);
900 a
= blobmsg_open_array(&b
, "dns-server");
901 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
902 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
903 blobmsg_close_array(&b
, a
);
904 a
= blobmsg_open_array(&b
, "dns-search");
905 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
906 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
907 blobmsg_close_array(&b
, a
);
908 a
= blobmsg_open_array(&b
, "neighbors");
909 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
910 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
911 blobmsg_close_array(&b
, a
);
912 blobmsg_close_table(&b
, inactive
);
915 a
= blobmsg_open_table(&b
, "data");
916 avl_for_each_element(&iface
->data
, data
, node
)
917 blobmsg_add_blob(&b
, data
->data
);
919 blobmsg_close_table(&b
, a
);
921 if (!list_empty(&iface
->errors
))
922 netifd_add_interface_errors(&b
, iface
);
926 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
927 struct ubus_request_data
*req
, const char *method
,
928 struct blob_attr
*msg
)
930 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
932 blob_buf_init(&b
, 0);
933 netifd_dump_status(iface
);
934 ubus_send_reply(ctx
, req
, b
.head
);
941 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
942 struct ubus_request_data
*req
, const char *method
,
943 struct blob_attr
*msg
)
945 blob_buf_init(&b
, 0);
946 void *a
= blobmsg_open_array(&b
, "interface");
948 struct interface
*iface
;
949 vlist_for_each_element(&interfaces
, iface
, node
) {
950 void *i
= blobmsg_open_table(&b
, NULL
);
951 blobmsg_add_string(&b
, "interface", iface
->name
);
952 netifd_dump_status(iface
);
953 blobmsg_close_table(&b
, i
);
956 blobmsg_close_array(&b
, a
);
957 ubus_send_reply(ctx
, req
, b
.head
);
969 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
970 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
971 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
972 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
976 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
977 struct ubus_request_data
*req
, const char *method
,
978 struct blob_attr
*msg
)
980 struct blob_attr
*tb
[__DEV_LINK_MAX
];
981 struct blob_attr
*cur
;
982 struct interface
*iface
;
983 bool add
= !strncmp(method
, "add", 3);
984 bool link_ext
= true;
986 iface
= container_of(obj
, struct interface
, ubus
);
988 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
990 if (!tb
[DEV_LINK_NAME
])
991 return UBUS_STATUS_INVALID_ARGUMENT
;
993 cur
= tb
[DEV_LINK_EXT
];
995 link_ext
= blobmsg_get_bool(cur
);
997 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
998 tb
[DEV_LINK_VLAN
], add
, link_ext
);
1003 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1004 struct ubus_request_data
*req
, const char *method
,
1005 struct blob_attr
*msg
)
1007 struct interface
*iface
;
1009 iface
= container_of(obj
, struct interface
, ubus
);
1011 if (!iface
->proto
|| !iface
->proto
->notify
)
1012 return UBUS_STATUS_NOT_SUPPORTED
;
1014 return iface
->proto
->notify(iface
->proto
, msg
);
1018 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
1020 struct interface
*iface
;
1022 iface
= container_of(timeout
, struct interface
, remove_timer
);
1023 vlist_delete(&interfaces
, &iface
->node
);
1027 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1028 struct ubus_request_data
*req
, const char *method
,
1029 struct blob_attr
*msg
)
1031 struct interface
*iface
;
1033 iface
= container_of(obj
, struct interface
, ubus
);
1034 if (iface
->remove_timer
.cb
)
1035 return UBUS_STATUS_INVALID_ARGUMENT
;
1037 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
1038 uloop_timeout_set(&iface
->remove_timer
, 100);
1043 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1044 struct ubus_request_data
*req
, const char *method
,
1045 struct blob_attr
*msg
)
1047 struct interface
*iface
;
1048 struct device
*dev
, *bridge_dev
= NULL
;
1049 const struct device_hotplug_ops
*ops
;
1051 iface
= container_of(obj
, struct interface
, ubus
);
1052 dev
= iface
->main_dev
.dev
;
1056 ops
= dev
->hotplug_ops
;
1060 ops
->prepare(dev
, &bridge_dev
);
1063 blob_buf_init(&b
, 0);
1065 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1066 ubus_send_reply(ctx
, req
, b
.head
);
1072 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1073 struct ubus_request_data
*req
, const char *method
,
1074 struct blob_attr
*msg
)
1076 struct interface
*iface
;
1078 iface
= container_of(obj
, struct interface
, ubus
);
1080 return interface_parse_data(iface
, msg
);
1083 static struct ubus_method iface_object_methods
[] = {
1084 { .name
= "up", .handler
= netifd_handle_up
},
1085 { .name
= "down", .handler
= netifd_handle_down
},
1086 { .name
= "renew", .handler
= netifd_handle_renew
},
1087 { .name
= "status", .handler
= netifd_handle_status
},
1088 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1089 { .name
= "dump", .handler
= netifd_handle_dump
},
1090 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1091 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1092 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1093 { .name
= "remove", .handler
= netifd_iface_remove
},
1094 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1097 static struct ubus_object_type iface_object_type
=
1098 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1101 static struct ubus_object iface_object
= {
1102 .name
= "network.interface",
1103 .type
= &iface_object_type
,
1104 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1107 static void netifd_add_object(struct ubus_object
*obj
)
1109 int ret
= ubus_add_object(ubus_ctx
, obj
);
1112 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1115 static const struct blobmsg_policy iface_policy
= {
1116 .name
= "interface",
1117 .type
= BLOBMSG_TYPE_STRING
,
1121 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1122 struct ubus_request_data
*req
, const char *method
,
1123 struct blob_attr
*msg
)
1125 struct interface
*iface
;
1126 struct blob_attr
*tb
;
1129 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1131 return UBUS_STATUS_INVALID_ARGUMENT
;
1133 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1135 return UBUS_STATUS_NOT_FOUND
;
1137 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1140 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1143 cb
= iface_object_methods
[i
].handler
;
1144 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1147 return UBUS_STATUS_INVALID_ARGUMENT
;
1150 static void netifd_add_iface_object(void)
1152 struct ubus_method
*methods
;
1155 methods
= calloc(1, sizeof(iface_object_methods
));
1159 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1160 iface_object
.methods
= methods
;
1162 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1163 if (methods
[i
].handler
== netifd_handle_dump
)
1166 methods
[i
].handler
= netifd_handle_iface
;
1167 methods
[i
].policy
= &iface_policy
;
1168 methods
[i
].n_policy
= 1;
1170 netifd_add_object(&iface_object
);
1173 static struct wireless_device
*
1174 get_wdev(struct blob_attr
*msg
, int *ret
)
1176 struct blobmsg_policy wdev_policy
= {
1178 .type
= BLOBMSG_TYPE_STRING
,
1180 struct blob_attr
*dev_attr
;
1181 struct wireless_device
*wdev
= NULL
;
1184 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1186 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1190 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1192 *ret
= UBUS_STATUS_NOT_FOUND
;
1201 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1202 struct ubus_request_data
*req
, const char *method
,
1203 struct blob_attr
*msg
)
1205 struct wireless_device
*wdev
;
1208 wdev
= get_wdev(msg
, &ret
);
1209 if (ret
== UBUS_STATUS_NOT_FOUND
)
1213 wireless_device_reconf(wdev
);
1215 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1216 wireless_device_reconf(wdev
);
1223 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1224 struct ubus_request_data
*req
, const char *method
,
1225 struct blob_attr
*msg
)
1227 struct wireless_device
*wdev
;
1230 wdev
= get_wdev(msg
, &ret
);
1231 if (ret
== UBUS_STATUS_NOT_FOUND
)
1235 wireless_device_set_up(wdev
);
1237 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1238 wireless_device_set_up(wdev
);
1245 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1246 struct ubus_request_data
*req
, const char *method
,
1247 struct blob_attr
*msg
)
1249 struct wireless_device
*wdev
;
1252 wdev
= get_wdev(msg
, &ret
);
1253 if (ret
== UBUS_STATUS_NOT_FOUND
)
1257 wireless_device_set_down(wdev
);
1259 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1260 wireless_device_set_down(wdev
);
1267 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1268 struct ubus_request_data
*req
, const char *method
,
1269 struct blob_attr
*msg
)
1271 struct wireless_device
*wdev
;
1274 wdev
= get_wdev(msg
, &ret
);
1275 if (ret
== UBUS_STATUS_NOT_FOUND
)
1278 blob_buf_init(&b
, 0);
1280 wireless_device_status(wdev
, &b
);
1282 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1283 wireless_device_status(wdev
, &b
);
1285 ubus_send_reply(ctx
, req
, b
.head
);
1290 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1291 struct ubus_request_data
*req
, const char *method
,
1292 struct blob_attr
*msg
)
1294 struct wireless_device
*wdev
;
1297 wdev
= get_wdev(msg
, &ret
);
1298 if (ret
== UBUS_STATUS_NOT_FOUND
)
1301 blob_buf_init(&b
, 0);
1303 wireless_device_get_validate(wdev
, &b
);
1305 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1306 wireless_device_get_validate(wdev
, &b
);
1308 ubus_send_reply(ctx
, req
, b
.head
);
1313 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1314 struct ubus_request_data
*req
, const char *method
,
1315 struct blob_attr
*msg
)
1317 struct wireless_device
*wdev
;
1320 wdev
= get_wdev(msg
, &ret
);
1324 return wireless_device_notify(wdev
, msg
, req
);
1327 static struct ubus_method wireless_object_methods
[] = {
1328 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1329 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1330 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1331 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1332 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1333 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1336 static struct ubus_object_type wireless_object_type
=
1337 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1340 static struct ubus_object wireless_object
= {
1341 .name
= "network.wireless",
1342 .type
= &wireless_object_type
,
1343 .methods
= wireless_object_methods
,
1344 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1348 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1349 ubus_data_handler_t data_cb
, void *data
)
1351 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1355 netifd_ubus_init(const char *path
)
1360 ubus_ctx
= ubus_connect(path
);
1364 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1365 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1366 netifd_ubus_add_fd();
1368 netifd_add_object(&main_object
);
1369 netifd_add_object(&dev_object
);
1370 netifd_add_object(&wireless_object
);
1371 netifd_add_iface_object();
1377 netifd_ubus_done(void)
1379 ubus_free(ubus_ctx
);
1383 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1385 blob_buf_init(&b
, 0);
1386 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1387 blobmsg_add_string(&b
, "interface", iface
->name
);
1388 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1392 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1394 const char *event
= (up
) ? "interface.update" : "interface.down";
1395 blob_buf_init(&b
, 0);
1396 blobmsg_add_string(&b
, "interface", iface
->name
);
1397 netifd_dump_status(iface
);
1398 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1399 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1403 netifd_ubus_add_interface(struct interface
*iface
)
1405 struct ubus_object
*obj
= &iface
->ubus
;
1408 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1412 obj
->type
= &iface_object_type
;
1413 obj
->methods
= iface_object_methods
;
1414 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1415 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1416 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1423 netifd_ubus_remove_interface(struct interface
*iface
)
1425 if (!iface
->ubus
.name
)
1428 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1429 free((void *) iface
->ubus
.name
);