773a3f1
diff -up dhcp-3.1.0/common/nit.c.xen dhcp-3.1.0/common/nit.c
773a3f1
--- dhcp-3.1.0/common/nit.c.xen	2007-05-23 19:30:32.000000000 -0400
773a3f1
+++ dhcp-3.1.0/common/nit.c	2007-10-22 16:12:27.000000000 -0400
773a3f1
@@ -371,7 +371,7 @@ ssize_t receive_packet (interface, buf, 
0713d0c
 
773a3f1
 	/* Decode the IP and UDP headers... */
773a3f1
 	offset = decode_udp_ip_header (interface, ibuf, bufix,
773a3f1
-				       from, length, &paylen);
773a3f1
+				       from, length, &paylen, 0);
27c9546
 
773a3f1
 	/* If the IP or UDP checksum was bad, skip the packet... */
773a3f1
 	if (offset < 0)
773a3f1
diff -up dhcp-3.1.0/common/dlpi.c.xen dhcp-3.1.0/common/dlpi.c
773a3f1
--- dhcp-3.1.0/common/dlpi.c.xen	2007-05-23 19:30:32.000000000 -0400
773a3f1
+++ dhcp-3.1.0/common/dlpi.c	2007-10-22 16:12:11.000000000 -0400
0713d0c
@@ -680,7 +680,7 @@ ssize_t receive_packet (interface, buf, 
27c9546
 	length -= offset;
27c9546
 #endif
27c9546
 	offset = decode_udp_ip_header (interface, dbuf, bufix,
0713d0c
-				       from, length, &paylen);
0713d0c
+				       from, length, &paylen, 0);
27c9546
 
27c9546
 	/* If the IP or UDP checksum was bad, skip the packet... */
