blob: 6c3f964de9e1d652841cfe9510e895db3519cec9 [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/*
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__
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080018#include <linux/bitops.h>
19#include <linux/compiler.h>
20#include <asm/atomic.h>
21
22#include <linux/netfilter/nf_conntrack_tcp.h>
Patrick McHardy2bc78042008-03-20 15:15:55 +010023#include <linux/netfilter/nf_conntrack_dccp.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080024#include <linux/netfilter/nf_conntrack_sctp.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080025#include <linux/netfilter/nf_conntrack_proto_gre.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080026#include <net/netfilter/ipv4/nf_conntrack_icmp.h>
27#include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
28
29#include <net/netfilter/nf_conntrack_tuple.h>
30
31/* per conntrack: protocol private data */
32union nf_conntrack_proto {
33 /* insert conntrack proto private data here */
Patrick McHardy2bc78042008-03-20 15:15:55 +010034 struct nf_ct_dccp dccp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035 struct ip_ct_sctp sctp;
36 struct ip_ct_tcp tcp;
37 struct ip_ct_icmp icmp;
38 struct nf_ct_icmpv6 icmpv6;
Patrick McHardyf09943f2006-12-02 22:09:41 -080039 struct nf_ct_gre gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040};
41
42union nf_conntrack_expect_proto {
43 /* insert expect proto private data here */
44};
45
46/* Add protocol helper include file here */
47#include <linux/netfilter/nf_conntrack_ftp.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080048#include <linux/netfilter/nf_conntrack_pptp.h>
Patrick McHardyf587de02006-12-02 22:08:46 -080049#include <linux/netfilter/nf_conntrack_h323.h>
Michal Schmidt6fecd192007-02-07 15:05:12 -080050#include <linux/netfilter/nf_conntrack_sane.h>
Patrick McHardy0f32a402008-03-25 20:25:13 -070051#include <linux/netfilter/nf_conntrack_sip.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052
53/* per conntrack: application helper private data */
54union nf_conntrack_help {
55 /* insert conntrack helper private data (master) here */
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080056 struct nf_ct_ftp_master ct_ftp_info;
Patrick McHardyf09943f2006-12-02 22:09:41 -080057 struct nf_ct_pptp_master ct_pptp_info;
Patrick McHardyf587de02006-12-02 22:08:46 -080058 struct nf_ct_h323_master ct_h323_info;
Michal Schmidt6fecd192007-02-07 15:05:12 -080059 struct nf_ct_sane_master ct_sane_info;
Patrick McHardy0f32a402008-03-25 20:25:13 -070060 struct nf_ct_sip_master ct_sip_info;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061};
62
63#include <linux/types.h>
64#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050065#include <linux/timer.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066
67#ifdef CONFIG_NETFILTER_DEBUG
Patrick McHardy55871d02008-04-14 11:15:51 +020068#define NF_CT_ASSERT(x) WARN_ON(!(x))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080069#else
70#define NF_CT_ASSERT(x)
71#endif
72
73struct nf_conntrack_helper;
74
Patrick McHardy6002f2662008-03-25 20:09:15 -070075/* Must be kept in sync with the classes defined by helpers */
Patrick McHardy0d0ab032008-03-25 20:26:24 -070076#define NF_CT_MAX_EXPECT_CLASSES 3
Patrick McHardy6002f2662008-03-25 20:09:15 -070077
Harald Weltedc808fe2006-03-20 17:56:32 -080078/* nf_conn feature for connections that have a helper */
79struct nf_conn_help {
80 /* Helper. if any */
81 struct nf_conntrack_helper *helper;
82
83 union nf_conntrack_help help;
84
Patrick McHardyb5605802007-07-07 22:35:56 -070085 struct hlist_head expectations;
86
Harald Weltedc808fe2006-03-20 17:56:32 -080087 /* Current number of expected connections */
Patrick McHardy6002f2662008-03-25 20:09:15 -070088 u8 expecting[NF_CT_MAX_EXPECT_CLASSES];
Harald Weltedc808fe2006-03-20 17:56:32 -080089};
90
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080091#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
Patrick McHardyf8eb24a2006-11-29 02:35:15 +010092#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
93
Eric Dumazetea781f12009-03-25 21:05:46 +010094struct nf_conn {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
96 plus 1 for any connection(s) we are `master' for */
97 struct nf_conntrack ct_general;
98
99 /* XXX should I move this to the tail ? - Y.K */
100 /* These are my tuples; original and reply */
101 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
102
103 /* Have we seen traffic both ways yet? (bitset) */
104 unsigned long status;
105
Harald Weltedc808fe2006-03-20 17:56:32 -0800106 /* If we were expected by an expectation, this will be it */
107 struct nf_conn *master;
108
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109 /* Timer function; drops refcnt when it goes off. */
110 struct timer_list timeout;
111
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800112#if defined(CONFIG_NF_CONNTRACK_MARK)
113 u_int32_t mark;
114#endif
115
James Morris7c9728c2006-06-09 00:31:46 -0700116#ifdef CONFIG_NF_CONNTRACK_SECMARK
117 u_int32_t secmark;
118#endif
119
Harald Weltedc808fe2006-03-20 17:56:32 -0800120 /* Storage reserved for other modules: */
121 union nf_conntrack_proto proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800122
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700123 /* Extensions */
124 struct nf_ct_ext *ext;
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200125#ifdef CONFIG_NET_NS
126 struct net *ct_net;
127#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800128};
129
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130static inline struct nf_conn *
131nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
132{
133 return container_of(hash, struct nf_conn,
134 tuplehash[hash->tuple.dst.dir]);
135}
136
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200137static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
138{
139 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
140}
141
142static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
143{
144 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
145}
146
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147/* get master conntrack via master expectation */
148#define master_ct(conntr) (conntr->master)
149
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200150extern struct net init_net;
151
152static inline struct net *nf_ct_net(const struct nf_conn *ct)
153{
154#ifdef CONFIG_NET_NS
155 return ct->ct_net;
156#else
157 return &init_net;
158#endif
159}
160
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800161/* Alter reply tuple (maybe alter helper). */
162extern void
Patrick McHardyc88130b2008-01-31 04:42:11 -0800163nf_conntrack_alter_reply(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164 const struct nf_conntrack_tuple *newreply);
165
166/* Is this tuple taken? (ignoring any belonging to the given
167 conntrack). */
168extern int
169nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
170 const struct nf_conn *ignored_conntrack);
171
172/* Return conntrack_info and tuple hash for given skb. */
173static inline struct nf_conn *
174nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
175{
176 *ctinfo = skb->nfctinfo;
177 return (struct nf_conn *)skb->nfct;
178}
179
180/* decrement reference count on a conntrack */
181static inline void nf_ct_put(struct nf_conn *ct)
182{
183 NF_CT_ASSERT(ct);
184 nf_conntrack_put(&ct->ct_general);
185}
186
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800187/* Protocol module loading */
188extern int nf_ct_l3proto_try_module_get(unsigned short l3proto);
189extern void nf_ct_l3proto_module_put(unsigned short l3proto);
190
Eric Dumazetea781f12009-03-25 21:05:46 +0100191/*
192 * Allocate a hashtable of hlist_head (if nulls == 0),
193 * or hlist_nulls_head (if nulls == 1)
194 */
195extern void *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced, int nulls);
196
197extern void nf_ct_free_hashtable(void *hash, int vmalloced, unsigned int size);
Patrick McHardyac565e52007-07-07 22:30:08 -0700198
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800199extern struct nf_conntrack_tuple_hash *
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200200__nf_conntrack_find(struct net *net, const struct nf_conntrack_tuple *tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800201
202extern void nf_conntrack_hash_insert(struct nf_conn *ct);
203
Pablo Neira Ayuso19abb7b2008-11-18 11:56:20 +0100204extern void nf_conntrack_flush(struct net *net, u32 pid, int report);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800205
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200206extern bool nf_ct_get_tuplepr(const struct sk_buff *skb,
207 unsigned int nhoff, u_int16_t l3num,
208 struct nf_conntrack_tuple *tuple);
209extern bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
210 const struct nf_conntrack_tuple *orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800211
212extern void __nf_ct_refresh_acct(struct nf_conn *ct,
213 enum ip_conntrack_info ctinfo,
214 const struct sk_buff *skb,
215 unsigned long extra_jiffies,
216 int do_acct);
217
218/* Refresh conntrack for this many jiffies and do accounting */
219static inline void nf_ct_refresh_acct(struct nf_conn *ct,
220 enum ip_conntrack_info ctinfo,
221 const struct sk_buff *skb,
222 unsigned long extra_jiffies)
223{
224 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
225}
226
227/* Refresh conntrack for this many jiffies */
228static inline void nf_ct_refresh(struct nf_conn *ct,
229 const struct sk_buff *skb,
230 unsigned long extra_jiffies)
231{
232 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
233}
234
David S. Miller4c889492008-07-14 20:22:38 -0700235extern bool __nf_ct_kill_acct(struct nf_conn *ct,
236 enum ip_conntrack_info ctinfo,
237 const struct sk_buff *skb,
238 int do_acct);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700239
240/* kill conntrack and do accounting */
David S. Miller4c889492008-07-14 20:22:38 -0700241static inline bool nf_ct_kill_acct(struct nf_conn *ct,
242 enum ip_conntrack_info ctinfo,
243 const struct sk_buff *skb)
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700244{
David S. Miller4c889492008-07-14 20:22:38 -0700245 return __nf_ct_kill_acct(ct, ctinfo, skb, 1);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700246}
247
248/* kill conntrack without accounting */
David S. Miller4c889492008-07-14 20:22:38 -0700249static inline bool nf_ct_kill(struct nf_conn *ct)
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700250{
David S. Miller4c889492008-07-14 20:22:38 -0700251 return __nf_ct_kill_acct(ct, 0, NULL, 0);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700252}
Patrick McHardy51091762008-06-09 15:59:06 -0700253
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800254/* These are for NAT. Icky. */
255/* Update TCP window tracking data when NAT mangles the packet */
Jan Engelhardt82f568f2008-01-31 04:52:07 -0800256extern void nf_conntrack_tcp_update(const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257 unsigned int dataoff,
Patrick McHardyc88130b2008-01-31 04:42:11 -0800258 struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800259 int dir);
260
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800261/* Fake conntrack entry for untracked connections */
262extern struct nf_conn nf_conntrack_untracked;
263
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800264/* Iterate over all conntracks: if iter returns true, it's deleted. */
265extern void
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200266nf_ct_iterate_cleanup(struct net *net, int (*iter)(struct nf_conn *i, void *data), void *data);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800267extern void nf_conntrack_free(struct nf_conn *ct);
268extern struct nf_conn *
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200269nf_conntrack_alloc(struct net *net,
270 const struct nf_conntrack_tuple *orig,
Pablo Neira Ayusob891c5a2008-07-08 02:35:55 -0700271 const struct nf_conntrack_tuple *repl,
272 gfp_t gfp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800273
274/* It's confirmed if it is, or has been in the hash table. */
275static inline int nf_ct_is_confirmed(struct nf_conn *ct)
276{
277 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
278}
279
280static inline int nf_ct_is_dying(struct nf_conn *ct)
281{
282 return test_bit(IPS_DYING_BIT, &ct->status);
283}
284
Patrick McHardy587aa642007-03-14 16:37:25 -0700285static inline int nf_ct_is_untracked(const struct sk_buff *skb)
286{
287 return (skb->nfct == &nf_conntrack_untracked.ct_general);
288}
289
Patrick McHardyfae718d2007-12-24 21:09:10 -0800290extern int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291extern unsigned int nf_conntrack_htable_size;
Hagen Paul Pfeifere4780752009-02-20 10:47:09 +0100292extern unsigned int nf_conntrack_max;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800293
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200294#define NF_CT_STAT_INC(net, count) \
295 (per_cpu_ptr((net)->ct.stat, raw_smp_processor_id())->count++)
296#define NF_CT_STAT_INC_ATOMIC(net, count) \
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800297do { \
298 local_bh_disable(); \
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200299 per_cpu_ptr((net)->ct.stat, raw_smp_processor_id())->count++; \
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800300 local_bh_enable(); \
301} while (0)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +0100303#define MODULE_ALIAS_NFCT_HELPER(helper) \
304 MODULE_ALIAS("nfct-helper-" helper)
305
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800306#endif /* __KERNEL__ */
307#endif /* _NF_CONNTRACK_H */