Skip to content

Commit f52e54a

Browse files
committed
lua: allow a hostname and pool to define a lease time
The default of 3600 isn't great for testing so this change is good for me too!
1 parent cd3df09 commit f52e54a

File tree

11 files changed

+166
-64
lines changed

11 files changed

+166
-64
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ Here is an example of how to use it.
6161
local domain = 'internal'
6262

6363
-- Lookup table to match hostname to IP address.
64+
-- Return a table of IP address and lease time to specify a lease time per host.
6465
local hostnames = {
6566
['netbsd'] = '10.73.1.70',
67+
['freebsd'] = { '10.73.1.71', 30 },
6668
}
6769

6870
-- Lookup table to match ethernet to hostnames.
@@ -102,13 +104,15 @@ end
102104
-- You can return a single table with address, netmask, from and to
103105
-- or a table of the above table (ie an array).
104106
-- Each address MUST exist on the interface.
107+
-- Setting a lease_time for the pool is optional.
105108
function configure_pools(if_name)
106109
if if_name == 'bridge0' then
107110
return {
108111
address = '10.73.1.1',
109112
netmask = '255.255.255.0',
110113
from = '10.73.1.100',
111114
to = '10.73.1.200',
115+
lease_time = 3600,
112116
}
113117
end
114118
end

src/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ ${TOP}/vendor/rbtree.o:
6161
${PROG}: ${DEPEND} ${OBJS}
6262
${CC} ${LDFLAGS} -o $@ ${OBJS} ${LDADD}
6363

64+
lint: _lint
65+
for x in ${SUBDIRS}; do ${MAKE} -C $$x $@ || exit $$?; done
66+
6467
proginstall: ${PROG}
6568
${INSTALL} -d ${DESTDIR}${SBINDIR}
6669
${INSTALL} -m ${BINMODE} ${PROG} ${DESTDIR}${SBINDIR}

src/Makefile.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ format:
1010
find . -iname "*.c" -o -iname "*.h" -not -name verstable.h | \
1111
xargs clang-format -i
1212

13-
lint:
13+
_lint:
1414
clang-tidy ${SRCS} -- ${CPPFLAGS} ${CFLAGS} -isystem ${TOP}/vendor
1515

1616
include ${TOP}/Makefile.inc

src/dhcp.c

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ dhcp_plugin_validateaddr(struct ctx *ctx, const struct in_addr *addr)
502502
}
503503

