system-linux: parse vti specific settings as nested json data object
[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_find(blobmsg_data(tb[DEV_NAME]));
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, 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                 if (addr->pclass)
445                         blobmsg_add_string(&b, "class", addr->pclass);
446
447                 blobmsg_close_table(&b, a);
448         }
449 }
450
451 static void
452 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
453 {
454         struct device_route *route;
455         int buflen = 128;
456         char *buf;
457         void *r;
458         int af;
459
460         time_t now = system_get_rtime();
461         vlist_for_each_element(&ip->route, route, node) {
462                 if (route->enabled != enabled)
463                         continue;
464
465                 if ((ip->no_defaultroute == enabled) && !route->mask)
466                         continue;
467
468                 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
469                         af = AF_INET;
470                 else
471                         af = AF_INET6;
472
473                 r = blobmsg_open_table(&b, NULL);
474
475                 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
476                 inet_ntop(af, &route->addr, buf, buflen);
477                 blobmsg_add_string_buffer(&b);
478
479                 blobmsg_add_u32(&b, "mask", route->mask);
480
481                 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
482                 inet_ntop(af, &route->nexthop, buf, buflen);
483                 blobmsg_add_string_buffer(&b);
484
485                 if (route->flags & DEVROUTE_TYPE)
486                         blobmsg_add_u32(&b, "type", route->type);
487
488                 if (route->flags & DEVROUTE_PROTO)
489                         blobmsg_add_u32(&b, "proto", route->proto);
490
491                 if (route->flags & DEVROUTE_MTU)
492                         blobmsg_add_u32(&b, "mtu", route->mtu);
493
494                 if (route->flags & DEVROUTE_METRIC)
495                         blobmsg_add_u32(&b, "metric", route->metric);
496
497                 if (route->flags & DEVROUTE_TABLE)
498                         blobmsg_add_u32(&b, "table", route->table);
499
500                 if (route->valid_until)
501                         blobmsg_add_u32(&b, "valid", route->valid_until - now);
502
503                 buf = blobmsg_alloc_string_buffer(&b, "source", buflen);
504                 inet_ntop(af, &route->source, buf, buflen);
505                 snprintf(buf + strlen(buf), buflen - strlen(buf), "/%u", route->sourcemask);
506                 blobmsg_add_string_buffer(&b);
507
508                 blobmsg_close_table(&b, r);
509         }
510 }
511
512
513 static void
514 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
515 {
516         struct device_prefix *prefix;
517         char *buf;
518         void *a, *c;
519         const int buflen = INET6_ADDRSTRLEN;
520
521         time_t now = system_get_rtime();
522         vlist_for_each_element(&ip->prefix, prefix, node) {
523                 a = blobmsg_open_table(&b, NULL);
524
525                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
526                 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
527                 blobmsg_add_string_buffer(&b);
528
529                 blobmsg_add_u32(&b, "mask", prefix->length);
530
531                 if (prefix->preferred_until) {
532                         int preferred = prefix->preferred_until - now;
533                         if (preferred < 0)
534                                 preferred = 0;
535                         blobmsg_add_u32(&b, "preferred", preferred);
536                 }
537
538                 if (prefix->valid_until)
539                         blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
540
541                 blobmsg_add_string(&b, "class", prefix->pclass);
542
543                 c = blobmsg_open_table(&b, "assigned");
544                 struct device_prefix_assignment *assign;
545                 list_for_each_entry(assign, &prefix->assignments, head) {
546                         if (!assign->name[0])
547                                 continue;
548
549                         struct in6_addr addr = prefix->addr;
550                         addr.s6_addr32[1] |= htonl(assign->assigned);
551
552                         void *d = blobmsg_open_table(&b, assign->name);
553
554                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
555                         inet_ntop(AF_INET6, &addr, buf, buflen);
556                         blobmsg_add_string_buffer(&b);
557
558                         blobmsg_add_u32(&b, "mask", assign->length);
559
560                         blobmsg_close_table(&b, d);
561                 }
562                 blobmsg_close_table(&b, c);
563
564                 blobmsg_close_table(&b, a);
565         }
566 }
567
568
569 static void
570 interface_ip_dump_prefix_assignment_list(struct interface *iface)
571 {
572         void *a;
573         char *buf;
574         const int buflen = INET6_ADDRSTRLEN;
575         time_t now = system_get_rtime();
576
577         struct device_prefix *prefix;
578         list_for_each_entry(prefix, &prefixes, head) {
579                 struct device_prefix_assignment *assign;
580                 list_for_each_entry(assign, &prefix->assignments, head) {
581                         if (strcmp(assign->name, iface->name))
582                                 continue;
583
584                         struct in6_addr addr = prefix->addr;
585                         addr.s6_addr32[1] |= htonl(assign->assigned);
586
587                         a = blobmsg_open_table(&b, NULL);
588
589                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
590                         inet_ntop(AF_INET6, &addr, buf, buflen);
591                         blobmsg_add_string_buffer(&b);
592
593                         blobmsg_add_u32(&b, "mask", assign->length);
594
595                         if (prefix->preferred_until) {
596                                 int preferred = prefix->preferred_until - now;
597                                 if (preferred < 0)
598                                         preferred = 0;
599                                 blobmsg_add_u32(&b, "preferred", preferred);
600                         }
601
602                         if (prefix->valid_until)
603                                 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
604
605                         void *c = blobmsg_open_table(&b, "local-address");
606                         if (assign->enabled) {
607                                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
608                                 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
609                                 blobmsg_add_string_buffer(&b);
610
611                                 blobmsg_add_u32(&b, "mask", assign->length < 64 ? 64 : assign->length);
612                         }
613                         blobmsg_close_table(&b, c);
614
615                         blobmsg_close_table(&b, a);
616                 }
617         }
618 }
619
620 static void
621 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip, bool enabled)
622 {
623         struct dns_server *dns;
624         int buflen = 128;
625         char *buf;
626
627         vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
628                 if (ip->no_dns == enabled)
629                         continue;
630
631                 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
632                 inet_ntop(dns->af, &dns->addr, buf, buflen);
633                 blobmsg_add_string_buffer(&b);
634         }
635 }
636
637 static void
638 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip, bool enabled)
639 {
640         struct dns_search_domain *dns;
641
642         vlist_simple_for_each_element(&ip->dns_search, dns, node) {
643                 if (ip->no_dns == enabled)
644                         continue;
645
646                 blobmsg_add_string(&b, NULL, dns->name);
647         }
648 }
649
650 static void
651 netifd_dump_status(struct interface *iface)
652 {
653         struct interface_data *data;
654         struct device *dev;
655         void *a, *inactive;
656
657         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
658         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
659         blobmsg_add_u8(&b, "available", iface->available);
660         blobmsg_add_u8(&b, "autostart", iface->autostart);
661         blobmsg_add_u8(&b, "dynamic", iface->dynamic);
662
663         if (iface->state == IFS_UP) {
664                 time_t cur = system_get_rtime();
665                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
666                 if (iface->l3_dev.dev)
667                         blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
668         }
669
670         if (iface->proto_handler)
671                 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
672
673         dev = iface->main_dev.dev;
674         if (dev && !dev->hidden && iface->proto_handler &&
675             !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
676                 blobmsg_add_string(&b, "device", dev->ifname);
677
678         if (iface->state == IFS_UP) {
679                 if (iface->updated) {
680                         a = blobmsg_open_array(&b, "updated");
681
682                         if (iface->updated & IUF_ADDRESS)
683                                 blobmsg_add_string(&b, NULL, "addresses");
684                         if (iface->updated & IUF_ROUTE)
685                                 blobmsg_add_string(&b, NULL, "routes");
686                         if (iface->updated & IUF_PREFIX)
687                                 blobmsg_add_string(&b, NULL, "prefixes");
688                         if (iface->updated & IUF_DATA)
689                                 blobmsg_add_string(&b, NULL, "data");
690
691                         blobmsg_close_array(&b, a);
692                 }
693
694                 if (iface->ip4table)
695                         blobmsg_add_u32(&b, "ip4table", iface->ip4table);
696                 if (iface->ip6table)
697                         blobmsg_add_u32(&b, "ip6table", iface->ip6table);
698                 blobmsg_add_u32(&b, "metric", iface->metric);
699                 blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
700                 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
701                 if (iface->assignment_weight)
702                         blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
703                 a = blobmsg_open_array(&b, "ipv4-address");
704                 interface_ip_dump_address_list(&iface->config_ip, false, true);
705                 interface_ip_dump_address_list(&iface->proto_ip, false, true);
706                 blobmsg_close_array(&b, a);
707                 a = blobmsg_open_array(&b, "ipv6-address");
708                 interface_ip_dump_address_list(&iface->config_ip, true, true);
709                 interface_ip_dump_address_list(&iface->proto_ip, true, true);
710                 blobmsg_close_array(&b, a);
711                 a = blobmsg_open_array(&b, "ipv6-prefix");
712                 interface_ip_dump_prefix_list(&iface->config_ip);
713                 interface_ip_dump_prefix_list(&iface->proto_ip);
714                 blobmsg_close_array(&b, a);
715                 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
716                 interface_ip_dump_prefix_assignment_list(iface);
717                 blobmsg_close_array(&b, a);
718                 a = blobmsg_open_array(&b, "route");
719                 interface_ip_dump_route_list(&iface->config_ip, true);
720                 interface_ip_dump_route_list(&iface->proto_ip, true);
721                 blobmsg_close_array(&b, a);
722                 a = blobmsg_open_array(&b, "dns-server");
723                 interface_ip_dump_dns_server_list(&iface->config_ip, true);
724                 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
725                 blobmsg_close_array(&b, a);
726                 a = blobmsg_open_array(&b, "dns-search");
727                 interface_ip_dump_dns_search_list(&iface->config_ip, true);
728                 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
729                 blobmsg_close_array(&b, a);
730
731                 inactive = blobmsg_open_table(&b, "inactive");
732                 a = blobmsg_open_array(&b, "ipv4-address");
733                 interface_ip_dump_address_list(&iface->config_ip, false, false);
734                 interface_ip_dump_address_list(&iface->proto_ip, false, false);
735                 blobmsg_close_array(&b, a);
736                 a = blobmsg_open_array(&b, "ipv6-address");
737                 interface_ip_dump_address_list(&iface->config_ip, true, false);
738                 interface_ip_dump_address_list(&iface->proto_ip, true, false);
739                 blobmsg_close_array(&b, a);
740                 a = blobmsg_open_array(&b, "route");
741                 interface_ip_dump_route_list(&iface->config_ip, false);
742                 interface_ip_dump_route_list(&iface->proto_ip, false);
743                 blobmsg_close_array(&b, a);
744                 a = blobmsg_open_array(&b, "dns-server");
745                 interface_ip_dump_dns_server_list(&iface->config_ip, false);
746                 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
747                 blobmsg_close_array(&b, a);
748                 a = blobmsg_open_array(&b, "dns-search");
749                 interface_ip_dump_dns_search_list(&iface->config_ip, false);
750                 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
751                 blobmsg_close_array(&b, a);
752                 blobmsg_close_table(&b, inactive);
753         }
754
755         a = blobmsg_open_table(&b, "data");
756         avl_for_each_element(&iface->data, data, node)
757                 blobmsg_add_blob(&b, data->data);
758
759         blobmsg_close_table(&b, a);
760
761         if (!list_empty(&iface->errors))
762                 netifd_add_interface_errors(&b, iface);
763 }
764
765 static int
766 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
767                      struct ubus_request_data *req, const char *method,
768                      struct blob_attr *msg)
769 {
770         struct interface *iface = container_of(obj, struct interface, ubus);
771
772         blob_buf_init(&b, 0);
773         netifd_dump_status(iface);
774         ubus_send_reply(ctx, req, b.head);
775
776         return 0;
777 }
778
779
780 static int
781 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
782                      struct ubus_request_data *req, const char *method,
783                      struct blob_attr *msg)
784 {
785         blob_buf_init(&b, 0);
786         void *a = blobmsg_open_array(&b, "interface");
787
788         struct interface *iface;
789         vlist_for_each_element(&interfaces, iface, node) {
790                 void *i = blobmsg_open_table(&b, NULL);
791                 blobmsg_add_string(&b, "interface", iface->name);
792                 netifd_dump_status(iface);
793                 blobmsg_close_table(&b, i);
794         }
795
796         blobmsg_close_array(&b, a);
797         ubus_send_reply(ctx, req, b.head);
798
799         return 0;
800 }
801
802 enum {
803         DEV_LINK_NAME,
804         DEV_LINK_EXT,
805         __DEV_LINK_MAX,
806 };
807
808 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
809         [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
810         [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
811 };
812
813 static int
814 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
815                            struct ubus_request_data *req, const char *method,
816                            struct blob_attr *msg)
817 {
818         struct blob_attr *tb[__DEV_LINK_MAX];
819         struct blob_attr *cur;
820         struct interface *iface;
821         bool add = !strncmp(method, "add", 3);
822         bool link_ext = true;
823
824         iface = container_of(obj, struct interface, ubus);
825
826         blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
827
828         if (!tb[DEV_LINK_NAME])
829                 return UBUS_STATUS_INVALID_ARGUMENT;
830
831         cur = tb[DEV_LINK_EXT];
832         if (cur)
833                 link_ext = blobmsg_get_bool(cur);
834
835         return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
836 }
837
838
839 static int
840 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
841                           struct ubus_request_data *req, const char *method,
842                           struct blob_attr *msg)
843 {
844         struct interface *iface;
845
846         iface = container_of(obj, struct interface, ubus);
847
848         if (!iface->proto || !iface->proto->notify)
849                 return UBUS_STATUS_NOT_SUPPORTED;
850
851         return iface->proto->notify(iface->proto, msg);
852 }
853
854 static void
855 netifd_iface_do_remove(struct uloop_timeout *timeout)
856 {
857         struct interface *iface;
858
859         iface = container_of(timeout, struct interface, remove_timer);
860         vlist_delete(&interfaces, &iface->node);
861 }
862
863 static int
864 netifd_iface_remove(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
870         iface = container_of(obj, struct interface, ubus);
871         if (iface->remove_timer.cb)
872                 return UBUS_STATUS_INVALID_ARGUMENT;
873
874         iface->remove_timer.cb = netifd_iface_do_remove;
875         uloop_timeout_set(&iface->remove_timer, 100);
876         return 0;
877 }
878
879 static int
880 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
881                             struct ubus_request_data *req, const char *method,
882                             struct blob_attr *msg)
883 {
884         struct interface *iface;
885         struct device *dev;
886         const struct device_hotplug_ops *ops;
887
888         iface = container_of(obj, struct interface, ubus);
889         dev = iface->main_dev.dev;
890         if (!dev)
891                 return 0;
892
893         ops = dev->hotplug_ops;
894         if (!ops)
895                 return 0;
896
897         return ops->prepare(dev);
898 }
899
900 static int
901 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
902                        struct ubus_request_data *req, const char *method,
903                        struct blob_attr *msg)
904 {
905         struct interface *iface;
906
907         iface = container_of(obj, struct interface, ubus);
908
909         return interface_parse_data(iface, msg);
910 }
911
912 static struct ubus_method iface_object_methods[] = {
913         { .name = "up", .handler = netifd_handle_up },
914         { .name = "down", .handler = netifd_handle_down },
915         { .name = "status", .handler = netifd_handle_status },
916         { .name = "prepare", .handler = netifd_handle_iface_prepare },
917         { .name = "dump", .handler = netifd_handle_dump },
918         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
919         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
920         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
921         { .name = "remove", .handler = netifd_iface_remove },
922         { .name = "set_data", .handler = netifd_handle_set_data },
923 };
924
925 static struct ubus_object_type iface_object_type =
926         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
927
928
929 static struct ubus_object iface_object = {
930         .name = "network.interface",
931         .type = &iface_object_type,
932         .n_methods = ARRAY_SIZE(iface_object_methods),
933 };
934
935 static void netifd_add_object(struct ubus_object *obj)
936 {
937         int ret = ubus_add_object(ubus_ctx, obj);
938
939         if (ret != 0)
940                 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
941 }
942
943 static const struct blobmsg_policy iface_policy = {
944         .name = "interface",
945         .type = BLOBMSG_TYPE_STRING,
946 };
947
948 static int
949 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
950                     struct ubus_request_data *req, const char *method,
951                     struct blob_attr *msg)
952 {
953         struct interface *iface;
954         struct blob_attr *tb;
955         int i;
956
957         blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
958         if (!tb)
959                 return UBUS_STATUS_INVALID_ARGUMENT;
960
961         iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
962         if (!iface)
963                 return UBUS_STATUS_NOT_FOUND;
964
965         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
966                 ubus_handler_t cb;
967
968                 if (strcmp(method, iface_object_methods[i].name) != 0)
969                         continue;
970
971                 cb = iface_object_methods[i].handler;
972                 return cb(ctx, &iface->ubus, req, method, msg);
973         }
974
975         return UBUS_STATUS_INVALID_ARGUMENT;
976 }
977
978 static void netifd_add_iface_object(void)
979 {
980         struct ubus_method *methods;
981         int i;
982
983         methods = calloc(1, sizeof(iface_object_methods));
984         if (!methods)
985                 return;
986
987         memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
988         iface_object.methods = methods;
989
990         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
991                 if (methods[i].handler == netifd_handle_dump)
992                         continue;
993
994                 methods[i].handler = netifd_handle_iface;
995                 methods[i].policy = &iface_policy;
996                 methods[i].n_policy = 1;
997         }
998         netifd_add_object(&iface_object);
999 }
1000
1001 static struct wireless_device *
1002 get_wdev(struct blob_attr *msg, int *ret)
1003 {
1004         struct blobmsg_policy wdev_policy = {
1005                 .name = "device",
1006                 .type = BLOBMSG_TYPE_STRING,
1007         };
1008         struct blob_attr *dev_attr;
1009         struct wireless_device *wdev = NULL;
1010
1011
1012         blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1013         if (!dev_attr) {
1014                 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1015                 return NULL;
1016         }
1017
1018         wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1019         if (!wdev) {
1020                 *ret = UBUS_STATUS_NOT_FOUND;
1021                 return NULL;
1022         }
1023
1024         *ret = 0;
1025         return wdev;
1026 }
1027
1028 static int
1029 netifd_handle_wdev_up(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_up(wdev);
1042         } else {
1043                 vlist_for_each_element(&wireless_devices, wdev, node)
1044                         wireless_device_set_up(wdev);
1045         }
1046
1047         return 0;
1048 }
1049
1050 static int
1051 netifd_handle_wdev_down(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         if (wdev) {
1063                 wireless_device_set_down(wdev);
1064         } else {
1065                 vlist_for_each_element(&wireless_devices, wdev, node)
1066                         wireless_device_set_down(wdev);
1067         }
1068
1069         return 0;
1070 }
1071
1072 static int
1073 netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
1074                           struct ubus_request_data *req, const char *method,
1075                           struct blob_attr *msg)
1076 {
1077         struct wireless_device *wdev;
1078         int ret;
1079
1080         wdev = get_wdev(msg, &ret);
1081         if (ret == UBUS_STATUS_NOT_FOUND)
1082                 return ret;
1083
1084         blob_buf_init(&b, 0);
1085         if (wdev) {
1086                 wireless_device_status(wdev, &b);
1087         } else {
1088                 vlist_for_each_element(&wireless_devices, wdev, node)
1089                         wireless_device_status(wdev, &b);
1090         }
1091         ubus_send_reply(ctx, req, b.head);
1092         return 0;
1093 }
1094
1095 static int
1096 netifd_handle_wdev_get_validate(struct ubus_context *ctx, struct ubus_object *obj,
1097                           struct ubus_request_data *req, const char *method,
1098                           struct blob_attr *msg)
1099 {
1100         struct wireless_device *wdev;
1101         int ret;
1102
1103         wdev = get_wdev(msg, &ret);
1104         if (ret == UBUS_STATUS_NOT_FOUND)
1105                 return ret;
1106
1107         blob_buf_init(&b, 0);
1108         if (wdev) {
1109                 wireless_device_get_validate(wdev, &b);
1110         } else {
1111                 vlist_for_each_element(&wireless_devices, wdev, node)
1112                         wireless_device_get_validate(wdev, &b);
1113         }
1114         ubus_send_reply(ctx, req, b.head);
1115         return 0;
1116 }
1117
1118 static int
1119 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1120                           struct ubus_request_data *req, const char *method,
1121                           struct blob_attr *msg)
1122 {
1123         struct wireless_device *wdev;
1124         int ret;
1125
1126         wdev = get_wdev(msg, &ret);
1127         if (!wdev)
1128                 return ret;
1129
1130         return wireless_device_notify(wdev, msg, req);
1131 }
1132
1133 static struct ubus_method wireless_object_methods[] = {
1134         { .name = "up", .handler = netifd_handle_wdev_up },
1135         { .name = "down", .handler = netifd_handle_wdev_down },
1136         { .name = "status", .handler = netifd_handle_wdev_status },
1137         { .name = "notify", .handler = netifd_handle_wdev_notify },
1138         { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1139 };
1140
1141 static struct ubus_object_type wireless_object_type =
1142         UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1143
1144
1145 static struct ubus_object wireless_object = {
1146         .name = "network.wireless",
1147         .type = &wireless_object_type,
1148         .methods = wireless_object_methods,
1149         .n_methods = ARRAY_SIZE(wireless_object_methods),
1150 };
1151
1152 int
1153 netifd_ubus_init(const char *path)
1154 {
1155         uloop_init();
1156         ubus_path = path;
1157
1158         ubus_ctx = ubus_connect(path);
1159         if (!ubus_ctx)
1160                 return -EIO;
1161
1162         DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1163         ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1164         netifd_ubus_add_fd();
1165
1166         netifd_add_object(&main_object);
1167         netifd_add_object(&dev_object);
1168         netifd_add_object(&wireless_object);
1169         netifd_add_iface_object();
1170
1171         return 0;
1172 }
1173
1174 void
1175 netifd_ubus_done(void)
1176 {
1177         ubus_free(ubus_ctx);
1178 }
1179
1180 void
1181 netifd_ubus_interface_event(struct interface *iface, bool up)
1182 {
1183         blob_buf_init(&b, 0);
1184         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1185         blobmsg_add_string(&b, "interface", iface->name);
1186         ubus_send_event(ubus_ctx, "network.interface", b.head);
1187 }
1188
1189 void
1190 netifd_ubus_interface_notify(struct interface *iface, bool up)
1191 {
1192         const char *event = (up) ? "interface.update" : "interface.down";
1193         blob_buf_init(&b, 0);
1194         blobmsg_add_string(&b, "interface", iface->name);
1195         netifd_dump_status(iface);
1196         ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1197         ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1198 }
1199
1200 void
1201 netifd_ubus_add_interface(struct interface *iface)
1202 {
1203         struct ubus_object *obj = &iface->ubus;
1204         char *name = NULL;
1205
1206         if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1207                 return;
1208
1209         obj->name = name;
1210         obj->type = &iface_object_type;
1211         obj->methods = iface_object_methods;
1212         obj->n_methods = ARRAY_SIZE(iface_object_methods);
1213         if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1214                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1215                 free(name);
1216                 obj->name = NULL;
1217         }
1218 }
1219
1220 void
1221 netifd_ubus_remove_interface(struct interface *iface)
1222 {
1223         if (!iface->ubus.name)
1224                 return;
1225
1226         ubus_remove_object(ubus_ctx, &iface->ubus);
1227         free((void *) iface->ubus.name);
1228 }