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>
55 /* per conntrack: protocol private data */
56 union ip_conntrack_proto {
57 /* insert conntrack proto private data here */
59 struct ip_ct_icmp icmp;
62 union ip_conntrack_expect_proto {
63 /* insert expect proto private data here */
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>
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;
78 #ifdef CONFIG_IP_NF_NAT_NEEDED
80 /* insert nat helper private data (expect) here */
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;
92 #ifdef CONFIG_IP_NF_NAT_NEEDED
93 #include <linux/netfilter_ipv4/ip_nat.h>
95 /* per conntrack: nat application helper private data */
96 union ip_conntrack_nat_help {
97 /* insert nat helper private data here */
103 #include <linux/types.h>
104 #include <linux/skbuff.h>
106 #ifdef CONFIG_NETFILTER_DEBUG
107 #define IP_NF_ASSERT(x) \
110 /* Wooah! I'm tripping my conntrack in a frenzy of \
112 printk("NF_IP_ASSERT: %s:%i(%s)\n", \
113 __FILE__, __LINE__, __FUNCTION__); \
116 #define IP_NF_ASSERT(x)
119 struct ip_conntrack_expect
121 /* Internal linked list (global expectation list) */
122 struct list_head list;
124 /* reference count */
127 /* expectation list for this master */
128 struct list_head expected_list;
130 /* The conntrack of the master connection */
131 struct ip_conntrack *expectant;
133 /* The conntrack of the sibling connection, set after
134 * expectation arrived */
135 struct ip_conntrack *sibling;
137 /* Tuple saved for conntrack */
138 struct ip_conntrack_tuple ct_tuple;
140 /* Timer function; deletes the expectation. */
141 struct timer_list timeout;
143 /* Data filled out by the conntrack helpers follow: */
145 /* We expect this tuple, with the following mask */
146 struct ip_conntrack_tuple tuple, mask;
148 /* Function to call after setup and insertion */
149 int (*expectfn)(struct ip_conntrack *new);
151 /* At which sequence number did this expectation occur */
154 union ip_conntrack_expect_proto proto;
156 union ip_conntrack_expect_help help;
159 struct ip_conntrack_helper;
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;
167 /* These are my tuples; original and reply */
168 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
170 /* Have we seen traffic both ways yet? (bitset) */
171 unsigned long status;
173 /* Timer function; drops refcnt when it goes off. */
174 struct timer_list timeout;
176 /* If we're expecting another related connection, this will be
177 in expected linked list */
178 struct list_head sibling_list;
180 /* Current number of expected connections */
181 unsigned int expecting;
183 /* If we were expected by an expectation, this will be it */
184 struct ip_conntrack_expect *master;
186 /* Helper, if any. */
187 struct ip_conntrack_helper *helper;
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];
193 /* Storage reserved for other modules: */
195 union ip_conntrack_proto proto;
197 union ip_conntrack_help help;
199 #ifdef CONFIG_IP_NF_NAT_NEEDED
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)
208 #endif /* CONFIG_IP_NF_NAT_NEEDED */
212 /* get master conntrack via master expectation */
213 #define master_ct(conntr) (conntr->master ? conntr->master->expectant : NULL)
215 /* Alter reply tuple (maybe alter helper). If it's already taken,
216 return 0 and don't do alteration. */
218 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
219 const struct ip_conntrack_tuple *newreply);
221 /* Is this tuple taken? (ignoring any belonging to the given
224 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
225 const struct ip_conntrack *ignored_conntrack);
227 /* Return conntrack_info and tuple hash for given skb. */
228 extern struct ip_conntrack *
229 ip_conntrack_get(struct sk_buff *skb, enum ip_conntrack_info *ctinfo);
231 /* decrement reference count on a conntrack */
232 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
234 /* find unconfirmed expectation based on tuple */
235 struct ip_conntrack_expect *
236 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
238 /* decrement reference count on an expectation */
239 void ip_conntrack_expect_put(struct ip_conntrack_expect *exp);
241 /* call to create an explicit dependency on ip_conntrack. */
242 extern void need_ip_conntrack(void);
244 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
245 const struct ip_conntrack_tuple *orig);
247 /* Refresh conntrack for this many jiffies */
248 extern void ip_ct_refresh(struct ip_conntrack *ct,
249 unsigned long extra_jiffies);
251 /* These are for NAT. Icky. */
252 /* Call me when a conntrack is destroyed. */
253 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
255 /* Fake conntrack entry for untracked connections */
256 extern struct ip_conntrack ip_conntrack_untracked;
258 /* Returns new sk_buff, or NULL */
260 ip_ct_gather_frags(struct sk_buff *skb);
262 /* Delete all conntracks which match. */
264 ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
267 /* It's confirmed if it is, or has been in the hash table. */
268 static inline int is_confirmed(struct ip_conntrack *ct)
270 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
273 extern unsigned int ip_conntrack_htable_size;
275 /* eg. PROVIDES_CONNTRACK(ftp); */
276 #define PROVIDES_CONNTRACK(name) \
277 int needs_ip_conntrack_##name; \
278 EXPORT_SYMBOL(needs_ip_conntrack_##name)
280 /*. eg. NEEDS_CONNTRACK(ftp); */
281 #define NEEDS_CONNTRACK(name) \
282 extern int needs_ip_conntrack_##name; \
283 static int *need_ip_conntrack_##name __attribute_used__ = &needs_ip_conntrack_##name
285 #endif /* __KERNEL__ */
286 #endif /* _IP_CONNTRACK_H */