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