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