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