5516b3e64b4330baa7ec14778d762cf760de1df1
[safe/jmp/linux-2.6] / net / netfilter / nf_conntrack_ecache.c
1 /* Event cache for netfilter. */
2
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>
6  *
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.
10  */
11
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
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24
25 static DEFINE_MUTEX(nf_ct_ecache_mutex);
26
27 struct nf_ct_event_notifier *nf_conntrack_event_cb __read_mostly;
28 EXPORT_SYMBOL_GPL(nf_conntrack_event_cb);
29
30 struct nf_exp_event_notifier *nf_expect_event_cb __read_mostly;
31 EXPORT_SYMBOL_GPL(nf_expect_event_cb);
32
33 /* deliver cached events and clear cache entry - must be called with locally
34  * disabled softirqs */
35 static inline void
36 __nf_ct_deliver_cached_events(struct nf_conntrack_ecache *ecache)
37 {
38         struct nf_ct_event_notifier *notify;
39
40         rcu_read_lock();
41         notify = rcu_dereference(nf_conntrack_event_cb);
42         if (notify == NULL)
43                 goto out_unlock;
44
45         if (nf_ct_is_confirmed(ecache->ct) && !nf_ct_is_dying(ecache->ct)
46             && ecache->events) {
47                 struct nf_ct_event item = {
48                         .ct     = ecache->ct,
49                         .pid    = 0,
50                         .report = 0
51                 };
52
53                 notify->fcn(ecache->events, &item);
54         }
55
56         ecache->events = 0;
57         nf_ct_put(ecache->ct);
58         ecache->ct = NULL;
59
60 out_unlock:
61         rcu_read_unlock();
62 }
63
64 /* Deliver all cached events for a particular conntrack. This is called
65  * by code prior to async packet handling for freeing the skb */
66 void nf_ct_deliver_cached_events(const struct nf_conn *ct)
67 {
68         struct net *net = nf_ct_net(ct);
69         struct nf_conntrack_ecache *ecache;
70
71         local_bh_disable();
72         ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id());
73         if (ecache->ct == ct)
74                 __nf_ct_deliver_cached_events(ecache);
75         local_bh_enable();
76 }
77 EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);
78
79 /* Deliver cached events for old pending events, if current conntrack != old */
80 void __nf_ct_event_cache_init(struct nf_conn *ct)
81 {
82         struct net *net = nf_ct_net(ct);
83         struct nf_conntrack_ecache *ecache;
84
85         /* take care of delivering potentially old events */
86         ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id());
87         BUG_ON(ecache->ct == ct);
88         if (ecache->ct)
89                 __nf_ct_deliver_cached_events(ecache);
90         /* initialize for this conntrack/packet */
91         ecache->ct = ct;
92         nf_conntrack_get(&ct->ct_general);
93 }
94 EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init);
95
96 /* flush the event cache - touches other CPU's data and must not be called
97  * while packets are still passing through the code */
98 void nf_ct_event_cache_flush(struct net *net)
99 {
100         struct nf_conntrack_ecache *ecache;
101         int cpu;
102
103         for_each_possible_cpu(cpu) {
104                 ecache = per_cpu_ptr(net->ct.ecache, cpu);
105                 if (ecache->ct)
106                         nf_ct_put(ecache->ct);
107         }
108 }
109
110 int nf_conntrack_ecache_init(struct net *net)
111 {
112         net->ct.ecache = alloc_percpu(struct nf_conntrack_ecache);
113         if (!net->ct.ecache)
114                 return -ENOMEM;
115         return 0;
116 }
117
118 void nf_conntrack_ecache_fini(struct net *net)
119 {
120         free_percpu(net->ct.ecache);
121 }
122
123 int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new)
124 {
125         int ret = 0;
126         struct nf_ct_event_notifier *notify;
127
128         mutex_lock(&nf_ct_ecache_mutex);
129         notify = rcu_dereference(nf_conntrack_event_cb);
130         if (notify != NULL) {
131                 ret = -EBUSY;
132                 goto out_unlock;
133         }
134         rcu_assign_pointer(nf_conntrack_event_cb, new);
135         mutex_unlock(&nf_ct_ecache_mutex);
136         return ret;
137
138 out_unlock:
139         mutex_unlock(&nf_ct_ecache_mutex);
140         return ret;
141 }
142 EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier);
143
144 void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new)
145 {
146         struct nf_ct_event_notifier *notify;
147
148         mutex_lock(&nf_ct_ecache_mutex);
149         notify = rcu_dereference(nf_conntrack_event_cb);
150         BUG_ON(notify != new);
151         rcu_assign_pointer(nf_conntrack_event_cb, NULL);
152         mutex_unlock(&nf_ct_ecache_mutex);
153 }
154 EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);
155
156 int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new)
157 {
158         int ret = 0;
159         struct nf_exp_event_notifier *notify;
160
161         mutex_lock(&nf_ct_ecache_mutex);
162         notify = rcu_dereference(nf_expect_event_cb);
163         if (notify != NULL) {
164                 ret = -EBUSY;
165                 goto out_unlock;
166         }
167         rcu_assign_pointer(nf_expect_event_cb, new);
168         mutex_unlock(&nf_ct_ecache_mutex);
169         return ret;
170
171 out_unlock:
172         mutex_unlock(&nf_ct_ecache_mutex);
173         return ret;
174 }
175 EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier);
176
177 void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new)
178 {
179         struct nf_exp_event_notifier *notify;
180
181         mutex_lock(&nf_ct_ecache_mutex);
182         notify = rcu_dereference(nf_expect_event_cb);
183         BUG_ON(notify != new);
184         rcu_assign_pointer(nf_expect_event_cb, NULL);
185         mutex_unlock(&nf_ct_ecache_mutex);
186 }
187 EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);