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