1 /* Event cache for netfilter. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/netfilter.h>
14 #include <linux/skbuff.h>
15 #include <linux/vmalloc.h>
16 #include <linux/stddef.h>
17 #include <linux/err.h>
18 #include <linux/percpu.h>
19 #include <linux/kernel.h>
20 #include <linux/netdevice.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
25 static DEFINE_MUTEX(nf_ct_ecache_mutex);
27 struct nf_ct_event_notifier *nf_conntrack_event_cb __read_mostly;
28 EXPORT_SYMBOL_GPL(nf_conntrack_event_cb);
30 struct nf_exp_event_notifier *nf_expect_event_cb __read_mostly;
31 EXPORT_SYMBOL_GPL(nf_expect_event_cb);
33 /* deliver cached events and clear cache entry - must be called with locally
34 * disabled softirqs */
36 __nf_ct_deliver_cached_events(struct nf_conntrack_ecache *ecache)
38 struct nf_ct_event_notifier *notify;
41 notify = rcu_dereference(nf_conntrack_event_cb);
45 if (nf_ct_is_confirmed(ecache->ct) && !nf_ct_is_dying(ecache->ct)
47 struct nf_ct_event item = {
53 notify->fcn(ecache->events, &item);
57 nf_ct_put(ecache->ct);
64 /* Deliver all cached events for a particular conntrack. This is called
65 * by code prior to async packet handling for freeing the skb */
66 void nf_ct_deliver_cached_events(const struct nf_conn *ct)
68 struct net *net = nf_ct_net(ct);
69 struct nf_conntrack_ecache *ecache;
72 ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id());
74 __nf_ct_deliver_cached_events(ecache);
77 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
79 /* Deliver cached events for old pending events, if current conntrack != old */
80 void __nf_ct_event_cache_init(struct nf_conn *ct)
82 struct net *net = nf_ct_net(ct);
83 struct nf_conntrack_ecache *ecache;
85 /* take care of delivering potentially old events */
86 ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id());
87 BUG_ON(ecache->ct == ct);
89 __nf_ct_deliver_cached_events(ecache);
90 /* initialize for this conntrack/packet */
92 nf_conntrack_get(&ct->ct_general);
94 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
96 /* flush the event cache - touches other CPU's data and must not be called
97 * while packets are still passing through the code */
98 void nf_ct_event_cache_flush(struct net *net)
100 struct nf_conntrack_ecache *ecache;
103 for_each_possible_cpu(cpu) {
104 ecache = per_cpu_ptr(net->ct.ecache, cpu);
106 nf_ct_put(ecache->ct);
110 int nf_conntrack_ecache_init(struct net *net)
112 net->ct.ecache = alloc_percpu(struct nf_conntrack_ecache);
118 void nf_conntrack_ecache_fini(struct net *net)
120 free_percpu(net->ct.ecache);
123 int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new)
126 struct nf_ct_event_notifier *notify;
128 mutex_lock(&nf_ct_ecache_mutex);
129 notify = rcu_dereference(nf_conntrack_event_cb);
130 if (notify != NULL) {
134 rcu_assign_pointer(nf_conntrack_event_cb, new);
135 mutex_unlock(&nf_ct_ecache_mutex);
139 mutex_unlock(&nf_ct_ecache_mutex);
142 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
144 void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new)
146 struct nf_ct_event_notifier *notify;
148 mutex_lock(&nf_ct_ecache_mutex);
149 notify = rcu_dereference(nf_conntrack_event_cb);
150 BUG_ON(notify != new);
151 rcu_assign_pointer(nf_conntrack_event_cb, NULL);
152 mutex_unlock(&nf_ct_ecache_mutex);
154 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
156 int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new)
159 struct nf_exp_event_notifier *notify;
161 mutex_lock(&nf_ct_ecache_mutex);
162 notify = rcu_dereference(nf_expect_event_cb);
163 if (notify != NULL) {
167 rcu_assign_pointer(nf_expect_event_cb, new);
168 mutex_unlock(&nf_ct_ecache_mutex);
172 mutex_unlock(&nf_ct_ecache_mutex);
175 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
177 void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new)
179 struct nf_exp_event_notifier *notify;
181 mutex_lock(&nf_ct_ecache_mutex);
182 notify = rcu_dereference(nf_expect_event_cb);
183 BUG_ON(notify != new);
184 rcu_assign_pointer(nf_expect_event_cb, NULL);
185 mutex_unlock(&nf_ct_ecache_mutex);
187 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);