1 /* Masquerade.  Simple mapping which alters range to a local IP address
2    (depending on route). */
3 
4 /* (C) 1999-2001 Paul `Rusty' Russell
5  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/types.h>
13 #include <linux/inetdevice.h>
14 #include <linux/ip.h>
15 #include <linux/timer.h>
16 #include <linux/module.h>
17 #include <linux/netfilter.h>
18 #include <net/protocol.h>
19 #include <net/ip.h>
20 #include <net/checksum.h>
21 #include <net/route.h>
22 #include <net/netfilter/nf_nat_rule.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter/x_tables.h>
25 
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
28 MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
29 
30 /* FIXME: Multiple targets. --RR */
masquerade_tg_check(const struct xt_tgchk_param * par)31 static int masquerade_tg_check(const struct xt_tgchk_param *par)
32 {
33 	const struct nf_nat_multi_range_compat *mr = par->targinfo;
34 
35 	if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
36 		pr_debug("bad MAP_IPS.\n");
37 		return -EINVAL;
38 	}
39 	if (mr->rangesize != 1) {
40 		pr_debug("bad rangesize %u\n", mr->rangesize);
41 		return -EINVAL;
42 	}
43 	return 0;
44 }
45 
46 static unsigned int
masquerade_tg(struct sk_buff * skb,const struct xt_action_param * par)47 masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
48 {
49 	struct nf_conn *ct;
50 	struct nf_conn_nat *nat;
51 	enum ip_conntrack_info ctinfo;
52 	struct nf_nat_range newrange;
53 	const struct nf_nat_multi_range_compat *mr;
54 	const struct rtable *rt;
55 	__be32 newsrc;
56 
57 	NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
58 
59 	ct = nf_ct_get(skb, &ctinfo);
60 	nat = nfct_nat(ct);
61 
62 	NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
63 			    ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
64 
65 	/* Source address is 0.0.0.0 - locally generated packet that is
66 	 * probably not supposed to be masqueraded.
67 	 */
68 	if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == 0)
69 		return NF_ACCEPT;
70 
71 	mr = par->targinfo;
72 	rt = skb_rtable(skb);
73 	newsrc = inet_select_addr(par->out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
74 	if (!newsrc) {
75 		pr_info("%s ate my IP address\n", par->out->name);
76 		return NF_DROP;
77 	}
78 
79 	nat->masq_index = par->out->ifindex;
80 
81 	/* Transfer from original range. */
82 	newrange = ((struct nf_nat_range)
83 		{ mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
84 		  newsrc, newsrc,
85 		  mr->range[0].min, mr->range[0].max });
86 
87 	/* Hand modified range to generic setup. */
88 	return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_SRC);
89 }
90 
91 static int
device_cmp(struct nf_conn * i,void * ifindex)92 device_cmp(struct nf_conn *i, void *ifindex)
93 {
94 	const struct nf_conn_nat *nat = nfct_nat(i);
95 
96 	if (!nat)
97 		return 0;
98 
99 	return nat->masq_index == (int)(long)ifindex;
100 }
101 
masq_device_event(struct notifier_block * this,unsigned long event,void * ptr)102 static int masq_device_event(struct notifier_block *this,
103 			     unsigned long event,
104 			     void *ptr)
105 {
106 	const struct net_device *dev = ptr;
107 	struct net *net = dev_net(dev);
108 
109 	if (event == NETDEV_DOWN) {
110 		/* Device was downed.  Search entire table for
111 		   conntracks which were associated with that device,
112 		   and forget them. */
113 		NF_CT_ASSERT(dev->ifindex != 0);
114 
115 		nf_ct_iterate_cleanup(net, device_cmp,
116 				      (void *)(long)dev->ifindex);
117 	}
118 
119 	return NOTIFY_DONE;
120 }
121 
masq_inet_event(struct notifier_block * this,unsigned long event,void * ptr)122 static int masq_inet_event(struct notifier_block *this,
123 			   unsigned long event,
124 			   void *ptr)
125 {
126 	struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
127 	return masq_device_event(this, event, dev);
128 }
129 
130 static struct notifier_block masq_dev_notifier = {
131 	.notifier_call	= masq_device_event,
132 };
133 
134 static struct notifier_block masq_inet_notifier = {
135 	.notifier_call	= masq_inet_event,
136 };
137 
138 static struct xt_target masquerade_tg_reg __read_mostly = {
139 	.name		= "MASQUERADE",
140 	.family		= NFPROTO_IPV4,
141 	.target		= masquerade_tg,
142 	.targetsize	= sizeof(struct nf_nat_multi_range_compat),
143 	.table		= "nat",
144 	.hooks		= 1 << NF_INET_POST_ROUTING,
145 	.checkentry	= masquerade_tg_check,
146 	.me		= THIS_MODULE,
147 };
148 
masquerade_tg_init(void)149 static int __init masquerade_tg_init(void)
150 {
151 	int ret;
152 
153 	ret = xt_register_target(&masquerade_tg_reg);
154 
155 	if (ret == 0) {
156 		/* Register for device down reports */
157 		register_netdevice_notifier(&masq_dev_notifier);
158 		/* Register IP address change reports */
159 		register_inetaddr_notifier(&masq_inet_notifier);
160 	}
161 
162 	return ret;
163 }
164 
masquerade_tg_exit(void)165 static void __exit masquerade_tg_exit(void)
166 {
167 	xt_unregister_target(&masquerade_tg_reg);
168 	unregister_netdevice_notifier(&masq_dev_notifier);
169 	unregister_inetaddr_notifier(&masq_inet_notifier);
170 }
171 
172 module_init(masquerade_tg_init);
173 module_exit(masquerade_tg_exit);
174