Skip to contentSkip to conversation

netfilter: ctnetlink: attach expectations to unconfirmed conntracks

PATCH In review Pablo Neira Ayuso 2 participants 2 messages Aug 7, 2013

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v3 1/1 netfilter: ctnetlink: attach expectations to unconfirmed conntracks

This patch adds the capability to attach expectations to unconfirmed conntrack entries. I'm using this from nfqueue, in that case the packet is retained until the user-space application returns the verdict on it. This patch is required by conntrack helpers that trigger expectations based on the first packet seen like the TFTP and the DHCPv6 user-space helpers.

I don't see any practical use for this out of the nfqueue context as you won't likely find what you look for. But if that ever happens, we have to bump the refcount to avoid holding a reference to an invalid conntrack.

---
 include/net/netfilter/nf_conntrack_core.h          |    4 ++
 include/uapi/linux/netfilter/nfnetlink_conntrack.h |    1 +
 net/netfilter/nf_conntrack_core.c                  |   46 ++++++++++++++++++++
 net/netfilter/nf_conntrack_netlink.c               |   14 +++++-
 4 files changed, 63 insertions(+), 2 deletions(-)
include/net/netfilter/nf_conntrack_core.h +4 -0
index fb2b623..75e8a30 100644
@@ -62,6 +62,10 @@ extern struct nf_conntrack_tuple_hash *
 nf_conntrack_find_get(struct net *net, u16 zone,
 		      const struct nf_conntrack_tuple *tuple);
 
+extern struct nf_conntrack_tuple_hash *
+nf_ct_unconfirmed_find_get(struct net *net, u16 zone,
+			   const struct nf_conntrack_tuple *tuple);
+
 extern int __nf_conntrack_confirm(struct sk_buff *skb);
 
 /* Confirm a connection: returns NF_DROP if packet must be dropped. */
include/uapi/linux/netfilter/nfnetlink_conntrack.h +1 -0
index 08fabc6..8f7c2fe 100644
@@ -187,6 +187,7 @@ enum ctattr_expect {
 	CTA_EXPECT_CLASS,
 	CTA_EXPECT_NAT,
 	CTA_EXPECT_FN,
+	CTA_EXPECT_MASTER_STATUS,
 	__CTA_EXPECT_MAX
 };
 #define CTA_EXPECT_MAX (__CTA_EXPECT_MAX - 1)
net/netfilter/nf_conntrack_core.c +46 -0

52 lines not on this page · raw message

net/netfilter/nf_conntrack_netlink.c +12 -2
index e842c0d..c578e76 100644
@@ -2747,7 +2747,7 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 	struct nf_conn *ct;
 	struct nf_conn_help *help;
 	struct nf_conntrack_helper *helper = NULL;
-	u_int32_t class = 0;
+	u_int32_t class = 0, master_status;
 	int err = 0;
 
 	/* caller guarantees that those three CTA_EXPECT_* exist */
@@ -2761,8 +2761,18 @@ ctnetlink_create_expect(struct net *net, u16 zone,
 	if (err < 0)
 		return err;
 
+	if (cda[CTA_EXPECT_MASTER_STATUS]) {
+		master_status =
+			ntohl(nla_get_be32(cda[CTA_EXPECT_MASTER_STATUS]));
+	} else
+		master_status = IPS_CONFIRMED;
+
 	/* Look for master conntrack of this expectation */
-	h = nf_conntrack_find_get(net, zone, &master_tuple);
+	if (master_status & IPS_CONFIRMED)
+		h = nf_conntrack_find_get(net, zone, &master_tuple);
+	else
+		h = nf_ct_unconfirmed_find_get(net, zone, &master_tuple);
+
 	if (!h)
 		return -ENOENT;
 	ct = nf_ct_tuplehash_to_ctrack(h);
Signature · 1 line
1.7.10.4
maintainer
#1 lore raw
Quoted 11 lines · Pablo Neira Ayuso

This patch adds the capability to attach expectations to unconfirmed conntrack entries. I'm using this from nfqueue, in that case the packet is retained until the user-space application returns the verdict on it. This patch is required by conntrack helpers that trigger expectations based on the first packet seen like the TFTP and the DHCPv6 user-space helpers.

I don't see any practical use for this out of the nfqueue context as you won't likely find what you look for. But if that ever happens, we have to bump the refcount to avoid holding a reference to an invalid conntrack.

[..]

net/netfilter/nf_conntrack_core.c nf_conntrack_find_get(struct net *net, u16 zone, · line 416

Quoted 1 line
net/netfilter/nf_conntrack_core.c +1 -0
+	hlist_nulls_for_each_entry_rcu(h, n, &net->ct.unconfirmed, hnnode) {

Its possible that "h" has just been moved to the conntrack hash table. In this case, on nf_ct_tuple_equal() mismatch, then next element might be a bucket NULLs value, i.e., __nf_ct_unconfirmed_find() might only have checked 2 out of 1000 entries on the unconfirmed list.

Theoretically, you could avoid this by checking if the nulls

net/netfilter/nf_conntrack_core.c nf_conntrack_find_get(struct net *net, u16 zone, · line 422

Quoted 6 lines
net/netfilter/nf_conntrack_core.c +6 -0
+		if (nf_ct_tuple_equal(tuple, &h->tuple) &&
+		    nf_ct_zone(nf_ct_tuplehash_to_ctrack(h)) == zone) {
+			ret = h;
+			break;
+		}
+	}

value is the right one in the "no match found" case, and restart the search again.

But I have concerns about the expected run-time behaviour on when you machine has a high new-connections-per-second rate.

If this is only used from nfqueue, can't you re-use skb->nfct? It would avoid the unconfirmed list traversal.