774f9d1e6befd5801f8a93baaeefa1640167cda1
[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                 c = blobmsg_open_table(&b, "assigned");
467                 struct device_prefix_assignment *assign;
468                 list_for_each_entry(assign, &prefix->assignments, head) {
469                         if (!assign->name[0])
470                                 continue;
471
472                         struct in6_addr addr = prefix->addr;
473                         addr.s6_addr32[1] |= htonl(assign->assigned);
474
475                         void *d = blobmsg_open_table(&b, assign->name);
476
477                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
478                         inet_ntop(AF_INET6, &addr, buf, buflen);
479                         blobmsg_add_string_buffer(&b);
480
481                         blobmsg_add_u32(&b, "mask", assign->length);
482
483                         blobmsg_close_table(&b, d);
484                 }
485                 blobmsg_close_table(&b, c);
486
487                 blobmsg_close_table(&b, a);
488         }
489 }
490
491
492 static void
493 interface_ip_dump_prefix_assignment_list(struct interface *iface)
494 {
495         void *a;
496         char *buf;
497         const int buflen = INET6_ADDRSTRLEN;
498         time_t now = system_get_rtime();
499
500         struct device_prefix *prefix;
501         list_for_each_entry(prefix, &prefixes, head) {
502                 struct device_prefix_assignment *assign;
503                 list_for_each_entry(assign, &prefix->assignments, head) {
504                         if (strcmp(assign->name, iface->name))
505                                 continue;
506
507                         struct in6_addr addr = prefix->addr;
508                         addr.s6_addr32[1] |= htonl(assign->assigned);
509
510                         a = blobmsg_open_table(&b, NULL);
511
512                         buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
513                         inet_ntop(AF_INET6, &addr, buf, buflen);
514                         blobmsg_add_string_buffer(&b);
515
516                         blobmsg_add_u32(&b, "mask", assign->length);
517
518                         if (prefix->preferred_until) {
519                                 int preferred = prefix->preferred_until - now;
520                                 if (preferred < 0)
521                                         preferred = 0;
522                                 blobmsg_add_u32(&b, "preferred", preferred);
523                         }
524
525                         if (prefix->valid_until)
526                                 blobmsg_add_u32(&b, "valid", prefix->valid_until - now);
527
528                         blobmsg_close_table(&b, a);
529                 }
530         }
531 }
532
533
534 static void
535 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip,
536                                   bool enabled)
537 {
538         struct dns_server *dns;
539         int buflen = 128;
540         char *buf;
541
542         vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
543                 if (ip->no_dns == enabled)
544                         continue;
545
546                 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
547                 inet_ntop(dns->af, &dns->addr, buf, buflen);
548                 blobmsg_add_string_buffer(&b);
549         }
550 }
551
552 static void
553 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip,
554                                   bool enabled)
555 {
556         struct dns_search_domain *dns;
557
558         vlist_simple_for_each_element(&ip->dns_search, dns, node) {
559                 if (ip->no_dns == enabled)
560                         continue;
561
562                 blobmsg_add_string(&b, NULL, dns->name);
563         }
564 }
565
566 static int
567 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
568                      struct ubus_request_data *req, const char *method,
569                      struct blob_attr *msg)
570 {
571         struct interface *iface;
572         struct interface_data *data;
573         struct device *dev;
574         void *a, *inactive;
575
576         iface = container_of(obj, struct interface, ubus);
577
578         blob_buf_init(&b, 0);
579         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
580         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
581         blobmsg_add_u8(&b, "available", iface->available);
582         blobmsg_add_u8(&b, "autostart", iface->autostart);
583
584         if (iface->state == IFS_UP) {
585                 time_t cur = system_get_rtime();
586                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
587                 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
588         }
589
590         if (iface->proto_handler)
591                 blobmsg_add_string(&b, "proto", iface->proto_handler->name);
592
593         dev = iface->main_dev.dev;
594         if (dev && !dev->hidden &&
595             !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
596                 blobmsg_add_string(&b, "device", dev->ifname);
597
598         if (iface->state == IFS_UP) {
599                 blobmsg_add_u32(&b, "metric", iface->metric);
600                 a = blobmsg_open_array(&b, "ipv4-address");
601                 interface_ip_dump_address_list(&iface->config_ip, false, true);
602                 interface_ip_dump_address_list(&iface->proto_ip, false, true);
603                 blobmsg_close_array(&b, a);
604                 a = blobmsg_open_array(&b, "ipv6-address");
605                 interface_ip_dump_address_list(&iface->config_ip, true, true);
606                 interface_ip_dump_address_list(&iface->proto_ip, true, true);
607                 blobmsg_close_array(&b, a);
608                 a = blobmsg_open_array(&b, "ipv6-prefix");
609                 interface_ip_dump_prefix_list(&iface->config_ip);
610                 interface_ip_dump_prefix_list(&iface->proto_ip);
611                 blobmsg_close_array(&b, a);
612                 a = blobmsg_open_array(&b, "ipv6-prefix-assignment");
613                 interface_ip_dump_prefix_assignment_list(iface);
614                 blobmsg_close_array(&b, a);
615                 a = blobmsg_open_array(&b, "route");
616                 interface_ip_dump_route_list(&iface->config_ip, true);
617                 interface_ip_dump_route_list(&iface->proto_ip, true);
618                 blobmsg_close_array(&b, a);
619                 a = blobmsg_open_array(&b, "dns-server");
620                 interface_ip_dump_dns_server_list(&iface->config_ip, true);
621                 interface_ip_dump_dns_server_list(&iface->proto_ip, true);
622                 blobmsg_close_array(&b, a);
623                 a = blobmsg_open_array(&b, "dns-search");
624                 interface_ip_dump_dns_search_list(&iface->config_ip, true);
625                 interface_ip_dump_dns_search_list(&iface->proto_ip, true);
626                 blobmsg_close_array(&b, a);
627
628                 inactive = blobmsg_open_table(&b, "inactive");
629                 a = blobmsg_open_array(&b, "ipv4-address");
630                 interface_ip_dump_address_list(&iface->config_ip, false, false);
631                 interface_ip_dump_address_list(&iface->proto_ip, false, false);
632                 blobmsg_close_array(&b, a);
633                 a = blobmsg_open_array(&b, "ipv6-address");
634                 interface_ip_dump_address_list(&iface->config_ip, true, false);
635                 interface_ip_dump_address_list(&iface->proto_ip, true, false);
636                 blobmsg_close_array(&b, a);
637                 a = blobmsg_open_array(&b, "route");
638                 interface_ip_dump_route_list(&iface->config_ip, false);
639                 interface_ip_dump_route_list(&iface->proto_ip, false);
640                 blobmsg_close_array(&b, a);
641                 a = blobmsg_open_array(&b, "dns-server");
642                 interface_ip_dump_dns_server_list(&iface->config_ip, false);
643                 interface_ip_dump_dns_server_list(&iface->proto_ip, false);
644                 blobmsg_close_array(&b, a);
645                 a = blobmsg_open_array(&b, "dns-search");
646                 interface_ip_dump_dns_search_list(&iface->config_ip, false);
647                 interface_ip_dump_dns_search_list(&iface->proto_ip, false);
648                 blobmsg_close_array(&b, a);
649                 blobmsg_close_table(&b, inactive);
650         }
651
652         a = blobmsg_open_table(&b, "data");
653         avl_for_each_element(&iface->data, data, node)
654                 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
655
656         blobmsg_close_table(&b, a);
657
658         if (!list_is_empty(&iface->errors))
659                 netifd_add_interface_errors(&b, iface);
660
661         ubus_send_reply(ctx, req, b.head);
662
663         return 0;
664 }
665
666 static int
667 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
668                            struct ubus_request_data *req, const char *method,
669                            struct blob_attr *msg)
670 {
671         struct blob_attr *tb[__DEV_MAX];
672         struct interface *iface;
673         struct device *dev;
674         bool add = !strncmp(method, "add", 3);
675         int ret;
676
677         iface = container_of(obj, struct interface, ubus);
678
679         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
680
681         if (!tb[DEV_NAME])
682                 return UBUS_STATUS_INVALID_ARGUMENT;
683
684         device_lock();
685
686         dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
687         if (!dev) {
688                 ret = UBUS_STATUS_NOT_FOUND;
689                 goto out;
690         }
691
692         if (add) {
693                 device_set_present(dev, true);
694                 if (iface->device_config)
695                         device_set_config(dev, &simple_device_type, iface->config);
696
697                 system_if_apply_settings(dev, &dev->settings);
698                 ret = interface_add_link(iface, dev);
699         } else {
700                 ret = interface_remove_link(iface, dev);
701         }
702
703 out:
704         device_unlock();
705
706         return ret;
707 }
708
709
710 static int
711 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
712                           struct ubus_request_data *req, const char *method,
713                           struct blob_attr *msg)
714 {
715         struct interface *iface;
716
717         iface = container_of(obj, struct interface, ubus);
718
719         if (!iface->proto || !iface->proto->notify)
720                 return UBUS_STATUS_NOT_SUPPORTED;
721
722         return iface->proto->notify(iface->proto, msg);
723 }
724
725 static void
726 netifd_iface_do_remove(struct uloop_timeout *timeout)
727 {
728         struct interface *iface;
729
730         iface = container_of(timeout, struct interface, remove_timer);
731         vlist_delete(&interfaces, &iface->node);
732 }
733
734 static int
735 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
736                     struct ubus_request_data *req, const char *method,
737                     struct blob_attr *msg)
738 {
739         struct interface *iface;
740
741         iface = container_of(obj, struct interface, ubus);
742         if (iface->remove_timer.cb)
743                 return UBUS_STATUS_INVALID_ARGUMENT;
744
745         iface->remove_timer.cb = netifd_iface_do_remove;
746         uloop_timeout_set(&iface->remove_timer, 100);
747         return 0;
748 }
749
750 static int
751 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
752                             struct ubus_request_data *req, const char *method,
753                             struct blob_attr *msg)
754 {
755         struct interface *iface;
756         struct device *dev;
757         const struct device_hotplug_ops *ops;
758
759         iface = container_of(obj, struct interface, ubus);
760         dev = iface->main_dev.dev;
761         if (!dev)
762                 return 0;
763
764         ops = dev->hotplug_ops;
765         if (!ops)
766                 return 0;
767
768         return ops->prepare(dev);
769 }
770
771 static int
772 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
773                        struct ubus_request_data *req, const char *method,
774                        struct blob_attr *msg)
775 {
776         struct interface *iface;
777         struct blob_attr *cur;
778         int rem, ret;
779
780         iface = container_of(obj, struct interface, ubus);
781
782         blob_for_each_attr(cur, msg, rem) {
783                 ret = interface_add_data(iface, cur);
784                 if (ret)
785                         return ret;
786         }
787
788         return 0;
789 }
790
791 static struct ubus_method iface_object_methods[] = {
792         { .name = "up", .handler = netifd_handle_up },
793         { .name = "down", .handler = netifd_handle_down },
794         { .name = "status", .handler = netifd_handle_status },
795         { .name = "prepare", .handler = netifd_handle_iface_prepare },
796         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
797         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
798         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
799         { .name = "remove", .handler = netifd_iface_remove },
800         { .name = "set_data", .handler = netifd_handle_set_data },
801 };
802
803 static struct ubus_object_type iface_object_type =
804         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
805
806
807 static struct ubus_object iface_object = {
808         .name = "network.interface",
809         .type = &iface_object_type,
810         .n_methods = ARRAY_SIZE(iface_object_methods),
811 };
812
813 static void netifd_add_object(struct ubus_object *obj)
814 {
815         int ret = ubus_add_object(ctx, obj);
816
817         if (ret != 0)
818                 fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
819 }
820
821 static const struct blobmsg_policy iface_policy = {
822         .name = "interface",
823         .type = BLOBMSG_TYPE_STRING,
824 };
825
826 static int
827 netifd_handle_iface(struct ubus_context *ctx, struct ubus_object *obj,
828                     struct ubus_request_data *req, const char *method,
829                     struct blob_attr *msg)
830 {
831         struct interface *iface;
832         struct blob_attr *tb;
833         int i;
834
835         blobmsg_parse(&iface_policy, 1, &tb, blob_data(msg), blob_len(msg));
836         if (!tb)
837                 return UBUS_STATUS_INVALID_ARGUMENT;
838
839         iface = vlist_find(&interfaces, blobmsg_data(tb), iface, node);
840         if (!iface)
841                 return UBUS_STATUS_NOT_FOUND;
842
843         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
844                 ubus_handler_t cb;
845
846                 if (strcmp(method, iface_object_methods[i].name) != 0)
847                         continue;
848
849                 cb = iface_object_methods[i].handler;
850                 return cb(ctx, &iface->ubus, req, method, msg);
851         }
852
853         return UBUS_STATUS_INVALID_ARGUMENT;
854 }
855
856 static void netifd_add_iface_object(void)
857 {
858         struct ubus_method *methods;
859         int i;
860
861         methods = calloc(1, sizeof(iface_object_methods));
862         memcpy(methods, iface_object_methods, sizeof(iface_object_methods));
863         iface_object.methods = methods;
864
865         for (i = 0; i < ARRAY_SIZE(iface_object_methods); i++) {
866                 methods[i].handler = netifd_handle_iface;
867                 methods[i].policy = &iface_policy;
868                 methods[i].n_policy = 1;
869         }
870         netifd_add_object(&iface_object);
871 }
872
873 int
874 netifd_ubus_init(const char *path)
875 {
876         uloop_init();
877         ubus_path = path;
878
879         ctx = ubus_connect(path);
880         if (!ctx)
881                 return -EIO;
882
883         DPRINTF("connected as %08x\n", ctx->local_id);
884         ctx->connection_lost = netifd_ubus_connection_lost;
885         netifd_ubus_add_fd();
886
887         netifd_add_object(&main_object);
888         netifd_add_object(&dev_object);
889         netifd_add_iface_object();
890
891         return 0;
892 }
893
894 void
895 netifd_ubus_done(void)
896 {
897         ubus_free(ctx);
898 }
899
900 void
901 netifd_ubus_interface_event(struct interface *iface, bool up)
902 {
903         blob_buf_init(&b, 0);
904         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
905         blobmsg_add_string(&b, "interface", iface->name);
906         ubus_send_event(ctx, "network.interface", b.head);
907 }
908
909 void
910 netifd_ubus_add_interface(struct interface *iface)
911 {
912         struct ubus_object *obj = &iface->ubus;
913         char *name = NULL;
914
915         if (asprintf(&name, "%s.interface.%s", main_object.name, iface->name) == -1)
916                 return;
917
918         obj->name = name;
919         obj->type = &iface_object_type;
920         obj->methods = iface_object_methods;
921         obj->n_methods = ARRAY_SIZE(iface_object_methods);
922         if (ubus_add_object(ctx, &iface->ubus)) {
923                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
924                 free(name);
925                 obj->name = NULL;
926         }
927 }
928
929 void
930 netifd_ubus_remove_interface(struct interface *iface)
931 {
932         if (!iface->ubus.name)
933                 return;
934
935         ubus_remove_object(ctx, &iface->ubus);
936         free((void *) iface->ubus.name);
937 }