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 #define ECACHE_RETRY_WAIT (HZ/10)
40 static enum retry_state
ecache_work_evict_list(struct ct_pcpu
*pcpu
)
42 struct nf_conn
*refs
[16];
43 struct nf_conntrack_tuple_hash
*h
;
44 struct hlist_nulls_node
*n
;
45 unsigned int evicted
= 0;
46 enum retry_state ret
= STATE_DONE
;
48 spin_lock(&pcpu
->lock
);
50 hlist_nulls_for_each_entry(h
, n
, &pcpu
->dying
, hnnode
) {
51 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
53 if (nf_ct_is_dying(ct
))
56 if (nf_conntrack_event(IPCT_DESTROY
, ct
)) {
57 ret
= STATE_CONGESTED
;
61 /* we've got the event delivered, now it's dying */
62 set_bit(IPS_DYING_BIT
, &ct
->status
);
65 if (++evicted
>= ARRAY_SIZE(refs
)) {
71 spin_unlock(&pcpu
->lock
);
73 /* can't _put while holding lock */
75 nf_ct_put(refs
[--evicted
]);
80 static void ecache_work(struct work_struct
*work
)
82 struct netns_ct
*ctnet
=
83 container_of(work
, struct netns_ct
, ecache_dwork
.work
);
89 for_each_possible_cpu(cpu
) {
92 pcpu
= per_cpu_ptr(ctnet
->pcpu_lists
, cpu
);
94 ret
= ecache_work_evict_list(pcpu
);
98 delay
= ECACHE_RETRY_WAIT
;
111 ctnet
->ecache_dwork_pending
= delay
> 0;
113 schedule_delayed_work(&ctnet
->ecache_dwork
, delay
);
116 int nf_conntrack_eventmask_report(unsigned int eventmask
, struct nf_conn
*ct
,
117 u32 portid
, int report
)
120 struct net
*net
= nf_ct_net(ct
);
121 struct nf_ct_event_notifier
*notify
;
122 struct nf_conntrack_ecache
*e
;
125 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
129 e
= nf_ct_ecache_find(ct
);
133 if (nf_ct_is_confirmed(ct
) && !nf_ct_is_dying(ct
)) {
134 struct nf_ct_event item
= {
136 .portid
= e
->portid
? e
->portid
: portid
,
139 /* This is a resent of a destroy event? If so, skip missed */
140 unsigned long missed
= e
->portid
? 0 : e
->missed
;
142 if (!((eventmask
| missed
) & e
->ctmask
))
145 ret
= notify
->fcn(eventmask
| missed
, &item
);
146 if (unlikely(ret
< 0 || missed
)) {
147 spin_lock_bh(&ct
->lock
);
149 /* This is a destroy event that has been
150 * triggered by a process, we store the PORTID
151 * to include it in the retransmission.
153 if (eventmask
& (1 << IPCT_DESTROY
) &&
154 e
->portid
== 0 && portid
!= 0)
157 e
->missed
|= eventmask
;
159 e
->missed
&= ~missed
;
161 spin_unlock_bh(&ct
->lock
);
168 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report
);
170 /* deliver cached events and clear cache entry - must be called with locally
171 * disabled softirqs */
172 void nf_ct_deliver_cached_events(struct nf_conn
*ct
)
174 struct net
*net
= nf_ct_net(ct
);
175 unsigned long events
, missed
;
176 struct nf_ct_event_notifier
*notify
;
177 struct nf_conntrack_ecache
*e
;
178 struct nf_ct_event item
;
182 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
186 e
= nf_ct_ecache_find(ct
);
190 events
= xchg(&e
->cache
, 0);
192 if (!nf_ct_is_confirmed(ct
) || nf_ct_is_dying(ct
) || !events
)
195 /* We make a copy of the missed event cache without taking
196 * the lock, thus we may send missed events twice. However,
197 * this does not harm and it happens very rarely. */
200 if (!((events
| missed
) & e
->ctmask
))
207 ret
= notify
->fcn(events
| missed
, &item
);
209 if (likely(ret
>= 0 && !missed
))
212 spin_lock_bh(&ct
->lock
);
216 e
->missed
&= ~missed
;
217 spin_unlock_bh(&ct
->lock
);
222 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events
);
224 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event
,
225 struct nf_conntrack_expect
*exp
,
226 u32 portid
, int report
)
229 struct net
*net
= nf_ct_exp_net(exp
);
230 struct nf_exp_event_notifier
*notify
;
231 struct nf_conntrack_ecache
*e
;
234 notify
= rcu_dereference(net
->ct
.nf_expect_event_cb
);
238 e
= nf_ct_ecache_find(exp
->master
);
242 if (e
->expmask
& (1 << event
)) {
243 struct nf_exp_event item
= {
248 notify
->fcn(1 << event
, &item
);
254 int nf_conntrack_register_notifier(struct net
*net
,
255 struct nf_ct_event_notifier
*new)
258 struct nf_ct_event_notifier
*notify
;
260 mutex_lock(&nf_ct_ecache_mutex
);
261 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
262 lockdep_is_held(&nf_ct_ecache_mutex
));
263 if (notify
!= NULL
) {
267 rcu_assign_pointer(net
->ct
.nf_conntrack_event_cb
, new);
271 mutex_unlock(&nf_ct_ecache_mutex
);
274 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier
);
276 void nf_conntrack_unregister_notifier(struct net
*net
,
277 struct nf_ct_event_notifier
*new)
279 struct nf_ct_event_notifier
*notify
;
281 mutex_lock(&nf_ct_ecache_mutex
);
282 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
283 lockdep_is_held(&nf_ct_ecache_mutex
));
284 BUG_ON(notify
!= new);
285 RCU_INIT_POINTER(net
->ct
.nf_conntrack_event_cb
, NULL
);
286 mutex_unlock(&nf_ct_ecache_mutex
);
288 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier
);
290 int nf_ct_expect_register_notifier(struct net
*net
,
291 struct nf_exp_event_notifier
*new)
294 struct nf_exp_event_notifier
*notify
;
296 mutex_lock(&nf_ct_ecache_mutex
);
297 notify
= rcu_dereference_protected(net
->ct
.nf_expect_event_cb
,
298 lockdep_is_held(&nf_ct_ecache_mutex
));
299 if (notify
!= NULL
) {
303 rcu_assign_pointer(net
->ct
.nf_expect_event_cb
, new);
307 mutex_unlock(&nf_ct_ecache_mutex
);
310 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier
);
312 void nf_ct_expect_unregister_notifier(struct net
*net
,
313 struct nf_exp_event_notifier
*new)
315 struct nf_exp_event_notifier
*notify
;
317 mutex_lock(&nf_ct_ecache_mutex
);
318 notify
= rcu_dereference_protected(net
->ct
.nf_expect_event_cb
,
319 lockdep_is_held(&nf_ct_ecache_mutex
));
320 BUG_ON(notify
!= new);
321 RCU_INIT_POINTER(net
->ct
.nf_expect_event_cb
, NULL
);
322 mutex_unlock(&nf_ct_ecache_mutex
);
324 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier
);
326 #define NF_CT_EVENTS_DEFAULT 1
327 static int nf_ct_events __read_mostly
= NF_CT_EVENTS_DEFAULT
;
330 static struct ctl_table event_sysctl_table
[] = {
332 .procname
= "nf_conntrack_events",
333 .data
= &init_net
.ct
.sysctl_events
,
334 .maxlen
= sizeof(unsigned int),
336 .proc_handler
= proc_dointvec
,
340 #endif /* CONFIG_SYSCTL */
342 static struct nf_ct_ext_type event_extend __read_mostly
= {
343 .len
= sizeof(struct nf_conntrack_ecache
),
344 .align
= __alignof__(struct nf_conntrack_ecache
),
345 .id
= NF_CT_EXT_ECACHE
,
349 static int nf_conntrack_event_init_sysctl(struct net
*net
)
351 struct ctl_table
*table
;
353 table
= kmemdup(event_sysctl_table
, sizeof(event_sysctl_table
),
358 table
[0].data
= &net
->ct
.sysctl_events
;
360 /* Don't export sysctls to unprivileged users */
361 if (net
->user_ns
!= &init_user_ns
)
362 table
[0].procname
= NULL
;
364 net
->ct
.event_sysctl_header
=
365 register_net_sysctl(net
, "net/netfilter", table
);
366 if (!net
->ct
.event_sysctl_header
) {
367 printk(KERN_ERR
"nf_ct_event: can't register to sysctl.\n");
378 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
380 struct ctl_table
*table
;
382 table
= net
->ct
.event_sysctl_header
->ctl_table_arg
;
383 unregister_net_sysctl_table(net
->ct
.event_sysctl_header
);
387 static int nf_conntrack_event_init_sysctl(struct net
*net
)
392 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
395 #endif /* CONFIG_SYSCTL */
397 int nf_conntrack_ecache_pernet_init(struct net
*net
)
399 net
->ct
.sysctl_events
= nf_ct_events
;
400 INIT_DELAYED_WORK(&net
->ct
.ecache_dwork
, ecache_work
);
401 return nf_conntrack_event_init_sysctl(net
);
404 void nf_conntrack_ecache_pernet_fini(struct net
*net
)
406 cancel_delayed_work_sync(&net
->ct
.ecache_dwork
);
407 nf_conntrack_event_fini_sysctl(net
);
410 int nf_conntrack_ecache_init(void)
412 int ret
= nf_ct_extend_register(&event_extend
);
414 pr_err("nf_ct_event: Unable to register event extension.\n");
418 void nf_conntrack_ecache_fini(void)
420 nf_ct_extend_unregister(&event_extend
);