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