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