Blame SOURCES/0052-xtables-arp-Don-t-use-ARPT_INV_.patch

082114
From a5d52efe21e0f0ba6447b48e1646bb7046cb09eb Mon Sep 17 00:00:00 2001
082114
From: Phil Sutter <phil@nwl.cc>
082114
Date: Tue, 3 Nov 2020 12:21:29 +0100
082114
Subject: [PATCH] xtables-arp: Don't use ARPT_INV_*
082114
082114
Arptables invflags are partly identical to IPT_INV_* ones but the bits
082114
are differently assigned. Eliminate this incompatibility by definition
082114
of the unique invflags in nft-arp.h on bits that don't collide with
082114
IPT_INV_* ones, then use those in combination with IPT_INV_* ones in
082114
arptables-specific code.
082114
082114
Note that ARPT_INV_ARPPRO is replaced by IPT_INV_PROTO although these
082114
are in fact different options - yet since '-p' option is not supported
082114
by arptables, this does not lead to a collision.
082114
082114
Signed-off-by: Phil Sutter <phil@nwl.cc>
082114
(cherry picked from commit 44457c0805905ea22b4ecf9156648e774dd29155)
082114
---
082114
 iptables/nft-arp.c     | 92 ++++++++++++++++--------------------------
082114
 iptables/nft-arp.h     |  7 ++++
082114
 iptables/xtables-arp.c | 22 +++++-----
082114
 3 files changed, 53 insertions(+), 68 deletions(-)
082114
082114
diff --git a/iptables/nft-arp.c b/iptables/nft-arp.c
082114
index 776b55949472b..ec8147dd58c0d 100644
082114
--- a/iptables/nft-arp.c
082114
+++ b/iptables/nft-arp.c
082114
@@ -134,34 +134,34 @@ static int nft_arp_add(struct nft_handle *h, struct nftnl_rule *r, void *data)
082114
 	int ret = 0;
082114
 
082114
 	if (fw->arp.iniface[0] != '\0') {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_VIA_IN);
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_VIA_IN);
082114
 		add_iniface(r, fw->arp.iniface, op);
082114
 	}
082114
 
082114
 	if (fw->arp.outiface[0] != '\0') {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_VIA_OUT);
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_VIA_OUT);
082114
 		add_outiface(r, fw->arp.outiface, op);
082114
 	}
082114
 
082114
 	if (fw->arp.arhrd != 0 ||
082114
-	    fw->arp.invflags & ARPT_INV_ARPHRD) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_ARPHRD);
082114
+	    fw->arp.invflags & IPT_INV_ARPHRD) {
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_ARPHRD);
082114
 		add_payload(r, offsetof(struct arphdr, ar_hrd), 2,
082114
 			    NFT_PAYLOAD_NETWORK_HEADER);
082114
 		add_cmp_u16(r, fw->arp.arhrd, op);
082114
 	}
082114
 
082114
 	if (fw->arp.arpro != 0 ||
082114
-	    fw->arp.invflags & ARPT_INV_ARPPRO) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_ARPPRO);
082114
+	    fw->arp.invflags & IPT_INV_PROTO) {
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_PROTO);
082114
 	        add_payload(r, offsetof(struct arphdr, ar_pro), 2,
082114
 			    NFT_PAYLOAD_NETWORK_HEADER);
082114
 		add_cmp_u16(r, fw->arp.arpro, op);
082114
 	}
082114
 
082114
 	if (fw->arp.arhln != 0 ||
082114
-	    fw->arp.invflags & ARPT_INV_ARPHLN) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_ARPHLN);
082114
+	    fw->arp.invflags & IPT_INV_ARPHLN) {
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_ARPHLN);
082114
 		add_proto(r, offsetof(struct arphdr, ar_hln), 1,
082114
 			  fw->arp.arhln, op);
082114
 	}
082114
@@ -169,15 +169,15 @@ static int nft_arp_add(struct nft_handle *h, struct nftnl_rule *r, void *data)
082114
 	add_proto(r, offsetof(struct arphdr, ar_pln), 1, 4, NFT_CMP_EQ);
082114
 
