Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Redirect. Simple mapping which alters dst to a local IP address. */ |
| 2 | /* (C) 1999-2001 Paul `Rusty' Russell |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 3 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 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 Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 21 | #include <linux/netfilter/x_tables.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 22 | #include <net/netfilter/nf_nat_rule.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 26 | MODULE_DESCRIPTION("iptables REDIRECT target module"); |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | /* FIXME: Take multiple ranges --RR */ |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 29 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 30 | redirect_tg_check(const char *tablename, const void *e, |
| 31 | const struct xt_target *target, void *targinfo, |
| 32 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 34 | const struct nf_nat_multi_range_compat *mr = targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 37 | pr_debug("redirect_check: bad MAP_IPS.\n"); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 38 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | if (mr->rangesize != 1) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 41 | pr_debug("redirect_check: bad rangesize %u.\n", mr->rangesize); |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 42 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
Jan Engelhardt | e1931b7 | 2007-07-07 22:16:26 -0700 | [diff] [blame] | 44 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static unsigned int |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 48 | redirect_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 52 | struct nf_conn *ct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | enum ip_conntrack_info ctinfo; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 54 | __be32 newdst; |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 55 | const struct nf_nat_multi_range_compat *mr = targinfo; |
| 56 | struct nf_nat_range newrange; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 58 | NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING |
| 59 | || hooknum == NF_INET_LOCAL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 61 | ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 62 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* Local packets: make them go to loopback */ |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 65 | if (hooknum == NF_INET_LOCAL_OUT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | newdst = htonl(0x7F000001); |
| 67 | else { |
| 68 | struct in_device *indev; |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 69 | struct in_ifaddr *ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 71 | newdst = 0; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 72 | |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 73 | rcu_read_lock(); |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 74 | indev = __in_dev_get_rcu(skb->dev); |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 75 | if (indev && (ifa = indev->ifa_list)) |
| 76 | newdst = ifa->ifa_local; |
| 77 | rcu_read_unlock(); |
| 78 | |
| 79 | if (!newdst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Transfer from original range. */ |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 84 | newrange = ((struct nf_nat_range) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { 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 McHardy | cc01dcb | 2007-12-17 22:38:20 -0800 | [diff] [blame^] | 90 | return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_DST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 93 | static struct xt_target redirect_tg_reg __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .name = "REDIRECT", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame] | 95 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 96 | .target = redirect_tg, |
Patrick McHardy | 587aa64 | 2007-03-14 16:37:25 -0700 | [diff] [blame] | 97 | .targetsize = sizeof(struct nf_nat_multi_range_compat), |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 98 | .table = "nat", |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 99 | .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 100 | .checkentry = redirect_tg_check, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .me = THIS_MODULE, |
| 102 | }; |
| 103 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 104 | static int __init redirect_tg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 106 | return xt_register_target(&redirect_tg_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 109 | static void __exit redirect_tg_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 111 | xt_unregister_target(&redirect_tg_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 114 | module_init(redirect_tg_init); |
| 115 | module_exit(redirect_tg_exit); |