[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: dhcp problem with several routers
From:       Denys Vlasenko <vda.linux () googlemail ! com>
Date:       2007-12-20 19:47:41
Message-ID: 200712201947.41382.vda.linux () googlemail ! com
[Download RAW message or body]

On Thursday 20 December 2007 16:13, ftrenc wrote:
> Hi
>
> I've been having problems with the dhcp  when connecting to several
> routers.  I saw you aplied this solution to the problem:
>
> http://busybox.net/cgi-bin/viewcvs.cgi/trunk/busybox/networking/udhcp/clien
>tpacket.c?rev=20565&r1=20483&r2=20565
>
>
> but I still having the same problems.
>
> The problem it's that router answers us with 1090 bytes frame. Finally
> we saw that making the dhcp discover to not send the amount of padding
> it sends though the router at the end of frame, there was no problem,
> and also the router don't send us that amount of padding to.
>
>
> Our solution to the problem (Packet.c):

You indeed found a problem. We don't calculate DHCP packet lenght,
and always send full-sized packets.

Can you try attached patch?

In case patch doesn't apply to your tree,
I attached entire networking/udhcp/* directory in an archive too.
--
vda

["udhcp.tar.bz2" (application/x-tbz)]
["6.patch" (text/x-diff)]

diff -d -urpN busybox.5/networking/udhcp/clientpacket.c \
                busybox.6/networking/udhcp/clientpacket.c
--- busybox.5/networking/udhcp/clientpacket.c	2007-12-11 13:11:57.000000000 +0000
+++ busybox.6/networking/udhcp/clientpacket.c	2007-12-20 19:43:29.000000000 +0000
@@ -86,7 +86,7 @@ int send_decline(uint32_t xid, uint32_t 
 
 	bb_info_msg("Sending decline...");
 
-	return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+	return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
 		SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 #endif
@@ -106,7 +106,7 @@ int send_discover(uint32_t xid, uint32_t
 	add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
 	add_requests(&packet);
 	bb_info_msg("Sending discover...");
-	return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+	return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
 			SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 
@@ -126,7 +126,7 @@ int send_selecting(uint32_t xid, uint32_
 	add_requests(&packet);
 	addr.s_addr = requested;
 	bb_info_msg("Sending select for %s...", inet_ntoa(addr));
-	return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+	return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
 				SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 
@@ -143,9 +143,9 @@ int send_renew(uint32_t xid, uint32_t se
 	add_requests(&packet);
 	bb_info_msg("Sending renew...");
 	if (server)
-		return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+		return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, \
SERVER_PORT);  
-	return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
+	return udhcp_send_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
 				SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
 }
 
@@ -163,7 +163,7 @@ int send_release(uint32_t server, uint32
 	add_simple_option(packet.options, DHCP_SERVER_ID, server);
 
 	bb_info_msg("Sending release...");
-	return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
+	return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
 }
 
 
@@ -172,7 +172,6 @@ int get_raw_packet(struct dhcpMessage *p
 {
 	int bytes;
 	struct udp_dhcp_packet packet;
-	uint32_t source, dest;
 	uint16_t check;
 
 	memset(&packet, 0, sizeof(struct udp_dhcp_packet));
@@ -207,36 +206,31 @@ int get_raw_packet(struct dhcpMessage *p
 		return -2;
 	}
 
-	/* check IP checksum */
+	/* verify IP checksum */
 	check = packet.ip.check;
 	packet.ip.check = 0;
-	if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
-		DEBUG("bad IP header checksum, ignoring");
+	if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
+		DEBUG("Bad IP header checksum, ignoring");
 		return -1;
 	}
 
-	/* verify the UDP checksum by replacing the header with a pseudo header */
-	source = packet.ip.saddr;
-	dest = packet.ip.daddr;
+	/* verify UDP checksum. IP header has to be modified for this */
+	memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
+	/* fields which are not memset: protocol, check, saddr, daddr */
+	packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
 	check = packet.udp.check;
 	packet.udp.check = 0;
-	memset(&packet.ip, 0, sizeof(packet.ip));
-
-	packet.ip.protocol = IPPROTO_UDP;
-	packet.ip.saddr = source;
-	packet.ip.daddr = dest;
-	packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
 	if (check && check != udhcp_checksum(&packet, bytes)) {
 		bb_error_msg("packet with bad UDP checksum received, ignoring");
 		return -2;
 	}
 
-	memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
+	memcpy(payload, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
 
 	if (payload->cookie != htonl(DHCP_MAGIC)) {
 		bb_error_msg("received bogus message (bad magic) - ignoring");
 		return -2;
 	}
-	DEBUG("oooooh!!! got some!");
+	DEBUG("Got valid DHCP packet");
 	return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
 }
diff -d -urpN busybox.5/networking/udhcp/common.h busybox.6/networking/udhcp/common.h
--- busybox.5/networking/udhcp/common.h	2007-12-11 13:11:57.000000000 +0000
+++ busybox.6/networking/udhcp/common.h	2007-12-20 18:15:03.000000000 +0000
@@ -57,11 +57,11 @@ struct BUG_bad_sizeof_struct_udp_dhcp_pa
 void udhcp_init_header(struct dhcpMessage *packet, char type);
 int udhcp_get_packet(struct dhcpMessage *packet, int fd);
 uint16_t udhcp_checksum(void *addr, int count);
