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).
6 /* (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk>
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.
13 #include <linux/config.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>
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");
29 #define DEBUGP(format, args...)
33 check(const char *tablename
,
34 const struct ipt_entry
*e
,
36 unsigned int targinfosize
,
37 unsigned int hook_mask
)
39 const struct ip_nat_multi_range
*mr
= targinfo
;
41 if (strcmp(tablename
, "nat") != 0) {
42 DEBUGP(MODULENAME
":check: bad table `%s'.\n", tablename
);
45 if (targinfosize
!= IPT_ALIGN(sizeof(*mr
))) {
46 DEBUGP(MODULENAME
":check: size %u.\n", targinfosize
);
49 if (hook_mask
& ~((1 << NF_IP_PRE_ROUTING
) | (1 << NF_IP_POST_ROUTING
))) {
50 DEBUGP(MODULENAME
":check: bad hooks %x.\n", hook_mask
);
53 if (!(mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
)) {
54 DEBUGP(MODULENAME
":check: bad MAP_IPS.\n");
57 if (mr
->rangesize
!= 1) {
58 DEBUGP(MODULENAME
":check: bad rangesize %u.\n", mr
->rangesize
);
65 target(struct sk_buff
**pskb
,
66 const struct net_device
*in
,
67 const struct net_device
*out
,
72 struct ip_conntrack
*ct
;
73 enum ip_conntrack_info ctinfo
;
74 u_int32_t new_ip
, netmask
;
75 const struct ip_nat_multi_range
*mr
= targinfo
;
76 struct ip_nat_multi_range newrange
;
78 IP_NF_ASSERT(hooknum
== NF_IP_PRE_ROUTING
79 || hooknum
== NF_IP_POST_ROUTING
);
80 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
82 netmask
= ~(mr
->range
[0].min_ip
^ mr
->range
[0].max_ip
);
84 if (hooknum
== NF_IP_PRE_ROUTING
)
85 new_ip
= (*pskb
)->nh
.iph
->daddr
& ~netmask
;
87 new_ip
= (*pskb
)->nh
.iph
->saddr
& ~netmask
;
88 new_ip
|= mr
->range
[0].min_ip
& netmask
;
90 newrange
= ((struct ip_nat_multi_range
)
91 { 1, { { mr
->range
[0].flags
| IP_NAT_RANGE_MAP_IPS
,
93 mr
->range
[0].min
, mr
->range
[0].max
} } });
95 /* Hand modified range to generic setup. */
96 return ip_nat_setup_info(ct
, &newrange
, hooknum
);
99 static struct ipt_target target_module
= {
106 static int __init
init(void)
108 return ipt_register_target(&target_module
);
111 static void __exit
fini(void)
113 ipt_unregister_target(&target_module
);