blob: c0992c75bdac5d2df93990d681ccf9b491c5b74c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Masquerade. Simple mapping which alters range to a local IP address
2 (depending on route). */
3
4/* (C) 1999-2001 Paul `Rusty' Russell
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08005 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020013#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020021#include <net/route.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080022#include <net/netfilter/nf_nat_rule.h>
Patrick McHardy587aa6412007-03-14 16:37:25 -070023#include <linux/netfilter_ipv4.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080024#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080028MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* Lock protects masq region inside conntrack */
Patrick McHardye45b1be2005-06-21 14:01:30 -070031static DEFINE_RWLOCK(masq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* FIXME: Multiple targets. --RR */
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020034static bool masquerade_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020036 const struct nf_nat_multi_range_compat *mr = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
Patrick McHardy0d537782007-07-07 22:39:38 -070039 pr_debug("masquerade_check: bad MAP_IPS.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070040 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42 if (mr->rangesize != 1) {
Patrick McHardy0d537782007-07-07 22:39:38 -070043 pr_debug("masquerade_check: bad rangesize %u\n", mr->rangesize);
Jan Engelhardte1931b72007-07-07 22:16:26 -070044 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
Jan Engelhardte1931b72007-07-07 22:16:26 -070046 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020050masquerade_tg(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Patrick McHardy587aa6412007-03-14 16:37:25 -070052 struct nf_conn *ct;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080053 struct nf_conn_nat *nat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 enum ip_conntrack_info ctinfo;
Patrick McHardy587aa6412007-03-14 16:37:25 -070055 struct nf_nat_range newrange;
56 const struct nf_nat_multi_range_compat *mr;
Jan Engelhardta47362a2007-07-07 22:16:55 -070057 const struct rtable *rt;
Al Viroa61ced52006-09-26 21:27:54 -070058 __be32 newsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jan Engelhardt7eb35582008-10-08 11:35:19 +020060 NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Herbert Xu3db05fe2007-10-15 00:53:15 -070062 ct = nf_ct_get(skb, &ctinfo);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080063 nat = nfct_nat(ct);
Patrick McHardy587aa6412007-03-14 16:37:25 -070064
65 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090066 || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Patrick McHardyadcb5ad2005-09-13 13:49:15 -070068 /* Source address is 0.0.0.0 - locally generated packet that is
69 * probably not supposed to be masqueraded.
70 */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080071 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == 0)
Patrick McHardyadcb5ad2005-09-13 13:49:15 -070072 return NF_ACCEPT;
73
Jan Engelhardt7eb35582008-10-08 11:35:19 +020074 mr = par->targinfo;
Eric Dumazet511c3f92009-06-02 05:14:27 +000075 rt = skb_rtable(skb);
Jan Engelhardt7eb35582008-10-08 11:35:19 +020076 newsrc = inet_select_addr(par->out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!newsrc) {
Jan Engelhardt7eb35582008-10-08 11:35:19 +020078 printk("MASQUERADE: %s ate my IP address\n", par->out->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return NF_DROP;
80 }
81
Patrick McHardye45b1be2005-06-21 14:01:30 -070082 write_lock_bh(&masq_lock);
Jan Engelhardt7eb35582008-10-08 11:35:19 +020083 nat->masq_index = par->out->ifindex;
Patrick McHardye45b1be2005-06-21 14:01:30 -070084 write_unlock_bh(&masq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* Transfer from original range. */
Patrick McHardy587aa6412007-03-14 16:37:25 -070087 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
89 newsrc, newsrc,
90 mr->range[0].min, mr->range[0].max });
91
92 /* Hand modified range to generic setup. */
Patrick McHardycc01dcb2007-12-17 22:38:20 -080093 return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Jan Engelhardt170b1972007-07-07 22:17:36 -070096static int
Patrick McHardy587aa6412007-03-14 16:37:25 -070097device_cmp(struct nf_conn *i, void *ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Jan Engelhardta47362a2007-07-07 22:16:55 -070099 const struct nf_conn_nat *nat = nfct_nat(i);
Patrick McHardy587aa6412007-03-14 16:37:25 -0700100 int ret;
Martin Josefssonbbdc1762007-01-04 12:16:54 -0800101
102 if (!nat)
103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Patrick McHardye45b1be2005-06-21 14:01:30 -0700105 read_lock_bh(&masq_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800106 ret = (nat->masq_index == (int)(long)ifindex);
Patrick McHardye45b1be2005-06-21 14:01:30 -0700107 read_unlock_bh(&masq_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 return ret;
110}
111
112static int masq_device_event(struct notifier_block *this,
113 unsigned long event,
114 void *ptr)
115{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700116 const struct net_device *dev = ptr;
Alexey Dobriyanb8b80632008-10-08 11:35:10 +0200117 struct net *net = dev_net(dev);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (event == NETDEV_DOWN) {
120 /* Device was downed. Search entire table for
121 conntracks which were associated with that device,
122 and forget them. */
Patrick McHardy587aa6412007-03-14 16:37:25 -0700123 NF_CT_ASSERT(dev->ifindex != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Alexey Dobriyanb8b80632008-10-08 11:35:10 +0200125 nf_ct_iterate_cleanup(net, device_cmp,
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200126 (void *)(long)dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
129 return NOTIFY_DONE;
130}
131
132static int masq_inet_event(struct notifier_block *this,
133 unsigned long event,
134 void *ptr)
135{
Denis V. Lunev6fc68622008-02-28 20:45:41 -0800136 struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
137 return masq_device_event(this, event, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static struct notifier_block masq_dev_notifier = {
141 .notifier_call = masq_device_event,
142};
143
144static struct notifier_block masq_inet_notifier = {
145 .notifier_call = masq_inet_event,
146};
147
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800148static struct xt_target masquerade_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .name = "MASQUERADE",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200150 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800151 .target = masquerade_tg,
Patrick McHardy587aa6412007-03-14 16:37:25 -0700152 .targetsize = sizeof(struct nf_nat_multi_range_compat),
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800153 .table = "nat",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800154 .hooks = 1 << NF_INET_POST_ROUTING,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800155 .checkentry = masquerade_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .me = THIS_MODULE,
157};
158
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800159static int __init masquerade_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int ret;
162
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800163 ret = xt_register_target(&masquerade_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (ret == 0) {
166 /* Register for device down reports */
167 register_netdevice_notifier(&masq_dev_notifier);
168 /* Register IP address change reports */
169 register_inetaddr_notifier(&masq_inet_notifier);
170 }
171
172 return ret;
173}
174
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800175static void __exit masquerade_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800177 xt_unregister_target(&masquerade_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unregister_netdevice_notifier(&masq_dev_notifier);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900179 unregister_inetaddr_notifier(&masq_inet_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800182module_init(masquerade_tg_init);
183module_exit(masquerade_tg_exit);