16 static struct blob_buf b;
17 static int reload_pipe[2];
18 struct list_head leases = LIST_HEAD_INIT(leases);
19 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
20 struct config config = {.legacy = false, .dhcp_cb = NULL,
21 .dhcp_statefile = NULL, .log_level = LOG_INFO};
27 IFACE_ATTR_DYNAMICDHCP,
41 IFACE_ATTR_FILTER_CLASS,
42 IFACE_ATTR_DHCPV6_RAW,
43 IFACE_ATTR_RA_DEFAULT,
44 IFACE_ATTR_RA_MANAGEMENT,
45 IFACE_ATTR_RA_OFFLINK,
46 IFACE_ATTR_RA_PREFERENCE,
47 IFACE_ATTR_RA_ADVROUTER,
48 IFACE_ATTR_RA_MININTERVAL,
49 IFACE_ATTR_RA_MAXINTERVAL,
50 IFACE_ATTR_RA_LIFETIME,
51 IFACE_ATTR_RA_USELEASETIME,
52 IFACE_ATTR_PD_MANAGER,
54 IFACE_ATTR_NDPROXY_ROUTING,
55 IFACE_ATTR_NDPROXY_SLAVE,
59 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
60 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
61 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
62 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
64 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
65 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
66 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
67 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
68 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
69 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
70 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
73 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
74 [IFACE_ATTR_ROUTER] = { .name = "router", .type = BLOBMSG_TYPE_ARRAY },
75 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
76 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
77 [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
78 [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
79 [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
80 [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
81 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
82 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
83 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
84 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
85 [IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
86 [IFACE_ATTR_RA_MININTERVAL] = { .name = "ra_mininterval", .type = BLOBMSG_TYPE_INT32 },
87 [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
88 [IFACE_ATTR_RA_LIFETIME] = { .name = "ra_lifetime", .type = BLOBMSG_TYPE_INT32 },
89 [IFACE_ATTR_RA_USELEASETIME] = { .name = "ra_useleasetime", .type = BLOBMSG_TYPE_BOOL },
90 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
91 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
94 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
95 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
96 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
97 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
100 const struct uci_blob_param_list interface_attr_list = {
101 .n_params = IFACE_ATTR_MAX,
102 .params = iface_attrs,
103 .info = iface_attr_info,
111 LEASE_ATTR_LEASETIME,
116 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
117 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
118 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
119 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
120 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
121 [LEASE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
122 [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
125 const struct uci_blob_param_list lease_attr_list = {
126 .n_params = LEASE_ATTR_MAX,
127 .params = lease_attrs,
131 ODHCPD_ATTR_MAINDHCP,
132 ODHCPD_ATTR_LEASEFILE,
133 ODHCPD_ATTR_LEASETRIGGER,
134 ODHCPD_ATTR_LOGLEVEL,
138 static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
139 [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
140 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
141 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
142 [ODHCPD_ATTR_LOGLEVEL] = { .name = "loglevel", .type = BLOBMSG_TYPE_INT32 },
145 const struct uci_blob_param_list odhcpd_attr_list = {
146 .n_params = ODHCPD_ATTR_MAX,
147 .params = odhcpd_attrs,
150 static int mkdir_p(char *dir, mode_t mask)
152 char *l = strrchr(dir, '/');
160 if (mkdir_p(dir, mask))
165 ret = mkdir(dir, mask);
166 if (ret && errno == EEXIST)
170 syslog(LOG_ERR, "mkdir(%s, %d) failed: %s\n", dir, mask, strerror(errno));
175 static void free_lease(struct lease *l)
184 static struct interface* get_interface(const char *name)
187 list_for_each_entry(c, &interfaces, head)
188 if (!strcmp(c->name, name))
193 static void set_interface_defaults(struct interface *iface)
196 iface->learn_routes = 1;
197 iface->dhcpv4_leasetime = 43200;
198 iface->ra_maxinterval = 600;
199 iface->ra_mininterval = iface->ra_maxinterval/3;
200 iface->ra_lifetime = -1;
203 static void clean_interface(struct interface *iface)
207 free(iface->upstream);
208 free(iface->dhcpv4_router);
209 free(iface->dhcpv4_dns);
210 free(iface->dhcpv6_raw);
211 free(iface->filter_class);
212 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
213 set_interface_defaults(iface);
216 static void close_interface(struct interface *iface)
218 if (iface->head.next)
219 list_del(&iface->head);
221 setup_router_interface(iface, false);
222 setup_dhcpv6_interface(iface, false);
223 setup_ndp_interface(iface, false);
224 setup_dhcpv4_interface(iface, false);
226 clean_interface(iface);
230 static int parse_mode(const char *mode)
232 if (!strcmp(mode, "disabled"))
233 return RELAYD_DISABLED;
234 else if (!strcmp(mode, "server"))
235 return RELAYD_SERVER;
236 else if (!strcmp(mode, "relay"))
238 else if (!strcmp(mode, "hybrid"))
239 return RELAYD_HYBRID;
244 static void set_config(struct uci_section *s)
246 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
248 blob_buf_init(&b, 0);
249 uci_to_blob(&b, s, &odhcpd_attr_list);
250 blobmsg_parse(odhcpd_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
252 if ((c = tb[ODHCPD_ATTR_MAINDHCP]))
253 config.legacy = blobmsg_get_bool(c);
255 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
256 free(config.dhcp_statefile);
257 config.dhcp_statefile = strdup(blobmsg_get_string(c));
260 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
261 free(config.dhcp_cb);
262 config.dhcp_cb = strdup(blobmsg_get_string(c));
265 if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) {
266 int log_level = (blobmsg_get_u32(c) & LOG_PRIMASK);
268 if (config.log_level != log_level) {
269 config.log_level = log_level;
270 setlogmask(LOG_UPTO(config.log_level));
275 static double parse_leasetime(struct blob_attr *c) {
276 char *val = blobmsg_get_string(c), *endptr = NULL;
277 double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;
279 if (time && endptr && endptr[0]) {
280 if (endptr[0] == 's')
282 else if (endptr[0] == 'm')
284 else if (endptr[0] == 'h')
286 else if (endptr[0] == 'd')
288 else if (endptr[0] == 'w')
289 time *= 7 * 24 * 3600;
303 static int set_lease(struct uci_section *s)
305 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
307 blob_buf_init(&b, 0);
308 uci_to_blob(&b, s, &lease_attr_list);
309 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
312 if ((c = tb[LEASE_ATTR_NAME]))
313 hostlen = blobmsg_data_len(c);
315 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
320 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
322 if ((c = tb[LEASE_ATTR_IP]))
323 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
326 if ((c = tb[LEASE_ATTR_MAC]))
327 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
330 if ((c = tb[LEASE_ATTR_DUID])) {
331 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
332 lease->duid = malloc(duidlen);
336 ssize_t len = odhcpd_unhexlify(lease->duid,
337 duidlen, blobmsg_get_string(c));
342 lease->duid_len = len;
345 if ((c = tb[LEASE_ATTR_HOSTID])) {
347 lease->hostid = strtoul(blobmsg_get_string(c), NULL, 16);
352 if ((c = tb[LEASE_ATTR_LEASETIME])) {
353 double time = parse_leasetime(c);
357 lease->dhcpv4_leasetime = time;
360 list_add(&lease->head, &leases);
370 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
372 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
373 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
375 if (tb[IFACE_ATTR_INTERFACE])
376 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
381 struct interface *iface = get_interface(name);
383 iface = calloc(1, sizeof(*iface));
387 strncpy(iface->name, name, sizeof(iface->name) - 1);
389 set_interface_defaults(iface);
391 list_add(&iface->head, &interfaces);
395 const char *ifname = NULL;
397 if ((c = tb[IFACE_ATTR_IFNAME]))
398 ifname = blobmsg_get_string(c);
399 else if ((c = tb[IFACE_ATTR_NETWORKID]))
400 ifname = blobmsg_get_string(c);
404 if (overwrite || !iface->ifname[0])
405 ifname = ubus_get_ifname(name);
408 if (!iface->ifname[0] && !ifname)
412 strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
414 if ((iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
419 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
420 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
422 if (overwrite && (c = tb[IFACE_ATTR_IGNORE]))
423 iface->ignore = blobmsg_get_bool(c);
425 if ((c = tb[IFACE_ATTR_LEASETIME])) {
426 double time = parse_leasetime(c);
430 iface->dhcpv4_leasetime = time;
433 if ((c = tb[IFACE_ATTR_START])) {
434 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
437 iface->dhcpv4 = RELAYD_SERVER;
440 if ((c = tb[IFACE_ATTR_LIMIT]))
441 iface->dhcpv4_end.s_addr = htonl(
442 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
444 if ((c = tb[IFACE_ATTR_MASTER]))
445 iface->master = blobmsg_get_bool(c);
447 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
448 struct blob_attr *cur;
451 blobmsg_for_each_attr(cur, c, rem) {
452 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
455 iface->upstream = realloc(iface->upstream,
456 iface->upstream_len + blobmsg_data_len(cur));
457 if (!iface->upstream)
460 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
461 iface->upstream_len += blobmsg_data_len(cur);
466 if ((c = tb[IFACE_ATTR_RA])) {
467 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
473 if ((c = tb[IFACE_ATTR_DHCPV4])) {
474 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
475 iface->dhcpv4 = mode;
480 if ((c = tb[IFACE_ATTR_DHCPV6])) {
481 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
482 iface->dhcpv6 = mode;
487 if ((c = tb[IFACE_ATTR_NDP])) {
488 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
494 if ((c = tb[IFACE_ATTR_ROUTER])) {
495 struct blob_attr *cur;
498 blobmsg_for_each_attr(cur, c, rem) {
499 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
502 struct in_addr addr4;
503 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
504 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
505 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
506 if (!iface->dhcpv4_router)
509 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
515 if ((c = tb[IFACE_ATTR_DNS])) {
516 struct blob_attr *cur;
519 iface->always_rewrite_dns = true;
520 blobmsg_for_each_attr(cur, c, rem) {
521 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
524 struct in_addr addr4;
525 struct in6_addr addr6;
526 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
527 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
528 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
529 if (!iface->dhcpv4_dns)
532 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
533 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
534 iface->dns = realloc(iface->dns,
535 (++iface->dns_cnt) * sizeof(*iface->dns));
539 iface->dns[iface->dns_cnt - 1] = addr6;
545 if ((c = tb[IFACE_ATTR_DOMAIN])) {
546 struct blob_attr *cur;
549 blobmsg_for_each_attr(cur, c, rem) {
550 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
554 char *domain = blobmsg_get_string(cur);
555 size_t domainlen = strlen(domain);
556 if (domainlen > 0 && domain[domainlen - 1] == '.')
557 domain[domainlen - 1] = 0;
559 int len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
563 iface->search = realloc(iface->search, iface->search_len + len);
567 memcpy(&iface->search[iface->search_len], buf, len);
568 iface->search_len += len;
572 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
573 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
574 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
577 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
578 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
579 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
580 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
583 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
584 iface->default_router = blobmsg_get_u32(c);
586 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
587 iface->managed = blobmsg_get_u32(c);
589 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
590 iface->ra_not_onlink = blobmsg_get_bool(c);
592 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
593 iface->ra_advrouter = blobmsg_get_bool(c);
595 if ((c = tb[IFACE_ATTR_RA_MININTERVAL]))
596 iface->ra_mininterval = blobmsg_get_u32(c);
598 if ((c = tb[IFACE_ATTR_RA_MAXINTERVAL]))
599 iface->ra_maxinterval = blobmsg_get_u32(c);
601 if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
602 iface->ra_lifetime = blobmsg_get_u32(c);
604 if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
605 iface->ra_useleasetime = blobmsg_get_bool(c);
607 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
608 const char *prio = blobmsg_get_string(c);
610 if (!strcmp(prio, "high"))
611 iface->route_preference = 1;
612 else if (!strcmp(prio, "low"))
613 iface->route_preference = -1;
614 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
615 iface->route_preference = 0;
620 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
621 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
622 sizeof(iface->dhcpv6_pd_manager) - 1);
624 if ((c = tb[IFACE_ATTR_PD_CER]) &&
625 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
628 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
629 iface->learn_routes = blobmsg_get_bool(c);
631 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
632 iface->external = blobmsg_get_bool(c);
637 close_interface(iface);
641 static int set_interface(struct uci_section *s)
643 blob_buf_init(&b, 0);
644 uci_to_blob(&b, s, &interface_attr_list);
646 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
649 void odhcpd_reload(void)
651 struct uci_context *uci = uci_alloc_context();
653 while (!list_empty(&leases))
654 free_lease(list_first_entry(&leases, struct lease, head));
656 struct interface *master = NULL, *i, *n;
661 list_for_each_entry(i, &interfaces, head)
664 struct uci_package *dhcp = NULL;
665 if (!uci_load(uci, "dhcp", &dhcp)) {
666 struct uci_element *e;
667 uci_foreach_element(&dhcp->sections, e) {
668 struct uci_section *s = uci_to_section(e);
669 if (!strcmp(s->type, "host"))
671 else if (!strcmp(s->type, "odhcpd"))
675 uci_foreach_element(&dhcp->sections, e) {
676 struct uci_section *s = uci_to_section(e);
677 if (!strcmp(s->type, "dhcp"))
682 if (config.dhcp_statefile) {
683 char *path = strdup(config.dhcp_statefile);
685 mkdir_p(dirname(path), 0755);
690 ubus_apply_network();
693 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
696 list_for_each_entry(i, &interfaces, head) {
700 if (i->dhcpv6 == RELAYD_HYBRID || i->dhcpv6 == RELAYD_RELAY)
701 any_dhcpv6_slave = true;
703 if (i->ra == RELAYD_HYBRID || i->ra == RELAYD_RELAY)
706 if (i->ndp == RELAYD_HYBRID || i->ndp == RELAYD_RELAY)
707 any_ndp_slave = true;
710 /* Evaluate hybrid mode for master */
711 list_for_each_entry(i, &interfaces, head) {
715 enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
717 if (!ubus_has_prefix(i->name, i->ifname))
718 hybrid_mode = RELAYD_RELAY;
721 if (i->dhcpv6 == RELAYD_HYBRID)
722 i->dhcpv6 = hybrid_mode;
724 if (i->dhcpv6 == RELAYD_RELAY && !any_dhcpv6_slave)
725 i->dhcpv6 = RELAYD_DISABLED;
727 if (i->ra == RELAYD_HYBRID)
730 if (i->ra == RELAYD_RELAY && !any_ra_slave)
731 i->ra = RELAYD_DISABLED;
733 if (i->ndp == RELAYD_HYBRID)
734 i->ndp = hybrid_mode;
736 if (i->ndp == RELAYD_RELAY && !any_ndp_slave)
737 i->ndp = RELAYD_DISABLED;
739 if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
744 list_for_each_entry_safe(i, n, &interfaces, head) {
746 /* Resolve hybrid mode */
747 if (i->dhcpv6 == RELAYD_HYBRID)
748 i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
749 RELAYD_RELAY : RELAYD_SERVER;
751 if (i->ra == RELAYD_HYBRID)
752 i->ra = (master && master->ra == RELAYD_RELAY) ?
753 RELAYD_RELAY : RELAYD_SERVER;
755 if (i->ndp == RELAYD_HYBRID)
756 i->ndp = (master && master->ndp == RELAYD_RELAY) ?
757 RELAYD_RELAY : RELAYD_DISABLED;
759 setup_router_interface(i, !i->ignore || i->ra != RELAYD_DISABLED);
760 setup_dhcpv6_interface(i, !i->ignore || i->dhcpv6 != RELAYD_DISABLED);
761 setup_ndp_interface(i, !i->ignore || i->ndp != RELAYD_DISABLED);
762 setup_dhcpv4_interface(i, !i->ignore || i->dhcpv4 != RELAYD_DISABLED);
767 ndp_handle_addr6_dump();
768 uci_unload(uci, dhcp);
769 uci_free_context(uci);
772 static void handle_signal(int signal)
776 if (signal == SIGHUP) {
777 if (write(reload_pipe[1], b, sizeof(b)) < 0) {}
782 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
785 if (read(u->fd, b, sizeof(b)) < 0) {}
790 static struct uloop_fd reload_fd = { .cb = reload_cb };
792 void odhcpd_run(void)
794 if (pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC) < 0) {}
796 reload_fd.fd = reload_pipe[0];
797 uloop_fd_add(&reload_fd, ULOOP_READ);
799 signal(SIGTERM, handle_signal);
800 signal(SIGINT, handle_signal);
801 signal(SIGHUP, handle_signal);
811 while (!list_empty(&interfaces))
812 close_interface(list_first_entry(&interfaces, struct interface, head));