Blame SOURCES/Share-print_ipv-4-6-_addr-from-xtables.patch

9a3fa7
From 7c53ed370c79027455b4e342436da507be701e23 Mon Sep 17 00:00:00 2001
9a3fa7
From: Phil Sutter <psutter@redhat.com>
9a3fa7
Date: Fri, 15 Mar 2019 17:51:28 +0100
9a3fa7
Subject: [PATCH] Share print_ipv{4,6}_addr() from xtables
9a3fa7
9a3fa7
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1525980
9a3fa7
Upstream Status: iptables commit 8da04ffdca193
9a3fa7
Conflicts:
9a3fa7
* Dropped changes to non-existing nft-ipv4.c and nft-ipv6.c.
9a3fa7
* Context change in xshared.{c,h}.
9a3fa7
9a3fa7
commit 8da04ffdca1931402a6bc22c43c1a2fa1c6f1e14
9a3fa7
Author: Phil Sutter <phil@nwl.cc>
9a3fa7
Date:   Wed Sep 19 15:16:59 2018 +0200
9a3fa7
9a3fa7
    Share print_ipv{4,6}_addr() from xtables
9a3fa7
9a3fa7
    These functions contain code which occurs in legacy's print_firewall()
9a3fa7
    functions, so use them there.
9a3fa7
9a3fa7
    Rename them to at least make clear they print more than a single
9a3fa7
    address.
9a3fa7
9a3fa7
    Also introduce ipv{4,6}_addr_to_string() which take care of converting
9a3fa7
    an address/netmask pair into string representation in a way which
9a3fa7
    doesn't upset covscan (since that didn't detect that 'buf' may not be
9a3fa7
    exceeded by the strings written into it.
9a3fa7
9a3fa7
    Signed-off-by: Phil Sutter <phil@nwl.cc>
9a3fa7
    Signed-off-by: Florian Westphal <fw@strlen.de>
9a3fa7
9a3fa7
Signed-off-by: Phil Sutter <psutter@redhat.com>
9a3fa7
---
9a3fa7
 iptables/ip6tables.c | 27 +-----------------
9a3fa7
 iptables/iptables.c  | 25 +----------------
9a3fa7
 iptables/xshared.c   | 66 ++++++++++++++++++++++++++++++++++++++++++++
9a3fa7
 iptables/xshared.h   |  3 ++
9a3fa7
 4 files changed, 71 insertions(+), 50 deletions(-)
9a3fa7
9a3fa7
diff --git a/iptables/ip6tables.c b/iptables/ip6tables.c
9a3fa7
index 76e36d44f6b25..fc2fd37cfe919 100644
9a3fa7
--- a/iptables/ip6tables.c
9a3fa7
+++ b/iptables/ip6tables.c
9a3fa7
@@ -541,7 +541,6 @@ print_firewall(const struct ip6t_entry *fw,
9a3fa7
 {
9a3fa7
 	const struct xtables_target *target = NULL;
9a3fa7
 	const struct xt_entry_target *t;
9a3fa7
-	char buf[BUFSIZ];
9a3fa7
 
9a3fa7
 	if (!ip6tc_is_chain(targname, handle))
9a3fa7
 		target = xtables_find_target(targname, XTF_TRY_LOAD);
9a3fa7
@@ -609,31 +608,7 @@ print_firewall(const struct ip6t_entry *fw,
9a3fa7
 		printf(FMT("%-6s ","out %s "), iface);
9a3fa7
 	}
9a3fa7
 
9a3fa7
-	fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
9a3fa7
-	if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
9a3fa7
-	    && !(format & FMT_NUMERIC))
9a3fa7
-		printf(FMT("%-19s ","%s "), "anywhere");
9a3fa7
-	else {
9a3fa7
-		if (format & FMT_NUMERIC)
9a3fa7
-			strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
9a3fa7
-		else
9a3fa7
-			strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
9a3fa7
-		strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
9a3fa7
-		printf(FMT("%-19s ","%s "), buf);
9a3fa7
-	}
9a3fa7
-
9a3fa7
-	fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
9a3fa7
-	if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
9a3fa7
-	    && !(format & FMT_NUMERIC))
9a3fa7
-		printf(FMT("%-19s ","-> %s"), "anywhere");
9a3fa7
-	else {
9a3fa7
-		if (format & FMT_NUMERIC)
9a3fa7
-			strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
9a3fa7
-		else
9a3fa7
-			strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
9a3fa7
-		strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
9a3fa7
-		printf(FMT("%-19s ","-> %s"), buf);
9a3fa7
-	}
9a3fa7
+	print_ipv6_addresses(fw, format);
9a3fa7
 
