4ed90ad6c18f618985f5f62f6acb1675a74b8c86
[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_IP_NF_CONNTRACK_MARK)
225         unsigned long mark;
226 #endif
227
228 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
229         /* VServer context id */
230         xid_t xid[IP_CT_DIR_MAX];
231 #endif
232
233         /* Traversed often, so hopefully in different cacheline to top */
234         /* These are my tuples; original and reply */
235         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
236 };
237
238 /* get master conntrack via master expectation */
239 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
240
241 /* Alter reply tuple (maybe alter helper).  If it's already taken,
242    return 0 and don't do alteration. */
243 extern int
244 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
245                          const struct ip_conntrack_tuple *newreply);
246
247 /* Is this tuple taken? (ignoring any belonging to the given
248    conntrack). */
249 extern int
250 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
251                          const struct ip_conntrack *ignored_conntrack);
252
253 /* Return conntrack_info and tuple hash for given skb. */
254 static inline struct ip_conntrack *
255 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
256 {
257         *ctinfo = skb->nfctinfo;
258         return (struct ip_conntrack *)skb->nfct;
259 }
260
261 /* decrement reference count on a conntrack */
262 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
263
264 /* find unconfirmed expectation based on tuple */
265 struct ip_conntrack_expect *
266 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
267
268 /* decrement reference count on an expectation */
269 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
270
271 /* call to create an explicit dependency on ip_conntrack. */
272 extern void need_ip_conntrack(void);
273
274 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
275                           const struct ip_conntrack_tuple *orig);
276
277 /* Refresh conntrack for this many jiffies */
278 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
279                                enum ip_conntrack_info ctinfo,
280                                const struct sk_buff *skb,
281                                unsigned long extra_jiffies);
282
283 /* These are for NAT.  Icky. */
284 /* Update TCP window tracking data when NAT mangles the packet */
285 extern int ip_conntrack_tcp_update(struct sk_buff *skb,
286                                    struct ip_conntrack *conntrack,
287                                    int dir);
288
289 /* Call me when a conntrack is destroyed. */
290 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
291
292 /* Fake conntrack entry for untracked connections */
293 extern struct ip_conntrack ip_conntrack_untracked;
294
295 extern int ip_ct_no_defrag;
296 /* Returns new sk_buff, or NULL */
297 struct sk_buff *
298 ip_ct_gather_frags(struct sk_buff *skb);
299
300 /* Delete all conntracks which match. */
301 extern void
302 ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
303                         void *data);
304
305 /* It's confirmed if it is, or has been in the hash table. */
306 static inline int is_confirmed(struct ip_conntrack *ct)
307 {
308         return test_bit(IPS_CONFIRMED_BIT, &ct->status);
309 }
310
311 extern unsigned int ip_conntrack_htable_size;
312  
313 struct ip_conntrack_stat
314 {
315         unsigned int searched;
316         unsigned int found;
317         unsigned int new;
318         unsigned int invalid;
319         unsigned int ignore;
320         unsigned int delete;
321         unsigned int delete_list;
322         unsigned int insert;
323         unsigned int insert_failed;
324         unsigned int drop;
325         unsigned int early_drop;
326         unsigned int error;
327         unsigned int expect_new;
328         unsigned int expect_create;
329         unsigned int expect_delete;
330 };
331
332 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
333
334 /* eg. PROVIDES_CONNTRACK(ftp); */
335 #define PROVIDES_CONNTRACK(name)                        \
336         int needs_ip_conntrack_##name;                  \
337         EXPORT_SYMBOL(needs_ip_conntrack_##name)
338
339 /*. eg. NEEDS_CONNTRACK(ftp); */
340 #define NEEDS_CONNTRACK(name)                                           \
341         extern int needs_ip_conntrack_##name;                           \
342         static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name
343
344 #endif /* __KERNEL__ */
345 #endif /* _IP_CONNTRACK_H */