[RFC 2/2] udhcpc: Allow to limit maximum lease timeout

Sven Eckelmann sven.eckelmann at open-mesh.com
Tue Nov 15 12:47:59 UTC 2016


From: Marek Lindner <marek.lindner at open-mesh.com>

The lease timeout accepted by udhcpc can currently be any value between 16s
and 268435455s (~8.5 years). This maximum limit can be too much for mobile
setups with changing DHCP servers. Usually these settings will then be
lowered on the DHCP server. But the user may not have access over the DHCP
server and thus has to avoid these large lease timeouts purely on the client
side.

The new command line parameter lease-max-sec allow to set this maximum
limit. By default it will be set to 0 and not used at all.

Signed-off-by: Marek Lindner <marek.lindner at open-mesh.com>
Signed-off-by: Sven Eckelmann <sven.eckelmann at open-mesh.com>
---
Not sure if someone works with similar setups.  Maybe someone still has
interests in this patch (or in a variant of it)...
---
 networking/udhcp/dhcpc.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index 10b9e0f..13efe18 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -68,6 +68,7 @@ static const char udhcpc_longopts[] ALIGN1 =
 	"foreground\0"     No_argument       "f"
 	"background\0"     No_argument       "b"
 	"broadcast\0"      No_argument       "B"
+	"lease-max-sec\0"  Required_argument "l"
 	IF_FEATURE_UDHCPC_ARPING("arping\0"	Optional_argument "a")
 	IF_FEATURE_UDHCP_PORT("client-port\0"	Required_argument "P")
 	;
@@ -95,8 +96,9 @@ enum {
 	OPT_x = 1 << 18,
 	OPT_f = 1 << 19,
 	OPT_B = 1 << 20,
+	OPT_l = 1 << 21,
 /* The rest has variable bit positions, need to be clever */
-	OPTBIT_B = 20,
+	OPTBIT_l = 21,
 	USE_FOR_MMU(             OPTBIT_b,)
 	IF_FEATURE_UDHCPC_ARPING(OPTBIT_a,)
 	IF_FEATURE_UDHCP_PORT(   OPTBIT_P,)
@@ -1185,6 +1187,7 @@ static void client_background(void)
 //usage:     "\n	-t,--retries N		Send up to N discover packets (default 3)"
 //usage:     "\n	-T,--timeout SEC	Pause between packets (default 3)"
 //usage:     "\n	-A,--tryagain SEC	Wait if lease is not obtained (default 20)"
+//usage:     "\n	-l,--lease-max-sec SEC	Limit lease timeout to SEC"
 //usage:     "\n	-n,--now		Exit if lease is not obtained"
 //usage:     "\n	-q,--quit		Exit after obtaining lease"
 //usage:     "\n	-R,--release		Release IP on exit"
@@ -1266,6 +1269,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
 	int tryagain_timeout = 20;
 	int discover_timeout = 3;
 	int discover_retries = 3;
+	uint32_t lease_max_timeout = 0;
 	uint32_t server_addr = server_addr; /* for compiler */
 	uint32_t requested_ip = 0;
 	uint32_t xid = xid; /* for compiler */
@@ -1291,7 +1295,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
 	/* O,x: list; -T,-t,-A take numeric param */
 	IF_UDHCP_VERBOSE(opt_complementary = "vv";)
 	IF_LONG_OPTS(applet_long_options = udhcpc_longopts;)
-	opt = getopt32(argv, "CV:H:h:F:i:np:qRr:s:T:+t:+SA:+O:*ox:*fB"
+	opt = getopt32(argv, "CV:H:h:F:i:np:qRr:s:T:+t:+SA:+O:*ox:*fBl:+"
 		USE_FOR_MMU("b")
 		IF_FEATURE_UDHCPC_ARPING("a::")
 		IF_FEATURE_UDHCP_PORT("P:")
@@ -1302,6 +1306,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
 		, &discover_timeout, &discover_retries, &tryagain_timeout /* T,t,A */
 		, &list_O
 		, &list_x
+		, &lease_max_timeout /* l */
 		IF_FEATURE_UDHCPC_ARPING(, &str_a)
 		IF_FEATURE_UDHCP_PORT(, &str_P)
 		IF_UDHCP_VERBOSE(, &dhcp_verbose)
@@ -1735,11 +1740,20 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
 				temp = udhcp_get_option(&packet, DHCP_LEASE_TIME);
 				if (!temp) {
 					bb_error_msg("no lease time with ACK, using 1 hour lease");
+
 					lease_seconds = 60 * 60;
+					if (lease_max_timeout > 0 && lease_seconds > lease_max_timeout) {
+						lease_seconds = lease_max_timeout;
+					}
 				} else {
 					/* it IS unaligned sometimes, don't "optimize" */
 					move_from_unaligned32(lease_seconds, temp);
 					lease_seconds = ntohl(lease_seconds);
+
+					if (lease_max_timeout > 0 && lease_seconds > lease_max_timeout) {
+						lease_seconds = lease_max_timeout;
+					}
+
 					/* paranoia: must not be too small and not prone to overflows */
 					if (lease_seconds < 0x10)
 						lease_seconds = 0x10;
-- 
2.10.2



More information about the busybox mailing list