Skip to content

Commit cb5d804

Browse files
committed
confd: add dhcp-server validation of address pool and/or static host
A valid DHCP server setup for a subnet is one of pool and/or at least one static host entry/lease. If pool is enabled the pool must have a start and an end address. To allow setting up a DHCP server with no pool and at least one static host entry/lease, we make the pool a presence container, otherwise the pool will always be set and trigger the below inference. When an interactive CLI/Web user enables the address pool we infer a default range .100-.250, but only for /24, C-class networks. This is what most users know and expect. The YANG model now validates that: - If an address pool is created, both start-address and end-address must be set - Each subnet must have either a pool or at least one static host entry - The pool container is now a presence container, so "no pool" fully deletes it Fixes #1121 Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent d80186a commit cb5d804

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

doc/ChangeLog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Change Log
33

44
All notable changes to the project are documented in this file.
55

6-
[v25.10.0][UNRELEASED] -
6+
[v25.10.0][] - 2025-10-31
77
-------------------------
88

99
### Changes
@@ -31,6 +31,10 @@ All notable changes to the project are documented in this file.
3131

3232
- Fix #981: copying any file, including `running-config`, to the persistent
3333
back-end store for `startup-config`, does not take
34+
- Fix #1121: Ensure DHCP server does not crash if no address pool is set. This
35+
change infers a pool range (only) for /24 networks, and only when a pool is
36+
enabled. YANG validation for this and other use-cases is also included. As
37+
an unforeseen bonus, Infix now also support non-pool (static lease) setups
3438
- Fix #1146: Possible to set longer containers names than the system supports.
3539
Root cause, a limit of 15 characters implicitly imposed by the service mgmt
3640
daemon, Finit. The length has not been increased to 64 characters (min: 2)

src/confd/src/infix-dhcp-server.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ static void add(const char *subnet, struct lyd_node *cfg)
223223
start = lydx_get_cattr(node, "start-address");
224224
end = lydx_get_cattr(node, "end-address");
225225

226-
fprintf(fp, "\n# Subnet pool %s - %s\n", start, end);
227-
fprintf(fp, "dhcp-range=%s%sset:%s,%s,%s,%s\n",
228-
ifname ?: "", ifname ? "," : "", tag,
229-
start, end, lydx_get_cattr(node, "lease-time"));
226+
if (start && end) {
227+
fprintf(fp, "\n# Subnet pool %s - %s\n", start, end);
228+
fprintf(fp, "dhcp-range=%s%sset:%s,%s,%s,%s\n",
229+
ifname ?: "", ifname ? "," : "", tag,
230+
start, end, lydx_get_cattr(node, "lease-time"));
231+
}
230232
}
231233

232234
err:
@@ -400,6 +402,7 @@ static int cand(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
400402
"router",
401403
"dns-server",
402404
};
405+
sr_val_t *subnets = NULL;
403406
size_t i, cnt = 0;
404407

405408
if (event != SR_EV_UPDATE && event != SR_EV_CHANGE)
@@ -413,6 +416,37 @@ static int cand(sr_session_ctx_t *session, uint32_t sub_id, const char *module,
413416
srx_set_item(session, &inferred, 0, fmt, opt[i]);
414417
}
415418

419+
/* Infer pool: .100 to .250 for /24 networks */
420+
if (sr_get_items(session, CFG_XPATH "/subnet/subnet", 0, 0, &subnets, &cnt) == 0) {
421+
for (i = 0; i < cnt; i++) {
422+
const char *pool_xpathfmt = CFG_XPATH "/subnet[subnet='%s']/pool";
423+
const char *host_xpathfmt = CFG_XPATH "/subnet[subnet='%s']/host";
424+
const char *subnet = subnets[i].data.string_val;
425+
sr_val_t pool_val = { .type = SR_STRING_T };
426+
char start_addr[16], end_addr[16];
427+
unsigned int a, b, c, d, len;
428+
size_t pool_cnt = 0, host_cnt = 0;
429+
430+
if (sscanf(subnet, "%u.%u.%u.%u/%u", &a, &b, &c, &d, &len) != 5 || len != 24)
431+
continue;
432+
433+
/* Don't auto-infer if pool or static hosts already exist */
434+
if (!srx_nitems(session, &pool_cnt, pool_xpathfmt, subnet) && pool_cnt)
435+
continue;
436+
if (!srx_nitems(session, &host_cnt, host_xpathfmt, subnet) && host_cnt)
437+
continue;
438+
439+
snprintf(start_addr, sizeof(start_addr), "%u.%u.%u.100", a, b, c);
440+
snprintf(end_addr, sizeof(end_addr), "%u.%u.%u.250", a, b, c);
441+
442+
pool_val.data.string_val = start_addr;
443+
srx_set_item(session, &pool_val, 0, CFG_XPATH "/subnet[subnet='%s']/pool/start-address", subnet);
444+
pool_val.data.string_val = end_addr;
445+
srx_set_item(session, &pool_val, 0, CFG_XPATH "/subnet[subnet='%s']/pool/end-address", subnet);
446+
}
447+
sr_free_values(subnets, cnt);
448+
}
449+
416450
return 0;
417451
}
418452

src/confd/yang/confd.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ MODULES=(
2727
"infix-lldp@2025-05-05.yang"
2828
"infix-dhcp-common@2025-01-29.yang"
2929
"infix-dhcp-client@2025-01-29.yang"
30-
"infix-dhcp-server@2025-01-29.yang"
30+
"infix-dhcp-server@2025-10-28.yang"
3131
"infix-firewall@2025-04-26.yang"
3232
"infix-firewall-services@2025-04-26.yang"
3333
"infix-firewall-icmp-types@2025-04-26.yang"

src/confd/yang/confd/infix-dhcp-server.yang

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ module infix-dhcp-server {
2020
contact "kernelkit@googlegroups.com";
2121
description "This module implements a DHCPv4 server";
2222

23+
revision 2025-10-28 {
24+
description "Make pool a presence container and add pool validation.
25+
Also, require each subnet to have either a pool or
26+
at least one static host entry.";
27+
reference "internal";
28+
}
29+
2330
revision 2025-01-29 {
2431
description "Initial revision adapted for Infix from DHCPv6 model.";
2532
reference "internal";
@@ -132,6 +139,10 @@ module infix-dhcp-server {
132139
description "Subnet specific settings, including static host entries.";
133140
key "subnet";
134141

142+
must "pool or host" {
143+
error-message "Subnet must have either a pool or at least one static host entry.";
144+
}
145+
135146
leaf subnet {
136147
description "Subnet to serve DHCP leases from.";
137148
type inet:ipv4-prefix;
@@ -160,8 +171,13 @@ module infix-dhcp-server {
160171
}
161172

162173
container pool {
174+
presence "Enable dynamic DHCP address pool for this subnet.";
163175
description "IP address pool for this subnet.";
164176

177+
must "start-address and end-address" {
178+
error-message "Both start-address and end-address must be set if pool is configured.";
179+
}
180+
165181
leaf start-address {
166182
description "The start address of the DHCP address pool.";
167183
type inet:ipv4-address;

0 commit comments

Comments
 (0)