2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015-2019 Yandex LLC
5 * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/counter.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rmlock.h>
41 #include <sys/rwlock.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
46 #include <net/if_var.h>
47 #include <net/if_pflog.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_fib.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip_fw.h>
57 #include <netinet/ip6.h>
58 #include <netinet/icmp6.h>
59 #include <netinet/ip_icmp.h>
60 #include <netinet/tcp.h>
61 #include <netinet/udp.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/in6_fib.h>
64 #include <netinet6/ip6_var.h>
65 #include <netinet6/ip_fw_nat64.h>
67 #include <netpfil/pf/pf.h>
68 #include <netpfil/ipfw/ip_fw_private.h>
69 #include <machine/in_cksum.h>
71 #include "ip_fw_nat64.h"
72 #include "nat64_translate.h"
75 typedef int (*nat64_output_t)(struct ifnet *, struct mbuf *,
76 struct sockaddr *, struct nat64_counters *, void *);
77 typedef int (*nat64_output_one_t)(struct mbuf *, struct nat64_counters *,
80 static int nat64_find_route4(struct nhop4_basic *, struct sockaddr_in *,
82 static int nat64_find_route6(struct nhop6_basic *, struct sockaddr_in6 *,
84 static int nat64_output_one(struct mbuf *, struct nat64_counters *, void *);
85 static int nat64_output(struct ifnet *, struct mbuf *, struct sockaddr *,
86 struct nat64_counters *, void *);
87 static int nat64_direct_output_one(struct mbuf *, struct nat64_counters *,
89 static int nat64_direct_output(struct ifnet *, struct mbuf *,
90 struct sockaddr *, struct nat64_counters *, void *);
92 struct nat64_methods {
93 nat64_output_t output;
94 nat64_output_one_t output_one;
96 static const struct nat64_methods nat64_netisr = {
97 .output = nat64_output,
98 .output_one = nat64_output_one
100 static const struct nat64_methods nat64_direct = {
101 .output = nat64_direct_output,
102 .output_one = nat64_direct_output_one
104 static VNET_DEFINE(const struct nat64_methods *, nat64out) = &nat64_netisr;
105 #define V_nat64out VNET(nat64out)
108 nat64_set_output_method(int direct)
111 V_nat64out = direct != 0 ? &nat64_direct: &nat64_netisr;
115 nat64_get_output_method(void)
118 return (V_nat64out == &nat64_direct ? 1: 0);
122 nat64_log(struct pfloghdr *logdata, struct mbuf *m, sa_family_t family)
125 logdata->dir = PF_OUT;
126 logdata->af = family;
127 ipfw_bpf_mtap2(logdata, PFLOG_HDRLEN, m);
131 nat64_direct_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
132 struct nat64_counters *stats, void *logdata)
137 nat64_log(logdata, m, dst->sa_family);
138 error = (*ifp->if_output)(ifp, m, dst, NULL);
140 NAT64STAT_INC(stats, oerrors);
145 nat64_direct_output_one(struct mbuf *m, struct nat64_counters *stats,
148 struct nhop6_basic nh6;
149 struct nhop4_basic nh4;
150 struct sockaddr_in6 dst6;
151 struct sockaddr_in dst4;
152 struct sockaddr *dst;
158 ip4 = mtod(m, struct ip *);
161 dst4.sin_addr = ip4->ip_dst;
162 error = nat64_find_route4(&nh4, &dst4, m);
164 NAT64STAT_INC(stats, noroute4);
167 dst = (struct sockaddr *)&dst4;
170 case (IPV6_VERSION >> 4):
171 ip6 = mtod(m, struct ip6_hdr *);
172 dst6.sin6_addr = ip6->ip6_dst;
173 error = nat64_find_route6(&nh6, &dst6, m);
175 NAT64STAT_INC(stats, noroute6);
178 dst = (struct sockaddr *)&dst6;
183 NAT64STAT_INC(stats, dropped);
184 DPRINTF(DP_DROPS, "dropped due to unknown IP version");
185 return (EAFNOSUPPORT);
189 return (EHOSTUNREACH);
192 nat64_log(logdata, m, dst->sa_family);
193 error = (*ifp->if_output)(ifp, m, dst, NULL);
195 NAT64STAT_INC(stats, oerrors);
200 nat64_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
201 struct nat64_counters *stats, void *logdata)
206 ip4 = mtod(m, struct ip *);
212 case (IPV6_VERSION >> 4):
218 NAT64STAT_INC(stats, dropped);
219 DPRINTF(DP_DROPS, "unknown IP version");
220 return (EAFNOSUPPORT);
223 nat64_log(logdata, m, af);
224 if (m->m_pkthdr.rcvif == NULL)
225 m->m_pkthdr.rcvif = V_loif;
226 ret = netisr_queue(ret, m);
228 NAT64STAT_INC(stats, oerrors);
233 nat64_output_one(struct mbuf *m, struct nat64_counters *stats, void *logdata)
236 return (nat64_output(NULL, m, NULL, stats, logdata));
240 * Check the given IPv6 prefix and length according to RFC6052:
241 * The prefixes can only have one of the following lengths:
242 * 32, 40, 48, 56, 64, or 96 (The Well-Known Prefix is 96 bits long).
243 * Returns zero on success, otherwise EINVAL.
246 nat64_check_prefixlen(int length)
262 nat64_check_prefix6(const struct in6_addr *prefix, int length)
265 if (nat64_check_prefixlen(length) != 0)
268 /* Well-known prefix has 96 prefix length */
269 if (IN6_IS_ADDR_WKPFX(prefix) && length != 96)
272 /* Bits 64 to 71 must be set to zero */
273 if (prefix->__u6_addr.__u6_addr8[8] != 0)
276 /* Some extra checks */
277 if (IN6_IS_ADDR_MULTICAST(prefix) ||
278 IN6_IS_ADDR_UNSPECIFIED(prefix) ||
279 IN6_IS_ADDR_LOOPBACK(prefix))
285 nat64_check_private_ip4(const struct nat64_config *cfg, in_addr_t ia)
288 if (cfg->flags & NAT64_ALLOW_PRIVATE)
291 /* WKPFX must not be used to represent non-global IPv4 addresses */
292 if (cfg->flags & NAT64_WKPFX) {
294 if ((ia & htonl(0xff000000)) == htonl(0x0a000000) ||
295 (ia & htonl(0xfff00000)) == htonl(0xac100000) ||
296 (ia & htonl(0xffff0000)) == htonl(0xc0a80000))
300 * 192.0.0.0/24 - reserved for IETF protocol assignments
301 * 192.88.99.0/24 - for use as 6to4 relay anycast addresses
302 * 198.18.0.0/15 - for use in benchmark tests
303 * 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 - for use
304 * in documentation and example code
306 if ((ia & htonl(0xffffff00)) == htonl(0xc0000000) ||
307 (ia & htonl(0xffffff00)) == htonl(0xc0586300) ||
308 (ia & htonl(0xfffffe00)) == htonl(0xc6120000) ||
309 (ia & htonl(0xffffff00)) == htonl(0xc0000200) ||
310 (ia & htonl(0xfffffe00)) == htonl(0xc6336400) ||
311 (ia & htonl(0xffffff00)) == htonl(0xcb007100))
318 * Embed @ia IPv4 address into @ip6 IPv6 address.
319 * Place to embedding determined from prefix length @plen.
322 nat64_embed_ip4(struct in6_addr *ip6, int plen, in_addr_t ia)
328 ip6->s6_addr32[plen / 32] = ia;
334 * Preserve prefix bits.
335 * Since suffix bits should be zero and reserved for future
336 * use, we just overwrite the whole word, where they are.
338 ip6->s6_addr32[1] &= 0xffffffff << (32 - plen % 32);
339 #if BYTE_ORDER == BIG_ENDIAN
340 ip6->s6_addr32[1] |= ia >> (plen % 32);
341 ip6->s6_addr32[2] = ia << (24 - plen % 32);
342 #elif BYTE_ORDER == LITTLE_ENDIAN
343 ip6->s6_addr32[1] |= ia << (plen % 32);
344 ip6->s6_addr32[2] = ia >> (24 - plen % 32);
348 #if BYTE_ORDER == BIG_ENDIAN
349 ip6->s6_addr32[2] = ia >> 8;
350 ip6->s6_addr32[3] = ia << 24;
351 #elif BYTE_ORDER == LITTLE_ENDIAN
352 ip6->s6_addr32[2] = ia << 8;
353 ip6->s6_addr32[3] = ia >> 24;
357 panic("Wrong plen: %d", plen);
360 * Bits 64 to 71 of the address are reserved for compatibility
361 * with the host identifier format defined in the IPv6 addressing
362 * architecture [RFC4291]. These bits MUST be set to zero.
364 ip6->s6_addr8[8] = 0;
368 nat64_extract_ip4(const struct in6_addr *ip6, int plen)
373 * According to RFC 6052 p2.2:
374 * IPv4-embedded IPv6 addresses are composed of a variable-length
375 * prefix, the embedded IPv4 address, and a variable length suffix.
376 * The suffix bits are reserved for future extensions and SHOULD
381 if (ip6->s6_addr32[3] != 0 || ip6->s6_addr32[2] != 0)
385 if (ip6->s6_addr32[3] != 0 ||
386 (ip6->s6_addr32[2] & htonl(0xff00ffff)) != 0)
390 if (ip6->s6_addr32[3] != 0 ||
391 (ip6->s6_addr32[2] & htonl(0xff0000ff)) != 0)
395 if (ip6->s6_addr32[3] != 0 || ip6->s6_addr8[8] != 0)
399 if (ip6->s6_addr8[8] != 0 ||
400 (ip6->s6_addr32[3] & htonl(0x00ffffff)) != 0)
406 ia = ip6->s6_addr32[plen / 32];
411 #if BYTE_ORDER == BIG_ENDIAN
412 ia = (ip6->s6_addr32[1] << (plen % 32)) |
413 (ip6->s6_addr32[2] >> (24 - plen % 32));
414 #elif BYTE_ORDER == LITTLE_ENDIAN
415 ia = (ip6->s6_addr32[1] >> (plen % 32)) |
416 (ip6->s6_addr32[2] << (24 - plen % 32));
420 #if BYTE_ORDER == BIG_ENDIAN
421 ia = (ip6->s6_addr32[2] << 8) | (ip6->s6_addr32[3] >> 24);
422 #elif BYTE_ORDER == LITTLE_ENDIAN
423 ia = (ip6->s6_addr32[2] >> 8) | (ip6->s6_addr32[3] << 24);
429 if (nat64_check_ip4(ia) == 0)
432 DPRINTF(DP_GENERIC | DP_DROPS,
433 "invalid destination address: %08x", ia);
436 DPRINTF(DP_GENERIC | DP_DROPS, "invalid IPv4-embedded IPv6 address");
441 * According to RFC 1624 the equation for incremental checksum update is:
442 * HC' = ~(~HC + ~m + m') -- [Eqn. 3]
443 * HC' = HC - ~m - m' -- [Eqn. 4]
444 * So, when we are replacing IPv4 addresses to IPv6, we
445 * can assume, that new bytes previously were zeros, and vise versa -
446 * when we replacing IPv6 addresses to IPv4, now unused bytes become
447 * zeros. The payload length in pseudo header has bigger size, but one
448 * half of it should be zero. Using the equation 4 we get:
449 * HC' = HC - (~m0 + m0') -- m0 is first changed word
450 * HC' = (HC - (~m0 + m0')) - (~m1 + m1') -- m1 is second changed word
451 * HC' = HC - ~m0 - m0' - ~m1 - m1' - ... =
452 * = HC - sum(~m[i] + m'[i])
454 * The function result should be used as follows:
455 * IPv6 to IPv4: HC' = cksum_add(HC, result)
456 * IPv4 to IPv6: HC' = cksum_add(HC, ~result)
459 nat64_cksum_convert(struct ip6_hdr *ip6, struct ip *ip)
464 sum = ~ip->ip_src.s_addr >> 16;
465 sum += ~ip->ip_src.s_addr & 0xffff;
466 sum += ~ip->ip_dst.s_addr >> 16;
467 sum += ~ip->ip_dst.s_addr & 0xffff;
469 for (p = (uint16_t *)&ip6->ip6_src;
470 p < (uint16_t *)(&ip6->ip6_src + 2); p++)
474 sum = (sum & 0xffff) + (sum >> 16);
479 nat64_init_ip4hdr(const struct ip6_hdr *ip6, const struct ip6_frag *frag,
480 uint16_t plen, uint8_t proto, struct ip *ip)
483 /* assume addresses are already initialized */
484 ip->ip_v = IPVERSION;
485 ip->ip_hl = sizeof(*ip) >> 2;
486 ip->ip_tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
487 ip->ip_len = htons(sizeof(*ip) + plen);
488 ip->ip_ttl = ip6->ip6_hlim;
489 /* Forwarding code will decrement TTL for netisr based output. */
490 if (V_nat64out == &nat64_direct)
491 ip->ip_ttl -= IPV6_HLIMDEC;
493 ip->ip_p = (proto == IPPROTO_ICMPV6) ? IPPROTO_ICMP: proto;
496 ip->ip_off = htons(ntohs(frag->ip6f_offlg) >> 3);
497 if (frag->ip6f_offlg & IP6F_MORE_FRAG)
498 ip->ip_off |= htons(IP_MF);
500 ip->ip_off = htons(IP_DF);
502 ip->ip_sum = in_cksum_hdr(ip);
505 #define FRAGSZ(mtu) ((mtu) - sizeof(struct ip6_hdr) - sizeof(struct ip6_frag))
506 static NAT64NOINLINE int
507 nat64_fragment6(struct nat64_counters *stats, struct ip6_hdr *ip6,
508 struct mbufq *mq, struct mbuf *m, uint32_t mtu, uint16_t ip_id,
511 struct ip6_frag ip6f;
513 uint16_t hlen, len, offset;
516 plen = ntohs(ip6->ip6_plen);
517 hlen = sizeof(struct ip6_hdr);
519 /* Fragmentation isn't needed */
520 if (ip_off == 0 && plen <= mtu - hlen) {
521 M_PREPEND(m, hlen, M_NOWAIT);
523 NAT64STAT_INC(stats, nomem);
526 bcopy(ip6, mtod(m, void *), hlen);
527 if (mbufq_enqueue(mq, m) != 0) {
529 NAT64STAT_INC(stats, dropped);
530 DPRINTF(DP_DROPS, "dropped due to mbufq overflow");
536 hlen += sizeof(struct ip6_frag);
537 ip6f.ip6f_reserved = 0;
538 ip6f.ip6f_nxt = ip6->ip6_nxt;
539 ip6->ip6_nxt = IPPROTO_FRAGMENT;
542 * We have got an IPv4 fragment.
543 * Use offset value and ip_id from original fragment.
545 ip6f.ip6f_ident = htonl(ntohs(ip_id));
546 offset = (ntohs(ip_off) & IP_OFFMASK) << 3;
547 NAT64STAT_INC(stats, ifrags);
549 /* The packet size exceeds interface MTU */
550 ip6f.ip6f_ident = htonl(ip6_randomid());
551 offset = 0; /* First fragment*/
553 while (plen > 0 && m != NULL) {
555 len = FRAGSZ(mtu) & ~7;
558 ip6->ip6_plen = htons(len + sizeof(ip6f));
559 ip6f.ip6f_offlg = ntohs(offset);
560 if (len < plen || (ip_off & htons(IP_MF)) != 0)
561 ip6f.ip6f_offlg |= IP6F_MORE_FRAG;
565 n = m_split(m, len, M_NOWAIT);
569 M_PREPEND(m, hlen, M_NOWAIT);
572 bcopy(ip6, mtod(m, void *), sizeof(struct ip6_hdr));
573 bcopy(&ip6f, mtodo(m, sizeof(struct ip6_hdr)),
574 sizeof(struct ip6_frag));
575 if (mbufq_enqueue(mq, m) != 0)
579 NAT64STAT_ADD(stats, ofrags, mbufq_len(mq));
587 NAT64STAT_INC(stats, nomem);
591 static NAT64NOINLINE int
592 nat64_find_route6(struct nhop6_basic *pnh, struct sockaddr_in6 *dst,
596 if (fib6_lookup_nh_basic(M_GETFIB(m), &dst->sin6_addr, 0, 0, 0,
598 return (EHOSTUNREACH);
599 if (pnh->nh_flags & (NHF_BLACKHOLE | NHF_REJECT))
600 return (EHOSTUNREACH);
602 * XXX: we need to use destination address with embedded scope
603 * zone id, because LLTABLE uses such form of addresses for lookup.
605 dst->sin6_family = AF_INET6;
606 dst->sin6_len = sizeof(*dst);
607 dst->sin6_addr = pnh->nh_addr;
608 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
609 dst->sin6_addr.s6_addr16[1] =
610 htons(pnh->nh_ifp->if_index & 0xffff);
612 dst->sin6_scope_id = 0;
613 dst->sin6_flowinfo = 0;
618 #define NAT64_ICMP6_PLEN 64
619 static NAT64NOINLINE void
620 nat64_icmp6_reflect(struct mbuf *m, uint8_t type, uint8_t code, uint32_t mtu,
621 struct nat64_counters *stats, void *logdata)
623 struct icmp6_hdr *icmp6;
624 struct ip6_hdr *ip6, *oip6;
629 plen = nat64_getlasthdr(m, &len);
631 DPRINTF(DP_DROPS, "mbuf isn't contigious");
635 * Do not send ICMPv6 in reply to ICMPv6 errors.
637 if (plen == IPPROTO_ICMPV6) {
638 if (m->m_len < len + sizeof(*icmp6)) {
639 DPRINTF(DP_DROPS, "mbuf isn't contigious");
642 icmp6 = mtodo(m, len);
643 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST ||
644 icmp6->icmp6_type == ND_REDIRECT) {
645 DPRINTF(DP_DROPS, "do not send ICMPv6 in reply to "
651 if (icmp6_ratelimit(&ip6->ip6_src, type, code))
654 ip6 = mtod(m, struct ip6_hdr *);
656 case ICMP6_DST_UNREACH:
657 case ICMP6_PACKET_TOO_BIG:
658 case ICMP6_TIME_EXCEEDED:
659 case ICMP6_PARAM_PROB:
664 /* Calculate length of ICMPv6 payload */
665 len = (m->m_pkthdr.len > NAT64_ICMP6_PLEN) ? NAT64_ICMP6_PLEN:
668 /* Create new ICMPv6 datagram */
669 plen = len + sizeof(struct icmp6_hdr);
670 n = m_get2(sizeof(struct ip6_hdr) + plen + max_hdr, M_NOWAIT,
671 MT_HEADER, M_PKTHDR);
673 NAT64STAT_INC(stats, nomem);
678 * Move pkthdr from original mbuf. We should have initialized some
679 * fields, because we can reinject this mbuf to netisr and it will
680 * go trough input path (it requires at least rcvif should be set).
681 * Also do M_ALIGN() to reduce chances of need to allocate new mbuf
682 * in the chain, when we will do M_PREPEND() or make some type of
686 M_ALIGN(n, sizeof(struct ip6_hdr) + plen + max_hdr);
688 n->m_len = n->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
689 oip6 = mtod(n, struct ip6_hdr *);
690 oip6->ip6_src = ip6->ip6_dst;
691 oip6->ip6_dst = ip6->ip6_src;
692 oip6->ip6_nxt = IPPROTO_ICMPV6;
694 oip6->ip6_vfc |= IPV6_VERSION;
695 oip6->ip6_hlim = V_ip6_defhlim;
696 oip6->ip6_plen = htons(plen);
698 icmp6 = mtodo(n, sizeof(struct ip6_hdr));
699 icmp6->icmp6_cksum = 0;
700 icmp6->icmp6_type = type;
701 icmp6->icmp6_code = code;
702 icmp6->icmp6_mtu = htonl(mtu);
704 m_copydata(m, 0, len, mtodo(n, sizeof(struct ip6_hdr) +
705 sizeof(struct icmp6_hdr)));
706 icmp6->icmp6_cksum = in6_cksum(n, IPPROTO_ICMPV6,
707 sizeof(struct ip6_hdr), plen);
709 V_nat64out->output_one(n, stats, logdata);
712 NAT64STAT_INC(stats, dropped);
716 static NAT64NOINLINE int
717 nat64_find_route4(struct nhop4_basic *pnh, struct sockaddr_in *dst,
721 if (fib4_lookup_nh_basic(M_GETFIB(m), dst->sin_addr, 0, 0, pnh) != 0)
722 return (EHOSTUNREACH);
723 if (pnh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST | NHF_REJECT))
724 return (EHOSTUNREACH);
726 dst->sin_family = AF_INET;
727 dst->sin_len = sizeof(*dst);
728 dst->sin_addr = pnh->nh_addr;
733 #define NAT64_ICMP_PLEN 64
734 static NAT64NOINLINE void
735 nat64_icmp_reflect(struct mbuf *m, uint8_t type,
736 uint8_t code, uint16_t mtu, struct nat64_counters *stats, void *logdata)
743 ip = mtod(m, struct ip *);
744 /* Do not send ICMP error if packet is not the first fragment */
745 if (ip->ip_off & ~ntohs(IP_MF|IP_DF)) {
746 DPRINTF(DP_DROPS, "not first fragment");
749 /* Do not send ICMP in reply to ICMP errors */
750 if (ip->ip_p == IPPROTO_ICMP) {
751 if (m->m_len < (ip->ip_hl << 2)) {
752 DPRINTF(DP_DROPS, "mbuf isn't contigious");
755 icmp = mtodo(m, ip->ip_hl << 2);
756 if (!ICMP_INFOTYPE(icmp->icmp_type)) {
757 DPRINTF(DP_DROPS, "do not send ICMP in reply to "
770 /* Calculate length of ICMP payload */
771 len = (m->m_pkthdr.len > NAT64_ICMP_PLEN) ? (ip->ip_hl << 2) + 8:
774 /* Create new ICMPv4 datagram */
775 plen = len + sizeof(struct icmphdr) + sizeof(uint32_t);
776 n = m_get2(sizeof(struct ip) + plen + max_hdr, M_NOWAIT,
777 MT_HEADER, M_PKTHDR);
779 NAT64STAT_INC(stats, nomem);
784 M_ALIGN(n, sizeof(struct ip) + plen + max_hdr);
786 n->m_len = n->m_pkthdr.len = sizeof(struct ip) + plen;
787 oip = mtod(n, struct ip *);
788 oip->ip_v = IPVERSION;
789 oip->ip_hl = sizeof(struct ip) >> 2;
791 oip->ip_len = htons(n->m_pkthdr.len);
792 oip->ip_ttl = V_ip_defttl;
793 oip->ip_p = IPPROTO_ICMP;
795 oip->ip_off = htons(IP_DF);
796 oip->ip_src = ip->ip_dst;
797 oip->ip_dst = ip->ip_src;
799 oip->ip_sum = in_cksum_hdr(oip);
801 icmp = mtodo(n, sizeof(struct ip));
802 icmp->icmp_type = type;
803 icmp->icmp_code = code;
804 icmp->icmp_cksum = 0;
805 icmp->icmp_pmvoid = 0;
806 icmp->icmp_nextmtu = htons(mtu);
807 m_copydata(m, 0, len, mtodo(n, sizeof(struct ip) +
808 sizeof(struct icmphdr) + sizeof(uint32_t)));
809 icmp->icmp_cksum = in_cksum_skip(n, sizeof(struct ip) + plen,
812 V_nat64out->output_one(n, stats, logdata);
815 NAT64STAT_INC(stats, dropped);
819 /* Translate ICMP echo request/reply into ICMPv6 */
821 nat64_icmp_handle_echo(struct ip6_hdr *ip6, struct icmp6_hdr *icmp6,
822 uint16_t id, uint8_t type)
826 old = *(uint16_t *)icmp6; /* save type+code in one word */
827 icmp6->icmp6_type = type;
828 /* Reflect ICMPv6 -> ICMPv4 type translation in the cksum */
829 icmp6->icmp6_cksum = cksum_adjust(icmp6->icmp6_cksum,
830 old, *(uint16_t *)icmp6);
832 old = icmp6->icmp6_id;
833 icmp6->icmp6_id = id;
834 /* Reflect ICMP id translation in the cksum */
835 icmp6->icmp6_cksum = cksum_adjust(icmp6->icmp6_cksum,
838 /* Reflect IPv6 pseudo header in the cksum */
839 icmp6->icmp6_cksum = ~in6_cksum_pseudo(ip6, ntohs(ip6->ip6_plen),
840 IPPROTO_ICMPV6, ~icmp6->icmp6_cksum);
843 static NAT64NOINLINE struct mbuf *
844 nat64_icmp_translate(struct mbuf *m, struct ip6_hdr *ip6, uint16_t icmpid,
845 int offset, struct nat64_config *cfg)
851 struct ip6_hdr *eip6;
857 if (m->m_len < offset + ICMP_MINLEN)
858 m = m_pullup(m, offset + ICMP_MINLEN);
860 NAT64STAT_INC(&cfg->stats, nomem);
864 icmp = mtodo(m, offset);
866 switch (icmp->icmp_type) {
868 type = ICMP6_ECHO_REPLY;
872 type = ICMP6_DST_UNREACH;
873 switch (icmp->icmp_code) {
874 case ICMP_UNREACH_NET:
875 case ICMP_UNREACH_HOST:
876 case ICMP_UNREACH_SRCFAIL:
877 case ICMP_UNREACH_NET_UNKNOWN:
878 case ICMP_UNREACH_HOST_UNKNOWN:
879 case ICMP_UNREACH_TOSNET:
880 case ICMP_UNREACH_TOSHOST:
881 code = ICMP6_DST_UNREACH_NOROUTE;
883 case ICMP_UNREACH_PROTOCOL:
884 type = ICMP6_PARAM_PROB;
885 code = ICMP6_PARAMPROB_NEXTHEADER;
887 case ICMP_UNREACH_PORT:
888 code = ICMP6_DST_UNREACH_NOPORT;
890 case ICMP_UNREACH_NEEDFRAG:
891 type = ICMP6_PACKET_TOO_BIG;
893 /* XXX: needs an additional look */
894 mtu = max(IPV6_MMTU, ntohs(icmp->icmp_nextmtu) + 20);
896 case ICMP_UNREACH_NET_PROHIB:
897 case ICMP_UNREACH_HOST_PROHIB:
898 case ICMP_UNREACH_FILTER_PROHIB:
899 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
900 code = ICMP6_DST_UNREACH_ADMIN;
903 DPRINTF(DP_DROPS, "Unsupported ICMP type %d, code %d",
904 icmp->icmp_type, icmp->icmp_code);
909 type = ICMP6_TIME_EXCEEDED;
910 code = icmp->icmp_code;
913 type = ICMP6_ECHO_REQUEST;
917 type = ICMP6_PARAM_PROB;
918 switch (icmp->icmp_code) {
919 case ICMP_PARAMPROB_ERRATPTR:
920 case ICMP_PARAMPROB_LENGTH:
921 code = ICMP6_PARAMPROB_HEADER;
922 switch (icmp->icmp_pptr) {
923 case 0: /* Version/IHL */
924 case 1: /* Type Of Service */
925 mtu = icmp->icmp_pptr;
927 case 2: /* Total Length */
928 case 3: mtu = 4; /* Payload Length */
930 case 8: /* Time to Live */
931 mtu = 7; /* Hop Limit */
933 case 9: /* Protocol */
934 mtu = 6; /* Next Header */
936 case 12: /* Source address */
942 case 16: /* Destination address */
948 default: /* Silently drop */
949 DPRINTF(DP_DROPS, "Unsupported ICMP type %d,"
950 " code %d, pptr %d", icmp->icmp_type,
951 icmp->icmp_code, icmp->icmp_pptr);
956 DPRINTF(DP_DROPS, "Unsupported ICMP type %d,"
957 " code %d, pptr %d", icmp->icmp_type,
958 icmp->icmp_code, icmp->icmp_pptr);
963 DPRINTF(DP_DROPS, "Unsupported ICMP type %d, code %d",
964 icmp->icmp_type, icmp->icmp_code);
968 * For echo request/reply we can use original payload,
969 * but we need adjust icmp_cksum, because ICMPv6 cksum covers
970 * IPv6 pseudo header and ICMPv6 types differs from ICMPv4.
972 if (type == ICMP6_ECHO_REQUEST || type == ICMP6_ECHO_REPLY) {
973 nat64_icmp_handle_echo(ip6, ICMP6(icmp), icmpid, type);
977 * For other types of ICMP messages we need to translate inner
978 * IPv4 header to IPv6 header.
979 * Assume ICMP src is the same as payload dst
980 * E.g. we have ( GWsrc1 , NATIP1 ) in outer header
981 * and ( NATIP1, Hostdst1 ) in ICMP copy header.
982 * In that case, we already have map for NATIP1 and GWsrc1.
983 * The only thing we need is to copy IPv6 map prefix to
986 hlen = offset + ICMP_MINLEN;
987 if (m->m_pkthdr.len < hlen + sizeof(struct ip) + ICMP_MINLEN) {
988 DPRINTF(DP_DROPS, "Message is too short %d",
992 m_copydata(m, hlen, sizeof(struct ip), (char *)&ip);
993 if (ip.ip_v != IPVERSION) {
994 DPRINTF(DP_DROPS, "Wrong IP version %d", ip.ip_v);
997 hlen += ip.ip_hl << 2; /* Skip inner IP header */
998 if (nat64_check_ip4(ip.ip_src.s_addr) != 0 ||
999 nat64_check_ip4(ip.ip_dst.s_addr) != 0 ||
1000 nat64_check_private_ip4(cfg, ip.ip_src.s_addr) != 0 ||
1001 nat64_check_private_ip4(cfg, ip.ip_dst.s_addr) != 0) {
1002 DPRINTF(DP_DROPS, "IP addresses checks failed %04x -> %04x",
1003 ntohl(ip.ip_src.s_addr), ntohl(ip.ip_dst.s_addr));
1006 if (m->m_pkthdr.len < hlen + ICMP_MINLEN) {
1007 DPRINTF(DP_DROPS, "Message is too short %d",
1013 * Check that inner source matches the outer destination.
1014 * XXX: We need some method to convert IPv4 into IPv6 address here,
1015 * and compare IPv6 addresses.
1017 if (ip.ip_src.s_addr != nat64_get_ip4(&ip6->ip6_dst)) {
1018 DPRINTF(DP_GENERIC, "Inner source doesn't match destination ",
1019 "%04x vs %04x", ip.ip_src.s_addr,
1020 nat64_get_ip4(&ip6->ip6_dst));
1025 * Create new mbuf for ICMPv6 datagram.
1026 * NOTE: len is data length just after inner IP header.
1028 len = m->m_pkthdr.len - hlen;
1029 if (sizeof(struct ip6_hdr) +
1030 sizeof(struct icmp6_hdr) + len > NAT64_ICMP6_PLEN)
1031 len = NAT64_ICMP6_PLEN - sizeof(struct icmp6_hdr) -
1032 sizeof(struct ip6_hdr);
1033 plen = sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr) + len;
1034 n = m_get2(offset + plen + max_hdr, M_NOWAIT, MT_HEADER, M_PKTHDR);
1036 NAT64STAT_INC(&cfg->stats, nomem);
1040 m_move_pkthdr(n, m);
1041 M_ALIGN(n, offset + plen + max_hdr);
1042 n->m_len = n->m_pkthdr.len = offset + plen;
1043 /* Adjust ip6_plen in outer header */
1044 ip6->ip6_plen = htons(plen);
1045 /* Construct new inner IPv6 header */
1046 eip6 = mtodo(n, offset + sizeof(struct icmp6_hdr));
1047 eip6->ip6_src = ip6->ip6_dst;
1049 /* Use the same prefix that we have in outer header */
1050 eip6->ip6_dst = ip6->ip6_src;
1051 MPASS(cfg->flags & NAT64_PLATPFX);
1052 nat64_embed_ip4(&eip6->ip6_dst, cfg->plat_plen, ip.ip_dst.s_addr);
1054 eip6->ip6_flow = htonl(ip.ip_tos << 20);
1055 eip6->ip6_vfc |= IPV6_VERSION;
1056 eip6->ip6_hlim = ip.ip_ttl;
1057 eip6->ip6_plen = htons(ntohs(ip.ip_len) - (ip.ip_hl << 2));
1058 eip6->ip6_nxt = (ip.ip_p == IPPROTO_ICMP) ? IPPROTO_ICMPV6: ip.ip_p;
1059 m_copydata(m, hlen, len, (char *)(eip6 + 1));
1061 * We need to translate source port in the inner ULP header,
1062 * and adjust ULP checksum.
1066 if (len < offsetof(struct tcphdr, th_sum))
1068 tcp = TCP(eip6 + 1);
1070 tcp->th_sum = cksum_adjust(tcp->th_sum,
1071 tcp->th_sport, icmpid);
1072 tcp->th_sport = icmpid;
1074 tcp->th_sum = cksum_add(tcp->th_sum,
1075 ~nat64_cksum_convert(eip6, &ip));
1078 if (len < offsetof(struct udphdr, uh_sum))
1080 udp = UDP(eip6 + 1);
1082 udp->uh_sum = cksum_adjust(udp->uh_sum,
1083 udp->uh_sport, icmpid);
1084 udp->uh_sport = icmpid;
1086 udp->uh_sum = cksum_add(udp->uh_sum,
1087 ~nat64_cksum_convert(eip6, &ip));
1091 * Check if this is an ICMP error message for echo request
1092 * that we sent. I.e. ULP in the data containing invoking
1093 * packet is IPPROTO_ICMP and its type is ICMP_ECHO.
1095 icmp = (struct icmp *)(eip6 + 1);
1096 if (icmp->icmp_type != ICMP_ECHO) {
1101 * For our client this original datagram should looks
1102 * like it was ICMPv6 datagram with type ICMP6_ECHO_REQUEST.
1103 * Thus we need adjust icmp_cksum and convert type from
1104 * ICMP_ECHO to ICMP6_ECHO_REQUEST.
1106 nat64_icmp_handle_echo(eip6, ICMP6(icmp), icmpid,
1107 ICMP6_ECHO_REQUEST);
1110 /* Convert ICMPv4 into ICMPv6 header */
1111 icmp = mtodo(n, offset);
1112 ICMP6(icmp)->icmp6_type = type;
1113 ICMP6(icmp)->icmp6_code = code;
1114 ICMP6(icmp)->icmp6_mtu = htonl(mtu);
1115 ICMP6(icmp)->icmp6_cksum = 0;
1116 ICMP6(icmp)->icmp6_cksum = cksum_add(
1117 ~in6_cksum_pseudo(ip6, plen, IPPROTO_ICMPV6, 0),
1118 in_cksum_skip(n, n->m_pkthdr.len, offset));
1122 NAT64STAT_INC(&cfg->stats, dropped);
1127 nat64_getlasthdr(struct mbuf *m, int *offset)
1129 struct ip6_hdr *ip6;
1130 struct ip6_hbh *hbh;
1138 if (m->m_len < hlen + sizeof(*ip6))
1141 ip6 = mtodo(m, hlen);
1142 hlen += sizeof(*ip6);
1143 proto = ip6->ip6_nxt;
1144 /* Skip extension headers */
1145 while (proto == IPPROTO_HOPOPTS || proto == IPPROTO_ROUTING ||
1146 proto == IPPROTO_DSTOPTS) {
1147 hbh = mtodo(m, hlen);
1149 * We expect mbuf has contigious data up to
1150 * upper level header.
1152 if (m->m_len < hlen)
1155 * We doesn't support Jumbo payload option,
1158 if (proto == IPPROTO_HOPOPTS && ip6->ip6_plen == 0)
1160 proto = hbh->ip6h_nxt;
1161 hlen += (hbh->ip6h_len + 1) << 3;
1169 nat64_do_handle_ip4(struct mbuf *m, struct in6_addr *saddr,
1170 struct in6_addr *daddr, uint16_t lport, struct nat64_config *cfg,
1173 struct nhop6_basic nh;
1175 struct sockaddr_in6 dst;
1178 uint16_t ip_id, ip_off;
1183 ip = mtod(m, struct ip*);
1185 if (ip->ip_ttl <= IPTTLDEC) {
1186 nat64_icmp_reflect(m, ICMP_TIMXCEED,
1187 ICMP_TIMXCEED_INTRANS, 0, &cfg->stats, logdata);
1188 return (NAT64RETURN);
1191 ip6.ip6_dst = *daddr;
1192 ip6.ip6_src = *saddr;
1194 hlen = ip->ip_hl << 2;
1195 plen = ntohs(ip->ip_len) - hlen;
1198 /* Save ip_id and ip_off, both are in network byte order */
1200 ip_off = ip->ip_off & htons(IP_OFFMASK | IP_MF);
1202 /* Fragment length must be multiple of 8 octets */
1203 if ((ip->ip_off & htons(IP_MF)) != 0 && (plen & 0x7) != 0) {
1204 nat64_icmp_reflect(m, ICMP_PARAMPROB,
1205 ICMP_PARAMPROB_LENGTH, 0, &cfg->stats, logdata);
1206 return (NAT64RETURN);
1208 /* Fragmented ICMP is unsupported */
1209 if (proto == IPPROTO_ICMP && ip_off != 0) {
1210 DPRINTF(DP_DROPS, "dropped due to fragmented ICMP");
1211 NAT64STAT_INC(&cfg->stats, dropped);
1212 return (NAT64MFREE);
1215 dst.sin6_addr = ip6.ip6_dst;
1216 if (nat64_find_route6(&nh, &dst, m) != 0) {
1217 NAT64STAT_INC(&cfg->stats, noroute6);
1218 nat64_icmp_reflect(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0,
1219 &cfg->stats, logdata);
1220 return (NAT64RETURN);
1222 if (nh.nh_mtu < plen + sizeof(ip6) &&
1223 (ip->ip_off & htons(IP_DF)) != 0) {
1224 nat64_icmp_reflect(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
1225 FRAGSZ(nh.nh_mtu) + sizeof(struct ip), &cfg->stats, logdata);
1226 return (NAT64RETURN);
1229 ip6.ip6_flow = htonl(ip->ip_tos << 20);
1230 ip6.ip6_vfc |= IPV6_VERSION;
1231 ip6.ip6_hlim = ip->ip_ttl;
1232 /* Forwarding code will decrement TTL for netisr based output. */
1233 if (V_nat64out == &nat64_direct)
1234 ip6.ip6_hlim -= IPTTLDEC;
1235 ip6.ip6_plen = htons(plen);
1236 ip6.ip6_nxt = (proto == IPPROTO_ICMP) ? IPPROTO_ICMPV6: proto;
1237 /* Convert checksums. */
1240 csum = &TCP(mtodo(m, hlen))->th_sum;
1242 struct tcphdr *tcp = TCP(mtodo(m, hlen));
1243 *csum = cksum_adjust(*csum, tcp->th_dport, lport);
1244 tcp->th_dport = lport;
1246 *csum = cksum_add(*csum, ~nat64_cksum_convert(&ip6, ip));
1249 csum = &UDP(mtodo(m, hlen))->uh_sum;
1251 struct udphdr *udp = UDP(mtodo(m, hlen));
1252 *csum = cksum_adjust(*csum, udp->uh_dport, lport);
1253 udp->uh_dport = lport;
1255 *csum = cksum_add(*csum, ~nat64_cksum_convert(&ip6, ip));
1258 m = nat64_icmp_translate(m, &ip6, lport, hlen, cfg);
1259 if (m == NULL) /* stats already accounted */
1260 return (NAT64RETURN);
1264 mbufq_init(&mq, 255);
1265 nat64_fragment6(&cfg->stats, &ip6, &mq, m, nh.nh_mtu, ip_id, ip_off);
1266 while ((m = mbufq_dequeue(&mq)) != NULL) {
1267 if (V_nat64out->output(nh.nh_ifp, m, (struct sockaddr *)&dst,
1268 &cfg->stats, logdata) != 0)
1270 NAT64STAT_INC(&cfg->stats, opcnt46);
1273 return (NAT64RETURN);
1277 nat64_handle_icmp6(struct mbuf *m, int hlen, uint32_t aaddr, uint16_t aport,
1278 struct nat64_config *cfg, void *logdata)
1281 struct icmp6_hdr *icmp6;
1282 struct ip6_frag *ip6f;
1283 struct ip6_hdr *ip6, *ip6i;
1289 ip6 = mtod(m, struct ip6_hdr *);
1290 if (nat64_check_ip6(&ip6->ip6_src) != 0 ||
1291 nat64_check_ip6(&ip6->ip6_dst) != 0)
1294 proto = nat64_getlasthdr(m, &hlen);
1295 if (proto != IPPROTO_ICMPV6) {
1297 "dropped due to mbuf isn't contigious");
1298 NAT64STAT_INC(&cfg->stats, dropped);
1299 return (NAT64MFREE);
1304 * Translate ICMPv6 type and code to ICMPv4 (RFC7915).
1305 * NOTE: ICMPv6 echo handled by nat64_do_handle_ip6().
1307 icmp6 = mtodo(m, hlen);
1309 switch (icmp6->icmp6_type) {
1310 case ICMP6_DST_UNREACH:
1311 type = ICMP_UNREACH;
1312 switch (icmp6->icmp6_code) {
1313 case ICMP6_DST_UNREACH_NOROUTE:
1314 case ICMP6_DST_UNREACH_BEYONDSCOPE:
1315 case ICMP6_DST_UNREACH_ADDR:
1316 code = ICMP_UNREACH_HOST;
1318 case ICMP6_DST_UNREACH_ADMIN:
1319 code = ICMP_UNREACH_HOST_PROHIB;
1321 case ICMP6_DST_UNREACH_NOPORT:
1322 code = ICMP_UNREACH_PORT;
1325 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1326 " code %d", icmp6->icmp6_type,
1328 NAT64STAT_INC(&cfg->stats, dropped);
1329 return (NAT64MFREE);
1332 case ICMP6_PACKET_TOO_BIG:
1333 type = ICMP_UNREACH;
1334 code = ICMP_UNREACH_NEEDFRAG;
1335 mtu = ntohl(icmp6->icmp6_mtu);
1336 if (mtu < IPV6_MMTU) {
1337 DPRINTF(DP_DROPS, "Wrong MTU %d in ICMPv6 type %d,"
1338 " code %d", mtu, icmp6->icmp6_type,
1340 NAT64STAT_INC(&cfg->stats, dropped);
1341 return (NAT64MFREE);
1344 * Adjust MTU to reflect difference between
1345 * IPv6 an IPv4 headers.
1347 mtu -= sizeof(struct ip6_hdr) - sizeof(struct ip);
1349 case ICMP6_TIME_EXCEEDED:
1350 type = ICMP_TIMXCEED;
1351 code = icmp6->icmp6_code;
1353 case ICMP6_PARAM_PROB:
1354 switch (icmp6->icmp6_code) {
1355 case ICMP6_PARAMPROB_HEADER:
1356 type = ICMP_PARAMPROB;
1357 code = ICMP_PARAMPROB_ERRATPTR;
1358 mtu = ntohl(icmp6->icmp6_pptr);
1360 case 0: /* Version/Traffic Class */
1361 case 1: /* Traffic Class/Flow Label */
1363 case 4: /* Payload Length */
1367 case 6: /* Next Header */
1370 case 7: /* Hop Limit */
1374 if (mtu >= 8 && mtu <= 23) {
1375 mtu = 12; /* Source address */
1378 if (mtu >= 24 && mtu <= 39) {
1379 mtu = 16; /* Destination address */
1382 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1383 " code %d, pptr %d", icmp6->icmp6_type,
1384 icmp6->icmp6_code, mtu);
1385 NAT64STAT_INC(&cfg->stats, dropped);
1386 return (NAT64MFREE);
1388 case ICMP6_PARAMPROB_NEXTHEADER:
1389 type = ICMP_UNREACH;
1390 code = ICMP_UNREACH_PROTOCOL;
1393 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1394 " code %d, pptr %d", icmp6->icmp6_type,
1395 icmp6->icmp6_code, ntohl(icmp6->icmp6_pptr));
1396 NAT64STAT_INC(&cfg->stats, dropped);
1397 return (NAT64MFREE);
1401 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d, code %d",
1402 icmp6->icmp6_type, icmp6->icmp6_code);
1403 NAT64STAT_INC(&cfg->stats, dropped);
1404 return (NAT64MFREE);
1407 hlen += sizeof(struct icmp6_hdr);
1408 if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
1409 NAT64STAT_INC(&cfg->stats, dropped);
1410 DPRINTF(DP_DROPS, "Message is too short %d",
1412 return (NAT64MFREE);
1415 * We need at least ICMP_MINLEN bytes of original datagram payload
1416 * to generate ICMP message. It is nice that ICMP_MINLEN is equal
1417 * to sizeof(struct ip6_frag). So, if embedded datagram had a fragment
1418 * header we will not have to do m_pullup() again.
1420 * What we have here:
1421 * Outer header: (IPv6iGW, v4mapPRefix+v4exthost)
1422 * Inner header: (v4mapPRefix+v4host, IPv6iHost) [sport, dport]
1423 * We need to translate it to:
1425 * Outer header: (alias_host, v4exthost)
1426 * Inner header: (v4exthost, alias_host) [sport, alias_port]
1428 * Assume caller function has checked if v4mapPRefix+v4host
1429 * matches configured prefix.
1430 * The only two things we should be provided with are mapping between
1431 * IPv6iHost <> alias_host and between dport and alias_port.
1433 if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
1434 m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
1436 NAT64STAT_INC(&cfg->stats, nomem);
1437 return (NAT64RETURN);
1439 ip6 = mtod(m, struct ip6_hdr *);
1440 ip6i = mtodo(m, hlen);
1442 proto = ip6i->ip6_nxt;
1443 plen = ntohs(ip6i->ip6_plen);
1444 hlen += sizeof(struct ip6_hdr);
1445 if (proto == IPPROTO_FRAGMENT) {
1446 if (m->m_pkthdr.len < hlen + sizeof(struct ip6_frag) +
1449 ip6f = mtodo(m, hlen);
1450 proto = ip6f->ip6f_nxt;
1451 plen -= sizeof(struct ip6_frag);
1452 hlen += sizeof(struct ip6_frag);
1453 /* Ajust MTU to reflect frag header size */
1454 if (type == ICMP_UNREACH && code == ICMP_UNREACH_NEEDFRAG)
1455 mtu -= sizeof(struct ip6_frag);
1457 if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
1458 DPRINTF(DP_DROPS, "Unsupported proto %d in the inner header",
1462 if (nat64_check_ip6(&ip6i->ip6_src) != 0 ||
1463 nat64_check_ip6(&ip6i->ip6_dst) != 0) {
1464 DPRINTF(DP_DROPS, "Inner addresses do not passes the check");
1467 /* Check if outer dst is the same as inner src */
1468 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6i->ip6_src)) {
1469 DPRINTF(DP_DROPS, "Inner src doesn't match outer dst");
1473 /* Now we need to make a fake IPv4 packet to generate ICMP message */
1474 ip.ip_dst.s_addr = aaddr;
1475 ip.ip_src.s_addr = nat64_extract_ip4(&ip6i->ip6_src, cfg->plat_plen);
1476 if (ip.ip_src.s_addr == 0)
1478 /* XXX: Make fake ulp header */
1479 if (V_nat64out == &nat64_direct) /* init_ip4hdr will decrement it */
1480 ip6i->ip6_hlim += IPV6_HLIMDEC;
1481 nat64_init_ip4hdr(ip6i, ip6f, plen, proto, &ip);
1482 m_adj(m, hlen - sizeof(struct ip));
1483 bcopy(&ip, mtod(m, void *), sizeof(ip));
1484 nat64_icmp_reflect(m, type, code, (uint16_t)mtu, &cfg->stats,
1486 return (NAT64RETURN);
1489 * We must call m_freem() because mbuf pointer could be
1490 * changed with m_pullup().
1493 NAT64STAT_INC(&cfg->stats, dropped);
1494 return (NAT64RETURN);
1498 nat64_do_handle_ip6(struct mbuf *m, uint32_t aaddr, uint16_t aport,
1499 struct nat64_config *cfg, void *logdata)
1502 struct nhop4_basic nh;
1503 struct sockaddr_in dst;
1504 struct ip6_frag *frag;
1505 struct ip6_hdr *ip6;
1506 struct icmp6_hdr *icmp6;
1508 int plen, hlen, proto;
1511 * XXX: we expect ipfw_chk() did m_pullup() up to upper level
1512 * protocol's headers. Also we skip some checks, that ip6_input(),
1513 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
1515 ip6 = mtod(m, struct ip6_hdr *);
1516 if (nat64_check_ip6(&ip6->ip6_src) != 0 ||
1517 nat64_check_ip6(&ip6->ip6_dst) != 0) {
1521 /* Starting from this point we must not return zero */
1522 ip.ip_src.s_addr = aaddr;
1523 if (nat64_check_ip4(ip.ip_src.s_addr) != 0) {
1524 DPRINTF(DP_GENERIC | DP_DROPS, "invalid source address: %08x",
1526 NAT64STAT_INC(&cfg->stats, dropped);
1527 return (NAT64MFREE);
1530 ip.ip_dst.s_addr = nat64_extract_ip4(&ip6->ip6_dst, cfg->plat_plen);
1531 if (ip.ip_dst.s_addr == 0) {
1532 NAT64STAT_INC(&cfg->stats, dropped);
1533 return (NAT64MFREE);
1536 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
1537 nat64_icmp6_reflect(m, ICMP6_TIME_EXCEEDED,
1538 ICMP6_TIME_EXCEED_TRANSIT, 0, &cfg->stats, logdata);
1539 return (NAT64RETURN);
1543 plen = ntohs(ip6->ip6_plen);
1544 proto = nat64_getlasthdr(m, &hlen);
1546 DPRINTF(DP_DROPS, "dropped due to mbuf isn't contigious");
1547 NAT64STAT_INC(&cfg->stats, dropped);
1548 return (NAT64MFREE);
1551 if (proto == IPPROTO_FRAGMENT) {
1552 /* ipfw_chk should m_pullup up to frag header */
1553 if (m->m_len < hlen + sizeof(*frag)) {
1555 "dropped due to mbuf isn't contigious");
1556 NAT64STAT_INC(&cfg->stats, dropped);
1557 return (NAT64MFREE);
1559 frag = mtodo(m, hlen);
1560 proto = frag->ip6f_nxt;
1561 hlen += sizeof(*frag);
1562 /* Fragmented ICMPv6 is unsupported */
1563 if (proto == IPPROTO_ICMPV6) {
1564 DPRINTF(DP_DROPS, "dropped due to fragmented ICMPv6");
1565 NAT64STAT_INC(&cfg->stats, dropped);
1566 return (NAT64MFREE);
1568 /* Fragment length must be multiple of 8 octets */
1569 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0 &&
1570 ((plen + sizeof(struct ip6_hdr) - hlen) & 0x7) != 0) {
1571 nat64_icmp6_reflect(m, ICMP6_PARAM_PROB,
1572 ICMP6_PARAMPROB_HEADER,
1573 offsetof(struct ip6_hdr, ip6_plen), &cfg->stats,
1575 return (NAT64RETURN);
1578 plen -= hlen - sizeof(struct ip6_hdr);
1579 if (plen < 0 || m->m_pkthdr.len < plen + hlen) {
1580 DPRINTF(DP_DROPS, "plen %d, pkthdr.len %d, hlen %d",
1581 plen, m->m_pkthdr.len, hlen);
1582 NAT64STAT_INC(&cfg->stats, dropped);
1583 return (NAT64MFREE);
1586 icmp6 = NULL; /* Make gcc happy */
1587 if (proto == IPPROTO_ICMPV6) {
1588 icmp6 = mtodo(m, hlen);
1589 if (icmp6->icmp6_type != ICMP6_ECHO_REQUEST &&
1590 icmp6->icmp6_type != ICMP6_ECHO_REPLY)
1591 return (nat64_handle_icmp6(m, hlen, aaddr, aport,
1594 dst.sin_addr.s_addr = ip.ip_dst.s_addr;
1595 if (nat64_find_route4(&nh, &dst, m) != 0) {
1596 NAT64STAT_INC(&cfg->stats, noroute4);
1597 nat64_icmp6_reflect(m, ICMP6_DST_UNREACH,
1598 ICMP6_DST_UNREACH_NOROUTE, 0, &cfg->stats, logdata);
1599 return (NAT64RETURN);
1601 if (nh.nh_mtu < plen + sizeof(ip)) {
1602 nat64_icmp6_reflect(m, ICMP6_PACKET_TOO_BIG, 0, nh.nh_mtu,
1603 &cfg->stats, logdata);
1604 return (NAT64RETURN);
1606 nat64_init_ip4hdr(ip6, frag, plen, proto, &ip);
1607 /* Convert checksums. */
1610 csum = &TCP(mtodo(m, hlen))->th_sum;
1612 struct tcphdr *tcp = TCP(mtodo(m, hlen));
1613 *csum = cksum_adjust(*csum, tcp->th_sport, aport);
1614 tcp->th_sport = aport;
1616 *csum = cksum_add(*csum, nat64_cksum_convert(ip6, &ip));
1619 csum = &UDP(mtodo(m, hlen))->uh_sum;
1621 struct udphdr *udp = UDP(mtodo(m, hlen));
1622 *csum = cksum_adjust(*csum, udp->uh_sport, aport);
1623 udp->uh_sport = aport;
1625 *csum = cksum_add(*csum, nat64_cksum_convert(ip6, &ip));
1627 case IPPROTO_ICMPV6:
1628 /* Checksum in ICMPv6 covers pseudo header */
1629 csum = &icmp6->icmp6_cksum;
1630 *csum = cksum_add(*csum, in6_cksum_pseudo(ip6, plen,
1631 IPPROTO_ICMPV6, 0));
1632 /* Convert ICMPv6 types to ICMP */
1633 proto = *(uint16_t *)icmp6; /* save old word for cksum_adjust */
1634 if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST)
1635 icmp6->icmp6_type = ICMP_ECHO;
1636 else /* ICMP6_ECHO_REPLY */
1637 icmp6->icmp6_type = ICMP_ECHOREPLY;
1638 *csum = cksum_adjust(*csum, (uint16_t)proto,
1639 *(uint16_t *)icmp6);
1641 uint16_t old_id = icmp6->icmp6_id;
1642 icmp6->icmp6_id = aport;
1643 *csum = cksum_adjust(*csum, old_id, aport);
1648 m_adj(m, hlen - sizeof(ip));
1649 bcopy(&ip, mtod(m, void *), sizeof(ip));
1650 if (V_nat64out->output(nh.nh_ifp, m, (struct sockaddr *)&dst,
1651 &cfg->stats, logdata) == 0)
1652 NAT64STAT_INC(&cfg->stats, opcnt64);
1653 return (NAT64RETURN);