proto-shell: add l3 interface claim/release
[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 struct ubus_method iface_object_methods[] = {
261 { .name = "up", .handler = netifd_handle_up },
262 { .name = "down", .handler = netifd_handle_down },
263 { .name = "status", .handler = netifd_handle_status },
264 { .name = "add_device", .handler = netifd_iface_handle_device,
265 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
266 { .name = "remove_device", .handler = netifd_iface_handle_device,
267 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
268 { .name = "notify_proto", .handler = netifd_iface_notify_proto },
269 };
270
271 static struct ubus_object_type iface_object_type =
272 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
273
274
275 void
276 netifd_ubus_add_interface(struct interface *iface)
277 {
278 struct ubus_object *obj = &iface->ubus;
279 char *name;
280
281 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
282 if (!name)
283 return;
284
285 sprintf(name, "%s.%s", main_object.name, iface->name);
286 obj->name = name;
287 obj->type = &iface_object_type;
288 obj->methods = iface_object_methods;
289 obj->n_methods = ARRAY_SIZE(iface_object_methods);
290 if (ubus_add_object(ctx, &iface->ubus)) {
291 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
292 free(name);
293 obj->name = NULL;
294 }
295 }
296
297 void
298 netifd_ubus_remove_interface(struct interface *iface)
299 {
300 if (!iface->ubus.name)
301 return;
302
303 ubus_remove_object(ctx, &iface->ubus);
304 free((void *) iface->ubus.name);
305 }