Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, 2012 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 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <linux/ipv6.h> |
YOSHIFUJI Hideaki / 吉藤英明 | edb2722 | 2013-02-07 11:18:22 +0100 | [diff] [blame] | 12 | #include <net/ipv6.h> |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 13 | #include <linux/netfilter.h> |
| 14 | #include <linux/netfilter_ipv6.h> |
| 15 | #include <linux/netfilter_ipv6/ip6t_NPT.h> |
| 16 | #include <linux/netfilter/x_tables.h> |
| 17 | |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 18 | static int ip6t_npt_checkentry(const struct xt_tgchk_param *par) |
| 19 | { |
| 20 | struct ip6t_npt_tginfo *npt = par->targinfo; |
Ulrich Weber | 429da4c | 2013-01-02 05:24:40 +0000 | [diff] [blame] | 21 | __wsum src_sum = 0, dst_sum = 0; |
YOSHIFUJI Hideaki / 吉藤英明 | edb2722 | 2013-02-07 11:18:22 +0100 | [diff] [blame] | 22 | struct in6_addr pfx; |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 23 | unsigned int i; |
| 24 | |
| 25 | if (npt->src_pfx_len > 64 || npt->dst_pfx_len > 64) |
| 26 | return -EINVAL; |
| 27 | |
YOSHIFUJI Hideaki / 吉藤英明 | edb2722 | 2013-02-07 11:18:22 +0100 | [diff] [blame] | 28 | /* Ensure that LSB of prefix is zero */ |
| 29 | ipv6_addr_prefix(&pfx, &npt->src_pfx.in6, npt->src_pfx_len); |
| 30 | if (!ipv6_addr_equal(&pfx, &npt->src_pfx.in6)) |
| 31 | return -EINVAL; |
| 32 | ipv6_addr_prefix(&pfx, &npt->dst_pfx.in6, npt->dst_pfx_len); |
| 33 | if (!ipv6_addr_equal(&pfx, &npt->dst_pfx.in6)) |
| 34 | return -EINVAL; |
| 35 | |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 36 | for (i = 0; i < ARRAY_SIZE(npt->src_pfx.in6.s6_addr16); i++) { |
Ulrich Weber | 429da4c | 2013-01-02 05:24:40 +0000 | [diff] [blame] | 37 | src_sum = csum_add(src_sum, |
| 38 | (__force __wsum)npt->src_pfx.in6.s6_addr16[i]); |
| 39 | dst_sum = csum_add(dst_sum, |
| 40 | (__force __wsum)npt->dst_pfx.in6.s6_addr16[i]); |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 41 | } |
| 42 | |
YOSHIFUJI Hideaki / 吉藤英明 | f5271ff | 2013-01-26 08:37:48 +0000 | [diff] [blame] | 43 | npt->adjustment = ~csum_fold(csum_sub(src_sum, dst_sum)); |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static bool ip6t_npt_map_pfx(const struct ip6t_npt_tginfo *npt, |
| 48 | struct in6_addr *addr) |
| 49 | { |
| 50 | unsigned int pfx_len; |
| 51 | unsigned int i, idx; |
| 52 | __be32 mask; |
| 53 | __sum16 sum; |
| 54 | |
| 55 | pfx_len = max(npt->src_pfx_len, npt->dst_pfx_len); |
| 56 | for (i = 0; i < pfx_len; i += 32) { |
| 57 | if (pfx_len - i >= 32) |
| 58 | mask = 0; |
| 59 | else |
| 60 | mask = htonl(~((1 << (pfx_len - i)) - 1)); |
| 61 | |
| 62 | idx = i / 32; |
| 63 | addr->s6_addr32[idx] &= mask; |
YOSHIFUJI Hideaki / 吉藤英明 | d4c38fa | 2013-01-26 08:38:44 +0000 | [diff] [blame] | 64 | addr->s6_addr32[idx] |= ~mask & npt->dst_pfx.in6.s6_addr32[idx]; |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (pfx_len <= 48) |
| 68 | idx = 3; |
| 69 | else { |
| 70 | for (idx = 4; idx < ARRAY_SIZE(addr->s6_addr16); idx++) { |
| 71 | if ((__force __sum16)addr->s6_addr16[idx] != |
| 72 | CSUM_MANGLED_0) |
| 73 | break; |
| 74 | } |
| 75 | if (idx == ARRAY_SIZE(addr->s6_addr16)) |
| 76 | return false; |
| 77 | } |
| 78 | |
YOSHIFUJI Hideaki / 吉藤英明 | f5271ff | 2013-01-26 08:37:48 +0000 | [diff] [blame] | 79 | sum = ~csum_fold(csum_add(csum_unfold((__force __sum16)addr->s6_addr16[idx]), |
| 80 | csum_unfold(npt->adjustment))); |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 81 | if (sum == CSUM_MANGLED_0) |
| 82 | sum = 0; |
| 83 | *(__force __sum16 *)&addr->s6_addr16[idx] = sum; |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | static unsigned int |
| 89 | ip6t_snpt_tg(struct sk_buff *skb, const struct xt_action_param *par) |
| 90 | { |
| 91 | const struct ip6t_npt_tginfo *npt = par->targinfo; |
| 92 | |
| 93 | if (!ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->saddr)) { |
| 94 | icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD, |
| 95 | offsetof(struct ipv6hdr, saddr)); |
| 96 | return NF_DROP; |
| 97 | } |
| 98 | return XT_CONTINUE; |
| 99 | } |
| 100 | |
| 101 | static unsigned int |
| 102 | ip6t_dnpt_tg(struct sk_buff *skb, const struct xt_action_param *par) |
| 103 | { |
| 104 | const struct ip6t_npt_tginfo *npt = par->targinfo; |
| 105 | |
| 106 | if (!ip6t_npt_map_pfx(npt, &ipv6_hdr(skb)->daddr)) { |
| 107 | icmpv6_send(skb, ICMPV6_PARAMPROB, ICMPV6_HDR_FIELD, |
| 108 | offsetof(struct ipv6hdr, daddr)); |
| 109 | return NF_DROP; |
| 110 | } |
| 111 | return XT_CONTINUE; |
| 112 | } |
| 113 | |
| 114 | static struct xt_target ip6t_npt_target_reg[] __read_mostly = { |
| 115 | { |
| 116 | .name = "SNPT", |
Florian Westphal | a82783c | 2013-03-11 20:11:01 +0000 | [diff] [blame^] | 117 | .table = "mangle", |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 118 | .target = ip6t_snpt_tg, |
| 119 | .targetsize = sizeof(struct ip6t_npt_tginfo), |
| 120 | .checkentry = ip6t_npt_checkentry, |
| 121 | .family = NFPROTO_IPV6, |
| 122 | .hooks = (1 << NF_INET_LOCAL_IN) | |
| 123 | (1 << NF_INET_POST_ROUTING), |
| 124 | .me = THIS_MODULE, |
| 125 | }, |
| 126 | { |
| 127 | .name = "DNPT", |
Florian Westphal | a82783c | 2013-03-11 20:11:01 +0000 | [diff] [blame^] | 128 | .table = "mangle", |
Patrick McHardy | 8a91bb0 | 2012-08-26 19:14:31 +0200 | [diff] [blame] | 129 | .target = ip6t_dnpt_tg, |
| 130 | .targetsize = sizeof(struct ip6t_npt_tginfo), |
| 131 | .checkentry = ip6t_npt_checkentry, |
| 132 | .family = NFPROTO_IPV6, |
| 133 | .hooks = (1 << NF_INET_PRE_ROUTING) | |
| 134 | (1 << NF_INET_LOCAL_OUT), |
| 135 | .me = THIS_MODULE, |
| 136 | }, |
| 137 | }; |
| 138 | |
| 139 | static int __init ip6t_npt_init(void) |
| 140 | { |
| 141 | return xt_register_targets(ip6t_npt_target_reg, |
| 142 | ARRAY_SIZE(ip6t_npt_target_reg)); |
| 143 | } |
| 144 | |
| 145 | static void __exit ip6t_npt_exit(void) |
| 146 | { |
| 147 | xt_unregister_targets(ip6t_npt_target_reg, |
| 148 | ARRAY_SIZE(ip6t_npt_target_reg)); |
| 149 | } |
| 150 | |
| 151 | module_init(ip6t_npt_init); |
| 152 | module_exit(ip6t_npt_exit); |
| 153 | |
| 154 | MODULE_LICENSE("GPL"); |
| 155 | MODULE_DESCRIPTION("IPv6-to-IPv6 Network Prefix Translation (RFC 6296)"); |
| 156 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 157 | MODULE_ALIAS("ip6t_SNPT"); |
| 158 | MODULE_ALIAS("ip6t_DNPT"); |