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.
9 #include <linux/module.h>
10 #include <linux/gfp.h>
11 #include <linux/skbuff.h>
12 #include <linux/selinux.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
14 #include <linux/netfilter_ipv6/ip6_tables.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter/xt_CT.h>
17 #include <net/netfilter/nf_conntrack.h>
18 #include <net/netfilter/nf_conntrack_helper.h>
19 #include <net/netfilter/nf_conntrack_ecache.h>
20 #include <net/netfilter/nf_conntrack_zones.h>
22 static unsigned int xt_ct_target(struct sk_buff
*skb
,
23 const struct xt_action_param
*par
)
25 const struct xt_ct_target_info
*info
= par
->targinfo
;
26 struct nf_conn
*ct
= info
->ct
;
28 /* Previously seen (loopback)? Ignore. */
29 if (skb
->nfct
!= NULL
)
32 atomic_inc(&ct
->ct_general
.use
);
33 skb
->nfct
= &ct
->ct_general
;
34 skb
->nfctinfo
= IP_CT_NEW
;
39 static u8
xt_ct_find_proto(const struct xt_tgchk_param
*par
)
41 if (par
->family
== NFPROTO_IPV4
) {
42 const struct ipt_entry
*e
= par
->entryinfo
;
44 if (e
->ip
.invflags
& IPT_INV_PROTO
)
47 } else if (par
->family
== NFPROTO_IPV6
) {
48 const struct ip6t_entry
*e
= par
->entryinfo
;
50 if (e
->ipv6
.invflags
& IP6T_INV_PROTO
)
57 static int xt_ct_tg_check(const struct xt_tgchk_param
*par
)
59 struct xt_ct_target_info
*info
= par
->targinfo
;
60 struct nf_conntrack_tuple t
;
61 struct nf_conn_help
*help
;
66 if (info
->flags
& ~XT_CT_NOTRACK
)
69 if (info
->flags
& XT_CT_NOTRACK
) {
70 ct
= &nf_conntrack_untracked
;
71 atomic_inc(&ct
->ct_general
.use
);
75 #ifndef CONFIG_NF_CONNTRACK_ZONES
80 ret
= nf_ct_l3proto_try_module_get(par
->family
);
84 memset(&t
, 0, sizeof(t
));
85 ct
= nf_conntrack_alloc(par
->net
, info
->zone
, &t
, &t
, GFP_KERNEL
);
91 if ((info
->ct_events
|| info
->exp_events
) &&
92 !nf_ct_ecache_ext_add(ct
, info
->ct_events
, info
->exp_events
,
96 if (info
->helper
[0]) {
98 proto
= xt_ct_find_proto(par
);
103 help
= nf_ct_helper_ext_add(ct
, GFP_KERNEL
);
108 help
->helper
= nf_conntrack_helper_try_module_get(info
->helper
,
111 if (help
->helper
== NULL
)
115 __set_bit(IPS_TEMPLATE_BIT
, &ct
->status
);
116 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
122 nf_conntrack_free(ct
);
124 nf_ct_l3proto_module_put(par
->family
);
129 static void xt_ct_tg_destroy(const struct xt_tgdtor_param
*par
)
131 struct xt_ct_target_info
*info
= par
->targinfo
;
132 struct nf_conn
*ct
= info
->ct
;
133 struct nf_conn_help
*help
;
135 if (ct
!= &nf_conntrack_untracked
) {
136 help
= nfct_help(ct
);
138 module_put(help
->helper
->me
);
140 nf_ct_l3proto_module_put(par
->family
);
145 static struct xt_target xt_ct_tg __read_mostly
= {
147 .family
= NFPROTO_UNSPEC
,
148 .targetsize
= sizeof(struct xt_ct_target_info
),
149 .checkentry
= xt_ct_tg_check
,
150 .destroy
= xt_ct_tg_destroy
,
151 .target
= xt_ct_target
,
156 static int __init
xt_ct_tg_init(void)
158 return xt_register_target(&xt_ct_tg
);
161 static void __exit
xt_ct_tg_exit(void)
163 xt_unregister_target(&xt_ct_tg
);
166 module_init(xt_ct_tg_init
);
167 module_exit(xt_ct_tg_exit
);
169 MODULE_LICENSE("GPL");
170 MODULE_DESCRIPTION("Xtables: connection tracking target");
171 MODULE_ALIAS("ipt_CT");
172 MODULE_ALIAS("ip6t_CT");