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