Merge branch 'viafb-next' of git://github.com/schandinat/linux-2.6
[pandora-kernel.git] / include / net / netfilter / nf_conntrack_ecache.h
1 /*
2  * connection tracking event cache.
3  */
4
5 #ifndef _NF_CONNTRACK_ECACHE_H
6 #define _NF_CONNTRACK_ECACHE_H
7 #include <net/netfilter/nf_conntrack.h>
8
9 #include <net/net_namespace.h>
10 #include <net/netfilter/nf_conntrack_expect.h>
11 #include <linux/netfilter/nf_conntrack_common.h>
12 #include <linux/netfilter/nf_conntrack_tuple_common.h>
13 #include <net/netfilter/nf_conntrack_extend.h>
14
15 struct nf_conntrack_ecache {
16         unsigned long cache;    /* bitops want long */
17         unsigned long missed;   /* missed events */
18         u16 ctmask;             /* bitmask of ct events to be delivered */
19         u16 expmask;            /* bitmask of expect events to be delivered */
20         u32 pid;                /* netlink pid of destroyer */
21 };
22
23 static inline struct nf_conntrack_ecache *
24 nf_ct_ecache_find(const struct nf_conn *ct)
25 {
26 #ifdef CONFIG_NF_CONNTRACK_EVENTS
27         return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
28 #else
29         return NULL;
30 #endif
31 }
32
33 static inline struct nf_conntrack_ecache *
34 nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp)
35 {
36 #ifdef CONFIG_NF_CONNTRACK_EVENTS
37         struct net *net = nf_ct_net(ct);
38         struct nf_conntrack_ecache *e;
39
40         if (!ctmask && !expmask && net->ct.sysctl_events) {
41                 ctmask = ~0;
42                 expmask = ~0;
43         }
44         if (!ctmask && !expmask)
45                 return NULL;
46
47         e = nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
48         if (e) {
49                 e->ctmask  = ctmask;
50                 e->expmask = expmask;
51         }
52         return e;
53 #else
54         return NULL;
55 #endif
56 };
57
58 #ifdef CONFIG_NF_CONNTRACK_EVENTS
59 /* This structure is passed to event handler */
60 struct nf_ct_event {
61         struct nf_conn *ct;
62         u32 pid;
63         int report;
64 };
65
66 struct nf_ct_event_notifier {
67         int (*fcn)(unsigned int events, struct nf_ct_event *item);
68 };
69
70 extern struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb;
71 extern int nf_conntrack_register_notifier(struct nf_ct_event_notifier *nb);
72 extern void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *nb);
73
74 extern void nf_ct_deliver_cached_events(struct nf_conn *ct);
75
76 static inline void
77 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
78 {
79         struct nf_conntrack_ecache *e;
80
81         if (nf_conntrack_event_cb == NULL)
82                 return;
83
84         e = nf_ct_ecache_find(ct);
85         if (e == NULL)
86                 return;
87
88         set_bit(event, &e->cache);
89 }
90
91 static inline int
92 nf_conntrack_eventmask_report(unsigned int eventmask,
93                               struct nf_conn *ct,
94                               u32 pid,
95                               int report)
96 {
97         int ret = 0;
98         struct nf_ct_event_notifier *notify;
99         struct nf_conntrack_ecache *e;
100
101         rcu_read_lock();
102         notify = rcu_dereference(nf_conntrack_event_cb);
103         if (notify == NULL)
104                 goto out_unlock;
105
106         e = nf_ct_ecache_find(ct);
107         if (e == NULL)
108                 goto out_unlock;
109
110         if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
111                 struct nf_ct_event item = {
112                         .ct     = ct,
113                         .pid    = e->pid ? e->pid : pid,
114                         .report = report
115                 };
116                 /* This is a resent of a destroy event? If so, skip missed */
117                 unsigned long missed = e->pid ? 0 : e->missed;
118
119                 if (!((eventmask | missed) & e->ctmask))
120                         goto out_unlock;
121
122                 ret = notify->fcn(eventmask | missed, &item);
123                 if (unlikely(ret < 0 || missed)) {
124                         spin_lock_bh(&ct->lock);
125                         if (ret < 0) {
126                                 /* This is a destroy event that has been
127                                  * triggered by a process, we store the PID
128                                  * to include it in the retransmission. */
129                                 if (eventmask & (1 << IPCT_DESTROY) &&
130                                     e->pid == 0 && pid != 0)
131                                         e->pid = pid;
132                                 else
133                                         e->missed |= eventmask;
134                         } else
135                                 e->missed &= ~missed;
136                         spin_unlock_bh(&ct->lock);
137                 }
138         }
139 out_unlock:
140         rcu_read_unlock();
141         return ret;
142 }
143
144 static inline int
145 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
146                           u32 pid, int report)
147 {
148         return nf_conntrack_eventmask_report(1 << event, ct, pid, report);
149 }
150
151 static inline int
152 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
153 {
154         return nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
155 }
156
157 struct nf_exp_event {
158         struct nf_conntrack_expect *exp;
159         u32 pid;
160         int report;
161 };
162
163 struct nf_exp_event_notifier {
164         int (*fcn)(unsigned int events, struct nf_exp_event *item);
165 };
166
167 extern struct nf_exp_event_notifier __rcu *nf_expect_event_cb;
168 extern int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *nb);
169 extern void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *nb);
170
171 static inline void
172 nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
173                           struct nf_conntrack_expect *exp,
174                           u32 pid,
175                           int report)
176 {
177         struct nf_exp_event_notifier *notify;
178         struct nf_conntrack_ecache *e;
179
180         rcu_read_lock();
181         notify = rcu_dereference(nf_expect_event_cb);
182         if (notify == NULL)
183                 goto out_unlock;
184
185         e = nf_ct_ecache_find(exp->master);
186         if (e == NULL)
187                 goto out_unlock;
188
189         if (e->expmask & (1 << event)) {
190                 struct nf_exp_event item = {
191                         .exp    = exp,
192                         .pid    = pid,
193                         .report = report
194                 };
195                 notify->fcn(1 << event, &item);
196         }
197 out_unlock:
198         rcu_read_unlock();
199 }
200
201 static inline void
202 nf_ct_expect_event(enum ip_conntrack_expect_events event,
203                    struct nf_conntrack_expect *exp)
204 {
205         nf_ct_expect_event_report(event, exp, 0, 0);
206 }
207
208 extern int nf_conntrack_ecache_init(struct net *net);
209 extern void nf_conntrack_ecache_fini(struct net *net);
210
211 #else /* CONFIG_NF_CONNTRACK_EVENTS */
212
213 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
214                                             struct nf_conn *ct) {}
215 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
216                                                 struct nf_conn *ct,
217                                                 u32 pid,
218                                                 int report) { return 0; }
219 static inline int nf_conntrack_event(enum ip_conntrack_events event,
220                                      struct nf_conn *ct) { return 0; }
221 static inline int nf_conntrack_event_report(enum ip_conntrack_events event,
222                                             struct nf_conn *ct,
223                                             u32 pid,
224                                             int report) { return 0; }
225 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
226 static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
227                                       struct nf_conntrack_expect *exp) {}
228 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
229                                              struct nf_conntrack_expect *exp,
230                                              u32 pid,
231                                              int report) {}
232
233 static inline int nf_conntrack_ecache_init(struct net *net)
234 {
235         return 0;
236 }
237
238 static inline void nf_conntrack_ecache_fini(struct net *net)
239 {
240 }
241 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
242
243 #endif /*_NF_CONNTRACK_ECACHE_H*/
244