blob: 5a182f6de5d505704028aa535a7b48f4c8897515 [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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/* FIXME: Multiple targets. --RR */
Jan Engelhardt135367b2010-03-19 17:16:42 +010031static int masquerade_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020033 const struct nf_nat_multi_range_compat *mr = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010036 pr_debug("bad MAP_IPS.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070037 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39 if (mr->rangesize != 1) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010040 pr_debug("bad rangesize %u\n", mr->rangesize);
Jan Engelhardte1931b72007-07-07 22:16:26 -070041 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
Jan Engelhardte1931b72007-07-07 22:16:26 -070043 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020047masquerade_tg(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Patrick McHardy587aa6412007-03-14 16:37:25 -070049 struct nf_conn *ct;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080050 struct nf_conn_nat *nat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 enum ip_conntrack_info ctinfo;
Patrick McHardy587aa6412007-03-14 16:37:25 -070052 struct nf_nat_range newrange;
53 const struct nf_nat_multi_range_compat *mr;
Jan Engelhardta47362a2007-07-07 22:16:55 -070054 const struct rtable *rt;
Al Viroa61ced52006-09-26 21:27:54 -070055 __be32 newsrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jan Engelhardt7eb35582008-10-08 11:35:19 +020057 NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Herbert Xu3db05fe2007-10-15 00:53:15 -070059 ct = nf_ct_get(skb, &ctinfo);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080060 nat = nfct_nat(ct);
Patrick McHardy587aa6412007-03-14 16:37:25 -070061
Joe Perches3666ed12009-11-23 23:17:06 +010062 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
63 ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Patrick McHardyadcb5ad2005-09-13 13:49:15 -070065 /* Source address is 0.0.0.0 - locally generated packet that is
66 * probably not supposed to be masqueraded.
67 */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080068 if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == 0)
Patrick McHardyadcb5ad2005-09-13 13:49:15 -070069 return NF_ACCEPT;
70
Jan Engelhardt7eb35582008-10-08 11:35:19 +020071 mr = par->targinfo;
Eric Dumazet511c3f92009-06-02 05:14:27 +000072 rt = skb_rtable(skb);
Jan Engelhardt7eb35582008-10-08 11:35:19 +020073 newsrc = inet_select_addr(par->out, rt->rt_gateway, RT_SCOPE_UNIVERSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (!newsrc) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010075 pr_info("%s ate my IP address\n", par->out->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return NF_DROP;
77 }
78
Jan Engelhardt7eb35582008-10-08 11:35:19 +020079 nat->masq_index = par->out->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* Transfer from original range. */
Patrick McHardy587aa6412007-03-14 16:37:25 -070082 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 { 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. */
Patrick McHardycc01dcb2007-12-17 22:38:20 -080088 return nf_nat_setup_info(ct, &newrange, IP_NAT_MANIP_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Jan Engelhardt170b1972007-07-07 22:17:36 -070091static int
Patrick McHardy587aa6412007-03-14 16:37:25 -070092device_cmp(struct nf_conn *i, void *ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Jan Engelhardta47362a2007-07-07 22:16:55 -070094 const struct nf_conn_nat *nat = nfct_nat(i);
Martin Josefssonbbdc1762007-01-04 12:16:54 -080095
96 if (!nat)
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Florian Westphal17f2f522009-06-05 13:26:21 +020099 return nat->masq_index == (int)(long)ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static int masq_device_event(struct notifier_block *this,
103 unsigned long event,
104 void *ptr)
105{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700106 const struct net_device *dev = ptr;
Alexey Dobriyanb8b80632008-10-08 11:35:10 +0200107 struct net *net = dev_net(dev);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (event == NETDEV_DOWN) {
110 /* Device was downed. Search entire table for
111 conntracks which were associated with that device,
112 and forget them. */
Patrick McHardy587aa6412007-03-14 16:37:25 -0700113 NF_CT_ASSERT(dev->ifindex != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Alexey Dobriyanb8b80632008-10-08 11:35:10 +0200115 nf_ct_iterate_cleanup(net, device_cmp,
Alexey Dobriyan400dad32008-10-08 11:35:03 +0200116 (void *)(long)dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
119 return NOTIFY_DONE;
120}
121
122static int masq_inet_event(struct notifier_block *this,
123 unsigned long event,
124 void *ptr)
125{
Denis V. Lunev6fc68622008-02-28 20:45:41 -0800126 struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
127 return masq_device_event(this, event, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static struct notifier_block masq_dev_notifier = {
131 .notifier_call = masq_device_event,
132};
133
134static struct notifier_block masq_inet_notifier = {
135 .notifier_call = masq_inet_event,
136};
137
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800138static struct xt_target masquerade_tg_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .name = "MASQUERADE",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200140 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800141 .target = masquerade_tg,
Patrick McHardy587aa6412007-03-14 16:37:25 -0700142 .targetsize = sizeof(struct nf_nat_multi_range_compat),
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800143 .table = "nat",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800144 .hooks = 1 << NF_INET_POST_ROUTING,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800145 .checkentry = masquerade_tg_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .me = THIS_MODULE,
147};
148
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800149static int __init masquerade_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 int ret;
152
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800153 ret = xt_register_target(&masquerade_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
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
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800165static void __exit masquerade_tg_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800167 xt_unregister_target(&masquerade_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unregister_netdevice_notifier(&masq_dev_notifier);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900169 unregister_inetaddr_notifier(&masq_inet_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800172module_init(masquerade_tg_init);
173module_exit(masquerade_tg_exit);