blob: e0be97e636a48f54c1488ca70ae97a9a13e8be61 [file] [log] [blame]
Patrick McHardy58a317f2012-08-26 19:14:12 +02001/*
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 * Development of IPv6 NAT funded by Astaro.
9 */
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>
Stephen Rothwell2c969322012-09-05 15:34:58 +100018#include <net/ip6_checksum.h>
Patrick McHardy58a317f2012-08-26 19:14:12 +020019#include <net/ip6_route.h>
20#include <net/ipv6.h>
21
22#include <net/netfilter/nf_conntrack_core.h>
23#include <net/netfilter/nf_conntrack.h>
24#include <net/netfilter/nf_nat_core.h>
25#include <net/netfilter/nf_nat_l3proto.h>
26#include <net/netfilter/nf_nat_l4proto.h>
27
28static const struct nf_nat_l3proto nf_nat_l3proto_ipv6;
29
30#ifdef CONFIG_XFRM
31static void nf_nat_ipv6_decode_session(struct sk_buff *skb,
32 const struct nf_conn *ct,
33 enum ip_conntrack_dir dir,
34 unsigned long statusbit,
35 struct flowi *fl)
36{
37 const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
38 struct flowi6 *fl6 = &fl->u.ip6;
39
40 if (ct->status & statusbit) {
41 fl6->daddr = t->dst.u3.in6;
42 if (t->dst.protonum == IPPROTO_TCP ||
43 t->dst.protonum == IPPROTO_UDP ||
44 t->dst.protonum == IPPROTO_UDPLITE ||
45 t->dst.protonum == IPPROTO_DCCP ||
46 t->dst.protonum == IPPROTO_SCTP)
47 fl6->fl6_dport = t->dst.u.all;
48 }
49
50 statusbit ^= IPS_NAT_MASK;
51
52 if (ct->status & statusbit) {
53 fl6->saddr = t->src.u3.in6;
54 if (t->dst.protonum == IPPROTO_TCP ||
55 t->dst.protonum == IPPROTO_UDP ||
56 t->dst.protonum == IPPROTO_UDPLITE ||
57 t->dst.protonum == IPPROTO_DCCP ||
58 t->dst.protonum == IPPROTO_SCTP)
59 fl6->fl6_sport = t->src.u.all;
60 }
61}
62#endif
63
64static bool nf_nat_ipv6_in_range(const struct nf_conntrack_tuple *t,
65 const struct nf_nat_range *range)
66{
67 return ipv6_addr_cmp(&t->src.u3.in6, &range->min_addr.in6) >= 0 &&
68 ipv6_addr_cmp(&t->src.u3.in6, &range->max_addr.in6) <= 0;
69}
70
71static u32 nf_nat_ipv6_secure_port(const struct nf_conntrack_tuple *t,
72 __be16 dport)
73{
74 return secure_ipv6_port_ephemeral(t->src.u3.ip6, t->dst.u3.ip6, dport);
75}
76
77static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb,
78 unsigned int iphdroff,
79 const struct nf_nat_l4proto *l4proto,
80 const struct nf_conntrack_tuple *target,
81 enum nf_nat_manip_type maniptype)
82{
83 struct ipv6hdr *ipv6h;
84 __be16 frag_off;
85 int hdroff;
86 u8 nexthdr;
87
88 if (!skb_make_writable(skb, iphdroff + sizeof(*ipv6h)))
89 return false;
90
91 ipv6h = (void *)skb->data + iphdroff;
92 nexthdr = ipv6h->nexthdr;
93 hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h),
94 &nexthdr, &frag_off);
95 if (hdroff < 0)
96 goto manip_addr;
97
98 if ((frag_off & htons(~0x7)) == 0 &&
99 !l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv6, iphdroff, hdroff,
100 target, maniptype))
101 return false;
102manip_addr:
103 if (maniptype == NF_NAT_MANIP_SRC)
104 ipv6h->saddr = target->src.u3.in6;
105 else
106 ipv6h->daddr = target->dst.u3.in6;
107
108 return true;
109}
110
111static void nf_nat_ipv6_csum_update(struct sk_buff *skb,
112 unsigned int iphdroff, __sum16 *check,
113 const struct nf_conntrack_tuple *t,
114 enum nf_nat_manip_type maniptype)
115{
116 const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff);
117 const struct in6_addr *oldip, *newip;
118
119 if (maniptype == NF_NAT_MANIP_SRC) {
120 oldip = &ipv6h->saddr;
121 newip = &t->src.u3.in6;
122 } else {
123 oldip = &ipv6h->daddr;
124 newip = &t->dst.u3.in6;
125 }
126 inet_proto_csum_replace16(check, skb, oldip->s6_addr32,
Tom Herbert4b048d62015-08-17 13:42:25 -0700127 newip->s6_addr32, true);
Patrick McHardy58a317f2012-08-26 19:14:12 +0200128}
129
130static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
131 u8 proto, void *data, __sum16 *check,
132 int datalen, int oldlen)
133{
Patrick McHardy58a317f2012-08-26 19:14:12 +0200134 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Jarno Rajahalme26461902016-03-10 10:54:17 -0800135 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
136
137 skb->ip_summed = CHECKSUM_PARTIAL;
138 skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
139 (data - (void *)skb->data);
140 skb->csum_offset = (void *)check - data;
141 *check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
142 datalen, proto, 0);
Patrick McHardy58a317f2012-08-26 19:14:12 +0200143 } else
144 inet_proto_csum_replace2(check, skb,
Tom Herbert4b048d62015-08-17 13:42:25 -0700145 htons(oldlen), htons(datalen), true);
Patrick McHardy58a317f2012-08-26 19:14:12 +0200146}
147
Duan Jiong24de3d32014-06-30 09:19:32 +0800148#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardy58a317f2012-08-26 19:14:12 +0200149static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
150 struct nf_nat_range *range)
151{
152 if (tb[CTA_NAT_V6_MINIP]) {
153 nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
154 sizeof(struct in6_addr));
155 range->flags |= NF_NAT_RANGE_MAP_IPS;
156 }
157
158 if (tb[CTA_NAT_V6_MAXIP])
159 nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
160 sizeof(struct in6_addr));
161 else
162 range->max_addr = range->min_addr;
163
164 return 0;
165}
Duan Jiong24de3d32014-06-30 09:19:32 +0800166#endif
Patrick McHardy58a317f2012-08-26 19:14:12 +0200167
168static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = {
169 .l3proto = NFPROTO_IPV6,
170 .secure_port = nf_nat_ipv6_secure_port,
171 .in_range = nf_nat_ipv6_in_range,
172 .manip_pkt = nf_nat_ipv6_manip_pkt,
173 .csum_update = nf_nat_ipv6_csum_update,
174 .csum_recalc = nf_nat_ipv6_csum_recalc,
Duan Jiong24de3d32014-06-30 09:19:32 +0800175#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardy58a317f2012-08-26 19:14:12 +0200176 .nlattr_to_range = nf_nat_ipv6_nlattr_to_range,
Duan Jiong24de3d32014-06-30 09:19:32 +0800177#endif
Patrick McHardy58a317f2012-08-26 19:14:12 +0200178#ifdef CONFIG_XFRM
179 .decode_session = nf_nat_ipv6_decode_session,
180#endif
181};
182
183int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,
184 struct nf_conn *ct,
185 enum ip_conntrack_info ctinfo,
186 unsigned int hooknum,
187 unsigned int hdrlen)
188{
189 struct {
190 struct icmp6hdr icmp6;
191 struct ipv6hdr ip6;
192 } *inside;
193 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
194 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
195 const struct nf_nat_l4proto *l4proto;
196 struct nf_conntrack_tuple target;
197 unsigned long statusbit;
198
199 NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
200
201 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
202 return 0;
203 if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6))
204 return 0;
205
206 inside = (void *)skb->data + hdrlen;
207 if (inside->icmp6.icmp6_type == NDISC_REDIRECT) {
208 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
209 return 0;
210 if (ct->status & IPS_NAT_MASK)
211 return 0;
212 }
213
214 if (manip == NF_NAT_MANIP_SRC)
215 statusbit = IPS_SRC_NAT;
216 else
217 statusbit = IPS_DST_NAT;
218
219 /* Invert if this is reply direction */
220 if (dir == IP_CT_DIR_REPLY)
221 statusbit ^= IPS_NAT_MASK;
222
223 if (!(ct->status & statusbit))
224 return 1;
225
226 l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, inside->ip6.nexthdr);
227 if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6),
228 l4proto, &ct->tuplehash[!dir].tuple, !manip))
229 return 0;
230
231 if (skb->ip_summed != CHECKSUM_PARTIAL) {
232 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
233 inside = (void *)skb->data + hdrlen;
234 inside->icmp6.icmp6_cksum = 0;
235 inside->icmp6.icmp6_cksum =
236 csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
237 skb->len - hdrlen, IPPROTO_ICMPV6,
238 csum_partial(&inside->icmp6,
239 skb->len - hdrlen, 0));
240 }
241
242 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
243 l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, IPPROTO_ICMPV6);
244 if (!nf_nat_ipv6_manip_pkt(skb, 0, l4proto, &target, manip))
245 return 0;
246
247 return 1;
248}
249EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
250
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200251unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -0500252nf_nat_ipv6_fn(void *priv, struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400253 const struct nf_hook_state *state,
Eric W. Biederman06198b32015-09-18 14:33:06 -0500254 unsigned int (*do_chain)(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200255 struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400256 const struct nf_hook_state *state,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200257 struct nf_conn *ct))
258{
259 struct nf_conn *ct;
260 enum ip_conntrack_info ctinfo;
261 struct nf_conn_nat *nat;
Eric W. Biederman082a7582015-09-18 14:32:56 -0500262 enum nf_nat_manip_type maniptype = HOOK2MANIP(state->hook);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200263 __be16 frag_off;
264 int hdrlen;
265 u8 nexthdr;
266
267 ct = nf_ct_get(skb, &ctinfo);
268 /* Can't track? It's not due to stress, or conntrack would
269 * have dropped it. Hence it's the user's responsibilty to
270 * packet filter it out, or implement conntrack/NAT for that
271 * protocol. 8) --RR
272 */
273 if (!ct)
274 return NF_ACCEPT;
275
276 /* Don't try to NAT if this packet is not conntracked */
277 if (nf_ct_is_untracked(ct))
278 return NF_ACCEPT;
279
280 nat = nf_ct_nat_ext_add(ct);
281 if (nat == NULL)
282 return NF_ACCEPT;
283
284 switch (ctinfo) {
285 case IP_CT_RELATED:
286 case IP_CT_RELATED_REPLY:
287 nexthdr = ipv6_hdr(skb)->nexthdr;
288 hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
289 &nexthdr, &frag_off);
290
291 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
292 if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
Eric W. Biederman082a7582015-09-18 14:32:56 -0500293 state->hook,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200294 hdrlen))
295 return NF_DROP;
296 else
297 return NF_ACCEPT;
298 }
299 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
300 case IP_CT_NEW:
301 /* Seen it before? This can happen for loopback, retrans,
302 * or local packets.
303 */
304 if (!nf_nat_initialized(ct, maniptype)) {
305 unsigned int ret;
306
Eric W. Biederman06198b32015-09-18 14:33:06 -0500307 ret = do_chain(priv, skb, state, ct);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200308 if (ret != NF_ACCEPT)
309 return ret;
310
Eric W. Biederman082a7582015-09-18 14:32:56 -0500311 if (nf_nat_initialized(ct, HOOK2MANIP(state->hook)))
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200312 break;
313
Eric W. Biederman082a7582015-09-18 14:32:56 -0500314 ret = nf_nat_alloc_null_binding(ct, state->hook);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200315 if (ret != NF_ACCEPT)
316 return ret;
317 } else {
318 pr_debug("Already setup manip %s for ct %p\n",
319 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
320 ct);
Eric W. Biederman082a7582015-09-18 14:32:56 -0500321 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200322 goto oif_changed;
323 }
324 break;
325
326 default:
327 /* ESTABLISHED */
328 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
329 ctinfo == IP_CT_ESTABLISHED_REPLY);
Eric W. Biederman082a7582015-09-18 14:32:56 -0500330 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200331 goto oif_changed;
332 }
333
Eric W. Biederman082a7582015-09-18 14:32:56 -0500334 return nf_nat_packet(ct, ctinfo, state->hook, skb);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200335
336oif_changed:
337 nf_ct_kill_acct(ct, ctinfo, skb);
338 return NF_DROP;
339}
340EXPORT_SYMBOL_GPL(nf_nat_ipv6_fn);
341
342unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -0500343nf_nat_ipv6_in(void *priv, struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400344 const struct nf_hook_state *state,
Eric W. Biederman06198b32015-09-18 14:33:06 -0500345 unsigned int (*do_chain)(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200346 struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400347 const struct nf_hook_state *state,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200348 struct nf_conn *ct))
349{
350 unsigned int ret;
351 struct in6_addr daddr = ipv6_hdr(skb)->daddr;
352
Eric W. Biederman06198b32015-09-18 14:33:06 -0500353 ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200354 if (ret != NF_DROP && ret != NF_STOLEN &&
355 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
356 skb_dst_drop(skb);
357
358 return ret;
359}
360EXPORT_SYMBOL_GPL(nf_nat_ipv6_in);
361
362unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -0500363nf_nat_ipv6_out(void *priv, struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400364 const struct nf_hook_state *state,
Eric W. Biederman06198b32015-09-18 14:33:06 -0500365 unsigned int (*do_chain)(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200366 struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400367 const struct nf_hook_state *state,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200368 struct nf_conn *ct))
369{
370#ifdef CONFIG_XFRM
371 const struct nf_conn *ct;
372 enum ip_conntrack_info ctinfo;
373 int err;
374#endif
375 unsigned int ret;
376
377 /* root is playing with raw sockets. */
378 if (skb->len < sizeof(struct ipv6hdr))
379 return NF_ACCEPT;
380
Eric W. Biederman06198b32015-09-18 14:33:06 -0500381 ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200382#ifdef CONFIG_XFRM
383 if (ret != NF_DROP && ret != NF_STOLEN &&
384 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
385 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
386 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
387
388 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
389 &ct->tuplehash[!dir].tuple.dst.u3) ||
390 (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
391 ct->tuplehash[dir].tuple.src.u.all !=
392 ct->tuplehash[!dir].tuple.dst.u.all)) {
Eric W. Biedermanc7af6482015-09-18 14:33:07 -0500393 err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200394 if (err < 0)
395 ret = NF_DROP_ERR(err);
396 }
397 }
398#endif
399 return ret;
400}
401EXPORT_SYMBOL_GPL(nf_nat_ipv6_out);
402
403unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -0500404nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400405 const struct nf_hook_state *state,
Eric W. Biederman06198b32015-09-18 14:33:06 -0500406 unsigned int (*do_chain)(void *priv,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200407 struct sk_buff *skb,
David S. Miller8fe22382015-04-03 21:05:07 -0400408 const struct nf_hook_state *state,
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200409 struct nf_conn *ct))
410{
411 const struct nf_conn *ct;
412 enum ip_conntrack_info ctinfo;
413 unsigned int ret;
414 int err;
415
416 /* root is playing with raw sockets. */
417 if (skb->len < sizeof(struct ipv6hdr))
418 return NF_ACCEPT;
419
Eric W. Biederman06198b32015-09-18 14:33:06 -0500420 ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200421 if (ret != NF_DROP && ret != NF_STOLEN &&
422 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
423 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
424
425 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
426 &ct->tuplehash[!dir].tuple.src.u3)) {
Eric W. Biederman5f5d74d2015-09-25 15:07:31 -0500427 err = ip6_route_me_harder(state->net, skb);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200428 if (err < 0)
429 ret = NF_DROP_ERR(err);
430 }
431#ifdef CONFIG_XFRM
432 else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
433 ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
434 ct->tuplehash[dir].tuple.dst.u.all !=
435 ct->tuplehash[!dir].tuple.src.u.all) {
Eric W. Biedermanc7af6482015-09-18 14:33:07 -0500436 err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
Pablo Neira Ayuso2a5538e2014-08-25 12:05:27 +0200437 if (err < 0)
438 ret = NF_DROP_ERR(err);
439 }
440#endif
441 }
442 return ret;
443}
444EXPORT_SYMBOL_GPL(nf_nat_ipv6_local_fn);
445
Patrick McHardy58a317f2012-08-26 19:14:12 +0200446static int __init nf_nat_l3proto_ipv6_init(void)
447{
448 int err;
449
450 err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
451 if (err < 0)
452 goto err1;
453 err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv6);
454 if (err < 0)
455 goto err2;
456 return err;
457
458err2:
459 nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
460err1:
461 return err;
462}
463
464static void __exit nf_nat_l3proto_ipv6_exit(void)
465{
466 nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6);
467 nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
468}
469
470MODULE_LICENSE("GPL");
471MODULE_ALIAS("nf-nat-" __stringify(AF_INET6));
472
473module_init(nf_nat_l3proto_ipv6_init);
474module_exit(nf_nat_l3proto_ipv6_exit);