[PATCH] udhcpc: gracefully handle packets with CHECKSUM_PARTIAL

Alexey I. Froloff raorn at raorn.name
Fri Sep 7 08:50:08 UTC 2012


When running as KVM or Xen guest, packets may be received with
incomplete checksum[1].  Patch adapted from Fedora dhcp package.

[1]. http://article.gmane.org/gmane.linux.kernel/1003853

Signed-off-by: Alexey I. Froloff <raorn at raorn.name>
---
 networking/udhcp/dhcpc.c |   39 ++++++++++++++++++++++++++++++++++++---
 1 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index bc1db70..0a2d37f 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -26,8 +26,8 @@
 #include "dhcpc.h"
 
 #include <netinet/if_ether.h>
-#include <netpacket/packet.h>
 #include <linux/filter.h>
+#include <linux/if_packet.h>
 
 /* "struct client_config_t client_config" is in bb_common_bufsiz1 */
 
@@ -834,10 +834,26 @@ static int send_release(uint32_t server, uint32_t ciaddr)
 static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
 {
 	int bytes;
+	int nocsum;
 	struct ip_udp_dhcp_packet packet;
 	uint16_t check;
+	unsigned char cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
+	struct iovec iov;
+	struct msghdr msg;
+	struct cmsghdr *cmsg;
+
+	iov.iov_base = &packet;
+	iov.iov_len = sizeof(packet);
+
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = cmsgbuf;
+	msg.msg_controllen = sizeof(cmsgbuf);
+
+	do {
+		bytes = recvmsg(fd, &msg, 0);
+	} while (bytes < 0 && errno == EINTR);
 
-	bytes = safe_read(fd, &packet, sizeof(packet));
 	if (bytes < 0) {
 		log1("Packet read error, ignoring");
 		/* NB: possible down interface, etc. Caller should pause. */
@@ -855,6 +871,15 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
 		return -2;
 	}
 
+	nocsum = 0;
+	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+		if (cmsg->cmsg_level == SOL_PACKET &&
+			cmsg->cmsg_type == PACKET_AUXDATA) {
+			struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
+			nocsum = aux->tp_status & TP_STATUS_CSUMNOTREADY;
+		}
+	}
+
 	/* ignore any extra garbage bytes */
 	bytes = ntohs(packet.ip.tot_len);
 
@@ -884,7 +909,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
 	packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
 	check = packet.udp.check;
 	packet.udp.check = 0;
-	if (check && check != inet_cksum((uint16_t *)&packet, bytes)) {
+	if (!nocsum && check && check != inet_cksum((uint16_t *)&packet, bytes)) {
 		log1("Packet with bad UDP checksum received, ignoring");
 		return -2;
 	}
@@ -932,6 +957,7 @@ static int udhcp_raw_socket(int ifindex)
 {
 	int fd;
 	struct sockaddr_ll sock;
+	int val;
 
 	/*
 	 * Comment:
@@ -998,6 +1024,13 @@ static int udhcp_raw_socket(int ifindex)
 			log1("Attached filter to raw socket fd %d", fd); // log?
 	}
 
+	val = 1;
+	if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, &val,
+			sizeof(val)) < 0) {
+		if (errno != ENOPROTOOPT)
+			log1("Failed to set auxiliary packet data for socket fd %d, ignoring", fd);
+	}
+
 	log1("Created raw socket");
 
 	return fd;
-- 
1.7.3.4



More information about the busybox mailing list