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