Blame SOURCES/0070-Use-proto_to_name-from-xshared-in-more-places.patch

d8275f
From f8839b3651e0ffbb93b6ce4675809d60782a4396 Mon Sep 17 00:00:00 2001
d8275f
From: Phil Sutter <phil@nwl.cc>
d8275f
Date: Tue, 17 Nov 2020 00:57:10 +0100
d8275f
Subject: [PATCH] Use proto_to_name() from xshared in more places
d8275f
d8275f
Share the common proto name lookup code. While being at it, make proto
d8275f
number variable 16bit, values may exceed 256.
d8275f
d8275f
This aligns iptables-nft '-p' argument printing with legacy iptables. In
d8275f
practice, this should make a difference only in corner cases.
d8275f
d8275f
Signed-off-by: Phil Sutter <phil@nwl.cc>
d8275f
(cherry picked from commit 556f704458cdb509d395ddb7d2629987d60e762e)
d8275f
---
d8275f
 include/xtables.h     |  2 +-
d8275f
 iptables/ip6tables.c  | 22 +++++-----------------
d8275f
 iptables/iptables.c   | 20 +++++---------------
d8275f
 iptables/nft-shared.c |  6 +++---
d8275f
 iptables/xshared.c    |  2 +-
d8275f
 iptables/xshared.h    |  2 +-
d8275f
 6 files changed, 16 insertions(+), 38 deletions(-)
d8275f
d8275f
diff --git a/include/xtables.h b/include/xtables.h
d8275f
index d77a73a4303a7..06982e720cbb8 100644
d8275f
--- a/include/xtables.h
d8275f
+++ b/include/xtables.h
d8275f
@@ -395,7 +395,7 @@ struct xtables_rule_match {
d8275f
  */
d8275f
 struct xtables_pprot {
d8275f
 	const char *name;
d8275f
-	uint8_t num;
d8275f
+	uint16_t num;
d8275f
 };
d8275f
 
d8275f
 enum xtables_tryload {
d8275f
diff --git a/iptables/ip6tables.c b/iptables/ip6tables.c
d8275f
index b96dc033e7ebb..4860682001360 100644
d8275f
--- a/iptables/ip6tables.c
d8275f
+++ b/iptables/ip6tables.c
d8275f
@@ -849,28 +849,16 @@ print_iface(char letter, const char *iface, const unsigned char *mask,
d8275f
 	}
d8275f
 }
d8275f
 
d8275f
-/* The ip6tables looks up the /etc/protocols. */
d8275f
 static void print_proto(uint16_t proto, int invert)
d8275f
 {
d8275f
 	if (proto) {
d8275f
-		unsigned int i;
d8275f
+		const char *pname = proto_to_name(proto, 0);
d8275f
 		const char *invertstr = invert ? " !" : "";
d8275f
 
d8275f
-		const struct protoent *pent = getprotobynumber(proto);
d8275f
-		if (pent) {
d8275f
-			printf("%s -p %s",
d8275f
-			       invertstr, pent->p_name);
d8275f
-			return;
d8275f
-		}
d8275f
-
d8275f
-		for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
d8275f
-			if (xtables_chain_protos[i].num == proto) {
d8275f
-				printf("%s -p %s",
d8275f
-				       invertstr, xtables_chain_protos[i].name);
d8275f
-				return;
d8275f
-			}
d8275f
-
d8275f
-		printf("%s -p %u", invertstr, proto);
d8275f
+		if (pname)
d8275f
+			printf("%s -p %s", invertstr, pname);
d8275f
+		else
d8275f
+			printf("%s -p %u", invertstr, proto);
d8275f
 	}
d8275f
 }
d8275f
 
d8275f
diff --git a/iptables/iptables.c b/iptables/iptables.c
d8275f
index 6e2946f5660de..620429b5d4817 100644
d8275f
--- a/iptables/iptables.c
d8275f
+++ b/iptables/iptables.c
d8275f
@@ -819,23 +819,13 @@ list_entries(const xt_chainlabel chain, int rulenum, int verbose, int numeric,
d8275f
 static void print_proto(uint16_t proto, int invert)
d8275f
 {
d8275f
 	if (proto) {
d8275f
-		unsigned int i;
d8275f
+		const char *pname = proto_to_name(proto, 0);
d8275f
 		const char *invertstr = invert ? " !" : "";
d8275f
 
d8275f
-		const struct protoent *pent = getprotobynumber(proto);
d8275f
-		if (pent) {
d8275f
-			printf("%s -p %s", invertstr, pent->p_name);
d8275f
-			return;
d8275f
-		}
d8275f
-
d8275f
-		for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
d8275f
-			if (xtables_chain_protos[i].num == proto) {
d8275f
-				printf("%s -p %s",
d8275f
-				       invertstr, xtables_chain_protos[i].name);
d8275f
-				return;
d8275f
-			}
d8275f
-
d8275f
-		printf("%s -p %u", invertstr, proto);
d8275f
+		if (pname)
d8275f
+			printf("%s -p %s", invertstr, pname);
d8275f
+		else
d8275f
+			printf("%s -p %u", invertstr, proto);
d8275f
 	}
d8275f
 }
d8275f
 
d8275f
diff --git a/iptables/nft-shared.c b/iptables/nft-shared.c
d8275f
index d73d0b6159be6..e3ba4ac34146f 100644
d8275f
--- a/iptables/nft-shared.c
d8275f
+++ b/iptables/nft-shared.c
d8275f
@@ -821,13 +821,13 @@ void save_rule_details(const struct iptables_command_state *cs,
d8275f
 	}
d8275f
 
d8275f
 	if (proto > 0) {
d8275f
-		const struct protoent *pent = getprotobynumber(proto);
d8275f
+		const char *pname = proto_to_name(proto, 0);
d8275f
 
d8275f
 		if (invflags & XT_INV_PROTO)
d8275f
 			printf("! ");
d8275f
 
d8275f
-		if (pent)
d8275f
-			printf("-p %s ", pent->p_name);
d8275f
+		if (pname)
d8275f
+			printf("-p %s ", pname);
d8275f
 		else
d8275f
 			printf("-p %u ", proto);
d8275f
 	}
d8275f
diff --git a/iptables/xshared.c b/iptables/xshared.c
d8275f
index 0c232ca2ae8d5..7a55ed5d15715 100644
d8275f
--- a/iptables/xshared.c
d8275f
+++ b/iptables/xshared.c
d8275f
@@ -48,7 +48,7 @@ void print_extension_helps(const struct xtables_target *t,
d8275f
 }
d8275f
 
d8275f
 const char *
d8275f
-proto_to_name(uint8_t proto, int nolookup)
d8275f
+proto_to_name(uint16_t proto, int nolookup)
d8275f
 {
d8275f
 	unsigned int i;
d8275f
 
d8275f
diff --git a/iptables/xshared.h b/iptables/xshared.h
d8275f
index 095a574d85879..f3c7f28806619 100644
d8275f
--- a/iptables/xshared.h
d8275f
+++ b/iptables/xshared.h
d8275f
@@ -146,7 +146,7 @@ enum {
d8275f
 
d8275f
 extern void print_extension_helps(const struct xtables_target *,
d8275f
 	const struct xtables_rule_match *);
d8275f
-extern const char *proto_to_name(uint8_t, int);
d8275f
+extern const char *proto_to_name(uint16_t, int);
d8275f
 extern int command_default(struct iptables_command_state *,
d8275f
 	struct xtables_globals *);
d8275f
 extern struct xtables_match *load_proto(struct iptables_command_state *);
d8275f
-- 
d8275f
2.34.1
d8275f