2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2006-11-29 01:35:01 +00:00
|
|
|
/* Event cache for netfilter. */
|
|
|
|
|
2013-04-06 13:24:29 +00:00
|
|
|
/*
|
|
|
|
* (C) 2005 Harald Welte <laforge@gnumonks.org>
|
|
|
|
* (C) 2005 Patrick McHardy <kaber@trash.net>
|
|
|
|
* (C) 2005-2006 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
* (C) 2005 USAGI/WIDE Project <http://www.linux-ipv6.org>
|
2006-11-29 01:35:01 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-12 13:06:29 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2006-11-29 01:35:01 +00:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/percpu.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netdevice.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2011-07-15 15:47:34 +00:00
|
|
|
#include <linux/export.h>
|
2006-11-29 01:35:01 +00:00
|
|
|
|
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_core.h>
|
2019-09-13 08:13:05 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_ecache.h>
|
2009-06-13 10:26:29 +00:00
|
|
|
#include <net/netfilter/nf_conntrack_extend.h>
|
2006-11-29 01:35:01 +00:00
|
|
|
|
2009-06-03 08:32:06 +00:00
|
|
|
static DEFINE_MUTEX(nf_ct_ecache_mutex);
|
2006-12-03 06:11:25 +00:00
|
|
|
|
2014-06-10 21:12:56 +00:00
|
|
|
#define ECACHE_RETRY_WAIT (HZ/10)
|
2019-10-13 18:19:45 +00:00
|
|
|
#define ECACHE_STACK_ALLOC (256 / sizeof(void *))
|
2014-06-10 21:12:56 +00:00
|
|
|
|
|
|
|
enum retry_state {
|
|
|
|
STATE_CONGESTED,
|
|
|
|
STATE_RESTART,
|
|
|
|
STATE_DONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
|
|
|
|
{
|
2019-10-13 18:19:45 +00:00
|
|
|
struct nf_conn *refs[ECACHE_STACK_ALLOC];
|
|
|
|
enum retry_state ret = STATE_DONE;
|
2014-06-10 21:12:56 +00:00
|
|
|
struct nf_conntrack_tuple_hash *h;
|
|
|
|
struct hlist_nulls_node *n;
|
|
|
|
unsigned int evicted = 0;
|
|
|
|
|
|
|
|
spin_lock(&pcpu->lock);
|
|
|
|
|
|
|
|
hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
|
|
|
|
struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
|
2016-08-25 13:33:30 +00:00
|
|
|
struct nf_conntrack_ecache *e;
|
2014-06-10 21:12:56 +00:00
|
|
|
|
2016-08-25 13:33:30 +00:00
|
|
|
if (!nf_ct_is_confirmed(ct))
|
|
|
|
continue;
|
|
|
|
|
2019-10-13 18:19:45 +00:00
|
|
|
/* This ecache access is safe because the ct is on the
|
|
|
|
* pcpu dying list and we hold the spinlock -- the entry
|
|
|
|
* cannot be free'd until after the lock is released.
|
|
|
|
*
|
|
|
|
* This is true even if ct has a refcount of 0: the
|
|
|
|
* cpu that is about to free the entry must remove it
|
|
|
|
* from the dying list and needs the lock to do so.
|
|
|
|
*/
|
2016-08-25 13:33:30 +00:00
|
|
|
e = nf_ct_ecache_find(ct);
|
|
|
|
if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL)
|
2014-06-10 21:12:56 +00:00
|
|
|
continue;
|
|
|
|
|
2019-10-13 18:19:45 +00:00
|
|
|
/* ct is in NFCT_ECACHE_DESTROY_FAIL state, this means
|
|
|
|
* the worker owns this entry: the ct will remain valid
|
|
|
|
* until the worker puts its ct reference.
|
|
|
|
*/
|
2014-06-10 21:12:56 +00:00
|
|
|
if (nf_conntrack_event(IPCT_DESTROY, ct)) {
|
|
|
|
ret = STATE_CONGESTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-25 13:33:30 +00:00
|
|
|
e->state = NFCT_ECACHE_DESTROY_SENT;
|
2014-06-10 21:12:56 +00:00
|
|
|
refs[evicted] = ct;
|
|
|
|
|
|
|
|
if (++evicted >= ARRAY_SIZE(refs)) {
|
|
|
|
ret = STATE_RESTART;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&pcpu->lock);
|
|
|
|
|
|
|
|
/* can't _put while holding lock */
|
|
|
|
while (evicted)
|
|
|
|
nf_ct_put(refs[--evicted]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ecache_work(struct work_struct *work)
|
|
|
|
{
|
2021-04-01 14:11:13 +00:00
|
|
|
struct nf_conntrack_net *cnet = container_of(work, struct nf_conntrack_net, ecache_dwork.work);
|
|
|
|
struct netns_ct *ctnet = cnet->ct_net;
|
2014-06-10 21:12:56 +00:00
|
|
|
int cpu, delay = -1;
|
|
|
|
struct ct_pcpu *pcpu;
|
|
|
|
|
|
|
|
local_bh_disable();
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
|
|
enum retry_state ret;
|
|
|
|
|
|
|
|
pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
|
|
|
|
|
|
|
|
ret = ecache_work_evict_list(pcpu);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case STATE_CONGESTED:
|
|
|
|
delay = ECACHE_RETRY_WAIT;
|
|
|
|
goto out;
|
|
|
|
case STATE_RESTART:
|
|
|
|
delay = 0;
|
|
|
|
break;
|
|
|
|
case STATE_DONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
local_bh_enable();
|
|
|
|
|
|
|
|
ctnet->ecache_dwork_pending = delay > 0;
|
|
|
|
if (delay >= 0)
|
2021-04-01 14:11:13 +00:00
|
|
|
schedule_delayed_work(&cnet->ecache_dwork, delay);
|
2014-06-10 21:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 15:16:24 +00:00
|
|
|
static int __nf_conntrack_eventmask_report(struct nf_conntrack_ecache *e,
|
2022-02-05 12:00:04 +00:00
|
|
|
const u32 events,
|
|
|
|
const u32 missed,
|
2021-08-16 15:16:24 +00:00
|
|
|
const struct nf_ct_event *item)
|
2016-04-11 19:52:35 +00:00
|
|
|
{
|
2021-08-16 15:16:24 +00:00
|
|
|
struct net *net = nf_ct_net(item->ct);
|
2016-04-11 19:52:35 +00:00
|
|
|
struct nf_ct_event_notifier *notify;
|
2022-02-05 12:00:04 +00:00
|
|
|
u32 old, want;
|
2021-08-16 15:16:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!((events | missed) & e->ctmask))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
|
|
|
|
notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
|
|
|
|
if (!notify) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-16 15:16:25 +00:00
|
|
|
ret = notify->ct_event(events | missed, item);
|
2021-08-16 15:16:24 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
if (likely(ret >= 0 && missed == 0))
|
|
|
|
return 0;
|
|
|
|
|
2022-02-05 12:00:04 +00:00
|
|
|
do {
|
|
|
|
old = READ_ONCE(e->missed);
|
|
|
|
if (ret < 0)
|
|
|
|
want = old | events;
|
|
|
|
else
|
|
|
|
want = old & ~missed;
|
|
|
|
} while (cmpxchg(&e->missed, old, want) != old);
|
2021-08-16 15:16:24 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nf_conntrack_eventmask_report(unsigned int events, struct nf_conn *ct,
|
|
|
|
u32 portid, int report)
|
|
|
|
{
|
2016-04-11 19:52:35 +00:00
|
|
|
struct nf_conntrack_ecache *e;
|
2021-08-16 15:16:22 +00:00
|
|
|
struct nf_ct_event item;
|
2022-02-05 12:00:04 +00:00
|
|
|
unsigned int missed;
|
2021-08-16 15:16:24 +00:00
|
|
|
int ret;
|
2021-08-16 15:16:22 +00:00
|
|
|
|
|
|
|
if (!nf_ct_is_confirmed(ct))
|
2021-08-16 15:16:24 +00:00
|
|
|
return 0;
|
2016-04-11 19:52:35 +00:00
|
|
|
|
|
|
|
e = nf_ct_ecache_find(ct);
|
|
|
|
if (!e)
|
2021-08-16 15:16:24 +00:00
|
|
|
return 0;
|
2016-04-11 19:52:35 +00:00
|
|
|
|
2021-08-16 15:16:22 +00:00
|
|
|
memset(&item, 0, sizeof(item));
|
|
|
|
|
|
|
|
item.ct = ct;
|
|
|
|
item.portid = e->portid ? e->portid : portid;
|
|
|
|
item.report = report;
|
|
|
|
|
|
|
|
/* This is a resent of a destroy event? If so, skip missed */
|
|
|
|
missed = e->portid ? 0 : e->missed;
|
|
|
|
|
2021-08-16 15:16:24 +00:00
|
|
|
ret = __nf_conntrack_eventmask_report(e, events, missed, &item);
|
|
|
|
if (unlikely(ret < 0 && (events & (1 << IPCT_DESTROY)))) {
|
|
|
|
/* This is a destroy event that has been triggered by a process,
|
|
|
|
* we store the PORTID to include it in the retransmission.
|
2021-08-16 15:16:23 +00:00
|
|
|
*/
|
2021-08-16 15:16:24 +00:00
|
|
|
if (e->portid == 0 && portid != 0)
|
|
|
|
e->portid = portid;
|
|
|
|
e->state = NFCT_ECACHE_DESTROY_FAIL;
|
2016-04-11 19:52:35 +00:00
|
|
|
}
|
2021-08-16 15:16:23 +00:00
|
|
|
|
2016-04-11 19:52:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
|
|
|
|
|
2006-11-29 01:35:01 +00:00
|
|
|
/* deliver cached events and clear cache entry - must be called with locally
|
|
|
|
* disabled softirqs */
|
2009-06-13 10:26:29 +00:00
|
|
|
void nf_ct_deliver_cached_events(struct nf_conn *ct)
|
2006-11-29 01:35:01 +00:00
|
|
|
{
|
2009-06-13 10:26:29 +00:00
|
|
|
struct nf_conntrack_ecache *e;
|
2012-02-22 06:48:01 +00:00
|
|
|
struct nf_ct_event item;
|
2022-02-05 12:00:04 +00:00
|
|
|
unsigned int events;
|
2009-06-03 08:32:06 +00:00
|
|
|
|
2019-10-22 16:56:42 +00:00
|
|
|
if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
|
2021-08-16 15:16:24 +00:00
|
|
|
return;
|
2019-10-22 16:56:42 +00:00
|
|
|
|
2009-06-13 10:26:29 +00:00
|
|
|
e = nf_ct_ecache_find(ct);
|
|
|
|
if (e == NULL)
|
2021-08-16 15:16:24 +00:00
|
|
|
return;
|
2009-06-13 10:26:29 +00:00
|
|
|
|
|
|
|
events = xchg(&e->cache, 0);
|
|
|
|
|
2012-02-22 06:48:01 +00:00
|
|
|
item.ct = ct;
|
2012-09-07 20:12:54 +00:00
|
|
|
item.portid = 0;
|
2012-02-22 06:48:01 +00:00
|
|
|
item.report = 0;
|
|
|
|
|
2021-08-16 15:16:24 +00:00
|
|
|
/* We make a copy of the missed event cache without taking
|
|
|
|
* the lock, thus we may send missed events twice. However,
|
|
|
|
* this does not harm and it happens very rarely.
|
|
|
|
*/
|
|
|
|
__nf_conntrack_eventmask_report(e, events, e->missed, &item);
|
2006-11-29 01:35:01 +00:00
|
|
|
}
|
2006-12-03 06:11:25 +00:00
|
|
|
EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
|
2006-11-29 01:35:01 +00:00
|
|
|
|
2016-04-11 19:52:36 +00:00
|
|
|
void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
|
|
|
|
struct nf_conntrack_expect *exp,
|
|
|
|
u32 portid, int report)
|
|
|
|
|
|
|
|
{
|
|
|
|
struct net *net = nf_ct_exp_net(exp);
|
2021-08-16 15:16:26 +00:00
|
|
|
struct nf_ct_event_notifier *notify;
|
2016-04-11 19:52:36 +00:00
|
|
|
struct nf_conntrack_ecache *e;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2021-08-16 15:16:26 +00:00
|
|
|
notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
|
2016-04-11 19:52:36 +00:00
|
|
|
if (!notify)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
e = nf_ct_ecache_find(exp->master);
|
|
|
|
if (!e)
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
if (e->expmask & (1 << event)) {
|
|
|
|
struct nf_exp_event item = {
|
|
|
|
.exp = exp,
|
|
|
|
.portid = portid,
|
|
|
|
.report = report
|
|
|
|
};
|
2021-08-16 15:16:25 +00:00
|
|
|
notify->exp_event(1 << event, &item);
|
2016-04-11 19:52:36 +00:00
|
|
|
}
|
|
|
|
out_unlock:
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2021-08-16 15:16:25 +00:00
|
|
|
void nf_conntrack_register_notifier(struct net *net,
|
|
|
|
const struct nf_ct_event_notifier *new)
|
2007-03-14 23:40:10 +00:00
|
|
|
{
|
2010-05-10 16:47:57 +00:00
|
|
|
struct nf_ct_event_notifier *notify;
|
2009-06-03 08:32:06 +00:00
|
|
|
|
|
|
|
mutex_lock(&nf_ct_ecache_mutex);
|
2011-11-21 23:16:51 +00:00
|
|
|
notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
|
2010-05-10 16:47:57 +00:00
|
|
|
lockdep_is_held(&nf_ct_ecache_mutex));
|
2021-08-16 15:16:25 +00:00
|
|
|
WARN_ON_ONCE(notify);
|
2012-01-12 04:41:32 +00:00
|
|
|
rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
|
2009-06-03 08:32:06 +00:00
|
|
|
mutex_unlock(&nf_ct_ecache_mutex);
|
2007-03-14 23:40:10 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
|
|
|
|
|
2021-08-16 15:16:25 +00:00
|
|
|
void nf_conntrack_unregister_notifier(struct net *net)
|
2007-03-14 23:40:10 +00:00
|
|
|
{
|
2009-06-03 08:32:06 +00:00
|
|
|
mutex_lock(&nf_ct_ecache_mutex);
|
2011-11-21 23:16:51 +00:00
|
|
|
RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
|
2009-06-03 08:32:06 +00:00
|
|
|
mutex_unlock(&nf_ct_ecache_mutex);
|
2021-08-16 15:16:26 +00:00
|
|
|
/* synchronize_rcu() is called after netns pre_exit */
|
2007-03-14 23:40:10 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
|
|
|
|
|
2021-04-01 14:11:13 +00:00
|
|
|
void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state)
|
|
|
|
{
|
2021-06-02 10:39:07 +00:00
|
|
|
struct nf_conntrack_net *cnet = nf_ct_pernet(net);
|
2021-04-01 14:11:13 +00:00
|
|
|
|
|
|
|
if (state == NFCT_ECACHE_DESTROY_FAIL &&
|
|
|
|
!delayed_work_pending(&cnet->ecache_dwork)) {
|
|
|
|
schedule_delayed_work(&cnet->ecache_dwork, HZ);
|
|
|
|
net->ct.ecache_dwork_pending = true;
|
|
|
|
} else if (state == NFCT_ECACHE_DESTROY_SENT) {
|
|
|
|
net->ct.ecache_dwork_pending = false;
|
|
|
|
mod_delayed_work(system_wq, &cnet->ecache_dwork, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-13 10:26:29 +00:00
|
|
|
#define NF_CT_EVENTS_DEFAULT 1
|
|
|
|
static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
|
|
|
|
|
2018-12-18 22:04:49 +00:00
|
|
|
void nf_conntrack_ecache_pernet_init(struct net *net)
|
2009-06-13 10:26:29 +00:00
|
|
|
{
|
2021-06-02 10:39:07 +00:00
|
|
|
struct nf_conntrack_net *cnet = nf_ct_pernet(net);
|
2021-04-01 14:11:13 +00:00
|
|
|
|
2009-06-13 10:26:29 +00:00
|
|
|
net->ct.sysctl_events = nf_ct_events;
|
2021-04-01 14:11:13 +00:00
|
|
|
cnet->ct_net = &net->ct;
|
|
|
|
INIT_DELAYED_WORK(&cnet->ecache_dwork, ecache_work);
|
2022-01-20 12:07:02 +00:00
|
|
|
|
2022-02-05 12:00:04 +00:00
|
|
|
BUILD_BUG_ON(__IPCT_MAX >= 16); /* e->ctmask is u16 */
|
2013-01-21 22:10:28 +00:00
|
|
|
}
|
2009-06-13 10:26:29 +00:00
|
|
|
|
2013-01-21 22:10:28 +00:00
|
|
|
void nf_conntrack_ecache_pernet_fini(struct net *net)
|
|
|
|
{
|
2021-06-02 10:39:07 +00:00
|
|
|
struct nf_conntrack_net *cnet = nf_ct_pernet(net);
|
2021-04-01 14:11:13 +00:00
|
|
|
|
|
|
|
cancel_delayed_work_sync(&cnet->ecache_dwork);
|
2013-01-21 22:10:28 +00:00
|
|
|
}
|