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