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