8 static struct ubus_context *ctx = NULL;
9 static struct blob_buf b;
17 __DEV_MAX_NOFORCE = __DEV_MAX - 1,
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 },
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)
31 struct blob_attr *tb[__DEV_MAX];
32 bool add = !strncmp(method, "add", 3);
34 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
37 return UBUS_STATUS_INVALID_ARGUMENT;
39 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
41 return UBUS_STATUS_NOT_FOUND;
43 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
44 device_set_present(dev, add);
46 check_device_state(dev);
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)
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 },
66 static struct ubus_object_type main_object_type =
67 UBUS_OBJECT_TYPE("netifd", main_object_methods);
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),
77 netifd_ubus_init(const char *path)
81 ctx = ubus_connect(path);
85 DPRINTF("connected as %08x\n", ctx->local_id);
89 ret = ubus_add_object(ctx, &main_object);
91 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
97 netifd_ubus_done(void)
103 /* per-interface object */
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)
110 struct interface *iface;
112 iface = container_of(obj, struct interface, ubus);
113 interface_set_up(iface);
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)
123 struct interface *iface;
125 iface = container_of(obj, struct interface, ubus);
126 interface_set_down(iface);
132 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
134 struct interface_error *error;
138 e = blobmsg_open_array(b, "errors");
139 list_for_each_entry(error, &iface->errors, list) {
140 e2 = blobmsg_open_table(b, NULL);
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);
151 blobmsg_close_table(b, e2);
153 blobmsg_close_array(b, e);
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)
161 struct interface *iface;
163 iface = container_of(obj, struct interface, ubus);
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;
175 /* use a different field for virtual devices */
181 devinfo = blobmsg_open_table(&b, field);
182 blobmsg_add_string(&b, "name", dev->ifname);
184 if (dev->type->dump_status)
185 dev->type->dump_status(dev, &b);
187 blobmsg_close_table(&b, devinfo);
190 if (!list_is_empty(&iface->errors))
191 netifd_add_interface_errors(&b, iface);
193 ubus_send_reply(ctx, req, b.head);
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)
203 struct interface *iface;
204 struct device *dev, *main_dev;
205 struct blob_attr *tb[__DEV_MAX];
206 bool add = !strncmp(method, "add", 3);
209 iface = container_of(obj, struct interface, ubus);
211 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
214 return UBUS_STATUS_INVALID_ARGUMENT;
216 main_dev = iface->main_dev.dev;
218 return UBUS_STATUS_NOT_FOUND;
220 if (!main_dev->hotplug_ops)
221 return UBUS_STATUS_NOT_SUPPORTED;
223 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
225 return UBUS_STATUS_NOT_FOUND;
227 if (main_dev != dev) {
229 ret = main_dev->hotplug_ops->add(main_dev, dev);
231 ret = main_dev->hotplug_ops->del(main_dev, dev);
233 ret = UBUS_STATUS_UNKNOWN_ERROR;
235 ret = UBUS_STATUS_INVALID_ARGUMENT;
239 device_free_unused(dev);
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)
250 struct interface *iface;
252 iface = container_of(obj, struct interface, ubus);
254 if (!iface->proto || !iface->proto->notify)
255 return UBUS_STATUS_NOT_SUPPORTED;
257 return iface->proto->notify(iface->proto, msg);
261 netifd_iface_do_remove(struct uloop_timeout *timeout)
263 struct interface *iface;
265 iface = container_of(timeout, struct interface, remove_timer);
266 vlist_delete(&interfaces, &iface->node);
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)
274 struct interface *iface;
276 iface = container_of(obj, struct interface, ubus);
277 if (iface->remove_timer.cb)
278 return UBUS_STATUS_INVALID_ARGUMENT;
280 iface->remove_timer.cb = netifd_iface_do_remove;
281 uloop_timeout_set(&iface->remove_timer, 100);
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 }
297 static struct ubus_object_type iface_object_type =
298 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
302 netifd_ubus_add_interface(struct interface *iface)
304 struct ubus_object *obj = &iface->ubus;
307 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
311 sprintf(name, "%s.%s", main_object.name, iface->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);
324 netifd_ubus_remove_interface(struct interface *iface)
326 if (!iface->ubus.name)
329 ubus_remove_object(ctx, &iface->ubus);
330 free((void *) iface->ubus.name);