1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Event cache for netfilter. */
5 * (C) 2005 Harald Welte <laforge@gnumonks.org>
6 * (C) 2005 Patrick McHardy <kaber@trash.net>
7 * (C) 2005-2006 Netfilter Core Team <coreteam@netfilter.org>
8 * (C) 2005 USAGI/WIDE Project <http://www.linux-ipv6.org>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/types.h>
14 #include <linux/netfilter.h>
15 #include <linux/skbuff.h>
16 #include <linux/vmalloc.h>
17 #include <linux/stddef.h>
18 #include <linux/err.h>
19 #include <linux/percpu.h>
20 #include <linux/kernel.h>
21 #include <linux/netdevice.h>
22 #include <linux/slab.h>
23 #include <linux/export.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27 #include <net/netfilter/nf_conntrack_ecache.h>
28 #include <net/netfilter/nf_conntrack_extend.h>
30 extern unsigned int nf_conntrack_net_id;
32 static DEFINE_MUTEX(nf_ct_ecache_mutex);
34 #define ECACHE_RETRY_WAIT (HZ/10)
35 #define ECACHE_STACK_ALLOC (256 / sizeof(void *))
43 static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
45 struct nf_conn *refs[ECACHE_STACK_ALLOC];
46 enum retry_state ret = STATE_DONE;
47 struct nf_conntrack_tuple_hash *h;
48 struct hlist_nulls_node *n;
49 unsigned int evicted = 0;
51 spin_lock(&pcpu->lock);
53 hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
54 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
55 struct nf_conntrack_ecache *e;
57 if (!nf_ct_is_confirmed(ct))
60 /* This ecache access is safe because the ct is on the
61 * pcpu dying list and we hold the spinlock -- the entry
62 * cannot be free'd until after the lock is released.
64 * This is true even if ct has a refcount of 0: the
65 * cpu that is about to free the entry must remove it
66 * from the dying list and needs the lock to do so.
68 e = nf_ct_ecache_find(ct);
69 if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL)
72 /* ct is in NFCT_ECACHE_DESTROY_FAIL state, this means
73 * the worker owns this entry: the ct will remain valid
74 * until the worker puts its ct reference.
76 if (nf_conntrack_event(IPCT_DESTROY, ct)) {
77 ret = STATE_CONGESTED;
81 e->state = NFCT_ECACHE_DESTROY_SENT;
84 if (++evicted >= ARRAY_SIZE(refs)) {
90 spin_unlock(&pcpu->lock);
92 /* can't _put while holding lock */
94 nf_ct_put(refs[--evicted]);
99 static void ecache_work(struct work_struct *work)
101 struct nf_conntrack_net *cnet = container_of(work, struct nf_conntrack_net, ecache_dwork.work);
102 struct netns_ct *ctnet = cnet->ct_net;
104 struct ct_pcpu *pcpu;
108 for_each_possible_cpu(cpu) {
109 enum retry_state ret;
111 pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
113 ret = ecache_work_evict_list(pcpu);
116 case STATE_CONGESTED:
117 delay = ECACHE_RETRY_WAIT;
130 ctnet->ecache_dwork_pending = delay > 0;
132 schedule_delayed_work(&cnet->ecache_dwork, delay);
135 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
136 u32 portid, int report)
139 struct net *net = nf_ct_net(ct);
140 struct nf_ct_event_notifier *notify;
141 struct nf_conntrack_ecache *e;
144 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
148 e = nf_ct_ecache_find(ct);
152 if (nf_ct_is_confirmed(ct)) {
153 struct nf_ct_event item = {
155 .portid = e->portid ? e->portid : portid,
158 /* This is a resent of a destroy event? If so, skip missed */
159 unsigned long missed = e->portid ? 0 : e->missed;
161 if (!((eventmask | missed) & e->ctmask))
164 ret = notify->fcn(eventmask | missed, &item);
165 if (unlikely(ret < 0 || missed)) {
166 spin_lock_bh(&ct->lock);
168 /* This is a destroy event that has been
169 * triggered by a process, we store the PORTID
170 * to include it in the retransmission.
172 if (eventmask & (1 << IPCT_DESTROY)) {
173 if (e->portid == 0 && portid != 0)
175 e->state = NFCT_ECACHE_DESTROY_FAIL;
177 e->missed |= eventmask;
180 e->missed &= ~missed;
182 spin_unlock_bh(&ct->lock);
189 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
191 /* deliver cached events and clear cache entry - must be called with locally
192 * disabled softirqs */
193 void nf_ct_deliver_cached_events(struct nf_conn *ct)
195 struct net *net = nf_ct_net(ct);
196 unsigned long events, missed;
197 struct nf_ct_event_notifier *notify;
198 struct nf_conntrack_ecache *e;
199 struct nf_ct_event item;
203 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
207 if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
210 e = nf_ct_ecache_find(ct);
214 events = xchg(&e->cache, 0);
216 /* We make a copy of the missed event cache without taking
217 * the lock, thus we may send missed events twice. However,
218 * this does not harm and it happens very rarely. */
221 if (!((events | missed) & e->ctmask))
228 ret = notify->fcn(events | missed, &item);
230 if (likely(ret == 0 && !missed))
233 spin_lock_bh(&ct->lock);
237 e->missed &= ~missed;
238 spin_unlock_bh(&ct->lock);
243 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
245 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
246 struct nf_conntrack_expect *exp,
247 u32 portid, int report)
250 struct net *net = nf_ct_exp_net(exp);
251 struct nf_exp_event_notifier *notify;
252 struct nf_conntrack_ecache *e;
255 notify = rcu_dereference(net->ct.nf_expect_event_cb);
259 e = nf_ct_ecache_find(exp->master);
263 if (e->expmask & (1 << event)) {
264 struct nf_exp_event item = {
269 notify->fcn(1 << event, &item);
275 int nf_conntrack_register_notifier(struct net *net,
276 struct nf_ct_event_notifier *new)
279 struct nf_ct_event_notifier *notify;
281 mutex_lock(&nf_ct_ecache_mutex);
282 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
283 lockdep_is_held(&nf_ct_ecache_mutex));
284 if (notify != NULL) {
288 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
292 mutex_unlock(&nf_ct_ecache_mutex);
295 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
297 void nf_conntrack_unregister_notifier(struct net *net,
298 struct nf_ct_event_notifier *new)
300 struct nf_ct_event_notifier *notify;
302 mutex_lock(&nf_ct_ecache_mutex);
303 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
304 lockdep_is_held(&nf_ct_ecache_mutex));
305 BUG_ON(notify != new);
306 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
307 mutex_unlock(&nf_ct_ecache_mutex);
308 /* synchronize_rcu() is called from ctnetlink_exit. */
310 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
312 int nf_ct_expect_register_notifier(struct net *net,
313 struct nf_exp_event_notifier *new)
316 struct nf_exp_event_notifier *notify;
318 mutex_lock(&nf_ct_ecache_mutex);
319 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
320 lockdep_is_held(&nf_ct_ecache_mutex));
321 if (notify != NULL) {
325 rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
329 mutex_unlock(&nf_ct_ecache_mutex);
332 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
334 void nf_ct_expect_unregister_notifier(struct net *net,
335 struct nf_exp_event_notifier *new)
337 struct nf_exp_event_notifier *notify;
339 mutex_lock(&nf_ct_ecache_mutex);
340 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
341 lockdep_is_held(&nf_ct_ecache_mutex));
342 BUG_ON(notify != new);
343 RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
344 mutex_unlock(&nf_ct_ecache_mutex);
345 /* synchronize_rcu() is called from ctnetlink_exit. */
347 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
349 void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state)
351 struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
353 if (state == NFCT_ECACHE_DESTROY_FAIL &&
354 !delayed_work_pending(&cnet->ecache_dwork)) {
355 schedule_delayed_work(&cnet->ecache_dwork, HZ);
356 net->ct.ecache_dwork_pending = true;
357 } else if (state == NFCT_ECACHE_DESTROY_SENT) {
358 net->ct.ecache_dwork_pending = false;
359 mod_delayed_work(system_wq, &cnet->ecache_dwork, 0);
363 #define NF_CT_EVENTS_DEFAULT 1
364 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
366 static const struct nf_ct_ext_type event_extend = {
367 .len = sizeof(struct nf_conntrack_ecache),
368 .align = __alignof__(struct nf_conntrack_ecache),
369 .id = NF_CT_EXT_ECACHE,
372 void nf_conntrack_ecache_pernet_init(struct net *net)
374 struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
376 net->ct.sysctl_events = nf_ct_events;
377 cnet->ct_net = &net->ct;
378 INIT_DELAYED_WORK(&cnet->ecache_dwork, ecache_work);
381 void nf_conntrack_ecache_pernet_fini(struct net *net)
383 struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
385 cancel_delayed_work_sync(&cnet->ecache_dwork);
388 int nf_conntrack_ecache_init(void)
390 int ret = nf_ct_extend_register(&event_extend);
392 pr_err("Unable to register event extension\n");
394 BUILD_BUG_ON(__IPCT_MAX >= 16); /* ctmask, missed use u16 */
399 void nf_conntrack_ecache_fini(void)
401 nf_ct_extend_unregister(&event_extend);