Subject: bin/30963: sockstat(1) doesn't honor `USE_INET6=no', plus typo; patches provided
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <j+nbsd@2005.salmi.ch>
List: netbsd-bugs
Date: 08/10/2005 14:25:00
>Number:         30963
>Category:       bin
>Synopsis:       sockstat(1) doesn't honor `USE_INET6=no', plus typo; patches provided
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Aug 10 14:25:00 +0000 2005
>Originator:     Jukka Salmi
>Release:        NetBSD 3.99.7
>Environment:
System: NetBSD moray.salmi.ch 3.99.7 NetBSD 3.99.7 (GENERIC) #0: Tue Aug 9 18:52:17 CEST 2005 build@moray.salmi.ch:/build/nbsd/i386/sys/arch/i386/compile/GENERIC i386
Architecture: i386
Machine: i386
>Description:
sockstat(1) doesn't honor a possible `USE_INET6=no' setting in mk.conf
- inet6 code is always built.
Additionally, there's a typo in islistening(): sin6_port is dereferenced
in the PF_INET case, and sin_port is dereferenced in the PF_INET6 case
(should be the other way round, shouldn't it?).
>How-To-Repeat:
Run sockstat(1) (without arguments or with `-6') on a system running
a kernel without `options INET6':

$ sockstat
sockstat: sysctlnametomib: net.inet6.tcp6.pcblist: No such file or directory

>Fix:
A patch fixing both problems is attached and available from
http://salmi.ch/~jukka/patches/nbsd/HEAD/usr.bin/sockstat/no-inet6.patch

A patch applying cleanly to netbsd-3 sources is available from
http://salmi.ch/~jukka/patches/nbsd/netbsd-3/usr.bin/sockstat/no-inet6.patch

Patch against
	src/usr.bin/sockstat/Makefile: 1.1
	src/usr.bin/sockstat/sockstat.c: 1.7

Index: usr.bin/sockstat/Makefile
===================================================================
RCS file: /cvsroot/src/usr.bin/sockstat/Makefile,v
retrieving revision 1.1
diff -u -r1.1 Makefile
--- usr.bin/sockstat/Makefile	9 Mar 2005 05:20:05 -0000	1.1
+++ usr.bin/sockstat/Makefile	6 Aug 2005 13:57:29 -0000
@@ -1,5 +1,11 @@
 #	$NetBSD: Makefile,v 1.1 2005/03/09 05:20:05 atatat Exp $
 
+.include <bsd.own.mk>
+
 PROG=		sockstat
 
+.if (${USE_INET6} != "no")
+CPPFLAGS+=-DINET6
+.endif
+
 .include <bsd.prog.mk>
Index: usr.bin/sockstat/sockstat.c
===================================================================
RCS file: /cvsroot/src/usr.bin/sockstat/sockstat.c,v
retrieving revision 1.7
diff -u -r1.7 sockstat.c
--- usr.bin/sockstat/sockstat.c	2 Jun 2005 03:00:19 -0000	1.7
+++ usr.bin/sockstat/sockstat.c	6 Aug 2005 13:57:30 -0000
@@ -71,7 +71,9 @@
 
 #define satosun(sa)	((struct sockaddr_un *)(sa))
 #define satosin(sa)	((struct sockaddr_in *)(sa))
+#ifdef INET6
 #define satosin6(sa)	((struct sockaddr_in6 *)(sa))
+#endif
 
 void	parse_ports(const char *);
 int	get_num(const char *, const char **, const char **);
@@ -103,7 +105,9 @@
 bitstr_t *portmap;
 
 #define PF_LIST_INET	1
+#ifdef INET6
 #define PF_LIST_INET6	2
+#endif
 #define PF_LIST_LOCAL	4
 #define ONLY_CONNECTED	1
 #define ONLY_LISTEN	2
@@ -117,22 +121,30 @@
 
 	pf_list = only = 0;
 
+#ifdef INET6
 	while ((ch = getopt(argc, argv, "46cf:lnp:u")) != - 1) {
+#else
+	while ((ch = getopt(argc, argv, "4cf:lnp:u")) != - 1) {
+#endif
 		switch (ch) {
 		case '4':
 			pf_list |= PF_LIST_INET;
 			break;
+#ifdef INET6
 		case '6':
 			pf_list |= PF_LIST_INET6;
 			break;
+#endif
 		case 'c':
 			only |= ONLY_CONNECTED;
 			break;
 		case 'f':
 			if (strcasecmp(optarg, "inet") == 0)
 				pf_list |= PF_LIST_INET;
+#ifdef INET6
 			else if (strcasecmp(optarg, "inet6") == 0)
 				pf_list |= PF_LIST_INET6;
+#endif
 			else if (strcasecmp(optarg, "local") == 0)
 				pf_list |= PF_LIST_LOCAL;
 			else if (strcasecmp(optarg, "unix") == 0)
@@ -160,10 +172,18 @@
 	argc -= optind;
 	argv += optind;
 
-	if ((portmap != NULL) && (pf_list == 0))
-		pf_list = PF_LIST_INET | PF_LIST_INET6;
-	if (pf_list == 0)
-		pf_list = PF_LIST_INET | PF_LIST_INET6 | PF_LIST_LOCAL;
+	if ((portmap != NULL) && (pf_list == 0)) {
+		pf_list = PF_LIST_INET;
+#ifdef INET6
+		pf_list |= PF_LIST_INET6;
+#endif
+	}
+	if (pf_list == 0) {
+		pf_list = PF_LIST_INET | PF_LIST_LOCAL;
+#ifdef INET6
+		pf_list |= PF_LIST_INET6;
+#endif
+	}
 	if ((portmap != NULL) && (pf_list & PF_LIST_LOCAL))
 		errx(1, "local domain sockets do not have ports");
 
@@ -174,12 +194,14 @@
 			get_sockets("net.inet.raw.pcblist");
 	}
 
+#ifdef INET6
 	if (pf_list & PF_LIST_INET6) {
 		get_sockets("net.inet6.tcp6.pcblist");
 		get_sockets("net.inet6.udp6.pcblist");
 		if (portmap == NULL)
 			get_sockets("net.inet6.raw6.pcblist");
 	}
+#endif
 
 	if (pf_list & PF_LIST_LOCAL) {
 		get_sockets("net.local.stream.pcblist");
@@ -397,15 +419,17 @@
 	case PF_INET:
 		if (kp->ki_type == SOCK_RAW ||
 		    (kp->ki_type == SOCK_DGRAM &&
-		     ntohs(satosin6(&kp->ki_src)->sin6_port) != 0))
+		     ntohs(satosin(&kp->ki_src)->sin_port) != 0))
 			return (1);
 		break;
+#ifdef INET6
 	case PF_INET6:
 		if (kp->ki_type == SOCK_RAW ||
 		    (kp->ki_type == SOCK_DGRAM &&
-		     ntohs(satosin(&kp->ki_src)->sin_port) != 0))
+		     ntohs(satosin6(&kp->ki_src)->sin6_port) != 0))
 			return (1);
 		break;
+#endif
 	case PF_LOCAL:
 		if (satosun(&kp->ki_src)->sun_path[0] != '\0')
 			return (1);
@@ -471,6 +495,7 @@
 				      ntohs(satosin(&kp->ki_dst)->sin_port)))
 				return (NULL);
 			break;
