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