clear errors before attempting to bring up an interface
[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 static struct ubus_method dev_object_methods[] = {
84 UBUS_METHOD("status", netifd_dev_status, dev_policy)
85 };
86
87 static struct ubus_object_type dev_object_type =
88 UBUS_OBJECT_TYPE("device", dev_object_methods);
89
90 static struct ubus_object dev_object = {
91 .name = "network.device",
92 .type = &dev_object_type,
93 .methods = dev_object_methods,
94 .n_methods = ARRAY_SIZE(dev_object_methods),
95 };
96
97 int
98 netifd_ubus_init(const char *path)
99 {
100 int ret;
101
102 ctx = ubus_connect(path);
103 if (!ctx)
104 return -EIO;
105
106 DPRINTF("connected as %08x\n", ctx->local_id);
107 uloop_init();
108 ubus_add_uloop(ctx);
109 ubus_fd.fd = ctx->sock.fd;
110 netifd_fd_add(&ubus_fd);
111
112 ret = ubus_add_object(ctx, &main_object);
113 if (ret)
114 goto out;
115
116 ret = ubus_add_object(ctx, &dev_object);
117
118 out:
119 if (ret != 0)
120 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
121 return ret;
122 }
123
124 void
125 netifd_ubus_done(void)
126 {
127 ubus_free(ctx);
128 }
129
130
131 /* per-interface object */
132
133 static int
134 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method,
136 struct blob_attr *msg)
137 {
138 struct interface *iface;
139
140 iface = container_of(obj, struct interface, ubus);
141 interface_set_up(iface);
142
143 return 0;
144 }
145
146 static int
147 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
148 struct ubus_request_data *req, const char *method,
149 struct blob_attr *msg)
150 {
151 struct interface *iface;
152
153 iface = container_of(obj, struct interface, ubus);
154 interface_set_down(iface);
155
156 return 0;
157 }
158
159 static void
160 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
161 {
162 struct interface_error *error;
163 void *e, *e2, *e3;
164 int i;
165
166 e = blobmsg_open_array(b, "errors");
167 list_for_each_entry(error, &iface->errors, list) {
168 e2 = blobmsg_open_table(b, NULL);
169
170 blobmsg_add_string(b, "subsystem", error->subsystem);
171 blobmsg_add_string(b, "code", error->code);
172 if (error->data[0]) {
173 e3 = blobmsg_open_array(b, "data");
174 for (i = 0; error->data[i]; i++)
175 blobmsg_add_string(b, NULL, error->data[i]);
176 blobmsg_close_array(b, e3);
177 }
178
179 blobmsg_close_table(b, e2);
180 }
181 blobmsg_close_array(b, e);
182 }
183
184 static int
185 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
186 struct ubus_request_data *req, const char *method,
187 struct blob_attr *msg)
188 {
189 struct interface *iface;
190
191 iface = container_of(obj, struct interface, ubus);
192
193 blob_buf_init(&b, 0);
194 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
195 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
196 blobmsg_add_u8(&b, "available", iface->available);
197 blobmsg_add_u8(&b, "autostart", iface->autostart);
198
199 if (iface->state == IFS_UP) {
200 time_t cur = system_get_rtime();
201 blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
202 }
203
204 if (iface->main_dev.dev) {
205 struct device *dev = iface->main_dev.dev;
206 const char *field;
207 void *devinfo;
208
209 /* use a different field for virtual devices */
210 if (dev->avl.key)
211 field = "device";
212 else
213 field = "link";
214
215 devinfo = blobmsg_open_table(&b, field);
216 blobmsg_add_string(&b, "name", dev->ifname);
217
218 blobmsg_close_table(&b, devinfo);
219 }
220
221 if (!list_is_empty(&iface->errors))
222 netifd_add_interface_errors(&b, iface);
223
224 ubus_send_reply(ctx, req, b.head);
225
226 return 0;
227 }
228
229 static int
230 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
231 struct ubus_request_data *req, const char *method,
232 struct blob_attr *msg)
233 {
234 struct interface *iface;
235 struct device *dev, *main_dev;
236 struct blob_attr *tb[__DEV_MAX];
237 bool add = !strncmp(method, "add", 3);
238 int ret;
239
240 iface = container_of(obj, struct interface, ubus);
241
242 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
243
244 if (!tb[DEV_NAME])
245 return UBUS_STATUS_INVALID_ARGUMENT;
246
247 main_dev = iface->main_dev.dev;
248 if (!main_dev)
249 return UBUS_STATUS_NOT_FOUND;
250
251 if (!main_dev->hotplug_ops)
252 return UBUS_STATUS_NOT_SUPPORTED;
253
254 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
255 if (!dev)
256 return UBUS_STATUS_NOT_FOUND;
257
258 if (main_dev != dev) {
259 if (add)
260 ret = main_dev->hotplug_ops->add(main_dev, dev);
261 else
262 ret = main_dev->hotplug_ops->del(main_dev, dev);
263 if (ret)
264 ret = UBUS_STATUS_UNKNOWN_ERROR;
265 } else {
266 ret = UBUS_STATUS_INVALID_ARGUMENT;
267 }
268
269 if (add)
270 device_free_unused(dev);
271
272 return ret;
273 }
274
275
276 static int
277 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
278 struct ubus_request_data *req, const char *method,
279 struct blob_attr *msg)
280 {
281 struct interface *iface;
282
283 iface = container_of(obj, struct interface, ubus);
284
285 if (!iface->proto || !iface->proto->notify)
286 return UBUS_STATUS_NOT_SUPPORTED;
287
288 return iface->proto->notify(iface->proto, msg);
289 }
290
291 static void
292 netifd_iface_do_remove(struct uloop_timeout *timeout)
293 {
294 struct interface *iface;
295
296 iface = container_of(timeout, struct interface, remove_timer);
297 vlist_delete(&interfaces, &iface->node);
298 }
299
300 static int
301 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
302 struct ubus_request_data *req, const char *method,
303 struct blob_attr *msg)
304 {
305 struct interface *iface;
306
307 iface = container_of(obj, struct interface, ubus);
308 if (iface->remove_timer.cb)
309 return UBUS_STATUS_INVALID_ARGUMENT;
310
311 iface->remove_timer.cb = netifd_iface_do_remove;
312 uloop_timeout_set(&iface->remove_timer, 100);
313 return 0;
314 }
315
316 static struct ubus_method iface_object_methods[] = {
317 { .name = "up", .handler = netifd_handle_up },
318 { .name = "down", .handler = netifd_handle_down },
319 { .name = "status", .handler = netifd_handle_status },
320 { .name = "add_device", .handler = netifd_iface_handle_device,
321 .policy = dev_policy, .n_policy = __DEV_MAX },
322 { .name = "remove_device", .handler = netifd_iface_handle_device,
323 .policy = dev_policy, .n_policy = __DEV_MAX },
324 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
325 { .name = "remove", .handler = netifd_iface_remove }
326 };
327
328 static struct ubus_object_type iface_object_type =
329 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
330
331
332 void
333 netifd_ubus_interface_event(struct interface *iface, bool up)
334 {
335 blob_buf_init(&b, 0);
336 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
337 blobmsg_add_string(&b, "interface", iface->name);
338 ubus_send_event(ctx, "network.interface", b.head);
339 }
340
341 void
342 netifd_ubus_add_interface(struct interface *iface)
343 {
344 struct ubus_object *obj = &iface->ubus;
345 char *name = NULL;
346
347 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
348 if (!name)
349 return;
350
351 obj->name = name;
352 obj->type = &iface_object_type;
353 obj->methods = iface_object_methods;
354 obj->n_methods = ARRAY_SIZE(iface_object_methods);
355 if (ubus_add_object(ctx, &iface->ubus)) {
356 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
357 free(name);
358 obj->name = NULL;
359 }
360 }
361
362 void
363 netifd_ubus_remove_interface(struct interface *iface)
364 {
365 if (!iface->ubus.name)
366 return;
367
368 ubus_remove_object(ctx, &iface->ubus);
369 free((void *) iface->ubus.name);
370 }