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");
26 netmap_tg_check(const char *tablename
, const void *e
,
27 const struct xt_target
*target
, void *targinfo
,
28 unsigned int hook_mask
)
30 const struct nf_nat_multi_range_compat
*mr
= targinfo
;
32 if (!(mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
)) {
33 pr_debug("NETMAP:check: bad MAP_IPS.\n");
36 if (mr
->rangesize
!= 1) {
37 pr_debug("NETMAP:check: bad rangesize %u.\n", mr
->rangesize
);
44 netmap_tg(struct sk_buff
*skb
, const struct net_device
*in
,
45 const struct net_device
*out
, unsigned int hooknum
,
46 const struct xt_target
*target
, const void *targinfo
)
49 enum ip_conntrack_info ctinfo
;
50 __be32 new_ip
, netmask
;
51 const struct nf_nat_multi_range_compat
*mr
= targinfo
;
52 struct nf_nat_range newrange
;
54 NF_CT_ASSERT(hooknum
== NF_INET_PRE_ROUTING
55 || hooknum
== NF_INET_POST_ROUTING
56 || hooknum
== NF_INET_LOCAL_OUT
);
57 ct
= nf_ct_get(skb
, &ctinfo
);
59 netmask
= ~(mr
->range
[0].min_ip
^ mr
->range
[0].max_ip
);
61 if (hooknum
== NF_INET_PRE_ROUTING
|| hooknum
== NF_INET_LOCAL_OUT
)
62 new_ip
= ip_hdr(skb
)->daddr
& ~netmask
;
64 new_ip
= ip_hdr(skb
)->saddr
& ~netmask
;
65 new_ip
|= mr
->range
[0].min_ip
& netmask
;
67 newrange
= ((struct nf_nat_range
)
68 { mr
->range
[0].flags
| IP_NAT_RANGE_MAP_IPS
,
70 mr
->range
[0].min
, mr
->range
[0].max
});
72 /* Hand modified range to generic setup. */
73 return nf_nat_setup_info(ct
, &newrange
, HOOK2MANIP(hooknum
));
76 static struct xt_target netmap_tg_reg __read_mostly
= {
80 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
82 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
83 (1 << NF_INET_POST_ROUTING
) |
84 (1 << NF_INET_LOCAL_OUT
),
85 .checkentry
= netmap_tg_check
,
89 static int __init
netmap_tg_init(void)
91 return xt_register_target(&netmap_tg_reg
);
94 static void __exit
netmap_tg_exit(void)
96 xt_unregister_target(&netmap_tg_reg
);
99 module_init(netmap_tg_init
);
100 module_exit(netmap_tg_exit
);