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_timeout.h>
21 #include <net/netfilter/nf_conntrack_zones.h>
23 static inline int xt_ct_target(struct sk_buff
*skb
, struct nf_conn
*ct
)
25 /* Previously seen (loopback)? Ignore. */
26 if (skb
->nfct
!= NULL
)
29 atomic_inc(&ct
->ct_general
.use
);
30 skb
->nfct
= &ct
->ct_general
;
31 skb
->nfctinfo
= IP_CT_NEW
;
36 static unsigned int xt_ct_target_v0(struct sk_buff
*skb
,
37 const struct xt_action_param
*par
)
39 const struct xt_ct_target_info
*info
= par
->targinfo
;
40 struct nf_conn
*ct
= info
->ct
;
42 return xt_ct_target(skb
, ct
);
45 static unsigned int xt_ct_target_v1(struct sk_buff
*skb
,
46 const struct xt_action_param
*par
)
48 const struct xt_ct_target_info_v1
*info
= par
->targinfo
;
49 struct nf_conn
*ct
= info
->ct
;
51 return xt_ct_target(skb
, ct
);
54 static u8
xt_ct_find_proto(const struct xt_tgchk_param
*par
)
56 if (par
->family
== NFPROTO_IPV4
) {
57 const struct ipt_entry
*e
= par
->entryinfo
;
59 if (e
->ip
.invflags
& IPT_INV_PROTO
)
62 } else if (par
->family
== NFPROTO_IPV6
) {
63 const struct ip6t_entry
*e
= par
->entryinfo
;
65 if (e
->ipv6
.invflags
& IP6T_INV_PROTO
)
73 xt_ct_set_helper(struct nf_conn
*ct
, const char *helper_name
,
74 const struct xt_tgchk_param
*par
)
76 struct nf_conntrack_helper
*helper
;
77 struct nf_conn_help
*help
;
80 proto
= xt_ct_find_proto(par
);
82 pr_info("You must specify a L4 protocol, and not use "
83 "inversions on it.\n");
87 helper
= nf_conntrack_helper_try_module_get(helper_name
, par
->family
,
90 pr_info("No such helper \"%s\"\n", helper_name
);
94 help
= nf_ct_helper_ext_add(ct
, helper
, GFP_KERNEL
);
96 module_put(helper
->me
);
100 help
->helper
= helper
;
104 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
105 static void __xt_ct_tg_timeout_put(struct ctnl_timeout
*timeout
)
107 typeof(nf_ct_timeout_put_hook
) timeout_put
;
109 timeout_put
= rcu_dereference(nf_ct_timeout_put_hook
);
111 timeout_put(timeout
);
116 xt_ct_set_timeout(struct nf_conn
*ct
, const struct xt_tgchk_param
*par
,
117 const char *timeout_name
)
119 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
120 typeof(nf_ct_timeout_find_get_hook
) timeout_find_get
;
121 struct ctnl_timeout
*timeout
;
122 struct nf_conn_timeout
*timeout_ext
;
123 struct nf_conntrack_l4proto
*l4proto
;
128 timeout_find_get
= rcu_dereference(nf_ct_timeout_find_get_hook
);
129 if (timeout_find_get
== NULL
) {
131 pr_info("Timeout policy base is empty\n");
135 proto
= xt_ct_find_proto(par
);
138 pr_info("You must specify a L4 protocol, and not use "
139 "inversions on it.\n");
143 timeout
= timeout_find_get(timeout_name
);
144 if (timeout
== NULL
) {
146 pr_info("No such timeout policy \"%s\"\n", timeout_name
);
150 if (timeout
->l3num
!= par
->family
) {
152 pr_info("Timeout policy `%s' can only be used by L3 protocol "
153 "number %d\n", timeout_name
, timeout
->l3num
);
154 goto err_put_timeout
;
156 /* Make sure the timeout policy matches any existing protocol tracker,
157 * otherwise default to generic.
159 l4proto
= __nf_ct_l4proto_find(par
->family
, proto
);
160 if (timeout
->l4proto
->l4proto
!= l4proto
->l4proto
) {
162 pr_info("Timeout policy `%s' can only be used by L4 protocol "
164 timeout_name
, timeout
->l4proto
->l4proto
);
165 goto err_put_timeout
;
167 timeout_ext
= nf_ct_timeout_ext_add(ct
, timeout
, GFP_ATOMIC
);
168 if (timeout_ext
== NULL
)
172 __xt_ct_tg_timeout_put(timeout
);
181 static int xt_ct_tg_check(const struct xt_tgchk_param
*par
,
182 struct xt_ct_target_info_v1
*info
)
184 struct nf_conntrack_tuple t
;
186 int ret
= -EOPNOTSUPP
;
188 if (info
->flags
& XT_CT_NOTRACK
) {
189 ct
= nf_ct_untracked_get();
190 atomic_inc(&ct
->ct_general
.use
);
194 #ifndef CONFIG_NF_CONNTRACK_ZONES
199 ret
= nf_ct_l3proto_try_module_get(par
->family
);
203 memset(&t
, 0, sizeof(t
));
204 ct
= nf_conntrack_alloc(par
->net
, info
->zone
, &t
, &t
, GFP_KERNEL
);
210 if ((info
->ct_events
|| info
->exp_events
) &&
211 !nf_ct_ecache_ext_add(ct
, info
->ct_events
, info
->exp_events
,
215 if (info
->helper
[0]) {
216 ret
= xt_ct_set_helper(ct
, info
->helper
, par
);
221 if (info
->timeout
[0]) {
222 ret
= xt_ct_set_timeout(ct
, par
, info
->timeout
);
227 __set_bit(IPS_TEMPLATE_BIT
, &ct
->status
);
228 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
230 /* Overload tuple linked list to put us in template list. */
231 hlist_nulls_add_head_rcu(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].hnnode
,
238 nf_conntrack_free(ct
);
240 nf_ct_l3proto_module_put(par
->family
);
245 static int xt_ct_tg_check_v0(const struct xt_tgchk_param
*par
)
247 struct xt_ct_target_info
*info
= par
->targinfo
;
248 struct xt_ct_target_info_v1 info_v1
= {
249 .flags
= info
->flags
,
251 .ct_events
= info
->ct_events
,
252 .exp_events
= info
->exp_events
,
256 if (info
->flags
& ~XT_CT_NOTRACK
)
259 memcpy(info_v1
.helper
, info
->helper
, sizeof(info
->helper
));
261 ret
= xt_ct_tg_check(par
, &info_v1
);
265 info
->ct
= info_v1
.ct
;
270 static int xt_ct_tg_check_v1(const struct xt_tgchk_param
*par
)
272 struct xt_ct_target_info_v1
*info
= par
->targinfo
;
274 if (info
->flags
& ~XT_CT_NOTRACK
)
277 return xt_ct_tg_check(par
, par
->targinfo
);
280 static int xt_ct_tg_check_v2(const struct xt_tgchk_param
*par
)
282 struct xt_ct_target_info_v1
*info
= par
->targinfo
;
284 if (info
->flags
& ~XT_CT_MASK
)
287 return xt_ct_tg_check(par
, par
->targinfo
);
290 static void xt_ct_destroy_timeout(struct nf_conn
*ct
)
292 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
293 struct nf_conn_timeout
*timeout_ext
;
294 typeof(nf_ct_timeout_put_hook
) timeout_put
;
297 timeout_put
= rcu_dereference(nf_ct_timeout_put_hook
);
300 timeout_ext
= nf_ct_timeout_find(ct
);
302 timeout_put(timeout_ext
->timeout
);
308 static void xt_ct_tg_destroy(const struct xt_tgdtor_param
*par
,
309 struct xt_ct_target_info_v1
*info
)
311 struct nf_conn
*ct
= info
->ct
;
312 struct nf_conn_help
*help
;
314 if (!nf_ct_is_untracked(ct
)) {
315 help
= nfct_help(ct
);
317 module_put(help
->helper
->me
);
319 nf_ct_l3proto_module_put(par
->family
);
321 xt_ct_destroy_timeout(ct
);
326 static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param
*par
)
328 struct xt_ct_target_info
*info
= par
->targinfo
;
329 struct xt_ct_target_info_v1 info_v1
= {
330 .flags
= info
->flags
,
332 .ct_events
= info
->ct_events
,
333 .exp_events
= info
->exp_events
,
336 memcpy(info_v1
.helper
, info
->helper
, sizeof(info
->helper
));
338 xt_ct_tg_destroy(par
, &info_v1
);
341 static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param
*par
)
343 xt_ct_tg_destroy(par
, par
->targinfo
);
346 static struct xt_target xt_ct_tg_reg
[] __read_mostly
= {
349 .family
= NFPROTO_UNSPEC
,
350 .targetsize
= sizeof(struct xt_ct_target_info
),
351 .checkentry
= xt_ct_tg_check_v0
,
352 .destroy
= xt_ct_tg_destroy_v0
,
353 .target
= xt_ct_target_v0
,
359 .family
= NFPROTO_UNSPEC
,
361 .targetsize
= sizeof(struct xt_ct_target_info_v1
),
362 .checkentry
= xt_ct_tg_check_v1
,
363 .destroy
= xt_ct_tg_destroy_v1
,
364 .target
= xt_ct_target_v1
,
370 .family
= NFPROTO_UNSPEC
,
372 .targetsize
= sizeof(struct xt_ct_target_info_v1
),
373 .checkentry
= xt_ct_tg_check_v2
,
374 .destroy
= xt_ct_tg_destroy_v1
,
375 .target
= xt_ct_target_v1
,
382 notrack_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
384 /* Previously seen (loopback)? Ignore. */
385 if (skb
->nfct
!= NULL
)
388 skb
->nfct
= &nf_ct_untracked_get()->ct_general
;
389 skb
->nfctinfo
= IP_CT_NEW
;
390 nf_conntrack_get(skb
->nfct
);
395 static int notrack_chk(const struct xt_tgchk_param
*par
)
397 if (!par
->net
->xt
.notrack_deprecated_warning
) {
398 pr_info("netfilter: NOTRACK target is deprecated, "
399 "use CT instead or upgrade iptables\n");
400 par
->net
->xt
.notrack_deprecated_warning
= true;
405 static struct xt_target notrack_tg_reg __read_mostly
= {
408 .family
= NFPROTO_UNSPEC
,
409 .checkentry
= notrack_chk
,
410 .target
= notrack_tg
,
415 static int __init
xt_ct_tg_init(void)
419 ret
= xt_register_target(¬rack_tg_reg
);
423 ret
= xt_register_targets(xt_ct_tg_reg
, ARRAY_SIZE(xt_ct_tg_reg
));
425 xt_unregister_target(¬rack_tg_reg
);
431 static void __exit
xt_ct_tg_exit(void)
433 xt_unregister_targets(xt_ct_tg_reg
, ARRAY_SIZE(xt_ct_tg_reg
));
434 xt_unregister_target(¬rack_tg_reg
);
437 module_init(xt_ct_tg_init
);
438 module_exit(xt_ct_tg_exit
);
440 MODULE_LICENSE("GPL");
441 MODULE_DESCRIPTION("Xtables: connection tracking target");
442 MODULE_ALIAS("ipt_CT");
443 MODULE_ALIAS("ip6t_CT");
444 MODULE_ALIAS("ipt_NOTRACK");
445 MODULE_ALIAS("ip6t_NOTRACK");