]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/netfilter_ipv4/ip_conntrack.h
Merge /spare/repo/linux-2.6/
[linux-2.6-omap-h63xx.git] / include / linux / netfilter_ipv4 / ip_conntrack.h
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
5    extension. */
6 enum 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. */
27 enum 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),
68
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 */
75 enum 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
122 enum ip_conntrack_expect_events {
123         IPEXP_NEW_BIT = 0,
124         IPEXP_NEW = (1 << IPEXP_NEW_BIT),
125 };
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_proto_gre.h>
137 #include <linux/netfilter_ipv4/ip_conntrack_sctp.h>
138
139 /* per conntrack: protocol private data */
140 union ip_conntrack_proto {
141         /* insert conntrack proto private data here */
142         struct ip_ct_gre gre;
143         struct ip_ct_sctp sctp;
144         struct ip_ct_tcp tcp;
145         struct ip_ct_icmp icmp;
146 };
147
148 union ip_conntrack_expect_proto {
149         /* insert expect proto private data here */
150 };
151
152 /* Add protocol helper include file here */
153 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
154 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
155 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
156 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
157
158 /* per conntrack: application helper private data */
159 union ip_conntrack_help {
160         /* insert conntrack helper private data (master) here */
161         struct ip_ct_pptp_master ct_pptp_info;
162         struct ip_ct_ftp_master ct_ftp_info;
163         struct ip_ct_irc_master ct_irc_info;
164 };
165
166 #ifdef CONFIG_IP_NF_NAT_NEEDED
167 #include <linux/netfilter_ipv4/ip_nat.h>
168 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
169
170 /* per conntrack: nat application helper private data */
171 union ip_conntrack_nat_help {
172         /* insert nat helper private data here */
173         struct ip_nat_pptp nat_pptp_info;
174 };
175 #endif
176
177 #include <linux/types.h>
178 #include <linux/skbuff.h>
179
180 #ifdef CONFIG_NETFILTER_DEBUG
181 #define IP_NF_ASSERT(x)                                                 \
182 do {                                                                    \
183         if (!(x))                                                       \
184                 /* Wooah!  I'm tripping my conntrack in a frenzy of     \
185                    netplay... */                                        \
186                 printk("NF_IP_ASSERT: %s:%i(%s)\n",                     \
187                        __FILE__, __LINE__, __FUNCTION__);               \
188 } while(0)
189 #else
190 #define IP_NF_ASSERT(x)
191 #endif
192
193 struct ip_conntrack_counter
194 {
195         u_int64_t packets;
196         u_int64_t bytes;
197 };
198
199 struct ip_conntrack_helper;
200
201 struct ip_conntrack
202 {
203         /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
204            plus 1 for any connection(s) we are `master' for */
205         struct nf_conntrack ct_general;
206
207         /* Have we seen traffic both ways yet? (bitset) */
208         unsigned long status;
209
210         /* Timer function; drops refcnt when it goes off. */
211         struct timer_list timeout;
212
213 #ifdef CONFIG_IP_NF_CT_ACCT
214         /* Accounting Information (same cache line as other written members) */
215         struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
216 #endif
217         /* If we were expected by an expectation, this will be it */
218         struct ip_conntrack *master;
219
220         /* Current number of expected connections */
221         unsigned int expecting;
222
223         /* Unique ID that identifies this conntrack*/
224         unsigned int id;
225
226         /* Helper, if any. */
227         struct ip_conntrack_helper *helper;
228
229         /* Storage reserved for other modules: */
230         union ip_conntrack_proto proto;
231
232         union ip_conntrack_help help;
233
234 #ifdef CONFIG_IP_NF_NAT_NEEDED
235         struct {
236                 struct ip_nat_info info;
237                 union ip_conntrack_nat_help help;
238 #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \
239         defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE)
240                 int masq_index;
241 #endif
242         } nat;
243 #endif /* CONFIG_IP_NF_NAT_NEEDED */
244
245 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
246         u_int32_t mark;
247 #endif
248
249         /* Traversed often, so hopefully in different cacheline to top */
250         /* These are my tuples; original and reply */
251         struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
252 };
253
254 struct ip_conntrack_expect
255 {
256         /* Internal linked list (global expectation list) */
257         struct list_head list;
258
259         /* We expect this tuple, with the following mask */
260         struct ip_conntrack_tuple tuple, mask;
261  
262         /* Function to call after setup and insertion */
263         void (*expectfn)(struct ip_conntrack *new,
264                          struct ip_conntrack_expect *this);
265
266         /* The conntrack of the master connection */
267         struct ip_conntrack *master;
268
269         /* Timer function; deletes the expectation. */
270         struct timer_list timeout;
271
272         /* Usage count. */
273         atomic_t use;
274
275         /* Unique ID */
276         unsigned int id;
277
278         /* Flags */
279         unsigned int flags;
280
281 #ifdef CONFIG_IP_NF_NAT_NEEDED
282         /* This is the original per-proto part, used to map the
283          * expected connection the way the recipient expects. */
284         union ip_conntrack_manip_proto saved_proto;
285         /* Direction relative to the master connection. */
286         enum ip_conntrack_dir dir;
287 #endif
288 };
289
290 #define IP_CT_EXPECT_PERMANENT  0x1
291
292 static inline struct ip_conntrack *
293 tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash *hash)
294 {
295         return container_of(hash, struct ip_conntrack,
296                             tuplehash[hash->tuple.dst.dir]);
297 }
298
299 /* get master conntrack via master expectation */
300 #define master_ct(conntr) (conntr->master)
301
302 /* Alter reply tuple (maybe alter helper). */
303 extern void
304 ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
305                          const struct ip_conntrack_tuple *newreply);
306
307 /* Is this tuple taken? (ignoring any belonging to the given
308    conntrack). */
309 extern int
310 ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
311                          const struct ip_conntrack *ignored_conntrack);
312
313 /* Return conntrack_info and tuple hash for given skb. */
314 static inline struct ip_conntrack *
315 ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
316 {
317         *ctinfo = skb->nfctinfo;
318         return (struct ip_conntrack *)skb->nfct;
319 }
320
321 /* decrement reference count on a conntrack */
322 static inline void
323 ip_conntrack_put(struct ip_conntrack *ct)
324 {
325         IP_NF_ASSERT(ct);
326         nf_conntrack_put(&ct->ct_general);
327 }
328
329 /* call to create an explicit dependency on ip_conntrack. */
330 extern void need_ip_conntrack(void);
331
332 extern int invert_tuplepr(struct ip_conntrack_tuple *inverse,
333                           const struct ip_conntrack_tuple *orig);
334
335 extern void __ip_ct_refresh_acct(struct ip_conntrack *ct,
336                                  enum ip_conntrack_info ctinfo,
337                                  const struct sk_buff *skb,
338                                  unsigned long extra_jiffies,
339                                  int do_acct);
340
341 /* Refresh conntrack for this many jiffies and do accounting */
342 static inline void ip_ct_refresh_acct(struct ip_conntrack *ct, 
343                                       enum ip_conntrack_info ctinfo,
344                                       const struct sk_buff *skb,
345                                       unsigned long extra_jiffies)
346 {
347         __ip_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
348 }
349
350 /* Refresh conntrack for this many jiffies */
351 static inline void ip_ct_refresh(struct ip_conntrack *ct,
352                                  const struct sk_buff *skb,
353                                  unsigned long extra_jiffies)
354 {
355         __ip_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
356 }
357
358 /* These are for NAT.  Icky. */
359 /* Update TCP window tracking data when NAT mangles the packet */
360 extern void ip_conntrack_tcp_update(struct sk_buff *skb,
361                                     struct ip_conntrack *conntrack,
362                                     enum ip_conntrack_dir dir);
363
364 /* Call me when a conntrack is destroyed. */
365 extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack);
366
367 /* Fake conntrack entry for untracked connections */
368 extern struct ip_conntrack ip_conntrack_untracked;
369
370 /* Returns new sk_buff, or NULL */
371 struct sk_buff *
372 ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user);
373
374 /* Iterate over all conntracks: if iter returns true, it's deleted. */
375 extern void
376 ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data),
377                       void *data);
378
379 extern struct ip_conntrack_helper *
380 __ip_conntrack_helper_find_byname(const char *);
381 extern struct ip_conntrack_helper *
382 ip_conntrack_helper_find_get(const struct ip_conntrack_tuple *tuple);
383 extern void ip_conntrack_helper_put(struct ip_conntrack_helper *helper);
384
385 extern struct ip_conntrack_protocol *
386 __ip_conntrack_proto_find(u_int8_t protocol);
387 extern struct ip_conntrack_protocol *
388 ip_conntrack_proto_find_get(u_int8_t protocol);
389 extern void ip_conntrack_proto_put(struct ip_conntrack_protocol *proto);
390
391 extern void ip_ct_remove_expectations(struct ip_conntrack *ct);
392
393 extern struct ip_conntrack *ip_conntrack_alloc(struct ip_conntrack_tuple *,
394                                                struct ip_conntrack_tuple *);
395
396 extern void ip_conntrack_free(struct ip_conntrack *ct);
397
398 extern void ip_conntrack_hash_insert(struct ip_conntrack *ct);
399
400 extern struct ip_conntrack_expect *
401 __ip_conntrack_expect_find(const struct ip_conntrack_tuple *tuple);
402
403 extern struct ip_conntrack_expect *
404 ip_conntrack_expect_find(const struct ip_conntrack_tuple *tuple);
405
406 extern struct ip_conntrack_tuple_hash *
407 __ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
408                     const struct ip_conntrack *ignored_conntrack);
409
410 extern void ip_conntrack_flush(void);
411
412 /* It's confirmed if it is, or has been in the hash table. */
413 static inline int is_confirmed(struct ip_conntrack *ct)
414 {
415         return test_bit(IPS_CONFIRMED_BIT, &ct->status);
416 }
417
418 static inline int is_dying(struct ip_conntrack *ct)
419 {
420         return test_bit(IPS_DYING_BIT, &ct->status);
421 }
422
423 extern unsigned int ip_conntrack_htable_size;
424  
425 struct ip_conntrack_stat
426 {
427         unsigned int searched;
428         unsigned int found;
429         unsigned int new;
430         unsigned int invalid;
431         unsigned int ignore;
432         unsigned int delete;
433         unsigned int delete_list;
434         unsigned int insert;
435         unsigned int insert_failed;
436         unsigned int drop;
437         unsigned int early_drop;
438         unsigned int error;
439         unsigned int expect_new;
440         unsigned int expect_create;
441         unsigned int expect_delete;
442 };
443
444 #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++)
445
446 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
447 #include <linux/notifier.h>
448 #include <linux/interrupt.h>
449
450 struct ip_conntrack_ecache {
451         struct ip_conntrack *ct;
452         unsigned int events;
453 };
454 DECLARE_PER_CPU(struct ip_conntrack_ecache, ip_conntrack_ecache);
455
456 #define CONNTRACK_ECACHE(x)     (__get_cpu_var(ip_conntrack_ecache).x)
457  
458 extern struct notifier_block *ip_conntrack_chain;
459 extern struct notifier_block *ip_conntrack_expect_chain;
460
461 static inline int ip_conntrack_register_notifier(struct notifier_block *nb)
462 {
463         return notifier_chain_register(&ip_conntrack_chain, nb);
464 }
465
466 static inline int ip_conntrack_unregister_notifier(struct notifier_block *nb)
467 {
468         return notifier_chain_unregister(&ip_conntrack_chain, nb);
469 }
470
471 static inline int 
472 ip_conntrack_expect_register_notifier(struct notifier_block *nb)
473 {
474         return notifier_chain_register(&ip_conntrack_expect_chain, nb);
475 }
476
477 static inline int
478 ip_conntrack_expect_unregister_notifier(struct notifier_block *nb)
479 {
480         return notifier_chain_unregister(&ip_conntrack_expect_chain, nb);
481 }
482
483 extern void ip_ct_deliver_cached_events(const struct ip_conntrack *ct);
484 extern void __ip_ct_event_cache_init(struct ip_conntrack *ct);
485
486 static inline void 
487 ip_conntrack_event_cache(enum ip_conntrack_events event,
488                          const struct sk_buff *skb)
489 {
490         struct ip_conntrack *ct = (struct ip_conntrack *)skb->nfct;
491         struct ip_conntrack_ecache *ecache;
492         
493         local_bh_disable();
494         ecache = &__get_cpu_var(ip_conntrack_ecache);
495         if (ct != ecache->ct)
496                 __ip_ct_event_cache_init(ct);
497         ecache->events |= event;
498         local_bh_enable();
499 }
500
501 static inline void ip_conntrack_event(enum ip_conntrack_events event,
502                                       struct ip_conntrack *ct)
503 {
504         if (is_confirmed(ct) && !is_dying(ct))
505                 notifier_call_chain(&ip_conntrack_chain, event, ct);
506 }
507
508 static inline void 
509 ip_conntrack_expect_event(enum ip_conntrack_expect_events event,
510                           struct ip_conntrack_expect *exp)
511 {
512         notifier_call_chain(&ip_conntrack_expect_chain, event, exp);
513 }
514 #else /* CONFIG_IP_NF_CONNTRACK_EVENTS */
515 static inline void ip_conntrack_event_cache(enum ip_conntrack_events event, 
516                                             const struct sk_buff *skb) {}
517 static inline void ip_conntrack_event(enum ip_conntrack_events event, 
518                                       struct ip_conntrack *ct) {}
519 static inline void ip_ct_deliver_cached_events(const struct ip_conntrack *ct) {}
520 static inline void 
521 ip_conntrack_expect_event(enum ip_conntrack_expect_events event, 
522                           struct ip_conntrack_expect *exp) {}
523 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
524
525 #ifdef CONFIG_IP_NF_NAT_NEEDED
526 static inline int ip_nat_initialized(struct ip_conntrack *conntrack,
527                                      enum ip_nat_manip_type manip)
528 {
529         if (manip == IP_NAT_MANIP_SRC)
530                 return test_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status);
531         return test_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status);
532 }
533 #endif /* CONFIG_IP_NF_NAT_NEEDED */
534
535 #endif /* __KERNEL__ */
536 #endif /* _IP_CONNTRACK_H */