philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

Blame SOURCES/dhcp-multiple-6-clients.patch

54343e
From 4b8251a0c06b7d8706a28904fdef2414f045cc2c Mon Sep 17 00:00:00 2001
54343e
From: Shawn Routhier <sar@isc.org>
54343e
Date: Mon, 21 Oct 2013 14:59:41 -0700
54343e
Subject: [PATCH] -n [master] Fix the socket handling for DHCPv6 clients to
54343e
 allow multiple instances of a clinet on a single machine to work properly.
54343e
 [ISC-Bugs #34784]
54343e
54343e
---
54343e
 common/discover.c |  19 ++++-----
54343e
 common/socket.c   | 114 +++++++++++++++++++++++++++++++++++++++++++++---------
54343e
 includes/dhcpd.h  |   6 +--
54343e
 4 files changed, 112 insertions(+), 33 deletions(-)
54343e
54343e
diff --git a/common/discover.c b/common/discover.c
54343e
index 1d55317..30da566 100644
54343e
--- a/common/discover.c
54343e
+++ b/common/discover.c
54343e
@@ -58,10 +58,6 @@ struct in_addr limited_broadcast;
54343e
 int local_family = AF_INET;
54343e
 struct in_addr local_address;
54343e
 
54343e
-#ifdef DHCPv6
54343e
-struct in6_addr local_address6;
54343e
-#endif /* DHCPv6 */
54343e
-
54343e
 void (*bootp_packet_handler) (struct interface_info *,
54343e
 			      struct dhcp_packet *, unsigned,
54343e
 			      unsigned int,
54343e
@@ -1242,7 +1238,7 @@ discover_interfaces(int state) {
54343e
 			    (state == DISCOVER_RELAY)) {
54343e
 				if_register6(tmp, 1);
54343e
 			} else {
54343e
-				if_register6(tmp, 0);
54343e
+				if_register_linklocal6(tmp);
54343e
 			}
54343e
 #endif /* DHCPv6 */
54343e
 		}
54343e
@@ -1298,13 +1294,14 @@ discover_interfaces(int state) {
54343e
 				   tmp -> name, isc_result_totext (status));
54343e
 
54343e
 #if defined(DHCPv6)
54343e
-		/* Only register the first interface for V6, since they all
54343e
-		 * use the same socket.  XXX: This has some messy side
54343e
-		 * effects if we start dynamically adding and removing
54343e
-		 * interfaces, but we're well beyond that point in terms of
54343e
-		 * mess.
54343e
+		/* Only register the first interface for V6, since
54343e
+		 * servers and relays all use the same socket.
54343e
+		 * XXX: This has some messy side effects if we start
54343e
+		 * dynamically adding and removing interfaces, but
54343e
+		 * we're well beyond that point in terms of mess.
54343e
 		 */
54343e
-		if (local_family == AF_INET6)
54343e
+		if (((state == DISCOVER_SERVER) || (state == DISCOVER_RELAY)) &&
54343e
+		    (local_family == AF_INET6))
54343e
 			break;
54343e
 #endif
54343e
 	} /* for (tmp = interfaces; ... */
54343e
diff --git a/common/socket.c b/common/socket.c
54343e
index 8a9ebea..2bedd3a 100644
54343e
--- a/common/socket.c
54343e
+++ b/common/socket.c
54343e
@@ -67,6 +67,7 @@
54343e
  * XXX: this is gross.  we need to go back and overhaul the API for socket
54343e
  * handling.
54343e
  */
54343e
+static int no_global_v6_socket = 0;
54343e
 static unsigned int global_v6_socket_references = 0;
54343e
 static int global_v6_socket = -1;
54343e
 
54343e
@@ -127,7 +128,7 @@ void if_reinitialize_receive (info)
54343e
 /* Generic interface registration routine... */
54343e
 int
