diff options
| author | Pablo Neira Ayuso <pablo@netfilter.org> | 2014-11-26 06:46:50 -0500 |
|---|---|---|
| committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2014-11-27 07:08:42 -0500 |
| commit | b59eaf9e2871735ea7cc7e3dbf8bf83bddd786b9 (patch) | |
| tree | e566e49ab6f1736c5e5ce600ca5c9741f6c694a0 /net/netfilter | |
| parent | 1b63d4b9b54cee6002757a8d20b537aa4037ae8f (diff) | |
netfilter: combine IPv4 and IPv6 nf_nat_redirect code in one module
This resolves linking problems with CONFIG_IPV6=n:
net/built-in.o: In function `redirect_tg6':
xt_REDIRECT.c:(.text+0x6d021): undefined reference to `nf_nat_redirect_ipv6'
Reported-by: Andreas Ruprecht <rupran@einserver.de>
Reported-by: Or Gerlitz <ogerlitz@mellanox.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'net/netfilter')
| -rw-r--r-- | net/netfilter/Kconfig | 10 | ||||
| -rw-r--r-- | net/netfilter/Makefile | 1 | ||||
| -rw-r--r-- | net/netfilter/nf_nat_redirect.c | 127 | ||||
| -rw-r--r-- | net/netfilter/xt_REDIRECT.c | 3 |
4 files changed, 137 insertions, 4 deletions
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig index 57f15a9aa481..b02660fa9eb0 100644 --- a/net/netfilter/Kconfig +++ b/net/netfilter/Kconfig | |||
| @@ -411,6 +411,13 @@ config NF_NAT_TFTP | |||
| 411 | depends on NF_CONNTRACK && NF_NAT | 411 | depends on NF_CONNTRACK && NF_NAT |
| 412 | default NF_NAT && NF_CONNTRACK_TFTP | 412 | default NF_NAT && NF_CONNTRACK_TFTP |
| 413 | 413 | ||
| 414 | config NF_NAT_REDIRECT | ||
| 415 | tristate "IPv4/IPv6 redirect support" | ||
| 416 | depends on NF_NAT | ||
| 417 | help | ||
| 418 | This is the kernel functionality to redirect packets to local | ||
| 419 | machine through NAT. | ||
| 420 | |||
| 414 | config NETFILTER_SYNPROXY | 421 | config NETFILTER_SYNPROXY |
| 415 | tristate | 422 | tristate |
| 416 | 423 | ||
| @@ -844,8 +851,7 @@ config NETFILTER_XT_TARGET_RATEEST | |||
| 844 | config NETFILTER_XT_TARGET_REDIRECT | 851 | config NETFILTER_XT_TARGET_REDIRECT |
| 845 | tristate "REDIRECT target support" | 852 | tristate "REDIRECT target support" |
| 846 | depends on NF_NAT | 853 | depends on NF_NAT |
| 847 | select NF_NAT_REDIRECT_IPV4 if NF_NAT_IPV4 | 854 | select NF_NAT_REDIRECT |
| 848 | select NF_NAT_REDIRECT_IPV6 if NF_NAT_IPV6 | ||
| 849 | ---help--- | 855 | ---help--- |
| 850 | REDIRECT is a special case of NAT: all incoming connections are | 856 | REDIRECT is a special case of NAT: all incoming connections are |
| 851 | mapped onto the incoming interface's address, causing the packets to | 857 | mapped onto the incoming interface's address, causing the packets to |
diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile index f3eb4680f2ec..89f73a9e9874 100644 --- a/net/netfilter/Makefile +++ b/net/netfilter/Makefile | |||
| @@ -51,6 +51,7 @@ nf_nat-y := nf_nat_core.o nf_nat_proto_unknown.o nf_nat_proto_common.o \ | |||
| 51 | obj-$(CONFIG_NF_LOG_COMMON) += nf_log_common.o | 51 | obj-$(CONFIG_NF_LOG_COMMON) += nf_log_common.o |
| 52 | 52 | ||
| 53 | obj-$(CONFIG_NF_NAT) += nf_nat.o | 53 | obj-$(CONFIG_NF_NAT) += nf_nat.o |
| 54 | obj-$(CONFIG_NF_NAT_REDIRECT) += nf_nat_redirect.o | ||
| 54 | 55 | ||
| 55 | # NAT protocols (nf_nat) | 56 | # NAT protocols (nf_nat) |
| 56 | obj-$(CONFIG_NF_NAT_PROTO_DCCP) += nf_nat_proto_dccp.o | 57 | obj-$(CONFIG_NF_NAT_PROTO_DCCP) += nf_nat_proto_dccp.o |
diff --git a/net/netfilter/nf_nat_redirect.c b/net/netfilter/nf_nat_redirect.c new file mode 100644 index 000000000000..97b75f9bfbcd --- /dev/null +++ b/net/netfilter/nf_nat_redirect.c | |||
| @@ -0,0 +1,127 @@ | |||
| 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_ipv6.h> | ||
| 24 | #include <linux/netfilter/x_tables.h> | ||
| 25 | #include <net/addrconf.h> | ||
| 26 | #include <net/checksum.h> | ||
| 27 | #include <net/protocol.h> | ||
| 28 | #include <net/netfilter/nf_nat.h> | ||
| 29 | #include <net/netfilter/nf_nat_redirect.h> | ||
| 30 | |||
| 31 | unsigned int | ||
| 32 | nf_nat_redirect_ipv4(struct sk_buff *skb, | ||
| 33 | const struct nf_nat_ipv4_multi_range_compat *mr, | ||
| 34 | unsigned int hooknum) | ||
| 35 | { | ||
| 36 | struct nf_conn *ct; | ||
| 37 | enum ip_conntrack_info ctinfo; | ||
| 38 | __be32 newdst; | ||
| 39 | struct nf_nat_range newrange; | ||
| 40 | |||
| 41 | NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING || | ||
| 42 | hooknum == NF_INET_LOCAL_OUT); | ||
| 43 | |||
| 44 | ct = nf_ct_get(skb, &ctinfo); | ||
| 45 | NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)); | ||
| 46 | |||
| 47 | /* Local packets: make them go to loopback */ | ||
| 48 | if (hooknum == NF_INET_LOCAL_OUT) { | ||
| 49 | newdst = htonl(0x7F000001); | ||
| 50 | } else { | ||
| 51 | struct in_device *indev; | ||
| 52 | struct in_ifaddr *ifa; | ||
| 53 | |||
| 54 | newdst = 0; | ||
| 55 | |||
| 56 | rcu_read_lock(); | ||
| 57 | indev = __in_dev_get_rcu(skb->dev); | ||
| 58 | if (indev != NULL) { | ||
| 59 | ifa = indev->ifa_list; | ||
| 60 | newdst = ifa->ifa_local; | ||
| 61 | } | ||
| 62 | rcu_read_unlock(); | ||
| 63 | |||
| 64 | if (!newdst) | ||
| 65 | return NF_DROP; | ||
| 66 | } | ||
| 67 | |||
| 68 | /* Transfer from original range. */ | ||
| 69 | memset(&newrange.min_addr, 0, sizeof(newrange.min_addr)); | ||
| 70 | memset(&newrange.max_addr, 0, sizeof(newrange.max_addr)); | ||
| 71 | newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS; | ||
| 72 | newrange.min_addr.ip = newdst; | ||
| 73 | newrange.max_addr.ip = newdst; | ||
| 74 | newrange.min_proto = mr->range[0].min; | ||
| 75 | newrange.max_proto = mr->range[0].max; | ||
| 76 | |||
| 77 | /* Hand modified range to generic setup. */ | ||
| 78 | return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST); | ||
| 79 | } | ||
| 80 | EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4); | ||
| 81 | |||
| 82 | static const struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT; | ||
| 83 | |||
| 84 | unsigned int | ||
| 85 | nf_nat_redirect_ipv6(struct sk_buff *skb, const struct nf_nat_range *range, | ||
| 86 | unsigned int hooknum) | ||
| 87 | { | ||
| 88 | struct nf_nat_range newrange; | ||
| 89 | struct in6_addr newdst; | ||
| 90 | enum ip_conntrack_info ctinfo; | ||
| 91 | struct nf_conn *ct; | ||
| 92 | |||
| 93 | ct = nf_ct_get(skb, &ctinfo); | ||
| 94 | if (hooknum == NF_INET_LOCAL_OUT) { | ||
| 95 | newdst = loopback_addr; | ||
| 96 | } else { | ||
| 97 | struct inet6_dev *idev; | ||
| 98 | struct inet6_ifaddr *ifa; | ||
| 99 | bool addr = false; | ||
| 100 | |||
| 101 | rcu_read_lock(); | ||
| 102 | idev = __in6_dev_get(skb->dev); | ||
| 103 | if (idev != NULL) { | ||
| 104 | list_for_each_entry(ifa, &idev->addr_list, if_list) { | ||
| 105 | newdst = ifa->addr; | ||
| 106 | addr = true; | ||
| 107 | break; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | rcu_read_unlock(); | ||
| 111 | |||
| 112 | if (!addr) | ||
| 113 | return NF_DROP; | ||
| 114 | } | ||
| 115 | |||
| 116 | newrange.flags = range->flags | NF_NAT_RANGE_MAP_IPS; | ||
| 117 | newrange.min_addr.in6 = newdst; | ||
| 118 | newrange.max_addr.in6 = newdst; | ||
| 119 | newrange.min_proto = range->min_proto; | ||
| 120 | newrange.max_proto = range->max_proto; | ||
| 121 | |||
| 122 | return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST); | ||
| 123 | } | ||
| 124 | EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv6); | ||
| 125 | |||
| 126 | MODULE_LICENSE("GPL"); | ||
| 127 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | ||
diff --git a/net/netfilter/xt_REDIRECT.c b/net/netfilter/xt_REDIRECT.c index b6ec67efd900..03f0b370e178 100644 --- a/net/netfilter/xt_REDIRECT.c +++ b/net/netfilter/xt_REDIRECT.c | |||
| @@ -26,8 +26,7 @@ | |||
| 26 | #include <net/checksum.h> | 26 | #include <net/checksum.h> |
| 27 | #include <net/protocol.h> | 27 | #include <net/protocol.h> |
| 28 | #include <net/netfilter/nf_nat.h> | 28 | #include <net/netfilter/nf_nat.h> |
| 29 | #include <net/netfilter/ipv4/nf_nat_redirect.h> | 29 | #include <net/netfilter/nf_nat_redirect.h> |
| 30 | #include <net/netfilter/ipv6/nf_nat_redirect.h> | ||
| 31 | 30 | ||
| 32 | static unsigned int | 31 | static unsigned int |
| 33 | redirect_tg6(struct sk_buff *skb, const struct xt_action_param *par) | 32 | redirect_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
