Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* NETMAP - static NAT mapping of IP network addresses (1:1). |
| 2 | * The mapping can be applied to source (POSTROUTING), |
| 3 | * destination (PREROUTING), or both (with separate rules). |
| 4 | */ |
| 5 | |
| 6 | /* (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/ip.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/netfilter.h> |
| 17 | #include <linux/netfilter_ipv4.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 18 | #ifdef CONFIG_NF_NAT_NEEDED |
| 19 | #include <net/netfilter/nf_nat_rule.h> |
| 20 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 22 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define MODULENAME "NETMAP" |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>"); |
| 27 | MODULE_DESCRIPTION("iptables 1:1 NAT mapping of IP networks target"); |
| 28 | |
| 29 | #if 0 |
| 30 | #define DEBUGP printk |
| 31 | #else |
| 32 | #define DEBUGP(format, args...) |
| 33 | #endif |
| 34 | |
| 35 | static int |
| 36 | check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 37 | const void *e, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 38 | const struct xt_target *target, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | void *targinfo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | unsigned int hook_mask) |
| 41 | { |
| 42 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) { |
| 45 | DEBUGP(MODULENAME":check: bad MAP_IPS.\n"); |
| 46 | return 0; |
| 47 | } |
| 48 | if (mr->rangesize != 1) { |
| 49 | DEBUGP(MODULENAME":check: bad rangesize %u.\n", mr->rangesize); |
| 50 | return 0; |
| 51 | } |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static unsigned int |
| 56 | target(struct sk_buff **pskb, |
| 57 | const struct net_device *in, |
| 58 | const struct net_device *out, |
| 59 | unsigned int hooknum, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 60 | const struct xt_target *target, |
Patrick McHardy | fe1cb10 | 2006-08-22 00:35:47 -0700 | [diff] [blame] | 61 | const void *targinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | struct ip_conntrack *ct; |
| 64 | enum ip_conntrack_info ctinfo; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 65 | __be32 new_ip, netmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 67 | struct ip_nat_range newrange; |
| 68 | |
| 69 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING |
Gary Wayne Smith | 000efe1 | 2005-08-14 17:33:24 -0700 | [diff] [blame] | 70 | || hooknum == NF_IP_POST_ROUTING |
| 71 | || hooknum == NF_IP_LOCAL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | ct = ip_conntrack_get(*pskb, &ctinfo); |
| 73 | |
| 74 | netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip); |
| 75 | |
Gary Wayne Smith | 000efe1 | 2005-08-14 17:33:24 -0700 | [diff] [blame] | 76 | if (hooknum == NF_IP_PRE_ROUTING || hooknum == NF_IP_LOCAL_OUT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | new_ip = (*pskb)->nh.iph->daddr & ~netmask; |
| 78 | else |
| 79 | new_ip = (*pskb)->nh.iph->saddr & ~netmask; |
| 80 | new_ip |= mr->range[0].min_ip & netmask; |
| 81 | |
| 82 | newrange = ((struct ip_nat_range) |
| 83 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, |
| 84 | new_ip, new_ip, |
| 85 | mr->range[0].min, mr->range[0].max }); |
| 86 | |
| 87 | /* Hand modified range to generic setup. */ |
| 88 | return ip_nat_setup_info(ct, &newrange, hooknum); |
| 89 | } |
| 90 | |
| 91 | static struct ipt_target target_module = { |
| 92 | .name = MODULENAME, |
| 93 | .target = target, |
Patrick McHardy | 1d5cd90 | 2006-03-20 18:01:14 -0800 | [diff] [blame] | 94 | .targetsize = sizeof(struct ip_nat_multi_range_compat), |
| 95 | .table = "nat", |
| 96 | .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_POST_ROUTING) | |
| 97 | (1 << NF_IP_LOCAL_OUT), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | .checkentry = check, |
| 99 | .me = THIS_MODULE |
| 100 | }; |
| 101 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 102 | static int __init ipt_netmap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | return ipt_register_target(&target_module); |
| 105 | } |
| 106 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 107 | static void __exit ipt_netmap_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | ipt_unregister_target(&target_module); |
| 110 | } |
| 111 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 112 | module_init(ipt_netmap_init); |
| 113 | module_exit(ipt_netmap_fini); |