bb77af8
diff -up dhcp-4.2.2b1/common/bpf.c.xen dhcp-4.2.2b1/common/bpf.c
bb77af8
--- dhcp-4.2.2b1/common/bpf.c.xen	2009-11-20 02:48:59.000000000 +0100
bb77af8
+++ dhcp-4.2.2b1/common/bpf.c	2011-07-01 14:00:16.936959001 +0200
5a3797e
@@ -485,7 +485,7 @@ ssize_t receive_packet (interface, buf, 
4bf24c8
 		offset = decode_udp_ip_header (interface,
4bf24c8
 					       interface -> rbuf,
4bf24c8
 					       interface -> rbuf_offset,
4bf24c8
-  					       from, hdr.bh_caplen, &paylen);
4bf24c8
+  					       from, hdr.bh_caplen, &paylen, 0);
203b45c
 
4bf24c8
 		/* If the IP or UDP checksum was bad, skip the packet... */
4bf24c8
 		if (offset < 0) {
bb77af8
diff -up dhcp-4.2.2b1/common/dlpi.c.xen dhcp-4.2.2b1/common/dlpi.c
bb77af8
--- dhcp-4.2.2b1/common/dlpi.c.xen	2011-05-11 16:20:59.000000000 +0200
bb77af8
+++ dhcp-4.2.2b1/common/dlpi.c	2011-07-01 14:00:16.937958997 +0200
bb77af8
@@ -693,7 +693,7 @@ ssize_t receive_packet (interface, buf, 
203b45c
 	length -= offset;
203b45c
 #endif
203b45c
 	offset = decode_udp_ip_header (interface, dbuf, bufix,
203b45c
-				       from, length, &paylen);
203b45c
+				       from, length, &paylen, 0);
203b45c
 
9538db2
 	/*
9538db2
 	 * If the IP or UDP checksum was bad, skip the packet...
bb77af8
diff -up dhcp-4.2.2b1/common/lpf.c.xen dhcp-4.2.2b1/common/lpf.c
bb77af8
--- dhcp-4.2.2b1/common/lpf.c.xen	2011-05-10 16:38:58.000000000 +0200
bb77af8
+++ dhcp-4.2.2b1/common/lpf.c	2011-07-01 14:11:24.725748028 +0200
bb77af8
@@ -29,19 +29,33 @@
203b45c
 #include "dhcpd.h"
203b45c
 #if defined (USE_LPF_SEND) || defined (USE_LPF_RECEIVE)
203b45c
 #include <sys/ioctl.h>
203b45c
+#include <sys/socket.h>
203b45c
 #include <sys/uio.h>
82186fd
 #include <errno.h>
203b45c
 
203b45c
 #include <asm/types.h>
203b45c
 #include <linux/filter.h>
203b45c
 #include <linux/if_ether.h>
203b45c
+#include <linux/if_packet.h>
203b45c
 #include <netinet/in_systm.h>
bb77af8
-#include <net/if_packet.h>
203b45c
 #include "includes/netinet/ip.h"
203b45c
 #include "includes/netinet/udp.h"
203b45c
 #include "includes/netinet/if_ether.h"
82186fd
 #include <net/if.h>
203b45c
 
203b45c
+#ifndef PACKET_AUXDATA
203b45c
+#define PACKET_AUXDATA 8
203b45c
+
203b45c
+struct tpacket_auxdata
203b45c
+{
203b45c
+	__u32		tp_status;
203b45c
+	__u32		tp_len;
203b45c
+	__u32		tp_snaplen;
203b45c
+	__u16		tp_mac;
203b45c
+	__u16		tp_net;
203b45c
+};
203b45c
+#endif
203b45c
+
203b45c
 /* Reinitializes the specified interface after an address change.   This
203b45c
    is not required for packet-filter APIs. */
203b45c
 
bb77af8
@@ -67,10 +81,14 @@ int if_register_lpf (info)
82186fd
 	struct interface_info *info;
82186fd
 {
203b45c
 	int sock;
203b45c
-	struct sockaddr sa;
203b45c
+	union {
203b45c
+		struct sockaddr_ll ll;
203b45c
+		struct sockaddr common;
203b45c
+	} sa;
203b45c
+	struct ifreq ifr;
203b45c
 
203b45c
 	/* Make an LPF socket. */
203b45c
-	if ((sock = socket(PF_PACKET, SOCK_PACKET,
203b45c
+	if ((sock = socket(PF_PACKET, SOCK_RAW,
203b45c
 			   htons((short)ETH_P_ALL))) < 0) {
203b45c
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
203b45c
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
bb77af8
@@ -85,11 +103,17 @@ int if_register_lpf (info)
203b45c
 		log_fatal ("Open a socket for LPF: %m");
203b45c
 	}
203b45c
 
203b45c
+	memset (&ifr, 0, sizeof ifr);
203b45c
+	strncpy (ifr.ifr_name, (const char *)info -> ifp, sizeof ifr.ifr_name);
030991f
+	ifr.ifr_name[IFNAMSIZ-1] = '\0';
203b45c
+	if (ioctl (sock, SIOCGIFINDEX, &ifr))
203b45c
+		log_fatal ("Failed to get interface index: %m");
203b45c
+
203b45c
 	/* Bind to the interface name */
203b45c
 	memset (&sa, 0, sizeof sa);
203b45c
-	sa.sa_family = AF_PACKET;
203b45c
-	strncpy (sa.sa_data, (const char *)info -> ifp, sizeof sa.sa_data);
203b45c
-	if (bind (sock, &sa, sizeof sa)) {
203b45c
+	sa.ll.sll_family = AF_PACKET;
203b45c
+	sa.ll.sll_ifindex = ifr.ifr_ifindex;
203b45c
+	if (bind (sock, &sa.common, sizeof sa)) {
203b45c
 		if (errno == ENOPROTOOPT || errno == EPROTONOSUPPORT ||
203b45c
 		    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
203b45c
 		    errno == EAFNOSUPPORT || errno == EINVAL) {
bb77af8
@@ -171,9 +195,18 @@ static void lpf_gen_filter_setup (struct
203b45c
 void if_register_receive (info)
203b45c
 	struct interface_info *info;
203b45c
 {
203b45c
+	int val;
203b45c
+
203b45c
 	/* Open a LPF device and hang it on this interface... */
203b45c
 	info -> rfdesc = if_register_lpf (info);
203b45c
 
203b45c
+	val = 1;
203b45c
+	if (setsockopt (info -> rfdesc, SOL_PACKET, PACKET_AUXDATA, &val,
203b45c
+			sizeof val) < 0) {
203b45c
+		if (errno != ENOPROTOOPT)
203b45c
+			log_fatal ("Failed to set auxiliary packet data: %m");
203b45c
+	}
203b45c
+
203b45c
 #if defined (HAVE_TR_SUPPORT)
203b45c
 	if (info -> hw_address.hbuf [0] == HTYPE_IEEE802)
203b45c
 		lpf_tr_filter_setup (info);
bb77af8
@@ -295,7 +328,6 @@ ssize_t send_packet (interface, packet, 
203b45c
 	double hh [16];
203b45c
 	double ih [1536 / sizeof (double)];
203b45c
 	unsigned char *buf = (unsigned char *)ih;
bb77af8
-	struct sockaddr_pkt sa;
203b45c
 	int result;
203b45c
 	int fudge;
203b45c
 
bb77af8
@@ -316,17 +348,7 @@ ssize_t send_packet (interface, packet, 
203b45c
 				(unsigned char *)raw, len);
203b45c
 	memcpy (buf + ibufp, raw, len);
203b45c
 
203b45c
-	/* For some reason, SOCK_PACKET sockets can't be connected,
203b45c
-	   so we have to do a sentdo every time. */
203b45c
-	memset (&sa, 0, sizeof sa);
bb77af8
-	sa.spkt_family = AF_PACKET;
bb77af8
-	strncpy ((char *)sa.spkt_device,
bb77af8
-		 (const char *)interface -> ifp, sizeof sa.spkt_device);
bb77af8
-	sa.spkt_protocol = htons(ETH_P_IP);
203b45c
-
203b45c
-	result = sendto (interface -> wfdesc,
bb77af8
-			 buf + fudge, ibufp + len - fudge, 0, 
bb77af8
-			 (const struct sockaddr *)&sa, sizeof sa);
203b45c
+	result = write (interface -> wfdesc, buf + fudge, ibufp + len - fudge);
203b45c
 	if (result < 0)
203b45c
 		log_error ("send_packet: %m");
203b45c
 	return result;
bb77af8
@@ -343,14 +365,35 @@ ssize_t receive_packet (interface, buf, 
82186fd
 {
203b45c
 	int length = 0;
203b45c
 	int offset = 0;
203b45c
+	int nocsum = 0;
203b45c
 	unsigned char ibuf [1536];
203b45c
 	unsigned bufix = 0;
203b45c
 	unsigned paylen;
203b45c
+	unsigned char cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
203b45c
+	struct iovec iov = {
203b45c
+		.iov_base = ibuf,
203b45c
+		.iov_len = sizeof ibuf,
203b45c
+	};
203b45c
+	struct msghdr msg = {
203b45c
+		.msg_iov = &iov,
203b45c
+		.msg_iovlen = 1,
203b45c
+		.msg_control = cmsgbuf,
203b45c
+		.msg_controllen = sizeof(cmsgbuf),
203b45c
+	};
203b45c
+	struct cmsghdr *cmsg;
203b45c
 
203b45c
-	length = read (interface -> rfdesc, ibuf, sizeof ibuf);
203b45c
+	length = recvmsg (interface -> rfdesc, &msg, 0);
203b45c
 	if (length <= 0)
203b45c
 		return length;
203b45c
 
203b45c
+	for (cmsg = CMSG_FIRSTHDR(&msg;; cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
203b45c
+		if (cmsg->cmsg_level == SOL_PACKET &&
203b45c
+		    cmsg->cmsg_type == PACKET_AUXDATA) {
203b45c
+			struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
203b45c
+			nocsum = aux->tp_status & TP_STATUS_CSUMNOTREADY;
203b45c
+		}
203b45c
+	}
203b45c
+
203b45c
 	bufix = 0;
203b45c
 	/* Decode the physical header... */
203b45c
 	offset = decode_hw_header (interface, ibuf, bufix, hfrom);
bb77af8
@@ -367,7 +410,7 @@ ssize_t receive_packet (interface, buf, 
203b45c
 
203b45c
 	/* Decode the IP and UDP headers... */
203b45c
 	offset = decode_udp_ip_header (interface, ibuf, bufix, from,
203b45c
-				       (unsigned)length, &paylen);
203b45c
+				       (unsigned)length, &paylen, nocsum);
203b45c
 
203b45c
 	/* If the IP or UDP checksum was bad, skip the packet... */
203b45c
 	if (offset < 0)
bb77af8
diff -up dhcp-4.2.2b1/common/nit.c.xen dhcp-4.2.2b1/common/nit.c
bb77af8
--- dhcp-4.2.2b1/common/nit.c.xen	2009-11-20 02:49:01.000000000 +0100
bb77af8
+++ dhcp-4.2.2b1/common/nit.c	2011-07-01 14:00:16.939958989 +0200
5a3797e
@@ -369,7 +369,7 @@ ssize_t receive_packet (interface, buf, 
203b45c
 
4bf24c8
 	/* Decode the IP and UDP headers... */
4bf24c8
 	offset = decode_udp_ip_header (interface, ibuf, bufix,
4bf24c8
-				       from, length, &paylen);
4bf24c8
+				       from, length, &paylen, 0);
4bf24c8
 
4bf24c8
 	/* If the IP or UDP checksum was bad, skip the packet... */
4bf24c8
 	if (offset < 0)
bb77af8
diff -up dhcp-4.2.2b1/common/packet.c.xen dhcp-4.2.2b1/common/packet.c
bb77af8
--- dhcp-4.2.2b1/common/packet.c.xen	2009-07-23 20:52:20.000000000 +0200
bb77af8
+++ dhcp-4.2.2b1/common/packet.c	2011-07-01 14:00:16.939958989 +0200
9538db2
@@ -211,7 +211,7 @@ ssize_t
203b45c
 decode_udp_ip_header(struct interface_info *interface,
203b45c
 		     unsigned char *buf, unsigned bufix,
203b45c
 		     struct sockaddr_in *from, unsigned buflen,
203b45c
-		     unsigned *rbuflen)
203b45c
+		     unsigned *rbuflen, int nocsum)
203b45c
 {
203b45c
   unsigned char *data;
203b45c
   struct ip ip;
9538db2
@@ -322,7 +322,7 @@ decode_udp_ip_header(struct interface_in
203b45c
 					   8, IPPROTO_UDP + ulen))));
203b45c
 
203b45c
   udp_packets_seen++;
203b45c
-  if (usum && usum != sum) {
203b45c
+  if (!nocsum && usum && usum != sum) {
203b45c
 	  udp_packets_bad_checksum++;
203b45c
 	  if (udp_packets_seen > 4 &&
203b45c
 	      (udp_packets_seen / udp_packets_bad_checksum) < 2) {
bb77af8
diff -up dhcp-4.2.2b1/common/upf.c.xen dhcp-4.2.2b1/common/upf.c
bb77af8
--- dhcp-4.2.2b1/common/upf.c.xen	2009-11-20 02:49:01.000000000 +0100
bb77af8
+++ dhcp-4.2.2b1/common/upf.c	2011-07-01 14:00:16.940958986 +0200
5a3797e
@@ -320,7 +320,7 @@ ssize_t receive_packet (interface, buf, 
4bf24c8
 
4bf24c8
 	/* Decode the IP and UDP headers... */
4bf24c8
 	offset = decode_udp_ip_header (interface, ibuf, bufix,
4bf24c8
-				       from, length, &paylen);
4bf24c8
+				       from, length, &paylen, 0);
4bf24c8
 
4bf24c8
 	/* If the IP or UDP checksum was bad, skip the packet... */
4bf24c8
 	if (offset < 0)
bb77af8
diff -up dhcp-4.2.2b1/includes/dhcpd.h.xen dhcp-4.2.2b1/includes/dhcpd.h
bb77af8
--- dhcp-4.2.2b1/includes/dhcpd.h.xen	2011-07-01 14:00:16.000000000 +0200
bb77af8
+++ dhcp-4.2.2b1/includes/dhcpd.h	2011-07-01 14:12:18.069642470 +0200
bb77af8
@@ -2796,7 +2796,7 @@ ssize_t decode_hw_header (struct interfa
bb77af8
 			  unsigned, struct hardware *);
bb77af8
 ssize_t decode_udp_ip_header (struct interface_info *, unsigned char *,
bb77af8
 			      unsigned, struct sockaddr_in *,
bb77af8
-			      unsigned, unsigned *);
bb77af8
+			      unsigned, unsigned *, int);
203b45c
 
203b45c
 /* ethernet.c */
bb77af8
 void assemble_ethernet_header (struct interface_info *, unsigned char *,