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),
69 /* Connection is dying (removed from lists), can not be unset. */
71 IPS_DYING = (1 << IPS_DYING_BIT),
74 /* Connection tracking event bits */
75 enum ip_conntrack_events
79 IPCT_NEW = (1 << IPCT_NEW_BIT),
81 /* Expected connection */
83 IPCT_RELATED = (1 << IPCT_RELATED_BIT),
85 /* Destroyed conntrack */
87 IPCT_DESTROY = (1 << IPCT_DESTROY_BIT),
89 /* Timer has been refreshed */
91 IPCT_REFRESH = (1 << IPCT_REFRESH_BIT),
93 /* Status has changed */
95 IPCT_STATUS = (1 << IPCT_STATUS_BIT),
97 /* Update of protocol info */
98 IPCT_PROTOINFO_BIT = 5,
99 IPCT_PROTOINFO = (1 << IPCT_PROTOINFO_BIT),
101 /* Volatile protocol info */
102 IPCT_PROTOINFO_VOLATILE_BIT = 6,
103 IPCT_PROTOINFO_VOLATILE = (1 << IPCT_PROTOINFO_VOLATILE_BIT),
105 /* New helper for conntrack */
107 IPCT_HELPER = (1 << IPCT_HELPER_BIT),
109 /* Update of helper info */
110 IPCT_HELPINFO_BIT = 8,
111 IPCT_HELPINFO = (1 << IPCT_HELPINFO_BIT),
113 /* Volatile helper info */
114 IPCT_HELPINFO_VOLATILE_BIT = 9,
115 IPCT_HELPINFO_VOLATILE = (1 << IPCT_HELPINFO_VOLATILE_BIT),
118 IPCT_NATINFO_BIT = 10,
119 IPCT_NATINFO = (1 << IPCT_NATINFO_BIT),
122 enum ip_conntrack_expect_events {
124 IPEXP_NEW = (1 << IPEXP_NEW_BIT),
128 #include <linux/config.h>
129 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
130 #include <linux/bitops.h>
131 #include <linux/compiler.h>
132 #include <asm/atomic.h>
134 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
135 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
136 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
138 /* per conntrack: protocol private data */
139 union ip_conntrack_proto {
140 /* insert conntrack proto private data here */
141 struct ip_ct_sctp sctp;
142 struct ip_ct_tcp tcp;
143 struct ip_ct_icmp icmp;
146 union ip_conntrack_expect_proto {
147 /* insert expect proto private data here */
150 /* Add protocol helper include file here */
151 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
152 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
153 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
155 /* per conntrack: application helper private data */
156 union ip_conntrack_help {
157 /* insert conntrack helper private data (master) here */
158 struct ip_ct_ftp_master ct_ftp_info;
159 struct ip_ct_irc_master ct_irc_info;
162 #ifdef CONFIG_IP_NF_NAT_NEEDED
163 #include <linux/netfilter_ipv4/ip_nat.h>
166 #include <linux/types.h>
167 #include <linux/skbuff.h>
169 #ifdef CONFIG_NETFILTER_DEBUG
170 #define IP_NF_ASSERT(x) \
173 /* Wooah! I'm tripping my conntrack in a frenzy of \
175 printk("NF_IP_ASSERT: %s:%i(%s)\n", \
176 __FILE__, __LINE__, __FUNCTION__); \
179 #define IP_NF_ASSERT(x)
182 struct ip_conntrack_counter
188 struct ip_conntrack_helper;
192 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
193 plus 1 for any connection(s) we are `master' for */
194 struct nf_conntrack ct_general;
196 /* Have we seen traffic both ways yet? (bitset) */
197 unsigned long status;
199 /* Timer function; drops refcnt when it goes off. */
200 struct timer_list timeout;
202 #ifdef CONFIG_IP_NF_CT_ACCT
203 /* Accounting Information (same cache line as other written members) */
204 struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
206 /* If we were expected by an expectation, this will be it */
207 struct ip_conntrack *master;
209 /* Current number of expected connections */
210 unsigned int expecting;
212 /* Unique ID that identifies this conntrack*/
215 /* Helper, if any. */
216 struct ip_conntrack_helper *helper;
218 /* Storage reserved for other modules: */
219 union ip_conntrack_proto proto;
221 union ip_conntrack_help help;
223 #ifdef CONFIG_IP_NF_NAT_NEEDED
225 struct ip_nat_info info;
226 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
227 defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
231 #endif /* CONFIG_IP_NF_NAT_NEEDED */
233 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
237 /* Traversed often, so hopefully in different cacheline to top */
238 /* These are my tuples; original and reply */
239 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
242 struct ip_conntrack_expect
244 /* Internal linked list (global expectation list) */
245 struct list_head list;
247 /* We expect this tuple, with the following mask */
248 struct ip_conntrack_tuple tuple, mask;
250 /* Function to call after setup and insertion */
251 void (*expectfn)(struct ip_conntrack *new,
252 struct ip_conntrack_expect *this);
254 /* The conntrack of the master connection */
255 struct ip_conntrack *master;
257 /* Timer function; deletes the expectation. */
258 struct timer_list timeout;
266 #ifdef CONFIG_IP_NF_NAT_NEEDED
267 /* This is the original per-proto part, used to map the
268 * expected connection the way the recipient expects. */
269 union ip_conntrack_manip_proto saved_proto;
270 /* Direction relative to the master connection. */
271 enum ip_conntrack_dir dir;
275 static inline struct ip_conntrack *
276 tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash *hash)
278 return container_of(hash, struct ip_conntrack,
279 tuplehash[hash->tuple.dst.dir]);
282 /* get master conntrack via master expectation */
283 #define master_ct(conntr) (conntr->master)
285 /* Alter reply tuple (maybe alter helper). */
287 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
288 const struct ip_conntrack_tuple *newreply);
290 /* Is this tuple taken? (ignoring any belonging to the given
293 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
294 const struct ip_conntrack *ignored_conntrack);
296 /* Return conntrack_info and tuple hash for given skb. */
297 static inline struct ip_conntrack *
298 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
300 *ctinfo = skb->nfctinfo;
301 return (struct ip_conntrack *)skb->nfct;
304 /* decrement reference count on a conntrack */
306 ip_conntrack_put(struct ip_conntrack *ct)
309 nf_conntrack_put(&ct->ct_general);
312 /* call to create an explicit dependency on ip_conntrack. */
313 extern void need_ip_conntrack(void);
315 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
316 const struct ip_conntrack_tuple *orig);
318 /* Refresh conntrack for this many jiffies */
319 extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
320 enum ip_conntrack_info ctinfo,
321 const struct sk_buff *skb,
322 unsigned long extra_jiffies);
324 /* These are for NAT. Icky. */
325 /* Update TCP window tracking data when NAT mangles the packet */
326 extern void ip_conntrack_tcp_update(struct sk_buff *skb,
327 struct ip_conntrack *conntrack,
328 enum ip_conntrack_dir dir);
330 /* Call me when a conntrack is destroyed. */
331 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
333 /* Fake conntrack entry for untracked connections */
334 extern struct ip_conntrack ip_conntrack_untracked;
336 /* Returns new sk_buff, or NULL */
338 ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user);
340 /* Iterate over all conntracks: if iter returns true, it's deleted. */
342 ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data),
345 extern struct ip_conntrack_helper *
346 __ip_conntrack_helper_find_byname(const char *);
347 extern struct ip_conntrack_helper *
348 ip_conntrack_helper_find_get(const struct ip_conntrack_tuple *tuple);
349 extern void ip_conntrack_helper_put(struct ip_conntrack_helper *helper);
351 extern struct ip_conntrack_protocol *
352 __ip_conntrack_proto_find(u_int8_t protocol);
353 extern struct ip_conntrack_protocol *
354 ip_conntrack_proto_find_get(u_int8_t protocol);
355 extern void ip_conntrack_proto_put(struct ip_conntrack_protocol *proto);
357 extern void ip_ct_remove_expectations(struct ip_conntrack *ct);
359 extern struct ip_conntrack *ip_conntrack_alloc(struct ip_conntrack_tuple *,
360 struct ip_conntrack_tuple *);
362 extern void ip_conntrack_free(struct ip_conntrack *ct);
364 extern void ip_conntrack_hash_insert(struct ip_conntrack *ct);
366 extern struct ip_conntrack_expect *
367 __ip_conntrack_expect_find(const struct ip_conntrack_tuple *tuple);
369 extern struct ip_conntrack_expect *
370 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
372 extern struct ip_conntrack_tuple_hash *
373 __ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
374 const struct ip_conntrack *ignored_conntrack);
376 extern void ip_conntrack_flush(void);
378 /* It's confirmed if it is, or has been in the hash table. */
379 static inline int is_confirmed(struct ip_conntrack *ct)
381 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
384 static inline int is_dying(struct ip_conntrack *ct)
386 return test_bit(IPS_DYING_BIT, &ct->status);
389 extern unsigned int ip_conntrack_htable_size;
391 struct ip_conntrack_stat
393 unsigned int searched;
396 unsigned int invalid;
399 unsigned int delete_list;
401 unsigned int insert_failed;
403 unsigned int early_drop;
405 unsigned int expect_new;
406 unsigned int expect_create;
407 unsigned int expect_delete;
410 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
412 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
413 #include <linux/notifier.h>
414 #include <linux/interrupt.h>
416 struct ip_conntrack_ecache {
417 struct ip_conntrack *ct;
420 DECLARE_PER_CPU(struct ip_conntrack_ecache, ip_conntrack_ecache);
422 #define CONNTRACK_ECACHE(x) (__get_cpu_var(ip_conntrack_ecache).x)
424 extern struct notifier_block *ip_conntrack_chain;
425 extern struct notifier_block *ip_conntrack_expect_chain;
427 static inline int ip_conntrack_register_notifier(struct notifier_block *nb)
429 return notifier_chain_register(&ip_conntrack_chain, nb);
432 static inline int ip_conntrack_unregister_notifier(struct notifier_block *nb)
434 return notifier_chain_unregister(&ip_conntrack_chain, nb);
438 ip_conntrack_expect_register_notifier(struct notifier_block *nb)
440 return notifier_chain_register(&ip_conntrack_expect_chain, nb);
444 ip_conntrack_expect_unregister_notifier(struct notifier_block *nb)
446 return notifier_chain_unregister(&ip_conntrack_expect_chain, nb);
449 extern void ip_ct_deliver_cached_events(const struct ip_conntrack *ct);
450 extern void __ip_ct_event_cache_init(struct ip_conntrack *ct);
453 ip_conntrack_event_cache(enum ip_conntrack_events event,
454 const struct sk_buff *skb)
456 struct ip_conntrack *ct = (struct ip_conntrack *)skb->nfct;
457 struct ip_conntrack_ecache *ecache;
460 ecache = &__get_cpu_var(ip_conntrack_ecache);
461 if (ct != ecache->ct)
462 __ip_ct_event_cache_init(ct);
463 ecache->events |= event;
467 static inline void ip_conntrack_event(enum ip_conntrack_events event,
468 struct ip_conntrack *ct)
470 if (is_confirmed(ct) && !is_dying(ct))
471 notifier_call_chain(&ip_conntrack_chain, event, ct);
475 ip_conntrack_expect_event(enum ip_conntrack_expect_events event,
476 struct ip_conntrack_expect *exp)
478 notifier_call_chain(&ip_conntrack_expect_chain, event, exp);
480 #else /* CONFIG_IP_NF_CONNTRACK_EVENTS */
481 static inline void ip_conntrack_event_cache(enum ip_conntrack_events event,
482 const struct sk_buff *skb) {}
483 static inline void ip_conntrack_event(enum ip_conntrack_events event,
484 struct ip_conntrack *ct) {}
485 static inline void ip_ct_deliver_cached_events(const struct ip_conntrack *ct) {}
487 ip_conntrack_expect_event(enum ip_conntrack_expect_events event,
488 struct ip_conntrack_expect *exp) {}
489 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
491 #ifdef CONFIG_IP_NF_NAT_NEEDED
492 static inline int ip_nat_initialized(struct ip_conntrack *conntrack,
493 enum ip_nat_manip_type manip)
495 if (manip == IP_NAT_MANIP_SRC)
496 return test_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status);
497 return test_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status);
499 #endif /* CONFIG_IP_NF_NAT_NEEDED */
501 #endif /* __KERNEL__ */
502 #endif /* _IP_CONNTRACK_H */