add a new event for interface reload
[project/netifd.git] / ubus.c
1 #define _GNU_SOURCE
2
3 #include <arpa/inet.h>
4 #include <string.h>
5 #include <stdio.h>
6
7 #include "netifd.h"
8 #include "interface.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "system.h"
12
13 static struct ubus_context *ctx = NULL;
14 static struct blob_buf b;
15 static struct netifd_fd ubus_fd;
16
17 /* global object */
18
19 static int
20 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
21                       struct ubus_request_data *req, const char *method,
22                       struct blob_attr *msg)
23 {
24         netifd_restart();
25         return 0;
26 }
27
28 static int
29 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
30                      struct ubus_request_data *req, const char *method,
31                      struct blob_attr *msg)
32 {
33         netifd_reload();
34         return 0;
35 }
36
37 enum {
38         HR_TARGET,
39         HR_V6,
40         __HR_MAX
41 };
42
43 static const struct blobmsg_policy route_policy[__HR_MAX] = {
44         [HR_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
45         [HR_V6] = { .name = "v6", .type = BLOBMSG_TYPE_BOOL },
46 };
47
48 static int
49 netifd_add_host_route(struct ubus_context *ctx, struct ubus_object *obj,
50                       struct ubus_request_data *req, const char *method,
51                       struct blob_attr *msg)
52 {
53         struct blob_attr *tb[__HR_MAX];
54         struct interface *iface;
55         union if_addr a;
56         bool v6 = false;
57
58         blobmsg_parse(route_policy, __HR_MAX, tb, blob_data(msg), blob_len(msg));
59         if (!tb[HR_TARGET])
60                 return UBUS_STATUS_INVALID_ARGUMENT;
61
62         if (tb[HR_V6])
63                 v6 = blobmsg_get_bool(tb[HR_V6]);
64
65         memset(&a, 0, sizeof(a));
66         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(tb[HR_TARGET]), &a))
67                 return UBUS_STATUS_INVALID_ARGUMENT;
68
69
70         iface = interface_ip_add_target_route(&a, v6);
71         if (!iface)
72                 return UBUS_STATUS_NOT_FOUND;
73
74         blob_buf_init(&b, 0);
75         blobmsg_add_string(&b, "interface", iface->name);
76         ubus_send_reply(ctx, req, b.head);
77
78         return 0;
79 }
80
81 static struct ubus_method main_object_methods[] = {
82         { .name = "restart", .handler = netifd_handle_restart },
83         { .name = "reload", .handler = netifd_handle_reload },
84         UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy),
85 };
86
87 static struct ubus_object_type main_object_type =
88         UBUS_OBJECT_TYPE("netifd", main_object_methods);
89
90 static struct ubus_object main_object = {
91         .name = "network",
92         .type = &main_object_type,
93         .methods = main_object_methods,
94         .n_methods = ARRAY_SIZE(main_object_methods),
95 };
96
97 enum {
98         DEV_NAME,
99         __DEV_MAX,
100 };
101
102 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
103         [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
104 };
105
106 static int
107 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
108                   struct ubus_request_data *req, const char *method,
109                   struct blob_attr *msg)
110 {
111         struct device *dev = NULL;
112         struct blob_attr *tb[__DEV_MAX];
113
114         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
115
116         if (tb[DEV_NAME]) {
117                 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
118                 if (!dev)
119                         return UBUS_STATUS_INVALID_ARGUMENT;
120         }
121
122         blob_buf_init(&b, 0);
123         device_dump_status(&b, dev);
124         ubus_send_reply(ctx, req, b.head);
125
126         return 0;
127 }
128
129 enum {
130         ALIAS_ATTR_ALIAS,
131         ALIAS_ATTR_DEV,
132         __ALIAS_ATTR_MAX,
133 };
134
135 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
136         [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
137         [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
138 };
139
140 static int
141 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
142                     struct ubus_request_data *req, const char *method,
143                     struct blob_attr *msg)
144 {
145         struct device *dev = NULL;
146         struct blob_attr *tb[__ALIAS_ATTR_MAX];
147         struct blob_attr *cur;
148         int rem;
149
150         blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
151
152         if (!tb[ALIAS_ATTR_ALIAS])
153                 return UBUS_STATUS_INVALID_ARGUMENT;
154
155         if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
156                 dev = device_get(blobmsg_data(cur), true);
157                 if (!dev)
158                         return UBUS_STATUS_NOT_FOUND;
159         }
160
161         blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
162                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
163                         goto error;
164
165                 if (!blobmsg_check_attr(cur, NULL))
166                         goto error;
167
168                 alias_notify_device(blobmsg_data(cur), dev);
169         }
170         return 0;
171
172 error:
173         device_free_unused(dev);
174         return UBUS_STATUS_INVALID_ARGUMENT;
175 }
176
177 static struct ubus_method dev_object_methods[] = {
178         UBUS_METHOD("status", netifd_dev_status, dev_policy),
179         UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
180 };
181
182 static struct ubus_object_type dev_object_type =
183         UBUS_OBJECT_TYPE("device", dev_object_methods);
184
185 static struct ubus_object dev_object = {
186         .name = "network.device",
187         .type = &dev_object_type,
188         .methods = dev_object_methods,
189         .n_methods = ARRAY_SIZE(dev_object_methods),
190 };
191
192 int
193 netifd_ubus_init(const char *path)
194 {
195         int ret;
196
197         ctx = ubus_connect(path);
198         if (!ctx)
199                 return -EIO;
200
201         DPRINTF("connected as %08x\n", ctx->local_id);
202         uloop_init();
203         ubus_add_uloop(ctx);
204         ubus_fd.fd = ctx->sock.fd;
205         netifd_fd_add(&ubus_fd);
206
207         ret = ubus_add_object(ctx, &main_object);
208         if (ret)
209                 goto out;
210
211         ret = ubus_add_object(ctx, &dev_object);
212
213 out:
214         if (ret != 0)
215                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
216         return ret;
217 }
218
219 void
220 netifd_ubus_done(void)
221 {
222         ubus_free(ctx);
223 }
224
225
226 /* per-interface object */
227
228 static int
229 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
230                  struct ubus_request_data *req, const char *method,
231                  struct blob_attr *msg)
232 {
233         struct interface *iface;
234
235         iface = container_of(obj, struct interface, ubus);
236         interface_set_up(iface);
237
238         return 0;
239 }
240
241 static int
242 netifd_handle_down(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_down(iface);
250
251         return 0;
252 }
253
254 static void
255 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
256 {
257         struct interface_error *error;
258         void *e, *e2, *e3;
259         int i;
260
261         e = blobmsg_open_array(b, "errors");
262         list_for_each_entry(error, &iface->errors, list) {
263                 e2 = blobmsg_open_table(b, NULL);
264
265                 blobmsg_add_string(b, "subsystem", error->subsystem);
266                 blobmsg_add_string(b, "code", error->code);
267                 if (error->data[0]) {
268                         e3 = blobmsg_open_array(b, "data");
269                         for (i = 0; error->data[i]; i++)
270                                 blobmsg_add_string(b, NULL, error->data[i]);
271                         blobmsg_close_array(b, e3);
272                 }
273
274                 blobmsg_close_table(b, e2);
275         }
276         blobmsg_close_array(b, e);
277 }
278
279 static void
280 interface_ip_dump_address_list(struct interface_ip_settings *ip)
281 {
282         struct device_addr *addr;
283         char *buf;
284         void *a;
285         int buflen = 128;
286         int af;
287
288         vlist_for_each_element(&ip->addr, addr, node) {
289                 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
290                         af = AF_INET;
291                 else
292                         af = AF_INET6;
293
294                 a = blobmsg_open_table(&b, NULL);
295
296                 buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
297                 inet_ntop(af, &addr->addr, buf, buflen);
298                 blobmsg_add_string_buffer(&b);
299
300                 blobmsg_add_u32(&b, "mask", addr->mask);
301
302                 blobmsg_close_table(&b, a);
303         }
304 }
305
306 static void
307 interface_ip_dump_route_list(struct interface_ip_settings *ip)
308 {
309         struct device_route *route;
310         static char *buf;
311         int buflen = 128;
312         void *r;
313         int af;
314
315         vlist_for_each_element(&ip->route, route, node) {
316                 if ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4)
317                         af = AF_INET;
318                 else
319                         af = AF_INET6;
320
321                 r = blobmsg_open_table(&b, NULL);
322
323                 buf = blobmsg_alloc_string_buffer(&b, "target", buflen);
324                 inet_ntop(af, &route->addr, buf, buflen);
325                 blobmsg_add_string_buffer(&b);
326
327                 blobmsg_add_u32(&b, "mask", route->mask);
328
329                 buf = blobmsg_alloc_string_buffer(&b, "nexthop", buflen);
330                 inet_ntop(af, &route->nexthop, buf, buflen);
331                 blobmsg_add_string_buffer(&b);
332
333                 blobmsg_close_table(&b, r);
334         }
335 }
336
337 static int
338 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
339                      struct ubus_request_data *req, const char *method,
340                      struct blob_attr *msg)
341 {
342         struct interface *iface;
343         struct interface_data *data;
344         struct device *dev;
345         void *a;
346
347         iface = container_of(obj, struct interface, ubus);
348
349         blob_buf_init(&b, 0);
350         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
351         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
352         blobmsg_add_u8(&b, "available", iface->available);
353         blobmsg_add_u8(&b, "autostart", iface->autostart);
354
355         if (iface->state == IFS_UP) {
356                 time_t cur = system_get_rtime();
357                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
358                 blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
359         }
360
361         dev = iface->main_dev.dev;
362         if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
363                 blobmsg_add_string(&b, "device", dev->ifname);
364
365         if (iface->state == IFS_UP) {
366                 a = blobmsg_open_array(&b, "address");
367                 interface_ip_dump_address_list(&iface->config_ip);
368                 interface_ip_dump_address_list(&iface->proto_ip);
369                 blobmsg_close_array(&b, a);
370                 a = blobmsg_open_array(&b, "route");
371                 interface_ip_dump_route_list(&iface->config_ip);
372                 interface_ip_dump_route_list(&iface->proto_ip);
373                 blobmsg_close_array(&b, a);
374         }
375
376         a = blobmsg_open_table(&b, "data");
377         avl_for_each_element(&iface->data, data, node)
378                 blob_put(&b, blob_id(data->data), blob_data(data->data), blob_len(data->data));
379
380         blobmsg_close_table(&b, a);
381
382         if (!list_is_empty(&iface->errors))
383                 netifd_add_interface_errors(&b, iface);
384
385         ubus_send_reply(ctx, req, b.head);
386
387         return 0;
388 }
389
390 static int
391 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
392                            struct ubus_request_data *req, const char *method,
393                            struct blob_attr *msg)
394 {
395         struct blob_attr *tb[__DEV_MAX];
396         struct interface *iface;
397         struct device *dev;
398         bool add = !strncmp(method, "add", 3);
399         int ret;
400
401         iface = container_of(obj, struct interface, ubus);
402
403         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
404
405         if (!tb[DEV_NAME])
406                 return UBUS_STATUS_INVALID_ARGUMENT;
407
408         device_lock();
409
410         dev = device_get(blobmsg_data(tb[DEV_NAME]), add ? 2 : 0);
411         if (add && !dev)
412                 return UBUS_STATUS_NOT_FOUND;
413
414         if (add)
415                 return interface_add_link(iface, dev);
416         else
417                 return interface_remove_link(iface, dev);
418
419         device_unlock();
420
421         return ret;
422 }
423
424
425 static int
426 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
427                           struct ubus_request_data *req, const char *method,
428                           struct blob_attr *msg)
429 {
430         struct interface *iface;
431
432         iface = container_of(obj, struct interface, ubus);
433
434         if (!iface->proto || !iface->proto->notify)
435                 return UBUS_STATUS_NOT_SUPPORTED;
436
437         return iface->proto->notify(iface->proto, msg);
438 }
439
440 static void
441 netifd_iface_do_remove(struct uloop_timeout *timeout)
442 {
443         struct interface *iface;
444
445         iface = container_of(timeout, struct interface, remove_timer);
446         vlist_delete(&interfaces, &iface->node);
447 }
448
449 static int
450 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
451                     struct ubus_request_data *req, const char *method,
452                     struct blob_attr *msg)
453 {
454         struct interface *iface;
455
456         iface = container_of(obj, struct interface, ubus);
457         if (iface->remove_timer.cb)
458                 return UBUS_STATUS_INVALID_ARGUMENT;
459
460         iface->remove_timer.cb = netifd_iface_do_remove;
461         uloop_timeout_set(&iface->remove_timer, 100);
462         return 0;
463 }
464
465 static int
466 netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj,
467                             struct ubus_request_data *req, const char *method,
468                             struct blob_attr *msg)
469 {
470         struct interface *iface;
471         struct device *dev;
472         const struct device_hotplug_ops *ops;
473
474         iface = container_of(obj, struct interface, ubus);
475         dev = iface->main_dev.dev;
476         if (!dev)
477                 return 0;
478
479         ops = dev->hotplug_ops;
480         if (!ops)
481                 return 0;
482
483         return ops->prepare(dev);
484 }
485
486 static int
487 netifd_handle_set_data(struct ubus_context *ctx, struct ubus_object *obj,
488                        struct ubus_request_data *req, const char *method,
489                        struct blob_attr *msg)
490 {
491         struct interface *iface;
492         struct blob_attr *cur;
493         int rem, ret;
494
495         iface = container_of(obj, struct interface, ubus);
496
497         blob_for_each_attr(cur, msg, rem) {
498                 ret = interface_add_data(iface, cur);
499                 if (ret)
500                         return ret;
501         }
502
503         return 0;
504 }
505
506 static struct ubus_method iface_object_methods[] = {
507         { .name = "up", .handler = netifd_handle_up },
508         { .name = "down", .handler = netifd_handle_down },
509         { .name = "status", .handler = netifd_handle_status },
510         { .name = "prepare", .handler = netifd_handle_iface_prepare },
511         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
512         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
513         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
514         { .name = "remove", .handler = netifd_iface_remove },
515         { .name = "set_data", .handler = netifd_handle_set_data },
516 };
517
518 static struct ubus_object_type iface_object_type =
519         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
520
521
522 void
523 netifd_ubus_interface_event(struct interface *iface, bool up)
524 {
525         blob_buf_init(&b, 0);
526         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
527         blobmsg_add_string(&b, "interface", iface->name);
528         ubus_send_event(ctx, "network.interface", b.head);
529 }
530
531 void
532 netifd_ubus_add_interface(struct interface *iface)
533 {
534         struct ubus_object *obj = &iface->ubus;
535         char *name = NULL;
536
537         asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
538         if (!name)
539                 return;
540
541         obj->name = name;
542         obj->type = &iface_object_type;
543         obj->methods = iface_object_methods;
544         obj->n_methods = ARRAY_SIZE(iface_object_methods);
545         if (ubus_add_object(ctx, &iface->ubus)) {
546                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
547                 free(name);
548                 obj->name = NULL;
549         }
550 }
551
552 void
553 netifd_ubus_remove_interface(struct interface *iface)
554 {
555         if (!iface->ubus.name)
556                 return;
557
558         ubus_remove_object(ctx, &iface->ubus);
559         free((void *) iface->ubus.name);
560 }