-int udhcp_raw_packet(struct dhcpMessage *payload,
+int udhcp_send_raw_packet(struct dhcpMessage *payload,
 		uint32_t source_ip, int source_port,
 		uint32_t dest_ip, int dest_port,
 		const uint8_t *dest_arp, int ifindex);
-int udhcp_kernel_packet(struct dhcpMessage *payload,
+int udhcp_send_kernel_packet(struct dhcpMessage *payload,
 		uint32_t source_ip, int source_port,
 		uint32_t dest_ip, int dest_port);
 
diff -d -urpN busybox.5/networking/udhcp/packet.c busybox.6/networking/udhcp/packet.c
--- busybox.5/networking/udhcp/packet.c	2007-12-11 13:11:57.000000000 +0000
+++ busybox.6/networking/udhcp/packet.c	2007-12-20 19:15:12.000000000 +0000
@@ -111,7 +111,7 @@ uint16_t udhcp_checksum(void *addr, int 
 		/* Make sure that the left-over byte is added correctly both
 		 * with little and big endian hosts */
 		uint16_t tmp = 0;
-		*(uint8_t *) (&tmp) = * (uint8_t *) source;
+		*(uint8_t*)&tmp = *(uint8_t*)source;
 		sum += tmp;
 	}
 	/*  Fold 32-bit sum to 16 bits */
@@ -123,7 +123,7 @@ uint16_t udhcp_checksum(void *addr, int 
 
 
 /* Construct a ip/udp header for a packet, and specify the source and dest hardware \
                address */
-int udhcp_raw_packet(struct dhcpMessage *payload,
+int udhcp_send_raw_packet(struct dhcpMessage *payload,
 		uint32_t source_ip, int source_port,
 		uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex)
 {
@@ -131,6 +131,7 @@ int udhcp_raw_packet(struct dhcpMessage 
 	int result;
 	struct sockaddr_ll dest;
 	struct udp_dhcp_packet packet;
+	unsigned size;
 
 	fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
 	if (fd < 0) {
@@ -140,6 +141,8 @@ int udhcp_raw_packet(struct dhcpMessage 
 
 	memset(&dest, 0, sizeof(dest));
 	memset(&packet, 0, sizeof(packet));
+	packet.data = *payload; /* struct copy */
+	size = (char*)&packet.data.options[end_option(packet.data.options) + 1] - \
(char*)&packet;  
 	dest.sll_family = AF_PACKET;
 	dest.sll_protocol = htons(ETH_P_IP);
@@ -157,19 +160,19 @@ int udhcp_raw_packet(struct dhcpMessage 
 	packet.ip.daddr = dest_ip;
 	packet.udp.source = htons(source_port);
 	packet.udp.dest = htons(dest_port);
-	packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat \
on the psuedo-header */ +	/* size, excluding ip header: */
+	packet.udp.len = htons(size - offsetof(struct udp_dhcp_packet, udp));
+	/* for UDP checksumming, ip.len is set to UDP packet len */
 	packet.ip.tot_len = packet.udp.len;
-	memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
-	packet.udp.check = udhcp_checksum(&packet, sizeof(struct udp_dhcp_packet));
-
-	packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
+	packet.udp.check = udhcp_checksum(&packet, size);
+	/* but for sending, it is set to IP packet len */
+	packet.ip.tot_len = htons(size);
 	packet.ip.ihl = sizeof(packet.ip) >> 2;
 	packet.ip.version = IPVERSION;
 	packet.ip.ttl = IPDEFTTL;
-	packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip));
+	packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
 
-	result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0,
-			(struct sockaddr *) &dest, sizeof(dest));
+	result = sendto(fd, &packet, size, 0, (struct sockaddr *) &dest, sizeof(dest));
 	if (result <= 0) {
 		bb_perror_msg("sendto");
 	}
@@ -179,12 +182,13 @@ int udhcp_raw_packet(struct dhcpMessage 
 
 
 /* Let the kernel do all the work for packet generation */
-int udhcp_kernel_packet(struct dhcpMessage *payload,
+int udhcp_send_kernel_packet(struct dhcpMessage *payload,
 		uint32_t source_ip, int source_port,
 		uint32_t dest_ip, int dest_port)
 {
 	int fd, result;
 	struct sockaddr_in client;
+	unsigned size;
 
 	fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
 	if (fd < 0)
@@ -212,7 +216,8 @@ int udhcp_kernel_packet(struct dhcpMessa
 		return -1;
 	}
 
-	result = write(fd, payload, sizeof(struct dhcpMessage));
+	size = (char*)&payload->options[end_option(payload->options) + 1] - \
(char*)&payload; +	result = write(fd, payload, size);
 	close(fd);
 	return result;
 }
diff -d -urpN busybox.5/networking/udhcp/serverpacket.c \
                busybox.6/networking/udhcp/serverpacket.c
--- busybox.5/networking/udhcp/serverpacket.c	2007-12-11 13:11:57.000000000 +0000
+++ busybox.6/networking/udhcp/serverpacket.c	2007-12-20 18:14:40.000000000 +0000
@@ -30,7 +30,7 @@ static int send_packet_to_relay(struct d
 {
 	DEBUG("Forwarding packet to relay");
 
-	return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,
+	return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT,
 			payload->giaddr, SERVER_PORT);
 }
 
@@ -58,7 +58,7 @@ static int send_packet_to_client(struct 
 		ciaddr = payload->yiaddr;
 		chaddr = payload->chaddr;
 	}
-	return udhcp_raw_packet(payload, server_config.server, SERVER_PORT,
+	return udhcp_send_raw_packet(payload, server_config.server, SERVER_PORT,
 			ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);
 }
 



_______________________________________________
busybox mailing list
busybox@busybox.net
http://busybox.net/cgi-bin/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic