2019-06-04 10:11:33 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-08-10 04:44:15 +02:00
|
|
|
/*
|
|
|
|
* This is a module which is used for queueing packets and communicating with
|
2009-06-08 05:11:33 +02:00
|
|
|
* userspace via nfnetlink.
|
2005-08-10 04:44:15 +02:00
|
|
|
*
|
|
|
|
* (C) 2005 by Harald Welte <laforge@netfilter.org>
|
2007-12-05 10:31:17 +01:00
|
|
|
* (C) 2007 by Patrick McHardy <kaber@trash.net>
|
2005-08-10 04:44:15 +02:00
|
|
|
*
|
|
|
|
* Based on the old ipv4-only ip_queue.c:
|
|
|
|
* (C) 2000-2002 James Morris <jmorris@intercode.com.au>
|
|
|
|
* (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
*/
|
2018-03-12 14:06:29 +01:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/spinlock.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 09:04:11 +01:00
|
|
|
#include <linux/slab.h>
|
2005-08-10 04:44:15 +02:00
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/netfilter.h>
|
2005-08-10 04:50:45 +02:00
|
|
|
#include <linux/proc_fs.h>
|
2005-08-10 04:44:15 +02:00
|
|
|
#include <linux/netfilter_ipv4.h>
|
|
|
|
#include <linux/netfilter_ipv6.h>
|
2015-04-02 14:31:41 +02:00
|
|
|
#include <linux/netfilter_bridge.h>
|
2005-08-10 04:44:15 +02:00
|
|
|
#include <linux/netfilter/nfnetlink.h>
|
|
|
|
#include <linux/netfilter/nfnetlink_queue.h>
|
2015-09-30 23:53:44 +02:00
|
|
|
#include <linux/netfilter/nf_conntrack_common.h>
|
2005-08-10 04:44:15 +02:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <net/sock.h>
|
2014-01-06 19:36:06 +01:00
|
|
|
#include <net/tcp_states.h>
|
2007-12-05 10:24:48 +01:00
|
|
|
#include <net/netfilter/nf_queue.h>
|
2013-03-25 00:50:47 +01:00
|
|
|
#include <net/netns/generic.h>
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2011-07-27 01:09:06 +02:00
|
|
|
#include <linux/atomic.h>
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2014-10-01 11:19:17 +02:00
|
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
2005-08-10 05:22:10 +02:00
|
|
|
#include "../bridge/br_private.h"
|
|
|
|
#endif
|
|
|
|
|
2017-07-26 00:02:34 +02:00
|
|
|
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
|
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
|
|
|
#endif
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
#define NFQNL_QMAX_DEFAULT 1024
|
|
|
|
|
2013-06-05 00:22:15 +02:00
|
|
|
/* We're using struct nlattr which has 16bit nla_len. Note that nla_len
|
|
|
|
* includes the header length. Thus, the maximum packet length that we
|
|
|
|
* support is 65531 bytes. We send truncated packets if the specified length
|
|
|
|
* is larger than that. Userspace can check for presence of NFQA_CAP_LEN
|
|
|
|
* attribute to detect truncation.
|
|
|
|
*/
|
|
|
|
#define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
struct nfqnl_instance {
|
|
|
|
struct hlist_node hlist; /* global list of queues */
|
2007-12-05 10:28:50 +01:00
|
|
|
struct rcu_head rcu;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2015-04-13 00:52:37 +02:00
|
|
|
u32 peer_portid;
|
2005-08-10 04:44:15 +02:00
|
|
|
unsigned int queue_maxlen;
|
|
|
|
unsigned int copy_range;
|
|
|
|
unsigned int queue_dropped;
|
|
|
|
unsigned int queue_user_dropped;
|
|
|
|
|
|
|
|
|
|
|
|
u_int16_t queue_num; /* number of this queue */
|
|
|
|
u_int8_t copy_mode;
|
2012-05-24 05:56:44 +02:00
|
|
|
u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
|
2010-06-09 18:07:06 +02:00
|
|
|
/*
|
|
|
|
* Following fields are dirtied for each queued packet,
|
|
|
|
* keep them in same cache line if possible.
|
|
|
|
*/
|
2016-10-31 00:35:07 +01:00
|
|
|
spinlock_t lock ____cacheline_aligned_in_smp;
|
2010-06-09 18:07:06 +02:00
|
|
|
unsigned int queue_total;
|
2011-07-19 11:44:17 +02:00
|
|
|
unsigned int id_sequence; /* 'sequence' of pkt ids */
|
2005-08-10 04:44:15 +02:00
|
|
|
struct list_head queue_list; /* packets in queue */
|
|
|
|
};
|
|
|
|
|
2007-12-05 10:26:33 +01:00
|
|
|
typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
netns: make struct pernet_operations::id unsigned int
Make struct pernet_operations::id unsigned.
There are 2 reasons to do so:
1)
This field is really an index into an zero based array and
thus is unsigned entity. Using negative value is out-of-bound
access by definition.
2)
On x86_64 unsigned 32-bit data which are mixed with pointers
via array indexing or offsets added or subtracted to pointers
are preffered to signed 32-bit data.
"int" being used as an array index needs to be sign-extended
to 64-bit before being used.
void f(long *p, int i)
{
g(p[i]);
}
roughly translates to
movsx rsi, esi
mov rdi, [rsi+...]
call g
MOVSX is 3 byte instruction which isn't necessary if the variable is
unsigned because x86_64 is zero extending by default.
Now, there is net_generic() function which, you guessed it right, uses
"int" as an array index:
static inline void *net_generic(const struct net *net, int id)
{
...
ptr = ng->ptr[id - 1];
...
}
And this function is used a lot, so those sign extensions add up.
Patch snipes ~1730 bytes on allyesconfig kernel (without all junk
messing with code generation):
add/remove: 0/0 grow/shrink: 70/598 up/down: 396/-2126 (-1730)
Unfortunately some functions actually grow bigger.
This is a semmingly random artefact of code generation with register
allocator being used differently. gcc decides that some variable
needs to live in new r8+ registers and every access now requires REX
prefix. Or it is shifted into r12, so [r12+0] addressing mode has to be
used which is longer than [r8]
However, overall balance is in negative direction:
add/remove: 0/0 grow/shrink: 70/598 up/down: 396/-2126 (-1730)
function old new delta
nfsd4_lock 3886 3959 +73
tipc_link_build_proto_msg 1096 1140 +44
mac80211_hwsim_new_radio 2776 2808 +32
tipc_mon_rcv 1032 1058 +26
svcauth_gss_legacy_init 1413 1429 +16
tipc_bcbase_select_primary 379 392 +13
nfsd4_exchange_id 1247 1260 +13
nfsd4_setclientid_confirm 782 793 +11
...
put_client_renew_locked 494 480 -14
ip_set_sockfn_get 730 716 -14
geneve_sock_add 829 813 -16
nfsd4_sequence_done 721 703 -18
nlmclnt_lookup_host 708 686 -22
nfsd4_lockt 1085 1063 -22
nfs_get_client 1077 1050 -27
tcf_bpf_init 1106 1076 -30
nfsd4_encode_fattr 5997 5930 -67
Total: Before=154856051, After=154854321, chg -0.00%
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-11-17 02:58:21 +01:00
|
|
|
static unsigned int nfnl_queue_net_id __read_mostly;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
#define INSTANCE_BUCKETS 16
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net {
|
|
|
|
spinlock_t instances_lock;
|
|
|
|
struct hlist_head instance_table[INSTANCE_BUCKETS];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
|
|
|
|
{
|
|
|
|
return net_generic(net, nfnl_queue_net_id);
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
static inline u_int8_t instance_hashfn(u_int16_t queue_num)
|
|
|
|
{
|
2013-03-14 07:03:17 +01:00
|
|
|
return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfqnl_instance *
|
2013-03-25 00:50:47 +01:00
|
|
|
instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
|
|
|
struct hlist_head *head;
|
|
|
|
struct nfqnl_instance *inst;
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
head = &q->instance_table[instance_hashfn(queue_num)];
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 02:06:00 +01:00
|
|
|
hlist_for_each_entry_rcu(inst, head, hlist) {
|
2005-08-10 04:44:15 +02:00
|
|
|
if (inst->queue_num == queue_num)
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfqnl_instance *
|
2015-04-13 00:52:37 +02:00
|
|
|
instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2007-12-18 07:41:21 +01:00
|
|
|
struct nfqnl_instance *inst;
|
2007-12-05 10:28:50 +01:00
|
|
|
unsigned int h;
|
2007-12-18 07:41:21 +01:00
|
|
|
int err;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_lock(&q->instances_lock);
|
|
|
|
if (instance_lookup(q, queue_num)) {
|
2007-12-18 07:41:21 +01:00
|
|
|
err = -EEXIST;
|
2005-08-10 04:44:15 +02:00
|
|
|
goto out_unlock;
|
2007-12-18 07:41:21 +01:00
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2005-11-03 19:20:07 +01:00
|
|
|
inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
|
2007-12-18 07:41:21 +01:00
|
|
|
if (!inst) {
|
|
|
|
err = -ENOMEM;
|
2005-08-10 04:44:15 +02:00
|
|
|
goto out_unlock;
|
2007-12-18 07:41:21 +01:00
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
inst->queue_num = queue_num;
|
2012-09-07 22:12:54 +02:00
|
|
|
inst->peer_portid = portid;
|
2005-08-10 04:44:15 +02:00
|
|
|
inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
|
2013-06-05 00:22:15 +02:00
|
|
|
inst->copy_range = NFQNL_MAX_COPY_RANGE;
|
2005-08-10 04:44:15 +02:00
|
|
|
inst->copy_mode = NFQNL_COPY_NONE;
|
2006-01-04 22:56:54 +01:00
|
|
|
spin_lock_init(&inst->lock);
|
2005-08-10 04:44:15 +02:00
|
|
|
INIT_LIST_HEAD(&inst->queue_list);
|
|
|
|
|
2007-12-18 07:41:21 +01:00
|
|
|
if (!try_module_get(THIS_MODULE)) {
|
|
|
|
err = -EAGAIN;
|
2005-08-10 04:44:15 +02:00
|
|
|
goto out_free;
|
2007-12-18 07:41:21 +01:00
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
h = instance_hashfn(queue_num);
|
2013-03-25 00:50:47 +01:00
|
|
|
hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_unlock(&q->instances_lock);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
return inst;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(inst);
|
|
|
|
out_unlock:
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_unlock(&q->instances_lock);
|
2007-12-18 07:41:21 +01:00
|
|
|
return ERR_PTR(err);
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 10:25:30 +01:00
|
|
|
static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
|
|
|
|
unsigned long data);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
static void
|
2007-12-05 10:28:50 +01:00
|
|
|
instance_destroy_rcu(struct rcu_head *head)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2007-12-05 10:28:50 +01:00
|
|
|
struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
|
|
|
|
rcu);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2007-12-05 10:25:30 +01:00
|
|
|
nfqnl_flush(inst, NULL, 0);
|
2007-12-05 10:28:50 +01:00
|
|
|
kfree(inst);
|
2005-08-10 04:44:15 +02:00
|
|
|
module_put(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
static void
|
2005-08-10 04:44:15 +02:00
|
|
|
__instance_destroy(struct nfqnl_instance *inst)
|
|
|
|
{
|
2007-12-05 10:28:50 +01:00
|
|
|
hlist_del_rcu(&inst->hlist);
|
|
|
|
call_rcu(&inst->rcu, instance_destroy_rcu);
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
static void
|
2013-03-25 00:50:47 +01:00
|
|
|
instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_lock(&q->instances_lock);
|
2007-12-05 10:28:50 +01:00
|
|
|
__instance_destroy(inst);
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_unlock(&q->instances_lock);
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2007-12-05 10:26:33 +01:00
|
|
|
__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2007-12-05 10:25:03 +01:00
|
|
|
list_add_tail(&entry->list, &queue->queue_list);
|
2005-08-10 04:44:15 +02:00
|
|
|
queue->queue_total++;
|
|
|
|
}
|
|
|
|
|
2011-07-19 11:46:33 +02:00
|
|
|
static void
|
|
|
|
__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
|
|
|
|
{
|
|
|
|
list_del(&entry->list);
|
|
|
|
queue->queue_total--;
|
|
|
|
}
|
|
|
|
|
2007-12-05 10:26:33 +01:00
|
|
|
static struct nf_queue_entry *
|
2007-12-05 10:25:30 +01:00
|
|
|
find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2007-12-05 10:26:33 +01:00
|
|
|
struct nf_queue_entry *entry = NULL, *i;
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-12-05 10:25:30 +01:00
|
|
|
|
|
|
|
list_for_each_entry(i, &queue->queue_list, list) {
|
|
|
|
if (i->id == id) {
|
|
|
|
entry = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-19 11:46:33 +02:00
|
|
|
if (entry)
|
|
|
|
__dequeue_entry(queue, entry);
|
2007-12-05 10:25:30 +01:00
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2018-05-23 09:17:24 +02:00
|
|
|
static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
|
|
|
|
{
|
|
|
|
struct nf_ct_hook *ct_hook;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (verdict == NF_ACCEPT ||
|
2018-09-04 13:25:44 +02:00
|
|
|
verdict == NF_REPEAT ||
|
2018-05-23 09:17:24 +02:00
|
|
|
verdict == NF_STOP) {
|
|
|
|
rcu_read_lock();
|
|
|
|
ct_hook = rcu_dereference(nf_ct_hook);
|
|
|
|
if (ct_hook) {
|
|
|
|
err = ct_hook->update(entry->state.net, entry->skb);
|
|
|
|
if (err < 0)
|
|
|
|
verdict = NF_DROP;
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
nf_reinject(entry, verdict);
|
|
|
|
}
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
static void
|
2007-12-05 10:25:30 +01:00
|
|
|
nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2007-12-05 10:26:33 +01:00
|
|
|
struct nf_queue_entry *entry, *next;
|
2007-12-05 10:25:30 +01:00
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-12-05 10:25:30 +01:00
|
|
|
list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
|
|
|
|
if (!cmpfn || cmpfn(entry, data)) {
|
|
|
|
list_del(&entry->list);
|
|
|
|
queue->queue_total--;
|
2018-05-23 09:17:24 +02:00
|
|
|
nfqnl_reinject(entry, NF_DROP);
|
2007-12-05 10:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
}
|
|
|
|
|
2013-06-29 14:15:47 +02:00
|
|
|
static int
|
|
|
|
nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
|
|
|
|
bool csum_verify)
|
2013-04-19 06:58:26 +02:00
|
|
|
{
|
|
|
|
__u32 flags = 0;
|
|
|
|
|
|
|
|
if (packet->ip_summed == CHECKSUM_PARTIAL)
|
|
|
|
flags = NFQA_SKB_CSUMNOTREADY;
|
2013-06-29 14:15:47 +02:00
|
|
|
else if (csum_verify)
|
|
|
|
flags = NFQA_SKB_CSUM_NOTVERIFIED;
|
|
|
|
|
2013-04-19 06:58:26 +02:00
|
|
|
if (skb_is_gso(packet))
|
|
|
|
flags |= NFQA_SKB_GSO;
|
|
|
|
|
|
|
|
return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
|
|
|
|
}
|
|
|
|
|
2013-12-20 17:28:53 +01:00
|
|
|
static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
|
|
|
|
{
|
|
|
|
const struct cred *cred;
|
|
|
|
|
2015-03-17 05:06:15 +01:00
|
|
|
if (!sk_fullsock(sk))
|
2013-12-20 17:28:53 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
read_lock_bh(&sk->sk_callback_lock);
|
|
|
|
if (sk->sk_socket && sk->sk_socket->file) {
|
|
|
|
cred = sk->sk_socket->file->f_cred;
|
|
|
|
if (nla_put_be32(skb, NFQA_UID,
|
|
|
|
htonl(from_kuid_munged(&init_user_ns, cred->fsuid))))
|
|
|
|
goto nla_put_failure;
|
|
|
|
if (nla_put_be32(skb, NFQA_GID,
|
|
|
|
htonl(from_kgid_munged(&init_user_ns, cred->fsgid))))
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
|
|
|
read_unlock_bh(&sk->sk_callback_lock);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
read_unlock_bh(&sk->sk_callback_lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:32:57 +02:00
|
|
|
static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata)
|
|
|
|
{
|
|
|
|
u32 seclen = 0;
|
|
|
|
#if IS_ENABLED(CONFIG_NETWORK_SECMARK)
|
|
|
|
if (!skb || !sk_fullsock(skb->sk))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
read_lock_bh(&skb->sk->sk_callback_lock);
|
|
|
|
|
|
|
|
if (skb->secmark)
|
|
|
|
security_secid_to_secctx(skb->secmark, secdata, &seclen);
|
|
|
|
|
|
|
|
read_unlock_bh(&skb->sk->sk_callback_lock);
|
|
|
|
#endif
|
|
|
|
return seclen;
|
|
|
|
}
|
|
|
|
|
2016-03-26 08:42:11 +01:00
|
|
|
static u32 nfqnl_get_bridge_size(struct nf_queue_entry *entry)
|
|
|
|
{
|
|
|
|
struct sk_buff *entskb = entry->skb;
|
|
|
|
u32 nlalen = 0;
|
|
|
|
|
|
|
|
if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (skb_vlan_tag_present(entskb))
|
|
|
|
nlalen += nla_total_size(nla_total_size(sizeof(__be16)) +
|
|
|
|
nla_total_size(sizeof(__be16)));
|
|
|
|
|
|
|
|
if (entskb->network_header > entskb->mac_header)
|
|
|
|
nlalen += nla_total_size((entskb->network_header -
|
|
|
|
entskb->mac_header));
|
|
|
|
|
|
|
|
return nlalen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nfqnl_put_bridge(struct nf_queue_entry *entry, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct sk_buff *entskb = entry->skb;
|
|
|
|
|
|
|
|
if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (skb_vlan_tag_present(entskb)) {
|
|
|
|
struct nlattr *nest;
|
|
|
|
|
2019-04-26 11:13:06 +02:00
|
|
|
nest = nla_nest_start(skb, NFQA_VLAN);
|
2016-03-26 08:42:11 +01:00
|
|
|
if (!nest)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be16(skb, NFQA_VLAN_TCI, htons(entskb->vlan_tci)) ||
|
|
|
|
nla_put_be16(skb, NFQA_VLAN_PROTO, entskb->vlan_proto))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
nla_nest_end(skb, nest);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entskb->mac_header < entskb->network_header) {
|
|
|
|
int len = (int)(entskb->network_header - entskb->mac_header);
|
|
|
|
|
|
|
|
if (nla_put(skb, NFQA_L2HDR, len, skb_mac_header(entskb)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
static struct sk_buff *
|
2013-09-23 13:20:55 +02:00
|
|
|
nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
|
2011-07-19 11:44:17 +02:00
|
|
|
struct nf_queue_entry *entry,
|
|
|
|
__be32 **packet_id_ptr)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
|
|
|
size_t size;
|
2016-02-18 15:03:28 +01:00
|
|
|
size_t data_len = 0, cap_len = 0;
|
2013-12-13 15:22:17 +01:00
|
|
|
unsigned int hlen = 0;
|
2005-08-10 04:44:15 +02:00
|
|
|
struct sk_buff *skb;
|
2011-07-19 11:44:17 +02:00
|
|
|
struct nlattr *nla;
|
|
|
|
struct nfqnl_msg_packet_hdr *pmsg;
|
2005-08-10 04:44:15 +02:00
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
struct nfgenmsg *nfmsg;
|
2006-01-05 21:15:58 +01:00
|
|
|
struct sk_buff *entskb = entry->skb;
|
|
|
|
struct net_device *indev;
|
|
|
|
struct net_device *outdev;
|
2012-06-07 12:13:39 +02:00
|
|
|
struct nf_conn *ct = NULL;
|
|
|
|
enum ip_conntrack_info uninitialized_var(ctinfo);
|
2015-10-05 04:47:13 +02:00
|
|
|
struct nfnl_ct_hook *nfnl_ct;
|
2013-06-29 14:15:47 +02:00
|
|
|
bool csum_verify;
|
2015-06-12 12:32:57 +02:00
|
|
|
char *secdata = NULL;
|
|
|
|
u32 seclen = 0;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2013-03-27 07:47:04 +01:00
|
|
|
size = nlmsg_total_size(sizeof(struct nfgenmsg))
|
2007-09-28 23:37:03 +02:00
|
|
|
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
2014-10-01 11:19:17 +02:00
|
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
2007-09-28 23:37:03 +02:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
2005-08-10 05:22:10 +02:00
|
|
|
#endif
|
2007-09-28 23:37:03 +02:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* mark */
|
|
|
|
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
|
2013-04-19 06:58:26 +02:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* skbinfo */
|
2013-03-17 18:15:55 +01:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)); /* cap_len */
|
|
|
|
|
2016-12-25 11:38:40 +01:00
|
|
|
if (entskb->tstamp)
|
2013-03-17 18:15:55 +01:00
|
|
|
size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2016-03-26 08:42:11 +01:00
|
|
|
size += nfqnl_get_bridge_size(entry);
|
|
|
|
|
2015-04-03 22:31:01 +02:00
|
|
|
if (entry->state.hook <= NF_INET_FORWARD ||
|
|
|
|
(entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
|
2013-06-29 14:15:47 +02:00
|
|
|
csum_verify = !skb_csum_unnecessary(entskb);
|
|
|
|
else
|
|
|
|
csum_verify = false;
|
|
|
|
|
2015-04-03 22:31:01 +02:00
|
|
|
outdev = entry->state.out;
|
2006-01-05 21:15:58 +01:00
|
|
|
|
locking/atomics, net/netlink/netfilter: Convert ACCESS_ONCE() to READ_ONCE()/WRITE_ONCE()
For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't currently harmful.
However, for some features it is necessary to instrument reads and
writes separately, which is not possible with ACCESS_ONCE(). This
distinction is critical to correct operation.
It's possible to transform the bulk of kernel code using the Coccinelle
script below. However, this doesn't handle comments, leaving references
to ACCESS_ONCE() instances which have been removed. As a preparatory
step, this patch converts netlink and netfilter code and comments to use
{READ,WRITE}_ONCE() consistently.
----
virtual patch
@ depends on patch @
expression E1, E2;
@@
- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)
@ depends on patch @
expression E;
@@
- ACCESS_ONCE(E)
+ READ_ONCE(E)
----
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-7-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-10-23 23:07:17 +02:00
|
|
|
switch ((enum nfqnl_config_mode)READ_ONCE(queue->copy_mode)) {
|
2005-08-10 04:44:15 +02:00
|
|
|
case NFQNL_COPY_META:
|
|
|
|
case NFQNL_COPY_NONE:
|
|
|
|
break;
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
case NFQNL_COPY_PACKET:
|
2013-04-19 06:58:27 +02:00
|
|
|
if (!(queue->flags & NFQA_CFG_F_GSO) &&
|
|
|
|
entskb->ip_summed == CHECKSUM_PARTIAL &&
|
2010-06-09 18:07:06 +02:00
|
|
|
skb_checksum_help(entskb))
|
2005-09-07 00:10:00 +02:00
|
|
|
return NULL;
|
2010-06-09 18:07:06 +02:00
|
|
|
|
locking/atomics, net/netlink/netfilter: Convert ACCESS_ONCE() to READ_ONCE()/WRITE_ONCE()
For several reasons, it is desirable to use {READ,WRITE}_ONCE() in
preference to ACCESS_ONCE(), and new code is expected to use one of the
former. So far, there's been no reason to change most existing uses of
ACCESS_ONCE(), as these aren't currently harmful.
However, for some features it is necessary to instrument reads and
writes separately, which is not possible with ACCESS_ONCE(). This
distinction is critical to correct operation.
It's possible to transform the bulk of kernel code using the Coccinelle
script below. However, this doesn't handle comments, leaving references
to ACCESS_ONCE() instances which have been removed. As a preparatory
step, this patch converts netlink and netfilter code and comments to use
{READ,WRITE}_ONCE() consistently.
----
virtual patch
@ depends on patch @
expression E1, E2;
@@
- ACCESS_ONCE(E1) = E2
+ WRITE_ONCE(E1, E2)
@ depends on patch @
expression E;
@@
- ACCESS_ONCE(E)
+ READ_ONCE(E)
----
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-arch@vger.kernel.org
Cc: mpe@ellerman.id.au
Cc: shuah@kernel.org
Cc: snitzer@redhat.com
Cc: thor.thayer@linux.intel.com
Cc: tj@kernel.org
Cc: viro@zeniv.linux.org.uk
Cc: will.deacon@arm.com
Link: http://lkml.kernel.org/r/1508792849-3115-7-git-send-email-paulmck@linux.vnet.ibm.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2017-10-23 23:07:17 +02:00
|
|
|
data_len = READ_ONCE(queue->copy_range);
|
2013-06-05 00:22:15 +02:00
|
|
|
if (data_len > entskb->len)
|
2006-01-05 21:15:58 +01:00
|
|
|
data_len = entskb->len;
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2013-12-13 15:22:17 +01:00
|
|
|
hlen = skb_zerocopy_headlen(entskb);
|
|
|
|
hlen = min_t(unsigned int, hlen, data_len);
|
2013-03-17 18:15:55 +01:00
|
|
|
size += sizeof(struct nlattr) + hlen;
|
2012-09-24 14:52:12 +02:00
|
|
|
cap_len = entskb->len;
|
2005-08-10 04:44:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-19 13:49:59 +01:00
|
|
|
nfnl_ct = rcu_dereference(nfnl_ct_hook);
|
|
|
|
|
2015-09-30 23:53:44 +02:00
|
|
|
if (queue->flags & NFQA_CFG_F_CONNTRACK) {
|
2015-10-05 04:47:13 +02:00
|
|
|
if (nfnl_ct != NULL) {
|
|
|
|
ct = nfnl_ct->get_ct(entskb, &ctinfo);
|
2015-09-30 23:53:44 +02:00
|
|
|
if (ct != NULL)
|
2015-10-05 04:47:13 +02:00
|
|
|
size += nfnl_ct->build_size(ct);
|
2015-09-30 23:53:44 +02:00
|
|
|
}
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2013-12-20 17:28:53 +01:00
|
|
|
if (queue->flags & NFQA_CFG_F_UID_GID) {
|
|
|
|
size += (nla_total_size(sizeof(u_int32_t)) /* uid */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t))); /* gid */
|
|
|
|
}
|
|
|
|
|
2015-06-12 12:32:57 +02:00
|
|
|
if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) {
|
|
|
|
seclen = nfqnl_get_sk_secctx(entskb, &secdata);
|
|
|
|
if (seclen)
|
|
|
|
size += nla_total_size(seclen);
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:03:28 +01:00
|
|
|
skb = alloc_skb(size, GFP_ATOMIC);
|
2014-03-26 23:37:45 +01:00
|
|
|
if (!skb) {
|
|
|
|
skb_tx_error(entskb);
|
2017-03-28 16:59:25 +02:00
|
|
|
goto nlmsg_failure;
|
2014-03-26 23:37:45 +01:00
|
|
|
}
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2012-06-27 06:35:27 +02:00
|
|
|
nlh = nlmsg_put(skb, 0, 0,
|
2017-03-28 18:57:32 +02:00
|
|
|
nfnl_msg_type(NFNL_SUBSYS_QUEUE, NFQNL_MSG_PACKET),
|
2012-06-27 06:35:27 +02:00
|
|
|
sizeof(struct nfgenmsg), 0);
|
|
|
|
if (!nlh) {
|
2014-03-26 23:37:45 +01:00
|
|
|
skb_tx_error(entskb);
|
2012-06-27 06:35:27 +02:00
|
|
|
kfree_skb(skb);
|
2017-03-28 16:59:25 +02:00
|
|
|
goto nlmsg_failure;
|
2012-06-27 06:35:27 +02:00
|
|
|
}
|
|
|
|
nfmsg = nlmsg_data(nlh);
|
2015-04-03 22:31:01 +02:00
|
|
|
nfmsg->nfgen_family = entry->state.pf;
|
2005-08-10 04:44:15 +02:00
|
|
|
nfmsg->version = NFNETLINK_V0;
|
|
|
|
nfmsg->res_id = htons(queue->queue_num);
|
|
|
|
|
2011-07-19 11:44:17 +02:00
|
|
|
nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
|
|
|
|
pmsg = nla_data(nla);
|
|
|
|
pmsg->hw_protocol = entskb->protocol;
|
2015-04-03 22:31:01 +02:00
|
|
|
pmsg->hook = entry->state.hook;
|
2011-07-19 11:44:17 +02:00
|
|
|
*packet_id_ptr = &pmsg->packet_id;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2015-04-03 22:31:01 +02:00
|
|
|
indev = entry->state.in;
|
2006-01-05 21:15:58 +01:00
|
|
|
if (indev) {
|
2014-10-01 11:19:17 +02:00
|
|
|
#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 05:22:10 +02:00
|
|
|
#else
|
2015-04-03 22:31:01 +02:00
|
|
|
if (entry->state.pf == PF_BRIDGE) {
|
2005-08-10 05:22:10 +02:00
|
|
|
/* Case 1: indev is physical input device, we need to
|
2007-02-12 20:15:49 +01:00
|
|
|
* look for bridge group (when called from
|
2005-08-10 05:22:10 +02:00
|
|
|
* netfilter_bridge) */
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
|
|
|
|
htonl(indev->ifindex)) ||
|
2005-08-10 05:22:10 +02:00
|
|
|
/* this is the bridge group "brX" */
|
2010-06-15 08:50:45 +02:00
|
|
|
/* rcu_read_lock()ed by __nf_queue */
|
2012-03-30 05:27:38 +02:00
|
|
|
nla_put_be32(skb, NFQA_IFINDEX_INDEV,
|
|
|
|
htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 05:22:10 +02:00
|
|
|
} else {
|
2015-04-02 14:31:41 +02:00
|
|
|
int physinif;
|
|
|
|
|
2005-08-10 05:22:10 +02:00
|
|
|
/* Case 2: indev is bridge group, we need to look for
|
|
|
|
* physical device (when called from ipv4) */
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
|
|
|
|
htonl(indev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2015-04-02 14:31:41 +02:00
|
|
|
|
|
|
|
physinif = nf_bridge_get_physinif(entskb);
|
|
|
|
if (physinif &&
|
2012-03-30 05:27:38 +02:00
|
|
|
nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
|
2015-04-02 14:31:41 +02:00
|
|
|
htonl(physinif)))
|
2012-03-30 05:27:38 +02:00
|
|
|
goto nla_put_failure;
|
2005-08-10 05:22:10 +02:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2006-01-05 21:15:58 +01:00
|
|
|
if (outdev) {
|
2014-10-01 11:19:17 +02:00
|
|
|
#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 05:22:10 +02:00
|
|
|
#else
|
2015-04-03 22:31:01 +02:00
|
|
|
if (entry->state.pf == PF_BRIDGE) {
|
2005-08-10 05:22:10 +02:00
|
|
|
/* Case 1: outdev is physical output device, we need to
|
2007-02-12 20:15:49 +01:00
|
|
|
* look for bridge group (when called from
|
2005-08-10 05:22:10 +02:00
|
|
|
* netfilter_bridge) */
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
|
|
|
|
htonl(outdev->ifindex)) ||
|
2005-08-10 05:22:10 +02:00
|
|
|
/* this is the bridge group "brX" */
|
2010-06-15 08:50:45 +02:00
|
|
|
/* rcu_read_lock()ed by __nf_queue */
|
2012-03-30 05:27:38 +02:00
|
|
|
nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
|
|
|
|
htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 05:22:10 +02:00
|
|
|
} else {
|
2015-04-02 14:31:41 +02:00
|
|
|
int physoutif;
|
|
|
|
|
2005-08-10 05:22:10 +02:00
|
|
|
/* Case 2: outdev is bridge group, we need to look for
|
|
|
|
* physical output device (when called from ipv4) */
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
|
|
|
|
htonl(outdev->ifindex)))
|
|
|
|
goto nla_put_failure;
|
2015-04-02 14:31:41 +02:00
|
|
|
|
|
|
|
physoutif = nf_bridge_get_physoutif(entskb);
|
|
|
|
if (physoutif &&
|
2012-03-30 05:27:38 +02:00
|
|
|
nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
|
2015-04-02 14:31:41 +02:00
|
|
|
htonl(physoutif)))
|
2012-03-30 05:27:38 +02:00
|
|
|
goto nla_put_failure;
|
2005-08-10 05:22:10 +02:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2012-03-30 05:27:38 +02:00
|
|
|
if (entskb->mark &&
|
|
|
|
nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2011-06-16 17:27:04 +02:00
|
|
|
if (indev && entskb->dev &&
|
|
|
|
entskb->mac_header != entskb->network_header) {
|
2005-08-10 04:44:15 +02:00
|
|
|
struct nfqnl_msg_packet_hw phw;
|
2013-08-01 11:36:57 +02:00
|
|
|
int len;
|
|
|
|
|
|
|
|
memset(&phw, 0, sizeof(phw));
|
|
|
|
len = dev_parse_header(entskb, phw.hw_addr);
|
2007-09-27 07:13:38 +02:00
|
|
|
if (len) {
|
|
|
|
phw.hw_addrlen = htons(len);
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
|
|
|
|
goto nla_put_failure;
|
2007-09-27 07:13:38 +02:00
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-26 08:42:11 +01:00
|
|
|
if (nfqnl_put_bridge(entry, skb) < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2019-04-17 02:17:23 +02:00
|
|
|
if (entry->state.hook <= NF_INET_FORWARD && entskb->tstamp) {
|
2005-08-10 04:44:15 +02:00
|
|
|
struct nfqnl_msg_packet_timestamp ts;
|
2016-05-12 14:43:54 +02:00
|
|
|
struct timespec64 kts = ktime_to_timespec64(entskb->tstamp);
|
2015-10-04 19:38:14 +02:00
|
|
|
|
|
|
|
ts.sec = cpu_to_be64(kts.tv_sec);
|
|
|
|
ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2012-03-30 05:27:38 +02:00
|
|
|
if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2013-12-20 17:28:53 +01:00
|
|
|
if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk &&
|
|
|
|
nfqnl_put_sk_uidgid(skb, entskb->sk) < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2015-06-12 12:32:57 +02:00
|
|
|
if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2015-10-05 04:47:13 +02:00
|
|
|
if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0)
|
2013-03-17 18:15:55 +01:00
|
|
|
goto nla_put_failure;
|
|
|
|
|
2013-06-05 00:22:16 +02:00
|
|
|
if (cap_len > data_len &&
|
|
|
|
nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
|
2013-03-17 18:15:55 +01:00
|
|
|
goto nla_put_failure;
|
|
|
|
|
2013-06-29 14:15:47 +02:00
|
|
|
if (nfqnl_put_packet_info(skb, entskb, csum_verify))
|
2013-04-19 06:58:26 +02:00
|
|
|
goto nla_put_failure;
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
if (data_len) {
|
2007-09-28 23:37:03 +02:00
|
|
|
struct nlattr *nla;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2013-03-17 18:15:55 +01:00
|
|
|
if (skb_tailroom(skb) < sizeof(*nla) + hlen)
|
|
|
|
goto nla_put_failure;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
networking: make skb_put & friends return void pointers
It seems like a historic accident that these return unsigned char *,
and in many places that means casts are required, more often than not.
Make these functions (skb_put, __skb_put and pskb_put) return void *
and remove all the casts across the tree, adding a (u8 *) cast only
where the unsigned char pointer was used directly, all done with the
following spatch:
@@
expression SKB, LEN;
typedef u8;
identifier fn = { skb_put, __skb_put };
@@
- *(fn(SKB, LEN))
+ *(u8 *)fn(SKB, LEN)
@@
expression E, SKB, LEN;
identifier fn = { skb_put, __skb_put };
type T;
@@
- E = ((T *)(fn(SKB, LEN)))
+ E = fn(SKB, LEN)
which actually doesn't cover pskb_put since there are only three
users overall.
A handful of stragglers were converted manually, notably a macro in
drivers/isdn/i4l/isdn_bsdcomp.c and, oddly enough, one of the many
instances in net/bluetooth/hci_sock.c. In the former file, I also
had to fix one whitespace problem spatch introduced.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 14:29:21 +02:00
|
|
|
nla = skb_put(skb, sizeof(*nla));
|
2007-09-28 23:37:03 +02:00
|
|
|
nla->nla_type = NFQA_PAYLOAD;
|
2013-03-17 18:15:55 +01:00
|
|
|
nla->nla_len = nla_attr_size(data_len);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2014-03-26 23:37:45 +01:00
|
|
|
if (skb_zerocopy(skb, entskb, data_len, hlen))
|
|
|
|
goto nla_put_failure;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2013-03-17 18:15:55 +01:00
|
|
|
nlh->nlmsg_len = skb->len;
|
2017-03-28 16:59:25 +02:00
|
|
|
if (seclen)
|
|
|
|
security_release_secctx(secdata, seclen);
|
2005-08-10 04:44:15 +02:00
|
|
|
return skb;
|
|
|
|
|
2007-09-28 23:37:03 +02:00
|
|
|
nla_put_failure:
|
2014-03-26 23:37:45 +01:00
|
|
|
skb_tx_error(entskb);
|
2012-08-28 05:14:15 +02:00
|
|
|
kfree_skb(skb);
|
2012-05-13 23:56:26 +02:00
|
|
|
net_err_ratelimited("nf_queue: error creating packet message\n");
|
2017-03-28 16:59:25 +02:00
|
|
|
nlmsg_failure:
|
|
|
|
if (seclen)
|
|
|
|
security_release_secctx(secdata, seclen);
|
2005-08-10 04:44:15 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-07-26 00:02:34 +02:00
|
|
|
static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry *entry)
|
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
|
|
|
|
static const unsigned long flags = IPS_CONFIRMED | IPS_DYING;
|
|
|
|
const struct nf_conn *ct = (void *)skb_nfct(entry->skb);
|
|
|
|
|
|
|
|
if (ct && ((ct->status & flags) == IPS_DYING))
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
static int
|
2013-04-19 06:58:25 +02:00
|
|
|
__nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
|
|
|
|
struct nf_queue_entry *entry)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
|
|
|
struct sk_buff *nskb;
|
2011-01-18 15:27:28 +01:00
|
|
|
int err = -ENOBUFS;
|
2011-07-19 11:44:17 +02:00
|
|
|
__be32 *packet_id_ptr;
|
2012-05-24 05:56:44 +02:00
|
|
|
int failopen = 0;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2013-09-23 13:20:55 +02:00
|
|
|
nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
|
2011-01-18 15:27:28 +01:00
|
|
|
if (nskb == NULL) {
|
|
|
|
err = -ENOMEM;
|
2007-12-05 10:31:01 +01:00
|
|
|
goto err_out;
|
2011-01-18 15:27:28 +01:00
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2017-07-26 00:02:34 +02:00
|
|
|
if (nf_ct_drop_unconfirmed(entry))
|
|
|
|
goto err_out_free_nskb;
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
if (queue->queue_total >= queue->queue_maxlen) {
|
2012-05-24 05:56:44 +02:00
|
|
|
if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
|
|
|
|
failopen = 1;
|
|
|
|
err = 0;
|
|
|
|
} else {
|
|
|
|
queue->queue_dropped++;
|
|
|
|
net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
|
|
|
|
queue->queue_total);
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
goto err_out_free_nskb;
|
|
|
|
}
|
2011-07-19 11:44:17 +02:00
|
|
|
entry->id = ++queue->id_sequence;
|
|
|
|
*packet_id_ptr = htonl(entry->id);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
/* nfnetlink_unicast will either free the nskb or add it to a socket */
|
2013-03-25 00:50:47 +01:00
|
|
|
err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
|
2007-12-05 10:31:01 +01:00
|
|
|
if (err < 0) {
|
2016-03-23 12:58:01 +01:00
|
|
|
if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
|
|
|
|
failopen = 1;
|
|
|
|
err = 0;
|
|
|
|
} else {
|
|
|
|
queue->queue_user_dropped++;
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
__enqueue_entry(queue, entry);
|
|
|
|
|
|
|
|
spin_unlock_bh(&queue->lock);
|
2007-12-05 10:31:01 +01:00
|
|
|
return 0;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
err_out_free_nskb:
|
2007-02-12 20:15:49 +01:00
|
|
|
kfree_skb(nskb);
|
2005-08-10 04:44:15 +02:00
|
|
|
err_out_unlock:
|
|
|
|
spin_unlock_bh(&queue->lock);
|
2012-05-24 05:56:44 +02:00
|
|
|
if (failopen)
|
2018-05-23 09:17:24 +02:00
|
|
|
nfqnl_reinject(entry, NF_ACCEPT);
|
2007-12-05 10:31:01 +01:00
|
|
|
err_out:
|
2011-01-18 15:27:28 +01:00
|
|
|
return err;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2013-04-19 06:58:25 +02:00
|
|
|
static struct nf_queue_entry *
|
|
|
|
nf_queue_entry_dup(struct nf_queue_entry *e)
|
|
|
|
{
|
|
|
|
struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
|
2015-10-13 14:33:27 +02:00
|
|
|
if (entry)
|
|
|
|
nf_queue_entry_get_refs(entry);
|
|
|
|
return entry;
|
2013-04-19 06:58:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-01 11:19:17 +02:00
|
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
2013-04-19 06:58:25 +02:00
|
|
|
/* When called from bridge netfilter, skb->data must point to MAC header
|
|
|
|
* before calling skb_gso_segment(). Else, original MAC header is lost
|
|
|
|
* and segmented skbs will be sent to wrong destination.
|
|
|
|
*/
|
|
|
|
static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
|
|
|
|
{
|
2018-12-18 17:15:15 +01:00
|
|
|
if (nf_bridge_info_get(skb))
|
2013-04-19 06:58:25 +02:00
|
|
|
__skb_push(skb, skb->network_header - skb->mac_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
|
|
|
|
{
|
2018-12-18 17:15:15 +01:00
|
|
|
if (nf_bridge_info_get(skb))
|
2013-04-19 06:58:25 +02:00
|
|
|
__skb_pull(skb, skb->network_header - skb->mac_header);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define nf_bridge_adjust_skb_data(s) do {} while (0)
|
|
|
|
#define nf_bridge_adjust_segmented_data(s) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void free_entry(struct nf_queue_entry *entry)
|
|
|
|
{
|
|
|
|
nf_queue_entry_release_refs(entry);
|
|
|
|
kfree(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
__nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
|
|
|
|
struct sk_buff *skb, struct nf_queue_entry *entry)
|
|
|
|
{
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
struct nf_queue_entry *entry_seg;
|
|
|
|
|
|
|
|
nf_bridge_adjust_segmented_data(skb);
|
|
|
|
|
|
|
|
if (skb->next == NULL) { /* last packet, no need to copy entry */
|
|
|
|
struct sk_buff *gso_skb = entry->skb;
|
|
|
|
entry->skb = skb;
|
|
|
|
ret = __nfqnl_enqueue_packet(net, queue, entry);
|
|
|
|
if (ret)
|
|
|
|
entry->skb = gso_skb;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-30 05:42:53 +02:00
|
|
|
skb_mark_not_on_list(skb);
|
2013-04-19 06:58:25 +02:00
|
|
|
|
|
|
|
entry_seg = nf_queue_entry_dup(entry);
|
|
|
|
if (entry_seg) {
|
|
|
|
entry_seg->skb = skb;
|
|
|
|
ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
|
|
|
|
if (ret)
|
|
|
|
free_entry(entry_seg);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
|
|
|
|
{
|
|
|
|
unsigned int queued;
|
|
|
|
struct nfqnl_instance *queue;
|
|
|
|
struct sk_buff *skb, *segs;
|
|
|
|
int err = -ENOBUFS;
|
2015-09-16 03:04:17 +02:00
|
|
|
struct net *net = entry->state.net;
|
2013-04-19 06:58:25 +02:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
|
|
|
|
2016-09-21 17:35:04 +02:00
|
|
|
/* rcu_read_lock()ed by nf_hook_thresh */
|
2013-04-19 06:58:25 +02:00
|
|
|
queue = instance_lookup(q, queuenum);
|
|
|
|
if (!queue)
|
|
|
|
return -ESRCH;
|
|
|
|
|
|
|
|
if (queue->copy_mode == NFQNL_COPY_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
skb = entry->skb;
|
|
|
|
|
2015-04-03 22:31:01 +02:00
|
|
|
switch (entry->state.pf) {
|
2013-04-19 06:58:25 +02:00
|
|
|
case NFPROTO_IPV4:
|
|
|
|
skb->protocol = htons(ETH_P_IP);
|
|
|
|
break;
|
|
|
|
case NFPROTO_IPV6:
|
|
|
|
skb->protocol = htons(ETH_P_IPV6);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-07 18:42:00 +02:00
|
|
|
if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb))
|
|
|
|
return __nfqnl_enqueue_packet(net, queue, entry);
|
|
|
|
|
2013-04-19 06:58:25 +02:00
|
|
|
nf_bridge_adjust_skb_data(skb);
|
|
|
|
segs = skb_gso_segment(skb, 0);
|
|
|
|
/* Does not use PTR_ERR to limit the number of error codes that can be
|
2015-10-13 14:33:27 +02:00
|
|
|
* returned by nf_queue. For instance, callers rely on -ESRCH to
|
2013-04-19 06:58:25 +02:00
|
|
|
* mean 'ignore this hook'.
|
|
|
|
*/
|
2014-10-20 13:49:17 +02:00
|
|
|
if (IS_ERR_OR_NULL(segs))
|
2013-04-19 06:58:25 +02:00
|
|
|
goto out_err;
|
|
|
|
queued = 0;
|
|
|
|
err = 0;
|
|
|
|
do {
|
|
|
|
struct sk_buff *nskb = segs->next;
|
|
|
|
if (err == 0)
|
|
|
|
err = __nfqnl_enqueue_packet_gso(net, queue,
|
|
|
|
segs, entry);
|
|
|
|
if (err == 0)
|
|
|
|
queued++;
|
|
|
|
else
|
|
|
|
kfree_skb(segs);
|
|
|
|
segs = nskb;
|
|
|
|
} while (segs);
|
|
|
|
|
|
|
|
if (queued) {
|
|
|
|
if (err) /* some segments are already queued */
|
|
|
|
free_entry(entry);
|
|
|
|
kfree_skb(skb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
out_err:
|
|
|
|
nf_bridge_adjust_segmented_data(skb);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
static int
|
2012-06-07 13:31:25 +02:00
|
|
|
nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2008-02-20 02:17:52 +01:00
|
|
|
struct sk_buff *nskb;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2006-11-15 04:48:09 +01:00
|
|
|
if (diff < 0) {
|
|
|
|
if (pskb_trim(e->skb, data_len))
|
|
|
|
return -ENOMEM;
|
|
|
|
} else if (diff > 0) {
|
2005-08-10 04:44:15 +02:00
|
|
|
if (data_len > 0xFFFF)
|
|
|
|
return -EINVAL;
|
|
|
|
if (diff > skb_tailroom(e->skb)) {
|
netfilter: {nfnetlink,ip,ip6}_queue: fix skb_over_panic when enlarging packets
While reinjecting *bigger* modified versions of IPv6 packets using
libnetfilter_queue, things work fine on a 2.6.24 kernel (2.6.22 too)
but I get the following on recents kernels (2.6.25, trace below is
against today's net-2.6 git tree):
skb_over_panic: text:c04fddb0 len:696 put:632 head:f7592c00 data:f7592c00 tail:0xf7592eb8 end:0xf7592e80 dev:eth0
------------[ cut here ]------------
invalid opcode: 0000 [#1] PREEMPT
Process sendd (pid: 3657, ti=f6014000 task=f77c31d0 task.ti=f6014000)
Stack: c071e638 c04fddb0 000002b8 00000278 f7592c00 f7592c00 f7592eb8 f7592e80
f763c000 f6bc5200 f7592c40 f6015c34 c04cdbfc f6bc5200 00000278 f6015c60
c04fddb0 00000020 f72a10c0 f751b420 00000001 0000000a 000002b8 c065582c
Call Trace:
[<c04fddb0>] ? nfqnl_recv_verdict+0x1c0/0x2e0
[<c04cdbfc>] ? skb_put+0x3c/0x40
[<c04fddb0>] ? nfqnl_recv_verdict+0x1c0/0x2e0
[<c04fd115>] ? nfnetlink_rcv_msg+0xf5/0x160
[<c04fd03e>] ? nfnetlink_rcv_msg+0x1e/0x160
[<c04fd020>] ? nfnetlink_rcv_msg+0x0/0x160
[<c04f8ed7>] ? netlink_rcv_skb+0x77/0xa0
[<c04fcefc>] ? nfnetlink_rcv+0x1c/0x30
[<c04f8c73>] ? netlink_unicast+0x243/0x2b0
[<c04cfaba>] ? memcpy_fromiovec+0x4a/0x70
[<c04f9406>] ? netlink_sendmsg+0x1c6/0x270
[<c04c8244>] ? sock_sendmsg+0xc4/0xf0
[<c011970d>] ? set_next_entity+0x1d/0x50
[<c0133a80>] ? autoremove_wake_function+0x0/0x40
[<c0118f9e>] ? __wake_up_common+0x3e/0x70
[<c0342fbf>] ? n_tty_receive_buf+0x34f/0x1280
[<c011d308>] ? __wake_up+0x68/0x70
[<c02cea47>] ? copy_from_user+0x37/0x70
[<c04cfd7c>] ? verify_iovec+0x2c/0x90
[<c04c837a>] ? sys_sendmsg+0x10a/0x230
[<c011967a>] ? __dequeue_entity+0x2a/0xa0
[<c011970d>] ? set_next_entity+0x1d/0x50
[<c0345397>] ? pty_write+0x47/0x60
[<c033d59b>] ? tty_default_put_char+0x1b/0x20
[<c011d2e9>] ? __wake_up+0x49/0x70
[<c033df99>] ? tty_ldisc_deref+0x39/0x90
[<c033ff20>] ? tty_write+0x1a0/0x1b0
[<c04c93af>] ? sys_socketcall+0x7f/0x260
[<c0102ff9>] ? sysenter_past_esp+0x6a/0x91
[<c05f0000>] ? snd_intel8x0m_probe+0x270/0x6e0
=======================
Code: 00 00 89 5c 24 14 8b 98 9c 00 00 00 89 54 24 0c 89 5c 24 10 8b 40 50 89 4c 24 04 c7 04 24 38 e6 71 c0 89 44 24 08 e8 c4 46 c5 ff <0f> 0b eb fe 55 89 e5 56 89 d6 53 89 c3 83 ec 0c 8b 40 50 39 d0
EIP: [<c04ccdfc>] skb_over_panic+0x5c/0x60 SS:ESP 0068:f6015bf8
Looking at the code, I ended up in nfq_mangle() function (called by
nfqnl_recv_verdict()) which performs a call to skb_copy_expand() due to
the increased size of data passed to the function. AFAICT, it should ask
for 'diff' instead of 'diff - skb_tailroom(e->skb)'. Because the
resulting sk_buff has not enough space to support the skb_put(skb, diff)
call a few lines later, this results in the call to skb_over_panic().
The patch below asks for allocation of a copy with enough space for
mangled packet and the same amount of headroom as old sk_buff. While
looking at how the regression appeared (e2b58a67), I noticed the same
pattern in ipq_mangle_ipv6() and ipq_mangle_ipv4(). The patch corrects
those locations too.
Tested with bigger reinjected IPv6 packets (nfqnl_mangle() path), things
are ok (2.6.25 and today's net-2.6 git tree).
Signed-off-by: Arnaud Ebalard <arno@natisbad.org>
Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-04-29 12:16:34 +02:00
|
|
|
nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
|
|
|
|
diff, GFP_ATOMIC);
|
2018-03-12 14:06:29 +01:00
|
|
|
if (!nskb)
|
2008-02-20 02:17:52 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
kfree_skb(e->skb);
|
|
|
|
e->skb = nskb;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
skb_put(e->skb, diff);
|
|
|
|
}
|
2007-10-14 09:39:18 +02:00
|
|
|
if (!skb_make_writable(e->skb, data_len))
|
2005-08-10 04:44:15 +02:00
|
|
|
return -ENOMEM;
|
2007-03-31 16:55:19 +02:00
|
|
|
skb_copy_to_linear_data(e->skb, data, data_len);
|
2005-09-07 00:10:00 +02:00
|
|
|
e->skb->ip_summed = CHECKSUM_NONE;
|
2005-08-10 04:44:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_set_mode(struct nfqnl_instance *queue,
|
|
|
|
unsigned char mode, unsigned int range)
|
|
|
|
{
|
2007-12-05 10:29:05 +01:00
|
|
|
int status = 0;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
spin_lock_bh(&queue->lock);
|
2007-12-05 10:29:05 +01:00
|
|
|
switch (mode) {
|
|
|
|
case NFQNL_COPY_NONE:
|
|
|
|
case NFQNL_COPY_META:
|
|
|
|
queue->copy_mode = mode;
|
|
|
|
queue->copy_range = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NFQNL_COPY_PACKET:
|
|
|
|
queue->copy_mode = mode;
|
2013-06-05 00:22:15 +02:00
|
|
|
if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
|
|
|
|
queue->copy_range = NFQNL_MAX_COPY_RANGE;
|
2007-12-05 10:29:05 +01:00
|
|
|
else
|
|
|
|
queue->copy_range = range;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
status = -EINVAL;
|
|
|
|
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-05 10:26:33 +01:00
|
|
|
dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2018-12-18 17:15:15 +01:00
|
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
|
|
|
int physinif, physoutif;
|
|
|
|
|
|
|
|
physinif = nf_bridge_get_physinif(entry->skb);
|
|
|
|
physoutif = nf_bridge_get_physoutif(entry->skb);
|
|
|
|
|
|
|
|
if (physinif == ifindex || physoutif == ifindex)
|
|
|
|
return 1;
|
|
|
|
#endif
|
2015-04-03 22:31:01 +02:00
|
|
|
if (entry->state.in)
|
|
|
|
if (entry->state.in->ifindex == ifindex)
|
2005-08-10 04:44:15 +02:00
|
|
|
return 1;
|
2015-04-03 22:31:01 +02:00
|
|
|
if (entry->state.out)
|
|
|
|
if (entry->state.out->ifindex == ifindex)
|
2005-08-10 04:44:15 +02:00
|
|
|
return 1;
|
2015-04-02 14:31:41 +02:00
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drop all packets with either indev or outdev == ifindex from all queue
|
|
|
|
* instances */
|
|
|
|
static void
|
2013-03-25 00:50:47 +01:00
|
|
|
nfqnl_dev_drop(struct net *net, int ifindex)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
|
|
|
int i;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
rcu_read_lock();
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++) {
|
2005-08-10 04:44:15 +02:00
|
|
|
struct nfqnl_instance *inst;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct hlist_head *head = &q->instance_table[i];
|
2005-08-10 04:44:15 +02:00
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 02:06:00 +01:00
|
|
|
hlist_for_each_entry_rcu(inst, head, hlist)
|
2007-12-05 10:25:30 +01:00
|
|
|
nfqnl_flush(inst, dev_cmp, ifindex);
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
rcu_read_unlock();
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_rcv_dev_event(struct notifier_block *this,
|
|
|
|
unsigned long event, void *ptr)
|
|
|
|
{
|
2013-05-28 03:30:21 +02:00
|
|
|
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
/* Drop any packets associated with the downed device */
|
|
|
|
if (event == NETDEV_DOWN)
|
2013-03-25 00:50:47 +01:00
|
|
|
nfqnl_dev_drop(dev_net(dev), dev->ifindex);
|
2005-08-10 04:44:15 +02:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block nfqnl_dev_notifier = {
|
|
|
|
.notifier_call = nfqnl_rcv_dev_event,
|
|
|
|
};
|
|
|
|
|
2017-12-01 00:21:03 +01:00
|
|
|
static void nfqnl_nf_hook_drop(struct net *net)
|
2015-06-19 21:03:39 +02:00
|
|
|
{
|
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++) {
|
|
|
|
struct nfqnl_instance *inst;
|
|
|
|
struct hlist_head *head = &q->instance_table[i];
|
|
|
|
|
2017-12-01 00:21:03 +01:00
|
|
|
hlist_for_each_entry_rcu(inst, head, hlist)
|
2017-04-24 15:37:41 +02:00
|
|
|
nfqnl_flush(inst, NULL, 0);
|
2015-06-19 21:03:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
static int
|
|
|
|
nfqnl_rcv_nl_event(struct notifier_block *this,
|
|
|
|
unsigned long event, void *ptr)
|
|
|
|
{
|
|
|
|
struct netlink_notify *n = ptr;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2009-11-06 17:04:00 +01:00
|
|
|
if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
|
2005-08-10 04:44:15 +02:00
|
|
|
int i;
|
|
|
|
|
2012-09-07 22:12:54 +02:00
|
|
|
/* destroy all instances for this portid */
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_lock(&q->instances_lock);
|
2007-12-05 10:28:50 +01:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++) {
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 02:06:00 +01:00
|
|
|
struct hlist_node *t2;
|
2005-08-10 04:44:15 +02:00
|
|
|
struct nfqnl_instance *inst;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct hlist_head *head = &q->instance_table[i];
|
2005-08-10 04:44:15 +02:00
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 02:06:00 +01:00
|
|
|
hlist_for_each_entry_safe(inst, t2, head, hlist) {
|
2013-03-25 00:50:47 +01:00
|
|
|
if (n->portid == inst->peer_portid)
|
2005-08-10 04:44:15 +02:00
|
|
|
__instance_destroy(inst);
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_unlock(&q->instances_lock);
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block nfqnl_rtnl_notifier = {
|
|
|
|
.notifier_call = nfqnl_rcv_nl_event,
|
|
|
|
};
|
|
|
|
|
2016-03-26 08:42:12 +01:00
|
|
|
static const struct nla_policy nfqa_vlan_policy[NFQA_VLAN_MAX + 1] = {
|
|
|
|
[NFQA_VLAN_TCI] = { .type = NLA_U16},
|
|
|
|
[NFQA_VLAN_PROTO] = { .type = NLA_U16},
|
|
|
|
};
|
|
|
|
|
2007-09-28 23:39:26 +02:00
|
|
|
static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
|
|
|
|
[NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
|
|
|
|
[NFQA_MARK] = { .type = NLA_U32 },
|
|
|
|
[NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
|
2012-06-07 12:13:39 +02:00
|
|
|
[NFQA_CT] = { .type = NLA_UNSPEC },
|
2013-08-07 18:13:20 +02:00
|
|
|
[NFQA_EXP] = { .type = NLA_UNSPEC },
|
2016-03-26 08:42:12 +01:00
|
|
|
[NFQA_VLAN] = { .type = NLA_NESTED },
|
2005-08-10 04:50:45 +02:00
|
|
|
};
|
|
|
|
|
2011-07-19 11:46:33 +02:00
|
|
|
static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
|
|
|
|
[NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
|
|
|
|
[NFQA_MARK] = { .type = NLA_U32 },
|
|
|
|
};
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
static struct nfqnl_instance *
|
2015-04-13 00:52:37 +02:00
|
|
|
verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid)
|
2011-07-19 11:46:33 +02:00
|
|
|
{
|
|
|
|
struct nfqnl_instance *queue;
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
queue = instance_lookup(q, queue_num);
|
2011-07-19 11:46:33 +02:00
|
|
|
if (!queue)
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
2012-09-07 22:12:54 +02:00
|
|
|
if (queue->peer_portid != nlportid)
|
2011-07-19 11:46:33 +02:00
|
|
|
return ERR_PTR(-EPERM);
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfqnl_msg_verdict_hdr*
|
|
|
|
verdicthdr_get(const struct nlattr * const nfqa[])
|
|
|
|
{
|
|
|
|
struct nfqnl_msg_verdict_hdr *vhdr;
|
|
|
|
unsigned int verdict;
|
|
|
|
|
|
|
|
if (!nfqa[NFQA_VERDICT_HDR])
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
|
2011-08-30 15:01:20 +02:00
|
|
|
verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
|
|
|
|
if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
|
2011-07-19 11:46:33 +02:00
|
|
|
return NULL;
|
|
|
|
return vhdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nfq_id_after(unsigned int id, unsigned int max)
|
|
|
|
{
|
|
|
|
return (int)(id - max) > 0;
|
|
|
|
}
|
|
|
|
|
2015-12-15 18:41:56 +01:00
|
|
|
static int nfqnl_recv_verdict_batch(struct net *net, struct sock *ctnl,
|
|
|
|
struct sk_buff *skb,
|
|
|
|
const struct nlmsghdr *nlh,
|
2017-06-19 19:35:46 +02:00
|
|
|
const struct nlattr * const nfqa[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2011-07-19 11:46:33 +02:00
|
|
|
{
|
2012-06-27 06:35:27 +02:00
|
|
|
struct nfgenmsg *nfmsg = nlmsg_data(nlh);
|
2011-07-19 11:46:33 +02:00
|
|
|
struct nf_queue_entry *entry, *tmp;
|
|
|
|
unsigned int verdict, maxid;
|
|
|
|
struct nfqnl_msg_verdict_hdr *vhdr;
|
|
|
|
struct nfqnl_instance *queue;
|
|
|
|
LIST_HEAD(batch_list);
|
|
|
|
u16 queue_num = ntohs(nfmsg->res_id);
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
|
|
|
|
|
|
|
queue = verdict_instance_lookup(q, queue_num,
|
|
|
|
NETLINK_CB(skb).portid);
|
2011-07-19 11:46:33 +02:00
|
|
|
if (IS_ERR(queue))
|
|
|
|
return PTR_ERR(queue);
|
|
|
|
|
|
|
|
vhdr = verdicthdr_get(nfqa);
|
|
|
|
if (!vhdr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
verdict = ntohl(vhdr->verdict);
|
|
|
|
maxid = ntohl(vhdr->id);
|
|
|
|
|
|
|
|
spin_lock_bh(&queue->lock);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
|
|
|
|
if (nfq_id_after(entry->id, maxid))
|
|
|
|
break;
|
|
|
|
__dequeue_entry(queue, entry);
|
|
|
|
list_add_tail(&entry->list, &batch_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
if (list_empty(&batch_list))
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(entry, tmp, &batch_list, list) {
|
|
|
|
if (nfqa[NFQA_MARK])
|
|
|
|
entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
|
2018-05-23 09:17:24 +02:00
|
|
|
|
|
|
|
nfqnl_reinject(entry, verdict);
|
2011-07-19 11:46:33 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-05 04:47:13 +02:00
|
|
|
static struct nf_conn *nfqnl_ct_parse(struct nfnl_ct_hook *nfnl_ct,
|
2015-09-30 23:53:44 +02:00
|
|
|
const struct nlmsghdr *nlh,
|
|
|
|
const struct nlattr * const nfqa[],
|
|
|
|
struct nf_queue_entry *entry,
|
|
|
|
enum ip_conntrack_info *ctinfo)
|
|
|
|
{
|
|
|
|
struct nf_conn *ct;
|
|
|
|
|
2015-10-05 04:47:13 +02:00
|
|
|
ct = nfnl_ct->get_ct(entry->skb, ctinfo);
|
2015-09-30 23:53:44 +02:00
|
|
|
if (ct == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-10-05 04:47:13 +02:00
|
|
|
if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
|
2015-09-30 23:53:44 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (nfqa[NFQA_EXP])
|
2015-10-05 04:47:13 +02:00
|
|
|
nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
|
2015-09-30 23:53:44 +02:00
|
|
|
NETLINK_CB(entry->skb).portid,
|
|
|
|
nlmsg_report(nlh));
|
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
2016-03-26 08:42:12 +01:00
|
|
|
static int nfqa_parse_bridge(struct nf_queue_entry *entry,
|
|
|
|
const struct nlattr * const nfqa[])
|
|
|
|
{
|
|
|
|
if (nfqa[NFQA_VLAN]) {
|
|
|
|
struct nlattr *tb[NFQA_VLAN_MAX + 1];
|
|
|
|
int err;
|
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 14:07:28 +02:00
|
|
|
err = nla_parse_nested_deprecated(tb, NFQA_VLAN_MAX,
|
|
|
|
nfqa[NFQA_VLAN],
|
|
|
|
nfqa_vlan_policy, NULL);
|
2016-03-26 08:42:12 +01:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-11-09 00:18:02 +01:00
|
|
|
__vlan_hwaccel_put_tag(entry->skb,
|
|
|
|
nla_get_be16(tb[NFQA_VLAN_PROTO]),
|
|
|
|
ntohs(nla_get_be16(tb[NFQA_VLAN_TCI])));
|
2016-03-26 08:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nfqa[NFQA_L2HDR]) {
|
|
|
|
int mac_header_len = entry->skb->network_header -
|
|
|
|
entry->skb->mac_header;
|
|
|
|
|
|
|
|
if (mac_header_len != nla_len(nfqa[NFQA_L2HDR]))
|
|
|
|
return -EINVAL;
|
|
|
|
else if (mac_header_len > 0)
|
|
|
|
memcpy(skb_mac_header(entry->skb),
|
|
|
|
nla_data(nfqa[NFQA_L2HDR]),
|
|
|
|
mac_header_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-15 18:41:56 +01:00
|
|
|
static int nfqnl_recv_verdict(struct net *net, struct sock *ctnl,
|
|
|
|
struct sk_buff *skb,
|
|
|
|
const struct nlmsghdr *nlh,
|
2017-06-19 19:35:46 +02:00
|
|
|
const struct nlattr * const nfqa[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2012-06-27 06:35:27 +02:00
|
|
|
struct nfgenmsg *nfmsg = nlmsg_data(nlh);
|
2005-08-10 04:44:15 +02:00
|
|
|
u_int16_t queue_num = ntohs(nfmsg->res_id);
|
|
|
|
struct nfqnl_msg_verdict_hdr *vhdr;
|
|
|
|
struct nfqnl_instance *queue;
|
|
|
|
unsigned int verdict;
|
2007-12-05 10:26:33 +01:00
|
|
|
struct nf_queue_entry *entry;
|
2012-06-07 13:31:25 +02:00
|
|
|
enum ip_conntrack_info uninitialized_var(ctinfo);
|
2015-10-05 04:47:13 +02:00
|
|
|
struct nfnl_ct_hook *nfnl_ct;
|
2012-06-07 13:31:25 +02:00
|
|
|
struct nf_conn *ct = NULL;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
2016-03-26 08:42:12 +01:00
|
|
|
int err;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2016-08-08 16:07:27 +02:00
|
|
|
queue = verdict_instance_lookup(q, queue_num,
|
|
|
|
NETLINK_CB(skb).portid);
|
2011-07-19 11:46:33 +02:00
|
|
|
if (IS_ERR(queue))
|
|
|
|
return PTR_ERR(queue);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2011-07-19 11:46:33 +02:00
|
|
|
vhdr = verdicthdr_get(nfqa);
|
|
|
|
if (!vhdr)
|
2011-07-18 16:08:27 +02:00
|
|
|
return -EINVAL;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
verdict = ntohl(vhdr->verdict);
|
|
|
|
|
2007-12-05 10:25:30 +01:00
|
|
|
entry = find_dequeue_entry(queue, ntohl(vhdr->id));
|
2011-07-18 16:08:27 +02:00
|
|
|
if (entry == NULL)
|
|
|
|
return -ENOENT;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2015-11-19 13:49:59 +01:00
|
|
|
/* rcu lock already held from nfnl->call_rcu. */
|
|
|
|
nfnl_ct = rcu_dereference(nfnl_ct_hook);
|
|
|
|
|
2013-08-07 18:13:20 +02:00
|
|
|
if (nfqa[NFQA_CT]) {
|
2015-10-05 04:47:13 +02:00
|
|
|
if (nfnl_ct != NULL)
|
|
|
|
ct = nfqnl_ct_parse(nfnl_ct, nlh, nfqa, entry, &ctinfo);
|
2013-08-07 18:13:20 +02:00
|
|
|
}
|
2012-06-07 12:13:39 +02:00
|
|
|
|
2016-03-26 08:42:12 +01:00
|
|
|
if (entry->state.pf == PF_BRIDGE) {
|
|
|
|
err = nfqa_parse_bridge(entry, nfqa);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-09-28 23:37:03 +02:00
|
|
|
if (nfqa[NFQA_PAYLOAD]) {
|
2012-06-07 13:31:25 +02:00
|
|
|
u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
|
|
|
|
int diff = payload_len - entry->skb->len;
|
|
|
|
|
2007-09-28 23:37:03 +02:00
|
|
|
if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
|
2012-06-07 13:31:25 +02:00
|
|
|
payload_len, entry, diff) < 0)
|
2005-08-10 04:44:15 +02:00
|
|
|
verdict = NF_DROP;
|
2012-06-07 13:31:25 +02:00
|
|
|
|
2015-09-30 23:53:44 +02:00
|
|
|
if (ct && diff)
|
2015-10-05 04:47:13 +02:00
|
|
|
nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff);
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-28 23:37:03 +02:00
|
|
|
if (nfqa[NFQA_MARK])
|
2007-12-05 10:30:02 +01:00
|
|
|
entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2018-05-23 09:17:24 +02:00
|
|
|
nfqnl_reinject(entry, verdict);
|
2005-08-10 04:44:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-15 18:41:56 +01:00
|
|
|
static int nfqnl_recv_unsupp(struct net *net, struct sock *ctnl,
|
|
|
|
struct sk_buff *skb, const struct nlmsghdr *nlh,
|
2017-06-19 19:35:46 +02:00
|
|
|
const struct nlattr * const nfqa[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2007-09-28 23:39:26 +02:00
|
|
|
static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
|
|
|
|
[NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
|
|
|
|
[NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
|
2018-06-13 18:13:39 +02:00
|
|
|
[NFQA_CFG_QUEUE_MAXLEN] = { .type = NLA_U32 },
|
|
|
|
[NFQA_CFG_MASK] = { .type = NLA_U32 },
|
|
|
|
[NFQA_CFG_FLAGS] = { .type = NLA_U32 },
|
2005-08-10 04:50:45 +02:00
|
|
|
};
|
|
|
|
|
2007-12-05 10:23:57 +01:00
|
|
|
static const struct nf_queue_handler nfqh = {
|
2017-04-02 11:22:12 +02:00
|
|
|
.outfn = nfqnl_enqueue_packet,
|
|
|
|
.nf_hook_drop = nfqnl_nf_hook_drop,
|
2005-08-10 05:23:11 +02:00
|
|
|
};
|
|
|
|
|
2015-12-15 18:41:56 +01:00
|
|
|
static int nfqnl_recv_config(struct net *net, struct sock *ctnl,
|
|
|
|
struct sk_buff *skb, const struct nlmsghdr *nlh,
|
2017-06-19 19:35:46 +02:00
|
|
|
const struct nlattr * const nfqa[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2012-06-27 06:35:27 +02:00
|
|
|
struct nfgenmsg *nfmsg = nlmsg_data(nlh);
|
2005-08-10 04:44:15 +02:00
|
|
|
u_int16_t queue_num = ntohs(nfmsg->res_id);
|
|
|
|
struct nfqnl_instance *queue;
|
2007-12-05 10:28:50 +01:00
|
|
|
struct nfqnl_msg_config_cmd *cmd = NULL;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
2016-01-05 01:28:05 +01:00
|
|
|
__u32 flags = 0, mask = 0;
|
2005-08-10 04:50:45 +02:00
|
|
|
int ret = 0;
|
2005-08-10 04:44:15 +02:00
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
if (nfqa[NFQA_CFG_CMD]) {
|
|
|
|
cmd = nla_data(nfqa[NFQA_CFG_CMD]);
|
|
|
|
|
2012-11-23 07:22:21 +01:00
|
|
|
/* Obsolete commands without queue context */
|
2007-12-05 10:28:50 +01:00
|
|
|
switch (cmd->command) {
|
2012-11-23 07:22:21 +01:00
|
|
|
case NFQNL_CFG_CMD_PF_BIND: return 0;
|
|
|
|
case NFQNL_CFG_CMD_PF_UNBIND: return 0;
|
2007-12-05 10:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 01:28:05 +01:00
|
|
|
/* Check if we support these flags in first place, dependencies should
|
|
|
|
* be there too not to break atomicity.
|
|
|
|
*/
|
|
|
|
if (nfqa[NFQA_CFG_FLAGS]) {
|
|
|
|
if (!nfqa[NFQA_CFG_MASK]) {
|
|
|
|
/* A mask is needed to specify which flags are being
|
|
|
|
* changed.
|
|
|
|
*/
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
|
|
|
|
mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
|
|
|
|
|
|
|
|
if (flags >= NFQA_CFG_F_MAX)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
#if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
|
|
|
|
if (flags & mask & NFQA_CFG_F_SECCTX)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
#endif
|
2016-01-05 01:32:59 +01:00
|
|
|
if ((flags & mask & NFQA_CFG_F_CONNTRACK) &&
|
|
|
|
!rcu_access_pointer(nfnl_ct_hook)) {
|
|
|
|
#ifdef CONFIG_MODULES
|
|
|
|
nfnl_unlock(NFNL_SUBSYS_QUEUE);
|
|
|
|
request_module("ip_conntrack_netlink");
|
|
|
|
nfnl_lock(NFNL_SUBSYS_QUEUE);
|
|
|
|
if (rcu_access_pointer(nfnl_ct_hook))
|
|
|
|
return -EAGAIN;
|
|
|
|
#endif
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2016-01-05 01:28:05 +01:00
|
|
|
}
|
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
rcu_read_lock();
|
2013-03-25 00:50:47 +01:00
|
|
|
queue = instance_lookup(q, queue_num);
|
2012-09-07 22:12:54 +02:00
|
|
|
if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
|
2007-12-05 10:28:30 +01:00
|
|
|
ret = -EPERM;
|
2007-12-05 10:28:50 +01:00
|
|
|
goto err_out_unlock;
|
2007-12-05 10:28:30 +01:00
|
|
|
}
|
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
if (cmd != NULL) {
|
2005-08-10 04:44:15 +02:00
|
|
|
switch (cmd->command) {
|
|
|
|
case NFQNL_CFG_CMD_BIND:
|
2007-12-05 10:28:50 +01:00
|
|
|
if (queue) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2013-03-25 00:50:47 +01:00
|
|
|
queue = instance_create(q, queue_num,
|
|
|
|
NETLINK_CB(skb).portid);
|
2007-12-18 07:41:21 +01:00
|
|
|
if (IS_ERR(queue)) {
|
|
|
|
ret = PTR_ERR(queue);
|
2007-12-05 10:28:50 +01:00
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2005-08-10 04:44:15 +02:00
|
|
|
break;
|
|
|
|
case NFQNL_CFG_CMD_UNBIND:
|
2007-12-05 10:28:50 +01:00
|
|
|
if (!queue) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2013-03-25 00:50:47 +01:00
|
|
|
instance_destroy(q, queue);
|
2016-01-05 01:29:54 +01:00
|
|
|
goto err_out_unlock;
|
2005-08-10 04:44:15 +02:00
|
|
|
case NFQNL_CFG_CMD_PF_BIND:
|
|
|
|
case NFQNL_CFG_CMD_PF_UNBIND:
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-18 07:40:19 +01:00
|
|
|
ret = -ENOTSUPP;
|
2016-01-05 01:31:40 +01:00
|
|
|
goto err_out_unlock;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 01:28:05 +01:00
|
|
|
if (!queue) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
|
|
|
|
2007-09-28 23:37:03 +02:00
|
|
|
if (nfqa[NFQA_CFG_PARAMS]) {
|
2016-01-05 01:28:05 +01:00
|
|
|
struct nfqnl_msg_config_params *params =
|
|
|
|
nla_data(nfqa[NFQA_CFG_PARAMS]);
|
2005-08-10 04:44:15 +02:00
|
|
|
|
|
|
|
nfqnl_set_mode(queue, params->copy_mode,
|
|
|
|
ntohl(params->copy_range));
|
|
|
|
}
|
|
|
|
|
2007-09-28 23:37:03 +02:00
|
|
|
if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
|
2016-01-05 01:28:05 +01:00
|
|
|
__be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
|
2007-12-05 10:28:30 +01:00
|
|
|
|
2006-11-29 02:35:33 +01:00
|
|
|
spin_lock_bh(&queue->lock);
|
|
|
|
queue->queue_maxlen = ntohl(*queue_maxlen);
|
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
}
|
|
|
|
|
2012-05-24 05:56:44 +02:00
|
|
|
if (nfqa[NFQA_CFG_FLAGS]) {
|
|
|
|
spin_lock_bh(&queue->lock);
|
|
|
|
queue->flags &= ~mask;
|
|
|
|
queue->flags |= flags & mask;
|
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
}
|
|
|
|
|
2007-12-05 10:28:50 +01:00
|
|
|
err_out_unlock:
|
|
|
|
rcu_read_unlock();
|
2005-08-10 04:50:45 +02:00
|
|
|
return ret;
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-28 23:15:45 +02:00
|
|
|
static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
|
2011-07-18 16:08:27 +02:00
|
|
|
[NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
|
2005-11-15 00:24:59 +01:00
|
|
|
.attr_count = NFQA_MAX, },
|
2011-07-18 16:08:27 +02:00
|
|
|
[NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
|
2007-09-28 23:39:26 +02:00
|
|
|
.attr_count = NFQA_MAX,
|
|
|
|
.policy = nfqa_verdict_policy },
|
2005-08-10 04:44:15 +02:00
|
|
|
[NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
|
2007-09-28 23:39:26 +02:00
|
|
|
.attr_count = NFQA_CFG_MAX,
|
|
|
|
.policy = nfqa_cfg_policy },
|
2011-07-19 11:46:33 +02:00
|
|
|
[NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
|
|
|
|
.attr_count = NFQA_MAX,
|
|
|
|
.policy = nfqa_verdict_batch_policy },
|
2005-08-10 04:44:15 +02:00
|
|
|
};
|
|
|
|
|
2007-09-28 23:15:45 +02:00
|
|
|
static const struct nfnetlink_subsystem nfqnl_subsys = {
|
2005-08-10 04:44:15 +02:00
|
|
|
.name = "nf_queue",
|
|
|
|
.subsys_id = NFNL_SUBSYS_QUEUE,
|
|
|
|
.cb_count = NFQNL_MSG_MAX,
|
|
|
|
.cb = nfqnl_cb,
|
|
|
|
};
|
|
|
|
|
2005-08-10 04:50:45 +02:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct iter_state {
|
2013-03-25 00:50:47 +01:00
|
|
|
struct seq_net_private p;
|
2005-08-10 04:50:45 +02:00
|
|
|
unsigned int bucket;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hlist_node *get_first(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct iter_state *st = seq->private;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct net *net;
|
|
|
|
struct nfnl_queue_net *q;
|
2005-08-10 04:50:45 +02:00
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
net = seq_file_net(seq);
|
|
|
|
q = nfnl_queue_pernet(net);
|
2005-08-10 04:50:45 +02:00
|
|
|
for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
|
2013-03-25 00:50:47 +01:00
|
|
|
if (!hlist_empty(&q->instance_table[st->bucket]))
|
|
|
|
return q->instance_table[st->bucket].first;
|
2005-08-10 04:50:45 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
|
|
|
|
{
|
|
|
|
struct iter_state *st = seq->private;
|
2013-03-25 00:50:47 +01:00
|
|
|
struct net *net = seq_file_net(seq);
|
2005-08-10 04:50:45 +02:00
|
|
|
|
|
|
|
h = h->next;
|
|
|
|
while (!h) {
|
2013-03-25 00:50:47 +01:00
|
|
|
struct nfnl_queue_net *q;
|
|
|
|
|
2005-08-10 04:50:45 +02:00
|
|
|
if (++st->bucket >= INSTANCE_BUCKETS)
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
q = nfnl_queue_pernet(net);
|
|
|
|
h = q->instance_table[st->bucket].first;
|
2005-08-10 04:50:45 +02:00
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
|
|
|
|
{
|
|
|
|
struct hlist_node *head;
|
|
|
|
head = get_first(seq);
|
|
|
|
|
|
|
|
if (head)
|
|
|
|
while (pos && (head = get_next(seq, head)))
|
|
|
|
pos--;
|
|
|
|
return pos ? NULL : head;
|
|
|
|
}
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
static void *seq_start(struct seq_file *s, loff_t *pos)
|
|
|
|
__acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
|
2005-08-10 04:50:45 +02:00
|
|
|
{
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
|
|
|
|
return get_idx(s, *pos);
|
2005-08-10 04:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
(*pos)++;
|
|
|
|
return get_next(s, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void seq_stop(struct seq_file *s, void *v)
|
2013-03-25 00:50:47 +01:00
|
|
|
__releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
|
2005-08-10 04:50:45 +02:00
|
|
|
{
|
2013-03-25 00:50:47 +01:00
|
|
|
spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
|
2005-08-10 04:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int seq_show(struct seq_file *s, void *v)
|
|
|
|
{
|
|
|
|
const struct nfqnl_instance *inst = v;
|
|
|
|
|
2015-04-13 00:52:38 +02:00
|
|
|
seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
|
2014-10-27 22:43:45 +01:00
|
|
|
inst->queue_num,
|
|
|
|
inst->peer_portid, inst->queue_total,
|
|
|
|
inst->copy_mode, inst->copy_range,
|
|
|
|
inst->queue_dropped, inst->queue_user_dropped,
|
|
|
|
inst->id_sequence, 1);
|
2015-05-13 03:28:23 +02:00
|
|
|
return 0;
|
2005-08-10 04:50:45 +02:00
|
|
|
}
|
|
|
|
|
2007-07-11 08:07:31 +02:00
|
|
|
static const struct seq_operations nfqnl_seq_ops = {
|
2005-08-10 04:50:45 +02:00
|
|
|
.start = seq_start,
|
|
|
|
.next = seq_next,
|
|
|
|
.stop = seq_stop,
|
|
|
|
.show = seq_show,
|
|
|
|
};
|
|
|
|
#endif /* PROC_FS */
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
static int __net_init nfnl_queue_net_init(struct net *net)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2013-03-25 00:50:47 +01:00
|
|
|
unsigned int i;
|
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
2007-02-12 20:15:49 +01:00
|
|
|
|
2005-08-10 04:50:45 +02:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++)
|
2013-03-25 00:50:47 +01:00
|
|
|
INIT_HLIST_HEAD(&q->instance_table[i]);
|
|
|
|
|
|
|
|
spin_lock_init(&q->instances_lock);
|
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
2018-04-10 19:42:55 +02:00
|
|
|
if (!proc_create_net("nfnetlink_queue", 0440, net->nf.proc_netfilter,
|
|
|
|
&nfqnl_seq_ops, sizeof(struct iter_state)))
|
2013-03-25 00:50:47 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
#endif
|
2016-05-14 04:18:52 +02:00
|
|
|
nf_register_queue_handler(net, &nfqh);
|
2013-03-25 00:50:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit nfnl_queue_net_exit(struct net *net)
|
|
|
|
{
|
2017-11-12 12:32:37 +01:00
|
|
|
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
|
|
|
|
unsigned int i;
|
|
|
|
|
2016-05-14 04:18:52 +02:00
|
|
|
nf_unregister_queue_handler(net);
|
2013-04-30 10:01:18 +02:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2013-03-25 00:50:47 +01:00
|
|
|
remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
|
2013-04-30 10:01:18 +02:00
|
|
|
#endif
|
2017-11-12 12:32:37 +01:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++)
|
|
|
|
WARN_ON_ONCE(!hlist_empty(&q->instance_table[i]));
|
2013-03-25 00:50:47 +01:00
|
|
|
}
|
|
|
|
|
2016-05-14 04:18:52 +02:00
|
|
|
static void nfnl_queue_net_exit_batch(struct list_head *net_exit_list)
|
|
|
|
{
|
|
|
|
synchronize_rcu();
|
|
|
|
}
|
|
|
|
|
2013-03-25 00:50:47 +01:00
|
|
|
static struct pernet_operations nfnl_queue_net_ops = {
|
2016-05-14 04:18:52 +02:00
|
|
|
.init = nfnl_queue_net_init,
|
|
|
|
.exit = nfnl_queue_net_exit,
|
|
|
|
.exit_batch = nfnl_queue_net_exit_batch,
|
|
|
|
.id = &nfnl_queue_net_id,
|
|
|
|
.size = sizeof(struct nfnl_queue_net),
|
2013-03-25 00:50:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init nfnetlink_queue_init(void)
|
|
|
|
{
|
2015-05-17 23:30:31 +02:00
|
|
|
int status;
|
|
|
|
|
|
|
|
status = register_pernet_subsys(&nfnl_queue_net_ops);
|
|
|
|
if (status < 0) {
|
2018-03-12 14:06:29 +01:00
|
|
|
pr_err("failed to register pernet ops\n");
|
2015-05-17 23:30:31 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2005-08-10 04:50:45 +02:00
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
netlink_register_notifier(&nfqnl_rtnl_notifier);
|
|
|
|
status = nfnetlink_subsys_register(&nfqnl_subsys);
|
|
|
|
if (status < 0) {
|
2018-03-12 14:06:29 +01:00
|
|
|
pr_err("failed to create netlink socket\n");
|
2005-08-10 04:44:15 +02:00
|
|
|
goto cleanup_netlink_notifier;
|
|
|
|
}
|
|
|
|
|
2016-09-09 17:25:09 +02:00
|
|
|
status = register_netdevice_notifier(&nfqnl_dev_notifier);
|
|
|
|
if (status < 0) {
|
2018-03-12 14:06:29 +01:00
|
|
|
pr_err("failed to register netdevice notifier\n");
|
2016-09-09 17:25:09 +02:00
|
|
|
goto cleanup_netlink_subsys;
|
|
|
|
}
|
|
|
|
|
2005-08-10 04:44:15 +02:00
|
|
|
return status;
|
|
|
|
|
2016-09-09 17:25:09 +02:00
|
|
|
cleanup_netlink_subsys:
|
|
|
|
nfnetlink_subsys_unregister(&nfqnl_subsys);
|
2005-08-10 04:44:15 +02:00
|
|
|
cleanup_netlink_notifier:
|
|
|
|
netlink_unregister_notifier(&nfqnl_rtnl_notifier);
|
2015-12-07 11:13:26 +01:00
|
|
|
unregister_pernet_subsys(&nfnl_queue_net_ops);
|
2015-05-17 23:30:31 +02:00
|
|
|
out:
|
2005-08-10 04:44:15 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-03-29 02:37:06 +02:00
|
|
|
static void __exit nfnetlink_queue_fini(void)
|
2005-08-10 04:44:15 +02:00
|
|
|
{
|
2006-04-06 23:11:30 +02:00
|
|
|
unregister_netdevice_notifier(&nfqnl_dev_notifier);
|
|
|
|
nfnetlink_subsys_unregister(&nfqnl_subsys);
|
|
|
|
netlink_unregister_notifier(&nfqnl_rtnl_notifier);
|
2015-05-17 23:30:31 +02:00
|
|
|
unregister_pernet_subsys(&nfnl_queue_net_ops);
|
2009-06-08 05:11:33 +02:00
|
|
|
|
|
|
|
rcu_barrier(); /* Wait for completion of call_rcu()'s */
|
2005-08-10 04:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("netfilter packet queue handler");
|
|
|
|
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
|
|
|
|
|
2006-03-29 02:37:06 +02:00
|
|
|
module_init(nfnetlink_queue_init);
|
|
|
|
module_exit(nfnetlink_queue_fini);
|