DHCPREQUEST not working with gettimeofday() after system time jumps

Bertram Stemer bertram.stemer at omicron.at
Thu Sep 4 06:32:47 UTC 2014


we have a linux device that has no battery powered real-time clock. This means
that the system time has to be set to some clock source (e.g. Free Running,
Applied from PC, NTP, PTP). When using the isc-dhcp client we observed
a problem when the system time performs a jump.

The isc-dhcp client (4.2.3) uses the gettimeofday() function which, IMHO, shall not
be used for timeout or interval calculation. You have to use a monotonic clock
instead. A time jump can lead to the problem that the client does not send out
the DHCPREQUEST message within the offered lease time. After the lease time,
the server can assign the IP address, which is still in use, to another device (IP address conflicts!)

I preloaded the dhclient with my own implementation of gettimeofday().
Now, system time jumps do not affect the DHCPREQUEST behaviour of the
client any more. Could that lead to any other problems? Shouldn’t that be
fixed in the isc-dhcp implementation (both server and client)?

Many thanks in advance,

$ LD_PRELOAD=mysystime.so /usr/sbin/dhclient ....

$ cat mysystime.c
#define _GNU_SOURCE
#include <time.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <stdio.h>

void gettimeofday_init(void) __attribute__ ((constructor));

int *(*orig_gettimeofday)(struct timeval *tv, struct timezone *tz);

int gettimeofday(struct timeval *tv, struct timezone *tz)
                struct timespec ts;
                clock_gettime(CLOCK_MONOTONIC_RAW, &ts);

                if (tz) {
                                struct timeval tv1;
                                orig_gettimeofday(&tv1, tz);

                tv->tv_sec = ts.tv_sec;
                tv->tv_usec = ts.tv_nsec / 1000;

                return 0;

void gettimeofday_init(void)
                printf("Loading hacked function gettimeofday().\n");
                orig_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.isc.org/pipermail/dhcp-users/attachments/20140904/84305fb6/attachment.html>

More information about the dhcp-users mailing list