philipp / rpms / dhcp

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