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