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