dhcpv4: cleanup dhcpv4_test usage
[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, "ip", 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, NULL, 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         if (!ubus_invoke_async(ubus, objid, "dump", NULL, &req_dump)) {
229                 req_dump.data_cb = handle_dump;
230                 ubus_complete_request_async(ubus, &req_dump);
231         }
232 }
233
234
235 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
236                 _unused struct ubus_request_data *req, _unused const char *method,
237                 struct blob_attr *msg)
238 {
239         struct blob_attr *tb[IFACE_ATTR_MAX];
240         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
241
242         const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
243                         blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
244         const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
245                         blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
246
247         struct interface *c, *iface = NULL;
248         list_for_each_entry(c, &interfaces, head)
249                 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
250                         iface = c;
251
252         if (iface && iface->ignore)
253                 return 0;
254
255         update_netifd(false);
256         return 0;
257 }
258
259
260 void ubus_apply_network(void)
261 {
262         struct blob_attr *a;
263         unsigned rem;
264
265         if (!dump)
266                 return;
267
268         blobmsg_for_each_attr(a, dump, rem) {
269                 struct blob_attr *tb[IFACE_ATTR_MAX];
270                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(a), blobmsg_data_len(a));
271
272                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
273                         continue;
274
275                 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
276                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
277                 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
278                                 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
279
280                 bool matched = false;
281                 struct interface *c, *n;
282                 list_for_each_entry_safe(c, n, &interfaces, head) {
283                         char *f = memmem(c->upstream, c->upstream_len,
284                                         interface, strlen(interface) + 1);
285                         bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
286                         matched |= cmatched;
287
288                         if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
289                                 continue;
290
291                         if (!c->ignore)
292                                 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
293                                                 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
294                 }
295
296                 if (!matched)
297                         config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
298                                         blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
299         }
300 }
301
302
303 enum {
304         OBJ_ATTR_ID,
305         OBJ_ATTR_PATH,
306         OBJ_ATTR_MAX
307 };
308
309 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
310         [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
311         [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
312 };
313
314
315 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
316                 _unused const char *type, struct blob_attr *msg)
317 {
318         struct blob_attr *tb[OBJ_ATTR_MAX];
319         blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
320
321         if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
322                 return;
323
324         if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
325                 return;
326
327         objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
328         update_netifd(true);
329 }
330
331 static struct ubus_event_handler event_handler = { .cb = handle_event };
332
333
334 const char* ubus_get_ifname(const char *name)
335 {
336         struct blob_attr *c;
337         unsigned rem;
338
339         if (!dump)
340                 return NULL;
341
342         blobmsg_for_each_attr(c, dump, rem) {
343                 struct blob_attr *tb[IFACE_ATTR_MAX];
344                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
345
346                 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
347                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
348                         continue;
349
350                 if (tb[IFACE_ATTR_IFNAME])
351                         return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
352         }
353
354         return NULL;
355 }
356
357
358 bool ubus_has_prefix(const char *name, const char *ifname)
359 {
360         struct blob_attr *c, *cur;
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] || !tb[IFACE_ATTR_IFNAME])
371                         continue;
372
373                 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
374                                 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
375                         continue;
376
377                 if ((cur = tb[IFACE_ATTR_PREFIX])) {
378                         if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, false))
379                                 continue;
380
381                         struct blob_attr *d;
382                         unsigned drem;
383                         blobmsg_for_each_attr(d, cur, drem) {
384                                 return true;
385                         }
386                 }
387         }
388
389         return false;
390 }
391
392
393 int init_ubus(void)
394 {
395         if (!(ubus = ubus_connect(NULL))) {
396                 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
397                 return -1;
398         }
399
400         netifd.cb = handle_update;
401         ubus_register_subscriber(ubus, &netifd);
402
403         ubus_add_uloop(ubus);
404         ubus_add_object(ubus, &main_object);
405         ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
406         if (!ubus_lookup_id(ubus, "network.interface", &objid))
407                 update_netifd(true);
408
409         return 0;
410 }
411