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]) {
116 proto = xt_ct_find_proto(par);
118 pr_info("You must specify a L4 protocol, "
119 "and not use inversions on it.\n");
124 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
129 help->helper = nf_conntrack_helper_try_module_get(info->helper,
132 if (help->helper == NULL) {
133 pr_info("No such helper \"%s\"\n", info->helper);
138 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
139 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
145 nf_conntrack_free(ct);
147 nf_ct_l3proto_module_put(par->family);
152 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
153 static void __xt_ct_tg_timeout_put(struct ctnl_timeout *timeout)
155 typeof(nf_ct_timeout_put_hook) timeout_put;
157 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
159 timeout_put(timeout);
163 static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
165 struct xt_ct_target_info_v1 *info = par->targinfo;
166 struct nf_conntrack_tuple t;
167 struct nf_conn_help *help;
171 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
172 struct ctnl_timeout *timeout;
174 if (info->flags & ~XT_CT_NOTRACK)
177 if (info->flags & XT_CT_NOTRACK) {
178 ct = nf_ct_untracked_get();
179 atomic_inc(&ct->ct_general.use);
183 #ifndef CONFIG_NF_CONNTRACK_ZONES
188 ret = nf_ct_l3proto_try_module_get(par->family);
192 memset(&t, 0, sizeof(t));
193 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
199 if ((info->ct_events || info->exp_events) &&
200 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
204 if (info->helper[0]) {
206 proto = xt_ct_find_proto(par);
208 pr_info("You must specify a L4 protocol, "
209 "and not use inversions on it.\n");
214 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
219 help->helper = nf_conntrack_helper_try_module_get(info->helper,
222 if (help->helper == NULL) {
223 pr_info("No such helper \"%s\"\n", info->helper);
228 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
229 if (info->timeout[0]) {
230 typeof(nf_ct_timeout_find_get_hook) timeout_find_get;
231 struct nf_conn_timeout *timeout_ext;
235 rcu_dereference(nf_ct_timeout_find_get_hook);
237 if (timeout_find_get) {
238 const struct ipt_entry *e = par->entryinfo;
239 struct nf_conntrack_l4proto *l4proto;
241 if (e->ip.invflags & IPT_INV_PROTO) {
243 pr_info("You cannot use inversion on "
247 timeout = timeout_find_get(info->timeout);
248 if (timeout == NULL) {
250 pr_info("No such timeout policy \"%s\"\n",
254 if (timeout->l3num != par->family) {
256 pr_info("Timeout policy `%s' can only be "
257 "used by L3 protocol number %d\n",
258 info->timeout, timeout->l3num);
261 /* Make sure the timeout policy matches any existing
262 * protocol tracker, otherwise default to generic.
264 l4proto = __nf_ct_l4proto_find(par->family,
266 if (timeout->l4proto->l4proto != l4proto->l4proto) {
268 pr_info("Timeout policy `%s' can only be "
269 "used by L4 protocol number %d\n",
271 timeout->l4proto->l4proto);
274 timeout_ext = nf_ct_timeout_ext_add(ct, timeout,
276 if (timeout_ext == NULL) {
282 pr_info("Timeout policy base is empty\n");
289 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
290 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
295 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
297 __xt_ct_tg_timeout_put(timeout);
302 nf_conntrack_free(ct);
304 nf_ct_l3proto_module_put(par->family);
309 static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
311 struct xt_ct_target_info *info = par->targinfo;
312 struct nf_conn *ct = info->ct;
313 struct nf_conn_help *help;
315 if (!nf_ct_is_untracked(ct)) {
316 help = nfct_help(ct);
318 module_put(help->helper->me);
320 nf_ct_l3proto_module_put(par->family);
325 static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
327 struct xt_ct_target_info_v1 *info = par->targinfo;
328 struct nf_conn *ct = info->ct;
329 struct nf_conn_help *help;
330 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
331 struct nf_conn_timeout *timeout_ext;
332 typeof(nf_ct_timeout_put_hook) timeout_put;
334 if (!nf_ct_is_untracked(ct)) {
335 help = nfct_help(ct);
337 module_put(help->helper->me);
339 nf_ct_l3proto_module_put(par->family);
341 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
343 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
346 timeout_ext = nf_ct_timeout_find(ct);
348 timeout_put(timeout_ext->timeout);
356 static struct xt_target xt_ct_tg_reg[] __read_mostly = {
359 .family = NFPROTO_UNSPEC,
360 .targetsize = sizeof(struct xt_ct_target_info),
361 .checkentry = xt_ct_tg_check_v0,
362 .destroy = xt_ct_tg_destroy_v0,
363 .target = xt_ct_target_v0,
369 .family = NFPROTO_UNSPEC,
371 .targetsize = sizeof(struct xt_ct_target_info_v1),
372 .checkentry = xt_ct_tg_check_v1,
373 .destroy = xt_ct_tg_destroy_v1,
374 .target = xt_ct_target_v1,
380 static int __init xt_ct_tg_init(void)
382 return xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
385 static void __exit xt_ct_tg_exit(void)
387 xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
390 module_init(xt_ct_tg_init);
391 module_exit(xt_ct_tg_exit);
393 MODULE_LICENSE("GPL");
394 MODULE_DESCRIPTION("Xtables: connection tracking target");
395 MODULE_ALIAS("ipt_CT");
396 MODULE_ALIAS("ip6t_CT");