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).
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #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>
18 #include <linux/netfilter/x_tables.h>
19 #include <net/netfilter/nf_nat_rule.h>
20 
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
23 MODULE_DESCRIPTION("Xtables: 1:1 NAT mapping of IPv4 subnets");
24 
netmap_tg_check(const struct xt_tgchk_param * par)25 static int netmap_tg_check(const struct xt_tgchk_param *par)
26 {
27 	const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
28 
29 	if (!(mr->range[0].flags & NF_NAT_RANGE_MAP_IPS)) {
30 		pr_debug("bad MAP_IPS.\n");
31 		return -EINVAL;
32 	}
33 	if (mr->rangesize != 1) {
34 		pr_debug("bad rangesize %u.\n", mr->rangesize);
35 		return -EINVAL;
36 	}
37 	return 0;
38 }
39 
40 static unsigned int
netmap_tg(struct sk_buff * skb,const struct xt_action_param * par)41 netmap_tg(struct sk_buff *skb, const struct xt_action_param *par)
42 {
43 	struct nf_conn *ct;
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;
48 
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);
54 
55 	netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
56 
57 	if (par->hooknum == NF_INET_PRE_ROUTING ||
58 	    par->hooknum == NF_INET_LOCAL_OUT)
59 		new_ip = ip_hdr(skb)->daddr & ~netmask;
60 	else
61 		new_ip = ip_hdr(skb)->saddr & ~netmask;
62 	new_ip |= mr->range[0].min_ip & netmask;
63 
64 	newrange = ((struct nf_nat_ipv4_range)
65 		{ mr->range[0].flags | NF_NAT_RANGE_MAP_IPS,
66 		  new_ip, new_ip,
67 		  mr->range[0].min, mr->range[0].max });
68 
69 	/* Hand modified range to generic setup. */
70 	return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
71 }
72 
73 static struct xt_target netmap_tg_reg __read_mostly = {
74 	.name 		= "NETMAP",
75 	.family		= NFPROTO_IPV4,
76 	.target 	= netmap_tg,
77 	.targetsize	= sizeof(struct nf_nat_ipv4_multi_range_compat),
78 	.table		= "nat",
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,
84 	.me 		= THIS_MODULE
85 };
86 
netmap_tg_init(void)87 static int __init netmap_tg_init(void)
88 {
89 	return xt_register_target(&netmap_tg_reg);
90 }
91 
netmap_tg_exit(void)92 static void __exit netmap_tg_exit(void)
93 {
94 	xt_unregister_target(&netmap_tg_reg);
95 }
96 
97 module_init(netmap_tg_init);
98 module_exit(netmap_tg_exit);
99