display device and l3_device separately in ubus status
[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 blobmsg_add_string(&b, "l3_device", iface->l3_dev->dev->ifname);
203 }
204
205 if (!(iface->proto_handler->flags & PROTO_FLAG_NODEV))
206 blobmsg_add_string(&b, "device", iface->main_dev.dev->ifname);
207
208 if (!list_is_empty(&iface->errors))
209 netifd_add_interface_errors(&b, iface);
210
211 ubus_send_reply(ctx, req, b.head);
212
213 return 0;
214 }
215
216 static int
217 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
218 struct ubus_request_data *req, const char *method,
219 struct blob_attr *msg)
220 {
221 struct interface *iface;
222 struct device *dev, *main_dev;
223 struct blob_attr *tb[__DEV_MAX];
224 bool add = !strncmp(method, "add", 3);
225 int ret;
226
227 iface = container_of(obj, struct interface, ubus);
228
229 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
230
231 if (!tb[DEV_NAME])
232 return UBUS_STATUS_INVALID_ARGUMENT;
233
234 main_dev = iface->main_dev.dev;
235 if (!main_dev)
236 return UBUS_STATUS_NOT_FOUND;
237
238 if (!main_dev->hotplug_ops)
239 return UBUS_STATUS_NOT_SUPPORTED;
240
241 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
242 if (!dev)
243 return UBUS_STATUS_NOT_FOUND;
244
245 if (main_dev != dev) {
246 if (add)
247 ret = main_dev->hotplug_ops->add(main_dev, dev);
248 else
249 ret = main_dev->hotplug_ops->del(main_dev, dev);
250 if (ret)
251 ret = UBUS_STATUS_UNKNOWN_ERROR;
252 } else {
253 ret = UBUS_STATUS_INVALID_ARGUMENT;
254 }
255
256 if (add)
257 device_free_unused(dev);
258
259 return ret;
260 }
261
262
263 static int
264 netifd_iface_notify_proto(struct ubus_context *ctx, struct ubus_object *obj,
265 struct ubus_request_data *req, const char *method,
266 struct blob_attr *msg)
267 {
268 struct interface *iface;
269
270 iface = container_of(obj, struct interface, ubus);
271
272 if (!iface->proto || !iface->proto->notify)
273 return UBUS_STATUS_NOT_SUPPORTED;
274
275 return iface->proto->notify(iface->proto, msg);
276 }
277
278 static void
279 netifd_iface_do_remove(struct uloop_timeout *timeout)
280 {
281 struct interface *iface;
282
283 iface = container_of(timeout, struct interface, remove_timer);
284 vlist_delete(&interfaces, &iface->node);
285 }
286
287 static int
288 netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj,
289 struct ubus_request_data *req, const char *method,
290 struct blob_attr *msg)
291 {
292 struct interface *iface;
293
294 iface = container_of(obj, struct interface, ubus);
295 if (iface->remove_timer.cb)
296 return UBUS_STATUS_INVALID_ARGUMENT;
297
298 iface->remove_timer.cb = netifd_iface_do_remove;
299 uloop_timeout_set(&iface->remove_timer, 100);
300 return 0;
301 }
302
303 static struct ubus_method iface_object_methods[] = {
304 { .name = "up", .handler = netifd_handle_up },
305 { .name = "down", .handler = netifd_handle_down },
306 { .name = "status", .handler = netifd_handle_status },
307 { .name = "add_device", .handler = netifd_iface_handle_device,
308 .policy = dev_policy, .n_policy = __DEV_MAX },
309 { .name = "remove_device", .handler = netifd_iface_handle_device,
310 .policy = dev_policy, .n_policy = __DEV_MAX },
311 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
312 { .name = "remove", .handler = netifd_iface_remove }
313 };
314
315 static struct ubus_object_type iface_object_type =
316 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
317
318
319 void
320 netifd_ubus_interface_event(struct interface *iface, bool up)
321 {
322 blob_buf_init(&b, 0);
323 blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
324 blobmsg_add_string(&b, "interface", iface->name);
325 ubus_send_event(ctx, "network.interface", b.head);
326 }
327
328 void
329 netifd_ubus_add_interface(struct interface *iface)
330 {
331 struct ubus_object *obj = &iface->ubus;
332 char *name = NULL;
333
334 asprintf(&name, "%s.interface.%s", main_object.name, iface->name);
335 if (!name)
336 return;
337
338 obj->name = name;
339 obj->type = &iface_object_type;
340 obj->methods = iface_object_methods;
341 obj->n_methods = ARRAY_SIZE(iface_object_methods);
342 if (ubus_add_object(ctx, &iface->ubus)) {
343 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
344 free(name);
345 obj->name = NULL;
346 }
347 }
348
349 void
350 netifd_ubus_remove_interface(struct interface *iface)
351 {
352 if (!iface->ubus.name)
353 return;
354
355 ubus_remove_object(ctx, &iface->ubus);
356 free((void *) iface->ubus.name);
357 }