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
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>
14 enum ip_conntrack_info
16 /* Part of an established connection (either direction). */
19 /* Like NEW, but related to an existing connection, or ICMP error
20 (in either direction). */
23 /* Started a new connection to track (only
24 IP_CT_DIR_ORIGINAL); may be a retransmission. */
27 /* >= this indicates reply direction */
30 /* Number of distinct IP_CT types (no NEW in reply dirn). */
31 IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1
34 /* Bitset representing status of connection. */
35 enum ip_conntrack_status {
36 /* It's an expected connection: bit 0 set. This bit never changed */
38 IPS_EXPECTED = (1 << IPS_EXPECTED_BIT),
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),
44 /* Conntrack should never be early-expired. */
46 IPS_ASSURED = (1 << IPS_ASSURED_BIT),
48 /* Connection is confirmed: originating packet has left box */
49 IPS_CONFIRMED_BIT = 3,
50 IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
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 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
58 /* per conntrack: protocol private data */
59 union ip_conntrack_proto {
60 /* insert conntrack proto private data here */
62 struct ip_ct_sctp sctp;
64 struct ip_ct_icmp icmp;
67 union ip_conntrack_expect_proto {
68 /* insert expect proto private data here */
69 struct ip_ct_gre_expect gre;
72 /* Add protocol helper include file here */
73 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
74 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
75 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
76 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
78 /* per expectation: application helper private data */
79 union ip_conntrack_expect_help {
80 /* insert conntrack helper private data (expect) here */
81 struct ip_ct_pptp_expect exp_pptp_info;
82 struct ip_ct_amanda_expect exp_amanda_info;
83 struct ip_ct_ftp_expect exp_ftp_info;
84 struct ip_ct_irc_expect exp_irc_info;
86 #ifdef CONFIG_IP_NF_NAT_NEEDED
88 /* insert nat helper private data (expect) here */
93 /* per conntrack: application helper private data */
94 union ip_conntrack_help {
95 /* insert conntrack helper private data (master) here */
96 struct ip_ct_pptp_master ct_pptp_info;
97 struct ip_ct_ftp_master ct_ftp_info;
98 struct ip_ct_irc_master ct_irc_info;
101 #ifdef CONFIG_IP_NF_NAT_NEEDED
102 #include <linux/netfilter_ipv4/ip_nat.h>
103 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
105 /* per conntrack: nat application helper private data */
106 union ip_conntrack_nat_help {
107 /* insert nat helper private data here */
108 struct ip_nat_pptp nat_pptp_info;
114 #include <linux/types.h>
115 #include <linux/skbuff.h>
117 #ifdef CONFIG_NETFILTER_DEBUG
118 #define IP_NF_ASSERT(x) \
121 /* Wooah! I'm tripping my conntrack in a frenzy of \
123 printk("NF_IP_ASSERT: %s:%i(%s)\n", \
124 __FILE__, __LINE__, __FUNCTION__); \
127 #define IP_NF_ASSERT(x)
130 struct ip_conntrack_expect
132 /* Internal linked list (global expectation list) */
133 struct list_head list;
135 /* reference count */
138 /* expectation list for this master */
139 struct list_head expected_list;
141 /* The conntrack of the master connection */
142 struct ip_conntrack *expectant;
144 /* The conntrack of the sibling connection, set after
145 * expectation arrived */
146 struct ip_conntrack *sibling;
148 /* Tuple saved for conntrack */
149 struct ip_conntrack_tuple ct_tuple;
151 /* Timer function; deletes the expectation. */
152 struct timer_list timeout;
154 /* Data filled out by the conntrack helpers follow: */
156 /* We expect this tuple, with the following mask */
157 struct ip_conntrack_tuple tuple, mask;
159 /* Function to call after setup and insertion */
160 int (*expectfn)(struct ip_conntrack *new);
162 /* At which sequence number did this expectation occur */
165 union ip_conntrack_expect_proto proto;
167 union ip_conntrack_expect_help help;
170 struct ip_conntrack_counter
176 struct ip_conntrack_helper;
180 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
181 plus 1 for any connection(s) we are `master' for */
182 struct nf_conntrack ct_general;
184 /* Have we seen traffic both ways yet? (bitset) */
185 unsigned long status;
187 /* Timer function; drops refcnt when it goes off. */
188 struct timer_list timeout;
190 #ifdef CONFIG_IP_NF_CT_ACCT
191 /* Accounting Information (same cache line as other written members) */
192 struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
195 /* If we're expecting another related connection, this will be
196 in expected linked list */
197 struct list_head sibling_list;
199 /* Current number of expected connections */
200 unsigned int expecting;
202 /* If we were expected by an expectation, this will be it */
203 struct ip_conntrack_expect *master;
205 /* Helper, if any. */
206 struct ip_conntrack_helper *helper;
208 /* Storage reserved for other modules: */
209 union ip_conntrack_proto proto;
211 union ip_conntrack_help help;
213 #ifdef CONFIG_IP_NF_NAT_NEEDED
215 struct ip_nat_info info;
216 union ip_conntrack_nat_help help;
217 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
218 defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
222 #endif /* CONFIG_IP_NF_NAT_NEEDED */
224 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
225 /* VServer context id */
226 xid_t xid[IP_CT_DIR_MAX];
229 /* Traversed often, so hopefully in different cacheline to top */
230 /* These are my tuples; original and reply */
231 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
234 /* get master conntrack via master expectation */
235 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
237 /* Alter reply tuple (maybe alter helper). If it's already taken,
238 return 0 and don't do alteration. */
240 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
241 const struct ip_conntrack_tuple *newreply);
243 /* Is this tuple taken? (ignoring any belonging to the given
246 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
247 const struct ip_conntrack *ignored_conntrack);
249 /* Return conntrack_info and tuple hash for given skb. */
250 static inline struct ip_conntrack *
251 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
253 *ctinfo = skb->nfctinfo;
254 return (struct ip_conntrack *)skb->nfct;
257 /* decrement reference count on a conntrack */
258 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
260 /* find unconfirmed expectation based on tuple */
261 struct ip_conntrack_expect *
262 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
264 /* decrement reference count on an expectation */
265 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
267 /* call to create an explicit dependency on ip_conntrack. */
268 extern void need_ip_conntrack(void);
270 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
271 const struct ip_conntrack_tuple *orig);
273 /* Refresh conntrack for this many jiffies */
274 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
275 enum ip_conntrack_info ctinfo,
276 const struct sk_buff *skb,
277 unsigned long extra_jiffies);
279 /* These are for NAT. Icky. */
280 /* Update TCP window tracking data when NAT mangles the packet */
281 extern int ip_conntrack_tcp_update(struct sk_buff *skb,
282 struct ip_conntrack *conntrack,
285 /* Call me when a conntrack is destroyed. */
286 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
288 /* Fake conntrack entry for untracked connections */
289 extern struct ip_conntrack ip_conntrack_untracked;
291 extern int ip_ct_no_defrag;
292 /* Returns new sk_buff, or NULL */
294 ip_ct_gather_frags(struct sk_buff *skb);
296 /* Delete all conntracks which match. */
298 ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
301 /* It's confirmed if it is, or has been in the hash table. */
302 static inline int is_confirmed(struct ip_conntrack *ct)
304 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
307 extern unsigned int ip_conntrack_htable_size;
309 struct ip_conntrack_stat
311 unsigned int searched;
314 unsigned int invalid;
317 unsigned int delete_list;
319 unsigned int insert_failed;
321 unsigned int early_drop;
323 unsigned int expect_new;
324 unsigned int expect_create;
325 unsigned int expect_delete;
328 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
330 /* eg. PROVIDES_CONNTRACK(ftp); */
331 #define PROVIDES_CONNTRACK(name) \
332 int needs_ip_conntrack_##name; \
333 EXPORT_SYMBOL(needs_ip_conntrack_##name)
335 /*. eg. NEEDS_CONNTRACK(ftp); */
336 #define NEEDS_CONNTRACK(name) \
337 extern int needs_ip_conntrack_##name; \
338 static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name
340 #endif /* __KERNEL__ */
341 #endif /* _IP_CONNTRACK_H */