6eba0b07e8faa12739c54b010f0d85da17606166
[project/netifd.git] / ubus.c
1 #include <string.h>
2
3 #include "netifd.h"
4 #include "interface.h"
5 #include "ubus.h"
6
7 static struct ubus_context *ctx = NULL;
8 static struct blob_buf b;
9
10 /* global object */
11
12 enum {
13 DEV_NAME,
14 DEV_FORCE,
15 __DEV_MAX,
16 __DEV_MAX_NOFORCE = __DEV_MAX - 1,
17 };
18
19 static const struct blobmsg_policy dev_policy[__DEV_MAX] = {
20 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
21 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
22 };
23
24 static int
25 netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
26 struct ubus_request_data *req, const char *method,
27 struct blob_attr *msg)
28 {
29 struct device *dev;
30 struct blob_attr *tb[__DEV_MAX];
31 bool add = !strncmp(method, "add", 3);
32
33 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
34
35 if (!tb[DEV_NAME])
36 return UBUS_STATUS_INVALID_ARGUMENT;
37
38 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
39 if (!dev)
40 return UBUS_STATUS_NOT_FOUND;
41
42 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
43 device_set_present(dev, add);
44 else
45 check_device_state(dev);
46
47 return 0;
48 }
49
50 static struct ubus_method main_object_methods[] = {
51 UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
52 UBUS_METHOD("remove_device", netifd_handle_device, dev_policy),
53 };
54
55 static struct ubus_object_type main_object_type =
56 UBUS_OBJECT_TYPE("netifd", main_object_methods);
57
58 static struct ubus_object main_object = {
59 .name = "network.interface",
60 .type = &main_object_type,
61 .methods = main_object_methods,
62 .n_methods = ARRAY_SIZE(main_object_methods),
63 };
64
65 int
66 netifd_ubus_init(const char *path)
67 {
68 int ret;
69
70 ctx = ubus_connect(path);
71 if (!ctx)
72 return -EIO;
73
74 DPRINTF("connected as %08x\n", ctx->local_id);
75 uloop_init();
76 ubus_add_uloop(ctx);
77
78 ret = ubus_add_object(ctx, &main_object);
79 if (ret != 0)
80 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
81
82 return 0;
83 }
84
85 void
86 netifd_ubus_done(void)
87 {
88 ubus_free(ctx);
89 }
90
91
92 /* per-interface object */
93
94 static int
95 netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
96 struct ubus_request_data *req, const char *method,
97 struct blob_attr *msg)
98 {
99 struct interface *iface;
100
101 iface = container_of(obj, struct interface, ubus);
102 interface_set_up(iface);
103
104 return 0;
105 }
106
107 static int
108 netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
109 struct ubus_request_data *req, const char *method,
110 struct blob_attr *msg)
111 {
112 struct interface *iface;
113
114 iface = container_of(obj, struct interface, ubus);
115 interface_set_down(iface);
116
117 return 0;
118 }
119
120 static void
121 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
122 {
123 struct interface_error *error;
124 void *e, *e2, *e3;
125 int i;
126
127 e = blobmsg_open_array(b, "errors");
128 list_for_each_entry(error, &iface->errors, list) {
129 e2 = blobmsg_open_table(b, NULL);
130
131 blobmsg_add_string(b, "subsystem", error->subsystem);
132 blobmsg_add_string(b, "code", error->code);
133 if (error->data[0]) {
134 e3 = blobmsg_open_array(b, "data");
135 for (i = 0; error->data[i]; i++)
136 blobmsg_add_string(b, NULL, error->data[i]);
137 blobmsg_close_array(b, e3);
138 }
139
140 blobmsg_close_table(b, e2);
141 }
142 blobmsg_close_array(b, e);
143 }
144
145 static int
146 netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
147 struct ubus_request_data *req, const char *method,
148 struct blob_attr *msg)
149 {
150 struct interface *iface;
151
152 iface = container_of(obj, struct interface, ubus);
153
154 blob_buf_init(&b, 0);
155 blobmsg_add_u8(&b, "up", iface->state == IFS_UP);
156 blobmsg_add_u8(&b, "pending", iface->state == IFS_SETUP);
157 blobmsg_add_u8(&b, "active", iface->active);
158 blobmsg_add_u8(&b, "autostart", iface->autostart);
159 if (iface->main_dev.dev) {
160 struct device *dev = iface->main_dev.dev;
161 const char *field;
162 void *devinfo;
163
164 /* use a different field for virtual devices */
165 if (dev->avl.key)
166 field = "device";
167 else
168 field = "link";
169
170 devinfo = blobmsg_open_table(&b, field);
171 blobmsg_add_string(&b, "name", dev->ifname);
172
173 if (dev->type->dump_status)
174 dev->type->dump_status(dev, &b);
175
176 blobmsg_close_table(&b, devinfo);
177 }
178
179 if (!list_is_empty(&iface->errors))
180 netifd_add_interface_errors(&b, iface);
181
182 ubus_send_reply(ctx, req, b.head);
183
184 return 0;
185 }
186
187 static int
188 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
189 struct ubus_request_data *req, const char *method,
190 struct blob_attr *msg)
191 {
192 struct interface *iface;
193 struct device *dev, *main_dev;
194 struct blob_attr *tb[__DEV_MAX];
195 bool add = !strncmp(method, "add", 3);
196 int ret;
197
198 iface = container_of(obj, struct interface, ubus);
199
200 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
201
202 if (!tb[DEV_NAME])
203 return UBUS_STATUS_INVALID_ARGUMENT;
204
205 main_dev = iface->main_dev.dev;
206 if (!main_dev)
207 return UBUS_STATUS_NOT_FOUND;
208
209 if (!main_dev->hotplug_ops)
210 return UBUS_STATUS_NOT_SUPPORTED;
211
212 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
213 if (!dev)
214 return UBUS_STATUS_NOT_FOUND;
215
216 if (main_dev != dev) {
217 if (add)
218 ret = main_dev->hotplug_ops->add(main_dev, dev);
219 else
220 ret = main_dev->hotplug_ops->del(main_dev, dev);
221 if (ret)
222 ret = UBUS_STATUS_UNKNOWN_ERROR;
223 } else {
224 ret = UBUS_STATUS_INVALID_ARGUMENT;
225 }
226
227 if (add)
228 device_free_unused(dev);
229
230 return ret;
231 }
232
233
234 static struct ubus_method iface_object_methods[] = {
235 { .name = "up", .handler = netifd_handle_up },
236 { .name = "down", .handler = netifd_handle_down },
237 { .name = "status", .handler = netifd_handle_status },
238 { .name = "add_device", .handler = netifd_iface_handle_device,
239 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
240 { .name = "remove_device", .handler = netifd_iface_handle_device,
241 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
242 };
243
244 static struct ubus_object_type iface_object_type =
245 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
246
247
248 void
249 netifd_ubus_add_interface(struct interface *iface)
250 {
251 struct ubus_object *obj = &iface->ubus;
252 char *name;
253
254 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
255 if (!name)
256 return;
257
258 sprintf(name, "%s.%s", main_object.name, iface->name);
259 obj->name = name;
260 obj->type = &iface_object_type;
261 obj->methods = iface_object_methods;
262 obj->n_methods = ARRAY_SIZE(iface_object_methods);
263 if (ubus_add_object(ctx, &iface->ubus)) {
264 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
265 free(name);
266 obj->name = NULL;
267 }
268 }
269
270 void
271 netifd_ubus_remove_interface(struct interface *iface)
272 {
273 if (!iface->ubus.name)
274 return;
275
276 ubus_remove_object(ctx, &iface->ubus);
277 free((void *) iface->ubus.name);
278 }