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