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 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
154 static void __xt_ct_tg_timeout_put(struct ctnl_timeout *timeout)
156 typeof(nf_ct_timeout_put_hook) timeout_put;
158 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
160 timeout_put(timeout);
164 static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
166 struct xt_ct_target_info_v1 *info = par->targinfo;
167 struct nf_conntrack_tuple t;
168 struct nf_conn_help *help;
172 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
173 struct ctnl_timeout *timeout;
175 if (info->flags & ~XT_CT_NOTRACK)
178 if (info->flags & XT_CT_NOTRACK) {
179 ct = nf_ct_untracked_get();
180 atomic_inc(&ct->ct_general.use);
184 #ifndef CONFIG_NF_CONNTRACK_ZONES
189 ret = nf_ct_l3proto_try_module_get(par->family);
193 memset(&t, 0, sizeof(t));
194 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
200 if ((info->ct_events || info->exp_events) &&
201 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
205 if (info->helper[0]) {
207 proto = xt_ct_find_proto(par);
209 pr_info("You must specify a L4 protocol, "
210 "and not use inversions on it.\n");
215 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
220 help->helper = nf_conntrack_helper_try_module_get(info->helper,
223 if (help->helper == NULL) {
224 pr_info("No such helper \"%s\"\n", info->helper);
229 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
231 typeof(nf_ct_timeout_find_get_hook) timeout_find_get;
232 struct nf_conn_timeout *timeout_ext;
236 rcu_dereference(nf_ct_timeout_find_get_hook);
238 if (timeout_find_get) {
239 const struct ipt_entry *e = par->entryinfo;
240 struct nf_conntrack_l4proto *l4proto;
242 if (e->ip.invflags & IPT_INV_PROTO) {
244 pr_info("You cannot use inversion on "
248 timeout = timeout_find_get(info->timeout);
249 if (timeout == NULL) {
251 pr_info("No such timeout policy \"%s\"\n",
255 if (timeout->l3num != par->family) {
257 pr_info("Timeout policy `%s' can only be "
258 "used by L3 protocol number %d\n",
259 info->timeout, timeout->l3num);
262 /* Make sure the timeout policy matches any existing
263 * protocol tracker, otherwise default to generic.
265 l4proto = __nf_ct_l4proto_find(par->family,
267 if (timeout->l4proto->l4proto != l4proto->l4proto) {
269 pr_info("Timeout policy `%s' can only be "
270 "used by L4 protocol number %d\n",
272 timeout->l4proto->l4proto);
275 timeout_ext = nf_ct_timeout_ext_add(ct, timeout,
277 if (timeout_ext == NULL) {
283 pr_info("Timeout policy base is empty\n");
290 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
291 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
296 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
298 __xt_ct_tg_timeout_put(timeout);
303 nf_conntrack_free(ct);
305 nf_ct_l3proto_module_put(par->family);
310 static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
312 struct xt_ct_target_info *info = par->targinfo;
313 struct nf_conn *ct = info->ct;
314 struct nf_conn_help *help;
316 if (!nf_ct_is_untracked(ct)) {
317 help = nfct_help(ct);
319 module_put(help->helper->me);
321 nf_ct_l3proto_module_put(par->family);
326 static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
328 struct xt_ct_target_info_v1 *info = par->targinfo;
329 struct nf_conn *ct = info->ct;
330 struct nf_conn_help *help;
331 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
332 struct nf_conn_timeout *timeout_ext;
333 typeof(nf_ct_timeout_put_hook) timeout_put;
335 if (!nf_ct_is_untracked(ct)) {
336 help = nfct_help(ct);
338 module_put(help->helper->me);
340 nf_ct_l3proto_module_put(par->family);
342 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
344 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
347 timeout_ext = nf_ct_timeout_find(ct);
349 timeout_put(timeout_ext->timeout);
357 static struct xt_target xt_ct_tg_reg[] __read_mostly = {
360 .family = NFPROTO_UNSPEC,
361 .targetsize = sizeof(struct xt_ct_target_info),
362 .checkentry = xt_ct_tg_check_v0,
363 .destroy = xt_ct_tg_destroy_v0,
364 .target = xt_ct_target_v0,
370 .family = NFPROTO_UNSPEC,
372 .targetsize = sizeof(struct xt_ct_target_info_v1),
373 .checkentry = xt_ct_tg_check_v1,
374 .destroy = xt_ct_tg_destroy_v1,
375 .target = xt_ct_target_v1,
381 static int __init xt_ct_tg_init(void)
383 return xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
386 static void __exit xt_ct_tg_exit(void)
388 xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
391 module_init(xt_ct_tg_init);
392 module_exit(xt_ct_tg_exit);
394 MODULE_LICENSE("GPL");
395 MODULE_DESCRIPTION("Xtables: connection tracking target");
396 MODULE_ALIAS("ipt_CT");
397 MODULE_ALIAS("ip6t_CT");