Skip to contentSkip to conversation

add ct expectation support

PATCH Superseded Stéphane Veyret 2 participants 3 messages May 23, 2019 – May 24, 2019

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover add ct expectation support

Please find the patch for kernel including suggested modifications.
I will send library and nft modifications when code review will be finished for kernel part. Thank you.

Stéphane Veyret (1):
netfilter: nft_ct: add ct expectations support

 include/uapi/linux/netfilter/nf_tables.h |  14 ++-
 net/netfilter/nft_ct.c                   | 145 ++++++++++++++++++++++-
 2 files changed, 156 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v4 1/1 netfilter: nft_ct: add ct expectations support

This patch allows to add, list and delete expectations via nft objref infrastructure and assigning these expectations via nft rule.

This allows manual port triggering when no helper is defined to manage a specific protocol. For example, if I have an online game which protocol is based on initial connection to TCP port 9753 of the server, and where the server opens a connection to port 9876, I can set rules as follow:

table ip filter {
    ct expectation mygame {
        protocol udp;
        dport 9876;
        timeout 2m;
        size 1;
    }

    chain input {
        type filter hook input priority 0; policy drop;
        tcp dport 9753 ct expectation set "mygame";
    }

    chain output {
        type filter hook output priority 0; policy drop;
        udp dport 9876 ct status expected accept;
    }
}
---
 include/uapi/linux/netfilter/nf_tables.h |  14 ++-
 net/netfilter/nft_ct.c                   | 145 ++++++++++++++++++++++-
 2 files changed, 156 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +13 -1
index 505393c6e959..31a6b8f7ff73 100644
@@ -1445,6 +1445,17 @@ enum nft_ct_timeout_timeout_attributes {
 };
 #define NFTA_CT_TIMEOUT_MAX	(__NFTA_CT_TIMEOUT_MAX - 1)
 
+enum nft_ct_expectation_attributes {
+	NFTA_CT_EXPECT_UNSPEC,
+	NFTA_CT_EXPECT_L3PROTO,
+	NFTA_CT_EXPECT_L4PROTO,
+	NFTA_CT_EXPECT_DPORT,
+	NFTA_CT_EXPECT_TIMEOUT,
+	NFTA_CT_EXPECT_SIZE,
+	__NFTA_CT_EXPECT_MAX,
+};
+#define NFTA_CT_EXPECT_MAX	(__NFTA_CT_EXPECT_MAX - 1)
+
 #define NFT_OBJECT_UNSPEC	0
 #define NFT_OBJECT_COUNTER	1
 #define NFT_OBJECT_QUOTA	2
@@ -1454,7 +1465,8 @@ enum nft_ct_timeout_timeout_attributes {
 #define NFT_OBJECT_TUNNEL	6
 #define NFT_OBJECT_CT_TIMEOUT	7
 #define NFT_OBJECT_SECMARK	8
-#define __NFT_OBJECT_MAX	9
+#define NFT_OBJECT_CT_EXPECT	9
+#define __NFT_OBJECT_MAX	10
 #define NFT_OBJECT_MAX		(__NFT_OBJECT_MAX - 1)
 
 /**
net/netfilter/nft_ct.c +143 -2

181 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH nf-next v4 1/1] netfilter: nft_ct: add ct expectations support

On Thu, May 23, 2019 at 09:22:11PM +0200, Stéphane Veyret wrote:
[...]

Quoted 13 lines
net/netfilter/nft_ct.c +1 -0
index f043936763f3..d072d3c8e6bc 100644
@@ -24,6 +24,7 @@
 #include <net/netfilter/nf_conntrack_labels.h>
 #include <net/netfilter/nf_conntrack_timeout.h>
 #include <net/netfilter/nf_conntrack_l4proto.h>
+#include <net/netfilter/nf_conntrack_expect.h>
 
 struct nft_ct {
 	enum nft_ct_keys	key:8;

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 799

Quoted 9 lines
net/netfilter/nft_ct.c +6 -0
@@ -790,6 +791,138 @@ static struct nft_expr_type nft_notrack_type __read_mostly = {
 	.owner		= THIS_MODULE,
 };
 
+struct nft_ct_expect_obj {
+	int		l3num;
+	u8		l4proto;
+	__be16		dport;
+	u32		timeout;
+	u8		size;

Nit: Reorder to punch out holes in the structure layout, and use appropriate datatypes:

	u16		l3num;
	__be16		dport;
	u8		l4proto;
	u8		size;
	u32		timeout;
Quoted 45 lines
net/netfilter/nft_ct.c +45 -0
+};
+
+static int nft_ct_expect_obj_init(const struct nft_ctx *ctx,
+				  const struct nlattr * const tb[],
+				  struct nft_object *obj)
+{
+	struct nft_ct_expect_obj *priv = nft_obj_data(obj);
+
+	if (!tb[NFTA_CT_EXPECT_L4PROTO] ||
+	    !tb[NFTA_CT_EXPECT_DPORT] ||
+	    !tb[NFTA_CT_EXPECT_TIMEOUT] ||
+	    !tb[NFTA_CT_EXPECT_SIZE])
+		return -EINVAL;
+
+	priv->l3num = ctx->family;
+	if (tb[NFTA_CT_EXPECT_L3PROTO])
+		priv->l3num = ntohs(nla_get_be16(tb[NFTA_CT_EXPECT_L3PROTO]));
+
+	priv->l4proto = nla_get_u8(tb[NFTA_CT_EXPECT_L4PROTO]);
+	priv->dport = nla_get_be16(tb[NFTA_CT_EXPECT_DPORT]);
+	priv->timeout = nla_get_u32(tb[NFTA_CT_EXPECT_TIMEOUT]);
+	priv->size = nla_get_u8(tb[NFTA_CT_EXPECT_SIZE]);
+
+	return nf_ct_netns_get(ctx->net, ctx->family);
+}
+
+static void nft_ct_expect_obj_destroy(const struct nft_ctx *ctx,
+				       struct nft_object *obj)
+{
+	nf_ct_netns_put(ctx->net, ctx->family);
+}
+
+static int nft_ct_expect_obj_dump(struct sk_buff *skb,
+				  struct nft_object *obj, bool reset)
+{
+	const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
+
+	if (nla_put_be16(skb, NFTA_CT_EXPECT_L3PROTO, htons(priv->l3num)) ||
+	    nla_put_u8(skb, NFTA_CT_EXPECT_L4PROTO, priv->l4proto) ||
+	    nla_put_be16(skb, NFTA_CT_EXPECT_DPORT, priv->dport) ||
+	    nla_put_u32(skb, NFTA_CT_EXPECT_TIMEOUT, priv->timeout) ||
+	    nla_put_u8(skb, NFTA_CT_EXPECT_SIZE, priv->size))
+		return -1;
+
+	return 0;

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 854

Quoted 10 lines
net/netfilter/nft_ct.c +10 -0
+}
+
+static void nft_ct_expect_obj_eval(struct nft_object *obj,
+				   struct nft_regs *regs,
+				   const struct nft_pktinfo *pkt)
+{
+	const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
+	struct nf_conntrack_expect *exp;
+	enum ip_conntrack_info ctinfo;
+	int dir;

Please use 'enum ip_conntrack_dir' instead of int for "dir".

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 857

Quoted 3 lines
net/netfilter/nft_ct.c +3 -0
+	struct nf_conn *ct;
+	struct nf_conn_help *ct_help;
+	int l3num = priv->l3num;

Reverse xmas tree for variable definitions:

	const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
	struct nf_conntrack_expect *exp;
	enum ip_conntrack_info ctinfo;
	struct nf_conn_help *help;
	int l3num = priv->l3num;
	struct nf_conn *ct;
	int dir;

From largest line to smallest.

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 859

Quoted 2 lines
net/netfilter/nft_ct.c +2 -0
+
+	/* Check ct exists and is tracked */

Please, remove comment. We only use comments when something is not evident to the reader, as a last resort. This forces people to write good code :-)

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 871

