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>
21 #include <linux/slab.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/nf_conntrack_extend.h>
27 static DEFINE_MUTEX(nf_ct_ecache_mutex
);
29 struct nf_ct_event_notifier __rcu
*nf_conntrack_event_cb __read_mostly
;
30 EXPORT_SYMBOL_GPL(nf_conntrack_event_cb
);
32 struct nf_exp_event_notifier __rcu
*nf_expect_event_cb __read_mostly
;
33 EXPORT_SYMBOL_GPL(nf_expect_event_cb
);
35 /* deliver cached events and clear cache entry - must be called with locally
36 * disabled softirqs */
37 void nf_ct_deliver_cached_events(struct nf_conn
*ct
)
40 struct nf_ct_event_notifier
*notify
;
41 struct nf_conntrack_ecache
*e
;
44 notify
= rcu_dereference(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
) {
55 struct nf_ct_event item
= {
61 /* We make a copy of the missed event cache without taking
62 * the lock, thus we may send missed events twice. However,
63 * this does not harm and it happens very rarely. */
64 unsigned long missed
= e
->missed
;
66 ret
= notify
->fcn(events
| missed
, &item
);
67 if (unlikely(ret
< 0 || missed
)) {
68 spin_lock_bh(&ct
->lock
);
73 spin_unlock_bh(&ct
->lock
);
80 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events
);
82 int nf_conntrack_register_notifier(struct nf_ct_event_notifier
*new)
85 struct nf_ct_event_notifier
*notify
;
87 mutex_lock(&nf_ct_ecache_mutex
);
88 notify
= rcu_dereference_protected(nf_conntrack_event_cb
,
89 lockdep_is_held(&nf_ct_ecache_mutex
));
94 rcu_assign_pointer(nf_conntrack_event_cb
, new);
95 mutex_unlock(&nf_ct_ecache_mutex
);
99 mutex_unlock(&nf_ct_ecache_mutex
);
102 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier
);
104 void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier
*new)
106 struct nf_ct_event_notifier
*notify
;
108 mutex_lock(&nf_ct_ecache_mutex
);
109 notify
= rcu_dereference_protected(nf_conntrack_event_cb
,
110 lockdep_is_held(&nf_ct_ecache_mutex
));
111 BUG_ON(notify
!= new);
112 rcu_assign_pointer(nf_conntrack_event_cb
, NULL
);
113 mutex_unlock(&nf_ct_ecache_mutex
);
115 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier
);
117 int nf_ct_expect_register_notifier(struct nf_exp_event_notifier
*new)
120 struct nf_exp_event_notifier
*notify
;
122 mutex_lock(&nf_ct_ecache_mutex
);
123 notify
= rcu_dereference_protected(nf_expect_event_cb
,
124 lockdep_is_held(&nf_ct_ecache_mutex
));
125 if (notify
!= NULL
) {
129 rcu_assign_pointer(nf_expect_event_cb
, new);
130 mutex_unlock(&nf_ct_ecache_mutex
);
134 mutex_unlock(&nf_ct_ecache_mutex
);
137 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier
);
139 void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier
*new)
141 struct nf_exp_event_notifier
*notify
;
143 mutex_lock(&nf_ct_ecache_mutex
);
144 notify
= rcu_dereference_protected(nf_expect_event_cb
,
145 lockdep_is_held(&nf_ct_ecache_mutex
));
146 BUG_ON(notify
!= new);
147 rcu_assign_pointer(nf_expect_event_cb
, NULL
);
148 mutex_unlock(&nf_ct_ecache_mutex
);
150 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier
);
152 #define NF_CT_EVENTS_DEFAULT 1
153 static int nf_ct_events __read_mostly
= NF_CT_EVENTS_DEFAULT
;
154 static int nf_ct_events_retry_timeout __read_mostly
= 15*HZ
;
157 static struct ctl_table event_sysctl_table
[] = {
159 .procname
= "nf_conntrack_events",
160 .data
= &init_net
.ct
.sysctl_events
,
161 .maxlen
= sizeof(unsigned int),
163 .proc_handler
= proc_dointvec
,
166 .procname
= "nf_conntrack_events_retry_timeout",
167 .data
= &init_net
.ct
.sysctl_events_retry_timeout
,
168 .maxlen
= sizeof(unsigned int),
170 .proc_handler
= proc_dointvec_jiffies
,
174 #endif /* CONFIG_SYSCTL */
176 static struct nf_ct_ext_type event_extend __read_mostly
= {
177 .len
= sizeof(struct nf_conntrack_ecache
),
178 .align
= __alignof__(struct nf_conntrack_ecache
),
179 .id
= NF_CT_EXT_ECACHE
,
183 static int nf_conntrack_event_init_sysctl(struct net
*net
)
185 struct ctl_table
*table
;
187 table
= kmemdup(event_sysctl_table
, sizeof(event_sysctl_table
),
192 table
[0].data
= &net
->ct
.sysctl_events
;
193 table
[1].data
= &net
->ct
.sysctl_events_retry_timeout
;
195 net
->ct
.event_sysctl_header
=
196 register_net_sysctl_table(net
,
197 nf_net_netfilter_sysctl_path
, table
);
198 if (!net
->ct
.event_sysctl_header
) {
199 printk(KERN_ERR
"nf_ct_event: can't register to sysctl.\n");
210 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
212 struct ctl_table
*table
;
214 table
= net
->ct
.event_sysctl_header
->ctl_table_arg
;
215 unregister_net_sysctl_table(net
->ct
.event_sysctl_header
);
219 static int nf_conntrack_event_init_sysctl(struct net
*net
)
224 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
227 #endif /* CONFIG_SYSCTL */
229 int nf_conntrack_ecache_init(struct net
*net
)
233 net
->ct
.sysctl_events
= nf_ct_events
;
234 net
->ct
.sysctl_events_retry_timeout
= nf_ct_events_retry_timeout
;
236 if (net_eq(net
, &init_net
)) {
237 ret
= nf_ct_extend_register(&event_extend
);
239 printk(KERN_ERR
"nf_ct_event: Unable to register "
240 "event extension.\n");
241 goto out_extend_register
;
245 ret
= nf_conntrack_event_init_sysctl(net
);
252 if (net_eq(net
, &init_net
))
253 nf_ct_extend_unregister(&event_extend
);
258 void nf_conntrack_ecache_fini(struct net
*net
)
260 nf_conntrack_event_fini_sysctl(net
);
261 if (net_eq(net
, &init_net
))
262 nf_ct_extend_unregister(&event_extend
);