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