This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / include / linux / netfilter_ipv4 / ip_conntrack.h
1 #ifndef _IP_CONNTRACK_H
2 #define _IP_CONNTRACK_H
3 /* Connection state tracking for netfilter.  This is separated from,
4    but required by, the NAT layer; it can also be used by an iptables
5    extension. */
6
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 <linux/vserver/context.h>
12 #include <asm/atomic.h>
13
14 enum ip_conntrack_info
15 {
16         /* Part of an established connection (either direction). */
17         IP_CT_ESTABLISHED,
18
19         /* Like NEW, but related to an existing connection, or ICMP error
20            (in either direction). */
21         IP_CT_RELATED,
22
23         /* Started a new connection to track (only
24            IP_CT_DIR_ORIGINAL); may be a retransmission. */
25         IP_CT_NEW,
26
27         /* >= this indicates reply direction */
28         IP_CT_IS_REPLY,
29
30         /* Number of distinct IP_CT types (no NEW in reply dirn). */
31         IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1
32 };
33
34 /* Bitset representing status of connection. */
35 enum ip_conntrack_status {
36         /* It's an expected connection: bit 0 set.  This bit never changed */
37         IPS_EXPECTED_BIT = 0,
38         IPS_EXPECTED = (1 << IPS_EXPECTED_BIT),
39
40         /* We've seen packets both ways: bit 1 set.  Can be set, not unset. */
41         IPS_SEEN_REPLY_BIT = 1,
42         IPS_SEEN_REPLY = (1 << IPS_SEEN_REPLY_BIT),
43
44         /* Conntrack should never be early-expired. */
45         IPS_ASSURED_BIT = 2,
46         IPS_ASSURED = (1 << IPS_ASSURED_BIT),
47
48         /* Connection is confirmed: originating packet has left box */
49         IPS_CONFIRMED_BIT = 3,
50         IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
51 };
52
53 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
54 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
55 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
56 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
57
58 /* per conntrack: protocol private data */
59 union ip_conntrack_proto {
60         /* insert conntrack proto private data here */
61         struct ip_ct_gre gre;
62         struct ip_ct_sctp sctp;
63         struct ip_ct_tcp tcp;
64         struct ip_ct_icmp icmp;
65 };
66
67 union ip_conntrack_expect_proto {
68         /* insert expect proto private data here */
69         struct ip_ct_gre_expect gre;
70 };
71
72 /* Add protocol helper include file here */
73 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
74 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
75 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
76 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
77
78 /* per expectation: application helper private data */
79 union ip_conntrack_expect_help {
80         /* insert conntrack helper private data (expect) here */
81         struct ip_ct_pptp_expect exp_pptp_info;
82         struct ip_ct_amanda_expect exp_amanda_info;
83         struct ip_ct_ftp_expect exp_ftp_info;
84         struct ip_ct_irc_expect exp_irc_info;
85
86 #ifdef CONFIG_IP_NF_NAT_NEEDED
87         union {
88                 /* insert nat helper private data (expect) here */
89         } nat;
90 #endif
91 };
92
93 /* per conntrack: application helper private data */
94 union ip_conntrack_help {
95         /* insert conntrack helper private data (master) here */
96         struct ip_ct_pptp_master ct_pptp_info;
97         struct ip_ct_ftp_master ct_ftp_info;
98         struct ip_ct_irc_master ct_irc_info;
99 };
100
101 #ifdef CONFIG_IP_NF_NAT_NEEDED
102 #include <linux/netfilter_ipv4/ip_nat.h>
103 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
104
105 /* per conntrack: nat application helper private data */
106 union ip_conntrack_nat_help {
107         /* insert nat helper private data here */
108         struct ip_nat_pptp nat_pptp_info;
109 };
110 #endif
111
112 #ifdef __KERNEL__
113
114 #include <linux/types.h>
115 #include <linux/skbuff.h>
116
117 #ifdef CONFIG_NETFILTER_DEBUG
118 #define IP_NF_ASSERT(x)                                                 \
119 do {                                                                    \
120         if (!(x))                                                       \
121                 /* Wooah!  I'm tripping my conntrack in a frenzy of     \
122                    netplay... */                                        \
123                 printk("NF_IP_ASSERT: %s:%i(%s)\n",                     \
124                        __FILE__, __LINE__, __FUNCTION__);               \
125 } while(0)
126 #else
127 #define IP_NF_ASSERT(x)
128 #endif
129
130 struct ip_conntrack_expect
131 {
132         /* Internal linked list (global expectation list) */
133         struct list_head list;
134
135         /* reference count */
136         atomic_t use;
137
138         /* expectation list for this master */
139         struct list_head expected_list;
140
141         /* The conntrack of the master connection */
142         struct ip_conntrack *expectant;
143
144         /* The conntrack of the sibling connection, set after
145          * expectation arrived */
146         struct ip_conntrack *sibling;
147
148         /* Tuple saved for conntrack */
149         struct ip_conntrack_tuple ct_tuple;
150
151         /* Timer function; deletes the expectation. */
152         struct timer_list timeout;
153
154         /* Data filled out by the conntrack helpers follow: */
155
156         /* We expect this tuple, with the following mask */
157         struct ip_conntrack_tuple tuple, mask;
158
159         /* Function to call after setup and insertion */
160         int (*expectfn)(struct ip_conntrack *new);
161
162         /* At which sequence number did this expectation occur */
163         u_int32_t seq;
164   
165         union ip_conntrack_expect_proto proto;
166
167         union ip_conntrack_expect_help help;
168 };
169
170 struct ip_conntrack_counter
171 {
172         u_int64_t packets;
173         u_int64_t bytes;
174 };
175
176 struct ip_conntrack_helper;
177
178 struct ip_conntrack
179 {
180         /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
181            plus 1 for any connection(s) we are `master' for */
182         struct nf_conntrack ct_general;
183
184         /* Have we seen traffic both ways yet? (bitset) */
185         unsigned long status;
186
187         /* Timer function; drops refcnt when it goes off. */
188         struct timer_list timeout;
189
190 #ifdef CONFIG_IP_NF_CT_ACCT
191         /* Accounting Information (same cache line as other written members) */
192         struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
193 #endif
194
195         /* If we're expecting another related connection, this will be
196            in expected linked list */
197         struct list_head sibling_list;
198         
199         /* Current number of expected connections */
200         unsigned int expecting;
201
202         /* If we were expected by an expectation, this will be it */
203         struct ip_conntrack_expect *master;
204
205         /* Helper, if any. */
206         struct ip_conntrack_helper *helper;
207
208         /* Storage reserved for other modules: */
209         union ip_conntrack_proto proto;
210
211         union ip_conntrack_help help;
212
213 #ifdef CONFIG_IP_NF_NAT_NEEDED
214         struct {
215                 struct ip_nat_info info;
216                 union ip_conntrack_nat_help help;
217 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
218         defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
219                 int masq_index;
220 #endif
221         } nat;
222 #endif /* CONFIG_IP_NF_NAT_NEEDED */
223
224 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
225         /* VServer context id */
226         xid_t xid[IP_CT_DIR_MAX];
227 #endif
228
229         /* Traversed often, so hopefully in different cacheline to top */
230         /* These are my tuples; original and reply */
231         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
232 };
233
234 /* get master conntrack via master expectation */
235 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
236
237 /* Alter reply tuple (maybe alter helper).  If it's already taken,
238    return 0 and don't do alteration. */
239 extern int
240 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
241                          const struct ip_conntrack_tuple *newreply);
242
243 /* Is this tuple taken? (ignoring any belonging to the given
244    conntrack). */
245 extern int
246 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
247                          const struct ip_conntrack *ignored_conntrack);
248
249 /* Return conntrack_info and tuple hash for given skb. */
250 static inline struct ip_conntrack *
251 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
252 {
253         *ctinfo = skb->nfctinfo;
254         return (struct ip_conntrack *)skb->nfct;
255 }
256
257 /* decrement reference count on a conntrack */
258 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
259
260 /* find unconfirmed expectation based on tuple */
261 struct ip_conntrack_expect *
262 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
263
264 /* decrement reference count on an expectation */
265 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
266
267 /* call to create an explicit dependency on ip_conntrack. */
268 extern void need_ip_conntrack(void);
269
270 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
271                           const struct ip_conntrack_tuple *orig);
272
273 /* Refresh conntrack for this many jiffies */
274 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
275                                enum ip_conntrack_info ctinfo,
276                                const struct sk_buff *skb,
277                                unsigned long extra_jiffies);
278
279 /* These are for NAT.  Icky. */
280 /* Update TCP window tracking data when NAT mangles the packet */
281 extern int ip_conntrack_tcp_update(struct sk_buff *skb,
282                                    struct ip_conntrack *conntrack,
283                                    int dir);
284
285 /* Call me when a conntrack is destroyed. */
286 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
287
288 /* Fake conntrack entry for untracked connections */
289 extern struct ip_conntrack ip_conntrack_untracked;
290
291 extern int ip_ct_no_defrag;
292 /* Returns new sk_buff, or NULL */
293 struct sk_buff *
294 ip_ct_gather_frags(struct sk_buff *skb);
295
296 /* Delete all conntracks which match. */
297 extern void
298 ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
299                         void *data);
300
301 /* It's confirmed if it is, or has been in the hash table. */
302 static inline int is_confirmed(struct ip_conntrack *ct)
303 {
304         return test_bit(IPS_CONFIRMED_BIT, &ct->status);
305 }
306
307 extern unsigned int ip_conntrack_htable_size;
308  
309 struct ip_conntrack_stat
310 {
311         unsigned int searched;
312         unsigned int found;
313         unsigned int new;
314         unsigned int invalid;
315         unsigned int ignore;
316         unsigned int delete;
317         unsigned int delete_list;
318         unsigned int insert;
319         unsigned int insert_failed;
320         unsigned int drop;
321         unsigned int early_drop;
322         unsigned int error;
323         unsigned int expect_new;
324         unsigned int expect_create;
325         unsigned int expect_delete;
326 };
327
328 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
329
330 /* eg. PROVIDES_CONNTRACK(ftp); */
331 #define PROVIDES_CONNTRACK(name)                        \
332         int needs_ip_conntrack_##name;                  \
333         EXPORT_SYMBOL(needs_ip_conntrack_##name)
334
335 /*. eg. NEEDS_CONNTRACK(ftp); */
336 #define NEEDS_CONNTRACK(name)                                           \
337         extern int needs_ip_conntrack_##name;                           \
338         static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name
339
340 #endif /* __KERNEL__ */
341 #endif /* _IP_CONNTRACK_H */