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
;
61 static const struct blobmsg_policy route_policy
[__HR_MAX
] = {
62 [HR_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
63 [HR_V6
] = { .name
= "v6", .type
= BLOBMSG_TYPE_BOOL
},
64 [HR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
65 [HR_EXCLUDE
] = { .name
= "exclude", .type
= BLOBMSG_TYPE_BOOL
},
69 netifd_add_host_route(struct ubus_context
*ctx
, struct ubus_object
*obj
,
70 struct ubus_request_data
*req
, const char *method
,
71 struct blob_attr
*msg
)
73 struct blob_attr
*tb
[__HR_MAX
];
74 struct interface
*iface
= NULL
;
79 blobmsg_parse(route_policy
, __HR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
81 return UBUS_STATUS_INVALID_ARGUMENT
;
84 v6
= blobmsg_get_bool(tb
[HR_V6
]);
87 exclude
= blobmsg_get_bool(tb
[HR_EXCLUDE
]);
90 iface
= vlist_find(&interfaces
, blobmsg_data(tb
[HR_INTERFACE
]), iface
, node
);
92 memset(&a
, 0, sizeof(a
));
93 if (!inet_pton(v6
? AF_INET6
: AF_INET
, blobmsg_data(tb
[HR_TARGET
]), &a
))
94 return UBUS_STATUS_INVALID_ARGUMENT
;
96 iface
= interface_ip_add_target_route(&a
, v6
, iface
, exclude
);
98 return UBUS_STATUS_NOT_FOUND
;
100 blob_buf_init(&b
, 0);
101 blobmsg_add_string(&b
, "interface", iface
->name
);
102 ubus_send_reply(ctx
, req
, b
.head
);
108 netifd_get_proto_handlers(struct ubus_context
*ctx
, struct ubus_object
*obj
,
109 struct ubus_request_data
*req
, const char *method
,
110 struct blob_attr
*msg
)
112 blob_buf_init(&b
, 0);
113 proto_dump_handlers(&b
);
114 ubus_send_reply(ctx
, req
, b
.head
);
125 static const struct blobmsg_policy dynamic_policy
[__DI_MAX
] = {
126 [DI_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
130 netifd_add_dynamic(struct ubus_context
*ctx
, struct ubus_object
*obj
,
131 struct ubus_request_data
*req
, const char *method
,
132 struct blob_attr
*msg
)
134 struct blob_attr
*tb
[__DI_MAX
];
135 struct interface
*iface
;
136 struct blob_attr
*config
;
138 blobmsg_parse(dynamic_policy
, __DI_MAX
, tb
, blob_data(msg
), blob_len(msg
));
141 return UBUS_STATUS_INVALID_ARGUMENT
;
143 const char *name
= blobmsg_get_string(tb
[DI_NAME
]);
145 iface
= interface_alloc(name
, msg
, true);
147 return UBUS_STATUS_UNKNOWN_ERROR
;
149 config
= blob_memdup(msg
);
153 if (!interface_add(iface
, config
))
154 goto error_free_config
;
156 return UBUS_STATUS_OK
;
162 return UBUS_STATUS_UNKNOWN_ERROR
;
171 static const struct blobmsg_policy netns_updown_policy
[__NETNS_UPDOWN_MAX
] = {
172 [NETNS_UPDOWN_JAIL
] = { .name
= "jail", .type
= BLOBMSG_TYPE_STRING
},
173 [NETNS_UPDOWN_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_BOOL
},
177 netifd_netns_updown(struct ubus_context
*ctx
, struct ubus_object
*obj
,
178 struct ubus_request_data
*req
, const char *method
,
179 struct blob_attr
*msg
)
181 int target_netns_fd
= ubus_request_get_caller_fd(req
);
182 struct blob_attr
*tb
[__NETNS_UPDOWN_MAX
];
185 if (target_netns_fd
< 0)
186 return UBUS_STATUS_INVALID_ARGUMENT
;
188 blobmsg_parse(netns_updown_policy
, __NETNS_UPDOWN_MAX
, tb
, blob_data(msg
), blob_len(msg
));
190 start
= tb
[NETNS_UPDOWN_START
] && blobmsg_get_bool(tb
[NETNS_UPDOWN_START
]);
193 if (!tb
[NETNS_UPDOWN_JAIL
])
194 return UBUS_STATUS_INVALID_ARGUMENT
;
196 interface_start_jail(target_netns_fd
, blobmsg_get_string(tb
[NETNS_UPDOWN_JAIL
]));
198 interface_stop_jail(target_netns_fd
);
200 close(target_netns_fd
);
202 return UBUS_STATUS_OK
;
205 static struct ubus_method main_object_methods
[] = {
206 { .name
= "restart", .handler
= netifd_handle_restart
},
207 { .name
= "reload", .handler
= netifd_handle_reload
},
208 UBUS_METHOD("add_host_route", netifd_add_host_route
, route_policy
),
209 { .name
= "get_proto_handlers", .handler
= netifd_get_proto_handlers
},
210 UBUS_METHOD("add_dynamic", netifd_add_dynamic
, dynamic_policy
),
211 UBUS_METHOD("netns_updown", netifd_netns_updown
, netns_updown_policy
),
214 static struct ubus_object_type main_object_type
=
215 UBUS_OBJECT_TYPE("netifd", main_object_methods
);
217 static struct ubus_object main_object
= {
219 .type
= &main_object_type
,
220 .methods
= main_object_methods
,
221 .n_methods
= ARRAY_SIZE(main_object_methods
),
229 static const struct blobmsg_policy dev_policy
[__DEV_MAX
] = {
230 [DEV_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
234 netifd_dev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
235 struct ubus_request_data
*req
, const char *method
,
236 struct blob_attr
*msg
)
238 struct device
*dev
= NULL
;
239 struct blob_attr
*tb
[__DEV_MAX
];
241 blobmsg_parse(dev_policy
, __DEV_MAX
, tb
, blob_data(msg
), blob_len(msg
));
244 dev
= device_find(blobmsg_data(tb
[DEV_NAME
]));
246 return UBUS_STATUS_INVALID_ARGUMENT
;
249 blob_buf_init(&b
, 0);
250 device_dump_status(&b
, dev
);
251 ubus_send_reply(ctx
, req
, b
.head
);
262 static const struct blobmsg_policy alias_attrs
[__ALIAS_ATTR_MAX
] = {
263 [ALIAS_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
264 [ALIAS_ATTR_DEV
] = { "device", BLOBMSG_TYPE_STRING
},
268 netifd_handle_alias(struct ubus_context
*ctx
, struct ubus_object
*obj
,
269 struct ubus_request_data
*req
, const char *method
,
270 struct blob_attr
*msg
)
272 struct device
*dev
= NULL
;
273 struct blob_attr
*tb
[__ALIAS_ATTR_MAX
];
274 struct blob_attr
*cur
;
277 blobmsg_parse(alias_attrs
, __ALIAS_ATTR_MAX
, tb
, blob_data(msg
), blob_len(msg
));
279 if (!tb
[ALIAS_ATTR_ALIAS
])
280 return UBUS_STATUS_INVALID_ARGUMENT
;
282 if ((cur
= tb
[ALIAS_ATTR_DEV
]) != NULL
) {
283 dev
= device_get(blobmsg_data(cur
), true);
285 return UBUS_STATUS_NOT_FOUND
;
288 blobmsg_for_each_attr(cur
, tb
[ALIAS_ATTR_ALIAS
], rem
) {
289 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
292 if (!blobmsg_check_attr(cur
, false))
295 alias_notify_device(blobmsg_data(cur
), dev
);
300 device_free_unused();
301 return UBUS_STATUS_INVALID_ARGUMENT
;
307 DEV_STATE_AUTH_STATUS
,
311 static const struct blobmsg_policy dev_state_policy
[__DEV_STATE_MAX
] = {
312 [DEV_STATE_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
313 [DEV_STATE_DEFER
] = { .name
= "defer", .type
= BLOBMSG_TYPE_BOOL
},
314 [DEV_STATE_AUTH_STATUS
] = { .name
= "auth_status", .type
= BLOBMSG_TYPE_BOOL
},
318 netifd_handle_set_state(struct ubus_context
*ctx
, struct ubus_object
*obj
,
319 struct ubus_request_data
*req
, const char *method
,
320 struct blob_attr
*msg
)
322 struct device
*dev
= NULL
;
323 struct blob_attr
*tb
[__DEV_STATE_MAX
];
324 struct blob_attr
*cur
;
326 blobmsg_parse(dev_state_policy
, __DEV_STATE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
328 cur
= tb
[DEV_STATE_NAME
];
330 return UBUS_STATUS_INVALID_ARGUMENT
;
332 dev
= device_find(blobmsg_data(cur
));
334 return UBUS_STATUS_NOT_FOUND
;
336 cur
= tb
[DEV_STATE_DEFER
];
338 device_set_deferred(dev
, !!blobmsg_get_u8(cur
));
340 cur
= tb
[DEV_STATE_AUTH_STATUS
];
342 device_set_auth_status(dev
, !!blobmsg_get_u8(cur
));
349 DEV_HOTPLUG_ATTR_NAME
,
350 DEV_HOTPLUG_ATTR_ADD
,
351 __DEV_HOTPLUG_ATTR_MAX
,
354 static const struct blobmsg_policy dev_hotplug_policy
[__DEV_HOTPLUG_ATTR_MAX
] = {
355 [DEV_HOTPLUG_ATTR_NAME
] = { "name", BLOBMSG_TYPE_STRING
},
356 [DEV_HOTPLUG_ATTR_ADD
] = { "add", BLOBMSG_TYPE_BOOL
},
360 netifd_handle_dev_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
361 struct ubus_request_data
*req
, const char *method
,
362 struct blob_attr
*msg
)
364 struct blob_attr
*tb
[__DEV_HOTPLUG_ATTR_MAX
];
367 blobmsg_parse(dev_hotplug_policy
, __DEV_HOTPLUG_ATTR_MAX
, tb
,
368 blob_data(msg
), blob_len(msg
));
370 if (!tb
[DEV_HOTPLUG_ATTR_NAME
] || !tb
[DEV_HOTPLUG_ATTR_ADD
])
371 return UBUS_STATUS_INVALID_ARGUMENT
;
373 name
= blobmsg_get_string(tb
[DEV_HOTPLUG_ATTR_NAME
]);
374 device_hotplug_event(name
, blobmsg_get_bool(tb
[DEV_HOTPLUG_ATTR_ADD
]));
381 netifd_handle_stp_init(struct ubus_context
*ctx
, struct ubus_object
*obj
,
382 struct ubus_request_data
*req
, const char *method
,
383 struct blob_attr
*msg
)
390 static struct ubus_method dev_object_methods
[] = {
391 UBUS_METHOD("status", netifd_dev_status
, dev_policy
),
392 UBUS_METHOD("set_alias", netifd_handle_alias
, alias_attrs
),
393 UBUS_METHOD("set_state", netifd_handle_set_state
, dev_state_policy
),
395 UBUS_METHOD("hotplug_event", netifd_handle_dev_hotplug
, dev_hotplug_policy
),
397 UBUS_METHOD_NOARG("stp_init", netifd_handle_stp_init
)
400 static struct ubus_object_type dev_object_type
=
401 UBUS_OBJECT_TYPE("device", dev_object_methods
);
403 static struct ubus_object dev_object
= {
404 .name
= "network.device",
405 .type
= &dev_object_type
,
406 .methods
= dev_object_methods
,
407 .n_methods
= ARRAY_SIZE(dev_object_methods
),
411 netifd_ubus_add_fd(void)
413 ubus_add_uloop(ubus_ctx
);
414 system_fd_set_cloexec(ubus_ctx
->sock
.fd
);
417 void netifd_ubus_device_notify(const char *event
, struct blob_attr
*data
, int timeout
)
419 ubus_notify(ubus_ctx
, &dev_object
, event
, data
, timeout
);
423 netifd_ubus_reconnect_timer(struct uloop_timeout
*timeout
)
425 static struct uloop_timeout retry
= {
426 .cb
= netifd_ubus_reconnect_timer
,
430 if (ubus_reconnect(ubus_ctx
, ubus_path
) != 0) {
431 DPRINTF("failed to reconnect, trying again in %d seconds\n", t
);
432 uloop_timeout_set(&retry
, t
* 1000);
436 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx
->local_id
);
437 netifd_ubus_add_fd();
441 netifd_ubus_connection_lost(struct ubus_context
*ctx
)
443 netifd_ubus_reconnect_timer(NULL
);
446 /* per-interface object */
449 netifd_handle_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
450 struct ubus_request_data
*req
, const char *method
,
451 struct blob_attr
*msg
)
453 struct interface
*iface
;
455 iface
= container_of(obj
, struct interface
, ubus
);
456 interface_set_up(iface
);
462 netifd_handle_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
463 struct ubus_request_data
*req
, const char *method
,
464 struct blob_attr
*msg
)
466 struct interface
*iface
;
468 iface
= container_of(obj
, struct interface
, ubus
);
469 interface_set_down(iface
);
475 netifd_handle_renew(struct ubus_context
*ctx
, struct ubus_object
*obj
,
476 struct ubus_request_data
*req
, const char *method
,
477 struct blob_attr
*msg
)
479 struct interface
*iface
;
481 iface
= container_of(obj
, struct interface
, ubus
);
482 interface_renew(iface
);
488 netifd_add_interface_errors(struct blob_buf
*b
, struct interface
*iface
)
490 struct interface_error
*error
;
494 e
= blobmsg_open_array(b
, "errors");
495 list_for_each_entry(error
, &iface
->errors
, list
) {
496 e2
= blobmsg_open_table(b
, NULL
);
498 blobmsg_add_string(b
, "subsystem", error
->subsystem
);
499 blobmsg_add_string(b
, "code", error
->code
);
500 if (error
->data
[0]) {
501 e3
= blobmsg_open_array(b
, "data");
502 for (i
= 0; error
->data
[i
]; i
++)
503 blobmsg_add_string(b
, NULL
, error
->data
[i
]);
504 blobmsg_close_array(b
, e3
);
507 blobmsg_close_table(b
, e2
);
509 blobmsg_close_array(b
, e
);
513 interface_ip_dump_address_list(struct interface_ip_settings
*ip
, bool v6
, bool enabled
)
515 struct device_addr
*addr
;
521 time_t now
= system_get_rtime();
522 vlist_for_each_element(&ip
->addr
, addr
, node
) {
523 if (addr
->enabled
!= enabled
)
526 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
531 if (af
!= (v6
? AF_INET6
: AF_INET
))
534 a
= blobmsg_open_table(&b
, NULL
);
536 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
537 inet_ntop(af
, &addr
->addr
, buf
, buflen
);
538 blobmsg_add_string_buffer(&b
);
540 blobmsg_add_u32(&b
, "mask", addr
->mask
);
542 if (addr
->point_to_point
) {
543 buf
= blobmsg_alloc_string_buffer(&b
, "ptpaddress", buflen
);
544 inet_ntop(af
, &addr
->point_to_point
, buf
, buflen
);
545 blobmsg_add_string_buffer(&b
);
548 if (addr
->preferred_until
) {
549 int preferred
= addr
->preferred_until
- now
;
552 blobmsg_add_u32(&b
, "preferred", preferred
);
555 if (addr
->valid_until
)
556 blobmsg_add_u32(&b
, "valid", addr
->valid_until
- now
);
559 blobmsg_add_string(&b
, "class", addr
->pclass
);
561 blobmsg_close_table(&b
, a
);
566 interface_ip_dump_neighbor_list(struct interface_ip_settings
*ip
, bool enabled
)
568 struct device_neighbor
*neighbor
;
574 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
575 if (neighbor
->enabled
!= enabled
)
578 if ((neighbor
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
583 r
= blobmsg_open_table(&b
, NULL
);
585 if (neighbor
->flags
& DEVNEIGH_MAC
)
586 blobmsg_add_string(&b
, "mac", format_macaddr(neighbor
->macaddr
));
588 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
589 inet_ntop(af
, &neighbor
->addr
, buf
, buflen
);
590 blobmsg_add_string_buffer(&b
);
593 blobmsg_add_u32(&b
, "proxy", neighbor
->proxy
);
595 if (neighbor
->router
)
596 blobmsg_add_u32(&b
, "router", neighbor
->router
);
598 blobmsg_close_table(&b
, r
);
603 interface_ip_dump_route_list(struct interface_ip_settings
*ip
, bool enabled
)
605 struct device_route
*route
;
611 time_t now
= system_get_rtime();
612 vlist_for_each_element(&ip
->route
, route
, node
) {
613 if (route
->enabled
!= enabled
)
616 if ((ip
->no_defaultroute
== enabled
) && !route
->mask
)
619 if ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
)
624 r
= blobmsg_open_table(&b
, NULL
);
626 buf
= blobmsg_alloc_string_buffer(&b
, "target", buflen
);
627 inet_ntop(af
, &route
->addr
, buf
, buflen
);
628 blobmsg_add_string_buffer(&b
);
630 blobmsg_add_u32(&b
, "mask", route
->mask
);
632 buf
= blobmsg_alloc_string_buffer(&b
, "nexthop", buflen
);
633 inet_ntop(af
, &route
->nexthop
, buf
, buflen
);
634 blobmsg_add_string_buffer(&b
);
636 if (route
->flags
& DEVROUTE_TYPE
)
637 blobmsg_add_u32(&b
, "type", route
->type
);
639 if (route
->flags
& DEVROUTE_PROTO
)
640 blobmsg_add_u32(&b
, "proto", route
->proto
);
642 if (route
->flags
& DEVROUTE_MTU
)
643 blobmsg_add_u32(&b
, "mtu", route
->mtu
);
645 if (route
->flags
& DEVROUTE_METRIC
)
646 blobmsg_add_u32(&b
, "metric", route
->metric
);
648 if (route
->flags
& DEVROUTE_TABLE
)
649 blobmsg_add_u32(&b
, "table", route
->table
);
651 if (route
->valid_until
)
652 blobmsg_add_u32(&b
, "valid", route
->valid_until
- now
);
654 buf
= blobmsg_alloc_string_buffer(&b
, "source", buflen
);
655 inet_ntop(af
, &route
->source
, buf
, buflen
);
656 snprintf(buf
+ strlen(buf
), buflen
- strlen(buf
), "/%u", route
->sourcemask
);
657 blobmsg_add_string_buffer(&b
);
659 blobmsg_close_table(&b
, r
);
665 interface_ip_dump_prefix_list(struct interface_ip_settings
*ip
)
667 struct device_prefix
*prefix
;
670 const int buflen
= INET6_ADDRSTRLEN
;
672 time_t now
= system_get_rtime();
673 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
674 a
= blobmsg_open_table(&b
, NULL
);
676 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
677 inet_ntop(AF_INET6
, &prefix
->addr
, buf
, buflen
);
678 blobmsg_add_string_buffer(&b
);
680 blobmsg_add_u32(&b
, "mask", prefix
->length
);
682 if (prefix
->preferred_until
) {
683 int preferred
= prefix
->preferred_until
- now
;
686 blobmsg_add_u32(&b
, "preferred", preferred
);
689 if (prefix
->valid_until
)
690 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
692 blobmsg_add_string(&b
, "class", prefix
->pclass
);
694 c
= blobmsg_open_table(&b
, "assigned");
695 struct device_prefix_assignment
*assign
;
696 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
697 if (!assign
->name
[0])
700 struct in6_addr addr
= prefix
->addr
;
701 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
703 void *d
= blobmsg_open_table(&b
, assign
->name
);
705 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
706 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
707 blobmsg_add_string_buffer(&b
);
709 blobmsg_add_u32(&b
, "mask", assign
->length
);
711 blobmsg_close_table(&b
, d
);
713 blobmsg_close_table(&b
, c
);
715 blobmsg_close_table(&b
, a
);
721 interface_ip_dump_prefix_assignment_list(struct interface
*iface
)
725 const int buflen
= INET6_ADDRSTRLEN
;
726 time_t now
= system_get_rtime();
728 struct device_prefix
*prefix
;
729 list_for_each_entry(prefix
, &prefixes
, head
) {
730 struct device_prefix_assignment
*assign
;
731 list_for_each_entry(assign
, &prefix
->assignments
, head
) {
732 if (strcmp(assign
->name
, iface
->name
))
735 struct in6_addr addr
= prefix
->addr
;
736 addr
.s6_addr32
[1] |= htonl(assign
->assigned
);
738 a
= blobmsg_open_table(&b
, NULL
);
740 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
741 inet_ntop(AF_INET6
, &addr
, buf
, buflen
);
742 blobmsg_add_string_buffer(&b
);
744 blobmsg_add_u32(&b
, "mask", assign
->length
);
746 if (prefix
->preferred_until
) {
747 int preferred
= prefix
->preferred_until
- now
;
750 blobmsg_add_u32(&b
, "preferred", preferred
);
753 if (prefix
->valid_until
)
754 blobmsg_add_u32(&b
, "valid", prefix
->valid_until
- now
);
756 void *c
= blobmsg_open_table(&b
, "local-address");
757 if (assign
->enabled
) {
758 buf
= blobmsg_alloc_string_buffer(&b
, "address", buflen
);
759 inet_ntop(AF_INET6
, &assign
->addr
, buf
, buflen
);
760 blobmsg_add_string_buffer(&b
);
762 blobmsg_add_u32(&b
, "mask", assign
->length
);
764 blobmsg_close_table(&b
, c
);
766 blobmsg_close_table(&b
, a
);
772 interface_ip_dump_dns_server_list(struct interface_ip_settings
*ip
, bool enabled
)
774 struct dns_server
*dns
;
778 vlist_simple_for_each_element(&ip
->dns_servers
, dns
, node
) {
779 if (ip
->no_dns
== enabled
)
782 buf
= blobmsg_alloc_string_buffer(&b
, NULL
, buflen
);
783 inet_ntop(dns
->af
, &dns
->addr
, buf
, buflen
);
784 blobmsg_add_string_buffer(&b
);
789 interface_ip_dump_dns_search_list(struct interface_ip_settings
*ip
, bool enabled
)
791 struct dns_search_domain
*dns
;
793 vlist_simple_for_each_element(&ip
->dns_search
, dns
, node
) {
794 if (ip
->no_dns
== enabled
)
797 blobmsg_add_string(&b
, NULL
, dns
->name
);
802 netifd_dump_status(struct interface
*iface
)
804 struct interface_data
*data
;
808 blobmsg_add_u8(&b
, "up", iface
->state
== IFS_UP
);
809 blobmsg_add_u8(&b
, "pending", iface
->state
== IFS_SETUP
);
810 blobmsg_add_u8(&b
, "available", iface
->available
);
811 blobmsg_add_u8(&b
, "autostart", iface
->autostart
);
812 blobmsg_add_u8(&b
, "dynamic", iface
->dynamic
);
814 if (iface
->state
== IFS_UP
) {
815 time_t cur
= system_get_rtime();
816 blobmsg_add_u32(&b
, "uptime", cur
- iface
->start_time
);
817 if (iface
->l3_dev
.dev
)
818 blobmsg_add_string(&b
, "l3_device", iface
->l3_dev
.dev
->ifname
);
821 if (iface
->proto_handler
)
822 blobmsg_add_string(&b
, "proto", iface
->proto_handler
->name
);
824 dev
= iface
->main_dev
.dev
;
825 if (dev
&& !dev
->hidden
&& iface
->proto_handler
&&
826 !(iface
->proto_handler
->flags
& PROTO_FLAG_NODEV
))
827 blobmsg_add_string(&b
, "device", dev
->ifname
);
830 blobmsg_add_string(&b
, "jail", iface
->jail
);
832 if (iface
->jail_device
)
833 blobmsg_add_string(&b
, "jail_device", iface
->jail_device
);
835 if (iface
->state
== IFS_UP
) {
836 if (iface
->updated
) {
837 a
= blobmsg_open_array(&b
, "updated");
839 if (iface
->updated
& IUF_ADDRESS
)
840 blobmsg_add_string(&b
, NULL
, "addresses");
841 if (iface
->updated
& IUF_ROUTE
)
842 blobmsg_add_string(&b
, NULL
, "routes");
843 if (iface
->updated
& IUF_PREFIX
)
844 blobmsg_add_string(&b
, NULL
, "prefixes");
845 if (iface
->updated
& IUF_DATA
)
846 blobmsg_add_string(&b
, NULL
, "data");
848 blobmsg_close_array(&b
, a
);
852 blobmsg_add_u32(&b
, "ip4table", iface
->ip4table
);
854 blobmsg_add_u32(&b
, "ip6table", iface
->ip6table
);
855 blobmsg_add_u32(&b
, "metric", iface
->metric
);
856 blobmsg_add_u32(&b
, "dns_metric", iface
->dns_metric
);
857 blobmsg_add_u8(&b
, "delegation", !iface
->proto_ip
.no_delegation
);
858 if (iface
->assignment_weight
)
859 blobmsg_add_u32(&b
, "ip6weight", iface
->assignment_weight
);
860 a
= blobmsg_open_array(&b
, "ipv4-address");
861 interface_ip_dump_address_list(&iface
->config_ip
, false, true);
862 interface_ip_dump_address_list(&iface
->proto_ip
, false, true);
863 blobmsg_close_array(&b
, a
);
864 a
= blobmsg_open_array(&b
, "ipv6-address");
865 interface_ip_dump_address_list(&iface
->config_ip
, true, true);
866 interface_ip_dump_address_list(&iface
->proto_ip
, true, true);
867 blobmsg_close_array(&b
, a
);
868 a
= blobmsg_open_array(&b
, "ipv6-prefix");
869 interface_ip_dump_prefix_list(&iface
->config_ip
);
870 interface_ip_dump_prefix_list(&iface
->proto_ip
);
871 blobmsg_close_array(&b
, a
);
872 a
= blobmsg_open_array(&b
, "ipv6-prefix-assignment");
873 interface_ip_dump_prefix_assignment_list(iface
);
874 blobmsg_close_array(&b
, a
);
875 a
= blobmsg_open_array(&b
, "route");
876 interface_ip_dump_route_list(&iface
->config_ip
, true);
877 interface_ip_dump_route_list(&iface
->proto_ip
, true);
878 blobmsg_close_array(&b
, a
);
879 a
= blobmsg_open_array(&b
, "dns-server");
880 interface_ip_dump_dns_server_list(&iface
->config_ip
, true);
881 interface_ip_dump_dns_server_list(&iface
->proto_ip
, true);
882 blobmsg_close_array(&b
, a
);
883 a
= blobmsg_open_array(&b
, "dns-search");
884 interface_ip_dump_dns_search_list(&iface
->config_ip
, true);
885 interface_ip_dump_dns_search_list(&iface
->proto_ip
, true);
886 blobmsg_close_array(&b
, a
);
887 a
= blobmsg_open_array(&b
, "neighbors");
888 interface_ip_dump_neighbor_list(&iface
->config_ip
, true);
889 interface_ip_dump_neighbor_list(&iface
->proto_ip
, true);
890 blobmsg_close_array(&b
, a
);
892 inactive
= blobmsg_open_table(&b
, "inactive");
893 a
= blobmsg_open_array(&b
, "ipv4-address");
894 interface_ip_dump_address_list(&iface
->config_ip
, false, false);
895 interface_ip_dump_address_list(&iface
->proto_ip
, false, false);
896 blobmsg_close_array(&b
, a
);
897 a
= blobmsg_open_array(&b
, "ipv6-address");
898 interface_ip_dump_address_list(&iface
->config_ip
, true, false);
899 interface_ip_dump_address_list(&iface
->proto_ip
, true, false);
900 blobmsg_close_array(&b
, a
);
901 a
= blobmsg_open_array(&b
, "route");
902 interface_ip_dump_route_list(&iface
->config_ip
, false);
903 interface_ip_dump_route_list(&iface
->proto_ip
, false);
904 blobmsg_close_array(&b
, a
);
905 a
= blobmsg_open_array(&b
, "dns-server");
906 interface_ip_dump_dns_server_list(&iface
->config_ip
, false);
907 interface_ip_dump_dns_server_list(&iface
->proto_ip
, false);
908 blobmsg_close_array(&b
, a
);
909 a
= blobmsg_open_array(&b
, "dns-search");
910 interface_ip_dump_dns_search_list(&iface
->config_ip
, false);
911 interface_ip_dump_dns_search_list(&iface
->proto_ip
, false);
912 blobmsg_close_array(&b
, a
);
913 a
= blobmsg_open_array(&b
, "neighbors");
914 interface_ip_dump_neighbor_list(&iface
->config_ip
, false);
915 interface_ip_dump_neighbor_list(&iface
->proto_ip
, false);
916 blobmsg_close_array(&b
, a
);
917 blobmsg_close_table(&b
, inactive
);
920 a
= blobmsg_open_table(&b
, "data");
923 blobmsg_add_string(&b
, "zone", iface
->zone
);
924 avl_for_each_element(&iface
->data
, data
, node
)
925 blobmsg_add_blob(&b
, data
->data
);
927 blobmsg_close_table(&b
, a
);
929 if (!list_empty(&iface
->errors
))
930 netifd_add_interface_errors(&b
, iface
);
934 netifd_handle_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
935 struct ubus_request_data
*req
, const char *method
,
936 struct blob_attr
*msg
)
938 struct interface
*iface
= container_of(obj
, struct interface
, ubus
);
940 blob_buf_init(&b
, 0);
941 netifd_dump_status(iface
);
942 ubus_send_reply(ctx
, req
, b
.head
);
949 netifd_handle_dump(struct ubus_context
*ctx
, struct ubus_object
*obj
,
950 struct ubus_request_data
*req
, const char *method
,
951 struct blob_attr
*msg
)
953 blob_buf_init(&b
, 0);
954 void *a
= blobmsg_open_array(&b
, "interface");
956 struct interface
*iface
;
957 vlist_for_each_element(&interfaces
, iface
, node
) {
958 void *i
= blobmsg_open_table(&b
, NULL
);
959 blobmsg_add_string(&b
, "interface", iface
->name
);
960 netifd_dump_status(iface
);
961 blobmsg_close_table(&b
, i
);
964 blobmsg_close_array(&b
, a
);
965 ubus_send_reply(ctx
, req
, b
.head
);
977 static const struct blobmsg_policy dev_link_policy
[__DEV_LINK_MAX
] = {
978 [DEV_LINK_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
979 [DEV_LINK_EXT
] = { .name
= "link-ext", .type
= BLOBMSG_TYPE_BOOL
},
980 [DEV_LINK_VLAN
] = { .name
= "vlan", .type
= BLOBMSG_TYPE_ARRAY
},
984 netifd_iface_handle_device(struct ubus_context
*ctx
, struct ubus_object
*obj
,
985 struct ubus_request_data
*req
, const char *method
,
986 struct blob_attr
*msg
)
988 struct blob_attr
*tb
[__DEV_LINK_MAX
];
989 struct blob_attr
*cur
;
990 struct interface
*iface
;
991 bool add
= !strncmp(method
, "add", 3);
992 bool link_ext
= true;
994 iface
= container_of(obj
, struct interface
, ubus
);
996 blobmsg_parse(dev_link_policy
, __DEV_LINK_MAX
, tb
, blob_data(msg
), blob_len(msg
));
998 if (!tb
[DEV_LINK_NAME
])
999 return UBUS_STATUS_INVALID_ARGUMENT
;
1001 cur
= tb
[DEV_LINK_EXT
];
1003 link_ext
= blobmsg_get_bool(cur
);
1005 return interface_handle_link(iface
, blobmsg_data(tb
[DEV_LINK_NAME
]),
1006 tb
[DEV_LINK_VLAN
], add
, link_ext
);
1011 netifd_iface_notify_proto(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1012 struct ubus_request_data
*req
, const char *method
,
1013 struct blob_attr
*msg
)
1015 struct interface
*iface
;
1017 iface
= container_of(obj
, struct interface
, ubus
);
1019 if (!iface
->proto
|| !iface
->proto
->notify
)
1020 return UBUS_STATUS_NOT_SUPPORTED
;
1022 return iface
->proto
->notify(iface
->proto
, msg
);
1026 netifd_iface_do_remove(struct uloop_timeout
*timeout
)
1028 struct interface
*iface
;
1030 iface
= container_of(timeout
, struct interface
, remove_timer
);
1031 vlist_delete(&interfaces
, &iface
->node
);
1035 netifd_iface_remove(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1036 struct ubus_request_data
*req
, const char *method
,
1037 struct blob_attr
*msg
)
1039 struct interface
*iface
;
1041 iface
= container_of(obj
, struct interface
, ubus
);
1042 if (iface
->remove_timer
.cb
)
1043 return UBUS_STATUS_INVALID_ARGUMENT
;
1045 iface
->remove_timer
.cb
= netifd_iface_do_remove
;
1046 uloop_timeout_set(&iface
->remove_timer
, 100);
1051 netifd_handle_iface_prepare(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1052 struct ubus_request_data
*req
, const char *method
,
1053 struct blob_attr
*msg
)
1055 struct interface
*iface
;
1056 struct device
*dev
, *bridge_dev
= NULL
;
1057 const struct device_hotplug_ops
*ops
;
1059 iface
= container_of(obj
, struct interface
, ubus
);
1060 dev
= iface
->main_dev
.dev
;
1064 ops
= dev
->hotplug_ops
;
1068 ops
->prepare(dev
, &bridge_dev
);
1071 blob_buf_init(&b
, 0);
1073 blobmsg_add_string(&b
, "bridge", bridge_dev
->ifname
);
1074 ubus_send_reply(ctx
, req
, b
.head
);
1080 netifd_handle_set_data(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1081 struct ubus_request_data
*req
, const char *method
,
1082 struct blob_attr
*msg
)
1084 struct interface
*iface
;
1086 iface
= container_of(obj
, struct interface
, ubus
);
1088 return interface_parse_data(iface
, msg
);
1091 static struct ubus_method iface_object_methods
[] = {
1092 { .name
= "up", .handler
= netifd_handle_up
},
1093 { .name
= "down", .handler
= netifd_handle_down
},
1094 { .name
= "renew", .handler
= netifd_handle_renew
},
1095 { .name
= "status", .handler
= netifd_handle_status
},
1096 { .name
= "prepare", .handler
= netifd_handle_iface_prepare
},
1097 { .name
= "dump", .handler
= netifd_handle_dump
},
1098 UBUS_METHOD("add_device", netifd_iface_handle_device
, dev_link_policy
),
1099 UBUS_METHOD("remove_device", netifd_iface_handle_device
, dev_link_policy
),
1100 { .name
= "notify_proto", .handler
= netifd_iface_notify_proto
},
1101 { .name
= "remove", .handler
= netifd_iface_remove
},
1102 { .name
= "set_data", .handler
= netifd_handle_set_data
},
1105 static struct ubus_object_type iface_object_type
=
1106 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods
);
1109 static struct ubus_object iface_object
= {
1110 .name
= "network.interface",
1111 .type
= &iface_object_type
,
1112 .n_methods
= ARRAY_SIZE(iface_object_methods
),
1115 static void netifd_add_object(struct ubus_object
*obj
)
1117 int ret
= ubus_add_object(ubus_ctx
, obj
);
1120 fprintf(stderr
, "Failed to publish object '%s': %s\n", obj
->name
, ubus_strerror(ret
));
1123 static const struct blobmsg_policy iface_policy
= {
1124 .name
= "interface",
1125 .type
= BLOBMSG_TYPE_STRING
,
1129 netifd_handle_iface(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1130 struct ubus_request_data
*req
, const char *method
,
1131 struct blob_attr
*msg
)
1133 struct interface
*iface
;
1134 struct blob_attr
*tb
;
1137 blobmsg_parse(&iface_policy
, 1, &tb
, blob_data(msg
), blob_len(msg
));
1139 return UBUS_STATUS_INVALID_ARGUMENT
;
1141 iface
= vlist_find(&interfaces
, blobmsg_data(tb
), iface
, node
);
1143 return UBUS_STATUS_NOT_FOUND
;
1145 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1148 if (strcmp(method
, iface_object_methods
[i
].name
) != 0)
1151 cb
= iface_object_methods
[i
].handler
;
1152 return cb(ctx
, &iface
->ubus
, req
, method
, msg
);
1155 return UBUS_STATUS_INVALID_ARGUMENT
;
1158 static void netifd_add_iface_object(void)
1160 struct ubus_method
*methods
;
1163 methods
= calloc(1, sizeof(iface_object_methods
));
1167 memcpy(methods
, iface_object_methods
, sizeof(iface_object_methods
));
1168 iface_object
.methods
= methods
;
1170 for (i
= 0; i
< ARRAY_SIZE(iface_object_methods
); i
++) {
1171 if (methods
[i
].handler
== netifd_handle_dump
)
1174 methods
[i
].handler
= netifd_handle_iface
;
1175 methods
[i
].policy
= &iface_policy
;
1176 methods
[i
].n_policy
= 1;
1178 netifd_add_object(&iface_object
);
1181 static struct wireless_device
*
1182 get_wdev(struct blob_attr
*msg
, int *ret
)
1184 struct blobmsg_policy wdev_policy
= {
1186 .type
= BLOBMSG_TYPE_STRING
,
1188 struct blob_attr
*dev_attr
;
1189 struct wireless_device
*wdev
= NULL
;
1192 blobmsg_parse(&wdev_policy
, 1, &dev_attr
, blob_data(msg
), blob_len(msg
));
1194 *ret
= UBUS_STATUS_INVALID_ARGUMENT
;
1198 wdev
= vlist_find(&wireless_devices
, blobmsg_data(dev_attr
), wdev
, node
);
1200 *ret
= UBUS_STATUS_NOT_FOUND
;
1209 netifd_handle_wdev_reconf(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1210 struct ubus_request_data
*req
, const char *method
,
1211 struct blob_attr
*msg
)
1213 struct wireless_device
*wdev
;
1216 wdev
= get_wdev(msg
, &ret
);
1217 if (ret
== UBUS_STATUS_NOT_FOUND
)
1221 wireless_device_reconf(wdev
);
1223 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1224 wireless_device_reconf(wdev
);
1231 netifd_handle_wdev_up(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1232 struct ubus_request_data
*req
, const char *method
,
1233 struct blob_attr
*msg
)
1235 struct wireless_device
*wdev
;
1238 wdev
= get_wdev(msg
, &ret
);
1239 if (ret
== UBUS_STATUS_NOT_FOUND
)
1243 wireless_device_set_up(wdev
);
1245 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1246 wireless_device_set_up(wdev
);
1253 netifd_handle_wdev_down(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1254 struct ubus_request_data
*req
, const char *method
,
1255 struct blob_attr
*msg
)
1257 struct wireless_device
*wdev
;
1260 wdev
= get_wdev(msg
, &ret
);
1261 if (ret
== UBUS_STATUS_NOT_FOUND
)
1265 wireless_device_set_down(wdev
);
1267 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1268 wireless_device_set_down(wdev
);
1275 netifd_handle_wdev_status(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1276 struct ubus_request_data
*req
, const char *method
,
1277 struct blob_attr
*msg
)
1279 struct wireless_device
*wdev
;
1282 wdev
= get_wdev(msg
, &ret
);
1283 if (ret
== UBUS_STATUS_NOT_FOUND
)
1286 blob_buf_init(&b
, 0);
1288 wireless_device_status(wdev
, &b
);
1290 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1291 wireless_device_status(wdev
, &b
);
1293 ubus_send_reply(ctx
, req
, b
.head
);
1298 netifd_handle_wdev_get_validate(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1299 struct ubus_request_data
*req
, const char *method
,
1300 struct blob_attr
*msg
)
1302 struct wireless_device
*wdev
;
1305 wdev
= get_wdev(msg
, &ret
);
1306 if (ret
== UBUS_STATUS_NOT_FOUND
)
1309 blob_buf_init(&b
, 0);
1311 wireless_device_get_validate(wdev
, &b
);
1313 vlist_for_each_element(&wireless_devices
, wdev
, node
)
1314 wireless_device_get_validate(wdev
, &b
);
1316 ubus_send_reply(ctx
, req
, b
.head
);
1321 netifd_handle_wdev_notify(struct ubus_context
*ctx
, struct ubus_object
*obj
,
1322 struct ubus_request_data
*req
, const char *method
,
1323 struct blob_attr
*msg
)
1325 struct wireless_device
*wdev
;
1328 wdev
= get_wdev(msg
, &ret
);
1332 return wireless_device_notify(wdev
, msg
, req
);
1335 static struct ubus_method wireless_object_methods
[] = {
1336 { .name
= "up", .handler
= netifd_handle_wdev_up
},
1337 { .name
= "down", .handler
= netifd_handle_wdev_down
},
1338 { .name
= "reconf", .handler
= netifd_handle_wdev_reconf
},
1339 { .name
= "status", .handler
= netifd_handle_wdev_status
},
1340 { .name
= "notify", .handler
= netifd_handle_wdev_notify
},
1341 { .name
= "get_validate", .handler
= netifd_handle_wdev_get_validate
},
1344 static struct ubus_object_type wireless_object_type
=
1345 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods
);
1348 static struct ubus_object wireless_object
= {
1349 .name
= "network.wireless",
1350 .type
= &wireless_object_type
,
1351 .methods
= wireless_object_methods
,
1352 .n_methods
= ARRAY_SIZE(wireless_object_methods
),
1356 netifd_extdev_invoke(uint32_t id
, const char *method
, struct blob_attr
*msg
,
1357 ubus_data_handler_t data_cb
, void *data
)
1359 return ubus_invoke(ubus_ctx
, id
, method
, msg
, data_cb
, data
, 3000);
1363 netifd_ubus_init(const char *path
)
1368 ubus_ctx
= ubus_connect(path
);
1372 DPRINTF("connected as %08x\n", ubus_ctx
->local_id
);
1373 ubus_ctx
->connection_lost
= netifd_ubus_connection_lost
;
1374 netifd_ubus_add_fd();
1376 netifd_add_object(&main_object
);
1377 netifd_add_object(&dev_object
);
1378 netifd_add_object(&wireless_object
);
1379 netifd_add_iface_object();
1385 netifd_ubus_done(void)
1387 ubus_free(ubus_ctx
);
1391 netifd_ubus_interface_event(struct interface
*iface
, bool up
)
1393 blob_buf_init(&b
, 0);
1394 blobmsg_add_string(&b
, "action", up
? "ifup" : "ifdown");
1395 blobmsg_add_string(&b
, "interface", iface
->name
);
1396 ubus_send_event(ubus_ctx
, "network.interface", b
.head
);
1400 netifd_ubus_interface_notify(struct interface
*iface
, bool up
)
1402 const char *event
= (up
) ? "interface.update" : "interface.down";
1403 blob_buf_init(&b
, 0);
1404 blobmsg_add_string(&b
, "interface", iface
->name
);
1405 netifd_dump_status(iface
);
1406 ubus_notify(ubus_ctx
, &iface_object
, event
, b
.head
, -1);
1407 ubus_notify(ubus_ctx
, &iface
->ubus
, event
, b
.head
, -1);
1411 netifd_ubus_add_interface(struct interface
*iface
)
1413 struct ubus_object
*obj
= &iface
->ubus
;
1416 if (asprintf(&name
, "%s.interface.%s", main_object
.name
, iface
->name
) == -1)
1420 obj
->type
= &iface_object_type
;
1421 obj
->methods
= iface_object_methods
;
1422 obj
->n_methods
= ARRAY_SIZE(iface_object_methods
);
1423 if (ubus_add_object(ubus_ctx
, &iface
->ubus
)) {
1424 DPRINTF("failed to publish ubus object for interface '%s'\n", iface
->name
);
1431 netifd_ubus_remove_interface(struct interface
*iface
)
1433 if (!iface
->ubus
.name
)
1436 ubus_remove_object(ubus_ctx
, &iface
->ubus
);
1437 free((void *) iface
->ubus
.name
);