Blame SOURCES/0078-ebtables-Support-p-Length.patch

c89b2a
From 76c798f2f0021aaf740fa6cb90833e56130f7064 Mon Sep 17 00:00:00 2001
c89b2a
From: Phil Sutter <phil@nwl.cc>
c89b2a
Date: Tue, 27 Sep 2022 23:19:34 +0200
c89b2a
Subject: [PATCH] ebtables: Support '-p Length'
c89b2a
c89b2a
To match on Ethernet frames using the etherproto field as length value,
c89b2a
ebtables accepts the special protocol name "LENGTH". Implement this in
c89b2a
ebtables-nft using a native match for 'ether type < 0x0600'.
c89b2a
c89b2a
Since extension 802_3 matches are valid only with such Ethernet frames,
c89b2a
add a local add_match() wrapper which complains if the extension is used
c89b2a
without '-p Length' parameter. Legacy ebtables does this within the
c89b2a
extension's final_check callback, but it's not possible here due for lack of
c89b2a
fw->bitmask field access.
c89b2a
c89b2a
While being at it, add xlate support, adjust tests and make ebtables-nft
c89b2a
print the case-insensitive argument with capital 'L' like legacy
c89b2a
ebtables does.
c89b2a
c89b2a
Signed-off-by: Phil Sutter <phil@nwl.cc>
c89b2a
(cherry picked from commit aa0b8b03f7c7e741ccd96360bd64d90ea8c3c3aa)
c89b2a
c89b2a
Conflicts:
c89b2a
	iptables/nft-bridge.c
c89b2a
- Adjusted to missing commit 7e38890c6b4fb
c89b2a
  ("nft: prepare for dynamic register allocation")
c89b2a
- Context change due to missing commit 0c0cd4340ed88
c89b2a
  ("nft: Don't pass command state opaque to family ops callbacks")
c89b2a
---
c89b2a
 extensions/generic.txlate                     |  6 +++
c89b2a
 extensions/libebt_802_3.t                     |  6 ++-
c89b2a
 iptables/nft-bridge.c                         | 47 +++++++++++++++----
c89b2a
 .../ebtables/0002-ebtables-save-restore_0     | 12 ++---
c89b2a
 4 files changed, 53 insertions(+), 18 deletions(-)
c89b2a
c89b2a
diff --git a/extensions/generic.txlate b/extensions/generic.txlate
c89b2a
index 9ae9a5b54c1b9..6779d6f86dec8 100644
c89b2a
--- a/extensions/generic.txlate
c89b2a
+++ b/extensions/generic.txlate
c89b2a
@@ -67,6 +67,12 @@ nft add rule bridge filter FORWARD iifname != "iname" meta ibrname "ilogname" oi
c89b2a
 ebtables-translate -I INPUT -p ip -d 1:2:3:4:5:6/ff:ff:ff:ff:00:00
c89b2a
 nft insert rule bridge filter INPUT ether type 0x800 ether daddr 01:02:03:04:00:00 and ff:ff:ff:ff:00:00 == 01:02:03:04:00:00 counter
c89b2a
 
c89b2a
+ebtables-translate -I INPUT -p Length
c89b2a
+nft insert rule bridge filter INPUT ether type < 0x0600 counter
c89b2a
+
c89b2a
+ebtables-translate -I INPUT -p ! Length
c89b2a
+nft insert rule bridge filter INPUT ether type >= 0x0600 counter
c89b2a
+
c89b2a
 # asterisk is not special in iptables and it is even a valid interface name
c89b2a
 iptables-translate -A FORWARD -i '*' -o 'eth*foo'
c89b2a
 nft add rule ip filter FORWARD iifname "\*" oifname "eth\*foo" counter
c89b2a
diff --git a/extensions/libebt_802_3.t b/extensions/libebt_802_3.t
c89b2a
index ddfb2f0a72baf..a138f35d2c756 100644
c89b2a
--- a/extensions/libebt_802_3.t
c89b2a
+++ b/extensions/libebt_802_3.t
c89b2a
@@ -1,3 +1,5 @@
c89b2a
 :INPUT,FORWARD,OUTPUT
