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_checksum.h>
19 #include <net/ip6_route.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_nat_core.h>
26 #include <net/netfilter/nf_nat_l3proto.h>
27 #include <net/netfilter/nf_nat_l4proto.h>
29 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6;
32 static void nf_nat_ipv6_decode_session(struct sk_buff *skb,
33 const struct nf_conn *ct,
34 enum ip_conntrack_dir dir,
35 unsigned long statusbit,
38 const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
39 struct flowi6 *fl6 = &fl->u.ip6;
41 if (ct->status & statusbit) {
42 fl6->daddr = t->dst.u3.in6;
43 if (t->dst.protonum == IPPROTO_TCP ||
44 t->dst.protonum == IPPROTO_UDP ||
45 t->dst.protonum == IPPROTO_UDPLITE ||
46 t->dst.protonum == IPPROTO_DCCP ||
47 t->dst.protonum == IPPROTO_SCTP)
48 fl6->fl6_dport = t->dst.u.all;
51 statusbit ^= IPS_NAT_MASK;
53 if (ct->status & statusbit) {
54 fl6->saddr = t->src.u3.in6;
55 if (t->dst.protonum == IPPROTO_TCP ||
56 t->dst.protonum == IPPROTO_UDP ||
57 t->dst.protonum == IPPROTO_UDPLITE ||
58 t->dst.protonum == IPPROTO_DCCP ||
59 t->dst.protonum == IPPROTO_SCTP)
60 fl6->fl6_sport = t->src.u.all;
65 static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb,
66 unsigned int iphdroff,
67 const struct nf_conntrack_tuple *target,
68 enum nf_nat_manip_type maniptype)
70 struct ipv6hdr *ipv6h;
75 if (!skb_make_writable(skb, iphdroff + sizeof(*ipv6h)))
78 ipv6h = (void *)skb->data + iphdroff;
79 nexthdr = ipv6h->nexthdr;
80 hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h),
85 if ((frag_off & htons(~0x7)) == 0 &&
86 !nf_nat_l4proto_manip_pkt(skb, &nf_nat_l3proto_ipv6, iphdroff, hdroff,
90 /* must reload, offset might have changed */
91 ipv6h = (void *)skb->data + iphdroff;
94 if (maniptype == NF_NAT_MANIP_SRC)
95 ipv6h->saddr = target->src.u3.in6;
97 ipv6h->daddr = target->dst.u3.in6;
102 static void nf_nat_ipv6_csum_update(struct sk_buff *skb,
103 unsigned int iphdroff, __sum16 *check,
104 const struct nf_conntrack_tuple *t,
105 enum nf_nat_manip_type maniptype)
107 const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff);
108 const struct in6_addr *oldip, *newip;
110 if (maniptype == NF_NAT_MANIP_SRC) {
111 oldip = &ipv6h->saddr;
112 newip = &t->src.u3.in6;
114 oldip = &ipv6h->daddr;
115 newip = &t->dst.u3.in6;
117 inet_proto_csum_replace16(check, skb, oldip->s6_addr32,
118 newip->s6_addr32, true);
121 static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
122 u8 proto, void *data, __sum16 *check,
123 int datalen, int oldlen)
125 if (skb->ip_summed != CHECKSUM_PARTIAL) {
126 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
128 skb->ip_summed = CHECKSUM_PARTIAL;
129 skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
130 (data - (void *)skb->data);
131 skb->csum_offset = (void *)check - data;
132 *check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
135 inet_proto_csum_replace2(check, skb,
136 htons(oldlen), htons(datalen), true);
139 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
140 static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
141 struct nf_nat_range2 *range)
143 if (tb[CTA_NAT_V6_MINIP]) {
144 nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
145 sizeof(struct in6_addr));
146 range->flags |= NF_NAT_RANGE_MAP_IPS;
149 if (tb[CTA_NAT_V6_MAXIP])
150 nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
151 sizeof(struct in6_addr));
153 range->max_addr = range->min_addr;
159 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = {
160 .l3proto = NFPROTO_IPV6,
161 .manip_pkt = nf_nat_ipv6_manip_pkt,
162 .csum_update = nf_nat_ipv6_csum_update,
163 .csum_recalc = nf_nat_ipv6_csum_recalc,
164 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
165 .nlattr_to_range = nf_nat_ipv6_nlattr_to_range,
168 .decode_session = nf_nat_ipv6_decode_session,
172 int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,
174 enum ip_conntrack_info ctinfo,
175 unsigned int hooknum,
179 struct icmp6hdr icmp6;
182 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
183 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
184 struct nf_conntrack_tuple target;
185 unsigned long statusbit;
187 WARN_ON(ctinfo != IP_CT_RELATED && ctinfo != IP_CT_RELATED_REPLY);
189 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
191 if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6))
194 inside = (void *)skb->data + hdrlen;
195 if (inside->icmp6.icmp6_type == NDISC_REDIRECT) {
196 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
198 if (ct->status & IPS_NAT_MASK)
202 if (manip == NF_NAT_MANIP_SRC)
203 statusbit = IPS_SRC_NAT;
205 statusbit = IPS_DST_NAT;
207 /* Invert if this is reply direction */
208 if (dir == IP_CT_DIR_REPLY)
209 statusbit ^= IPS_NAT_MASK;
211 if (!(ct->status & statusbit))
214 if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6),
215 &ct->tuplehash[!dir].tuple, !manip))
218 if (skb->ip_summed != CHECKSUM_PARTIAL) {
219 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
220 inside = (void *)skb->data + hdrlen;
221 inside->icmp6.icmp6_cksum = 0;
222 inside->icmp6.icmp6_cksum =
223 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
224 skb->len - hdrlen, IPPROTO_ICMPV6,
225 skb_checksum(skb, hdrlen,
226 skb->len - hdrlen, 0));
229 nf_ct_invert_tuple(&target, &ct->tuplehash[!dir].tuple);
230 target.dst.protonum = IPPROTO_ICMPV6;
231 if (!nf_nat_ipv6_manip_pkt(skb, 0, &target, manip))
236 EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
239 nf_nat_ipv6_fn(void *priv, struct sk_buff *skb,
240 const struct nf_hook_state *state)
243 enum ip_conntrack_info ctinfo;
248 ct = nf_ct_get(skb, &ctinfo);
249 /* Can't track? It's not due to stress, or conntrack would
250 * have dropped it. Hence it's the user's responsibilty to
251 * packet filter it out, or implement conntrack/NAT for that
257 if (ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY) {
258 nexthdr = ipv6_hdr(skb)->nexthdr;
259 hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
260 &nexthdr, &frag_off);
262 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
263 if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
272 return nf_nat_inet_fn(priv, skb, state);
276 nf_nat_ipv6_in(void *priv, struct sk_buff *skb,
277 const struct nf_hook_state *state)
280 struct in6_addr daddr = ipv6_hdr(skb)->daddr;
282 ret = nf_nat_ipv6_fn(priv, skb, state);
283 if (ret != NF_DROP && ret != NF_STOLEN &&
284 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
291 nf_nat_ipv6_out(void *priv, struct sk_buff *skb,
292 const struct nf_hook_state *state)
295 const struct nf_conn *ct;
296 enum ip_conntrack_info ctinfo;
301 ret = nf_nat_ipv6_fn(priv, skb, state);
303 if (ret != NF_DROP && ret != NF_STOLEN &&
304 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
305 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
306 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
308 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
309 &ct->tuplehash[!dir].tuple.dst.u3) ||
310 (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
311 ct->tuplehash[dir].tuple.src.u.all !=
312 ct->tuplehash[!dir].tuple.dst.u.all)) {
313 err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
315 ret = NF_DROP_ERR(err);
322 static int nat_route_me_harder(struct net *net, struct sk_buff *skb)
324 #ifdef CONFIG_IPV6_MODULE
325 const struct nf_ipv6_ops *v6_ops = nf_get_ipv6_ops();
328 return -EHOSTUNREACH;
330 return v6_ops->route_me_harder(net, skb);
332 return ip6_route_me_harder(net, skb);
337 nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,
338 const struct nf_hook_state *state)
340 const struct nf_conn *ct;
341 enum ip_conntrack_info ctinfo;
345 ret = nf_nat_ipv6_fn(priv, skb, state);
346 if (ret != NF_DROP && ret != NF_STOLEN &&
347 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
348 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
350 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
351 &ct->tuplehash[!dir].tuple.src.u3)) {
352 err = nat_route_me_harder(state->net, skb);
354 ret = NF_DROP_ERR(err);
357 else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
358 ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
359 ct->tuplehash[dir].tuple.dst.u.all !=
360 ct->tuplehash[!dir].tuple.src.u.all) {
361 err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
363 ret = NF_DROP_ERR(err);
370 static const struct nf_hook_ops nf_nat_ipv6_ops[] = {
371 /* Before packet filtering, change destination */
373 .hook = nf_nat_ipv6_in,
375 .hooknum = NF_INET_PRE_ROUTING,
376 .priority = NF_IP6_PRI_NAT_DST,
378 /* After packet filtering, change source */
380 .hook = nf_nat_ipv6_out,
382 .hooknum = NF_INET_POST_ROUTING,
383 .priority = NF_IP6_PRI_NAT_SRC,
385 /* Before packet filtering, change destination */
387 .hook = nf_nat_ipv6_local_fn,
389 .hooknum = NF_INET_LOCAL_OUT,
390 .priority = NF_IP6_PRI_NAT_DST,
392 /* After packet filtering, change source */
394 .hook = nf_nat_ipv6_fn,
396 .hooknum = NF_INET_LOCAL_IN,
397 .priority = NF_IP6_PRI_NAT_SRC,
401 int nf_nat_l3proto_ipv6_register_fn(struct net *net, const struct nf_hook_ops *ops)
403 return nf_nat_register_fn(net, ops, nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
405 EXPORT_SYMBOL_GPL(nf_nat_l3proto_ipv6_register_fn);
407 void nf_nat_l3proto_ipv6_unregister_fn(struct net *net, const struct nf_hook_ops *ops)
409 nf_nat_unregister_fn(net, ops, ARRAY_SIZE(nf_nat_ipv6_ops));
411 EXPORT_SYMBOL_GPL(nf_nat_l3proto_ipv6_unregister_fn);
413 static int __init nf_nat_l3proto_ipv6_init(void)
415 return nf_nat_l3proto_register(&nf_nat_l3proto_ipv6);
418 static void __exit nf_nat_l3proto_ipv6_exit(void)
420 nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6);
423 MODULE_LICENSE("GPL");
424 MODULE_ALIAS("nf-nat-" __stringify(AF_INET6));
426 module_init(nf_nat_l3proto_ipv6_init);
427 module_exit(nf_nat_l3proto_ipv6_exit);