2 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
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.
8 * Development of IPv6 NAT funded by Astaro.
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/ipv6.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <net/secure_seq.h>
17 #include <net/checksum.h>
18 #include <net/ip6_route.h>
21 #include <net/netfilter/nf_conntrack_core.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_nat_core.h>
24 #include <net/netfilter/nf_nat_l3proto.h>
25 #include <net/netfilter/nf_nat_l4proto.h>
27 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6;
30 static void nf_nat_ipv6_decode_session(struct sk_buff *skb,
31 const struct nf_conn *ct,
32 enum ip_conntrack_dir dir,
33 unsigned long statusbit,
36 const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
37 struct flowi6 *fl6 = &fl->u.ip6;
39 if (ct->status & statusbit) {
40 fl6->daddr = t->dst.u3.in6;
41 if (t->dst.protonum == IPPROTO_TCP ||
42 t->dst.protonum == IPPROTO_UDP ||
43 t->dst.protonum == IPPROTO_UDPLITE ||
44 t->dst.protonum == IPPROTO_DCCP ||
45 t->dst.protonum == IPPROTO_SCTP)
46 fl6->fl6_dport = t->dst.u.all;
49 statusbit ^= IPS_NAT_MASK;
51 if (ct->status & statusbit) {
52 fl6->saddr = t->src.u3.in6;
53 if (t->dst.protonum == IPPROTO_TCP ||
54 t->dst.protonum == IPPROTO_UDP ||
55 t->dst.protonum == IPPROTO_UDPLITE ||
56 t->dst.protonum == IPPROTO_DCCP ||
57 t->dst.protonum == IPPROTO_SCTP)
58 fl6->fl6_sport = t->src.u.all;
63 static bool nf_nat_ipv6_in_range(const struct nf_conntrack_tuple *t,
64 const struct nf_nat_range *range)
66 return ipv6_addr_cmp(&t->src.u3.in6, &range->min_addr.in6) >= 0 &&
67 ipv6_addr_cmp(&t->src.u3.in6, &range->max_addr.in6) <= 0;
70 static u32 nf_nat_ipv6_secure_port(const struct nf_conntrack_tuple *t,
73 return secure_ipv6_port_ephemeral(t->src.u3.ip6, t->dst.u3.ip6, dport);
76 static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb,
77 unsigned int iphdroff,
78 const struct nf_nat_l4proto *l4proto,
79 const struct nf_conntrack_tuple *target,
80 enum nf_nat_manip_type maniptype)
82 struct ipv6hdr *ipv6h;
87 if (!skb_make_writable(skb, iphdroff + sizeof(*ipv6h)))
90 ipv6h = (void *)skb->data + iphdroff;
91 nexthdr = ipv6h->nexthdr;
92 hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h),
97 if ((frag_off & htons(~0x7)) == 0 &&
98 !l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv6, iphdroff, hdroff,
102 if (maniptype == NF_NAT_MANIP_SRC)
103 ipv6h->saddr = target->src.u3.in6;
105 ipv6h->daddr = target->dst.u3.in6;
110 static void nf_nat_ipv6_csum_update(struct sk_buff *skb,
111 unsigned int iphdroff, __sum16 *check,
112 const struct nf_conntrack_tuple *t,
113 enum nf_nat_manip_type maniptype)
115 const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff);
116 const struct in6_addr *oldip, *newip;
118 if (maniptype == NF_NAT_MANIP_SRC) {
119 oldip = &ipv6h->saddr;
120 newip = &t->src.u3.in6;
122 oldip = &ipv6h->daddr;
123 newip = &t->dst.u3.in6;
125 inet_proto_csum_replace16(check, skb, oldip->s6_addr32,
126 newip->s6_addr32, 1);
129 static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
130 u8 proto, void *data, __sum16 *check,
131 int datalen, int oldlen)
133 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
134 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
136 if (skb->ip_summed != CHECKSUM_PARTIAL) {
137 if (!(rt->rt6i_flags & RTF_LOCAL) &&
138 (!skb->dev || skb->dev->features & NETIF_F_V6_CSUM)) {
139 skb->ip_summed = CHECKSUM_PARTIAL;
140 skb->csum_start = skb_headroom(skb) +
141 skb_network_offset(skb) +
142 (data - (void *)skb->data);
143 skb->csum_offset = (void *)check - data;
144 *check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
148 *check = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
150 csum_partial(data, datalen,
152 if (proto == IPPROTO_UDP && !*check)
153 *check = CSUM_MANGLED_0;
156 inet_proto_csum_replace2(check, skb,
157 htons(oldlen), htons(datalen), 1);
160 static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
161 struct nf_nat_range *range)
163 if (tb[CTA_NAT_V6_MINIP]) {
164 nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
165 sizeof(struct in6_addr));
166 range->flags |= NF_NAT_RANGE_MAP_IPS;
169 if (tb[CTA_NAT_V6_MAXIP])
170 nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
171 sizeof(struct in6_addr));
173 range->max_addr = range->min_addr;
178 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = {
179 .l3proto = NFPROTO_IPV6,
180 .secure_port = nf_nat_ipv6_secure_port,
181 .in_range = nf_nat_ipv6_in_range,
182 .manip_pkt = nf_nat_ipv6_manip_pkt,
183 .csum_update = nf_nat_ipv6_csum_update,
184 .csum_recalc = nf_nat_ipv6_csum_recalc,
185 .nlattr_to_range = nf_nat_ipv6_nlattr_to_range,
187 .decode_session = nf_nat_ipv6_decode_session,
191 int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,
193 enum ip_conntrack_info ctinfo,
194 unsigned int hooknum,
198 struct icmp6hdr icmp6;
201 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
202 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
203 const struct nf_nat_l4proto *l4proto;
204 struct nf_conntrack_tuple target;
205 unsigned long statusbit;
207 NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
209 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
211 if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6))
214 inside = (void *)skb->data + hdrlen;
215 if (inside->icmp6.icmp6_type == NDISC_REDIRECT) {
216 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
218 if (ct->status & IPS_NAT_MASK)
222 if (manip == NF_NAT_MANIP_SRC)
223 statusbit = IPS_SRC_NAT;
225 statusbit = IPS_DST_NAT;
227 /* Invert if this is reply direction */
228 if (dir == IP_CT_DIR_REPLY)
229 statusbit ^= IPS_NAT_MASK;
231 if (!(ct->status & statusbit))
234 l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, inside->ip6.nexthdr);
235 if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6),
236 l4proto, &ct->tuplehash[!dir].tuple, !manip))
239 if (skb->ip_summed != CHECKSUM_PARTIAL) {
240 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
241 inside = (void *)skb->data + hdrlen;
242 inside->icmp6.icmp6_cksum = 0;
243 inside->icmp6.icmp6_cksum =
244 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
245 skb->len - hdrlen, IPPROTO_ICMPV6,
246 csum_partial(&inside->icmp6,
247 skb->len - hdrlen, 0));
250 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
251 l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, IPPROTO_ICMPV6);
252 if (!nf_nat_ipv6_manip_pkt(skb, 0, l4proto, &target, manip))
257 EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
259 static int __init nf_nat_l3proto_ipv6_init(void)
263 err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
266 err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv6);
272 nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
277 static void __exit nf_nat_l3proto_ipv6_exit(void)
279 nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6);
280 nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
283 MODULE_LICENSE("GPL");
284 MODULE_ALIAS("nf-nat-" __stringify(AF_INET6));
286 module_init(nf_nat_l3proto_ipv6_init);
287 module_exit(nf_nat_l3proto_ipv6_exit);