Skip to contentSkip to conversation

netfilter: ct: add helper assignment support

RFC In review Florian Westphal 2 participants 5 messages Feb 15, 2017 – Feb 16, 2017

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

RFC 1/1 netfilter: ct: add helper assignment support

This RFC adds native support to assign conntrack helpers. Not even compile tested.

It adds NFT_OBJECT_CT_HELPER to assign helpers to connections by using the stateful objects infra that is in place for quota and counter.

This would also need NFT_OBJECT_CT_TIMEOUT to support custom timeouts in nftables.

Does this look sane to you from a design p.o.v.? If yes, I would start looking into userspace side of things.

Thanks,
Florian

---
 include/uapi/linux/netfilter/nf_tables.h |  12 ++-
 net/netfilter/nft_ct.c                   | 130 ++++++++++++++++++++++++++++++-
 2 files changed, 139 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +4 -0
index 05215d30fe5c..121e79cacc49 100644
@@ -1246,10 +1246,20 @@ enum nft_fib_flags {
 	NFTA_FIB_F_OIF		= 1 << 4,	/* restrict to oif */
 };
 
+enum nft_ct_helper_attributes {
+	NFTA_CT_HELPER_UNSPEC,
+	NFTA_CT_HELPER_NAME,
+	NFTA_CT_HELPER_L3PROTO,
 

Note from myself, i dislike L3PROTO, it would be nicer to be able to handle this via the table family but I did not yet find a way to detect this from the obj->init() function.

Its needed for nf_conntrack_helper_try_module_get().

This also begs the question of how one would handle NFPROTO_INET, in that case we'd want both v4 and v6, but that would require stashing two struct nf_conntrack_helper in priv area.

Any ideas/suggestions?

+ NFTA_CT_HELPER_L4PROTO,
+ __NFTA_CT_HELPER_MAX,
+};
+#define NFTA_CT_HELPER_MAX (__NFTA_CT_HELPER_MAX - 1)
+
#define NFT_OBJECT_UNSPEC 0
#define NFT_OBJECT_COUNTER 1
#define NFT_OBJECT_QUOTA 2
-#define __NFT_OBJECT_MAX 3
+#define NFT_OBJECT_CT_HELPER 3
+#define __NFT_OBJECT_MAX 4
#define NFT_OBJECT_MAX (__NFT_OBJECT_MAX - 1)

/**

net/netfilter/nft_ct.c +128 -2

165 lines not on this page · raw message

Signature · 1 line
2.10.2
#1 lore raw
Quoted 11 lines · Florian Westphal

This RFC adds native support to assign conntrack helpers. Not even compile tested.

It adds NFT_OBJECT_CT_HELPER to assign helpers to connections by using the stateful objects infra that is in place for quota and counter.

This would also need NFT_OBJECT_CT_TIMEOUT to support custom timeouts in nftables.

Does this look sane to you from a design p.o.v.? If yes, I would start looking into userspace side of things.

Yes.

I would add a NFT_OBJ_F_STATEFUL flag that we can set for counter and quota. This flag would disable atomic-dump-and-reset which make no sense for this object type.

Quoted 18 lines
---
 include/uapi/linux/netfilter/nf_tables.h |  12 ++-
 net/netfilter/nft_ct.c                   | 130 ++++++++++++++++++++++++++++++-
 2 files changed, 139 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +4 -0
index 05215d30fe5c..121e79cacc49 100644
@@ -1246,10 +1246,20 @@ enum nft_fib_flags {
 	NFTA_FIB_F_OIF		= 1 << 4,	/* restrict to oif */
 };
 
+enum nft_ct_helper_attributes {
+	NFTA_CT_HELPER_UNSPEC,
+	NFTA_CT_HELPER_NAME,
+	NFTA_CT_HELPER_L3PROTO,
 
Quoted 3 lines

Note from myself, i dislike L3PROTO, it would be nicer to be able to handle this via the table family but I did not yet find a way to detect this from the obj->init() function.

We can pass nft_ctx to obj->init().

Quoted 6 lines

Its needed for nf_conntrack_helper_try_module_get().

This also begs the question of how one would handle NFPROTO_INET, in that case we'd want both v4 and v6, but that would require stashing two struct nf_conntrack_helper in priv area.

Still, someone may want to only enable helper for IPv4 in the inet chain, right? It's a bit of corner case but this attribute provides slight more flexibility.

Probably, we should handle NFPROTO_INET as a real family at some point, so user doesn't have to specify twice the same configuration to attach helpers from inet chains. We may be able to save that double pointer if we register the helper for NFPROTO_INET, but I would need to have a closer look at that cthelper code to see the implications of this.

On a different front, but related, I've been considering to handle the NFPROTO_INET family from the netfilter/core, so we can get rid of the existing specific code in nf_tables to handle this pseudofamily. Actually, just handle it as a real family. I have a patchset here in a branch I made to do this, I would need to revisit it.

include/uapi/linux/netfilter/nf_tables.h

Quoted 3 lines

Any ideas/suggestions?

include/uapi/linux/netfilter/nf_tables.h +1 -0
+	NFTA_CT_HELPER_L4PROTO,

Fine by now. We can place here more attributes, such as expectation timeouts and specific per-helper setting (eg. ftp loose). Just a matter of adding more attributes later on, no problem.

Quoted 53 lines
include/uapi/linux/netfilter/nf_tables.h +6 -1
+	__NFTA_CT_HELPER_MAX,
+};
+#define NFTA_CT_HELPER_MAX	(__NFTA_CT_HELPER_MAX - 1)
+
 #define NFT_OBJECT_UNSPEC	0
 #define NFT_OBJECT_COUNTER	1
 #define NFT_OBJECT_QUOTA	2
