Blame SOURCES/0047-xtables-monitor-print-packet-first.patch

082114
From 07f51d26e405b4a328813f35bc27015eb9324330 Mon Sep 17 00:00:00 2001
082114
From: Florian Westphal <fw@strlen.de>
082114
Date: Sat, 12 Dec 2020 16:15:34 +0100
082114
Subject: [PATCH] xtables-monitor: print packet first
082114
082114
The trace mode should first print the packet that was received and
082114
then the rule/verdict.
082114
082114
Furthermore, the monitor did sometimes print an extra newline.
082114
082114
After this patch, output is more consistent with nft monitor.
082114
082114
Signed-off-by: Florian Westphal <fw@strlen.de>
082114
(cherry picked from commit 180ba723d0b305fab9287d3bc5f845a43d9eb793)
082114
Signed-off-by: Phil Sutter <psutter@redhat.com>
082114
---
082114
 iptables/xtables-monitor.c | 34 +++++++++++++++++++++++-----------
082114
 1 file changed, 23 insertions(+), 11 deletions(-)
082114
082114
diff --git a/iptables/xtables-monitor.c b/iptables/xtables-monitor.c
082114
index 9fa1ca166a61e..23e828988bb8b 100644
082114
--- a/iptables/xtables-monitor.c
082114
+++ b/iptables/xtables-monitor.c
082114
@@ -106,6 +106,7 @@ static int rule_cb(const struct nlmsghdr *nlh, void *data)
082114
 		printf("-0 ");
082114
 		break;
082114
 	default:
082114
+		puts("");
082114
 		goto err_free;
082114
 	}
082114
 
082114
@@ -433,9 +434,18 @@ static void trace_print_packet(const struct nftnl_trace *nlt, struct cb_arg *arg
082114
 	mark = nftnl_trace_get_u32(nlt, NFTNL_TRACE_MARK);
082114
 	if (mark)
082114
 		printf("MARK=0x%x ", mark);
082114
+	puts("");
082114
+}
082114
+
082114
+static void trace_print_hdr(const struct nftnl_trace *nlt)
082114
+{
082114
+	printf(" TRACE: %d %08x %s:%s", nftnl_trace_get_u32(nlt, NFTNL_TABLE_FAMILY),
082114
+					nftnl_trace_get_u32(nlt, NFTNL_TRACE_ID),
082114
+					nftnl_trace_get_str(nlt, NFTNL_TRACE_TABLE),
082114
+					nftnl_trace_get_str(nlt, NFTNL_TRACE_CHAIN));
082114
 }
082114
 
082114
-static void print_verdict(struct nftnl_trace *nlt, uint32_t verdict)
082114
+static void print_verdict(const struct nftnl_trace *nlt, uint32_t verdict)
082114
 {
082114
 	const char *chain;
082114
 
082114
@@ -496,35 +506,37 @@ static int trace_cb(const struct nlmsghdr *nlh, struct cb_arg *arg)
082114
 	    arg->nfproto != nftnl_trace_get_u32(nlt, NFTNL_TABLE_FAMILY))
082114
 		goto err_free;
082114
 
082114
-	printf(" TRACE: %d %08x %s:%s", nftnl_trace_get_u32(nlt, NFTNL_TABLE_FAMILY),
082114
-					nftnl_trace_get_u32(nlt, NFTNL_TRACE_ID),
082114
-					nftnl_trace_get_str(nlt, NFTNL_TRACE_TABLE),
082114
-					nftnl_trace_get_str(nlt, NFTNL_TRACE_CHAIN));
082114
-
082114
 	switch (nftnl_trace_get_u32(nlt, NFTNL_TRACE_TYPE)) {
082114
 	case NFT_TRACETYPE_RULE:
082114
 		verdict = nftnl_trace_get_u32(nlt, NFTNL_TRACE_VERDICT);
082114
-		printf(":rule:0x%llx:", (unsigned long long)nftnl_trace_get_u64(nlt, NFTNL_TRACE_RULE_HANDLE));
082114
-		print_verdict(nlt, verdict);
082114
 
082114
-		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_RULE_HANDLE))
082114
-			trace_print_rule(nlt, arg);
082114
 		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_LL_HEADER) ||
082114
 		    nftnl_trace_is_set(nlt, NFTNL_TRACE_NETWORK_HEADER))
082114
 			trace_print_packet(nlt, arg);
082114
+
082114
+		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_RULE_HANDLE)) {
082114
+			trace_print_hdr(nlt);
082114
+			printf(":rule:0x%" PRIx64":", nftnl_trace_get_u64(nlt, NFTNL_TRACE_RULE_HANDLE));
082114
+			print_verdict(nlt, verdict);
082114
+			printf(" ");
082114
+			trace_print_rule(nlt, arg);
082114
+		}
082114
 		break;
082114
 	case NFT_TRACETYPE_POLICY:
082114
+		trace_print_hdr(nlt);
082114
 		printf(":policy:");
082114
 		verdict = nftnl_trace_get_u32(nlt, NFTNL_TRACE_POLICY);
082114
 
082114
 		print_verdict(nlt, verdict);
082114
+		puts("");
082114
 		break;
082114
 	case NFT_TRACETYPE_RETURN:
082114
+		trace_print_hdr(nlt);
082114
 		printf(":return:");
082114
 		trace_print_return(nlt);
082114
+		puts("");
082114
 		break;
082114
 	}
082114
-	puts("");
082114
 err_free:
082114
 	nftnl_trace_free(nlt);
082114
 err:
082114
-- 
082114
2.31.1
082114