reset iface->config_state when handling it
[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 struct netifd_fd ubus_fd;
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 struct ubus_method main_object_methods[] = {
95         { .name = "restart", .handler = netifd_handle_restart },
96         { .name = "reload", .handler = netifd_handle_reload },
97         UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
98 };
99
100 static struct ubus_object_type main_object_type =
101         UBUS_OBJECT_TYPE("netifd", main_object_methods);
102
103 static struct ubus_object main_object = {
104         .name = "network",
105         .type = &main_object_type,
106         .methods = main_object_methods,
107         .n_methods = ARRAY_SIZE(main_object_methods),
108 };
109
110 enum {
111         DEV_NAME,
112         __DEV_MAX,
113 };
114
115 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
116         [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
117 };
118
119 static int
120 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
121                   struct ubus_request_data *req, const char *method,
122                   struct blob_attr *msg)
123 {
124         struct device *dev = NULL;
125         struct blob_attr *tb[__DEV_MAX];
126
127         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
128
129         if (tb[DEV_NAME]) {
130                 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
131                 if (!dev)
132                         return UBUS_STATUS_INVALID_ARGUMENT;
133         }
134
135         blob_buf_init(&b, 0);
136         device_dump_status(&b, dev);
137         ubus_send_reply(ctx, req, b.head);
138
139         return 0;
140 }
141
142 enum {
143         ALIAS_ATTR_ALIAS,
144         ALIAS_ATTR_DEV,
145         __ALIAS_ATTR_MAX,
146 };
147
148 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
149         [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
150         [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
151 };
152
153 static int
154 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
155                     struct ubus_request_data *req, const char *method,
156                     struct blob_attr *msg)
157 {
158         struct device *dev = NULL;
159         struct blob_attr *tb[__ALIAS_ATTR_MAX];
160         struct blob_attr *cur;
161         int rem;
162
163         blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
164
165         if (!tb[ALIAS_ATTR_ALIAS])
166                 return UBUS_STATUS_INVALID_ARGUMENT;
167
168         if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
169                 dev = device_get(blobmsg_data(cur), true);
170                 if (!dev)
171                         return UBUS_STATUS_NOT_FOUND;
172         }
173
174         blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
175                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
176                         goto error;
177
178                 if (!blobmsg_check_attr(cur, NULL))
179                         goto error;
180
181                 alias_notify_device(blobmsg_data(cur), dev);
182         }
183         return 0;
184
185 error:
186         device_free_unused(dev);
187         return UBUS_STATUS_INVALID_ARGUMENT;
188 }
189
190 static struct ubus_method dev_object_methods[] = {
191         UBUS_METHOD("status", netifd_dev_status, dev_policy),
192         UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
193 };
194
195 static struct ubus_object_type dev_object_type =
196         UBUS_OBJECT_TYPE("device", dev_object_methods);
197
198 static struct ubus_object dev_object = {
199         .name = "network.device",
200         .type = &dev_object_type,
201         .methods = dev_object_methods,
202         .n_methods = ARRAY_SIZE(dev_object_methods),
203 };
204
205 int
206 netifd_ubus_init(const char *path)
207 {
208         int ret;
209
210         ctx = ubus_connect(path);
211         if (!ctx)
212                 return -EIO;
213
214         DPRINTF("connected as %08x\n", ctx->local_id);
215         uloop_init();
216         ubus_add_uloop(ctx);
217         ubus_fd.fd = ctx->sock.fd;
218         netifd_fd_add(&ubus_fd);
219
220         ret = ubus_add_object(ctx, &main_object);
221         if (ret)
222                 goto out;
223
224         ret = ubus_add_object(ctx, &dev_object);
225
226 out:
227         if (ret != 0)
228                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
229         return ret;
230 }
231
232 void
233 netifd_ubus_done(void)
234 {
235         ubus_free(ctx);
236 }
237
238
239 /* per-interface object */
240
241 static int
242 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
243                  struct ubus_request_data *req, const char *method,
244                  struct blob_attr *msg)
245 {
246         struct interface *iface;
247
248         iface = container_of(obj, struct interface, ubus);
249         interface_set_up(iface);
250
251         return 0;
252 }
253
254 static int
255 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
256                    struct ubus_request_data *req, const char *method,
257                    struct blob_attr *msg)
258 {
259         struct interface *iface;
260
261         iface = container_of(obj, struct interface, ubus);
262         interface_set_down(iface);
263
264         return 0;
265 }
266
267 static void
268 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
269 {
270         struct interface_error *error;
271         void *e, *e2, *e3;
272         int i;
273
274         e = blobmsg_open_array(b, "errors");
275         list_for_each_entry(error, &iface->errors, list) {
276                 e2 = blobmsg_open_table(b, NULL);
277
278                 blobmsg_add_string(b, "subsystem", error->subsystem);
279                 blobmsg_add_string(b, "code", error->code);
280                 if (error->data[0]) {
281                         e3 = blobmsg_open_array(b, "data");
282                         for (i = 0; error->data[i]; i++)
283                                 blobmsg_add_string(b, NULL, error->data[i]);
284                         blobmsg_close_array(b, e3);
285                 }
286
287                 blobmsg_close_table(b, e2);
288         }
289         blobmsg_close_array(b, e);
290 }
291
292 static void
293 interface_ip_dump_address_list(struct interface_ip_settings *ip, bool v6)
294 {
295         struct device_addr *addr;
296         char *buf;
297         void *a;
298         int buflen = 128;
299         int af;
300
301         vlist_for_each_element(&ip->addr, addr, node) {
302                 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
303                         af = AF_INET;
304                 else
305                         af = AF_INET6;
306
307                 if (af != (v6 ? AF_INET6 : AF_INET))
308                         continue;
309
310                 a = blobmsg_open_table(&b, NULL);
311
312                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
313                 inet_ntop(af, &addr->addr, buf, buflen);
314                 blobmsg_add_string_buffer(&b);
315
316                 blobmsg_add_u32(&b, "mask", addr->mask);
317
318                 blobmsg_close_table(&b, a);
319         }
320 }
321
322 static void
323 interface_ip_dump_route_list(struct interface_ip_settings *ip)
324 {
325         struct device_route *route;
326         int buflen = 128;
327         char *buf;
328         void *r;
329         int af;
330
331         vlist_for_each_element(&ip->route, route, node) {
332                 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
333                         af = AF_INET;
334                 else
335                         af = AF_INET6;
336
337                 r = blobmsg_open_table(&b, NULL);
338
339                 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
340                 inet_ntop(af, &route->addr, buf, buflen);
341                 blobmsg_add_string_buffer(&b);
342
343                 blobmsg_add_u32(&b, "mask", route->mask);
344
345                 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
346                 inet_ntop(af, &route->nexthop, buf, buflen);
347                 blobmsg_add_string_buffer(&b);
348
349                 blobmsg_close_table(&b, r);
350         }
351 }
352
353 static void
354 interface_ip_dump_dns_server_list(struct interface_ip_settings *ip)
355 {
356         struct dns_server *dns;
357         int buflen = 128;
358         char *buf;
359
360         vlist_simple_for_each_element(&ip->dns_servers, dns, node) {
361                 buf = blobmsg_alloc_string_buffer(&b, NULL, buflen);
362                 inet_ntop(dns->af, &dns->addr, buf, buflen);
363                 blobmsg_add_string_buffer(&b);
364         }
365 }
366
367 static void
368 interface_ip_dump_dns_search_list(struct interface_ip_settings *ip)
369 {
370         struct dns_search_domain *dns;
371
372         vlist_simple_for_each_element(&ip->dns_search, dns, node) {
373                 blobmsg_add_string(&b, NULL, dns->name);
374         }
375 }
376
377 static int
378 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
379                      struct ubus_request_data *req, const char *method,
380                      struct blob_attr *msg)
381 {
382         struct interface *iface;
383         struct interface_data *data;
384         struct device *dev;
385         void *a;
386
387         iface = container_of(obj, struct interface, ubus);
388
389         blob_buf_init(&b, 0);
390         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
391         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
392         blobmsg_add_u8(&b, "available", iface->available);
393         blobmsg_add_u8(&b, "autostart", iface->autostart);
394
395         if (iface->state == IFS_UP) {
396                 time_t cur = system_get_rtime();
397                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
398                 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
399         }
400
401         dev = iface->main_dev.dev;
402         if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
403                 blobmsg_add_string(&b, "device", dev->ifname);
404
405         if (iface->state == IFS_UP) {
406                 a = blobmsg_open_array(&b, "ipv4-address");
407                 interface_ip_dump_address_list(&iface->config_ip, false);
408                 interface_ip_dump_address_list(&iface->proto_ip, false);
409                 blobmsg_close_array(&b, a);
410                 a = blobmsg_open_array(&b, "ipv6-address");
411                 interface_ip_dump_address_list(&iface->config_ip, true);
412                 interface_ip_dump_address_list(&iface->proto_ip, true);
413                 blobmsg_close_array(&b, a);
414                 a = blobmsg_open_array(&b, "route");
415                 interface_ip_dump_route_list(&iface->config_ip);
416                 interface_ip_dump_route_list(&iface->proto_ip);
417                 blobmsg_close_array(&b, a);
418                 a = blobmsg_open_array(&b, "dns-server");
419                 interface_ip_dump_dns_server_list(&iface->config_ip);
420                 interface_ip_dump_dns_server_list(&iface->proto_ip);
421                 blobmsg_close_array(&b, a);
422                 a = blobmsg_open_array(&b, "dns-search");
423                 interface_ip_dump_dns_search_list(&iface->config_ip);
424                 interface_ip_dump_dns_search_list(&iface->proto_ip);
425                 blobmsg_close_array(&b, a);
426         }
427
428         a = blobmsg_open_table(&b, "data");
429         avl_for_each_element(&iface->data, data, node)
430                 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
431
432         blobmsg_close_table(&b, a);
433
434         if (!list_is_empty(&iface->errors))
435                 netifd_add_interface_errors(&b, iface);
436
437         ubus_send_reply(ctx, req, b.head);
438
439         return 0;
440 }
441
442 static int
443 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
444                            struct ubus_request_data *req, const char *method,
445                            struct blob_attr *msg)
446 {
447         struct blob_attr *tb[__DEV_MAX];
448         struct interface *iface;
449         struct device *dev;
450         bool add = !strncmp(method, "add", 3);
451         int ret;
452
453         iface = container_of(obj, struct interface, ubus);
454
455         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
456
457         if (!tb[DEV_NAME])
458                 return UBUS_STATUS_INVALID_ARGUMENT;
459
460         device_lock();
461
462         dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
463         if (add && !dev)
464                 return UBUS_STATUS_NOT_FOUND;
465
466         if (add)
467                 return interface_add_link(iface, dev);
468         else
469                 return interface_remove_link(iface, dev);
470
471         device_unlock();
472
473         return ret;
474 }
475
476
477 static int
478 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
479                           struct ubus_request_data *req, const char *method,
480                           struct blob_attr *msg)
481 {
482         struct interface *iface;
483
484         iface = container_of(obj, struct interface, ubus);
485
486         if (!iface->proto || !iface->proto->notify)
487                 return UBUS_STATUS_NOT_SUPPORTED;
488
489         return iface->proto->notify(iface->proto, msg);
490 }
491
492 static void
493 netifd_iface_do_remove(struct uloop_timeout *timeout)
494 {
495         struct interface *iface;
496
497         iface = container_of(timeout, struct interface, remove_timer);
498         vlist_delete(&interfaces, &iface->node);
499 }
500
501 static int
502 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
503                     struct ubus_request_data *req, const char *method,
504                     struct blob_attr *msg)
505 {
506         struct interface *iface;
507
508         iface = container_of(obj, struct interface, ubus);
509         if (iface->remove_timer.cb)
510                 return UBUS_STATUS_INVALID_ARGUMENT;
511
512         iface->remove_timer.cb = netifd_iface_do_remove;
513         uloop_timeout_set(&iface->remove_timer, 100);
514         return 0;
515 }
516
517 static int
518 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
519                             struct ubus_request_data *req, const char *method,
520                             struct blob_attr *msg)
521 {
522         struct interface *iface;
523         struct device *dev;
524         const struct device_hotplug_ops *ops;
525
526         iface = container_of(obj, struct interface, ubus);
527         dev = iface->main_dev.dev;
528         if (!dev)
529                 return 0;
530
531         ops = dev->hotplug_ops;
532         if (!ops)
533                 return 0;
534
535         return ops->prepare(dev);
536 }
537
538 static int
539 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
540                        struct ubus_request_data *req, const char *method,
541                        struct blob_attr *msg)
542 {
543         struct interface *iface;
544         struct blob_attr *cur;
545         int rem, ret;
546
547         iface = container_of(obj, struct interface, ubus);
548
549         blob_for_each_attr(cur, msg, rem) {
550                 ret = interface_add_data(iface, cur);
551                 if (ret)
552                         return ret;
553         }
554
555         return 0;
556 }
557
558 static struct ubus_method iface_object_methods[] = {
559         { .name = "up", .handler = netifd_handle_up },
560         { .name = "down", .handler = netifd_handle_down },
561         { .name = "status", .handler = netifd_handle_status },
562         { .name = "prepare", .handler = netifd_handle_iface_prepare },
563         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
564         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
565         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
566         { .name = "remove", .handler = netifd_iface_remove },
567         { .name = "set_data", .handler = netifd_handle_set_data },
568 };
569
570 static struct ubus_object_type iface_object_type =
571         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
572
573
574 void
575 netifd_ubus_interface_event(struct interface *iface, bool up)
576 {
577         blob_buf_init(&b, 0);
578         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
579         blobmsg_add_string(&b, "interface", iface->name);
580         ubus_send_event(ctx, "network.interface", b.head);
581 }
582
583 void
584 netifd_ubus_add_interface(struct interface *iface)
585 {
586         struct ubus_object *obj = &iface->ubus;
587         char *name = NULL;
588
589         asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
590         if (!name)
591                 return;
592
593         obj->name = name;
594         obj->type = &iface_object_type;
595         obj->methods = iface_object_methods;
596         obj->n_methods = ARRAY_SIZE(iface_object_methods);
597         if (ubus_add_object(ctx, &iface->ubus)) {
598                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
599                 free(name);
600                 obj->name = NULL;
601         }
602 }
603
604 void
605 netifd_ubus_remove_interface(struct interface *iface)
606 {
607         if (!iface->ubus.name)
608                 return;
609
610         ubus_remove_object(ctx, &iface->ubus);
611         free((void *) iface->ubus.name);
612 }