Skip to contentSkip to conversation

Support IPv6 in libexport.a

PATCH In review Chuck Lever 2 participants 9 messages Aug 24, 2010 – Aug 31, 2010 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Support IPv6 in libexport.a

Hi Steve-

This patch set should encapsulate IPv6-related changes needed in support/export/client.c . It updates logic that parses presentation addresses and netmasks, and logic that compares socket addresses.

---

Chuck Lever (5):
libexport.a: IPv6 support in client_check()
libexport.a: IPv6 support for client_init_subnet() libexport.a: Prepare init_subnetwork() for IPv6 support libexport.a: Use host helper to parse address in client_init() libexport.a: Prepare to recognize IPv6 addresses in client_gettype()

 support/export/client.c |  208 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 153 insertions(+), 55 deletions(-)
Signature · 1 line
Chuck Lever
#1 lore raw

PATCH 1/5 libexport.a: Prepare to recognize IPv6 addresses in client_gettype()

The current open-coded parsing logic in client_gettype() will be hard to modify to recognize IPv6 addresses. Use a more generic mechanism for detecting IP presentation addresses.

IPv6 will be enabled automatically in client_gettype() when host_pton() is changed to support IPv6 addresses.

---

 support/export/client.c |   23 ++++++++++++++---------
 1 files changed, 14 insertions(+), 9 deletions(-)
support/export/client.c +14 -9
index 21001ce..3e797c9 100644
@@ -602,7 +602,8 @@ client_check(const nfs_client *clp, const struct addrinfo *ai)
 int
 client_gettype(char *ident)
 {
-	char	*sp;
+	struct addrinfo *ai;
+	char *sp;
 
 	if (ident[0] == '\0' || strcmp(ident, "*")==0)
 		return MCL_ANONYMOUS;
@@ -622,12 +623,16 @@ client_gettype(char *ident)
 		if (*sp == '\\' && sp[1])
 			sp++;
 	}
-	/* check for N.N.N.N */
-	sp = ident;
-	if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
-	sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
-	sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '.') return MCL_FQDN;
-	sp++; if(!isdigit(*sp) || strtoul(sp, &sp, 10) > 255 || *sp != '\0') return MCL_FQDN;
-	/* we lie here a bit. but technically N.N.N.N == N.N.N.N/32 :) */
-	return MCL_SUBNETWORK;
+
+	/*
+	 * Treat unadorned IP addresses as MCL_SUBNETWORK.
+	 * Everything else is MCL_FQDN.
+	 */
+	ai = host_pton(ident);
+	if (ai != NULL) {
+		freeaddrinfo(ai);
+		return MCL_SUBNETWORK;
+	}
+
+	return MCL_FQDN;
 }
#2 lore raw

PATCH 2/5 libexport.a: Use host helper to parse address in client_init()

In reply to Chuck Lever

Take the first step towards making it possible to parse either IPv4 or IPv6 addresses in client_init(). It won't handle IPv6 until host_pton() has IPv6 support enabled, and it still doesn't deal with IPv6 netmasks yet.

---

 support/export/client.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)
support/export/client.c +8 -5
index 3e797c9..a89142d 100644
@@ -86,10 +86,8 @@ out_badprefix:
 static int
 init_subnetwork(nfs_client *clp)
 {
-	struct sockaddr_in sin = {
-		.sin_family		= AF_INET,
-	};
 	static char slash32[] = "/32";
+	struct addrinfo *ai;
 	char *cp;
 
 	cp = strchr(clp->m_hostname, '/');
@@ -97,9 +95,14 @@ init_subnetwork(nfs_client *clp)
 		cp = slash32;
 
 	*cp = '\0';
-	sin.sin_addr.s_addr = inet_addr(clp->m_hostname);
-	set_addrlist_in(clp, 0, &sin);
+	ai = host_pton(clp->m_hostname);
 	*cp = '/';
+	if (ai == NULL) {
+		xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
+		return false;
+	}
+	set_addrlist(clp, 0, ai->ai_addr);
+	freeaddrinfo(ai);
 
 	return init_netmask(clp, cp);
 }
#3 lore raw

PATCH 3/5 libexport.a: Prepare init_subnetwork() for IPv6 support

In reply to Chuck Lever

Retire the slash32 logic in inet_netmask() in favor of a more generic netmask parser that can support IPv6 addresses.

If an invalid IP address string is given to inet_addr(3), it returns INADDR_NONE, which is actually a "valid" address (255.255.255.255). We're none the wiser to the substitution until something breaks later.

This patch provides better sanity checking of the parsed address, now that such an error can be reported to client_init()'s callers. We can also check the prefixlen value a little more carefully as well.