9a3fa7
 	if (format & FMT_NOTABLE)
9a3fa7
 		fputs("  ", stdout);
9a3fa7
diff --git a/iptables/iptables.c b/iptables/iptables.c
9a3fa7
index bac9fe0905e9f..dc70cc6e9b0ec 100644
9a3fa7
--- a/iptables/iptables.c
9a3fa7
+++ b/iptables/iptables.c
9a3fa7
@@ -526,7 +526,6 @@ print_firewall(const struct ipt_entry *fw,
9a3fa7
 	const struct xtables_target *target = NULL;
9a3fa7
 	const struct xt_entry_target *t;
9a3fa7
 	uint8_t flags;
9a3fa7
-	char buf[BUFSIZ];
9a3fa7
 
9a3fa7
 	if (!iptc_is_chain(targname, handle))
9a3fa7
 		target = xtables_find_target(targname, XTF_TRY_LOAD);
9a3fa7
@@ -595,29 +594,7 @@ print_firewall(const struct ipt_entry *fw,
9a3fa7
 		printf(FMT("%-6s ","out %s "), iface);
9a3fa7
 	}
9a3fa7
 
9a3fa7
-	fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
9a3fa7
-	if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
9a3fa7
-		printf(FMT("%-19s ","%s "), "anywhere");
9a3fa7
-	else {
9a3fa7
-		if (format & FMT_NUMERIC)
9a3fa7
-			strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.src));
9a3fa7
-		else
9a3fa7
-			strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.src));
9a3fa7
-		strcat(buf, xtables_ipmask_to_numeric(&fw->ip.smsk));
9a3fa7
-		printf(FMT("%-19s ","%s "), buf);
9a3fa7
-	}
9a3fa7
-
9a3fa7
-	fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
9a3fa7
-	if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
9a3fa7
-		printf(FMT("%-19s ","-> %s"), "anywhere");
9a3fa7
-	else {
9a3fa7
-		if (format & FMT_NUMERIC)
9a3fa7
-			strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.dst));
9a3fa7
-		else
9a3fa7
-			strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.dst));
9a3fa7
-		strcat(buf, xtables_ipmask_to_numeric(&fw->ip.dmsk));
9a3fa7
-		printf(FMT("%-19s ","-> %s"), buf);
9a3fa7
-	}
9a3fa7
+	print_ipv4_addresses(fw, format);
9a3fa7
 
9a3fa7
 	if (format & FMT_NOTABLE)
9a3fa7
 		fputs("  ", stdout);
9a3fa7
diff --git a/iptables/xshared.c b/iptables/xshared.c
9a3fa7
index b8a81fd968361..742502154aa55 100644
9a3fa7
--- a/iptables/xshared.c
9a3fa7
+++ b/iptables/xshared.c
9a3fa7
@@ -340,3 +340,69 @@ inline bool xs_has_arg(int argc, char *argv[])
9a3fa7
 	       argv[optind][0] != '-' &&
9a3fa7
 	       argv[optind][0] != '!';
9a3fa7
 }
