2007-02-12 19:15:49 +00:00
|
|
|
/* netfilter.c: look after the filters for various protocols.
|
2005-08-10 03:21:49 +00:00
|
|
|
* Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
|
|
|
|
*
|
|
|
|
* Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
|
|
|
|
* way.
|
|
|
|
*
|
|
|
|
* Rusty Russell (C)2000 -- This code is GPL.
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/wait.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/inetdevice.h>
|
|
|
|
#include <linux/proc_fs.h>
|
2007-02-12 19:09:55 +00:00
|
|
|
#include <linux/mutex.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>
|
2007-09-12 10:01:34 +00:00
|
|
|
#include <net/net_namespace.h>
|
2005-08-10 03:21:49 +00:00
|
|
|
#include <net/sock.h>
|
|
|
|
|
|
|
|
#include "nf_internals.h"
|
|
|
|
|
2007-02-12 19:09:55 +00:00
|
|
|
static DEFINE_MUTEX(afinfo_mutex);
|
2006-04-06 21:18:09 +00:00
|
|
|
|
2010-03-09 19:59:15 +00:00
|
|
|
const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
|
2006-04-06 21:18:09 +00:00
|
|
|
EXPORT_SYMBOL(nf_afinfo);
|
|
|
|
|
2007-12-18 06:42:27 +00:00
|
|
|
int nf_register_afinfo(const struct nf_afinfo *afinfo)
|
2006-04-06 21:18:09 +00:00
|
|
|
{
|
2007-02-12 19:09:55 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = mutex_lock_interruptible(&afinfo_mutex);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2006-04-06 21:18:09 +00:00
|
|
|
rcu_assign_pointer(nf_afinfo[afinfo->family], afinfo);
|
2007-02-12 19:09:55 +00:00
|
|
|
mutex_unlock(&afinfo_mutex);
|
2006-04-06 21:18:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_register_afinfo);
|
|
|
|
|
2007-12-18 06:42:27 +00:00
|
|
|
void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
|
2006-04-06 21:18:09 +00:00
|
|
|
{
|
2007-02-12 19:09:55 +00:00
|
|
|
mutex_lock(&afinfo_mutex);
|
2006-04-06 21:18:09 +00:00
|
|
|
rcu_assign_pointer(nf_afinfo[afinfo->family], NULL);
|
2007-02-12 19:09:55 +00:00
|
|
|
mutex_unlock(&afinfo_mutex);
|
2006-04-06 21:18:09 +00:00
|
|
|
synchronize_rcu();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
|
|
|
|
|
2008-10-08 09:35:00 +00:00
|
|
|
struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
|
2005-08-10 03:21:49 +00:00
|
|
|
EXPORT_SYMBOL(nf_hooks);
|
2007-02-12 19:10:14 +00:00
|
|
|
static DEFINE_MUTEX(nf_hook_mutex);
|
2005-08-10 03:21:49 +00:00
|
|
|
|
|
|
|
int nf_register_hook(struct nf_hook_ops *reg)
|
|
|
|
{
|
2007-12-05 07:22:26 +00:00
|
|
|
struct nf_hook_ops *elem;
|
2007-02-12 19:10:14 +00:00
|
|
|
int err;
|
2005-08-10 03:21:49 +00:00
|
|
|
|
2007-02-12 19:10:14 +00:00
|
|
|
err = mutex_lock_interruptible(&nf_hook_mutex);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2007-12-05 07:22:26 +00:00
|
|
|
list_for_each_entry(elem, &nf_hooks[reg->pf][reg->hooknum], list) {
|
|
|
|
if (reg->priority < elem->priority)
|
2005-08-10 03:21:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-12-05 07:22:26 +00:00
|
|
|
list_add_rcu(®->list, elem->list.prev);
|
2007-02-12 19:10:14 +00:00
|
|
|
mutex_unlock(&nf_hook_mutex);
|
2005-08-10 03:21:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_register_hook);
|
|
|
|
|
|
|
|
void nf_unregister_hook(struct nf_hook_ops *reg)
|
|
|
|
{
|
2007-02-12 19:10:14 +00:00
|
|
|
mutex_lock(&nf_hook_mutex);
|
2005-08-10 03:21:49 +00:00
|
|
|
list_del_rcu(®->list);
|
2007-02-12 19:10:14 +00:00
|
|
|
mutex_unlock(&nf_hook_mutex);
|
2005-08-10 03:21:49 +00:00
|
|
|
|
|
|
|
synchronize_net();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_unregister_hook);
|
|
|
|
|
2006-04-06 21:09:12 +00:00
|
|
|
int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
err = nf_register_hook(®[i]);
|
|
|
|
if (err)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (i > 0)
|
|
|
|
nf_unregister_hooks(reg, i);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_register_hooks);
|
|
|
|
|
|
|
|
void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n)
|
|
|
|
{
|
2010-10-04 20:24:12 +00:00
|
|
|
while (n-- > 0)
|
|
|
|
nf_unregister_hook(®[n]);
|
2006-04-06 21:09:12 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_unregister_hooks);
|
|
|
|
|
2005-08-10 03:21:49 +00:00
|
|
|
unsigned int nf_iterate(struct list_head *head,
|
2007-10-15 07:53:15 +00:00
|
|
|
struct sk_buff *skb,
|
2008-10-08 09:35:00 +00:00
|
|
|
unsigned int hook,
|
2005-08-10 03:21:49 +00:00
|
|
|
const struct net_device *indev,
|
|
|
|
const struct net_device *outdev,
|
|
|
|
struct list_head **i,
|
|
|
|
int (*okfn)(struct sk_buff *),
|
|
|
|
int hook_thresh)
|
|
|
|
{
|
|
|
|
unsigned int verdict;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The caller must not block between calls to this
|
|
|
|
* function because of risk of continuing from deleted element.
|
|
|
|
*/
|
|
|
|
list_for_each_continue_rcu(*i, head) {
|
|
|
|
struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
|
|
|
|
|
|
|
|
if (hook_thresh > elem->priority)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Optimization: we don't need to hold module
|
2007-02-12 19:15:49 +00:00
|
|
|
reference here, since function can't sleep. --RR */
|
2005-08-10 03:21:49 +00:00
|
|
|
verdict = elem->hook(hook, skb, indev, outdev, okfn);
|
|
|
|
if (verdict != NF_ACCEPT) {
|
|
|
|
#ifdef CONFIG_NETFILTER_DEBUG
|
|
|
|
if (unlikely((verdict & NF_VERDICT_MASK)
|
|
|
|
> NF_MAX_VERDICT)) {
|
|
|
|
NFDEBUG("Evil return from %p(%u).\n",
|
2007-02-12 19:15:49 +00:00
|
|
|
elem->hook, hook);
|
2005-08-10 03:21:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (verdict != NF_REPEAT)
|
|
|
|
return verdict;
|
|
|
|
*i = (*i)->prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NF_ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns 1 if okfn() needs to be executed by the caller,
|
|
|
|
* -EPERM for NF_DROP, 0 otherwise. */
|
2008-10-08 09:35:00 +00:00
|
|
|
int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
|
2005-08-10 03:21:49 +00:00
|
|
|
struct net_device *indev,
|
|
|
|
struct net_device *outdev,
|
|
|
|
int (*okfn)(struct sk_buff *),
|
|
|
|
int hook_thresh)
|
|
|
|
{
|
|
|
|
struct list_head *elem;
|
|
|
|
unsigned int verdict;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* We may already have this, but read-locks nest anyway */
|
|
|
|
rcu_read_lock();
|
|
|
|
|
|
|
|
elem = &nf_hooks[pf][hook];
|
|
|
|
next_hook:
|
2007-10-15 07:53:15 +00:00
|
|
|
verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev,
|
2005-08-10 03:21:49 +00:00
|
|
|
outdev, &elem, okfn, hook_thresh);
|
|
|
|
if (verdict == NF_ACCEPT || verdict == NF_STOP) {
|
|
|
|
ret = 1;
|
|
|
|
} else if (verdict == NF_DROP) {
|
2007-10-15 07:53:15 +00:00
|
|
|
kfree_skb(skb);
|
2005-08-10 03:21:49 +00:00
|
|
|
ret = -EPERM;
|
2007-12-05 09:27:46 +00:00
|
|
|
} else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
|
2007-10-15 07:53:15 +00:00
|
|
|
if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
|
2005-08-10 03:21:49 +00:00
|
|
|
verdict >> NF_VERDICT_BITS))
|
|
|
|
goto next_hook;
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_hook_slow);
|
|
|
|
|
|
|
|
|
2007-10-14 07:39:18 +00:00
|
|
|
int skb_make_writable(struct sk_buff *skb, unsigned int writable_len)
|
2005-08-10 03:21:49 +00:00
|
|
|
{
|
2007-10-14 07:39:18 +00:00
|
|
|
if (writable_len > skb->len)
|
2005-08-10 03:21:49 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Not exclusive use of packet? Must copy. */
|
2007-10-14 07:39:18 +00:00
|
|
|
if (!skb_cloned(skb)) {
|
|
|
|
if (writable_len <= skb_headlen(skb))
|
|
|
|
return 1;
|
|
|
|
} else if (skb_clone_writable(skb, writable_len))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (writable_len <= skb_headlen(skb))
|
|
|
|
writable_len = 0;
|
|
|
|
else
|
|
|
|
writable_len -= skb_headlen(skb);
|
|
|
|
|
|
|
|
return !!__pskb_pull_tail(skb, writable_len);
|
2005-08-10 03:21:49 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(skb_make_writable);
|
|
|
|
|
2007-03-23 18:17:07 +00:00
|
|
|
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
|
2005-08-10 03:21:49 +00:00
|
|
|
/* This does not belong here, but locally generated errors need it if connection
|
|
|
|
tracking in use: without this, connection may not be in hash table, and hence
|
|
|
|
manufactured ICMP or RST packets will not be associated with it. */
|
|
|
|
void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
|
|
|
|
EXPORT_SYMBOL(ip_ct_attach);
|
|
|
|
|
|
|
|
void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
void (*attach)(struct sk_buff *, struct sk_buff *);
|
|
|
|
|
2007-02-12 19:09:19 +00:00
|
|
|
if (skb->nfct) {
|
|
|
|
rcu_read_lock();
|
|
|
|
attach = rcu_dereference(ip_ct_attach);
|
|
|
|
if (attach)
|
|
|
|
attach(new, skb);
|
|
|
|
rcu_read_unlock();
|
2005-08-10 03:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_ct_attach);
|
2007-03-23 18:17:27 +00:00
|
|
|
|
|
|
|
void (*nf_ct_destroy)(struct nf_conntrack *);
|
|
|
|
EXPORT_SYMBOL(nf_ct_destroy);
|
|
|
|
|
|
|
|
void nf_conntrack_destroy(struct nf_conntrack *nfct)
|
|
|
|
{
|
|
|
|
void (*destroy)(struct nf_conntrack *);
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
destroy = rcu_dereference(nf_ct_destroy);
|
|
|
|
BUG_ON(destroy == NULL);
|
|
|
|
destroy(nfct);
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(nf_conntrack_destroy);
|
|
|
|
#endif /* CONFIG_NF_CONNTRACK */
|
2005-08-10 03:21:49 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct proc_dir_entry *proc_net_netfilter;
|
|
|
|
EXPORT_SYMBOL(proc_net_netfilter);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void __init netfilter_init(void)
|
|
|
|
{
|
|
|
|
int i, h;
|
2008-10-08 09:35:00 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
|
2005-08-10 03:21:49 +00:00
|
|
|
for (h = 0; h < NF_MAX_HOOKS; h++)
|
|
|
|
INIT_LIST_HEAD(&nf_hooks[i][h]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2007-09-12 10:01:34 +00:00
|
|
|
proc_net_netfilter = proc_mkdir("netfilter", init_net.proc_net);
|
2005-08-10 03:21:49 +00:00
|
|
|
if (!proc_net_netfilter)
|
|
|
|
panic("cannot create netfilter proc entry");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (netfilter_queue_init() < 0)
|
|
|
|
panic("cannot initialize nf_queue");
|
|
|
|
if (netfilter_log_init() < 0)
|
|
|
|
panic("cannot initialize nf_log");
|
|
|
|
}
|
2008-01-15 07:48:39 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_SYSCTL
|
|
|
|
struct ctl_path nf_net_netfilter_sysctl_path[] = {
|
2009-11-05 21:32:03 +00:00
|
|
|
{ .procname = "net", },
|
|
|
|
{ .procname = "netfilter", },
|
2008-01-15 07:48:39 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(nf_net_netfilter_sysctl_path);
|
|
|
|
#endif /* CONFIG_SYSCTL */
|