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 #include <linux/types.h>
15 #include <linux/netfilter.h>
16 #include <linux/skbuff.h>
17 #include <linux/vmalloc.h>
18 #include <linux/stddef.h>
19 #include <linux/err.h>
20 #include <linux/percpu.h>
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_extend.h>
30 static DEFINE_MUTEX(nf_ct_ecache_mutex
);
32 /* deliver cached events and clear cache entry - must be called with locally
33 * disabled softirqs */
34 void nf_ct_deliver_cached_events(struct nf_conn
*ct
)
36 struct net
*net
= nf_ct_net(ct
);
37 unsigned long events
, missed
;
38 struct nf_ct_event_notifier
*notify
;
39 struct nf_conntrack_ecache
*e
;
40 struct nf_ct_event item
;
44 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
48 e
= nf_ct_ecache_find(ct
);
52 events
= xchg(&e
->cache
, 0);
54 if (!nf_ct_is_confirmed(ct
) || nf_ct_is_dying(ct
) || !events
)
57 /* We make a copy of the missed event cache without taking
58 * the lock, thus we may send missed events twice. However,
59 * this does not harm and it happens very rarely. */
62 if (!((events
| missed
) & e
->ctmask
))
69 ret
= notify
->fcn(events
| missed
, &item
);
71 if (likely(ret
>= 0 && !missed
))
74 spin_lock_bh(&ct
->lock
);
79 spin_unlock_bh(&ct
->lock
);
84 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events
);
86 int nf_conntrack_register_notifier(struct net
*net
,
87 struct nf_ct_event_notifier
*new)
90 struct nf_ct_event_notifier
*notify
;
92 mutex_lock(&nf_ct_ecache_mutex
);
93 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
94 lockdep_is_held(&nf_ct_ecache_mutex
));
99 rcu_assign_pointer(net
->ct
.nf_conntrack_event_cb
, new);
103 mutex_unlock(&nf_ct_ecache_mutex
);
106 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier
);
108 void nf_conntrack_unregister_notifier(struct net
*net
,
109 struct nf_ct_event_notifier
*new)
111 struct nf_ct_event_notifier
*notify
;
113 mutex_lock(&nf_ct_ecache_mutex
);
114 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
115 lockdep_is_held(&nf_ct_ecache_mutex
));
116 BUG_ON(notify
!= new);
117 RCU_INIT_POINTER(net
->ct
.nf_conntrack_event_cb
, NULL
);
118 mutex_unlock(&nf_ct_ecache_mutex
);
120 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier
);
122 int nf_ct_expect_register_notifier(struct net
*net
,
123 struct nf_exp_event_notifier
*new)
126 struct nf_exp_event_notifier
*notify
;
128 mutex_lock(&nf_ct_ecache_mutex
);
129 notify
= rcu_dereference_protected(net
->ct
.nf_expect_event_cb
,
130 lockdep_is_held(&nf_ct_ecache_mutex
));
131 if (notify
!= NULL
) {
135 rcu_assign_pointer(net
->ct
.nf_expect_event_cb
, new);
139 mutex_unlock(&nf_ct_ecache_mutex
);
142 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier
);
144 void nf_ct_expect_unregister_notifier(struct net
*net
,
145 struct nf_exp_event_notifier
*new)
147 struct nf_exp_event_notifier
*notify
;
149 mutex_lock(&nf_ct_ecache_mutex
);
150 notify
= rcu_dereference_protected(net
->ct
.nf_expect_event_cb
,
151 lockdep_is_held(&nf_ct_ecache_mutex
));
152 BUG_ON(notify
!= new);
153 RCU_INIT_POINTER(net
->ct
.nf_expect_event_cb
, NULL
);
154 mutex_unlock(&nf_ct_ecache_mutex
);
156 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier
);
158 #define NF_CT_EVENTS_DEFAULT 1
159 static int nf_ct_events __read_mostly
= NF_CT_EVENTS_DEFAULT
;
160 static int nf_ct_events_retry_timeout __read_mostly
= 15*HZ
;
163 static struct ctl_table event_sysctl_table
[] = {
165 .procname
= "nf_conntrack_events",
166 .data
= &init_net
.ct
.sysctl_events
,
167 .maxlen
= sizeof(unsigned int),
169 .proc_handler
= proc_dointvec
,
172 .procname
= "nf_conntrack_events_retry_timeout",
173 .data
= &init_net
.ct
.sysctl_events_retry_timeout
,
174 .maxlen
= sizeof(unsigned int),
176 .proc_handler
= proc_dointvec_jiffies
,
180 #endif /* CONFIG_SYSCTL */
182 static struct nf_ct_ext_type event_extend __read_mostly
= {
183 .len
= sizeof(struct nf_conntrack_ecache
),
184 .align
= __alignof__(struct nf_conntrack_ecache
),
185 .id
= NF_CT_EXT_ECACHE
,
189 static int nf_conntrack_event_init_sysctl(struct net
*net
)
191 struct ctl_table
*table
;
193 table
= kmemdup(event_sysctl_table
, sizeof(event_sysctl_table
),
198 table
[0].data
= &net
->ct
.sysctl_events
;
199 table
[1].data
= &net
->ct
.sysctl_events_retry_timeout
;
201 /* Don't export sysctls to unprivileged users */
202 if (net
->user_ns
!= &init_user_ns
)
203 table
[0].procname
= NULL
;
205 net
->ct
.event_sysctl_header
=
206 register_net_sysctl(net
, "net/netfilter", table
);
207 if (!net
->ct
.event_sysctl_header
) {
208 printk(KERN_ERR
"nf_ct_event: can't register to sysctl.\n");
219 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
221 struct ctl_table
*table
;
223 table
= net
->ct
.event_sysctl_header
->ctl_table_arg
;
224 unregister_net_sysctl_table(net
->ct
.event_sysctl_header
);
228 static int nf_conntrack_event_init_sysctl(struct net
*net
)
233 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
236 #endif /* CONFIG_SYSCTL */
238 int nf_conntrack_ecache_pernet_init(struct net
*net
)
240 net
->ct
.sysctl_events
= nf_ct_events
;
241 net
->ct
.sysctl_events_retry_timeout
= nf_ct_events_retry_timeout
;
242 return nf_conntrack_event_init_sysctl(net
);
245 void nf_conntrack_ecache_pernet_fini(struct net
*net
)
247 nf_conntrack_event_fini_sysctl(net
);
250 int nf_conntrack_ecache_init(void)
252 int ret
= nf_ct_extend_register(&event_extend
);
254 pr_err("nf_ct_event: Unable to register event extension.\n");
258 void nf_conntrack_ecache_fini(void)
260 nf_ct_extend_unregister(&event_extend
);