[U-Boot] [RFC] Preventing overriding of serverip when set in environment by user

Maupin, Chase chase.maupin at ti.com
Wed Apr 18 15:28:51 CEST 2012


All,

I recently was trying out a Linksys WRT54G2 V1 router and encountered a strange behaviour where the dhcp server on the router was overriding the serverip setting I had in my u-boot environment.  The behaviour looked like:

1. Boot by board and set my serverip using "setenv serverip 192.168.1.101"
2. printenv shows my serverip as 192.168.1.101
3. Do a dhcp command.  In the output I noticed the server it was trying to transfer my file from was 192.168.1.1 which is the router.
4. cancel the dhcp and do a printenv
5. serverip is not set to 192.168.1.1 in my environment

So the net effect was that the dhcp server in this router was providing information in its response packet that was overriding the setting I had programmed.  I dug into the u-boot code a little and found two ways to work around this.

The first was to set CONFIG_BOOTP_SERVERIP in my board config file.  I discarded this because this would seem to make me always ignore the serverip setting from the DHCP server, and it seemed likely that there were cases where you would want this.

The second was to check if the serverip was already set in the environment and if so then not override that value with the one in the response from the DHCP server.  The below patch snippet does this (NOTE: I based this on my 2011.09 tree but it is close to the master branch in this function except for the use of __maybe_unused).

net/bootp.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/net/bootp.c b/net/bootp.c
index a003c42..186a7ac 100644
--- a/net/bootp.c
+++ b/net/bootp.c
@@ -106,11 +106,19 @@ static int BootpCheckPkt(uchar *pkt, unsigned dest, unsigned src, unsigned len)
 static void BootpCopyNetParams(Bootp_t *bp)
 {
 	IPaddr_t tmp_ip;
+	IPaddr_t tmp_serverip;
 
 	NetCopyIP(&NetOurIP, &bp->bp_yiaddr);
 #if !defined(CONFIG_BOOTP_SERVERIP)
 	NetCopyIP(&tmp_ip, &bp->bp_siaddr);
-	if (tmp_ip != 0)
+
+	/* Check if the serverip variable is set.  If so then do not set
+	   NetServerIP which will result in overwriting the serverip variable
+	   in the environment.
+	 */
+	tmp_serverip = getenv_IPaddr("serverip");
+
+	if (!tmp_serverip && (tmp_ip != 0))
 		NetCopyIP(&NetServerIP, &bp->bp_siaddr);
 	memcpy (NetServerEther, ((Ethernet_t *)NetRxPacket)->et_src, 6);
 #endif
-- 
1.7.0.4

I tested the above patch with the same linksys router and in the case where I had done a setenv serverip xxx.xxx.xxx.xxx it was not overridden by the DHCP server settings.  If I had not set serverip then it was set to the DHCP server setting.

Is this the proper way to fix this issue and the right location to make the change?  The overall goal was to make sure that if I specified a particular serverip that I wanted to use, then the DHCP server should not be changing that.

Sincerely,
Chase Maupin



More information about the U-Boot mailing list