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