---

 support/export/client.c |   81 ++++++++++++++++++++++++++++++++++-------------
 1 files changed, 59 insertions(+), 22 deletions(-)
support/export/client.c +59 -22
index a89142d..780c74d 100644
@@ -17,6 +17,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <netdb.h>
+#include <errno.h>
 
 #include "misc.h"
 #include "nfslib.h"
@@ -58,25 +59,58 @@ client_free(nfs_client *clp)
 }
 
 static int
-init_netmask(nfs_client *clp, const char *slash)
+init_netmask(nfs_client *clp, const char *slash, const sa_family_t family)
 {
 	struct sockaddr_in sin = {
 		.sin_family		= AF_INET,
 	};
+	unsigned long prefixlen;
+	uint32_t shift;
 
-	if (strchr(slash + 1, '.') != NULL)
-		sin.sin_addr.s_addr = inet_addr(slash + 1);
-	else {
-		int prefixlen = atoi(slash + 1);
-		if (0 < prefixlen && prefixlen <= 32)
-			sin.sin_addr.s_addr =
-					htonl((uint32_t)~0 << (32 - prefixlen));
-		else
+	/* No slash present; assume netmask is all ones */
+	if (slash == NULL) {
+		switch (family) {
+		case AF_INET:
+			prefixlen = 32;
+			break;
+		default:
+			goto out_badfamily;
+		}
+	} else {
+		char *endptr;
+
+		/* A spelled out netmask address, perhaps? */
+		if (strchr(slash + 1, '.') != NULL) {
+			if (inet_pton(AF_INET, slash + 1,
+						&sin.sin_addr.s_addr) == 0)
+				goto out_badmask;
+			set_addrlist_in(clp, 1, &sin);
+			return 1;
+		}
+
+		/* A prefixlen was given */
+		prefixlen = strtoul(slash + 1, &endptr, 10);
+		if (*endptr != '\0' && prefixlen != ULONG_MAX && errno != ERANGE)
 			goto out_badprefix;
 	}
 
-	set_addrlist_in(clp, 1, &sin);
-	return 1;
+	switch (family) {
+	case AF_INET:
+		if (prefixlen > 32)
+			goto out_badprefix;
+		shift = 32 - (uint32_t)prefixlen;
+		sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
+		set_addrlist_in(clp, 1, &sin);
+		return 1;
+	}
+
+out_badfamily:
+	xlog(L_ERROR, "Unsupported address family for %s", clp->m_hostname);
+	return 0;
+
+out_badmask:
+	xlog(L_ERROR, "Invalid netmask `%s' for %s", slash + 1, clp->m_hostname);
+	return 0;
 
 out_badprefix:
 	xlog(L_ERROR, "Invalid prefix `%s' for %s", slash + 1, clp->m_hostname);
@@ -86,25 +120,28 @@ out_badprefix:
 static int
 init_subnetwork(nfs_client *clp)
 {
-	static char slash32[] = "/32";
 	struct addrinfo *ai;
-	char *cp;
-
-	cp = strchr(clp->m_hostname, '/');
-	if (cp == NULL)
-		cp = slash32;
-
-	*cp = '\0';
-	ai = host_pton(clp->m_hostname);
-	*cp = '/';
+	sa_family_t family;
+	char *slash;
+
+	slash = strchr(clp->m_hostname, '/');
+	if (slash != NULL) {
+		*slash = '\0';
+		ai = host_pton(clp->m_hostname);
+		*slash = '/';
+	} else
+		ai = host_pton(clp->m_hostname);
 	if (ai == NULL) {
 		xlog(L_ERROR, "Invalid IP address %s", clp->m_hostname);
 		return false;
 	}
+
 	set_addrlist(clp, 0, ai->ai_addr);
+	family = ai->ai_addr->sa_family;
+
 	freeaddrinfo(ai);
 
-	return init_netmask(clp, cp);
+	return init_netmask(clp, slash, family);
 }
 
 static int
#4 lore raw

PATCH 4/5 libexport.a: IPv6 support for client_init_subnet()

In reply to Chuck Lever

To parse and store an IPv6 host or subnet address, init_netmask() needs to handle 128 bit subnet masks.

Unfortunately what once was a pretty simple little function has grown much larger. This logic must now not only parse IPv6 addresses correctly, but must also distinguish between IPv4 and IPv6.

To avoid code duplication, I'm "bending" the cardinal rule of not using "#ifdef" inside functions.

---

 support/export/client.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
support/export/client.c +32 -0
index 780c74d..4c6cd69 100644
@@ -66,6 +66,12 @@ init_netmask(nfs_client *clp, const char *slash, const sa_family_t family)
 	};
 	unsigned long prefixlen;
 	uint32_t shift;
+#ifdef IPV6_SUPPORTED
+	struct sockaddr_in6 sin6 = {
+		.sin6_family		= AF_INET6,
+	};
+	int i;
+#endif
 
 	/* No slash present; assume netmask is all ones */
 	if (slash == NULL) {
@@ -73,6 +79,11 @@ init_netmask(nfs_client *clp, const char *slash, const sa_family_t family)
 		case AF_INET:
 			prefixlen = 32;
 			break;
+#ifdef IPV6_SUPPORTED
+		case AF_INET6:
+			prefixlen = 128;
+			break;
+#endif
 		default:
 			goto out_badfamily;
 		}
@@ -87,6 +98,14 @@ init_netmask(nfs_client *clp, const char *slash, const sa_family_t family)
 			set_addrlist_in(clp, 1, &sin);
 			return 1;
 		}
