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