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