c729c5d3a40a2be09cbe32fd2550452540dc6889
[project/netifd.git] / ubus.c
1 #define _GNU_SOURCE
2
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "netifd.h"
7 #include "interface.h"
8 #include "proto.h"
9 #include "ubus.h"
10 #include "system.h"
11
12 static struct ubus_context *ctx = NULL;
13 static struct blob_buf b;
14 static struct netifd_fd ubus_fd;
15
16 /* global object */
17
18 static int
19 netifd_handle_restart(struct ubus_context *ctx, struct ubus_object *obj,
20                       struct ubus_request_data *req, const char *method,
21                       struct blob_attr *msg)
22 {
23         netifd_restart();
24         return 0;
25 }
26
27 static int
28 netifd_handle_reload(struct ubus_context *ctx, struct ubus_object *obj,
29                       struct ubus_request_data *req, const char *method,
30                       struct blob_attr *msg)
31 {
32         netifd_reload();
33         return 0;
34 }
35
36 static struct ubus_method main_object_methods[] = {
37         { .name = "restart", .handler = netifd_handle_restart },
38         { .name = "reload", .handler = netifd_handle_reload },
39 };
40
41 static struct ubus_object_type main_object_type =
42         UBUS_OBJECT_TYPE("netifd", main_object_methods);
43
44 static struct ubus_object main_object = {
45         .name = "network",
46         .type = &main_object_type,
47         .methods = main_object_methods,
48         .n_methods = ARRAY_SIZE(main_object_methods),
49 };
50
51 enum {
52         DEV_NAME,
53         __DEV_MAX,
54 };
55
56 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
57         [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
58 };
59
60 static int
61 netifd_dev_status(struct ubus_context *ctx, struct ubus_object *obj,
62                   struct ubus_request_data *req, const char *method,
63                   struct blob_attr *msg)
64 {
65         struct device *dev = NULL;
66         struct blob_attr *tb[__DEV_MAX];
67
68         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
69
70         if (tb[DEV_NAME]) {
71                 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
72                 if (!dev)
73                         return UBUS_STATUS_INVALID_ARGUMENT;
74         }
75
76         blob_buf_init(&b, 0);
77         device_dump_status(&b, dev);
78         ubus_send_reply(ctx, req, b.head);
79
80         return 0;
81 }
82
83 enum {
84         ALIAS_ATTR_ALIAS,
85         ALIAS_ATTR_DEV,
86         __ALIAS_ATTR_MAX,
87 };
88
89 static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
90         [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
91         [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
92 };
93
94 static int
95 netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
96                     struct ubus_request_data *req, const char *method,
97                     struct blob_attr *msg)
98 {
99         struct device *dev = NULL;
100         struct blob_attr *tb[__ALIAS_ATTR_MAX];
101         struct blob_attr *cur;
102         int rem;
103
104         blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
105
106         if (!tb[ALIAS_ATTR_ALIAS])
107                 return UBUS_STATUS_INVALID_ARGUMENT;
108
109         if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
110                 dev = device_get(blobmsg_data(cur), true);
111                 if (!dev)
112                         return UBUS_STATUS_NOT_FOUND;
113         }
114
115         blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
116                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
117                         goto error;
118
119                 if (!blobmsg_check_attr(cur, NULL))
120                         goto error;
121
122                 alias_notify_device(blobmsg_data(cur), dev);
123         }
124         return 0;
125
126 error:
127         device_free_unused(dev);
128         return UBUS_STATUS_INVALID_ARGUMENT;
129 }
130
131 static struct ubus_method dev_object_methods[] = {
132         UBUS_METHOD("status", netifd_dev_status, dev_policy),
133         UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
134 };
135
136 static struct ubus_object_type dev_object_type =
137         UBUS_OBJECT_TYPE("device", dev_object_methods);
138
139 static struct ubus_object dev_object = {
140         .name = "network.device",
141         .type = &dev_object_type,
142         .methods = dev_object_methods,
143         .n_methods = ARRAY_SIZE(dev_object_methods),
144 };
145
146 int
147 netifd_ubus_init(const char *path)
148 {
149         int ret;
150
151         ctx = ubus_connect(path);
152         if (!ctx)
153                 return -EIO;
154
155         DPRINTF("connected as %08x\n", ctx->local_id);
156         uloop_init();
157         ubus_add_uloop(ctx);
158         ubus_fd.fd = ctx->sock.fd;
159         netifd_fd_add(&ubus_fd);
160
161         ret = ubus_add_object(ctx, &main_object);
162         if (ret)
163                 goto out;
164
165         ret = ubus_add_object(ctx, &dev_object);
166
167 out:
168         if (ret != 0)
169                 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
170         return ret;
171 }
172
173 void
174 netifd_ubus_done(void)
175 {
176         ubus_free(ctx);
177 }
178
179
180 /* per-interface object */
181
182 static int
183 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
184                  struct ubus_request_data *req, const char *method,
185                  struct blob_attr *msg)
186 {
187         struct interface *iface;
188
189         iface = container_of(obj, struct interface, ubus);
190         interface_set_up(iface);
191
192         return 0;
193 }
194
195 static int
196 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
197                    struct ubus_request_data *req, const char *method,
198                    struct blob_attr *msg)
199 {
200         struct interface *iface;
201
202         iface = container_of(obj, struct interface, ubus);
203         interface_set_down(iface);
204
205         return 0;
206 }
207
208 static void
209 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
210 {
211         struct interface_error *error;
212         void *e, *e2, *e3;
213         int i;
214
215         e = blobmsg_open_array(b, "errors");
216         list_for_each_entry(error, &iface->errors, list) {
217                 e2 = blobmsg_open_table(b, NULL);
218
219                 blobmsg_add_string(b, "subsystem", error->subsystem);
220                 blobmsg_add_string(b, "code", error->code);
221                 if (error->data[0]) {
222                         e3 = blobmsg_open_array(b, "data");
223                         for (i = 0; error->data[i]; i++)
224                                 blobmsg_add_string(b, NULL, error->data[i]);
225                         blobmsg_close_array(b, e3);
226                 }
227
228                 blobmsg_close_table(b, e2);
229         }
230         blobmsg_close_array(b, e);
231 }
232
233 static int
234 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
235                      struct ubus_request_data *req, const char *method,
236                      struct blob_attr *msg)
237 {
238         struct interface *iface;
239         struct device *dev;
240
241         iface = container_of(obj, struct interface, ubus);
242
243         blob_buf_init(&b, 0);
244         blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
245         blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
246         blobmsg_add_u8(&b, "available", iface->available);
247         blobmsg_add_u8(&b, "autostart", iface->autostart);
248
249         if (iface->state == IFS_UP) {
250                 time_t cur = system_get_rtime();
251                 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
252                 blobmsg_add_string(&b, "l3_device", iface->l3_dev->dev->ifname);
253         }
254
255         dev = iface->main_dev.dev;
256         if (dev && !(iface->proto_handler->flags & PROTO_FLAG_NODEV))
257                 blobmsg_add_string(&b, "device", dev->ifname);
258
259         if (!list_is_empty(&iface->errors))
260                 netifd_add_interface_errors(&b, iface);
261
262         ubus_send_reply(ctx, req, b.head);
263
264         return 0;
265 }
266
267 static int
268 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
269                            struct ubus_request_data *req, const char *method,
270                            struct blob_attr *msg)
271 {
272         struct interface *iface;
273         struct device *dev, *main_dev = NULL;
274         struct blob_attr *tb[__DEV_MAX];
275         bool add = !strncmp(method, "add", 3);
276         const char *devname;
277         int ret;
278
279         iface = container_of(obj, struct interface, ubus);
280
281         blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
282
283         if (!tb[DEV_NAME])
284                 return UBUS_STATUS_INVALID_ARGUMENT;
285
286         devname = blobmsg_data(tb[DEV_NAME]);
287
288         device_lock();
289
290         if (iface->main_dev.hotplug) {
291                 dev = iface->main_dev.dev;
292
293                 if (dev) {
294                         if (!add && strcmp(dev->ifname, devname) != 0) {
295                                 ret = UBUS_STATUS_INVALID_ARGUMENT;
296                                 goto out;
297                         }
298
299                         interface_set_available(iface, false);
300                         device_remove_user(&iface->main_dev);
301                 }
302         } else
303                 main_dev = iface->main_dev.dev;
304
305         dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
306         if (!dev && (main_dev || add)) {
307                 ret = UBUS_STATUS_NOT_FOUND;
308                 goto out;
309         }
310
311         if (!main_dev) {
312                 if (add) {
313                         device_add_user(&iface->main_dev, dev);
314                         iface->main_dev.hotplug = true;
315                 }
316                 ret = 0;
317                 goto out;
318         }
319
320         if (!main_dev->hotplug_ops) {
321                 ret = UBUS_STATUS_NOT_SUPPORTED;
322                 goto out;
323         }
324
325         if (main_dev != dev) {
326                 if (add)
327                         ret = main_dev->hotplug_ops->add(main_dev, dev);
328                 else
329                         ret = main_dev->hotplug_ops->del(main_dev, dev);
330                 if (ret)
331                         ret = UBUS_STATUS_UNKNOWN_ERROR;
332         } else {
333                 ret = UBUS_STATUS_INVALID_ARGUMENT;
334         }
335
336 out:
337         device_unlock();
338
339         return ret;
340 }
341
342
343 static int
344 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
345                           struct ubus_request_data *req, const char *method,
346                           struct blob_attr *msg)
347 {
348         struct interface *iface;
349
350         iface = container_of(obj, struct interface, ubus);
351
352         if (!iface->proto || !iface->proto->notify)
353                 return UBUS_STATUS_NOT_SUPPORTED;
354
355         return iface->proto->notify(iface->proto, msg);
356 }
357
358 static void
359 netifd_iface_do_remove(struct uloop_timeout *timeout)
360 {
361         struct interface *iface;
362
363         iface = container_of(timeout, struct interface, remove_timer);
364         vlist_delete(&interfaces, &iface->node);
365 }
366
367 static int
368 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
369                     struct ubus_request_data *req, const char *method,
370                     struct blob_attr *msg)
371 {
372         struct interface *iface;
373
374         iface = container_of(obj, struct interface, ubus);
375         if (iface->remove_timer.cb)
376                 return UBUS_STATUS_INVALID_ARGUMENT;
377
378         iface->remove_timer.cb = netifd_iface_do_remove;
379         uloop_timeout_set(&iface->remove_timer, 100);
380         return 0;
381 }
382
383 static struct ubus_method iface_object_methods[] = {
384         { .name = "up", .handler = netifd_handle_up },
385         { .name = "down", .handler = netifd_handle_down },
386         { .name = "status", .handler = netifd_handle_status },
387         UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ),
388         UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ),
389         { .name = "notify_proto", .handler = netifd_iface_notify_proto },
390         { .name = "remove", .handler = netifd_iface_remove }
391 };
392
393 static struct ubus_object_type iface_object_type =
394         UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
395
396
397 void
398 netifd_ubus_interface_event(struct interface *iface, bool up)
399 {
400         blob_buf_init(&b, 0);
401         blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
402         blobmsg_add_string(&b, "interface", iface->name);
403         ubus_send_event(ctx, "network.interface", b.head);
404 }
405
406 void
407 netifd_ubus_add_interface(struct interface *iface)
408 {
409         struct ubus_object *obj = &iface->ubus;
410         char *name = NULL;
411
412         asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
413         if (!name)
414                 return;
415
416         obj->name = name;
417         obj->type = &iface_object_type;
418         obj->methods = iface_object_methods;
419         obj->n_methods = ARRAY_SIZE(iface_object_methods);
420         if (ubus_add_object(ctx, &iface->ubus)) {
421                 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
422                 free(name);
423                 obj->name = NULL;
424         }
425 }
426
427 void
428 netifd_ubus_remove_interface(struct interface *iface)
429 {
430         if (!iface->ubus.name)
431                 return;
432
433         ubus_remove_object(ctx, &iface->ubus);
434         free((void *) iface->ubus.name);
435 }