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 <asm/atomic.h>
13 enum ip_conntrack_info
15 /* Part of an established connection (either direction). */
18 /* Like NEW, but related to an existing connection, or ICMP error
19 (in either direction). */
22 /* Started a new connection to track (only
23 IP_CT_DIR_ORIGINAL); may be a retransmission. */
26 /* >= this indicates reply direction */
29 /* Number of distinct IP_CT types (no NEW in reply dirn). */
30 IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1
33 /* Bitset representing status of connection. */
34 enum ip_conntrack_status {
35 /* It's an expected connection: bit 0 set. This bit never changed */
37 IPS_EXPECTED = (1 << IPS_EXPECTED_BIT),
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),
43 /* Conntrack should never be early-expired. */
45 IPS_ASSURED = (1 << IPS_ASSURED_BIT),
47 /* Connection is confirmed: originating packet has left box */
48 IPS_CONFIRMED_BIT = 3,
49 IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
52 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
53 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
54 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
56 /* per conntrack: protocol private data */
57 union ip_conntrack_proto {
58 /* insert conntrack proto private data here */
59 struct ip_ct_sctp sctp;
61 struct ip_ct_icmp icmp;
64 union ip_conntrack_expect_proto {
65 /* insert expect proto private data here */
68 /* Add protocol helper include file here */
69 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
70 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
71 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
73 /* per expectation: application helper private data */
74 union ip_conntrack_expect_help {
75 /* insert conntrack helper private data (expect) here */
76 struct ip_ct_amanda_expect exp_amanda_info;
77 struct ip_ct_ftp_expect exp_ftp_info;
78 struct ip_ct_irc_expect exp_irc_info;
80 #ifdef CONFIG_IP_NF_NAT_NEEDED
82 /* insert nat helper private data (expect) here */
87 /* per conntrack: application helper private data */
88 union ip_conntrack_help {
89 /* insert conntrack helper private data (master) here */
90 struct ip_ct_ftp_master ct_ftp_info;
91 struct ip_ct_irc_master ct_irc_info;
94 #ifdef CONFIG_IP_NF_NAT_NEEDED
95 #include <linux/netfilter_ipv4/ip_nat.h>
97 /* per conntrack: nat application helper private data */
98 union ip_conntrack_nat_help {
99 /* insert nat helper private data here */
105 #include <linux/types.h>
106 #include <linux/skbuff.h>
108 #ifdef CONFIG_NETFILTER_DEBUG
109 #define IP_NF_ASSERT(x) \
112 /* Wooah! I'm tripping my conntrack in a frenzy of \
114 printk("NF_IP_ASSERT: %s:%i(%s)\n", \
115 __FILE__, __LINE__, __FUNCTION__); \
118 #define IP_NF_ASSERT(x)
121 struct ip_conntrack_expect
123 /* Internal linked list (global expectation list) */
124 struct list_head list;
126 /* reference count */
129 /* expectation list for this master */
130 struct list_head expected_list;
132 /* The conntrack of the master connection */
133 struct ip_conntrack *expectant;
135 /* The conntrack of the sibling connection, set after
136 * expectation arrived */
137 struct ip_conntrack *sibling;
139 /* Tuple saved for conntrack */
140 struct ip_conntrack_tuple ct_tuple;
142 /* Timer function; deletes the expectation. */
143 struct timer_list timeout;
145 /* Data filled out by the conntrack helpers follow: */
147 /* We expect this tuple, with the following mask */
148 struct ip_conntrack_tuple tuple, mask;
150 /* Function to call after setup and insertion */
151 int (*expectfn)(struct ip_conntrack *new);
153 /* At which sequence number did this expectation occur */
156 union ip_conntrack_expect_proto proto;
158 union ip_conntrack_expect_help help;
161 struct ip_conntrack_counter
167 struct ip_conntrack_helper;
171 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
172 plus 1 for any connection(s) we are `master' for */
173 struct nf_conntrack ct_general;
175 /* Have we seen traffic both ways yet? (bitset) */
176 unsigned long status;
178 /* Timer function; drops refcnt when it goes off. */
179 struct timer_list timeout;
181 #ifdef CONFIG_IP_NF_CT_ACCT
182 /* Accounting Information (same cache line as other written members) */
183 struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
186 /* If we're expecting another related connection, this will be
187 in expected linked list */
188 struct list_head sibling_list;
190 /* Current number of expected connections */
191 unsigned int expecting;
193 /* If we were expected by an expectation, this will be it */
194 struct ip_conntrack_expect *master;
196 /* Helper, if any. */
197 struct ip_conntrack_helper *helper;
199 /* Storage reserved for other modules: */
200 union ip_conntrack_proto proto;
202 union ip_conntrack_help help;
204 #ifdef CONFIG_IP_NF_NAT_NEEDED
206 struct ip_nat_info info;
207 union ip_conntrack_nat_help help;
208 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
209 defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
213 #endif /* CONFIG_IP_NF_NAT_NEEDED */
215 /* Traversed often, so hopefully in different cacheline to top */
216 /* These are my tuples; original and reply */
217 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
220 /* get master conntrack via master expectation */
221 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
223 /* Alter reply tuple (maybe alter helper). If it's already taken,
224 return 0 and don't do alteration. */
226 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
227 const struct ip_conntrack_tuple *newreply);
229 /* Is this tuple taken? (ignoring any belonging to the given
232 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
233 const struct ip_conntrack *ignored_conntrack);
235 /* Return conntrack_info and tuple hash for given skb. */
236 static inline struct ip_conntrack *
237 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
239 *ctinfo = skb->nfctinfo;
240 return (struct ip_conntrack *)skb->nfct;
243 /* decrement reference count on a conntrack */
244 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
246 /* find unconfirmed expectation based on tuple */
247 struct ip_conntrack_expect *
248 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
250 /* decrement reference count on an expectation */
251 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
253 /* call to create an explicit dependency on ip_conntrack. */
254 extern void need_ip_conntrack(void);
256 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
257 const struct ip_conntrack_tuple *orig);
259 /* Refresh conntrack for this many jiffies */
260 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
261 enum ip_conntrack_info ctinfo,
262 const struct sk_buff *skb,
263 unsigned long extra_jiffies);
265 /* These are for NAT. Icky. */
266 /* Update TCP window tracking data when NAT mangles the packet */
267 extern int ip_conntrack_tcp_update(struct sk_buff *skb,
268 struct ip_conntrack *conntrack,
271 /* Call me when a conntrack is destroyed. */
272 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
274 /* Fake conntrack entry for untracked connections */
275 extern struct ip_conntrack ip_conntrack_untracked;
277 extern int ip_ct_no_defrag;
278 /* Returns new sk_buff, or NULL */
280 ip_ct_gather_frags(struct sk_buff *skb);
282 /* Delete all conntracks which match. */
284 ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
287 /* It's confirmed if it is, or has been in the hash table. */
288 static inline int is_confirmed(struct ip_conntrack *ct)
290 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
293 extern unsigned int ip_conntrack_htable_size;
295 struct ip_conntrack_stat
297 unsigned int searched;
300 unsigned int invalid;
303 unsigned int delete_list;
305 unsigned int insert_failed;
307 unsigned int early_drop;
309 unsigned int expect_new;
310 unsigned int expect_create;
311 unsigned int expect_delete;
314 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
316 /* eg. PROVIDES_CONNTRACK(ftp); */
317 #define PROVIDES_CONNTRACK(name) \
318 int needs_ip_conntrack_##name; \
319 EXPORT_SYMBOL(needs_ip_conntrack_##name)
321 /*. eg. NEEDS_CONNTRACK(ftp); */
322 #define NEEDS_CONNTRACK(name) \
323 extern int needs_ip_conntrack_##name; \
324 static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name
326 #endif /* __KERNEL__ */
327 #endif /* _IP_CONNTRACK_H */