ubus: display the point-to-point IPv4 address
[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 #include "wireless.h"
26
27 struct ubus_context *ubus_ctx = NULL;
28 static struct blob_buf b;
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 if (netifd_reload())
48 return UBUS_STATUS_NOT_FOUND;
49
50 return UBUS_STATUS_OK;
51 }
52
53 enum {
54 HR_TARGET,
55 HR_V6,
56 HR_INTERFACE,
57 __HR_MAX
58 };
59
60 static const struct blobmsg_policy route_policy[__HR_MAX] = {
61 [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
62 [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
63 [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
64 };
65
66 static int
67 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
68 struct ubus_request_data *req, const char *method,
69 struct blob_attr *msg)
70 {
71 struct blob_attr *tb[__HR_MAX];
72 struct interface *iface = NULL;
73 union if_addr a;
74 bool v6 = false;
75
76 blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
77 if (!tb[HR_TARGET])
78 return UBUS_STATUS_INVALID_ARGUMENT;
79
80 if (tb[HR_V6])
81 v6 = blobmsg_get_bool(tb[HR_V6]);
82
83 if (tb[HR_INTERFACE])
84 iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
85
86 memset(&a, 0, sizeof(a));
87 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
88 return UBUS_STATUS_INVALID_ARGUMENT;
89
90
91 iface = interface_ip_add_target_route(&a, v6, iface);
92 if (!iface)
93 return UBUS_STATUS_NOT_FOUND;
94
95 blob_buf_init(&b, 0);
96 blobmsg_add_string(&b, "interface", iface->name);
97 ubus_send_reply(ctx, req, b.head);
98
99 return 0;
100 }
101
102 static int
103 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
104 struct ubus_request_data *req, const char *method,
105 struct blob_attr *msg)
106 {
107 blob_buf_init(&b, 0);
108 proto_dump_handlers(&b);
109 ubus_send_reply(ctx, req, b.head);
110
111 return 0;
112 }
113
114
115 enum {
116 DI_NAME,
117 __DI_MAX
118 };
119
120 static const struct blobmsg_policy dynamic_policy[__DI_MAX] = {
121 [DI_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
122 };
123
124 static int
125 netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
126 struct ubus_request_data *req, const char *method,
127 struct blob_attr *msg)
128 {
129 struct blob_attr *tb[__DI_MAX];
130 struct interface *iface;
131 struct blob_attr *config;
132
133 blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
134
135 if (!tb[DI_NAME])
136 return UBUS_STATUS_INVALID_ARGUMENT;
137
138 const char *name = blobmsg_get_string(tb[DI_NAME]);
139
140 iface = interface_alloc(name, msg);
141 if (!iface)
142 return UBUS_STATUS_UNKNOWN_ERROR;
143
144 config = blob_memdup(msg);
145 if (!config)
146 goto error;
147
148 interface_add(iface, config);
149
150 // need to look up the interface name again, in case of config update
151 // the pointer will have changed
152 iface = vlist_find(&interfaces, name, iface, node);
153 if (!iface)
154 return UBUS_STATUS_UNKNOWN_ERROR;
155
156 // Set interface as dynamic
157 interface_set_dynamic(iface);
158
159 return UBUS_STATUS_OK;
160
161 error:
162 free(iface);
163 return UBUS_STATUS_UNKNOWN_ERROR;
164 }
165
166 static struct ubus_method main_object_methods[] = {
167 { .name = "restart", .handler = netifd_handle_restart },
168 { .name = "reload", .handler = netifd_handle_reload },
169 UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
170 { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
171 UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy),
172 };
173
174 static struct ubus_object_type main_object_type =
175 UBUS_OBJECT_TYPE("netifd", main_object_methods);
176
177 static struct ubus_object main_object = {
178 .name = "network",
179 .type = &main_object_type,
180 .methods = main_object_methods,
181 .n_methods = ARRAY_SIZE(main_object_methods),
182 };
183
184 enum {
185 DEV_NAME,
186 __DEV_MAX,
187 };
188
189 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
190 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
191 };
192
193 static int
194 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
195 struct ubus_request_data *req, const char *method,
196 struct blob_attr *msg)
197 {
198 struct device *dev = NULL;
199 struct blob_attr *tb[__DEV_MAX];
200
201 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
202
203 if (tb[DEV_NAME]) {
204 dev = device_find(blobmsg_data(tb[DEV_NAME]));
205 if (!dev)
206 return UBUS_STATUS_INVALID_ARGUMENT;
207 }
208
209 blob_buf_init(&b, 0);
210 device_dump_status(&b, dev);
211 ubus_send_reply(ctx, req, b.head);
212
213 return 0;
214 }
215
216 enum {
217 ALIAS_ATTR_ALIAS,
218 ALIAS_ATTR_DEV,
219 __ALIAS_ATTR_MAX,
220 };
221
222 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
223 [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
224 [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
225 };
226
227 static int
228 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
229 struct ubus_request_data *req, const char *method,
230 struct blob_attr *msg)
231 {
232 struct device *dev = NULL;
233 struct blob_attr *tb[__ALIAS_ATTR_MAX];
234 struct blob_attr *cur;
235 int rem;
236
237 blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
238
239 if (!tb[ALIAS_ATTR_ALIAS])
240 return UBUS_STATUS_INVALID_ARGUMENT;
241
242 if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
243 dev = device_get(blobmsg_data(cur), true);
244 if (!dev)
245 return UBUS_STATUS_NOT_FOUND;
246 }
247
248 blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
249 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
250 goto error;
251
252 if (!blobmsg_check_attr(cur, NULL))
253 goto error;
254
255 alias_notify_device(blobmsg_data(cur), dev);
256 }
257 return 0;
258
259 error:
260 device_free_unused(dev);
261 return UBUS_STATUS_INVALID_ARGUMENT;
262 }
263
264 enum {
265 DEV_STATE_NAME,
266 DEV_STATE_DEFER,
267 __DEV_STATE_MAX,
268 };
269
270 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
271 [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
272 [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
273 };
274
275 static int
276 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
277 struct ubus_request_data *req, const char *method,
278 struct blob_attr *msg)
279 {
280 struct device *dev = NULL;
281 struct blob_attr *tb[__DEV_STATE_MAX];
282 struct blob_attr *cur;
283
284 blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
285
286 cur = tb[DEV_STATE_NAME];
287 if (!cur)
288 return UBUS_STATUS_INVALID_ARGUMENT;
289
290 dev = device_find(blobmsg_data(cur));
291 if (!dev)
292 return UBUS_STATUS_NOT_FOUND;
293
294 cur = tb[DEV_STATE_DEFER];
295 if (cur)
296 device_set_deferred(dev, !!blobmsg_get_u8(cur));
297
298 return 0;
299 }
300
301 static struct ubus_method dev_object_methods[] = {
302 UBUS_METHOD("status", netifd_dev_status, dev_policy),
303 UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
304 UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
305 };
306
307 static struct ubus_object_type dev_object_type =
308 UBUS_OBJECT_TYPE("device", dev_object_methods);
309
310 static struct ubus_object dev_object = {
311 .name = "network.device",
312 .type = &dev_object_type,
313 .methods = dev_object_methods,
314 .n_methods = ARRAY_SIZE(dev_object_methods),
315 };
316
317 static void
318 netifd_ubus_add_fd(void)
319 {
320 ubus_add_uloop(ubus_ctx);
321 system_fd_set_cloexec(ubus_ctx->sock.fd);
322 }
323
324 static void
325 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
326 {
327 static struct uloop_timeout retry = {
328 .cb = netifd_ubus_reconnect_timer,
329 };
330 int t = 2;
331
332 if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
333 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
334 uloop_timeout_set(&retry, t * 1000);
335 return;
336 }
337
338 DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
339 netifd_ubus_add_fd();
340 }
341
342 static void
343 netifd_ubus_connection_lost(struct ubus_context *ctx)
344 {
345 netifd_ubus_reconnect_timer(NULL);
346 }
347
348 /* per-interface object */
349
350 static int
351 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
352 struct ubus_request_data *req, const char *method,
353 struct blob_attr *msg)
354 {
355 struct interface *iface;
356
357 iface = container_of(obj, struct interface, ubus);
358 interface_set_up(iface);
359
360 return 0;
361 }
362
363 static int
364 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
365 struct ubus_request_data *req, const char *method,
366 struct blob_attr *msg)
367 {
368 struct interface *iface;
369
370 iface = container_of(obj, struct interface, ubus);
371 interface_set_down(iface);
372
373 return 0;
374 }
375
376 static int
377 netifd_handle_renew(struct ubus_context *ctx, struct ubus_object *obj,
378 struct ubus_request_data *req, const char *method,
379 struct blob_attr *msg)
380 {
381 struct interface *iface;
382
383 iface = container_of(obj, struct interface, ubus);
384 interface_renew(iface);
385
386 return 0;
387 }
388
389 static void
390 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
391 {
392 struct interface_error *error;
393 void *e, *e2, *e3;
394 int i;
395
396 e = blobmsg_open_array(b, "errors");
397 list_for_each_entry(error, &iface->errors, list) {
398 e2 = blobmsg_open_table(b, NULL);
399
400 blobmsg_add_string(b, "subsystem", error->subsystem);
401 blobmsg_add_string(b, "code", error->code);
402 if (error->data[0]) {
403 e3 = blobmsg_open_array(b, "data");
404 for (i = 0; error->data[i]; i++)
405 blobmsg_add_string(b, NULL, error->data[i]);
406 blobmsg_close_array(b, e3);
407 }
408
409 blobmsg_close_table(b, e2);
410 }
411 blobmsg_close_array(b, e);
412 }
413
414 static void
415 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6, bool enabled)
416 {
417 struct device_addr *addr;
418 char *buf;
419 void *a;
420 int buflen = 128;
421 int af;
422
423 time_t now = system_get_rtime();
424 vlist_for_each_element(&ip->addr, addr, node) {
425 if (addr->enabled != enabled)
426 continue;
427
428 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
429 af = AF_INET;
430 else
431 af = AF_INET6;
432
433 if (af != (v6 ? AF_INET6 : AF_INET))
434 continue;
435
436 a = blobmsg_open_table(&b, NULL);
437
438 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
439 inet_ntop(af, &addr->addr, buf, buflen);
440 blobmsg_add_string_buffer(&b);
441
442 blobmsg_add_u32(&b, "mask", addr->mask);
443
444 if (addr->point_to_point) {
445 buf = blobmsg_alloc_string_buffer(&b, "ptpaddress", buflen);
446 inet_ntop(af, &addr->point_to_point, buf, buflen);
447 blobmsg_add_string_buffer(&b);
448 }
449
450 if (addr->preferred_until) {
451 int preferred = addr->preferred_until - now;
452 if (preferred < 0)
453 preferred = 0;
454 blobmsg_add_u32(&b, "preferred", preferred);
455 }
456
457 if (addr->valid_until)
458 blobmsg_add_u32(&b, "valid", addr->valid_until - now);
459
460 if (addr->pclass)
461 blobmsg_add_string(&b, "class", addr->pclass);
462
463 blobmsg_close_table(&b, a);
464 }
465 }
466
467 static void
468 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
469 {
470 struct device_route *route;
471 int buflen = 128;
472 char *buf;
473 void *r;
474 int af;
475
476 time_t now = system_get_rtime();
477 vlist_for_each_element(&ip->route, route, node) {
478 if (route->enabled != enabled)
479 continue;
480
481 if ((ip->no_defaultroute == enabled) && !route->mask)
482 continue;
483
484 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
485 af = AF_INET;
486 else
487 af = AF_INET6;
488
489 r = blobmsg_open_table(&b, NULL);
490
491 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
492 inet_ntop(af, &route->addr, buf, buflen);
493 blobmsg_add_string_buffer(&b);
494
495 blobmsg_add_u32(&b, "mask", route->mask);
496
497 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
498 inet_ntop(af, &route->nexthop, buf, buflen);
499 blobmsg_add_string_buffer(&b);
500
501 if (route->flags & DEVROUTE_TYPE)
502 blobmsg_add_u32(&b, "type", route->type);
503
504 if (route->flags & DEVROUTE_PROTO)
505 blobmsg_add_u32(&b, "proto", route->proto);
506
507 if (route->flags & DEVROUTE_MTU)
508 blobmsg_add_u32(&b, "mtu", route->mtu);
509
510 if (route->flags & DEVROUTE_METRIC)
511 blobmsg_add_u32(&b, "metric", route->metric);
512
513 if (route->flags & DEVROUTE_TABLE)
514 blobmsg_add_u32(&b, "table", route->table);
515
516 if (route->valid_until)
517 blobmsg_add_u32(&b, "valid", route->valid_until - now);
518
519 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
520 inet_ntop(af, &route->source, buf, buflen);
521 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
522 blobmsg_add_string_buffer(&b);
523
524 blobmsg_close_table(&b, r);
525 }
526 }
527
528
529 static void
530 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
531 {
532 struct device_prefix *prefix;
533 char *buf;
534 void *a, *c;
535 const int buflen = INET6_ADDRSTRLEN;
536
537 time_t now = system_get_rtime();
538 vlist_for_each_element(&ip->prefix, prefix, node) {
539 a = blobmsg_open_table(&b, NULL);
540
541 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
542 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
543 blobmsg_add_string_buffer(&b);
544
545 blobmsg_add_u32(&b, "mask", prefix->length);
546
547 if (prefix->preferred_until) {
548 int preferred = prefix->preferred_until - now;
549 if (preferred < 0)
550 preferred = 0;
551 blobmsg_add_u32(&b, "preferred", preferred);
552 }
553
554 if (prefix->valid_until)
555 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
556
557 blobmsg_add_string(&b, "class", prefix->pclass);
558
559 c = blobmsg_open_table(&b, "assigned");
560 struct device_prefix_assignment *assign;
561 list_for_each_entry(assign, &prefix->assignments, head) {
562 if (!assign->name[0])
563 continue;
564
565 struct in6_addr addr = prefix->addr;
566 addr.s6_addr32[1] |= htonl(assign->assigned);
567
568 void *d = blobmsg_open_table(&b, assign->name);
569
570 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
571 inet_ntop(AF_INET6, &addr, buf, buflen);
572 blobmsg_add_string_buffer(&b);
573
574 blobmsg_add_u32(&b, "mask", assign->length);
575
576 blobmsg_close_table(&b, d);
577 }
578 blobmsg_close_table(&b, c);
579
580 blobmsg_close_table(&b, a);
581 }
582 }
583
584
585 static void
586 interface_ip_dump_prefix_assignment_list(struct interface *iface)
587 {
588 void *a;
589 char *buf;
590 const int buflen = INET6_ADDRSTRLEN;
591 time_t now = system_get_rtime();
592
593 struct device_prefix *prefix;
594 list_for_each_entry(prefix, &prefixes, head) {
595 struct device_prefix_assignment *assign;
596 list_for_each_entry(assign, &prefix->assignments, head) {
597 if (strcmp(assign->name, iface->name))
598 continue;
599
600 struct in6_addr addr = prefix->addr;
601 addr.s6_addr32[1] |= htonl(assign->assigned);
602
603 a = blobmsg_open_table(&b, NULL);
604
605 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
606 inet_ntop(AF_INET6, &addr, buf, buflen);
607 blobmsg_add_string_buffer(&b);
608
609 blobmsg_add_u32(&b, "mask", assign->length);
610
611 if (prefix->preferred_until) {
612 int preferred = prefix->preferred_until - now;
613 if (preferred < 0)
614 preferred = 0;
615 blobmsg_add_u32(&b, "preferred", preferred);
616 }
617
618 if (prefix->valid_until)
619 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
620
621 void *c = blobmsg_open_table(&b, "local-address");
622 if (assign->enabled) {
623 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
624 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
625 blobmsg_add_string_buffer(&b);
626
627 blobmsg_add_u32(&b, "mask", assign->length < 64 ? 64 : assign->length);
628 }
629 blobmsg_close_table(&b, c);
630
631 blobmsg_close_table(&b, a);
632 }
633 }
634 }
635
636 static void
637 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip, bool enabled)
638 {
639 struct dns_server *dns;
640 int buflen = 128;
641 char *buf;
642
643 vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
644 if (ip->no_dns == enabled)
645 continue;
646
647 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
648 inet_ntop(dns->af, &dns->addr, buf, buflen);
649 blobmsg_add_string_buffer(&b);
650 }
651 }
652
653 static void
654 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip, bool enabled)
655 {
656 struct dns_search_domain *dns;
657
658 vlist_simple_for_each_element(&ip->dns_search, dns, node) {
659 if (ip->no_dns == enabled)
660 continue;
661
662 blobmsg_add_string(&b, NULL, dns->name);
663 }
664 }
665
666 static void
667 netifd_dump_status(struct interface *iface)
668 {
669 struct interface_data *data;
670 struct device *dev;
671 void *a, *inactive;
672
673 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
674 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
675 blobmsg_add_u8(&b, "available", iface->available);
676 blobmsg_add_u8(&b, "autostart", iface->autostart);
677 blobmsg_add_u8(&b, "dynamic", iface->dynamic);
678
679 if (iface->state == IFS_UP) {
680 time_t cur = system_get_rtime();
681 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
682 if (iface->l3_dev.dev)
683 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
684 }
685
686 if (iface->proto_handler)
687 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
688
689 dev = iface->main_dev.dev;
690 if (dev && !dev->hidden && iface->proto_handler &&
691 !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
692 blobmsg_add_string(&b, "device", dev->ifname);
693
694 if (iface->state == IFS_UP) {
695 if (iface->updated) {
696 a = blobmsg_open_array(&b, "updated");
697
698 if (iface->updated & IUF_ADDRESS)
699 blobmsg_add_string(&b, NULL, "addresses");
700 if (iface->updated & IUF_ROUTE)
701 blobmsg_add_string(&b, NULL, "routes");
702 if (iface->updated & IUF_PREFIX)
703 blobmsg_add_string(&b, NULL, "prefixes");
704 if (iface->updated & IUF_DATA)
705 blobmsg_add_string(&b, NULL, "data");
706
707 blobmsg_close_array(&b, a);
708 }
709
710 if (iface->ip4table)
711 blobmsg_add_u32(&b, "ip4table", iface->ip4table);
712 if (iface->ip6table)
713 blobmsg_add_u32(&b, "ip6table", iface->ip6table);
714 blobmsg_add_u32(&b, "metric", iface->metric);
715 blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
716 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
717 if (iface->assignment_weight)
718 blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
719 a = blobmsg_open_array(&b, "ipv4-address");
720 interface_ip_dump_address_list(&iface->config_ip, false, true);
721 interface_ip_dump_address_list(&iface->proto_ip, false, true);
722 blobmsg_close_array(&b, a);
723 a = blobmsg_open_array(&b, "ipv6-address");
724 interface_ip_dump_address_list(&iface->config_ip, true, true);
725 interface_ip_dump_address_list(&iface->proto_ip, true, true);
726 blobmsg_close_array(&b, a);
727 a = blobmsg_open_array(&b, "ipv6-prefix");
728 interface_ip_dump_prefix_list(&iface->config_ip);
729 interface_ip_dump_prefix_list(&iface->proto_ip);
730 blobmsg_close_array(&b, a);
731 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
732 interface_ip_dump_prefix_assignment_list(iface);
733 blobmsg_close_array(&b, a);
734 a = blobmsg_open_array(&b, "route");
735 interface_ip_dump_route_list(&iface->config_ip, true);
736 interface_ip_dump_route_list(&iface->proto_ip, true);
737 blobmsg_close_array(&b, a);
738 a = blobmsg_open_array(&b, "dns-server");
739 interface_ip_dump_dns_server_list(&iface->config_ip, true);
740 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
741 blobmsg_close_array(&b, a);
742 a = blobmsg_open_array(&b, "dns-search");
743 interface_ip_dump_dns_search_list(&iface->config_ip, true);
744 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
745 blobmsg_close_array(&b, a);
746
747 inactive = blobmsg_open_table(&b, "inactive");
748 a = blobmsg_open_array(&b, "ipv4-address");
749 interface_ip_dump_address_list(&iface->config_ip, false, false);
750 interface_ip_dump_address_list(&iface->proto_ip, false, false);
751 blobmsg_close_array(&b, a);
752 a = blobmsg_open_array(&b, "ipv6-address");
753 interface_ip_dump_address_list(&iface->config_ip, true, false);
754 interface_ip_dump_address_list(&iface->proto_ip, true, false);
755 blobmsg_close_array(&b, a);
756 a = blobmsg_open_array(&b, "route");
757 interface_ip_dump_route_list(&iface->config_ip, false);
758 interface_ip_dump_route_list(&iface->proto_ip, false);
759 blobmsg_close_array(&b, a);
760 a = blobmsg_open_array(&b, "dns-server");
761 interface_ip_dump_dns_server_list(&iface->config_ip, false);
762 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
763 blobmsg_close_array(&b, a);
764 a = blobmsg_open_array(&b, "dns-search");
765 interface_ip_dump_dns_search_list(&iface->config_ip, false);
766 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
767 blobmsg_close_array(&b, a);
768 blobmsg_close_table(&b, inactive);
769 }
770
771 a = blobmsg_open_table(&b, "data");
772 avl_for_each_element(&iface->data, data, node)
773 blobmsg_add_blob(&b, data->data);
774
775 blobmsg_close_table(&b, a);
776
777 if (!list_empty(&iface->errors))
778 netifd_add_interface_errors(&b, iface);
779 }
780
781 static int
782 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
783 struct ubus_request_data *req, const char *method,
784 struct blob_attr *msg)
785 {
786 struct interface *iface = container_of(obj, struct interface, ubus);
787
788 blob_buf_init(&b, 0);
789 netifd_dump_status(iface);
790 ubus_send_reply(ctx, req, b.head);
791
792 return 0;
793 }
794
795
796 static int
797 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
798 struct ubus_request_data *req, const char *method,
799 struct blob_attr *msg)
800 {
801 blob_buf_init(&b, 0);
802 void *a = blobmsg_open_array(&b, "interface");
803
804 struct interface *iface;
805 vlist_for_each_element(&interfaces, iface, node) {
806 void *i = blobmsg_open_table(&b, NULL);
807 blobmsg_add_string(&b, "interface", iface->name);
808 netifd_dump_status(iface);
809 blobmsg_close_table(&b, i);
810 }
811
812 blobmsg_close_array(&b, a);
813 ubus_send_reply(ctx, req, b.head);
814
815 return 0;
816 }
817
818 enum {
819 DEV_LINK_NAME,
820 DEV_LINK_EXT,
821 __DEV_LINK_MAX,
822 };
823
824 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
825 [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
826 [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
827 };
828
829 static int
830 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
831 struct ubus_request_data *req, const char *method,
832 struct blob_attr *msg)
833 {
834 struct blob_attr *tb[__DEV_LINK_MAX];
835 struct blob_attr *cur;
836 struct interface *iface;
837 bool add = !strncmp(method, "add", 3);
838 bool link_ext = true;
839
840 iface = container_of(obj, struct interface, ubus);
841
842 blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
843
844 if (!tb[DEV_LINK_NAME])
845 return UBUS_STATUS_INVALID_ARGUMENT;
846
847 cur = tb[DEV_LINK_EXT];
848 if (cur)
849 link_ext = blobmsg_get_bool(cur);
850
851 return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
852 }
853
854
855 static int
856 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
857 struct ubus_request_data *req, const char *method,
858 struct blob_attr *msg)
859 {
860 struct interface *iface;
861
862 iface = container_of(obj, struct interface, ubus);
863
864 if (!iface->proto || !iface->proto->notify)
865 return UBUS_STATUS_NOT_SUPPORTED;
866
867 return iface->proto->notify(iface->proto, msg);
868 }
869
870 static void
871 netifd_iface_do_remove(struct uloop_timeout *timeout)
872 {
873 struct interface *iface;
874
875 iface = container_of(timeout, struct interface, remove_timer);
876 vlist_delete(&interfaces, &iface->node);
877 }
878
879 static int
880 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
881 struct ubus_request_data *req, const char *method,
882 struct blob_attr *msg)
883 {
884 struct interface *iface;
885
886 iface = container_of(obj, struct interface, ubus);
887 if (iface->remove_timer.cb)
888 return UBUS_STATUS_INVALID_ARGUMENT;
889
890 iface->remove_timer.cb = netifd_iface_do_remove;
891 uloop_timeout_set(&iface->remove_timer, 100);
892 return 0;
893 }
894
895 static int
896 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
897 struct ubus_request_data *req, const char *method,
898 struct blob_attr *msg)
899 {
900 struct interface *iface;
901 struct device *dev;
902 const struct device_hotplug_ops *ops;
903
904 iface = container_of(obj, struct interface, ubus);
905 dev = iface->main_dev.dev;
906 if (!dev)
907 return 0;
908
909 ops = dev->hotplug_ops;
910 if (!ops)
911 return 0;
912
913 return ops->prepare(dev);
914 }
915
916 static int
917 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
918 struct ubus_request_data *req, const char *method,
919 struct blob_attr *msg)
920 {
921 struct interface *iface;
922
923 iface = container_of(obj, struct interface, ubus);
924
925 return interface_parse_data(iface, msg);
926 }
927
928 static struct ubus_method iface_object_methods[] = {
929 { .name = "up", .handler = netifd_handle_up },
930 { .name = "down", .handler = netifd_handle_down },
931 { .name = "renew", .handler = netifd_handle_renew },
932 { .name = "status", .handler = netifd_handle_status },
933 { .name = "prepare", .handler = netifd_handle_iface_prepare },
934 { .name = "dump", .handler = netifd_handle_dump },
935 UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
936 UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
937 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
938 { .name = "remove", .handler = netifd_iface_remove },
939 { .name = "set_data", .handler = netifd_handle_set_data },
940 };
941
942 static struct ubus_object_type iface_object_type =
943 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
944
945
946 static struct ubus_object iface_object = {
947 .name = "network.interface",
948 .type = &iface_object_type,
949 .n_methods = ARRAY_SIZE(iface_object_methods),
950 };
951
952 static void netifd_add_object(struct ubus_object *obj)
953 {
954 int ret = ubus_add_object(ubus_ctx, obj);
955
956 if (ret != 0)
957 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
958 }
959
960 static const struct blobmsg_policy iface_policy = {
961 .name = "interface",
962 .type = BLOBMSG_TYPE_STRING,
963 };
964
965 static int
966 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
967 struct ubus_request_data *req, const char *method,
968 struct blob_attr *msg)
969 {
970 struct interface *iface;
971 struct blob_attr *tb;
972 int i;
973
974 blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
975 if (!tb)
976 return UBUS_STATUS_INVALID_ARGUMENT;
977
978 iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
979 if (!iface)
980 return UBUS_STATUS_NOT_FOUND;
981
982 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
983 ubus_handler_t cb;
984
985 if (strcmp(method, iface_object_methods[i].name) != 0)
986 continue;
987
988 cb = iface_object_methods[i].handler;
989 return cb(ctx, &iface->ubus, req, method, msg);
990 }
991
992 return UBUS_STATUS_INVALID_ARGUMENT;
993 }
994
995 static void netifd_add_iface_object(void)
996 {
997 struct ubus_method *methods;
998 int i;
999
1000 methods = calloc(1, sizeof(iface_object_methods));
1001 if (!methods)
1002 return;
1003
1004 memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
1005 iface_object.methods = methods;
1006
1007 for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
1008 if (methods[i].handler == netifd_handle_dump)
1009 continue;
1010
1011 methods[i].handler = netifd_handle_iface;
1012 methods[i].policy = &iface_policy;
1013 methods[i].n_policy = 1;
1014 }
1015 netifd_add_object(&iface_object);
1016 }
1017
1018 static struct wireless_device *
1019 get_wdev(struct blob_attr *msg, int *ret)
1020 {
1021 struct blobmsg_policy wdev_policy = {
1022 .name = "device",
1023 .type = BLOBMSG_TYPE_STRING,
1024 };
1025 struct blob_attr *dev_attr;
1026 struct wireless_device *wdev = NULL;
1027
1028
1029 blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1030 if (!dev_attr) {
1031 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1032 return NULL;
1033 }
1034
1035 wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1036 if (!wdev) {
1037 *ret = UBUS_STATUS_NOT_FOUND;
1038 return NULL;
1039 }
1040
1041 *ret = 0;
1042 return wdev;
1043 }
1044
1045 static int
1046 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1047 struct ubus_request_data *req, const char *method,
1048 struct blob_attr *msg)
1049 {
1050 struct wireless_device *wdev;
1051 int ret;
1052
1053 wdev = get_wdev(msg, &ret);
1054 if (ret == UBUS_STATUS_NOT_FOUND)
1055 return ret;
1056
1057 if (wdev) {
1058 wireless_device_set_up(wdev);
1059 } else {
1060 vlist_for_each_element(&wireless_devices, wdev, node)
1061 wireless_device_set_up(wdev);
1062 }
1063
1064 return 0;
1065 }
1066
1067 static int
1068 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1069 struct ubus_request_data *req, const char *method,
1070 struct blob_attr *msg)
1071 {
1072 struct wireless_device *wdev;
1073 int ret;
1074
1075 wdev = get_wdev(msg, &ret);
1076 if (ret == UBUS_STATUS_NOT_FOUND)
1077 return ret;
1078
1079 if (wdev) {
1080 wireless_device_set_down(wdev);
1081 } else {
1082 vlist_for_each_element(&wireless_devices, wdev, node)
1083 wireless_device_set_down(wdev);
1084 }
1085
1086 return 0;
1087 }
1088
1089 static int
1090 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1091 struct ubus_request_data *req, const char *method,
1092 struct blob_attr *msg)
1093 {
1094 struct wireless_device *wdev;
1095 int ret;
1096
1097 wdev = get_wdev(msg, &ret);
1098 if (ret == UBUS_STATUS_NOT_FOUND)
1099 return ret;
1100
1101 blob_buf_init(&b, 0);
1102 if (wdev) {
1103 wireless_device_status(wdev, &b);
1104 } else {
1105 vlist_for_each_element(&wireless_devices, wdev, node)
1106 wireless_device_status(wdev, &b);
1107 }
1108 ubus_send_reply(ctx, req, b.head);
1109 return 0;
1110 }
1111
1112 static int
1113 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1114 struct ubus_request_data *req, const char *method,
1115 struct blob_attr *msg)
1116 {
1117 struct wireless_device *wdev;
1118 int ret;
1119
1120 wdev = get_wdev(msg, &ret);
1121 if (ret == UBUS_STATUS_NOT_FOUND)
1122 return ret;
1123
1124 blob_buf_init(&b, 0);
1125 if (wdev) {
1126 wireless_device_get_validate(wdev, &b);
1127 } else {
1128 vlist_for_each_element(&wireless_devices, wdev, node)
1129 wireless_device_get_validate(wdev, &b);
1130 }
1131 ubus_send_reply(ctx, req, b.head);
1132 return 0;
1133 }
1134
1135 static int
1136 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1137 struct ubus_request_data *req, const char *method,
1138 struct blob_attr *msg)
1139 {
1140 struct wireless_device *wdev;
1141 int ret;
1142
1143 wdev = get_wdev(msg, &ret);
1144 if (!wdev)
1145 return ret;
1146
1147 return wireless_device_notify(wdev, msg, req);
1148 }
1149
1150 static struct ubus_method wireless_object_methods[] = {
1151 { .name = "up", .handler = netifd_handle_wdev_up },
1152 { .name = "down", .handler = netifd_handle_wdev_down },
1153 { .name = "status", .handler = netifd_handle_wdev_status },
1154 { .name = "notify", .handler = netifd_handle_wdev_notify },
1155 { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1156 };
1157
1158 static struct ubus_object_type wireless_object_type =
1159 UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1160
1161
1162 static struct ubus_object wireless_object = {
1163 .name = "network.wireless",
1164 .type = &wireless_object_type,
1165 .methods = wireless_object_methods,
1166 .n_methods = ARRAY_SIZE(wireless_object_methods),
1167 };
1168
1169 int
1170 netifd_ubus_init(const char *path)
1171 {
1172 uloop_init();
1173 ubus_path = path;
1174
1175 ubus_ctx = ubus_connect(path);
1176 if (!ubus_ctx)
1177 return -EIO;
1178
1179 DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1180 ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1181 netifd_ubus_add_fd();
1182
1183 netifd_add_object(&main_object);
1184 netifd_add_object(&dev_object);
1185 netifd_add_object(&wireless_object);
1186 netifd_add_iface_object();
1187
1188 return 0;
1189 }
1190
1191 void
1192 netifd_ubus_done(void)
1193 {
1194 ubus_free(ubus_ctx);
1195 }
1196
1197 void
1198 netifd_ubus_interface_event(struct interface *iface, bool up)
1199 {
1200 blob_buf_init(&b, 0);
1201 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1202 blobmsg_add_string(&b, "interface", iface->name);
1203 ubus_send_event(ubus_ctx, "network.interface", b.head);
1204 }
1205
1206 void
1207 netifd_ubus_interface_notify(struct interface *iface, bool up)
1208 {
1209 const char *event = (up) ? "interface.update" : "interface.down";
1210 blob_buf_init(&b, 0);
1211 blobmsg_add_string(&b, "interface", iface->name);
1212 netifd_dump_status(iface);
1213 ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1214 ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1215 }
1216
1217 void
1218 netifd_ubus_add_interface(struct interface *iface)
1219 {
1220 struct ubus_object *obj = &iface->ubus;
1221 char *name = NULL;
1222
1223 if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1224 return;
1225
1226 obj->name = name;
1227 obj->type = &iface_object_type;
1228 obj->methods = iface_object_methods;
1229 obj->n_methods = ARRAY_SIZE(iface_object_methods);
1230 if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1231 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1232 free(name);
1233 obj->name = NULL;
1234 }
1235 }
1236
1237 void
1238 netifd_ubus_remove_interface(struct interface *iface)
1239 {
1240 if (!iface->ubus.name)
1241 return;
1242
1243 ubus_remove_object(ubus_ctx, &iface->ubus);
1244 free((void *) iface->ubus.name);
1245 }