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

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