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