6 static struct ubus_context *ctx = NULL;
7 static struct blob_buf b;
11 static const struct ubus_signature main_object_sig[] = {
12 UBUS_METHOD_START("add_device"),
13 UBUS_FIELD(STRING, "name"),
16 UBUS_METHOD_START("del_device"),
17 UBUS_FIELD(STRING, "name"),
21 static struct ubus_object_type main_object_type =
22 UBUS_OBJECT_TYPE("netifd", main_object_sig);
30 static const struct blobmsg_policy dev_policy[] = {
31 [DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
32 [DEV_FORCE] = { .name = "force", .type = BLOBMSG_TYPE_INT8 },
35 static int netifd_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
36 struct ubus_request_data *req, const char *method,
37 struct blob_attr *msg)
40 struct blob_attr *tb[DEV_LAST];
41 bool add = !strncmp(method, "add", 3);
43 blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
46 return UBUS_STATUS_INVALID_ARGUMENT;
48 dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
50 return UBUS_STATUS_NOT_FOUND;
52 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
53 set_device_present(dev, add);
55 check_device_state(dev);
60 static struct ubus_method main_object_methods[] = {
61 { .name = "add_device", .handler = netifd_handle_device },
62 { .name = "del_device", .handler = netifd_handle_device },
65 static struct ubus_object main_object = {
66 .name = "network.interface",
67 .type = &main_object_type,
68 .methods = main_object_methods,
69 .n_methods = ARRAY_SIZE(main_object_methods),
72 int netifd_ubus_init(const char *path)
76 ctx = ubus_connect(path);
80 DPRINTF("connected as %08x\n", ctx->local_id);
84 ret = ubus_add_object(ctx, &main_object);
86 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
91 void netifd_ubus_done(void)
97 /* per-interface object */
98 static const struct ubus_signature iface_object_sig[] = {
99 UBUS_METHOD_START("up"),
102 UBUS_METHOD_START("down"),
106 static struct ubus_object_type iface_object_type =
107 UBUS_OBJECT_TYPE("netifd_iface", iface_object_sig);
110 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
111 struct ubus_request_data *req, const char *method,
112 struct blob_attr *msg)
114 struct interface *iface;
116 iface = container_of(obj, struct interface, ubus);
117 set_interface_up(iface);
122 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
123 struct ubus_request_data *req, const char *method,
124 struct blob_attr *msg)
126 struct interface *iface;
128 iface = container_of(obj, struct interface, ubus);
129 set_interface_down(iface);
134 static int netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
135 struct ubus_request_data *req, const char *method,
136 struct blob_attr *msg)
138 struct interface *iface;
140 iface = container_of(obj, struct interface, ubus);
142 blob_buf_init(&b, 0);
143 blobmsg_add_u8(&b, "up", iface->up);
144 blobmsg_add_u8(&b, "active", iface->active);
145 blobmsg_add_u8(&b, "autostart", iface->autostart);
146 if (iface->main_dev.dev) {
147 struct device *dev = iface->main_dev.dev;
150 blobmsg_add_string(&b, "device", dev->ifname);
152 /* use a different field for virtual devices */
153 blobmsg_add_string(&b, "link", dev->ifname);
155 if (dev->type->dump_status)
156 dev->type->dump_status(dev, &b);
159 ubus_send_reply(ctx, req, b.head);
164 static struct ubus_method iface_object_methods[] = {
165 { .name = "up", .handler = netifd_handle_up },
166 { .name = "down", .handler = netifd_handle_down },
167 { .name = "status", .handler = netifd_handle_status },
171 void netifd_ubus_add_interface(struct interface *iface)
173 struct ubus_object *obj = &iface->ubus;
176 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
180 sprintf(name, "%s.%s", main_object.name, iface->name);
182 obj->type = &iface_object_type;
183 obj->methods = iface_object_methods;
184 obj->n_methods = ARRAY_SIZE(iface_object_methods);
185 if (ubus_add_object(ctx, &iface->ubus)) {
186 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
192 void netifd_ubus_remove_interface(struct interface *iface)
194 if (!iface->ubus.name)
197 ubus_remove_object(ctx, &iface->ubus);
198 free((void *) iface->ubus.name);