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