blob: 0c8e43810ce363190c2c49477a3490ee6a077191 [file] [log] [blame]
Patrick McHardy84f3bb92010-02-03 17:17:06 +01001/*
2 * Copyright (c) 2010 Patrick McHardy <kaber@trash.net>
3 *
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.
7 */
Jan Engelhardta7fed762011-04-21 11:05:14 +02008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Patrick McHardy84f3bb92010-02-03 17:17:06 +01009#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Patrick McHardy84f3bb92010-02-03 17:17:06 +010011#include <linux/skbuff.h>
Patrick McHardy84f3bb92010-02-03 17:17:06 +010012#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>
Pablo Neira Ayusoeeb4cb92012-03-23 00:02:07 +010017#include <net/netfilter/nf_conntrack_l4proto.h>
Patrick McHardy84f3bb92010-02-03 17:17:06 +010018#include <net/netfilter/nf_conntrack_helper.h>
19#include <net/netfilter/nf_conntrack_ecache.h>
Pablo Neira Ayusoc1ebd7d2012-03-22 23:40:01 +010020#include <net/netfilter/nf_conntrack_l4proto.h>
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +010021#include <net/netfilter/nf_conntrack_timeout.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010022#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardy84f3bb92010-02-03 17:17:06 +010023
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +010024static unsigned int xt_ct_target_v0(struct sk_buff *skb,
25 const struct xt_action_param *par)
Patrick McHardy84f3bb92010-02-03 17:17:06 +010026{
27 const struct xt_ct_target_info *info = par->targinfo;
28 struct nf_conn *ct = info->ct;
29
30 /* Previously seen (loopback)? Ignore. */
31 if (skb->nfct != NULL)
32 return XT_CONTINUE;
33
34 atomic_inc(&ct->ct_general.use);
35 skb->nfct = &ct->ct_general;
36 skb->nfctinfo = IP_CT_NEW;
37
38 return XT_CONTINUE;
39}
40
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +010041static unsigned int xt_ct_target_v1(struct sk_buff *skb,
42 const struct xt_action_param *par)
43{
44 const struct xt_ct_target_info_v1 *info = par->targinfo;
45 struct nf_conn *ct = info->ct;
46
47 /* Previously seen (loopback)? Ignore. */
48 if (skb->nfct != NULL)
49 return XT_CONTINUE;
50
51 atomic_inc(&ct->ct_general.use);
52 skb->nfct = &ct->ct_general;
53 skb->nfctinfo = IP_CT_NEW;
54
55 return XT_CONTINUE;
56}
57
Patrick McHardy84f3bb92010-02-03 17:17:06 +010058static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
59{
Jan Engelhardt076f7832010-03-11 00:38:44 +010060 if (par->family == NFPROTO_IPV4) {
Patrick McHardy84f3bb92010-02-03 17:17:06 +010061 const struct ipt_entry *e = par->entryinfo;
62
63 if (e->ip.invflags & IPT_INV_PROTO)
64 return 0;
65 return e->ip.proto;
Jan Engelhardt076f7832010-03-11 00:38:44 +010066 } else if (par->family == NFPROTO_IPV6) {
Patrick McHardy84f3bb92010-02-03 17:17:06 +010067 const struct ip6t_entry *e = par->entryinfo;
68
69 if (e->ipv6.invflags & IP6T_INV_PROTO)
70 return 0;
71 return e->ipv6.proto;
72 } else
73 return 0;
74}
75
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +010076static int xt_ct_tg_check_v0(const struct xt_tgchk_param *par)
Patrick McHardy84f3bb92010-02-03 17:17:06 +010077{
78 struct xt_ct_target_info *info = par->targinfo;
79 struct nf_conntrack_tuple t;
80 struct nf_conn_help *help;
81 struct nf_conn *ct;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010082 int ret = 0;
Patrick McHardy84f3bb92010-02-03 17:17:06 +010083 u8 proto;
84
Pablo Neira Ayuso9bf04642012-01-15 16:57:12 +010085 if (info->flags & ~XT_CT_NOTRACK)
86 return -EINVAL;
Patrick McHardy84f3bb92010-02-03 17:17:06 +010087
88 if (info->flags & XT_CT_NOTRACK) {
Eric Dumazet5bfddbd2010-06-08 16:09:52 +020089 ct = nf_ct_untracked_get();
Patrick McHardy84f3bb92010-02-03 17:17:06 +010090 atomic_inc(&ct->ct_general.use);
91 goto out;
92 }
93
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010094#ifndef CONFIG_NF_CONNTRACK_ZONES
95 if (info->zone)
96 goto err1;
97#endif
98
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010099 ret = nf_ct_l3proto_try_module_get(par->family);
100 if (ret < 0)
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100101 goto err1;
102
103 memset(&t, 0, sizeof(t));
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100104 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100105 ret = PTR_ERR(ct);
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100106 if (IS_ERR(ct))
107 goto err2;
108
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100109 ret = 0;
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100110 if ((info->ct_events || info->exp_events) &&
111 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
112 GFP_KERNEL))
113 goto err3;
114
Pablo Neira Ayuso9bf04642012-01-15 16:57:12 +0100115 if (info->helper[0]) {
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100116 ret = -ENOENT;
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100117 proto = xt_ct_find_proto(par);
Jan Engelhardta7fed762011-04-21 11:05:14 +0200118 if (!proto) {
119 pr_info("You must specify a L4 protocol, "
120 "and not use inversions on it.\n");
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100121 goto err3;
Jan Engelhardta7fed762011-04-21 11:05:14 +0200122 }
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100123
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100124 ret = -ENOMEM;
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100125 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
126 if (help == NULL)
127 goto err3;
128
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100129 ret = -ENOENT;
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100130 help->helper = nf_conntrack_helper_try_module_get(info->helper,
131 par->family,
132 proto);
Jan Engelhardta7fed762011-04-21 11:05:14 +0200133 if (help->helper == NULL) {
134 pr_info("No such helper \"%s\"\n", info->helper);
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100135 goto err3;
Jan Engelhardta7fed762011-04-21 11:05:14 +0200136 }
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100137 }
138
139 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
140 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
141out:
142 info->ct = ct;
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100143 return 0;
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100144
145err3:
146 nf_conntrack_free(ct);
147err2:
148 nf_ct_l3proto_module_put(par->family);
149err1:
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100150 return ret;
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100151}
152
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100153static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
154{
155 struct xt_ct_target_info_v1 *info = par->targinfo;
156 struct nf_conntrack_tuple t;
157 struct nf_conn_help *help;
158 struct nf_conn *ct;
159 int ret = 0;
160 u8 proto;
161
162 if (info->flags & ~XT_CT_NOTRACK)
163 return -EINVAL;
164
165 if (info->flags & XT_CT_NOTRACK) {
166 ct = nf_ct_untracked_get();
167 atomic_inc(&ct->ct_general.use);
168 goto out;
169 }
170
171#ifndef CONFIG_NF_CONNTRACK_ZONES
172 if (info->zone)
173 goto err1;
174#endif
175
176 ret = nf_ct_l3proto_try_module_get(par->family);
177 if (ret < 0)
178 goto err1;
179
180 memset(&t, 0, sizeof(t));
181 ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL);
182 ret = PTR_ERR(ct);
183 if (IS_ERR(ct))
184 goto err2;
185
186 ret = 0;
187 if ((info->ct_events || info->exp_events) &&
188 !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
189 GFP_KERNEL))
190 goto err3;
191
192 if (info->helper[0]) {
193 ret = -ENOENT;
194 proto = xt_ct_find_proto(par);
195 if (!proto) {
196 pr_info("You must specify a L4 protocol, "
197 "and not use inversions on it.\n");
198 goto err3;
199 }
200
201 ret = -ENOMEM;
202 help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
203 if (help == NULL)
204 goto err3;
205
206 ret = -ENOENT;
207 help->helper = nf_conntrack_helper_try_module_get(info->helper,
208 par->family,
209 proto);
210 if (help->helper == NULL) {
211 pr_info("No such helper \"%s\"\n", info->helper);
212 goto err3;
213 }
214 }
215
216#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
217 if (info->timeout) {
218 typeof(nf_ct_timeout_find_get_hook) timeout_find_get;
219 struct ctnl_timeout *timeout;
220 struct nf_conn_timeout *timeout_ext;
221
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100222 rcu_read_lock();
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100223 timeout_find_get =
224 rcu_dereference(nf_ct_timeout_find_get_hook);
225
226 if (timeout_find_get) {
227 const struct ipt_entry *e = par->entryinfo;
Pablo Neira Ayusoeeb4cb92012-03-23 00:02:07 +0100228 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100229
230 if (e->ip.invflags & IPT_INV_PROTO) {
231 ret = -EINVAL;
232 pr_info("You cannot use inversion on "
233 "L4 protocol\n");
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100234 goto err4;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100235 }
236 timeout = timeout_find_get(info->timeout);
237 if (timeout == NULL) {
238 ret = -ENOENT;
239 pr_info("No such timeout policy \"%s\"\n",
240 info->timeout);
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100241 goto err4;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100242 }
243 if (timeout->l3num != par->family) {
244 ret = -EINVAL;
245 pr_info("Timeout policy `%s' can only be "
246 "used by L3 protocol number %d\n",
247 info->timeout, timeout->l3num);
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100248 goto err4;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100249 }
Pablo Neira Ayusoeeb4cb92012-03-23 00:02:07 +0100250 /* Make sure the timeout policy matches any existing
251 * protocol tracker, otherwise default to generic.
252 */
253 l4proto = __nf_ct_l4proto_find(par->family,
254 e->ip.proto);
255 if (timeout->l4proto->l4proto != l4proto->l4proto) {
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100256 ret = -EINVAL;
257 pr_info("Timeout policy `%s' can only be "
258 "used by L4 protocol number %d\n",
Pablo Neira Ayusoc1ebd7d2012-03-22 23:40:01 +0100259 info->timeout,
260 timeout->l4proto->l4proto);
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100261 goto err4;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100262 }
263 timeout_ext = nf_ct_timeout_ext_add(ct, timeout,
264 GFP_KERNEL);
265 if (timeout_ext == NULL) {
266 ret = -ENOMEM;
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100267 goto err4;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100268 }
269 } else {
270 ret = -ENOENT;
271 pr_info("Timeout policy base is empty\n");
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100272 goto err4;
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100273 }
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100274 rcu_read_unlock();
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100275 }
276#endif
277
278 __set_bit(IPS_TEMPLATE_BIT, &ct->status);
279 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
280out:
281 info->ct = ct;
282 return 0;
283
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100284err4:
285 rcu_read_unlock();
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100286err3:
287 nf_conntrack_free(ct);
288err2:
289 nf_ct_l3proto_module_put(par->family);
290err1:
291 return ret;
292}
293
294static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100295{
296 struct xt_ct_target_info *info = par->targinfo;
297 struct nf_conn *ct = info->ct;
298 struct nf_conn_help *help;
299
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200300 if (!nf_ct_is_untracked(ct)) {
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100301 help = nfct_help(ct);
302 if (help)
303 module_put(help->helper->me);
304
305 nf_ct_l3proto_module_put(par->family);
306 }
307 nf_ct_put(info->ct);
308}
309
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100310static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
311{
312 struct xt_ct_target_info_v1 *info = par->targinfo;
313 struct nf_conn *ct = info->ct;
314 struct nf_conn_help *help;
315#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
316 struct nf_conn_timeout *timeout_ext;
317 typeof(nf_ct_timeout_put_hook) timeout_put;
318#endif
319 if (!nf_ct_is_untracked(ct)) {
320 help = nfct_help(ct);
321 if (help)
322 module_put(help->helper->me);
323
324 nf_ct_l3proto_module_put(par->family);
325
326#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100327 rcu_read_lock();
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100328 timeout_put = rcu_dereference(nf_ct_timeout_put_hook);
329
330 if (timeout_put) {
331 timeout_ext = nf_ct_timeout_find(ct);
332 if (timeout_ext)
333 timeout_put(timeout_ext->timeout);
334 }
Pablo Neira Ayuso1ac0bf92012-03-22 23:58:41 +0100335 rcu_read_unlock();
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100336#endif
337 }
338 nf_ct_put(info->ct);
339}
340
341static struct xt_target xt_ct_tg_reg[] __read_mostly = {
342 {
343 .name = "CT",
344 .family = NFPROTO_UNSPEC,
345 .targetsize = sizeof(struct xt_ct_target_info),
346 .checkentry = xt_ct_tg_check_v0,
347 .destroy = xt_ct_tg_destroy_v0,
348 .target = xt_ct_target_v0,
349 .table = "raw",
350 .me = THIS_MODULE,
351 },
352 {
353 .name = "CT",
354 .family = NFPROTO_UNSPEC,
355 .revision = 1,
356 .targetsize = sizeof(struct xt_ct_target_info_v1),
357 .checkentry = xt_ct_tg_check_v1,
358 .destroy = xt_ct_tg_destroy_v1,
359 .target = xt_ct_target_v1,
360 .table = "raw",
361 .me = THIS_MODULE,
362 },
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100363};
364
365static int __init xt_ct_tg_init(void)
366{
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100367 return xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100368}
369
370static void __exit xt_ct_tg_exit(void)
371{
Pablo Neira Ayuso24de58f2012-02-29 02:19:19 +0100372 xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
Patrick McHardy84f3bb92010-02-03 17:17:06 +0100373}
374
375module_init(xt_ct_tg_init);
376module_exit(xt_ct_tg_exit);
377
378MODULE_LICENSE("GPL");
379MODULE_DESCRIPTION("Xtables: connection tracking target");
380MODULE_ALIAS("ipt_CT");
381MODULE_ALIAS("ip6t_CT");