d562c0f85e26ae8cf89c10af196dc18e3a8e8b61
[project/odhcpd.git] / src / ubus.c
1 #include <syslog.h>
2 #include <libubus.h>
3 #include <libubox/uloop.h>
4 #include <arpa/inet.h>
5
6 #include "odhcpd.h"
7 #include "dhcpv6.h"
8 #include "dhcpv4.h"
9
10
11 static struct ubus_context *ubus = NULL;
12 static struct ubus_subscriber netifd;
13 static struct blob_buf b;
14 static struct blob_attr *dump = NULL;
15 static uint32_t objid = 0;
16 static struct ubus_request req_dump = { .list = LIST_HEAD_INIT(req_dump.list) };
17
18
19 static int handle_dhcpv4_leases(struct ubus_context *ctx, _unused struct ubus_object *obj,
20 struct ubus_request_data *req, _unused const char *method,
21 _unused struct blob_attr *msg)
22 {
23 blob_buf_init(&b, 0);
24 void *a = blobmsg_open_table(&b, "device");
25 time_t now = odhcpd_time();
26
27 struct interface *iface;
28 list_for_each_entry(iface, &interfaces, head) {
29 if (iface->dhcpv4 != RELAYD_SERVER)
30 continue;
31
32 void *i = blobmsg_open_table(&b, iface->ifname);
33 void *j = blobmsg_open_array(&b, "leases");
34
35 struct dhcpv4_assignment *lease;
36 list_for_each_entry(lease, &iface->dhcpv4_assignments, head) {
37 if (!INFINITE_VALID(lease->valid_until) && lease->valid_until < now)
38 continue;
39
40 void *l = blobmsg_open_table(&b, NULL);
41
42 char *buf = blobmsg_alloc_string_buffer(&b, "mac", 13);
43 odhcpd_hexlify(buf, lease->hwaddr, sizeof(lease->hwaddr));
44 blobmsg_add_string_buffer(&b);
45
46 blobmsg_add_string(&b, "hostname", lease->hostname);
47
48 buf = blobmsg_alloc_string_buffer(&b, "ip", INET_ADDRSTRLEN);
49 struct in_addr addr = {htonl(lease->addr)};
50 inet_ntop(AF_INET, &addr, buf, INET_ADDRSTRLEN);
51 blobmsg_add_string_buffer(&b);
52
53 blobmsg_add_u32(&b, "valid", INFINITE_VALID(lease->valid_until) ?
54 (uint32_t)-1 : (uint32_t)(lease->valid_until - now));
55
56 blobmsg_close_table(&b, l);
57 }
58
59 blobmsg_close_array(&b, j);
60 blobmsg_close_table(&b, i);
61 }
62
63 blobmsg_close_table(&b, a);
64 ubus_send_reply(ctx, req, b.head);
65 return 0;
66 }
67
68
69 static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
70 _unused struct ubus_request_data *req, _unused const char *method,
71 _unused struct blob_attr *msg)
72 {
73 blob_buf_init(&b, 0);
74 void *a = blobmsg_open_table(&b, "device");
75 time_t now = odhcpd_time();
76
77 struct interface *iface;
78 list_for_each_entry(iface, &interfaces, head) {
79 if (iface->dhcpv6 != RELAYD_SERVER)
80 continue;
81
82 void *i = blobmsg_open_table(&b, iface->ifname);
83 void *j = blobmsg_open_array(&b, "leases");
84
85 struct dhcpv6_assignment *lease;
86 list_for_each_entry(lease, &iface->ia_assignments, head) {
87 if (!INFINITE_VALID(lease->valid_until) && lease->valid_until < now)
88 continue;
89
90 void *l = blobmsg_open_table(&b, NULL);
91
92 char *buf = blobmsg_alloc_string_buffer(&b, "duid", 264);
93 odhcpd_hexlify(buf, lease->clid_data, lease->clid_len);
94 blobmsg_add_string_buffer(&b);
95
96 blobmsg_add_u32(&b, "iaid", ntohl(lease->iaid));
97 blobmsg_add_string(&b, "hostname", (lease->hostname) ? lease->hostname : "");
98 blobmsg_add_u32(&b, "assigned", lease->assigned);
99 blobmsg_add_u32(&b, "length", lease->length);
100
101 void *m = blobmsg_open_array(&b, "ipv6");
102 struct in6_addr addr;
103 for (size_t i = 0; i < iface->ia_addr_len; ++i) {
104 if (iface->ia_addr[i].prefix > 64)
105 continue;
106
107 addr = iface->ia_addr[i].addr;
108 if (lease->length == 128)
109 addr.s6_addr32[3] = htonl(lease->assigned);
110 else {
111 addr.s6_addr32[1] |= htonl(lease->assigned);
112 addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
113 }
114
115 char *c = blobmsg_alloc_string_buffer(&b, NULL, INET6_ADDRSTRLEN);
116 inet_ntop(AF_INET6, &addr, c, INET6_ADDRSTRLEN);
117 blobmsg_add_string_buffer(&b);
118 }
119 blobmsg_close_table(&b, m);
120
121 blobmsg_add_u32(&b, "valid", INFINITE_VALID(lease->valid_until) ?
122 (uint32_t)-1 : (uint32_t)(lease->valid_until - now));
123
124 blobmsg_close_table(&b, l);
125 }
126
127 blobmsg_close_array(&b, j);
128 blobmsg_close_table(&b, i);
129 }
130
131 blobmsg_close_table(&b, a);
132 ubus_send_reply(ctx, req, b.head);
133 return 0;
134 }
135
136
137 static struct ubus_method main_object_methods[] = {
138 {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
139 {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
140 };
141
142 static struct ubus_object_type main_object_type =
143 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
144
145 static struct ubus_object main_object = {
146 .name = "dhcp",
147 .type = &main_object_type,
148 .methods = main_object_methods,
149 .n_methods = ARRAY_SIZE(main_object_methods),
150 };
151
152
153 enum {
154 DUMP_ATTR_INTERFACE,
155 DUMP_ATTR_MAX
156 };
157
158 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
159 [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
160 };
161
162
163 enum {
164 IFACE_ATTR_INTERFACE,
165 IFACE_ATTR_IFNAME,
166 IFACE_ATTR_UP,
167 IFACE_ATTR_DATA,
168 IFACE_ATTR_PREFIX,
169 IFACE_ATTR_ADDRESS,
170 IFACE_ATTR_MAX,
171 };
172
173 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
174 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
175 [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
176 [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
177 [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
178 [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
179 [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
180 };
181
182 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
183 {
184 struct blob_attr *tb[DUMP_ATTR_MAX];
185 blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
186
187 if (!tb[DUMP_ATTR_INTERFACE])
188 return;
189
190 free(dump);
191 dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
192 odhcpd_reload();
193 }
194
195
196 static void update_netifd(bool subscribe)
197 {
198 if (subscribe)
199 ubus_subscribe(ubus, &netifd, objid);
200
201 ubus_abort_request(ubus, &req_dump);
202 if (!ubus_invoke_async(ubus, objid, "dump", NULL, &req_dump)) {
203 req_dump.data_cb = handle_dump;
204 ubus_complete_request_async(ubus, &req_dump);
205 }
206 }
207
208
209 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
210 _unused struct ubus_request_data *req, _unused const char *method,
211 struct blob_attr *msg)
212 {
213 struct blob_attr *tb[IFACE_ATTR_MAX];
214 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
215
216 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
217 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
218 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
219 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
220
221 struct interface *c, *iface = NULL;
222 list_for_each_entry(c, &interfaces, head)
223 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
224 iface = c;
225
226 if (iface && iface->ignore)
227 return 0;
228
229 update_netifd(false);
230 return 0;
231 }
232
233
234 void ubus_apply_network(void)
235 {
236 struct blob_attr *a;
237 unsigned rem;
238
239 if (!dump)
240 return;
241
242 blobmsg_for_each_attr(a, dump, rem) {
243 struct blob_attr *tb[IFACE_ATTR_MAX];
244 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(a), blobmsg_data_len(a));
245
246 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
247 continue;
248
249 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
250 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
251 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
252 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
253
254 bool matched = false;
255 struct interface *c, *n;
256 list_for_each_entry_safe(c, n, &interfaces, head) {
257 char *f = memmem(c->upstream, c->upstream_len,
258 interface, strlen(interface) + 1);
259 bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
260 matched |= cmatched;
261
262 if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
263 continue;
264
265 if (!c->ignore)
266 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
267 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
268 }
269
270 if (!matched)
271 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
272 blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
273 }
274 }
275
276
277 enum {
278 OBJ_ATTR_ID,
279 OBJ_ATTR_PATH,
280 OBJ_ATTR_MAX
281 };
282
283 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
284 [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
285 [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
286 };
287
288
289 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
290 _unused const char *type, struct blob_attr *msg)
291 {
292 struct blob_attr *tb[OBJ_ATTR_MAX];
293 blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
294
295 if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
296 return;
297
298 if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
299 return;
300
301 objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
302 update_netifd(true);
303 }
304
305 static struct ubus_event_handler event_handler = { .cb = handle_event };
306
307
308 const char* ubus_get_ifname(const char *name)
309 {
310 struct blob_attr *c;
311 unsigned rem;
312
313 if (!dump)
314 return NULL;
315
316 blobmsg_for_each_attr(c, dump, rem) {
317 struct blob_attr *tb[IFACE_ATTR_MAX];
318 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
319
320 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
321 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
322 continue;
323
324 if (tb[IFACE_ATTR_IFNAME])
325 return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
326 }
327
328 return NULL;
329 }
330
331
332 bool ubus_has_prefix(const char *name, const char *ifname)
333 {
334 struct blob_attr *c, *cur;
335 unsigned rem;
336
337 if (!dump)
338 return NULL;
339
340 blobmsg_for_each_attr(c, dump, rem) {
341 struct blob_attr *tb[IFACE_ATTR_MAX];
342 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
343
344 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
345 continue;
346
347 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
348 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
349 continue;
350
351 if ((cur = tb[IFACE_ATTR_PREFIX])) {
352 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, false))
353 continue;
354
355 struct blob_attr *d;
356 unsigned drem;
357 blobmsg_for_each_attr(d, cur, drem) {
358 return true;
359 }
360 }
361 }
362
363 return false;
364 }
365
366
367 int init_ubus(void)
368 {
369 if (!(ubus = ubus_connect(NULL))) {
370 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
371 return -1;
372 }
373
374 netifd.cb = handle_update;
375 ubus_register_subscriber(ubus, &netifd);
376
377 ubus_add_uloop(ubus);
378 ubus_add_object(ubus, &main_object);
379 ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
380 if (!ubus_lookup_id(ubus, "network.interface", &objid))
381 update_netifd(true);
382
383 return 0;
384 }
385