netfilter: combine ipt_NETMAP and ip6t_NETMAP
[deliverable/linux.git] / net / ipv4 / netfilter / ipt_REDIRECT.c
CommitLineData
1da177e4
LT
1/* Redirect. Simple mapping which alters dst to a local IP address. */
2/* (C) 1999-2001 Paul `Rusty' Russell
5b1158e9 3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
1da177e4
LT
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
ff67e4e4 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4
LT
10#include <linux/types.h>
11#include <linux/ip.h>
12#include <linux/timer.h>
13#include <linux/module.h>
14#include <linux/netfilter.h>
15#include <linux/netdevice.h>
16#include <linux/if.h>
17#include <linux/inetdevice.h>
18#include <net/protocol.h>
19#include <net/checksum.h>
20#include <linux/netfilter_ipv4.h>
6709dbbb 21#include <linux/netfilter/x_tables.h>
c7232c99 22#include <net/netfilter/nf_nat.h>
1da177e4
LT
23
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
2ae15b64 26MODULE_DESCRIPTION("Xtables: Connection redirection to localhost");
1da177e4 27
1da177e4 28/* FIXME: Take multiple ranges --RR */
135367b8 29static int redirect_tg_check(const struct xt_tgchk_param *par)
1da177e4 30{
cbc9f2f4 31 const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
1da177e4 32
cbc9f2f4 33 if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
ff67e4e4 34 pr_debug("bad MAP_IPS.\n");
d6b00a53 35 return -EINVAL;
1da177e4
LT
36 }
37 if (mr->rangesize != 1) {
ff67e4e4 38 pr_debug("bad rangesize %u.\n", mr->rangesize);
d6b00a53 39 return -EINVAL;
1da177e4 40 }
d6b00a53 41 return 0;
1da177e4
LT
42}
43
44static unsigned int
4b560b44 45redirect_tg(struct sk_buff *skb, const struct xt_action_param *par)
1da177e4 46{
587aa641 47 struct nf_conn *ct;
1da177e4 48 enum ip_conntrack_info ctinfo;
a144ea4b 49 __be32 newdst;
cbc9f2f4 50 const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
c7232c99 51 struct nf_nat_range newrange;
1da177e4 52
7eb35586
JE
53 NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
54 par->hooknum == NF_INET_LOCAL_OUT);
1da177e4 55
3db05fea 56 ct = nf_ct_get(skb, &ctinfo);
587aa641 57 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
1da177e4
LT
58
59 /* Local packets: make them go to loopback */
7eb35586 60 if (par->hooknum == NF_INET_LOCAL_OUT)
1da177e4
LT
61 newdst = htonl(0x7F000001);
62 else {
63 struct in_device *indev;
cd0bf2d7 64 struct in_ifaddr *ifa;
1da177e4 65
cd0bf2d7 66 newdst = 0;
e905a9ed 67
cd0bf2d7 68 rcu_read_lock();
3db05fea 69 indev = __in_dev_get_rcu(skb->dev);
cd0bf2d7
PM
70 if (indev && (ifa = indev->ifa_list))
71 newdst = ifa->ifa_local;
72 rcu_read_unlock();
1da177e4 73
cd0bf2d7
PM
74 if (!newdst)
75 return NF_DROP;
1da177e4
LT
76 }
77
78 /* Transfer from original range. */
c7232c99
PM
79 memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
80 memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
81 newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS;
82 newrange.min_addr.ip = newdst;
83 newrange.max_addr.ip = newdst;
84 newrange.min_proto = mr->range[0].min;
85 newrange.max_proto = mr->range[0].max;
1da177e4
LT
86
87 /* Hand modified range to generic setup. */
cbc9f2f4 88 return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
1da177e4
LT
89}
90
d3c5ee6d 91static struct xt_target redirect_tg_reg __read_mostly = {
1da177e4 92 .name = "REDIRECT",
ee999d8b 93 .family = NFPROTO_IPV4,
d3c5ee6d 94 .target = redirect_tg,
cbc9f2f4 95 .targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
1d5cd909 96 .table = "nat",
6e23ae2a 97 .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
d3c5ee6d 98 .checkentry = redirect_tg_check,
1da177e4
LT
99 .me = THIS_MODULE,
100};
101
d3c5ee6d 102static int __init redirect_tg_init(void)
1da177e4 103{
d3c5ee6d 104 return xt_register_target(&redirect_tg_reg);
1da177e4
LT
105}
106
d3c5ee6d 107static void __exit redirect_tg_exit(void)
1da177e4 108{
d3c5ee6d 109 xt_unregister_target(&redirect_tg_reg);
1da177e4
LT
110}
111
d3c5ee6d
JE
112module_init(redirect_tg_init);
113module_exit(redirect_tg_exit);
This page took 0.619363 seconds and 5 git commands to generate.