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/bitops.h>
19 #include <linux/compiler.h>
20 #include <asm/atomic.h>
21
22 #include <linux/netfilter/nf_conntrack_tcp.h>
23 #include <linux/netfilter/nf_conntrack_dccp.h>
24 #include <linux/netfilter/nf_conntrack_sctp.h>
25 #include <linux/netfilter/nf_conntrack_proto_gre.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 nf_ct_dccp dccp;
34 struct ip_ct_sctp sctp;
35 struct ip_ct_tcp tcp;
36 struct nf_ct_gre gre;
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 #include <linux/netfilter/nf_conntrack_pptp.h>
46 #include <linux/netfilter/nf_conntrack_h323.h>
47 #include <linux/netfilter/nf_conntrack_sane.h>
48 #include <linux/netfilter/nf_conntrack_sip.h>
49
50 /* per conntrack: application helper private data */
51 union nf_conntrack_help {
52 /* insert conntrack helper private data (master) here */
53 #if defined(CONFIG_NF_CONNTRACK_FTP) || defined(CONFIG_NF_CONNTRACK_FTP_MODULE)
54 struct nf_ct_ftp_master ct_ftp_info;
55 #endif
56 #if defined(CONFIG_NF_CONNTRACK_PPTP) || \
57 defined(CONFIG_NF_CONNTRACK_PPTP_MODULE)
58 struct nf_ct_pptp_master ct_pptp_info;
59 #endif
60 #if defined(CONFIG_NF_CONNTRACK_H323) || \
61 defined(CONFIG_NF_CONNTRACK_H323_MODULE)
62 struct nf_ct_h323_master ct_h323_info;
63 #endif
64 #if defined(CONFIG_NF_CONNTRACK_SANE) || \
65 defined(CONFIG_NF_CONNTRACK_SANE_MODULE)
66 struct nf_ct_sane_master ct_sane_info;
67 #endif
68 #if defined(CONFIG_NF_CONNTRACK_SIP) || defined(CONFIG_NF_CONNTRACK_SIP_MODULE)
69 struct nf_ct_sip_master ct_sip_info;
70 #endif
71 };
72
73 #include <linux/types.h>
74 #include <linux/skbuff.h>
75 #include <linux/timer.h>
76
77 #ifdef CONFIG_NETFILTER_DEBUG
78 #define NF_CT_ASSERT(x) WARN_ON(!(x))
79 #else
80 #define NF_CT_ASSERT(x)
81 #endif
82
83 struct nf_conntrack_helper;
84
85 /* Must be kept in sync with the classes defined by helpers */
86 #define NF_CT_MAX_EXPECT_CLASSES 4
87
88 /* nf_conn feature for connections that have a helper */
89 struct nf_conn_help {
90 /* Helper. if any */
91 struct nf_conntrack_helper __rcu *helper;
92
93 union nf_conntrack_help help;
94
95 struct hlist_head expectations;
96
97 /* Current number of expected connections */
98 u8 expecting[NF_CT_MAX_EXPECT_CLASSES];
99 };
100
101 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
102 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
103
104 struct nf_conn {
105 /* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
106 plus 1 for any connection(s) we are `master' for */
107 struct nf_conntrack ct_general;
108
109 spinlock_t lock;
110
111 /* XXX should I move this to the tail ? - Y.K */
112 /* These are my tuples; original and reply */
113 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
114
115 /* Have we seen traffic both ways yet? (bitset) */
116 unsigned long status;
117
118 /* If we were expected by an expectation, this will be it */
119 struct nf_conn *master;
120
121 /* Timer function; drops refcnt when it goes off. */
122 struct timer_list timeout;
123
124 #if defined(CONFIG_NF_CONNTRACK_MARK)
125 u_int32_t mark;
126 #endif
127
128 #ifdef CONFIG_NF_CONNTRACK_SECMARK
129 u_int32_t secmark;
130 #endif
131
132 /* Extensions */
133 struct nf_ct_ext *ext;
134 #ifdef CONFIG_NET_NS
135 struct net *ct_net;
136 #endif
137
138 /* Storage reserved for other modules, must be the last member */
139 union nf_conntrack_proto proto;
140 };
141
142 static inline struct nf_conn *
nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash * hash)143 nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
144 {
145 return container_of(hash, struct nf_conn,
146 tuplehash[hash->tuple.dst.dir]);
147 }
148
nf_ct_l3num(const struct nf_conn * ct)149 static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
150 {
151 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
152 }
153
nf_ct_protonum(const struct nf_conn * ct)154 static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
155 {
156 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
157 }
158
159 #define nf_ct_tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
160
161 /* get master conntrack via master expectation */
162 #define master_ct(conntr) (conntr->master)
163
164 extern struct net init_net;
165
nf_ct_net(const struct nf_conn * ct)166 static inline struct net *nf_ct_net(const struct nf_conn *ct)
167 {
168 return read_pnet(&ct->ct_net);
169 }
170
171 /* Alter reply tuple (maybe alter helper). */
172 extern void
173 nf_conntrack_alter_reply(struct nf_conn *ct,
174 const struct nf_conntrack_tuple *newreply);
175
176 /* Is this tuple taken? (ignoring any belonging to the given
177 conntrack). */
178 extern int
179 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
180 const struct nf_conn *ignored_conntrack);
181
182 /* Return conntrack_info and tuple hash for given skb. */
183 static inline struct nf_conn *
nf_ct_get(const struct sk_buff * skb,enum ip_conntrack_info * ctinfo)184 nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
185 {
186 *ctinfo = skb->nfctinfo;
187 return (struct nf_conn *)skb->nfct;
188 }
189
190 /* decrement reference count on a conntrack */
nf_ct_put(struct nf_conn * ct)191 static inline void nf_ct_put(struct nf_conn *ct)
192 {
193 NF_CT_ASSERT(ct);
194 nf_conntrack_put(&ct->ct_general);
195 }
196
197 /* Protocol module loading */
198 extern int nf_ct_l3proto_try_module_get(unsigned short l3proto);
199 extern void nf_ct_l3proto_module_put(unsigned short l3proto);
200
201 /*
202 * Allocate a hashtable of hlist_head (if nulls == 0),
203 * or hlist_nulls_head (if nulls == 1)
204 */
205 extern void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls);
206
207 extern void nf_ct_free_hashtable(void *hash, unsigned int size);
208
209 extern struct nf_conntrack_tuple_hash *
210 __nf_conntrack_find(struct net *net, u16 zone,
211 const struct nf_conntrack_tuple *tuple);
212
213 extern void nf_conntrack_hash_insert(struct nf_conn *ct);
214 extern void nf_ct_delete_from_lists(struct nf_conn *ct);
215 extern void nf_ct_insert_dying_list(struct nf_conn *ct);
216
217 extern void nf_conntrack_flush_report(struct net *net, u32 pid, int report);
218
219 extern bool nf_ct_get_tuplepr(const struct sk_buff *skb,
220 unsigned int nhoff, u_int16_t l3num,
221 struct nf_conntrack_tuple *tuple);
222 extern bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
223 const struct nf_conntrack_tuple *orig);
224
225 extern void __nf_ct_refresh_acct(struct nf_conn *ct,
226 enum ip_conntrack_info ctinfo,
227 const struct sk_buff *skb,
228 unsigned long extra_jiffies,
229 int do_acct);
230
231 /* Refresh conntrack for this many jiffies and do accounting */
nf_ct_refresh_acct(struct nf_conn * ct,enum ip_conntrack_info ctinfo,const struct sk_buff * skb,unsigned long extra_jiffies)232 static inline void nf_ct_refresh_acct(struct nf_conn *ct,
233 enum ip_conntrack_info ctinfo,
234 const struct sk_buff *skb,
235 unsigned long extra_jiffies)
236 {
237 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1);
238 }
239
240 /* Refresh conntrack for this many jiffies */
nf_ct_refresh(struct nf_conn * ct,const struct sk_buff * skb,unsigned long extra_jiffies)241 static inline void nf_ct_refresh(struct nf_conn *ct,
242 const struct sk_buff *skb,
243 unsigned long extra_jiffies)
244 {
245 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0);
246 }
247
248 extern bool __nf_ct_kill_acct(struct nf_conn *ct,
249 enum ip_conntrack_info ctinfo,
250 const struct sk_buff *skb,
251 int do_acct);
252
253 /* kill conntrack and do accounting */
nf_ct_kill_acct(struct nf_conn * ct,enum ip_conntrack_info ctinfo,const struct sk_buff * skb)254 static inline bool nf_ct_kill_acct(struct nf_conn *ct,
255 enum ip_conntrack_info ctinfo,
256 const struct sk_buff *skb)
257 {
258 return __nf_ct_kill_acct(ct, ctinfo, skb, 1);
259 }
260
261 /* kill conntrack without accounting */
nf_ct_kill(struct nf_conn * ct)262 static inline bool nf_ct_kill(struct nf_conn *ct)
263 {
264 return __nf_ct_kill_acct(ct, 0, NULL, 0);
265 }
266
267 /* These are for NAT. Icky. */
268 extern s16 (*nf_ct_nat_offset)(const struct nf_conn *ct,
269 enum ip_conntrack_dir dir,
270 u32 seq);
271
272 /* Fake conntrack entry for untracked connections */
273 DECLARE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
nf_ct_untracked_get(void)274 static inline struct nf_conn *nf_ct_untracked_get(void)
275 {
276 return &__raw_get_cpu_var(nf_conntrack_untracked);
277 }
278 extern void nf_ct_untracked_status_or(unsigned long bits);
279
280 /* Iterate over all conntracks: if iter returns true, it's deleted. */
281 extern void
282 nf_ct_iterate_cleanup(struct net *net, int (*iter)(struct nf_conn *i, void *data), void *data);
283 extern void nf_conntrack_free(struct nf_conn *ct);
284 extern struct nf_conn *
285 nf_conntrack_alloc(struct net *net, u16 zone,
286 const struct nf_conntrack_tuple *orig,
287 const struct nf_conntrack_tuple *repl,
288 gfp_t gfp);
289
nf_ct_is_template(const struct nf_conn * ct)290 static inline int nf_ct_is_template(const struct nf_conn *ct)
291 {
292 return test_bit(IPS_TEMPLATE_BIT, &ct->status);
293 }
294
295 /* It's confirmed if it is, or has been in the hash table. */
nf_ct_is_confirmed(struct nf_conn * ct)296 static inline int nf_ct_is_confirmed(struct nf_conn *ct)
297 {
298 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
299 }
300
nf_ct_is_dying(struct nf_conn * ct)301 static inline int nf_ct_is_dying(struct nf_conn *ct)
302 {
303 return test_bit(IPS_DYING_BIT, &ct->status);
304 }
305
nf_ct_is_untracked(const struct nf_conn * ct)306 static inline int nf_ct_is_untracked(const struct nf_conn *ct)
307 {
308 return test_bit(IPS_UNTRACKED_BIT, &ct->status);
309 }
310
311 extern int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp);
312 extern unsigned int nf_conntrack_htable_size;
313 extern unsigned int nf_conntrack_max;
314 extern unsigned int nf_conntrack_hash_rnd;
315 void init_nf_conntrack_hash_rnd(void);
316
317 #define NF_CT_STAT_INC(net, count) \
318 __this_cpu_inc((net)->ct.stat->count)
319 #define NF_CT_STAT_INC_ATOMIC(net, count) \
320 do { \
321 local_bh_disable(); \
322 __this_cpu_inc((net)->ct.stat->count); \
323 local_bh_enable(); \
324 } while (0)
325
326 #define MODULE_ALIAS_NFCT_HELPER(helper) \
327 MODULE_ALIAS("nfct-helper-" helper)
328
329 #endif /* __KERNEL__ */
330 #endif /* _NF_CONNTRACK_H */
331