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.
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <net/netfilter/nf_nat_rule.h>
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
23 MODULE_DESCRIPTION("Xtables: 1:1 NAT mapping of IPv4 subnets");
25 static bool netmap_tg_check(const struct xt_tgchk_param *par)
27 const struct nf_nat_multi_range_compat *mr = par->targinfo;
29 if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
30 pr_debug("NETMAP:check: bad MAP_IPS.\n");
33 if (mr->rangesize != 1) {
34 pr_debug("NETMAP:check: bad rangesize %u.\n", mr->rangesize);
41 netmap_tg(struct sk_buff *skb, const struct xt_target_param *par)
44 enum ip_conntrack_info ctinfo;
45 __be32 new_ip, netmask;
46 const struct nf_nat_multi_range_compat *mr = par->targinfo;
47 struct nf_nat_range newrange;
49 NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
50 par->hooknum == NF_INET_POST_ROUTING ||
51 par->hooknum == NF_INET_LOCAL_OUT);
52 ct = nf_ct_get(skb, &ctinfo);
54 netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
56 if (par->hooknum == NF_INET_PRE_ROUTING ||
57 par->hooknum == NF_INET_LOCAL_OUT)
58 new_ip = ip_hdr(skb)->daddr & ~netmask;
60 new_ip = ip_hdr(skb)->saddr & ~netmask;
61 new_ip |= mr->range[0].min_ip & netmask;
63 newrange = ((struct nf_nat_range)
64 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
66 mr->range[0].min, mr->range[0].max });
68 /* Hand modified range to generic setup. */
69 return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
72 static struct xt_target netmap_tg_reg __read_mostly = {
74 .family = NFPROTO_IPV4,
76 .targetsize = sizeof(struct nf_nat_multi_range_compat),
78 .hooks = (1 << NF_INET_PRE_ROUTING) |
79 (1 << NF_INET_POST_ROUTING) |
80 (1 << NF_INET_LOCAL_OUT),
81 .checkentry = netmap_tg_check,
85 static int __init netmap_tg_init(void)
87 return xt_register_target(&netmap_tg_reg);
90 static void __exit netmap_tg_exit(void)
92 xt_unregister_target(&netmap_tg_reg);
95 module_init(netmap_tg_init);
96 module_exit(netmap_tg_exit);