Skip to contentSkip to conversation

generic CONNTRACK target

RFC Pablo Neira Ayuso 3 participants 7 messages Jan 14, 2008 – Jan 15, 2008

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

Hi Patrick,

Attached an untested RFC patch to discuss the posibility of introducing a generic CONNTRACK target which replaces several existing specific targets. It's a quick and dirty hack. I have also somehow merged the recent Phil Oester's proposition on manual timeouts. We may also introduce there the explicit helper tracking via iptables that you've been discussing lately.

Also, I'd like to discuss some kind of mechanisms to reduce the number of event messages generated by ctnetlink such as introducing IPCT_VOLATILE to explicitly tell ctnetlink ignore certain events via iptables. I think that, ideally, an alternative can be some kind of ctnetlink filtering based on unicast netlink socket (similar to nfnetlink_queue) in which we attach a filter via CMD_SUBSCRIBE_EVENTS or something. This solution let us define a per-socket filter in the style of BSF. However, this means that we'll need a complete filtering facility for ctnetlink to classify events, and iptables already provides such classificator. This is intended to reduce the CPU consumption of conntrackd (around 25% avg, 45% max, in my testbed [1] with 2500 HTTP GET request per second) by only replicating TCP ESTABLISHED states.

Any ideas?

[1] http://people.netfilter.org/pablo/conntrack-tools/testcase.html

Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
author maintainer
#1 lore raw
Quoted 1 line · Pablo Neira Ayuso

Attached an untested RFC patch [...]

I forgot to attach the patch...

Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers

[RFC] Introduce the generic conntrack target

This patch implements the generic CONNTRACK target which merges CONNMARK, CONNSECMARK, NOTRACK and TIMEOUT into a single target. Also, This patch introduces the volatile events marking to make ctnetlink ignore certain events.

include/linux/netfilter/nf_conntrack_common.h +8 -0
Index: net-2.6.git/include/linux/netfilter/nf_conntrack_common.h
===================================================================
@@ -73,6 +73,10 @@ enum ip_conntrack_status {
 	/* Connection has fixed timeout. */
 	IPS_FIXED_TIMEOUT_BIT = 10,
 	IPS_FIXED_TIMEOUT = (1 << IPS_FIXED_TIMEOUT_BIT),
+
+	/* Connection has a manually configured timeout. */
+	IPS_MANUAL_TIMEOUT_BIT = 11,
+	IPS_MANUAL_TIMEOUT = (1 << IPS_MANUAL_TIMEOUT_BIT),
 };
 
 /* Connection tracking event bits */
@@ -137,6 +141,10 @@ enum ip_conntrack_events
 	/* Secmark is set */
 	IPCT_SECMARK_BIT = 14,
 	IPCT_SECMARK = (1 << IPCT_SECMARK_BIT),
