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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 int netmap_tg_check(const struct xt_tgchk_param
*par
)
27 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
29 if (!(mr
->range
[0].flags
& NF_NAT_RANGE_MAP_IPS
)) {
30 pr_debug("bad MAP_IPS.\n");
33 if (mr
->rangesize
!= 1) {
34 pr_debug("bad rangesize %u.\n", mr
->rangesize
);
41 netmap_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
44 enum ip_conntrack_info ctinfo
;
45 __be32 new_ip
, netmask
;
46 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
47 struct nf_nat_ipv4_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 par
->hooknum
== NF_INET_LOCAL_IN
);
53 ct
= nf_ct_get(skb
, &ctinfo
);
55 netmask
= ~(mr
->range
[0].min_ip
^ mr
->range
[0].max_ip
);
57 if (par
->hooknum
== NF_INET_PRE_ROUTING
||
58 par
->hooknum
== NF_INET_LOCAL_OUT
)
59 new_ip
= ip_hdr(skb
)->daddr
& ~netmask
;
61 new_ip
= ip_hdr(skb
)->saddr
& ~netmask
;
62 new_ip
|= mr
->range
[0].min_ip
& netmask
;
64 newrange
= ((struct nf_nat_ipv4_range
)
65 { mr
->range
[0].flags
| NF_NAT_RANGE_MAP_IPS
,
67 mr
->range
[0].min
, mr
->range
[0].max
});
69 /* Hand modified range to generic setup. */
70 return nf_nat_setup_info(ct
, &newrange
, HOOK2MANIP(par
->hooknum
));
73 static struct xt_target netmap_tg_reg __read_mostly
= {
75 .family
= NFPROTO_IPV4
,
77 .targetsize
= sizeof(struct nf_nat_ipv4_multi_range_compat
),
79 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
80 (1 << NF_INET_POST_ROUTING
) |
81 (1 << NF_INET_LOCAL_OUT
) |
82 (1 << NF_INET_LOCAL_IN
),
83 .checkentry
= netmap_tg_check
,
87 static int __init
netmap_tg_init(void)
89 return xt_register_target(&netmap_tg_reg
);
92 static void __exit
netmap_tg_exit(void)
94 xt_unregister_target(&netmap_tg_reg
);
97 module_init(netmap_tg_init
);
98 module_exit(netmap_tg_exit
);