git: c2aa91745e87 - main - netstat: restore printing the "default" keyword, provide -nn option

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Mon, 03 Feb 2025 18:10:07 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=c2aa91745e870d9d925cb054cc114f65180ed8c6

commit c2aa91745e870d9d925cb054cc114f65180ed8c6
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-02-03 18:09:58 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-02-03 18:09:58 +0000

    netstat: restore printing the "default" keyword, provide -nn option
    
    Avoid POLA breakage and preserve output standard that really predates
    the FreeBSD project itself.  There are scripts in the wild that rely
    on the behavior.
    
    Provide option to specify -nn twice to have a completely numeric
    output of the routing tables.
    
    Fixes:  9206c79961986c2114a9a2cfccf009ac010ad259
    This reverts commit e090646d6f5a4a6848ecd4bcb1f2db498ea3b3e2.
    
    Reviewed by:            zlei, gallatin, melifaro, allanjude, markj, emaste
    Differential Revision:  https://reviews.freebsd.org/D48729
---
 tests/sys/netinet/fibs_test.sh | 10 +++++-----
 tests/sys/netinet6/ndp.sh      |  6 +++---
 usr.bin/netstat/main.c         |  7 ++++---
 usr.bin/netstat/netstat.1      |  8 +++++++-
 usr.bin/netstat/netstat.h      |  4 ++--
 usr.bin/netstat/route.c        |  4 ++--
 6 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/tests/sys/netinet/fibs_test.sh b/tests/sys/netinet/fibs_test.sh
index a57d999496fc..b58a45b26f3e 100644
--- a/tests/sys/netinet/fibs_test.sh
+++ b/tests/sys/netinet/fibs_test.sh
@@ -240,9 +240,9 @@ default_route_with_multiple_fibs_on_same_subnet_body()
 
 	# Verify that the default route exists for both fibs, with their
 	# respective interfaces.
-	atf_check -o match:"^0\.0\.0\.0.*${TAP0}$" \
+	atf_check -o match:"^default.*${TAP0}$" \
 		setfib ${FIB0} netstat -rn -f inet
-	atf_check -o match:"^0\.0\.0\.0.*${TAP1}$" \
+	atf_check -o match:"^default.*${TAP1}$" \
 		setfib ${FIB1} netstat -rn -f inet
 }
 
@@ -286,9 +286,9 @@ default_route_with_multiple_fibs_on_same_subnet_inet6_body()
 
 	# Verify that the default route exists for both fibs, with their
 	# respective interfaces.
-	atf_check -o match:"^::/0.*${TAP0}$" \
+	atf_check -o match:"^default.*${TAP0}$" \
 		setfib ${FIB0} netstat -rn -f inet6
-	atf_check -o match:"^::/0.*${TAP1}$" \
+	atf_check -o match:"^default.*${TAP1}$" \
 		setfib ${FIB1} netstat -rn -f inet6
 }
 
@@ -479,7 +479,7 @@ slaac_on_nondefault_fib6_body()
 	atf_check -o match:"${SUBNET}:/${MASK}.*\<U\>.*$EPAIRB" \
 		netstat -rnf inet6 -F $FIB1
 	# Check default route
-	atf_check -o match:"^::/0.*\<UG\>.*$EPAIRB" \
+	atf_check -o match:"default.*\<UG\>.*$EPAIRB" \
 		netstat -rnf inet6 -F $FIB1
 
 	# Check that none of the above routes appeared on other routes
