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