7 static struct ubus_context *ctx = NULL;
8 static struct blob_buf b;
18 static const struct blobmsg_policy dev_policy[] = {
19 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
20 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
23 static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
24 struct ubus_request_data *req, const char *method,
25 struct blob_attr *msg)
28 struct blob_attr *tb[DEV_LAST];
29 bool add = !strncmp(method, "add", 3);
31 blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
34 return UBUS_STATUS_INVALID_ARGUMENT;
36 dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
38 return UBUS_STATUS_NOT_FOUND;
40 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
41 set_device_present(dev, add);
43 check_device_state(dev);
48 static struct ubus_method main_object_methods[] = {
49 UBUS_METHOD("add_device", netifd_handle_device, dev_policy),
50 UBUS_METHOD("del_device", netifd_handle_device, dev_policy),
53 static struct ubus_object_type main_object_type =
54 UBUS_OBJECT_TYPE("netifd", main_object_methods);
56 static struct ubus_object main_object = {
57 .name = "network.interface",
58 .type = &main_object_type,
59 .methods = main_object_methods,
60 .n_methods = ARRAY_SIZE(main_object_methods),
63 int netifd_ubus_init(const char *path)
67 ctx = ubus_connect(path);
71 DPRINTF("connected as %08x\n", ctx->local_id);
75 ret = ubus_add_object(ctx, &main_object);
77 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
82 void netifd_ubus_done(void)
88 /* per-interface object */
90 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
91 struct ubus_request_data *req, const char *method,
92 struct blob_attr *msg)
94 struct interface *iface;
96 iface = container_of(obj, struct interface, ubus);
97 set_interface_up(iface);
102 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
103 struct ubus_request_data *req, const char *method,
104 struct blob_attr *msg)
106 struct interface *iface;
108 iface = container_of(obj, struct interface, ubus);
109 set_interface_down(iface);
114 static void netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
116 struct interface_error *error;
120 e = blobmsg_open_array(b, "errors");
121 list_for_each_entry(error, &iface->errors, list) {
122 e2 = blobmsg_open_table(b, NULL);
124 blobmsg_add_string(b, "subsystem", error->subsystem);
125 blobmsg_add_string(b, "code", error->code);
126 if (error->data[0]) {
127 e3 = blobmsg_open_array(b, "data");
128 for (i = 0; error->data[i]; i++)
129 blobmsg_add_string(b, NULL, error->data[i]);
130 blobmsg_close_array(b, e3);
133 blobmsg_close_table(b, e2);
135 blobmsg_close_array(b, e);
138 static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
139 struct ubus_request_data *req, const char *method,
140 struct blob_attr *msg)
142 static const char *iface_state[] = {
143 [IFS_SETUP] = "setup",
145 [IFS_TEARDOWN] = "teardown",
148 struct interface *iface;
150 iface = container_of(obj, struct interface, ubus);
152 blob_buf_init(&b, 0);
153 blobmsg_add_string(&b, "state", iface_state[iface->state]);
154 blobmsg_add_u8(&b, "active", iface->active);
155 blobmsg_add_u8(&b, "autostart", iface->autostart);
156 if (iface->main_dev.dev) {
157 struct device *dev = iface->main_dev.dev;
161 /* use a different field for virtual devices */
167 devinfo = blobmsg_open_table(&b, field);
168 blobmsg_add_string(&b, "name", dev->ifname);
170 if (dev->type->dump_status)
171 dev->type->dump_status(dev, &b);
173 blobmsg_close_table(&b, devinfo);
176 if (!list_is_empty(&iface->errors))
177 netifd_add_interface_errors(&b, iface);
179 ubus_send_reply(ctx, req, b.head);
185 static struct ubus_method iface_object_methods[] = {
186 { .name = "up", .handler = netifd_handle_up },
187 { .name = "down", .handler = netifd_handle_down },
188 { .name = "status", .handler = netifd_handle_status },
191 static struct ubus_object_type iface_object_type =
192 UBUS_OBJECT_TYPE("netifd_iface", iface_object_methods);
195 void netifd_ubus_add_interface(struct interface *iface)
197 struct ubus_object *obj = &iface->ubus;
200 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
204 sprintf(name, "%s.%s", main_object.name, iface->name);
206 obj->type = &iface_object_type;
207 obj->methods = iface_object_methods;
208 obj->n_methods = ARRAY_SIZE(iface_object_methods);
209 if (ubus_add_object(ctx, &iface->ubus)) {
210 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
216 void netifd_ubus_remove_interface(struct interface *iface)
218 if (!iface->ubus.name)
221 ubus_remove_object(ctx, &iface->ubus);
222 free((void *) iface->ubus.name);