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