add an ubus method for dumping available protocol handlers
[project/netifd.git] / ubus.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
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
8 *
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.
13 */
14 #define _GNU_SOURCE
15
16 #include <arpa/inet.h>
17 #include <string.h>
18 #include <stdio.h>
19
20 #include "netifd.h"
21 #include "interface.h"
22 #include "proto.h"
23 #include "ubus.h"
24 #include "system.h"
25
26 static struct ubus_context *ctx = NULL;
27 static struct blob_buf b;
28 static struct netifd_fd ubus_fd;
29 static const char *ubus_path;
30
31 /* global object */
32
33 static int
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)
37 {
38 netifd_restart();
39 return 0;
40 }
41
42 static int
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)
46 {
47 netifd_reload();
48 return 0;
49 }
50
51 enum {
52 HR_TARGET,
53 HR_V6,
54 __HR_MAX
55 };
56
57 static const struct blobmsg_policy route_policy[__HR_MAX] = {
58 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
59 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
60 };
61
62 static int
63 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
64 struct ubus_request_data *req, const char *method,
65 struct blob_attr *msg)
66 {
67 struct blob_attr *tb[__HR_MAX];
68 struct interface *iface;
69 union if_addr a;
70 bool v6 = false;
71
72 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
73 if (!tb[HR_TARGET])
74 return UBUS_STATUS_INVALID_ARGUMENT;
75
76 if (tb[HR_V6])
77 v6 = blobmsg_get_bool(tb[HR_V6]);
78
79 memset(&a, 0, sizeof(a));
80 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
81 return UBUS_STATUS_INVALID_ARGUMENT;
82
83
84 iface = interface_ip_add_target_route(&a, v6);
85 if (!iface)
86 return UBUS_STATUS_NOT_FOUND;
87
88 blob_buf_init(&b, 0);
89 blobmsg_add_string(&b, "interface", iface->name);
90 ubus_send_reply(ctx, req, b.head);
91
92 return 0;
93 }
94
95 static int
96 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
97 struct ubus_request_data *req, const char *method,
98 struct blob_attr *msg)
99 {
100 blob_buf_init(&b, 0);
101 proto_dump_handlers(&b);
102 ubus_send_reply(ctx, req, b.head);
103
104 return 0;
105 }
106
107 static struct ubus_method main_object_methods[] = {
108 { .name = "restart", .handler = netifd_handle_restart },
109 { .name = "reload", .handler = netifd_handle_reload },
110 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
111 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
112 };
113
114 static struct ubus_object_type main_object_type =
115 UBUS_OBJECT_TYPE("netifd", main_object_methods);
116
117 static struct ubus_object main_object = {
118 .name = "network",
119 .type = &main_object_type,
120 .methods = main_object_methods,
121 .n_methods = ARRAY_SIZE(main_object_methods),
122 };
123
124 enum {
125 DEV_NAME,
126 __DEV_MAX,
127 };
128
129 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
130 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
131 };
132
133 static int
134 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method,
136 struct blob_attr *msg)
137 {
138 struct device *dev = NULL;
139 struct blob_attr *tb[__DEV_MAX];
140
141 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
142
143 if (tb[DEV_NAME]) {
144 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
145 if (!dev)
146 return UBUS_STATUS_INVALID_ARGUMENT;
147 }
148
149 blob_buf_init(&b, 0);
150 device_dump_status(&b, dev);
151 ubus_send_reply(ctx, req, b.head);
152
153 return 0;
154 }
155
156 enum {
157 ALIAS_ATTR_ALIAS,
158 ALIAS_ATTR_DEV,
159 __ALIAS_ATTR_MAX,
160 };
161
162 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
163 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
164 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
165 };
166
167 static int
168 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
169 struct ubus_request_data *req, const char *method,
170 struct blob_attr *msg)
171 {
172 struct device *dev = NULL;
173 struct blob_attr *tb[__ALIAS_ATTR_MAX];
174 struct blob_attr *cur;
175 int rem;
176
177 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
178
179 if (!tb[ALIAS_ATTR_ALIAS])
180 return UBUS_STATUS_INVALID_ARGUMENT;
181
182 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
183 dev = device_get(blobmsg_data(cur), true);
184 if (!dev)
185 return UBUS_STATUS_NOT_FOUND;
186 }
187
188 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
189 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
190 goto error;
191
192 if (!blobmsg_check_attr(cur, NULL))
193 goto error;
194
195 alias_notify_device(blobmsg_data(cur), dev);
196 }
197 return 0;
198
199 error:
200 device_free_unused(dev);
201 return UBUS_STATUS_INVALID_ARGUMENT;
202 }
203
204 static struct ubus_method dev_object_methods[] = {
205 UBUS_METHOD("status", netifd_dev_status, dev_policy),
206 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
207 };
208
209 static struct ubus_object_type dev_object_type =
210 UBUS_OBJECT_TYPE("device", dev_object_methods);
211
212 static struct ubus_object dev_object = {
213 .name = "network.device",
214 .type = &dev_object_type,
215 .methods = dev_object_methods,
216 .n_methods = ARRAY_SIZE(dev_object_methods),
217 };
218
219 static void
220 netifd_ubus_add_fd(void)
221 {
222 ubus_add_uloop(ctx);
223 ubus_fd.fd = ctx->sock.fd;
224 netifd_fd_add(&ubus_fd);
225 }
226
227 static void
228 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
229 {
230 static struct uloop_timeout retry = {
231 .cb = netifd_ubus_reconnect_timer,
232 };
233 int t = 2;
234
235 if (ubus_reconnect(ctx, ubus_path) != 0) {
236 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
237 uloop_timeout_set(&retry, t * 1000);
238 return;
239 }
240
241 DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
242 netifd_ubus_add_fd();
243 }
244
245 static void
246 netifd_ubus_connection_lost(struct ubus_context *ctx)
247 {
248 netifd_fd_delete(&ubus_fd);
249 netifd_ubus_reconnect_timer(NULL);
250 }
251
252 int
253 netifd_ubus_init(const char *path)
254 {
255 int ret;
256
257 uloop_init();
258 ubus_path = path;
259
260 ctx = ubus_connect(path);
261 if (!ctx)
262 return -EIO;
263
264 DPRINTF("connected as %08x\n", ctx->local_id);
265 ctx->connection_lost = netifd_ubus_connection_lost;
266 netifd_ubus_add_fd();
267
268 ret = ubus_add_object(ctx, &main_object);
269 if (ret)
270 goto out;
271
272 ret = ubus_add_object(ctx, &dev_object);
273
274 out:
275 if (ret != 0)
276 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
277 return ret;
278 }
279
280 void
281 netifd_ubus_done(void)
282 {
283 ubus_free(ctx);
284 }
285
286
287 /* per-interface object */
288
289 static int
290 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
291 struct ubus_request_data *req, const char *method,
292 struct blob_attr *msg)
293 {
294 struct interface *iface;
295
296 iface = container_of(obj, struct interface, ubus);
297 interface_set_up(iface);
298
299 return 0;
300 }
301
302 static int
303 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
304 struct ubus_request_data *req, const char *method,
305 struct blob_attr *msg)
306 {
307 struct interface *iface;
308
309 iface = container_of(obj, struct interface, ubus);
310 interface_set_down(iface);
311
312 return 0;
313 }
314
315 static void
316 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
317 {
318 struct interface_error *error;
319 void *e, *e2, *e3;
320 int i;
321
322 e = blobmsg_open_array(b, "errors");
323 list_for_each_entry(error, &iface->errors, list) {
324 e2 = blobmsg_open_table(b, NULL);
325
326 blobmsg_add_string(b, "subsystem", error->subsystem);
327 blobmsg_add_string(b, "code", error->code);
328 if (error->data[0]) {
329 e3 = blobmsg_open_array(b, "data");
330 for (i = 0; error->data[i]; i++)
331 blobmsg_add_string(b, NULL, error->data[i]);
332 blobmsg_close_array(b, e3);
333 }
334
335 blobmsg_close_table(b, e2);
336 }
337 blobmsg_close_array(b, e);
338 }
339
340 static void
341 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6)
342 {
343 struct device_addr *addr;
344 char *buf;
345 void *a;
346 int buflen = 128;
347 int af;
348
349 vlist_for_each_element(&ip->addr, addr, node) {
350 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
351 af = AF_INET;
352 else
353 af = AF_INET6;
354
355 if (af != (v6 ? AF_INET6 : AF_INET))
356 continue;
357
358 a = blobmsg_open_table(&b, NULL);
359
360 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
361 inet_ntop(af, &addr->addr, buf, buflen);
362 blobmsg_add_string_buffer(&b);
363
364 blobmsg_add_u32(&b, "mask", addr->mask);
365
366 blobmsg_close_table(&b, a);
367 }
368 }
369
370 static void
371 interface_ip_dump_route_list(struct interface_ip_settings *ip)
372 {
373 struct device_route *route;
374 int buflen = 128;
375 char *buf;
376 void *r;
377 int af;
378
379 vlist_for_each_element(&ip->route, route, node) {
380 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
381 af = AF_INET;
382 else
383 af = AF_INET6;
384
385 r = blobmsg_open_table(&b, NULL);
386
387 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
388 inet_ntop(af, &route->addr, buf, buflen);
389 blobmsg_add_string_buffer(&b);
390
391 blobmsg_add_u32(&b, "mask", route->mask);
392
393 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
394 inet_ntop(af, &route->nexthop, buf, buflen);
395 blobmsg_add_string_buffer(&b);
396
397 blobmsg_close_table(&b, r);
398 }
399 }
400
401 static void
402 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip)
403 {
404 struct dns_server *dns;
405 int buflen = 128;
406 char *buf;
407
408 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
409 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
410 inet_ntop(dns->af, &dns->addr, buf, buflen);
411 blobmsg_add_string_buffer(&b);
412 }
413 }
414
415 static void
416 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip)
417 {
418 struct dns_search_domain *dns;
419
420 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
421 blobmsg_add_string(&b, NULL, dns->name);
422 }
423 }
424
425 static int
426 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
427 struct ubus_request_data *req, const char *method,
428 struct blob_attr *msg)
429 {
430 struct interface *iface;
431 struct interface_data *data;
432 struct device *dev;
433 void *a;
434
435 iface = container_of(obj, struct interface, ubus);
436
437 blob_buf_init(&b, 0);
438 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
439 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
440 blobmsg_add_u8(&b, "available", iface->available);
441 blobmsg_add_u8(&b, "autostart", iface->autostart);
442
443 if (iface->state == IFS_UP) {
444 time_t cur = system_get_rtime();
445 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
446 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
447 }
448
449 if (iface->proto_handler)
450 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
451
452 dev = iface->main_dev.dev;
453 if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
454 blobmsg_add_string(&b, "device", dev->ifname);
455
456 if (iface->state == IFS_UP) {
457 a = blobmsg_open_array(&b, "ipv4-address");
458 interface_ip_dump_address_list(&iface->config_ip, false);
459 interface_ip_dump_address_list(&iface->proto_ip, false);
460 blobmsg_close_array(&b, a);
461 a = blobmsg_open_array(&b, "ipv6-address");
462 interface_ip_dump_address_list(&iface->config_ip, true);
463 interface_ip_dump_address_list(&iface->proto_ip, true);
464 blobmsg_close_array(&b, a);
465 a = blobmsg_open_array(&b, "route");
466 interface_ip_dump_route_list(&iface->config_ip);
467 interface_ip_dump_route_list(&iface->proto_ip);
468 blobmsg_close_array(&b, a);
469 a = blobmsg_open_array(&b, "dns-server");
470 interface_ip_dump_dns_server_list(&iface->config_ip);
471 interface_ip_dump_dns_server_list(&iface->proto_ip);
472 blobmsg_close_array(&b, a);
473 a = blobmsg_open_array(&b, "dns-search");
474 interface_ip_dump_dns_search_list(&iface->config_ip);
475 interface_ip_dump_dns_search_list(&iface->proto_ip);
476 blobmsg_close_array(&b, a);
477 }
478
479 a = blobmsg_open_table(&b, "data");
480 avl_for_each_element(&iface->data, data, node)
481 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
482
483 blobmsg_close_table(&b, a);
484
485 if (!list_is_empty(&iface->errors))
486 netifd_add_interface_errors(&b, iface);
487
488 ubus_send_reply(ctx, req, b.head);
489
490 return 0;
491 }
492
493 static int
494 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
495 struct ubus_request_data *req, const char *method,
496 struct blob_attr *msg)
497 {
498 struct blob_attr *tb[__DEV_MAX];
499 struct interface *iface;
500 struct device *dev;
501 bool add = !strncmp(method, "add", 3);
502 int ret;
503
504 iface = container_of(obj, struct interface, ubus);
505
506 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
507
508 if (!tb[DEV_NAME])
509 return UBUS_STATUS_INVALID_ARGUMENT;
510
511 device_lock();
512
513 dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
514 if (add && !dev)
515 return UBUS_STATUS_NOT_FOUND;
516
517 if (add)
518 return interface_add_link(iface, dev);
519 else
520 return interface_remove_link(iface, dev);
521
522 device_unlock();
523
524 return ret;
525 }
526
527
528 static int
529 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
530 struct ubus_request_data *req, const char *method,
531 struct blob_attr *msg)
532 {
533 struct interface *iface;
534
535 iface = container_of(obj, struct interface, ubus);
536
537 if (!iface->proto || !iface->proto->notify)
538 return UBUS_STATUS_NOT_SUPPORTED;
539
540 return iface->proto->notify(iface->proto, msg);
541 }
542
543 static void
544 netifd_iface_do_remove(struct uloop_timeout *timeout)
545 {
546 struct interface *iface;
547
548 iface = container_of(timeout, struct interface, remove_timer);
549 vlist_delete(&interfaces, &iface->node);
550 }
551
552 static int
553 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
554 struct ubus_request_data *req, const char *method,
555 struct blob_attr *msg)
556 {
557 struct interface *iface;
558
559 iface = container_of(obj, struct interface, ubus);
560 if (iface->remove_timer.cb)
561 return UBUS_STATUS_INVALID_ARGUMENT;
562
563 iface->remove_timer.cb = netifd_iface_do_remove;
564 uloop_timeout_set(&iface->remove_timer, 100);
565 return 0;
566 }
567
568 static int
569 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
570 struct ubus_request_data *req, const char *method,
571 struct blob_attr *msg)
572 {
573 struct interface *iface;
574 struct device *dev;
575 const struct device_hotplug_ops *ops;
576
577 iface = container_of(obj, struct interface, ubus);
578 dev = iface->main_dev.dev;
579 if (!dev)
580 return 0;
581
582 ops = dev->hotplug_ops;
583 if (!ops)
584 return 0;
585
586 return ops->prepare(dev);
587 }
588
589 static int
590 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
591 struct ubus_request_data *req, const char *method,
592 struct blob_attr *msg)
593 {
594 struct interface *iface;
595 struct blob_attr *cur;
596 int rem, ret;
597
598 iface = container_of(obj, struct interface, ubus);
599
600 blob_for_each_attr(cur, msg, rem) {
601 ret = interface_add_data(iface, cur);
602 if (ret)
603 return ret;
604 }
605
606 return 0;
607 }
608
609 static struct ubus_method iface_object_methods[] = {
610 { .name = "up", .handler = netifd_handle_up },
611 { .name = "down", .handler = netifd_handle_down },
612 { .name = "status", .handler = netifd_handle_status },
613 { .name = "prepare", .handler = netifd_handle_iface_prepare },
614 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
615 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
616 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
617 { .name = "remove", .handler = netifd_iface_remove },
618 { .name = "set_data", .handler = netifd_handle_set_data },
619 };
620
621 static struct ubus_object_type iface_object_type =
622 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
623
624
625 void
626 netifd_ubus_interface_event(struct interface *iface, bool up)
627 {
628 blob_buf_init(&b, 0);
629 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
630 blobmsg_add_string(&b, "interface", iface->name);
631 ubus_send_event(ctx, "network.interface", b.head);
632 }
633
634 void
635 netifd_ubus_add_interface(struct interface *iface)
636 {
637 struct ubus_object *obj = &iface->ubus;
638 char *name = NULL;
639
640 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
641 if (!name)
642 return;
643
644 obj->name = name;
645 obj->type = &iface_object_type;
646 obj->methods = iface_object_methods;
647 obj->n_methods = ARRAY_SIZE(iface_object_methods);
648 if (ubus_add_object(ctx, &iface->ubus)) {
649 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
650 free(name);
651 obj->name = NULL;
652 }
653 }
654
655 void
656 netifd_ubus_remove_interface(struct interface *iface)
657 {
658 if (!iface->ubus.name)
659 return;
660
661 ubus_remove_object(ctx, &iface->ubus);
662 free((void *) iface->ubus.name);
663 }