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