interface-ip: route proto config support (FS#170)
[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,
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_PROTO)
490                         blobmsg_add_u32(&b, "proto", route->proto);
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                         void *c = blobmsg_open_table(&b, "local-address");
607                         if (assign->enabled) {
608                                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
609                                 inet_ntop(AF_INET6, &assign->addr, buf, buflen);
610                                 blobmsg_add_string_buffer(&b);
611
612                                 blobmsg_add_u32(&b, "mask", assign->length < 64 ? 64 : assign->length);
613                         }
614                         blobmsg_close_table(&b, c);
615
616                         blobmsg_close_table(&b, a);
617                 }
618         }
619 }
620
621 static void
622 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
623                                   bool enabled)
624 {
625         struct dns_server *dns;
626         int buflen = 128;
627         char *buf;
628
629         vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
630                 if (ip->no_dns == enabled)
631                         continue;
632
633                 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
634                 inet_ntop(dns->af, &dns->addr, buf, buflen);
635                 blobmsg_add_string_buffer(&b);
636         }
637 }
638
639 static void
640 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
641                                   bool enabled)
642 {
643         struct dns_search_domain *dns;
644
645         vlist_simple_for_each_element(&ip->dns_search, dns, node) {
646                 if (ip->no_dns == enabled)
647                         continue;
648
649                 blobmsg_add_string(&b, NULL, dns->name);
650         }
651 }
652
653 static void
654 netifd_dump_status(struct interface *iface)
655 {
656         struct interface_data *data;
657         struct device *dev;
658         void *a, *inactive;
659
660         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
661         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
662         blobmsg_add_u8(&b, "available", iface->available);
663         blobmsg_add_u8(&b, "autostart", iface->autostart);
664         blobmsg_add_u8(&b, "dynamic", iface->dynamic);
665
666         if (iface->state == IFS_UP) {
667                 time_t cur = system_get_rtime();
668                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
669                 if (iface->l3_dev.dev)
670                         blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
671         }
672
673         if (iface->proto_handler)
674                 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
675
676         dev = iface->main_dev.dev;
677         if (dev && !dev->hidden && iface->proto_handler &&
678             !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
679                 blobmsg_add_string(&b, "device", dev->ifname);
680
681         if (iface->state == IFS_UP) {
682                 if (iface->updated) {
683                         a = blobmsg_open_array(&b, "updated");
684
685                         if (iface->updated & IUF_ADDRESS)
686                                 blobmsg_add_string(&b, NULL, "addresses");
687                         if (iface->updated & IUF_ROUTE)
688                                 blobmsg_add_string(&b, NULL, "routes");
689                         if (iface->updated & IUF_PREFIX)
690                                 blobmsg_add_string(&b, NULL, "prefixes");
691                         if (iface->updated & IUF_DATA)
692                                 blobmsg_add_string(&b, NULL, "data");
693
694                         blobmsg_close_array(&b, a);
695                 }
696
697                 if (iface->ip4table)
698                         blobmsg_add_u32(&b, "ip4table", iface->ip4table);
699                 if (iface->ip6table)
700                         blobmsg_add_u32(&b, "ip6table", iface->ip6table);
701                 blobmsg_add_u32(&b, "metric", iface->metric);
702                 blobmsg_add_u32(&b, "dns_metric", iface->dns_metric);
703                 blobmsg_add_u8(&b, "delegation", !iface->proto_ip.no_delegation);
704                 a = blobmsg_open_array(&b, "ipv4-address");
705                 interface_ip_dump_address_list(&iface->config_ip, false, true);
706                 interface_ip_dump_address_list(&iface->proto_ip, false, true);
707                 blobmsg_close_array(&b, a);
708                 a = blobmsg_open_array(&b, "ipv6-address");
709                 interface_ip_dump_address_list(&iface->config_ip, true, true);
710                 interface_ip_dump_address_list(&iface->proto_ip, true, true);
711                 blobmsg_close_array(&b, a);
712                 a = blobmsg_open_array(&b, "ipv6-prefix");
713                 interface_ip_dump_prefix_list(&iface->config_ip);
714                 interface_ip_dump_prefix_list(&iface->proto_ip);
715                 blobmsg_close_array(&b, a);
716                 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
717                 interface_ip_dump_prefix_assignment_list(iface);
718                 blobmsg_close_array(&b, a);
719                 a = blobmsg_open_array(&b, "route");
720                 interface_ip_dump_route_list(&iface->config_ip, true);
721                 interface_ip_dump_route_list(&iface->proto_ip, true);
722                 blobmsg_close_array(&b, a);
723                 a = blobmsg_open_array(&b, "dns-server");
724                 interface_ip_dump_dns_server_list(&iface->config_ip, true);
725                 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
726                 blobmsg_close_array(&b, a);
727                 a = blobmsg_open_array(&b, "dns-search");
728                 interface_ip_dump_dns_search_list(&iface->config_ip, true);
729                 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
730                 blobmsg_close_array(&b, a);
731
732                 inactive = blobmsg_open_table(&b, "inactive");
733                 a = blobmsg_open_array(&b, "ipv4-address");
734                 interface_ip_dump_address_list(&iface->config_ip, false, false);
735                 interface_ip_dump_address_list(&iface->proto_ip, false, false);
736                 blobmsg_close_array(&b, a);
737                 a = blobmsg_open_array(&b, "ipv6-address");
738                 interface_ip_dump_address_list(&iface->config_ip, true, false);
739                 interface_ip_dump_address_list(&iface->proto_ip, true, false);
740                 blobmsg_close_array(&b, a);
741                 a = blobmsg_open_array(&b, "route");
742                 interface_ip_dump_route_list(&iface->config_ip, false);
743                 interface_ip_dump_route_list(&iface->proto_ip, false);
744                 blobmsg_close_array(&b, a);
745                 a = blobmsg_open_array(&b, "dns-server");
746                 interface_ip_dump_dns_server_list(&iface->config_ip, false);
747                 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
748                 blobmsg_close_array(&b, a);
749                 a = blobmsg_open_array(&b, "dns-search");
750                 interface_ip_dump_dns_search_list(&iface->config_ip, false);
751                 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
752                 blobmsg_close_array(&b, a);
753                 blobmsg_close_table(&b, inactive);
754         }
755
756         a = blobmsg_open_table(&b, "data");
757         avl_for_each_element(&iface->data, data, node)
758                 blobmsg_add_blob(&b, data->data);
759
760         blobmsg_close_table(&b, a);
761
762         if (!list_empty(&iface->errors))
763                 netifd_add_interface_errors(&b, iface);
764 }
765
766 static int
767 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
768                      struct ubus_request_data *req, const char *method,
769                      struct blob_attr *msg)
770 {
771         struct interface *iface = container_of(obj, struct interface, ubus);
772
773         blob_buf_init(&b, 0);
774         netifd_dump_status(iface);
775         ubus_send_reply(ctx, req, b.head);
776
777         return 0;
778 }
779
780
781 static int
782 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
783                      struct ubus_request_data *req, const char *method,
784                      struct blob_attr *msg)
785 {
786         blob_buf_init(&b, 0);
787         void *a = blobmsg_open_array(&b, "interface");
788
789         struct interface *iface;
790         vlist_for_each_element(&interfaces, iface, node) {
791                 void *i = blobmsg_open_table(&b, NULL);
792                 blobmsg_add_string(&b, "interface", iface->name);
793                 netifd_dump_status(iface);
794                 blobmsg_close_table(&b, i);
795         }
796
797         blobmsg_close_array(&b, a);
798         ubus_send_reply(ctx, req, b.head);
799
800         return 0;
801 }
802
803 enum {
804         DEV_LINK_NAME,
805         DEV_LINK_EXT,
806         __DEV_LINK_MAX,
807 };
808
809 static const struct blobmsg_policy dev_link_policy[__DEV_LINK_MAX] = {
810         [DEV_LINK_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
811         [DEV_LINK_EXT] = { .name = "link-ext", .type = BLOBMSG_TYPE_BOOL },
812 };
813
814 static int
815 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
816                            struct ubus_request_data *req, const char *method,
817                            struct blob_attr *msg)
818 {
819         struct blob_attr *tb[__DEV_LINK_MAX];
820         struct blob_attr *cur;
821         struct interface *iface;
822         bool add = !strncmp(method, "add", 3);
823         bool link_ext = true;
824
825         iface = container_of(obj, struct interface, ubus);
826
827         blobmsg_parse(dev_link_policy, __DEV_LINK_MAX, tb, blob_data(msg), blob_len(msg));
828
829         if (!tb[DEV_LINK_NAME])
830                 return UBUS_STATUS_INVALID_ARGUMENT;
831
832         cur = tb[DEV_LINK_EXT];
833         if (cur)
834                 link_ext = blobmsg_get_bool(cur);
835
836         return interface_handle_link(iface, blobmsg_data(tb[DEV_LINK_NAME]), add, link_ext);
837 }
838
839
840 static int
841 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
842                           struct ubus_request_data *req, const char *method,
843                           struct blob_attr *msg)
844 {
845         struct interface *iface;
846
847         iface = container_of(obj, struct interface, ubus);
848
849         if (!iface->proto || !iface->proto->notify)
850                 return UBUS_STATUS_NOT_SUPPORTED;
851
852         return iface->proto->notify(iface->proto, msg);
853 }
854
855 static void
856 netifd_iface_do_remove(struct uloop_timeout *timeout)
857 {
858         struct interface *iface;
859
860         iface = container_of(timeout, struct interface, remove_timer);
861         vlist_delete(&interfaces, &iface->node);
862 }
863
864 static int
865 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
866                     struct ubus_request_data *req, const char *method,
867                     struct blob_attr *msg)
868 {
869         struct interface *iface;
870
871         iface = container_of(obj, struct interface, ubus);
872         if (iface->remove_timer.cb)
873                 return UBUS_STATUS_INVALID_ARGUMENT;
874
875         iface->remove_timer.cb = netifd_iface_do_remove;
876         uloop_timeout_set(&iface->remove_timer, 100);
877         return 0;
878 }
879
880 static int
881 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
882                             struct ubus_request_data *req, const char *method,
883                             struct blob_attr *msg)
884 {
885         struct interface *iface;
886         struct device *dev;
887         const struct device_hotplug_ops *ops;
888
889         iface = container_of(obj, struct interface, ubus);
890         dev = iface->main_dev.dev;
891         if (!dev)
892                 return 0;
893
894         ops = dev->hotplug_ops;
895         if (!ops)
896                 return 0;
897
898         return ops->prepare(dev);
899 }
900
901 static int
902 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
903                        struct ubus_request_data *req, const char *method,
904                        struct blob_attr *msg)
905 {
906         struct interface *iface;
907
908         iface = container_of(obj, struct interface, ubus);
909
910         return interface_parse_data(iface, msg);
911 }
912
913 static struct ubus_method iface_object_methods[] = {
914         { .name = "up", .handler = netifd_handle_up },
915         { .name = "down", .handler = netifd_handle_down },
916         { .name = "status", .handler = netifd_handle_status },
917         { .name = "prepare", .handler = netifd_handle_iface_prepare },
918         { .name = "dump", .handler = netifd_handle_dump },
919         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_link_policy ),
920         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_link_policy ),
921         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
922         { .name = "remove", .handler = netifd_iface_remove },
923         { .name = "set_data", .handler = netifd_handle_set_data },
924 };
925
926 static struct ubus_object_type iface_object_type =
927         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
928
929
930 static struct ubus_object iface_object = {
931         .name = "network.interface",
932         .type = &iface_object_type,
933         .n_methods = ARRAY_SIZE(iface_object_methods),
934 };
935
936 static void netifd_add_object(struct ubus_object *obj)
937 {
938         int ret = ubus_add_object(ubus_ctx, obj);
939
940         if (ret != 0)
941                 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
942 }
943
944 static const struct blobmsg_policy iface_policy = {
945         .name = "interface",
946         .type = BLOBMSG_TYPE_STRING,
947 };
948
949 static int
950 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
951                     struct ubus_request_data *req, const char *method,
952                     struct blob_attr *msg)
953 {
954         struct interface *iface;
955         struct blob_attr *tb;
956         int i;
957
958         blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
959         if (!tb)
960                 return UBUS_STATUS_INVALID_ARGUMENT;
961
962         iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
963         if (!iface)
964                 return UBUS_STATUS_NOT_FOUND;
965
966         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
967                 ubus_handler_t cb;
968
969                 if (strcmp(method, iface_object_methods[i].name) != 0)
970                         continue;
971
972                 cb = iface_object_methods[i].handler;
973                 return cb(ctx, &iface->ubus, req, method, msg);
974         }
975
976         return UBUS_STATUS_INVALID_ARGUMENT;
977 }
978
979 static void netifd_add_iface_object(void)
980 {
981         struct ubus_method *methods;
982         int i;
983
984         methods = calloc(1, sizeof(iface_object_methods));
985         if (!methods)
986                 return;
987
988         memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
989         iface_object.methods = methods;
990
991         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
992                 if (methods[i].handler == netifd_handle_dump)
993                         continue;
994
995                 methods[i].handler = netifd_handle_iface;
996                 methods[i].policy = &iface_policy;
997                 methods[i].n_policy = 1;
998         }
999         netifd_add_object(&iface_object);
1000 }
1001
1002 static struct wireless_device *
1003 get_wdev(struct blob_attr *msg, int *ret)
1004 {
1005         struct blobmsg_policy wdev_policy = {
1006                 .name = "device",
1007                 .type = BLOBMSG_TYPE_STRING,
1008         };
1009         struct blob_attr *dev_attr;
1010         struct wireless_device *wdev = NULL;
1011
1012
1013         blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
1014         if (!dev_attr) {
1015                 *ret = UBUS_STATUS_INVALID_ARGUMENT;
1016                 return NULL;
1017         }
1018
1019         wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
1020         if (!wdev) {
1021                 *ret = UBUS_STATUS_NOT_FOUND;
1022                 return NULL;
1023         }
1024
1025         *ret = 0;
1026         return wdev;
1027 }
1028
1029 static int
1030 netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
1031                       struct ubus_request_data *req, const char *method,
1032                       struct blob_attr *msg)
1033 {
1034         struct wireless_device *wdev;
1035         int ret;
1036
1037         wdev = get_wdev(msg, &ret);
1038         if (ret == UBUS_STATUS_NOT_FOUND)
1039                 return ret;
1040
1041         if (wdev) {
1042                 wireless_device_set_up(wdev);
1043         } else {
1044                 vlist_for_each_element(&wireless_devices, wdev, node)
1045                         wireless_device_set_up(wdev);
1046         }
1047
1048         return 0;
1049 }
1050
1051 static int
1052 netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
1053                         struct ubus_request_data *req, const char *method,
1054                         struct blob_attr *msg)
1055 {
1056         struct wireless_device *wdev;
1057         int ret;
1058
1059         wdev = get_wdev(msg, &ret);
1060         if (ret == UBUS_STATUS_NOT_FOUND)
1061                 return ret;
1062
1063         if (wdev) {
1064                 wireless_device_set_down(wdev);
1065         } else {
1066                 vlist_for_each_element(&wireless_devices, wdev, node)
1067                         wireless_device_set_down(wdev);
1068         }
1069
1070         return 0;
1071 }
1072
1073 static int
1074 netifd_handle_wdev_status(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_status(wdev, &b);
1088         } else {
1089                 vlist_for_each_element(&wireless_devices, wdev, node)
1090                         wireless_device_status(wdev, &b);
1091         }
1092         ubus_send_reply(ctx, req, b.head);
1093         return 0;
1094 }
1095
1096 static int
1097 netifd_handle_wdev_get_validate(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 (ret == UBUS_STATUS_NOT_FOUND)
1106                 return ret;
1107
1108         blob_buf_init(&b, 0);
1109         if (wdev) {
1110                 wireless_device_get_validate(wdev, &b);
1111         } else {
1112                 vlist_for_each_element(&wireless_devices, wdev, node)
1113                         wireless_device_get_validate(wdev, &b);
1114         }
1115         ubus_send_reply(ctx, req, b.head);
1116         return 0;
1117 }
1118
1119 static int
1120 netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
1121                           struct ubus_request_data *req, const char *method,
1122                           struct blob_attr *msg)
1123 {
1124         struct wireless_device *wdev;
1125         int ret;
1126
1127         wdev = get_wdev(msg, &ret);
1128         if (!wdev)
1129                 return ret;
1130
1131         return wireless_device_notify(wdev, msg, req);
1132 }
1133
1134 static struct ubus_method wireless_object_methods[] = {
1135         { .name = "up", .handler = netifd_handle_wdev_up },
1136         { .name = "down", .handler = netifd_handle_wdev_down },
1137         { .name = "status", .handler = netifd_handle_wdev_status },
1138         { .name = "notify", .handler = netifd_handle_wdev_notify },
1139         { .name = "get_validate", .handler = netifd_handle_wdev_get_validate },
1140 };
1141
1142 static struct ubus_object_type wireless_object_type =
1143         UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
1144
1145
1146 static struct ubus_object wireless_object = {
1147         .name = "network.wireless",
1148         .type = &wireless_object_type,
1149         .methods = wireless_object_methods,
1150         .n_methods = ARRAY_SIZE(wireless_object_methods),
1151 };
1152
1153 int
1154 netifd_ubus_init(const char *path)
1155 {
1156         uloop_init();
1157         ubus_path = path;
1158
1159         ubus_ctx = ubus_connect(path);
1160         if (!ubus_ctx)
1161                 return -EIO;
1162
1163         DPRINTF("connected as %08x\n", ubus_ctx->local_id);
1164         ubus_ctx->connection_lost = netifd_ubus_connection_lost;
1165         netifd_ubus_add_fd();
1166
1167         netifd_add_object(&main_object);
1168         netifd_add_object(&dev_object);
1169         netifd_add_object(&wireless_object);
1170         netifd_add_iface_object();
1171
1172         return 0;
1173 }
1174
1175 void
1176 netifd_ubus_done(void)
1177 {
1178         ubus_free(ubus_ctx);
1179 }
1180
1181 void
1182 netifd_ubus_interface_event(struct interface *iface, bool up)
1183 {
1184         blob_buf_init(&b, 0);
1185         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
1186         blobmsg_add_string(&b, "interface", iface->name);
1187         ubus_send_event(ubus_ctx, "network.interface", b.head);
1188 }
1189
1190 void
1191 netifd_ubus_interface_notify(struct interface *iface, bool up)
1192 {
1193         const char *event = (up) ? "interface.update" : "interface.down";
1194         blob_buf_init(&b, 0);
1195         blobmsg_add_string(&b, "interface", iface->name);
1196         netifd_dump_status(iface);
1197         ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
1198         ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
1199 }
1200
1201 void
1202 netifd_ubus_add_interface(struct interface *iface)
1203 {
1204         struct ubus_object *obj = &iface->ubus;
1205         char *name = NULL;
1206
1207         if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
1208                 return;
1209
1210         obj->name = name;
1211         obj->type = &iface_object_type;
1212         obj->methods = iface_object_methods;
1213         obj->n_methods = ARRAY_SIZE(iface_object_methods);
1214         if (ubus_add_object(ubus_ctx, &iface->ubus)) {
1215                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
1216                 free(name);
1217                 obj->name = NULL;
1218         }
1219 }
1220
1221 void
1222 netifd_ubus_remove_interface(struct interface *iface)
1223 {
1224         if (!iface->ubus.name)
1225                 return;
1226
1227         ubus_remove_object(ubus_ctx, &iface->ubus);
1228         free((void *) iface->ubus.name);
1229 }