e82a50
diff -up dhcp-4.0.0/common/nit.c.xen dhcp-4.0.0/common/nit.c
e82a50
--- dhcp-4.0.0/common/nit.c.xen	2007-09-05 07:32:10.000000000 -1000
e82a50
+++ dhcp-4.0.0/common/nit.c	2007-12-29 06:39:16.000000000 -1000
e82a50
@@ -366,7 +366,7 @@ ssize_t receive_packet (interface, buf, 
8b41d8
 
8b41d8
 	/* Decode the IP and UDP headers... */
8b41d8
 	offset = decode_udp_ip_header (interface, ibuf, bufix,
8b41d8
-				       from, length, &paylen);
8b41d8
+				       from, length, &paylen, 0);
8b41d8
 
8b41d8
 	/* If the IP or UDP checksum was bad, skip the packet... */
8b41d8
 	if (offset < 0)
e82a50
diff -up dhcp-4.0.0/common/dlpi.c.xen dhcp-4.0.0/common/dlpi.c
e82a50
--- dhcp-4.0.0/common/dlpi.c.xen	2007-10-08 04:27:53.000000000 -1000
e82a50
+++ dhcp-4.0.0/common/dlpi.c	2007-12-29 06:39:13.000000000 -1000
e82a50
@@ -689,7 +689,7 @@ ssize_t receive_packet (interface, buf, 
8b41d8
 	length -= offset;
8b41d8
 #endif
8b41d8
 	offset = decode_udp_ip_header (interface, dbuf, bufix,
8b41d8
-				       from, length, &paylen);
8b41d8
+				       from, length, &paylen, 0);
8b41d8
 
8b41d8
 	/* If the IP or UDP checksum was bad, skip the packet... */
