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 static DEFINE_MUTEX(nf_ct_ecache_mutex);
32 #define ECACHE_RETRY_WAIT (HZ/10)
33 #define ECACHE_STACK_ALLOC (256 / sizeof(void *))
41 static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
43 struct nf_conn *refs[ECACHE_STACK_ALLOC];
44 enum retry_state ret = STATE_DONE;
45 struct nf_conntrack_tuple_hash *h;
46 struct hlist_nulls_node *n;
47 unsigned int evicted = 0;
49 spin_lock(&pcpu->lock);
51 hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
52 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
53 struct nf_conntrack_ecache *e;
55 if (!nf_ct_is_confirmed(ct))
58 /* This ecache access is safe because the ct is on the
59 * pcpu dying list and we hold the spinlock -- the entry
60 * cannot be free'd until after the lock is released.
62 * This is true even if ct has a refcount of 0: the
63 * cpu that is about to free the entry must remove it
64 * from the dying list and needs the lock to do so.
66 e = nf_ct_ecache_find(ct);
67 if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL)
70 /* ct is in NFCT_ECACHE_DESTROY_FAIL state, this means
71 * the worker owns this entry: the ct will remain valid
72 * until the worker puts its ct reference.
74 if (nf_conntrack_event(IPCT_DESTROY, ct)) {
75 ret = STATE_CONGESTED;
79 e->state = NFCT_ECACHE_DESTROY_SENT;
82 if (++evicted >= ARRAY_SIZE(refs)) {
88 spin_unlock(&pcpu->lock);
90 /* can't _put while holding lock */
92 nf_ct_put(refs[--evicted]);
97 static void ecache_work(struct work_struct *work)
99 struct nf_conntrack_net *cnet = container_of(work, struct nf_conntrack_net, ecache_dwork.work);
100 struct netns_ct *ctnet = cnet->ct_net;
102 struct ct_pcpu *pcpu;
106 for_each_possible_cpu(cpu) {
107 enum retry_state ret;
109 pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
111 ret = ecache_work_evict_list(pcpu);
114 case STATE_CONGESTED:
115 delay = ECACHE_RETRY_WAIT;
128 ctnet->ecache_dwork_pending = delay > 0;
130 schedule_delayed_work(&cnet->ecache_dwork, delay);
133 static int __nf_conntrack_eventmask_report(struct nf_conntrack_ecache *e,
134 const unsigned int events,
135 const unsigned long missed,
136 const struct nf_ct_event *item)
138 struct nf_conn *ct = item->ct;
139 struct net *net = nf_ct_net(item->ct);
140 struct nf_ct_event_notifier *notify;
143 if (!((events | missed) & e->ctmask))
148 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
154 ret = notify->ct_event(events | missed, item);
157 if (likely(ret >= 0 && missed == 0))
160 spin_lock_bh(&ct->lock);
164 e->missed &= ~missed;
165 spin_unlock_bh(&ct->lock);
170 int nf_conntrack_eventmask_report(unsigned int events, struct nf_conn *ct,
171 u32 portid, int report)
173 struct nf_conntrack_ecache *e;
174 struct nf_ct_event item;
175 unsigned long missed;
178 if (!nf_ct_is_confirmed(ct))
181 e = nf_ct_ecache_find(ct);
185 memset(&item, 0, sizeof(item));
188 item.portid = e->portid ? e->portid : portid;
189 item.report = report;
191 /* This is a resent of a destroy event? If so, skip missed */
192 missed = e->portid ? 0 : e->missed;
194 ret = __nf_conntrack_eventmask_report(e, events, missed, &item);
195 if (unlikely(ret < 0 && (events & (1 << IPCT_DESTROY)))) {
196 /* This is a destroy event that has been triggered by a process,
197 * we store the PORTID to include it in the retransmission.
199 if (e->portid == 0 && portid != 0)
201 e->state = NFCT_ECACHE_DESTROY_FAIL;
206 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
208 /* deliver cached events and clear cache entry - must be called with locally
209 * disabled softirqs */
210 void nf_ct_deliver_cached_events(struct nf_conn *ct)
212 struct nf_conntrack_ecache *e;
213 struct nf_ct_event item;
214 unsigned long events;
216 if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
219 e = nf_ct_ecache_find(ct);
223 events = xchg(&e->cache, 0);
229 /* We make a copy of the missed event cache without taking
230 * the lock, thus we may send missed events twice. However,
231 * this does not harm and it happens very rarely.
233 __nf_conntrack_eventmask_report(e, events, e->missed, &item);
235 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
237 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
238 struct nf_conntrack_expect *exp,
239 u32 portid, int report)
242 struct net *net = nf_ct_exp_net(exp);
243 struct nf_ct_event_notifier *notify;
244 struct nf_conntrack_ecache *e;
247 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
251 e = nf_ct_ecache_find(exp->master);
255 if (e->expmask & (1 << event)) {
256 struct nf_exp_event item = {
261 notify->exp_event(1 << event, &item);
267 void nf_conntrack_register_notifier(struct net *net,
268 const struct nf_ct_event_notifier *new)
270 struct nf_ct_event_notifier *notify;
272 mutex_lock(&nf_ct_ecache_mutex);
273 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
274 lockdep_is_held(&nf_ct_ecache_mutex));
275 WARN_ON_ONCE(notify);
276 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
277 mutex_unlock(&nf_ct_ecache_mutex);
279 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
281 void nf_conntrack_unregister_notifier(struct net *net)
283 mutex_lock(&nf_ct_ecache_mutex);
284 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
285 mutex_unlock(&nf_ct_ecache_mutex);
286 /* synchronize_rcu() is called after netns pre_exit */
288 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
290 void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state)
292 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
294 if (state == NFCT_ECACHE_DESTROY_FAIL &&
295 !delayed_work_pending(&cnet->ecache_dwork)) {
296 schedule_delayed_work(&cnet->ecache_dwork, HZ);
297 net->ct.ecache_dwork_pending = true;
298 } else if (state == NFCT_ECACHE_DESTROY_SENT) {
299 net->ct.ecache_dwork_pending = false;
300 mod_delayed_work(system_wq, &cnet->ecache_dwork, 0);
304 #define NF_CT_EVENTS_DEFAULT 1
305 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
307 static const struct nf_ct_ext_type event_extend = {
308 .len = sizeof(struct nf_conntrack_ecache),
309 .align = __alignof__(struct nf_conntrack_ecache),
310 .id = NF_CT_EXT_ECACHE,
313 void nf_conntrack_ecache_pernet_init(struct net *net)
315 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
317 net->ct.sysctl_events = nf_ct_events;
318 cnet->ct_net = &net->ct;
319 INIT_DELAYED_WORK(&cnet->ecache_dwork, ecache_work);
322 void nf_conntrack_ecache_pernet_fini(struct net *net)
324 struct nf_conntrack_net *cnet = nf_ct_pernet(net);
326 cancel_delayed_work_sync(&cnet->ecache_dwork);
329 int nf_conntrack_ecache_init(void)
331 int ret = nf_ct_extend_register(&event_extend);
333 pr_err("Unable to register event extension\n");
335 BUILD_BUG_ON(__IPCT_MAX >= 16); /* ctmask, missed use u16 */
340 void nf_conntrack_ecache_fini(void)
342 nf_ct_extend_unregister(&event_extend);