+#ifdef INET6
 		case AF_INET6:
 			if (!bit_test(portmap,
 				      ntohs(satosin6(&kp->ki_src)->sin6_port)) &&
@@ -478,6 +503,7 @@
 				      ntohs(satosin6(&kp->ki_dst)->sin6_port)))
 				return (NULL);
 			break;
+#endif
 		default:
 			return (NULL);
 		}
@@ -544,6 +570,7 @@
 		default:		t = proto;	break;
 		}
 		break;
+#ifdef INET6
 	case PF_INET6:
 		switch (kp->ki_protocol) {
 		case IPPROTO_TCP:	t = "tcp6";	break;
@@ -552,6 +579,7 @@
 		default:		t = proto;	break;
 		}
 		break;
+#endif
 	case PF_LOCAL:
 		switch (kp->ki_type) {
 		case SOCK_STREAM:	t = "stream";	break;
@@ -588,7 +616,11 @@
 
 	if (isconnected(kp))
 		print_addr(0, kp->ki_type, kp->ki_pflags, &kp->ki_dst);
-	else if (kp->ki_family == PF_INET || kp->ki_family == PF_INET6)
+	else if (kp->ki_family == PF_INET
+#ifdef INET6
+	    || kp->ki_family == PF_INET6
+#endif
+	    )
 		printf("%-*s", 0, "*.*");
 	/* else if (kp->ki_src.sa_len == 2)
 	   printf("%-*s", 0, "-"); */
@@ -632,6 +664,7 @@
 			r = printf("*.*");
 		break;
 	}
+#ifdef INET6
 	case PF_INET6: {
 		struct sockaddr_in6 *si6 = satosin6(sa);
 		if (!IN6_IS_ADDR_UNSPECIFIED(&si6->sin6_addr))
@@ -642,6 +675,7 @@
 			r = printf("*.*");
 		break;
 	}
+#endif
 	case PF_LOCAL: {
 		struct sockaddr_un *sun = satosun(sa);
 		r = printf("%s", sun->sun_path);