+#ifdef IPV6_SUPPORTED
+		if (strchr(slash + 1, ':')) {
+			if (!inet_pton(AF_INET6, slash + 1, &sin6.sin6_addr))
+				goto out_badmask;
+			set_addrlist_in6(clp, 1, &sin6);
+			return 1;
+		}
+#endif
 
 		/* A prefixlen was given */
 		prefixlen = strtoul(slash + 1, &endptr, 10);
@@ -102,6 +121,19 @@ init_netmask(nfs_client *clp, const char *slash, const sa_family_t family)
 		sin.sin_addr.s_addr = htonl((uint32_t)~0 << shift);
 		set_addrlist_in(clp, 1, &sin);
 		return 1;
+#ifdef IPV6_SUPPORTED
+	case AF_INET6:
+		if (prefixlen > 128)
+			goto out_badprefix;
+		for (i = 0; prefixlen > 32; i++) {
+			sin6.sin6_addr.s6_addr32[i] = 0xffffffff;
+			prefixlen -= 32;
+		}
+		shift = 32 - (uint32_t)prefixlen;
+		sin6.sin6_addr.s6_addr32[i] = htonl((uint32_t)~0 << shift);
+		set_addrlist_in6(clp, 1, &sin6);
+		return 1;
+#endif
 	}
 
 out_badfamily:
#5 lore raw

PATCH 5/5 libexport.a: IPv6 support in client_check()

In reply to Chuck Lever

Introduce support for IPv6 in client_check()'s helpers. The local addrs_match() twins are no longer needed since we can use nfs_compare_addrs() now.

---

 support/export/client.c |   65 +++++++++++++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 22 deletions(-)
support/export/client.c +43 -22
index 4c6cd69..c74961e 100644
@@ -19,6 +19,7 @@
 #include <netdb.h>
 #include <errno.h>
 
+#include "sockaddr.h"
 #include "misc.h"
 #include "nfslib.h"
 #include "exportfs.h"
@@ -444,27 +445,6 @@ add_name(char *old, const char *add)
 	return new;
 }
 
