![Thierry Du Tre](/assets/img/avatar_default.png)
This is a patch proposal to support shifted ranges in portmaps. (i.e. tcp/udp incoming port 5000-5100 on WAN redirected to LAN 192.168.1.5:2000-2100) Currently DNAT only works for single port or identical port ranges. (i.e. ports 5000-5100 on WAN interface redirected to a LAN host while original destination port is not altered) When different port ranges are configured, either 'random' mode should be used, or else all incoming connections are mapped onto the first port in the redirect range. (in described example WAN:5000-5100 will all be mapped to 192.168.1.5:2000) This patch introduces a new mode indicated by flag NF_NAT_RANGE_PROTO_OFFSET which uses a base port value to calculate an offset with the destination port present in the incoming stream. That offset is then applied as index within the redirect port range (index modulo rangewidth to handle range overflow). In described example the base port would be 5000. An incoming stream with destination port 5004 would result in an offset value 4 which means that the NAT'ed stream will be using destination port 2004. Other possibilities include deterministic mapping of larger or multiple ranges to a smaller range : WAN:5000-5999 -> LAN:5000-5099 (maps WAN port 5*xx to port 51xx) This patch does not change any current behavior. It just adds new NAT proto range functionality which must be selected via the specific flag when intended to use. A patch for iptables (libipt_DNAT.c + libip6t_DNAT.c) will also be proposed which makes this functionality immediately available. Signed-off-by: Thierry Du Tre <thierry@dtsystems.be> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
130 lines
3.4 KiB
C
130 lines
3.4 KiB
C
/*
|
|
* (C) 1999-2001 Paul `Rusty' Russell
|
|
* (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
|
|
* Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
|
|
*
|
|
* 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.
|
|
*
|
|
* 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/module.h>
|
|
#include <linux/netdevice.h>
|
|
#include <linux/netfilter.h>
|
|
#include <linux/types.h>
|
|
#include <linux/netfilter_ipv4.h>
|
|
#include <linux/netfilter_ipv6.h>
|
|
#include <linux/netfilter/x_tables.h>
|
|
#include <net/addrconf.h>
|
|
#include <net/checksum.h>
|
|
#include <net/protocol.h>
|
|
#include <net/netfilter/nf_nat.h>
|
|
#include <net/netfilter/nf_nat_redirect.h>
|
|
|
|
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;
|
|
struct nf_nat_range2 newrange;
|
|
|
|
WARN_ON(hooknum != NF_INET_PRE_ROUTING &&
|
|
hooknum != NF_INET_LOCAL_OUT);
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
|
WARN_ON(!(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)));
|
|
|
|
/* Local packets: make them go to loopback */
|
|
if (hooknum == NF_INET_LOCAL_OUT) {
|
|
newdst = htonl(0x7F000001);
|
|
} else {
|
|
struct in_device *indev;
|
|
struct in_ifaddr *ifa;
|
|
|
|
newdst = 0;
|
|
|
|
rcu_read_lock();
|
|
indev = __in_dev_get_rcu(skb->dev);
|
|
if (indev && indev->ifa_list) {
|
|
ifa = indev->ifa_list;
|
|
newdst = ifa->ifa_local;
|
|
}
|
|
rcu_read_unlock();
|
|
|
|
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);
|
|
|
|
static const struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
|
|
|
|
unsigned int
|
|
nf_nat_redirect_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,
|
|
unsigned int hooknum)
|
|
{
|
|
struct nf_nat_range2 newrange;
|
|
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;
|
|
|
|
rcu_read_lock();
|
|
idev = __in6_dev_get(skb->dev);
|
|
if (idev != NULL) {
|
|
read_lock_bh(&idev->lock);
|
|
list_for_each_entry(ifa, &idev->addr_list, if_list) {
|
|
newdst = ifa->addr;
|
|
addr = true;
|
|
break;
|
|
}
|
|
read_unlock_bh(&idev->lock);
|
|
}
|
|
rcu_read_unlock();
|
|
|
|
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);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
|