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