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>
24 #include <net/netfilter/nf_conntrack_extend.h>
26 static DEFINE_MUTEX(nf_ct_ecache_mutex);
28 struct nf_ct_event_notifier *nf_conntrack_event_cb __read_mostly;
29 EXPORT_SYMBOL_GPL(nf_conntrack_event_cb);
31 struct nf_exp_event_notifier *nf_expect_event_cb __read_mostly;
32 EXPORT_SYMBOL_GPL(nf_expect_event_cb);
34 /* deliver cached events and clear cache entry - must be called with locally
35 * disabled softirqs */
36 void nf_ct_deliver_cached_events(struct nf_conn *ct)
39 struct nf_ct_event_notifier *notify;
40 struct nf_conntrack_ecache *e;
43 notify = rcu_dereference(nf_conntrack_event_cb);
47 e = nf_ct_ecache_find(ct);
51 events = xchg(&e->cache, 0);
53 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct) && events) {
54 struct nf_ct_event item = {
60 /* We make a copy of the missed event cache without taking
61 * the lock, thus we may send missed events twice. However,
62 * this does not harm and it happens very rarely. */
63 unsigned long missed = e->missed;
65 ret = notify->fcn(events | missed, &item);
66 if (unlikely(ret < 0 || missed)) {
67 spin_lock_bh(&ct->lock);
72 spin_unlock_bh(&ct->lock);
79 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
81 int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new)
84 struct nf_ct_event_notifier *notify;
86 mutex_lock(&nf_ct_ecache_mutex);
87 notify = rcu_dereference(nf_conntrack_event_cb);
92 rcu_assign_pointer(nf_conntrack_event_cb, new);
93 mutex_unlock(&nf_ct_ecache_mutex);
97 mutex_unlock(&nf_ct_ecache_mutex);
100 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
102 void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new)
104 struct nf_ct_event_notifier *notify;
106 mutex_lock(&nf_ct_ecache_mutex);
107 notify = rcu_dereference(nf_conntrack_event_cb);
108 BUG_ON(notify != new);
109 rcu_assign_pointer(nf_conntrack_event_cb, NULL);
110 mutex_unlock(&nf_ct_ecache_mutex);
112 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
114 int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new)
117 struct nf_exp_event_notifier *notify;
119 mutex_lock(&nf_ct_ecache_mutex);
120 notify = rcu_dereference(nf_expect_event_cb);
121 if (notify != NULL) {
125 rcu_assign_pointer(nf_expect_event_cb, new);
126 mutex_unlock(&nf_ct_ecache_mutex);
130 mutex_unlock(&nf_ct_ecache_mutex);
133 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
135 void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new)
137 struct nf_exp_event_notifier *notify;
139 mutex_lock(&nf_ct_ecache_mutex);
140 notify = rcu_dereference(nf_expect_event_cb);
141 BUG_ON(notify != new);
142 rcu_assign_pointer(nf_expect_event_cb, NULL);
143 mutex_unlock(&nf_ct_ecache_mutex);
145 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);
147 #define NF_CT_EVENTS_DEFAULT 1
148 static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT;
149 static int nf_ct_events_retry_timeout __read_mostly = 15*HZ;
152 static struct ctl_table event_sysctl_table[] = {
154 .ctl_name = CTL_UNNUMBERED,
155 .procname = "nf_conntrack_events",
156 .data = &init_net.ct.sysctl_events,
157 .maxlen = sizeof(unsigned int),
159 .proc_handler = proc_dointvec,
162 .ctl_name = CTL_UNNUMBERED,
163 .procname = "nf_conntrack_events_retry_timeout",
164 .data = &init_net.ct.sysctl_events_retry_timeout,
165 .maxlen = sizeof(unsigned int),
167 .proc_handler = proc_dointvec_jiffies,
171 #endif /* CONFIG_SYSCTL */
173 static struct nf_ct_ext_type event_extend __read_mostly = {
174 .len = sizeof(struct nf_conntrack_ecache),
175 .align = __alignof__(struct nf_conntrack_ecache),
176 .id = NF_CT_EXT_ECACHE,
180 static int nf_conntrack_event_init_sysctl(struct net *net)
182 struct ctl_table *table;
184 table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table),
189 table[0].data = &net->ct.sysctl_events;
190 table[1].data = &net->ct.sysctl_events_retry_timeout;
192 net->ct.event_sysctl_header =
193 register_net_sysctl_table(net,
194 nf_net_netfilter_sysctl_path, table);
195 if (!net->ct.event_sysctl_header) {
196 printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n");
207 static void nf_conntrack_event_fini_sysctl(struct net *net)
209 struct ctl_table *table;
211 table = net->ct.event_sysctl_header->ctl_table_arg;
212 unregister_net_sysctl_table(net->ct.event_sysctl_header);
216 static int nf_conntrack_event_init_sysctl(struct net *net)
221 static void nf_conntrack_event_fini_sysctl(struct net *net)
224 #endif /* CONFIG_SYSCTL */
226 int nf_conntrack_ecache_init(struct net *net)
230 net->ct.sysctl_events = nf_ct_events;
231 net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;
233 if (net_eq(net, &init_net)) {
234 ret = nf_ct_extend_register(&event_extend);
236 printk(KERN_ERR "nf_ct_event: Unable to register "
237 "event extension.\n");
238 goto out_extend_register;
242 ret = nf_conntrack_event_init_sysctl(net);
249 if (net_eq(net, &init_net))
250 nf_ct_extend_unregister(&event_extend);
255 void nf_conntrack_ecache_fini(struct net *net)
257 nf_conntrack_event_fini_sysctl(net);
258 if (net_eq(net, &init_net))
259 nf_ct_extend_unregister(&event_extend);