+
+	/* These events has been set as volatile (via iptables) */
+	IPCT_VOLATILE_BIT = 15,
+	IPCT_VOLATILE = (1 << IPCT_VOLATILE_BIT),
 };
 
 enum ip_conntrack_expect_events {
include/net/netfilter/nf_conntrack.h +4 -0
Index: net-2.6.git/include/net/netfilter/nf_conntrack.h
===================================================================
@@ -88,6 +88,10 @@ struct nf_conn_help {
 	unsigned int expecting;
 };
 
+/* nf_conn feature for manual timeouts via iptables */
+struct nf_conn_timeout {
+	u_int32_t       timeout;
+};
 
 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
include/net/netfilter/nf_conntrack_extend.h +8 -0
Index: net-2.6.git/include/net/netfilter/nf_conntrack_extend.h
===================================================================
@@ -7,11 +7,13 @@ enum nf_ct_ext_id
 {
 	NF_CT_EXT_HELPER,
 	NF_CT_EXT_NAT,
+	NF_CT_EXT_TIMEOUT,
 	NF_CT_EXT_NUM,
 };
 
 #define NF_CT_EXT_HELPER_TYPE struct nf_conn_help
 #define NF_CT_EXT_NAT_TYPE struct nf_conn_nat
+#define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout
 
 /* Extensions: optional stuff which isn't permanently in struct. */
 struct nf_ct_ext {
@@ -82,4 +84,10 @@ struct nf_ct_ext_type
 
 int nf_ct_extend_register(struct nf_ct_ext_type *type);
 void nf_ct_extend_unregister(struct nf_ct_ext_type *type);
+
+static inline struct nf_conn_timeout *nfct_timeout(const struct nf_conn *ct)
+{
+	return nf_ct_ext_find(ct, NF_CT_EXT_TIMEOUT);
+}
+
 #endif /* _NF_CONNTRACK_EXTEND_H */
net/netfilter/Kconfig +24 -0
Index: net-2.6.git/net/netfilter/Kconfig
===================================================================
@@ -420,6 +420,30 @@ config NETFILTER_XT_TARGET_CONNSECMARK
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_TARGET_CONNTRACK
+	tristate '"CONNTRACK" target support'
+	depends on NETFILTER_XTABLES
+	depends on NF_CONNTRACK
+	default m if NETFILTER_ADVANCED=n
+	help
+	  The generic CONNTRACK target:
+	  a) allows you to manipulate the connection mark value. Similar
+	  to the MARK target, but affects the connection mark value rather
+	  than the packet mark value.
+	  b) allows you to copy security markings from packets to connections,
+	  and restores security markings from connections to packets (if the
+	  packets are not already marked).  This would normally be used in
+	  conjunction with the SECMARK target.
+	  c) allows you to alter the timeout of specific sessions in the
+	  conntrack/NAT subsystem.  Specific conntracks can be given longer
+	  or shorter timeouts than the global defaults.
+	  d) allows a select rule to specify which packets *not* to enter
+	  the conntrack/NAT subsystem with all the consequences (no ICMP error
+	  tracking, no protocol helpers for the selected packets).
+	  e) allows you to skip ctnetlink events delivery
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 config NETFILTER_XT_TARGET_TCPMSS
 	tristate '"TCPMSS" target support'
 	depends on NETFILTER_XTABLES && (IPV6 || IPV6=n)
net/netfilter/Makefile +1 -0
Index: net-2.6.git/net/netfilter/Makefile
===================================================================
@@ -41,6 +41,7 @@ obj-$(CONFIG_NETFILTER_XTABLES) += x_tab
 obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIFY) += xt_CLASSIFY.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMARK) += xt_CONNMARK.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
+obj-$(CONFIG_NETFILTER_XT_TARGET_CONNTRACK) += xt_CONNTRACK.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_MARK) += xt_MARK.o
 obj-$(CONFIG_NETFILTER_XT_TARGET_NFLOG) += xt_NFLOG.o
net/netfilter/nf_conntrack_core.c +4 -0
Index: net-2.6.git/net/netfilter/nf_conntrack_core.c
===================================================================
@@ -781,6 +781,10 @@ void __nf_ct_refresh_acct(struct nf_conn
 		return;
 	}
 
