cfdfcf3ae967e4b7eee97001c5a772d4ffd89bad
[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
26 static struct ubus_context *ctx = NULL;
27 static struct blob_buf b;
28 static const char *ubus_path;
29
30 /* global object */
31
32 static int
33 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
34                       struct ubus_request_data *req, const char *method,
35                       struct blob_attr *msg)
36 {
37         netifd_restart();
38         return 0;
39 }
40
41 static int
42 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
43                      struct ubus_request_data *req, const char *method,
44                      struct blob_attr *msg)
45 {
46         netifd_reload();
47         return 0;
48 }
49
50 enum {
51         HR_TARGET,
52         HR_V6,
53         HR_INTERFACE,
54         __HR_MAX
55 };
56
57 static const struct blobmsg_policy route_policy[__HR_MAX] = {
58         [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
59         [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
60         [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
61 };
62
63 static int
64 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
65                       struct ubus_request_data *req, const char *method,
66                       struct blob_attr *msg)
67 {
68         struct blob_attr *tb[__HR_MAX];
69         struct interface *iface = NULL;
70         union if_addr a;
71         bool v6 = false;
72
73         blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
74         if (!tb[HR_TARGET])
75                 return UBUS_STATUS_INVALID_ARGUMENT;
76
77         if (tb[HR_V6])
78                 v6 = blobmsg_get_bool(tb[HR_V6]);
79
80         if (tb[HR_INTERFACE])
81                 iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
82
83         memset(&a, 0, sizeof(a));
84         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
85                 return UBUS_STATUS_INVALID_ARGUMENT;
86
87
88         iface = interface_ip_add_target_route(&a, v6, iface);
89         if (!iface)
90                 return UBUS_STATUS_NOT_FOUND;
91
92         blob_buf_init(&b, 0);
93         blobmsg_add_string(&b, "interface", iface->name);
94         ubus_send_reply(ctx, req, b.head);
95
96         return 0;
97 }
98
99 static int
100 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
101                           struct ubus_request_data *req, const char *method,
102                           struct blob_attr *msg)
103 {
104         blob_buf_init(&b, 0);
105         proto_dump_handlers(&b);
106         ubus_send_reply(ctx, req, b.head);
107
108         return 0;
109 }
110
111
112 enum {
113         DI_NAME,
114         __DI_MAX
115 };
116
117 static const struct blobmsg_policy dynamic_policy[__DI_MAX] = {
118         [DI_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
119 };
120
121 static int
122 netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj,
123                       struct ubus_request_data *req, const char *method,
124                       struct blob_attr *msg)
125 {
126         struct blob_attr *tb[__DI_MAX];
127         struct interface *iface;
128         struct blob_attr *config;
129         struct device *dev;
130
131         blobmsg_parse(dynamic_policy, __DI_MAX, tb, blob_data(msg), blob_len(msg));
132
133         if (!tb[DI_NAME])
134                 return UBUS_STATUS_INVALID_ARGUMENT;
135
136         const char *name = blobmsg_get_string(tb[DI_NAME]);
137
138         iface = calloc(1, sizeof(*iface));
139         if (!iface)
140                 return UBUS_STATUS_UNKNOWN_ERROR;
141
142         interface_init(iface, name, msg);
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(ctx);
324         system_fd_set_cloexec(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(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", 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                 blobmsg_close_table(&b, a);
446         }
447 }
448
449 static void
450 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
451 {
452         struct device_route *route;
453         int buflen = 128;
454         char *buf;
455         void *r;
456         int af;
457
458         time_t now = system_get_rtime();
459         vlist_for_each_element(&ip->route, route, node) {
460                 if (route->enabled != enabled)
461                         continue;
462
463                 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
464                         af = AF_INET;
465                 else
466                         af = AF_INET6;
467
468                 r = blobmsg_open_table(&b, NULL);
469
470                 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
471                 inet_ntop(af, &route->addr, buf, buflen);
472                 blobmsg_add_string_buffer(&b);
473
474                 blobmsg_add_u32(&b, "mask", route->mask);
475
476                 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
477                 inet_ntop(af, &route->nexthop, buf, buflen);
478                 blobmsg_add_string_buffer(&b);
479
480                 if (route->flags & DEVROUTE_MTU)
481                         blobmsg_add_u32(&b, "mtu", route->mtu);
482
483                 if (route->flags & DEVROUTE_METRIC)
484                         blobmsg_add_u32(&b, "metric", route->metric);
485
486                 if (route->flags & DEVROUTE_TABLE)
487                         blobmsg_add_u32(&b, "table", route->table);
488
489                 if (route->valid_until)
490                         blobmsg_add_u32(&b, "valid", route->valid_until - now);
491
492                 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
493                 inet_ntop(af, &route->source, buf, buflen);
494                 snprintf(buf + strlen(buf), 4, "/%u", route->sourcemask);
495                 blobmsg_add_string_buffer(&b);
496
497                 blobmsg_close_table(&b, r);
498         }
499 }
500
501
502 static void
503 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
504 {
505         struct device_prefix *prefix;
506         char *buf;
507         void *a, *c;
508         const int buflen = INET6_ADDRSTRLEN;
509
510         time_t now = system_get_rtime();
511         vlist_for_each_element(&ip->prefix, prefix, node) {
512                 a = blobmsg_open_table(&b, NULL);
513
514                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
515                 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
516                 blobmsg_add_string_buffer(&b);
517
518                 blobmsg_add_u32(&b, "mask", prefix->length);
519
520                 if (prefix->preferred_until) {
521                         int preferred = prefix->preferred_until - now;
522                         if (preferred < 0)
523                                 preferred = 0;
524                         blobmsg_add_u32(&b, "preferred", preferred);
525                 }
526
527                 if (prefix->valid_until)
528                         blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
529
530                 blobmsg_add_string(&b, "class", prefix->pclass);
531
532                 c = blobmsg_open_table(&b, "assigned");
533                 struct device_prefix_assignment *assign;
534                 list_for_each_entry(assign, &prefix->assignments, head) {
535                         if (!assign->name[0])
536                                 continue;
537
538                         struct in6_addr addr = prefix->addr;
539                         addr.s6_addr32[1] |= htonl(assign->assigned);
540
541                         void *d = blobmsg_open_table(&b, assign->name);
542
543                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
544                         inet_ntop(AF_INET6, &addr, buf, buflen);
545                         blobmsg_add_string_buffer(&b);
546
547                         blobmsg_add_u32(&b, "mask", assign->length);
548
549                         blobmsg_close_table(&b, d);
550                 }
551                 blobmsg_close_table(&b, c);
552
553                 blobmsg_close_table(&b, a);
554         }
555 }
556
557
558 static void
559 interface_ip_dump_prefix_assignment_list(struct interface *iface)
560 {
561         void *a;
562         char *buf;
563         const int buflen = INET6_ADDRSTRLEN;
564         time_t now = system_get_rtime();
565
566         struct device_prefix *prefix;
567         list_for_each_entry(prefix, &prefixes, head) {
568                 struct device_prefix_assignment *assign;
569                 list_for_each_entry(assign, &prefix->assignments, head) {
570                         if (strcmp(assign->name, iface->name))
571                                 continue;
572
573                         struct in6_addr addr = prefix->addr;
574                         addr.s6_addr32[1] |= htonl(assign->assigned);
575
576                         a = blobmsg_open_table(&b, NULL);
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                         if (prefix->preferred_until) {
585                                 int preferred = prefix->preferred_until - now;
586                                 if (preferred < 0)
587                                         preferred = 0;
588                                 blobmsg_add_u32(&b, "preferred", preferred);
589                         }
590
591                         if (prefix->valid_until)
592                                 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
593
594                         blobmsg_close_table(&b, a);
595                 }
596         }
597 }
598
599
600 static void
601 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
602                                   bool enabled)
603 {
604         struct dns_server *dns;
605         int buflen = 128;
606         char *buf;
607
608         vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
609                 if (ip->no_dns == enabled)
610                         continue;
611
612                 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
613                 inet_ntop(dns->af, &dns->addr, buf, buflen);
614                 blobmsg_add_string_buffer(&b);
615         }
616 }
617
618 static void
619 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
620                                   bool enabled)
621 {
622         struct dns_search_domain *dns;
623
624         vlist_simple_for_each_element(&ip->dns_search, dns, node) {
625                 if (ip->no_dns == enabled)
626                         continue;
627
628                 blobmsg_add_string(&b, NULL, dns->name);
629         }
630 }
631
632 static void
633 netifd_dump_status(struct interface *iface)
634 {
635         struct interface_data *data;
636         struct device *dev;
637         void *a, *inactive;
638
639         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
640         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
641         blobmsg_add_u8(&b, "available", iface->available);
642         blobmsg_add_u8(&b, "autostart", iface->autostart);
643
644         if (iface->state == IFS_UP) {
645                 time_t cur = system_get_rtime();
646                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
647                 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
648         }
649
650         if (iface->proto_handler)
651                 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
652
653         dev = iface->main_dev.dev;
654         if (dev && !dev->hidden &&
655             !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
656                 blobmsg_add_string(&b, "device", dev->ifname);
657
658         if (iface->state == IFS_UP) {
659                 blobmsg_add_u32(&b, "metric", iface->metric);
660                 a = blobmsg_open_array(&b, "ipv4-address");
661                 interface_ip_dump_address_list(&iface->config_ip, false, true);
662                 interface_ip_dump_address_list(&iface->proto_ip, false, true);
663                 blobmsg_close_array(&b, a);
664                 a = blobmsg_open_array(&b, "ipv6-address");
665                 interface_ip_dump_address_list(&iface->config_ip, true, true);
666                 interface_ip_dump_address_list(&iface->proto_ip, true, true);
667                 blobmsg_close_array(&b, a);
668                 a = blobmsg_open_array(&b, "ipv6-prefix");
669                 interface_ip_dump_prefix_list(&iface->config_ip);
670                 interface_ip_dump_prefix_list(&iface->proto_ip);
671                 blobmsg_close_array(&b, a);
672                 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
673                 interface_ip_dump_prefix_assignment_list(iface);
674                 blobmsg_close_array(&b, a);
675                 a = blobmsg_open_array(&b, "route");
676                 interface_ip_dump_route_list(&iface->config_ip, true);
677                 interface_ip_dump_route_list(&iface->proto_ip, true);
678                 blobmsg_close_array(&b, a);
679                 a = blobmsg_open_array(&b, "dns-server");
680                 interface_ip_dump_dns_server_list(&iface->config_ip, true);
681                 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
682                 blobmsg_close_array(&b, a);
683                 a = blobmsg_open_array(&b, "dns-search");
684                 interface_ip_dump_dns_search_list(&iface->config_ip, true);
685                 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
686                 blobmsg_close_array(&b, a);
687
688                 inactive = blobmsg_open_table(&b, "inactive");
689                 a = blobmsg_open_array(&b, "ipv4-address");
690                 interface_ip_dump_address_list(&iface->config_ip, false, false);
691                 interface_ip_dump_address_list(&iface->proto_ip, false, false);
692                 blobmsg_close_array(&b, a);
693                 a = blobmsg_open_array(&b, "ipv6-address");
694                 interface_ip_dump_address_list(&iface->config_ip, true, false);
695                 interface_ip_dump_address_list(&iface->proto_ip, true, false);
696                 blobmsg_close_array(&b, a);
697                 a = blobmsg_open_array(&b, "route");
698                 interface_ip_dump_route_list(&iface->config_ip, false);
699                 interface_ip_dump_route_list(&iface->proto_ip, false);
700                 blobmsg_close_array(&b, a);
701                 a = blobmsg_open_array(&b, "dns-server");
702                 interface_ip_dump_dns_server_list(&iface->config_ip, false);
703                 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
704                 blobmsg_close_array(&b, a);
705                 a = blobmsg_open_array(&b, "dns-search");
706                 interface_ip_dump_dns_search_list(&iface->config_ip, false);
707                 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
708                 blobmsg_close_array(&b, a);
709                 blobmsg_close_table(&b, inactive);
710         }
711
712         a = blobmsg_open_table(&b, "data");
713         avl_for_each_element(&iface->data, data, node)
714                 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
715
716         blobmsg_close_table(&b, a);
717
718         if (!list_is_empty(&iface->errors))
719                 netifd_add_interface_errors(&b, iface);
720 }
721
722 static int
723 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
724                      struct ubus_request_data *req, const char *method,
725                      struct blob_attr *msg)
726 {
727         struct interface *iface = container_of(obj, struct interface, ubus);
728
729         blob_buf_init(&b, 0);
730         netifd_dump_status(iface);
731         ubus_send_reply(ctx, req, b.head);
732
733         return 0;
734 }
735
736
737 static int
738 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
739                      struct ubus_request_data *req, const char *method,
740                      struct blob_attr *msg)
741 {
742         blob_buf_init(&b, 0);
743         void *a = blobmsg_open_array(&b, "interface");
744
745         struct interface *iface;
746         vlist_for_each_element(&interfaces, iface, node) {
747                 void *i = blobmsg_open_table(&b, NULL);
748                 blobmsg_add_string(&b, "interface", iface->name);
749                 netifd_dump_status(iface);
750                 blobmsg_close_table(&b, i);
751         }
752
753         blobmsg_close_array(&b, a);
754         ubus_send_reply(ctx, req, b.head);
755
756         return 0;
757 }
758
759 static int
760 netifd_iface_handle_device(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 blob_attr *tb[__DEV_MAX];
765         struct interface *iface;
766         struct device *dev;
767         bool add = !strncmp(method, "add", 3);
768         int ret;
769
770         iface = container_of(obj, struct interface, ubus);
771
772         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
773
774         if (!tb[DEV_NAME])
775                 return UBUS_STATUS_INVALID_ARGUMENT;
776
777         device_lock();
778
779         dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
780         if (!dev) {
781                 ret = UBUS_STATUS_NOT_FOUND;
782                 goto out;
783         }
784
785         if (add) {
786                 device_set_present(dev, true);
787                 if (iface->device_config)
788                         device_set_config(dev, &simple_device_type, iface->config);
789
790                 system_if_apply_settings(dev, &dev->settings);
791                 ret = interface_add_link(iface, dev);
792         } else {
793                 ret = interface_remove_link(iface, dev);
794         }
795
796 out:
797         device_unlock();
798
799         return ret;
800 }
801
802
803 static int
804 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
805                           struct ubus_request_data *req, const char *method,
806                           struct blob_attr *msg)
807 {
808         struct interface *iface;
809
810         iface = container_of(obj, struct interface, ubus);
811
812         if (!iface->proto || !iface->proto->notify)
813                 return UBUS_STATUS_NOT_SUPPORTED;
814
815         return iface->proto->notify(iface->proto, msg);
816 }
817
818 static void
819 netifd_iface_do_remove(struct uloop_timeout *timeout)
820 {
821         struct interface *iface;
822
823         iface = container_of(timeout, struct interface, remove_timer);
824         vlist_delete(&interfaces, &iface->node);
825 }
826
827 static int
828 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
829                     struct ubus_request_data *req, const char *method,
830                     struct blob_attr *msg)
831 {
832         struct interface *iface;
833
834         iface = container_of(obj, struct interface, ubus);
835         if (iface->remove_timer.cb)
836                 return UBUS_STATUS_INVALID_ARGUMENT;
837
838         iface->remove_timer.cb = netifd_iface_do_remove;
839         uloop_timeout_set(&iface->remove_timer, 100);
840         return 0;
841 }
842
843 static int
844 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
845                             struct ubus_request_data *req, const char *method,
846                             struct blob_attr *msg)
847 {
848         struct interface *iface;
849         struct device *dev;
850         const struct device_hotplug_ops *ops;
851
852         iface = container_of(obj, struct interface, ubus);
853         dev = iface->main_dev.dev;
854         if (!dev)
855                 return 0;
856
857         ops = dev->hotplug_ops;
858         if (!ops)
859                 return 0;
860
861         return ops->prepare(dev);
862 }
863
864 static int
865 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
866                        struct ubus_request_data *req, const char *method,
867                        struct blob_attr *msg)
868 {
869         struct interface *iface;
870         struct blob_attr *cur;
871         int rem, ret;
872
873         iface = container_of(obj, struct interface, ubus);
874
875         blob_for_each_attr(cur, msg, rem) {
876                 ret = interface_add_data(iface, cur);
877                 if (ret)
878                         return ret;
879         }
880
881         return 0;
882 }
883
884 static struct ubus_method iface_object_methods[] = {
885         { .name = "up", .handler = netifd_handle_up },
886         { .name = "down", .handler = netifd_handle_down },
887         { .name = "status", .handler = netifd_handle_status },
888         { .name = "prepare", .handler = netifd_handle_iface_prepare },
889         { .name = "dump", .handler = netifd_handle_dump },
890         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
891         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
892         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
893         { .name = "remove", .handler = netifd_iface_remove },
894         { .name = "set_data", .handler = netifd_handle_set_data },
895 };
896
897 static struct ubus_object_type iface_object_type =
898         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
899
900
901 static struct ubus_object iface_object = {
902         .name = "network.interface",
903         .type = &iface_object_type,
904         .n_methods = ARRAY_SIZE(iface_object_methods),
905 };
906
907 static void netifd_add_object(struct ubus_object *obj)
908 {
909         int ret = ubus_add_object(ctx, obj);
910
911         if (ret != 0)
912                 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
913 }
914
915 static const struct blobmsg_policy iface_policy = {
916         .name = "interface",
917         .type = BLOBMSG_TYPE_STRING,
918 };
919
920 static int
921 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
922                     struct ubus_request_data *req, const char *method,
923                     struct blob_attr *msg)
924 {
925         struct interface *iface;
926         struct blob_attr *tb;
927         int i;
928
929         blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
930         if (!tb)
931                 return UBUS_STATUS_INVALID_ARGUMENT;
932
933         iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
934         if (!iface)
935                 return UBUS_STATUS_NOT_FOUND;
936
937         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
938                 ubus_handler_t cb;
939
940                 if (strcmp(method, iface_object_methods[i].name) != 0)
941                         continue;
942
943                 cb = iface_object_methods[i].handler;
944                 return cb(ctx, &iface->ubus, req, method, msg);
945         }
946
947         return UBUS_STATUS_INVALID_ARGUMENT;
948 }
949
950 static void netifd_add_iface_object(void)
951 {
952         struct ubus_method *methods;
953         int i;
954
955         methods = calloc(1, sizeof(iface_object_methods));
956         memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
957         iface_object.methods = methods;
958
959         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
960                 if (methods[i].handler == netifd_handle_dump)
961                         continue;
962
963                 methods[i].handler = netifd_handle_iface;
964                 methods[i].policy = &iface_policy;
965                 methods[i].n_policy = 1;
966         }
967         netifd_add_object(&iface_object);
968 }
969
970 int
971 netifd_ubus_init(const char *path)
972 {
973         uloop_init();
974         ubus_path = path;
975
976         ctx = ubus_connect(path);
977         if (!ctx)
978                 return -EIO;
979
980         DPRINTF("connected as %08x\n", ctx->local_id);
981         ctx->connection_lost = netifd_ubus_connection_lost;
982         netifd_ubus_add_fd();
983
984         netifd_add_object(&main_object);
985         netifd_add_object(&dev_object);
986         netifd_add_iface_object();
987
988         return 0;
989 }
990
991 void
992 netifd_ubus_done(void)
993 {
994         ubus_free(ctx);
995 }
996
997 void
998 netifd_ubus_interface_event(struct interface *iface, bool up)
999 {
1000         blob_buf_init(&b, 0);
1001         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1002         blobmsg_add_string(&b, "interface", iface->name);
1003         ubus_send_event(ctx, "network.interface", b.head);
1004 }
1005
1006 void
1007 netifd_ubus_interface_notify(struct interface *iface, bool up)
1008 {
1009         const char *event = (up) ? "update" : "down";
1010         blob_buf_init(&b, 0);
1011         blobmsg_add_string(&b, "interface", iface->name);
1012         netifd_dump_status(iface);
1013         ubus_notify(ctx, &iface_object, event, b.head, -1);
1014         ubus_notify(ctx, &iface->ubus, event, b.head, -1);
1015 }
1016
1017 void
1018 netifd_ubus_add_interface(struct interface *iface)
1019 {
1020         struct ubus_object *obj = &iface->ubus;
1021         char *name = NULL;
1022
1023         if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1024                 return;
1025
1026         obj->name = name;
1027         obj->type = &iface_object_type;
1028         obj->methods = iface_object_methods;
1029         obj->n_methods = ARRAY_SIZE(iface_object_methods);
1030         if (ubus_add_object(ctx, &iface->ubus)) {
1031                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1032                 free(name);
1033                 obj->name = NULL;
1034         }
1035 }
1036
1037 void
1038 netifd_ubus_remove_interface(struct interface *iface)
1039 {
1040         if (!iface->ubus.name)
1041                 return;
1042
1043         ubus_remove_object(ctx, &iface->ubus);
1044         free((void *) iface->ubus.name);
1045 }