philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

Blame SOURCES/dhcp-4.2.2-xen-checksum.patch

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