Skip to content

Commit 481fa37

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: conntrack: add nf_conntrack_default_on sysctl
This switch (default on) can be used to disable automatic registration of connection tracking functionality in newly created network namespaces. This means that when net namespace goes down (or the tracker protocol module is unloaded) we *might* have to unregister the hooks. We can either add another per-netns variable that tells if the hooks got registered by default, or, alternatively, just call the protocol _put() function and have the callee deal with a possible 'extra' put() operation that doesn't pair with a get() one. This uses the latter approach, i.e. a put() without a get has no effect. Conntrack is still enabled automatically regardless of the new sysctl setting if the new net namespace requires connection tracking, e.g. when NAT rules are created. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 0c66dc1 commit 481fa37

4 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎Documentation/networking/nf_conntrack-sysctl.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ nf_conntrack_max - INTEGER
9696
Size of connection tracking table. Default value is
9797
nf_conntrack_buckets value * 4.
9898

99+
nf_conntrack_default_on - BOOLEAN
100+
0 - don't register conntrack in new net namespaces
101+
1 - register conntrack in new net namespaces (default)
102+
103+
This controls wheter newly created network namespaces have connection
104+
tracking enabled by default. It will be enabled automatically
105+
regardless of this setting if the new net namespace requires
106+
connection tracking, e.g. when NAT rules are created.
107+
This setting is only visible in initial user namespace, it has no
108+
effect on existing namespaces.
109+
99110
nf_conntrack_tcp_be_liberal - BOOLEAN
100111
0 - disabled (default)
101112
not 0 - enabled

‎include/net/netfilter/nf_conntrack_l3proto.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,18 @@ struct nf_conntrack_l3proto {
7373

7474
extern struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX];
7575

76+
#ifdef CONFIG_SYSCTL
7677
/* Protocol pernet registration. */
7778
int nf_ct_l3proto_pernet_register(struct net *net,
7879
struct nf_conntrack_l3proto *proto);
80+
#else
81+
static inline int nf_ct_l3proto_pernet_register(struct net *n,
82+
struct nf_conntrack_l3proto *p)
83+
{
84+
return 0;
85+
}
86+
#endif
87+
7988
void nf_ct_l3proto_pernet_unregister(struct net *net,
8089
struct nf_conntrack_l3proto *proto);
8190

‎net/netfilter/nf_conntrack_proto.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,19 @@ int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto)
238238
}
239239
EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
240240

241+
#ifdef CONFIG_SYSCTL
242+
extern unsigned int nf_conntrack_default_on;
243+
241244
int nf_ct_l3proto_pernet_register(struct net *net,
242245
struct nf_conntrack_l3proto *proto)
243246
{
244-
return 0;
247+
if (nf_conntrack_default_on == 0)
248+
return 0;
249+
250+
return proto->net_ns_get ? proto->net_ns_get(net) : 0;
245251
}
246252
EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register);
253+
#endif
247254

248255
void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
249256
{
@@ -264,6 +271,16 @@ EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
264271
void nf_ct_l3proto_pernet_unregister(struct net *net,
265272
struct nf_conntrack_l3proto *proto)
266273
{
274+
/*
275+
* nf_conntrack_default_on *might* have registered hooks.
276+
* ->net_ns_put must cope with more puts() than get(), i.e.
277+
* if nf_conntrack_default_on was 0 at time of
278+
* nf_ct_l3proto_pernet_register invocation this net_ns_put()
279+
* should be a noop.
280+
*/
281+
if (proto->net_ns_put)
282+
proto->net_ns_put(net);
283+
267284
/* Remove all contrack entries for this protocol */
268285
nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0);
269286
}

‎net/netfilter/nf_conntrack_standalone.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ static int log_invalid_proto_max __read_mostly = 255;
452452
/* size the user *wants to set */
453453
static unsigned int nf_conntrack_htable_size_user __read_mostly;
454454

455+
extern unsigned int nf_conntrack_default_on;
456+
unsigned int nf_conntrack_default_on __read_mostly = 1;
457+
455458
static int
456459
nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
457460
void __user *buffer, size_t *lenp, loff_t *ppos)
@@ -517,6 +520,13 @@ static struct ctl_table nf_ct_sysctl_table[] = {
517520
.mode = 0644,
518521
.proc_handler = proc_dointvec,
519522
},
523+
{
524+
.procname = "nf_conntrack_default_on",
525+
.data = &nf_conntrack_default_on,
526+
.maxlen = sizeof(unsigned int),
527+
.mode = 0644,
528+
.proc_handler = proc_dointvec,
529+
},
520530
{ }
521531
};
522532

0 commit comments

Comments
 (0)