netfilter: conntrack: move event caching to conntrack extension infrastructure
[safe/jmp/linux-2.6] / 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 /* Connection tracking event types */
16 enum ip_conntrack_events
17 {
18         IPCT_NEW                = 0,    /* new conntrack */
19         IPCT_RELATED            = 1,    /* related conntrack */
20         IPCT_DESTROY            = 2,    /* destroyed conntrack */
21         IPCT_STATUS             = 3,    /* status has changed */
22         IPCT_PROTOINFO          = 4,    /* protocol information has changed */
23         IPCT_HELPER             = 5,    /* new helper has been set */
24         IPCT_MARK               = 6,    /* new mark has been set */
25         IPCT_NATSEQADJ          = 7,    /* NAT is doing sequence adjustment */
26         IPCT_SECMARK            = 8,    /* new security mark has been set */
27 };
28
29 enum ip_conntrack_expect_events {
30         IPEXP_NEW               = 0,    /* new expectation */
31 };
32
33 struct nf_conntrack_ecache {
34         unsigned long cache;            /* bitops want long */
35 };
36
37 static inline struct nf_conntrack_ecache *
38 nf_ct_ecache_find(const struct nf_conn *ct)
39 {
40         return nf_ct_ext_find(ct, NF_CT_EXT_ECACHE);
41 }
42
43 static inline struct nf_conntrack_ecache *
44 nf_ct_ecache_ext_add(struct nf_conn *ct, gfp_t gfp)
45 {
46         struct net *net = nf_ct_net(ct);
47
48         if (!net->ct.sysctl_events)
49                 return NULL;
50
51         return nf_ct_ext_add(ct, NF_CT_EXT_ECACHE, gfp);
52 };
53
54 #ifdef CONFIG_NF_CONNTRACK_EVENTS
55 /* This structure is passed to event handler */
56 struct nf_ct_event {
57         struct nf_conn *ct;
58         u32 pid;
59         int report;
60 };
61
62 struct nf_ct_event_notifier {
63         int (*fcn)(unsigned int events, struct nf_ct_event *item);
64 };
65
66 extern struct nf_ct_event_notifier *nf_conntrack_event_cb;
67 extern int nf_conntrack_register_notifier(struct nf_ct_event_notifier *nb);
68 extern void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *nb);
69
70 extern void nf_ct_deliver_cached_events(struct nf_conn *ct);
71
72 static inline void
73 nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
74 {
75         struct nf_conntrack_ecache *e;
76
77         if (nf_conntrack_event_cb == NULL)
78                 return;
79
80         e = nf_ct_ecache_find(ct);
81         if (e == NULL)
82                 return;
83
84         set_bit(event, &e->cache);
85 }
86
87 static inline void
88 nf_conntrack_eventmask_report(unsigned int eventmask,
89                               struct nf_conn *ct,
90                               u32 pid,
91                               int report)
92 {
93         struct net *net = nf_ct_net(ct);
94         struct nf_ct_event_notifier *notify;
95
96         rcu_read_lock();
97         notify = rcu_dereference(nf_conntrack_event_cb);
98         if (notify == NULL)
99                 goto out_unlock;
100
101         if (!net->ct.sysctl_events)
102                 goto out_unlock;
103
104         if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
105                 struct nf_ct_event item = {
106                         .ct     = ct,
107                         .pid    = pid,
108                         .report = report
109                 };
110                 notify->fcn(eventmask, &item);
111         }
112 out_unlock:
113         rcu_read_unlock();
114 }
115
116 static inline void
117 nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct,
118                           u32 pid, int report)
119 {
120         nf_conntrack_eventmask_report(1 << event, ct, pid, report);
121 }
122
123 static inline void
124 nf_conntrack_event(enum ip_conntrack_events event, struct nf_conn *ct)
125 {
126         nf_conntrack_eventmask_report(1 << event, ct, 0, 0);
127 }
128
129 struct nf_exp_event {
130         struct nf_conntrack_expect *exp;
131         u32 pid;
132         int report;
133 };
134
135 struct nf_exp_event_notifier {
136         int (*fcn)(unsigned int events, struct nf_exp_event *item);
137 };
138
139 extern struct nf_exp_event_notifier *nf_expect_event_cb;
140 extern int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *nb);
141 extern void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *nb);
142
143 static inline void
144 nf_ct_expect_event_report(enum ip_conntrack_expect_events event,
145                           struct nf_conntrack_expect *exp,
146                           u32 pid,
147                           int report)
148 {
149         struct net *net = nf_ct_exp_net(exp);
150         struct nf_exp_event_notifier *notify;
151
152         rcu_read_lock();
153         notify = rcu_dereference(nf_expect_event_cb);
154         if (notify == NULL)
155                 goto out_unlock;
156
157         if (!net->ct.sysctl_events)
158                 goto out_unlock;
159
160         {
161                 struct nf_exp_event item = {
162                         .exp    = exp,
163                         .pid    = pid,
164                         .report = report
165                 };
166                 notify->fcn(1 << event, &item);
167         }
168 out_unlock:
169         rcu_read_unlock();
170 }
171
172 static inline void
173 nf_ct_expect_event(enum ip_conntrack_expect_events event,
174                    struct nf_conntrack_expect *exp)
175 {
176         nf_ct_expect_event_report(event, exp, 0, 0);
177 }
178
179 extern int nf_conntrack_ecache_init(struct net *net);
180 extern void nf_conntrack_ecache_fini(struct net *net);
181
182 #else /* CONFIG_NF_CONNTRACK_EVENTS */
183
184 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
185                                             struct nf_conn *ct) {}
186 static inline void nf_conntrack_eventmask_report(unsigned int eventmask,
187                                                  struct nf_conn *ct,
188                                                  u32 pid,
189                                                  int report) {}
190 static inline void nf_conntrack_event(enum ip_conntrack_events event,
191                                       struct nf_conn *ct) {}
192 static inline void nf_conntrack_event_report(enum ip_conntrack_events event,
193                                              struct nf_conn *ct,
194                                              u32 pid,
195                                              int report) {}
196 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
197 static inline void nf_ct_expect_event(enum ip_conntrack_expect_events event,
198                                       struct nf_conntrack_expect *exp) {}
199 static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e,
200                                              struct nf_conntrack_expect *exp,
201                                              u32 pid,
202                                              int report) {}
203
204 static inline int nf_conntrack_ecache_init(struct net *net)
205 {
206         return 0;
207 }
208
209 static inline void nf_conntrack_ecache_fini(struct net *net)
210 {
211 }
212 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
213
214 #endif /*_NF_CONNTRACK_ECACHE_H*/
215