9a3fa7
+
9a3fa7
+static const char *ipv4_addr_to_string(const struct in_addr *addr,
9a3fa7
+				       const struct in_addr *mask,
9a3fa7
+				       unsigned int format)
9a3fa7
+{
9a3fa7
+	static char buf[BUFSIZ];
9a3fa7
+
9a3fa7
+	if (!mask->s_addr && !(format & FMT_NUMERIC))
9a3fa7
+		return "anywhere";
9a3fa7
+
9a3fa7
+	if (format & FMT_NUMERIC)
9a3fa7
+		strncpy(buf, xtables_ipaddr_to_numeric(addr), BUFSIZ - 1);
9a3fa7
+	else
9a3fa7
+		strncpy(buf, xtables_ipaddr_to_anyname(addr), BUFSIZ - 1);
9a3fa7
+	buf[BUFSIZ - 1] = '\0';
9a3fa7
+
9a3fa7
+	strncat(buf, xtables_ipmask_to_numeric(mask),
9a3fa7
+		BUFSIZ - strlen(buf) - 1);
9a3fa7
+
9a3fa7
+	return buf;
9a3fa7
+}
9a3fa7
+
9a3fa7
+void print_ipv4_addresses(const struct ipt_entry *fw, unsigned int format)
9a3fa7
+{
9a3fa7
+	fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
9a3fa7
+	printf(FMT("%-19s ", "%s "),
9a3fa7
+	       ipv4_addr_to_string(&fw->ip.src, &fw->ip.smsk, format));
9a3fa7
+
9a3fa7
+	fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
9a3fa7
+	printf(FMT("%-19s ", "-> %s"),
9a3fa7
+	       ipv4_addr_to_string(&fw->ip.dst, &fw->ip.dmsk, format));
9a3fa7
+}
9a3fa7
+
9a3fa7
+static const char *ipv6_addr_to_string(const struct in6_addr *addr,
9a3fa7
+				       const struct in6_addr *mask,
9a3fa7
+				       unsigned int format)
9a3fa7
+{
9a3fa7
+	static char buf[BUFSIZ];
9a3fa7
+
9a3fa7
+	if (IN6_IS_ADDR_UNSPECIFIED(addr) && !(format & FMT_NUMERIC))
9a3fa7
+		return "anywhere";
9a3fa7
+
9a3fa7
+	if (format & FMT_NUMERIC)
9a3fa7
+		strncpy(buf, xtables_ip6addr_to_numeric(addr), BUFSIZ - 1);
9a3fa7
+	else
9a3fa7
+		strncpy(buf, xtables_ip6addr_to_anyname(addr), BUFSIZ - 1);
9a3fa7
+	buf[BUFSIZ - 1] = '\0';
9a3fa7
+
9a3fa7
+	strncat(buf, xtables_ip6mask_to_numeric(mask),
9a3fa7
+		BUFSIZ - strlen(buf) - 1);
9a3fa7
+
9a3fa7
+	return buf;
9a3fa7
+}
9a3fa7
+
9a3fa7
+void print_ipv6_addresses(const struct ip6t_entry *fw6, unsigned int format)
9a3fa7
+{
9a3fa7
+	fputc(fw6->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
9a3fa7
+	printf(FMT("%-19s ", "%s "),
9a3fa7
+	       ipv6_addr_to_string(&fw6->ipv6.src,
9a3fa7
+				   &fw6->ipv6.smsk, format));
9a3fa7
+
9a3fa7
+	fputc(fw6->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
9a3fa7
+	printf(FMT("%-19s ", "-> %s"),
9a3fa7
+	       ipv6_addr_to_string(&fw6->ipv6.dst,
9a3fa7
+				   &fw6->ipv6.dmsk, format));
9a3fa7
+}
9a3fa7
diff --git a/iptables/xshared.h b/iptables/xshared.h
9a3fa7
index c35dfee47577d..20dbbd12118ad 100644
9a3fa7
--- a/iptables/xshared.h
9a3fa7
+++ b/iptables/xshared.h
9a3fa7
@@ -113,4 +113,7 @@ bool xs_has_arg(int argc, char *argv[]);
9a3fa7
 
9a3fa7
 extern const struct xtables_afinfo *afinfo;
9a3fa7
 
9a3fa7
+void print_ipv4_addresses(const struct ipt_entry *fw, unsigned int format);
9a3fa7
+void print_ipv6_addresses(const struct ip6t_entry *fw6, unsigned int format);
9a3fa7
+
9a3fa7
 #endif /* IPTABLES_XSHARED_H */
9a3fa7
-- 
9a3fa7
2.21.0
9a3fa7