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