blob: 5999c5313d0b95a256d2979aca27a33024a1661e [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 McHardy6002f262008-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 McHardy6002f262008-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 McHardy6002f262008-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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094struct nf_conn
95{
96 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
97 plus 1 for any connection(s) we are `master' for */
98 struct nf_conntrack ct_general;
99
100 /* XXX should I move this to the tail ? - Y.K */
101 /* These are my tuples; original and reply */
102 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
103
104 /* Have we seen traffic both ways yet? (bitset) */
105 unsigned long status;
106
Harald Weltedc808fe2006-03-20 17:56:32 -0800107 /* If we were expected by an expectation, this will be it */
108 struct nf_conn *master;
109
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800110 /* Timer function; drops refcnt when it goes off. */
111 struct timer_list timeout;
112
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800113#if defined(CONFIG_NF_CONNTRACK_MARK)
114 u_int32_t mark;
115#endif
116
James Morris7c9728c2006-06-09 00:31:46 -0700117#ifdef CONFIG_NF_CONNTRACK_SECMARK
118 u_int32_t secmark;
119#endif
120
Harald Weltedc808fe2006-03-20 17:56:32 -0800121 /* Storage reserved for other modules: */
122 union nf_conntrack_proto proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700124 /* Extensions */
125 struct nf_ct_ext *ext;
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200126#ifdef CONFIG_NET_NS
127 struct net *ct_net;
128#endif
Patrick McHardy76507f62008-01-31 04:38:38 -0800129 struct rcu_head rcu;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800130};
131
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132static inline struct nf_conn *
133nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
134{
135 return container_of(hash, struct nf_conn,
136 tuplehash[hash->tuple.dst.dir]);
137}
138
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200139static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
140{
141 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
142}
143
144static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
145{
146 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
147}
148
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149/* get master conntrack via master expectation */
150#define master_ct(conntr) (conntr->master)
151
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200152extern struct net init_net;
153
154static inline struct net *nf_ct_net(const struct nf_conn *ct)
155{
156#ifdef CONFIG_NET_NS
157 return ct->ct_net;
158#else
159 return &init_net;
160#endif
161}
162
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163/* Alter reply tuple (maybe alter helper). */
164extern void
Patrick McHardyc88130b2008-01-31 04:42:11 -0800165nf_conntrack_alter_reply(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166 const struct nf_conntrack_tuple *newreply);
167
168/* Is this tuple taken? (ignoring any belonging to the given
169 conntrack). */
170extern int
171nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
172 const struct nf_conn *ignored_conntrack);
173
174/* Return conntrack_info and tuple hash for given skb. */
175static inline struct nf_conn *
176nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
177{
178 *ctinfo = skb->nfctinfo;
179 return (struct nf_conn *)skb->nfct;
180}
181
182/* decrement reference count on a conntrack */
183static inline void nf_ct_put(struct nf_conn *ct)
184{
185 NF_CT_ASSERT(ct);
186 nf_conntrack_put(&ct->ct_general);
187}
188
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800189/* Protocol module loading */
190extern int nf_ct_l3proto_try_module_get(unsigned short l3proto);
191extern void nf_ct_l3proto_module_put(unsigned short l3proto);
192
Stephen Hemminger96eb24d2008-01-31 04:07:29 -0800193extern struct hlist_head *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced);
Patrick McHardyac565e52007-07-07 22:30:08 -0700194extern void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced,
Stephen Hemminger96eb24d2008-01-31 04:07:29 -0800195 unsigned int size);
Patrick McHardyac565e52007-07-07 22:30:08 -0700196
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800197extern struct nf_conntrack_tuple_hash *
Patrick McHardyba419af2008-01-31 04:39:23 -0800198__nf_conntrack_find(const struct nf_conntrack_tuple *tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800199
200extern void nf_conntrack_hash_insert(struct nf_conn *ct);
201
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800202extern void nf_conntrack_flush(void);
203
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200204extern bool nf_ct_get_tuplepr(const struct sk_buff *skb,
205 unsigned int nhoff, u_int16_t l3num,
206 struct nf_conntrack_tuple *tuple);
207extern bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
208 const struct nf_conntrack_tuple *orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800209
210extern void __nf_ct_refresh_acct(struct nf_conn *ct,
211 enum ip_conntrack_info ctinfo,
212 const struct sk_buff *skb,
213 unsigned long extra_jiffies,
214 int do_acct);
215
216/* Refresh conntrack for this many jiffies and do accounting */
217static inline void nf_ct_refresh_acct(struct nf_conn *ct,
218 enum ip_conntrack_info ctinfo,
219 const struct sk_buff *skb,
220 unsigned long extra_jiffies)
221{
222 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
223}
224
225/* Refresh conntrack for this many jiffies */
226static inline void nf_ct_refresh(struct nf_conn *ct,
227 const struct sk_buff *skb,
228 unsigned long extra_jiffies)
229{
230 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
231}
232
David S. Miller4c889492008-07-14 20:22:38 -0700233extern bool __nf_ct_kill_acct(struct nf_conn *ct,
234 enum ip_conntrack_info ctinfo,
235 const struct sk_buff *skb,
236 int do_acct);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700237
238/* kill conntrack and do accounting */
David S. Miller4c889492008-07-14 20:22:38 -0700239static inline bool nf_ct_kill_acct(struct nf_conn *ct,
240 enum ip_conntrack_info ctinfo,
241 const struct sk_buff *skb)
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700242{
David S. Miller4c889492008-07-14 20:22:38 -0700243 return __nf_ct_kill_acct(ct, ctinfo, skb, 1);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700244}
245
246/* kill conntrack without accounting */
David S. Miller4c889492008-07-14 20:22:38 -0700247static inline bool nf_ct_kill(struct nf_conn *ct)
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700248{
David S. Miller4c889492008-07-14 20:22:38 -0700249 return __nf_ct_kill_acct(ct, 0, NULL, 0);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700250}
Patrick McHardy51091762008-06-09 15:59:06 -0700251
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800252/* These are for NAT. Icky. */
253/* Update TCP window tracking data when NAT mangles the packet */
Jan Engelhardt82f568f2008-01-31 04:52:07 -0800254extern void nf_conntrack_tcp_update(const struct sk_buff *skb,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800255 unsigned int dataoff,
Patrick McHardyc88130b2008-01-31 04:42:11 -0800256 struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800257 int dir);
258
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800259/* Fake conntrack entry for untracked connections */
260extern struct nf_conn nf_conntrack_untracked;
261
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800262/* Iterate over all conntracks: if iter returns true, it's deleted. */
263extern void
264nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data);
265extern void nf_conntrack_free(struct nf_conn *ct);
266extern struct nf_conn *
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200267nf_conntrack_alloc(struct net *net,
268 const struct nf_conntrack_tuple *orig,
Pablo Neira Ayusob891c5a2008-07-08 02:35:55 -0700269 const struct nf_conntrack_tuple *repl,
270 gfp_t gfp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271
272/* It's confirmed if it is, or has been in the hash table. */
273static inline int nf_ct_is_confirmed(struct nf_conn *ct)
274{
275 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
276}
277
278static inline int nf_ct_is_dying(struct nf_conn *ct)
279{
280 return test_bit(IPS_DYING_BIT, &ct->status);
281}
282
Patrick McHardy587aa642007-03-14 16:37:25 -0700283static inline int nf_ct_is_untracked(const struct sk_buff *skb)
284{
285 return (skb->nfct == &nf_conntrack_untracked.ct_general);
286}
287
Patrick McHardyfae718d2007-12-24 21:09:10 -0800288extern int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800289extern unsigned int nf_conntrack_htable_size;
Patrick McHardy39a27a32006-05-29 18:23:54 -0700290extern int nf_conntrack_checksum;
Patrick McHardyf8eb24a2006-11-29 02:35:15 +0100291extern int nf_conntrack_max;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800292
Patrick McHardyf8eb24a2006-11-29 02:35:15 +0100293DECLARE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294#define NF_CT_STAT_INC(count) (__get_cpu_var(nf_conntrack_stat).count++)
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800295#define NF_CT_STAT_INC_ATOMIC(count) \
296do { \
297 local_bh_disable(); \
298 __get_cpu_var(nf_conntrack_stat).count++; \
299 local_bh_enable(); \
300} while (0)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800301
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800302#endif /* __KERNEL__ */
303#endif /* _NF_CONNTRACK_H */