linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / include / linux / netfilter_ipv4 / ip_conntrack.h
1 #ifndef _IP_CONNTRACK_H
2 #define _IP_CONNTRACK_H
3
4 #include <linux/netfilter/nf_conntrack_common.h>
5
6 #ifdef __KERNEL__
7 #include <linux/config.h>
8 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
9 #include <linux/bitops.h>
10 #include <linux/compiler.h>
11 #include <asm/atomic.h>
12
13 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
14 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
15 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
16 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
17
18 /* per conntrack: protocol private data */
19 union ip_conntrack_proto {
20         /* insert conntrack proto private data here */
21         struct ip_ct_gre gre;
22         struct ip_ct_sctp sctp;
23         struct ip_ct_tcp tcp;
24         struct ip_ct_icmp icmp;
25 };
26
27 union ip_conntrack_expect_proto {
28         /* insert expect proto private data here */
29 };
30
31 /* Add protocol helper include file here */
32 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
33 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
36
37 /* per conntrack: application helper private data */
38 union ip_conntrack_help {
39         /* insert conntrack helper private data (master) here */
40         struct ip_ct_pptp_master ct_pptp_info;
41         struct ip_ct_ftp_master ct_ftp_info;
42         struct ip_ct_irc_master ct_irc_info;
43 };
44
45 #ifdef CONFIG_IP_NF_NAT_NEEDED
46 #include <linux/netfilter_ipv4/ip_nat.h>
47 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
48
49 /* per conntrack: nat application helper private data */
50 union ip_conntrack_nat_help {
51         /* insert nat helper private data here */
52         struct ip_nat_pptp nat_pptp_info;
53 };
54 #endif
55
56 #include <linux/types.h>
57 #include <linux/skbuff.h>
58
59 #ifdef CONFIG_NETFILTER_DEBUG
60 #define IP_NF_ASSERT(x)                                                 \
61 do {                                                                    \
62         if (!(x))                                                       \
63                 /* Wooah!  I'm tripping my conntrack in a frenzy of     \
64                    netplay... */                                        \
65                 printk("NF_IP_ASSERT: %s:%i(%s)\n",                     \
66                        __FILE__, __LINE__, __FUNCTION__);               \
67 } while(0)
68 #else
69 #define IP_NF_ASSERT(x)
70 #endif
71
72 struct ip_conntrack_helper;
73
74 struct ip_conntrack
75 {
76         /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
77            plus 1 for any connection(s) we are `master' for */
78         struct nf_conntrack ct_general;
79
80         /* Have we seen traffic both ways yet? (bitset) */
81         unsigned long status;
82
83         /* Timer function; drops refcnt when it goes off. */
84         struct timer_list timeout;
85
86 #ifdef CONFIG_IP_NF_CT_ACCT
87         /* Accounting Information (same cache line as other written members) */
88         struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
89 #endif
90         /* If we were expected by an expectation, this will be it */
91         struct ip_conntrack *master;
92
93         /* Current number of expected connections */
94         unsigned int expecting;
95
96         /* Unique ID that identifies this conntrack*/
97         unsigned int id;
98
99         /* Helper, if any. */
100         struct ip_conntrack_helper *helper;
101
102         /* Storage reserved for other modules: */
103         union ip_conntrack_proto proto;
104
105         union ip_conntrack_help help;
106
107 #ifdef CONFIG_IP_NF_NAT_NEEDED
108         struct {
109                 struct ip_nat_info info;
110                 union ip_conntrack_nat_help help;
111 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
112         defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
113                 int masq_index;
114 #endif
115         } nat;
116 #endif /* CONFIG_IP_NF_NAT_NEEDED */
117
118 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
119         u_int32_t mark;
120 #endif
121
122
123 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
124         /* VServer context id */
125         xid_t xid[IP_CT_DIR_MAX];
126
127         /* Connection priority (pushed to skb->priority) */
128         /* Can be used directly by some classful qdiscs such as HTB */
129         u_int32_t priority;
130 #endif
131
132         /* Traversed often, so hopefully in different cacheline to top */
133         /* These are my tuples; original and reply */
134         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
135 };
136
137 struct ip_conntrack_expect
138 {
139         /* Internal linked list (global expectation list) */
140         struct list_head list;
141
142         /* We expect this tuple, with the following mask */
143         struct ip_conntrack_tuple tuple, mask;
144  
145         /* Function to call after setup and insertion */
146         void (*expectfn)(struct ip_conntrack *new,
147                          struct ip_conntrack_expect *this);
148
149         /* The conntrack of the master connection */
150         struct ip_conntrack *master;
151
152         /* Timer function; deletes the expectation. */
153         struct timer_list timeout;
154
155         /* Usage count. */
156         atomic_t use;
157
158         /* Unique ID */
159         unsigned int id;
160
161         /* Flags */
162         unsigned int flags;
163
164 #ifdef CONFIG_IP_NF_NAT_NEEDED
165         /* This is the original per-proto part, used to map the
166          * expected connection the way the recipient expects. */
167         union ip_conntrack_manip_proto saved_proto;
168         /* Direction relative to the master connection. */
169         enum ip_conntrack_dir dir;
170 #endif
171 };
172
173 #define IP_CT_EXPECT_PERMANENT  0x1
174
175 static inline struct ip_conntrack *
176 tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash *hash)
177 {
178         return container_of(hash, struct ip_conntrack,
179                             tuplehash[hash->tuple.dst.dir]);
180 }
181
182 /* get master conntrack via master expectation */
183 #define master_ct(conntr) (conntr->master)
184
185 /* Alter reply tuple (maybe alter helper). */
186 extern void
187 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
188                          const struct ip_conntrack_tuple *newreply);
189
190 /* Is this tuple taken? (ignoring any belonging to the given
191    conntrack). */
192 extern int
193 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
194                          const struct ip_conntrack *ignored_conntrack);
195
196 /* Return conntrack_info and tuple hash for given skb. */
197 static inline struct ip_conntrack *
198 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
199 {
200         *ctinfo = skb->nfctinfo;
201         return (struct ip_conntrack *)skb->nfct;
202 }
203
204 /* decrement reference count on a conntrack */
205 static inline void
206 ip_conntrack_put(struct ip_conntrack *ct)
207 {
208         IP_NF_ASSERT(ct);
209         nf_conntrack_put(&ct->ct_general);
210 }
211
212 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
213                           const struct ip_conntrack_tuple *orig);
214
215 extern void __ip_ct_refresh_acct(struct ip_conntrack *ct,
216                                  enum ip_conntrack_info ctinfo,
217                                  const struct sk_buff *skb,
218                                  unsigned long extra_jiffies,
219                                  int do_acct);
220
221 /* Refresh conntrack for this many jiffies and do accounting */
222 static inline void ip_ct_refresh_acct(struct ip_conntrack *ct, 
223                                       enum ip_conntrack_info ctinfo,
224                                       const struct sk_buff *skb,
225                                       unsigned long extra_jiffies)
226 {
227         __ip_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
228 }
229
230 /* Refresh conntrack for this many jiffies */
231 static inline void ip_ct_refresh(struct ip_conntrack *ct,
232                                  const struct sk_buff *skb,
233                                  unsigned long extra_jiffies)
234 {
235         __ip_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
236 }
237
238 /* These are for NAT.  Icky. */
239 /* Update TCP window tracking data when NAT mangles the packet */
240 extern void ip_conntrack_tcp_update(struct sk_buff *skb,
241                                     struct ip_conntrack *conntrack,
242                                     enum ip_conntrack_dir dir);
243
244 /* Call me when a conntrack is destroyed. */
245 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
246
247 /* Fake conntrack entry for untracked connections */
248 extern struct ip_conntrack ip_conntrack_untracked;
249
250 /* Returns new sk_buff, or NULL */
251 struct sk_buff *
252 ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user);
253
254 /* Iterate over all conntracks: if iter returns true, it's deleted. */
255 extern void
256 ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data),
257                       void *data);
258
259 extern struct ip_conntrack_helper *
260 __ip_conntrack_helper_find_byname(const char *);
261 extern struct ip_conntrack_helper *
262 ip_conntrack_helper_find_get(const struct ip_conntrack_tuple *tuple);
263 extern void ip_conntrack_helper_put(struct ip_conntrack_helper *helper);
264
265 extern struct ip_conntrack_protocol *
266 __ip_conntrack_proto_find(u_int8_t protocol);
267 extern struct ip_conntrack_protocol *
268 ip_conntrack_proto_find_get(u_int8_t protocol);
269 extern void ip_conntrack_proto_put(struct ip_conntrack_protocol *proto);
270
271 extern void ip_ct_remove_expectations(struct ip_conntrack *ct);
272
273 extern struct ip_conntrack *ip_conntrack_alloc(struct ip_conntrack_tuple *,
274                                                struct ip_conntrack_tuple *);
275
276 extern void ip_conntrack_free(struct ip_conntrack *ct);
277
278 extern void ip_conntrack_hash_insert(struct ip_conntrack *ct);
279
280 extern struct ip_conntrack_expect *
281 __ip_conntrack_expect_find(const struct ip_conntrack_tuple *tuple);
282
283 extern struct ip_conntrack_expect *
284 ip_conntrack_expect_find(const struct ip_conntrack_tuple *tuple);
285
286 extern struct ip_conntrack_tuple_hash *
287 __ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
288                     const struct ip_conntrack *ignored_conntrack);
289
290 extern void ip_conntrack_flush(void);
291
292 /* It's confirmed if it is, or has been in the hash table. */
293 static inline int is_confirmed(struct ip_conntrack *ct)
294 {
295         return test_bit(IPS_CONFIRMED_BIT, &ct->status);
296 }
297
298 static inline int is_dying(struct ip_conntrack *ct)
299 {
300         return test_bit(IPS_DYING_BIT, &ct->status);
301 }
302
303 extern unsigned int ip_conntrack_htable_size;
304  
305 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
306
307 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
308 #include <linux/notifier.h>
309 #include <linux/interrupt.h>
310
311 struct ip_conntrack_ecache {
312         struct ip_conntrack *ct;
313         unsigned int events;
314 };
315 DECLARE_PER_CPU(struct ip_conntrack_ecache, ip_conntrack_ecache);
316
317 #define CONNTRACK_ECACHE(x)     (__get_cpu_var(ip_conntrack_ecache).x)
318  
319 extern struct notifier_block *ip_conntrack_chain;
320 extern struct notifier_block *ip_conntrack_expect_chain;
321
322 static inline int ip_conntrack_register_notifier(struct notifier_block *nb)
323 {
324         return notifier_chain_register(&ip_conntrack_chain, nb);
325 }
326
327 static inline int ip_conntrack_unregister_notifier(struct notifier_block *nb)
328 {
329         return notifier_chain_unregister(&ip_conntrack_chain, nb);
330 }
331
332 static inline int 
333 ip_conntrack_expect_register_notifier(struct notifier_block *nb)
334 {
335         return notifier_chain_register(&ip_conntrack_expect_chain, nb);
336 }
337
338 static inline int
339 ip_conntrack_expect_unregister_notifier(struct notifier_block *nb)
340 {
341         return notifier_chain_unregister(&ip_conntrack_expect_chain, nb);
342 }
343
344 extern void ip_ct_deliver_cached_events(const struct ip_conntrack *ct);
345 extern void __ip_ct_event_cache_init(struct ip_conntrack *ct);
346
347 static inline void 
348 ip_conntrack_event_cache(enum ip_conntrack_events event,
349                          const struct sk_buff *skb)
350 {
351         struct ip_conntrack *ct = (struct ip_conntrack *)skb->nfct;
352         struct ip_conntrack_ecache *ecache;
353         
354         local_bh_disable();
355         ecache = &__get_cpu_var(ip_conntrack_ecache);
356         if (ct != ecache->ct)
357                 __ip_ct_event_cache_init(ct);
358         ecache->events |= event;
359         local_bh_enable();
360 }
361
362 static inline void ip_conntrack_event(enum ip_conntrack_events event,
363                                       struct ip_conntrack *ct)
364 {
365         if (is_confirmed(ct) && !is_dying(ct))
366                 notifier_call_chain(&ip_conntrack_chain, event, ct);
367 }
368
369 static inline void 
370 ip_conntrack_expect_event(enum ip_conntrack_expect_events event,
371                           struct ip_conntrack_expect *exp)
372 {
373         notifier_call_chain(&ip_conntrack_expect_chain, event, exp);
374 }
375 #else /* CONFIG_IP_NF_CONNTRACK_EVENTS */
376 static inline void ip_conntrack_event_cache(enum ip_conntrack_events event, 
377                                             const struct sk_buff *skb) {}
378 static inline void ip_conntrack_event(enum ip_conntrack_events event, 
379                                       struct ip_conntrack *ct) {}
380 static inline void ip_ct_deliver_cached_events(const struct ip_conntrack *ct) {}
381 static inline void 
382 ip_conntrack_expect_event(enum ip_conntrack_expect_events event, 
383                           struct ip_conntrack_expect *exp) {}
384 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
385
386 #ifdef CONFIG_IP_NF_NAT_NEEDED
387 static inline int ip_nat_initialized(struct ip_conntrack *conntrack,
388                                      enum ip_nat_manip_type manip)
389 {
390         if (manip == IP_NAT_MANIP_SRC)
391                 return test_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status);
392         return test_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status);
393 }
394 #endif /* CONFIG_IP_NF_NAT_NEEDED */
395
396 #endif /* __KERNEL__ */
397 #endif /* _IP_CONNTRACK_H */