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