blob: 74f1f55fd61a78f0346b9cfb4ce56c5093502a40 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Redirect. Simple mapping which alters dst to a local IP address. */
2/* (C) 1999-2001 Paul `Rusty' Russell
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08003 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080021#include <linux/netfilter/x_tables.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080022#include <net/netfilter/nf_nat_rule.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24MODULE_LICENSE("GPL");
25MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080026MODULE_DESCRIPTION("Xtables: Connection redirection to localhost");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* FIXME: Take multiple ranges --RR */
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020029static bool redirect_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020031 const struct nf_nat_multi_range_compat *mr = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010034 pr_debug("bad MAP_IPS.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070035 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 }
37 if (mr->rangesize != 1) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010038 pr_debug("bad rangesize %u.\n", mr->rangesize);
Jan Engelhardte1931b72007-07-07 22:16:26 -070039 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
Jan Engelhardte1931b72007-07-07 22:16:26 -070041 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
44static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020045redirect_tg(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Patrick McHardy587aa642007-03-14 16:37:25 -070047 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 enum ip_conntrack_info ctinfo;
Al Viroa144ea42006-09-28 18:00:55 -070049 __be32 newdst;
Jan Engelhardt7eb35582008-10-08 11:35:19 +020050 const struct nf_nat_multi_range_compat *mr = par->targinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -070051 struct nf_nat_range newrange;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jan Engelhardt7eb35582008-10-08 11:35:19 +020053 NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
54 par->hooknum == NF_INET_LOCAL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Herbert Xu3db05fe2007-10-15 00:53:15 -070056 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy587aa642007-03-14 16:37:25 -070057 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 /* Local packets: make them go to loopback */
Jan Engelhardt7eb35582008-10-08 11:35:19 +020060 if (par->hooknum == NF_INET_LOCAL_OUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 newdst = htonl(0x7F000001);
62 else {
63 struct in_device *indev;
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070064 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070066 newdst = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090067
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070068 rcu_read_lock();
Herbert Xu3db05fe2007-10-15 00:53:15 -070069 indev = __in_dev_get_rcu(skb->dev);
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070070 if (indev && (ifa = indev->ifa_list))
71 newdst = ifa->ifa_local;
72 rcu_read_unlock();
73
74 if (!newdst)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 /* Transfer from original range. */
Patrick McHardy587aa642007-03-14 16:37:25 -070079 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
81 newdst, newdst,
82 mr->range[0].min, mr->range[0].max });
83
84 /* Hand modified range to generic setup. */
Patrick McHardycc01dcb2007-12-17 22:38:20 -080085 return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_DST);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080088static struct xt_target redirect_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .name = "REDIRECT",
Jan Engelhardtee999d82008-10-08 11:35:01 +020090 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080091 .target = redirect_tg,
Patrick McHardy587aa642007-03-14 16:37:25 -070092 .targetsize = sizeof(struct nf_nat_multi_range_compat),
Patrick McHardy1d5cd902006-03-20 18:01:14 -080093 .table = "nat",
Patrick McHardy6e23ae22007-11-19 18:53:30 -080094 .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095 .checkentry = redirect_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .me = THIS_MODULE,
97};
98
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080099static int __init redirect_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800101 return xt_register_target(&redirect_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104static void __exit redirect_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800106 xt_unregister_target(&redirect_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109module_init(redirect_tg_init);
110module_exit(redirect_tg_exit);