Skip to contentSkip to conversation

unify ether type and meta protocol decoding

RFC In review Florian Westphal 2 participants 5 messages Nov 30, 2022 – Dec 20, 2022 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

RFC 1/1 unify ether type and meta protocol decoding

Handle "ether protocol" and "meta protcol" the same.

Problem is that this breaks the test case *again*:

I: [EXECUTING] iptables/tests/shell/testcases/ebtables/0006-flush_0
--A FORWARD --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT --A OUTPUT --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

+-A FORWARD -p IPv4 --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT
+-A OUTPUT -p IPv4 --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

... because ebtables-nft will now render meta protocol as "-p IPv4".

ebtables-legacy does not have any special handling for this.

Solving this would need more internal annotations during decode, so we can suppress/ignore "meta protocol" once a "among-type" set is encountered.

Any (other) suggestions?

---
 iptables/nft-bridge.c | 74 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 13 deletions(-)
iptables/nft-bridge.c +61 -13
index 50e90b22cf2f..4488ff172c2e 100644
@@ -188,6 +188,64 @@ static int nft_bridge_add(struct nft_handle *h,
 	return _add_action(r, cs);
 }
 
+static bool nft_bridge_parse_ethproto(struct nft_xt_ctx *ctx,
+				      struct nftnl_expr *e,
+				      struct iptables_command_state *cs)
+{
+	struct ebt_entry *fw = &cs->eb;
+	bool already_seen;
+	uint16_t ethproto;
+	uint8_t op;
+
+	already_seen = (fw->bitmask & EBT_NOPROTO) == 0;
+
+	__get_cmp_data(e, &ethproto, sizeof(ethproto), &op);
+
+	switch (op) {
+	case NFT_CMP_EQ:
+		if (already_seen && fw->invflags & EBT_IPROTO) {
+			ctx->errmsg = "ethproto eq test contradicts previous";
+			return false;
+		}
+		break;
+	case NFT_CMP_NEQ:
+		if (already_seen && (fw->invflags & EBT_IPROTO) == 0) {
+			ctx->errmsg = "ethproto ne test contradicts previous";
+			return false;
+		}
+		fw->invflags |= EBT_IPROTO;
+		break;
+	case NFT_CMP_GTE:
+		if (already_seen && (fw->invflags & EBT_IPROTO) == 0) {
+			ctx->errmsg = "ethproto gte test contradicts previous";
+			return false;
+		}
+		fw->invflags |= EBT_IPROTO;
+		/* fallthrough */
+	case NFT_CMP_LT:
+		/* -p Length mode */
+		if (ethproto == htons(0x0600))
+			fw->bitmask |= EBT_802_3;
+		break;
+	default:
+		ctx->errmsg = "ethproto only supports eq/ne test";
+		return false;
+	}
+
+	if (already_seen) {
+		if (fw->ethproto != ethproto) {
+			ctx->errmsg = "ethproto ne test contradicts previous";
+			return false;
+		}
+	} else if ((fw->bitmask & EBT_802_3) == 0) {
+		fw->ethproto = ethproto;
+	}
+
+	fw->bitmask &= ~EBT_NOPROTO;
+
+	return true;
+}
+
 static void nft_bridge_parse_meta(struct nft_xt_ctx *ctx,
 				  const struct nft_xt_ctx_reg *reg,
 				  struct nftnl_expr *e,
@@ -199,6 +257,7 @@ static void nft_bridge_parse_meta(struct nft_xt_ctx *ctx,
 
 	switch (reg->meta_dreg.key) {
 	case NFT_META_PROTOCOL:
+		nft_bridge_parse_ethproto(ctx, e, cs);
 		return;
 	}
 
@@ -241,8 +300,6 @@ static void nft_bridge_parse_payload(struct nft_xt_ctx *ctx,
 {
 	struct ebt_entry *fw = &cs->eb;
 	unsigned char addr[ETH_ALEN];
-	unsigned short int ethproto;
-	uint8_t op;
 	bool inv;
 	int i;
 
@@ -275,17 +332,8 @@ static void nft_bridge_parse_payload(struct nft_xt_ctx *ctx,
 		fw->bitmask |= EBT_ISOURCE;
 		break;
 	case offsetof(struct ethhdr, h_proto):
-		__get_cmp_data(e, &ethproto, sizeof(ethproto), &op);
-		if (ethproto == htons(0x0600)) {
-			fw->bitmask |= EBT_802_3;
-			inv = (op == NFT_CMP_GTE);
-		} else {
-			fw->ethproto = ethproto;
-			inv = (op == NFT_CMP_NEQ);
-		}
-		if (inv)
-			fw->invflags |= EBT_IPROTO;
-		fw->bitmask &= ~EBT_NOPROTO;
+		if (!nft_bridge_parse_ethproto(ctx, e, cs))
+			return;
 		break;
 	}
 }
Signature · 1 line
2.38.1
reviewer
#1 lore raw
Quoted 12 lines · Florian Westphal

Handle "ether protocol" and "meta protcol" the same.

Problem is that this breaks the test case *again*:

I: [EXECUTING] iptables/tests/shell/testcases/ebtables/0006-flush_0
--A FORWARD --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT --A OUTPUT --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

+-A FORWARD -p IPv4 --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT
+-A OUTPUT -p IPv4 --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

... because ebtables-nft will now render meta protocol as "-p IPv4".

Quoted 7 lines

ebtables-legacy does not have any special handling for this.

Solving this would need more internal annotations during decode, so we can suppress/ignore "meta protocol" once a "among-type" set is encountered.

Any (other) suggestions?

Since ebtables among does not support IPv6, match elimination should be pretty simple, no? Entirely untested:

iptables/nft-bridge.c +4 -0
index 08c93feeba2c9..0daebfd983127 100644
@@ -520,6 +520,10 @@ static void nft_bridge_parse_lookup(struct nft_xt_ctx *ctx,
        if (set_elems_to_among_pairs(among_data->pairs + poff, s, cnt))
                xtables_error(OTHER_PROBLEM,
                              "ebtables among pair parsing failed");
+
+       if (!(ctx->cs.eb.bitmask & EBT_NOPROTO) &&
+           ctx->cs.eb.ethproto == htons(0x0800))
+               ctx->cs.eb.bitmask |= EBT_NOPROTO;
 }
 
 static void parse_watcher(void *object, struct ebt_match **match_list,

Cheers, Phil

author maintainer
#2 lore raw
Quoted 29 lines · Phil Sutter
Quoted 12 lines · Florian Westphal

Handle "ether protocol" and "meta protcol" the same.

Problem is that this breaks the test case *again*:

I: [EXECUTING] iptables/tests/shell/testcases/ebtables/0006-flush_0
--A FORWARD --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT --A OUTPUT --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

+-A FORWARD -p IPv4 --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT
+-A OUTPUT -p IPv4 --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

... because ebtables-nft will now render meta protocol as "-p IPv4".

Quoted 7 lines

ebtables-legacy does not have any special handling for this.

Solving this would need more internal annotations during decode, so we can suppress/ignore "meta protocol" once a "among-type" set is encountered.

Any (other) suggestions?

Since ebtables among does not support IPv6, match elimination should be pretty simple, no? Entirely untested:

iptables/nft-bridge.c +0 -0
index 08c93feeba2c9..0daebfd983127 100644

iptables/nft-bridge.c static void nft_bridge_parse_lookup(struct nft_xt_ctx *ctx, · line 526

Quoted 7 lines
iptables/nft-bridge.c +4 -0
@@ -520,6 +520,10 @@ static void nft_bridge_parse_lookup(struct nft_xt_ctx *ctx,
        if (set_elems_to_among_pairs(among_data->pairs + poff, s, cnt))
                xtables_error(OTHER_PROBLEM,
                              "ebtables among pair parsing failed");
+
+       if (!(ctx->cs.eb.bitmask & EBT_NOPROTO) &&
+           ctx->cs.eb.ethproto == htons(0x0800))
+               ctx->cs.eb.bitmask |= EBT_NOPROTO;

But that would munge
ebtables-nft -p ipv4 ....
ebtables-nft ....

We don't know if "-p" was added explicitly or not.

reviewer
#3 lore raw
Quoted 42 lines · Florian Westphal
Quoted 29 lines · Phil Sutter
Quoted 12 lines · Florian Westphal

Handle "ether protocol" and "meta protcol" the same.

Problem is that this breaks the test case *again*:

I: [EXECUTING] iptables/tests/shell/testcases/ebtables/0006-flush_0
--A FORWARD --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT --A OUTPUT --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

+-A FORWARD -p IPv4 --among-dst fe:ed:ba:be:13:37=10.0.0.1 -j ACCEPT
+-A OUTPUT -p IPv4 --among-src c0:ff:ee:90:0:0=192.168.0.1 -j DROP

... because ebtables-nft will now render meta protocol as "-p IPv4".

Quoted 7 lines

ebtables-legacy does not have any special handling for this.

Solving this would need more internal annotations during decode, so we can suppress/ignore "meta protocol" once a "among-type" set is encountered.

Any (other) suggestions?

Since ebtables among does not support IPv6, match elimination should be pretty simple, no? Entirely untested:

iptables/nft-bridge.c +0 -0
index 08c93feeba2c9..0daebfd983127 100644

iptables/nft-bridge.c static void nft_bridge_parse_lookup(struct nft_xt_ctx *ctx, · line 526

Quoted 7 lines
iptables/nft-bridge.c +4 -0
@@ -520,6 +520,10 @@ static void nft_bridge_parse_lookup(struct nft_xt_ctx *ctx,
        if (set_elems_to_among_pairs(among_data->pairs + poff, s, cnt))
                xtables_error(OTHER_PROBLEM,
                              "ebtables among pair parsing failed");
+
+       if (!(ctx->cs.eb.bitmask & EBT_NOPROTO) &&
+           ctx->cs.eb.ethproto == htons(0x0800))
+               ctx->cs.eb.bitmask |= EBT_NOPROTO;

But that would munge
ebtables-nft -p ipv4 ....
ebtables-nft ....

Quoted 1 line

We don't know if "-p" was added explicitly or not.

Ah, the infamous explicit vs. implicit problem. :(

Looking at ebt_among.c in kernel, it seems packets which are neither IPv4 nor ARP are treated as non-matching. Since ebtables-nft doesn't support ARP with among anyway, can we assume people will not specify '-p ipv4' when using among?

Cheers, Phil

reviewer
#4 lore raw

RFC 1/1 ebtables: among: Embed meta protocol match into set lookup

In reply to Florian Westphal

This way the match will be consumed by among match parser and not confused with explicit '-p IPv4' match.

---
This is a bit of a hack to avoid the implicit vs. explicit confusion but it works and is backwards compatible. WDYT?

---
 extensions/libebt_among.c |  1 +
 iptables/nft-bridge.c     | 65 ++++++++++++++++++++++++---------------
 iptables/nft-bridge.h     |  1 +
 iptables/nft.c            | 30 ++++++++++++------
 4 files changed, 62 insertions(+), 35 deletions(-)
extensions/libebt_among.c +1 -0
index a80fb80404ee1..49580ea6d3fc2 100644
@@ -69,6 +69,7 @@ parse_nft_among_pair(char *buf, struct nft_among_pair *pair, bool have_ip)
 		if (!inet_pton(AF_INET, sep + 1, &pair->in))
 			xtables_error(PARAMETER_PROBLEM,
 				      "Invalid IP address '%s'", sep + 1);
+		pair->proto = htons(ETH_P_IP);
 	}
 	ether = ether_aton(buf);
 	if (!ether)
iptables/nft-bridge.c +40 -25

102 lines not on this page · raw message

iptables/nft-bridge.h +1 -0
index eb1b3928b6543..c87065ef48195 100644
@@ -125,6 +125,7 @@ int ebt_command_default(struct iptables_command_state *cs);
 struct nft_among_pair {
 	struct ether_addr ether;
 	struct in_addr in __attribute__((aligned (4)));
+	uint16_t proto __attribute__((aligned (4)));
 };
 
 struct nft_among_data {
iptables/nft.c +20 -10
index 430888e864a5f..05422c91f3827 100644
@@ -1139,6 +1139,7 @@ gen_lookup(uint32_t sreg, const char *set_name, uint32_t set_id, uint32_t flags)
 /* from nftables:include/datatype.h, enum datatypes */
 #define NFT_DATATYPE_IPADDR	7
 #define NFT_DATATYPE_ETHERADDR	9
+#define NFT_DATATYPE_ETHERTYPE	10
 
 static int __add_nft_among(struct nft_handle *h, const char *table,
 			   struct nftnl_rule *r, struct nft_among_pair *pairs,
@@ -1164,6 +1165,11 @@ static int __add_nft_among(struct nft_handle *h, const char *table,
 		type = type << CONCAT_TYPE_BITS | NFT_DATATYPE_IPADDR;
 		len += sizeof(struct in_addr) + NETLINK_ALIGN - 1;
 		len &= ~(NETLINK_ALIGN - 1);
+
+		type = type << CONCAT_TYPE_BITS | NFT_DATATYPE_ETHERTYPE;
+		len += sizeof(uint16_t) + NETLINK_ALIGN - 1;
+		len &= ~(NETLINK_ALIGN - 1);
+
 		flags = NFT_SET_INTERVAL | NFT_SET_CONCAT;
 	}
 
@@ -1173,7 +1179,11 @@ static int __add_nft_among(struct nft_handle *h, const char *table,
 	set_id = nftnl_set_get_u32(s, NFTNL_SET_ID);
 
 	if (ip) {
-		uint8_t field_len[2] = { ETH_ALEN, sizeof(struct in_addr) };
+		uint8_t field_len[] = {
+			ETH_ALEN,
+			sizeof(struct in_addr),
+			sizeof(uint16_t),
+		};
 
 		nftnl_set_set_data(s, NFTNL_SET_DESC_CONCAT,
 				   field_len, sizeof(field_len));
@@ -1211,6 +1221,15 @@ static int __add_nft_among(struct nft_handle *h, const char *table,
 		if (!e)
 			return -ENOMEM;
 		nftnl_rule_add_expr(r, e);
+
+		/* FIXME: Fix add_meta() instead to accept a reg */
+		reg = nft_get_next_reg(reg, sizeof(struct in_addr));
+		e = nftnl_expr_alloc("meta");
+		if (!e)
+			return -ENOMEM;
+		nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, NFT_META_PROTOCOL);
+		nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, reg);
+		nftnl_rule_add_expr(r, e);
 	}
 
 	reg = NFT_REG_1;
@@ -1228,15 +1247,6 @@ static int add_nft_among(struct nft_handle *h,
 	struct nft_among_data *data = (struct nft_among_data *)m->data;
 	const char *table = nftnl_rule_get(r, NFTNL_RULE_TABLE);
 
-	if ((data->src.cnt && data->src.ip) ||
-	    (data->dst.cnt && data->dst.ip)) {
-		uint16_t eth_p_ip = htons(ETH_P_IP);
-		uint8_t reg;
-
-		add_meta(h, r, NFT_META_PROTOCOL, &reg);
-		add_cmp_ptr(r, NFT_CMP_EQ, &eth_p_ip, 2, reg);
-	}
-
 	if (data->src.cnt)
 		__add_nft_among(h, table, r, data->pairs, data->src.cnt,
 				false, data->src.inv, data->src.ip);
Signature · 1 line
2.38.0