082114
 	if (fw->arp.arpop != 0 ||
082114
-	    fw->arp.invflags & ARPT_INV_ARPOP) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_ARPOP);
082114
+	    fw->arp.invflags & IPT_INV_ARPOP) {
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_ARPOP);
082114
 		add_payload(r, offsetof(struct arphdr, ar_op), 2,
082114
 			    NFT_PAYLOAD_NETWORK_HEADER);
082114
 		add_cmp_u16(r, fw->arp.arpop, op);
082114
 	}
082114
 
082114
 	if (need_devaddr(&fw->arp.src_devaddr)) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_SRCDEVADDR);
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_SRCDEVADDR);
082114
 		add_addr(r, NFT_PAYLOAD_NETWORK_HEADER,
082114
 			 sizeof(struct arphdr),
082114
 			 &fw->arp.src_devaddr.addr,
082114
@@ -188,8 +188,8 @@ static int nft_arp_add(struct nft_handle *h, struct nftnl_rule *r, void *data)
082114
 
082114
 	if (fw->arp.src.s_addr != 0 ||
082114
 	    fw->arp.smsk.s_addr != 0 ||
082114
-	    fw->arp.invflags & ARPT_INV_SRCIP) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_SRCIP);
082114
+	    fw->arp.invflags & IPT_INV_SRCIP) {
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_SRCIP);
082114
 		add_addr(r, NFT_PAYLOAD_NETWORK_HEADER,
082114
 			 sizeof(struct arphdr) + fw->arp.arhln,
082114
 			 &fw->arp.src.s_addr, &fw->arp.smsk.s_addr,
082114
@@ -198,7 +198,7 @@ static int nft_arp_add(struct nft_handle *h, struct nftnl_rule *r, void *data)
082114
 
082114
 
082114
 	if (need_devaddr(&fw->arp.tgt_devaddr)) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_TGTDEVADDR);
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_TGTDEVADDR);
082114
 		add_addr(r, NFT_PAYLOAD_NETWORK_HEADER,
082114
 			 sizeof(struct arphdr) + fw->arp.arhln + sizeof(struct in_addr),
082114
 			 &fw->arp.tgt_devaddr.addr,
082114
@@ -208,8 +208,8 @@ static int nft_arp_add(struct nft_handle *h, struct nftnl_rule *r, void *data)
082114
 
082114
 	if (fw->arp.tgt.s_addr != 0 ||
082114
 	    fw->arp.tmsk.s_addr != 0 ||
082114
-	    fw->arp.invflags & ARPT_INV_TGTIP) {
082114
-		op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_TGTIP);
082114
+	    fw->arp.invflags & IPT_INV_DSTIP) {
082114
+		op = nft_invflags2cmp(fw->arp.invflags, IPT_INV_DSTIP);
082114
 		add_addr(r, NFT_PAYLOAD_NETWORK_HEADER,
082114
 			 sizeof(struct arphdr) + fw->arp.arhln + sizeof(struct in_addr) + fw->arp.arhln,
082114
 			 &fw->arp.tgt.s_addr, &fw->arp.tmsk.s_addr,
082114
@@ -240,28 +240,6 @@ static int nft_arp_add(struct nft_handle *h, struct nftnl_rule *r, void *data)
082114
 	return ret;
082114
 }
082114
 
082114
-static uint16_t ipt_to_arpt_flags(uint8_t invflags)
082114
-{
082114
-	uint16_t result = 0;
082114
-
082114
-	if (invflags & IPT_INV_VIA_IN)
082114
-		result |= ARPT_INV_VIA_IN;
082114
-
082114
-	if (invflags & IPT_INV_VIA_OUT)
082114
-		result |= ARPT_INV_VIA_OUT;
082114
-
082114
-	if (invflags & IPT_INV_SRCIP)
082114
-		result |= ARPT_INV_SRCIP;
082114
-
082114
-	if (invflags & IPT_INV_DSTIP)
082114
-		result |= ARPT_INV_TGTIP;
082114
-
082114
-	if (invflags & IPT_INV_PROTO)
082114
-		result |= ARPT_INV_ARPPRO;
082114
-
082114
-	return result;
082114
-}
082114
-
082114
 static void nft_arp_parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e,
082114
 			       void *data)
