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 | #ifdef CONFIG_NF_NAT_NEEDED |
| 23 | #include <net/netfilter/nf_nat_rule.h> |
| 24 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 26 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 30 | MODULE_DESCRIPTION("iptables REDIRECT target module"); |
| 31 | |
| 32 | #if 0 |
| 33 | #define DEBUGP printk |
| 34 | #else |
| 35 | #define DEBUGP(format, args...) |
| 36 | #endif |
| 37 | |
| 38 | /* FIXME: Take multiple ranges --RR */ |
| 39 | static int |
| 40 | redirect_check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 41 | const void *e, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 42 | const struct xt_target *target, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | void *targinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | unsigned int hook_mask) |
| 45 | { |
| 46 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
| 49 | DEBUGP("redirect_check: bad MAP_IPS.\n"); |
| 50 | return 0; |
| 51 | } |
| 52 | if (mr->rangesize != 1) { |
| 53 | DEBUGP("redirect_check: bad rangesize %u.\n", mr->rangesize); |
| 54 | return 0; |
| 55 | } |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | static unsigned int |
| 60 | redirect_target(struct sk_buff **pskb, |
| 61 | const struct net_device *in, |
| 62 | const struct net_device *out, |
| 63 | unsigned int hooknum, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 64 | const struct xt_target *target, |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 65 | const void *targinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | struct ip_conntrack *ct; |
| 68 | enum ip_conntrack_info ctinfo; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 69 | __be32 newdst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 71 | struct ip_nat_range newrange; |
| 72 | |
| 73 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING |
| 74 | || hooknum == NF_IP_LOCAL_OUT); |
| 75 | |
| 76 | ct = ip_conntrack_get(*pskb, &ctinfo); |
| 77 | IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); |
| 78 | |
| 79 | /* Local packets: make them go to loopback */ |
| 80 | if (hooknum == NF_IP_LOCAL_OUT) |
| 81 | newdst = htonl(0x7F000001); |
| 82 | else { |
| 83 | struct in_device *indev; |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 84 | struct in_ifaddr *ifa; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 86 | newdst = 0; |
| 87 | |
| 88 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 89 | indev = __in_dev_get_rcu((*pskb)->dev); |
Patrick McHardy | cd0bf2d | 2005-09-13 13:48:58 -0700 | [diff] [blame] | 90 | if (indev && (ifa = indev->ifa_list)) |
| 91 | newdst = ifa->ifa_local; |
| 92 | rcu_read_unlock(); |
| 93 | |
| 94 | if (!newdst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* Transfer from original range. */ |
| 99 | newrange = ((struct ip_nat_range) |
| 100 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, |
| 101 | newdst, newdst, |
| 102 | mr->range[0].min, mr->range[0].max }); |
| 103 | |
| 104 | /* Hand modified range to generic setup. */ |
| 105 | return ip_nat_setup_info(ct, &newrange, hooknum); |
| 106 | } |
| 107 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 108 | static struct xt_target redirect_reg = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | .name = "REDIRECT", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 110 | .family = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .target = redirect_target, |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 112 | .targetsize = sizeof(struct ip_nat_multi_range_compat), |
| 113 | .table = "nat", |
| 114 | .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .checkentry = redirect_check, |
| 116 | .me = THIS_MODULE, |
| 117 | }; |
| 118 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 119 | static int __init ipt_redirect_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 121 | return xt_register_target(&redirect_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 124 | static void __exit ipt_redirect_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 126 | xt_unregister_target(&redirect_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 129 | module_init(ipt_redirect_init); |
| 130 | module_exit(ipt_redirect_fini); |