blob: ab86036bbf0c2807240afde3cf970329107358b8 [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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080017#include <linux/bitops.h>
18#include <linux/compiler.h>
Arun Sharma600634972011-07-26 16:09:06 -070019#include <linux/atomic.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020
21#include <linux/netfilter/nf_conntrack_tcp.h>
Patrick McHardy2bc78042008-03-20 15:15:55 +010022#include <linux/netfilter/nf_conntrack_dccp.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023#include <linux/netfilter/nf_conntrack_sctp.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080024#include <linux/netfilter/nf_conntrack_proto_gre.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
26
27#include <net/netfilter/nf_conntrack_tuple.h>
28
29/* per conntrack: protocol private data */
30union nf_conntrack_proto {
31 /* insert conntrack proto private data here */
Patrick McHardy2bc78042008-03-20 15:15:55 +010032 struct nf_ct_dccp dccp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033 struct ip_ct_sctp sctp;
34 struct ip_ct_tcp tcp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080035 struct nf_ct_gre gre;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036};
37
38union nf_conntrack_expect_proto {
39 /* insert expect proto private data here */
40};
41
42/* Add protocol helper include file here */
43#include <linux/netfilter/nf_conntrack_ftp.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080044#include <linux/netfilter/nf_conntrack_pptp.h>
Patrick McHardyf587de02006-12-02 22:08:46 -080045#include <linux/netfilter/nf_conntrack_h323.h>
Michal Schmidt6fecd192007-02-07 15:05:12 -080046#include <linux/netfilter/nf_conntrack_sane.h>
Patrick McHardy0f32a402008-03-25 20:25:13 -070047#include <linux/netfilter/nf_conntrack_sip.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048
49/* per conntrack: application helper private data */
50union nf_conntrack_help {
51 /* insert conntrack helper private data (master) here */
Changli Gao0f8e8002010-11-15 11:51:06 +010052#if defined(CONFIG_NF_CONNTRACK_FTP) || defined(CONFIG_NF_CONNTRACK_FTP_MODULE)
Jozsef Kadlecsik55a73322006-12-02 22:07:44 -080053 struct nf_ct_ftp_master ct_ftp_info;
Changli Gao0f8e8002010-11-15 11:51:06 +010054#endif
55#if defined(CONFIG_NF_CONNTRACK_PPTP) || \
56 defined(CONFIG_NF_CONNTRACK_PPTP_MODULE)
Patrick McHardyf09943f2006-12-02 22:09:41 -080057 struct nf_ct_pptp_master ct_pptp_info;
Changli Gao0f8e8002010-11-15 11:51:06 +010058#endif
59#if defined(CONFIG_NF_CONNTRACK_H323) || \
60 defined(CONFIG_NF_CONNTRACK_H323_MODULE)
Patrick McHardyf587de02006-12-02 22:08:46 -080061 struct nf_ct_h323_master ct_h323_info;
Changli Gao0f8e8002010-11-15 11:51:06 +010062#endif
63#if defined(CONFIG_NF_CONNTRACK_SANE) || \
64 defined(CONFIG_NF_CONNTRACK_SANE_MODULE)
Michal Schmidt6fecd192007-02-07 15:05:12 -080065 struct nf_ct_sane_master ct_sane_info;
Changli Gao0f8e8002010-11-15 11:51:06 +010066#endif
67#if defined(CONFIG_NF_CONNTRACK_SIP) || defined(CONFIG_NF_CONNTRACK_SIP_MODULE)
Patrick McHardy0f32a402008-03-25 20:25:13 -070068 struct nf_ct_sip_master ct_sip_info;
Changli Gao0f8e8002010-11-15 11:51:06 +010069#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080070};
71
72#include <linux/types.h>
73#include <linux/skbuff.h>
Al Virod7fe0f22006-12-03 23:15:30 -050074#include <linux/timer.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080075
76#ifdef CONFIG_NETFILTER_DEBUG
Patrick McHardy55871d02008-04-14 11:15:51 +020077#define NF_CT_ASSERT(x) WARN_ON(!(x))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078#else
79#define NF_CT_ASSERT(x)
80#endif
81
82struct nf_conntrack_helper;
83
Patrick McHardy6002f2662008-03-25 20:09:15 -070084/* Must be kept in sync with the classes defined by helpers */
Patrick McHardy9d288df2010-02-11 12:30:21 +010085#define NF_CT_MAX_EXPECT_CLASSES 4
Patrick McHardy6002f2662008-03-25 20:09:15 -070086
Harald Weltedc808fe2006-03-20 17:56:32 -080087/* nf_conn feature for connections that have a helper */
88struct nf_conn_help {
89 /* Helper. if any */
Arnd Bergmann0906a372010-03-09 20:59:15 +010090 struct nf_conntrack_helper __rcu *helper;
Harald Weltedc808fe2006-03-20 17:56:32 -080091
92 union nf_conntrack_help help;
93
Patrick McHardyb5605802007-07-07 22:35:56 -070094 struct hlist_head expectations;
95
Harald Weltedc808fe2006-03-20 17:56:32 -080096 /* Current number of expected connections */
Patrick McHardy6002f2662008-03-25 20:09:15 -070097 u8 expecting[NF_CT_MAX_EXPECT_CLASSES];
Harald Weltedc808fe2006-03-20 17:56:32 -080098};
99
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800100#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
Patrick McHardyf8eb24a2006-11-29 02:35:15 +0100101#include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
102
Eric Dumazetea781f12009-03-25 21:05:46 +0100103struct nf_conn {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800104 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
105 plus 1 for any connection(s) we are `master' for */
106 struct nf_conntrack ct_general;
107
Patrick McHardy440f0d52009-06-10 14:32:47 +0200108 spinlock_t lock;
109
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800110 /* XXX should I move this to the tail ? - Y.K */
111 /* These are my tuples; original and reply */
112 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
113
114 /* Have we seen traffic both ways yet? (bitset) */
115 unsigned long status;
116
Harald Weltedc808fe2006-03-20 17:56:32 -0800117 /* If we were expected by an expectation, this will be it */
118 struct nf_conn *master;
119
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800120 /* Timer function; drops refcnt when it goes off. */
121 struct timer_list timeout;
122
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123#if defined(CONFIG_NF_CONNTRACK_MARK)
124 u_int32_t mark;
125#endif
126
James Morris7c9728c2006-06-09 00:31:46 -0700127#ifdef CONFIG_NF_CONNTRACK_SECMARK
128 u_int32_t secmark;
129#endif
130
Yasuyuki Kozakaiecfab2c2007-07-07 22:23:21 -0700131 /* Extensions */
132 struct nf_ct_ext *ext;
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200133#ifdef CONFIG_NET_NS
134 struct net *ct_net;
135#endif
Changli Gaoe5fc9e72010-11-12 17:33:17 +0100136
137 /* Storage reserved for other modules, must be the last member */
138 union nf_conntrack_proto proto;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139};
140
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800141static inline struct nf_conn *
142nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
143{
144 return container_of(hash, struct nf_conn,
145 tuplehash[hash->tuple.dst.dir]);
146}
147
Patrick McHardy5e8fbe22008-04-14 11:15:52 +0200148static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
149{
150 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
151}
152
153static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
154{
155 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
156}
157
Pablo Neira Ayusof2f3e382009-06-02 20:03:35 +0200158#define nf_ct_tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
159
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160/* get master conntrack via master expectation */
161#define master_ct(conntr) (conntr->master)
162
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200163extern struct net init_net;
164
165static inline struct net *nf_ct_net(const struct nf_conn *ct)
166{
Eric Dumazetc2d9ba92010-06-01 06:51:19 +0000167 return read_pnet(&ct->ct_net);
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200168}
169
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800170/* Alter reply tuple (maybe alter helper). */
171extern void
Patrick McHardyc88130b2008-01-31 04:42:11 -0800172nf_conntrack_alter_reply(struct nf_conn *ct,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800173 const struct nf_conntrack_tuple *newreply);
174
175/* Is this tuple taken? (ignoring any belonging to the given
176 conntrack). */
177extern int
178nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
179 const struct nf_conn *ignored_conntrack);
180
181/* Return conntrack_info and tuple hash for given skb. */
182static inline struct nf_conn *
183nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
184{
185 *ctinfo = skb->nfctinfo;
186 return (struct nf_conn *)skb->nfct;
187}
188
189/* decrement reference count on a conntrack */
190static inline void nf_ct_put(struct nf_conn *ct)
191{
192 NF_CT_ASSERT(ct);
193 nf_conntrack_put(&ct->ct_general);
194}
195
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800196/* Protocol module loading */
197extern int nf_ct_l3proto_try_module_get(unsigned short l3proto);
198extern void nf_ct_l3proto_module_put(unsigned short l3proto);
199
Eric Dumazetea781f12009-03-25 21:05:46 +0100200/*
201 * Allocate a hashtable of hlist_head (if nulls == 0),
202 * or hlist_nulls_head (if nulls == 1)
203 */
Patrick McHardyd862a662011-01-14 15:45:56 +0100204extern void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls);
Eric Dumazetea781f12009-03-25 21:05:46 +0100205
Patrick McHardyd862a662011-01-14 15:45:56 +0100206extern void nf_ct_free_hashtable(void *hash, unsigned int size);
Patrick McHardyac565e52007-07-07 22:30:08 -0700207
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800208extern struct nf_conntrack_tuple_hash *
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100209__nf_conntrack_find(struct net *net, u16 zone,
210 const struct nf_conntrack_tuple *tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800211
Jozsef Kadlecsik7d367e02012-02-24 11:45:49 +0100212extern int nf_conntrack_hash_check_insert(struct nf_conn *ct);
Pablo Neira Ayusodd7669a2009-06-13 12:30:52 +0200213extern void nf_ct_delete_from_lists(struct nf_conn *ct);
214extern void nf_ct_insert_dying_list(struct nf_conn *ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800215
Pablo Neira Ayuso274d3832009-06-02 20:08:38 +0200216extern void nf_conntrack_flush_report(struct net *net, u32 pid, int report);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800217
Jan Engelhardt5f2b4c92008-04-14 11:15:53 +0200218extern bool nf_ct_get_tuplepr(const struct sk_buff *skb,
219 unsigned int nhoff, u_int16_t l3num,
220 struct nf_conntrack_tuple *tuple);
221extern bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
222 const struct nf_conntrack_tuple *orig);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223
224extern void __nf_ct_refresh_acct(struct nf_conn *ct,
225 enum ip_conntrack_info ctinfo,
226 const struct sk_buff *skb,
227 unsigned long extra_jiffies,
228 int do_acct);
229
230/* Refresh conntrack for this many jiffies and do accounting */
231static inline void nf_ct_refresh_acct(struct nf_conn *ct,
232 enum ip_conntrack_info ctinfo,
233 const struct sk_buff *skb,
234 unsigned long extra_jiffies)
235{
236 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
237}
238
239/* Refresh conntrack for this many jiffies */
240static inline void nf_ct_refresh(struct nf_conn *ct,
241 const struct sk_buff *skb,
242 unsigned long extra_jiffies)
243{
244 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
245}
246
David S. Miller4c889492008-07-14 20:22:38 -0700247extern bool __nf_ct_kill_acct(struct nf_conn *ct,
248 enum ip_conntrack_info ctinfo,
249 const struct sk_buff *skb,
250 int do_acct);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700251
252/* kill conntrack and do accounting */
David S. Miller4c889492008-07-14 20:22:38 -0700253static inline bool nf_ct_kill_acct(struct nf_conn *ct,
254 enum ip_conntrack_info ctinfo,
255 const struct sk_buff *skb)
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700256{
David S. Miller4c889492008-07-14 20:22:38 -0700257 return __nf_ct_kill_acct(ct, ctinfo, skb, 1);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700258}
259
260/* kill conntrack without accounting */
David S. Miller4c889492008-07-14 20:22:38 -0700261static inline bool nf_ct_kill(struct nf_conn *ct)
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700262{
David S. Miller4c889492008-07-14 20:22:38 -0700263 return __nf_ct_kill_acct(ct, 0, NULL, 0);
Fabian Hugelshofer718d4ad2008-06-09 15:59:40 -0700264}
Patrick McHardy51091762008-06-09 15:59:06 -0700265
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800266/* These are for NAT. Icky. */
Jozsef Kadlecsikf9dd09c2009-11-06 00:43:42 -0800267extern s16 (*nf_ct_nat_offset)(const struct nf_conn *ct,
268 enum ip_conntrack_dir dir,
269 u32 seq);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800270
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800271/* Fake conntrack entry for untracked connections */
Eric Dumazetb3c51632010-06-09 14:43:38 +0200272DECLARE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200273static inline struct nf_conn *nf_ct_untracked_get(void)
274{
Eric Dumazetb3c51632010-06-09 14:43:38 +0200275 return &__raw_get_cpu_var(nf_conntrack_untracked);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200276}
277extern void nf_ct_untracked_status_or(unsigned long bits);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800278
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800279/* Iterate over all conntracks: if iter returns true, it's deleted. */
280extern void
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200281nf_ct_iterate_cleanup(struct net *net, int (*iter)(struct nf_conn *i, void *data), void *data);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800282extern void nf_conntrack_free(struct nf_conn *ct);
283extern struct nf_conn *
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100284nf_conntrack_alloc(struct net *net, u16 zone,
Alexey Dobriyan5a1fb392008-10-08 11:35:02 +0200285 const struct nf_conntrack_tuple *orig,
Pablo Neira Ayusob891c5a2008-07-08 02:35:55 -0700286 const struct nf_conntrack_tuple *repl,
287 gfp_t gfp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800288
Patrick McHardyb2a15a62010-02-03 14:13:03 +0100289static inline int nf_ct_is_template(const struct nf_conn *ct)
290{
291 return test_bit(IPS_TEMPLATE_BIT, &ct->status);
292}
293
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800294/* It's confirmed if it is, or has been in the hash table. */
295static inline int nf_ct_is_confirmed(struct nf_conn *ct)
296{
297 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
298}
299
300static inline int nf_ct_is_dying(struct nf_conn *ct)
301{
302 return test_bit(IPS_DYING_BIT, &ct->status);
303}
304
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200305static inline int nf_ct_is_untracked(const struct nf_conn *ct)
Patrick McHardy587aa642007-03-14 16:37:25 -0700306{
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200307 return test_bit(IPS_UNTRACKED_BIT, &ct->status);
Patrick McHardy587aa642007-03-14 16:37:25 -0700308}
309
Julian Anastasov42c1edd2011-06-16 17:29:22 +0200310/* Packet is received from loopback */
311static inline bool nf_is_loopback_packet(const struct sk_buff *skb)
312{
313 return skb->dev && skb->skb_iif && skb->dev->flags & IFF_LOOPBACK;
314}
315
Paul Gortmaker34641c62011-08-29 13:21:14 -0400316struct kernel_param;
317
Patrick McHardyfae718d2007-12-24 21:09:10 -0800318extern int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800319extern unsigned int nf_conntrack_htable_size;
Hagen Paul Pfeifere4780752009-02-20 10:47:09 +0100320extern unsigned int nf_conntrack_max;
Changli Gaof682cef2011-01-05 04:23:23 +0000321extern unsigned int nf_conntrack_hash_rnd;
322void init_nf_conntrack_hash_rnd(void);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800323
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200324#define NF_CT_STAT_INC(net, count) \
Christoph Lameter4ea73342009-10-03 19:48:22 +0900325 __this_cpu_inc((net)->ct.stat->count)
Alexey Dobriyan0d55af82008-10-08 11:35:07 +0200326#define NF_CT_STAT_INC_ATOMIC(net, count) \
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800327do { \
328 local_bh_disable(); \
Christoph Lameter4ea73342009-10-03 19:48:22 +0900329 __this_cpu_inc((net)->ct.stat->count); \
Patrick McHardyc0e912d2007-02-12 11:13:43 -0800330 local_bh_enable(); \
331} while (0)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800332
Pablo Neira Ayuso4dc06f92008-11-17 16:01:42 +0100333#define MODULE_ALIAS_NFCT_HELPER(helper) \
334 MODULE_ALIAS("nfct-helper-" helper)
335
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800336#endif /* _NF_CONNTRACK_H */