philipp / rpms / dhcp

Forked from rpms/dhcp 4 years ago
Clone

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

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