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_helper.h>
18 #include <net/netfilter/nf_conntrack_ecache.h>
19 #include <net/netfilter/nf_conntrack_zones.h>
21 static unsigned int xt_ct_target(struct sk_buff *skb,
22 const struct xt_action_param *par)
24 const struct xt_ct_target_info *info = par->targinfo;
25 struct nf_conn *ct = info->ct;
27 /* Previously seen (loopback)? Ignore. */
28 if (skb->nfct != NULL)
31 atomic_inc(&ct->ct_general.use);
32 skb->nfct = &ct->ct_general;
33 skb->nfctinfo = IP_CT_NEW;
38 static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
40 if (par->family == NFPROTO_IPV4) {
41 const struct ipt_entry *e = par->entryinfo;
43 if (e->ip.invflags & IPT_INV_PROTO)
46 } else if (par->family == NFPROTO_IPV6) {
47 const struct ip6t_entry *e = par->entryinfo;
49 if (e->ipv6.invflags & IP6T_INV_PROTO)
56 static int xt_ct_tg_check(const struct xt_tgchk_param *par)
58 struct xt_ct_target_info *info = par->targinfo;
59 struct nf_conntrack_tuple t;
60 struct nf_conn_help *help;
65 if (info->flags & ~(XT_CT_NOTRACK | XT_CT_USERSPACE_HELPER))
68 if (info->flags & XT_CT_NOTRACK) {
69 ct = nf_ct_untracked_get();
70 atomic_inc(&ct->ct_general.use);
74 #ifndef CONFIG_NF_CONNTRACK_ZONES
79 ret = nf_ct_l3proto_try_module_get(par->family);
83 memset(&t, 0, sizeof(t));
84 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
90 if ((info->ct_events || info->exp_events) &&
91 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
95 if (info->flags & XT_CT_USERSPACE_HELPER) {
96 __set_bit(IPS_USERSPACE_HELPER_BIT, &ct->status);
97 } else if (info->helper[0]) {
99 proto = xt_ct_find_proto(par);
101 pr_info("You must specify a L4 protocol, "
102 "and not use inversions on it.\n");
107 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
112 help->helper = nf_conntrack_helper_try_module_get(info->helper,
115 if (help->helper == NULL) {
116 pr_info("No such helper \"%s\"\n", info->helper);
121 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
122 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
128 nf_conntrack_free(ct);
130 nf_ct_l3proto_module_put(par->family);
135 static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par)
137 struct xt_ct_target_info *info = par->targinfo;
138 struct nf_conn *ct = info->ct;
139 struct nf_conn_help *help;
141 if (!nf_ct_is_untracked(ct)) {
142 help = nfct_help(ct);
144 module_put(help->helper->me);
146 nf_ct_l3proto_module_put(par->family);
151 static struct xt_target xt_ct_tg __read_mostly = {
153 .family = NFPROTO_UNSPEC,
154 .targetsize = sizeof(struct xt_ct_target_info),
155 .checkentry = xt_ct_tg_check,
156 .destroy = xt_ct_tg_destroy,
157 .target = xt_ct_target,
162 static int __init xt_ct_tg_init(void)
164 return xt_register_target(&xt_ct_tg);
167 static void __exit xt_ct_tg_exit(void)
169 xt_unregister_target(&xt_ct_tg);
172 module_init(xt_ct_tg_init);
173 module_exit(xt_ct_tg_exit);
175 MODULE_LICENSE("GPL");
176 MODULE_DESCRIPTION("Xtables: connection tracking target");
177 MODULE_ALIAS("ipt_CT");
178 MODULE_ALIAS("ip6t_CT");