Skip to contentSkip to conversation

nft: prefer meta pkttype to libxt_pkttype

PATCH In review Florian Westphal 2 participants 6 messages Sep 8, 2022 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover nft: prefer meta pkttype to libxt_pkttype

low hanging fruit: use native meta+cmp instead of libxt_pkttype. First patch adds dissection, second patch switches to native expression.

Last patch adds support for 'otherhost' mnemonic, useless for iptables but useful for ebtables.

author maintainer
#1 lore raw

PATCH 1/3 nft: support dissection of meta pkktype mode

Makes iptables-nft-save dump 'nft meta pkttype' rules.

---
 iptables/nft-shared.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
iptables/nft-shared.c +25 -0
index 74e19ccad226..79c93fe82c60 100644
@@ -25,6 +25,7 @@
 #include <linux/netfilter/xt_limit.h>
 #include <linux/netfilter/xt_NFLOG.h>
 #include <linux/netfilter/xt_mark.h>
+#include <linux/netfilter/xt_pkttype.h>
 
 #include <libmnl/libmnl.h>
 #include <libnftnl/rule.h>
@@ -323,6 +324,27 @@ static int parse_meta_mark(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
 	return 0;
 }
 
+static int parse_meta_pkttype(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
+{
+	struct xt_pkttype_info *pkttype;
+	struct xtables_match *match;
+	uint8_t value;
+
+	match = nft_create_match(ctx, ctx->cs, "pkttype");
+	if (!match)
+		return -1;
+
+	pkttype = (void*)match->m->data;
+
+	if (nftnl_expr_get_u32(e, NFTNL_EXPR_CMP_OP) == NFT_CMP_NEQ)
+		pkttype->invert = 1;
+
+	value = nftnl_expr_get_u8(e, NFTNL_EXPR_CMP_DATA);
+	pkttype->pkttype = value;
+
+	return 0;
+}
+
 int parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e, uint8_t key,
 	       char *iniface, unsigned char *iniface_mask,
 	       char *outiface, unsigned char *outiface_mask, uint8_t *invflags)
@@ -369,6 +391,9 @@ int parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e, uint8_t key,
 	case NFT_META_MARK:
 		parse_meta_mark(ctx, e);
 		break;
+	case NFT_META_PKTTYPE:
+		parse_meta_pkttype(ctx, e);
+		break;
 	default:
 		return -1;
 	}
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 2/3 nft: prefer native 'meta pkttype' instead of xt match

In reply to Florian Westphal
---
 iptables/nft.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
iptables/nft.c +22 -0
index ee003511ab7f..f122075db2b2 100644
@@ -41,6 +41,7 @@
 #include <linux/netfilter/xt_limit.h>
 #include <linux/netfilter/xt_NFLOG.h>
 #include <linux/netfilter/xt_mark.h>
+#include <linux/netfilter/xt_pkttype.h>
 
 #include <libmnl/libmnl.h>
 #include <libnftnl/gen.h>
@@ -1445,6 +1446,25 @@ static int add_nft_mark(struct nft_handle *h, struct nftnl_rule *r,
 	return 0;
 }
 
+static int add_nft_pkttype(struct nft_handle *h, struct nftnl_rule *r,
+			   struct xt_entry_match *m)
+{
+	struct xt_pkttype_info *pkti = (void *)m->data;
+	uint8_t reg;
+	int op;
+
+	add_meta(h, r, NFT_META_PKTTYPE, &reg);
+
+	if (pkti->invert)
+		op = NFT_CMP_NEQ;
+	else
+		op = NFT_CMP_EQ;
+
+	add_cmp_u8(r, pkti->pkttype, op, reg);
+
+	return 0;
+}
+
 int add_match(struct nft_handle *h,
 	      struct nftnl_rule *r, struct xt_entry_match *m)
 {
@@ -1461,6 +1481,8 @@ int add_match(struct nft_handle *h,
 		return add_nft_tcp(h, r, m);
 	else if (!strcmp(m->u.user.name, "mark"))
 		return add_nft_mark(h, r, m);
+	else if (!strcmp(m->u.user.name, "pkttype"))
+		return add_nft_pkttype(h, r, m);
 
 	expr = nftnl_expr_alloc("match");
 	if (expr == NULL)
Signature · 1 line
2.35.1
author maintainer
#3 lore raw

PATCH 3/3 extensions: libxt_pkttype: support otherhost

In reply to Florian Westphal

Makes no sense for iptables/ip6tables but it does make sense for ebtables. Classic ebtables uses libebt_pkttype which isn't compatible, but iptables-nft can use the libxt_pkttype version when printing native 'meta pkttype'.

---
 extensions/libxt_pkttype.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
extensions/libxt_pkttype.c +1 -1
index bf6f5b960662..a76310b053b4 100644
@@ -30,8 +30,8 @@ static const struct pkttypes supported_types[] = {
 	{"unicast", PACKET_HOST, 1, "to us"},
 	{"broadcast", PACKET_BROADCAST, 1, "to all"},
 	{"multicast", PACKET_MULTICAST, 1, "to group"},
-/*
 	{"otherhost", PACKET_OTHERHOST, 1, "to someone else"},
+/*
 	{"outgoing", PACKET_OUTGOING, 1, "outgoing of any type"},
 */
 	/* aliases */
Signature · 1 line
2.35.1
reviewer Reviewed-by on the v1 series
#4 lore raw
In reply to Florian Westphal
Quoted 5 lines · Florian Westphal

low hanging fruit: use native meta+cmp instead of libxt_pkttype. First patch adds dissection, second patch switches to native expression.

Last patch adds support for 'otherhost' mnemonic, useless for iptables but useful for ebtables.

author maintainer
#5 lore raw
Quoted 8 lines · Phil Sutter
Quoted 5 lines · Florian Westphal

low hanging fruit: use native meta+cmp instead of libxt_pkttype. First patch adds dissection, second patch switches to native expression.

Last patch adds support for 'otherhost' mnemonic, useless for iptables but useful for ebtables.

Thanks for the quick review, applied.