blob: 7e033e9271a88efbb2b214e4834176147d33d28f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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
5 extension. */
6enum ip_conntrack_info
7{
8 /* Part of an established connection (either direction). */
9 IP_CT_ESTABLISHED,
10
11 /* Like NEW, but related to an existing connection, or ICMP error
12 (in either direction). */
13 IP_CT_RELATED,
14
15 /* Started a new connection to track (only
16 IP_CT_DIR_ORIGINAL); may be a retransmission. */
17 IP_CT_NEW,
18
19 /* >= this indicates reply direction */
20 IP_CT_IS_REPLY,
21
22 /* Number of distinct IP_CT types (no NEW in reply dirn). */
23 IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1
24};
25
26/* Bitset representing status of connection. */
27enum ip_conntrack_status {
28 /* It's an expected connection: bit 0 set. This bit never changed */
29 IPS_EXPECTED_BIT = 0,
30 IPS_EXPECTED = (1 << IPS_EXPECTED_BIT),
31
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),
35
36 /* Conntrack should never be early-expired. */
37 IPS_ASSURED_BIT = 2,
38 IPS_ASSURED = (1 << IPS_ASSURED_BIT),
39
40 /* Connection is confirmed: originating packet has left box */
41 IPS_CONFIRMED_BIT = 3,
42 IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT),
43
44 /* Connection needs src nat in orig dir. This bit never changed. */
45 IPS_SRC_NAT_BIT = 4,
46 IPS_SRC_NAT = (1 << IPS_SRC_NAT_BIT),
47
48 /* Connection needs dst nat in orig dir. This bit never changed. */
49 IPS_DST_NAT_BIT = 5,
50 IPS_DST_NAT = (1 << IPS_DST_NAT_BIT),
51
52 /* Both together. */
53 IPS_NAT_MASK = (IPS_DST_NAT | IPS_SRC_NAT),
54
55 /* Connection needs TCP sequence adjusted. */
56 IPS_SEQ_ADJUST_BIT = 6,
57 IPS_SEQ_ADJUST = (1 << IPS_SEQ_ADJUST_BIT),
58
59 /* NAT initialization bits. */
60 IPS_SRC_NAT_DONE_BIT = 7,
61 IPS_SRC_NAT_DONE = (1 << IPS_SRC_NAT_DONE_BIT),
62
63 IPS_DST_NAT_DONE_BIT = 8,
64 IPS_DST_NAT_DONE = (1 << IPS_DST_NAT_DONE_BIT),
65
66 /* Both together */
67 IPS_NAT_DONE_MASK = (IPS_DST_NAT_DONE | IPS_SRC_NAT_DONE),
Harald Welteac3247b2005-08-09 19:28:03 -070068
69 /* Connection is dying (removed from lists), can not be unset. */
70 IPS_DYING_BIT = 9,
71 IPS_DYING = (1 << IPS_DYING_BIT),
72};
73
74/* Connection tracking event bits */
75enum ip_conntrack_events
76{
77 /* New conntrack */
78 IPCT_NEW_BIT = 0,
79 IPCT_NEW = (1 << IPCT_NEW_BIT),
80
81 /* Expected connection */
82 IPCT_RELATED_BIT = 1,
83 IPCT_RELATED = (1 << IPCT_RELATED_BIT),
84
85 /* Destroyed conntrack */
86 IPCT_DESTROY_BIT = 2,
87 IPCT_DESTROY = (1 << IPCT_DESTROY_BIT),
88
89 /* Timer has been refreshed */
90 IPCT_REFRESH_BIT = 3,
91 IPCT_REFRESH = (1 << IPCT_REFRESH_BIT),
92
93 /* Status has changed */
94 IPCT_STATUS_BIT = 4,
95 IPCT_STATUS = (1 << IPCT_STATUS_BIT),
96
97 /* Update of protocol info */
98 IPCT_PROTOINFO_BIT = 5,
99 IPCT_PROTOINFO = (1 << IPCT_PROTOINFO_BIT),
100
101 /* Volatile protocol info */
102 IPCT_PROTOINFO_VOLATILE_BIT = 6,
103 IPCT_PROTOINFO_VOLATILE = (1 << IPCT_PROTOINFO_VOLATILE_BIT),
104
105 /* New helper for conntrack */
106 IPCT_HELPER_BIT = 7,
107 IPCT_HELPER = (1 << IPCT_HELPER_BIT),
108
109 /* Update of helper info */
110 IPCT_HELPINFO_BIT = 8,
111 IPCT_HELPINFO = (1 << IPCT_HELPINFO_BIT),
112
113 /* Volatile helper info */
114 IPCT_HELPINFO_VOLATILE_BIT = 9,
115 IPCT_HELPINFO_VOLATILE = (1 << IPCT_HELPINFO_VOLATILE_BIT),
116
117 /* NAT info */
118 IPCT_NATINFO_BIT = 10,
119 IPCT_NATINFO = (1 << IPCT_NATINFO_BIT),
120};
121
122enum ip_conntrack_expect_events {
123 IPEXP_NEW_BIT = 0,
124 IPEXP_NEW = (1 << IPEXP_NEW_BIT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127#ifdef __KERNEL__
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>
133
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>
137
138/* per conntrack: protocol private data */
139union 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;
144};
145
146union ip_conntrack_expect_proto {
147 /* insert expect proto private data here */
148};
149
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>
154
155/* per conntrack: application helper private data */
156union 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;
160};
161
162#ifdef CONFIG_IP_NF_NAT_NEEDED
163#include <linux/netfilter_ipv4/ip_nat.h>
164#endif
165
166#include <linux/types.h>
167#include <linux/skbuff.h>
168
169#ifdef CONFIG_NETFILTER_DEBUG
170#define IP_NF_ASSERT(x) \
171do { \
172 if (!(x)) \
173 /* Wooah! I'm tripping my conntrack in a frenzy of \
174 netplay... */ \
175 printk("NF_IP_ASSERT: %s:%i(%s)\n", \
176 __FILE__, __LINE__, __FUNCTION__); \
177} while(0)
178#else
179#define IP_NF_ASSERT(x)
180#endif
181
182struct ip_conntrack_counter
183{
184 u_int64_t packets;
185 u_int64_t bytes;
186};
187
188struct ip_conntrack_helper;
189
190struct ip_conntrack
191{
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;
195
196 /* Have we seen traffic both ways yet? (bitset) */
197 unsigned long status;
198
199 /* Timer function; drops refcnt when it goes off. */
200 struct timer_list timeout;
201
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];
205#endif
206 /* If we were expected by an expectation, this will be it */
207 struct ip_conntrack *master;
208
209 /* Current number of expected connections */
210 unsigned int expecting;
211
Harald Welte080774a2005-08-09 19:32:58 -0700212 /* Unique ID that identifies this conntrack*/
213 unsigned int id;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Helper, if any. */
216 struct ip_conntrack_helper *helper;
217
218 /* Storage reserved for other modules: */
219 union ip_conntrack_proto proto;
220
221 union ip_conntrack_help help;
222
223#ifdef CONFIG_IP_NF_NAT_NEEDED
224 struct {
225 struct ip_nat_info info;
226#if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
227 defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
228 int masq_index;
229#endif
230 } nat;
231#endif /* CONFIG_IP_NF_NAT_NEEDED */
232
233#if defined(CONFIG_IP_NF_CONNTRACK_MARK)
Harald Weltebf3a46a2005-08-09 19:22:01 -0700234 u_int32_t mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif
236
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];
240};
241
242struct ip_conntrack_expect
243{
244 /* Internal linked list (global expectation list) */
245 struct list_head list;
246
247 /* We expect this tuple, with the following mask */
248 struct ip_conntrack_tuple tuple, mask;
249
250 /* Function to call after setup and insertion */
251 void (*expectfn)(struct ip_conntrack *new,
252 struct ip_conntrack_expect *this);
253
254 /* The conntrack of the master connection */
255 struct ip_conntrack *master;
256
257 /* Timer function; deletes the expectation. */
258 struct timer_list timeout;
259
Rusty Russell4acdbdb2005-07-21 13:14:46 -0700260 /* Usage count. */
261 atomic_t use;
262
Harald Welte080774a2005-08-09 19:32:58 -0700263 /* Unique ID */
264 unsigned int id;
265
Patrick McHardy2248bcf2005-09-06 15:06:42 -0700266 /* Flags */
267 unsigned int flags;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#ifdef CONFIG_IP_NF_NAT_NEEDED
270 /* This is the original per-proto part, used to map the
271 * expected connection the way the recipient expects. */
272 union ip_conntrack_manip_proto saved_proto;
273 /* Direction relative to the master connection. */
274 enum ip_conntrack_dir dir;
275#endif
276};
277
Patrick McHardy2248bcf2005-09-06 15:06:42 -0700278#define IP_CT_EXPECT_PERMANENT 0x1
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static inline struct ip_conntrack *
281tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash *hash)
282{
283 return container_of(hash, struct ip_conntrack,
284 tuplehash[hash->tuple.dst.dir]);
285}
286
287/* get master conntrack via master expectation */
288#define master_ct(conntr) (conntr->master)
289
290/* Alter reply tuple (maybe alter helper). */
291extern void
292ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
293 const struct ip_conntrack_tuple *newreply);
294
295/* Is this tuple taken? (ignoring any belonging to the given
296 conntrack). */
297extern int
298ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
299 const struct ip_conntrack *ignored_conntrack);
300
301/* Return conntrack_info and tuple hash for given skb. */
302static inline struct ip_conntrack *
303ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
304{
305 *ctinfo = skb->nfctinfo;
306 return (struct ip_conntrack *)skb->nfct;
307}
308
309/* decrement reference count on a conntrack */
Harald Welte080774a2005-08-09 19:32:58 -0700310static inline void
311ip_conntrack_put(struct ip_conntrack *ct)
312{
313 IP_NF_ASSERT(ct);
314 nf_conntrack_put(&ct->ct_general);
315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/* call to create an explicit dependency on ip_conntrack. */
318extern void need_ip_conntrack(void);
319
320extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
321 const struct ip_conntrack_tuple *orig);
322
323/* Refresh conntrack for this many jiffies */
324extern void ip_ct_refresh_acct(struct ip_conntrack *ct,
325 enum ip_conntrack_info ctinfo,
326 const struct sk_buff *skb,
327 unsigned long extra_jiffies);
328
329/* These are for NAT. Icky. */
330/* Update TCP window tracking data when NAT mangles the packet */
331extern void ip_conntrack_tcp_update(struct sk_buff *skb,
332 struct ip_conntrack *conntrack,
333 enum ip_conntrack_dir dir);
334
335/* Call me when a conntrack is destroyed. */
336extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
337
338/* Fake conntrack entry for untracked connections */
339extern struct ip_conntrack ip_conntrack_untracked;
340
341/* Returns new sk_buff, or NULL */
342struct sk_buff *
343ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user);
344
345/* Iterate over all conntracks: if iter returns true, it's deleted. */
346extern void
347ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data),
348 void *data);
349
Harald Welte080774a2005-08-09 19:32:58 -0700350extern struct ip_conntrack_helper *
351__ip_conntrack_helper_find_byname(const char *);
352extern struct ip_conntrack_helper *
353ip_conntrack_helper_find_get(const struct ip_conntrack_tuple *tuple);
354extern void ip_conntrack_helper_put(struct ip_conntrack_helper *helper);
355
356extern struct ip_conntrack_protocol *
357__ip_conntrack_proto_find(u_int8_t protocol);
358extern struct ip_conntrack_protocol *
359ip_conntrack_proto_find_get(u_int8_t protocol);
360extern void ip_conntrack_proto_put(struct ip_conntrack_protocol *proto);
361
362extern void ip_ct_remove_expectations(struct ip_conntrack *ct);
363
364extern struct ip_conntrack *ip_conntrack_alloc(struct ip_conntrack_tuple *,
365 struct ip_conntrack_tuple *);
366
367extern void ip_conntrack_free(struct ip_conntrack *ct);
368
369extern void ip_conntrack_hash_insert(struct ip_conntrack *ct);
370
371extern struct ip_conntrack_expect *
372__ip_conntrack_expect_find(const struct ip_conntrack_tuple *tuple);
373
374extern struct ip_conntrack_expect *
375ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple);
376
377extern struct ip_conntrack_tuple_hash *
378__ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
379 const struct ip_conntrack *ignored_conntrack);
380
381extern void ip_conntrack_flush(void);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* It's confirmed if it is, or has been in the hash table. */
384static inline int is_confirmed(struct ip_conntrack *ct)
385{
386 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
387}
388
Harald Welteac3247b2005-08-09 19:28:03 -0700389static inline int is_dying(struct ip_conntrack *ct)
390{
391 return test_bit(IPS_DYING_BIT, &ct->status);
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394extern unsigned int ip_conntrack_htable_size;
395
396struct ip_conntrack_stat
397{
398 unsigned int searched;
399 unsigned int found;
400 unsigned int new;
401 unsigned int invalid;
402 unsigned int ignore;
403 unsigned int delete;
404 unsigned int delete_list;
405 unsigned int insert;
406 unsigned int insert_failed;
407 unsigned int drop;
408 unsigned int early_drop;
409 unsigned int error;
410 unsigned int expect_new;
411 unsigned int expect_create;
412 unsigned int expect_delete;
413};
414
415#define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
416
Harald Welteac3247b2005-08-09 19:28:03 -0700417#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
418#include <linux/notifier.h>
Patrick McHardya86888b2005-08-09 20:02:13 -0700419#include <linux/interrupt.h>
Harald Welteac3247b2005-08-09 19:28:03 -0700420
421struct ip_conntrack_ecache {
422 struct ip_conntrack *ct;
423 unsigned int events;
424};
425DECLARE_PER_CPU(struct ip_conntrack_ecache, ip_conntrack_ecache);
426
427#define CONNTRACK_ECACHE(x) (__get_cpu_var(ip_conntrack_ecache).x)
428
429extern struct notifier_block *ip_conntrack_chain;
430extern struct notifier_block *ip_conntrack_expect_chain;
431
432static inline int ip_conntrack_register_notifier(struct notifier_block *nb)
433{
434 return notifier_chain_register(&ip_conntrack_chain, nb);
435}
436
437static inline int ip_conntrack_unregister_notifier(struct notifier_block *nb)
438{
439 return notifier_chain_unregister(&ip_conntrack_chain, nb);
440}
441
442static inline int
443ip_conntrack_expect_register_notifier(struct notifier_block *nb)
444{
445 return notifier_chain_register(&ip_conntrack_expect_chain, nb);
446}
447
448static inline int
449ip_conntrack_expect_unregister_notifier(struct notifier_block *nb)
450{
451 return notifier_chain_unregister(&ip_conntrack_expect_chain, nb);
452}
453
Patrick McHardya86888b2005-08-09 20:02:13 -0700454extern void ip_ct_deliver_cached_events(const struct ip_conntrack *ct);
455extern void __ip_ct_event_cache_init(struct ip_conntrack *ct);
456
Harald Welteac3247b2005-08-09 19:28:03 -0700457static inline void
458ip_conntrack_event_cache(enum ip_conntrack_events event,
459 const struct sk_buff *skb)
460{
Patrick McHardya86888b2005-08-09 20:02:13 -0700461 struct ip_conntrack *ct = (struct ip_conntrack *)skb->nfct;
462 struct ip_conntrack_ecache *ecache;
463
464 local_bh_disable();
465 ecache = &__get_cpu_var(ip_conntrack_ecache);
466 if (ct != ecache->ct)
467 __ip_ct_event_cache_init(ct);
Harald Welteac3247b2005-08-09 19:28:03 -0700468 ecache->events |= event;
Patrick McHardya86888b2005-08-09 20:02:13 -0700469 local_bh_enable();
Harald Welteac3247b2005-08-09 19:28:03 -0700470}
471
Harald Welteac3247b2005-08-09 19:28:03 -0700472static inline void ip_conntrack_event(enum ip_conntrack_events event,
473 struct ip_conntrack *ct)
474{
475 if (is_confirmed(ct) && !is_dying(ct))
476 notifier_call_chain(&ip_conntrack_chain, event, ct);
477}
478
479static inline void
480ip_conntrack_expect_event(enum ip_conntrack_expect_events event,
481 struct ip_conntrack_expect *exp)
482{
483 notifier_call_chain(&ip_conntrack_expect_chain, event, exp);
484}
485#else /* CONFIG_IP_NF_CONNTRACK_EVENTS */
486static inline void ip_conntrack_event_cache(enum ip_conntrack_events event,
487 const struct sk_buff *skb) {}
488static inline void ip_conntrack_event(enum ip_conntrack_events event,
489 struct ip_conntrack *ct) {}
Patrick McHardya86888b2005-08-09 20:02:13 -0700490static inline void ip_ct_deliver_cached_events(const struct ip_conntrack *ct) {}
Harald Welteac3247b2005-08-09 19:28:03 -0700491static inline void
492ip_conntrack_expect_event(enum ip_conntrack_expect_events event,
493 struct ip_conntrack_expect *exp) {}
494#endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#ifdef CONFIG_IP_NF_NAT_NEEDED
497static inline int ip_nat_initialized(struct ip_conntrack *conntrack,
498 enum ip_nat_manip_type manip)
499{
500 if (manip == IP_NAT_MANIP_SRC)
501 return test_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status);
502 return test_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status);
503}
504#endif /* CONFIG_IP_NF_NAT_NEEDED */
505
506#endif /* __KERNEL__ */
507#endif /* _IP_CONNTRACK_H */