+	/* This conntrack has a manual timeout set via iptables */
+	if (test_bit(IPS_MANUAL_TIMEOUT_BIT, &ct->status) && nfct_timeout(ct))
+		extra_jiffies = nfct_timeout(ct)->timeout;
+
 	/* If not in hash table, timer will not be active yet */
 	if (!nf_ct_is_confirmed(ct)) {
 		ct->timeout.expires = extra_jiffies;
net/netfilter/nf_conntrack_netlink.c +4 -1
Index: net-2.6.git/net/netfilter/nf_conntrack_netlink.c
===================================================================
@@ -4,7 +4,7 @@
  * (C) 2001 by Jay Schulist <[email protected]>
  * (C) 2002-2006 by Harald Welte <[email protected]>
  * (C) 2003 by Patrick Mchardy <[email protected]>
- * (C) 2005-2007 by Pablo Neira Ayuso <[email protected]>
+ * (C) 2005-2008 by Pablo Neira Ayuso <[email protected]>
  *
  * Initial connection tracking via netlink development funded and
  * generally made possible by Network Robots, Inc. (www.networkrobots.com)
@@ -427,6 +427,9 @@ static int ctnetlink_conntrack_event(str
 	if (ct == &nf_conntrack_untracked)
 		return NOTIFY_DONE;
 
+	if (events & IPCT_VOLATILE)
+		return NOTIFY_DONE;
+
 	if (events & IPCT_DESTROY) {
 		type = IPCTNL_MSG_CT_DELETE;
 		group = NFNLGRP_CONNTRACK_DESTROY;
net/netfilter/xt_CONNTRACK.c new file +304 -0

304 lines not on this page · raw message

include/linux/netfilter/xt_CONNTRACK.h new file +22 -0
Index: net-2.6.git/include/linux/netfilter/xt_CONNTRACK.h
===================================================================
@@ -0,0 +1,22 @@
+#ifndef _XT_CONNTRACK_H_target
+#define _XT_CONNTRACK_H_target
+
+enum {
+	CONNTRACK_TG_MARK_SET = 0,
+	CONNTRACK_TG_MARK_SAVE,
+	CONNTRACK_TG_MARK_RESTORE,
+	CONNTRACK_TG_SECMARK_SAVE,
+	CONNTRACK_TG_SECMARK_RESTORE,
+	CONNTRACK_TG_NOTRACK,
+	CONNTRACK_TG_MANUAL_TIMEOUT,
+	CONNTRACK_TG_VOLATILE_EVENTS,
+	CONNTRACK_TG_MAX
+};
+
+struct xt_conntrack_tg_info {
+	u_int32_t mode;
+	u_int32_t data_0;
+	u_int32_t data_1;
+};
+
+#endif /*_XT_CONNTRACK_H_target */
#2 lore raw

net/netfilter/xt_CONNTRACK.c line 26

Quoted 9 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

Attached an untested RFC patch [...]

I forgot to attach the patch...

net/netfilter/xt_CONNTRACK.c new file +3 -0
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>");
+MODULE_DESCRIPTION("ip[6]tables CONNTRACK module");

A better description perhaps, along the lines of a recent patch of mine ( http://marc.info/?l=netfilter-devel&m=119980886105511&w=2 )

net/netfilter/xt_CONNTRACK.c line 31

Quoted 2 lines
net/netfilter/xt_CONNTRACK.c new file +2 -0
+#define CT_TG_MASK(x) (x->data_0)
+#define CT_TG_MARK(x) (x->data_1)
			((x)->data_0)

net/netfilter/xt_CONNTRACK.c line 44

Quoted 12 lines
net/netfilter/xt_CONNTRACK.c new file +12 -0
+static void mark_set(struct sk_buff *skb,
+		     struct nf_conn *ct,
+		     const struct xt_conntrack_tg_info *info)
+{
+	u_int32_t newmark;
+
+	newmark = (ct->mark & ~CT_TG_MASK(info)) | CT_TG_MARK(info);
+	if (newmark != ct->mark) {
+		ct->mark = newmark;
+		nf_conntrack_event_cache(IPCT_MARK, skb);
+	}
+}

The CONNMARK v1 pieces should be added in the upcoming future.

net/netfilter/xt_CONNTRACK.c line 93

Quoted 5 lines
net/netfilter/xt_CONNTRACK.c new file +5 -0
+static void notrack(struct sk_buff *skb,
+		    struct nf_conn *ct,
+		    const struct xt_conntrack_tg_info *info)
+{
+	/* Previously seen (loopback)? Ignore. */

xfrm probably.

net/netfilter/xt_CONNTRACK.c line 106

Quoted 13 lines
net/netfilter/xt_CONNTRACK.c new file +13 -0
+	if (skb->nfct != NULL)
+		return;
+
+	/* Attach fake conntrack entry.
+	 * If there is a real ct entry correspondig to this packet,
+	 * it'll hang aroun till timing out. We don't deal with it
+	 * for performance reasons. JK */
+	skb->nfct = &nf_conntrack_untracked.ct_general;
+	skb->nfctinfo = IP_CT_NEW;
+	nf_conntrack_get(skb->nfct);
+}
+
+#define CT_TG_TIMEOUT(x) (x->data_0)

((x)->data_0)

net/netfilter/xt_CONNTRACK.c line 137

Quoted 5 lines
net/netfilter/xt_CONNTRACK.c new file +5 -0
+typedef void (*ct_tg_array)(struct sk_buff *skb,
+			    struct nf_conn *ct,
+			    const struct xt_conntrack_tg_info *info);
+
+ct_tg_array conntrack_tg_array[CONNTRACK_TG_MAX] = {

^static const ct_tg_array conntrack_tg_array[] =

Quoted 19 lines
net/netfilter/xt_CONNTRACK.c new file +19 -0
+	[CONNTRACK_TG_MARK_SET]			= mark_set,
+	[CONNTRACK_TG_MARK_SAVE]		= mark_save,
+	[CONNTRACK_TG_MARK_RESTORE]		= mark_restore,
+	[CONNTRACK_TG_SECMARK_SAVE]		= secmark_save,
+	[CONNTRACK_TG_SECMARK_RESTORE]		= secmark_restore,
+	[CONNTRACK_TG_NOTRACK]			= notrack,
+	[CONNTRACK_TG_MANUAL_TIMEOUT]		= manual_timeout,
+	[CONNTRACK_TG_VOLATILE_EVENTS]		= volatile_events,
+};
+
+static bool
+conntrack_tg_check(const char *tablename, const void *entry,
+                   const struct xt_target *target, void *targinfo,
+                   unsigned int hook_mask)
+{
+	const struct xt_conntrack_tg_info *info = targinfo;
+
+	switch(info->mode) {
+	case CONNTRACK_TG_MARK_SET:

net/netfilter/xt_CONNTRACK.c line 187

Quoted 10 lines
net/netfilter/xt_CONNTRACK.c new file +10 -0
+	case CONNTRACK_TG_MARK_SAVE:
+	case CONNTRACK_TG_MARK_RESTORE:
+	case CONNTRACK_TG_SECMARK_SAVE:
+	case CONNTRACK_TG_SECMARK_RESTORE:
+	case CONNTRACK_TG_MANUAL_TIMEOUT:
+	case CONNTRACK_TG_VOLATILE_EVENTS:
+		if (strcmp(tablename, "mangle") != 0) {
+			printk(KERN_INFO PFX "use mode %hu in table "
+					     "`mangle\'\n", info->mode);
+			return false;

I had to give this a few seconds of thinking. At first interpretation, "use mode %hu in table mangle" sounds like "the mangle table can do %hu". Well, nice to know that mangle can do that, but that does not really say anything about why it failed.
I suggest changing the wording to what other targets/matches have:

"XXX is only valid in the YYY table"

net/netfilter/xt_CONNTRACK.c line 195

Quoted 8 lines
net/netfilter/xt_CONNTRACK.c new file +8 -0
+		}
+		break;
+	case CONNTRACK_TG_NOTRACK:
+		if (strcmp(tablename, "raw") != 0) {
+			printk(KERN_INFO PFX "use mode: %hu in table "
+					     "`raw\'\n", info->mode);
+			return false;
+		}

same.

net/netfilter/xt_CONNTRACK.c line 199

Quoted 4 lines
net/netfilter/xt_CONNTRACK.c new file +4 -0
+		break;
+	default:
+		printk(KERN_INFO PFX "unsupported mode: %hu\n", info->mode);
+		return false;

This one is ok, though.

Quoted 12 lines
net/netfilter/xt_CONNTRACK.c new file +12 -0
+static struct xt_target conntrack_tg_mangle_reg[] __read_mostly = {
+	{
+		.name		= "CONNTRACK",
+		.family		= AF_INET,
+		.checkentry	= conntrack_tg_check,
+		.destroy	= conntrack_tg_destroy,
+		.target		= conntrack_tg,
+		.targetsize	= sizeof(struct xt_conntrack_tg_info),
+		.table		= "mangle",
+};
+static struct xt_target conntrack_tg_raw_reg[] __read_mostly = {
+	{

net/netfilter/xt_CONNTRACK.c line 260

Quoted 10 lines
net/netfilter/xt_CONNTRACK.c new file +10 -0
+		.name		= "CONNTRACK",
+		.family		= AF_INET,
+		.checkentry	= conntrack_tg_check,
+		.destroy	= conntrack_tg_destroy,
+		.target		= conntrack_tg,
+		.targetsize	= sizeof(struct xt_conntrack_tg_info),
+		.table		= "raw",
+		.me		= THIS_MODULE,
+	},
+};

I have tried to reproduce this, and it does not work. The primary key tuple seems to be (name, revision, family), but you have two of these tuples. My test case was:

        {
                .name   = "STEAL",
                .family = AF_INET,
                .target = steal_tg,
                .me     = THIS_MODULE,
                .table  = "mangle",
        },
        {
                .name   = "STEAL",
                .family = AF_INET,
                .target = steal_tg,
                .me     = THIS_MODULE,
                .table  = "raw",
        },

And the kernel tells me:

[30800.792835] ip_tables: STEAL target: only valid in raw table, not 

mangle.

So much for that.
Drop the .table lines, you are checking the table in connmark_tg_check() anyways (like all other modules).

One of conntrack_tg_{mangle,raw}_reg is then redundant.

#3 lore raw
In reply to Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

Attached an untested RFC patch [...]

I forgot to attach the patch...

This doesn't seem to actually consolidate any code, just put it all in one file and dispatch based on the desired operation. Whats the advantage over the existing modules?

Another issue is that it merges all conntrack-related targets existing today, but as soon as we're going to add something new we're going to run into compatibility issues again and it will be easier to add a new module. I don't think the trouble is worth it as long as we have to suffer our crappy userspace interface.

#6 lore raw
In reply to Pablo Neira Ayuso
Quoted 13 lines · Pablo Neira Ayuso

Also, I'd like to discuss some kind of mechanisms to reduce the number of event messages generated by ctnetlink such as introducing IPCT_VOLATILE to explicitly tell ctnetlink ignore certain events via iptables. I think that, ideally, an alternative can be some kind of ctnetlink filtering based on unicast netlink socket (similar to nfnetlink_queue) in which we attach a filter via CMD_SUBSCRIBE_EVENTS or something. This solution let us define a per-socket filter in the style of BSF. However, this means that we'll need a complete filtering facility for ctnetlink to classify events, and iptables already provides such classificator. This is intended to reduce the CPU consumption of conntrackd (around 25% avg, 45% max, in my testbed [1] with 2500 HTTP GET request per second) by only replicating TCP ESTABLISHED states.

Quoted 1 line

Any ideas?

The part that supresses conntrack events seems useful, why not simply split it in a seperate module? It would be even more useful if we had a match to match on conntrack protocol states I guess.

#4 lore raw
In reply to Patrick McHardy
Quoted 4 lines · Patrick McHardy

This doesn't seem to actually consolidate any code, just put it all in one file and dispatch based on the desired operation. Whats the advantage over the existing modules?

I could imagine it is the "less .ko overhead" thing as I tried with xt_REJECT (12 kb, replacing a 24kb ipt_+ip6t_REJECT solution) and xt_ah.

Also, as noticed in http://lkml.org/lkml/2007/12/31/59, modules are aligned at page boundaries, which means

ipt_REJECT.ko 11121 bytes uses up 12288 bytes of RAM. ip6t_REJECT.ko 12107 bytes also uses up 12288 bytes of RAM. (All values on x86_32).
xt_REJECT.ko 14665 bytes would use just 16384 bytes of RAM, though of course, as correctly noticed, it has the ugly runtime dependency on IPv6. While xt_CONNTRACK would not have this IPv6 dependency, if people do not use IPv6 at all, they _save_ 4096 bytes. It's really a tradeoff.

(Yes it shows that the ELF .ko header bloat should probably be reduced.)

Quoted 6 lines

Another issue is that it merges all conntrack-related targets existing today, but as soon as we're going to add something new we're going to run into compatibility issues again and it will be easier to add a new module. I don't think the trouble is worth it as long as we have to suffer our crappy userspace interface.

I would have a third suggestion you can tinker with. It involves using separate struct xt_targetNameHere_tginfo for all submodules, so that all xt_CONNTRACK would do is code agglomeration rather than target combination. In other words:

struct xt_target xt_conntrack_tg_reg[] __read_mostly = {
	{
		.name = "NOTRACK",
		.targetsize = sizeof(struct xt_notrack_tginfo),
		...
	},
	{
		.name = "CONNMARK",
		.targetsize = sizeof(struct xt_connmark_tginfo),
		...
	},
};

This would at least leave the compatibility forest as-is, but be a benefit (either + or -) on RAM space (see above).

#5 lore raw
Quoted 5 lines · Jan Engelhardt
Quoted 3 lines · Patrick McHardy

This doesn't seem to actually consolidate any code, just put it all in one file and dispatch based on the desired operation. Whats the advantage over the existing modules?

Quoted 7 lines

I could imagine it is the "less .ko overhead" thing as I tried with xt_REJECT (12 kb, replacing a 24kb ipt_+ip6t_REJECT solution) and xt_ah.

Also, as noticed in http://lkml.org/lkml/2007/12/31/59, modules are aligned at page boundaries, which means

ipt_REJECT.ko 11121 bytes uses up 12288 bytes of RAM. ip6t_REJECT.ko 12107 bytes also uses up 12288 bytes of RAM. (All values on x86_32).

Well, don't use module then. Putting everything in one module is a rather strange way of doing this though :)