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