14 #include <libubox/utils.h>
15 #include <libubox/avl.h>
16 #include <libubox/avl-cmp.h>
20 static struct blob_buf b;
21 static int reload_pipe[2];
22 struct list_head leases = LIST_HEAD_INIT(leases);
23 AVL_TREE(interfaces, avl_strcmp, false, NULL);
24 struct config config = {.legacy = false, .main_dhcpv4 = false,
25 .dhcp_cb = NULL, .dhcp_statefile = NULL,
26 .log_level = LOG_INFO};
32 IFACE_ATTR_DYNAMICDHCP,
46 IFACE_ATTR_FILTER_CLASS,
47 IFACE_ATTR_DHCPV4_FORCERECONF,
48 IFACE_ATTR_DHCPV6_RAW,
49 IFACE_ATTR_DHCPV6_ASSIGNALL,
52 IFACE_ATTR_RA_DEFAULT,
53 IFACE_ATTR_RA_MANAGEMENT,
54 IFACE_ATTR_RA_OFFLINK,
55 IFACE_ATTR_RA_PREFERENCE,
56 IFACE_ATTR_RA_ADVROUTER,
57 IFACE_ATTR_RA_MININTERVAL,
58 IFACE_ATTR_RA_MAXINTERVAL,
59 IFACE_ATTR_RA_LIFETIME,
60 IFACE_ATTR_RA_USELEASETIME,
61 IFACE_ATTR_RA_REACHABLETIME,
62 IFACE_ATTR_RA_RETRANSTIME,
63 IFACE_ATTR_RA_HOPLIMIT,
66 IFACE_ATTR_PD_MANAGER,
68 IFACE_ATTR_NDPROXY_ROUTING,
69 IFACE_ATTR_NDPROXY_SLAVE,
70 IFACE_ATTR_PREFIX_FILTER,
74 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
75 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
76 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
77 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
78 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
79 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
80 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
81 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
82 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
83 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
84 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
85 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
86 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
87 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
88 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
89 [IFACE_ATTR_ROUTER] = { .name = "router", .type = BLOBMSG_TYPE_ARRAY },
90 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
91 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
92 [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
93 [IFACE_ATTR_DHCPV4_FORCERECONF] = { .name = "dhcpv4_forcereconf", .type = BLOBMSG_TYPE_BOOL },
94 [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
95 [IFACE_ATTR_DHCPV6_ASSIGNALL] = { .name ="dhcpv6_assignall", .type = BLOBMSG_TYPE_BOOL },
96 [IFACE_ATTR_DHCPV6_PD] = { .name = "dhcpv6_pd", .type = BLOBMSG_TYPE_BOOL },
97 [IFACE_ATTR_DHCPV6_NA] = { .name = "dhcpv6_na", .type = BLOBMSG_TYPE_BOOL },
98 [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
99 [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
100 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
101 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
102 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
103 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
104 [IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
105 [IFACE_ATTR_RA_MININTERVAL] = { .name = "ra_mininterval", .type = BLOBMSG_TYPE_INT32 },
106 [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
107 [IFACE_ATTR_RA_LIFETIME] = { .name = "ra_lifetime", .type = BLOBMSG_TYPE_INT32 },
108 [IFACE_ATTR_RA_USELEASETIME] = { .name = "ra_useleasetime", .type = BLOBMSG_TYPE_BOOL },
109 [IFACE_ATTR_RA_REACHABLETIME] = { .name = "ra_reachabletime", .type = BLOBMSG_TYPE_INT32 },
110 [IFACE_ATTR_RA_RETRANSTIME] = { .name = "ra_retranstime", .type = BLOBMSG_TYPE_INT32 },
111 [IFACE_ATTR_RA_HOPLIMIT] = { .name = "ra_hoplimit", .type = BLOBMSG_TYPE_INT32 },
112 [IFACE_ATTR_RA_MTU] = { .name = "ra_mtu", .type = BLOBMSG_TYPE_INT32 },
113 [IFACE_ATTR_RA_DNS] = { .name = "ra_dns", .type = BLOBMSG_TYPE_BOOL },
114 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
115 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
116 [IFACE_ATTR_PREFIX_FILTER] = { .name = "prefix_filter", .type = BLOBMSG_TYPE_STRING },
119 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
120 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
121 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
122 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
125 const struct uci_blob_param_list interface_attr_list = {
126 .n_params = IFACE_ATTR_MAX,
127 .params = iface_attrs,
128 .info = iface_attr_info,
136 LEASE_ATTR_LEASETIME,
141 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
142 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
143 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
144 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
145 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
146 [LEASE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
147 [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
150 const struct uci_blob_param_list lease_attr_list = {
151 .n_params = LEASE_ATTR_MAX,
152 .params = lease_attrs,
157 ODHCPD_ATTR_MAINDHCP,
158 ODHCPD_ATTR_LEASEFILE,
159 ODHCPD_ATTR_LEASETRIGGER,
160 ODHCPD_ATTR_LOGLEVEL,
164 static const struct blobmsg_policy odhcpd_attrs[ODHCPD_ATTR_MAX] = {
165 [ODHCPD_ATTR_LEGACY] = { .name = "legacy", .type = BLOBMSG_TYPE_BOOL },
166 [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
167 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
168 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
169 [ODHCPD_ATTR_LOGLEVEL] = { .name = "loglevel", .type = BLOBMSG_TYPE_INT32 },
172 const struct uci_blob_param_list odhcpd_attr_list = {
173 .n_params = ODHCPD_ATTR_MAX,
174 .params = odhcpd_attrs,
177 static int mkdir_p(char *dir, mode_t mask)
179 char *l = strrchr(dir, '/');
187 if (mkdir_p(dir, mask))
192 ret = mkdir(dir, mask);
193 if (ret && errno == EEXIST)
197 syslog(LOG_ERR, "mkdir(%s, %d) failed: %m\n", dir, mask);
202 static void free_lease(struct lease *l)
211 static void set_interface_defaults(struct interface *iface)
213 iface->learn_routes = 1;
214 iface->dhcpv4_leasetime = 43200;
215 iface->dhcpv6_assignall = true;
216 iface->dhcpv6_pd = true;
217 iface->dhcpv6_na = true;
218 iface->ra_managed = RA_MANAGED_MFLAG;
219 iface->ra_maxinterval = 600;
220 iface->ra_mininterval = iface->ra_maxinterval/3;
221 iface->ra_lifetime = -1;
222 iface->ra_dns = true;
225 static void clean_interface(struct interface *iface)
229 free(iface->upstream);
230 free(iface->dhcpv4_router);
231 free(iface->dhcpv4_dns);
232 free(iface->dhcpv6_raw);
233 free(iface->filter_class);
234 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
235 set_interface_defaults(iface);
238 static void close_interface(struct interface *iface)
240 avl_delete(&interfaces, &iface->avl);
242 router_setup_interface(iface, false);
243 dhcpv6_setup_interface(iface, false);
244 ndp_setup_interface(iface, false);
245 #ifdef DHCPV4_SUPPORT
246 dhcpv4_setup_interface(iface, false);
249 clean_interface(iface);
256 static int parse_mode(const char *mode)
258 if (!strcmp(mode, "disabled"))
259 return MODE_DISABLED;
260 else if (!strcmp(mode, "server"))
262 else if (!strcmp(mode, "relay"))
264 else if (!strcmp(mode, "hybrid"))
270 static void set_config(struct uci_section *s)
272 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
274 blob_buf_init(&b, 0);
275 uci_to_blob(&b, s, &odhcpd_attr_list);
276 blobmsg_parse(odhcpd_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
278 if ((c = tb[ODHCPD_ATTR_LEGACY]))
279 config.legacy = blobmsg_get_bool(c);
281 if ((c = tb[ODHCPD_ATTR_MAINDHCP]))
282 config.main_dhcpv4 = blobmsg_get_bool(c);
284 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
285 free(config.dhcp_statefile);
286 config.dhcp_statefile = strdup(blobmsg_get_string(c));
289 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
290 free(config.dhcp_cb);
291 config.dhcp_cb = strdup(blobmsg_get_string(c));
294 if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) {
295 int log_level = (blobmsg_get_u32(c) & LOG_PRIMASK);
297 if (config.log_level != log_level) {
298 config.log_level = log_level;
299 setlogmask(LOG_UPTO(config.log_level));
304 static double parse_leasetime(struct blob_attr *c) {
305 char *val = blobmsg_get_string(c), *endptr = NULL;
306 double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;
308 if (time && endptr && endptr[0]) {
309 if (endptr[0] == 's')
311 else if (endptr[0] == 'm')
313 else if (endptr[0] == 'h')
315 else if (endptr[0] == 'd')
317 else if (endptr[0] == 'w')
318 time *= 7 * 24 * 3600;
332 static int set_lease(struct uci_section *s)
334 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
336 blob_buf_init(&b, 0);
337 uci_to_blob(&b, s, &lease_attr_list);
338 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
341 if ((c = tb[LEASE_ATTR_NAME]))
342 hostlen = blobmsg_data_len(c);
344 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
349 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
350 if (!odhcpd_valid_hostname(lease->hostname))
354 if ((c = tb[LEASE_ATTR_IP]))
355 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
358 if ((c = tb[LEASE_ATTR_MAC]))
359 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
362 if ((c = tb[LEASE_ATTR_DUID])) {
363 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
364 lease->duid = malloc(duidlen);
368 ssize_t len = odhcpd_unhexlify(lease->duid,
369 duidlen, blobmsg_get_string(c));
374 lease->duid_len = len;
377 if ((c = tb[LEASE_ATTR_HOSTID])) {
379 lease->hostid = strtoul(blobmsg_get_string(c), NULL, 16);
384 if ((c = tb[LEASE_ATTR_LEASETIME])) {
385 double time = parse_leasetime(c);
389 lease->dhcpv4_leasetime = time;
392 list_add(&lease->head, &leases);
402 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
404 struct interface *iface;
405 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
406 bool get_addrs = false;
408 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
410 if (tb[IFACE_ATTR_INTERFACE])
411 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
416 iface = avl_find_element(&interfaces, name, iface, avl);
420 iface = calloc_a(sizeof(*iface), &new_name, strlen(name) + 1);
424 iface->name = strcpy(new_name, name);
425 iface->avl.key = iface->name;
426 set_interface_defaults(iface);
428 avl_insert(&interfaces, &iface->avl);
429 get_addrs = overwrite = true;
432 const char *ifname = NULL;
434 if ((c = tb[IFACE_ATTR_IFNAME]))
435 ifname = blobmsg_get_string(c);
436 else if ((c = tb[IFACE_ATTR_NETWORKID]))
437 ifname = blobmsg_get_string(c);
441 if (overwrite || !iface->ifname)
442 ifname = ubus_get_ifname(name);
445 if (!iface->ifname && !ifname)
450 iface->ifname = strdup(ifname);
455 if (!iface->ifindex &&
456 (iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
461 ssize_t len = netlink_get_interface_addrs(iface->ifindex,
462 true, &iface->addr6);
465 iface->addr6_len = len;
467 len = netlink_get_interface_addrs(iface->ifindex,
468 false, &iface->addr4);
470 iface->addr4_len = len;
475 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
476 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
478 if (overwrite && (c = tb[IFACE_ATTR_IGNORE]))
479 iface->ignore = blobmsg_get_bool(c);
481 if ((c = tb[IFACE_ATTR_LEASETIME])) {
482 double time = parse_leasetime(c);
486 iface->dhcpv4_leasetime = time;
489 if ((c = tb[IFACE_ATTR_START])) {
490 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
492 if (config.main_dhcpv4 && config.legacy)
493 iface->dhcpv4 = MODE_SERVER;
496 if ((c = tb[IFACE_ATTR_LIMIT]))
497 iface->dhcpv4_end.s_addr = htonl(
498 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
500 if ((c = tb[IFACE_ATTR_MASTER]))
501 iface->master = blobmsg_get_bool(c);
503 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
504 struct blob_attr *cur;
507 blobmsg_for_each_attr(cur, c, rem) {
508 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
511 iface->upstream = realloc(iface->upstream,
512 iface->upstream_len + blobmsg_data_len(cur));
513 if (!iface->upstream)
516 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
517 iface->upstream_len += blobmsg_data_len(cur);
522 if ((c = tb[IFACE_ATTR_RA])) {
523 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
529 if ((c = tb[IFACE_ATTR_DHCPV4])) {
530 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
531 if (config.main_dhcpv4)
532 iface->dhcpv4 = mode;
538 if ((c = tb[IFACE_ATTR_DHCPV6])) {
539 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
540 iface->dhcpv6 = mode;
545 if ((c = tb[IFACE_ATTR_NDP])) {
546 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
552 if ((c = tb[IFACE_ATTR_ROUTER])) {
553 struct blob_attr *cur;
556 blobmsg_for_each_attr(cur, c, rem) {
557 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
560 struct in_addr addr4;
561 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
562 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
563 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
564 if (!iface->dhcpv4_router)
567 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
573 if ((c = tb[IFACE_ATTR_DNS])) {
574 struct blob_attr *cur;
577 iface->always_rewrite_dns = true;
578 blobmsg_for_each_attr(cur, c, rem) {
579 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
582 struct in_addr addr4;
583 struct in6_addr addr6;
584 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
585 if (addr4.s_addr == INADDR_ANY)
588 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
589 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
590 if (!iface->dhcpv4_dns)
593 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
594 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
595 if (IN6_IS_ADDR_UNSPECIFIED(&addr6))
598 iface->dns = realloc(iface->dns,
599 (++iface->dns_cnt) * sizeof(*iface->dns));
603 iface->dns[iface->dns_cnt - 1] = addr6;
609 if ((c = tb[IFACE_ATTR_DOMAIN])) {
610 struct blob_attr *cur;
613 blobmsg_for_each_attr(cur, c, rem) {
614 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
618 char *domain = blobmsg_get_string(cur);
619 size_t domainlen = strlen(domain);
620 if (domainlen > 0 && domain[domainlen - 1] == '.')
621 domain[domainlen - 1] = 0;
623 int len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
627 iface->search = realloc(iface->search, iface->search_len + len);
631 memcpy(&iface->search[iface->search_len], buf, len);
632 iface->search_len += len;
636 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
637 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
638 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
641 if ((c = tb[IFACE_ATTR_DHCPV4_FORCERECONF]))
642 iface->dhcpv4_forcereconf = blobmsg_get_bool(c);
644 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
645 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
646 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
647 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
650 if ((c = tb[IFACE_ATTR_DHCPV6_ASSIGNALL]))
651 iface->dhcpv6_assignall = blobmsg_get_bool(c);
653 if ((c = tb[IFACE_ATTR_DHCPV6_PD]))
654 iface->dhcpv6_pd = blobmsg_get_bool(c);
656 if ((c = tb[IFACE_ATTR_DHCPV6_NA]))
657 iface->dhcpv6_na = blobmsg_get_bool(c);
659 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
660 iface->default_router = blobmsg_get_u32(c);
662 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
663 iface->ra_managed = blobmsg_get_u32(c);
665 if ((c = tb[IFACE_ATTR_RA_REACHABLETIME])) {
666 uint32_t ra_reachabletime = blobmsg_get_u32(c);
667 if (ra_reachabletime > 3600000)
670 iface->ra_reachabletime = ra_reachabletime;
673 if ((c = tb[IFACE_ATTR_RA_RETRANSTIME])) {
674 uint32_t ra_retranstime = blobmsg_get_u32(c);
675 if (ra_retranstime > 60000)
678 iface->ra_retranstime = ra_retranstime;
681 if ((c = tb[IFACE_ATTR_RA_HOPLIMIT])) {
682 uint32_t ra_hoplimit = blobmsg_get_u32(c);
683 if (ra_hoplimit > 255)
686 iface->ra_hoplimit = ra_hoplimit;
689 if ((c = tb[IFACE_ATTR_RA_MTU])) {
690 uint32_t ra_mtu = blobmsg_get_u32(c);
691 if (ra_mtu < 1280 || ra_mtu > 65535)
694 iface->ra_mtu = ra_mtu;
697 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
698 iface->ra_not_onlink = blobmsg_get_bool(c);
700 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
701 iface->ra_advrouter = blobmsg_get_bool(c);
703 if ((c = tb[IFACE_ATTR_RA_MININTERVAL]))
704 iface->ra_mininterval = blobmsg_get_u32(c);
706 if ((c = tb[IFACE_ATTR_RA_MAXINTERVAL]))
707 iface->ra_maxinterval = blobmsg_get_u32(c);
709 if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
710 iface->ra_lifetime = blobmsg_get_u32(c);
712 if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
713 iface->ra_useleasetime = blobmsg_get_bool(c);
715 if ((c = tb[IFACE_ATTR_RA_DNS]))
716 iface->ra_dns = blobmsg_get_bool(c);
718 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
719 const char *prio = blobmsg_get_string(c);
721 if (!strcmp(prio, "high"))
722 iface->route_preference = 1;
723 else if (!strcmp(prio, "low"))
724 iface->route_preference = -1;
725 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
726 iface->route_preference = 0;
731 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
732 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
733 sizeof(iface->dhcpv6_pd_manager) - 1);
735 if ((c = tb[IFACE_ATTR_PD_CER]) &&
736 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
739 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
740 iface->learn_routes = blobmsg_get_bool(c);
742 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
743 iface->external = blobmsg_get_bool(c);
745 if ((c = tb[IFACE_ATTR_PREFIX_FILTER])) {
746 const char *str = blobmsg_get_string(c);
747 char *astr = malloc(strlen(str) + 1);
751 if (!astr || !strcpy(astr, str) ||
752 (delim = strchr(astr, '/')) == NULL || (*(delim++) = 0) ||
753 sscanf(delim, "%i", &l) == 0 || l > 128 ||
754 inet_pton(AF_INET6, astr, &iface->pio_filter_addr) == 0)
755 iface->pio_filter_length = 0;
757 iface->pio_filter_length = l;
766 close_interface(iface);
770 static int set_interface(struct uci_section *s)
772 blob_buf_init(&b, 0);
773 uci_to_blob(&b, s, &interface_attr_list);
775 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
778 void odhcpd_reload(void)
780 struct uci_context *uci = uci_alloc_context();
781 struct interface *master = NULL, *i, *tmp;
783 while (!list_empty(&leases))
784 free_lease(list_first_entry(&leases, struct lease, head));
789 avl_for_each_element(&interfaces, i, avl)
792 struct uci_package *dhcp = NULL;
793 if (!uci_load(uci, "dhcp", &dhcp)) {
794 struct uci_element *e;
795 uci_foreach_element(&dhcp->sections, e) {
796 struct uci_section *s = uci_to_section(e);
797 if (!strcmp(s->type, "host"))
799 else if (!strcmp(s->type, "odhcpd"))
803 uci_foreach_element(&dhcp->sections, e) {
804 struct uci_section *s = uci_to_section(e);
805 if (!strcmp(s->type, "dhcp"))
810 if (config.dhcp_statefile) {
811 char *path = strdup(config.dhcp_statefile);
813 mkdir_p(dirname(path), 0755);
818 ubus_apply_network();
821 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
824 avl_for_each_element(&interfaces, i, avl) {
828 if (i->dhcpv6 == MODE_HYBRID || i->dhcpv6 == MODE_RELAY)
829 any_dhcpv6_slave = true;
831 if (i->ra == MODE_HYBRID || i->ra == MODE_RELAY)
834 if (i->ndp == MODE_HYBRID || i->ndp == MODE_RELAY)
835 any_ndp_slave = true;
838 /* Evaluate hybrid mode for master */
839 avl_for_each_element(&interfaces, i, avl) {
843 enum odhcpd_mode hybrid_mode = MODE_DISABLED;
845 if (!ubus_has_prefix(i->name, i->ifname))
846 hybrid_mode = MODE_RELAY;
849 if (i->dhcpv6 == MODE_HYBRID)
850 i->dhcpv6 = hybrid_mode;
852 if (i->dhcpv6 == MODE_RELAY && !any_dhcpv6_slave)
853 i->dhcpv6 = MODE_DISABLED;
855 if (i->ra == MODE_HYBRID)
858 if (i->ra == MODE_RELAY && !any_ra_slave)
859 i->ra = MODE_DISABLED;
861 if (i->ndp == MODE_HYBRID)
862 i->ndp = hybrid_mode;
864 if (i->ndp == MODE_RELAY && !any_ndp_slave)
865 i->ndp = MODE_DISABLED;
867 if (i->dhcpv6 == MODE_RELAY || i->ra == MODE_RELAY || i->ndp == MODE_RELAY)
872 avl_for_each_element_safe(&interfaces, i, avl, tmp) {
874 /* Resolve hybrid mode */
875 if (i->dhcpv6 == MODE_HYBRID)
876 i->dhcpv6 = (master && master->dhcpv6 == MODE_RELAY) ?
877 MODE_RELAY : MODE_SERVER;
879 if (i->ra == MODE_HYBRID)
880 i->ra = (master && master->ra == MODE_RELAY) ?
881 MODE_RELAY : MODE_SERVER;
883 if (i->ndp == MODE_HYBRID)
884 i->ndp = (master && master->ndp == MODE_RELAY) ?
885 MODE_RELAY : MODE_DISABLED;
887 router_setup_interface(i, !i->ignore || i->ra != MODE_DISABLED);
888 dhcpv6_setup_interface(i, !i->ignore || i->dhcpv6 != MODE_DISABLED);
889 ndp_setup_interface(i, !i->ignore || i->ndp != MODE_DISABLED);
890 #ifdef DHCPV4_SUPPORT
891 dhcpv4_setup_interface(i, !i->ignore || i->dhcpv4 != MODE_DISABLED);
897 uci_unload(uci, dhcp);
898 uci_free_context(uci);
901 static void handle_signal(int signal)
905 if (signal == SIGHUP) {
906 if (write(reload_pipe[1], b, sizeof(b)) < 0) {}
911 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
914 if (read(u->fd, b, sizeof(b)) < 0) {}
919 static struct uloop_fd reload_fd = { .cb = reload_cb };
921 void odhcpd_run(void)
923 if (pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC) < 0) {}
925 reload_fd.fd = reload_pipe[0];
926 uloop_fd_add(&reload_fd, ULOOP_READ);
928 signal(SIGTERM, handle_signal);
929 signal(SIGINT, handle_signal);
930 signal(SIGHUP, handle_signal);