6 static struct ubus_context *ctx = NULL;
7 static struct blob_buf b;
17 static const struct blobmsg_policy dev_policy[] = {
18 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
19 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
22 static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
23 struct ubus_request_data *req, const char *method,
24 struct blob_attr *msg)
27 struct blob_attr *tb[DEV_LAST];
28 bool add = !strncmp(method, "add", 3);
30 blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
33 return UBUS_STATUS_INVALID_ARGUMENT;
35 dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
37 return UBUS_STATUS_NOT_FOUND;
39 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
40 set_device_present(dev, add);
42 check_device_state(dev);
47 static struct ubus_method main_object_methods[] = {
48 UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
49 UBUS_METHOD("del_device", netifd_handle_device, dev_policy),
52 static struct ubus_object_type main_object_type =
53 UBUS_OBJECT_TYPE("netifd", main_object_methods);
55 static struct ubus_object main_object = {
56 .name = "network.interface",
57 .type = &main_object_type,
58 .methods = main_object_methods,
59 .n_methods = ARRAY_SIZE(main_object_methods),
62 int netifd_ubus_init(const char *path)
66 ctx = ubus_connect(path);
70 DPRINTF("connected as %08x\n", ctx->local_id);
74 ret = ubus_add_object(ctx, &main_object);
76 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
81 void netifd_ubus_done(void)
87 /* per-interface object */
89 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
90 struct ubus_request_data *req, const char *method,
91 struct blob_attr *msg)
93 struct interface *iface;
95 iface = container_of(obj, struct interface, ubus);
96 set_interface_up(iface);
101 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
102 struct ubus_request_data *req, const char *method,
103 struct blob_attr *msg)
105 struct interface *iface;
107 iface = container_of(obj, struct interface, ubus);
108 set_interface_down(iface);
113 static void netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
115 struct interface_error *error;
119 e = blobmsg_open_array(b, "errors");
120 list_for_each_entry(error, &iface->errors, list) {
121 e2 = blobmsg_open_table(b, NULL);
123 blobmsg_add_string(b, "subsystem", error->subsystem);
124 blobmsg_add_string(b, "code", error->code);
125 if (error->data[0]) {
126 e3 = blobmsg_open_array(b, "data");
127 for (i = 0; error->data[i]; i++)
128 blobmsg_add_string(b, NULL, error->data[i]);
129 blobmsg_close_array(b, e3);
132 blobmsg_close_table(b, e2);
134 blobmsg_close_array(b, e);
137 static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
138 struct ubus_request_data *req, const char *method,
139 struct blob_attr *msg)
141 static const char *iface_state[] = {
142 [IFS_SETUP] = "setup",
144 [IFS_TEARDOWN] = "teardown",
147 struct interface *iface;
149 iface = container_of(obj, struct interface, ubus);
151 blob_buf_init(&b, 0);
152 blobmsg_add_string(&b, "state", iface_state[iface->state]);
153 blobmsg_add_u8(&b, "active", iface->active);
154 blobmsg_add_u8(&b, "autostart", iface->autostart);
155 if (iface->main_dev.dev) {
156 struct device *dev = iface->main_dev.dev;
160 /* use a different field for virtual devices */
166 devinfo = blobmsg_open_table(&b, field);
167 blobmsg_add_string(&b, "name", dev->ifname);
169 if (dev->type->dump_status)
170 dev->type->dump_status(dev, &b);
172 blobmsg_close_table(&b, devinfo);
175 if (!list_is_empty(&iface->errors))
176 netifd_add_interface_errors(&b, iface);
178 ubus_send_reply(ctx, req, b.head);
184 static struct ubus_method iface_object_methods[] = {
185 { .name = "up", .handler = netifd_handle_up },
186 { .name = "down", .handler = netifd_handle_down },
187 { .name = "status", .handler = netifd_handle_status },
190 static struct ubus_object_type iface_object_type =
191 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
194 void netifd_ubus_add_interface(struct interface *iface)
196 struct ubus_object *obj = &iface->ubus;
199 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
203 sprintf(name, "%s.%s", main_object.name, iface->name);
205 obj->type = &iface_object_type;
206 obj->methods = iface_object_methods;
207 obj->n_methods = ARRAY_SIZE(iface_object_methods);
208 if (ubus_add_object(ctx, &iface->ubus)) {
209 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
215 void netifd_ubus_remove_interface(struct interface *iface)
217 if (!iface->ubus.name)
220 ubus_remove_object(ctx, &iface->ubus);
221 free((void *) iface->ubus.name);