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 unsigned int xt_ct_target_v0(struct sk_buff *skb,
24 const struct xt_action_param *par)
26 const struct xt_ct_target_info *info = par->targinfo;
27 struct nf_conn *ct = info->ct;
29 /* Previously seen (loopback)? Ignore. */
30 if (skb->nfct != NULL)
33 atomic_inc(&ct->ct_general.use);
34 skb->nfct = &ct->ct_general;
35 skb->nfctinfo = IP_CT_NEW;
40 static unsigned int xt_ct_target_v1(struct sk_buff *skb,
41 const struct xt_action_param *par)
43 const struct xt_ct_target_info_v1 *info = par->targinfo;
44 struct nf_conn *ct = info->ct;
46 /* Previously seen (loopback)? Ignore. */
47 if (skb->nfct != NULL)
50 atomic_inc(&ct->ct_general.use);
51 skb->nfct = &ct->ct_general;
52 skb->nfctinfo = IP_CT_NEW;
57 static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
59 if (par->family == NFPROTO_IPV4) {
60 const struct ipt_entry *e = par->entryinfo;
62 if (e->ip.invflags & IPT_INV_PROTO)
65 } else if (par->family == NFPROTO_IPV6) {
66 const struct ip6t_entry *e = par->entryinfo;
68 if (e->ipv6.invflags & IP6T_INV_PROTO)
75 static int xt_ct_tg_check_v0(const struct xt_tgchk_param *par)
77 struct xt_ct_target_info *info = par->targinfo;
78 struct nf_conntrack_tuple t;
79 struct nf_conn_help *help;
84 if (info->flags & ~XT_CT_NOTRACK)
87 if (info->flags & XT_CT_NOTRACK) {
88 ct = nf_ct_untracked_get();
89 atomic_inc(&ct->ct_general.use);
93 #ifndef CONFIG_NF_CONNTRACK_ZONES
98 ret = nf_ct_l3proto_try_module_get(par->family);
102 memset(&t, 0, sizeof(t));
103 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
109 if ((info->ct_events || info->exp_events) &&
110 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
114 if (info->helper[0]) {
115 struct nf_conntrack_helper *helper;
118 proto = xt_ct_find_proto(par);
120 pr_info("You must specify a L4 protocol, "
121 "and not use inversions on it.\n");
126 helper = nf_conntrack_helper_try_module_get(info->helper,
129 if (helper == NULL) {
130 pr_info("No such helper \"%s\"\n", info->helper);
135 help = nf_ct_helper_ext_add(ct, helper, GFP_KERNEL);
139 help->helper = helper;
142 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
143 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
149 nf_conntrack_free(ct);
151 nf_ct_l3proto_module_put(par->family);
156 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
157 static void __xt_ct_tg_timeout_put(struct ctnl_timeout *timeout)
159 typeof(nf_ct_timeout_put_hook) timeout_put;
161 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
163 timeout_put(timeout);
167 static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
169 struct xt_ct_target_info_v1 *info = par->targinfo;
170 struct nf_conntrack_tuple t;
171 struct nf_conn_help *help;
175 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
176 struct ctnl_timeout *timeout;
178 if (info->flags & ~XT_CT_NOTRACK)
181 if (info->flags & XT_CT_NOTRACK) {
182 ct = nf_ct_untracked_get();
183 atomic_inc(&ct->ct_general.use);
187 #ifndef CONFIG_NF_CONNTRACK_ZONES
192 ret = nf_ct_l3proto_try_module_get(par->family);
196 memset(&t, 0, sizeof(t));
197 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
203 if ((info->ct_events || info->exp_events) &&
204 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
208 if (info->helper[0]) {
209 struct nf_conntrack_helper *helper;
212 proto = xt_ct_find_proto(par);
214 pr_info("You must specify a L4 protocol, "
215 "and not use inversions on it.\n");
220 helper = nf_conntrack_helper_try_module_get(info->helper,
223 if (helper == NULL) {
224 pr_info("No such helper \"%s\"\n", info->helper);
229 help = nf_ct_helper_ext_add(ct, helper, GFP_KERNEL);
233 help->helper = helper;
236 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
237 if (info->timeout[0]) {
238 typeof(nf_ct_timeout_find_get_hook) timeout_find_get;
239 struct nf_conn_timeout *timeout_ext;
243 rcu_dereference(nf_ct_timeout_find_get_hook);
245 if (timeout_find_get) {
246 const struct ipt_entry *e = par->entryinfo;
247 struct nf_conntrack_l4proto *l4proto;
249 if (e->ip.invflags & IPT_INV_PROTO) {
251 pr_info("You cannot use inversion on "
255 timeout = timeout_find_get(info->timeout);
256 if (timeout == NULL) {
258 pr_info("No such timeout policy \"%s\"\n",
262 if (timeout->l3num != par->family) {
264 pr_info("Timeout policy `%s' can only be "
265 "used by L3 protocol number %d\n",
266 info->timeout, timeout->l3num);
269 /* Make sure the timeout policy matches any existing
270 * protocol tracker, otherwise default to generic.
272 l4proto = __nf_ct_l4proto_find(par->family,
274 if (timeout->l4proto->l4proto != l4proto->l4proto) {
276 pr_info("Timeout policy `%s' can only be "
277 "used by L4 protocol number %d\n",
279 timeout->l4proto->l4proto);
282 timeout_ext = nf_ct_timeout_ext_add(ct, timeout,
284 if (timeout_ext == NULL) {
290 pr_info("Timeout policy base is empty\n");
297 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
298 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
303 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
305 __xt_ct_tg_timeout_put(timeout);
310 nf_conntrack_free(ct);
312 nf_ct_l3proto_module_put(par->family);
317 static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
319 struct xt_ct_target_info *info = par->targinfo;
320 struct nf_conn *ct = info->ct;
321 struct nf_conn_help *help;
323 if (!nf_ct_is_untracked(ct)) {
324 help = nfct_help(ct);
326 module_put(help->helper->me);
328 nf_ct_l3proto_module_put(par->family);
333 static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
335 struct xt_ct_target_info_v1 *info = par->targinfo;
336 struct nf_conn *ct = info->ct;
337 struct nf_conn_help *help;
338 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
339 struct nf_conn_timeout *timeout_ext;
340 typeof(nf_ct_timeout_put_hook) timeout_put;
342 if (!nf_ct_is_untracked(ct)) {
343 help = nfct_help(ct);
345 module_put(help->helper->me);
347 nf_ct_l3proto_module_put(par->family);
349 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
351 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
354 timeout_ext = nf_ct_timeout_find(ct);
356 timeout_put(timeout_ext->timeout);
364 static struct xt_target xt_ct_tg_reg[] __read_mostly = {
367 .family = NFPROTO_UNSPEC,
368 .targetsize = sizeof(struct xt_ct_target_info),
369 .checkentry = xt_ct_tg_check_v0,
370 .destroy = xt_ct_tg_destroy_v0,
371 .target = xt_ct_target_v0,
377 .family = NFPROTO_UNSPEC,
379 .targetsize = sizeof(struct xt_ct_target_info_v1),
380 .checkentry = xt_ct_tg_check_v1,
381 .destroy = xt_ct_tg_destroy_v1,
382 .target = xt_ct_target_v1,
388 static int __init xt_ct_tg_init(void)
390 return xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
393 static void __exit xt_ct_tg_exit(void)
395 xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
398 module_init(xt_ct_tg_init);
399 module_exit(xt_ct_tg_exit);
401 MODULE_LICENSE("GPL");
402 MODULE_DESCRIPTION("Xtables: connection tracking target");
403 MODULE_ALIAS("ipt_CT");
404 MODULE_ALIAS("ip6t_CT");