3 #include <sys/select.h>
6 #include <libubox/blob.h>
7 #include <libubox/blobmsg.h>
8 #include <libubox/list.h>
10 #include <arpa/inet.h>
12 #include <netinet/in.h>
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
17 #include "configuration.h"
18 #include "interface.h"
22 #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 // Definition of interface function enums (to be used with ubox's blobmsg
59 enum { INTERFACE_IFNAME
, __INTERFACE_MAX
};
61 // Definition of interface parsing (to be used with ubox's blobmsg helpers).
62 static const struct blobmsg_policy interface_policy
[__INTERFACE_MAX
] = {
63 [INTERFACE_IFNAME
] = {"ifname", BLOBMSG_TYPE_STRING
},
66 // Adds an inteface (ubus equivalent to "interface"-function).
67 static int babeld_ubus_add_interface(struct ubus_context
*ctx_local
,
68 struct ubus_object
*obj
,
69 struct ubus_request_data
*req
,
71 struct blob_attr
*msg
) {
72 struct blob_attr
*tb
[__INTERFACE_MAX
];
73 struct blob_buf b
= {0};
74 struct interface
*ifp
= NULL
;
78 blobmsg_parse(interface_policy
, __INTERFACE_MAX
, tb
, blob_data(msg
),
81 if (!tb
[INTERFACE_IFNAME
])
82 return UBUS_STATUS_INVALID_ARGUMENT
;
84 ifname
= blobmsg_get_string(tb
[INTERFACE_IFNAME
]);
86 ifp
= add_interface(ifname
, NULL
);
88 return UBUS_STATUS_UNKNOWN_ERROR
;
90 return UBUS_STATUS_OK
;
93 // Sends a babel info message on ubus socket.
94 static int babeld_ubus_babeld_info(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};
103 blob_buf_init(&b
, 0);
104 blobmsg_add_string(&b
, "babeld-version", BABELD_VERSION
);
105 blobmsg_add_string(&b
, "my-id", format_eui64(myid
));
106 if (!gethostname(host
, sizeof(host
)))
107 blobmsg_add_string(&b
, "host", host
);
109 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
111 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
118 // Appends an exported route message entry to the buffer.
119 static void babeld_add_xroute_buf(struct xroute
*xroute
, struct blob_buf
*b
) {
122 prefix
= blobmsg_open_table(b
, format_prefix(xroute
->prefix
, xroute
->plen
));
124 blobmsg_add_string(b
, "src-prefix",
125 format_prefix(xroute
->src_prefix
, xroute
->src_plen
));
126 blobmsg_add_u32(b
, "metric", xroute
->metric
);
127 blobmsg_close_table(b
, prefix
);
130 // Sends an exported routes message on ubus socket, splitting apart IPv4 and
132 static int babeld_ubus_get_xroutes(struct ubus_context
*ctx_local
,
133 struct ubus_object
*obj
,
134 struct ubus_request_data
*req
,
135 const char *method
, struct blob_attr
*msg
) {
136 struct blob_buf b
= {0};
137 struct xroute_stream
*xroutes
;
138 struct xroute_list_entry
*cur
, *tmp
;
141 LIST_HEAD(xroute_ipv4_list
);
142 LIST_HEAD(xroute_ipv6_list
);
144 blob_buf_init(&b
, 0);
146 xroutes
= xroute_stream();
149 struct xroute
*xroute
= xroute_stream_next(xroutes
);
153 struct xroute_list_entry
*xr
=
154 calloc(1, sizeof(struct xroute_list_entry
));
157 if (v4mapped(xroute
->prefix
)) {
158 list_add(&xr
->list
, &xroute_ipv4_list
);
160 list_add(&xr
->list
, &xroute_ipv6_list
);
163 xroute_stream_done(xroutes
);
166 ipv4
= blobmsg_open_table(&b
, "IPv4");
167 list_for_each_entry_safe(cur
, tmp
, &xroute_ipv4_list
, list
) {
168 babeld_add_xroute_buf(cur
->xroute
, &b
);
169 list_del(&cur
->list
);
172 blobmsg_close_table(&b
, ipv4
);
174 ipv6
= blobmsg_open_table(&b
, "IPv6");
175 list_for_each_entry_safe(cur
, tmp
, &xroute_ipv6_list
, list
) {
176 babeld_add_xroute_buf(cur
->xroute
, &b
);
177 list_del(&cur
->list
);
180 blobmsg_close_table(&b
, ipv6
);
182 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
184 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
191 // Appends an route message entry to the buffer.
192 static void babeld_add_route_buf(struct babel_route
*route
,
193 struct blob_buf
*b
) {
197 if (route
->channels_len
== 0) {
201 snprintf(channels
, sizeof(channels
), " chan (");
202 j
= strlen(channels
);
203 for (i
= 0; i
< route
->channels_len
; i
++) {
206 snprintf(channels
+ j
, sizeof(channels
) - j
, "%u",
207 (unsigned)route
->channels
[i
]);
208 j
= strlen(channels
);
210 snprintf(channels
+ j
, sizeof(channels
) - j
, ")");
213 prefix
= blobmsg_open_table(
214 b
, format_prefix(route
->src
->prefix
, route
->src
->plen
));
218 format_prefix(route
->src
->src_prefix
, route
->src
->src_plen
));
219 blobmsg_add_u32(b
, "route_metric", route_metric(route
));
220 blobmsg_add_u32(b
, "route_smoothed_metric", route_smoothed_metric(route
));
221 blobmsg_add_u32(b
, "refmetric", route
->refmetric
);
222 blobmsg_add_string(b
, "id", format_eui64(route
->src
->id
));
223 blobmsg_add_u32(b
, "seqno", (uint32_t)route
->seqno
);
224 blobmsg_add_string(b
, "channels", channels
);
225 blobmsg_add_u32(b
, "age", (int)(now
.tv_sec
- route
->time
));
226 blobmsg_add_string(b
, "via", format_address(route
->neigh
->address
));
227 if (memcmp(route
->nexthop
, route
->neigh
->address
, 16) != 0)
228 blobmsg_add_string(b
, "nexthop", format_address(route
->nexthop
));
230 blobmsg_add_u8(b
, "installed", route
->installed
);
231 blobmsg_add_u8(b
, "feasible", route_feasible(route
));
233 blobmsg_close_table(b
, prefix
);
236 // Sends received routes message on ubus socket, splitting apart IPv4 and IPv6
238 static int babeld_ubus_get_routes(struct ubus_context
*ctx_local
,
239 struct ubus_object
*obj
,
240 struct ubus_request_data
*req
,
241 const char *method
, struct blob_attr
*msg
) {
242 struct blob_buf b
= {0};
243 struct route_stream
*routes
;
244 struct route_list_entry
*cur
, *tmp
;
245 void *prefix
, *ipv4
, *ipv6
;
247 LIST_HEAD(route_ipv4_list
);
248 LIST_HEAD(route_ipv6_list
);
250 blob_buf_init(&b
, 0);
252 routes
= route_stream(0);
255 struct babel_route
*route
= route_stream_next(routes
);
258 struct route_list_entry
*r
= calloc(1, sizeof(struct route_list_entry
));
261 if (v4mapped(route
->src
->prefix
)) {
262 list_add(&r
->list
, &route_ipv4_list
);
264 list_add(&r
->list
, &route_ipv6_list
);
267 route_stream_done(routes
);
270 ipv4
= blobmsg_open_table(&b
, "IPv4");
271 list_for_each_entry_safe(cur
, tmp
, &route_ipv4_list
, list
) {
272 babeld_add_route_buf(cur
->route
, &b
);
273 list_del(&cur
->list
);
276 blobmsg_close_table(&b
, ipv4
);
278 ipv6
= blobmsg_open_table(&b
, "IPv6");
279 list_for_each_entry_safe(cur
, tmp
, &route_ipv6_list
, list
) {
280 babeld_add_route_buf(cur
->route
, &b
);
281 list_del(&cur
->list
);
284 blobmsg_close_table(&b
, ipv6
);
286 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
288 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
295 // Appends an neighbour entry to the buffer.
296 static void babeld_add_neighbour_buf(struct neighbour
*neigh
,
297 struct blob_buf
*b
) {
300 neighbour
= blobmsg_open_table(b
, format_address(neigh
->address
));
301 blobmsg_add_string(b
, "dev", neigh
->ifp
->name
);
302 blobmsg_add_u32(b
, "hello-reach", neigh
->hello
.reach
);
303 blobmsg_add_u32(b
, "uhello-reach", neigh
->uhello
.reach
);
304 blobmsg_add_u32(b
, "rxcost", neighbour_rxcost(neigh
));
305 blobmsg_add_u32(b
, "txcost", neigh
->txcost
);
306 blobmsg_add_string(b
, "rtt", format_thousands(neigh
->rtt
));
307 blobmsg_add_u32(b
, "channel", neigh
->ifp
->channel
);
308 blobmsg_add_u8(b
, "if_up", if_up(neigh
->ifp
));
309 blobmsg_close_table(b
, neighbour
);
312 // Sends neighbours message on ubus socket, splitting apart IPv4 and IPv6
314 static int babeld_ubus_get_neighbours(struct ubus_context
*ctx_local
,
315 struct ubus_object
*obj
,
316 struct ubus_request_data
*req
,
318 struct blob_attr
*msg
) {
319 struct blob_buf b
= {0};
320 struct neighbour
*neigh
;
321 struct neighbour_list_entry
*cur
, *tmp
;
324 LIST_HEAD(neighbour_ipv4_list
);
325 LIST_HEAD(neighbour_ipv6_list
);
327 blob_buf_init(&b
, 0);
329 FOR_ALL_NEIGHBOURS(neigh
) {
330 struct neighbour_list_entry
*n
=
331 calloc(1, sizeof(struct neighbour_list_entry
));
332 n
->neighbour
= neigh
;
333 if (v4mapped(neigh
->address
)) {
334 list_add(&n
->list
, &neighbour_ipv4_list
);
336 list_add(&n
->list
, &neighbour_ipv6_list
);
340 ipv4
= blobmsg_open_table(&b
, "IPv4");
341 list_for_each_entry_safe(cur
, tmp
, &neighbour_ipv4_list
, list
) {
342 babeld_add_neighbour_buf(cur
->neighbour
, &b
);
343 list_del(&cur
->list
);
346 blobmsg_close_table(&b
, ipv4
);
348 ipv6
= blobmsg_open_table(&b
, "IPv6");
349 list_for_each_entry_safe(cur
, tmp
, &neighbour_ipv6_list
, list
) {
350 babeld_add_neighbour_buf(cur
->neighbour
, &b
);
351 list_del(&cur
->list
);
354 blobmsg_close_table(&b
, ipv6
);
356 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
358 fprintf(stderr
, "Failed to send reply: %s\n", ubus_strerror(ret
));
365 // List of functions we expose via the ubus bus.
366 static const struct ubus_method babeld_methods
[] = {
367 UBUS_METHOD("add_interface", babeld_ubus_add_interface
, interface_policy
),
368 UBUS_METHOD_NOARG("get_info", babeld_ubus_babeld_info
),
369 UBUS_METHOD_NOARG("get_xroutes", babeld_ubus_get_xroutes
),
370 UBUS_METHOD_NOARG("get_routes", babeld_ubus_get_routes
),
371 UBUS_METHOD_NOARG("get_neighbours", babeld_ubus_get_neighbours
),
374 // Definition of the ubus object type.
375 static struct ubus_object_type babeld_object_type
=
376 UBUS_OBJECT_TYPE("babeld", babeld_methods
);
378 // Object we announce via the ubus bus.
379 static struct ubus_object babeld_object
= {
381 .type
= &babeld_object_type
,
382 .methods
= babeld_methods
,
383 .n_methods
= ARRAY_SIZE(babeld_methods
),
386 // Registers handlers for babel methods in the global ubus context.
387 static bool ubus_init_object() {
390 ret
= ubus_add_object(shared_ctx
, &babeld_object
);
392 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
399 // Initializes the global ubus context, connecting to the bus to be able to
400 // receive and send messages.
401 static bool babeld_ubus_init(void) {
405 shared_ctx
= ubus_connect(NULL
);
412 void ubus_notify_route(struct babel_route
*route
, int kind
) {
413 struct blob_buf b
= {0};
414 char method
[50]; // possible methods are route.change, route.add, route.flush
416 if (!babeld_object
.has_subscribers
)
425 blob_buf_init(&b
, 0);
426 babeld_add_route_buf(route
, &b
);
427 snprintf(method
, sizeof(method
), "route.%s", local_kind(kind
));
428 ubus_notify(shared_ctx
, &babeld_object
, method
, b
.head
, -1);
432 void ubus_notify_xroute(struct xroute
*xroute
, int kind
) {
433 struct blob_buf b
= {0};
434 char method
[50]; // possible methods are xroute.change, xroute.add,
437 if (!babeld_object
.has_subscribers
)
446 blob_buf_init(&b
, 0);
447 babeld_add_xroute_buf(xroute
, &b
);
448 snprintf(method
, sizeof(method
), "xroute.%s", local_kind(kind
));
449 ubus_notify(shared_ctx
, &babeld_object
, method
, b
.head
, -1);
453 void ubus_notify_neighbour(struct neighbour
*neigh
, int kind
) {
454 struct blob_buf b
= {0};
455 char method
[50]; // possible methods are neigh.change, neigh.add, neigh.flush
457 if (!babeld_object
.has_subscribers
)
466 blob_buf_init(&b
, 0);
467 babeld_add_neighbour_buf(neigh
, &b
);
468 snprintf(method
, sizeof(method
), "neigh.%s", local_kind(kind
));
469 ubus_notify(shared_ctx
, &babeld_object
, method
, b
.head
, -1);
473 void babeld_ubus_receive(fd_set
*readfds
) {
476 if (FD_ISSET(shared_ctx
->sock
.fd
, readfds
))
477 ubus_handle_event(shared_ctx
);
480 int babeld_ubus_add_read_sock(fd_set
*readfds
, int maxfd
) {
484 FD_SET(shared_ctx
->sock
.fd
, readfds
);
485 return MAX(maxfd
, shared_ctx
->sock
.fd
);
488 bool babeld_add_ubus() {
489 if (!babeld_ubus_init()) {
490 fprintf(stderr
, "Failed to initialize ubus!\n");
494 if (!ubus_init_object()) {
495 fprintf(stderr
, "Failed to add objects to ubus!\n");