[PATCH] udhcpd static route support

Michael McTernan Michael.McTernan.2001 at cs.bris.ac.uk
Thu Dec 15 22:57:19 UTC 2011


Hi,

I found support for the classless static route options in udhcpd were partially implemented, so added the bits needed to make it work.

I've tested this on an ARM9, where it costs 112 extra bytes in exe size (-Os and -mthumb mode).  It parses the config given such as:

  option staticroutes 10.0.0.0/8 10.127.0.1, 10.10.10.0/24 10.128.0.2
  option msstaticroutes 10.0.0.0/8 10.127.0.1

The options have then been verified as sent using Wireshark and by checking the actions of my Linux DHCP client.

Please consider the following patch for inclusion in BusyBox.

Kind Regards,

Mike

Signed-off-by: Michael McTernan <Michael.McTernan.1001 at cs.bris.ac.uk>
---
 networking/udhcp/common.c |   38 +++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff -Naud busybox-1.19.3/networking/udhcp/common.c busybox-1.19.3-static-route/networking/udhcp/common.c
--- busybox-1.19.3/networking/udhcp/common.c	2011-09-06 03:35:17.000000000 +0100
+++ busybox-1.19.3-static-route/networking/udhcp/common.c	2011-12-15 22:36:31.885936640 +0000
@@ -54,12 +54,12 @@
 	{ OPTION_DNS_STRING | OPTION_LIST         , 0x77 }, /* DHCP_DOMAIN_SEARCH */
 	{ OPTION_SIP_SERVERS                      , 0x78 }, /* DHCP_SIP_SERVERS   */
 #endif
-	{ OPTION_STATIC_ROUTES                    , 0x79 }, /* DHCP_STATIC_ROUTES */
+	{ OPTION_STATIC_ROUTES | OPTION_LIST      , 0x79 }, /* DHCP_STATIC_ROUTES */
 #if ENABLE_FEATURE_UDHCP_8021Q
 	{ OPTION_U16                              , 0x84 }, /* DHCP_VLAN_ID       */
 	{ OPTION_U8                               , 0x85 }, /* DHCP_VLAN_PRIORITY */
 #endif
-	{ OPTION_STATIC_ROUTES                    , 0xf9 }, /* DHCP_MS_STATIC_ROUTES */
+	{ OPTION_STATIC_ROUTES | OPTION_LIST      , 0xf9 }, /* DHCP_MS_STATIC_ROUTES */
 	{ OPTION_STRING                           , 0xfc }, /* DHCP_WPAD          */

 	/* Options below have no match in dhcp_option_strings[],
@@ -119,8 +119,6 @@
 // is not handled yet by "string->option" conversion code:
 	"sipsrv" "\0"      /* DHCP_SIP_SERVERS    */
 #endif
-// doesn't work in udhcpd.conf since OPTION_STATIC_ROUTES
-// is not handled yet by "string->option" conversion code:
 	"staticroutes" "\0"/* DHCP_STATIC_ROUTES  */
 #if ENABLE_FEATURE_UDHCP_8021Q
 	"vlanid" "\0"      /* DHCP_VLAN_ID        */
@@ -331,7 +329,7 @@
 	lsa = host_and_af2sockaddr(str, 0, AF_INET);
 	if (!lsa)
 		return 0;
-	*(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
+	memcpy(arg, &lsa->u.sin.sin_addr.s_addr, sizeof(uint32_t)); /* *arg maybe unaligned */
 	free(lsa);
 	return 1;
 }
@@ -434,7 +432,7 @@
 	struct dhcp_optflag bin_optflag;
 	unsigned optcode;
 	int retval, length;
-	char buffer[8] ALIGNED(4);
+	char buffer[9] ALIGNED(4);
 	uint16_t *result_u16 = (uint16_t *) buffer;
 	uint32_t *result_u32 = (uint32_t *) buffer;

@@ -521,9 +519,35 @@
 			retval = (endptr[0] == '\0');
 			break;
 		}
-		case OPTION_BIN: /* handled in attach_option() */
+		case OPTION_BIN: { /* handled in attach_option() */
 			opt = val;
 			retval = 1;
+			break;
+		}
+		case OPTION_STATIC_ROUTES: {
+			char *bits;
+
+			/* expect a.b.c.d/m format */
+			bits = strchr(val, '/');
+			if(!bits) {
+				retval = 0;
+			} else {
+				*bits = '\0';
+				buffer[0] = strtoul(bits + 1, NULL, 0);
+
+				retval = udhcp_str2nip(val, buffer + 1);
+				val = strtok(NULL, ", \t/-");
+				if (!val || (unsigned char)buffer[0] > 32)
+					retval = 0;
+			}
+
+			if (retval) {
+				length = ((buffer[0] + 7) >> 3) + 5;
+				retval = udhcp_str2nip(val, buffer + (length - 4));
+				opt = buffer;
+			}
+			break;
+		}
 		default:
 			break;
 		}


--
Mike


More information about the busybox mailing list