netfilter: conntrack: replace notify chain by function pointer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / nf_conntrack_ecache.c
blob5516b3e64b4330baa7ec14778d762cf760de1df1
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 */
35 static inline void
36 __nf_ct_deliver_cached_events(struct nf_conntrack_ecache *ecache)
38 struct nf_ct_event_notifier *notify;
40 rcu_read_lock();
41 notify = rcu_dereference(nf_conntrack_event_cb);
42 if (notify == NULL)
43 goto out_unlock;
45 if (nf_ct_is_confirmed(ecache->ct) && !nf_ct_is_dying(ecache->ct)
46 && ecache->events) {
47 struct nf_ct_event item = {
48 .ct = ecache->ct,
49 .pid = 0,
50 .report = 0
53 notify->fcn(ecache->events, &item);
56 ecache->events = 0;
57 nf_ct_put(ecache->ct);
58 ecache->ct = NULL;
60 out_unlock:
61 rcu_read_unlock();
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;
71 local_bh_disable();
72 ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id());
73 if (ecache->ct == ct)
74 __nf_ct_deliver_cached_events(ecache);
75 local_bh_enable();
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);
88 if (ecache->ct)
89 __nf_ct_deliver_cached_events(ecache);
90 /* initialize for this conntrack/packet */
91 ecache->ct = ct;
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;
101 int cpu;
103 for_each_possible_cpu(cpu) {
104 ecache = per_cpu_ptr(net->ct.ecache, cpu);
105 if (ecache->ct)
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);
113 if (!net->ct.ecache)
114 return -ENOMEM;
115 return 0;
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)
125 int ret = 0;
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) {
131 ret = -EBUSY;
132 goto out_unlock;
134 rcu_assign_pointer(nf_conntrack_event_cb, new);
135 mutex_unlock(&nf_ct_ecache_mutex);
136 return ret;
138 out_unlock:
139 mutex_unlock(&nf_ct_ecache_mutex);
140 return ret;
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)
158 int ret = 0;
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) {
164 ret = -EBUSY;
165 goto out_unlock;
167 rcu_assign_pointer(nf_expect_event_cb, new);
168 mutex_unlock(&nf_ct_ecache_mutex);
169 return ret;
171 out_unlock:
172 mutex_unlock(&nf_ct_ecache_mutex);
173 return ret;
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);