54343e
 if_register_socket(struct interface_info *info, int family,
54343e
-		   int *do_multicast)
54343e
+		   int *do_multicast, struct in6_addr *linklocal6)
54343e
 {
54343e
 	struct sockaddr_storage name;
54343e
 	int name_len;
54343e
@@ -161,10 +162,12 @@ if_register_socket(struct interface_info *info, int family,
54343e
 		addr6 = (struct sockaddr_in6 *)&nam;; 
54343e
 		addr6->sin6_family = AF_INET6;
54343e
 		addr6->sin6_port = local_port;
54343e
-		/* XXX: What will happen to multicasts if this is nonzero? */
54343e
-		memcpy(&addr6->sin6_addr,
54343e
-		       &local_address6, 
54343e
-		       sizeof(addr6->sin6_addr));
54343e
+		if (linklocal6) {
54343e
+			memcpy(&addr6->sin6_addr,
54343e
+			       linklocal6,
54343e
+			       sizeof(addr6->sin6_addr));
54343e
+			addr6->sin6_scope_id = if_nametoindex(info->name);
54343e
+		}
54343e
 #ifdef HAVE_SA_LEN
54343e
 		addr6->sin6_len = sizeof(*addr6);
54343e
 #endif
54343e
@@ -221,7 +224,7 @@ if_register_socket(struct interface_info *info, int family,
54343e
 	 * daemons can bind to their own sockets and get data for their
54343e
 	 * respective interfaces.  This does not (and should not) affect
54343e
 	 * DHCPv4 sockets; we can't yet support BSD sockets well, much
54343e
-	 * less multiple sockets.
54343e
+	 * less multiple sockets. Make sense only with multicast.
54343e
 	 */
54343e
 	if (local_family == AF_INET6) {
54343e
 		flag = 1;
54343e
@@ -322,7 +325,7 @@ void if_register_send (info)
54343e
 	struct interface_info *info;
54343e
 {
54343e
 #ifndef USE_SOCKET_RECEIVE
54343e
-	info->wfdesc = if_register_socket(info, AF_INET, 0);
54343e
+	info->wfdesc = if_register_socket(info, AF_INET, 0, NULL);
54343e
 	/* If this is a normal IPv4 address, get the hardware address. */
54343e
 	if (strcmp(info->name, "fallback") != 0)
54343e
 		get_hw_addr(info);
54343e
@@ -368,7 +371,7 @@ void if_register_receive (info)
54343e
 
54343e
 #if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
54343e
 	if (global_v4_socket_references == 0) {
54343e
-		global_v4_socket = if_register_socket(info, AF_INET, 0);
54343e
+		global_v4_socket = if_register_socket(info, AF_INET, 0, NULL);
54343e
 		if (global_v4_socket < 0) {
54343e
 			/*
54343e
 			 * if_register_socket() fatally logs if it fails to
54343e
@@ -384,7 +387,7 @@ void if_register_receive (info)
54343e
 #else
54343e
 	/* If we're using the socket API for sending and receiving,
54343e
 	   we don't need to register this interface twice. */
54343e
-	info->rfdesc = if_register_socket(info, AF_INET, 0);
54343e
+	info->rfdesc = if_register_socket(info, AF_INET, 0, NULL);
54343e
 #endif /* IP_PKTINFO... */
54343e
 	/* If this is a normal IPv4 address, get the hardware address. */
54343e
 	if (strcmp(info->name, "fallback") != 0)
54343e
@@ -477,9 +480,13 @@ if_register6(struct interface_info *info, int do_multicast) {
54343e
 	/* Bounce do_multicast to a stack variable because we may change it. */
54343e
 	int req_multi = do_multicast;
54343e
 
54343e
+	if (no_global_v6_socket) {
54343e
+		log_fatal("Impossible condition at %s:%d", MDL);
54343e
+	}
54343e
+
54343e
 	if (global_v6_socket_references == 0) {
54343e
 		global_v6_socket = if_register_socket(info, AF_INET6,
54343e
-						      &req_multi);
54343e
+						      &req_multi, NULL);
54343e
 		if (global_v6_socket < 0) {
54343e
 			/*
54343e
 			 * if_register_socket() fatally logs if it fails to
54343e
@@ -515,12 +522,73 @@ if_register6(struct interface_info *info, int do_multicast) {
54343e
 	}
54343e
 }
54343e
 
54343e
+/*
54343e
+ * Register an IPv6 socket bound to the link-local address of
54343e
+ * the argument interface (used by clients on a multiple interface box,
54343e
+ * vs. a server or a relay using the global IPv6 socket and running
54343e
+ * *only* in a single instance).
54343e
+ */
54343e
+void
54343e
+if_register_linklocal6(struct interface_info *info) {
54343e
+	int sock;
54343e
+	int count;
54343e
+	struct in6_addr *addr6 = NULL;
54343e
+	int req_multi = 0;
54343e
+
54343e
+	if (global_v6_socket >= 0) {
54343e
+		log_fatal("Impossible condition at %s:%d", MDL);
54343e
+	}
54343e
+		
54343e
+	no_global_v6_socket = 1;
54343e
+
54343e
+	/* get the (?) link-local address */
54343e
+	for (count = 0; count < info->v6address_count; count++) {
54343e
+		addr6 = &info->v6addresses[count];
54343e
+		if (IN6_IS_ADDR_LINKLOCAL(addr6))
54343e
+			break;
54343e
+	}
54343e
+
54343e
+	if (!addr6) {
54343e
+		log_fatal("no link-local IPv6 address for %s", info->name);
54343e
+	}
54343e
+
54343e
+	sock = if_register_socket(info, AF_INET6, &req_multi, addr6);
54343e
+
54343e
+	if (sock < 0) {
54343e
+		log_fatal("if_register_socket for %s fails", info->name);
54343e
+	}
54343e
+
54343e
+	info->rfdesc = sock;
54343e
+	info->wfdesc = sock;
54343e
+
54343e
+	get_hw_addr(info);
54343e
+
54343e
+	if (!quiet_interface_discovery) {
54343e
+		if (info->shared_network != NULL) {
54343e
+			log_info("Listening on Socket/%d/%s/%s",
54343e
+				 global_v6_socket, info->name, 
54343e
+				 info->shared_network->name);
54343e
+			log_info("Sending on   Socket/%d/%s/%s",
54343e
+				 global_v6_socket, info->name,
54343e
+				 info->shared_network->name);
54343e
+		} else {
54343e
+			log_info("Listening on Socket/%s", info->name);
54343e
+			log_info("Sending on   Socket/%s", info->name);
54343e
+		}
54343e
+	}
54343e
+}
54343e
+
54343e
 void 
54343e
 if_deregister6(struct interface_info *info) {
54343e
-	/* Dereference the global v6 socket. */
54343e
-	if ((info->rfdesc == global_v6_socket) &&
54343e
-	    (info->wfdesc == global_v6_socket) &&
54343e
-	    (global_v6_socket_references > 0)) {
54343e
+	/* client case */
54343e
+	if (no_global_v6_socket) {
54343e
+		close(info->rfdesc);
54343e
+		info->rfdesc = -1;
54343e
+		info->wfdesc = -1;
54343e
+	} else if ((info->rfdesc == global_v6_socket) &&
54343e
+		   (info->wfdesc == global_v6_socket) &&
54343e
+		   (global_v6_socket_references > 0)) {
54343e
+		/* Dereference the global v6 socket. */
54343e
 		global_v6_socket_references--;
54343e
 		info->rfdesc = -1;
54343e
 		info->wfdesc = -1;
54343e
@@ -540,7 +608,8 @@ if_deregister6(struct interface_info *info) {
54343e
 		}
54343e
 	}
54343e
 
54343e
-	if (global_v6_socket_references == 0) {
54343e
+	if (!no_global_v6_socket &&
54343e
+	    (global_v6_socket_references == 0)) {
54343e
 		close(global_v6_socket);
54343e
 		global_v6_socket = -1;
54343e
 
54343e
@@ -692,9 +761,11 @@ ssize_t send_packet6(struct interface_info *interface,
54343e
 		     struct sockaddr_in6 *to) {
54343e
 	struct msghdr m;
54343e
 	struct iovec v;
54343e
+	struct sockaddr_in6 dst;
54343e
 	int result;
54343e
 	struct in6_pktinfo *pktinfo;
54343e
 	struct cmsghdr *cmsg;
54343e
+	unsigned int ifindex;
54343e
 
54343e
 	/*
54343e
 	 * If necessary allocate space for the control message header.
54343e
@@ -717,9 +788,14 @@ ssize_t send_packet6(struct interface_info *interface,
54343e
 
54343e
 	/*
54343e
 	 * Set the target address we're sending to.
54343e
+	 * Enforce the scope ID for bogus BSDs.
54343e
 	 */
54343e
-	m.msg_name = to;
54343e
-	m.msg_namelen = sizeof(*to);
54343e
+	memcpy(&dst, to, sizeof(dst));
54343e
+	m.msg_name = &dst;
54343e
+	m.msg_namelen = sizeof(dst);
54343e
+	ifindex = if_nametoindex(interface->name);
54343e
+	if (no_global_v6_socket)
54343e
+		dst.sin6_scope_id = ifindex;
54343e
 
54343e
 	/*
54343e
 	 * Set the data buffer we're sending. (Using this wacky 
54343e
@@ -748,7 +824,7 @@ ssize_t send_packet6(struct interface_info *interface,
54343e
 	cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
54343e
 	pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
54343e
 	memset(pktinfo, 0, sizeof(*pktinfo));
54343e
-	pktinfo->ipi6_ifindex = if_nametoindex(interface->name);
54343e
+	pktinfo->ipi6_ifindex = ifindex;
54343e
 	m.msg_controllen = cmsg->cmsg_len;
54343e
 
54343e
 	result = sendmsg(interface->wfdesc, &m, 0);
54343e
@@ -1047,7 +1123,7 @@ void maybe_setup_fallback ()
54343e
 	isc_result_t status;
54343e
 	struct interface_info *fbi = (struct interface_info *)0;
54343e
 	if (setup_fallback (&fbi, MDL)) {
54343e
-		fbi -> wfdesc = if_register_socket (fbi, AF_INET, 0);
54343e
+		fbi -> wfdesc = if_register_socket (fbi, AF_INET, 0, NULL);
54343e
 		fbi -> rfdesc = fbi -> wfdesc;
54343e
 		log_info ("Sending on   Socket/%s%s%s",
54343e
 		      fbi -> name,
54343e
diff --git a/includes/dhcpd.h b/includes/dhcpd.h
54343e
index 73c632f..9e18818 100644
54343e
--- a/includes/dhcpd.h
54343e
+++ b/includes/dhcpd.h
54343e
@@ -2414,7 +2414,7 @@ void get_hw_addr(const char *name, struct hardware *hw);
54343e
 /* socket.c */
54343e
 #if defined (USE_SOCKET_SEND) || defined (USE_SOCKET_RECEIVE) \
54343e
 	|| defined (USE_SOCKET_FALLBACK)
54343e
-int if_register_socket(struct interface_info *, int, int *);
54343e
+int if_register_socket(struct interface_info *, int, int *, struct in6_addr *);
54343e
 #endif
54343e
 
54343e
 #if defined (USE_SOCKET_FALLBACK) && !defined (USE_SOCKET_SEND)
54343e
@@ -2425,7 +2425,7 @@ ssize_t send_fallback (struct interface_info *,
54343e
 		       struct in_addr,
54343e
 		       struct sockaddr_in *, struct hardware *);
54343e
 ssize_t send_fallback6(struct interface_info *, struct packet *,
54343e
-		       struct dhcp_packet *, size_t, struct in6_addr,
54343e
+		       struct dhcp_packet *, size_t, struct in6_addr *,
54343e
 		       struct sockaddr_in6 *, struct hardware *);
54343e
 #endif
54343e
 
54343e
@@ -2461,6 +2461,7 @@ void maybe_setup_fallback (void);
54343e
 #endif
54343e
 
54343e
 void if_register6(struct interface_info *info, int do_multicast);
54343e
+void if_register_linklocal6(struct interface_info *info);
54343e
 ssize_t receive_packet6(struct interface_info *interface,
54343e
 			unsigned char *buf, size_t len,
54343e
 			struct sockaddr_in6 *from, struct in6_addr *to_addr,
54343e
@@ -2606,7 +2607,6 @@ void interface_trace_setup (void);
54343e
 extern struct in_addr limited_broadcast;
54343e
 extern int local_family;
54343e
 extern struct in_addr local_address;
54343e
-extern struct in6_addr local_address6;
54343e
 
54343e
 extern u_int16_t local_port;
54343e
 extern u_int16_t remote_port;
54343e
-- 
54343e
2.1.0
54343e