1 #ifndef _IP_CONNTRACK_H
2 #define _IP_CONNTRACK_H
4 #include <linux/netfilter/nf_conntrack_common.h>
7 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
8 #include <linux/bitops.h>
9 #include <linux/compiler.h>
10 #include <asm/atomic.h>
12 #include <linux/timer.h>
13 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
14 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
15 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
16 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
18 /* per conntrack: protocol private data */
19 union ip_conntrack_proto
{
20 /* insert conntrack proto private data here */
22 struct ip_ct_sctp sctp
;
24 struct ip_ct_icmp icmp
;
27 union ip_conntrack_expect_proto
{
28 /* insert expect proto private data here */
31 /* Add protocol helper include file here */
32 #include <linux/netfilter_ipv4/ip_conntrack_h323.h>
33 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
38 /* per conntrack: application helper private data */
39 union ip_conntrack_help
{
40 /* insert conntrack helper private data (master) here */
41 struct ip_ct_h323_master ct_h323_info
;
42 struct ip_ct_pptp_master ct_pptp_info
;
43 struct ip_ct_ftp_master ct_ftp_info
;
44 struct ip_ct_irc_master ct_irc_info
;
47 #ifdef CONFIG_IP_NF_NAT_NEEDED
48 #include <linux/netfilter_ipv4/ip_nat.h>
49 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
51 /* per conntrack: nat application helper private data */
52 union ip_conntrack_nat_help
{
53 /* insert nat helper private data here */
54 struct ip_nat_pptp nat_pptp_info
;
58 #include <linux/types.h>
59 #include <linux/skbuff.h>
61 #ifdef CONFIG_NETFILTER_DEBUG
62 #define IP_NF_ASSERT(x) \
65 /* Wooah! I'm tripping my conntrack in a frenzy of \
67 printk("NF_IP_ASSERT: %s:%i(%s)\n", \
68 __FILE__, __LINE__, __FUNCTION__); \
71 #define IP_NF_ASSERT(x)
74 struct ip_conntrack_helper
;
78 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
79 plus 1 for any connection(s) we are `master' for */
80 struct nf_conntrack ct_general
;
82 /* Have we seen traffic both ways yet? (bitset) */
85 /* Timer function; drops refcnt when it goes off. */
86 struct timer_list timeout
;
88 #ifdef CONFIG_IP_NF_CT_ACCT
89 /* Accounting Information (same cache line as other written members) */
90 struct ip_conntrack_counter counters
[IP_CT_DIR_MAX
];
92 /* If we were expected by an expectation, this will be it */
93 struct ip_conntrack
*master
;
95 /* Current number of expected connections */
96 unsigned int expecting
;
98 /* Unique ID that identifies this conntrack*/
101 /* Helper, if any. */
102 struct ip_conntrack_helper
*helper
;
104 /* Storage reserved for other modules: */
105 union ip_conntrack_proto proto
;
107 union ip_conntrack_help help
;
109 #ifdef CONFIG_IP_NF_NAT_NEEDED
111 struct ip_nat_info info
;
112 union ip_conntrack_nat_help help
;
113 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
114 defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
118 #endif /* CONFIG_IP_NF_NAT_NEEDED */
120 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
124 #ifdef CONFIG_IP_NF_CONNTRACK_SECMARK
128 /* Traversed often, so hopefully in different cacheline to top */
129 /* These are my tuples; original and reply */
130 struct ip_conntrack_tuple_hash tuplehash
[IP_CT_DIR_MAX
];
133 struct ip_conntrack_expect
135 /* Internal linked list (global expectation list) */
136 struct list_head list
;
138 /* We expect this tuple, with the following mask */
139 struct ip_conntrack_tuple tuple
, mask
;
141 /* Function to call after setup and insertion */
142 void (*expectfn
)(struct ip_conntrack
*new,
143 struct ip_conntrack_expect
*this);
145 /* The conntrack of the master connection */
146 struct ip_conntrack
*master
;
148 /* Timer function; deletes the expectation. */
149 struct timer_list timeout
;
160 #ifdef CONFIG_IP_NF_NAT_NEEDED
162 /* This is the original per-proto part, used to map the
163 * expected connection the way the recipient expects. */
164 union ip_conntrack_manip_proto saved_proto
;
165 /* Direction relative to the master connection. */
166 enum ip_conntrack_dir dir
;
170 #define IP_CT_EXPECT_PERMANENT 0x1
172 static inline struct ip_conntrack
*
173 tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash
*hash
)
175 return container_of(hash
, struct ip_conntrack
,
176 tuplehash
[hash
->tuple
.dst
.dir
]);
179 /* get master conntrack via master expectation */
180 #define master_ct(conntr) (conntr->master)
182 /* Alter reply tuple (maybe alter helper). */
184 ip_conntrack_alter_reply(struct ip_conntrack
*conntrack
,
185 const struct ip_conntrack_tuple
*newreply
);
187 /* Is this tuple taken? (ignoring any belonging to the given
190 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple
*tuple
,
191 const struct ip_conntrack
*ignored_conntrack
);
193 /* Return conntrack_info and tuple hash for given skb. */
194 static inline struct ip_conntrack
*
195 ip_conntrack_get(const struct sk_buff
*skb
, enum ip_conntrack_info
*ctinfo
)
197 *ctinfo
= skb
->nfctinfo
;
198 return (struct ip_conntrack
*)skb
->nfct
;
201 /* decrement reference count on a conntrack */
203 ip_conntrack_put(struct ip_conntrack
*ct
)
206 nf_conntrack_put(&ct
->ct_general
);
209 extern int invert_tuplepr(struct ip_conntrack_tuple
*inverse
,
210 const struct ip_conntrack_tuple
*orig
);
212 extern void __ip_ct_refresh_acct(struct ip_conntrack
*ct
,
213 enum ip_conntrack_info ctinfo
,
214 const struct sk_buff
*skb
,
215 unsigned long extra_jiffies
,
218 /* Refresh conntrack for this many jiffies and do accounting */
219 static inline void ip_ct_refresh_acct(struct ip_conntrack
*ct
,
220 enum ip_conntrack_info ctinfo
,
221 const struct sk_buff
*skb
,
222 unsigned long extra_jiffies
)
224 __ip_ct_refresh_acct(ct
, ctinfo
, skb
, extra_jiffies
, 1);
227 /* Refresh conntrack for this many jiffies */
228 static inline void ip_ct_refresh(struct ip_conntrack
*ct
,
229 const struct sk_buff
*skb
,
230 unsigned long extra_jiffies
)
232 __ip_ct_refresh_acct(ct
, 0, skb
, extra_jiffies
, 0);
235 /* These are for NAT. Icky. */
236 /* Update TCP window tracking data when NAT mangles the packet */
237 extern void ip_conntrack_tcp_update(struct sk_buff
*skb
,
238 struct ip_conntrack
*conntrack
,
239 enum ip_conntrack_dir dir
);
241 /* Call me when a conntrack is destroyed. */
242 extern void (*ip_conntrack_destroyed
)(struct ip_conntrack
*conntrack
);
244 /* Fake conntrack entry for untracked connections */
245 extern struct ip_conntrack ip_conntrack_untracked
;
247 /* Returns new sk_buff, or NULL */
249 ip_ct_gather_frags(struct sk_buff
*skb
, u_int32_t user
);
251 /* Iterate over all conntracks: if iter returns true, it's deleted. */
253 ip_ct_iterate_cleanup(int (*iter
)(struct ip_conntrack
*i
, void *data
),
256 extern struct ip_conntrack_helper
*
257 __ip_conntrack_helper_find_byname(const char *);
258 extern struct ip_conntrack_helper
*
259 ip_conntrack_helper_find_get(const struct ip_conntrack_tuple
*tuple
);
260 extern void ip_conntrack_helper_put(struct ip_conntrack_helper
*helper
);
262 extern struct ip_conntrack_protocol
*
263 __ip_conntrack_proto_find(u_int8_t protocol
);
264 extern struct ip_conntrack_protocol
*
265 ip_conntrack_proto_find_get(u_int8_t protocol
);
266 extern void ip_conntrack_proto_put(struct ip_conntrack_protocol
*proto
);
268 extern void ip_ct_remove_expectations(struct ip_conntrack
*ct
);
270 extern struct ip_conntrack
*ip_conntrack_alloc(struct ip_conntrack_tuple
*,
271 struct ip_conntrack_tuple
*);
273 extern void ip_conntrack_free(struct ip_conntrack
*ct
);
275 extern void ip_conntrack_hash_insert(struct ip_conntrack
*ct
);
277 extern struct ip_conntrack_expect
*
278 __ip_conntrack_expect_find(const struct ip_conntrack_tuple
*tuple
);
280 extern struct ip_conntrack_expect
*
281 ip_conntrack_expect_find_get(const struct ip_conntrack_tuple
*tuple
);
283 extern struct ip_conntrack_tuple_hash
*
284 __ip_conntrack_find(const struct ip_conntrack_tuple
*tuple
,
285 const struct ip_conntrack
*ignored_conntrack
);
287 extern void ip_conntrack_flush(void);
289 /* It's confirmed if it is, or has been in the hash table. */
290 static inline int is_confirmed(struct ip_conntrack
*ct
)
292 return test_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
295 static inline int is_dying(struct ip_conntrack
*ct
)
297 return test_bit(IPS_DYING_BIT
, &ct
->status
);
300 extern unsigned int ip_conntrack_htable_size
;
301 extern int ip_conntrack_checksum
;
303 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
304 #define CONNTRACK_STAT_INC_ATOMIC(count) \
306 local_bh_disable(); \
307 __get_cpu_var(ip_conntrack_stat).count++; \
311 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
312 #include <linux/notifier.h>
313 #include <linux/interrupt.h>
315 struct ip_conntrack_ecache
{
316 struct ip_conntrack
*ct
;
319 DECLARE_PER_CPU(struct ip_conntrack_ecache
, ip_conntrack_ecache
);
321 #define CONNTRACK_ECACHE(x) (__get_cpu_var(ip_conntrack_ecache).x)
323 extern struct atomic_notifier_head ip_conntrack_chain
;
324 extern struct atomic_notifier_head ip_conntrack_expect_chain
;
326 static inline int ip_conntrack_register_notifier(struct notifier_block
*nb
)
328 return atomic_notifier_chain_register(&ip_conntrack_chain
, nb
);
331 static inline int ip_conntrack_unregister_notifier(struct notifier_block
*nb
)
333 return atomic_notifier_chain_unregister(&ip_conntrack_chain
, nb
);
337 ip_conntrack_expect_register_notifier(struct notifier_block
*nb
)
339 return atomic_notifier_chain_register(&ip_conntrack_expect_chain
, nb
);
343 ip_conntrack_expect_unregister_notifier(struct notifier_block
*nb
)
345 return atomic_notifier_chain_unregister(&ip_conntrack_expect_chain
,
349 extern void ip_ct_deliver_cached_events(const struct ip_conntrack
*ct
);
350 extern void __ip_ct_event_cache_init(struct ip_conntrack
*ct
);
353 ip_conntrack_event_cache(enum ip_conntrack_events event
,
354 const struct sk_buff
*skb
)
356 struct ip_conntrack
*ct
= (struct ip_conntrack
*)skb
->nfct
;
357 struct ip_conntrack_ecache
*ecache
;
360 ecache
= &__get_cpu_var(ip_conntrack_ecache
);
361 if (ct
!= ecache
->ct
)
362 __ip_ct_event_cache_init(ct
);
363 ecache
->events
|= event
;
367 static inline void ip_conntrack_event(enum ip_conntrack_events event
,
368 struct ip_conntrack
*ct
)
370 if (is_confirmed(ct
) && !is_dying(ct
))
371 atomic_notifier_call_chain(&ip_conntrack_chain
, event
, ct
);
375 ip_conntrack_expect_event(enum ip_conntrack_expect_events event
,
376 struct ip_conntrack_expect
*exp
)
378 atomic_notifier_call_chain(&ip_conntrack_expect_chain
, event
, exp
);
380 #else /* CONFIG_IP_NF_CONNTRACK_EVENTS */
381 static inline void ip_conntrack_event_cache(enum ip_conntrack_events event
,
382 const struct sk_buff
*skb
) {}
383 static inline void ip_conntrack_event(enum ip_conntrack_events event
,
384 struct ip_conntrack
*ct
) {}
385 static inline void ip_ct_deliver_cached_events(const struct ip_conntrack
*ct
) {}
387 ip_conntrack_expect_event(enum ip_conntrack_expect_events event
,
388 struct ip_conntrack_expect
*exp
) {}
389 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
391 #ifdef CONFIG_IP_NF_NAT_NEEDED
392 static inline int ip_nat_initialized(struct ip_conntrack
*conntrack
,
393 enum ip_nat_manip_type manip
)
395 if (manip
== IP_NAT_MANIP_SRC
)
396 return test_bit(IPS_SRC_NAT_DONE_BIT
, &conntrack
->status
);
397 return test_bit(IPS_DST_NAT_DONE_BIT
, &conntrack
->status
);
399 #endif /* CONFIG_IP_NF_NAT_NEEDED */
401 #endif /* __KERNEL__ */
402 #endif /* _IP_CONNTRACK_H */