3 #include <sys/select.h>
5 #include <libubox/blob.h>
6 #include <libubox/blobmsg.h>
7 #include <libubox/list.h>
11 #include <netinet/in.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
16 #include "configuration.h"
17 #include "interface.h"
21 #include "neighbour.h"
33 // Definition of header variable whether to enable ubus bindings.
34 int ubus_bindings
= 0;
36 // Shared state maintained throughout calls to handle ubus messages.
37 static struct ubus_context
*shared_ctx
;
39 // List of exported routes (to be used with ubox's list helpers).
40 struct xroute_list_entry
{
41 struct list_head list
;
42 struct xroute
*xroute
;
45 // List of received routes (to be used with ubox's list helpers).
46 struct route_list_entry
{
47 struct list_head list
;
48 struct babel_route
*route
;
51 // List of neighbours (to be used with ubox's list helpers).
52 struct neighbour_list_entry
{
53 struct list_head list
;
54 struct neighbour
*neighbour
;
57 // Sends a babel info message on ubus socket.
58 static int babeld_ubus_babeld_info(struct ubus_context
*ctx_local
,
59 struct ubus_object
*obj
,
60 struct ubus_request_data
*req
,
61 const char *method
, struct blob_attr
*msg
) {
62 struct blob_buf b
= {0};
68 blobmsg_add_string(&b
, "babeld-version", BABELD_VERSION
);
69 blobmsg_add_string(&b
, "my-id", format_eui64(myid
));
70 if (!gethostname(host
, sizeof(host
)))
71 blobmsg_add_string(&b
, "host", host
);
73 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
75 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
80 // Appends an exported route message entry to the buffer.
81 static void babeld_add_xroute_buf(struct xroute
*xroute
, struct blob_buf
*b
) {
84 prefix
= blobmsg_open_table(b
, format_prefix(xroute
->prefix
, xroute
->plen
));
86 blobmsg_add_string(b
, "src-prefix",
87 format_prefix(xroute
->src_prefix
, xroute
->src_plen
));
88 blobmsg_add_u32(b
, "metric", xroute
->metric
);
89 blobmsg_close_table(b
, prefix
);
92 // Sends an exported routes message on ubus socket, splitting apart IPv4 and
94 static int babeld_ubus_get_xroutes(struct ubus_context
*ctx_local
,
95 struct ubus_object
*obj
,
96 struct ubus_request_data
*req
,
97 const char *method
, struct blob_attr
*msg
) {
98 struct blob_buf b
= {0};
99 struct xroute_stream
*xroutes
;
100 struct xroute_list_entry
*cur
, *tmp
;
103 LIST_HEAD(xroute_ipv4_list
);
104 LIST_HEAD(xroute_ipv6_list
);
106 xroutes
= xroute_stream();
109 struct xroute
*xroute
= xroute_stream_next(xroutes
);
113 struct xroute_list_entry
*xr
=
114 calloc(1, sizeof(struct xroute_list_entry
));
117 if (v4mapped(xroute
->prefix
)) {
118 list_add(&xr
->list
, &xroute_ipv4_list
);
120 list_add(&xr
->list
, &xroute_ipv6_list
);
123 xroute_stream_done(xroutes
);
126 blob_buf_init(&b
, 0);
127 ipv4
= blobmsg_open_table(&b
, "IPv4");
128 list_for_each_entry_safe(cur
, tmp
, &xroute_ipv4_list
, list
) {
129 babeld_add_xroute_buf(cur
->xroute
, &b
);
130 list_del(&cur
->list
);
133 blobmsg_close_table(&b
, ipv4
);
135 ipv6
= blobmsg_open_table(&b
, "IPv6");
136 list_for_each_entry_safe(cur
, tmp
, &xroute_ipv6_list
, list
) {
137 babeld_add_xroute_buf(cur
->xroute
, &b
);
138 list_del(&cur
->list
);
141 blobmsg_close_table(&b
, ipv6
);
143 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
145 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
150 // Appends an route message entry to the buffer.
151 static void babeld_add_route_buf(struct babel_route
*route
,
152 struct blob_buf
*b
) {
156 if (route
->channels_len
== 0) {
160 snprintf(channels
, sizeof(channels
), " chan (");
161 j
= strlen(channels
);
162 for (i
= 0; i
< route
->channels_len
; i
++) {
165 snprintf(channels
+ j
, sizeof(channels
) - j
, "%u",
166 (unsigned)route
->channels
[i
]);
167 j
= strlen(channels
);
169 snprintf(channels
+ j
, sizeof(channels
) - j
, ")");
172 prefix
= blobmsg_open_table(
173 b
, format_prefix(route
->src
->prefix
, route
->src
->plen
));
177 format_prefix(route
->src
->src_prefix
, route
->src
->src_plen
));
178 blobmsg_add_u32(b
, "route_metric", route_metric(route
));
179 blobmsg_add_u32(b
, "route_smoothed_metric", route_smoothed_metric(route
));
180 blobmsg_add_u32(b
, "refmetric", route
->refmetric
);
181 blobmsg_add_string(b
, "id", format_eui64(route
->src
->id
));
182 blobmsg_add_u32(b
, "seqno", (uint32_t)route
->seqno
);
183 blobmsg_add_string(b
, "channels", channels
);
184 blobmsg_add_u32(b
, "age", (int)(now
.tv_sec
- route
->time
));
185 blobmsg_add_string(b
, "via", format_address(route
->neigh
->address
));
186 if (memcmp(route
->nexthop
, route
->neigh
->address
, 16) != 0)
187 blobmsg_add_string(b
, "nexthop", format_address(route
->nexthop
));
189 blobmsg_add_u8(b
, "installed", route
->installed
);
190 blobmsg_add_u8(b
, "feasible", route_feasible(route
));
192 blobmsg_close_table(b
, prefix
);
195 // Sends received routes message on ubus socket, splitting apart IPv4 and IPv6
197 static int babeld_ubus_get_routes(struct ubus_context
*ctx_local
,
198 struct ubus_object
*obj
,
199 struct ubus_request_data
*req
,
200 const char *method
, struct blob_attr
*msg
) {
201 struct blob_buf b
= {0};
202 struct route_stream
*routes
;
203 struct route_list_entry
*cur
, *tmp
;
204 void *prefix
, *ipv4
, *ipv6
;
206 LIST_HEAD(route_ipv4_list
);
207 LIST_HEAD(route_ipv6_list
);
209 blob_buf_init(&b
, 0);
211 routes
= route_stream(ROUTE_ALL
);
214 struct babel_route
*route
= route_stream_next(routes
);
217 struct route_list_entry
*r
= calloc(1, sizeof(struct route_list_entry
));
220 if (v4mapped(route
->src
->prefix
)) {
221 list_add(&r
->list
, &route_ipv4_list
);
223 list_add(&r
->list
, &route_ipv6_list
);
226 route_stream_done(routes
);
229 blob_buf_init(&b
, 0);
230 ipv4
= blobmsg_open_table(&b
, "IPv4");
231 list_for_each_entry_safe(cur
, tmp
, &route_ipv4_list
, list
) {
232 babeld_add_route_buf(cur
->route
, &b
);
233 list_del(&cur
->list
);
236 blobmsg_close_table(&b
, ipv4
);
238 ipv6
= blobmsg_open_table(&b
, "IPv6");
239 list_for_each_entry_safe(cur
, tmp
, &route_ipv6_list
, list
) {
240 babeld_add_route_buf(cur
->route
, &b
);
241 list_del(&cur
->list
);
244 blobmsg_close_table(&b
, ipv6
);
246 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
248 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
253 // Appends an neighbour entry to the buffer.
254 static void babeld_add_neighbour_buf(struct neighbour
*neigh
,
255 struct blob_buf
*b
) {
258 neighbour
= blobmsg_open_table(b
, format_address(neigh
->address
));
259 blobmsg_add_string(b
, "dev", neigh
->ifp
->name
);
260 blobmsg_add_u32(b
, "hello-reach", neigh
->hello
.reach
);
261 blobmsg_add_u32(b
, "uhello-reach", neigh
->uhello
.reach
);
262 blobmsg_add_u32(b
, "rxcost", neighbour_rxcost(neigh
));
263 blobmsg_add_u32(b
, "txcost", neigh
->txcost
);
264 blobmsg_add_string(b
, "rtt", format_thousands(neigh
->rtt
));
265 blobmsg_add_u32(b
, "channel", neigh
->ifp
->channel
);
266 blobmsg_add_u8(b
, "if_up", if_up(neigh
->ifp
));
267 blobmsg_close_table(b
, neighbour
);
270 // Sends neighbours message on ubus socket, splitting apart IPv4 and IPv6
272 static int babeld_ubus_get_neighbours(struct ubus_context
*ctx_local
,
273 struct ubus_object
*obj
,
274 struct ubus_request_data
*req
,
276 struct blob_attr
*msg
) {
277 struct blob_buf b
= {0};
278 struct neighbour
*neigh
;
279 struct neighbour_list_entry
*cur
, *tmp
;
282 LIST_HEAD(neighbour_ipv4_list
);
283 LIST_HEAD(neighbour_ipv6_list
);
285 blob_buf_init(&b
, 0);
287 FOR_ALL_NEIGHBOURS(neigh
) {
288 struct neighbour_list_entry
*n
=
289 calloc(1, sizeof(struct neighbour_list_entry
));
290 n
->neighbour
= neigh
;
291 if (v4mapped(neigh
->address
)) {
292 list_add(&n
->list
, &neighbour_ipv4_list
);
294 list_add(&n
->list
, &neighbour_ipv6_list
);
298 blob_buf_init(&b
, 0);
300 ipv4
= blobmsg_open_table(&b
, "IPv4");
301 list_for_each_entry_safe(cur
, tmp
, &neighbour_ipv4_list
, list
) {
302 babeld_add_neighbour_buf(cur
->neighbour
, &b
);
303 list_del(&cur
->list
);
306 blobmsg_close_table(&b
, ipv4
);
308 ipv6
= blobmsg_open_table(&b
, "IPv6");
309 list_for_each_entry_safe(cur
, tmp
, &neighbour_ipv6_list
, list
) {
310 babeld_add_neighbour_buf(cur
->neighbour
, &b
);
311 list_del(&cur
->list
);
314 blobmsg_close_table(&b
, ipv6
);
316 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
318 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
323 // List of functions we expose via the ubus bus.
324 static const struct ubus_method babeld_methods
[] = {
325 UBUS_METHOD_NOARG("get_info", babeld_ubus_babeld_info
),
326 UBUS_METHOD_NOARG("get_xroutes", babeld_ubus_get_xroutes
),
327 UBUS_METHOD_NOARG("get_routes", babeld_ubus_get_routes
),
328 UBUS_METHOD_NOARG("get_neighbours", babeld_ubus_get_neighbours
),
331 // Definition of the ubus object type.
332 static struct ubus_object_type babeld_object_type
=
333 UBUS_OBJECT_TYPE("babeld", babeld_methods
);
335 // Object we announce via the ubus bus.
336 static struct ubus_object babeld_object
= {
338 .type
= &babeld_object_type
,
339 .methods
= babeld_methods
,
340 .n_methods
= ARRAY_SIZE(babeld_methods
),
343 // Registers handlers for babel methods in the global ubus context.
344 static bool ubus_init_object() {
347 ret
= ubus_add_object(shared_ctx
, &babeld_object
);
349 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
356 // Initializes the global ubus context, connecting to the bus to be able to
357 // receive and send messages.
358 static bool babeld_ubus_init(void) {
362 shared_ctx
= ubus_connect(NULL
);
369 void ubus_notify_route(struct babel_route
*route
, int kind
) {
370 struct blob_buf b
= {0};
371 char method
[50]; // possible methods are route.change, route.add, route.flush
373 if (!babeld_object
.has_subscribers
)
382 blob_buf_init(&b
, 0);
383 babeld_add_route_buf(route
, &b
);
384 snprintf(method
, sizeof(method
), "route.%s", local_kind(kind
));
385 ubus_notify(shared_ctx
, &babeld_object
, method
, b
.head
, -1);
388 void ubus_notify_xroute(struct xroute
*xroute
, int kind
) {
389 struct blob_buf b
= {0};
390 char method
[50]; // possible methods are xroute.change, xroute.add,
393 if (!babeld_object
.has_subscribers
)
402 blob_buf_init(&b
, 0);
403 babeld_add_xroute_buf(xroute
, &b
);
404 snprintf(method
, sizeof(method
), "xroute.%s", local_kind(kind
));
405 ubus_notify(shared_ctx
, &babeld_object
, method
, b
.head
, -1);
408 void ubus_notify_neighbour(struct neighbour
*neigh
, int kind
) {
409 struct blob_buf b
= {0};
410 char method
[50]; // possible methods are neigh.change, neigh.add, neigh.flush
412 if (!babeld_object
.has_subscribers
)
421 blob_buf_init(&b
, 0);
422 babeld_add_neighbour_buf(neigh
, &b
);
423 snprintf(method
, sizeof(method
), "neigh.%s", local_kind(kind
));
424 ubus_notify(shared_ctx
, &babeld_object
, method
, b
.head
, -1);
427 void babeld_ubus_receive(fd_set
*readfds
) {
430 if (FD_ISSET(shared_ctx
->sock
.fd
, readfds
))
431 ubus_handle_event(shared_ctx
);
434 int babeld_ubus_add_read_sock(fd_set
*readfds
, int maxfd
) {
438 FD_SET(shared_ctx
->sock
.fd
, readfds
);
439 return MAX(maxfd
, shared_ctx
->sock
.fd
);
442 bool babeld_add_ubus() {
443 if (!babeld_ubus_init()) {
444 fprintf(stderr
, "Failed to initialize ubus!\n");
448 if (!ubus_init_object()) {
449 fprintf(stderr
, "Failed to add objects to ubus!\n");