2006-11-29 01:35:03 +00:00
|
|
|
/* L3/L4 protocol support for nf_conntrack. */
|
|
|
|
|
|
|
|
/* (C) 1999-2001 Paul `Rusty' Russell
|
|
|
|
* (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
* (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
|
2013-04-06 13:24:29 +00:00
|
|
|
* (C) 2006-2012 Patrick McHardy <kaber@trash.net>
|
2006-11-29 01:35:03 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/module.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2006-11-29 01:35:17 +00:00
|
|
|
#include <linux/mutex.h>
|
2006-11-29 01:35:03 +00:00
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/percpu.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_l3proto.h>
|
2006-11-29 01:35:06 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_l4proto.h>
|
2006-11-29 01:35:03 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_core.h>
|
2017-10-11 08:47:40 +00:00
|
|
|
#include <net/netfilter/nf_log.h>
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2017-06-14 09:54:07 +00:00
|
|
|
static struct nf_conntrack_l4proto __rcu **nf_ct_protos[NFPROTO_NUMPROTO] __read_mostly;
|
|
|
|
struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[NFPROTO_NUMPROTO] __read_mostly;
|
2006-12-03 06:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l3protos);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2007-03-14 23:37:52 +00:00
|
|
|
static DEFINE_MUTEX(nf_ct_proto_mutex);
|
2006-11-29 01:35:17 +00:00
|
|
|
|
2007-03-14 23:37:52 +00:00
|
|
|
#ifdef CONFIG_SYSCTL
|
2006-11-29 01:35:17 +00:00
|
|
|
static int
|
2012-05-28 21:04:09 +00:00
|
|
|
nf_ct_register_sysctl(struct net *net,
|
|
|
|
struct ctl_table_header **header,
|
|
|
|
const char *path,
|
2012-06-21 04:36:41 +00:00
|
|
|
struct ctl_table *table)
|
2006-11-29 01:35:17 +00:00
|
|
|
{
|
|
|
|
if (*header == NULL) {
|
2012-05-28 21:04:09 +00:00
|
|
|
*header = register_net_sysctl(net, path, table);
|
2006-11-29 01:35:17 +00:00
|
|
|
if (*header == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2012-05-28 21:04:09 +00:00
|
|
|
|
2006-11-29 01:35:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nf_ct_unregister_sysctl(struct ctl_table_header **header,
|
2012-05-28 21:04:09 +00:00
|
|
|
struct ctl_table **table,
|
2012-06-21 04:36:41 +00:00
|
|
|
unsigned int users)
|
2006-11-29 01:35:17 +00:00
|
|
|
{
|
2012-06-21 04:36:41 +00:00
|
|
|
if (users > 0)
|
2006-11-29 01:35:17 +00:00
|
|
|
return;
|
2008-01-09 08:34:02 +00:00
|
|
|
|
2012-04-19 13:24:33 +00:00
|
|
|
unregister_net_sysctl_table(*header);
|
2012-05-28 21:04:09 +00:00
|
|
|
kfree(*table);
|
2006-11-29 01:35:17 +00:00
|
|
|
*header = NULL;
|
2012-05-28 21:04:09 +00:00
|
|
|
*table = NULL;
|
2006-11-29 01:35:17 +00:00
|
|
|
}
|
2017-10-11 08:47:40 +00:00
|
|
|
|
|
|
|
__printf(5, 6)
|
|
|
|
void nf_l4proto_log_invalid(const struct sk_buff *skb,
|
|
|
|
struct net *net,
|
|
|
|
u16 pf, u8 protonum,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct va_format vaf;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (net->ct.sysctl_log_invalid != protonum ||
|
|
|
|
net->ct.sysctl_log_invalid != IPPROTO_RAW)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vaf.fmt = fmt;
|
|
|
|
vaf.va = &args;
|
|
|
|
|
|
|
|
nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
|
|
|
|
"nf_ct_proto_%d: %pV ", protonum, &vaf);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_l4proto_log_invalid);
|
2006-11-29 01:35:17 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-11 22:57:08 +00:00
|
|
|
const struct nf_conntrack_l4proto *
|
2006-11-29 01:35:06 +00:00
|
|
|
__nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
|
2006-11-29 01:35:03 +00:00
|
|
|
{
|
2017-06-14 09:54:07 +00:00
|
|
|
if (unlikely(l3proto >= NFPROTO_NUMPROTO || nf_ct_protos[l3proto] == NULL))
|
2006-11-29 01:35:06 +00:00
|
|
|
return &nf_conntrack_l4proto_generic;
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2007-02-12 19:12:57 +00:00
|
|
|
return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
2006-12-03 06:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
|
|
|
/* this is guaranteed to always return a valid protocol helper, since
|
|
|
|
* it falls back to generic_protocol */
|
2017-08-11 22:57:08 +00:00
|
|
|
const struct nf_conntrack_l3proto *
|
2006-11-29 01:35:03 +00:00
|
|
|
nf_ct_l3proto_find_get(u_int16_t l3proto)
|
|
|
|
{
|
|
|
|
struct nf_conntrack_l3proto *p;
|
|
|
|
|
2007-02-12 19:12:57 +00:00
|
|
|
rcu_read_lock();
|
2006-11-29 01:35:03 +00:00
|
|
|
p = __nf_ct_l3proto_find(l3proto);
|
|
|
|
if (!try_module_get(p->me))
|
2006-11-29 01:35:06 +00:00
|
|
|
p = &nf_conntrack_l3proto_generic;
|
2007-02-12 19:12:57 +00:00
|
|
|
rcu_read_unlock();
|
2006-11-29 01:35:03 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2006-12-03 06:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
nf_ct_l3proto_try_module_get(unsigned short l3proto)
|
|
|
|
{
|
2017-08-11 22:57:08 +00:00
|
|
|
const struct nf_conntrack_l3proto *p;
|
2006-11-29 01:35:03 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
retry: p = nf_ct_l3proto_find_get(l3proto);
|
2006-11-29 01:35:06 +00:00
|
|
|
if (p == &nf_conntrack_l3proto_generic) {
|
2006-11-29 01:35:03 +00:00
|
|
|
ret = request_module("nf_conntrack-%d", l3proto);
|
|
|
|
if (!ret)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
return -EPROTOTYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-03 06:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
|
|
|
void nf_ct_l3proto_module_put(unsigned short l3proto)
|
|
|
|
{
|
|
|
|
struct nf_conntrack_l3proto *p;
|
|
|
|
|
2010-05-10 15:45:56 +00:00
|
|
|
/* rcu_read_lock not necessary since the caller holds a reference, but
|
|
|
|
* taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
|
|
|
|
*/
|
|
|
|
rcu_read_lock();
|
2006-11-29 01:35:03 +00:00
|
|
|
p = __nf_ct_l3proto_find(l3proto);
|
|
|
|
module_put(p->me);
|
2010-05-10 15:45:56 +00:00
|
|
|
rcu_read_unlock();
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
2006-12-03 06:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2016-11-15 20:36:40 +00:00
|
|
|
int nf_ct_netns_get(struct net *net, u8 nfproto)
|
|
|
|
{
|
2016-11-15 20:36:43 +00:00
|
|
|
const struct nf_conntrack_l3proto *l3proto;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
might_sleep();
|
|
|
|
|
|
|
|
ret = nf_ct_l3proto_try_module_get(nfproto);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* we already have a reference, can't fail */
|
|
|
|
rcu_read_lock();
|
|
|
|
l3proto = __nf_ct_l3proto_find(nfproto);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
if (!l3proto->net_ns_get)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = l3proto->net_ns_get(net);
|
|
|
|
if (ret < 0)
|
|
|
|
nf_ct_l3proto_module_put(nfproto);
|
|
|
|
|
|
|
|
return ret;
|
2016-11-15 20:36:40 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_netns_get);
|
|
|
|
|
|
|
|
void nf_ct_netns_put(struct net *net, u8 nfproto)
|
|
|
|
{
|
2016-11-15 20:36:43 +00:00
|
|
|
const struct nf_conntrack_l3proto *l3proto;
|
|
|
|
|
|
|
|
might_sleep();
|
|
|
|
|
|
|
|
/* same as nf_conntrack_netns_get(), reference assumed */
|
|
|
|
rcu_read_lock();
|
|
|
|
l3proto = __nf_ct_l3proto_find(nfproto);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
if (WARN_ON(!l3proto))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (l3proto->net_ns_put)
|
|
|
|
l3proto->net_ns_put(net);
|
|
|
|
|
2016-11-15 20:36:40 +00:00
|
|
|
nf_ct_l3proto_module_put(nfproto);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_netns_put);
|
|
|
|
|
2017-08-11 22:57:08 +00:00
|
|
|
const struct nf_conntrack_l4proto *
|
2012-03-22 22:40:01 +00:00
|
|
|
nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
|
|
|
|
{
|
2017-08-11 22:57:08 +00:00
|
|
|
const struct nf_conntrack_l4proto *p;
|
2012-03-22 22:40:01 +00:00
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
p = __nf_ct_l4proto_find(l3num, l4num);
|
|
|
|
if (!try_module_get(p->me))
|
|
|
|
p = &nf_conntrack_l4proto_generic;
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
|
|
|
|
|
2017-08-01 10:25:01 +00:00
|
|
|
void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p)
|
2012-03-22 22:40:01 +00:00
|
|
|
{
|
|
|
|
module_put(p->me);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
|
|
|
|
|
2006-11-29 01:35:03 +00:00
|
|
|
static int kill_l3proto(struct nf_conn *i, void *data)
|
|
|
|
{
|
2017-08-11 22:57:08 +00:00
|
|
|
return nf_ct_l3num(i) == ((const struct nf_conntrack_l3proto *)data)->l3proto;
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
|
|
|
|
2006-11-29 01:35:06 +00:00
|
|
|
static int kill_l4proto(struct nf_conn *i, void *data)
|
2006-11-29 01:35:03 +00:00
|
|
|
{
|
2017-08-11 22:57:08 +00:00
|
|
|
const struct nf_conntrack_l4proto *l4proto;
|
2017-03-28 19:05:16 +00:00
|
|
|
l4proto = data;
|
2008-04-14 09:15:52 +00:00
|
|
|
return nf_ct_protonum(i) == l4proto->l4proto &&
|
|
|
|
nf_ct_l3num(i) == l4proto->l3proto;
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 22:57:08 +00:00
|
|
|
int nf_ct_l3proto_register(const struct nf_conntrack_l3proto *proto)
|
2006-11-29 01:35:03 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2010-11-15 17:17:21 +00:00
|
|
|
struct nf_conntrack_l3proto *old;
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2017-06-14 09:54:07 +00:00
|
|
|
if (proto->l3proto >= NFPROTO_NUMPROTO)
|
2007-03-14 23:38:48 +00:00
|
|
|
return -EBUSY;
|
2017-08-11 22:57:02 +00:00
|
|
|
#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
|
|
|
|
if (proto->tuple_to_nlattr && proto->nla_size == 0)
|
2009-03-25 17:24:48 +00:00
|
|
|
return -EINVAL;
|
2017-08-11 22:57:02 +00:00
|
|
|
#endif
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_lock(&nf_ct_proto_mutex);
|
2010-11-15 17:17:21 +00:00
|
|
|
old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
|
|
|
|
lockdep_is_held(&nf_ct_proto_mutex));
|
|
|
|
if (old != &nf_conntrack_l3proto_generic) {
|
2006-11-29 01:35:03 +00:00
|
|
|
ret = -EBUSY;
|
2006-11-29 01:35:08 +00:00
|
|
|
goto out_unlock;
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
2006-11-29 01:35:17 +00:00
|
|
|
|
2007-03-14 23:38:48 +00:00
|
|
|
rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2006-11-29 01:35:08 +00:00
|
|
|
out_unlock:
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_unlock(&nf_ct_proto_mutex);
|
2006-11-29 01:35:03 +00:00
|
|
|
return ret;
|
2012-05-28 21:04:10 +00:00
|
|
|
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
2013-01-21 22:10:33 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2017-08-11 22:57:08 +00:00
|
|
|
void nf_ct_l3proto_unregister(const struct nf_conntrack_l3proto *proto)
|
2012-05-28 21:04:10 +00:00
|
|
|
{
|
2017-06-14 09:54:07 +00:00
|
|
|
BUG_ON(proto->l3proto >= NFPROTO_NUMPROTO);
|
2006-11-29 01:35:08 +00:00
|
|
|
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_lock(&nf_ct_proto_mutex);
|
2010-11-15 17:17:21 +00:00
|
|
|
BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
|
|
|
|
lockdep_is_held(&nf_ct_proto_mutex)
|
|
|
|
) != proto);
|
2007-02-12 19:12:57 +00:00
|
|
|
rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
|
|
|
|
&nf_conntrack_l3proto_generic);
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_unlock(&nf_ct_proto_mutex);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2007-03-14 23:38:48 +00:00
|
|
|
synchronize_rcu();
|
2017-05-30 09:31:06 +00:00
|
|
|
/* Remove all contrack entries for this protocol */
|
2017-08-11 22:57:08 +00:00
|
|
|
nf_ct_iterate_destroy(kill_l3proto, (void*)proto);
|
2012-05-28 21:04:10 +00:00
|
|
|
}
|
2013-01-21 22:10:33 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
|
2012-05-28 21:04:10 +00:00
|
|
|
|
2012-05-28 21:04:09 +00:00
|
|
|
static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
|
2017-08-01 10:25:01 +00:00
|
|
|
const struct nf_conntrack_l4proto *l4proto)
|
2012-05-28 21:04:09 +00:00
|
|
|
{
|
2012-06-29 05:23:24 +00:00
|
|
|
if (l4proto->get_net_proto) {
|
|
|
|
/* statically built-in protocols use static per-net */
|
|
|
|
return l4proto->get_net_proto(net);
|
|
|
|
} else if (l4proto->net_id) {
|
|
|
|
/* ... and loadable protocols use dynamic per-net */
|
|
|
|
return net_generic(net, *l4proto->net_id);
|
2012-05-28 21:04:11 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2012-05-28 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int nf_ct_l4proto_register_sysctl(struct net *net,
|
2012-06-21 04:36:41 +00:00
|
|
|
struct nf_proto_net *pn,
|
2017-08-01 10:25:01 +00:00
|
|
|
const struct nf_conntrack_l4proto *l4proto)
|
2006-11-29 01:35:17 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSCTL
|
2012-05-28 21:04:09 +00:00
|
|
|
if (pn->ctl_table != NULL) {
|
|
|
|
err = nf_ct_register_sysctl(net,
|
|
|
|
&pn->ctl_table_header,
|
2012-04-19 13:43:55 +00:00
|
|
|
"net/netfilter",
|
2012-06-21 04:36:41 +00:00
|
|
|
pn->ctl_table);
|
2012-05-28 21:04:09 +00:00
|
|
|
if (err < 0) {
|
|
|
|
if (!pn->users) {
|
|
|
|
kfree(pn->ctl_table);
|
|
|
|
pn->ctl_table = NULL;
|
|
|
|
}
|
|
|
|
}
|
2006-11-29 01:35:17 +00:00
|
|
|
}
|
2006-11-29 01:35:18 +00:00
|
|
|
#endif /* CONFIG_SYSCTL */
|
2006-11-29 01:35:17 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-05-28 21:04:09 +00:00
|
|
|
static
|
|
|
|
void nf_ct_l4proto_unregister_sysctl(struct net *net,
|
2017-08-01 10:25:01 +00:00
|
|
|
struct nf_proto_net *pn,
|
|
|
|
const struct nf_conntrack_l4proto *l4proto)
|
2006-11-29 01:35:17 +00:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_SYSCTL
|
2012-05-28 21:04:09 +00:00
|
|
|
if (pn->ctl_table_header != NULL)
|
|
|
|
nf_ct_unregister_sysctl(&pn->ctl_table_header,
|
|
|
|
&pn->ctl_table,
|
2012-06-21 04:36:41 +00:00
|
|
|
pn->users);
|
2006-11-29 01:35:18 +00:00
|
|
|
#endif /* CONFIG_SYSCTL */
|
2006-11-29 01:35:17 +00:00
|
|
|
}
|
|
|
|
|
2006-11-29 01:35:03 +00:00
|
|
|
/* FIXME: Allow NULL functions and sub in pointers to generic for
|
|
|
|
them. --RR */
|
2016-11-07 17:31:17 +00:00
|
|
|
int nf_ct_l4proto_register_one(struct nf_conntrack_l4proto *l4proto)
|
2006-11-29 01:35:03 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-06-14 09:54:07 +00:00
|
|
|
if (l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos))
|
2007-03-14 23:38:48 +00:00
|
|
|
return -EBUSY;
|
2006-11-29 01:35:08 +00:00
|
|
|
|
2016-11-07 17:31:17 +00:00
|
|
|
if ((l4proto->to_nlattr && !l4proto->nlattr_size) ||
|
|
|
|
(l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
|
2009-03-25 17:24:48 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_lock(&nf_ct_proto_mutex);
|
2007-03-14 23:39:07 +00:00
|
|
|
if (!nf_ct_protos[l4proto->l3proto]) {
|
2006-11-29 01:35:03 +00:00
|
|
|
/* l3proto may be loaded latter. */
|
2010-11-15 18:45:13 +00:00
|
|
|
struct nf_conntrack_l4proto __rcu **proto_array;
|
2006-11-29 01:35:03 +00:00
|
|
|
int i;
|
|
|
|
|
2007-03-14 23:39:07 +00:00
|
|
|
proto_array = kmalloc(MAX_NF_CT_PROTO *
|
|
|
|
sizeof(struct nf_conntrack_l4proto *),
|
|
|
|
GFP_KERNEL);
|
2006-11-29 01:35:03 +00:00
|
|
|
if (proto_array == NULL) {
|
|
|
|
ret = -ENOMEM;
|
2007-03-14 23:37:52 +00:00
|
|
|
goto out_unlock;
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
2007-03-14 23:39:07 +00:00
|
|
|
|
2006-11-29 01:35:03 +00:00
|
|
|
for (i = 0; i < MAX_NF_CT_PROTO; i++)
|
2016-11-07 17:31:17 +00:00
|
|
|
RCU_INIT_POINTER(proto_array[i],
|
|
|
|
&nf_conntrack_l4proto_generic);
|
2010-10-29 17:59:40 +00:00
|
|
|
|
|
|
|
/* Before making proto_array visible to lockless readers,
|
|
|
|
* we must make sure its content is committed to memory.
|
|
|
|
*/
|
|
|
|
smp_wmb();
|
|
|
|
|
2007-03-14 23:39:07 +00:00
|
|
|
nf_ct_protos[l4proto->l3proto] = proto_array;
|
2010-11-15 17:17:21 +00:00
|
|
|
} else if (rcu_dereference_protected(
|
|
|
|
nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
|
|
|
|
lockdep_is_held(&nf_ct_proto_mutex)
|
|
|
|
) != &nf_conntrack_l4proto_generic) {
|
2007-03-14 23:39:07 +00:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto out_unlock;
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
|
|
|
|
2009-03-25 17:24:48 +00:00
|
|
|
l4proto->nla_size = 0;
|
|
|
|
if (l4proto->nlattr_size)
|
|
|
|
l4proto->nla_size += l4proto->nlattr_size();
|
|
|
|
if (l4proto->nlattr_tuple_size)
|
|
|
|
l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
|
|
|
|
|
2007-03-14 23:39:07 +00:00
|
|
|
rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
|
|
|
|
l4proto);
|
2006-11-29 01:35:03 +00:00
|
|
|
out_unlock:
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_unlock(&nf_ct_proto_mutex);
|
2006-11-29 01:35:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-11-07 17:31:17 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2016-11-07 17:31:17 +00:00
|
|
|
int nf_ct_l4proto_pernet_register_one(struct net *net,
|
2017-08-01 10:25:01 +00:00
|
|
|
const struct nf_conntrack_l4proto *l4proto)
|
2006-11-29 01:35:03 +00:00
|
|
|
{
|
2012-05-28 21:04:09 +00:00
|
|
|
int ret = 0;
|
2012-06-21 04:36:41 +00:00
|
|
|
struct nf_proto_net *pn = NULL;
|
2012-05-28 21:04:09 +00:00
|
|
|
|
2012-06-21 04:36:38 +00:00
|
|
|
if (l4proto->init_net) {
|
2012-06-21 04:36:39 +00:00
|
|
|
ret = l4proto->init_net(net, l4proto->l3proto);
|
2012-06-21 04:36:38 +00:00
|
|
|
if (ret < 0)
|
2012-06-21 04:36:41 +00:00
|
|
|
goto out;
|
2012-06-21 04:36:38 +00:00
|
|
|
}
|
2008-10-08 09:35:07 +00:00
|
|
|
|
2012-06-21 04:36:41 +00:00
|
|
|
pn = nf_ct_l4proto_net(net, l4proto);
|
|
|
|
if (pn == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
|
2012-05-28 21:04:09 +00:00
|
|
|
if (ret < 0)
|
2012-06-21 04:36:41 +00:00
|
|
|
goto out;
|
2012-05-28 21:04:09 +00:00
|
|
|
|
2012-06-21 04:36:41 +00:00
|
|
|
pn->users++;
|
|
|
|
out:
|
2012-06-21 04:36:38 +00:00
|
|
|
return ret;
|
2012-05-28 21:04:09 +00:00
|
|
|
}
|
2016-11-07 17:31:17 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one);
|
2012-05-28 21:04:09 +00:00
|
|
|
|
2017-08-01 10:25:01 +00:00
|
|
|
static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
|
2017-05-30 09:31:06 +00:00
|
|
|
|
2012-05-28 21:04:09 +00:00
|
|
|
{
|
2017-06-14 09:54:07 +00:00
|
|
|
BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos));
|
2006-11-29 01:35:08 +00:00
|
|
|
|
2010-11-15 17:17:21 +00:00
|
|
|
BUG_ON(rcu_dereference_protected(
|
|
|
|
nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
|
|
|
|
lockdep_is_held(&nf_ct_proto_mutex)
|
|
|
|
) != l4proto);
|
2007-02-12 19:12:57 +00:00
|
|
|
rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
|
|
|
|
&nf_conntrack_l4proto_generic);
|
2017-05-30 09:31:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 10:25:01 +00:00
|
|
|
void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
|
2017-05-30 09:31:06 +00:00
|
|
|
{
|
|
|
|
mutex_lock(&nf_ct_proto_mutex);
|
|
|
|
__nf_ct_l4proto_unregister_one(l4proto);
|
2007-03-14 23:37:52 +00:00
|
|
|
mutex_unlock(&nf_ct_proto_mutex);
|
2006-11-29 01:35:03 +00:00
|
|
|
|
2007-03-14 23:38:48 +00:00
|
|
|
synchronize_rcu();
|
2012-05-28 21:04:09 +00:00
|
|
|
}
|
2016-11-07 17:31:17 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one);
|
2006-11-29 01:35:17 +00:00
|
|
|
|
2016-11-07 17:31:17 +00:00
|
|
|
void nf_ct_l4proto_pernet_unregister_one(struct net *net,
|
2017-08-01 10:25:01 +00:00
|
|
|
const struct nf_conntrack_l4proto *l4proto)
|
2012-05-28 21:04:09 +00:00
|
|
|
{
|
2017-04-12 20:32:54 +00:00
|
|
|
struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto);
|
2012-06-21 04:36:41 +00:00
|
|
|
|
|
|
|
if (pn == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pn->users--;
|
|
|
|
nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
|
2006-11-29 01:35:03 +00:00
|
|
|
}
|
2016-11-07 17:31:17 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one);
|
|
|
|
|
|
|
|
int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto[],
|
|
|
|
unsigned int num_proto)
|
|
|
|
{
|
|
|
|
int ret = -EINVAL, ver;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_proto; i++) {
|
|
|
|
ret = nf_ct_l4proto_register_one(l4proto[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i != num_proto) {
|
|
|
|
ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4;
|
2017-08-11 22:57:04 +00:00
|
|
|
pr_err("nf_conntrack_ipv%d: can't register l4 %d proto.\n",
|
|
|
|
ver, l4proto[i]->l4proto);
|
2016-11-07 17:31:17 +00:00
|
|
|
nf_ct_l4proto_unregister(l4proto, i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
|
|
|
|
|
|
|
|
int nf_ct_l4proto_pernet_register(struct net *net,
|
2017-08-01 10:25:01 +00:00
|
|
|
struct nf_conntrack_l4proto *const l4proto[],
|
2016-11-07 17:31:17 +00:00
|
|
|
unsigned int num_proto)
|
|
|
|
{
|
|
|
|
int ret = -EINVAL;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_proto; i++) {
|
|
|
|
ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i != num_proto) {
|
2017-08-11 22:57:04 +00:00
|
|
|
pr_err("nf_conntrack_proto_%d %d: pernet registration failed\n",
|
|
|
|
l4proto[i]->l4proto,
|
2016-11-07 17:31:17 +00:00
|
|
|
l4proto[i]->l3proto == PF_INET6 ? 6 : 4);
|
|
|
|
nf_ct_l4proto_pernet_unregister(net, l4proto, i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
|
|
|
|
|
|
|
|
void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto[],
|
|
|
|
unsigned int num_proto)
|
|
|
|
{
|
2017-05-30 09:31:06 +00:00
|
|
|
mutex_lock(&nf_ct_proto_mutex);
|
2016-11-07 17:31:17 +00:00
|
|
|
while (num_proto-- != 0)
|
2017-05-30 09:31:06 +00:00
|
|
|
__nf_ct_l4proto_unregister_one(l4proto[num_proto]);
|
|
|
|
mutex_unlock(&nf_ct_proto_mutex);
|
|
|
|
|
|
|
|
synchronize_net();
|
|
|
|
/* Remove all contrack entries for this protocol */
|
|
|
|
nf_ct_iterate_destroy(kill_l4proto, l4proto);
|
2016-11-07 17:31:17 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
|
|
|
|
|
|
|
|
void nf_ct_l4proto_pernet_unregister(struct net *net,
|
2017-08-01 10:25:01 +00:00
|
|
|
struct nf_conntrack_l4proto *const l4proto[],
|
|
|
|
unsigned int num_proto)
|
2016-11-07 17:31:17 +00:00
|
|
|
{
|
|
|
|
while (num_proto-- != 0)
|
|
|
|
nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]);
|
|
|
|
}
|
2013-01-23 11:51:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
|
2007-03-14 23:38:25 +00:00
|
|
|
|
2013-01-21 22:10:32 +00:00
|
|
|
int nf_conntrack_proto_pernet_init(struct net *net)
|
2007-03-14 23:38:25 +00:00
|
|
|
{
|
|
|
|
int err;
|
2012-06-21 04:36:41 +00:00
|
|
|
struct nf_proto_net *pn = nf_ct_l4proto_net(net,
|
|
|
|
&nf_conntrack_l4proto_generic);
|
|
|
|
|
2012-06-21 04:36:39 +00:00
|
|
|
err = nf_conntrack_l4proto_generic.init_net(net,
|
|
|
|
nf_conntrack_l4proto_generic.l3proto);
|
2012-05-28 21:04:11 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = nf_ct_l4proto_register_sysctl(net,
|
2012-06-21 04:36:41 +00:00
|
|
|
pn,
|
2012-05-28 21:04:11 +00:00
|
|
|
&nf_conntrack_l4proto_generic);
|
2007-03-14 23:38:25 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
2012-06-21 04:36:41 +00:00
|
|
|
pn->users++;
|
2007-03-14 23:38:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-21 22:10:32 +00:00
|
|
|
void nf_conntrack_proto_pernet_fini(struct net *net)
|
2007-03-14 23:38:25 +00:00
|
|
|
{
|
2012-06-21 04:36:41 +00:00
|
|
|
struct nf_proto_net *pn = nf_ct_l4proto_net(net,
|
|
|
|
&nf_conntrack_l4proto_generic);
|
|
|
|
|
|
|
|
pn->users--;
|
2012-05-28 21:04:11 +00:00
|
|
|
nf_ct_l4proto_unregister_sysctl(net,
|
2012-06-21 04:36:41 +00:00
|
|
|
pn,
|
2012-05-28 21:04:11 +00:00
|
|
|
&nf_conntrack_l4proto_generic);
|
2013-01-21 22:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int nf_conntrack_proto_init(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2017-06-14 09:54:07 +00:00
|
|
|
for (i = 0; i < NFPROTO_NUMPROTO; i++)
|
2013-01-21 22:10:32 +00:00
|
|
|
rcu_assign_pointer(nf_ct_l3protos[i],
|
|
|
|
&nf_conntrack_l3proto_generic);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nf_conntrack_proto_fini(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
/* free l3proto protocol tables */
|
2017-06-14 09:54:07 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++)
|
2013-01-21 22:10:32 +00:00
|
|
|
kfree(nf_ct_protos[i]);
|
2007-03-14 23:38:25 +00:00
|
|
|
}
|