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
57 /* per conntrack: protocol private data */
58 union ip_conntrack_proto {
59         /* insert conntrack proto private data here */
60         struct ip_ct_gre gre;
61         struct ip_ct_tcp tcp;
62         struct ip_ct_icmp icmp;
63 };
64
65 union ip_conntrack_expect_proto {
66         /* insert expect proto private data here */
67         struct ip_ct_gre_expect gre;
68 };
69
70 /* Add protocol helper include file here */
71 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
72 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
73 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
74 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
75
76 /* per expectation: application helper private data */
77 union ip_conntrack_expect_help {
78         /* insert conntrack helper private data (expect) here */
79         struct ip_ct_pptp_expect exp_pptp_info;
80         struct ip_ct_amanda_expect exp_amanda_info;
81         struct ip_ct_ftp_expect exp_ftp_info;
82         struct ip_ct_irc_expect exp_irc_info;
83
84 #ifdef CONFIG_IP_NF_NAT_NEEDED
85         union {
86                 /* insert nat helper private data (expect) here */
87         } nat;
88 #endif
89 };
90
91 /* per conntrack: application helper private data */
92 union ip_conntrack_help {
93         /* insert conntrack helper private data (master) here */
94         struct ip_ct_pptp_master ct_pptp_info;
95         struct ip_ct_ftp_master ct_ftp_info;
96         struct ip_ct_irc_master ct_irc_info;
97 };
98
99 #ifdef CONFIG_IP_NF_NAT_NEEDED
100 #include <linux/netfilter_ipv4/ip_nat.h>
101 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
102
103 /* per conntrack: nat application helper private data */
104 union ip_conntrack_nat_help {
105         /* insert nat helper private data here */
106         struct ip_nat_pptp nat_pptp_info;
107 };
108 #endif
109
110 #ifdef __KERNEL__
111
112 #include <linux/types.h>
113 #include <linux/skbuff.h>
114
115 #ifdef CONFIG_NETFILTER_DEBUG
116 #define IP_NF_ASSERT(x)                                                 \
117 do {                                                                    \
118         if (!(x))                                                       \
119                 /* Wooah!  I'm tripping my conntrack in a frenzy of     \
120                    netplay... */                                        \
121                 printk("NF_IP_ASSERT: %s:%i(%s)\n",                     \
122                        __FILE__, __LINE__, __FUNCTION__);               \
123 } while(0)
124 #else
125 #define IP_NF_ASSERT(x)
126 #endif
127
128 struct ip_conntrack_expect
129 {
130         /* Internal linked list (global expectation list) */
131         struct list_head list;
132
133         /* reference count */
134         atomic_t use;
135
136         /* expectation list for this master */
137         struct list_head expected_list;
138
139         /* The conntrack of the master connection */
140         struct ip_conntrack *expectant;
141
142         /* The conntrack of the sibling connection, set after
143          * expectation arrived */
144         struct ip_conntrack *sibling;
145
146         /* Tuple saved for conntrack */
147         struct ip_conntrack_tuple ct_tuple;
148
149         /* Timer function; deletes the expectation. */
150         struct timer_list timeout;
151
152         /* Data filled out by the conntrack helpers follow: */
153
154         /* We expect this tuple, with the following mask */
155         struct ip_conntrack_tuple tuple, mask;
156
157         /* Function to call after setup and insertion */
158         int (*expectfn)(struct ip_conntrack *new);
159
160         /* At which sequence number did this expectation occur */
161         u_int32_t seq;
162   
163         union ip_conntrack_expect_proto proto;
164
165         union ip_conntrack_expect_help help;
166 };
167
168 struct ip_conntrack_counter
169 {
170         u_int64_t packets;
171         u_int64_t bytes;
172 };
173
174 struct ip_conntrack_helper;
175
176 struct ip_conntrack
177 {
178         /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
179            plus 1 for any connection(s) we are `master' for */
180         struct nf_conntrack ct_general;
181
182         /* These are my tuples; original and reply */
183         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
184
185         /* Have we seen traffic both ways yet? (bitset) */
186         unsigned long status;
187
188         /* Timer function; drops refcnt when it goes off. */
189         struct timer_list timeout;
190
191 #ifdef CONFIG_IP_NF_CT_ACCT
192         /* Accounting Information (same cache line as other written members) */
193         struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
194 #endif
195
196         /* If we're expecting another related connection, this will be
197            in expected linked list */
198         struct list_head sibling_list;
199         
200         /* Current number of expected connections */
201         unsigned int expecting;
202
203         /* If we were expected by an expectation, this will be it */
204         struct ip_conntrack_expect *master;
205
206         /* Helper, if any. */
207         struct ip_conntrack_helper *helper;
208
209         /* Our various nf_ct_info structs specify *what* relation this
210            packet has to the conntrack */
211         struct nf_ct_info infos[IP_CT_NUMBER];
212
213         /* Storage reserved for other modules: */
214
215         union ip_conntrack_proto proto;
216
217         union ip_conntrack_help help;
218
219 #ifdef CONFIG_IP_NF_NAT_NEEDED
220         struct {
221                 struct ip_nat_info info;
222                 union ip_conntrack_nat_help help;
223 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
224         defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
225                 int masq_index;
226 #endif
227         } nat;
228 #endif /* CONFIG_IP_NF_NAT_NEEDED */
229
230         /* VServer context id */
231         xid_t xid[IP_CT_DIR_MAX];
232
233 };
234
235 /* get master conntrack via master expectation */
236 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
237
238 /* Alter reply tuple (maybe alter helper).  If it's already taken,
239    return 0 and don't do alteration. */
240 extern int
241 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
242                          const struct ip_conntrack_tuple *newreply);
243
244 /* Is this tuple taken? (ignoring any belonging to the given
245    conntrack). */
246 extern int
247 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
248                          const struct ip_conntrack *ignored_conntrack);
249
250 /* Return conntrack_info and tuple hash for given skb. */
251 extern struct ip_conntrack *
252 ip_conntrack_get(struct sk_buff *skb, enum ip_conntrack_info *ctinfo);
253
254 /* decrement reference count on a conntrack */
255 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
256
257 /* find unconfirmed expectation based on tuple */
258 struct ip_conntrack_expect *
259 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
260
261 /* decrement reference count on an expectation */
262 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
263
264 /* call to create an explicit dependency on ip_conntrack. */
265 extern void need_ip_conntrack(void);
266
267 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
268                           const struct ip_conntrack_tuple *orig);
269
270 /* Refresh conntrack for this many jiffies */
271 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
272                                enum ip_conntrack_info ctinfo,
273                                const struct sk_buff *skb,
274                                unsigned long extra_jiffies);
275
276 /* These are for NAT.  Icky. */
277 /* Call me when a conntrack is destroyed. */
278 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
279
280 /* Fake conntrack entry for untracked connections */
281 extern struct ip_conntrack ip_conntrack_untracked;
282
283 /* Returns new sk_buff, or NULL */
284 struct sk_buff *
285 ip_ct_gather_frags(struct sk_buff *skb);
286
287 /* Delete all conntracks which match. */
288 extern void
289 ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
290                         void *data);
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 extern unsigned int ip_conntrack_htable_size;
299
300 /* eg. PROVIDES_CONNTRACK(ftp); */
301 #define PROVIDES_CONNTRACK(name)                        \
302         int needs_ip_conntrack_##name;                  \
303         EXPORT_SYMBOL(needs_ip_conntrack_##name)
304
305 /*. eg. NEEDS_CONNTRACK(ftp); */
306 #define NEEDS_CONNTRACK(name)                                           \
307         extern int needs_ip_conntrack_##name;                           \
308         static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name
309
310 #endif /* __KERNEL__ */
311 #endif /* _IP_CONNTRACK_H */