ubus: Fix displayed valid paramater for both DHCPv4 and DHCPv6 lease
[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 INT32_MAX : (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
113 char *c = blobmsg_alloc_string_buffer(&b, NULL, INET6_ADDRSTRLEN);
114 inet_ntop(AF_INET6, &addr, c, INET6_ADDRSTRLEN);
115 blobmsg_add_string_buffer(&b);
116 }
117 blobmsg_close_table(&b, m);
118
119 blobmsg_add_u32(&b, "valid", INFINITE_VALID(lease->valid_until) ?
120 INT32_MAX : (uint32_t)(lease->valid_until - now));
121
122 blobmsg_close_table(&b, l);
123 }
124
125 blobmsg_close_array(&b, j);
126 blobmsg_close_table(&b, i);
127 }
128
129 blobmsg_close_table(&b, a);
130 ubus_send_reply(ctx, req, b.head);
131 return 0;
132 }
133
134
135 static struct ubus_method main_object_methods[] = {
136 {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
137 {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
138 };
139
140 static struct ubus_object_type main_object_type =
141 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
142
143 static struct ubus_object main_object = {
144 .name = "dhcp",
145 .type = &main_object_type,
146 .methods = main_object_methods,
147 .n_methods = ARRAY_SIZE(main_object_methods),
148 };
149
150
151 enum {
152 DUMP_ATTR_INTERFACE,
153 DUMP_ATTR_MAX
154 };
155
156 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
157 [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
158 };
159
160
161 enum {
162 IFACE_ATTR_INTERFACE,
163 IFACE_ATTR_IFNAME,
164 IFACE_ATTR_UP,
165 IFACE_ATTR_DATA,
166 IFACE_ATTR_PREFIX,
167 IFACE_ATTR_ADDRESS,
168 IFACE_ATTR_MAX,
169 };
170
171 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
172 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
173 [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
174 [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
175 [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
176 [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
177 [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
178 };
179
180 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
181 {
182 struct blob_attr *tb[DUMP_ATTR_MAX];
183 blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
184
185 if (!tb[DUMP_ATTR_INTERFACE])
186 return;
187
188 free(dump);
189 dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
190 odhcpd_reload();
191 }
192
193
194 static void update_netifd(bool subscribe)
195 {
196 if (subscribe)
197 ubus_subscribe(ubus, &netifd, objid);
198
199 ubus_abort_request(ubus, &req_dump);
200 if (!ubus_invoke_async(ubus, objid, "dump", NULL, &req_dump)) {
201 req_dump.data_cb = handle_dump;
202 ubus_complete_request_async(ubus, &req_dump);
203 }
204 }
205
206
207 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
208 _unused struct ubus_request_data *req, _unused const char *method,
209 struct blob_attr *msg)
210 {
211 struct blob_attr *tb[IFACE_ATTR_MAX];
212 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
213
214 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
215 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
216 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
217 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
218
219 struct interface *c, *iface = NULL;
220 list_for_each_entry(c, &interfaces, head)
221 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
222 iface = c;
223
224 if (iface && iface->ignore)
225 return 0;
226
227 update_netifd(false);
228 return 0;
229 }
230
231
232 void ubus_apply_network(void)
233 {
234 struct blob_attr *a;
235 unsigned rem;
236
237 if (!dump)
238 return;
239
240 blobmsg_for_each_attr(a, dump, rem) {
241 struct blob_attr *tb[IFACE_ATTR_MAX];
242 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(a), blobmsg_data_len(a));
243
244 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
245 continue;
246
247 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
248 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
249 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
250 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
251
252 bool matched = false;
253 struct interface *c, *n;
254 list_for_each_entry_safe(c, n, &interfaces, head) {
255 char *f = memmem(c->upstream, c->upstream_len,
256 interface, strlen(interface) + 1);
257 bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
258 matched |= cmatched;
259
260 if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
261 continue;
262
263 if (!c->ignore)
264 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
265 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
266 }
267
268 if (!matched)
269 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
270 blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
271 }
272 }
273
274
275 enum {
276 OBJ_ATTR_ID,
277 OBJ_ATTR_PATH,
278 OBJ_ATTR_MAX
279 };
280
281 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
282 [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
283 [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
284 };
285
286
287 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
288 _unused const char *type, struct blob_attr *msg)
289 {
290 struct blob_attr *tb[OBJ_ATTR_MAX];
291 blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
292
293 if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
294 return;
295
296 if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
297 return;
298
299 objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
300 update_netifd(true);
301 }
302
303 static struct ubus_event_handler event_handler = { .cb = handle_event };
304
305
306 const char* ubus_get_ifname(const char *name)
307 {
308 struct blob_attr *c;
309 unsigned rem;
310
311 if (!dump)
312 return NULL;
313
314 blobmsg_for_each_attr(c, dump, rem) {
315 struct blob_attr *tb[IFACE_ATTR_MAX];
316 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
317
318 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
319 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
320 continue;
321
322 if (tb[IFACE_ATTR_IFNAME])
323 return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
324 }
325
326 return NULL;
327 }
328
329
330 bool ubus_has_prefix(const char *name, const char *ifname)
331 {
332 struct blob_attr *c, *cur;
333 unsigned rem;
334
335 if (!dump)
336 return NULL;
337
338 blobmsg_for_each_attr(c, dump, rem) {
339 struct blob_attr *tb[IFACE_ATTR_MAX];
340 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
341
342 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
343 continue;
344
345 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
346 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
347 continue;
348
349 if ((cur = tb[IFACE_ATTR_PREFIX])) {
350 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, false))
351 continue;
352
353 struct blob_attr *d;
354 unsigned drem;
355 blobmsg_for_each_attr(d, cur, drem) {
356 return true;
357 }
358 }
359 }
360
361 return false;
362 }
363
364
365 int init_ubus(void)
366 {
367 if (!(ubus = ubus_connect(NULL))) {
368 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
369 return -1;
370 }
371
372 netifd.cb = handle_update;
373 ubus_register_subscriber(ubus, &netifd);
374
375 ubus_add_uloop(ubus);
376 ubus_add_object(ubus, &main_object);
377 ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
378 if (!ubus_lookup_id(ubus, "network.interface", &objid))
379 update_netifd(true);
380
381 return 0;
382 }
383