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"),
105 UBUS_METHOD_START("status"),
109 static struct ubus_object_type iface_object_type =
110 UBUS_OBJECT_TYPE("netifd_iface", iface_object_sig);
113 static int netifd_handle_up(struct ubus_context *ctx, struct ubus_object *obj,
114 struct ubus_request_data *req, const char *method,
115 struct blob_attr *msg)
117 struct interface *iface;
119 iface = container_of(obj, struct interface, ubus);
120 set_interface_up(iface);
125 static int netifd_handle_down(struct ubus_context *ctx, struct ubus_object *obj,
126 struct ubus_request_data *req, const char *method,
127 struct blob_attr *msg)
129 struct interface *iface;
131 iface = container_of(obj, struct interface, ubus);
132 set_interface_down(iface);
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 struct interface *iface;
143 iface = container_of(obj, struct interface, ubus);
145 blob_buf_init(&b, 0);
146 blobmsg_add_u8(&b, "up", iface->up);
147 blobmsg_add_u8(&b, "active", iface->active);
148 blobmsg_add_u8(&b, "autostart", iface->autostart);
149 if (iface->main_dev.dev) {
150 struct device *dev = iface->main_dev.dev;
154 /* use a different field for virtual devices */
160 devinfo = blobmsg_open_table(&b, field);
161 blobmsg_add_string(&b, "name", dev->ifname);
163 if (dev->type->dump_status)
164 dev->type->dump_status(dev, &b);
166 blobmsg_close_table(&b, devinfo);
169 ubus_send_reply(ctx, req, b.head);
174 static struct ubus_method iface_object_methods[] = {
175 { .name = "up", .handler = netifd_handle_up },
176 { .name = "down", .handler = netifd_handle_down },
177 { .name = "status", .handler = netifd_handle_status },
181 void netifd_ubus_add_interface(struct interface *iface)
183 struct ubus_object *obj = &iface->ubus;
186 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
190 sprintf(name, "%s.%s", main_object.name, iface->name);
192 obj->type = &iface_object_type;
193 obj->methods = iface_object_methods;
194 obj->n_methods = ARRAY_SIZE(iface_object_methods);
195 if (ubus_add_object(ctx, &iface->ubus)) {
196 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
202 void netifd_ubus_remove_interface(struct interface *iface)
204 if (!iface->ubus.name)
207 ubus_remove_object(ctx, &iface->ubus);
208 free((void *) iface->ubus.name);