]> bbs.cooldavid.org Git - net-next-2.6.git/blob - include/net/netfilter/nf_conntrack.h
[NETFILTER]: Add nf_conntrack subsystem.
[net-next-2.6.git] / include / net / netfilter / nf_conntrack.h
1 /*
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
4  * extension.
5  *
6  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
7  *      - generalize L3 protocol dependent part.
8  *
9  * Derived from include/linux/netfiter_ipv4/ip_conntrack.h
10  */
11
12 #ifndef _NF_CONNTRACK_H
13 #define _NF_CONNTRACK_H
14
15 #include <linux/netfilter/nf_conntrack_common.h>
16
17 #ifdef __KERNEL__
18 #include <linux/config.h>
19 #include <linux/bitops.h>
20 #include <linux/compiler.h>
21 #include <asm/atomic.h>
22
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>
27
28 #include <net/netfilter/nf_conntrack_tuple.h>
29
30 /* per conntrack: protocol private data */
31 union nf_conntrack_proto {
32         /* insert conntrack proto private data here */
33         struct ip_ct_sctp sctp;
34         struct ip_ct_tcp tcp;
35         struct ip_ct_icmp icmp;
36         struct nf_ct_icmpv6 icmpv6;
37 };
38
39 union nf_conntrack_expect_proto {
40         /* insert expect proto private data here */
41 };
42
43 /* Add protocol helper include file here */
44 #include <linux/netfilter/nf_conntrack_ftp.h>
45
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;
50 };
51
52 #include <linux/types.h>
53 #include <linux/skbuff.h>
54
55 #ifdef CONFIG_NETFILTER_DEBUG
56 #define NF_CT_ASSERT(x)                                                 \
57 do {                                                                    \
58         if (!(x))                                                       \
59                 /* Wooah!  I'm tripping my conntrack in a frenzy of     \
60                    netplay... */                                        \
61                 printk("NF_CT_ASSERT: %s:%i(%s)\n",                     \
62                        __FILE__, __LINE__, __FUNCTION__);               \
63 } while(0)
64 #else
65 #define NF_CT_ASSERT(x)
66 #endif
67
68 struct nf_conntrack_helper;
69
70 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
71 struct nf_conn
72 {
73         /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
74            plus 1 for any connection(s) we are `master' for */
75         struct nf_conntrack ct_general;
76
77         /* XXX should I move this to the tail ? - Y.K */
78         /* These are my tuples; original and reply */
79         struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
80
81         /* Have we seen traffic both ways yet? (bitset) */
82         unsigned long status;
83
84         /* Timer function; drops refcnt when it goes off. */
85         struct timer_list timeout;
86
87 #ifdef CONFIG_NF_CT_ACCT
88         /* Accounting Information (same cache line as other written members) */
89         struct ip_conntrack_counter counters[IP_CT_DIR_MAX];
90 #endif
91         /* If we were expected by an expectation, this will be it */
92         struct nf_conn *master;
93         
94         /* Current number of expected connections */
95         unsigned int expecting;
96
97         /* Helper. if any */
98         struct nf_conntrack_helper *helper;
99
100         /* features - nat, helper, ... used by allocating system */
101         u_int32_t features;
102
103         /* Storage reserved for other modules: */
104
105         union nf_conntrack_proto proto;
106
107 #if defined(CONFIG_NF_CONNTRACK_MARK)
108         u_int32_t mark;
109 #endif
110
111         /* These members are dynamically allocated. */
112
113         union nf_conntrack_help *help;
114
115         /* Layer 3 dependent members. (ex: NAT) */
116         union {
117                 struct nf_conntrack_ipv4 *ipv4;
118         } l3proto;
119         void *data[0];
120 };
121
122 struct nf_conntrack_expect
123 {
124         /* Internal linked list (global expectation list) */
125         struct list_head list;
126
127         /* We expect this tuple, with the following mask */
128         struct nf_conntrack_tuple tuple, mask;
129  
130         /* Function to call after setup and insertion */
131         void (*expectfn)(struct nf_conn *new,
132                          struct nf_conntrack_expect *this);
133
134         /* The conntrack of the master connection */
135         struct nf_conn *master;
136
137         /* Timer function; deletes the expectation. */
138         struct timer_list timeout;
139
140         /* Usage count. */
141         atomic_t use;
142
143         /* Flags */
144         unsigned int flags;
145
146 #ifdef CONFIG_NF_NAT_NEEDED
147         /* This is the original per-proto part, used to map the
148          * expected connection the way the recipient expects. */
149         union nf_conntrack_manip_proto saved_proto;
150         /* Direction relative to the master connection. */
151         enum ip_conntrack_dir dir;
152 #endif
153 };
154
155 #define NF_CT_EXPECT_PERMANENT 0x1
156
157 static inline struct nf_conn *
158 nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
159 {
160         return container_of(hash, struct nf_conn,
161                             tuplehash[hash->tuple.dst.dir]);
162 }
163
164 /* get master conntrack via master expectation */
165 #define master_ct(conntr) (conntr->master)
166
167 /* Alter reply tuple (maybe alter helper). */
168 extern void
169 nf_conntrack_alter_reply(struct nf_conn *conntrack,
170                          const struct nf_conntrack_tuple *newreply);
171
172 /* Is this tuple taken? (ignoring any belonging to the given
173    conntrack). */
174 extern int
175 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
176                          const struct nf_conn *ignored_conntrack);
177
178 /* Return conntrack_info and tuple hash for given skb. */
179 static inline struct nf_conn *
180 nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
181 {
182         *ctinfo = skb->nfctinfo;
183         return (struct nf_conn *)skb->nfct;
184 }
185
186 /* decrement reference count on a conntrack */
187 static inline void nf_ct_put(struct nf_conn *ct)
188 {
189         NF_CT_ASSERT(ct);
190         nf_conntrack_put(&ct->ct_general);
191 }
192
193 /* call to create an explicit dependency on nf_conntrack. */
194 extern void need_nf_conntrack(void);
195
196 extern int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
197                                 const struct nf_conntrack_tuple *orig);
198
199 extern void __nf_ct_refresh_acct(struct nf_conn *ct,
200                                  enum ip_conntrack_info ctinfo,
201                                  const struct sk_buff *skb,
202                                  unsigned long extra_jiffies,
203                                  int do_acct);
204
205 /* Refresh conntrack for this many jiffies and do accounting */
206 static inline void nf_ct_refresh_acct(struct nf_conn *ct,
207                                       enum ip_conntrack_info ctinfo,
208                                       const struct sk_buff *skb,
209                                       unsigned long extra_jiffies)
210 {
211         __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
212 }
213
214 /* Refresh conntrack for this many jiffies */
215 static inline void nf_ct_refresh(struct nf_conn *ct,
216                                  const struct sk_buff *skb,
217                                  unsigned long extra_jiffies)
218 {
219         __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
220 }
221
222 /* These are for NAT.  Icky. */
223 /* Update TCP window tracking data when NAT mangles the packet */
224 extern void nf_conntrack_tcp_update(struct sk_buff *skb,
225                                     unsigned int dataoff,
226                                     struct nf_conn *conntrack,
227                                     int dir);
228
229 /* Call me when a conntrack is destroyed. */
230 extern void (*nf_conntrack_destroyed)(struct nf_conn *conntrack);
231
232 /* Fake conntrack entry for untracked connections */
233 extern struct nf_conn nf_conntrack_untracked;
234
235 extern int nf_ct_no_defrag;
236
237 /* Iterate over all conntracks: if iter returns true, it's deleted. */
238 extern void
239 nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data);
240 extern void nf_conntrack_free(struct nf_conn *ct);
241 extern struct nf_conn *
242 nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
243                    const struct nf_conntrack_tuple *repl);
244
245 /* It's confirmed if it is, or has been in the hash table. */
246 static inline int nf_ct_is_confirmed(struct nf_conn *ct)
247 {
248         return test_bit(IPS_CONFIRMED_BIT, &ct->status);
249 }
250
251 static inline int nf_ct_is_dying(struct nf_conn *ct)
252 {
253         return test_bit(IPS_DYING_BIT, &ct->status);
254 }
255
256 extern unsigned int nf_conntrack_htable_size;
257
258 #define NF_CT_STAT_INC(count) (__get_cpu_var(nf_conntrack_stat).count++)
259
260 #ifdef CONFIG_NF_CONNTRACK_EVENTS
261 #include <linux/notifier.h>
262 #include <linux/interrupt.h>
263
264 struct nf_conntrack_ecache {
265         struct nf_conn *ct;
266         unsigned int events;
267 };
268 DECLARE_PER_CPU(struct nf_conntrack_ecache, nf_conntrack_ecache);
269
270 #define CONNTRACK_ECACHE(x)     (__get_cpu_var(nf_conntrack_ecache).x)
271
272 extern struct notifier_block *nf_conntrack_chain;
273 extern struct notifier_block *nf_conntrack_expect_chain;
274
275 static inline int nf_conntrack_register_notifier(struct notifier_block *nb)
276 {
277         return notifier_chain_register(&nf_conntrack_chain, nb);
278 }
279
280 static inline int nf_conntrack_unregister_notifier(struct notifier_block *nb)
281 {
282         return notifier_chain_unregister(&nf_conntrack_chain, nb);
283 }
284
285 static inline int
286 nf_conntrack_expect_register_notifier(struct notifier_block *nb)
287 {
288         return notifier_chain_register(&nf_conntrack_expect_chain, nb);
289 }
290
291 static inline int
292 nf_conntrack_expect_unregister_notifier(struct notifier_block *nb)
293 {
294         return notifier_chain_unregister(&nf_conntrack_expect_chain, nb);
295 }
296
297 extern void nf_ct_deliver_cached_events(const struct nf_conn *ct);
298 extern void __nf_ct_event_cache_init(struct nf_conn *ct);
299
300 static inline void
301 nf_conntrack_event_cache(enum ip_conntrack_events event,
302                          const struct sk_buff *skb)
303 {
304         struct nf_conn *ct = (struct nf_conn *)skb->nfct;
305         struct nf_conntrack_ecache *ecache;
306
307         local_bh_disable();
308         ecache = &__get_cpu_var(nf_conntrack_ecache);
309         if (ct != ecache->ct)
310                 __nf_ct_event_cache_init(ct);
311         ecache->events |= event;
312         local_bh_enable();
313 }
314
315 static inline void nf_conntrack_event(enum ip_conntrack_events event,
316                                       struct nf_conn *ct)
317 {
318         if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct))
319                 notifier_call_chain(&nf_conntrack_chain, event, ct);
320 }
321
322 static inline void
323 nf_conntrack_expect_event(enum ip_conntrack_expect_events event,
324                           struct nf_conntrack_expect *exp)
325 {
326         notifier_call_chain(&nf_conntrack_expect_chain, event, exp);
327 }
328 #else /* CONFIG_NF_CONNTRACK_EVENTS */
329 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
330                                             const struct sk_buff *skb) {}
331 static inline void nf_conntrack_event(enum ip_conntrack_events event,
332                                       struct nf_conn *ct) {}
333 static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {}
334 static inline void
335 nf_conntrack_expect_event(enum ip_conntrack_expect_events event,
336                           struct nf_conntrack_expect *exp) {}
337 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
338
339 /* no helper, no nat */
340 #define NF_CT_F_BASIC   0
341 /* for helper */
342 #define NF_CT_F_HELP    1
343 /* for nat. */
344 #define NF_CT_F_NAT     2
345 #define NF_CT_F_NUM     4
346
347 extern int
348 nf_conntrack_register_cache(u_int32_t features, const char *name, size_t size,
349                             int (*init_conntrack)(struct nf_conn *, u_int32_t));
350 extern void
351 nf_conntrack_unregister_cache(u_int32_t features);
352
353 #endif /* __KERNEL__ */
354 #endif /* _NF_CONNTRACK_H */