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 |
| 3 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 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> |
| 21 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| 22 | |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 25 | MODULE_DESCRIPTION("iptables REDIRECT target module"); |
| 26 | |
| 27 | #if 0 |
| 28 | #define DEBUGP printk |
| 29 | #else |
| 30 | #define DEBUGP(format, args...) |
| 31 | #endif |
| 32 | |
| 33 | /* FIXME: Take multiple ranges --RR */ |
| 34 | static int |
| 35 | redirect_check(const char *tablename, |
| 36 | const struct ipt_entry *e, |
| 37 | void *targinfo, |
| 38 | unsigned int targinfosize, |
| 39 | unsigned int hook_mask) |
| 40 | { |
| 41 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 42 | |
| 43 | if (strcmp(tablename, "nat") != 0) { |
| 44 | DEBUGP("redirect_check: bad table `%s'.\n", table); |
| 45 | return 0; |
| 46 | } |
| 47 | if (targinfosize != IPT_ALIGN(sizeof(*mr))) { |
| 48 | DEBUGP("redirect_check: size %u.\n", targinfosize); |
| 49 | return 0; |
| 50 | } |
| 51 | if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) { |
| 52 | DEBUGP("redirect_check: bad hooks %x.\n", hook_mask); |
| 53 | return 0; |
| 54 | } |
| 55 | if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) { |
| 56 | DEBUGP("redirect_check: bad MAP_IPS.\n"); |
| 57 | return 0; |
| 58 | } |
| 59 | if (mr->rangesize != 1) { |
| 60 | DEBUGP("redirect_check: bad rangesize %u.\n", mr->rangesize); |
| 61 | return 0; |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | static unsigned int |
| 67 | redirect_target(struct sk_buff **pskb, |
| 68 | const struct net_device *in, |
| 69 | const struct net_device *out, |
| 70 | unsigned int hooknum, |
| 71 | const void *targinfo, |
| 72 | void *userinfo) |
| 73 | { |
| 74 | struct ip_conntrack *ct; |
| 75 | enum ip_conntrack_info ctinfo; |
| 76 | u_int32_t newdst; |
| 77 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 78 | struct ip_nat_range newrange; |
| 79 | |
| 80 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING |
| 81 | || hooknum == NF_IP_LOCAL_OUT); |
| 82 | |
| 83 | ct = ip_conntrack_get(*pskb, &ctinfo); |
| 84 | IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); |
| 85 | |
| 86 | /* Local packets: make them go to loopback */ |
| 87 | if (hooknum == NF_IP_LOCAL_OUT) |
| 88 | newdst = htonl(0x7F000001); |
| 89 | else { |
| 90 | struct in_device *indev; |
| 91 | |
| 92 | /* Device might not have an associated in_device. */ |
| 93 | indev = (struct in_device *)(*pskb)->dev->ip_ptr; |
| 94 | if (indev == NULL || indev->ifa_list == NULL) |
| 95 | return NF_DROP; |
| 96 | |
| 97 | /* Grab first address on interface. */ |
| 98 | newdst = indev->ifa_list->ifa_local; |
| 99 | } |
| 100 | |
| 101 | /* Transfer from original range. */ |
| 102 | newrange = ((struct ip_nat_range) |
| 103 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, |
| 104 | newdst, newdst, |
| 105 | mr->range[0].min, mr->range[0].max }); |
| 106 | |
| 107 | /* Hand modified range to generic setup. */ |
| 108 | return ip_nat_setup_info(ct, &newrange, hooknum); |
| 109 | } |
| 110 | |
| 111 | static struct ipt_target redirect_reg = { |
| 112 | .name = "REDIRECT", |
| 113 | .target = redirect_target, |
| 114 | .checkentry = redirect_check, |
| 115 | .me = THIS_MODULE, |
| 116 | }; |
| 117 | |
| 118 | static int __init init(void) |
| 119 | { |
| 120 | return ipt_register_target(&redirect_reg); |
| 121 | } |
| 122 | |
| 123 | static void __exit fini(void) |
| 124 | { |
| 125 | ipt_unregister_target(&redirect_reg); |
| 126 | } |
| 127 | |
| 128 | module_init(init); |
| 129 | module_exit(fini); |