diff --git a/tests/sys/netinet6/ndp.sh b/tests/sys/netinet6/ndp.sh
index 24c0853d7361..038a640f331e 100755
--- a/tests/sys/netinet6/ndp.sh
+++ b/tests/sys/netinet6/ndp.sh
@@ -163,12 +163,12 @@ ndp_slaac_default_route_body() {
 	while [ -z "$(jexec ${jname} ndp -r)" ]; do
 		sleep 0.1
 	done
-	atf_check -o match:"^::/0[[:space:]]+fe80:" \
+	atf_check -o match:"^default[[:space:]]+fe80:" \
 	    jexec ${jname} netstat -rn -6
 
 	# Get rid of the default route.
 	jexec ${jname} route -6 flush
-	atf_check -o not-match:"^::/0[[:space:]]+fe80:" \
+	atf_check -o not-match:"^default[[:space:]]+fe80:" \
 	    jexec ${jname} netstat -rn -6
 
 	# Send another RA, make sure that the default route is installed again.
@@ -180,7 +180,7 @@ ndp_slaac_default_route_body() {
 	while [ -z "$(jexec ${jname} ndp -r)" ]; do
 		sleep 0.1
 	done
-	atf_check -o match:"^::/0[[:space:]]+fe80:" \
+	atf_check -o match:"^default[[:space:]]+fe80:" \
 	    jexec ${jname} netstat -rn -6
 }
 
diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c
index 97509ea6798b..371216f4be59 100644
--- a/usr.bin/netstat/main.c
+++ b/usr.bin/netstat/main.c
@@ -207,8 +207,8 @@ int	iflag;		/* show interfaces */
 int	Lflag;		/* show size of listen queues */
 int	mflag;		/* show memory stats */
 int	noutputs = 0;	/* how much outputs before we exit */
-int	numeric_addr;	/* show addresses numerically */
-int	numeric_port;	/* show ports numerically */
+u_int	numeric_addr = 0; /* show addresses numerically */
+bool	numeric_port;	/* show ports numerically */
 int	Oflag;		/* show nhgrp objects*/
 int	oflag;		/* show nexthop objects*/
 int	Pflag;		/* show TCP log ID */
@@ -361,7 +361,8 @@ main(int argc, char *argv[])
 			nlistf = optarg;
 			break;
 		case 'n':
-			numeric_addr = numeric_port = 1;
+			numeric_addr++;
+			numeric_port = true;
 			break;
 		case 'o':
 			oflag = 1;
diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1
index a5bbfc2f1b35..559cb1932080 100644
--- a/usr.bin/netstat/netstat.1
+++ b/usr.bin/netstat/netstat.1
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd July 29, 2024
+.Dd January 29, 2025
 .Dt NETSTAT 1
 .Os
 .Sh NAME
@@ -872,6 +872,12 @@ Normally
 .Nm
 attempts to resolve addresses and ports,
 and display them symbolically.
+Specifying
+.Fl n
+twice will also disable printing the keyword
+.Qq Dv default
+for the default IPv4 and IPv6 routes when displaying contents of routing
+tables.
 .It Fl W
 Wider output; expand address fields, etc, to avoid truncation.
 Non-numeric values such as domain names may still be truncated; use the
diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h
index 7ebfc5180f44..7e37b9e5e820 100644
--- a/usr.bin/netstat/netstat.h
+++ b/usr.bin/netstat/netstat.h
@@ -49,8 +49,8 @@ extern int	iflag;	/* show interfaces */
 extern int	Lflag;	/* show size of listen queues */
 extern int	mflag;	/* show memory stats */
 extern int	noutputs;	/* how much outputs before we exit */
-extern int	numeric_addr;	/* show addresses numerically */
-extern int	numeric_port;	/* show ports numerically */
+extern u_int	numeric_addr;	/* show addresses numerically */
+extern bool	numeric_port;	/* show ports numerically */
 extern int	Pflag;	/* show TCP log ID */
 extern int	rflag;	/* show routing tables (or routing stats) */
 extern int	Rflag;	/* show flowid / RSS information */
diff --git a/usr.bin/netstat/route.c b/usr.bin/netstat/route.c
index c54b1e61aa11..697c7ba2e9e1 100644
--- a/usr.bin/netstat/route.c
+++ b/usr.bin/netstat/route.c
@@ -578,7 +578,7 @@ netname4(in_addr_t in, in_addr_t mask)
 	struct netent *np = 0;
 	in_addr_t i;
 
-	if (!numeric_addr && in == INADDR_ANY && mask == 0) {
+	if (numeric_addr < 2 && in == INADDR_ANY && mask == 0) {
 		strlcpy(line, "default", sizeof(line));
 		return (line);
 	}
@@ -673,7 +673,7 @@ netname6(struct sockaddr_in6 *sa6, struct sockaddr_in6 *mask)
 	else
 		masklen = 128;
 
-	if (!numeric_addr && masklen == 0 &&
+	if (numeric_addr < 2 && masklen == 0 &&
 	    IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
 		return("default");