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