082114
 {
082114
@@ -273,7 +251,7 @@ static void nft_arp_parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e,
082114
 		   fw->arp.outiface, fw->arp.outiface_mask,
082114
 		   &flags);
082114
 
082114
-	fw->arp.invflags |= ipt_to_arpt_flags(flags);
082114
+	fw->arp.invflags |= flags;
082114
 }
082114
 
082114
 static void nft_arp_parse_immediate(const char *jumpto, bool nft_goto,
082114
@@ -330,33 +308,33 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
082114
 		fw->arp.arhrd = ar_hrd;
082114
 		fw->arp.arhrd_mask = 0xffff;
082114
 		if (inv)
082114
-			fw->arp.invflags |= ARPT_INV_ARPHRD;
082114
+			fw->arp.invflags |= IPT_INV_ARPHRD;
082114
 		break;
082114
 	case offsetof(struct arphdr, ar_pro):
082114
 		get_cmp_data(e, &ar_pro, sizeof(ar_pro), &inv;;
082114
 		fw->arp.arpro = ar_pro;
082114
 		fw->arp.arpro_mask = 0xffff;
082114
 		if (inv)
082114
-			fw->arp.invflags |= ARPT_INV_ARPPRO;
082114
+			fw->arp.invflags |= IPT_INV_PROTO;
082114
 		break;
082114
 	case offsetof(struct arphdr, ar_op):
082114
 		get_cmp_data(e, &ar_op, sizeof(ar_op), &inv;;
082114
 		fw->arp.arpop = ar_op;
082114
 		fw->arp.arpop_mask = 0xffff;
082114
 		if (inv)
082114
-			fw->arp.invflags |= ARPT_INV_ARPOP;
082114
+			fw->arp.invflags |= IPT_INV_ARPOP;
082114
 		break;
082114
 	case offsetof(struct arphdr, ar_hln):
082114
 		get_cmp_data(e, &ar_hln, sizeof(ar_hln), &inv;;
082114
 		fw->arp.arhln = ar_hln;
082114
 		fw->arp.arhln_mask = 0xff;
082114
 		if (inv)
082114
-			fw->arp.invflags |= ARPT_INV_ARPOP;
082114
+			fw->arp.invflags |= IPT_INV_ARPOP;
082114
 		break;
082114
 	default:
082114
 		if (ctx->payload.offset == sizeof(struct arphdr)) {
082114
 			if (nft_arp_parse_devaddr(ctx, e, &fw->arp.src_devaddr))
082114
-				fw->arp.invflags |= ARPT_INV_SRCDEVADDR;
082114
+				fw->arp.invflags |= IPT_INV_SRCDEVADDR;
082114
 		} else if (ctx->payload.offset == sizeof(struct arphdr) +
082114
 					   fw->arp.arhln) {
082114
 			get_cmp_data(e, &addr, sizeof(addr), &inv;;
082114
@@ -371,12 +349,12 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
082114
 			}
082114
 
082114
 			if (inv)
082114
-				fw->arp.invflags |= ARPT_INV_SRCIP;
082114
+				fw->arp.invflags |= IPT_INV_SRCIP;
082114
 		} else if (ctx->payload.offset == sizeof(struct arphdr) +
082114
 						  fw->arp.arhln +
082114
 						  sizeof(struct in_addr)) {
082114
 			if (nft_arp_parse_devaddr(ctx, e, &fw->arp.tgt_devaddr))
082114
-				fw->arp.invflags |= ARPT_INV_TGTDEVADDR;
082114
+				fw->arp.invflags |= IPT_INV_TGTDEVADDR;
082114
 		} else if (ctx->payload.offset == sizeof(struct arphdr) +
082114
 						  fw->arp.arhln +
082114
 						  sizeof(struct in_addr) +
082114
@@ -393,7 +371,7 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
082114
 			}
082114
 
082114
 			if (inv)
082114
-				fw->arp.invflags |= ARPT_INV_TGTIP;
082114
+				fw->arp.invflags |= IPT_INV_DSTIP;
082114
 		}
082114
 		break;
082114
 	}
082114
@@ -448,7 +426,7 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 		else strcat(iface, "any");
082114
 	}
082114
 	if (print_iface) {
082114
-		printf("%s%s-i %s", sep, fw->arp.invflags & ARPT_INV_VIA_IN ?
082114
+		printf("%s%s-i %s", sep, fw->arp.invflags & IPT_INV_VIA_IN ?
082114
 				   "! " : "", iface);
082114
 		sep = " ";
082114
 	}
082114
@@ -466,13 +444,13 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 		else strcat(iface, "any");
082114
 	}
082114
 	if (print_iface) {
082114
-		printf("%s%s-o %s", sep, fw->arp.invflags & ARPT_INV_VIA_OUT ?
082114
+		printf("%s%s-o %s", sep, fw->arp.invflags & IPT_INV_VIA_OUT ?
082114
 				   "! " : "", iface);
082114
 		sep = " ";
082114
 	}
082114
 
082114
 	if (fw->arp.smsk.s_addr != 0L) {
082114
-		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_SRCIP
082114
+		printf("%s%s", sep, fw->arp.invflags & IPT_INV_SRCIP
082114
 			? "! " : "");
082114
 		if (format & FMT_NUMERIC)
082114
 			sprintf(buf, "%s", addr_to_dotted(&(fw->arp.src)));
082114
@@ -489,7 +467,7 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 			break;
082114
 	if (i == ARPT_DEV_ADDR_LEN_MAX)
082114
 		goto after_devsrc;
082114
-	printf("%s%s", sep, fw->arp.invflags & ARPT_INV_SRCDEVADDR
082114
+	printf("%s%s", sep, fw->arp.invflags & IPT_INV_SRCDEVADDR
082114
 		? "! " : "");
082114
 	printf("--src-mac ");
082114
 	xtables_print_mac_and_mask((unsigned char *)fw->arp.src_devaddr.addr,
082114
@@ -498,7 +476,7 @@ static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
082114
 after_devsrc:
082114
 
082114
 	if (fw->arp.tmsk.s_addr != 0L) {
082114
-		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_TGTIP
082114
+		printf("%s%s", sep, fw->arp.invflags & IPT_INV_DSTIP
082114
 			? "! " : "");
082114
 		if (format & FMT_NUMERIC)
082114
 			sprintf(buf, "%s", addr_to_dotted(&(fw->arp.tgt)));
082114
@@ -515,7 +493,7 @@ after_devsrc:
082114
 			break;
082114
 	if (i == ARPT_DEV_ADDR_LEN_MAX)
082114
 		goto after_devdst;
082114
-	printf("%s%s", sep, fw->arp.invflags & ARPT_INV_TGTDEVADDR
082114
+	printf("%s%s", sep, fw->arp.invflags & IPT_INV_TGTDEVADDR
082114
 		? "! " : "");
082114
 	printf("--dst-mac ");
082114
 	xtables_print_mac_and_mask((unsigned char *)fw->arp.tgt_devaddr.addr,
082114
@@ -525,7 +503,7 @@ after_devsrc:
082114
 after_devdst:
082114
 
082114
 	if (fw->arp.arhln_mask != 255 || fw->arp.arhln != 6) {
082114
-		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPHLN
082114
+		printf("%s%s", sep, fw->arp.invflags & IPT_INV_ARPHLN
082114
 			? "! " : "");
082114
 		printf("--h-length %d", fw->arp.arhln);
082114
 		if (fw->arp.arhln_mask != 255)
082114
@@ -536,7 +514,7 @@ after_devdst:
082114
 	if (fw->arp.arpop_mask != 0) {
082114
 		int tmp = ntohs(fw->arp.arpop);
082114
 
082114
-		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPOP
082114
+		printf("%s%s", sep, fw->arp.invflags & IPT_INV_ARPOP
082114
 			? "! " : "");
082114
 		if (tmp <= NUMOPCODES && !(format & FMT_NUMERIC))
082114
 			printf("--opcode %s", arp_opcodes[tmp-1]);
082114
@@ -551,7 +529,7 @@ after_devdst:
082114
 	if (fw->arp.arhrd_mask != 65535 || fw->arp.arhrd != htons(1)) {
082114
 		uint16_t tmp = ntohs(fw->arp.arhrd);
082114
 
082114
-		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPHRD
082114
+		printf("%s%s", sep, fw->arp.invflags & IPT_INV_ARPHRD
082114
 			? "! " : "");
082114
 		if (tmp == 1 && !(format & FMT_NUMERIC))
082114
 			printf("--h-type %s", "Ethernet");
082114
@@ -565,7 +543,7 @@ after_devdst:
082114
 	if (fw->arp.arpro_mask != 0) {
082114
 		int tmp = ntohs(fw->arp.arpro);
082114
 
082114
-		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPPRO
082114
+		printf("%s%s", sep, fw->arp.invflags & IPT_INV_PROTO
082114
 			? "! " : "");
082114
 		if (tmp == 0x0800 && !(format & FMT_NUMERIC))
082114
 			printf("--proto-type %s", "IPv4");
082114
diff --git a/iptables/nft-arp.h b/iptables/nft-arp.h
082114
index 3411fc3d7c7b3..0d93a31f563b1 100644
082114
--- a/iptables/nft-arp.h
082114
+++ b/iptables/nft-arp.h
082114
@@ -4,4 +4,11 @@
082114
 extern char *arp_opcodes[];
082114
 #define NUMOPCODES 9
082114
 
082114
+/* define invflags which won't collide with IPT ones */
082114
+#define IPT_INV_SRCDEVADDR	0x0080
082114
+#define IPT_INV_TGTDEVADDR	0x0100
082114
+#define IPT_INV_ARPHLN		0x0200
082114
+#define IPT_INV_ARPOP		0x0400
082114
+#define IPT_INV_ARPHRD		0x0800
082114
+
082114
 #endif
082114
diff --git a/iptables/xtables-arp.c b/iptables/xtables-arp.c
082114
index c8196f08baa59..3f96cb22c3ec0 100644
082114
--- a/iptables/xtables-arp.c
082114
+++ b/iptables/xtables-arp.c
082114
@@ -113,22 +113,22 @@ struct xtables_globals arptables_globals = {
082114
 static int inverse_for_options[] =
082114
 {
082114
 /* -n */ 0,
082114
-/* -s */ ARPT_INV_SRCIP,
082114
-/* -d */ ARPT_INV_TGTIP,
082114
+/* -s */ IPT_INV_SRCIP,
082114
+/* -d */ IPT_INV_DSTIP,
082114
 /* -p */ 0,
082114
 /* -j */ 0,
082114
 /* -v */ 0,
082114
 /* -x */ 0,
082114
-/* -i */ ARPT_INV_VIA_IN,
082114
-/* -o */ ARPT_INV_VIA_OUT,
082114
+/* -i */ IPT_INV_VIA_IN,
082114
+/* -o */ IPT_INV_VIA_OUT,
082114
 /*--line*/ 0,
082114
 /* -c */ 0,
082114
-/* 2 */ ARPT_INV_SRCDEVADDR,
082114
-/* 3 */ ARPT_INV_TGTDEVADDR,
082114
-/* -l */ ARPT_INV_ARPHLN,
082114
-/* 4 */ ARPT_INV_ARPOP,
082114
-/* 5 */ ARPT_INV_ARPHRD,
082114
-/* 6 */ ARPT_INV_ARPPRO,
082114
+/* 2 */ IPT_INV_SRCDEVADDR,
082114
+/* 3 */ IPT_INV_TGTDEVADDR,
082114
+/* -l */ IPT_INV_ARPHLN,
082114
+/* 4 */ IPT_INV_ARPOP,
082114
+/* 5 */ IPT_INV_ARPHRD,
082114
+/* 6 */ IPT_INV_PROTO,
082114
 };
082114
 
082114
 /***********************************************/
082114
@@ -903,7 +903,7 @@ int do_commandarp(struct nft_handle *h, int argc, char *argv[], char **table,
082114
 					 &dmasks, &ndaddrs);
082114
 
082114
 	if ((nsaddrs > 1 || ndaddrs > 1) &&
082114
-	    (cs.arp.arp.invflags & (ARPT_INV_SRCIP | ARPT_INV_TGTIP)))
082114
+	    (cs.arp.arp.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
082114
 		xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
082114
 				" source or destination IP addresses");
082114
 
082114
-- 
082114
2.31.1
082114