2 * Connection state tracking for netfilter. This is separated from,
3 * but required by, the (future) NAT layer; it can also be used by an iptables
6 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
7 * - generalize L3 protocol dependent part.
9 * Derived from include/linux/netfiter_ipv4/ip_conntrack.h
12 #ifndef _NF_CONNTRACK_H
13 #define _NF_CONNTRACK_H
15 #include <linux/netfilter/nf_conntrack_common.h>
18 #include <linux/config.h>
19 #include <linux/bitops.h>
20 #include <linux/compiler.h>
21 #include <asm/atomic.h>
23 #include <linux/netfilter/nf_conntrack_tcp.h>
24 #include <linux/netfilter/nf_conntrack_sctp.h>
25 #include <net/netfilter/ipv4/nf_conntrack_icmp.h>
26 #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
28 #include <net/netfilter/nf_conntrack_tuple.h>
30 /* per conntrack: protocol private data */
31 union nf_conntrack_proto {
32 /* insert conntrack proto private data here */
33 struct ip_ct_sctp sctp;
35 struct ip_ct_icmp icmp;
36 struct nf_ct_icmpv6 icmpv6;
39 union nf_conntrack_expect_proto {
40 /* insert expect proto private data here */
43 /* Add protocol helper include file here */
44 #include <linux/netfilter/nf_conntrack_ftp.h>
46 /* per conntrack: application helper private data */
47 union nf_conntrack_help {
48 /* insert conntrack helper private data (master) here */
49 struct ip_ct_ftp_master ct_ftp_info;
52 #include <linux/types.h>
53 #include <linux/skbuff.h>
55 #ifdef CONFIG_NETFILTER_DEBUG
56 #define NF_CT_ASSERT(x) \
59 /* Wooah! I'm tripping my conntrack in a frenzy of \
61 printk("NF_CT_ASSERT: %s:%i(%s)\n", \
62 __FILE__, __LINE__, __FUNCTION__); \
65 #define NF_CT_ASSERT(x)
68 struct nf_conntrack_helper;
70 /* nf_conn feature for connections that have a helper */
73 struct nf_conntrack_helper *helper;
75 union nf_conntrack_help help;
77 /* Current number of expected connections */
78 unsigned int expecting;
82 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
85 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
86 plus 1 for any connection(s) we are `master' for */
87 struct nf_conntrack ct_general;
89 /* XXX should I move this to the tail ? - Y.K */
90 /* These are my tuples; original and reply */
91 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
93 /* Have we seen traffic both ways yet? (bitset) */
96 /* If we were expected by an expectation, this will be it */
97 struct nf_conn *master;
99 /* Timer function; drops refcnt when it goes off. */
100 struct timer_list timeout;
102 #ifdef CONFIG_NF_CT_ACCT
103 /* Accounting Information (same cache line as other written members) */
104 struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
107 /* Unique ID that identifies this conntrack*/
110 /* features - nat, helper, ... used by allocating system */
113 #if defined(CONFIG_NF_CONNTRACK_MARK)
117 /* Storage reserved for other modules: */
118 union nf_conntrack_proto proto;
120 /* features dynamically at the end: helper, nat (both optional) */
124 struct nf_conntrack_expect
126 /* Internal linked list (global expectation list) */
127 struct list_head list;
129 /* We expect this tuple, with the following mask */
130 struct nf_conntrack_tuple tuple, mask;
132 /* Function to call after setup and insertion */
133 void (*expectfn)(struct nf_conn *new,
134 struct nf_conntrack_expect *this);
136 /* The conntrack of the master connection */
137 struct nf_conn *master;
139 /* Timer function; deletes the expectation. */
140 struct timer_list timeout;
151 #ifdef CONFIG_NF_NAT_NEEDED
152 /* This is the original per-proto part, used to map the
153 * expected connection the way the recipient expects. */
154 union nf_conntrack_manip_proto saved_proto;
155 /* Direction relative to the master connection. */
156 enum ip_conntrack_dir dir;
160 #define NF_CT_EXPECT_PERMANENT 0x1
162 static inline struct nf_conn *
163 nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
165 return container_of(hash, struct nf_conn,
166 tuplehash[hash->tuple.dst.dir]);
169 /* get master conntrack via master expectation */
170 #define master_ct(conntr) (conntr->master)
172 /* Alter reply tuple (maybe alter helper). */
174 nf_conntrack_alter_reply(struct nf_conn *conntrack,
175 const struct nf_conntrack_tuple *newreply);
177 /* Is this tuple taken? (ignoring any belonging to the given
180 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
181 const struct nf_conn *ignored_conntrack);
183 /* Return conntrack_info and tuple hash for given skb. */
184 static inline struct nf_conn *
185 nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
187 *ctinfo = skb->nfctinfo;
188 return (struct nf_conn *)skb->nfct;
191 /* decrement reference count on a conntrack */
192 static inline void nf_ct_put(struct nf_conn *ct)
195 nf_conntrack_put(&ct->ct_general);
198 /* Protocol module loading */
199 extern int nf_ct_l3proto_try_module_get(unsigned short l3proto);
200 extern void nf_ct_l3proto_module_put(unsigned short l3proto);
202 extern struct nf_conntrack_tuple_hash *
203 __nf_conntrack_find(const struct nf_conntrack_tuple *tuple,
204 const struct nf_conn *ignored_conntrack);
206 extern void nf_conntrack_hash_insert(struct nf_conn *ct);
208 extern struct nf_conntrack_expect *
209 __nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple);
211 extern struct nf_conntrack_expect *
212 nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple);
214 extern void nf_ct_unlink_expect(struct nf_conntrack_expect *exp);
216 extern void nf_ct_remove_expectations(struct nf_conn *ct);
218 extern void nf_conntrack_flush(void);
220 extern struct nf_conntrack_helper *
221 nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple);
222 extern void nf_ct_helper_put(struct nf_conntrack_helper *helper);
224 extern struct nf_conntrack_helper *
225 __nf_conntrack_helper_find_byname(const char *name);
227 extern int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
228 const struct nf_conntrack_tuple *orig);
230 extern void __nf_ct_refresh_acct(struct nf_conn *ct,
231 enum ip_conntrack_info ctinfo,
232 const struct sk_buff *skb,
233 unsigned long extra_jiffies,
236 /* Refresh conntrack for this many jiffies and do accounting */
237 static inline void nf_ct_refresh_acct(struct nf_conn *ct,
238 enum ip_conntrack_info ctinfo,
239 const struct sk_buff *skb,
240 unsigned long extra_jiffies)
242 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
245 /* Refresh conntrack for this many jiffies */
246 static inline void nf_ct_refresh(struct nf_conn *ct,
247 const struct sk_buff *skb,
248 unsigned long extra_jiffies)
250 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
253 /* These are for NAT. Icky. */
254 /* Update TCP window tracking data when NAT mangles the packet */
255 extern void nf_conntrack_tcp_update(struct sk_buff *skb,
256 unsigned int dataoff,
257 struct nf_conn *conntrack,
260 /* Call me when a conntrack is destroyed. */
261 extern void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
263 /* Fake conntrack entry for untracked connections */
264 extern struct nf_conn nf_conntrack_untracked;
266 extern int nf_ct_no_defrag;
268 /* Iterate over all conntracks: if iter returns true, it's deleted. */
270 nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data);
271 extern void nf_conntrack_free(struct nf_conn *ct);
272 extern struct nf_conn *
273 nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
274 const struct nf_conntrack_tuple *repl);
276 /* It's confirmed if it is, or has been in the hash table. */
277 static inline int nf_ct_is_confirmed(struct nf_conn *ct)
279 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
282 static inline int nf_ct_is_dying(struct nf_conn *ct)
284 return test_bit(IPS_DYING_BIT, &ct->status);
287 extern unsigned int nf_conntrack_htable_size;
289 #define NF_CT_STAT_INC(count) (__get_cpu_var(nf_conntrack_stat).count++)
291 #ifdef CONFIG_NF_CONNTRACK_EVENTS
292 #include <linux/notifier.h>
293 #include <linux/interrupt.h>
295 struct nf_conntrack_ecache {
299 DECLARE_PER_CPU(struct nf_conntrack_ecache, nf_conntrack_ecache);
301 #define CONNTRACK_ECACHE(x) (__get_cpu_var(nf_conntrack_ecache).x)
303 extern struct atomic_notifier_head nf_conntrack_chain;
304 extern struct atomic_notifier_head nf_conntrack_expect_chain;
306 static inline int nf_conntrack_register_notifier(struct notifier_block *nb)
308 return atomic_notifier_chain_register(&nf_conntrack_chain, nb);
311 static inline int nf_conntrack_unregister_notifier(struct notifier_block *nb)
313 return atomic_notifier_chain_unregister(&nf_conntrack_chain, nb);
317 nf_conntrack_expect_register_notifier(struct notifier_block *nb)
319 return atomic_notifier_chain_register(&nf_conntrack_expect_chain, nb);
323 nf_conntrack_expect_unregister_notifier(struct notifier_block *nb)
325 return atomic_notifier_chain_unregister(&nf_conntrack_expect_chain,
329 extern void nf_ct_deliver_cached_events(const struct nf_conn *ct);
330 extern void __nf_ct_event_cache_init(struct nf_conn *ct);
333 nf_conntrack_event_cache(enum ip_conntrack_events event,
334 const struct sk_buff *skb)
336 struct nf_conn *ct = (struct nf_conn *)skb->nfct;
337 struct nf_conntrack_ecache *ecache;
340 ecache = &__get_cpu_var(nf_conntrack_ecache);
341 if (ct != ecache->ct)
342 __nf_ct_event_cache_init(ct);
343 ecache->events |= event;
347 static inline void nf_conntrack_event(enum ip_conntrack_events event,
350 if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct))
351 atomic_notifier_call_chain(&nf_conntrack_chain, event, ct);
355 nf_conntrack_expect_event(enum ip_conntrack_expect_events event,
356 struct nf_conntrack_expect *exp)
358 atomic_notifier_call_chain(&nf_conntrack_expect_chain, event, exp);
360 #else /* CONFIG_NF_CONNTRACK_EVENTS */
361 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
362 const struct sk_buff *skb) {}
363 static inline void nf_conntrack_event(enum ip_conntrack_events event,
364 struct nf_conn *ct) {}
365 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
367 nf_conntrack_expect_event(enum ip_conntrack_expect_events event,
368 struct nf_conntrack_expect *exp) {}
369 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
371 /* no helper, no nat */
372 #define NF_CT_F_BASIC 0
374 #define NF_CT_F_HELP 1
376 #define NF_CT_F_NAT 2
377 #define NF_CT_F_NUM 4
380 nf_conntrack_register_cache(u_int32_t features, const char *name, size_t size);
382 nf_conntrack_unregister_cache(u_int32_t features);
384 /* valid combinations:
385 * basic: nf_conn, nf_conn .. nf_conn_help
386 * nat: nf_conn .. nf_conn_nat, nf_conn .. nf_conn_nat, nf_conn help
388 static inline struct nf_conn_help *nfct_help(const struct nf_conn *ct)
390 unsigned int offset = sizeof(struct nf_conn);
392 if (!(ct->features & NF_CT_F_HELP))
395 return (struct nf_conn_help *) ((void *)ct + offset);
398 #endif /* __KERNEL__ */
399 #endif /* _NF_CONNTRACK_H */