2 * Copyright (c) 2010 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/gfp.h>
11 #include <linux/skbuff.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13 #include <linux/netfilter_ipv6/ip6_tables.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter/xt_CT.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_l4proto.h>
18 #include <net/netfilter/nf_conntrack_helper.h>
19 #include <net/netfilter/nf_conntrack_ecache.h>
20 #include <net/netfilter/nf_conntrack_l4proto.h>
21 #include <net/netfilter/nf_conntrack_timeout.h>
22 #include <net/netfilter/nf_conntrack_zones.h>
24 static unsigned int xt_ct_target_v0(struct sk_buff
*skb
,
25 const struct xt_action_param
*par
)
27 const struct xt_ct_target_info
*info
= par
->targinfo
;
28 struct nf_conn
*ct
= info
->ct
;
30 /* Previously seen (loopback)? Ignore. */
31 if (skb
->nfct
!= NULL
)
34 atomic_inc(&ct
->ct_general
.use
);
35 skb
->nfct
= &ct
->ct_general
;
36 skb
->nfctinfo
= IP_CT_NEW
;
41 static unsigned int xt_ct_target_v1(struct sk_buff
*skb
,
42 const struct xt_action_param
*par
)
44 const struct xt_ct_target_info_v1
*info
= par
->targinfo
;
45 struct nf_conn
*ct
= info
->ct
;
47 /* Previously seen (loopback)? Ignore. */
48 if (skb
->nfct
!= NULL
)
51 atomic_inc(&ct
->ct_general
.use
);
52 skb
->nfct
= &ct
->ct_general
;
53 skb
->nfctinfo
= IP_CT_NEW
;
58 static u8
xt_ct_find_proto(const struct xt_tgchk_param
*par
)
60 if (par
->family
== NFPROTO_IPV4
) {
61 const struct ipt_entry
*e
= par
->entryinfo
;
63 if (e
->ip
.invflags
& IPT_INV_PROTO
)
66 } else if (par
->family
== NFPROTO_IPV6
) {
67 const struct ip6t_entry
*e
= par
->entryinfo
;
69 if (e
->ipv6
.invflags
& IP6T_INV_PROTO
)
76 static int xt_ct_tg_check_v0(const struct xt_tgchk_param
*par
)
78 struct xt_ct_target_info
*info
= par
->targinfo
;
79 struct nf_conntrack_tuple t
;
80 struct nf_conn_help
*help
;
85 if (info
->flags
& ~XT_CT_NOTRACK
)
88 if (info
->flags
& XT_CT_NOTRACK
) {
89 ct
= nf_ct_untracked_get();
90 atomic_inc(&ct
->ct_general
.use
);
94 #ifndef CONFIG_NF_CONNTRACK_ZONES
99 ret
= nf_ct_l3proto_try_module_get(par
->family
);
103 memset(&t
, 0, sizeof(t
));
104 ct
= nf_conntrack_alloc(par
->net
, info
->zone
, &t
, &t
, GFP_KERNEL
);
110 if ((info
->ct_events
|| info
->exp_events
) &&
111 !nf_ct_ecache_ext_add(ct
, info
->ct_events
, info
->exp_events
,
115 if (info
->helper
[0]) {
117 proto
= xt_ct_find_proto(par
);
119 pr_info("You must specify a L4 protocol, "
120 "and not use inversions on it.\n");
125 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
130 help
->helper
= nf_conntrack_helper_try_module_get(info
->helper
,
133 if (help
->helper
== NULL
) {
134 pr_info("No such helper \"%s\"\n", info
->helper
);
139 __set_bit(IPS_TEMPLATE_BIT
, &ct
->status
);
140 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
146 nf_conntrack_free(ct
);
148 nf_ct_l3proto_module_put(par
->family
);
153 static int xt_ct_tg_check_v1(const struct xt_tgchk_param
*par
)
155 struct xt_ct_target_info_v1
*info
= par
->targinfo
;
156 struct nf_conntrack_tuple t
;
157 struct nf_conn_help
*help
;
162 if (info
->flags
& ~XT_CT_NOTRACK
)
165 if (info
->flags
& XT_CT_NOTRACK
) {
166 ct
= nf_ct_untracked_get();
167 atomic_inc(&ct
->ct_general
.use
);
171 #ifndef CONFIG_NF_CONNTRACK_ZONES
176 ret
= nf_ct_l3proto_try_module_get(par
->family
);
180 memset(&t
, 0, sizeof(t
));
181 ct
= nf_conntrack_alloc(par
->net
, info
->zone
, &t
, &t
, GFP_KERNEL
);
187 if ((info
->ct_events
|| info
->exp_events
) &&
188 !nf_ct_ecache_ext_add(ct
, info
->ct_events
, info
->exp_events
,
192 if (info
->helper
[0]) {
194 proto
= xt_ct_find_proto(par
);
196 pr_info("You must specify a L4 protocol, "
197 "and not use inversions on it.\n");
202 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
207 help
->helper
= nf_conntrack_helper_try_module_get(info
->helper
,
210 if (help
->helper
== NULL
) {
211 pr_info("No such helper \"%s\"\n", info
->helper
);
216 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
218 typeof(nf_ct_timeout_find_get_hook
) timeout_find_get
;
219 struct ctnl_timeout
*timeout
;
220 struct nf_conn_timeout
*timeout_ext
;
224 rcu_dereference(nf_ct_timeout_find_get_hook
);
226 if (timeout_find_get
) {
227 const struct ipt_entry
*e
= par
->entryinfo
;
228 struct nf_conntrack_l4proto
*l4proto
;
230 if (e
->ip
.invflags
& IPT_INV_PROTO
) {
232 pr_info("You cannot use inversion on "
236 timeout
= timeout_find_get(info
->timeout
);
237 if (timeout
== NULL
) {
239 pr_info("No such timeout policy \"%s\"\n",
243 if (timeout
->l3num
!= par
->family
) {
245 pr_info("Timeout policy `%s' can only be "
246 "used by L3 protocol number %d\n",
247 info
->timeout
, timeout
->l3num
);
250 /* Make sure the timeout policy matches any existing
251 * protocol tracker, otherwise default to generic.
253 l4proto
= __nf_ct_l4proto_find(par
->family
,
255 if (timeout
->l4proto
->l4proto
!= l4proto
->l4proto
) {
257 pr_info("Timeout policy `%s' can only be "
258 "used by L4 protocol number %d\n",
260 timeout
->l4proto
->l4proto
);
263 timeout_ext
= nf_ct_timeout_ext_add(ct
, timeout
,
265 if (timeout_ext
== NULL
) {
271 pr_info("Timeout policy base is empty\n");
278 __set_bit(IPS_TEMPLATE_BIT
, &ct
->status
);
279 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
284 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
289 nf_conntrack_free(ct
);
291 nf_ct_l3proto_module_put(par
->family
);
296 static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param
*par
)
298 struct xt_ct_target_info
*info
= par
->targinfo
;
299 struct nf_conn
*ct
= info
->ct
;
300 struct nf_conn_help
*help
;
302 if (!nf_ct_is_untracked(ct
)) {
303 help
= nfct_help(ct
);
305 module_put(help
->helper
->me
);
307 nf_ct_l3proto_module_put(par
->family
);
312 static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param
*par
)
314 struct xt_ct_target_info_v1
*info
= par
->targinfo
;
315 struct nf_conn
*ct
= info
->ct
;
316 struct nf_conn_help
*help
;
317 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
318 struct nf_conn_timeout
*timeout_ext
;
319 typeof(nf_ct_timeout_put_hook
) timeout_put
;
321 if (!nf_ct_is_untracked(ct
)) {
322 help
= nfct_help(ct
);
324 module_put(help
->helper
->me
);
326 nf_ct_l3proto_module_put(par
->family
);
328 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
330 timeout_put
= rcu_dereference(nf_ct_timeout_put_hook
);
333 timeout_ext
= nf_ct_timeout_find(ct
);
335 timeout_put(timeout_ext
->timeout
);
343 static struct xt_target xt_ct_tg_reg
[] __read_mostly
= {
346 .family
= NFPROTO_UNSPEC
,
347 .targetsize
= sizeof(struct xt_ct_target_info
),
348 .checkentry
= xt_ct_tg_check_v0
,
349 .destroy
= xt_ct_tg_destroy_v0
,
350 .target
= xt_ct_target_v0
,
356 .family
= NFPROTO_UNSPEC
,
358 .targetsize
= sizeof(struct xt_ct_target_info_v1
),
359 .checkentry
= xt_ct_tg_check_v1
,
360 .destroy
= xt_ct_tg_destroy_v1
,
361 .target
= xt_ct_target_v1
,
367 static int __init
xt_ct_tg_init(void)
369 return xt_register_targets(xt_ct_tg_reg
, ARRAY_SIZE(xt_ct_tg_reg
));
372 static void __exit
xt_ct_tg_exit(void)
374 xt_unregister_targets(xt_ct_tg_reg
, ARRAY_SIZE(xt_ct_tg_reg
));
377 module_init(xt_ct_tg_init
);
378 module_exit(xt_ct_tg_exit
);
380 MODULE_LICENSE("GPL");
381 MODULE_DESCRIPTION("Xtables: connection tracking target");
382 MODULE_ALIAS("ipt_CT");
383 MODULE_ALIAS("ip6t_CT");