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};
18 IFACE_ATTR_DYNAMICDHCP,
31 IFACE_ATTR_ULA_COMPAT,
32 IFACE_ATTR_RA_DEFAULT,
33 IFACE_ATTR_RA_MANAGEMENT,
34 IFACE_ATTR_RA_OFFLINK,
35 IFACE_ATTR_RA_PREFERENCE,
36 IFACE_ATTR_NDPROXY_ROUTING,
37 IFACE_ATTR_NDPROXY_SLAVE,
38 IFACE_ATTR_NDPROXY_STATIC,
42 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
43 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
44 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
45 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
46 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
47 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
48 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
49 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
50 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
51 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
52 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
54 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
55 [IFACE_ATTR_NDPROXY] = { .name = "ndproxy", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
57 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
58 [IFACE_ATTR_ULA_COMPAT] = { .name = "ula_compat", .type = BLOBMSG_TYPE_BOOL },
59 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
60 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
61 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
62 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
64 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
65 [IFACE_ATTR_NDPROXY_STATIC] = { .name = "ndproxy_static", .type = BLOBMSG_TYPE_ARRAY },
68 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
69 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_NDPROXY_STATIC] = { .type = BLOBMSG_TYPE_STRING },
75 const struct uci_blob_param_list interface_attr_list = {
76 .n_params = IFACE_ATTR_MAX,
77 .params = iface_attrs,
78 .info = iface_attr_info,
92 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
93 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
94 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
95 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
96 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
97 [LEASE_ATTR_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
101 const struct uci_blob_param_list lease_attr_list = {
102 .n_params = LEASE_ATTR_MAX,
103 .params = lease_attrs,
109 ODHCPD_ATTR_LEASEFILE,
110 ODHCPD_ATTR_LEASETRIGGER,
115 static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
116 [ODHCPD_ATTR_LEGACY] = { .name = "legacy", .type = BLOBMSG_TYPE_BOOL },
117 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
118 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
122 const struct uci_blob_param_list odhcpd_attr_list = {
123 .n_params = ODHCPD_ATTR_MAX,
124 .params = odhcpd_attrs,
128 static struct interface* get_interface(const char *name)
131 list_for_each_entry(c, &interfaces, head)
132 if (!strcmp(c->name, name))
138 static void clean_interface(struct interface *iface)
142 free(iface->upstream);
143 free(iface->static_ndp);
144 free(iface->dhcpv4_dns);
145 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
149 static void close_interface(struct interface *iface)
151 if (iface->head.next)
152 list_del(&iface->head);
154 setup_router_interface(iface, false);
155 setup_dhcpv6_interface(iface, false);
156 setup_ndp_interface(iface, false);
157 setup_dhcpv4_interface(iface, false);
159 clean_interface(iface);
164 static int parse_mode(const char *mode)
166 if (!strcmp(mode, "disabled")) {
167 return RELAYD_DISABLED;
168 } else if (!strcmp(mode, "server")) {
169 return RELAYD_SERVER;
170 } else if (!strcmp(mode, "relay")) {
172 } else if (!strcmp(mode, "hybrid")) {
173 return RELAYD_HYBRID;
180 static void set_config(struct uci_section *s)
182 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
184 blob_buf_init(&b, 0);
185 uci_to_blob(&b, s, &lease_attr_list);
186 blobmsg_parse(lease_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
188 if ((c = tb[ODHCPD_ATTR_LEGACY]))
189 config.legacy = blobmsg_get_bool(c);
191 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
192 free(config.dhcp_statefile);
193 config.dhcp_statefile = strdup(blobmsg_get_string(c));
196 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
197 free(config.dhcp_cb);
198 config.dhcp_cb = strdup(blobmsg_get_string(c));
203 static int set_lease(struct uci_section *s)
205 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
207 blob_buf_init(&b, 0);
208 uci_to_blob(&b, s, &lease_attr_list);
209 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
212 if ((c = tb[LEASE_ATTR_HOSTNAME]))
213 hostlen = blobmsg_data_len(c);
215 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
218 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
220 if ((c = tb[LEASE_ATTR_IP]))
221 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
224 if ((c = tb[LEASE_ATTR_MAC]))
225 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
228 if ((c = tb[LEASE_ATTR_DUID])) {
229 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
230 lease->duid = malloc(duidlen);
231 ssize_t len = odhcpd_unhexlify(lease->duid,
232 duidlen, blobmsg_get_string(c));
237 lease->duid_len = len;
240 if ((c = tb[LEASE_ATTR_HOSTID]))
241 if (odhcpd_unhexlify((uint8_t*)&lease->hostid, sizeof(lease->hostid),
242 blobmsg_get_string(c)) < 0)
245 list_add(&lease->head, &leases);
255 int config_parse_interface(struct blob_attr *b, const char *name, bool overwrite)
257 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
258 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(b), blob_len(b));
260 if (tb[IFACE_ATTR_INTERFACE])
261 name = blobmsg_data(tb[IFACE_ATTR_INTERFACE]);
263 struct interface *iface = get_interface(name);
265 iface = calloc(1, sizeof(*iface));
266 strncpy(iface->name, name, sizeof(iface->name) - 1);
267 list_add(&iface->head, &interfaces);
269 clean_interface(iface);
272 const char *ifname = NULL;
275 ifname = ubus_get_ifname(name);
277 if ((c = tb[IFACE_ATTR_IFNAME]))
278 ifname = blobmsg_get_string(c);
280 strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
284 clean_interface(iface);
286 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
287 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
289 if ((c = tb[IFACE_ATTR_IGNORE]))
290 iface->ignore = blobmsg_get_bool(c);
292 if ((c = tb[IFACE_ATTR_LEASETIME])) {
293 char *val = blobmsg_get_string(c), *endptr;
294 double time = strtod(val, &endptr);
295 if (time && endptr[0]) {
296 if (endptr[0] == 's')
298 else if (endptr[0] == 'm')
300 else if (endptr[0] == 'h')
302 else if (endptr[0] == 'd')
304 else if (endptr[0] == 'w')
305 time *= 7 * 24 * 3600;
311 iface->dhcpv4_leasetime = time;
314 if ((c = tb[IFACE_ATTR_START])) {
315 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
318 iface->dhcpv4 = RELAYD_SERVER;
321 if ((c = tb[IFACE_ATTR_LIMIT]))
322 iface->dhcpv4_end.s_addr = htonl(
323 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
325 if ((c = tb[IFACE_ATTR_MASTER]))
326 iface->master = blobmsg_get_bool(c);
328 if ((c = tb[IFACE_ATTR_UPSTREAM])) {
329 struct blob_attr *cur;
332 blobmsg_for_each_attr(cur, c, rem) {
333 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
336 iface->upstream = realloc(iface->upstream,
337 iface->upstream_len + blobmsg_data_len(cur));
338 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
339 iface->upstream_len += blobmsg_data_len(cur);
343 if ((c = tb[IFACE_ATTR_RA]))
344 if ((iface->ra = parse_mode(blobmsg_get_string(c))) < 0)
347 if ((c = tb[IFACE_ATTR_DHCPV4]))
348 if ((iface->dhcpv4 = parse_mode(blobmsg_get_string(c))) < 0)
351 if ((c = tb[IFACE_ATTR_DHCPV6]))
352 if ((iface->dhcpv6 = parse_mode(blobmsg_get_string(c))) < 0)
355 if ((c = tb[IFACE_ATTR_NDPROXY]))
356 iface->ndp = blobmsg_get_bool(c) ? RELAYD_RELAY : RELAYD_DISABLED;
358 if ((c = tb[IFACE_ATTR_DNS])) {
359 struct blob_attr *cur;
362 blobmsg_for_each_attr(cur, c, rem) {
363 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
366 struct in_addr addr4;
367 struct in6_addr addr6;
368 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
369 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
370 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
371 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
372 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
373 iface->dns = realloc(iface->dns,
374 (++iface->dns_cnt) * sizeof(*iface->dns));
375 iface->dns[iface->dns_cnt - 1] = addr6;
382 if ((c = tb[IFACE_ATTR_DOMAIN])) {
383 struct blob_attr *cur;
386 blobmsg_for_each_attr(cur, c, rem) {
387 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
391 int len = dn_comp(blobmsg_get_string(cur), buf, sizeof(buf), NULL, NULL);
395 iface->search = realloc(iface->search, iface->search_len + len);
396 memcpy(&iface->search[iface->search_len], buf, len);
397 iface->search_len += len;
401 if ((c = tb[IFACE_ATTR_ULA_COMPAT]))
402 iface->deprecate_ula_if_public_avail = blobmsg_get_bool(c);
404 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
405 iface->default_router = blobmsg_get_u32(c);
407 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
408 iface->managed = blobmsg_get_u32(c);
410 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
411 iface->ra_not_onlink = blobmsg_get_bool(c);
413 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
414 const char *prio = blobmsg_get_string(c);
416 if (!strcmp(prio, "high"))
417 iface->route_preference = 1;
418 else if (!strcmp(prio, "low"))
419 iface->route_preference = -1;
420 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
421 iface->route_preference = 0;
426 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
427 iface->learn_routes = blobmsg_get_bool(c);
429 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
430 iface->external = blobmsg_get_bool(c);
432 if ((c = tb[IFACE_ATTR_NDPROXY_STATIC])) {
433 struct blob_attr *cur;
436 blobmsg_for_each_attr(cur, c, rem) {
437 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
440 int len = blobmsg_data_len(cur);
441 iface->static_ndp = realloc(iface->static_ndp, iface->static_ndp_len + len);
442 memcpy(&iface->static_ndp[iface->static_ndp_len], blobmsg_get_string(cur), len);
443 iface->static_ndp_len += len;
447 iface->ignore = (iface->ifindex = if_nametoindex(iface->ifname)) < 0;
451 close_interface(iface);
455 static int set_interface(struct uci_section *s)
457 blob_buf_init(&b, 0);
458 uci_to_blob(&b, s, &interface_attr_list);
459 return config_parse_interface(b.head, s->e.name, true);
463 static volatile bool do_reload = false;
464 static void set_stop(int signal)
467 do_reload = (signal == SIGHUP);
470 void odhcpd_run(void)
472 struct uci_context *uci = uci_alloc_context();
473 signal(SIGTERM, set_stop);
474 signal(SIGHUP, set_stop);
475 signal(SIGINT, set_stop);
481 list_for_each_entry(l, &leases, head) {
487 struct uci_package *dhcp = NULL;
488 if (!uci_load(uci, "dhcp", &dhcp)) {
489 struct uci_element *e;
490 uci_foreach_element(&dhcp->sections, e) {
491 struct uci_section *s = uci_to_section(e);
492 if (!strcmp(s->type, "lease"))
494 else if (!strcmp(s->type, "odhcpd"))
498 uci_foreach_element(&dhcp->sections, e) {
499 struct uci_section *s = uci_to_section(e);
500 if (!strcmp(s->type, "dhcp"))
506 ubus_apply_network();
509 // Evaluate hybrid mode for master
510 struct interface *master = NULL, *i;
511 list_for_each_entry(i, &interfaces, head) {
515 enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
516 if (i->dhcpv6 == RELAYD_HYBRID)
517 i->dhcpv6 = hybrid_mode;
519 if (i->ra == RELAYD_HYBRID)
522 if (i->ndp == RELAYD_HYBRID)
523 i->ndp = hybrid_mode;
525 if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
530 list_for_each_entry(i, &interfaces, head) {
531 if (i->inuse && !i->ignore) {
532 // Resolve hybrid mode
533 if (i->dhcpv6 == RELAYD_HYBRID)
534 i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
535 RELAYD_RELAY : RELAYD_SERVER;
537 if (i->ra == RELAYD_HYBRID)
538 i->ra = (master && master->ra == RELAYD_RELAY) ?
539 RELAYD_RELAY : RELAYD_SERVER;
541 if (i->ndp == RELAYD_HYBRID)
542 i->ndp = (master && master->ndp == RELAYD_RELAY) ?
543 RELAYD_RELAY : RELAYD_SERVER;
545 setup_router_interface(i, true);
546 setup_dhcpv6_interface(i, true);
547 setup_ndp_interface(i, true);
548 setup_dhcpv4_interface(i, true);
557 uci_unload(uci, dhcp);