-static _Bool
-addrs_match4(const struct sockaddr *sa1, const struct sockaddr *sa2)
-{
-	const struct sockaddr_in *si1 = (const struct sockaddr_in *)sa1;
-	const struct sockaddr_in *si2 = (const struct sockaddr_in *)sa2;
-
-	return si1->sin_addr.s_addr == si2->sin_addr.s_addr;
-}
-
-static _Bool
-addrs_match(const struct sockaddr *sa1, const struct sockaddr *sa2)
-{
-	if (sa1->sa_family == sa2->sa_family)
-		switch (sa1->sa_family) {
-		case AF_INET:
-			return addrs_match4(sa1, sa2);
-		}
-
-	return false;
-}
-
 /*
  * Check each address listed in @ai against each address
  * stored in @clp.  Return 1 if a match is found, otherwise
@@ -477,7 +457,8 @@ check_fqdn(const nfs_client *clp, const struct addrinfo *ai)
 
 	for (; ai; ai = ai->ai_next)
 		for (i = 0; i < clp->m_naddr; i++)
-			if (addrs_match(ai->ai_addr, get_addrlist(clp, i)))
+			if (nfs_compare_sockaddr(ai->ai_addr,
+							get_addrlist(clp, i)))
 				return 1;
 
 	return 0;
@@ -507,6 +488,43 @@ check_subnet_v4(const struct sockaddr_in *address,
 	return 0;
 }
 
+#ifdef IPV6_SUPPORTED
+static int
+check_subnet_v6(const struct sockaddr_in6 *address,
+		const struct sockaddr_in6 *mask, const struct addrinfo *ai)
+{
+	for (; ai; ai = ai->ai_next) {
+		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
+
+		if (sin6->sin6_family != AF_INET6)
+			continue;
+
+		if (mask_match(address->sin6_addr.s6_addr32[0],
+				sin6->sin6_addr.s6_addr32[0],
+				mask->sin6_addr.s6_addr32[0]) &&
+		    mask_match(address->sin6_addr.s6_addr32[1],
+				sin6->sin6_addr.s6_addr32[1],
+				mask->sin6_addr.s6_addr32[1]) &&
+		    mask_match(address->sin6_addr.s6_addr32[2],
+				sin6->sin6_addr.s6_addr32[2],
+				mask->sin6_addr.s6_addr32[2]) &&
+		    mask_match(address->sin6_addr.s6_addr32[3],
+				sin6->sin6_addr.s6_addr32[3],
+				mask->sin6_addr.s6_addr32[3]))
+			return 1;
+	}
+	return 0;
+}
+#else	/* !IPV6_SUPPORTED */
+static int
+check_subnet_v6(const struct sockaddr_in6 *UNUSED(address),
+		const struct sockaddr_in6 *UNUSED(mask),
+		const struct addrinfo *UNUSED(ai))
+{
+	return 0;
+}
+#endif	/* !IPV6_SUPPORTED */
+
 /*
  * Check each address listed in @ai against the subnetwork or
  * host address stored in @clp.  Return 1 if an address in @hp
@@ -519,6 +537,9 @@ check_subnetwork(const nfs_client *clp, const struct addrinfo *ai)
 	case AF_INET:
 		return check_subnet_v4(get_addrlist_in(clp, 0),
 				get_addrlist_in(clp, 1), ai);
+	case AF_INET6:
+		return check_subnet_v6(get_addrlist_in6(clp, 0),
+				get_addrlist_in6(clp, 1), ai);
 	}
 
 	return 0;
#6 lore raw
In reply to Chuck Lever
Quoted 16 lines · Chuck Lever

Hi Steve-

This patch set should encapsulate IPv6-related changes needed in support/export/client.c . It updates logic that parses presentation addresses and netmasks, and logic that compares socket addresses.

---

Chuck Lever (5):
libexport.a: IPv6 support in client_check()
libexport.a: IPv6 support for client_init_subnet() libexport.a: Prepare init_subnetwork() for IPv6 support libexport.a: Use host helper to parse address in client_init() libexport.a: Prepare to recognize IPv6 addresses in client_gettype()

Quoted 3 lines
 support/export/client.c |  208 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 153 insertions(+), 55 deletions(-)

All 5 Committed... As far as "bending the cardinal rule of not using "#ifdef" inside functions"... I too did not see a cleaner way of doing it... so I don't think its a big deal.. but maybe down the road we can rework things were those ifdef are not needed... but only time will tell...

steved.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 19 lines · Steve Dickson
Quoted 16 lines · Chuck Lever

Hi Steve-

This patch set should encapsulate IPv6-related changes needed in support/export/client.c . It updates logic that parses presentation addresses and netmasks, and logic that compares socket addresses.

---

Chuck Lever (5):
libexport.a: IPv6 support in client_check()
libexport.a: IPv6 support for client_init_subnet() libexport.a: Prepare init_subnetwork() for IPv6 support libexport.a: Use host helper to parse address in client_init() libexport.a: Prepare to recognize IPv6 addresses in client_gettype()

Quoted 4 lines
Quoted 3 lines

support/export/client.c | 208 +++++++++++++++++++++++++++++++++++------------

1 files changed, 153 insertions(+), 55 deletions(-)

All 5 Committed...

Thanks. Can you push these to your repo on linux-nfs.org?

Quoted 7 lines

As far as "bending the cardinal rule of not
using "#ifdef" inside functions"... I too did not see a cleaner way of doing it... so I don't think its a big deal.. but maybe down the road we can rework things where those ifdef are not needed... but only time will tell...

steved.

Signature · 1 line
chuck[dot]lever[at]oracle[dot]com
#9 lore raw
Quoted 22 lines · Chuck Lever
Quoted 19 lines · Steve Dickson
Quoted 16 lines · Chuck Lever

Hi Steve-

This patch set should encapsulate IPv6-related changes needed in support/export/client.c . It updates logic that parses presentation addresses and netmasks, and logic that compares socket addresses.

---

Chuck Lever (5):
libexport.a: IPv6 support in client_check()
libexport.a: IPv6 support for client_init_subnet() libexport.a: Prepare init_subnetwork() for IPv6 support libexport.a: Use host helper to parse address in client_init() libexport.a: Prepare to recognize IPv6 addresses in client_gettype()

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

support/export/client.c | 208 +++++++++++++++++++++++++++++++++++------------

1 files changed, 153 insertions(+), 55 deletions(-)

All 5 Committed...

Thanks. Can you push these to your repo on linux-nfs.org?

Sorry about that.... I thought I did but it appears I used the --dry-run flag... these commits are there now...

steved.