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