27c9546
 	if (offset < 0) {
773a3f1
diff -up dhcp-3.1.0/common/upf.c.xen dhcp-3.1.0/common/upf.c
773a3f1
--- dhcp-3.1.0/common/upf.c.xen	2007-05-23 19:30:32.000000000 -0400
773a3f1
+++ dhcp-3.1.0/common/upf.c	2007-10-22 16:12:52.000000000 -0400
773a3f1
@@ -322,7 +322,7 @@ ssize_t receive_packet (interface, buf, 
773a3f1
 
773a3f1
 	/* Decode the IP and UDP headers... */
773a3f1
 	offset = decode_udp_ip_header (interface, ibuf, bufix,
773a3f1
-				       from, length, &paylen);
773a3f1
+				       from, length, &paylen, 0);
773a3f1
 
773a3f1
 	/* If the IP or UDP checksum was bad, skip the packet... */
773a3f1
 	if (offset < 0)
773a3f1
diff -up dhcp-3.1.0/common/lpf.c.xen dhcp-3.1.0/common/lpf.c
773a3f1
--- dhcp-3.1.0/common/lpf.c.xen	2007-10-22 16:11:12.000000000 -0400
773a3f1
+++ dhcp-3.1.0/common/lpf.c	2007-10-22 16:15:24.000000000 -0400
0713d0c
@@ -34,16 +34,31 @@ static char copyright[] =
27c9546
 #include "dhcpd.h"
27c9546
 #if defined (USE_LPF_SEND) || defined (USE_LPF_RECEIVE)
27c9546
 #include <sys/ioctl.h>
27c9546
+#include <sys/socket.h>
27c9546
 #include <sys/uio.h>
27c9546
 
27c9546
 #include <asm/types.h>
27c9546
 #include <linux/filter.h>
27c9546
 #include <linux/if_ether.h>
27c9546
+#include <linux/if_packet.h>
27c9546
 #include <netinet/in_systm.h>
27c9546
 #include "includes/netinet/ip.h"
27c9546
 #include "includes/netinet/udp.h"
27c9546
 #include "includes/netinet/if_ether.h"
27c9546
 
27c9546
+#ifndef PACKET_AUXDATA
27c9546
+#define PACKET_AUXDATA 8
27c9546
+
27c9546
+struct tpacket_auxdata
27c9546
+{
27c9546
+	__u32		tp_status;
27c9546
+	__u32		tp_len;
27c9546
+	__u32		tp_snaplen;
27c9546
+	__u16		tp_mac;
27c9546
+	__u16		tp_net;
27c9546
+};
27c9546
+#endif
27c9546
+
27c9546
 /* Reinitializes the specified interface after an address change.   This
27c9546
    is not required for packet-filter APIs. */
27c9546
 
773a3f1
@@ -71,10 +86,14 @@ int if_register_lpf (info)
27c9546
 	int sock;
773a3f1
 	char filename[50];
773a3f1
 	int b;
27c9546
-	struct sockaddr sa;
27c9546
+	union {
27c9546
+		struct sockaddr_ll ll;
27c9546
+		struct sockaddr common;
27c9546
+	} sa;
27c9546
+	struct ifreq ifr;
27c9546
 
27c9546
 	/* Make an LPF socket. */
27c9546
-	if ((sock = socket(PF_PACKET, SOCK_PACKET,
27c9546
+	if ((sock = socket(PF_PACKET, SOCK_RAW,
27c9546
 			   htons((short)ETH_P_ALL))) < 0) {
27c9546
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
27c9546
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
773a3f1
@@ -89,11 +108,16 @@ int if_register_lpf (info)
27c9546
 		log_fatal ("Open a socket for LPF: %m");
27c9546
 	}
27c9546
 
27c9546
+	memset (&ifr, 0, sizeof ifr);
27c9546
+	strncpy (ifr.ifr_name, (const char *)info -> ifp, sizeof ifr.ifr_name);
27c9546
+	if (ioctl (sock, SIOCGIFINDEX, &ifr))
27c9546
+		log_fatal ("Failed to get interface index: %m");
27c9546
+
27c9546
 	/* Bind to the interface name */
27c9546
 	memset (&sa, 0, sizeof sa);
27c9546
-	sa.sa_family = AF_PACKET;
27c9546
-	strncpy (sa.sa_data, (const char *)info -> ifp, sizeof sa.sa_data);
27c9546
-	if (bind (sock, &sa, sizeof sa)) {
27c9546
+	sa.ll.sll_family = AF_PACKET;
27c9546
+	sa.ll.sll_ifindex = ifr.ifr_ifindex;
27c9546
+	if (bind (sock, &sa.common, sizeof sa)) {
27c9546
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
27c9546
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
27c9546
 		    errno == EAFNOSUPPORT || errno == EINVAL) {
773a3f1
@@ -173,9 +197,18 @@ static void lpf_gen_filter_setup (struct
27c9546
 void if_register_receive (info)
27c9546
 	struct interface_info *info;
27c9546
 {
27c9546
+	int val;
27c9546
+
27c9546
 	/* Open a LPF device and hang it on this interface... */
27c9546
 	info -> rfdesc = if_register_lpf (info);
27c9546
 
27c9546
+	val = 1;
27c9546
+	if (setsockopt (info -> rfdesc, SOL_PACKET, PACKET_AUXDATA, &val,
27c9546
+			sizeof val) < 0) {
27c9546
+		if (errno != ENOPROTOOPT)
27c9546
+			log_fatal ("Failed to set auxiliary packet data: %m");
27c9546
+	}
27c9546
+
27c9546
 #if defined (HAVE_TR_SUPPORT)
27c9546
 	if (info -> hw_address.hbuf [0] == HTYPE_IEEE802)
27c9546
 		lpf_tr_filter_setup (info);
773a3f1
@@ -294,7 +327,6 @@ ssize_t send_packet (interface, packet, 
27c9546
 	double hh [16];
27c9546
 	double ih [1536 / sizeof (double)];
27c9546
 	unsigned char *buf = (unsigned char *)ih;
27c9546
-	struct sockaddr sa;
27c9546
 	int result;
27c9546
 	int fudge;
27c9546
 
773a3f1
@@ -312,15 +344,7 @@ ssize_t send_packet (interface, packet, 
27c9546
 				(unsigned char *)raw, len);
27c9546
 	memcpy (buf + ibufp, raw, len);
27c9546
 
27c9546
-	/* For some reason, SOCK_PACKET sockets can't be connected,
27c9546
-	   so we have to do a sentdo every time. */
27c9546
-	memset (&sa, 0, sizeof sa);
27c9546
-	sa.sa_family = AF_PACKET;
27c9546
-	strncpy (sa.sa_data,
27c9546
-		 (const char *)interface -> ifp, sizeof sa.sa_data);
27c9546
-
27c9546
-	result = sendto (interface -> wfdesc,
27c9546
-			 buf + fudge, ibufp + len - fudge, 0, &sa, sizeof sa);
27c9546
+	result = write (interface -> wfdesc, buf + fudge, ibufp + len - fudge);
27c9546
 	if (result < 0)
27c9546
 		log_error ("send_packet: %m");
27c9546
 	return result;
773a3f1
@@ -338,14 +362,35 @@ ssize_t receive_packet (interface, buf, 
773a3f1
 	int nread;
27c9546
 	int length = 0;
27c9546
 	int offset = 0;
27c9546
+	int nocsum = 0;
27c9546
 	unsigned char ibuf [1536];
27c9546
 	unsigned bufix = 0;
0713d0c
 	unsigned paylen;
27c9546
+	unsigned char cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
27c9546
+	struct iovec iov = {
27c9546
+		.iov_base = ibuf,
27c9546
+		.iov_len = sizeof ibuf,
27c9546
+	};
27c9546
+	struct msghdr msg = {
27c9546
+		.msg_iov = &iov,
27c9546
+		.msg_iovlen = 1,
27c9546
+		.msg_control = cmsgbuf,
27c9546
+		.msg_controllen = sizeof(cmsgbuf),
27c9546
+	};
27c9546
+	struct cmsghdr *cmsg;
27c9546
 
27c9546
-	length = read (interface -> rfdesc, ibuf, sizeof ibuf);
27c9546
+	length = recvmsg (interface -> rfdesc, &msg, 0);
27c9546
 	if (length <= 0)
27c9546
 		return length;
27c9546
 
27c9546
+	for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
27c9546
+		if (cmsg->cmsg_level == SOL_PACKET &&
27c9546
+		    cmsg->cmsg_type == PACKET_AUXDATA) {
27c9546
+			struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
27c9546
+			nocsum = aux->tp_status & TP_STATUS_CSUMNOTREADY;
27c9546
+		}
27c9546
+	}
27c9546
+
27c9546
 	bufix = 0;
27c9546
 	/* Decode the physical header... */
27c9546
 	offset = decode_hw_header (interface, ibuf, bufix, hfrom);
773a3f1
@@ -362,7 +407,7 @@ ssize_t receive_packet (interface, buf, 
27c9546
 
27c9546
 	/* Decode the IP and UDP headers... */
27c9546
 	offset = decode_udp_ip_header (interface, ibuf, bufix, from,
0713d0c
-				       (unsigned)length, &paylen);
0713d0c
+				       (unsigned)length, &paylen, nocsum);
27c9546
 
27c9546
 	/* If the IP or UDP checksum was bad, skip the packet... */
27c9546
 	if (offset < 0)
773a3f1
diff -up dhcp-3.1.0/common/bpf.c.xen dhcp-3.1.0/common/bpf.c
773a3f1
--- dhcp-3.1.0/common/bpf.c.xen	2007-05-23 19:30:32.000000000 -0400
773a3f1
+++ dhcp-3.1.0/common/bpf.c	2007-10-22 16:13:14.000000000 -0400
773a3f1
@@ -478,7 +478,7 @@ ssize_t receive_packet (interface, buf, 
773a3f1
 		offset = decode_udp_ip_header (interface,
773a3f1
 					       interface -> rbuf,
773a3f1
 					       interface -> rbuf_offset,
773a3f1
-  					       from, hdr.bh_caplen, &paylen);
773a3f1
+  					       from, hdr.bh_caplen, &paylen, 0);
27c9546
 
773a3f1
 		/* If the IP or UDP checksum was bad, skip the packet... */
773a3f1
 		if (offset < 0) {
773a3f1
diff -up dhcp-3.1.0/common/packet.c.xen dhcp-3.1.0/common/packet.c
773a3f1
--- dhcp-3.1.0/common/packet.c.xen	2007-10-22 16:11:12.000000000 -0400
773a3f1
+++ dhcp-3.1.0/common/packet.c	2007-10-22 16:12:41.000000000 -0400
0713d0c
@@ -215,7 +215,7 @@ ssize_t
0713d0c
 decode_udp_ip_header(struct interface_info *interface,
0713d0c
 		     unsigned char *buf, unsigned bufix,
0713d0c
 		     struct sockaddr_in *from, unsigned buflen,
0713d0c
-		     unsigned *rbuflen)
0713d0c
+		     unsigned *rbuflen, int nocsum)
27c9546
 {
27c9546
   unsigned char *data;
27c9546
   struct ip ip;
0713d0c
@@ -326,7 +326,7 @@ decode_udp_ip_header(struct interface_in
0713d0c
 					   8, IPPROTO_UDP + ulen))));
27c9546
 
27c9546
   udp_packets_seen++;
27c9546
-  if (usum && usum != sum) {
27c9546
+  if (!nocsum && usum && usum != sum) {
27c9546
 	  udp_packets_bad_checksum++;
27c9546
 	  if (udp_packets_seen > 4 &&
27c9546
 	      (udp_packets_seen / udp_packets_bad_checksum) < 2) {
773a3f1
diff -up dhcp-3.1.0/includes/dhcpd.h.xen dhcp-3.1.0/includes/dhcpd.h
773a3f1
--- dhcp-3.1.0/includes/dhcpd.h.xen	2007-10-22 16:11:12.000000000 -0400
773a3f1
+++ dhcp-3.1.0/includes/dhcpd.h	2007-10-22 16:13:01.000000000 -0400
773a3f1
@@ -2140,7 +2140,7 @@ ssize_t decode_hw_header PROTO ((struct 
773a3f1
 				 unsigned, struct hardware *));
773a3f1
 ssize_t decode_udp_ip_header PROTO ((struct interface_info *, unsigned char *,
773a3f1
 				     unsigned, struct sockaddr_in *,
773a3f1
-				     unsigned, unsigned *));
773a3f1
+				     unsigned, unsigned *, int));
27c9546
 
773a3f1
 /* ethernet.c */
773a3f1
 void assemble_ethernet_header PROTO ((struct interface_info *, unsigned char *,