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

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