8b41d8
 	if (offset < 0) {
e82a50
diff -up dhcp-4.0.0/common/upf.c.xen dhcp-4.0.0/common/upf.c
e82a50
--- dhcp-4.0.0/common/upf.c.xen	2007-07-12 20:43:42.000000000 -1000
e82a50
+++ dhcp-4.0.0/common/upf.c	2007-12-29 06:39:24.000000000 -1000
e82a50
@@ -317,7 +317,7 @@ ssize_t receive_packet (interface, buf, 
8b41d8
 
8b41d8
 	/* Decode the IP and UDP headers... */
8b41d8
 	offset = decode_udp_ip_header (interface, ibuf, bufix,
8b41d8
-				       from, length, &paylen);
8b41d8
+				       from, length, &paylen, 0);
8b41d8
 
8b41d8
 	/* If the IP or UDP checksum was bad, skip the packet... */
8b41d8
 	if (offset < 0)
e82a50
diff -up dhcp-4.0.0/common/lpf.c.xen dhcp-4.0.0/common/lpf.c
e82a50
--- dhcp-4.0.0/common/lpf.c.xen	2007-12-29 06:37:53.000000000 -1000
e82a50
+++ dhcp-4.0.0/common/lpf.c	2007-12-29 06:43:08.000000000 -1000
e82a50
@@ -29,18 +29,33 @@
8b41d8
 #include "dhcpd.h"
8b41d8
 #if defined (USE_LPF_SEND) || defined (USE_LPF_RECEIVE)
8b41d8
 #include <sys/ioctl.h>
8b41d8
+#include <sys/socket.h>
8b41d8
 #include <sys/uio.h>
e82a50
 #include <errno.h>
8b41d8
 
8b41d8
 #include <asm/types.h>
8b41d8
 #include <linux/filter.h>
8b41d8
 #include <linux/if_ether.h>
8b41d8
+#include <linux/if_packet.h>
8b41d8
 #include <netinet/in_systm.h>
8b41d8
 #include "includes/netinet/ip.h"
8b41d8
 #include "includes/netinet/udp.h"
8b41d8
 #include "includes/netinet/if_ether.h"
e82a50
 #include <net/if.h>
8b41d8
 
8b41d8
+#ifndef PACKET_AUXDATA
8b41d8
+#define PACKET_AUXDATA 8
8b41d8
+
8b41d8
+struct tpacket_auxdata
8b41d8
+{
8b41d8
+	__u32		tp_status;
8b41d8
+	__u32		tp_len;
8b41d8
+	__u32		tp_snaplen;
8b41d8
+	__u16		tp_mac;
8b41d8
+	__u16		tp_net;
8b41d8
+};
8b41d8
+#endif
8b41d8
+
8b41d8
 /* Reinitializes the specified interface after an address change.   This
8b41d8
    is not required for packet-filter APIs. */
8b41d8
 
e82a50
@@ -66,10 +81,14 @@ int if_register_lpf (info)
e82a50
 	struct interface_info *info;
e82a50
 {
8b41d8
 	int sock;
8b41d8
-	struct sockaddr sa;
8b41d8
+	union {
8b41d8
+		struct sockaddr_ll ll;
8b41d8
+		struct sockaddr common;
8b41d8
+	} sa;
8b41d8
+	struct ifreq ifr;
8b41d8
 
8b41d8
 	/* Make an LPF socket. */
8b41d8
-	if ((sock = socket(PF_PACKET, SOCK_PACKET,
8b41d8
+	if ((sock = socket(PF_PACKET, SOCK_RAW,
8b41d8
 			   htons((short)ETH_P_ALL))) < 0) {
8b41d8
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
8b41d8
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
e82a50
@@ -84,11 +103,16 @@ int if_register_lpf (info)
8b41d8
 		log_fatal ("Open a socket for LPF: %m");
8b41d8
 	}
8b41d8
 
8b41d8
+	memset (&ifr, 0, sizeof ifr);
8b41d8
+	strncpy (ifr.ifr_name, (const char *)info -> ifp, sizeof ifr.ifr_name);
8b41d8
+	if (ioctl (sock, SIOCGIFINDEX, &ifr))
8b41d8
+		log_fatal ("Failed to get interface index: %m");
8b41d8
+
8b41d8
 	/* Bind to the interface name */
8b41d8
 	memset (&sa, 0, sizeof sa);
8b41d8
-	sa.sa_family = AF_PACKET;
8b41d8
-	strncpy (sa.sa_data, (const char *)info -> ifp, sizeof sa.sa_data);
8b41d8
-	if (bind (sock, &sa, sizeof sa)) {
8b41d8
+	sa.ll.sll_family = AF_PACKET;
8b41d8
+	sa.ll.sll_ifindex = ifr.ifr_ifindex;
8b41d8
+	if (bind (sock, &sa.common, sizeof sa)) {
8b41d8
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
8b41d8
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
8b41d8
 		    errno == EAFNOSUPPORT || errno == EINVAL) {
e82a50
@@ -170,9 +194,18 @@ static void lpf_gen_filter_setup (struct
8b41d8
 void if_register_receive (info)
8b41d8
 	struct interface_info *info;
8b41d8
 {
8b41d8
+	int val;
8b41d8
+
8b41d8
 	/* Open a LPF device and hang it on this interface... */
8b41d8
 	info -> rfdesc = if_register_lpf (info);
8b41d8
 
8b41d8
+	val = 1;
8b41d8
+	if (setsockopt (info -> rfdesc, SOL_PACKET, PACKET_AUXDATA, &val,
8b41d8
+			sizeof val) < 0) {
8b41d8
+		if (errno != ENOPROTOOPT)
8b41d8
+			log_fatal ("Failed to set auxiliary packet data: %m");
8b41d8
+	}
8b41d8
+
8b41d8
 #if defined (HAVE_TR_SUPPORT)
8b41d8
 	if (info -> hw_address.hbuf [0] == HTYPE_IEEE802)
8b41d8
 		lpf_tr_filter_setup (info);
e82a50
@@ -291,7 +324,6 @@ ssize_t send_packet (interface, packet, 
8b41d8
 	double hh [16];
8b41d8
 	double ih [1536 / sizeof (double)];
8b41d8
 	unsigned char *buf = (unsigned char *)ih;
8b41d8
-	struct sockaddr sa;
8b41d8
 	int result;
8b41d8
 	int fudge;
8b41d8
 
e82a50
@@ -309,15 +341,7 @@ ssize_t send_packet (interface, packet, 
8b41d8
 				(unsigned char *)raw, len);
8b41d8
 	memcpy (buf + ibufp, raw, len);
8b41d8
 
8b41d8
-	/* For some reason, SOCK_PACKET sockets can't be connected,
8b41d8
-	   so we have to do a sentdo every time. */
8b41d8
-	memset (&sa, 0, sizeof sa);
8b41d8
-	sa.sa_family = AF_PACKET;
8b41d8
-	strncpy (sa.sa_data,
8b41d8
-		 (const char *)interface -> ifp, sizeof sa.sa_data);
8b41d8
-
8b41d8
-	result = sendto (interface -> wfdesc,
8b41d8
-			 buf + fudge, ibufp + len - fudge, 0, &sa, sizeof sa);
8b41d8
+	result = write (interface -> wfdesc, buf + fudge, ibufp + len - fudge);
8b41d8
 	if (result < 0)
8b41d8
 		log_error ("send_packet: %m");
8b41d8
 	return result;
e82a50
@@ -334,14 +358,35 @@ ssize_t receive_packet (interface, buf, 
e82a50
 {
8b41d8
 	int length = 0;
8b41d8
 	int offset = 0;
8b41d8
+	int nocsum = 0;
8b41d8
 	unsigned char ibuf [1536];
8b41d8
 	unsigned bufix = 0;
8b41d8
 	unsigned paylen;
8b41d8
+	unsigned char cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
8b41d8
+	struct iovec iov = {
8b41d8
+		.iov_base = ibuf,
8b41d8
+		.iov_len = sizeof ibuf,
8b41d8
+	};
8b41d8
+	struct msghdr msg = {
8b41d8
+		.msg_iov = &iov,
8b41d8
+		.msg_iovlen = 1,
8b41d8
+		.msg_control = cmsgbuf,
8b41d8
+		.msg_controllen = sizeof(cmsgbuf),
8b41d8
+	};
8b41d8
+	struct cmsghdr *cmsg;
8b41d8
 
8b41d8
-	length = read (interface -> rfdesc, ibuf, sizeof ibuf);
8b41d8
+	length = recvmsg (interface -> rfdesc, &msg, 0);
8b41d8
 	if (length <= 0)
8b41d8
 		return length;
8b41d8
 
8b41d8
+	for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
8b41d8
+		if (cmsg->cmsg_level == SOL_PACKET &&
8b41d8
+		    cmsg->cmsg_type == PACKET_AUXDATA) {
8b41d8
+			struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
8b41d8
+			nocsum = aux->tp_status & TP_STATUS_CSUMNOTREADY;
8b41d8
+		}
8b41d8
+	}
8b41d8
+
8b41d8
 	bufix = 0;
8b41d8
 	/* Decode the physical header... */
8b41d8
 	offset = decode_hw_header (interface, ibuf, bufix, hfrom);
e82a50
@@ -358,7 +403,7 @@ ssize_t receive_packet (interface, buf, 
8b41d8
 
8b41d8
 	/* Decode the IP and UDP headers... */
8b41d8
 	offset = decode_udp_ip_header (interface, ibuf, bufix, from,
8b41d8
-				       (unsigned)length, &paylen);
8b41d8
+				       (unsigned)length, &paylen, nocsum);
8b41d8
 
8b41d8
 	/* If the IP or UDP checksum was bad, skip the packet... */
8b41d8
 	if (offset < 0)
e82a50
diff -up dhcp-4.0.0/common/bpf.c.xen dhcp-4.0.0/common/bpf.c
e82a50
--- dhcp-4.0.0/common/bpf.c.xen	2007-08-22 23:49:51.000000000 -1000
e82a50
+++ dhcp-4.0.0/common/bpf.c	2007-12-29 06:39:09.000000000 -1000
e82a50
@@ -482,7 +482,7 @@ ssize_t receive_packet (interface, buf, 
8b41d8
 		offset = decode_udp_ip_header (interface,
8b41d8
 					       interface -> rbuf,
8b41d8
 					       interface -> rbuf_offset,
8b41d8
-  					       from, hdr.bh_caplen, &paylen);
8b41d8
+  					       from, hdr.bh_caplen, &paylen, 0);
8b41d8
 
8b41d8
 		/* If the IP or UDP checksum was bad, skip the packet... */
8b41d8
 		if (offset < 0) {
e82a50
diff -up dhcp-4.0.0/common/packet.c.xen dhcp-4.0.0/common/packet.c
e82a50
--- dhcp-4.0.0/common/packet.c.xen	2007-12-29 06:37:53.000000000 -1000
e82a50
+++ dhcp-4.0.0/common/packet.c	2007-12-29 06:39:20.000000000 -1000
e82a50
@@ -210,7 +210,7 @@ ssize_t
8b41d8
 decode_udp_ip_header(struct interface_info *interface,
8b41d8
 		     unsigned char *buf, unsigned bufix,
8b41d8
 		     struct sockaddr_in *from, unsigned buflen,
8b41d8
-		     unsigned *rbuflen)
8b41d8
+		     unsigned *rbuflen, int nocsum)
8b41d8
 {
8b41d8
   unsigned char *data;
8b41d8
   struct ip ip;
e82a50
@@ -321,7 +321,7 @@ decode_udp_ip_header(struct interface_in
8b41d8
 					   8, IPPROTO_UDP + ulen))));
8b41d8
 
8b41d8
   udp_packets_seen++;
8b41d8
-  if (usum && usum != sum) {
8b41d8
+  if (!nocsum && usum && usum != sum) {
8b41d8
 	  udp_packets_bad_checksum++;
8b41d8
 	  if (udp_packets_seen > 4 &&
8b41d8
 	      (udp_packets_seen / udp_packets_bad_checksum) < 2) {
e82a50
diff -up dhcp-4.0.0/includes/dhcpd.h.xen dhcp-4.0.0/includes/dhcpd.h
e82a50
--- dhcp-4.0.0/includes/dhcpd.h.xen	2007-12-29 06:37:53.000000000 -1000
e82a50
+++ dhcp-4.0.0/includes/dhcpd.h	2007-12-29 06:39:27.000000000 -1000
e82a50
@@ -2561,7 +2561,7 @@ ssize_t decode_hw_header PROTO ((struct 
8b41d8
 				 unsigned, struct hardware *));
8b41d8
 ssize_t decode_udp_ip_header PROTO ((struct interface_info *, unsigned char *,
8b41d8
 				     unsigned, struct sockaddr_in *,
8b41d8
-				     unsigned, unsigned *));
8b41d8
+				     unsigned, unsigned *, int));
8b41d8
 
8b41d8
 /* ethernet.c */
8b41d8
 void assemble_ethernet_header PROTO ((struct interface_info *, unsigned char *,