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>
|
2014-09-04 14:06:33 +02:00
|
|
|
#include <net/netfilter/ipv4/nf_nat_masquerade.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
|
|
|
}
|
2016-11-15 21:36:41 +01:00
|
|
|
return nf_ct_netns_get(par->net, par->family);
|
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
|
|
|
{
|
2014-09-04 14:06:33 +02:00
|
|
|
struct nf_nat_range range;
|
2011-12-23 13:59:49 +01:00
|
|
|
const struct nf_nat_ipv4_multi_range_compat *mr;
|
2005-09-13 22:49:15 +02:00
|
|
|
|
2008-10-08 11:35:19 +02:00
|
|
|
mr = par->targinfo;
|
2014-09-04 14:06:33 +02:00
|
|
|
range.flags = mr->range[0].flags;
|
|
|
|
range.min_proto = mr->range[0].min;
|
|
|
|
range.max_proto = mr->range[0].max;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2016-11-03 10:56:21 +01:00
|
|
|
return nf_nat_masquerade_ipv4(skb, xt_hooknum(par), &range,
|
|
|
|
xt_out(par));
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 21:36:41 +01:00
|
|
|
static void masquerade_tg_destroy(const struct xt_tgdtor_param *par)
|
|
|
|
{
|
|
|
|
nf_ct_netns_put(par->net, par->family);
|
|
|
|
}
|
|
|
|
|
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,
|
2016-11-15 21:36:41 +01:00
|
|
|
.destroy = masquerade_tg_destroy,
|
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
|
|
|
|
2014-09-04 14:06:33 +02:00
|
|
|
if (ret == 0)
|
|
|
|
nf_nat_masquerade_ipv4_register_notifier();
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
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);
|
2014-09-04 14:06:33 +02:00
|
|
|
nf_nat_masquerade_ipv4_unregister_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);
|