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