diff options
| author | Patrick McHardy <kaber@trash.net> | 2012-08-26 13:14:18 -0400 |
|---|---|---|
| committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2012-08-29 21:00:19 -0400 |
| commit | ed72d9e294a66fce8f4b4a2f6c8c011b22f1a87c (patch) | |
| tree | e7a8ad02c42ab3ed375c063851b5746cc6b779ff | |
| parent | 115e23ac78f87b632b5406e9d504fd56d17ffef1 (diff) | |
netfilter: ip6tables: add NETMAP target
Signed-off-by: Patrick McHardy <kaber@trash.net>
| -rw-r--r-- | net/ipv6/netfilter/Kconfig | 10 | ||||
| -rw-r--r-- | net/ipv6/netfilter/Makefile | 1 | ||||
| -rw-r--r-- | net/ipv6/netfilter/ip6t_NETMAP.c | 94 |
3 files changed, 105 insertions, 0 deletions
diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig index 585590f16f8c..7bdf73be9a99 100644 --- a/net/ipv6/netfilter/Kconfig +++ b/net/ipv6/netfilter/Kconfig | |||
| @@ -156,6 +156,16 @@ config IP6_NF_TARGET_MASQUERADE | |||
| 156 | 156 | ||
| 157 | To compile it as a module, choose M here. If unsure, say N. | 157 | To compile it as a module, choose M here. If unsure, say N. |
| 158 | 158 | ||
| 159 | config IP6_NF_TARGET_NETMAP | ||
| 160 | tristate "NETMAP target support" | ||
| 161 | depends on NF_NAT_IPV6 | ||
| 162 | help | ||
| 163 | NETMAP is an implementation of static 1:1 NAT mapping of network | ||
| 164 | addresses. It maps the network address part, while keeping the host | ||
| 165 | address part intact. | ||
| 166 | |||
| 167 | To compile it as a module, choose M here. If unsure, say N. | ||
| 168 | |||
| 159 | config IP6_NF_TARGET_REDIRECT | 169 | config IP6_NF_TARGET_REDIRECT |
| 160 | tristate "REDIRECT target support" | 170 | tristate "REDIRECT target support" |
| 161 | depends on NF_NAT_IPV6 | 171 | depends on NF_NAT_IPV6 |
diff --git a/net/ipv6/netfilter/Makefile b/net/ipv6/netfilter/Makefile index e30a531d40ce..0864ce601651 100644 --- a/net/ipv6/netfilter/Makefile +++ b/net/ipv6/netfilter/Makefile | |||
| @@ -35,5 +35,6 @@ obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o | |||
| 35 | 35 | ||
| 36 | # targets | 36 | # targets |
| 37 | obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.o | 37 | obj-$(CONFIG_IP6_NF_TARGET_MASQUERADE) += ip6t_MASQUERADE.o |
| 38 | obj-$(CONFIG_IP6_NF_TARGET_NETMAP) += ip6t_NETMAP.o | ||
| 38 | obj-$(CONFIG_IP6_NF_TARGET_REDIRECT) += ip6t_REDIRECT.o | 39 | obj-$(CONFIG_IP6_NF_TARGET_REDIRECT) += ip6t_REDIRECT.o |
| 39 | obj-$(CONFIG_IP6_NF_TARGET_REJECT) += ip6t_REJECT.o | 40 | obj-$(CONFIG_IP6_NF_TARGET_REJECT) += ip6t_REJECT.o |
diff --git a/net/ipv6/netfilter/ip6t_NETMAP.c b/net/ipv6/netfilter/ip6t_NETMAP.c new file mode 100644 index 000000000000..4f3bf360e50f --- /dev/null +++ b/net/ipv6/netfilter/ip6t_NETMAP.c | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2011 Patrick McHardy <kaber@trash.net> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 as | ||
| 6 | * published by the Free Software Foundation. | ||
| 7 | * | ||
| 8 | * Based on Svenning Soerensen's IPv4 NETMAP target. Development of IPv6 | ||
| 9 | * NAT funded by Astaro. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <linux/kernel.h> | ||
| 13 | #include <linux/module.h> | ||
| 14 | #include <linux/ipv6.h> | ||
| 15 | #include <linux/netfilter.h> | ||
| 16 | #include <linux/netfilter_ipv6.h> | ||
| 17 | #include <linux/netfilter/x_tables.h> | ||
| 18 | #include <net/netfilter/nf_nat.h> | ||
| 19 | |||
| 20 | static unsigned int | ||
| 21 | netmap_tg6(struct sk_buff *skb, const struct xt_action_param *par) | ||
| 22 | { | ||
| 23 | const struct nf_nat_range *range = par->targinfo; | ||
| 24 | struct nf_nat_range newrange; | ||
| 25 | struct nf_conn *ct; | ||
| 26 | enum ip_conntrack_info ctinfo; | ||
| 27 | union nf_inet_addr new_addr, netmask; | ||
| 28 | unsigned int i; | ||
| 29 | |||
| 30 | ct = nf_ct_get(skb, &ctinfo); | ||
| 31 | for (i = 0; i < ARRAY_SIZE(range->min_addr.ip6); i++) | ||
| 32 | netmask.ip6[i] = ~(range->min_addr.ip6[i] ^ | ||
| 33 | range->max_addr.ip6[i]); | ||
| 34 | |||
| 35 | if (par->hooknum == NF_INET_PRE_ROUTING || | ||
| 36 | par->hooknum == NF_INET_LOCAL_OUT) | ||
| 37 | new_addr.in6 = ipv6_hdr(skb)->daddr; | ||
| 38 | else | ||
| 39 | new_addr.in6 = ipv6_hdr(skb)->saddr; | ||
| 40 | |||
| 41 | for (i = 0; i < ARRAY_SIZE(new_addr.ip6); i++) { | ||
| 42 | new_addr.ip6[i] &= ~netmask.ip6[i]; | ||
| 43 | new_addr.ip6[i] |= range->min_addr.ip6[i] & | ||
| 44 | netmask.ip6[i]; | ||
| 45 | } | ||
| 46 | |||
| 47 | newrange.flags = range->flags | NF_NAT_RANGE_MAP_IPS; | ||
| 48 | newrange.min_addr = new_addr; | ||
| 49 | newrange.max_addr = new_addr; | ||
| 50 | newrange.min_proto = range->min_proto; | ||
| 51 | newrange.max_proto = range->max_proto; | ||
| 52 | |||
| 53 | return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum)); | ||
| 54 | } | ||
| 55 | |||
| 56 | static int netmap_tg6_checkentry(const struct xt_tgchk_param *par) | ||
| 57 | { | ||
| 58 | const struct nf_nat_range *range = par->targinfo; | ||
| 59 | |||
| 60 | if (!(range->flags & NF_NAT_RANGE_MAP_IPS)) | ||
| 61 | return -EINVAL; | ||
| 62 | return 0; | ||
| 63 | } | ||
| 64 | |||
| 65 | static struct xt_target netmap_tg6_reg __read_mostly = { | ||
| 66 | .name = "NETMAP", | ||
| 67 | .family = NFPROTO_IPV6, | ||
| 68 | .target = netmap_tg6, | ||
| 69 | .targetsize = sizeof(struct nf_nat_range), | ||
| 70 | .table = "nat", | ||
| 71 | .hooks = (1 << NF_INET_PRE_ROUTING) | | ||
| 72 | (1 << NF_INET_POST_ROUTING) | | ||
| 73 | (1 << NF_INET_LOCAL_OUT) | | ||
| 74 | (1 << NF_INET_LOCAL_IN), | ||
| 75 | .checkentry = netmap_tg6_checkentry, | ||
| 76 | .me = THIS_MODULE, | ||
| 77 | }; | ||
| 78 | |||
| 79 | static int __init netmap_tg6_init(void) | ||
| 80 | { | ||
| 81 | return xt_register_target(&netmap_tg6_reg); | ||
| 82 | } | ||
| 83 | |||
| 84 | static void netmap_tg6_exit(void) | ||
| 85 | { | ||
| 86 | xt_unregister_target(&netmap_tg6_reg); | ||
| 87 | } | ||
| 88 | |||
| 89 | module_init(netmap_tg6_init); | ||
| 90 | module_exit(netmap_tg6_exit); | ||
| 91 | |||
| 92 | MODULE_LICENSE("GPL"); | ||
| 93 | MODULE_DESCRIPTION("Xtables: 1:1 NAT mapping of IPv6 subnets"); | ||
| 94 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | ||
