2019-06-04 02:11:33 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2014-10-16 04:23:29 -06:00
|
|
|
/*
|
|
|
|
* (C) 1999-2001 Paul `Rusty' Russell
|
|
|
|
* (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
* Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
|
|
|
|
*
|
|
|
|
* Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
|
|
|
|
* NAT funded by Astaro.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/inetdevice.h>
|
|
|
|
#include <linux/ip.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/netfilter_ipv4.h>
|
2014-11-26 04:46:50 -07:00
|
|
|
#include <linux/netfilter_ipv6.h>
|
2014-10-16 04:23:29 -06:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <net/addrconf.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <net/netfilter/nf_nat.h>
|
2014-11-26 04:46:50 -07:00
|
|
|
#include <net/netfilter/nf_nat_redirect.h>
|
2014-10-16 04:23:29 -06:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
nf_nat_redirect_ipv4(struct sk_buff *skb,
|
|
|
|
const struct nf_nat_ipv4_multi_range_compat *mr,
|
|
|
|
unsigned int hooknum)
|
|
|
|
{
|
|
|
|
struct nf_conn *ct;
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
__be32 newdst;
|
2018-04-04 07:38:22 -06:00
|
|
|
struct nf_nat_range2 newrange;
|
2014-10-16 04:23:29 -06:00
|
|
|
|
2017-08-30 02:07:11 -06:00
|
|
|
WARN_ON(hooknum != NF_INET_PRE_ROUTING &&
|
|
|
|
hooknum != NF_INET_LOCAL_OUT);
|
2014-10-16 04:23:29 -06:00
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
2017-08-30 02:07:11 -06:00
|
|
|
WARN_ON(!(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)));
|
2014-10-16 04:23:29 -06:00
|
|
|
|
|
|
|
/* Local packets: make them go to loopback */
|
|
|
|
if (hooknum == NF_INET_LOCAL_OUT) {
|
|
|
|
newdst = htonl(0x7F000001);
|
|
|
|
} else {
|
2019-05-31 10:27:09 -06:00
|
|
|
const struct in_device *indev;
|
2014-10-16 04:23:29 -06:00
|
|
|
|
|
|
|
newdst = 0;
|
|
|
|
|
|
|
|
indev = __in_dev_get_rcu(skb->dev);
|
2019-05-31 10:27:09 -06:00
|
|
|
if (indev) {
|
|
|
|
const struct in_ifaddr *ifa;
|
|
|
|
|
|
|
|
ifa = rcu_dereference(indev->ifa_list);
|
|
|
|
if (ifa)
|
|
|
|
newdst = ifa->ifa_local;
|
2014-10-16 04:23:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!newdst)
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer from original range. */
|
|
|
|
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 = newdst;
|
|
|
|
newrange.max_addr.ip = newdst;
|
|
|
|
newrange.min_proto = mr->range[0].min;
|
|
|
|
newrange.max_proto = mr->range[0].max;
|
|
|
|
|
|
|
|
/* Hand modified range to generic setup. */
|
|
|
|
return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4);
|
|
|
|
|
2014-11-26 04:46:50 -07:00
|
|
|
static const struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
|
|
|
|
|
|
|
|
unsigned int
|
2018-04-04 07:38:22 -06:00
|
|
|
nf_nat_redirect_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,
|
2014-11-26 04:46:50 -07:00
|
|
|
unsigned int hooknum)
|
|
|
|
{
|
2018-04-04 07:38:22 -06:00
|
|
|
struct nf_nat_range2 newrange;
|
2014-11-26 04:46:50 -07:00
|
|
|
struct in6_addr newdst;
|
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
struct nf_conn *ct;
|
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
|
|
if (hooknum == NF_INET_LOCAL_OUT) {
|
|
|
|
newdst = loopback_addr;
|
|
|
|
} else {
|
|
|
|
struct inet6_dev *idev;
|
|
|
|
struct inet6_ifaddr *ifa;
|
|
|
|
bool addr = false;
|
|
|
|
|
|
|
|
idev = __in6_dev_get(skb->dev);
|
|
|
|
if (idev != NULL) {
|
2017-04-02 03:27:53 -06:00
|
|
|
read_lock_bh(&idev->lock);
|
2014-11-26 04:46:50 -07:00
|
|
|
list_for_each_entry(ifa, &idev->addr_list, if_list) {
|
|
|
|
newdst = ifa->addr;
|
|
|
|
addr = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-02 03:27:53 -06:00
|
|
|
read_unlock_bh(&idev->lock);
|
2014-11-26 04:46:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
return NF_DROP;
|
|
|
|
}
|
|
|
|
|
|
|
|
newrange.flags = range->flags | NF_NAT_RANGE_MAP_IPS;
|
|
|
|
newrange.min_addr.in6 = newdst;
|
|
|
|
newrange.max_addr.in6 = newdst;
|
|
|
|
newrange.min_proto = range->min_proto;
|
|
|
|
newrange.max_proto = range->max_proto;
|
|
|
|
|
|
|
|
return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv6);
|