7 static struct ubus_context *ctx = NULL;
8 static struct blob_buf b;
16 __DEV_MAX_NOFORCE = __DEV_MAX - 1,
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 },
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)
30 struct blob_attr *tb[__DEV_MAX];
31 bool add = !strncmp(method, "add", 3);
33 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
36 return UBUS_STATUS_INVALID_ARGUMENT;
38 dev = device_get(blobmsg_data(tb[DEV_NAME]), false);
40 return UBUS_STATUS_NOT_FOUND;
42 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
43 device_set_present(dev, add);
45 check_device_state(dev);
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),
55 static struct ubus_object_type main_object_type =
56 UBUS_OBJECT_TYPE("netifd", main_object_methods);
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),
66 netifd_ubus_init(const char *path)
70 ctx = ubus_connect(path);
74 DPRINTF("connected as %08x\n", ctx->local_id);
78 ret = ubus_add_object(ctx, &main_object);
80 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
86 netifd_ubus_done(void)
92 /* per-interface object */
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)
99 struct interface *iface;
101 iface = container_of(obj, struct interface, ubus);
102 interface_set_up(iface);
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)
112 struct interface *iface;
114 iface = container_of(obj, struct interface, ubus);
115 interface_set_down(iface);
121 netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
123 struct interface_error *error;
127 e = blobmsg_open_array(b, "errors");
128 list_for_each_entry(error, &iface->errors, list) {
129 e2 = blobmsg_open_table(b, NULL);
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);
140 blobmsg_close_table(b, e2);
142 blobmsg_close_array(b, e);
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)
150 struct interface *iface;
152 iface = container_of(obj, struct interface, ubus);
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;
164 /* use a different field for virtual devices */
170 devinfo = blobmsg_open_table(&b, field);
171 blobmsg_add_string(&b, "name", dev->ifname);
173 if (dev->type->dump_status)
174 dev->type->dump_status(dev, &b);
176 blobmsg_close_table(&b, devinfo);
179 if (!list_is_empty(&iface->errors))
180 netifd_add_interface_errors(&b, iface);
182 ubus_send_reply(ctx, req, b.head);
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)
192 struct interface *iface;
193 struct device *dev, *main_dev;
194 struct blob_attr *tb[__DEV_MAX];
195 bool add = !strncmp(method, "add", 3);
198 iface = container_of(obj, struct interface, ubus);
200 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
203 return UBUS_STATUS_INVALID_ARGUMENT;
205 main_dev = iface->main_dev.dev;
207 return UBUS_STATUS_NOT_FOUND;
209 if (!main_dev->hotplug_ops)
210 return UBUS_STATUS_NOT_SUPPORTED;
212 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
214 return UBUS_STATUS_NOT_FOUND;
216 if (main_dev != dev) {
218 ret = main_dev->hotplug_ops->add(main_dev, dev);
220 ret = main_dev->hotplug_ops->del(main_dev, dev);
222 ret = UBUS_STATUS_UNKNOWN_ERROR;
224 ret = UBUS_STATUS_INVALID_ARGUMENT;
228 device_free_unused(dev);
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 },
244 static struct ubus_object_type iface_object_type =
245 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
249 netifd_ubus_add_interface(struct interface *iface)
251 struct ubus_object *obj = &iface->ubus;
254 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
258 sprintf(name, "%s.%s", main_object.name, iface->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);
271 netifd_ubus_remove_interface(struct interface *iface)
273 if (!iface->ubus.name)
276 ubus_remove_object(ctx, &iface->ubus);
277 free((void *) iface->ubus.name);