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