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