Fix illegal memory access
[project/odhcpd.git] / src / config.c
1 #include <resolv.h>
2 #include <signal.h>
3 #include <arpa/inet.h>
4
5 #include <uci.h>
6 #include <uci_blob.h>
7
8 #include "odhcpd.h"
9
10 static struct blob_buf b;
11 struct list_head leases = LIST_HEAD_INIT(leases);
12 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
13 struct config config = {false, NULL, NULL};
14
15 enum {
16 IFACE_ATTR_INTERFACE,
17 IFACE_ATTR_IFNAME,
18 IFACE_ATTR_NETWORKID,
19 IFACE_ATTR_DYNAMICDHCP,
20 IFACE_ATTR_IGNORE,
21 IFACE_ATTR_LEASETIME,
22 IFACE_ATTR_LIMIT,
23 IFACE_ATTR_START,
24 IFACE_ATTR_MASTER,
25 IFACE_ATTR_UPSTREAM,
26 IFACE_ATTR_RA,
27 IFACE_ATTR_DHCPV4,
28 IFACE_ATTR_DHCPV6,
29 IFACE_ATTR_NDP,
30 IFACE_ATTR_DNS,
31 IFACE_ATTR_DOMAIN,
32 IFACE_ATTR_ULA_COMPAT,
33 IFACE_ATTR_RA_DEFAULT,
34 IFACE_ATTR_RA_MANAGEMENT,
35 IFACE_ATTR_RA_OFFLINK,
36 IFACE_ATTR_RA_PREFERENCE,
37 IFACE_ATTR_NDPROXY_ROUTING,
38 IFACE_ATTR_NDPROXY_SLAVE,
39 IFACE_ATTR_NDPROXY_STATIC,
40 IFACE_ATTR_MAX
41 };
42
43 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
44 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
45 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
46 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
47 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
48 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
49 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
50 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
51 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
52 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
53 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
54 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
55 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
56 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
57 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
58 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
59 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
60 [IFACE_ATTR_ULA_COMPAT] = { .name = "ula_compat", .type = BLOBMSG_TYPE_BOOL },
61 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
62 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
63 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
64 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
65 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
66 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
67 [IFACE_ATTR_NDPROXY_STATIC] = { .name = "ndproxy_static", .type = BLOBMSG_TYPE_ARRAY },
68 };
69
70 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
71 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
73 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
74 [IFACE_ATTR_NDPROXY_STATIC] = { .type = BLOBMSG_TYPE_STRING },
75 };
76
77 const struct uci_blob_param_list interface_attr_list = {
78 .n_params = IFACE_ATTR_MAX,
79 .params = iface_attrs,
80 .info = iface_attr_info,
81 };
82
83
84 enum {
85 LEASE_ATTR_IP,
86 LEASE_ATTR_MAC,
87 LEASE_ATTR_DUID,
88 LEASE_ATTR_HOSTID,
89 LEASE_ATTR_HOSTNAME,
90 LEASE_ATTR_MAX
91 };
92
93
94 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
95 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
96 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
97 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
98 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
99 [LEASE_ATTR_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
100 };
101
102
103 const struct uci_blob_param_list lease_attr_list = {
104 .n_params = LEASE_ATTR_MAX,
105 .params = lease_attrs,
106 };
107
108
109 enum {
110 ODHCPD_ATTR_LEGACY,
111 ODHCPD_ATTR_LEASEFILE,
112 ODHCPD_ATTR_LEASETRIGGER,
113 ODHCPD_ATTR_MAX
114 };
115
116
117 static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
118 [ODHCPD_ATTR_LEGACY] = { .name = "legacy", .type = BLOBMSG_TYPE_BOOL },
119 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
120 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
121 };
122
123
124 const struct uci_blob_param_list odhcpd_attr_list = {
125 .n_params = ODHCPD_ATTR_MAX,
126 .params = odhcpd_attrs,
127 };
128
129
130 static struct interface* get_interface(const char *name)
131 {
132 struct interface *c;
133 list_for_each_entry(c, &interfaces, head)
134 if (!strcmp(c->name, name))
135 return c;
136 return NULL;
137 }
138
139
140 static void clean_interface(struct interface *iface)
141 {
142 free(iface->dns);
143 free(iface->search);
144 free(iface->upstream);
145 free(iface->static_ndp);
146 free(iface->dhcpv4_dns);
147 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
148 }
149
150
151 static void close_interface(struct interface *iface)
152 {
153 if (iface->head.next)
154 list_del(&iface->head);
155
156 setup_router_interface(iface, false);
157 setup_dhcpv6_interface(iface, false);
158 setup_ndp_interface(iface, false);
159 setup_dhcpv4_interface(iface, false);
160
161 clean_interface(iface);
162 free(iface);
163 }
164
165
166 static int parse_mode(const char *mode)
167 {
168 if (!strcmp(mode, "disabled")) {
169 return RELAYD_DISABLED;
170 } else if (!strcmp(mode, "server")) {
171 return RELAYD_SERVER;
172 } else if (!strcmp(mode, "relay")) {
173 return RELAYD_RELAY;
174 } else if (!strcmp(mode, "hybrid")) {
175 return RELAYD_HYBRID;
176 } else {
177 return -1;
178 }
179 }
180
181
182 static void set_config(struct uci_section *s)
183 {
184 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
185
186 blob_buf_init(&b, 0);
187 uci_to_blob(&b, s, &lease_attr_list);
188 blobmsg_parse(lease_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
189
190 if ((c = tb[ODHCPD_ATTR_LEGACY]))
191 config.legacy = blobmsg_get_bool(c);
192
193 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
194 free(config.dhcp_statefile);
195 config.dhcp_statefile = strdup(blobmsg_get_string(c));
196 }
197
198 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
199 free(config.dhcp_cb);
200 config.dhcp_cb = strdup(blobmsg_get_string(c));
201 }
202 }
203
204
205 static int set_lease(struct uci_section *s)
206 {
207 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
208
209 blob_buf_init(&b, 0);
210 uci_to_blob(&b, s, &lease_attr_list);
211 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
212
213 size_t hostlen = 1;
214 if ((c = tb[LEASE_ATTR_HOSTNAME]))
215 hostlen = blobmsg_data_len(c);
216
217 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
218
219 if (hostlen > 1)
220 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
221
222 if ((c = tb[LEASE_ATTR_IP]))
223 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
224 goto err;
225
226 if ((c = tb[LEASE_ATTR_MAC]))
227 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
228 goto err;
229
230 if ((c = tb[LEASE_ATTR_DUID])) {
231 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
232 lease->duid = malloc(duidlen);
233 ssize_t len = odhcpd_unhexlify(lease->duid,
234 duidlen, blobmsg_get_string(c));
235
236 if (len < 0)
237 goto err;
238
239 lease->duid_len = len;
240 }
241
242 if ((c = tb[LEASE_ATTR_HOSTID]))
243 if (odhcpd_unhexlify((uint8_t*)&lease->hostid, sizeof(lease->hostid),
244 blobmsg_get_string(c)) < 0)
245 goto err;
246
247 list_add(&lease->head, &leases);
248 return 0;
249
250 err:
251 free(lease->duid);
252 free(lease);
253 return -1;
254 }
255
256
257 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
258 {
259 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
260 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
261
262 if (tb[IFACE_ATTR_INTERFACE])
263 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
264
265 if (!name)
266 return -1;
267
268 struct interface *iface = get_interface(name);
269 if (!iface) {
270 iface = calloc(1, sizeof(*iface));
271 strncpy(iface->name, name, sizeof(iface->name) - 1);
272 list_add(&iface->head, &interfaces);
273 } else if (overwrite) {
274 clean_interface(iface);
275 }
276
277 const char *ifname = NULL;
278 #ifdef WITH_UBUS
279 if (overwrite)
280 ifname = ubus_get_ifname(name);
281 #endif
282 if ((c = tb[IFACE_ATTR_IFNAME]))
283 ifname = blobmsg_get_string(c);
284 else if ((c = tb[IFACE_ATTR_NETWORKID]))
285 ifname = blobmsg_get_string(c);
286
287 if (!iface->ifname[0] && !ifname)
288 return -1;
289
290 if (ifname)
291 strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
292
293 iface->inuse = true;
294
295 if (overwrite)
296 clean_interface(iface);
297
298 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
299 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
300
301 if ((c = tb[IFACE_ATTR_IGNORE]))
302 iface->ignore = blobmsg_get_bool(c);
303
304 if ((c = tb[IFACE_ATTR_LEASETIME])) {
305 char *val = blobmsg_get_string(c), *endptr;
306 double time = strtod(val, &endptr);
307 if (time && endptr[0]) {
308 if (endptr[0] == 's')
309 time *= 1;
310 else if (endptr[0] == 'm')
311 time *= 60;
312 else if (endptr[0] == 'h')
313 time *= 3600;
314 else if (endptr[0] == 'd')
315 time *= 24 * 3600;
316 else if (endptr[0] == 'w')
317 time *= 7 * 24 * 3600;
318 else
319 goto err;
320 }
321
322 if (time >= 60)
323 iface->dhcpv4_leasetime = time;
324 }
325
326 if ((c = tb[IFACE_ATTR_START])) {
327 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
328
329 if (config.legacy)
330 iface->dhcpv4 = RELAYD_SERVER;
331 }
332
333 if ((c = tb[IFACE_ATTR_LIMIT]))
334 iface->dhcpv4_end.s_addr = htonl(
335 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
336
337 if ((c = tb[IFACE_ATTR_MASTER]))
338 iface->master = blobmsg_get_bool(c);
339
340 if ((c = tb[IFACE_ATTR_UPSTREAM])) {
341 struct blob_attr *cur;
342 unsigned rem;
343
344 blobmsg_for_each_attr(cur, c, rem) {
345 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
346 continue;
347
348 iface->upstream = realloc(iface->upstream,
349 iface->upstream_len + blobmsg_data_len(cur));
350 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
351 iface->upstream_len += blobmsg_data_len(cur);
352 }
353 }
354
355 int mode;
356 if ((c = tb[IFACE_ATTR_RA])) {
357 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
358 iface->ra = mode;
359 else
360 goto err;
361 }
362
363 if ((c = tb[IFACE_ATTR_DHCPV4])) {
364 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
365 iface->dhcpv4 = mode;
366 else
367 goto err;
368 }
369
370 if ((c = tb[IFACE_ATTR_DHCPV6])) {
371 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
372 iface->dhcpv6 = mode;
373 else
374 goto err;
375 }
376
377 if ((c = tb[IFACE_ATTR_NDP])) {
378 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
379 iface->ndp = mode;
380 else
381 goto err;
382 }
383
384 if ((c = tb[IFACE_ATTR_DNS])) {
385 struct blob_attr *cur;
386 unsigned rem;
387
388 iface->always_rewrite_dns = true;
389 blobmsg_for_each_attr(cur, c, rem) {
390 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
391 continue;
392
393 struct in_addr addr4;
394 struct in6_addr addr6;
395 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
396 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
397 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
398 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
399 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
400 iface->dns = realloc(iface->dns,
401 (++iface->dns_cnt) * sizeof(*iface->dns));
402 iface->dns[iface->dns_cnt - 1] = addr6;
403 } else {
404 goto err;
405 }
406 }
407 }
408
409 if ((c = tb[IFACE_ATTR_DOMAIN])) {
410 struct blob_attr *cur;
411 unsigned rem;
412
413 blobmsg_for_each_attr(cur, c, rem) {
414 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
415 continue;
416
417 uint8_t buf[256];
418 int len = dn_comp(blobmsg_get_string(cur), buf, sizeof(buf), NULL, NULL);
419 if (len <= 0)
420 goto err;
421
422 iface->search = realloc(iface->search, iface->search_len + len);
423 memcpy(&iface->search[iface->search_len], buf, len);
424 iface->search_len += len;
425 }
426 }
427
428 if ((c = tb[IFACE_ATTR_ULA_COMPAT]))
429 iface->deprecate_ula_if_public_avail = blobmsg_get_bool(c);
430
431 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
432 iface->default_router = blobmsg_get_u32(c);
433
434 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
435 iface->managed = blobmsg_get_u32(c);
436
437 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
438 iface->ra_not_onlink = blobmsg_get_bool(c);
439
440 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
441 const char *prio = blobmsg_get_string(c);
442
443 if (!strcmp(prio, "high"))
444 iface->route_preference = 1;
445 else if (!strcmp(prio, "low"))
446 iface->route_preference = -1;
447 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
448 iface->route_preference = 0;
449 else
450 goto err;
451 }
452
453 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
454 iface->learn_routes = blobmsg_get_bool(c);
455
456 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
457 iface->external = blobmsg_get_bool(c);
458
459 if ((c = tb[IFACE_ATTR_NDPROXY_STATIC])) {
460 struct blob_attr *cur;
461 unsigned rem;
462
463 blobmsg_for_each_attr(cur, c, rem) {
464 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
465 continue;
466
467 int len = blobmsg_data_len(cur);
468 iface->static_ndp = realloc(iface->static_ndp, iface->static_ndp_len + len);
469 memcpy(&iface->static_ndp[iface->static_ndp_len], blobmsg_get_string(cur), len);
470 iface->static_ndp_len += len;
471 }
472 }
473
474 iface->ignore = (iface->ifindex = if_nametoindex(iface->ifname)) < 0;
475 return 0;
476
477 err:
478 close_interface(iface);
479 return -1;
480 }
481
482 static int set_interface(struct uci_section *s)
483 {
484 blob_buf_init(&b, 0);
485 uci_to_blob(&b, s, &interface_attr_list);
486 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
487 }
488
489
490 static volatile int do_reload = false;
491 static void set_stop(int signal)
492 {
493 uloop_end();
494 do_reload = (signal == SIGHUP);
495 }
496
497 void odhcpd_run(void)
498 {
499 struct uci_context *uci = uci_alloc_context();
500 signal(SIGTERM, set_stop);
501 signal(SIGHUP, set_stop);
502 signal(SIGINT, set_stop);
503
504 #ifdef WITH_UBUS
505 init_ubus();
506 #endif
507
508 do {
509 do_reload = uloop_cancelled = false;
510
511 struct lease *l;
512 list_for_each_entry(l, &leases, head) {
513 list_del(&l->head);
514 free(l->duid);
515 free(l);
516 }
517
518 struct uci_package *dhcp = NULL;
519 if (!uci_load(uci, "dhcp", &dhcp)) {
520 struct uci_element *e;
521 uci_foreach_element(&dhcp->sections, e) {
522 struct uci_section *s = uci_to_section(e);
523 if (!strcmp(s->type, "lease"))
524 set_lease(s);
525 else if (!strcmp(s->type, "odhcpd"))
526 set_config(s);
527 }
528
529 uci_foreach_element(&dhcp->sections, e) {
530 struct uci_section *s = uci_to_section(e);
531 if (!strcmp(s->type, "dhcp"))
532 set_interface(s);
533 }
534 }
535
536 #ifdef WITH_UBUS
537 ubus_apply_network();
538 #endif
539
540 // Evaluate hybrid mode for master
541 struct interface *master = NULL, *i, *n;
542 list_for_each_entry(i, &interfaces, head) {
543 if (!i->master)
544 continue;
545
546 enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
547 #ifdef WITH_UBUS
548 if (ubus_has_prefix(i->name, i->ifname))
549 hybrid_mode = RELAYD_RELAY;
550 #endif
551
552 if (i->dhcpv6 == RELAYD_HYBRID)
553 i->dhcpv6 = hybrid_mode;
554
555 if (i->ra == RELAYD_HYBRID)
556 i->ra = hybrid_mode;
557
558 if (i->ndp == RELAYD_HYBRID)
559 i->ndp = hybrid_mode;
560
561 if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
562 master = i;
563 }
564
565
566 list_for_each_entry_safe(i, n, &interfaces, head) {
567 if (i->inuse) {
568 // Resolve hybrid mode
569 if (i->dhcpv6 == RELAYD_HYBRID)
570 i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
571 RELAYD_RELAY : RELAYD_SERVER;
572
573 if (i->ra == RELAYD_HYBRID)
574 i->ra = (master && master->ra == RELAYD_RELAY) ?
575 RELAYD_RELAY : RELAYD_SERVER;
576
577 if (i->ndp == RELAYD_HYBRID)
578 i->ndp = (master && master->ndp == RELAYD_RELAY) ?
579 RELAYD_RELAY : RELAYD_SERVER;
580
581 setup_router_interface(i, true);
582 setup_dhcpv6_interface(i, true);
583 setup_ndp_interface(i, true);
584 setup_dhcpv4_interface(i, true);
585 } else {
586 close_interface(i);
587 }
588 }
589
590 uloop_run();
591
592 if (dhcp)
593 uci_unload(uci, dhcp);
594 } while (do_reload);
595 }
596