Add ubus methods for global interface status
[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
26 static struct ubus_context *ctx = NULL;
27 static struct blob_buf b;
28 static const char *ubus_path;
29
30 /* global object */
31
32 static int
33 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
34                       struct ubus_request_data *req, const char *method,
35                       struct blob_attr *msg)
36 {
37         netifd_restart();
38         return 0;
39 }
40
41 static int
42 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
43                      struct ubus_request_data *req, const char *method,
44                      struct blob_attr *msg)
45 {
46         netifd_reload();
47         return 0;
48 }
49
50 enum {
51         HR_TARGET,
52         HR_V6,
53         HR_INTERFACE,
54         __HR_MAX
55 };
56
57 static const struct blobmsg_policy route_policy[__HR_MAX] = {
58         [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
59         [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
60         [HR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
61 };
62
63 static int
64 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
65                       struct ubus_request_data *req, const char *method,
66                       struct blob_attr *msg)
67 {
68         struct blob_attr *tb[__HR_MAX];
69         struct interface *iface = NULL;
70         union if_addr a;
71         bool v6 = false;
72
73         blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
74         if (!tb[HR_TARGET])
75                 return UBUS_STATUS_INVALID_ARGUMENT;
76
77         if (tb[HR_V6])
78                 v6 = blobmsg_get_bool(tb[HR_V6]);
79
80         if (tb[HR_INTERFACE])
81                 iface = vlist_find(&interfaces, blobmsg_data(tb[HR_INTERFACE]), iface, node);
82
83         memset(&a, 0, sizeof(a));
84         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
85                 return UBUS_STATUS_INVALID_ARGUMENT;
86
87
88         iface = interface_ip_add_target_route(&a, v6, iface);
89         if (!iface)
90                 return UBUS_STATUS_NOT_FOUND;
91
92         blob_buf_init(&b, 0);
93         blobmsg_add_string(&b, "interface", iface->name);
94         ubus_send_reply(ctx, req, b.head);
95
96         return 0;
97 }
98
99 static int
100 netifd_get_proto_handlers(struct ubus_context *ctx, struct ubus_object *obj,
101                           struct ubus_request_data *req, const char *method,
102                           struct blob_attr *msg)
103 {
104         blob_buf_init(&b, 0);
105         proto_dump_handlers(&b);
106         ubus_send_reply(ctx, req, b.head);
107
108         return 0;
109 }
110
111 static struct ubus_method main_object_methods[] = {
112         { .name = "restart", .handler = netifd_handle_restart },
113         { .name = "reload", .handler = netifd_handle_reload },
114         UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
115         { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers },
116 };
117
118 static struct ubus_object_type main_object_type =
119         UBUS_OBJECT_TYPE("netifd", main_object_methods);
120
121 static struct ubus_object main_object = {
122         .name = "network",
123         .type = &main_object_type,
124         .methods = main_object_methods,
125         .n_methods = ARRAY_SIZE(main_object_methods),
126 };
127
128 enum {
129         DEV_NAME,
130         __DEV_MAX,
131 };
132
133 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
134         [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
135 };
136
137 static int
138 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
139                   struct ubus_request_data *req, const char *method,
140                   struct blob_attr *msg)
141 {
142         struct device *dev = NULL;
143         struct blob_attr *tb[__DEV_MAX];
144
145         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
146
147         if (tb[DEV_NAME]) {
148                 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
149                 if (!dev)
150                         return UBUS_STATUS_INVALID_ARGUMENT;
151         }
152
153         blob_buf_init(&b, 0);
154         device_dump_status(&b, dev);
155         ubus_send_reply(ctx, req, b.head);
156
157         return 0;
158 }
159
160 enum {
161         ALIAS_ATTR_ALIAS,
162         ALIAS_ATTR_DEV,
163         __ALIAS_ATTR_MAX,
164 };
165
166 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
167         [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
168         [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
169 };
170
171 static int
172 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
173                     struct ubus_request_data *req, const char *method,
174                     struct blob_attr *msg)
175 {
176         struct device *dev = NULL;
177         struct blob_attr *tb[__ALIAS_ATTR_MAX];
178         struct blob_attr *cur;
179         int rem;
180
181         blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
182
183         if (!tb[ALIAS_ATTR_ALIAS])
184                 return UBUS_STATUS_INVALID_ARGUMENT;
185
186         if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
187                 dev = device_get(blobmsg_data(cur), true);
188                 if (!dev)
189                         return UBUS_STATUS_NOT_FOUND;
190         }
191
192         blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
193                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
194                         goto error;
195
196                 if (!blobmsg_check_attr(cur, NULL))
197                         goto error;
198
199                 alias_notify_device(blobmsg_data(cur), dev);
200         }
201         return 0;
202
203 error:
204         device_free_unused(dev);
205         return UBUS_STATUS_INVALID_ARGUMENT;
206 }
207
208 enum {
209         DEV_STATE_NAME,
210         DEV_STATE_DEFER,
211         __DEV_STATE_MAX,
212 };
213
214 static const struct blobmsg_policy dev_state_policy[__DEV_STATE_MAX] = {
215         [DEV_STATE_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
216         [DEV_STATE_DEFER] = { .name = "defer", .type = BLOBMSG_TYPE_BOOL },
217 };
218
219 static int
220 netifd_handle_set_state(struct ubus_context *ctx, struct ubus_object *obj,
221                         struct ubus_request_data *req, const char *method,
222                         struct blob_attr *msg)
223 {
224         struct device *dev = NULL;
225         struct blob_attr *tb[__DEV_STATE_MAX];
226         struct blob_attr *cur;
227
228         blobmsg_parse(dev_state_policy, __DEV_STATE_MAX, tb, blob_data(msg), blob_len(msg));
229
230         cur = tb[DEV_STATE_NAME];
231         if (!cur)
232                 return UBUS_STATUS_INVALID_ARGUMENT;
233
234         dev = device_get(blobmsg_data(cur), false);
235         if (!dev)
236                 return UBUS_STATUS_NOT_FOUND;
237
238         cur = tb[DEV_STATE_DEFER];
239         if (cur)
240                 device_set_deferred(dev, !!blobmsg_get_u8(cur));
241
242         return 0;
243 }
244
245 static struct ubus_method dev_object_methods[] = {
246         UBUS_METHOD("status", netifd_dev_status, dev_policy),
247         UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
248         UBUS_METHOD("set_state", netifd_handle_set_state, dev_state_policy),
249 };
250
251 static struct ubus_object_type dev_object_type =
252         UBUS_OBJECT_TYPE("device", dev_object_methods);
253
254 static struct ubus_object dev_object = {
255         .name = "network.device",
256         .type = &dev_object_type,
257         .methods = dev_object_methods,
258         .n_methods = ARRAY_SIZE(dev_object_methods),
259 };
260
261 static void
262 netifd_ubus_add_fd(void)
263 {
264         ubus_add_uloop(ctx);
265         system_fd_set_cloexec(ctx->sock.fd);
266 }
267
268 static void
269 netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
270 {
271         static struct uloop_timeout retry = {
272                 .cb = netifd_ubus_reconnect_timer,
273         };
274         int t = 2;
275
276         if (ubus_reconnect(ctx, ubus_path) != 0) {
277                 DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
278                 uloop_timeout_set(&retry, t * 1000);
279                 return;
280         }
281
282         DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
283         netifd_ubus_add_fd();
284 }
285
286 static void
287 netifd_ubus_connection_lost(struct ubus_context *ctx)
288 {
289         netifd_ubus_reconnect_timer(NULL);
290 }
291
292 /* per-interface object */
293
294 static int
295 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
296                  struct ubus_request_data *req, const char *method,
297                  struct blob_attr *msg)
298 {
299         struct interface *iface;
300
301         iface = container_of(obj, struct interface, ubus);
302         interface_set_up(iface);
303
304         return 0;
305 }
306
307 static int
308 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
309                    struct ubus_request_data *req, const char *method,
310                    struct blob_attr *msg)
311 {
312         struct interface *iface;
313
314         iface = container_of(obj, struct interface, ubus);
315         interface_set_down(iface);
316
317         return 0;
318 }
319
320 static void
321 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
322 {
323         struct interface_error *error;
324         void *e, *e2, *e3;
325         int i;
326
327         e = blobmsg_open_array(b, "errors");
328         list_for_each_entry(error, &iface->errors, list) {
329                 e2 = blobmsg_open_table(b, NULL);
330
331                 blobmsg_add_string(b, "subsystem", error->subsystem);
332                 blobmsg_add_string(b, "code", error->code);
333                 if (error->data[0]) {
334                         e3 = blobmsg_open_array(b, "data");
335                         for (i = 0; error->data[i]; i++)
336                                 blobmsg_add_string(b, NULL, error->data[i]);
337                         blobmsg_close_array(b, e3);
338                 }
339
340                 blobmsg_close_table(b, e2);
341         }
342         blobmsg_close_array(b, e);
343 }
344
345 static void
346 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6,
347                                bool enabled)
348 {
349         struct device_addr *addr;
350         char *buf;
351         void *a;
352         int buflen = 128;
353         int af;
354
355         time_t now = system_get_rtime();
356         vlist_for_each_element(&ip->addr, addr, node) {
357                 if (addr->enabled != enabled)
358                         continue;
359
360                 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
361                         af = AF_INET;
362                 else
363                         af = AF_INET6;
364
365                 if (af != (v6 ? AF_INET6 : AF_INET))
366                         continue;
367
368                 a = blobmsg_open_table(&b, NULL);
369
370                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
371                 inet_ntop(af, &addr->addr, buf, buflen);
372                 blobmsg_add_string_buffer(&b);
373
374                 blobmsg_add_u32(&b, "mask", addr->mask);
375
376                 if (addr->preferred_until) {
377                         int preferred = addr->preferred_until - now;
378                         if (preferred < 0)
379                                 preferred = 0;
380                         blobmsg_add_u32(&b, "preferred", preferred);
381                 }
382
383                 if (addr->valid_until)
384                         blobmsg_add_u32(&b, "valid", addr->valid_until - now);
385
386                 blobmsg_close_table(&b, a);
387         }
388 }
389
390 static void
391 interface_ip_dump_route_list(struct interface_ip_settings *ip, bool enabled)
392 {
393         struct device_route *route;
394         int buflen = 128;
395         char *buf;
396         void *r;
397         int af;
398
399         time_t now = system_get_rtime();
400         vlist_for_each_element(&ip->route, route, node) {
401                 if (route->enabled != enabled)
402                         continue;
403
404                 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
405                         af = AF_INET;
406                 else
407                         af = AF_INET6;
408
409                 r = blobmsg_open_table(&b, NULL);
410
411                 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
412                 inet_ntop(af, &route->addr, buf, buflen);
413                 blobmsg_add_string_buffer(&b);
414
415                 blobmsg_add_u32(&b, "mask", route->mask);
416
417                 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
418                 inet_ntop(af, &route->nexthop, buf, buflen);
419                 blobmsg_add_string_buffer(&b);
420
421                 if (route->flags & DEVROUTE_MTU)
422                         blobmsg_add_u32(&b, "mtu", route->mtu);
423
424                 if (route->flags & DEVROUTE_METRIC)
425                         blobmsg_add_u32(&b, "metric", route->metric);
426
427                 if (route->flags & DEVROUTE_TABLE)
428                         blobmsg_add_u32(&b, "table", route->table);
429
430                 if (route->valid_until)
431                         blobmsg_add_u32(&b, "valid", route->valid_until - now);
432
433                 blobmsg_close_table(&b, r);
434         }
435 }
436
437
438 static void
439 interface_ip_dump_prefix_list(struct interface_ip_settings *ip)
440 {
441         struct device_prefix *prefix;
442         char *buf;
443         void *a, *c;
444         const int buflen = INET6_ADDRSTRLEN;
445
446         time_t now = system_get_rtime();
447         vlist_for_each_element(&ip->prefix, prefix, node) {
448                 a = blobmsg_open_table(&b, NULL);
449
450                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
451                 inet_ntop(AF_INET6, &prefix->addr, buf, buflen);
452                 blobmsg_add_string_buffer(&b);
453
454                 blobmsg_add_u32(&b, "mask", prefix->length);
455
456                 if (prefix->preferred_until) {
457                         int preferred = prefix->preferred_until - now;
458                         if (preferred < 0)
459                                 preferred = 0;
460                         blobmsg_add_u32(&b, "preferred", preferred);
461                 }
462
463                 if (prefix->valid_until)
464                         blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
465
466                 blobmsg_add_string(&b, "class", prefix->pclass);
467
468                 c = blobmsg_open_table(&b, "assigned");
469                 struct device_prefix_assignment *assign;
470                 list_for_each_entry(assign, &prefix->assignments, head) {
471                         if (!assign->name[0])
472                                 continue;
473
474                         struct in6_addr addr = prefix->addr;
475                         addr.s6_addr32[1] |= htonl(assign->assigned);
476
477                         void *d = blobmsg_open_table(&b, assign->name);
478
479                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
480                         inet_ntop(AF_INET6, &addr, buf, buflen);
481                         blobmsg_add_string_buffer(&b);
482
483                         blobmsg_add_u32(&b, "mask", assign->length);
484
485                         blobmsg_close_table(&b, d);
486                 }
487                 blobmsg_close_table(&b, c);
488
489                 blobmsg_close_table(&b, a);
490         }
491 }
492
493
494 static void
495 interface_ip_dump_prefix_assignment_list(struct interface *iface)
496 {
497         void *a;
498         char *buf;
499         const int buflen = INET6_ADDRSTRLEN;
500         time_t now = system_get_rtime();
501
502         struct device_prefix *prefix;
503         list_for_each_entry(prefix, &prefixes, head) {
504                 struct device_prefix_assignment *assign;
505                 list_for_each_entry(assign, &prefix->assignments, head) {
506                         if (strcmp(assign->name, iface->name))
507                                 continue;
508
509                         struct in6_addr addr = prefix->addr;
510                         addr.s6_addr32[1] |= htonl(assign->assigned);
511
512                         a = blobmsg_open_table(&b, NULL);
513
514                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
515                         inet_ntop(AF_INET6, &addr, buf, buflen);
516                         blobmsg_add_string_buffer(&b);
517
518                         blobmsg_add_u32(&b, "mask", assign->length);
519
520                         if (prefix->preferred_until) {
521                                 int preferred = prefix->preferred_until - now;
522                                 if (preferred < 0)
523                                         preferred = 0;
524                                 blobmsg_add_u32(&b, "preferred", preferred);
525                         }
526
527                         if (prefix->valid_until)
528                                 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
529
530                         blobmsg_close_table(&b, a);
531                 }
532         }
533 }
534
535
536 static void
537 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
538                                   bool enabled)
539 {
540         struct dns_server *dns;
541         int buflen = 128;
542         char *buf;
543
544         vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
545                 if (ip->no_dns == enabled)
546                         continue;
547
548                 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
549                 inet_ntop(dns->af, &dns->addr, buf, buflen);
550                 blobmsg_add_string_buffer(&b);
551         }
552 }
553
554 static void
555 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
556                                   bool enabled)
557 {
558         struct dns_search_domain *dns;
559
560         vlist_simple_for_each_element(&ip->dns_search, dns, node) {
561                 if (ip->no_dns == enabled)
562                         continue;
563
564                 blobmsg_add_string(&b, NULL, dns->name);
565         }
566 }
567
568 static void
569 netifd_dump_status(struct interface *iface)
570 {
571         struct interface_data *data;
572         struct device *dev;
573         void *a, *inactive;
574
575         blobmsg_add_string(&b, "name", iface->name);
576         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
577         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
578         blobmsg_add_u8(&b, "available", iface->available);
579         blobmsg_add_u8(&b, "autostart", iface->autostart);
580
581         if (iface->state == IFS_UP) {
582                 time_t cur = system_get_rtime();
583                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
584                 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
585         }
586
587         if (iface->proto_handler)
588                 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
589
590         dev = iface->main_dev.dev;
591         if (dev && !dev->hidden &&
592             !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
593                 blobmsg_add_string(&b, "device", dev->ifname);
594
595         if (iface->state == IFS_UP) {
596                 blobmsg_add_u32(&b, "metric", iface->metric);
597                 a = blobmsg_open_array(&b, "ipv4-address");
598                 interface_ip_dump_address_list(&iface->config_ip, false, true);
599                 interface_ip_dump_address_list(&iface->proto_ip, false, true);
600                 blobmsg_close_array(&b, a);
601                 a = blobmsg_open_array(&b, "ipv6-address");
602                 interface_ip_dump_address_list(&iface->config_ip, true, true);
603                 interface_ip_dump_address_list(&iface->proto_ip, true, true);
604                 blobmsg_close_array(&b, a);
605                 a = blobmsg_open_array(&b, "ipv6-prefix");
606                 interface_ip_dump_prefix_list(&iface->config_ip);
607                 interface_ip_dump_prefix_list(&iface->proto_ip);
608                 blobmsg_close_array(&b, a);
609                 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
610                 interface_ip_dump_prefix_assignment_list(iface);
611                 blobmsg_close_array(&b, a);
612                 a = blobmsg_open_array(&b, "route");
613                 interface_ip_dump_route_list(&iface->config_ip, true);
614                 interface_ip_dump_route_list(&iface->proto_ip, true);
615                 blobmsg_close_array(&b, a);
616                 a = blobmsg_open_array(&b, "dns-server");
617                 interface_ip_dump_dns_server_list(&iface->config_ip, true);
618                 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
619                 blobmsg_close_array(&b, a);
620                 a = blobmsg_open_array(&b, "dns-search");
621                 interface_ip_dump_dns_search_list(&iface->config_ip, true);
622                 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
623                 blobmsg_close_array(&b, a);
624
625                 inactive = blobmsg_open_table(&b, "inactive");
626                 a = blobmsg_open_array(&b, "ipv4-address");
627                 interface_ip_dump_address_list(&iface->config_ip, false, false);
628                 interface_ip_dump_address_list(&iface->proto_ip, false, false);
629                 blobmsg_close_array(&b, a);
630                 a = blobmsg_open_array(&b, "ipv6-address");
631                 interface_ip_dump_address_list(&iface->config_ip, true, false);
632                 interface_ip_dump_address_list(&iface->proto_ip, true, false);
633                 blobmsg_close_array(&b, a);
634                 a = blobmsg_open_array(&b, "route");
635                 interface_ip_dump_route_list(&iface->config_ip, false);
636                 interface_ip_dump_route_list(&iface->proto_ip, false);
637                 blobmsg_close_array(&b, a);
638                 a = blobmsg_open_array(&b, "dns-server");
639                 interface_ip_dump_dns_server_list(&iface->config_ip, false);
640                 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
641                 blobmsg_close_array(&b, a);
642                 a = blobmsg_open_array(&b, "dns-search");
643                 interface_ip_dump_dns_search_list(&iface->config_ip, false);
644                 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
645                 blobmsg_close_array(&b, a);
646                 blobmsg_close_table(&b, inactive);
647         }
648
649         a = blobmsg_open_table(&b, "data");
650         avl_for_each_element(&iface->data, data, node)
651                 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
652
653         blobmsg_close_table(&b, a);
654
655         if (!list_is_empty(&iface->errors))
656                 netifd_add_interface_errors(&b, iface);
657 }
658
659 static int
660 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
661                      struct ubus_request_data *req, const char *method,
662                      struct blob_attr *msg)
663 {
664         struct interface *iface = container_of(obj, struct interface, ubus);
665
666         blob_buf_init(&b, 0);
667         netifd_dump_status(iface);
668         ubus_send_reply(ctx, req, b.head);
669
670         return 0;
671 }
672
673 static int
674 netifd_handle_dump(struct ubus_context *ctx, struct ubus_object *obj,
675                      struct ubus_request_data *req, const char *method,
676                      struct blob_attr *msg)
677 {
678         blob_buf_init(&b, 0);
679         void *a = blobmsg_open_array(&b, "interface");
680
681         struct interface *iface;
682         vlist_for_each_element(&interfaces, iface, node) {
683                 void *i = blobmsg_open_table(&b, NULL);
684                 netifd_dump_status(iface);
685                 blobmsg_close_table(&b, i);
686         }
687
688         blobmsg_close_array(&b, a);
689         ubus_send_reply(ctx, req, b.head);
690
691         return 0;
692 }
693
694 static int
695 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
696                            struct ubus_request_data *req, const char *method,
697                            struct blob_attr *msg)
698 {
699         struct blob_attr *tb[__DEV_MAX];
700         struct interface *iface;
701         struct device *dev;
702         bool add = !strncmp(method, "add", 3);
703         int ret;
704
705         iface = container_of(obj, struct interface, ubus);
706
707         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
708
709         if (!tb[DEV_NAME])
710                 return UBUS_STATUS_INVALID_ARGUMENT;
711
712         device_lock();
713
714         dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
715         if (!dev) {
716                 ret = UBUS_STATUS_NOT_FOUND;
717                 goto out;
718         }
719
720         if (add) {
721                 device_set_present(dev, true);
722                 if (iface->device_config)
723                         device_set_config(dev, &simple_device_type, iface->config);
724
725                 system_if_apply_settings(dev, &dev->settings);
726                 ret = interface_add_link(iface, dev);
727         } else {
728                 ret = interface_remove_link(iface, dev);
729         }
730
731 out:
732         device_unlock();
733
734         return ret;
735 }
736
737
738 static int
739 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
740                           struct ubus_request_data *req, const char *method,
741                           struct blob_attr *msg)
742 {
743         struct interface *iface;
744
745         iface = container_of(obj, struct interface, ubus);
746
747         if (!iface->proto || !iface->proto->notify)
748                 return UBUS_STATUS_NOT_SUPPORTED;
749
750         return iface->proto->notify(iface->proto, msg);
751 }
752
753 static void
754 netifd_iface_do_remove(struct uloop_timeout *timeout)
755 {
756         struct interface *iface;
757
758         iface = container_of(timeout, struct interface, remove_timer);
759         vlist_delete(&interfaces, &iface->node);
760 }
761
762 static int
763 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
764                     struct ubus_request_data *req, const char *method,
765                     struct blob_attr *msg)
766 {
767         struct interface *iface;
768
769         iface = container_of(obj, struct interface, ubus);
770         if (iface->remove_timer.cb)
771                 return UBUS_STATUS_INVALID_ARGUMENT;
772
773         iface->remove_timer.cb = netifd_iface_do_remove;
774         uloop_timeout_set(&iface->remove_timer, 100);
775         return 0;
776 }
777
778 static int
779 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
780                             struct ubus_request_data *req, const char *method,
781                             struct blob_attr *msg)
782 {
783         struct interface *iface;
784         struct device *dev;
785         const struct device_hotplug_ops *ops;
786
787         iface = container_of(obj, struct interface, ubus);
788         dev = iface->main_dev.dev;
789         if (!dev)
790                 return 0;
791
792         ops = dev->hotplug_ops;
793         if (!ops)
794                 return 0;
795
796         return ops->prepare(dev);
797 }
798
799 static int
800 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
801                        struct ubus_request_data *req, const char *method,
802                        struct blob_attr *msg)
803 {
804         struct interface *iface;
805         struct blob_attr *cur;
806         int rem, ret;
807
808         iface = container_of(obj, struct interface, ubus);
809
810         blob_for_each_attr(cur, msg, rem) {
811                 ret = interface_add_data(iface, cur);
812                 if (ret)
813                         return ret;
814         }
815
816         return 0;
817 }
818
819 static struct ubus_method iface_object_methods[] = {
820         { .name = "up", .handler = netifd_handle_up },
821         { .name = "down", .handler = netifd_handle_down },
822         { .name = "status", .handler = netifd_handle_status },
823         { .name = "prepare", .handler = netifd_handle_iface_prepare },
824         { .name = "dump", .handler = netifd_handle_dump },
825         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
826         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
827         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
828         { .name = "remove", .handler = netifd_iface_remove },
829         { .name = "set_data", .handler = netifd_handle_set_data },
830 };
831
832 static struct ubus_object_type iface_object_type =
833         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
834
835
836 static struct ubus_object iface_object = {
837         .name = "network.interface",
838         .type = &iface_object_type,
839         .n_methods = ARRAY_SIZE(iface_object_methods),
840 };
841
842 static void netifd_add_object(struct ubus_object *obj)
843 {
844         int ret = ubus_add_object(ctx, obj);
845
846         if (ret != 0)
847                 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
848 }
849
850 static const struct blobmsg_policy iface_policy = {
851         .name = "interface",
852         .type = BLOBMSG_TYPE_STRING,
853 };
854
855 static int
856 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
857                     struct ubus_request_data *req, const char *method,
858                     struct blob_attr *msg)
859 {
860         struct interface *iface;
861         struct blob_attr *tb;
862         int i;
863
864         blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
865         if (!tb)
866                 return UBUS_STATUS_INVALID_ARGUMENT;
867
868         iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
869         if (!iface)
870                 return UBUS_STATUS_NOT_FOUND;
871
872         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
873                 ubus_handler_t cb;
874
875                 if (strcmp(method, iface_object_methods[i].name) != 0)
876                         continue;
877
878                 cb = iface_object_methods[i].handler;
879                 return cb(ctx, &iface->ubus, req, method, msg);
880         }
881
882         return UBUS_STATUS_INVALID_ARGUMENT;
883 }
884
885 static void netifd_add_iface_object(void)
886 {
887         struct ubus_method *methods;
888         int i;
889
890         methods = calloc(1, sizeof(iface_object_methods));
891         memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
892         iface_object.methods = methods;
893
894         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
895                 if (methods[i].handler == netifd_handle_dump)
896                         continue;
897
898                 methods[i].handler = netifd_handle_iface;
899                 methods[i].policy = &iface_policy;
900                 methods[i].n_policy = 1;
901         }
902         netifd_add_object(&iface_object);
903 }
904
905 int
906 netifd_ubus_init(const char *path)
907 {
908         uloop_init();
909         ubus_path = path;
910
911         ctx = ubus_connect(path);
912         if (!ctx)
913                 return -EIO;
914
915         DPRINTF("connected as %08x\n", ctx->local_id);
916         ctx->connection_lost = netifd_ubus_connection_lost;
917         netifd_ubus_add_fd();
918
919         netifd_add_object(&main_object);
920         netifd_add_object(&dev_object);
921         netifd_add_iface_object();
922
923         return 0;
924 }
925
926 void
927 netifd_ubus_done(void)
928 {
929         ubus_free(ctx);
930 }
931
932 void
933 netifd_ubus_interface_event(struct interface *iface, bool up)
934 {
935         blob_buf_init(&b, 0);
936         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
937         blobmsg_add_string(&b, "interface", iface->name);
938         ubus_send_event(ctx, "network.interface", b.head);
939 }
940
941 void
942 netifd_ubus_add_interface(struct interface *iface)
943 {
944         struct ubus_object *obj = &iface->ubus;
945         char *name = NULL;
946
947         if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
948                 return;
949
950         obj->name = name;
951         obj->type = &iface_object_type;
952         obj->methods = iface_object_methods;
953         obj->n_methods = ARRAY_SIZE(iface_object_methods);
954         if (ubus_add_object(ctx, &iface->ubus)) {
955                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
956                 free(name);
957                 obj->name = NULL;
958         }
959 }
960
961 void
962 netifd_ubus_remove_interface(struct interface *iface)
963 {
964         if (!iface->ubus.name)
965                 return;
966
967         ubus_remove_object(ctx, &iface->ubus);
968         free((void *) iface->ubus.name);
969 }