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
};
19 IFACE_ATTR_DYNAMICDHCP
,
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
,
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
},
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
},
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
,
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
},
103 const struct uci_blob_param_list lease_attr_list
= {
104 .n_params
= LEASE_ATTR_MAX
,
105 .params
= lease_attrs
,
111 ODHCPD_ATTR_LEASEFILE
,
112 ODHCPD_ATTR_LEASETRIGGER
,
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
},
124 const struct uci_blob_param_list odhcpd_attr_list
= {
125 .n_params
= ODHCPD_ATTR_MAX
,
126 .params
= odhcpd_attrs
,
130 static struct interface
* get_interface(const char *name
)
133 list_for_each_entry(c
, &interfaces
, head
)
134 if (!strcmp(c
->name
, name
))
140 static void clean_interface(struct interface
*iface
)
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
));
151 static void close_interface(struct interface
*iface
)
153 if (iface
->head
.next
)
154 list_del(&iface
->head
);
156 setup_router_interface(iface
, false);
157 setup_dhcpv6_interface(iface
, false);
158 setup_ndp_interface(iface
, false);
159 setup_dhcpv4_interface(iface
, false);
161 clean_interface(iface
);
166 static int parse_mode(const char *mode
)
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")) {
174 } else if (!strcmp(mode
, "hybrid")) {
175 return RELAYD_HYBRID
;
182 static void set_config(struct uci_section
*s
)
184 struct blob_attr
*tb
[ODHCPD_ATTR_MAX
], *c
;
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
));
190 if ((c
= tb
[ODHCPD_ATTR_LEGACY
]))
191 config
.legacy
= blobmsg_get_bool(c
);
193 if ((c
= tb
[ODHCPD_ATTR_LEASEFILE
])) {
194 free(config
.dhcp_statefile
);
195 config
.dhcp_statefile
= strdup(blobmsg_get_string(c
));
198 if ((c
= tb
[ODHCPD_ATTR_LEASETRIGGER
])) {
199 free(config
.dhcp_cb
);
200 config
.dhcp_cb
= strdup(blobmsg_get_string(c
));
205 static int set_lease(struct uci_section
*s
)
207 struct blob_attr
*tb
[LEASE_ATTR_MAX
], *c
;
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
));
214 if ((c
= tb
[LEASE_ATTR_HOSTNAME
]))
215 hostlen
= blobmsg_data_len(c
);
217 struct lease
*lease
= calloc(1, sizeof(*lease
) + hostlen
);
220 memcpy(lease
->hostname
, blobmsg_get_string(c
), hostlen
);
222 if ((c
= tb
[LEASE_ATTR_IP
]))
223 if (inet_pton(AF_INET
, blobmsg_get_string(c
), &lease
->ipaddr
) < 0)
226 if ((c
= tb
[LEASE_ATTR_MAC
]))
227 if (!ether_aton_r(blobmsg_get_string(c
), &lease
->mac
))
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
));
239 lease
->duid_len
= len
;
242 if ((c
= tb
[LEASE_ATTR_HOSTID
]))
243 if (odhcpd_unhexlify((uint8_t*)&lease
->hostid
, sizeof(lease
->hostid
),
244 blobmsg_get_string(c
)) < 0)
247 list_add(&lease
->head
, &leases
);
257 int config_parse_interface(struct blob_attr
*b
, const char *name
, bool overwrite
)
259 struct blob_attr
*tb
[IFACE_ATTR_MAX
], *c
;
260 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, blob_data(b
), blob_len(b
));
262 if (tb
[IFACE_ATTR_INTERFACE
])
263 name
= blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]);
268 struct interface
*iface
= get_interface(name
);
270 iface
= calloc(1, sizeof(*iface
));
271 strncpy(iface
->name
, name
, sizeof(iface
->name
) - 1);
272 list_add(&iface
->head
, &interfaces
);
274 clean_interface(iface
);
277 const char *ifname
= NULL
;
280 ifname
= ubus_get_ifname(name
);
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
);
290 strncpy(iface
->ifname
, ifname
, sizeof(iface
->ifname
) - 1);
294 clean_interface(iface
);
296 if ((c
= tb
[IFACE_ATTR_DYNAMICDHCP
]))
297 iface
->no_dynamic_dhcp
= !blobmsg_get_bool(c
);
299 if ((c
= tb
[IFACE_ATTR_IGNORE
]))
300 iface
->ignore
= blobmsg_get_bool(c
);
302 if ((c
= tb
[IFACE_ATTR_LEASETIME
])) {
303 char *val
= blobmsg_get_string(c
), *endptr
;
304 double time
= strtod(val
, &endptr
);
305 if (time
&& endptr
[0]) {
306 if (endptr
[0] == 's')
308 else if (endptr
[0] == 'm')
310 else if (endptr
[0] == 'h')
312 else if (endptr
[0] == 'd')
314 else if (endptr
[0] == 'w')
315 time
*= 7 * 24 * 3600;
321 iface
->dhcpv4_leasetime
= time
;
324 if ((c
= tb
[IFACE_ATTR_START
])) {
325 iface
->dhcpv4_start
.s_addr
= htonl(blobmsg_get_u32(c
));
328 iface
->dhcpv4
= RELAYD_SERVER
;
331 if ((c
= tb
[IFACE_ATTR_LIMIT
]))
332 iface
->dhcpv4_end
.s_addr
= htonl(
333 ntohl(iface
->dhcpv4_start
.s_addr
) + blobmsg_get_u32(c
));
335 if ((c
= tb
[IFACE_ATTR_MASTER
]))
336 iface
->master
= blobmsg_get_bool(c
);
338 if ((c
= tb
[IFACE_ATTR_UPSTREAM
])) {
339 struct blob_attr
*cur
;
342 blobmsg_for_each_attr(cur
, c
, rem
) {
343 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, NULL
))
346 iface
->upstream
= realloc(iface
->upstream
,
347 iface
->upstream_len
+ blobmsg_data_len(cur
));
348 memcpy(iface
->upstream
+ iface
->upstream_len
, blobmsg_get_string(cur
), blobmsg_data_len(cur
));
349 iface
->upstream_len
+= blobmsg_data_len(cur
);
354 if ((c
= tb
[IFACE_ATTR_RA
])) {
355 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
361 if ((c
= tb
[IFACE_ATTR_DHCPV4
])) {
362 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
363 iface
->dhcpv4
= mode
;
368 if ((c
= tb
[IFACE_ATTR_DHCPV6
])) {
369 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
370 iface
->dhcpv6
= mode
;
375 if ((c
= tb
[IFACE_ATTR_NDP
])) {
376 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
382 if ((c
= tb
[IFACE_ATTR_DNS
])) {
383 struct blob_attr
*cur
;
386 iface
->always_rewrite_dns
= true;
387 blobmsg_for_each_attr(cur
, c
, rem
) {
388 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, NULL
))
391 struct in_addr addr4
;
392 struct in6_addr addr6
;
393 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
394 iface
->dhcpv4_dns
= realloc(iface
->dhcpv4_dns
,
395 (++iface
->dhcpv4_dns_cnt
) * sizeof(*iface
->dhcpv4_dns
));
396 iface
->dhcpv4_dns
[iface
->dhcpv4_dns_cnt
- 1] = addr4
;
397 } else if (inet_pton(AF_INET6
, blobmsg_get_string(cur
), &addr6
) == 1) {
398 iface
->dns
= realloc(iface
->dns
,
399 (++iface
->dns_cnt
) * sizeof(*iface
->dns
));
400 iface
->dns
[iface
->dns_cnt
- 1] = addr6
;
407 if ((c
= tb
[IFACE_ATTR_DOMAIN
])) {
408 struct blob_attr
*cur
;
411 blobmsg_for_each_attr(cur
, c
, rem
) {
412 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, NULL
))
416 int len
= dn_comp(blobmsg_get_string(cur
), buf
, sizeof(buf
), NULL
, NULL
);
420 iface
->search
= realloc(iface
->search
, iface
->search_len
+ len
);
421 memcpy(&iface
->search
[iface
->search_len
], buf
, len
);
422 iface
->search_len
+= len
;
426 if ((c
= tb
[IFACE_ATTR_ULA_COMPAT
]))
427 iface
->deprecate_ula_if_public_avail
= blobmsg_get_bool(c
);
429 if ((c
= tb
[IFACE_ATTR_RA_DEFAULT
]))
430 iface
->default_router
= blobmsg_get_u32(c
);
432 if ((c
= tb
[IFACE_ATTR_RA_MANAGEMENT
]))
433 iface
->managed
= blobmsg_get_u32(c
);
435 if ((c
= tb
[IFACE_ATTR_RA_OFFLINK
]))
436 iface
->ra_not_onlink
= blobmsg_get_bool(c
);
438 if ((c
= tb
[IFACE_ATTR_RA_PREFERENCE
])) {
439 const char *prio
= blobmsg_get_string(c
);
441 if (!strcmp(prio
, "high"))
442 iface
->route_preference
= 1;
443 else if (!strcmp(prio
, "low"))
444 iface
->route_preference
= -1;
445 else if (!strcmp(prio
, "medium") || !strcmp(prio
, "default"))
446 iface
->route_preference
= 0;
451 if ((c
= tb
[IFACE_ATTR_NDPROXY_ROUTING
]))
452 iface
->learn_routes
= blobmsg_get_bool(c
);
454 if ((c
= tb
[IFACE_ATTR_NDPROXY_SLAVE
]))
455 iface
->external
= blobmsg_get_bool(c
);
457 if ((c
= tb
[IFACE_ATTR_NDPROXY_STATIC
])) {
458 struct blob_attr
*cur
;
461 blobmsg_for_each_attr(cur
, c
, rem
) {
462 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, NULL
))
465 int len
= blobmsg_data_len(cur
);
466 iface
->static_ndp
= realloc(iface
->static_ndp
, iface
->static_ndp_len
+ len
);
467 memcpy(&iface
->static_ndp
[iface
->static_ndp_len
], blobmsg_get_string(cur
), len
);
468 iface
->static_ndp_len
+= len
;
472 iface
->ignore
= (iface
->ifindex
= if_nametoindex(iface
->ifname
)) < 0;
476 close_interface(iface
);
480 static int set_interface(struct uci_section
*s
)
482 blob_buf_init(&b
, 0);
483 uci_to_blob(&b
, s
, &interface_attr_list
);
484 return config_parse_interface(b
.head
, s
->e
.name
, true);
488 static volatile int do_reload
= false;
489 static void set_stop(int signal
)
492 do_reload
= (signal
== SIGHUP
);
495 void odhcpd_run(void)
497 struct uci_context
*uci
= uci_alloc_context();
498 signal(SIGTERM
, set_stop
);
499 signal(SIGHUP
, set_stop
);
500 signal(SIGINT
, set_stop
);
507 do_reload
= uloop_cancelled
= false;
510 list_for_each_entry(l
, &leases
, head
) {
516 struct uci_package
*dhcp
= NULL
;
517 if (!uci_load(uci
, "dhcp", &dhcp
)) {
518 struct uci_element
*e
;
519 uci_foreach_element(&dhcp
->sections
, e
) {
520 struct uci_section
*s
= uci_to_section(e
);
521 if (!strcmp(s
->type
, "lease"))
523 else if (!strcmp(s
->type
, "odhcpd"))
527 uci_foreach_element(&dhcp
->sections
, e
) {
528 struct uci_section
*s
= uci_to_section(e
);
529 if (!strcmp(s
->type
, "dhcp"))
535 ubus_apply_network();
538 // Evaluate hybrid mode for master
539 struct interface
*master
= NULL
, *i
, *n
;
540 list_for_each_entry(i
, &interfaces
, head
) {
544 enum odhcpd_mode hybrid_mode
= RELAYD_DISABLED
;
546 if (ubus_has_prefix(i
->name
, i
->ifname
))
547 hybrid_mode
= RELAYD_RELAY
;
550 if (i
->dhcpv6
== RELAYD_HYBRID
)
551 i
->dhcpv6
= hybrid_mode
;
553 if (i
->ra
== RELAYD_HYBRID
)
556 if (i
->ndp
== RELAYD_HYBRID
)
557 i
->ndp
= hybrid_mode
;
559 if (i
->dhcpv6
== RELAYD_RELAY
|| i
->ra
== RELAYD_RELAY
|| i
->ndp
== RELAYD_RELAY
)
564 list_for_each_entry_safe(i
, n
, &interfaces
, head
) {
566 // Resolve hybrid mode
567 if (i
->dhcpv6
== RELAYD_HYBRID
)
568 i
->dhcpv6
= (master
&& master
->dhcpv6
== RELAYD_RELAY
) ?
569 RELAYD_RELAY
: RELAYD_SERVER
;
571 if (i
->ra
== RELAYD_HYBRID
)
572 i
->ra
= (master
&& master
->ra
== RELAYD_RELAY
) ?
573 RELAYD_RELAY
: RELAYD_SERVER
;
575 if (i
->ndp
== RELAYD_HYBRID
)
576 i
->ndp
= (master
&& master
->ndp
== RELAYD_RELAY
) ?
577 RELAYD_RELAY
: RELAYD_SERVER
;
579 setup_router_interface(i
, true);
580 setup_dhcpv6_interface(i
, true);
581 setup_ndp_interface(i
, true);
582 setup_dhcpv4_interface(i
, true);
591 uci_unload(uci
, dhcp
);