504504
static struct in_addr
505-
dhcp_plugin_findaddr(struct ctx *ctx, char *hostname, const struct bootp *bootp,
506-
size_t len)
505+
dhcp_plugin_findaddr(struct ctx *ctx, char *hostname, uint32_t *ltime,
506+
const struct bootp *bootp, size_t len)
507507
{
508508
struct plugin *p;
509509
struct sockaddr_in sin = {
@@ -517,6 +517,9 @@ dhcp_plugin_findaddr(struct ctx *ctx, char *hostname, const struct bootp *bootp,
517517
const char *hname = NULL;
518518
size_t l;
519519

520+
/* We might not get a least time so set to zero for a default. */
521+
*ltime = 0;
522+
520523
PLUGIN_FOREACH(ctx, p)
521524
{
522525
if (p->p_lookup_hostname == NULL)
@@ -547,8 +550,8 @@ dhcp_plugin_findaddr(struct ctx *ctx, char *hostname, const struct bootp *bootp,
547550
{
548551
if (p->p_lookup_addr == NULL)
549552
continue;
550-
err = p->p_lookup_addr(p, (struct sockaddr *)&sin, hname, bootp,
551-
len);
553+
err = p->p_lookup_addr(p, (struct sockaddr *)&sin, ltime, hname,
554+
bootp, len);
552555
if (err == -1) {
553556
if (errno != ESRCH && errno != ENOSYS)
554557
logerr("plugin %s", p->p_name);
@@ -787,10 +790,17 @@ dhcp_addoptions(struct bootp *bootp, uint8_t **p, const uint8_t *e,
787790
DHCP_PUT_B(p, e, DHO_MESSAGETYPE, type);
788791
DHCP_PUT_U32(p, e, DHO_SERVERID, pool->dp_addr.s_addr);
789792
if (type == DHCP_OFFER || type == DHCP_ACK) {
790-
uint32_t u32, lease_time = ctx->dhcp_lease_time;
793+
uint32_t u32, lease_time;
791794
struct plugin *plug;
792795
int n;
793796

797+
if (pool->dp_lease_time != 0)
798+
lease_time = pool->dp_lease_time;
799+
else
800+
lease_time = ctx->dhcp_lease_time;
801+
802+
/* Allow the client to request a shorter leasetime
803+
* but not a longer one. */
794804
opt = dhcp_findoption(req, reqlen, DHO_LEASETIME);
795805
if (opt != NULL && opt[0] == sizeof(u32)) {
796806
memcpy(&u32, opt + 1, sizeof(u32));
@@ -988,17 +998,23 @@ dhcp_set_expire_timeout(struct dhcp_ctx *ctx)
988998

989999
static void
9901000
dhcp_lease_settime(struct dhcp_ctx *ctx, struct dhcp_lease *lease,
991-
struct timespec *now, const struct bootp *bootp, size_t len)
1001+
struct timespec *now, uint32_t lease_time, const struct bootp *bootp,
1002+
size_t len)
9921003
{
9931004
const uint8_t *opt;
994-
uint32_t u32, lease_time = ctx->dhcp_lease_time;
9951005

996-
opt = dhcp_findoption(bootp, len, DHO_LEASETIME);
997-
if (opt != NULL && opt[0] == sizeof(u32)) {
998-
memcpy(&u32, opt + 1, sizeof(u32));
999-
u32 = ntohl(u32);
1000-
if (u32 < lease_time)
1001-
lease_time = u32;
1006+
if (lease_time == 0) {
1007+
uint32_t u32;
1008+
1009+
lease_time = ctx->dhcp_lease_time;
1010+
1011+
opt = dhcp_findoption(bootp, len, DHO_LEASETIME);
1012+
if (opt != NULL && opt[0] == sizeof(u32)) {
1013+
memcpy(&u32, opt + 1, sizeof(u32));
1014+
u32 = ntohl(u32);
1015+
if (u32 < lease_time)
1016+
lease_time = u32;
1017+
}
10021018
}
10031019

10041020
if (lease->dl_in_expire_tree) {
@@ -1095,6 +1111,7 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
10951111
uint8_t type, clientid[DHCP_CLIENTID_LEN + 1], fqdn_flags;
10961112
struct in_addr addr = { .s_addr = INADDR_ANY };
10971113
char phostname[DHCP_HOSTNAME_LEN] = { '\0' };
1114+
uint32_t pltime = 0;
10981115
struct in_addr paddr = { .s_addr = INADDR_ANY };
10991116
struct dhcp_lease *lease = NULL, *wanted = NULL;
11001117
char clid_buf[sizeof(clientid) * 3];
@@ -1153,8 +1170,10 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
11531170
clientid[0] = bootp->hlen + 1;
11541171
clientid[1] = bootp->htype;
11551172
memcpy(clientid + 2, bootp->chaddr, bootp->hlen);
1156-
} else
1173+
} else {
11571174
clientid[0] = '\0';
1175+
clientid[1] = '\0'; /* silences maybe uninitialiased warning */
1176+
}
11581177

11591178
clid = hwaddr_ntoa(clientid + 1, clientid[0], clid_buf,
11601179
sizeof(clid_buf));
@@ -1170,7 +1189,7 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
11701189
return;
11711190
if (lease->dl_addr.s_addr == INADDR_ANY) {
11721191
lease->dl_addr = dhcp_plugin_findaddr(ifp->if_ctx,
1173-
phostname, bootp, len);
1192+
phostname, &pltime, bootp, len);
11741193
if (lease->dl_addr.s_addr != INADDR_ANY)
11751194
lease->dl_flags |= DL_PLUGIN_ADDRESS;
11761195
}
@@ -1260,8 +1279,8 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
12601279
}
12611280
/* FALLTHROUGH */
12621281
case DHCP_REQUEST:
1263-
paddr = dhcp_plugin_findaddr(ifp->if_ctx, phostname, bootp,
1264-
len);
1282+
paddr = dhcp_plugin_findaddr(ifp->if_ctx, phostname, &pltime,
1283+
bootp, len);
12651284
if (paddr.s_addr == INADDR_ANY)
12661285
break;
12671286
wanted = dhcp_lease_findaddr(ctx, &paddr);
@@ -1365,7 +1384,7 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
13651384
expires = lease->dl_expires;
13661385
else
13671386
timespecclear(&expires);
1368-
dhcp_lease_settime(ctx, lease, &now, bootp, len);
1387+
dhcp_lease_settime(ctx, lease, &now, pltime, bootp, len);
13691388
if (wanted != lease) {
13701389
if (dhcp_lease_insertaddr(ctx, lease) == -1) {
13711390
logerr("%s: dhcp_lease_insertaddr", __func__);
@@ -1417,7 +1436,7 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
14171436
lease->dl_flags |= DL_PLUGIN_ADDRESS;
14181437
else
14191438
lease->dl_flags &= ~DL_PLUGIN_ADDRESS;
1420-
dhcp_lease_settime(ctx, lease, &now, bootp, len);
1439+
dhcp_lease_settime(ctx, lease, &now, pltime, bootp, len);
14211440
if (wanted != lease) {
14221441
if (dhcp_lease_insertaddr(ctx, lease) == -1) {
14231442
logerr("%s: dhcp_lease_insertaddr", __func__);
@@ -1557,7 +1576,6 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
15571576
if (lease->dl_hostname[0] != '\0')
15581577
lease->dl_flags |= DL_HOSTNAME;
15591578
}
1560-
dhcp_commit_lease(ctx, lease, bootp, len);
15611579
break;
15621580
}
15631581

@@ -1567,6 +1585,10 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len,
15671585
case DHCP_ACK:
15681586
case DHCP_NAK:
15691587
dhcp_output(ifp, lease, type, msg, bootp, len);
1588+
/* Commit the lease after sending it as it might
1589+
* be a slow operation depending on what the plugins do. */
1590+
if (type != DHCP_NAK)
1591+
dhcp_commit_lease(ctx, lease, bootp, len);
15701592
break;
15711593
}
15721594
}

src/dhcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ struct dhcp_pool {
157157
struct in_addr dp_mask;
158158
struct in_addr dp_from;
159159
struct in_addr dp_to;
160+
uint32_t dp_lease_time;
160161
};
161162

162163
#define DHCP_CLIENTID_LEN 1 + 255 /* first byte is length */

src/plugin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ struct plugin {
4848
int (*p_init_proto)(struct plugin *);
4949
int (*p_unload)(struct plugin *);
5050
ssize_t (*p_configure_pools)(struct plugin *, struct interface *ifp);
51-
int (*p_lookup_addr)(struct plugin *, struct sockaddr *, const char *,
52-
const struct bootp *, size_t);
51+
int (*p_lookup_addr)(struct plugin *, struct sockaddr *, uint32_t *,
52+
const char *, const struct bootp *, size_t);
5353
int (*p_lookup_hostname)(struct plugin *, char *, const struct bootp *,
5454
size_t);
5555
int (*p_addr_to_hostname)(struct plugin *, const struct sockaddr *sa);

src/plugins/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ lua.So:
4848
lua.so: lua.So
4949
${CC} ${LDFLAGS} -shared -Wl,-x -o $@ -Wl,-soname,$@ lua.So ${LUA_LIBS}
5050

51+
lint: _lint
52+
5153
proginstall: ${PLUGS}
5254
${INSTALL} -d ${DESTDIR}${PLUGINDIR}
5355
${INSTALL} -m ${BINMODE} ${PROG} ${PLUGS} ${DESTDIR}${PLUGINDIR}

src/plugins/addrinfo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ static const char addrinfo_description[] =
5050

5151
static int
5252
addrinfo_lookup_addr(struct plugin *p, struct sockaddr *sa,
53-
const char *hostname, const struct bootp *bootp, size_t bootplen)
53+
__unused uint32_t *ltime, const char *hostname, const struct bootp *bootp,
54+
size_t bootplen)
5455
{
5556
const uint8_t *opt;
5657
char hname[MAXHOSTNAMELEN + 1];

src/plugins/icmp.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct icmp_hold {
8888
struct icmp_ctx {
8989
struct ctx *i_ctx;
9090
int i_fd;
91-
uint8_t *i_buf;
91+
void *i_buf;
9292
size_t i_buflen;
9393
icmp_addr_map i_hold;
9494
#ifdef HAVE_CASPER
@@ -119,6 +119,7 @@ icmp_echo_request(void *arg)
119119
.icmp_seq = htons(ih->ih_nrequests),
120120
};
121121
struct sockaddr *sa = (struct sockaddr *)&ih->ih_sin;
122+
socklen_t salen = (socklen_t)sa_len(sa);
122123

123124
icmp.icmp_cksum = in_cksum(&icmp, sizeof(icmp), NULL);
124125
if (icmp.icmp_cksum == 0)
@@ -132,13 +133,12 @@ icmp_echo_request(void *arg)
132133
logdebugx("%s: echo request: %s %u", icmp_name,
133134
inet_ntoa(ih->ih_sin.sin_addr), ih->ih_id);
134135
#ifdef HAVE_CASPER
135-
if (cap_connect(ctx->i_ctx->ctx_capnet, ctx->i_capfd, sa, sa_len(sa)) ==
136-
-1)
136+
if (cap_connect(ctx->i_ctx->ctx_capnet, ctx->i_capfd, sa, salen) == -1)
137137
logerr("%s: cap_connect", __func__);
138138
else if (send(ctx->i_capfd, &icmp, sizeof(icmp), 0) == -1)
139139
logerr("%s: send", __func__);
140140
#else
141-
if (sendto(ctx->i_fd, &icmp, sizeof(icmp), 0, sa, sa_len(sa)) == -1)
141+
if (sendto(ctx->i_fd, &icmp, sizeof(icmp), 0, sa, salen) == -1)
142142
logerr("%s: sendto", __func__);
143143
#endif
144144
}
@@ -230,7 +230,7 @@ icmp_read0(struct icmp_ctx *ctx, int fd, unsigned short e)
230230
return;
231231
}
232232

233-
icmp = (struct icmp *)(ctx->i_buf + hlen);
233+
icmp = (struct icmp *)(void *)((uint8_t *)ctx->i_buf + hlen);
234234
if (icmp->icmp_type != ICMP_ECHOREPLY)
235235
return;
236236

src/plugins/leasefile.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ lf_write_lease(void *arg, struct dhcp_lease *lease)
8686
char clientidbuf[lease->dl_clientid[0] * 3];
8787
const char *clientid, *flags;
8888

89-
gmtime_r(&lease->dl_leased.tv_sec, &leased);
89+
if (gmtime_r(&lease->dl_leased.tv_sec, &leased) == NULL)
90+
return -1;
9091
if (strftime(sleased, sizeof(sleased), ISO8061_FMT, &leased) == 0)
9192
return -1;
92-
gmtime_r(&lease->dl_expires.tv_sec, &expires);
93+
if (gmtime_r(&lease->dl_expires.tv_sec, &expires) == NULL)
94+
return -1;
9395
if (strftime(sexpires, sizeof(sexpires), ISO8061_FMT, &expires) == 0)
9496
return -1;
9597

0 commit comments

Comments
 (0)