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
8 /* Part of an established connection (either direction). */
11 /* Like NEW, but related to an existing connection, or ICMP error
12 (in either direction). */
15 /* Started a new connection to track (only
16 IP_CT_DIR_ORIGINAL); may be a retransmission. */
19 /* >= this indicates reply direction */
22 /* Number of distinct IP_CT types (no NEW in reply dirn). */
23 IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1
26 /* Bitset representing status of connection. */
27 enum ip_conntrack_status {
28 /* It's an expected connection: bit 0 set. This bit never changed */
30 IPS_EXPECTED = (1 << IPS_EXPECTED_BIT),
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),
36 /* Conntrack should never be early-expired. */
38 IPS_ASSURED = (1 << IPS_ASSURED_BIT),
40 /* Connection is confirmed: originating packet has left box */
41 IPS_CONFIRMED_BIT = 3,
42 IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
44 /* Connection needs src nat in orig dir. This bit never changed. */
46 IPS_SRC_NAT = (1 << IPS_SRC_NAT_BIT),
48 /* Connection needs dst nat in orig dir. This bit never changed. */
50 IPS_DST_NAT = (1 << IPS_DST_NAT_BIT),
53 IPS_NAT_MASK = (IPS_DST_NAT | IPS_SRC_NAT),
55 /* Connection needs TCP sequence adjusted. */
56 IPS_SEQ_ADJUST_BIT = 6,
57 IPS_SEQ_ADJUST = (1 << IPS_SEQ_ADJUST_BIT),
59 /* NAT initialization bits. */
60 IPS_SRC_NAT_DONE_BIT = 7,
61 IPS_SRC_NAT_DONE = (1 << IPS_SRC_NAT_DONE_BIT),
63 IPS_DST_NAT_DONE_BIT = 8,
64 IPS_DST_NAT_DONE = (1 << IPS_DST_NAT_DONE_BIT),
67 IPS_NAT_DONE_MASK = (IPS_DST_NAT_DONE | IPS_SRC_NAT_DONE),
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>
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>
82 /* per conntrack: protocol private data */
83 union ip_conntrack_proto {
84 /* insert conntrack proto private data here */
86 struct ip_ct_sctp sctp;
88 struct ip_ct_icmp icmp;
91 union ip_conntrack_expect_proto {
92 /* insert expect proto private data here */
93 struct ip_ct_gre_expect gre;
96 /* Add protocol helper include file here */
97 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
98 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
99 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
100 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
102 /* per conntrack: application helper private data */
103 union ip_conntrack_help {
104 /* insert conntrack helper private data (master) here */
105 struct ip_ct_pptp_master ct_pptp_info;
106 struct ip_ct_ftp_master ct_ftp_info;
107 struct ip_ct_irc_master ct_irc_info;
110 #ifdef CONFIG_IP_NF_NAT_NEEDED
111 #include <linux/netfilter_ipv4/ip_nat.h>
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_counter
136 struct ip_conntrack_helper;
140 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
141 plus 1 for any connection(s) we are `master' for */
142 struct nf_conntrack ct_general;
144 /* Have we seen traffic both ways yet? (bitset) */
145 unsigned long status;
147 /* Timer function; drops refcnt when it goes off. */
148 struct timer_list timeout;
150 #ifdef CONFIG_IP_NF_CT_ACCT
151 /* Accounting Information (same cache line as other written members) */
152 struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
154 /* If we were expected by an expectation, this will be it */
155 struct ip_conntrack *master;
157 /* Current number of expected connections */
158 unsigned int expecting;
160 /* Helper, if any. */
161 struct ip_conntrack_helper *helper;
163 /* Storage reserved for other modules: */
164 union ip_conntrack_proto proto;
166 union ip_conntrack_help help;
168 #ifdef CONFIG_IP_NF_NAT_NEEDED
170 struct ip_nat_info info;
171 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
172 defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
176 #endif /* CONFIG_IP_NF_NAT_NEEDED */
178 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
182 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
183 /* VServer context id */
184 xid_t xid[IP_CT_DIR_MAX];
187 /* Traversed often, so hopefully in different cacheline to top */
188 /* These are my tuples; original and reply */
189 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
192 struct ip_conntrack_expect
194 /* Internal linked list (global expectation list) */
195 struct list_head list;
197 /* We expect this tuple, with the following mask */
198 struct ip_conntrack_tuple tuple, mask;
200 /* Function to call after setup and insertion */
201 void (*expectfn)(struct ip_conntrack *new,
202 struct ip_conntrack_expect *this);
204 /* The conntrack of the master connection */
205 struct ip_conntrack *master;
207 /* Timer function; deletes the expectation. */
208 struct timer_list timeout;
210 #ifdef CONFIG_IP_NF_NAT_NEEDED
211 /* This is the original per-proto part, used to map the
212 * expected connection the way the recipient expects. */
213 union ip_conntrack_manip_proto saved_proto;
214 /* Direction relative to the master connection. */
215 enum ip_conntrack_dir dir;
219 static inline struct ip_conntrack *
220 tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash *hash)
222 return container_of(hash, struct ip_conntrack,
223 tuplehash[hash->tuple.dst.dir]);
226 /* get master conntrack via master expectation */
227 #define master_ct(conntr) (conntr->master)
229 /* Alter reply tuple (maybe alter helper). */
231 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
232 const struct ip_conntrack_tuple *newreply);
234 /* Is this tuple taken? (ignoring any belonging to the given
237 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
238 const struct ip_conntrack *ignored_conntrack);
240 /* Return conntrack_info and tuple hash for given skb. */
241 static inline struct ip_conntrack *
242 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
244 *ctinfo = skb->nfctinfo;
245 return (struct ip_conntrack *)skb->nfct;
248 /* decrement reference count on a conntrack */
249 extern inline void ip_conntrack_put(struct ip_conntrack *ct);
251 /* call to create an explicit dependency on ip_conntrack. */
252 extern void need_ip_conntrack(void);
254 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
255 const struct ip_conntrack_tuple *orig);
257 /* Refresh conntrack for this many jiffies */
258 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
259 enum ip_conntrack_info ctinfo,
260 const struct sk_buff *skb,
261 unsigned long extra_jiffies);
263 /* These are for NAT. Icky. */
264 /* Update TCP window tracking data when NAT mangles the packet */
265 extern void ip_conntrack_tcp_update(struct sk_buff *skb,
266 struct ip_conntrack *conntrack,
267 enum ip_conntrack_dir dir);
269 /* Call me when a conntrack is destroyed. */
270 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
272 /* Fake conntrack entry for untracked connections */
273 extern struct ip_conntrack ip_conntrack_untracked;
275 /* Returns new sk_buff, or NULL */
277 ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user);
279 /* Iterate over all conntracks: if iter returns true, it's deleted. */
281 ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data),
284 /* It's confirmed if it is, or has been in the hash table. */
285 static inline int is_confirmed(struct ip_conntrack *ct)
287 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
290 extern unsigned int ip_conntrack_htable_size;
292 struct ip_conntrack_stat
294 unsigned int searched;
297 unsigned int invalid;
300 unsigned int delete_list;
302 unsigned int insert_failed;
304 unsigned int early_drop;
306 unsigned int expect_new;
307 unsigned int expect_create;
308 unsigned int expect_delete;
311 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
313 #ifdef CONFIG_IP_NF_NAT_NEEDED
314 static inline int ip_nat_initialized(struct ip_conntrack *conntrack,
315 enum ip_nat_manip_type manip)
317 if (manip == IP_NAT_MANIP_SRC)
318 return test_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status);
319 return test_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status);
321 #endif /* CONFIG_IP_NF_NAT_NEEDED */
323 #endif /* __KERNEL__ */
324 #endif /* _IP_CONNTRACK_H */