[BusyBox] Serious bug in udhcpc

Joel Coltoff joel at wmi.com
Tue Apr 22 15:07:48 UTC 2003


I bumped into this problem a few weeks back but it showed up in
a different way. We have no clock on the board. The kernel is
configured with a default time of Decemeber 31, 2002. It may
be 12:00 but I'm too lazy to check now. It hardly matters. We
do fine getting an initial address and then use rdate to set the
clock to the current time of day. This is what causes our problem.
When we go to renew the calculation of

	tv.tv_sec = timeout - time(0);

is not what it should be. The first time (and only the first
time) we renew there is a burst of DHCPREQUEST at the server. If
I do a reboot before the first renew I also get a burst. My fix
takes time() out of the picture becuase all we care about (I think)
Before select() is called the current time is subtracted out anyway.
One case where using time() would be helpful would be if you suspend
the process and then wake it up later. If you wake it before the
renew time you'll get back in sync. If you wake it after things will
get messed up. If you do suspend it you deserver whatever you get.
Here is what I did to fix it.

****************
--- old/dhcpc.c	Tue Apr 22 11:04:01 2003
+++ new/dhcpc.c	Tue Apr 22 11:03:46 2003
@@ -323,9 +323,11 @@
 	run_script(NULL, "deconfig");
 	change_mode(LISTEN_RAW);

+	timeout = -1;
 	for (;;) {

-		tv.tv_sec = timeout - time(0);
+		//tv.tv_sec = timeout - time(0);
+		tv.tv_sec = timeout;
 		tv.tv_usec = 0;
 		FD_ZERO(&rfds);

@@ -360,7 +362,8 @@
 					/* send discover packet */
 					send_discover(xid, requested_ip); /* broadcast */

-					timeout = now + ((packet_num == 2) ? 4 : 2);
+					//timeout = now + ((packet_num == 2) ? 4 : 2);
+					timeout = ((packet_num == 2) ? 4 : 2);
 					packet_num++;
 				} else {
 					if (client_config.background_if_no_lease) {
@@ -372,7 +375,8 @@
 				  	}
 					/* wait to try again */
 					packet_num = 0;
-					timeout = now + 60;
+					//timeout = now + 60;
+					timeout = 60;
 				}
 				break;
 			case RENEW_REQUESTED:
@@ -383,13 +387,15 @@
 						send_renew(xid, server_addr, requested_ip); /* unicast */
 					else send_selecting(xid, server_addr, requested_ip); /* broadcast */

-					timeout = now + ((packet_num == 2) ? 10 : 2);
+					//timeout = now + ((packet_num == 2) ? 10 : 2);
+					timeout = ((packet_num == 2) ? 10 : 2);
 					packet_num++;
 				} else {
 					/* timed out, go back to init state */
 					if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
 					state = INIT_SELECTING;
-					timeout = now;
+					//timeout = now;
+					timeout = 0;
 					packet_num = 0;
 					change_mode(LISTEN_RAW);
 				}
@@ -405,14 +411,16 @@
 				if ((t2 - t1) <= (lease / 14400 + 1)) {
 					/* timed out, enter rebinding state */
 					state = REBINDING;
-					timeout = now + (t2 - t1);
+					//timeout = now + (t2 - t1);
+					timeout = (t2 - t1);
 					DEBUG(LOG_INFO, "Entering rebinding state");
 				} else {
 					/* send a request packet */
 					send_renew(xid, server_addr, requested_ip); /* unicast */

-					t1 = (t2 - t1) / 2 + t1;
-					timeout = t1 + start;
+					//timeout = t1 + start;
+					timeout = (t2 - t1) / 2;
+					t1 = timeout + t1;
 				}
 				break;
 			case REBINDING:
@@ -422,15 +430,17 @@
 					state = INIT_SELECTING;
 					LOG(LOG_INFO, "Lease lost, entering init state");
 					run_script(NULL, "deconfig");
-					timeout = now;
+					//timeout = now;
+					timeout = 0;
 					packet_num = 0;
 					change_mode(LISTEN_RAW);
 				} else {
 					/* send a request packet */
 					send_renew(xid, 0, requested_ip); /* broadcast */

-					t2 = (lease - t2) / 2 + t2;
-					timeout = t2 + start;
+					//timeout = t2 + start;
+					timeout = (lease - t2) / 2;
+					t2 = timeout + t2;
 				}
 				break;
 			case RELEASED:
@@ -473,7 +483,8 @@

 						/* enter requesting state */
 						state = REQUESTING;
-						timeout = now;
+						//timeout = now;
+						timeout = 0;
 						packet_num = 0;
 					} else {
 						DEBUG(LOG_ERR, "No server ID in message");
@@ -502,7 +513,8 @@
 					LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
 						inet_ntoa(temp_addr), lease);
 					start = now;
-					timeout = t1 + start;
+					//timeout = t1 + start;
+					timeout = t1;
 					requested_ip = packet.yiaddr;
 					run_script(&packet,
 						   ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
@@ -521,7 +533,8 @@
 					if (state != REQUESTING)
 						run_script(NULL, "deconfig");
 					state = INIT_SELECTING;
-					timeout = now;
+					//timeout = now;
+					timeout = 0;
 					requested_ip = 0;
 					packet_num = 0;
 					change_mode(LISTEN_RAW);
@@ -531,7 +544,7 @@
 			/* case BOUND, RELEASED: - ignore all packets */
 			}
 		} else if (retval > 0 && FD_ISSET(signal_pipe[0], &rfds)) {
-			if (read(signal_pipe[0], &sig, sizeof(signal)) < 0) {
+			if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) {
 				DEBUG(LOG_ERR, "Could not read signal: %s",
 					strerror(errno));
 				continue; /* probably just EINTR */
****************

-- 
Joel Coltoff

Education is not the filling of a pail, but the lighting of a fire.
    -- W. B. Yeats



More information about the busybox mailing list