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 static const char *iface_state[] = {
151 [IFS_SETUP] = "setup",
153 [IFS_TEARDOWN] = "teardown",
156 struct interface *iface;
158 iface = container_of(obj, struct interface, ubus);
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;
169 /* use a different field for virtual devices */
175 devinfo = blobmsg_open_table(&b, field);
176 blobmsg_add_string(&b, "name", dev->ifname);
178 if (dev->type->dump_status)
179 dev->type->dump_status(dev, &b);
181 blobmsg_close_table(&b, devinfo);
184 if (!list_is_empty(&iface->errors))
185 netifd_add_interface_errors(&b, iface);
187 ubus_send_reply(ctx, req, b.head);
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)
197 struct interface *iface;
198 struct device *dev, *main_dev;
199 struct blob_attr *tb[__DEV_MAX];
200 bool add = !strncmp(method, "add", 3);
203 iface = container_of(obj, struct interface, ubus);
205 blobmsg_parse(dev_policy, __DEV_MAX, tb, blob_data(msg), blob_len(msg));
208 return UBUS_STATUS_INVALID_ARGUMENT;
210 main_dev = iface->main_dev.dev;
212 return UBUS_STATUS_NOT_FOUND;
214 if (!main_dev->hotplug_ops)
215 return UBUS_STATUS_NOT_SUPPORTED;
217 dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
219 return UBUS_STATUS_NOT_FOUND;
221 if (main_dev != dev) {
223 ret = main_dev->hotplug_ops->add(main_dev, dev);
225 ret = main_dev->hotplug_ops->del(main_dev, dev);
227 ret = UBUS_STATUS_UNKNOWN_ERROR;
229 ret = UBUS_STATUS_INVALID_ARGUMENT;
233 device_free_unused(dev);
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 },
249 static struct ubus_object_type iface_object_type =
250 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
254 netifd_ubus_add_interface(struct interface *iface)
256 struct ubus_object *obj = &iface->ubus;
259 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
263 sprintf(name, "%s.%s", main_object.name, iface->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);
276 netifd_ubus_remove_interface(struct interface *iface)
278 if (!iface->ubus.name)
281 ubus_remove_object(ctx, &iface->ubus);
282 free((void *) iface->ubus.name);