blob: 03f0b370e17876b5e1f4ed382260509bdf0f2235 [file] [log] [blame]
Jan Engelhardt2cbc78a2012-09-21 11:41:34 +02001/*
2 * (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
11 * NAT funded by Astaro.
12 */
13
14#include <linux/if.h>
15#include <linux/inetdevice.h>
16#include <linux/ip.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/netdevice.h>
20#include <linux/netfilter.h>
21#include <linux/types.h>
22#include <linux/netfilter_ipv4.h>
23#include <linux/netfilter_ipv6.h>
24#include <linux/netfilter/x_tables.h>
25#include <net/addrconf.h>
26#include <net/checksum.h>
27#include <net/protocol.h>
28#include <net/netfilter/nf_nat.h>
Pablo Neira Ayusob59eaf92014-11-26 12:46:50 +010029#include <net/netfilter/nf_nat_redirect.h>
Jan Engelhardt2cbc78a2012-09-21 11:41:34 +020030
31static unsigned int
32redirect_tg6(struct sk_buff *skb, const struct xt_action_param *par)
33{
Arturo Borrero9de920e2014-10-17 12:37:52 +020034 return nf_nat_redirect_ipv6(skb, par->targinfo, par->hooknum);
Jan Engelhardt2cbc78a2012-09-21 11:41:34 +020035}
36
37static int redirect_tg6_checkentry(const struct xt_tgchk_param *par)
38{
39 const struct nf_nat_range *range = par->targinfo;
40
41 if (range->flags & NF_NAT_RANGE_MAP_IPS)
42 return -EINVAL;
43 return 0;
44}
45
46/* FIXME: Take multiple ranges --RR */
47static int redirect_tg4_check(const struct xt_tgchk_param *par)
48{
49 const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
50
51 if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
52 pr_debug("bad MAP_IPS.\n");
53 return -EINVAL;
54 }
55 if (mr->rangesize != 1) {
56 pr_debug("bad rangesize %u.\n", mr->rangesize);
57 return -EINVAL;
58 }
59 return 0;
60}
61
62static unsigned int
63redirect_tg4(struct sk_buff *skb, const struct xt_action_param *par)
64{
Arturo Borrero8b13edd2014-10-16 12:23:29 +020065 return nf_nat_redirect_ipv4(skb, par->targinfo, par->hooknum);
Jan Engelhardt2cbc78a2012-09-21 11:41:34 +020066}
67
68static struct xt_target redirect_tg_reg[] __read_mostly = {
69 {
70 .name = "REDIRECT",
71 .family = NFPROTO_IPV6,
72 .revision = 0,
73 .table = "nat",
74 .checkentry = redirect_tg6_checkentry,
75 .target = redirect_tg6,
76 .targetsize = sizeof(struct nf_nat_range),
77 .hooks = (1 << NF_INET_PRE_ROUTING) |
78 (1 << NF_INET_LOCAL_OUT),
79 .me = THIS_MODULE,
80 },
81 {
82 .name = "REDIRECT",
83 .family = NFPROTO_IPV4,
84 .revision = 0,
85 .table = "nat",
86 .target = redirect_tg4,
87 .checkentry = redirect_tg4_check,
88 .targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
89 .hooks = (1 << NF_INET_PRE_ROUTING) |
90 (1 << NF_INET_LOCAL_OUT),
91 .me = THIS_MODULE,
92 },
93};
94
95static int __init redirect_tg_init(void)
96{
97 return xt_register_targets(redirect_tg_reg,
98 ARRAY_SIZE(redirect_tg_reg));
99}
100
101static void __exit redirect_tg_exit(void)
102{
103 xt_unregister_targets(redirect_tg_reg, ARRAY_SIZE(redirect_tg_reg));
104}
105
106module_init(redirect_tg_init);
107module_exit(redirect_tg_exit);
108
109MODULE_LICENSE("GPL");
110MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
111MODULE_DESCRIPTION("Xtables: Connection redirection to localhost");
112MODULE_ALIAS("ip6t_REDIRECT");
113MODULE_ALIAS("ipt_REDIRECT");