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