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_helper.h>
18 #include <net/netfilter/nf_conntrack_ecache.h>
19 #include <net/netfilter/nf_conntrack_timeout.h>
20 #include <net/netfilter/nf_conntrack_zones.h>
22 static unsigned int xt_ct_target_v0(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 unsigned int xt_ct_target_v1(struct sk_buff *skb,
40 const struct xt_action_param *par)
42 const struct xt_ct_target_info_v1 *info = par->targinfo;
43 struct nf_conn *ct = info->ct;
45 /* Previously seen (loopback)? Ignore. */
46 if (skb->nfct != NULL)
49 atomic_inc(&ct->ct_general.use);
50 skb->nfct = &ct->ct_general;
51 skb->nfctinfo = IP_CT_NEW;
56 static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
58 if (par->family == NFPROTO_IPV4) {
59 const struct ipt_entry *e = par->entryinfo;
61 if (e->ip.invflags & IPT_INV_PROTO)
64 } else if (par->family == NFPROTO_IPV6) {
65 const struct ip6t_entry *e = par->entryinfo;
67 if (e->ipv6.invflags & IP6T_INV_PROTO)
74 static int xt_ct_tg_check_v0(const struct xt_tgchk_param *par)
76 struct xt_ct_target_info *info = par->targinfo;
77 struct nf_conntrack_tuple t;
78 struct nf_conn_help *help;
83 if (info->flags & ~XT_CT_NOTRACK)
86 if (info->flags & XT_CT_NOTRACK) {
87 ct = nf_ct_untracked_get();
88 atomic_inc(&ct->ct_general.use);
92 #ifndef CONFIG_NF_CONNTRACK_ZONES
97 ret = nf_ct_l3proto_try_module_get(par->family);
101 memset(&t, 0, sizeof(t));
102 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
108 if ((info->ct_events || info->exp_events) &&
109 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
113 if (info->helper[0]) {
115 proto = xt_ct_find_proto(par);
117 pr_info("You must specify a L4 protocol, "
118 "and not use inversions on it.\n");
123 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
128 help->helper = nf_conntrack_helper_try_module_get(info->helper,
131 if (help->helper == NULL) {
132 pr_info("No such helper \"%s\"\n", info->helper);
137 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
138 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
144 nf_conntrack_free(ct);
146 nf_ct_l3proto_module_put(par->family);
151 static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
153 struct xt_ct_target_info_v1 *info = par->targinfo;
154 struct nf_conntrack_tuple t;
155 struct nf_conn_help *help;
160 if (info->flags & ~XT_CT_NOTRACK)
163 if (info->flags & XT_CT_NOTRACK) {
164 ct = nf_ct_untracked_get();
165 atomic_inc(&ct->ct_general.use);
169 #ifndef CONFIG_NF_CONNTRACK_ZONES
174 ret = nf_ct_l3proto_try_module_get(par->family);
178 memset(&t, 0, sizeof(t));
179 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
185 if ((info->ct_events || info->exp_events) &&
186 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
190 if (info->helper[0]) {
192 proto = xt_ct_find_proto(par);
194 pr_info("You must specify a L4 protocol, "
195 "and not use inversions on it.\n");
200 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
205 help->helper = nf_conntrack_helper_try_module_get(info->helper,
208 if (help->helper == NULL) {
209 pr_info("No such helper \"%s\"\n", info->helper);
214 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
216 typeof(nf_ct_timeout_find_get_hook) timeout_find_get;
217 struct ctnl_timeout *timeout;
218 struct nf_conn_timeout *timeout_ext;
221 rcu_dereference(nf_ct_timeout_find_get_hook);
223 if (timeout_find_get) {
224 const struct ipt_entry *e = par->entryinfo;
226 if (e->ip.invflags & IPT_INV_PROTO) {
228 pr_info("You cannot use inversion on "
232 timeout = timeout_find_get(info->timeout);
233 if (timeout == NULL) {
235 pr_info("No such timeout policy \"%s\"\n",
239 if (timeout->l3num != par->family) {
241 pr_info("Timeout policy `%s' can only be "
242 "used by L3 protocol number %d\n",
243 info->timeout, timeout->l3num);
246 if (timeout->l4num != e->ip.proto) {
248 pr_info("Timeout policy `%s' can only be "
249 "used by L4 protocol number %d\n",
250 info->timeout, timeout->l4num);
253 timeout_ext = nf_ct_timeout_ext_add(ct, timeout,
255 if (timeout_ext == NULL) {
261 pr_info("Timeout policy base is empty\n");
267 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
268 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
274 nf_conntrack_free(ct);
276 nf_ct_l3proto_module_put(par->family);
281 static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
283 struct xt_ct_target_info *info = par->targinfo;
284 struct nf_conn *ct = info->ct;
285 struct nf_conn_help *help;
287 if (!nf_ct_is_untracked(ct)) {
288 help = nfct_help(ct);
290 module_put(help->helper->me);
292 nf_ct_l3proto_module_put(par->family);
297 static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
299 struct xt_ct_target_info_v1 *info = par->targinfo;
300 struct nf_conn *ct = info->ct;
301 struct nf_conn_help *help;
302 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
303 struct nf_conn_timeout *timeout_ext;
304 typeof(nf_ct_timeout_put_hook) timeout_put;
306 if (!nf_ct_is_untracked(ct)) {
307 help = nfct_help(ct);
309 module_put(help->helper->me);
311 nf_ct_l3proto_module_put(par->family);
313 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
314 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
317 timeout_ext = nf_ct_timeout_find(ct);
319 timeout_put(timeout_ext->timeout);
326 static struct xt_target xt_ct_tg_reg[] __read_mostly = {
329 .family = NFPROTO_UNSPEC,
330 .targetsize = sizeof(struct xt_ct_target_info),
331 .checkentry = xt_ct_tg_check_v0,
332 .destroy = xt_ct_tg_destroy_v0,
333 .target = xt_ct_target_v0,
339 .family = NFPROTO_UNSPEC,
341 .targetsize = sizeof(struct xt_ct_target_info_v1),
342 .checkentry = xt_ct_tg_check_v1,
343 .destroy = xt_ct_tg_destroy_v1,
344 .target = xt_ct_target_v1,
350 static int __init xt_ct_tg_init(void)
352 return xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
355 static void __exit xt_ct_tg_exit(void)
357 xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
360 module_init(xt_ct_tg_init);
361 module_exit(xt_ct_tg_exit);
363 MODULE_LICENSE("GPL");
364 MODULE_DESCRIPTION("Xtables: connection tracking target");
365 MODULE_ALIAS("ipt_CT");
366 MODULE_ALIAS("ip6t_CT");