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_extend.h>
29 static DEFINE_MUTEX(nf_ct_ecache_mutex);
31 #define ECACHE_RETRY_WAIT (HZ/10)
39 static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
41 struct nf_conn *refs[16];
42 struct nf_conntrack_tuple_hash *h;
43 struct hlist_nulls_node *n;
44 unsigned int evicted = 0;
45 enum retry_state ret = STATE_DONE;
47 spin_lock(&pcpu->lock);
49 hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
50 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
51 struct nf_conntrack_ecache *e;
53 if (!nf_ct_is_confirmed(ct))
56 e = nf_ct_ecache_find(ct);
57 if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL)
60 if (nf_conntrack_event(IPCT_DESTROY, ct)) {
61 ret = STATE_CONGESTED;
65 e->state = NFCT_ECACHE_DESTROY_SENT;
68 if (++evicted >= ARRAY_SIZE(refs)) {
74 spin_unlock(&pcpu->lock);
76 /* can't _put while holding lock */
78 nf_ct_put(refs[--evicted]);
83 static void ecache_work(struct work_struct *work)
85 struct netns_ct *ctnet =
86 container_of(work, struct netns_ct, ecache_dwork.work);
92 for_each_possible_cpu(cpu) {
95 pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
97 ret = ecache_work_evict_list(pcpu);
100 case STATE_CONGESTED:
101 delay = ECACHE_RETRY_WAIT;
114 ctnet->ecache_dwork_pending = delay > 0;
116 schedule_delayed_work(&ctnet->ecache_dwork, delay);
119 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
120 u32 portid, int report)
123 struct net *net = nf_ct_net(ct);
124 struct nf_ct_event_notifier *notify;
125 struct nf_conntrack_ecache *e;
128 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
132 e = nf_ct_ecache_find(ct);
136 if (nf_ct_is_confirmed(ct)) {
137 struct nf_ct_event item = {
139 .portid = e->portid ? e->portid : portid,
142 /* This is a resent of a destroy event? If so, skip missed */
143 unsigned long missed = e->portid ? 0 : e->missed;
145 if (!((eventmask | missed) & e->ctmask))
148 ret = notify->fcn(eventmask | missed, &item);
149 if (unlikely(ret < 0 || missed)) {
150 spin_lock_bh(&ct->lock);
152 /* This is a destroy event that has been
153 * triggered by a process, we store the PORTID
154 * to include it in the retransmission.
156 if (eventmask & (1 << IPCT_DESTROY)) {
157 if (e->portid == 0 && portid != 0)
159 e->state = NFCT_ECACHE_DESTROY_FAIL;
161 e->missed |= eventmask;
164 e->missed &= ~missed;
166 spin_unlock_bh(&ct->lock);
173 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
175 /* deliver cached events and clear cache entry - must be called with locally
176 * disabled softirqs */
177 void nf_ct_deliver_cached_events(struct nf_conn *ct)
179 struct net *net = nf_ct_net(ct);
180 unsigned long events, missed;
181 struct nf_ct_event_notifier *notify;
182 struct nf_conntrack_ecache *e;
183 struct nf_ct_event item;
187 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
191 e = nf_ct_ecache_find(ct);
195 events = xchg(&e->cache, 0);
197 if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
200 /* We make a copy of the missed event cache without taking
201 * the lock, thus we may send missed events twice. However,
202 * this does not harm and it happens very rarely. */
205 if (!((events | missed) & e->ctmask))
212 ret = notify->fcn(events | missed, &item);
214 if (likely(ret == 0 && !missed))
217 spin_lock_bh(&ct->lock);
221 e->missed &= ~missed;
222 spin_unlock_bh(&ct->lock);
227 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
229 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
230 struct nf_conntrack_expect *exp,
231 u32 portid, int report)
234 struct net *net = nf_ct_exp_net(exp);
235 struct nf_exp_event_notifier *notify;
236 struct nf_conntrack_ecache *e;
239 notify = rcu_dereference(net->ct.nf_expect_event_cb);
243 e = nf_ct_ecache_find(exp->master);
247 if (e->expmask & (1 << event)) {
248 struct nf_exp_event item = {
253 notify->fcn(1 << event, &item);
259 int nf_conntrack_register_notifier(struct net *net,
260 struct nf_ct_event_notifier *new)
263 struct nf_ct_event_notifier *notify;
265 mutex_lock(&nf_ct_ecache_mutex);
266 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
267 lockdep_is_held(&nf_ct_ecache_mutex));
268 if (notify != NULL) {
272 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
276 mutex_unlock(&nf_ct_ecache_mutex);
279 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
281 void nf_conntrack_unregister_notifier(struct net *net,
282 struct nf_ct_event_notifier *new)
284 struct nf_ct_event_notifier *notify;
286 mutex_lock(&nf_ct_ecache_mutex);
287 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
288 lockdep_is_held(&nf_ct_ecache_mutex));
289 BUG_ON(notify != new);
290 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
291 mutex_unlock(&nf_ct_ecache_mutex);
292 /* synchronize_rcu() is called from ctnetlink_exit. */
294 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
296 int nf_ct_expect_register_notifier(struct net *net,
297 struct nf_exp_event_notifier *new)
300 struct nf_exp_event_notifier *notify;
302 mutex_lock(&nf_ct_ecache_mutex);
303 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
304 lockdep_is_held(&nf_ct_ecache_mutex));
305 if (notify != NULL) {
309 rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
313 mutex_unlock(&nf_ct_ecache_mutex);
316 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
318 void nf_ct_expect_unregister_notifier(struct net *net,
319 struct nf_exp_event_notifier *new)
321 struct nf_exp_event_notifier *notify;
323 mutex_lock(&nf_ct_ecache_mutex);
324 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
325 lockdep_is_held(&nf_ct_ecache_mutex));
326 BUG_ON(notify != new);
327 RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
328 mutex_unlock(&nf_ct_ecache_mutex);
329 /* synchronize_rcu() is called from ctnetlink_exit. */
331 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
333 #define NF_CT_EVENTS_DEFAULT 1
334 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
336 static const struct nf_ct_ext_type event_extend = {
337 .len = sizeof(struct nf_conntrack_ecache),
338 .align = __alignof__(struct nf_conntrack_ecache),
339 .id = NF_CT_EXT_ECACHE,
342 void nf_conntrack_ecache_pernet_init(struct net *net)
344 net->ct.sysctl_events = nf_ct_events;
345 INIT_DELAYED_WORK(&net->ct.ecache_dwork, ecache_work);
348 void nf_conntrack_ecache_pernet_fini(struct net *net)
350 cancel_delayed_work_sync(&net->ct.ecache_dwork);
353 int nf_conntrack_ecache_init(void)
355 int ret = nf_ct_extend_register(&event_extend);
357 pr_err("Unable to register event extension\n");
359 BUILD_BUG_ON(__IPCT_MAX >= 16); /* ctmask, missed use u16 */
364 void nf_conntrack_ecache_fini(void)
366 nf_ct_extend_unregister(&event_extend);