2005-08-10 02:58:39 +00:00
|
|
|
/*
|
|
|
|
* This is a module which is used for logging packets to userspace via
|
|
|
|
* nfetlink.
|
|
|
|
*
|
|
|
|
* (C) 2005 by Harald Welte <laforge@netfilter.org>
|
|
|
|
*
|
|
|
|
* Based on the old ipv4-only ipt_ULOG.c:
|
|
|
|
* (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
|
|
|
|
*
|
|
|
|
* 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/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/netfilter/nfnetlink.h>
|
|
|
|
#include <linux/netfilter/nfnetlink_log.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/sysctl.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/random.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>
|
2005-08-10 02:58:39 +00:00
|
|
|
#include <net/sock.h>
|
2007-12-18 06:38:49 +00:00
|
|
|
#include <net/netfilter/nf_log.h>
|
2008-11-18 11:16:52 +00:00
|
|
|
#include <net/netfilter/nfnetlink_log.h>
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2011-07-26 23:09:06 +00:00
|
|
|
#include <linux/atomic.h>
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2005-08-10 03:22:10 +00:00
|
|
|
#ifdef CONFIG_BRIDGE_NETFILTER
|
|
|
|
#include "../bridge/br_private.h"
|
|
|
|
#endif
|
|
|
|
|
2006-02-04 10:13:14 +00:00
|
|
|
#define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
|
2009-02-18 14:29:49 +00:00
|
|
|
#define NFULNL_TIMEOUT_DEFAULT 100 /* every second */
|
2005-08-10 02:58:39 +00:00
|
|
|
#define NFULNL_QTHRESH_DEFAULT 100 /* 100 packets */
|
2007-09-28 21:45:52 +00:00
|
|
|
#define NFULNL_COPY_RANGE_MAX 0xFFFF /* max packet size is limited by 16-bit struct nfattr nfa_len field */
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
#define PRINTR(x, args...) do { if (net_ratelimit()) \
|
|
|
|
printk(x, ## args); } while (0);
|
|
|
|
|
|
|
|
struct nfulnl_instance {
|
|
|
|
struct hlist_node hlist; /* global list of instances */
|
|
|
|
spinlock_t lock;
|
|
|
|
atomic_t use; /* use count */
|
|
|
|
|
|
|
|
unsigned int qlen; /* number of nlmsgs in skb */
|
|
|
|
struct sk_buff *skb; /* pre-allocatd skb */
|
|
|
|
struct timer_list timer;
|
|
|
|
int peer_pid; /* PID of the peer process */
|
|
|
|
|
|
|
|
/* configurable parameters */
|
|
|
|
unsigned int flushtimeout; /* timeout until queue flush */
|
|
|
|
unsigned int nlbufsiz; /* netlink buffer allocation size */
|
|
|
|
unsigned int qthreshold; /* threshold of the queue */
|
|
|
|
u_int32_t copy_range;
|
2006-03-21 01:15:11 +00:00
|
|
|
u_int32_t seq; /* instance-local sequential counter */
|
2005-08-10 02:58:39 +00:00
|
|
|
u_int16_t group_num; /* number of this queue */
|
2006-03-21 01:15:11 +00:00
|
|
|
u_int16_t flags;
|
2007-02-12 19:15:49 +00:00
|
|
|
u_int8_t copy_mode;
|
2010-06-09 16:14:58 +00:00
|
|
|
struct rcu_head rcu;
|
2005-08-10 02:58:39 +00:00
|
|
|
};
|
|
|
|
|
2010-06-09 16:14:58 +00:00
|
|
|
static DEFINE_SPINLOCK(instances_lock);
|
2006-03-21 01:15:11 +00:00
|
|
|
static atomic_t global_seq;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
#define INSTANCE_BUCKETS 16
|
|
|
|
static struct hlist_head instance_table[INSTANCE_BUCKETS];
|
|
|
|
static unsigned int hash_init;
|
|
|
|
|
|
|
|
static inline u_int8_t instance_hashfn(u_int16_t group_num)
|
|
|
|
{
|
|
|
|
return ((group_num & 0xff) % INSTANCE_BUCKETS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfulnl_instance *
|
|
|
|
__instance_lookup(u_int16_t group_num)
|
|
|
|
{
|
|
|
|
struct hlist_head *head;
|
|
|
|
struct hlist_node *pos;
|
|
|
|
struct nfulnl_instance *inst;
|
|
|
|
|
|
|
|
head = &instance_table[instance_hashfn(group_num)];
|
2010-06-09 16:14:58 +00:00
|
|
|
hlist_for_each_entry_rcu(inst, pos, head, hlist) {
|
2005-08-10 02:58:39 +00:00
|
|
|
if (inst->group_num == group_num)
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
instance_get(struct nfulnl_instance *inst)
|
|
|
|
{
|
|
|
|
atomic_inc(&inst->use);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfulnl_instance *
|
|
|
|
instance_lookup_get(u_int16_t group_num)
|
|
|
|
{
|
|
|
|
struct nfulnl_instance *inst;
|
|
|
|
|
2010-06-09 16:14:58 +00:00
|
|
|
rcu_read_lock_bh();
|
2005-08-10 02:58:39 +00:00
|
|
|
inst = __instance_lookup(group_num);
|
2010-06-14 14:15:23 +00:00
|
|
|
if (inst && !atomic_inc_not_zero(&inst->use))
|
|
|
|
inst = NULL;
|
2010-06-09 16:14:58 +00:00
|
|
|
rcu_read_unlock_bh();
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
2010-06-09 16:14:58 +00:00
|
|
|
static void nfulnl_instance_free_rcu(struct rcu_head *head)
|
|
|
|
{
|
|
|
|
kfree(container_of(head, struct nfulnl_instance, rcu));
|
|
|
|
module_put(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
static void
|
|
|
|
instance_put(struct nfulnl_instance *inst)
|
|
|
|
{
|
2010-06-09 16:14:58 +00:00
|
|
|
if (inst && atomic_dec_and_test(&inst->use))
|
|
|
|
call_rcu_bh(&inst->rcu, nfulnl_instance_free_rcu);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nfulnl_timer(unsigned long data);
|
|
|
|
|
|
|
|
static struct nfulnl_instance *
|
|
|
|
instance_create(u_int16_t group_num, int pid)
|
|
|
|
{
|
|
|
|
struct nfulnl_instance *inst;
|
2007-12-18 06:41:21 +00:00
|
|
|
int err;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_lock_bh(&instances_lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
if (__instance_lookup(group_num)) {
|
2007-12-18 06:41:21 +00:00
|
|
|
err = -EEXIST;
|
2005-08-10 02:58:39 +00:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2005-11-03 18:20:07 +00:00
|
|
|
inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
|
2007-12-18 06:41:21 +00:00
|
|
|
if (!inst) {
|
|
|
|
err = -ENOMEM;
|
2005-08-10 02:58:39 +00:00
|
|
|
goto out_unlock;
|
2007-12-18 06:41:21 +00:00
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-09-28 21:45:27 +00:00
|
|
|
if (!try_module_get(THIS_MODULE)) {
|
|
|
|
kfree(inst);
|
2007-12-18 06:41:21 +00:00
|
|
|
err = -EAGAIN;
|
2007-09-28 21:45:27 +00:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
INIT_HLIST_NODE(&inst->hlist);
|
2006-01-04 21:56:54 +00:00
|
|
|
spin_lock_init(&inst->lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
/* needs to be two, since we _put() after creation */
|
|
|
|
atomic_set(&inst->use, 2);
|
|
|
|
|
2007-03-23 18:16:30 +00:00
|
|
|
setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
inst->peer_pid = pid;
|
|
|
|
inst->group_num = group_num;
|
|
|
|
|
|
|
|
inst->qthreshold = NFULNL_QTHRESH_DEFAULT;
|
|
|
|
inst->flushtimeout = NFULNL_TIMEOUT_DEFAULT;
|
|
|
|
inst->nlbufsiz = NFULNL_NLBUFSIZ_DEFAULT;
|
|
|
|
inst->copy_mode = NFULNL_COPY_PACKET;
|
2007-09-28 21:45:52 +00:00
|
|
|
inst->copy_range = NFULNL_COPY_RANGE_MAX;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2010-06-14 14:15:23 +00:00
|
|
|
hlist_add_head_rcu(&inst->hlist,
|
2005-08-10 02:58:39 +00:00
|
|
|
&instance_table[instance_hashfn(group_num)]);
|
|
|
|
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_unlock_bh(&instances_lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
return inst;
|
|
|
|
|
|
|
|
out_unlock:
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_unlock_bh(&instances_lock);
|
2007-12-18 06:41:21 +00:00
|
|
|
return ERR_PTR(err);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 21:44:21 +00:00
|
|
|
static void __nfulnl_flush(struct nfulnl_instance *inst);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2010-06-14 14:15:23 +00:00
|
|
|
/* called with BH disabled */
|
2005-08-10 02:58:39 +00:00
|
|
|
static void
|
2007-03-23 18:12:50 +00:00
|
|
|
__instance_destroy(struct nfulnl_instance *inst)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
/* first pull it out of the global list */
|
2010-06-14 14:15:23 +00:00
|
|
|
hlist_del_rcu(&inst->hlist);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
/* then flush all pending packets from skb */
|
|
|
|
|
2010-06-14 14:15:23 +00:00
|
|
|
spin_lock(&inst->lock);
|
|
|
|
|
|
|
|
/* lockless readers wont be able to use us */
|
|
|
|
inst->copy_mode = NFULNL_COPY_DISABLED;
|
|
|
|
|
2007-09-28 21:44:21 +00:00
|
|
|
if (inst->skb)
|
|
|
|
__nfulnl_flush(inst);
|
2010-06-14 14:15:23 +00:00
|
|
|
spin_unlock(&inst->lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
/* and finally put the refcount */
|
|
|
|
instance_put(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
instance_destroy(struct nfulnl_instance *inst)
|
|
|
|
{
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_lock_bh(&instances_lock);
|
2007-03-23 18:12:50 +00:00
|
|
|
__instance_destroy(inst);
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_unlock_bh(&instances_lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
|
|
|
|
unsigned int range)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
spin_lock_bh(&inst->lock);
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
switch (mode) {
|
|
|
|
case NFULNL_COPY_NONE:
|
|
|
|
case NFULNL_COPY_META:
|
|
|
|
inst->copy_mode = mode;
|
|
|
|
inst->copy_range = 0;
|
|
|
|
break;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
case NFULNL_COPY_PACKET:
|
|
|
|
inst->copy_mode = mode;
|
2007-09-28 21:45:52 +00:00
|
|
|
inst->copy_range = min_t(unsigned int,
|
|
|
|
range, NFULNL_COPY_RANGE_MAX);
|
2005-08-10 02:58:39 +00:00
|
|
|
break;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
default:
|
|
|
|
status = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_bh(&inst->lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
spin_lock_bh(&inst->lock);
|
|
|
|
if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
|
|
|
|
status = -ERANGE;
|
|
|
|
else if (nlbufsiz > 131072)
|
|
|
|
status = -ERANGE;
|
|
|
|
else {
|
|
|
|
inst->nlbufsiz = nlbufsiz;
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
spin_unlock_bh(&inst->lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
|
|
|
|
{
|
|
|
|
spin_lock_bh(&inst->lock);
|
|
|
|
inst->flushtimeout = timeout;
|
|
|
|
spin_unlock_bh(&inst->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
|
|
|
|
{
|
|
|
|
spin_lock_bh(&inst->lock);
|
|
|
|
inst->qthreshold = qthresh;
|
|
|
|
spin_unlock_bh(&inst->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 01:15:11 +00:00
|
|
|
static int
|
|
|
|
nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
|
|
|
|
{
|
|
|
|
spin_lock_bh(&inst->lock);
|
2006-05-19 09:17:18 +00:00
|
|
|
inst->flags = flags;
|
2006-03-21 01:15:11 +00:00
|
|
|
spin_unlock_bh(&inst->lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:45:06 +00:00
|
|
|
static struct sk_buff *
|
|
|
|
nfulnl_alloc_skb(unsigned int inst_size, unsigned int pkt_size)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
2006-02-04 10:13:57 +00:00
|
|
|
unsigned int n;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
/* alloc skb which should be big enough for a whole multipart
|
|
|
|
* message. WARNING: has to be <= 128k due to slab restrictions */
|
|
|
|
|
2006-02-04 10:13:57 +00:00
|
|
|
n = max(inst_size, pkt_size);
|
|
|
|
skb = alloc_skb(n, GFP_ATOMIC);
|
2005-08-10 02:58:39 +00:00
|
|
|
if (!skb) {
|
2006-02-04 10:13:57 +00:00
|
|
|
if (n > pkt_size) {
|
|
|
|
/* try to allocate only as much as we need for current
|
|
|
|
* packet */
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2006-02-04 10:13:57 +00:00
|
|
|
skb = alloc_skb(pkt_size, GFP_ATOMIC);
|
|
|
|
if (!skb)
|
2011-08-29 21:17:25 +00:00
|
|
|
pr_err("nfnetlink_log: can't even alloc %u bytes\n",
|
|
|
|
pkt_size);
|
2006-02-04 10:13:57 +00:00
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
__nfulnl_send(struct nfulnl_instance *inst)
|
|
|
|
{
|
2007-09-18 20:07:15 +00:00
|
|
|
int status = -1;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2012-06-27 04:34:03 +00:00
|
|
|
if (inst->qlen > 1) {
|
|
|
|
struct nlmsghdr *nlh = nlmsg_put(inst->skb, 0, 0,
|
|
|
|
NLMSG_DONE,
|
|
|
|
sizeof(struct nfgenmsg),
|
|
|
|
0);
|
|
|
|
if (!nlh)
|
|
|
|
goto out;
|
|
|
|
}
|
2010-01-13 15:02:14 +00:00
|
|
|
status = nfnetlink_unicast(inst->skb, &init_net, inst->peer_pid,
|
|
|
|
MSG_DONTWAIT);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
inst->qlen = 0;
|
|
|
|
inst->skb = NULL;
|
2012-06-27 04:34:03 +00:00
|
|
|
out:
|
2005-08-10 02:58:39 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:44:21 +00:00
|
|
|
static void
|
|
|
|
__nfulnl_flush(struct nfulnl_instance *inst)
|
|
|
|
{
|
|
|
|
/* timer holds a reference */
|
|
|
|
if (del_timer(&inst->timer))
|
|
|
|
instance_put(inst);
|
|
|
|
if (inst->skb)
|
|
|
|
__nfulnl_send(inst);
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:45:06 +00:00
|
|
|
static void
|
|
|
|
nfulnl_timer(unsigned long data)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
2007-02-12 19:15:49 +00:00
|
|
|
struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
spin_lock_bh(&inst->lock);
|
2007-03-23 18:12:21 +00:00
|
|
|
if (inst->skb)
|
|
|
|
__nfulnl_send(inst);
|
2005-08-10 02:58:39 +00:00
|
|
|
spin_unlock_bh(&inst->lock);
|
2007-03-04 23:58:40 +00:00
|
|
|
instance_put(inst);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 01:15:11 +00:00
|
|
|
/* This is an inline function, we don't really care about a long
|
|
|
|
* list of arguments */
|
2007-02-12 19:15:49 +00:00
|
|
|
static inline int
|
2005-08-10 02:58:39 +00:00
|
|
|
__build_packet_message(struct nfulnl_instance *inst,
|
2007-02-12 19:15:49 +00:00
|
|
|
const struct sk_buff *skb,
|
2005-08-10 02:58:39 +00:00
|
|
|
unsigned int data_len,
|
2008-10-08 09:35:00 +00:00
|
|
|
u_int8_t pf,
|
2005-08-10 02:58:39 +00:00
|
|
|
unsigned int hooknum,
|
|
|
|
const struct net_device *indev,
|
|
|
|
const struct net_device *outdev,
|
2006-11-29 01:35:34 +00:00
|
|
|
const char *prefix, unsigned int plen)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
struct nfulnl_msg_packet_hdr pmsg;
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
struct nfgenmsg *nfmsg;
|
2007-04-20 03:29:13 +00:00
|
|
|
sk_buff_data_t old_tail = inst->skb->tail;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2012-06-27 04:34:03 +00:00
|
|
|
nlh = nlmsg_put(inst->skb, 0, 0,
|
2005-08-10 02:58:39 +00:00
|
|
|
NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
|
2012-06-27 04:34:03 +00:00
|
|
|
sizeof(struct nfgenmsg), 0);
|
|
|
|
if (!nlh)
|
|
|
|
return -1;
|
|
|
|
nfmsg = nlmsg_data(nlh);
|
2005-08-10 02:58:39 +00:00
|
|
|
nfmsg->nfgen_family = pf;
|
|
|
|
nfmsg->version = NFNETLINK_V0;
|
|
|
|
nfmsg->res_id = htons(inst->group_num);
|
|
|
|
|
2006-11-03 08:59:17 +00:00
|
|
|
pmsg.hw_protocol = skb->protocol;
|
2005-08-10 02:58:39 +00:00
|
|
|
pmsg.hook = hooknum;
|
|
|
|
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2012-03-30 03:31:16 +00:00
|
|
|
if (prefix &&
|
|
|
|
nla_put(inst->skb, NFULA_PREFIX, plen, prefix))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
if (indev) {
|
2005-08-10 03:22:10 +00:00
|
|
|
#ifndef CONFIG_BRIDGE_NETFILTER
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
|
|
|
|
htonl(indev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 03:22:10 +00:00
|
|
|
#else
|
|
|
|
if (pf == PF_BRIDGE) {
|
|
|
|
/* Case 1: outdev is physical input device, we need to
|
|
|
|
* look for bridge group (when called from
|
|
|
|
* netfilter_bridge) */
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
|
|
|
|
htonl(indev->ifindex)) ||
|
2005-08-10 03:22:10 +00:00
|
|
|
/* this is the bridge group "brX" */
|
2010-06-15 06:50:45 +00:00
|
|
|
/* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
|
2012-03-30 03:31:16 +00:00
|
|
|
nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
|
|
|
|
htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 03:22:10 +00:00
|
|
|
} else {
|
|
|
|
/* Case 2: indev is bridge group, we need to look for
|
|
|
|
* physical device (when called from ipv4) */
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
|
|
|
|
htonl(indev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
if (skb->nf_bridge && skb->nf_bridge->physindev &&
|
|
|
|
nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
|
|
|
|
htonl(skb->nf_bridge->physindev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 03:22:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outdev) {
|
2005-08-10 03:22:10 +00:00
|
|
|
#ifndef CONFIG_BRIDGE_NETFILTER
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
|
|
|
|
htonl(outdev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 03:22:10 +00:00
|
|
|
#else
|
|
|
|
if (pf == PF_BRIDGE) {
|
|
|
|
/* Case 1: outdev is physical output device, we need to
|
|
|
|
* look for bridge group (when called from
|
|
|
|
* netfilter_bridge) */
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
|
|
|
|
htonl(outdev->ifindex)) ||
|
2005-08-10 03:22:10 +00:00
|
|
|
/* this is the bridge group "brX" */
|
2010-06-15 06:50:45 +00:00
|
|
|
/* rcu_read_lock()ed by nf_hook_slow or nf_log_packet */
|
2012-03-30 03:31:16 +00:00
|
|
|
nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
|
|
|
|
htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 03:22:10 +00:00
|
|
|
} else {
|
|
|
|
/* Case 2: indev is a bridge group, we need to look
|
|
|
|
* for physical device (when called from ipv4) */
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
|
|
|
|
htonl(outdev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
if (skb->nf_bridge && skb->nf_bridge->physoutdev &&
|
|
|
|
nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
|
|
|
|
htonl(skb->nf_bridge->physoutdev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 03:22:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2012-03-30 03:31:16 +00:00
|
|
|
if (skb->mark &&
|
|
|
|
nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2011-06-16 15:27:04 +00:00
|
|
|
if (indev && skb->dev &&
|
|
|
|
skb->mac_header != skb->network_header) {
|
2005-08-10 02:58:39 +00:00
|
|
|
struct nfulnl_msg_packet_hw phw;
|
2007-09-27 05:13:38 +00:00
|
|
|
int len = dev_parse_header(skb, phw.hw_addr);
|
|
|
|
if (len > 0) {
|
|
|
|
phw.hw_addrlen = htons(len);
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put(inst->skb, NFULA_HWADDR, sizeof(phw), &phw))
|
|
|
|
goto nla_put_failure;
|
2007-09-27 05:13:38 +00:00
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2008-07-21 17:02:35 +00:00
|
|
|
if (indev && skb_mac_header_was_set(skb)) {
|
2012-08-19 10:16:08 +00:00
|
|
|
if (nla_put_be16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type)) ||
|
2012-03-30 03:31:16 +00:00
|
|
|
nla_put_be16(inst->skb, NFULA_HWLEN,
|
|
|
|
htons(skb->dev->hard_header_len)) ||
|
|
|
|
nla_put(inst->skb, NFULA_HWHEADER, skb->dev->hard_header_len,
|
|
|
|
skb_mac_header(skb)))
|
|
|
|
goto nla_put_failure;
|
2008-07-21 17:02:35 +00:00
|
|
|
}
|
|
|
|
|
2007-04-19 23:16:32 +00:00
|
|
|
if (skb->tstamp.tv64) {
|
2005-08-10 02:58:39 +00:00
|
|
|
struct nfulnl_msg_packet_timestamp ts;
|
2007-04-19 23:16:32 +00:00
|
|
|
struct timeval tv = ktime_to_timeval(skb->tstamp);
|
|
|
|
ts.sec = cpu_to_be64(tv.tv_sec);
|
|
|
|
ts.usec = cpu_to_be64(tv.tv_usec);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* UID */
|
|
|
|
if (skb->sk) {
|
|
|
|
read_lock_bh(&skb->sk->sk_callback_lock);
|
|
|
|
if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
|
2008-11-13 23:39:25 +00:00
|
|
|
struct file *file = skb->sk->sk_socket->file;
|
|
|
|
__be32 uid = htonl(file->f_cred->fsuid);
|
|
|
|
__be32 gid = htonl(file->f_cred->fsgid);
|
2005-08-10 02:58:39 +00:00
|
|
|
read_unlock_bh(&skb->sk->sk_callback_lock);
|
2012-03-30 03:31:16 +00:00
|
|
|
if (nla_put_be32(inst->skb, NFULA_UID, uid) ||
|
|
|
|
nla_put_be32(inst->skb, NFULA_GID, gid))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 02:58:39 +00:00
|
|
|
} else
|
|
|
|
read_unlock_bh(&skb->sk->sk_callback_lock);
|
|
|
|
}
|
|
|
|
|
2006-03-21 01:15:11 +00:00
|
|
|
/* local sequence number */
|
2012-03-30 03:31:16 +00:00
|
|
|
if ((inst->flags & NFULNL_CFG_F_SEQ) &&
|
|
|
|
nla_put_be32(inst->skb, NFULA_SEQ, htonl(inst->seq++)))
|
|
|
|
goto nla_put_failure;
|
2007-12-18 06:41:35 +00:00
|
|
|
|
2006-03-21 01:15:11 +00:00
|
|
|
/* global sequence number */
|
2012-03-30 03:31:16 +00:00
|
|
|
if ((inst->flags & NFULNL_CFG_F_SEQ_GLOBAL) &&
|
|
|
|
nla_put_be32(inst->skb, NFULA_SEQ_GLOBAL,
|
|
|
|
htonl(atomic_inc_return(&global_seq))))
|
|
|
|
goto nla_put_failure;
|
2006-03-21 01:15:11 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
if (data_len) {
|
2007-09-28 21:37:03 +00:00
|
|
|
struct nlattr *nla;
|
|
|
|
int size = nla_attr_size(data_len);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
|
2005-08-10 02:58:39 +00:00
|
|
|
printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
|
2012-06-27 04:34:03 +00:00
|
|
|
return -1;
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
|
|
|
|
nla->nla_type = NFULA_PAYLOAD;
|
|
|
|
nla->nla_len = size;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
|
2005-08-10 02:58:39 +00:00
|
|
|
BUG();
|
|
|
|
}
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
nlh->nlmsg_len = inst->skb->tail - old_tail;
|
|
|
|
return 0;
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
nla_put_failure:
|
2005-08-10 02:58:39 +00:00
|
|
|
PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
|
|
|
|
|
|
|
|
static struct nf_loginfo default_loginfo = {
|
|
|
|
.type = NF_LOG_TYPE_ULOG,
|
|
|
|
.u = {
|
|
|
|
.ulog = {
|
|
|
|
.copy_len = 0xffff,
|
|
|
|
.group = 0,
|
|
|
|
.qthreshold = 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* log handler for internal netfilter logging api */
|
2008-11-04 13:21:08 +00:00
|
|
|
void
|
2008-10-08 09:35:00 +00:00
|
|
|
nfulnl_log_packet(u_int8_t pf,
|
2005-08-10 02:58:39 +00:00
|
|
|
unsigned int hooknum,
|
|
|
|
const struct sk_buff *skb,
|
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
|
|
|
const struct nf_loginfo *li_user,
|
|
|
|
const char *prefix)
|
|
|
|
{
|
|
|
|
unsigned int size, data_len;
|
|
|
|
struct nfulnl_instance *inst;
|
|
|
|
const struct nf_loginfo *li;
|
|
|
|
unsigned int qthreshold;
|
2006-11-29 01:35:34 +00:00
|
|
|
unsigned int plen;
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-02-12 19:15:49 +00:00
|
|
|
if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
|
2005-08-10 02:58:39 +00:00
|
|
|
li = li_user;
|
|
|
|
else
|
|
|
|
li = &default_loginfo;
|
|
|
|
|
|
|
|
inst = instance_lookup_get(li->u.ulog.group);
|
|
|
|
if (!inst)
|
|
|
|
return;
|
|
|
|
|
2006-11-29 01:35:34 +00:00
|
|
|
plen = 0;
|
|
|
|
if (prefix)
|
2007-03-07 04:24:35 +00:00
|
|
|
plen = strlen(prefix) + 1;
|
2006-11-29 01:35:34 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
/* FIXME: do we want to make the size calculation conditional based on
|
|
|
|
* what is actually present? way more branches and checks, but more
|
|
|
|
* memory efficient... */
|
2008-03-10 23:42:04 +00:00
|
|
|
size = NLMSG_SPACE(sizeof(struct nfgenmsg))
|
2007-09-28 21:37:03 +00:00
|
|
|
+ nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
2005-08-10 03:22:10 +00:00
|
|
|
#ifdef CONFIG_BRIDGE_NETFILTER
|
2007-09-28 21:37:03 +00:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
2005-08-10 03:22:10 +00:00
|
|
|
#endif
|
2007-09-28 21:37:03 +00:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* mark */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* uid */
|
2007-12-18 06:41:52 +00:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* gid */
|
2007-09-28 21:37:03 +00:00
|
|
|
+ nla_total_size(plen) /* prefix */
|
|
|
|
+ nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
|
|
|
|
+ nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp));
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2009-05-27 13:49:11 +00:00
|
|
|
if (in && skb_mac_header_was_set(skb)) {
|
|
|
|
size += nla_total_size(skb->dev->hard_header_len)
|
|
|
|
+ nla_total_size(sizeof(u_int16_t)) /* hwtype */
|
|
|
|
+ nla_total_size(sizeof(u_int16_t)); /* hwlen */
|
|
|
|
}
|
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
spin_lock_bh(&inst->lock);
|
|
|
|
|
2006-03-21 01:15:11 +00:00
|
|
|
if (inst->flags & NFULNL_CFG_F_SEQ)
|
2007-09-28 21:37:03 +00:00
|
|
|
size += nla_total_size(sizeof(u_int32_t));
|
2006-03-21 01:15:11 +00:00
|
|
|
if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
|
2007-09-28 21:37:03 +00:00
|
|
|
size += nla_total_size(sizeof(u_int32_t));
|
2006-03-21 01:15:11 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
qthreshold = inst->qthreshold;
|
|
|
|
/* per-rule qthreshold overrides per-instance */
|
2009-02-18 14:29:23 +00:00
|
|
|
if (li->u.ulog.qthreshold)
|
|
|
|
if (qthreshold > li->u.ulog.qthreshold)
|
|
|
|
qthreshold = li->u.ulog.qthreshold;
|
|
|
|
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
switch (inst->copy_mode) {
|
|
|
|
case NFULNL_COPY_META:
|
|
|
|
case NFULNL_COPY_NONE:
|
|
|
|
data_len = 0;
|
|
|
|
break;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
case NFULNL_COPY_PACKET:
|
2007-02-12 19:15:49 +00:00
|
|
|
if (inst->copy_range == 0
|
2005-08-10 02:58:39 +00:00
|
|
|
|| inst->copy_range > skb->len)
|
|
|
|
data_len = skb->len;
|
|
|
|
else
|
|
|
|
data_len = inst->copy_range;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
size += nla_total_size(data_len);
|
2005-08-10 02:58:39 +00:00
|
|
|
break;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2010-06-14 14:15:23 +00:00
|
|
|
case NFULNL_COPY_DISABLED:
|
2005-08-10 02:58:39 +00:00
|
|
|
default:
|
2007-03-23 18:11:05 +00:00
|
|
|
goto unlock_and_release;
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 21:44:44 +00:00
|
|
|
if (inst->skb &&
|
|
|
|
size > skb_tailroom(inst->skb) - sizeof(struct nfgenmsg)) {
|
2005-08-10 02:58:39 +00:00
|
|
|
/* either the queue len is too high or we don't have
|
|
|
|
* enough room in the skb left. flush to userspace. */
|
2007-09-28 21:44:21 +00:00
|
|
|
__nfulnl_flush(inst);
|
2007-03-23 18:11:05 +00:00
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-03-23 18:11:05 +00:00
|
|
|
if (!inst->skb) {
|
|
|
|
inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
|
|
|
|
if (!inst->skb)
|
2005-08-10 02:58:39 +00:00
|
|
|
goto alloc_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
inst->qlen++;
|
|
|
|
|
|
|
|
__build_packet_message(inst, skb, data_len, pf,
|
2011-02-15 20:59:37 +00:00
|
|
|
hooknum, in, out, prefix, plen);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-09-28 21:44:44 +00:00
|
|
|
if (inst->qlen >= qthreshold)
|
|
|
|
__nfulnl_flush(inst);
|
2005-08-10 02:58:39 +00:00
|
|
|
/* timer_pending always called within inst->lock, so there
|
|
|
|
* is no chance of a race here */
|
2007-09-28 21:44:44 +00:00
|
|
|
else if (!timer_pending(&inst->timer)) {
|
2005-08-10 02:58:39 +00:00
|
|
|
instance_get(inst);
|
|
|
|
inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
|
|
|
|
add_timer(&inst->timer);
|
|
|
|
}
|
|
|
|
|
2007-03-04 23:58:15 +00:00
|
|
|
unlock_and_release:
|
|
|
|
spin_unlock_bh(&inst->lock);
|
|
|
|
instance_put(inst);
|
2005-08-10 02:58:39 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
alloc_failure:
|
|
|
|
/* FIXME: statistics */
|
2007-03-04 23:58:15 +00:00
|
|
|
goto unlock_and_release;
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
2008-11-04 13:21:08 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nfulnl_log_packet);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_rcv_nl_event(struct notifier_block *this,
|
|
|
|
unsigned long event, void *ptr)
|
|
|
|
{
|
|
|
|
struct netlink_notify *n = ptr;
|
|
|
|
|
2009-11-06 16:04:00 +00:00
|
|
|
if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
|
2005-08-10 02:58:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* destroy all instances for this pid */
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_lock_bh(&instances_lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++) {
|
|
|
|
struct hlist_node *tmp, *t2;
|
|
|
|
struct nfulnl_instance *inst;
|
|
|
|
struct hlist_head *head = &instance_table[i];
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
|
2009-11-25 23:14:13 +00:00
|
|
|
if ((net_eq(n->net, &init_net)) &&
|
2007-09-12 11:05:38 +00:00
|
|
|
(n->pid == inst->peer_pid))
|
2005-08-10 02:58:39 +00:00
|
|
|
__instance_destroy(inst);
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 16:14:58 +00:00
|
|
|
spin_unlock_bh(&instances_lock);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block nfulnl_rtnl_notifier = {
|
|
|
|
.notifier_call = nfulnl_rcv_nl_event,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
|
2009-08-25 14:07:58 +00:00
|
|
|
const struct nlmsghdr *nlh,
|
|
|
|
const struct nlattr * const nfqa[])
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2009-03-16 13:54:21 +00:00
|
|
|
static struct nf_logger nfulnl_logger __read_mostly = {
|
2005-08-10 02:58:39 +00:00
|
|
|
.name = "nfnetlink_log",
|
|
|
|
.logfn = &nfulnl_log_packet,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2007-09-28 21:39:09 +00:00
|
|
|
static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
|
|
|
|
[NFULA_CFG_CMD] = { .len = sizeof(struct nfulnl_msg_config_cmd) },
|
|
|
|
[NFULA_CFG_MODE] = { .len = sizeof(struct nfulnl_msg_config_mode) },
|
|
|
|
[NFULA_CFG_TIMEOUT] = { .type = NLA_U32 },
|
|
|
|
[NFULA_CFG_QTHRESH] = { .type = NLA_U32 },
|
|
|
|
[NFULA_CFG_NLBUFSIZ] = { .type = NLA_U32 },
|
|
|
|
[NFULA_CFG_FLAGS] = { .type = NLA_U16 },
|
2005-08-10 02:58:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
|
2009-08-25 14:07:58 +00:00
|
|
|
const struct nlmsghdr *nlh,
|
|
|
|
const struct nlattr * const nfula[])
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
2012-06-27 04:34:03 +00:00
|
|
|
struct nfgenmsg *nfmsg = nlmsg_data(nlh);
|
2005-08-10 02:58:39 +00:00
|
|
|
u_int16_t group_num = ntohs(nfmsg->res_id);
|
|
|
|
struct nfulnl_instance *inst;
|
2008-03-10 23:44:13 +00:00
|
|
|
struct nfulnl_msg_config_cmd *cmd = NULL;
|
2005-08-10 02:58:39 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2008-03-10 23:44:13 +00:00
|
|
|
if (nfula[NFULA_CFG_CMD]) {
|
|
|
|
u_int8_t pf = nfmsg->nfgen_family;
|
|
|
|
cmd = nla_data(nfula[NFULA_CFG_CMD]);
|
|
|
|
|
|
|
|
/* Commands without queue context */
|
|
|
|
switch (cmd->command) {
|
|
|
|
case NFULNL_CFG_CMD_PF_BIND:
|
2009-03-16 13:54:21 +00:00
|
|
|
return nf_log_bind_pf(pf, &nfulnl_logger);
|
2008-03-10 23:44:13 +00:00
|
|
|
case NFULNL_CFG_CMD_PF_UNBIND:
|
2009-03-16 13:54:21 +00:00
|
|
|
nf_log_unbind_pf(pf);
|
2008-03-10 23:44:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
inst = instance_lookup_get(group_num);
|
2007-12-18 06:39:55 +00:00
|
|
|
if (inst && inst->peer_pid != NETLINK_CB(skb).pid) {
|
|
|
|
ret = -EPERM;
|
|
|
|
goto out_put;
|
|
|
|
}
|
|
|
|
|
2008-03-10 23:44:13 +00:00
|
|
|
if (cmd != NULL) {
|
2005-08-10 02:58:39 +00:00
|
|
|
switch (cmd->command) {
|
|
|
|
case NFULNL_CFG_CMD_BIND:
|
|
|
|
if (inst) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out_put;
|
|
|
|
}
|
|
|
|
|
|
|
|
inst = instance_create(group_num,
|
|
|
|
NETLINK_CB(skb).pid);
|
2007-12-18 06:41:21 +00:00
|
|
|
if (IS_ERR(inst)) {
|
|
|
|
ret = PTR_ERR(inst);
|
2007-03-23 18:11:31 +00:00
|
|
|
goto out;
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NFULNL_CFG_CMD_UNBIND:
|
|
|
|
if (!inst) {
|
|
|
|
ret = -ENODEV;
|
2007-03-23 18:11:31 +00:00
|
|
|
goto out;
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instance_destroy(inst);
|
2010-02-26 16:48:40 +00:00
|
|
|
goto out_put;
|
2005-08-10 02:58:39 +00:00
|
|
|
default:
|
2007-12-18 06:40:19 +00:00
|
|
|
ret = -ENOTSUPP;
|
2005-08-10 02:58:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (nfula[NFULA_CFG_MODE]) {
|
2005-08-10 02:58:39 +00:00
|
|
|
struct nfulnl_msg_config_mode *params;
|
2007-09-28 21:37:03 +00:00
|
|
|
params = nla_data(nfula[NFULA_CFG_MODE]);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-12-18 06:39:55 +00:00
|
|
|
if (!inst) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
nfulnl_set_mode(inst, params->copy_mode,
|
2006-11-03 08:58:41 +00:00
|
|
|
ntohl(params->copy_range));
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (nfula[NFULA_CFG_TIMEOUT]) {
|
2007-12-18 06:41:35 +00:00
|
|
|
__be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-12-18 06:39:55 +00:00
|
|
|
if (!inst) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
nfulnl_set_timeout(inst, ntohl(timeout));
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (nfula[NFULA_CFG_NLBUFSIZ]) {
|
2007-12-18 06:41:35 +00:00
|
|
|
__be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-12-18 06:39:55 +00:00
|
|
|
if (!inst) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (nfula[NFULA_CFG_QTHRESH]) {
|
2007-12-18 06:41:35 +00:00
|
|
|
__be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2007-12-18 06:39:55 +00:00
|
|
|
if (!inst) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-08-10 02:58:39 +00:00
|
|
|
nfulnl_set_qthresh(inst, ntohl(qthresh));
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:37:03 +00:00
|
|
|
if (nfula[NFULA_CFG_FLAGS]) {
|
2007-12-18 06:41:35 +00:00
|
|
|
__be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
|
2007-12-18 06:39:55 +00:00
|
|
|
|
|
|
|
if (!inst) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-05-19 09:17:18 +00:00
|
|
|
nfulnl_set_flags(inst, ntohs(flags));
|
2006-03-21 01:15:11 +00:00
|
|
|
}
|
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
out_put:
|
|
|
|
instance_put(inst);
|
2007-03-04 23:59:20 +00:00
|
|
|
out:
|
2005-08-10 02:58:39 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-28 21:15:45 +00:00
|
|
|
static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
|
2005-08-10 02:58:39 +00:00
|
|
|
[NFULNL_MSG_PACKET] = { .call = nfulnl_recv_unsupp,
|
2005-11-14 23:24:59 +00:00
|
|
|
.attr_count = NFULA_MAX, },
|
2005-08-10 02:58:39 +00:00
|
|
|
[NFULNL_MSG_CONFIG] = { .call = nfulnl_recv_config,
|
2007-09-28 21:39:09 +00:00
|
|
|
.attr_count = NFULA_CFG_MAX,
|
|
|
|
.policy = nfula_cfg_policy },
|
2005-08-10 02:58:39 +00:00
|
|
|
};
|
|
|
|
|
2007-09-28 21:15:45 +00:00
|
|
|
static const struct nfnetlink_subsystem nfulnl_subsys = {
|
2005-08-10 02:58:39 +00:00
|
|
|
.name = "log",
|
|
|
|
.subsys_id = NFNL_SUBSYS_ULOG,
|
|
|
|
.cb_count = NFULNL_MSG_MAX,
|
|
|
|
.cb = nfulnl_cb,
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct iter_state {
|
|
|
|
unsigned int bucket;
|
|
|
|
};
|
|
|
|
|
2007-03-23 18:12:03 +00:00
|
|
|
static struct hlist_node *get_first(struct iter_state *st)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
if (!st)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
|
|
|
|
if (!hlist_empty(&instance_table[st->bucket]))
|
2010-11-15 17:17:21 +00:00
|
|
|
return rcu_dereference_bh(hlist_first_rcu(&instance_table[st->bucket]));
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-23 18:12:03 +00:00
|
|
|
static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
2010-11-15 17:17:21 +00:00
|
|
|
h = rcu_dereference_bh(hlist_next_rcu(h));
|
2005-08-10 02:58:39 +00:00
|
|
|
while (!h) {
|
|
|
|
if (++st->bucket >= INSTANCE_BUCKETS)
|
|
|
|
return NULL;
|
|
|
|
|
2010-11-15 17:17:21 +00:00
|
|
|
h = rcu_dereference_bh(hlist_first_rcu(&instance_table[st->bucket]));
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2007-03-23 18:12:03 +00:00
|
|
|
static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
struct hlist_node *head;
|
2007-03-23 18:12:03 +00:00
|
|
|
head = get_first(st);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
|
|
|
if (head)
|
2007-03-23 18:12:03 +00:00
|
|
|
while (pos && (head = get_next(st, head)))
|
2005-08-10 02:58:39 +00:00
|
|
|
pos--;
|
|
|
|
return pos ? NULL : head;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *seq_start(struct seq_file *seq, loff_t *pos)
|
2010-06-09 16:14:58 +00:00
|
|
|
__acquires(rcu_bh)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
2010-06-09 16:14:58 +00:00
|
|
|
rcu_read_lock_bh();
|
2007-03-23 18:12:03 +00:00
|
|
|
return get_idx(seq->private, *pos);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
(*pos)++;
|
2007-03-23 18:12:03 +00:00
|
|
|
return get_next(s->private, v);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void seq_stop(struct seq_file *s, void *v)
|
2010-06-09 16:14:58 +00:00
|
|
|
__releases(rcu_bh)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
2010-06-09 16:14:58 +00:00
|
|
|
rcu_read_unlock_bh();
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int seq_show(struct seq_file *s, void *v)
|
|
|
|
{
|
|
|
|
const struct nfulnl_instance *inst = v;
|
|
|
|
|
2007-02-12 19:15:49 +00:00
|
|
|
return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
|
2005-08-10 02:58:39 +00:00
|
|
|
inst->group_num,
|
2007-02-12 19:15:49 +00:00
|
|
|
inst->peer_pid, inst->qlen,
|
2005-08-10 02:58:39 +00:00
|
|
|
inst->copy_mode, inst->copy_range,
|
|
|
|
inst->flushtimeout, atomic_read(&inst->use));
|
|
|
|
}
|
|
|
|
|
2007-07-11 06:07:31 +00:00
|
|
|
static const struct seq_operations nful_seq_ops = {
|
2005-08-10 02:58:39 +00:00
|
|
|
.start = seq_start,
|
|
|
|
.next = seq_next,
|
|
|
|
.stop = seq_stop,
|
|
|
|
.show = seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int nful_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-10-10 09:29:58 +00:00
|
|
|
return seq_open_private(file, &nful_seq_ops,
|
|
|
|
sizeof(struct iter_state));
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 08:55:36 +00:00
|
|
|
static const struct file_operations nful_file_ops = {
|
2005-08-10 02:58:39 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = nful_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release_private,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* PROC_FS */
|
|
|
|
|
2006-04-06 21:11:30 +00:00
|
|
|
static int __init nfnetlink_log_init(void)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
|
|
|
int i, status = -ENOMEM;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++)
|
|
|
|
INIT_HLIST_HEAD(&instance_table[i]);
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
/* it's not really all that important to have a random value, so
|
|
|
|
* we can do this from the init function, even if there hasn't
|
|
|
|
* been that much entropy yet */
|
|
|
|
get_random_bytes(&hash_init, sizeof(hash_init));
|
|
|
|
|
|
|
|
netlink_register_notifier(&nfulnl_rtnl_notifier);
|
|
|
|
status = nfnetlink_subsys_register(&nfulnl_subsys);
|
|
|
|
if (status < 0) {
|
|
|
|
printk(KERN_ERR "log: failed to create netlink socket\n");
|
|
|
|
goto cleanup_netlink_notifier;
|
|
|
|
}
|
|
|
|
|
2009-03-16 13:54:21 +00:00
|
|
|
status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
|
|
|
|
if (status < 0) {
|
|
|
|
printk(KERN_ERR "log: failed to register logger\n");
|
|
|
|
goto cleanup_subsys;
|
|
|
|
}
|
|
|
|
|
2005-08-10 02:58:39 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-03-27 23:55:53 +00:00
|
|
|
if (!proc_create("nfnetlink_log", 0440,
|
|
|
|
proc_net_netfilter, &nful_file_ops))
|
2009-03-16 13:54:21 +00:00
|
|
|
goto cleanup_logger;
|
2005-08-10 02:58:39 +00:00
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2009-03-16 13:54:21 +00:00
|
|
|
cleanup_logger:
|
|
|
|
nf_log_unregister(&nfulnl_logger);
|
|
|
|
#endif
|
2005-08-10 02:58:39 +00:00
|
|
|
cleanup_subsys:
|
|
|
|
nfnetlink_subsys_unregister(&nfulnl_subsys);
|
|
|
|
cleanup_netlink_notifier:
|
|
|
|
netlink_unregister_notifier(&nfulnl_rtnl_notifier);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static void __exit nfnetlink_log_fini(void)
|
2005-08-10 02:58:39 +00:00
|
|
|
{
|
2007-02-12 19:11:55 +00:00
|
|
|
nf_log_unregister(&nfulnl_logger);
|
2006-04-06 21:11:30 +00:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
remove_proc_entry("nfnetlink_log", proc_net_netfilter);
|
|
|
|
#endif
|
|
|
|
nfnetlink_subsys_unregister(&nfulnl_subsys);
|
|
|
|
netlink_unregister_notifier(&nfulnl_rtnl_notifier);
|
2005-08-10 02:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("netfilter userspace logging");
|
|
|
|
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
|
|
|
|
MODULE_LICENSE("GPL");
|
2005-08-10 03:20:34 +00:00
|
|
|
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
|
2005-08-10 02:58:39 +00:00
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
module_init(nfnetlink_log_init);
|
|
|
|
module_exit(nfnetlink_log_fini);
|