blob: ef496105eae18c27bbc83b077c0597abba4d69c4 [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 */
9
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>
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 Engelhardte1931b72007-07-07 22:16:26 -070029static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080030redirect_tg_check(const char *tablename, const void *e,
31 const struct xt_target *target, void *targinfo,
32 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Patrick McHardy587aa642007-03-14 16:37:25 -070034 const struct nf_nat_multi_range_compat *mr = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
Patrick McHardy0d537782007-07-07 22:39:38 -070037 pr_debug("redirect_check: bad MAP_IPS.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070038 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
40 if (mr->rangesize != 1) {
Patrick McHardy0d537782007-07-07 22:39:38 -070041 pr_debug("redirect_check: bad rangesize %u.\n", mr->rangesize);
Jan Engelhardte1931b72007-07-07 22:16:26 -070042 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
Jan Engelhardte1931b72007-07-07 22:16:26 -070044 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47static unsigned int
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080048redirect_tg(struct sk_buff *skb, const struct net_device *in,
49 const struct net_device *out, unsigned int hooknum,
50 const struct xt_target *target, const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Patrick McHardy587aa642007-03-14 16:37:25 -070052 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 enum ip_conntrack_info ctinfo;
Al Viroa144ea42006-09-28 18:00:55 -070054 __be32 newdst;
Patrick McHardy587aa642007-03-14 16:37:25 -070055 const struct nf_nat_multi_range_compat *mr = targinfo;
56 struct nf_nat_range newrange;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Patrick McHardy6e23ae22007-11-19 18:53:30 -080058 NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING
59 || hooknum == NF_INET_LOCAL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Herbert Xu3db05fe2007-10-15 00:53:15 -070061 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy587aa642007-03-14 16:37:25 -070062 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Local packets: make them go to loopback */
Patrick McHardy6e23ae22007-11-19 18:53:30 -080065 if (hooknum == NF_INET_LOCAL_OUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 newdst = htonl(0x7F000001);
67 else {
68 struct in_device *indev;
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070069 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070071 newdst = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090072
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070073 rcu_read_lock();
Herbert Xu3db05fe2007-10-15 00:53:15 -070074 indev = __in_dev_get_rcu(skb->dev);
Patrick McHardycd0bf2d2005-09-13 13:48:58 -070075 if (indev && (ifa = indev->ifa_list))
76 newdst = ifa->ifa_local;
77 rcu_read_unlock();
78
79 if (!newdst)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82
83 /* Transfer from original range. */
Patrick McHardy587aa642007-03-14 16:37:25 -070084 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
86 newdst, newdst,
87 mr->range[0].min, mr->range[0].max });
88
89 /* Hand modified range to generic setup. */
Patrick McHardycc01dcb2007-12-17 22:38:20 -080090 return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_DST);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080093static struct xt_target redirect_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .name = "REDIRECT",
Jan Engelhardtee999d82008-10-08 11:35:01 +020095 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080096 .target = redirect_tg,
Patrick McHardy587aa642007-03-14 16:37:25 -070097 .targetsize = sizeof(struct nf_nat_multi_range_compat),
Patrick McHardy1d5cd902006-03-20 18:01:14 -080098 .table = "nat",
Patrick McHardy6e23ae22007-11-19 18:53:30 -080099 .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800100 .checkentry = redirect_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .me = THIS_MODULE,
102};
103
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104static int __init redirect_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800106 return xt_register_target(&redirect_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109static void __exit redirect_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800111 xt_unregister_target(&redirect_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800114module_init(redirect_tg_init);
115module_exit(redirect_tg_exit);