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 "opt_ipstealth.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/counter.h>
37 #include <sys/errno.h>
38 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/rmlock.h>
43 #include <sys/rwlock.h>
44 #include <sys/socket.h>
45 #include <sys/queue.h>
48 #include <net/if_var.h>
49 #include <net/if_pflog.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_fib.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/ip_fw.h>
59 #include <netinet/ip6.h>
60 #include <netinet/icmp6.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/tcp.h>
63 #include <netinet/udp.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet6/in6_fib.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/ip_fw_nat64.h>
69 #include <netpfil/pf/pf.h>
70 #include <netpfil/ipfw/ip_fw_private.h>
71 #include <machine/in_cksum.h>
73 #include "ip_fw_nat64.h"
74 #include "nat64_translate.h"
77 typedef int (*nat64_output_t)(struct ifnet *, struct mbuf *,
78 struct sockaddr *, struct nat64_counters *, void *);
79 typedef int (*nat64_output_one_t)(struct mbuf *, struct nat64_counters *,
82 static int nat64_find_route4(struct nhop4_basic *, struct sockaddr_in *,
84 static int nat64_find_route6(struct nhop6_basic *, struct sockaddr_in6 *,
86 static int nat64_output_one(struct mbuf *, struct nat64_counters *, void *);
87 static int nat64_output(struct ifnet *, struct mbuf *, struct sockaddr *,
88 struct nat64_counters *, void *);
89 static int nat64_direct_output_one(struct mbuf *, struct nat64_counters *,
91 static int nat64_direct_output(struct ifnet *, struct mbuf *,
92 struct sockaddr *, struct nat64_counters *, void *);
94 struct nat64_methods {
95 nat64_output_t output;
96 nat64_output_one_t output_one;
98 static const struct nat64_methods nat64_netisr = {
99 .output = nat64_output,
100 .output_one = nat64_output_one
102 static const struct nat64_methods nat64_direct = {
103 .output = nat64_direct_output,
104 .output_one = nat64_direct_output_one
107 /* These variables should be initialized explicitly on module loading */
108 VNET_DEFINE_STATIC(const struct nat64_methods *, nat64out);
109 VNET_DEFINE_STATIC(const int *, nat64ipstealth);
110 VNET_DEFINE_STATIC(const int *, nat64ip6stealth);
111 #define V_nat64out VNET(nat64out)
112 #define V_nat64ipstealth VNET(nat64ipstealth)
113 #define V_nat64ip6stealth VNET(nat64ip6stealth)
115 static const int stealth_on = 1;
117 static const int stealth_off = 0;
121 nat64_set_output_method(int direct)
125 V_nat64out = &nat64_direct;
127 /* Honor corresponding variables, if IPSTEALTH is defined */
128 V_nat64ipstealth = &V_ipstealth;
129 V_nat64ip6stealth = &V_ip6stealth;
131 /* otherwise we need to decrement HLIM/TTL for direct case */
132 V_nat64ipstealth = V_nat64ip6stealth = &stealth_off;
135 V_nat64out = &nat64_netisr;
136 /* Leave TTL/HLIM decrementing to forwarding code */
137 V_nat64ipstealth = V_nat64ip6stealth = &stealth_on;
142 nat64_get_output_method(void)
145 return (V_nat64out == &nat64_direct ? 1: 0);
149 nat64_log(struct pfloghdr *logdata, struct mbuf *m, sa_family_t family)
152 logdata->dir = PF_OUT;
153 logdata->af = family;
154 ipfw_bpf_mtap2(logdata, PFLOG_HDRLEN, m);
158 nat64_direct_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
159 struct nat64_counters *stats, void *logdata)
164 nat64_log(logdata, m, dst->sa_family);
165 error = (*ifp->if_output)(ifp, m, dst, NULL);
167 NAT64STAT_INC(stats, oerrors);
172 nat64_direct_output_one(struct mbuf *m, struct nat64_counters *stats,
175 struct nhop6_basic nh6;
176 struct nhop4_basic nh4;
177 struct sockaddr_in6 dst6;
178 struct sockaddr_in dst4;
179 struct sockaddr *dst;
185 ip4 = mtod(m, struct ip *);
188 dst4.sin_addr = ip4->ip_dst;
189 error = nat64_find_route4(&nh4, &dst4, m);
191 NAT64STAT_INC(stats, noroute4);
194 dst = (struct sockaddr *)&dst4;
197 case (IPV6_VERSION >> 4):
198 ip6 = mtod(m, struct ip6_hdr *);
199 dst6.sin6_addr = ip6->ip6_dst;
200 error = nat64_find_route6(&nh6, &dst6, m);
202 NAT64STAT_INC(stats, noroute6);
205 dst = (struct sockaddr *)&dst6;
210 NAT64STAT_INC(stats, dropped);
211 DPRINTF(DP_DROPS, "dropped due to unknown IP version");
212 return (EAFNOSUPPORT);
216 return (EHOSTUNREACH);
219 nat64_log(logdata, m, dst->sa_family);
220 error = (*ifp->if_output)(ifp, m, dst, NULL);
222 NAT64STAT_INC(stats, oerrors);
227 nat64_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
228 struct nat64_counters *stats, void *logdata)
233 ip4 = mtod(m, struct ip *);
239 case (IPV6_VERSION >> 4):
245 NAT64STAT_INC(stats, dropped);
246 DPRINTF(DP_DROPS, "unknown IP version");
247 return (EAFNOSUPPORT);
250 nat64_log(logdata, m, af);
251 if (m->m_pkthdr.rcvif == NULL)
252 m->m_pkthdr.rcvif = V_loif;
253 ret = netisr_queue(ret, m);
255 NAT64STAT_INC(stats, oerrors);
260 nat64_output_one(struct mbuf *m, struct nat64_counters *stats, void *logdata)
263 return (nat64_output(NULL, m, NULL, stats, logdata));
267 * Check the given IPv6 prefix and length according to RFC6052:
268 * The prefixes can only have one of the following lengths:
269 * 32, 40, 48, 56, 64, or 96 (The Well-Known Prefix is 96 bits long).
270 * Returns zero on success, otherwise EINVAL.
273 nat64_check_prefixlen(int length)
289 nat64_check_prefix6(const struct in6_addr *prefix, int length)
292 if (nat64_check_prefixlen(length) != 0)
295 /* Well-known prefix has 96 prefix length */
296 if (IN6_IS_ADDR_WKPFX(prefix) && length != 96)
299 /* Bits 64 to 71 must be set to zero */
300 if (prefix->__u6_addr.__u6_addr8[8] != 0)
303 /* Some extra checks */
304 if (IN6_IS_ADDR_MULTICAST(prefix) ||
305 IN6_IS_ADDR_UNSPECIFIED(prefix) ||
306 IN6_IS_ADDR_LOOPBACK(prefix))
312 nat64_check_private_ip4(const struct nat64_config *cfg, in_addr_t ia)
315 if (cfg->flags & NAT64_ALLOW_PRIVATE)
318 /* WKPFX must not be used to represent non-global IPv4 addresses */
319 if (cfg->flags & NAT64_WKPFX) {
321 if ((ia & htonl(0xff000000)) == htonl(0x0a000000) ||
322 (ia & htonl(0xfff00000)) == htonl(0xac100000) ||
323 (ia & htonl(0xffff0000)) == htonl(0xc0a80000))
327 * 192.0.0.0/24 - reserved for IETF protocol assignments
328 * 192.88.99.0/24 - for use as 6to4 relay anycast addresses
329 * 198.18.0.0/15 - for use in benchmark tests
330 * 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 - for use
331 * in documentation and example code
333 if ((ia & htonl(0xffffff00)) == htonl(0xc0000000) ||
334 (ia & htonl(0xffffff00)) == htonl(0xc0586300) ||
335 (ia & htonl(0xfffffe00)) == htonl(0xc6120000) ||
336 (ia & htonl(0xffffff00)) == htonl(0xc0000200) ||
337 (ia & htonl(0xfffffe00)) == htonl(0xc6336400) ||
338 (ia & htonl(0xffffff00)) == htonl(0xcb007100))
345 * Embed @ia IPv4 address into @ip6 IPv6 address.
346 * Place to embedding determined from prefix length @plen.
349 nat64_embed_ip4(struct in6_addr *ip6, int plen, in_addr_t ia)
355 ip6->s6_addr32[plen / 32] = ia;
361 * Preserve prefix bits.
362 * Since suffix bits should be zero and reserved for future
363 * use, we just overwrite the whole word, where they are.
365 ip6->s6_addr32[1] &= 0xffffffff << (32 - plen % 32);
366 #if BYTE_ORDER == BIG_ENDIAN
367 ip6->s6_addr32[1] |= ia >> (plen % 32);
368 ip6->s6_addr32[2] = ia << (24 - plen % 32);
369 #elif BYTE_ORDER == LITTLE_ENDIAN
370 ip6->s6_addr32[1] |= ia << (plen % 32);
371 ip6->s6_addr32[2] = ia >> (24 - plen % 32);
375 #if BYTE_ORDER == BIG_ENDIAN
376 ip6->s6_addr32[2] = ia >> 8;
377 ip6->s6_addr32[3] = ia << 24;
378 #elif BYTE_ORDER == LITTLE_ENDIAN
379 ip6->s6_addr32[2] = ia << 8;
380 ip6->s6_addr32[3] = ia >> 24;
384 panic("Wrong plen: %d", plen);
387 * Bits 64 to 71 of the address are reserved for compatibility
388 * with the host identifier format defined in the IPv6 addressing
389 * architecture [RFC4291]. These bits MUST be set to zero.
391 ip6->s6_addr8[8] = 0;
395 nat64_extract_ip4(const struct in6_addr *ip6, int plen)
400 * According to RFC 6052 p2.2:
401 * IPv4-embedded IPv6 addresses are composed of a variable-length
402 * prefix, the embedded IPv4 address, and a variable length suffix.
403 * The suffix bits are reserved for future extensions and SHOULD
408 if (ip6->s6_addr32[3] != 0 || ip6->s6_addr32[2] != 0)
412 if (ip6->s6_addr32[3] != 0 ||
413 (ip6->s6_addr32[2] & htonl(0xff00ffff)) != 0)
417 if (ip6->s6_addr32[3] != 0 ||
418 (ip6->s6_addr32[2] & htonl(0xff0000ff)) != 0)
422 if (ip6->s6_addr32[3] != 0 || ip6->s6_addr8[8] != 0)
426 if (ip6->s6_addr8[8] != 0 ||
427 (ip6->s6_addr32[3] & htonl(0x00ffffff)) != 0)
433 ia = ip6->s6_addr32[plen / 32];
438 #if BYTE_ORDER == BIG_ENDIAN
439 ia = (ip6->s6_addr32[1] << (plen % 32)) |
440 (ip6->s6_addr32[2] >> (24 - plen % 32));
441 #elif BYTE_ORDER == LITTLE_ENDIAN
442 ia = (ip6->s6_addr32[1] >> (plen % 32)) |
443 (ip6->s6_addr32[2] << (24 - plen % 32));
447 #if BYTE_ORDER == BIG_ENDIAN
448 ia = (ip6->s6_addr32[2] << 8) | (ip6->s6_addr32[3] >> 24);
449 #elif BYTE_ORDER == LITTLE_ENDIAN
450 ia = (ip6->s6_addr32[2] >> 8) | (ip6->s6_addr32[3] << 24);
456 if (nat64_check_ip4(ia) == 0)
459 DPRINTF(DP_GENERIC | DP_DROPS,
460 "invalid destination address: %08x", ia);
463 DPRINTF(DP_GENERIC | DP_DROPS, "invalid IPv4-embedded IPv6 address");
468 * According to RFC 1624 the equation for incremental checksum update is:
469 * HC' = ~(~HC + ~m + m') -- [Eqn. 3]
470 * HC' = HC - ~m - m' -- [Eqn. 4]
471 * So, when we are replacing IPv4 addresses to IPv6, we
472 * can assume, that new bytes previously were zeros, and vise versa -
473 * when we replacing IPv6 addresses to IPv4, now unused bytes become
474 * zeros. The payload length in pseudo header has bigger size, but one
475 * half of it should be zero. Using the equation 4 we get:
476 * HC' = HC - (~m0 + m0') -- m0 is first changed word
477 * HC' = (HC - (~m0 + m0')) - (~m1 + m1') -- m1 is second changed word
478 * HC' = HC - ~m0 - m0' - ~m1 - m1' - ... =
479 * = HC - sum(~m[i] + m'[i])
481 * The function result should be used as follows:
482 * IPv6 to IPv4: HC' = cksum_add(HC, result)
483 * IPv4 to IPv6: HC' = cksum_add(HC, ~result)
486 nat64_cksum_convert(struct ip6_hdr *ip6, struct ip *ip)
491 sum = ~ip->ip_src.s_addr >> 16;
492 sum += ~ip->ip_src.s_addr & 0xffff;
493 sum += ~ip->ip_dst.s_addr >> 16;
494 sum += ~ip->ip_dst.s_addr & 0xffff;
496 for (p = (uint16_t *)&ip6->ip6_src;
497 p < (uint16_t *)(&ip6->ip6_src + 2); p++)
501 sum = (sum & 0xffff) + (sum >> 16);
506 nat64_init_ip4hdr(const struct ip6_hdr *ip6, const struct ip6_frag *frag,
507 uint16_t plen, uint8_t proto, struct ip *ip)
510 /* assume addresses are already initialized */
511 ip->ip_v = IPVERSION;
512 ip->ip_hl = sizeof(*ip) >> 2;
513 ip->ip_tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
514 ip->ip_len = htons(sizeof(*ip) + plen);
515 ip->ip_ttl = ip6->ip6_hlim;
516 if (*V_nat64ip6stealth == 0)
517 ip->ip_ttl -= IPV6_HLIMDEC;
519 ip->ip_p = (proto == IPPROTO_ICMPV6) ? IPPROTO_ICMP: proto;
522 ip->ip_off = htons(ntohs(frag->ip6f_offlg) >> 3);
523 if (frag->ip6f_offlg & IP6F_MORE_FRAG)
524 ip->ip_off |= htons(IP_MF);
526 ip->ip_off = htons(IP_DF);
528 ip->ip_sum = in_cksum_hdr(ip);
531 #define FRAGSZ(mtu) ((mtu) - sizeof(struct ip6_hdr) - sizeof(struct ip6_frag))
532 static NAT64NOINLINE int
533 nat64_fragment6(struct nat64_counters *stats, struct ip6_hdr *ip6,
534 struct mbufq *mq, struct mbuf *m, uint32_t mtu, uint16_t ip_id,
537 struct ip6_frag ip6f;
539 uint16_t hlen, len, offset;
542 plen = ntohs(ip6->ip6_plen);
543 hlen = sizeof(struct ip6_hdr);
545 /* Fragmentation isn't needed */
546 if (ip_off == 0 && plen <= mtu - hlen) {
547 M_PREPEND(m, hlen, M_NOWAIT);
549 NAT64STAT_INC(stats, nomem);
552 bcopy(ip6, mtod(m, void *), hlen);
553 if (mbufq_enqueue(mq, m) != 0) {
555 NAT64STAT_INC(stats, dropped);
556 DPRINTF(DP_DROPS, "dropped due to mbufq overflow");
562 hlen += sizeof(struct ip6_frag);
563 ip6f.ip6f_reserved = 0;
564 ip6f.ip6f_nxt = ip6->ip6_nxt;
565 ip6->ip6_nxt = IPPROTO_FRAGMENT;
568 * We have got an IPv4 fragment.
569 * Use offset value and ip_id from original fragment.
571 ip6f.ip6f_ident = htonl(ntohs(ip_id));
572 offset = (ntohs(ip_off) & IP_OFFMASK) << 3;
573 NAT64STAT_INC(stats, ifrags);
575 /* The packet size exceeds interface MTU */
576 ip6f.ip6f_ident = htonl(ip6_randomid());
577 offset = 0; /* First fragment*/
579 while (plen > 0 && m != NULL) {
581 len = FRAGSZ(mtu) & ~7;
584 ip6->ip6_plen = htons(len + sizeof(ip6f));
585 ip6f.ip6f_offlg = ntohs(offset);
586 if (len < plen || (ip_off & htons(IP_MF)) != 0)
587 ip6f.ip6f_offlg |= IP6F_MORE_FRAG;
591 n = m_split(m, len, M_NOWAIT);
595 M_PREPEND(m, hlen, M_NOWAIT);
598 bcopy(ip6, mtod(m, void *), sizeof(struct ip6_hdr));
599 bcopy(&ip6f, mtodo(m, sizeof(struct ip6_hdr)),
600 sizeof(struct ip6_frag));
601 if (mbufq_enqueue(mq, m) != 0)
605 NAT64STAT_ADD(stats, ofrags, mbufq_len(mq));
613 NAT64STAT_INC(stats, nomem);
617 static NAT64NOINLINE int
618 nat64_find_route6(struct nhop6_basic *pnh, struct sockaddr_in6 *dst,
622 if (fib6_lookup_nh_basic(M_GETFIB(m), &dst->sin6_addr, 0, 0, 0,
624 return (EHOSTUNREACH);
625 if (pnh->nh_flags & (NHF_BLACKHOLE | NHF_REJECT))
626 return (EHOSTUNREACH);
628 * XXX: we need to use destination address with embedded scope
629 * zone id, because LLTABLE uses such form of addresses for lookup.
631 dst->sin6_family = AF_INET6;
632 dst->sin6_len = sizeof(*dst);
633 dst->sin6_addr = pnh->nh_addr;
634 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
635 dst->sin6_addr.s6_addr16[1] =
636 htons(pnh->nh_ifp->if_index & 0xffff);
638 dst->sin6_scope_id = 0;
639 dst->sin6_flowinfo = 0;
644 #define NAT64_ICMP6_PLEN 64
645 static NAT64NOINLINE void
646 nat64_icmp6_reflect(struct mbuf *m, uint8_t type, uint8_t code, uint32_t mtu,
647 struct nat64_counters *stats, void *logdata)
649 struct icmp6_hdr *icmp6;
650 struct ip6_hdr *ip6, *oip6;
652 int len, plen, proto;
655 proto = nat64_getlasthdr(m, &len);
657 DPRINTF(DP_DROPS, "mbuf isn't contigious");
661 * Do not send ICMPv6 in reply to ICMPv6 errors.
663 if (proto == IPPROTO_ICMPV6) {
664 if (m->m_len < len + sizeof(*icmp6)) {
665 DPRINTF(DP_DROPS, "mbuf isn't contigious");
668 icmp6 = mtodo(m, len);
669 if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST ||
670 icmp6->icmp6_type == ND_REDIRECT) {
671 DPRINTF(DP_DROPS, "do not send ICMPv6 in reply to "
676 * If there are extra headers between IPv6 and ICMPv6,
679 if (len > sizeof(struct ip6_hdr)) {
681 * NOTE: ipfw_chk already did m_pullup() and it is
682 * expected that data is contigious from the start
683 * of IPv6 header up to the end of ICMPv6 header.
685 bcopy(mtod(m, caddr_t),
686 mtodo(m, len - sizeof(struct ip6_hdr)),
687 sizeof(struct ip6_hdr));
688 m_adj(m, len - sizeof(struct ip6_hdr));
692 if (icmp6_ratelimit(&ip6->ip6_src, type, code))
695 ip6 = mtod(m, struct ip6_hdr *);
697 case ICMP6_DST_UNREACH:
698 case ICMP6_PACKET_TOO_BIG:
699 case ICMP6_TIME_EXCEEDED:
700 case ICMP6_PARAM_PROB:
705 /* Calculate length of ICMPv6 payload */
706 len = (m->m_pkthdr.len > NAT64_ICMP6_PLEN) ? NAT64_ICMP6_PLEN:
709 /* Create new ICMPv6 datagram */
710 plen = len + sizeof(struct icmp6_hdr);
711 n = m_get2(sizeof(struct ip6_hdr) + plen + max_hdr, M_NOWAIT,
712 MT_HEADER, M_PKTHDR);
714 NAT64STAT_INC(stats, nomem);
719 * Move pkthdr from original mbuf. We should have initialized some
720 * fields, because we can reinject this mbuf to netisr and it will
721 * go trough input path (it requires at least rcvif should be set).
722 * Also do M_ALIGN() to reduce chances of need to allocate new mbuf
723 * in the chain, when we will do M_PREPEND() or make some type of
727 M_ALIGN(n, sizeof(struct ip6_hdr) + plen + max_hdr);
729 n->m_len = n->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
730 oip6 = mtod(n, struct ip6_hdr *);
732 * Make IPv6 source address selection for reflected datagram.
733 * nat64_check_ip6() doesn't allow scoped addresses, therefore
734 * we use zero scopeid.
736 if (in6_selectsrc_addr(M_GETFIB(n), &ip6->ip6_src, 0,
737 n->m_pkthdr.rcvif, &oip6->ip6_src, NULL) != 0) {
739 * Failed to find proper source address, drop the packet.
744 oip6->ip6_dst = ip6->ip6_src;
745 oip6->ip6_nxt = IPPROTO_ICMPV6;
747 oip6->ip6_vfc |= IPV6_VERSION;
748 oip6->ip6_hlim = V_ip6_defhlim;
749 oip6->ip6_plen = htons(plen);
751 icmp6 = mtodo(n, sizeof(struct ip6_hdr));
752 icmp6->icmp6_cksum = 0;
753 icmp6->icmp6_type = type;
754 icmp6->icmp6_code = code;
755 icmp6->icmp6_mtu = htonl(mtu);
757 m_copydata(m, 0, len, mtodo(n, sizeof(struct ip6_hdr) +
758 sizeof(struct icmp6_hdr)));
759 icmp6->icmp6_cksum = in6_cksum(n, IPPROTO_ICMPV6,
760 sizeof(struct ip6_hdr), plen);
762 V_nat64out->output_one(n, stats, logdata);
765 NAT64STAT_INC(stats, dropped);
769 static NAT64NOINLINE int
770 nat64_find_route4(struct nhop4_basic *pnh, struct sockaddr_in *dst,
774 if (fib4_lookup_nh_basic(M_GETFIB(m), dst->sin_addr, 0, 0, pnh) != 0)
775 return (EHOSTUNREACH);
776 if (pnh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST | NHF_REJECT))
777 return (EHOSTUNREACH);
779 dst->sin_family = AF_INET;
780 dst->sin_len = sizeof(*dst);
781 dst->sin_addr = pnh->nh_addr;
786 #define NAT64_ICMP_PLEN 64
787 static NAT64NOINLINE void
788 nat64_icmp_reflect(struct mbuf *m, uint8_t type,
789 uint8_t code, uint16_t mtu, struct nat64_counters *stats, void *logdata)
796 ip = mtod(m, struct ip *);
797 /* Do not send ICMP error if packet is not the first fragment */
798 if (ip->ip_off & ~ntohs(IP_MF|IP_DF)) {
799 DPRINTF(DP_DROPS, "not first fragment");
802 /* Do not send ICMP in reply to ICMP errors */
803 if (ip->ip_p == IPPROTO_ICMP) {
804 if (m->m_len < (ip->ip_hl << 2)) {
805 DPRINTF(DP_DROPS, "mbuf isn't contigious");
808 icmp = mtodo(m, ip->ip_hl << 2);
809 if (!ICMP_INFOTYPE(icmp->icmp_type)) {
810 DPRINTF(DP_DROPS, "do not send ICMP in reply to "
823 /* Calculate length of ICMP payload */
824 len = (m->m_pkthdr.len > NAT64_ICMP_PLEN) ? (ip->ip_hl << 2) + 8:
827 /* Create new ICMPv4 datagram */
828 plen = len + sizeof(struct icmphdr) + sizeof(uint32_t);
829 n = m_get2(sizeof(struct ip) + plen + max_hdr, M_NOWAIT,
830 MT_HEADER, M_PKTHDR);
832 NAT64STAT_INC(stats, nomem);
837 M_ALIGN(n, sizeof(struct ip) + plen + max_hdr);
839 n->m_len = n->m_pkthdr.len = sizeof(struct ip) + plen;
840 oip = mtod(n, struct ip *);
841 oip->ip_v = IPVERSION;
842 oip->ip_hl = sizeof(struct ip) >> 2;
844 oip->ip_len = htons(n->m_pkthdr.len);
845 oip->ip_ttl = V_ip_defttl;
846 oip->ip_p = IPPROTO_ICMP;
848 oip->ip_off = htons(IP_DF);
849 oip->ip_src = ip->ip_dst;
850 oip->ip_dst = ip->ip_src;
852 oip->ip_sum = in_cksum_hdr(oip);
854 icmp = mtodo(n, sizeof(struct ip));
855 icmp->icmp_type = type;
856 icmp->icmp_code = code;
857 icmp->icmp_cksum = 0;
858 icmp->icmp_pmvoid = 0;
859 icmp->icmp_nextmtu = htons(mtu);
860 m_copydata(m, 0, len, mtodo(n, sizeof(struct ip) +
861 sizeof(struct icmphdr) + sizeof(uint32_t)));
862 icmp->icmp_cksum = in_cksum_skip(n, sizeof(struct ip) + plen,
865 V_nat64out->output_one(n, stats, logdata);
868 NAT64STAT_INC(stats, dropped);
872 /* Translate ICMP echo request/reply into ICMPv6 */
874 nat64_icmp_handle_echo(struct ip6_hdr *ip6, struct icmp6_hdr *icmp6,
875 uint16_t id, uint8_t type)
879 old = *(uint16_t *)icmp6; /* save type+code in one word */
880 icmp6->icmp6_type = type;
881 /* Reflect ICMPv6 -> ICMPv4 type translation in the cksum */
882 icmp6->icmp6_cksum = cksum_adjust(icmp6->icmp6_cksum,
883 old, *(uint16_t *)icmp6);
885 old = icmp6->icmp6_id;
886 icmp6->icmp6_id = id;
887 /* Reflect ICMP id translation in the cksum */
888 icmp6->icmp6_cksum = cksum_adjust(icmp6->icmp6_cksum,
891 /* Reflect IPv6 pseudo header in the cksum */
892 icmp6->icmp6_cksum = ~in6_cksum_pseudo(ip6, ntohs(ip6->ip6_plen),
893 IPPROTO_ICMPV6, ~icmp6->icmp6_cksum);
896 static NAT64NOINLINE struct mbuf *
897 nat64_icmp_translate(struct mbuf *m, struct ip6_hdr *ip6, uint16_t icmpid,
898 int offset, struct nat64_config *cfg)
904 struct ip6_hdr *eip6;
910 if (m->m_len < offset + ICMP_MINLEN)
911 m = m_pullup(m, offset + ICMP_MINLEN);
913 NAT64STAT_INC(&cfg->stats, nomem);
917 icmp = mtodo(m, offset);
919 switch (icmp->icmp_type) {
921 type = ICMP6_ECHO_REPLY;
925 type = ICMP6_DST_UNREACH;
926 switch (icmp->icmp_code) {
927 case ICMP_UNREACH_NET:
928 case ICMP_UNREACH_HOST:
929 case ICMP_UNREACH_SRCFAIL:
930 case ICMP_UNREACH_NET_UNKNOWN:
931 case ICMP_UNREACH_HOST_UNKNOWN:
932 case ICMP_UNREACH_TOSNET:
933 case ICMP_UNREACH_TOSHOST:
934 code = ICMP6_DST_UNREACH_NOROUTE;
936 case ICMP_UNREACH_PROTOCOL:
937 type = ICMP6_PARAM_PROB;
938 code = ICMP6_PARAMPROB_NEXTHEADER;
940 case ICMP_UNREACH_PORT:
941 code = ICMP6_DST_UNREACH_NOPORT;
943 case ICMP_UNREACH_NEEDFRAG:
944 type = ICMP6_PACKET_TOO_BIG;
946 /* XXX: needs an additional look */
947 mtu = max(IPV6_MMTU, ntohs(icmp->icmp_nextmtu) + 20);
949 case ICMP_UNREACH_NET_PROHIB:
950 case ICMP_UNREACH_HOST_PROHIB:
951 case ICMP_UNREACH_FILTER_PROHIB:
952 case ICMP_UNREACH_PRECEDENCE_CUTOFF:
953 code = ICMP6_DST_UNREACH_ADMIN;
956 DPRINTF(DP_DROPS, "Unsupported ICMP type %d, code %d",
957 icmp->icmp_type, icmp->icmp_code);
962 type = ICMP6_TIME_EXCEEDED;
963 code = icmp->icmp_code;
966 type = ICMP6_ECHO_REQUEST;
970 type = ICMP6_PARAM_PROB;
971 switch (icmp->icmp_code) {
972 case ICMP_PARAMPROB_ERRATPTR:
973 case ICMP_PARAMPROB_LENGTH:
974 code = ICMP6_PARAMPROB_HEADER;
975 switch (icmp->icmp_pptr) {
976 case 0: /* Version/IHL */
977 case 1: /* Type Of Service */
978 mtu = icmp->icmp_pptr;
980 case 2: /* Total Length */
981 case 3: mtu = 4; /* Payload Length */
983 case 8: /* Time to Live */
984 mtu = 7; /* Hop Limit */
986 case 9: /* Protocol */
987 mtu = 6; /* Next Header */
989 case 12: /* Source address */
995 case 16: /* Destination address */
1001 default: /* Silently drop */
1002 DPRINTF(DP_DROPS, "Unsupported ICMP type %d,"
1003 " code %d, pptr %d", icmp->icmp_type,
1004 icmp->icmp_code, icmp->icmp_pptr);
1009 DPRINTF(DP_DROPS, "Unsupported ICMP type %d,"
1010 " code %d, pptr %d", icmp->icmp_type,
1011 icmp->icmp_code, icmp->icmp_pptr);
1016 DPRINTF(DP_DROPS, "Unsupported ICMP type %d, code %d",
1017 icmp->icmp_type, icmp->icmp_code);
1021 * For echo request/reply we can use original payload,
1022 * but we need adjust icmp_cksum, because ICMPv6 cksum covers
1023 * IPv6 pseudo header and ICMPv6 types differs from ICMPv4.
1025 if (type == ICMP6_ECHO_REQUEST || type == ICMP6_ECHO_REPLY) {
1026 nat64_icmp_handle_echo(ip6, ICMP6(icmp), icmpid, type);
1030 * For other types of ICMP messages we need to translate inner
1031 * IPv4 header to IPv6 header.
1032 * Assume ICMP src is the same as payload dst
1033 * E.g. we have ( GWsrc1 , NATIP1 ) in outer header
1034 * and ( NATIP1, Hostdst1 ) in ICMP copy header.
1035 * In that case, we already have map for NATIP1 and GWsrc1.
1036 * The only thing we need is to copy IPv6 map prefix to
1039 hlen = offset + ICMP_MINLEN;
1040 if (m->m_pkthdr.len < hlen + sizeof(struct ip) + ICMP_MINLEN) {
1041 DPRINTF(DP_DROPS, "Message is too short %d",
1045 m_copydata(m, hlen, sizeof(struct ip), (char *)&ip);
1046 if (ip.ip_v != IPVERSION) {
1047 DPRINTF(DP_DROPS, "Wrong IP version %d", ip.ip_v);
1050 hlen += ip.ip_hl << 2; /* Skip inner IP header */
1051 if (nat64_check_ip4(ip.ip_src.s_addr) != 0 ||
1052 nat64_check_ip4(ip.ip_dst.s_addr) != 0 ||
1053 nat64_check_private_ip4(cfg, ip.ip_src.s_addr) != 0 ||
1054 nat64_check_private_ip4(cfg, ip.ip_dst.s_addr) != 0) {
1055 DPRINTF(DP_DROPS, "IP addresses checks failed %04x -> %04x",
1056 ntohl(ip.ip_src.s_addr), ntohl(ip.ip_dst.s_addr));
1059 if (m->m_pkthdr.len < hlen + ICMP_MINLEN) {
1060 DPRINTF(DP_DROPS, "Message is too short %d",
1066 * Check that inner source matches the outer destination.
1067 * XXX: We need some method to convert IPv4 into IPv6 address here,
1068 * and compare IPv6 addresses.
1070 if (ip.ip_src.s_addr != nat64_get_ip4(&ip6->ip6_dst)) {
1071 DPRINTF(DP_GENERIC, "Inner source doesn't match destination ",
1072 "%04x vs %04x", ip.ip_src.s_addr,
1073 nat64_get_ip4(&ip6->ip6_dst));
1078 * Create new mbuf for ICMPv6 datagram.
1079 * NOTE: len is data length just after inner IP header.
1081 len = m->m_pkthdr.len - hlen;
1082 if (sizeof(struct ip6_hdr) +
1083 sizeof(struct icmp6_hdr) + len > NAT64_ICMP6_PLEN)
1084 len = NAT64_ICMP6_PLEN - sizeof(struct icmp6_hdr) -
1085 sizeof(struct ip6_hdr);
1086 plen = sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr) + len;
1087 n = m_get2(offset + plen + max_hdr, M_NOWAIT, MT_HEADER, M_PKTHDR);
1089 NAT64STAT_INC(&cfg->stats, nomem);
1093 m_move_pkthdr(n, m);
1094 M_ALIGN(n, offset + plen + max_hdr);
1095 n->m_len = n->m_pkthdr.len = offset + plen;
1096 /* Adjust ip6_plen in outer header */
1097 ip6->ip6_plen = htons(plen);
1098 /* Construct new inner IPv6 header */
1099 eip6 = mtodo(n, offset + sizeof(struct icmp6_hdr));
1100 eip6->ip6_src = ip6->ip6_dst;
1102 /* Use the same prefix that we have in outer header */
1103 eip6->ip6_dst = ip6->ip6_src;
1104 MPASS(cfg->flags & NAT64_PLATPFX);
1105 nat64_embed_ip4(&eip6->ip6_dst, cfg->plat_plen, ip.ip_dst.s_addr);
1107 eip6->ip6_flow = htonl(ip.ip_tos << 20);
1108 eip6->ip6_vfc |= IPV6_VERSION;
1109 eip6->ip6_hlim = ip.ip_ttl;
1110 eip6->ip6_plen = htons(ntohs(ip.ip_len) - (ip.ip_hl << 2));
1111 eip6->ip6_nxt = (ip.ip_p == IPPROTO_ICMP) ? IPPROTO_ICMPV6: ip.ip_p;
1112 m_copydata(m, hlen, len, (char *)(eip6 + 1));
1114 * We need to translate source port in the inner ULP header,
1115 * and adjust ULP checksum.
1119 if (len < offsetof(struct tcphdr, th_sum))
1121 tcp = TCP(eip6 + 1);
1123 tcp->th_sum = cksum_adjust(tcp->th_sum,
1124 tcp->th_sport, icmpid);
1125 tcp->th_sport = icmpid;
1127 tcp->th_sum = cksum_add(tcp->th_sum,
1128 ~nat64_cksum_convert(eip6, &ip));
1131 if (len < offsetof(struct udphdr, uh_sum))
1133 udp = UDP(eip6 + 1);
1135 udp->uh_sum = cksum_adjust(udp->uh_sum,
1136 udp->uh_sport, icmpid);
1137 udp->uh_sport = icmpid;
1139 udp->uh_sum = cksum_add(udp->uh_sum,
1140 ~nat64_cksum_convert(eip6, &ip));
1144 * Check if this is an ICMP error message for echo request
1145 * that we sent. I.e. ULP in the data containing invoking
1146 * packet is IPPROTO_ICMP and its type is ICMP_ECHO.
1148 icmp = (struct icmp *)(eip6 + 1);
1149 if (icmp->icmp_type != ICMP_ECHO) {
1154 * For our client this original datagram should looks
1155 * like it was ICMPv6 datagram with type ICMP6_ECHO_REQUEST.
1156 * Thus we need adjust icmp_cksum and convert type from
1157 * ICMP_ECHO to ICMP6_ECHO_REQUEST.
1159 nat64_icmp_handle_echo(eip6, ICMP6(icmp), icmpid,
1160 ICMP6_ECHO_REQUEST);
1163 /* Convert ICMPv4 into ICMPv6 header */
1164 icmp = mtodo(n, offset);
1165 ICMP6(icmp)->icmp6_type = type;
1166 ICMP6(icmp)->icmp6_code = code;
1167 ICMP6(icmp)->icmp6_mtu = htonl(mtu);
1168 ICMP6(icmp)->icmp6_cksum = 0;
1169 ICMP6(icmp)->icmp6_cksum = cksum_add(
1170 ~in6_cksum_pseudo(ip6, plen, IPPROTO_ICMPV6, 0),
1171 in_cksum_skip(n, n->m_pkthdr.len, offset));
1175 NAT64STAT_INC(&cfg->stats, dropped);
1180 nat64_getlasthdr(struct mbuf *m, int *offset)
1182 struct ip6_hdr *ip6;
1183 struct ip6_hbh *hbh;
1191 if (m->m_len < hlen + sizeof(*ip6))
1194 ip6 = mtodo(m, hlen);
1195 hlen += sizeof(*ip6);
1196 proto = ip6->ip6_nxt;
1197 /* Skip extension headers */
1198 while (proto == IPPROTO_HOPOPTS || proto == IPPROTO_ROUTING ||
1199 proto == IPPROTO_DSTOPTS) {
1200 hbh = mtodo(m, hlen);
1202 * We expect mbuf has contigious data up to
1203 * upper level header.
1205 if (m->m_len < hlen)
1208 * We doesn't support Jumbo payload option,
1211 if (proto == IPPROTO_HOPOPTS && ip6->ip6_plen == 0)
1213 proto = hbh->ip6h_nxt;
1214 hlen += (hbh->ip6h_len + 1) << 3;
1222 nat64_do_handle_ip4(struct mbuf *m, struct in6_addr *saddr,
1223 struct in6_addr *daddr, uint16_t lport, struct nat64_config *cfg,
1226 struct nhop6_basic nh;
1228 struct sockaddr_in6 dst;
1231 uint16_t ip_id, ip_off;
1236 ip = mtod(m, struct ip*);
1238 if (*V_nat64ipstealth == 0 && ip->ip_ttl <= IPTTLDEC) {
1239 nat64_icmp_reflect(m, ICMP_TIMXCEED,
1240 ICMP_TIMXCEED_INTRANS, 0, &cfg->stats, logdata);
1241 return (NAT64RETURN);
1244 ip6.ip6_dst = *daddr;
1245 ip6.ip6_src = *saddr;
1247 hlen = ip->ip_hl << 2;
1248 plen = ntohs(ip->ip_len) - hlen;
1251 /* Save ip_id and ip_off, both are in network byte order */
1253 ip_off = ip->ip_off & htons(IP_OFFMASK | IP_MF);
1255 /* Fragment length must be multiple of 8 octets */
1256 if ((ip->ip_off & htons(IP_MF)) != 0 && (plen & 0x7) != 0) {
1257 nat64_icmp_reflect(m, ICMP_PARAMPROB,
1258 ICMP_PARAMPROB_LENGTH, 0, &cfg->stats, logdata);
1259 return (NAT64RETURN);
1261 /* Fragmented ICMP is unsupported */
1262 if (proto == IPPROTO_ICMP && ip_off != 0) {
1263 DPRINTF(DP_DROPS, "dropped due to fragmented ICMP");
1264 NAT64STAT_INC(&cfg->stats, dropped);
1265 return (NAT64MFREE);
1268 dst.sin6_addr = ip6.ip6_dst;
1269 if (nat64_find_route6(&nh, &dst, m) != 0) {
1270 NAT64STAT_INC(&cfg->stats, noroute6);
1271 nat64_icmp_reflect(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0,
1272 &cfg->stats, logdata);
1273 return (NAT64RETURN);
1275 if (nh.nh_mtu < plen + sizeof(ip6) &&
1276 (ip->ip_off & htons(IP_DF)) != 0) {
1277 nat64_icmp_reflect(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
1278 FRAGSZ(nh.nh_mtu) + sizeof(struct ip), &cfg->stats, logdata);
1279 return (NAT64RETURN);
1282 ip6.ip6_flow = htonl(ip->ip_tos << 20);
1283 ip6.ip6_vfc |= IPV6_VERSION;
1284 ip6.ip6_hlim = ip->ip_ttl;
1285 if (*V_nat64ipstealth == 0)
1286 ip6.ip6_hlim -= IPTTLDEC;
1287 ip6.ip6_plen = htons(plen);
1288 ip6.ip6_nxt = (proto == IPPROTO_ICMP) ? IPPROTO_ICMPV6: proto;
1289 /* Convert checksums. */
1292 csum = &TCP(mtodo(m, hlen))->th_sum;
1294 struct tcphdr *tcp = TCP(mtodo(m, hlen));
1295 *csum = cksum_adjust(*csum, tcp->th_dport, lport);
1296 tcp->th_dport = lport;
1298 *csum = cksum_add(*csum, ~nat64_cksum_convert(&ip6, ip));
1301 csum = &UDP(mtodo(m, hlen))->uh_sum;
1303 struct udphdr *udp = UDP(mtodo(m, hlen));
1304 *csum = cksum_adjust(*csum, udp->uh_dport, lport);
1305 udp->uh_dport = lport;
1307 *csum = cksum_add(*csum, ~nat64_cksum_convert(&ip6, ip));
1310 m = nat64_icmp_translate(m, &ip6, lport, hlen, cfg);
1311 if (m == NULL) /* stats already accounted */
1312 return (NAT64RETURN);
1316 mbufq_init(&mq, 255);
1317 nat64_fragment6(&cfg->stats, &ip6, &mq, m, nh.nh_mtu, ip_id, ip_off);
1318 while ((m = mbufq_dequeue(&mq)) != NULL) {
1319 if (V_nat64out->output(nh.nh_ifp, m, (struct sockaddr *)&dst,
1320 &cfg->stats, logdata) != 0)
1322 NAT64STAT_INC(&cfg->stats, opcnt46);
1325 return (NAT64RETURN);
1329 nat64_handle_icmp6(struct mbuf *m, int hlen, uint32_t aaddr, uint16_t aport,
1330 struct nat64_config *cfg, void *logdata)
1333 struct icmp6_hdr *icmp6;
1334 struct ip6_frag *ip6f;
1335 struct ip6_hdr *ip6, *ip6i;
1341 ip6 = mtod(m, struct ip6_hdr *);
1342 if (nat64_check_ip6(&ip6->ip6_src) != 0 ||
1343 nat64_check_ip6(&ip6->ip6_dst) != 0)
1346 proto = nat64_getlasthdr(m, &hlen);
1347 if (proto != IPPROTO_ICMPV6) {
1349 "dropped due to mbuf isn't contigious");
1350 NAT64STAT_INC(&cfg->stats, dropped);
1351 return (NAT64MFREE);
1356 * Translate ICMPv6 type and code to ICMPv4 (RFC7915).
1357 * NOTE: ICMPv6 echo handled by nat64_do_handle_ip6().
1359 icmp6 = mtodo(m, hlen);
1361 switch (icmp6->icmp6_type) {
1362 case ICMP6_DST_UNREACH:
1363 type = ICMP_UNREACH;
1364 switch (icmp6->icmp6_code) {
1365 case ICMP6_DST_UNREACH_NOROUTE:
1366 case ICMP6_DST_UNREACH_BEYONDSCOPE:
1367 case ICMP6_DST_UNREACH_ADDR:
1368 code = ICMP_UNREACH_HOST;
1370 case ICMP6_DST_UNREACH_ADMIN:
1371 code = ICMP_UNREACH_HOST_PROHIB;
1373 case ICMP6_DST_UNREACH_NOPORT:
1374 code = ICMP_UNREACH_PORT;
1377 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1378 " code %d", icmp6->icmp6_type,
1380 NAT64STAT_INC(&cfg->stats, dropped);
1381 return (NAT64MFREE);
1384 case ICMP6_PACKET_TOO_BIG:
1385 type = ICMP_UNREACH;
1386 code = ICMP_UNREACH_NEEDFRAG;
1387 mtu = ntohl(icmp6->icmp6_mtu);
1388 if (mtu < IPV6_MMTU) {
1389 DPRINTF(DP_DROPS, "Wrong MTU %d in ICMPv6 type %d,"
1390 " code %d", mtu, icmp6->icmp6_type,
1392 NAT64STAT_INC(&cfg->stats, dropped);
1393 return (NAT64MFREE);
1396 * Adjust MTU to reflect difference between
1397 * IPv6 an IPv4 headers.
1399 mtu -= sizeof(struct ip6_hdr) - sizeof(struct ip);
1401 case ICMP6_TIME_EXCEEDED:
1402 type = ICMP_TIMXCEED;
1403 code = icmp6->icmp6_code;
1405 case ICMP6_PARAM_PROB:
1406 switch (icmp6->icmp6_code) {
1407 case ICMP6_PARAMPROB_HEADER:
1408 type = ICMP_PARAMPROB;
1409 code = ICMP_PARAMPROB_ERRATPTR;
1410 mtu = ntohl(icmp6->icmp6_pptr);
1412 case 0: /* Version/Traffic Class */
1413 case 1: /* Traffic Class/Flow Label */
1415 case 4: /* Payload Length */
1419 case 6: /* Next Header */
1422 case 7: /* Hop Limit */
1426 if (mtu >= 8 && mtu <= 23) {
1427 mtu = 12; /* Source address */
1430 if (mtu >= 24 && mtu <= 39) {
1431 mtu = 16; /* Destination address */
1434 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1435 " code %d, pptr %d", icmp6->icmp6_type,
1436 icmp6->icmp6_code, mtu);
1437 NAT64STAT_INC(&cfg->stats, dropped);
1438 return (NAT64MFREE);
1440 case ICMP6_PARAMPROB_NEXTHEADER:
1441 type = ICMP_UNREACH;
1442 code = ICMP_UNREACH_PROTOCOL;
1445 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1446 " code %d, pptr %d", icmp6->icmp6_type,
1447 icmp6->icmp6_code, ntohl(icmp6->icmp6_pptr));
1448 NAT64STAT_INC(&cfg->stats, dropped);
1449 return (NAT64MFREE);
1453 DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d, code %d",
1454 icmp6->icmp6_type, icmp6->icmp6_code);
1455 NAT64STAT_INC(&cfg->stats, dropped);
1456 return (NAT64MFREE);
1459 hlen += sizeof(struct icmp6_hdr);
1460 if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
1461 NAT64STAT_INC(&cfg->stats, dropped);
1462 DPRINTF(DP_DROPS, "Message is too short %d",
1464 return (NAT64MFREE);
1467 * We need at least ICMP_MINLEN bytes of original datagram payload
1468 * to generate ICMP message. It is nice that ICMP_MINLEN is equal
1469 * to sizeof(struct ip6_frag). So, if embedded datagram had a fragment
1470 * header we will not have to do m_pullup() again.
1472 * What we have here:
1473 * Outer header: (IPv6iGW, v4mapPRefix+v4exthost)
1474 * Inner header: (v4mapPRefix+v4host, IPv6iHost) [sport, dport]
1475 * We need to translate it to:
1477 * Outer header: (alias_host, v4exthost)
1478 * Inner header: (v4exthost, alias_host) [sport, alias_port]
1480 * Assume caller function has checked if v4mapPRefix+v4host
1481 * matches configured prefix.
1482 * The only two things we should be provided with are mapping between
1483 * IPv6iHost <> alias_host and between dport and alias_port.
1485 if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
1486 m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
1488 NAT64STAT_INC(&cfg->stats, nomem);
1489 return (NAT64RETURN);
1491 ip6 = mtod(m, struct ip6_hdr *);
1492 ip6i = mtodo(m, hlen);
1494 proto = ip6i->ip6_nxt;
1495 plen = ntohs(ip6i->ip6_plen);
1496 hlen += sizeof(struct ip6_hdr);
1497 if (proto == IPPROTO_FRAGMENT) {
1498 if (m->m_pkthdr.len < hlen + sizeof(struct ip6_frag) +
1501 ip6f = mtodo(m, hlen);
1502 proto = ip6f->ip6f_nxt;
1503 plen -= sizeof(struct ip6_frag);
1504 hlen += sizeof(struct ip6_frag);
1505 /* Ajust MTU to reflect frag header size */
1506 if (type == ICMP_UNREACH && code == ICMP_UNREACH_NEEDFRAG)
1507 mtu -= sizeof(struct ip6_frag);
1509 if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
1510 DPRINTF(DP_DROPS, "Unsupported proto %d in the inner header",
1514 if (nat64_check_ip6(&ip6i->ip6_src) != 0 ||
1515 nat64_check_ip6(&ip6i->ip6_dst) != 0) {
1516 DPRINTF(DP_DROPS, "Inner addresses do not passes the check");
1519 /* Check if outer dst is the same as inner src */
1520 if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6i->ip6_src)) {
1521 DPRINTF(DP_DROPS, "Inner src doesn't match outer dst");
1525 /* Now we need to make a fake IPv4 packet to generate ICMP message */
1526 ip.ip_dst.s_addr = aaddr;
1527 ip.ip_src.s_addr = nat64_extract_ip4(&ip6i->ip6_src, cfg->plat_plen);
1528 if (ip.ip_src.s_addr == 0)
1530 /* XXX: Make fake ulp header */
1531 if (V_nat64out == &nat64_direct) /* init_ip4hdr will decrement it */
1532 ip6i->ip6_hlim += IPV6_HLIMDEC;
1533 nat64_init_ip4hdr(ip6i, ip6f, plen, proto, &ip);
1534 m_adj(m, hlen - sizeof(struct ip));
1535 bcopy(&ip, mtod(m, void *), sizeof(ip));
1536 nat64_icmp_reflect(m, type, code, (uint16_t)mtu, &cfg->stats,
1538 return (NAT64RETURN);
1541 * We must call m_freem() because mbuf pointer could be
1542 * changed with m_pullup().
1545 NAT64STAT_INC(&cfg->stats, dropped);
1546 return (NAT64RETURN);
1550 nat64_do_handle_ip6(struct mbuf *m, uint32_t aaddr, uint16_t aport,
1551 struct nat64_config *cfg, void *logdata)
1554 struct nhop4_basic nh;
1555 struct sockaddr_in dst;
1556 struct ip6_frag *frag;
1557 struct ip6_hdr *ip6;
1558 struct icmp6_hdr *icmp6;
1560 int plen, hlen, proto;
1563 * XXX: we expect ipfw_chk() did m_pullup() up to upper level
1564 * protocol's headers. Also we skip some checks, that ip6_input(),
1565 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
1567 ip6 = mtod(m, struct ip6_hdr *);
1568 if (nat64_check_ip6(&ip6->ip6_src) != 0 ||
1569 nat64_check_ip6(&ip6->ip6_dst) != 0) {
1573 /* Starting from this point we must not return zero */
1574 ip.ip_src.s_addr = aaddr;
1575 if (nat64_check_ip4(ip.ip_src.s_addr) != 0) {
1576 DPRINTF(DP_GENERIC | DP_DROPS, "invalid source address: %08x",
1578 NAT64STAT_INC(&cfg->stats, dropped);
1579 return (NAT64MFREE);
1582 ip.ip_dst.s_addr = nat64_extract_ip4(&ip6->ip6_dst, cfg->plat_plen);
1583 if (ip.ip_dst.s_addr == 0) {
1584 NAT64STAT_INC(&cfg->stats, dropped);
1585 return (NAT64MFREE);
1588 if (*V_nat64ip6stealth == 0 && ip6->ip6_hlim <= IPV6_HLIMDEC) {
1589 nat64_icmp6_reflect(m, ICMP6_TIME_EXCEEDED,
1590 ICMP6_TIME_EXCEED_TRANSIT, 0, &cfg->stats, logdata);
1591 return (NAT64RETURN);
1595 plen = ntohs(ip6->ip6_plen);
1596 proto = nat64_getlasthdr(m, &hlen);
1598 DPRINTF(DP_DROPS, "dropped due to mbuf isn't contigious");
1599 NAT64STAT_INC(&cfg->stats, dropped);
1600 return (NAT64MFREE);
1603 if (proto == IPPROTO_FRAGMENT) {
1604 /* ipfw_chk should m_pullup up to frag header */
1605 if (m->m_len < hlen + sizeof(*frag)) {
1607 "dropped due to mbuf isn't contigious");
1608 NAT64STAT_INC(&cfg->stats, dropped);
1609 return (NAT64MFREE);
1611 frag = mtodo(m, hlen);
1612 proto = frag->ip6f_nxt;
1613 hlen += sizeof(*frag);
1614 /* Fragmented ICMPv6 is unsupported */
1615 if (proto == IPPROTO_ICMPV6) {
1616 DPRINTF(DP_DROPS, "dropped due to fragmented ICMPv6");
1617 NAT64STAT_INC(&cfg->stats, dropped);
1618 return (NAT64MFREE);
1620 /* Fragment length must be multiple of 8 octets */
1621 if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0 &&
1622 ((plen + sizeof(struct ip6_hdr) - hlen) & 0x7) != 0) {
1623 nat64_icmp6_reflect(m, ICMP6_PARAM_PROB,
1624 ICMP6_PARAMPROB_HEADER,
1625 offsetof(struct ip6_hdr, ip6_plen), &cfg->stats,
1627 return (NAT64RETURN);
1630 plen -= hlen - sizeof(struct ip6_hdr);
1631 if (plen < 0 || m->m_pkthdr.len < plen + hlen) {
1632 DPRINTF(DP_DROPS, "plen %d, pkthdr.len %d, hlen %d",
1633 plen, m->m_pkthdr.len, hlen);
1634 NAT64STAT_INC(&cfg->stats, dropped);
1635 return (NAT64MFREE);
1638 icmp6 = NULL; /* Make gcc happy */
1639 if (proto == IPPROTO_ICMPV6) {
1640 icmp6 = mtodo(m, hlen);
1641 if (icmp6->icmp6_type != ICMP6_ECHO_REQUEST &&
1642 icmp6->icmp6_type != ICMP6_ECHO_REPLY)
1643 return (nat64_handle_icmp6(m, hlen, aaddr, aport,
1646 dst.sin_addr.s_addr = ip.ip_dst.s_addr;
1647 if (nat64_find_route4(&nh, &dst, m) != 0) {
1648 NAT64STAT_INC(&cfg->stats, noroute4);
1649 nat64_icmp6_reflect(m, ICMP6_DST_UNREACH,
1650 ICMP6_DST_UNREACH_NOROUTE, 0, &cfg->stats, logdata);
1651 return (NAT64RETURN);
1653 if (nh.nh_mtu < plen + sizeof(ip)) {
1654 nat64_icmp6_reflect(m, ICMP6_PACKET_TOO_BIG, 0, nh.nh_mtu,
1655 &cfg->stats, logdata);
1656 return (NAT64RETURN);
1658 nat64_init_ip4hdr(ip6, frag, plen, proto, &ip);
1659 /* Convert checksums. */
1662 csum = &TCP(mtodo(m, hlen))->th_sum;
1664 struct tcphdr *tcp = TCP(mtodo(m, hlen));
1665 *csum = cksum_adjust(*csum, tcp->th_sport, aport);
1666 tcp->th_sport = aport;
1668 *csum = cksum_add(*csum, nat64_cksum_convert(ip6, &ip));
1671 csum = &UDP(mtodo(m, hlen))->uh_sum;
1673 struct udphdr *udp = UDP(mtodo(m, hlen));
1674 *csum = cksum_adjust(*csum, udp->uh_sport, aport);
1675 udp->uh_sport = aport;
1677 *csum = cksum_add(*csum, nat64_cksum_convert(ip6, &ip));
1679 case IPPROTO_ICMPV6:
1680 /* Checksum in ICMPv6 covers pseudo header */
1681 csum = &icmp6->icmp6_cksum;
1682 *csum = cksum_add(*csum, in6_cksum_pseudo(ip6, plen,
1683 IPPROTO_ICMPV6, 0));
1684 /* Convert ICMPv6 types to ICMP */
1685 proto = *(uint16_t *)icmp6; /* save old word for cksum_adjust */
1686 if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST)
1687 icmp6->icmp6_type = ICMP_ECHO;
1688 else /* ICMP6_ECHO_REPLY */
1689 icmp6->icmp6_type = ICMP_ECHOREPLY;
1690 *csum = cksum_adjust(*csum, (uint16_t)proto,
1691 *(uint16_t *)icmp6);
1693 uint16_t old_id = icmp6->icmp6_id;
1694 icmp6->icmp6_id = aport;
1695 *csum = cksum_adjust(*csum, old_id, aport);
1700 m_adj(m, hlen - sizeof(ip));
1701 bcopy(&ip, mtod(m, void *), sizeof(ip));
1702 if (V_nat64out->output(nh.nh_ifp, m, (struct sockaddr *)&dst,
1703 &cfg->stats, logdata) == 0)
1704 NAT64STAT_INC(&cfg->stats, opcnt64);
1705 return (NAT64RETURN);