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