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