1 /* Event cache for netfilter. */
4 * (C) 2005 Harald Welte <laforge@gnumonks.org>
5 * (C) 2005 Patrick McHardy <kaber@trash.net>
6 * (C) 2005-2006 Netfilter Core Team <coreteam@netfilter.org>
7 * (C) 2005 USAGI/WIDE Project <http://www.linux-ipv6.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/types.h>
17 #include <linux/netfilter.h>
18 #include <linux/skbuff.h>
19 #include <linux/vmalloc.h>
20 #include <linux/stddef.h>
21 #include <linux/err.h>
22 #include <linux/percpu.h>
23 #include <linux/kernel.h>
24 #include <linux/netdevice.h>
25 #include <linux/slab.h>
26 #include <linux/export.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_extend.h>
32 static DEFINE_MUTEX(nf_ct_ecache_mutex);
34 #define ECACHE_RETRY_WAIT (HZ/10)
42 static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu)
44 struct nf_conn *refs[16];
45 struct nf_conntrack_tuple_hash *h;
46 struct hlist_nulls_node *n;
47 unsigned int evicted = 0;
48 enum retry_state ret = STATE_DONE;
50 spin_lock(&pcpu->lock);
52 hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) {
53 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
54 struct nf_conntrack_ecache *e;
56 if (!nf_ct_is_confirmed(ct))
59 e = nf_ct_ecache_find(ct);
60 if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL)
63 if (nf_conntrack_event(IPCT_DESTROY, ct)) {
64 ret = STATE_CONGESTED;
68 e->state = NFCT_ECACHE_DESTROY_SENT;
71 if (++evicted >= ARRAY_SIZE(refs)) {
77 spin_unlock(&pcpu->lock);
79 /* can't _put while holding lock */
81 nf_ct_put(refs[--evicted]);
86 static void ecache_work(struct work_struct *work)
88 struct netns_ct *ctnet =
89 container_of(work, struct netns_ct, ecache_dwork.work);
95 for_each_possible_cpu(cpu) {
98 pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu);
100 ret = ecache_work_evict_list(pcpu);
103 case STATE_CONGESTED:
104 delay = ECACHE_RETRY_WAIT;
117 ctnet->ecache_dwork_pending = delay > 0;
119 schedule_delayed_work(&ctnet->ecache_dwork, delay);
122 int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct,
123 u32 portid, int report)
126 struct net *net = nf_ct_net(ct);
127 struct nf_ct_event_notifier *notify;
128 struct nf_conntrack_ecache *e;
131 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
135 e = nf_ct_ecache_find(ct);
139 if (nf_ct_is_confirmed(ct)) {
140 struct nf_ct_event item = {
142 .portid = e->portid ? e->portid : portid,
145 /* This is a resent of a destroy event? If so, skip missed */
146 unsigned long missed = e->portid ? 0 : e->missed;
148 if (!((eventmask | missed) & e->ctmask))
151 ret = notify->fcn(eventmask | missed, &item);
152 if (unlikely(ret < 0 || missed)) {
153 spin_lock_bh(&ct->lock);
155 /* This is a destroy event that has been
156 * triggered by a process, we store the PORTID
157 * to include it in the retransmission.
159 if (eventmask & (1 << IPCT_DESTROY)) {
160 if (e->portid == 0 && portid != 0)
162 e->state = NFCT_ECACHE_DESTROY_FAIL;
164 e->missed |= eventmask;
167 e->missed &= ~missed;
169 spin_unlock_bh(&ct->lock);
176 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report);
178 /* deliver cached events and clear cache entry - must be called with locally
179 * disabled softirqs */
180 void nf_ct_deliver_cached_events(struct nf_conn *ct)
182 struct net *net = nf_ct_net(ct);
183 unsigned long events, missed;
184 struct nf_ct_event_notifier *notify;
185 struct nf_conntrack_ecache *e;
186 struct nf_ct_event item;
190 notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
194 e = nf_ct_ecache_find(ct);
198 events = xchg(&e->cache, 0);
200 if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct))
203 /* We make a copy of the missed event cache without taking
204 * the lock, thus we may send missed events twice. However,
205 * this does not harm and it happens very rarely. */
208 if (!((events | missed) & e->ctmask))
215 ret = notify->fcn(events | missed, &item);
217 if (likely(ret == 0 && !missed))
220 spin_lock_bh(&ct->lock);
224 e->missed &= ~missed;
225 spin_unlock_bh(&ct->lock);
230 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
232 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
233 struct nf_conntrack_expect *exp,
234 u32 portid, int report)
237 struct net *net = nf_ct_exp_net(exp);
238 struct nf_exp_event_notifier *notify;
239 struct nf_conntrack_ecache *e;
242 notify = rcu_dereference(net->ct.nf_expect_event_cb);
246 e = nf_ct_ecache_find(exp->master);
250 if (e->expmask & (1 << event)) {
251 struct nf_exp_event item = {
256 notify->fcn(1 << event, &item);
262 int nf_conntrack_register_notifier(struct net *net,
263 struct nf_ct_event_notifier *new)
266 struct nf_ct_event_notifier *notify;
268 mutex_lock(&nf_ct_ecache_mutex);
269 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
270 lockdep_is_held(&nf_ct_ecache_mutex));
271 if (notify != NULL) {
275 rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new);
279 mutex_unlock(&nf_ct_ecache_mutex);
282 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
284 void nf_conntrack_unregister_notifier(struct net *net,
285 struct nf_ct_event_notifier *new)
287 struct nf_ct_event_notifier *notify;
289 mutex_lock(&nf_ct_ecache_mutex);
290 notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb,
291 lockdep_is_held(&nf_ct_ecache_mutex));
292 BUG_ON(notify != new);
293 RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
294 mutex_unlock(&nf_ct_ecache_mutex);
295 /* synchronize_rcu() is called from ctnetlink_exit. */
297 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
299 int nf_ct_expect_register_notifier(struct net *net,
300 struct nf_exp_event_notifier *new)
303 struct nf_exp_event_notifier *notify;
305 mutex_lock(&nf_ct_ecache_mutex);
306 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
307 lockdep_is_held(&nf_ct_ecache_mutex));
308 if (notify != NULL) {
312 rcu_assign_pointer(net->ct.nf_expect_event_cb, new);
316 mutex_unlock(&nf_ct_ecache_mutex);
319 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
321 void nf_ct_expect_unregister_notifier(struct net *net,
322 struct nf_exp_event_notifier *new)
324 struct nf_exp_event_notifier *notify;
326 mutex_lock(&nf_ct_ecache_mutex);
327 notify = rcu_dereference_protected(net->ct.nf_expect_event_cb,
328 lockdep_is_held(&nf_ct_ecache_mutex));
329 BUG_ON(notify != new);
330 RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL);
331 mutex_unlock(&nf_ct_ecache_mutex);
332 /* synchronize_rcu() is called from ctnetlink_exit. */
334 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
336 #define NF_CT_EVENTS_DEFAULT 1
337 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
339 static const struct nf_ct_ext_type event_extend = {
340 .len = sizeof(struct nf_conntrack_ecache),
341 .align = __alignof__(struct nf_conntrack_ecache),
342 .id = NF_CT_EXT_ECACHE,
345 int nf_conntrack_ecache_pernet_init(struct net *net)
347 net->ct.sysctl_events = nf_ct_events;
348 INIT_DELAYED_WORK(&net->ct.ecache_dwork, ecache_work);
352 void nf_conntrack_ecache_pernet_fini(struct net *net)
354 cancel_delayed_work_sync(&net->ct.ecache_dwork);
357 int nf_conntrack_ecache_init(void)
359 int ret = nf_ct_extend_register(&event_extend);
361 pr_err("Unable to register event extension\n");
363 BUILD_BUG_ON(__IPCT_MAX >= 16); /* ctmask, missed use u16 */
368 void nf_conntrack_ecache_fini(void)
370 nf_ct_extend_unregister(&event_extend);