handle the interface autostart option
[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 static const char *iface_state[] = {
151 [IFS_SETUP] = "setup",
152 [IFS_UP] = "up",
153 [IFS_TEARDOWN] = "teardown",
154 [IFS_DOWN] = "down",
155 };
156 struct interface *iface;
157
158 iface = container_of(obj, struct interface, ubus);
159
160 blob_buf_init(&b, 0);
161 blobmsg_add_string(&b, "state", iface_state[iface->state]);
162 blobmsg_add_u8(&b, "active", iface->active);
163 blobmsg_add_u8(&b, "autostart", iface->autostart);
164 if (iface->main_dev.dev) {
165 struct device *dev = iface->main_dev.dev;
166 const char *field;
167 void *devinfo;
168
169 /* use a different field for virtual devices */
170 if (dev->avl.key)
171 field = "device";
172 else
173 field = "link";
174
175 devinfo = blobmsg_open_table(&b, field);
176 blobmsg_add_string(&b, "name", dev->ifname);
177
178 if (dev->type->dump_status)
179 dev->type->dump_status(dev, &b);
180
181 blobmsg_close_table(&b, devinfo);
182 }
183
184 if (!list_is_empty(&iface->errors))
185 netifd_add_interface_errors(&b, iface);
186
187 ubus_send_reply(ctx, req, b.head);
188
189 return 0;
190 }
191
192 static int
193 netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
194 struct ubus_request_data *req, const char *method,
195 struct blob_attr *msg)
196 {
197 struct interface *iface;
198 struct device *dev, *main_dev;
199 struct blob_attr *tb[__DEV_MAX];
200 bool add = !strncmp(method, "add", 3);
201 int ret;
202
203 iface = container_of(obj, struct interface, ubus);
204
205 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
206
207 if (!tb[DEV_NAME])
208 return UBUS_STATUS_INVALID_ARGUMENT;
209
210 main_dev = iface->main_dev.dev;
211 if (!main_dev)
212 return UBUS_STATUS_NOT_FOUND;
213
214 if (!main_dev->hotplug_ops)
215 return UBUS_STATUS_NOT_SUPPORTED;
216
217 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
218 if (!dev)
219 return UBUS_STATUS_NOT_FOUND;
220
221 if (main_dev != dev) {
222 if (add)
223 ret = main_dev->hotplug_ops->add(main_dev, dev);
224 else
225 ret = main_dev->hotplug_ops->del(main_dev, dev);
226 if (ret)
227 ret = UBUS_STATUS_UNKNOWN_ERROR;
228 } else {
229 ret = UBUS_STATUS_INVALID_ARGUMENT;
230 }
231
232 if (add)
233 device_free_unused(dev);
234
235 return ret;
236 }
237
238
239 static struct ubus_method iface_object_methods[] = {
240 { .name = "up", .handler = netifd_handle_up },
241 { .name = "down", .handler = netifd_handle_down },
242 { .name = "status", .handler = netifd_handle_status },
243 { .name = "add_device", .handler = netifd_iface_handle_device,
244 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
245 { .name = "remove_device", .handler = netifd_iface_handle_device,
246 .policy = dev_policy, .n_policy = __DEV_MAX_NOFORCE },
247 };
248
249 static struct ubus_object_type iface_object_type =
250 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
251
252
253 void
254 netifd_ubus_add_interface(struct interface *iface)
255 {
256 struct ubus_object *obj = &iface->ubus;
257 char *name;
258
259 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
260 if (!name)
261 return;
262
263 sprintf(name, "%s.%s", main_object.name, iface->name);
264 obj->name = name;
265 obj->type = &iface_object_type;
266 obj->methods = iface_object_methods;
267 obj->n_methods = ARRAY_SIZE(iface_object_methods);
268 if (ubus_add_object(ctx, &iface->ubus)) {
269 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
270 free(name);
271 obj->name = NULL;
272 }
273 }
274
275 void
276 netifd_ubus_remove_interface(struct interface *iface)
277 {
278 if (!iface->ubus.name)
279 return;
280
281 ubus_remove_object(ctx, &iface->ubus);
282 free((void *) iface->ubus.name);
283 }