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