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
);
52 struct nf_conntrack_ecache
*e
;
54 if (!nf_ct_is_confirmed(ct
))
57 e
= nf_ct_ecache_find(ct
);
58 if (!e
|| e
->state
!= NFCT_ECACHE_DESTROY_FAIL
)
61 if (nf_conntrack_event(IPCT_DESTROY
, ct
)) {
62 ret
= STATE_CONGESTED
;
66 e
->state
= NFCT_ECACHE_DESTROY_SENT
;
69 if (++evicted
>= ARRAY_SIZE(refs
)) {
75 spin_unlock(&pcpu
->lock
);
77 /* can't _put while holding lock */
79 nf_ct_put(refs
[--evicted
]);
84 static void ecache_work(struct work_struct
*work
)
86 struct netns_ct
*ctnet
=
87 container_of(work
, struct netns_ct
, ecache_dwork
.work
);
93 for_each_possible_cpu(cpu
) {
96 pcpu
= per_cpu_ptr(ctnet
->pcpu_lists
, cpu
);
98 ret
= ecache_work_evict_list(pcpu
);
101 case STATE_CONGESTED
:
102 delay
= ECACHE_RETRY_WAIT
;
115 ctnet
->ecache_dwork_pending
= delay
> 0;
117 schedule_delayed_work(&ctnet
->ecache_dwork
, delay
);
120 int nf_conntrack_eventmask_report(unsigned int eventmask
, struct nf_conn
*ct
,
121 u32 portid
, int report
)
124 struct net
*net
= nf_ct_net(ct
);
125 struct nf_ct_event_notifier
*notify
;
126 struct nf_conntrack_ecache
*e
;
129 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
133 e
= nf_ct_ecache_find(ct
);
137 if (nf_ct_is_confirmed(ct
)) {
138 struct nf_ct_event item
= {
140 .portid
= e
->portid
? e
->portid
: portid
,
143 /* This is a resent of a destroy event? If so, skip missed */
144 unsigned long missed
= e
->portid
? 0 : e
->missed
;
146 if (!((eventmask
| missed
) & e
->ctmask
))
149 ret
= notify
->fcn(eventmask
| missed
, &item
);
150 if (unlikely(ret
< 0 || missed
)) {
151 spin_lock_bh(&ct
->lock
);
153 /* This is a destroy event that has been
154 * triggered by a process, we store the PORTID
155 * to include it in the retransmission.
157 if (eventmask
& (1 << IPCT_DESTROY
)) {
158 if (e
->portid
== 0 && portid
!= 0)
160 e
->state
= NFCT_ECACHE_DESTROY_FAIL
;
162 e
->missed
|= eventmask
;
165 e
->missed
&= ~missed
;
167 spin_unlock_bh(&ct
->lock
);
174 EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report
);
176 /* deliver cached events and clear cache entry - must be called with locally
177 * disabled softirqs */
178 void nf_ct_deliver_cached_events(struct nf_conn
*ct
)
180 struct net
*net
= nf_ct_net(ct
);
181 unsigned long events
, missed
;
182 struct nf_ct_event_notifier
*notify
;
183 struct nf_conntrack_ecache
*e
;
184 struct nf_ct_event item
;
188 notify
= rcu_dereference(net
->ct
.nf_conntrack_event_cb
);
192 e
= nf_ct_ecache_find(ct
);
196 events
= xchg(&e
->cache
, 0);
198 if (!nf_ct_is_confirmed(ct
) || nf_ct_is_dying(ct
) || !events
)
201 /* We make a copy of the missed event cache without taking
202 * the lock, thus we may send missed events twice. However,
203 * this does not harm and it happens very rarely. */
206 if (!((events
| missed
) & e
->ctmask
))
213 ret
= notify
->fcn(events
| missed
, &item
);
215 if (likely(ret
>= 0 && !missed
))
218 spin_lock_bh(&ct
->lock
);
222 e
->missed
&= ~missed
;
223 spin_unlock_bh(&ct
->lock
);
228 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events
);
230 void nf_ct_expect_event_report(enum ip_conntrack_expect_events event
,
231 struct nf_conntrack_expect
*exp
,
232 u32 portid
, int report
)
235 struct net
*net
= nf_ct_exp_net(exp
);
236 struct nf_exp_event_notifier
*notify
;
237 struct nf_conntrack_ecache
*e
;
240 notify
= rcu_dereference(net
->ct
.nf_expect_event_cb
);
244 e
= nf_ct_ecache_find(exp
->master
);
248 if (e
->expmask
& (1 << event
)) {
249 struct nf_exp_event item
= {
254 notify
->fcn(1 << event
, &item
);
260 int nf_conntrack_register_notifier(struct net
*net
,
261 struct nf_ct_event_notifier
*new)
264 struct nf_ct_event_notifier
*notify
;
266 mutex_lock(&nf_ct_ecache_mutex
);
267 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
268 lockdep_is_held(&nf_ct_ecache_mutex
));
269 if (notify
!= NULL
) {
273 rcu_assign_pointer(net
->ct
.nf_conntrack_event_cb
, new);
277 mutex_unlock(&nf_ct_ecache_mutex
);
280 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier
);
282 void nf_conntrack_unregister_notifier(struct net
*net
,
283 struct nf_ct_event_notifier
*new)
285 struct nf_ct_event_notifier
*notify
;
287 mutex_lock(&nf_ct_ecache_mutex
);
288 notify
= rcu_dereference_protected(net
->ct
.nf_conntrack_event_cb
,
289 lockdep_is_held(&nf_ct_ecache_mutex
));
290 BUG_ON(notify
!= new);
291 RCU_INIT_POINTER(net
->ct
.nf_conntrack_event_cb
, NULL
);
292 mutex_unlock(&nf_ct_ecache_mutex
);
294 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier
);
296 int nf_ct_expect_register_notifier(struct net
*net
,
297 struct nf_exp_event_notifier
*new)
300 struct nf_exp_event_notifier
*notify
;
302 mutex_lock(&nf_ct_ecache_mutex
);
303 notify
= rcu_dereference_protected(net
->ct
.nf_expect_event_cb
,
304 lockdep_is_held(&nf_ct_ecache_mutex
));
305 if (notify
!= NULL
) {
309 rcu_assign_pointer(net
->ct
.nf_expect_event_cb
, new);
313 mutex_unlock(&nf_ct_ecache_mutex
);
316 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier
);
318 void nf_ct_expect_unregister_notifier(struct net
*net
,
319 struct nf_exp_event_notifier
*new)
321 struct nf_exp_event_notifier
*notify
;
323 mutex_lock(&nf_ct_ecache_mutex
);
324 notify
= rcu_dereference_protected(net
->ct
.nf_expect_event_cb
,
325 lockdep_is_held(&nf_ct_ecache_mutex
));
326 BUG_ON(notify
!= new);
327 RCU_INIT_POINTER(net
->ct
.nf_expect_event_cb
, NULL
);
328 mutex_unlock(&nf_ct_ecache_mutex
);
330 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier
);
332 #define NF_CT_EVENTS_DEFAULT 1
333 static int nf_ct_events __read_mostly
= NF_CT_EVENTS_DEFAULT
;
336 static struct ctl_table event_sysctl_table
[] = {
338 .procname
= "nf_conntrack_events",
339 .data
= &init_net
.ct
.sysctl_events
,
340 .maxlen
= sizeof(unsigned int),
342 .proc_handler
= proc_dointvec
,
346 #endif /* CONFIG_SYSCTL */
348 static struct nf_ct_ext_type event_extend __read_mostly
= {
349 .len
= sizeof(struct nf_conntrack_ecache
),
350 .align
= __alignof__(struct nf_conntrack_ecache
),
351 .id
= NF_CT_EXT_ECACHE
,
355 static int nf_conntrack_event_init_sysctl(struct net
*net
)
357 struct ctl_table
*table
;
359 table
= kmemdup(event_sysctl_table
, sizeof(event_sysctl_table
),
364 table
[0].data
= &net
->ct
.sysctl_events
;
366 /* Don't export sysctls to unprivileged users */
367 if (net
->user_ns
!= &init_user_ns
)
368 table
[0].procname
= NULL
;
370 net
->ct
.event_sysctl_header
=
371 register_net_sysctl(net
, "net/netfilter", table
);
372 if (!net
->ct
.event_sysctl_header
) {
373 printk(KERN_ERR
"nf_ct_event: can't register to sysctl.\n");
384 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
386 struct ctl_table
*table
;
388 table
= net
->ct
.event_sysctl_header
->ctl_table_arg
;
389 unregister_net_sysctl_table(net
->ct
.event_sysctl_header
);
393 static int nf_conntrack_event_init_sysctl(struct net
*net
)
398 static void nf_conntrack_event_fini_sysctl(struct net
*net
)
401 #endif /* CONFIG_SYSCTL */
403 int nf_conntrack_ecache_pernet_init(struct net
*net
)
405 net
->ct
.sysctl_events
= nf_ct_events
;
406 INIT_DELAYED_WORK(&net
->ct
.ecache_dwork
, ecache_work
);
407 return nf_conntrack_event_init_sysctl(net
);
410 void nf_conntrack_ecache_pernet_fini(struct net
*net
)
412 cancel_delayed_work_sync(&net
->ct
.ecache_dwork
);
413 nf_conntrack_event_fini_sysctl(net
);
416 int nf_conntrack_ecache_init(void)
418 int ret
= nf_ct_extend_register(&event_extend
);
420 pr_err("nf_ct_event: Unable to register event extension.\n");
424 void nf_conntrack_ecache_fini(void)
426 nf_ct_extend_unregister(&event_extend
);