2019-06-04 02:11:33 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 16:20:36 -06:00
|
|
|
/* Masquerade. Simple mapping which alters range to a local IP address
|
|
|
|
(depending on route). */
|
|
|
|
|
|
|
|
/* (C) 1999-2001 Paul `Rusty' Russell
|
2006-12-02 23:07:13 -07:00
|
|
|
* (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
|
2005-04-16 16:20:36 -06:00
|
|
|
*/
|
2010-03-19 14:08:16 -06:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2005-04-16 16:20:36 -06:00
|
|
|
#include <linux/module.h>
|
2007-02-07 16:11:19 -07:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2012-08-26 11:14:06 -06:00
|
|
|
#include <net/netfilter/nf_nat.h>
|
2019-04-09 02:44:06 -06:00
|
|
|
#include <net/netfilter/nf_nat_masquerade.h>
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
|
2008-01-15 00:42:28 -07:00
|
|
|
MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
/* FIXME: Multiple targets. --RR */
|
2010-03-19 10:16:42 -06:00
|
|
|
static int masquerade_tg_check(const struct xt_tgchk_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2011-12-23 05:59:49 -07:00
|
|
|
const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2011-12-23 05:59:49 -07:00
|
|
|
if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("bad MAP_IPS.\n");
|
2010-03-25 09:34:45 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
if (mr->rangesize != 1) {
|
2010-03-19 14:08:16 -06:00
|
|
|
pr_debug("bad rangesize %u\n", mr->rangesize);
|
2010-03-25 09:34:45 -06:00
|
|
|
return -EINVAL;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
2016-11-15 13:36:41 -07:00
|
|
|
return nf_ct_netns_get(par->net, par->family);
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2009-07-05 11:43:26 -06:00
|
|
|
masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2018-04-04 07:38:22 -06:00
|
|
|
struct nf_nat_range2 range;
|
2011-12-23 05:59:49 -07:00
|
|
|
const struct nf_nat_ipv4_multi_range_compat *mr;
|
2005-09-13 14:49:15 -06:00
|
|
|
|
2008-10-08 03:35:19 -06:00
|
|
|
mr = par->targinfo;
|
2014-09-04 06:06:33 -06:00
|
|
|
range.flags = mr->range[0].flags;
|
|
|
|
range.min_proto = mr->range[0].min;
|
|
|
|
range.max_proto = mr->range[0].max;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2016-11-03 03:56:21 -06:00
|
|
|
return nf_nat_masquerade_ipv4(skb, xt_hooknum(par), &range,
|
|
|
|
xt_out(par));
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2016-11-15 13:36:41 -07:00
|
|
|
static void masquerade_tg_destroy(const struct xt_tgdtor_param *par)
|
|
|
|
{
|
|
|
|
nf_ct_netns_put(par->net, par->family);
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:44:07 -06:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
|
|
static unsigned int
|
|
|
|
masquerade_tg6(struct sk_buff *skb, const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
return nf_nat_masquerade_ipv6(skb, par->targinfo, xt_out(par));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int masquerade_tg6_checkentry(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
const struct nf_nat_range2 *range = par->targinfo;
|
|
|
|
|
|
|
|
if (range->flags & NF_NAT_RANGE_MAP_IPS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return nf_ct_netns_get(par->net, par->family);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct xt_target masquerade_tg_reg[] __read_mostly = {
|
|
|
|
{
|
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
|
|
.name = "MASQUERADE",
|
|
|
|
.family = NFPROTO_IPV6,
|
|
|
|
.target = masquerade_tg6,
|
|
|
|
.targetsize = sizeof(struct nf_nat_range),
|
|
|
|
.table = "nat",
|
|
|
|
.hooks = 1 << NF_INET_POST_ROUTING,
|
|
|
|
.checkentry = masquerade_tg6_checkentry,
|
|
|
|
.destroy = masquerade_tg_destroy,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
}, {
|
|
|
|
#endif
|
|
|
|
.name = "MASQUERADE",
|
|
|
|
.family = NFPROTO_IPV4,
|
|
|
|
.target = masquerade_tg,
|
|
|
|
.targetsize = sizeof(struct nf_nat_ipv4_multi_range_compat),
|
|
|
|
.table = "nat",
|
|
|
|
.hooks = 1 << NF_INET_POST_ROUTING,
|
|
|
|
.checkentry = masquerade_tg_check,
|
|
|
|
.destroy = masquerade_tg_destroy,
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
};
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static int __init masquerade_tg_init(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2019-04-09 02:44:07 -06:00
|
|
|
ret = xt_register_targets(masquerade_tg_reg,
|
|
|
|
ARRAY_SIZE(masquerade_tg_reg));
|
2018-11-22 03:59:46 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2019-04-09 02:44:08 -06:00
|
|
|
ret = nf_nat_masquerade_inet_register_notifiers();
|
2019-04-09 02:44:07 -06:00
|
|
|
if (ret) {
|
|
|
|
xt_unregister_targets(masquerade_tg_reg,
|
|
|
|
ARRAY_SIZE(masquerade_tg_reg));
|
|
|
|
return ret;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
static void __exit masquerade_tg_exit(void)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2019-04-09 02:44:07 -06:00
|
|
|
xt_unregister_targets(masquerade_tg_reg, ARRAY_SIZE(masquerade_tg_reg));
|
2019-04-09 02:44:08 -06:00
|
|
|
nf_nat_masquerade_inet_unregister_notifiers();
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 00:24:03 -07:00
|
|
|
module_init(masquerade_tg_init);
|
|
|
|
module_exit(masquerade_tg_exit);
|
2019-04-09 02:44:07 -06:00
|
|
|
#if IS_ENABLED(CONFIG_IPV6)
|
|
|
|
MODULE_ALIAS("ip6t_MASQUERADE");
|
|
|
|
#endif
|
|
|
|
MODULE_ALIAS("ipt_MASQUERADE");
|