aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4
diff options
context:
space:
mode:
authorArturo Borrero <arturo.borrero.glez@gmail.com>2014-10-16 06:23:29 -0400
committerPablo Neira Ayuso <pablo@netfilter.org>2014-10-27 17:47:06 -0400
commit8b13eddfdf04cbfa561725cfc42d6868fe896f56 (patch)
treea54fcd289d9acb1566a841f66045127227c84d25 /net/ipv4
parentb8901ac319768cdd3afa060787503e0c405f9607 (diff)
netfilter: refactor NAT redirect IPv4 to use it from nf_tables
This patch refactors the IPv4 code so it can be usable both from xt and nf_tables. A similar patch follows-up to handle IPv6. Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'net/ipv4')
-rw-r--r--net/ipv4/netfilter/Kconfig6
-rw-r--r--net/ipv4/netfilter/Makefile1
-rw-r--r--net/ipv4/netfilter/nf_nat_redirect_ipv4.c82
3 files changed, 89 insertions, 0 deletions
diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
index 4c019d5c3f57..a300e2c32b26 100644
--- a/net/ipv4/netfilter/Kconfig
+++ b/net/ipv4/netfilter/Kconfig
@@ -104,6 +104,12 @@ config NF_NAT_MASQUERADE_IPV4
104 This is the kernel functionality to provide NAT in the masquerade 104 This is the kernel functionality to provide NAT in the masquerade
105 flavour (automatic source address selection). 105 flavour (automatic source address selection).
106 106
107config NF_NAT_REDIRECT_IPV4
108 tristate "IPv4 redirect support"
109 help
110 This is the kernel functionality to provide NAT in the redirect
111 flavour (redirect packets to local machine).
112
107config NFT_MASQ_IPV4 113config NFT_MASQ_IPV4
108 tristate "IPv4 masquerading support for nf_tables" 114 tristate "IPv4 masquerading support for nf_tables"
109 depends on NF_TABLES_IPV4 115 depends on NF_TABLES_IPV4
diff --git a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile
index f4cef5af0969..34e436c92015 100644
--- a/net/ipv4/netfilter/Makefile
+++ b/net/ipv4/netfilter/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o
31obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o 31obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o
32obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o 32obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o
33obj-$(CONFIG_NF_NAT_MASQUERADE_IPV4) += nf_nat_masquerade_ipv4.o 33obj-$(CONFIG_NF_NAT_MASQUERADE_IPV4) += nf_nat_masquerade_ipv4.o
34obj-$(CONFIG_NF_NAT_REDIRECT_IPV4) += nf_nat_redirect_ipv4.o
34 35
35# NAT protocols (nf_nat) 36# NAT protocols (nf_nat)
36obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o 37obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
diff --git a/net/ipv4/netfilter/nf_nat_redirect_ipv4.c b/net/ipv4/netfilter/nf_nat_redirect_ipv4.c
new file mode 100644
index 000000000000..a220552fc532
--- /dev/null
+++ b/net/ipv4/netfilter/nf_nat_redirect_ipv4.c
@@ -0,0 +1,82 @@
1/*
2 * (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
11 * NAT funded by Astaro.
12 */
13
14#include <linux/if.h>
15#include <linux/inetdevice.h>
16#include <linux/ip.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/netdevice.h>
20#include <linux/netfilter.h>
21#include <linux/types.h>
22#include <linux/netfilter_ipv4.h>
23#include <linux/netfilter/x_tables.h>
24#include <net/addrconf.h>
25#include <net/checksum.h>
26#include <net/protocol.h>
27#include <net/netfilter/nf_nat.h>
28#include <net/netfilter/ipv4/nf_nat_redirect.h>
29
30unsigned int
31nf_nat_redirect_ipv4(struct sk_buff *skb,
32 const struct nf_nat_ipv4_multi_range_compat *mr,
33 unsigned int hooknum)
34{
35 struct nf_conn *ct;
36 enum ip_conntrack_info ctinfo;
37 __be32 newdst;
38 struct nf_nat_range newrange;
39
40 NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING ||
41 hooknum == NF_INET_LOCAL_OUT);
42
43 ct = nf_ct_get(skb, &ctinfo);
44 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
45
46 /* Local packets: make them go to loopback */
47 if (hooknum == NF_INET_LOCAL_OUT) {
48 newdst = htonl(0x7F000001);
49 } else {
50 struct in_device *indev;
51 struct in_ifaddr *ifa;
52
53 newdst = 0;
54
55 rcu_read_lock();
56 indev = __in_dev_get_rcu(skb->dev);
57 if (indev != NULL) {
58 ifa = indev->ifa_list;
59 newdst = ifa->ifa_local;
60 }
61 rcu_read_unlock();
62
63 if (!newdst)
64 return NF_DROP;
65 }
66
67 /* Transfer from original range. */
68 memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
69 memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
70 newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS;
71 newrange.min_addr.ip = newdst;
72 newrange.max_addr.ip = newdst;
73 newrange.min_proto = mr->range[0].min;
74 newrange.max_proto = mr->range[0].max;
75
76 /* Hand modified range to generic setup. */
77 return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
78}
79EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4);
80
81MODULE_LICENSE("GPL");
82MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");