2005-04-17 00:20:36 +02:00
|
|
|
/* Masquerade. Simple mapping which alters range to a local IP address
|
|
|
|
(depending on route). */
|
|
|
|
|
|
|
|
/* (C) 1999-2001 Paul `Rusty' Russell
|
2006-12-03 07:07:13 +01:00
|
|
|
* (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
|
2005-04-17 00:20:36 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
2010-03-19 21:08:16 +01:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/types.h>
|
2005-12-27 05:43:12 +01:00
|
|
|
#include <linux/inetdevice.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <net/ip.h>
|
|
|
|
#include <net/checksum.h>
|
2005-12-27 05:43:12 +01:00
|
|
|
#include <net/route.h>
|
2007-03-15 00:37:25 +01:00
|
|
|
#include <linux/netfilter_ipv4.h>
|
2007-02-08 00:11:19 +01:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2012-08-26 19:14:06 +02:00
|
|
|
#include <net/netfilter/nf_nat.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
|
2008-01-15 08:42:28 +01:00
|
|
|
MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* FIXME: Multiple targets. --RR */
|
2010-03-19 17:16:42 +01:00
|
|
|
static int masquerade_tg_check(const struct xt_tgchk_param *par)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2011-12-23 13:59:49 +01:00
|
|
|
const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2011-12-23 13:59:49 +01:00
|
|
|
if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
|
2010-03-19 21:08:16 +01:00
|
|
|
pr_debug("bad MAP_IPS.\n");
|
2010-03-25 16:34:45 +01:00
|
|
|
return -EINVAL;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
if (mr->rangesize != 1) {
|
2010-03-19 21:08:16 +01:00
|
|
|
pr_debug("bad rangesize %u\n", mr->rangesize);
|
2010-03-25 16:34:45 +01:00
|
|
|
return -EINVAL;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
2010-03-25 16:34:45 +01:00
|
|
|
return 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2009-07-05 19:43:26 +02:00
|
|
|
masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-03-15 00:37:25 +01:00
|
|
|
struct nf_conn *ct;
|
2006-12-03 07:07:13 +01:00
|
|
|
struct nf_conn_nat *nat;
|
2005-04-17 00:20:36 +02:00
|
|
|
enum ip_conntrack_info ctinfo;
|
2012-08-26 19:14:06 +02:00
|
|
|
struct nf_nat_range newrange;
|
2011-12-23 13:59:49 +01:00
|
|
|
const struct nf_nat_ipv4_multi_range_compat *mr;
|
2007-07-08 07:16:55 +02:00
|
|
|
const struct rtable *rt;
|
2012-07-13 14:03:45 +02:00
|
|
|
__be32 newsrc, nh;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-10-08 11:35:19 +02:00
|
|
|
NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2007-10-15 09:53:15 +02:00
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
2006-12-03 07:07:13 +01:00
|
|
|
nat = nfct_nat(ct);
|
2007-03-15 00:37:25 +01:00
|
|
|
|
2009-11-23 23:17:06 +01:00
|
|
|
NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
|
2011-05-19 15:44:27 +02:00
|
|
|
ctinfo == IP_CT_RELATED_REPLY));
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-09-13 22:49:15 +02:00
|
|
|
/* Source address is 0.0.0.0 - locally generated packet that is
|
|
|
|
* probably not supposed to be masqueraded.
|
|
|
|
*/
|
2006-12-03 07:07:13 +01:00
|
|
|
if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip == 0)
|
2005-09-13 22:49:15 +02:00
|
|
|
return NF_ACCEPT;
|
|
|
|
|
2008-10-08 11:35:19 +02:00
|
|
|
mr = par->targinfo;
|
2009-06-02 07:14:27 +02:00
|
|
|
rt = skb_rtable(skb);
|
2012-07-13 14:03:45 +02:00
|
|
|
nh = rt_nexthop(rt, ip_hdr(skb)->daddr);
|
|
|
|
newsrc = inet_select_addr(par->out, nh, RT_SCOPE_UNIVERSE);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!newsrc) {
|
2010-03-19 21:08:16 +01:00
|
|
|
pr_info("%s ate my IP address\n", par->out->name);
|
2005-04-17 00:20:36 +02:00
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:35:19 +02:00
|
|
|
nat->masq_index = par->out->ifindex;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Transfer from original range. */
|
2012-08-26 19:14:06 +02:00
|
|
|
memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
|
|
|
|
memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
|
|
|
|
newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS;
|
|
|
|
newrange.min_addr.ip = newsrc;
|
|
|
|
newrange.max_addr.ip = newsrc;
|
|
|
|
newrange.min_proto = mr->range[0].min;
|
|
|
|
newrange.max_proto = mr->range[0].max;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Hand modified range to generic setup. */
|
2011-12-23 13:59:49 +01:00
|
|
|
return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_SRC);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2007-07-08 07:17:36 +02:00
|
|
|
static int
|
2007-03-15 00:37:25 +01:00
|
|
|
device_cmp(struct nf_conn *i, void *ifindex)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-07-08 07:16:55 +02:00
|
|
|
const struct nf_conn_nat *nat = nfct_nat(i);
|
2007-01-04 21:16:54 +01:00
|
|
|
|
|
|
|
if (!nat)
|
|
|
|
return 0;
|
2012-08-26 19:14:14 +02:00
|
|
|
if (nf_ct_l3num(i) != NFPROTO_IPV4)
|
|
|
|
return 0;
|
2009-06-05 13:26:21 +02:00
|
|
|
return nat->masq_index == (int)(long)ifindex;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int masq_device_event(struct notifier_block *this,
|
|
|
|
unsigned long event,
|
|
|
|
void *ptr)
|
|
|
|
{
|
2013-05-28 03:30:21 +02:00
|
|
|
const struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
2008-10-08 11:35:10 +02:00
|
|
|
struct net *net = dev_net(dev);
|
2007-09-12 13:02:17 +02:00
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
if (event == NETDEV_DOWN) {
|
|
|
|
/* Device was downed. Search entire table for
|
|
|
|
conntracks which were associated with that device,
|
|
|
|
and forget them. */
|
2007-03-15 00:37:25 +01:00
|
|
|
NF_CT_ASSERT(dev->ifindex != 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-10-08 11:35:10 +02:00
|
|
|
nf_ct_iterate_cleanup(net, device_cmp,
|
2013-07-29 15:41:55 +02:00
|
|
|
(void *)(long)dev->ifindex, 0, 0);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int masq_inet_event(struct notifier_block *this,
|
|
|
|
unsigned long event,
|
|
|
|
void *ptr)
|
|
|
|
{
|
2008-02-29 05:45:41 +01:00
|
|
|
struct net_device *dev = ((struct in_ifaddr *)ptr)->ifa_dev->dev;
|
2013-05-29 05:30:50 +02:00
|
|
|
struct netdev_notifier_info info;
|
|
|
|
|
|
|
|
netdev_notifier_info_init(&info, dev);
|
|
|
|
return masq_device_event(this, event, &info);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block masq_dev_notifier = {
|
|
|
|
.notifier_call = masq_device_event,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct notifier_block masq_inet_notifier = {
|
|
|
|
.notifier_call = masq_inet_event,
|
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static struct xt_target masquerade_tg_reg __read_mostly = {
|
2005-04-17 00:20:36 +02:00
|
|
|
.name = "MASQUERADE",
|
2008-10-08 11:35:01 +02:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 08:24:03 +01:00
|
|
|
.target = masquerade_tg,
|
2011-12-23 13:59:49 +01:00
|
|
|
.targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
|
2006-03-21 03:01:14 +01:00
|
|
|
.table = "nat",
|
2007-11-20 03:53:30 +01:00
|
|
|
.hooks = 1 << NF_INET_POST_ROUTING,
|
2007-12-05 08:24:03 +01:00
|
|
|
.checkentry = masquerade_tg_check,
|
2005-04-17 00:20:36 +02:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static int __init masquerade_tg_init(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
ret = xt_register_target(&masquerade_tg_reg);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
/* Register for device down reports */
|
|
|
|
register_netdevice_notifier(&masq_dev_notifier);
|
|
|
|
/* Register IP address change reports */
|
|
|
|
register_inetaddr_notifier(&masq_inet_notifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static void __exit masquerade_tg_exit(void)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2007-12-05 08:24:03 +01:00
|
|
|
xt_unregister_target(&masquerade_tg_reg);
|
2005-04-17 00:20:36 +02:00
|
|
|
unregister_netdevice_notifier(&masq_dev_notifier);
|
2007-02-09 15:24:47 +01:00
|
|
|
unregister_inetaddr_notifier(&masq_inet_notifier);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
module_init(masquerade_tg_init);
|
|
|
|
module_exit(masquerade_tg_exit);
|