12 static struct blob_buf b;
13 static int reload_pipe[2];
14 struct list_head leases = LIST_HEAD_INIT(leases);
15 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
16 struct config config = {false, NULL, NULL};
22 IFACE_ATTR_DYNAMICDHCP,
36 IFACE_ATTR_FILTER_CLASS,
37 IFACE_ATTR_DHCPV6_RAW,
38 IFACE_ATTR_RA_DEFAULT,
39 IFACE_ATTR_RA_MANAGEMENT,
40 IFACE_ATTR_RA_OFFLINK,
41 IFACE_ATTR_RA_PREFERENCE,
42 IFACE_ATTR_RA_ADVROUTER,
43 IFACE_ATTR_PD_MANAGER,
45 IFACE_ATTR_NDPROXY_ROUTING,
46 IFACE_ATTR_NDPROXY_SLAVE,
50 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
51 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
52 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
54 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
55 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
57 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
58 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
59 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
60 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
61 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
62 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
65 [IFACE_ATTR_ROUTER] = { .name = "router", .type = BLOBMSG_TYPE_ARRAY },
66 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
67 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
68 [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
69 [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
73 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
74 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
75 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
76 [IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
77 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
78 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
81 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
82 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
83 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
84 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
87 const struct uci_blob_param_list interface_attr_list = {
88 .n_params = IFACE_ATTR_MAX,
89 .params = iface_attrs,
90 .info = iface_attr_info,
104 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
105 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
106 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
107 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
108 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
109 [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
113 const struct uci_blob_param_list lease_attr_list = {
114 .n_params = LEASE_ATTR_MAX,
115 .params = lease_attrs,
120 ODHCPD_ATTR_MAINDHCP,
121 ODHCPD_ATTR_LEASEFILE,
122 ODHCPD_ATTR_LEASETRIGGER,
127 static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
128 [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
129 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
130 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
134 const struct uci_blob_param_list odhcpd_attr_list = {
135 .n_params = ODHCPD_ATTR_MAX,
136 .params = odhcpd_attrs,
140 static struct interface* get_interface(const char *name)
143 list_for_each_entry(c, &interfaces, head)
144 if (!strcmp(c->name, name))
150 static void clean_interface(struct interface *iface)
154 free(iface->upstream);
155 free(iface->dhcpv4_router);
156 free(iface->dhcpv4_dns);
157 free(iface->dhcpv6_raw);
158 free(iface->filter_class);
159 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
163 static void close_interface(struct interface *iface)
165 if (iface->head.next)
166 list_del(&iface->head);
168 setup_router_interface(iface, false);
169 setup_dhcpv6_interface(iface, false);
170 setup_ndp_interface(iface, false);
171 setup_dhcpv4_interface(iface, false);
173 clean_interface(iface);
178 static int parse_mode(const char *mode)
180 if (!strcmp(mode, "disabled")) {
181 return RELAYD_DISABLED;
182 } else if (!strcmp(mode, "server")) {
183 return RELAYD_SERVER;
184 } else if (!strcmp(mode, "relay")) {
186 } else if (!strcmp(mode, "hybrid")) {
187 return RELAYD_HYBRID;
194 static void set_config(struct uci_section *s)
196 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
198 blob_buf_init(&b, 0);
199 uci_to_blob(&b, s, &odhcpd_attr_list);
200 blobmsg_parse(odhcpd_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
202 if ((c = tb[ODHCPD_ATTR_MAINDHCP]))
203 config.legacy = blobmsg_get_bool(c);
205 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
206 free(config.dhcp_statefile);
207 config.dhcp_statefile = strdup(blobmsg_get_string(c));
210 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
211 free(config.dhcp_cb);
212 config.dhcp_cb = strdup(blobmsg_get_string(c));
217 static int set_lease(struct uci_section *s)
219 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
221 blob_buf_init(&b, 0);
222 uci_to_blob(&b, s, &lease_attr_list);
223 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
226 if ((c = tb[LEASE_ATTR_NAME]))
227 hostlen = blobmsg_data_len(c);
229 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
234 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
236 if ((c = tb[LEASE_ATTR_IP]))
237 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
240 if ((c = tb[LEASE_ATTR_MAC]))
241 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
244 if ((c = tb[LEASE_ATTR_DUID])) {
245 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
246 lease->duid = malloc(duidlen);
250 ssize_t len = odhcpd_unhexlify(lease->duid,
251 duidlen, blobmsg_get_string(c));
256 lease->duid_len = len;
259 if ((c = tb[LEASE_ATTR_HOSTID])) {
261 lease->hostid = strtoul(blobmsg_get_string(c), NULL, 16);
266 list_add(&lease->head, &leases);
278 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
280 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
281 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
283 if (tb[IFACE_ATTR_INTERFACE])
284 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
289 struct interface *iface = get_interface(name);
291 iface = calloc(1, sizeof(*iface));
295 strncpy(iface->name, name, sizeof(iface->name) - 1);
296 list_add(&iface->head, &interfaces);
300 const char *ifname = NULL;
302 if (overwrite || !iface->ifname[0])
303 ifname = ubus_get_ifname(name);
307 if ((c = tb[IFACE_ATTR_IFNAME]))
308 ifname = blobmsg_get_string(c);
309 else if ((c = tb[IFACE_ATTR_NETWORKID]))
310 ifname = blobmsg_get_string(c);
313 if (!iface->ifname[0] && !ifname)
317 strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
319 if ((iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
324 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
325 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
327 if (overwrite && (c = tb[IFACE_ATTR_IGNORE]))
328 iface->ignore = blobmsg_get_bool(c);
330 if ((c = tb[IFACE_ATTR_LEASETIME])) {
331 char *val = blobmsg_get_string(c), *endptr;
332 double time = strtod(val, &endptr);
333 if (time && endptr[0]) {
334 if (endptr[0] == 's')
336 else if (endptr[0] == 'm')
338 else if (endptr[0] == 'h')
340 else if (endptr[0] == 'd')
342 else if (endptr[0] == 'w')
343 time *= 7 * 24 * 3600;
349 iface->dhcpv4_leasetime = time;
352 if ((c = tb[IFACE_ATTR_START])) {
353 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
356 iface->dhcpv4 = RELAYD_SERVER;
359 if ((c = tb[IFACE_ATTR_LIMIT]))
360 iface->dhcpv4_end.s_addr = htonl(
361 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
363 if ((c = tb[IFACE_ATTR_MASTER]))
364 iface->master = blobmsg_get_bool(c);
366 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
367 struct blob_attr *cur;
370 blobmsg_for_each_attr(cur, c, rem) {
371 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
374 iface->upstream = realloc(iface->upstream,
375 iface->upstream_len + blobmsg_data_len(cur));
376 if (!iface->upstream)
379 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
380 iface->upstream_len += blobmsg_data_len(cur);
385 if ((c = tb[IFACE_ATTR_RA])) {
386 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
392 if ((c = tb[IFACE_ATTR_DHCPV4])) {
393 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
394 iface->dhcpv4 = mode;
399 if ((c = tb[IFACE_ATTR_DHCPV6])) {
400 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
401 iface->dhcpv6 = mode;
406 if ((c = tb[IFACE_ATTR_NDP])) {
407 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
413 if ((c = tb[IFACE_ATTR_ROUTER])) {
414 struct blob_attr *cur;
417 blobmsg_for_each_attr(cur, c, rem) {
418 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
421 struct in_addr addr4;
422 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
423 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
424 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
425 if (!iface->dhcpv4_router)
428 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
435 if ((c = tb[IFACE_ATTR_DNS])) {
436 struct blob_attr *cur;
439 iface->always_rewrite_dns = true;
440 blobmsg_for_each_attr(cur, c, rem) {
441 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
444 struct in_addr addr4;
445 struct in6_addr addr6;
446 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
447 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
448 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
449 if (!iface->dhcpv4_dns)
452 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
453 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
454 iface->dns = realloc(iface->dns,
455 (++iface->dns_cnt) * sizeof(*iface->dns));
459 iface->dns[iface->dns_cnt - 1] = addr6;
466 if ((c = tb[IFACE_ATTR_DOMAIN])) {
467 struct blob_attr *cur;
470 blobmsg_for_each_attr(cur, c, rem) {
471 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
475 char *domain = blobmsg_get_string(cur);
476 size_t domainlen = strlen(domain);
477 if (domainlen > 0 && domain[domainlen - 1] == '.')
478 domain[domainlen - 1] = 0;
480 int len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
484 iface->search = realloc(iface->search, iface->search_len + len);
488 memcpy(&iface->search[iface->search_len], buf, len);
489 iface->search_len += len;
493 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
494 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
495 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
498 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
499 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
500 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
501 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
504 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
505 iface->default_router = blobmsg_get_u32(c);
507 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
508 iface->managed = blobmsg_get_u32(c);
512 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
513 iface->ra_not_onlink = blobmsg_get_bool(c);
515 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
516 iface->ra_advrouter = blobmsg_get_bool(c);
518 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
519 const char *prio = blobmsg_get_string(c);
521 if (!strcmp(prio, "high"))
522 iface->route_preference = 1;
523 else if (!strcmp(prio, "low"))
524 iface->route_preference = -1;
525 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
526 iface->route_preference = 0;
531 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
532 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
533 sizeof(iface->dhcpv6_pd_manager) - 1);
535 if ((c = tb[IFACE_ATTR_PD_CER]) &&
536 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
539 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
540 iface->learn_routes = blobmsg_get_bool(c);
542 iface->learn_routes = true;
544 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
545 iface->external = blobmsg_get_bool(c);
550 close_interface(iface);
554 static int set_interface(struct uci_section *s)
556 blob_buf_init(&b, 0);
557 uci_to_blob(&b, s, &interface_attr_list);
558 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
562 void odhcpd_reload(void)
564 struct uci_context *uci = uci_alloc_context();
565 while (!list_empty(&leases)) {
566 struct lease *l = list_first_entry(&leases, struct lease, head);
572 struct interface *master = NULL, *i, *n;
577 list_for_each_entry(i, &interfaces, head)
580 struct uci_package *dhcp = NULL;
581 if (!uci_load(uci, "dhcp", &dhcp)) {
582 struct uci_element *e;
583 uci_foreach_element(&dhcp->sections, e) {
584 struct uci_section *s = uci_to_section(e);
585 if (!strcmp(s->type, "host"))
587 else if (!strcmp(s->type, "odhcpd"))
591 uci_foreach_element(&dhcp->sections, e) {
592 struct uci_section *s = uci_to_section(e);
593 if (!strcmp(s->type, "dhcp"))
600 ubus_apply_network();
603 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
606 list_for_each_entry(i, &interfaces, head) {
610 if (i->dhcpv6 == RELAYD_HYBRID || i->dhcpv6 == RELAYD_RELAY)
611 any_dhcpv6_slave = true;
613 if (i->ra == RELAYD_HYBRID || i->ra == RELAYD_RELAY)
616 if (i->ndp == RELAYD_HYBRID || i->ndp == RELAYD_RELAY)
617 any_ndp_slave = true;
620 // Evaluate hybrid mode for master
621 list_for_each_entry(i, &interfaces, head) {
625 enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
627 if (!ubus_has_prefix(i->name, i->ifname))
628 hybrid_mode = RELAYD_RELAY;
631 if (i->dhcpv6 == RELAYD_HYBRID)
632 i->dhcpv6 = hybrid_mode;
634 if (i->dhcpv6 == RELAYD_RELAY && !any_dhcpv6_slave)
635 i->dhcpv6 = RELAYD_DISABLED;
637 if (i->ra == RELAYD_HYBRID)
640 if (i->ra == RELAYD_RELAY && !any_ra_slave)
641 i->ra = RELAYD_DISABLED;
643 if (i->ndp == RELAYD_HYBRID)
644 i->ndp = hybrid_mode;
646 if (i->ndp == RELAYD_RELAY && !any_ndp_slave)
647 i->ndp = RELAYD_DISABLED;
649 if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
654 list_for_each_entry_safe(i, n, &interfaces, head) {
656 // Resolve hybrid mode
657 if (i->dhcpv6 == RELAYD_HYBRID)
658 i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
659 RELAYD_RELAY : RELAYD_SERVER;
661 if (i->ra == RELAYD_HYBRID)
662 i->ra = (master && master->ra == RELAYD_RELAY) ?
663 RELAYD_RELAY : RELAYD_SERVER;
665 if (i->ndp == RELAYD_HYBRID)
666 i->ndp = (master && master->ndp == RELAYD_RELAY) ?
667 RELAYD_RELAY : RELAYD_DISABLED;
669 setup_router_interface(i, true);
670 setup_dhcpv6_interface(i, true);
671 setup_ndp_interface(i, true);
672 setup_dhcpv4_interface(i, true);
678 uci_unload(uci, dhcp);
679 uci_free_context(uci);
683 static void handle_signal(int signal)
686 if (signal == SIGHUP)
687 write(reload_pipe[1], b, sizeof(b));
694 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
697 read(u->fd, b, sizeof(b));
701 static struct uloop_fd reload_fd = { .cb = reload_cb };
703 void odhcpd_run(void)
705 pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC);
706 reload_fd.fd = reload_pipe[0];
707 uloop_fd_add(&reload_fd, ULOOP_READ);
709 signal(SIGTERM, handle_signal);
710 signal(SIGINT, handle_signal);
711 signal(SIGHUP, handle_signal);
721 while (!list_empty(&interfaces))
722 close_interface(list_first_entry(&interfaces, struct interface, head));