c89b2a
---802_3-sap ! 0x0a -j CONTINUE;=;OK
c89b2a
---802_3-type 0x000a -j RETURN;=;OK
c89b2a
+--802_3-sap ! 0x0a -j CONTINUE;=;FAIL
c89b2a
+--802_3-type 0x000a -j RETURN;=;FAIL
c89b2a
+-p Length --802_3-sap ! 0x0a -j CONTINUE;=;OK
c89b2a
+-p Length --802_3-type 0x000a -j RETURN;=;OK
c89b2a
diff --git a/iptables/nft-bridge.c b/iptables/nft-bridge.c
c89b2a
index 3fd03fb8de4ff..176304e9f2f23 100644
c89b2a
--- a/iptables/nft-bridge.c
c89b2a
+++ b/iptables/nft-bridge.c
c89b2a
@@ -129,6 +129,18 @@ static int _add_action(struct nftnl_rule *r, struct iptables_command_state *cs)
c89b2a
 	return add_action(r, cs, false);
c89b2a
 }
c89b2a
 
c89b2a
+static int
c89b2a
+nft_bridge_add_match(struct nft_handle *h, const struct ebt_entry *fw,
c89b2a
+		     struct nftnl_rule *r, struct xt_entry_match *m)
c89b2a
+{
c89b2a
+	if (!strcmp(m->u.user.name, "802_3") &&
c89b2a
+	    !(fw->bitmask & EBT_802_3))
c89b2a
+		xtables_error(PARAMETER_PROBLEM,
c89b2a
+			      "For 802.3 DSAP/SSAP filtering the protocol must be LENGTH");
c89b2a
+
c89b2a
+	return add_match(h, r, m);
c89b2a
+}
c89b2a
+
c89b2a
 static int nft_bridge_add(struct nft_handle *h,
c89b2a
 			  struct nftnl_rule *r, void *data)
