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 | |
| 13 | #include <linux/config.h> |
| 14 | #include <linux/ip.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/netfilter.h> |
| 18 | #include <linux/netfilter_ipv4.h> |
| 19 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| 20 | |
| 21 | #define MODULENAME "NETMAP" |
| 22 | MODULE_LICENSE("GPL"); |
| 23 | MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>"); |
| 24 | MODULE_DESCRIPTION("iptables 1:1 NAT mapping of IP networks target"); |
| 25 | |
| 26 | #if 0 |
| 27 | #define DEBUGP printk |
| 28 | #else |
| 29 | #define DEBUGP(format, args...) |
| 30 | #endif |
| 31 | |
| 32 | static int |
| 33 | check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame^] | 34 | const void *e, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | void *targinfo, |
| 36 | unsigned int targinfosize, |
| 37 | unsigned int hook_mask) |
| 38 | { |
| 39 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 40 | |
| 41 | if (strcmp(tablename, "nat") != 0) { |
| 42 | DEBUGP(MODULENAME":check: bad table `%s'.\n", tablename); |
| 43 | return 0; |
| 44 | } |
| 45 | if (targinfosize != IPT_ALIGN(sizeof(*mr))) { |
| 46 | DEBUGP(MODULENAME":check: size %u.\n", targinfosize); |
| 47 | return 0; |
| 48 | } |
Gary Wayne Smith | 000efe1 | 2005-08-14 17:33:24 -0700 | [diff] [blame] | 49 | if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_POST_ROUTING) | |
| 50 | (1 << NF_IP_LOCAL_OUT))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | DEBUGP(MODULENAME":check: bad hooks %x.\n", hook_mask); |
| 52 | return 0; |
| 53 | } |
| 54 | if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) { |
| 55 | DEBUGP(MODULENAME":check: bad MAP_IPS.\n"); |
| 56 | return 0; |
| 57 | } |
| 58 | if (mr->rangesize != 1) { |
| 59 | DEBUGP(MODULENAME":check: bad rangesize %u.\n", mr->rangesize); |
| 60 | return 0; |
| 61 | } |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | static unsigned int |
| 66 | target(struct sk_buff **pskb, |
| 67 | const struct net_device *in, |
| 68 | const struct net_device *out, |
| 69 | unsigned int hooknum, |
| 70 | const void *targinfo, |
| 71 | void *userinfo) |
| 72 | { |
| 73 | struct ip_conntrack *ct; |
| 74 | enum ip_conntrack_info ctinfo; |
| 75 | u_int32_t new_ip, netmask; |
| 76 | const struct ip_nat_multi_range_compat *mr = targinfo; |
| 77 | struct ip_nat_range newrange; |
| 78 | |
| 79 | IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING |
Gary Wayne Smith | 000efe1 | 2005-08-14 17:33:24 -0700 | [diff] [blame] | 80 | || hooknum == NF_IP_POST_ROUTING |
| 81 | || hooknum == NF_IP_LOCAL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | ct = ip_conntrack_get(*pskb, &ctinfo); |
| 83 | |
| 84 | netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip); |
| 85 | |
Gary Wayne Smith | 000efe1 | 2005-08-14 17:33:24 -0700 | [diff] [blame] | 86 | if (hooknum == NF_IP_PRE_ROUTING || hooknum == NF_IP_LOCAL_OUT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | new_ip = (*pskb)->nh.iph->daddr & ~netmask; |
| 88 | else |
| 89 | new_ip = (*pskb)->nh.iph->saddr & ~netmask; |
| 90 | new_ip |= mr->range[0].min_ip & netmask; |
| 91 | |
| 92 | newrange = ((struct ip_nat_range) |
| 93 | { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS, |
| 94 | new_ip, new_ip, |
| 95 | mr->range[0].min, mr->range[0].max }); |
| 96 | |
| 97 | /* Hand modified range to generic setup. */ |
| 98 | return ip_nat_setup_info(ct, &newrange, hooknum); |
| 99 | } |
| 100 | |
| 101 | static struct ipt_target target_module = { |
| 102 | .name = MODULENAME, |
| 103 | .target = target, |
| 104 | .checkentry = check, |
| 105 | .me = THIS_MODULE |
| 106 | }; |
| 107 | |
| 108 | static int __init init(void) |
| 109 | { |
| 110 | return ipt_register_target(&target_module); |
| 111 | } |
| 112 | |
| 113 | static void __exit fini(void) |
| 114 | { |
| 115 | ipt_unregister_target(&target_module); |
| 116 | } |
| 117 | |
| 118 | module_init(init); |
| 119 | module_exit(fini); |