move all device data to its own table to prevent namespace collisions
[project/netifd.git] / ubus.c
1 #include <string.h>
2
3 #include "netifd.h"
4 #include "ubus.h"
5
6 static struct ubus_context *ctx = NULL;
7 static struct blob_buf b;
8
9 /* global object */
10
11 static const struct ubus_signature main_object_sig[] = {
12 UBUS_METHOD_START("add_device"),
13 UBUS_FIELD(STRING, "name"),
14 UBUS_METHOD_END(),
15
16 UBUS_METHOD_START("del_device"),
17 UBUS_FIELD(STRING, "name"),
18 UBUS_METHOD_END(),
19 };
20
21 static struct ubus_object_type main_object_type =
22 UBUS_OBJECT_TYPE("netifd", main_object_sig);
23
24 enum {
25 DEV_NAME,
26 DEV_FORCE,
27 DEV_LAST,
28 };
29
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 },
33 };
34
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)
38 {
39 struct device *dev;
40 struct blob_attr *tb[DEV_LAST];
41 bool add = !strncmp(method, "add", 3);
42
43 blobmsg_parse(dev_policy, ARRAY_SIZE(dev_policy), tb, blob_data(msg), blob_len(msg));
44
45 if (!tb[DEV_NAME])
46 return UBUS_STATUS_INVALID_ARGUMENT;
47
48 dev = get_device(blobmsg_data(tb[DEV_NAME]), false);
49 if (!dev)
50 return UBUS_STATUS_NOT_FOUND;
51
52 if (!add || (tb[DEV_FORCE] && blobmsg_get_u8(tb[DEV_FORCE])))
53 set_device_present(dev, add);
54 else
55 check_device_state(dev);
56
57 return 0;
58 }
59
60 static struct ubus_method main_object_methods[] = {
61 { .name = "add_device", .handler = netifd_handle_device },
62 { .name = "del_device", .handler = netifd_handle_device },
63 };
64
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),
70 };
71
72 int netifd_ubus_init(const char *path)
73 {
74 int ret;
75
76 ctx = ubus_connect(path);
77 if (!ctx)
78 return -EIO;
79
80 DPRINTF("connected as %08x\n", ctx->local_id);
81 uloop_init();
82 ubus_add_uloop(ctx);
83
84 ret = ubus_add_object(ctx, &main_object);
85 if (ret != 0)
86 fprintf(stderr, "Failed to publish object: %s\n", ubus_strerror(ret));
87
88 return 0;
89 }
90
91 void netifd_ubus_done(void)
92 {
93 ubus_free(ctx);
94 }
95
96
97 /* per-interface object */
98 static const struct ubus_signature iface_object_sig[] = {
99 UBUS_METHOD_START("up"),
100 UBUS_METHOD_END(),
101
102 UBUS_METHOD_START("down"),
103 UBUS_METHOD_END(),
104 };
105
106 static struct ubus_object_type iface_object_type =
107 UBUS_OBJECT_TYPE("netifd_iface", iface_object_sig);
108
109
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)
113 {
114 struct interface *iface;
115
116 iface = container_of(obj, struct interface, ubus);
117 set_interface_up(iface);
118
119 return 0;
120 }
121
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)
125 {
126 struct interface *iface;
127
128 iface = container_of(obj, struct interface, ubus);
129 set_interface_down(iface);
130
131 return 0;
132 }
133
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)
137 {
138 struct interface *iface;
139
140 iface = container_of(obj, struct interface, ubus);
141
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;
148 const char *field;
149 void *devinfo;
150
151 /* use a different field for virtual devices */
152 if (dev->avl.key)
153 field = "device";
154 else
155 field = "link";
156
157 devinfo = blobmsg_open_table(&b, field);
158 blobmsg_add_string(&b, "name", dev->ifname);
159
160 if (dev->type->dump_status)
161 dev->type->dump_status(dev, &b);
162
163 blobmsg_close_table(&b, devinfo);
164 }
165
166 ubus_send_reply(ctx, req, b.head);
167
168 return 0;
169 }
170
171 static struct ubus_method iface_object_methods[] = {
172 { .name = "up", .handler = netifd_handle_up },
173 { .name = "down", .handler = netifd_handle_down },
174 { .name = "status", .handler = netifd_handle_status },
175 };
176
177
178 void netifd_ubus_add_interface(struct interface *iface)
179 {
180 struct ubus_object *obj = &iface->ubus;
181 char *name;
182
183 name = malloc(strlen(main_object.name) + strlen(iface->name) + 2);
184 if (!name)
185 return;
186
187 sprintf(name, "%s.%s", main_object.name, iface->name);
188 obj->name = name;
189 obj->type = &iface_object_type;
190 obj->methods = iface_object_methods;
191 obj->n_methods = ARRAY_SIZE(iface_object_methods);
192 if (ubus_add_object(ctx, &iface->ubus)) {
193 DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
194 free(name);
195 obj->name = NULL;
196 }
197 }
198
199 void netifd_ubus_remove_interface(struct interface *iface)
200 {
201 if (!iface->ubus.name)
202 return;
203
204 ubus_remove_object(ctx, &iface->ubus);
205 free((void *) iface->ubus.name);
206 }