-#define __NFT_OBJECT_MAX	3
+#define NFT_OBJECT_CT_HELPER	3
+#define __NFT_OBJECT_MAX	4
 #define NFT_OBJECT_MAX		(__NFT_OBJECT_MAX - 1)
 
 /**
net/netfilter/nft_ct.c +25 -0
index c6b8022c0e47..2a5bd9955122 100644
@@ -32,6 +32,12 @@ struct nft_ct {
 	};
 };
 
+struct nft_ct_helper_obj  {
+	struct nf_conntrack_helper *helper;
+	u16 l3proto;
+	u8 l4proto;
+};
+
 #ifdef CONFIG_NF_CONNTRACK_ZONES
 static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template);
 static unsigned int nft_ct_pcpu_template_refcnt __read_mostly;
@@ -729,28 +735,147 @@ static struct nft_expr_type nft_notrack_type __read_mostly = {
 	.owner		= THIS_MODULE,
 };
 
+static int nft_ct_helper_obj_init(const struct nlattr * const tb[],
+				  struct nft_object *obj)
+{
+	struct nft_ct_helper_obj *priv = nft_obj_data(obj);
+	struct nf_conntrack_helper *helper;
+	char name[NF_CT_HELPER_NAME_LEN];
+	int family;
+	u8 proto;
+
+	if (!tb[NFTA_CT_HELPER_NAME] || !tb[NFTA_CT_HELPER_L4PROTO] ||
+	    !tb[NFTA_CT_HELPER_L3PROTO])
+		return -EINVAL;
+
+	family = ntohs(nla_get_be16(tb[NFTA_CT_HELPER_L3PROTO]));
+	switch (family) {
+	case NFPROTO_IPV4:
+	case NFPROTO_IPV6:
+		break;
+	default:

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

Quoted 10 lines
net/netfilter/nft_ct.c +10 -0
+		return -EAFNOSUPPORT;
+	}
+
+	proto = nla_get_u8(tb[NFTA_CT_HELPER_L4PROTO]);
+	if (!proto)
+		return -ENOENT;
+
+
+	if (nla_strlcpy(name, tb[NFTA_CT_HELPER_NAME], sizeof(name)) >= sizeof(name))
+		return -EINVAL;

nla_policy already ensures we don't go over helper name size.

Quoted 10 lines
net/netfilter/nft_ct.c +10 -0
+	helper = nf_conntrack_helper_try_module_get(name, family, proto);
+	if (!helper)
+		return -ENOENT;
+
+	priv->helper = helper;
+	priv->l4proto = proto;
+	priv->l3proto = family;
+
+	return 0;
+}
author maintainer
#2 lore raw
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines

Note from myself, i dislike L3PROTO, it would be nicer to be able to handle this via the table family but I did not yet find a way to detect this from the obj->init() function.

We can pass nft_ctx to obj->init().

OK, I can make that change then, no problem.

Quoted 10 lines
Quoted 6 lines

Its needed for nf_conntrack_helper_try_module_get().

This also begs the question of how one would handle NFPROTO_INET, in that case we'd want both v4 and v6, but that would require stashing two struct nf_conntrack_helper in priv area.

Still, someone may want to only enable helper for IPv4 in the inet chain, right? It's a bit of corner case but this attribute provides slight more flexibility.

But assignment can be limited via nft ... meta nfproto ipv4, no?

Quoted 3 lines

Probably, we should handle NFPROTO_INET as a real family at some point, so user doesn't have to specify twice the same configuration to attach helpers from inet chains.

Yes, that what I wanted to avoid ...

Quoted 5 lines

On a different front, but related, I've been considering to handle the NFPROTO_INET family from the netfilter/core, so we can get rid of the existing specific code in nf_tables to handle this pseudofamily. Actually, just handle it as a real family. I have a patchset here in a branch I made to do this, I would need to revisit it.

Sounds good to me, although I am not sure how you're going to pull this off ;)

Quoted 7 lines

Review

Quoted 3 lines

Any ideas/suggestions?

(quoted diff) +1 -0
+	NFTA_CT_HELPER_L4PROTO,

Fine by now. We can place here more attributes, such as expectation timeouts and specific per-helper setting (eg. ftp loose). Just a matter of adding more attributes later on, no problem.

Right, I wanted to be sure that this is the right direction before doing this.

Quoted 4 lines

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

Quoted 2 lines
net/netfilter/nft_ct.c +2 -0
+	if (nla_strlcpy(name, tb[NFTA_CT_HELPER_NAME], sizeof(name)) >= sizeof(name))
+		return -EINVAL;

nla_policy already ensures we don't go over helper name size.

Right, I'll remove the conditional, thanks Pablo.

#3 lore raw
Quoted 16 lines · Florian Westphal
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines

Note from myself, i dislike L3PROTO, it would be nicer to be able to handle this via the table family but I did not yet find a way to detect this from the obj->init() function.

We can pass nft_ctx to obj->init().

OK, I can make that change then, no problem.

Quoted 7 lines
Quoted 6 lines

Its needed for nf_conntrack_helper_try_module_get().

This also begs the question of how one would handle NFPROTO_INET, in that case we'd want both v4 and v6, but that would require stashing two struct nf_conntrack_helper in priv area.

Quoted 5 lines
Quoted 3 lines

Still, someone may want to only enable helper for IPv4 in the inet chain, right? It's a bit of corner case but this attribute provides slight more flexibility.

But assignment can be limited via nft ... meta nfproto ipv4, no?

Right, that's a possible solution to restrict this. Downside is that we have some slight packet runtime cost, since we have to evaluate this extra expression in the rule for each packet.

Another aspect is that we end up having part of the helper configuration spread between the helper definition and the rule, given that l4proto would restrict setting the helper to the transport protocol and that is set from the helper definition itself.

Quoted 1 line

From a user perspective, we can just hide this detail by infering it

from context around if not specified, so:

table ip x {
        helper "ftp" {
                type "ftp"
                l4proto tcp
        }
}

So in this case:

table inet x {
        helper "ftp" {
                type "ftp"
                l4proto tcp
        }
}

We register it for both ip and ip6.

In this case, with explicit layer 3 protocol:

table bridge x {
        helper "ftp" {
                type "ftp"
                protocol ip
                l4proto tcp
        }
}
Quoted 1 line

From helper ->eval() we could just skip traffic that is neither IPv4

at layer 3 nor TCP at layer 4 without having to add this dependency.

If protocol specified in helper for bridge, then default to NFPROTO_INET, ie. both enabled.

So yes, I'm still proposing to keep with that layer 3 attribute around. What do you think?

author maintainer
#4 lore raw
Quoted 63 lines · Pablo Neira Ayuso
Quoted 16 lines · Florian Westphal
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines

Note from myself, i dislike L3PROTO, it would be nicer to be able to handle this via the table family but I did not yet find a way to detect this from the obj->init() function.

We can pass nft_ctx to obj->init().

OK, I can make that change then, no problem.

Quoted 7 lines
Quoted 6 lines

Its needed for nf_conntrack_helper_try_module_get().

This also begs the question of how one would handle NFPROTO_INET, in that case we'd want both v4 and v6, but that would require stashing two struct nf_conntrack_helper in priv area.

Quoted 5 lines
Quoted 3 lines

Still, someone may want to only enable helper for IPv4 in the inet chain, right? It's a bit of corner case but this attribute provides slight more flexibility.

But assignment can be limited via nft ... meta nfproto ipv4, no?

Right, that's a possible solution to restrict this. Downside is that we have some slight packet runtime cost, since we have to evaluate this extra expression in the rule for each packet.

Another aspect is that we end up having part of the helper configuration spread between the helper definition and the rule, given that l4proto would restrict setting the helper to the transport protocol and that is set from the helper definition itself.

From a user perspective, we can just hide this detail by infering it from context around if not specified, so:

table ip x {
        helper "ftp" {
                type "ftp"
                l4proto tcp
        }
}

So in this case:

table inet x {
        helper "ftp" {
                type "ftp"
                l4proto tcp
        }
}

We register it for both ip and ip6.

In this case, with explicit layer 3 protocol:

table bridge x {
        helper "ftp" {
                type "ftp"
                protocol ip
                l4proto tcp
        }
}
Quoted 8 lines

From helper ->eval() we could just skip traffic that is neither IPv4 at layer 3 nor TCP at layer 4 without having to add this dependency.

If protocol specified in helper for bridge, then default to NFPROTO_INET, ie. both enabled.

So yes, I'm still proposing to keep with that layer 3 attribute around. What do you think?

Sure, I have no issue with this, its just a minor implementation detail to me.

I will work on nft parser side for this now and will get back to you.