blob: fd7aaa347cd8aac4332066af2c9edd9123ca3725 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080018#include <linux/netfilter/x_tables.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080019#ifdef CONFIG_NF_NAT_NEEDED
20#include <net/netfilter/nf_nat_rule.h>
21#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/netfilter_ipv4/ip_nat_rule.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define MODULENAME "NETMAP"
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
28MODULE_DESCRIPTION("iptables 1:1 NAT mapping of IP networks target");
29
30#if 0
31#define DEBUGP printk
32#else
33#define DEBUGP(format, args...)
34#endif
35
36static int
37check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080038 const void *e,
Patrick McHardyc4986732006-03-20 18:02:56 -080039 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 unsigned int hook_mask)
42{
43 const struct ip_nat_multi_range_compat *mr = targinfo;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
46 DEBUGP(MODULENAME":check: bad MAP_IPS.\n");
47 return 0;
48 }
49 if (mr->rangesize != 1) {
50 DEBUGP(MODULENAME":check: bad rangesize %u.\n", mr->rangesize);
51 return 0;
52 }
53 return 1;
54}
55
56static unsigned int
57target(struct sk_buff **pskb,
58 const struct net_device *in,
59 const struct net_device *out,
60 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -080061 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070062 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct ip_conntrack *ct;
65 enum ip_conntrack_info ctinfo;
Al Viro6a19d612006-09-28 14:22:24 -070066 __be32 new_ip, netmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 const struct ip_nat_multi_range_compat *mr = targinfo;
68 struct ip_nat_range newrange;
69
70 IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
Gary Wayne Smith000efe12005-08-14 17:33:24 -070071 || hooknum == NF_IP_POST_ROUTING
72 || hooknum == NF_IP_LOCAL_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 ct = ip_conntrack_get(*pskb, &ctinfo);
74
75 netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
76
Gary Wayne Smith000efe12005-08-14 17:33:24 -070077 if (hooknum == NF_IP_PRE_ROUTING || hooknum == NF_IP_LOCAL_OUT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 new_ip = (*pskb)->nh.iph->daddr & ~netmask;
79 else
80 new_ip = (*pskb)->nh.iph->saddr & ~netmask;
81 new_ip |= mr->range[0].min_ip & netmask;
82
83 newrange = ((struct ip_nat_range)
84 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
85 new_ip, new_ip,
86 mr->range[0].min, mr->range[0].max });
87
88 /* Hand modified range to generic setup. */
89 return ip_nat_setup_info(ct, &newrange, hooknum);
90}
91
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080092static struct xt_target target_module = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .name = MODULENAME,
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080094 .family = AF_INET,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090095 .target = target,
Patrick McHardy1d5cd902006-03-20 18:01:14 -080096 .targetsize = sizeof(struct ip_nat_multi_range_compat),
97 .table = "nat",
98 .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_POST_ROUTING) |
99 (1 << NF_IP_LOCAL_OUT),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .checkentry = check,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900101 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800104static int __init ipt_netmap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800106 return xt_register_target(&target_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800109static void __exit ipt_netmap_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800111 xt_unregister_target(&target_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800114module_init(ipt_netmap_init);
115module_exit(ipt_netmap_fini);