41fa610622d87aceb5a48c8b7502e52500546642
[linux-flexiantxendom0-3.2.10.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
55 /* per conntrack: protocol private data */
56 union ip_conntrack_proto {
57         /* insert conntrack proto private data here */
58         struct ip_ct_tcp tcp;
59         struct ip_ct_icmp icmp;
60 };
61
62 union ip_conntrack_expect_proto {
63         /* insert expect proto private data here */
64 };
65
66 /* Add protocol helper include file here */
67 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
68 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
69 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
70
71 /* per expectation: application helper private data */
72 union ip_conntrack_expect_help {
73         /* insert conntrack helper private data (expect) here */
74         struct ip_ct_amanda_expect exp_amanda_info;
75         struct ip_ct_ftp_expect exp_ftp_info;
76         struct ip_ct_irc_expect exp_irc_info;
77
78 #ifdef CONFIG_IP_NF_NAT_NEEDED
79         union {
80                 /* insert nat helper private data (expect) here */
81         } nat;
82 #endif
83 };
84
85 /* per conntrack: application helper private data */
86 union ip_conntrack_help {
87         /* insert conntrack helper private data (master) here */
88         struct ip_ct_ftp_master ct_ftp_info;
89         struct ip_ct_irc_master ct_irc_info;
90 };
91
92 #ifdef CONFIG_IP_NF_NAT_NEEDED
93 #include <linux/netfilter_ipv4/ip_nat.h>
94
95 /* per conntrack: nat application helper private data */
96 union ip_conntrack_nat_help {
97         /* insert nat helper private data here */
98 };
99 #endif
100
101 #ifdef __KERNEL__
102
103 #include <linux/types.h>
104 #include <linux/skbuff.h>
105
106 #ifdef CONFIG_NETFILTER_DEBUG
107 #define IP_NF_ASSERT(x)                                                 \
108 do {                                                                    \
109         if (!(x))                                                       \
110                 /* Wooah!  I'm tripping my conntrack in a frenzy of     \
111                    netplay... */                                        \
112                 printk("NF_IP_ASSERT: %s:%i(%s)\n",                     \
113                        __FILE__, __LINE__, __FUNCTION__);               \
114 } while(0)
115 #else
116 #define IP_NF_ASSERT(x)
117 #endif
118
119 struct ip_conntrack_expect
120 {
121         /* Internal linked list (global expectation list) */
122         struct list_head list;
123
124         /* reference count */
125         atomic_t use;
126
127         /* expectation list for this master */
128         struct list_head expected_list;
129
130         /* The conntrack of the master connection */
131         struct ip_conntrack *expectant;
132
133         /* The conntrack of the sibling connection, set after
134          * expectation arrived */
135         struct ip_conntrack *sibling;
136
137         /* Tuple saved for conntrack */
138         struct ip_conntrack_tuple ct_tuple;
139
140         /* Timer function; deletes the expectation. */
141         struct timer_list timeout;
142
143         /* Data filled out by the conntrack helpers follow: */
144
145         /* We expect this tuple, with the following mask */
146         struct ip_conntrack_tuple tuple, mask;
147
148         /* Function to call after setup and insertion */
149         int (*expectfn)(struct ip_conntrack *new);
150
151         /* At which sequence number did this expectation occur */
152         u_int32_t seq;
153   
154         union ip_conntrack_expect_proto proto;
155
156         union ip_conntrack_expect_help help;
157 };
158
159 struct ip_conntrack_helper;
160
161 struct ip_conntrack
162 {
163         /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
164            plus 1 for any connection(s) we are `master' for */
165         struct nf_conntrack ct_general;
166
167         /* These are my tuples; original and reply */
168         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
169
170         /* Have we seen traffic both ways yet? (bitset) */
171         unsigned long status;
172
173         /* Timer function; drops refcnt when it goes off. */
174         struct timer_list timeout;
175
176         /* If we're expecting another related connection, this will be
177            in expected linked list */
178         struct list_head sibling_list;
179         
180         /* Current number of expected connections */
181         unsigned int expecting;
182
183         /* If we were expected by an expectation, this will be it */
184         struct ip_conntrack_expect *master;
185
186         /* Helper, if any. */
187         struct ip_conntrack_helper *helper;
188
189         /* Our various nf_ct_info structs specify *what* relation this
190            packet has to the conntrack */
191         struct nf_ct_info infos[IP_CT_NUMBER];
192
193         /* Storage reserved for other modules: */
194
195         union ip_conntrack_proto proto;
196
197         union ip_conntrack_help help;
198
199 #ifdef CONFIG_IP_NF_NAT_NEEDED
200         struct {
201                 struct ip_nat_info info;
202                 union ip_conntrack_nat_help help;
203 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
204         defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
205                 int masq_index;
206 #endif
207         } nat;
208 #endif /* CONFIG_IP_NF_NAT_NEEDED */
209
210 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
211         unsigned long mark;
212 #endif
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 */