c89b2a
 {
c89b2a
@@ -172,17 +184,25 @@ static int nft_bridge_add(struct nft_handle *h,
c89b2a
 	}
c89b2a
 
c89b2a
 	if ((fw->bitmask & EBT_NOPROTO) == 0) {
c89b2a
+		uint16_t ethproto = fw->ethproto;
c89b2a
+
c89b2a
 		op = nft_invflags2cmp(fw->invflags, EBT_IPROTO);
c89b2a
 		add_payload(r, offsetof(struct ethhdr, h_proto), 2,
c89b2a
 			    NFT_PAYLOAD_LL_HEADER);
c89b2a
-		add_cmp_u16(r, fw->ethproto, op);
c89b2a
+
c89b2a
+		if (fw->bitmask & EBT_802_3) {
c89b2a
+			op = (op == NFT_CMP_EQ ? NFT_CMP_LT : NFT_CMP_GTE);
c89b2a
+			ethproto = htons(0x0600);
c89b2a
+		}
c89b2a
+
c89b2a
+		add_cmp_u16(r, ethproto, op);
c89b2a
 	}
c89b2a
 
c89b2a
 	add_compat(r, fw->ethproto, fw->invflags & EBT_IPROTO);
c89b2a
 
c89b2a
 	for (iter = cs->match_list; iter; iter = iter->next) {
c89b2a
 		if (iter->ismatch) {
c89b2a
-			if (add_match(h, r, iter->u.match->m))
c89b2a
+			if (nft_bridge_add_match(h, fw, r, iter->u.match->m))
c89b2a
 				break;
c89b2a
 		} else {
c89b2a
 			if (add_target(r, iter->u.watcher->t))
c89b2a
@@ -239,6 +259,7 @@ static void nft_bridge_parse_payload(struct nft_xt_ctx *ctx,
c89b2a
 	struct ebt_entry *fw = &cs->eb;
c89b2a
 	unsigned char addr[ETH_ALEN];
c89b2a
 	unsigned short int ethproto;
c89b2a
+	uint8_t op;
c89b2a
 	bool inv;
c89b2a
 	int i;
c89b2a
 
c89b2a
@@ -275,8 +296,14 @@ static void nft_bridge_parse_payload(struct nft_xt_ctx *ctx,
c89b2a
 		fw->bitmask |= EBT_ISOURCE;
c89b2a
 		break;
c89b2a
 	case offsetof(struct ethhdr, h_proto):
c89b2a
-		get_cmp_data(e, &ethproto, sizeof(ethproto), &inv;;
c89b2a
-		fw->ethproto = ethproto;
c89b2a
+		__get_cmp_data(e, &ethproto, sizeof(ethproto), &op);
c89b2a
+		if (ethproto == htons(0x0600)) {
c89b2a
+			fw->bitmask |= EBT_802_3;
c89b2a
+			inv = (op == NFT_CMP_GTE);
c89b2a
+		} else {
c89b2a
+			fw->ethproto = ethproto;
c89b2a
+			inv = (op == NFT_CMP_NEQ);
c89b2a
+		}
c89b2a
 		if (inv)
c89b2a
 			fw->invflags |= EBT_IPROTO;
c89b2a
 		fw->bitmask &= ~EBT_NOPROTO;
c89b2a
@@ -611,7 +638,7 @@ static void print_protocol(uint16_t ethproto, bool invert, unsigned int bitmask)
c89b2a
 		printf("! ");
c89b2a
 
c89b2a
 	if (bitmask & EBT_802_3) {
c89b2a
-		printf("length ");
c89b2a
+		printf("Length ");
c89b2a
 		return;
c89b2a
 	}
c89b2a
 
c89b2a
@@ -626,7 +653,7 @@ static void nft_bridge_save_rule(const void *data, unsigned int format)
c89b2a
 {
c89b2a
 	const struct iptables_command_state *cs = data;
c89b2a
 
c89b2a
-	if (cs->eb.ethproto)
c89b2a
+	if (!(cs->eb.bitmask & EBT_NOPROTO))
c89b2a
 		print_protocol(cs->eb.ethproto, cs->eb.invflags & EBT_IPROTO,
c89b2a
 			       cs->eb.bitmask);
c89b2a
 	if (cs->eb.bitmask & EBT_ISOURCE)
c89b2a
@@ -892,7 +919,10 @@ static int nft_bridge_xlate(const void *data, struct xt_xlate *xl)
c89b2a
 	xlate_ifname(xl, "meta obrname", cs->eb.logical_out,
c89b2a
 		     cs->eb.invflags & EBT_ILOGICALOUT);
c89b2a
 
c89b2a
-	if ((cs->eb.bitmask & EBT_NOPROTO) == 0) {
c89b2a
+	if (cs->eb.bitmask & EBT_802_3) {
c89b2a
+		xt_xlate_add(xl, "ether type %s 0x0600 ",
c89b2a
+			     cs->eb.invflags & EBT_IPROTO ? ">=" : "<");
c89b2a
+	} else if ((cs->eb.bitmask & EBT_NOPROTO) == 0) {
c89b2a
 		const char *implicit = NULL;
c89b2a
 
c89b2a
 		switch (ntohs(cs->eb.ethproto)) {
c89b2a
@@ -915,9 +945,6 @@ static int nft_bridge_xlate(const void *data, struct xt_xlate *xl)
c89b2a
 				     ntohs(cs->eb.ethproto));
c89b2a
 	}
c89b2a
 
c89b2a
-	if (cs->eb.bitmask & EBT_802_3)
c89b2a
-		return 0;
c89b2a
-
c89b2a
 	if (cs->eb.bitmask & EBT_ISOURCE)
c89b2a
 		nft_bridge_xlate_mac(xl, "saddr", cs->eb.invflags & EBT_ISOURCE,
c89b2a
 				     cs->eb.sourcemac, cs->eb.sourcemsk);
c89b2a
diff --git a/iptables/tests/shell/testcases/ebtables/0002-ebtables-save-restore_0 b/iptables/tests/shell/testcases/ebtables/0002-ebtables-save-restore_0
c89b2a
index b84f63a7c3672..a4fc31548e323 100755
c89b2a
--- a/iptables/tests/shell/testcases/ebtables/0002-ebtables-save-restore_0
c89b2a
+++ b/iptables/tests/shell/testcases/ebtables/0002-ebtables-save-restore_0
c89b2a
@@ -13,8 +13,8 @@ $XT_MULTI ebtables -A INPUT -p IPv4 -i lo -j ACCEPT
c89b2a
 $XT_MULTI ebtables -P FORWARD DROP
c89b2a
 $XT_MULTI ebtables -A OUTPUT -s ff:ff:ff:ff:ff:ff/ff:ff:ff:ff:ff:ff -j DROP
c89b2a
 $XT_MULTI ebtables -N foo
c89b2a
-$XT_MULTI ebtables -A foo --802_3-sap 0x23 -j ACCEPT
c89b2a
-$XT_MULTI ebtables -A foo --802_3-sap 0xaa --802_3-type 0x1337 -j ACCEPT
c89b2a
+$XT_MULTI ebtables -A foo -p length --802_3-sap 0x23 -j ACCEPT
c89b2a
+$XT_MULTI ebtables -A foo -p length --802_3-sap 0xaa --802_3-type 0x1337 -j ACCEPT
c89b2a
 #$XT_MULTI ebtables -A foo --among-dst fe:ed:ba:be:00:01,fe:ed:ba:be:00:02,fe:ed:ba:be:00:03 -j ACCEPT
c89b2a
 $XT_MULTI ebtables -A foo -p ARP --arp-gratuitous -j ACCEPT
c89b2a
 $XT_MULTI ebtables -A foo -p ARP --arp-opcode Request -j ACCEPT
c89b2a
@@ -44,7 +44,7 @@ $XT_MULTI ebtables -A foo --pkttype-type multicast -j ACCEPT
c89b2a
 $XT_MULTI ebtables -A foo --stp-type config -j ACCEPT
c89b2a
 #$XT_MULTI ebtables -A foo --vlan-id 42 -j ACCEPT
c89b2a
 
c89b2a
-$XT_MULTI ebtables -A foo --802_3-sap 0x23 --limit 100 -j ACCEPT
c89b2a
+$XT_MULTI ebtables -A foo -p length --802_3-sap 0x23 --limit 100 -j ACCEPT
c89b2a
 $XT_MULTI ebtables -A foo --pkttype-type multicast --log
c89b2a
 $XT_MULTI ebtables -A foo --pkttype-type multicast --limit 100 -j ACCEPT
c89b2a
 
c89b2a
@@ -75,8 +75,8 @@ DUMP='*filter
c89b2a
 -A INPUT -p IPv4 -i lo -j ACCEPT
c89b2a
 -A FORWARD -j foo
c89b2a
 -A OUTPUT -s Broadcast -j DROP
c89b2a
--A foo --802_3-sap 0x23 -j ACCEPT
c89b2a
--A foo --802_3-sap 0xaa --802_3-type 0x1337 -j ACCEPT
c89b2a
+-A foo -p Length --802_3-sap 0x23 -j ACCEPT
c89b2a
+-A foo -p Length --802_3-sap 0xaa --802_3-type 0x1337 -j ACCEPT
c89b2a
 -A foo -p ARP --arp-gratuitous -j ACCEPT
c89b2a
 -A foo -p ARP --arp-op Request -j ACCEPT
c89b2a
 -A foo -p ARP --arp-ip-src 10.0.0.1 -j ACCEPT
c89b2a
@@ -96,7 +96,7 @@ DUMP='*filter
c89b2a
 -A foo --nflog-group 1 -j CONTINUE
c89b2a
 -A foo --pkttype-type multicast -j ACCEPT
c89b2a
 -A foo --stp-type config -j ACCEPT
c89b2a
--A foo --802_3-sap 0x23 --limit 100/sec --limit-burst 5 -j ACCEPT
c89b2a
+-A foo -p Length --802_3-sap 0x23 --limit 100/sec --limit-burst 5 -j ACCEPT
c89b2a
 -A foo --pkttype-type multicast --log-level notice --log-prefix "" -j CONTINUE
c89b2a
 -A foo --pkttype-type multicast --limit 100/sec --limit-burst 5 -j ACCEPT
c89b2a
 *nat
c89b2a
-- 
c89b2a
2.38.0
c89b2a