dhcpv4: avoid segfault when there's no IPv4 prefix
[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_u8(&b, "accept-reconf", a->accept_reconf);
132                         blobmsg_add_u32(&b, "assigned", a->assigned);
133
134                         m = blobmsg_open_array(&b, "flags");
135                         if (a->flags & OAF_BOUND)
136                                 blobmsg_add_string(&b, NULL, "bound");
137
138                         if (a->flags & OAF_STATIC)
139                                 blobmsg_add_string(&b, NULL, "static");
140                         blobmsg_close_array(&b, m);
141
142                         m = blobmsg_open_array(&b, a->length == 128 ? "ipv6-addr": "ipv6-prefix");
143                         dhcpv6_enum_ia_addrs(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL);
144                         blobmsg_close_table(&b, m);
145
146                         blobmsg_add_u32(&b, "valid", INFINITE_VALID(a->valid_until) ?
147                                                 (uint32_t)-1 : (uint32_t)(a->valid_until - now));
148
149                         blobmsg_close_table(&b, l);
150                 }
151
152                 blobmsg_close_array(&b, j);
153                 blobmsg_close_table(&b, i);
154         }
155
156         blobmsg_close_table(&b, a);
157         ubus_send_reply(ctx, req, b.head);
158         return 0;
159 }
160
161
162 static struct ubus_method main_object_methods[] = {
163         {.name = "ipv4leases", .handler = handle_dhcpv4_leases},
164         {.name = "ipv6leases", .handler = handle_dhcpv6_leases},
165 };
166
167 static struct ubus_object_type main_object_type =
168                 UBUS_OBJECT_TYPE("dhcp", main_object_methods);
169
170 static struct ubus_object main_object = {
171         .name = "dhcp",
172         .type = &main_object_type,
173         .methods = main_object_methods,
174         .n_methods = ARRAY_SIZE(main_object_methods),
175 };
176
177
178 enum {
179         DUMP_ATTR_INTERFACE,
180         DUMP_ATTR_MAX
181 };
182
183 static const struct blobmsg_policy dump_attrs[DUMP_ATTR_MAX] = {
184         [DUMP_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_ARRAY },
185 };
186
187
188 enum {
189         IFACE_ATTR_INTERFACE,
190         IFACE_ATTR_IFNAME,
191         IFACE_ATTR_UP,
192         IFACE_ATTR_DATA,
193         IFACE_ATTR_PREFIX,
194         IFACE_ATTR_ADDRESS,
195         IFACE_ATTR_MAX,
196 };
197
198 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
199         [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
200         [IFACE_ATTR_IFNAME] = { .name = "l3_device", .type = BLOBMSG_TYPE_STRING },
201         [IFACE_ATTR_UP] = { .name = "up", .type = BLOBMSG_TYPE_BOOL },
202         [IFACE_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
203         [IFACE_ATTR_PREFIX] = { .name = "ipv6-prefix", .type = BLOBMSG_TYPE_ARRAY },
204         [IFACE_ATTR_ADDRESS] = { .name = "ipv6-address", .type = BLOBMSG_TYPE_ARRAY },
205 };
206
207 static void handle_dump(_unused struct ubus_request *req, _unused int type, struct blob_attr *msg)
208 {
209         struct blob_attr *tb[DUMP_ATTR_MAX];
210         blobmsg_parse(dump_attrs, DUMP_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
211
212         if (!tb[DUMP_ATTR_INTERFACE])
213                 return;
214
215         free(dump);
216         dump = blob_memdup(tb[DUMP_ATTR_INTERFACE]);
217         odhcpd_reload();
218 }
219
220
221 static void update_netifd(bool subscribe)
222 {
223         if (subscribe)
224                 ubus_subscribe(ubus, &netifd, objid);
225
226         ubus_abort_request(ubus, &req_dump);
227         if (!ubus_invoke_async(ubus, objid, "dump", NULL, &req_dump)) {
228                 req_dump.data_cb = handle_dump;
229                 ubus_complete_request_async(ubus, &req_dump);
230         }
231 }
232
233
234 static int handle_update(_unused struct ubus_context *ctx, _unused struct ubus_object *obj,
235                 _unused struct ubus_request_data *req, _unused const char *method,
236                 struct blob_attr *msg)
237 {
238         struct blob_attr *tb[IFACE_ATTR_MAX];
239         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
240
241         const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
242                         blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
243         const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
244                         blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
245
246         struct interface *c, *iface = NULL;
247         list_for_each_entry(c, &interfaces, head)
248                 if (!strcmp(interface, c->name) || !strcmp(ifname, c->ifname))
249                         iface = c;
250
251         if (iface && iface->ignore)
252                 return 0;
253
254         update_netifd(false);
255         return 0;
256 }
257
258
259 void ubus_apply_network(void)
260 {
261         struct blob_attr *a;
262         unsigned rem;
263
264         if (!dump)
265                 return;
266
267         blobmsg_for_each_attr(a, dump, rem) {
268                 struct blob_attr *tb[IFACE_ATTR_MAX];
269                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(a), blobmsg_data_len(a));
270
271                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_DATA])
272                         continue;
273
274                 const char *interface = (tb[IFACE_ATTR_INTERFACE]) ?
275                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]) : "";
276                 const char *ifname = (tb[IFACE_ATTR_IFNAME]) ?
277                                 blobmsg_get_string(tb[IFACE_ATTR_IFNAME]) : "";
278
279                 bool matched = false;
280                 struct interface *c, *n;
281                 list_for_each_entry_safe(c, n, &interfaces, head) {
282                         char *f = memmem(c->upstream, c->upstream_len,
283                                         interface, strlen(interface) + 1);
284                         bool cmatched = !strcmp(interface, c->name) || !strcmp(ifname, c->ifname);
285                         matched |= cmatched;
286
287                         if (!cmatched && (!c->upstream_len || !f || (f != c->upstream && f[-1] != 0)))
288                                 continue;
289
290                         if (!c->ignore)
291                                 config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
292                                                 blobmsg_data_len(tb[IFACE_ATTR_DATA]), c->name, false);
293                 }
294
295                 if (!matched)
296                         config_parse_interface(blobmsg_data(tb[IFACE_ATTR_DATA]),
297                                         blobmsg_data_len(tb[IFACE_ATTR_DATA]), interface, false);
298         }
299 }
300
301
302 enum {
303         OBJ_ATTR_ID,
304         OBJ_ATTR_PATH,
305         OBJ_ATTR_MAX
306 };
307
308 static const struct blobmsg_policy obj_attrs[OBJ_ATTR_MAX] = {
309         [OBJ_ATTR_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
310         [OBJ_ATTR_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
311 };
312
313
314 static void handle_event(_unused struct ubus_context *ctx, _unused struct ubus_event_handler *ev,
315                 _unused const char *type, struct blob_attr *msg)
316 {
317         struct blob_attr *tb[OBJ_ATTR_MAX];
318         blobmsg_parse(obj_attrs, OBJ_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
319
320         if (!tb[OBJ_ATTR_ID] || !tb[OBJ_ATTR_PATH])
321                 return;
322
323         if (strcmp(blobmsg_get_string(tb[OBJ_ATTR_PATH]), "network.interface"))
324                 return;
325
326         objid = blobmsg_get_u32(tb[OBJ_ATTR_ID]);
327         update_netifd(true);
328 }
329
330 static struct ubus_event_handler event_handler = { .cb = handle_event };
331
332
333 const char* ubus_get_ifname(const char *name)
334 {
335         struct blob_attr *c;
336         unsigned rem;
337
338         if (!dump)
339                 return NULL;
340
341         blobmsg_for_each_attr(c, dump, rem) {
342                 struct blob_attr *tb[IFACE_ATTR_MAX];
343                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
344
345                 if (!tb[IFACE_ATTR_INTERFACE] || strcmp(name,
346                                 blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])))
347                         continue;
348
349                 if (tb[IFACE_ATTR_IFNAME])
350                         return blobmsg_get_string(tb[IFACE_ATTR_IFNAME]);
351         }
352
353         return NULL;
354 }
355
356
357 bool ubus_has_prefix(const char *name, const char *ifname)
358 {
359         struct blob_attr *c, *cur;
360         unsigned rem;
361
362         if (!dump)
363                 return NULL;
364
365         blobmsg_for_each_attr(c, dump, rem) {
366                 struct blob_attr *tb[IFACE_ATTR_MAX];
367                 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blobmsg_data(c), blobmsg_data_len(c));
368
369                 if (!tb[IFACE_ATTR_INTERFACE] || !tb[IFACE_ATTR_IFNAME])
370                         continue;
371
372                 if (strcmp(name, blobmsg_get_string(tb[IFACE_ATTR_INTERFACE])) ||
373                                 strcmp(ifname, blobmsg_get_string(tb[IFACE_ATTR_IFNAME])))
374                         continue;
375
376                 if ((cur = tb[IFACE_ATTR_PREFIX])) {
377                         if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY || !blobmsg_check_attr(cur, false))
378                                 continue;
379
380                         struct blob_attr *d;
381                         unsigned drem;
382                         blobmsg_for_each_attr(d, cur, drem) {
383                                 return true;
384                         }
385                 }
386         }
387
388         return false;
389 }
390
391
392 int init_ubus(void)
393 {
394         if (!(ubus = ubus_connect(NULL))) {
395                 syslog(LOG_ERR, "Unable to connect to ubus: %s", strerror(errno));
396                 return -1;
397         }
398
399         netifd.cb = handle_update;
400         ubus_register_subscriber(ubus, &netifd);
401
402         ubus_add_uloop(ubus);
403         ubus_add_object(ubus, &main_object);
404         ubus_register_event_handler(ubus, &event_handler, "ubus.object.add");
405         if (!ubus_lookup_id(ubus, "network.interface", &objid))
406                 update_netifd(true);
407
408         return 0;
409 }
410