Quoted 12 lines
net/netfilter/nft_ct.c +12 -0
+	ct = nf_ct_get(pkt->skb, &ctinfo);
+	if (!ct || ctinfo == IP_CT_UNTRACKED) {
+		regs->verdict.code = NFT_BREAK;
+		return;
+	}
+	dir = CTINFO2DIR(ctinfo);
+
+	/* ct extention is required */
+	ct_help = nfct_help(ct);
+	if (!ct_help) {
+		ct_help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
+	}

Remove curly braces for single statement.

Nitpick: I'd suggest you rename 'ct_help' to 'help', for consistency with other existing codebase.

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 873

Quoted 2 lines
net/netfilter/nft_ct.c +2 -0
+
+	/* Did we reach the limit? */

No need for comment either.

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 875

Quoted 2 lines
net/netfilter/nft_ct.c +2 -0
+	if (ct_help->expecting[NF_CT_EXPECT_CLASS_DEFAULT] >= priv->size) {
+		regs->verdict.code = NF_DROP;

Probably just NFT_BREAK instead of NF_DROP ?

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 879

Quoted 4 lines
net/netfilter/nft_ct.c +4 -0
+		return;
+	}
+
+	/* If l3num is set to INET, use the current ct proto */

Remove comment.

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 882

Quoted 3 lines
net/netfilter/nft_ct.c +3 -0
+	if (l3num == NFPROTO_INET) {
+		l3num = nf_ct_l3num(ct);
+	}

Remove curly for single statement, ie.g

        if (l3num == NFPROTO_INET)
                l3num = nf_ct_l3num(ct);

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 884

Quoted 1 line
net/netfilter/nft_ct.c +1 -0
+	/* Create expectation */

Remove comment.

net/netfilter/nft_ct.c static struct nft_expr_type nft_notrack_type __read_mostly = { · line 887

Quoted 3 lines
net/netfilter/nft_ct.c +3 -0
+	exp = nf_ct_expect_alloc(ct);
+	if (exp == NULL) {
+		regs->verdict.code = NF_DROP;

NF_DROP looks fine in this case, do not change it.

Quoted 10 lines
net/netfilter/nft_ct.c +10 -0
+		return;
+	}
+	nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, l3num,
+		          &ct->tuplehash[!dir].tuple.src.u3,
+		          &ct->tuplehash[!dir].tuple.dst.u3,
+		          priv->l4proto, NULL, &priv->dport);
+	exp->timeout.expires = jiffies + priv->timeout * HZ;
+	if (nf_ct_expect_related(exp) != 0)
+		regs->verdict.code = NF_DROP;
+}