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