Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2 05/11] ss: replay all dumps in TCPDIAG_FILE
Date: Mon,  9 Nov 2026 13:25:21 -0800	[thread overview]
Message-ID: <20261109212559.39661-6-stephen@networkplumber.org> (raw)
In-Reply-To: <20261109212559.39661-1-stephen@networkplumber.org>

ss -D records an IPv4 dump followed by an IPv6 dump, but the
replay stopped at the first NLMSG_DONE, so IPv6 sockets were never
shown. Keep reading until end of file, and apply the -4/-6 family
filter, which a live dump applies in the request. The family comes
from the file, so check its range.

The ssfilter test expected the IPv4-only output; add -4 where both
families match and add IPv6 cases.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 misc/ss.c                     | 18 +++++++++++++-----
 testsuite/tests/ss/ssfilter.t | 26 +++++++++++++++++++-------
 2 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index e3a2e590..527725bd 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -444,6 +444,9 @@ static void filter_af_set(struct filter *f, int af)
 
 static int filter_af_get(struct filter *f, int af)
 {
+	if (af < 0 || af >= 64)
+		return 0;
+
 	return !!(f->families & FAMILY_MASK(af));
 }
 
@@ -3943,8 +3946,8 @@ static int tcp_show_netlink_file(struct filter *f)
 		if (status != sizeof(hdr)) {
 			if (ferror(fp))
 				perror("Reading header from $TCPDIAG_FILE");
-			if (feof(fp))
-				fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE");
+			else if (status || err)
+				fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE\n");
 			break;
 		}
 
@@ -3973,15 +3976,17 @@ static int tcp_show_netlink_file(struct filter *f)
 			if (ferror(fp))
 				perror("Reading $TCPDIAG_FILE");
 			if (feof(fp))
-				fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE");
+				fprintf(stderr, "Unexpected EOF reading $TCPDIAG_FILE\n");
+			err = -1;
 			break;
 		}
 
-		/* The only legal exit point */
+		/* One dump per address family; the file ends after a DONE */
 		if (h->nlmsg_type == NLMSG_DONE) {
 			err = 0;
-			break;
+			continue;
 		}
+		err = -1;
 
 		if (h->nlmsg_type == NLMSG_ERROR) {
 			struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
@@ -3998,6 +4003,9 @@ static int tcp_show_netlink_file(struct filter *f)
 		parse_diag_msg(h, &s);
 		s.type = IPPROTO_TCP;
 
+		if (f && !filter_af_get(f, s.local.family))
+			continue;
+
 		if (f && f->f && run_ssfilter(f->f, &s) == 0)
 			continue;
 
diff --git a/testsuite/tests/ss/ssfilter.t b/testsuite/tests/ss/ssfilter.t
index 4c2315ca..cc0f01a5 100755
--- a/testsuite/tests/ss/ssfilter.t
+++ b/testsuite/tests/ss/ssfilter.t
@@ -3,21 +3,27 @@
 . lib/generic.sh
 
 # % ./misc/ss -Htna
-# LISTEN  0    128    0.0.0.0:22       0.0.0.0:*
-# ESTAB   0    0     10.0.0.1:22      10.0.0.1:36266
-# ESTAB   0    0     10.0.0.1:36266   10.0.0.1:22
-# ESTAB   0    0     10.0.0.1:22      10.0.0.2:50312
+# LISTEN 0      128          0.0.0.0:22         0.0.0.0:*
+# ESTAB  0      0           10.0.0.1:22        10.0.0.1:36266
+# ESTAB  0      0           10.0.0.1:36266     10.0.0.1:22
+# ESTAB  0      0           10.0.0.1:22        10.0.0.2:50312
+# LISTEN 0      128             [::]:22            [::]:*
+# ESTAB  0      0      [fec0:42::24]:40520 [fec0:42::1]:22
+# ESTAB  0      0      [fec0:42::24]:22    [fec0:42::1]:44248
+#
+# Each test filters to one address family, so the address columns are
+# not padded for the wider IPv6 rows.
 export TCPDIAG_FILE="$(dirname $0)/ss1.dump"
 
 ts_log "[Testing ssfilter]"
 
-ts_ss "$0" "Match dport = 22" -Htna dport = 22
+ts_ss "$0" "Match dport = 22" -Htna4 dport = 22
 test_on "ESTAB 0      0      10.0.0.1:36266 10.0.0.1:22"
 
-ts_ss "$0" "Match dport 22" -Htna dport 22
+ts_ss "$0" "Match dport 22" -Htna4 dport 22
 test_on "ESTAB 0      0      10.0.0.1:36266 10.0.0.1:22"
 
-ts_ss "$0" "Match (dport)" -Htna '( dport = 22 )'
+ts_ss "$0" "Match (dport)" -Htna4 '( dport = 22 )'
 test_on "ESTAB 0      0      10.0.0.1:36266 10.0.0.1:22"
 
 ts_ss "$0" "Match src = 0.0.0.0" -Htna src = 0.0.0.0
@@ -46,3 +52,9 @@ test_on "ESTAB 0      0      10.0.0.1:22 10.0.0.2:50312"
 
 ts_ss "$0" "Match (src or src) and dst" -Htna '( src 0.0.0.0 or src 10.0.0.1 ) and dst 10.0.0.2'
 test_on "ESTAB 0      0      10.0.0.1:22 10.0.0.2:50312"
+
+ts_ss "$0" "Match IPv6 dport = 22" -Htna6 dport = 22
+test_on "ESTAB 0      0      \[fec0:42::24\]:40520 \[fec0:42::1\]:22"
+
+ts_ss "$0" "Match IPv6 src:port" -Htna 'src [fec0:42::24]:22'
+test_on "ESTAB 0      0      \[fec0:42::24\]:22 \[fec0:42::1\]:44248"
-- 
2.53.0


  parent reply	other threads:[~2026-11-09 21:26 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 21:25 [PATCH iproute2 00/11] ss fixes from an OSS scan Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 01/11] ss: fix double close on sock_diag send failure Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 02/11] ss: drop /proc fallback for socket listing Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 03/11] ss: escape names chosen by the socket owner Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 04/11] ss: size the TCPDIAG_FILE buffer to the record Stephen Hemminger
2026-11-09 21:25 ` Stephen Hemminger [this message]
2026-11-09 21:25 ` [PATCH iproute2 06/11] ss: bound state and type name lookups Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 07/11] ss: fix rpcbind service cache Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 08/11] ss: read socket SELinux context only for -z Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 09/11] ss: check owner before naming a netlink socket Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 10/11] ss: keep following events after ENOBUFS Stephen Hemminger
2026-11-09 21:25 ` [PATCH iproute2 11/11] ss: report a failed SCTP or MPTCP dump Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261109212559.39661-6-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dsahern@kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox