1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015-2019 Yandex LLC
5  * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ipstealth.h"
33 
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>
39 #include <sys/lock.h>
40 #include <sys/mbuf.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>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_pflog.h>
50 #include <net/pfil.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
53 #include <net/route/nhop.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_fib.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_fw.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #include <netinet/ip_icmp.h>
64 #include <netinet/tcp.h>
65 #include <netinet/udp.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_fib.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/ip_fw_nat64.h>
70 
71 #include <netpfil/pf/pf.h>
72 #include <netpfil/ipfw/ip_fw_private.h>
73 #include <machine/in_cksum.h>
74 
75 #include "ip_fw_nat64.h"
76 #include "nat64_translate.h"
77 
78 
79 typedef int (*nat64_output_t)(struct ifnet *, struct mbuf *,
80     struct sockaddr *, struct nat64_counters *, void *);
81 typedef int (*nat64_output_one_t)(struct mbuf *, struct nat64_counters *,
82     void *);
83 
84 static struct nhop_object *nat64_find_route4(struct sockaddr_in *,
85     struct mbuf *);
86 static struct nhop_object *nat64_find_route6(struct sockaddr_in6 *,
87     struct mbuf *);
88 static int nat64_output_one(struct mbuf *, struct nat64_counters *, void *);
89 static int nat64_output(struct ifnet *, struct mbuf *, struct sockaddr *,
90     struct nat64_counters *, void *);
91 static int nat64_direct_output_one(struct mbuf *, struct nat64_counters *,
92     void *);
93 static int nat64_direct_output(struct ifnet *, struct mbuf *,
94     struct sockaddr *, struct nat64_counters *, void *);
95 
96 struct nat64_methods {
97 	nat64_output_t		output;
98 	nat64_output_one_t	output_one;
99 };
100 static const struct nat64_methods nat64_netisr = {
101 	.output = nat64_output,
102 	.output_one = nat64_output_one
103 };
104 static const struct nat64_methods nat64_direct = {
105 	.output = nat64_direct_output,
106 	.output_one = nat64_direct_output_one
107 };
108 
109 /* These variables should be initialized explicitly on module loading */
110 VNET_DEFINE_STATIC(const struct nat64_methods *, nat64out);
111 VNET_DEFINE_STATIC(const int *, nat64ipstealth);
112 VNET_DEFINE_STATIC(const int *, nat64ip6stealth);
113 #define	V_nat64out		VNET(nat64out)
114 #define	V_nat64ipstealth	VNET(nat64ipstealth)
115 #define	V_nat64ip6stealth	VNET(nat64ip6stealth)
116 
117 static const int stealth_on = 1;
118 #ifndef IPSTEALTH
119 static const int stealth_off = 0;
120 #endif
121 
122 void
123 nat64_set_output_method(int direct)
124 {
125 
126 	if (direct != 0) {
127 		V_nat64out = &nat64_direct;
128 #ifdef IPSTEALTH
129 		/* Honor corresponding variables, if IPSTEALTH is defined */
130 		V_nat64ipstealth = &V_ipstealth;
131 		V_nat64ip6stealth = &V_ip6stealth;
132 #else
133 		/* otherwise we need to decrement HLIM/TTL for direct case */
134 		V_nat64ipstealth = V_nat64ip6stealth = &stealth_off;
135 #endif
136 	} else {
137 		V_nat64out = &nat64_netisr;
138 		/* Leave TTL/HLIM decrementing to forwarding code */
139 		V_nat64ipstealth = V_nat64ip6stealth = &stealth_on;
140 	}
141 }
142 
143 int
144 nat64_get_output_method(void)
145 {
146 
147 	return (V_nat64out == &nat64_direct ? 1: 0);
148 }
149 
150 static void
151 nat64_log(struct pfloghdr *logdata, struct mbuf *m, sa_family_t family)
152 {
153 
154 	logdata->dir = PF_OUT;
155 	logdata->af = family;
156 	ipfw_bpf_mtap2(logdata, PFLOG_HDRLEN, m);
157 }
158 
159 static int
160 nat64_direct_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
161     struct nat64_counters *stats, void *logdata)
162 {
163 	int error;
164 
165 	if (logdata != NULL)
166 		nat64_log(logdata, m, dst->sa_family);
167 	error = (*ifp->if_output)(ifp, m, dst, NULL);
168 	if (error != 0)
169 		NAT64STAT_INC(stats, oerrors);
170 	return (error);
171 }
172 
173 static int
174 nat64_direct_output_one(struct mbuf *m, struct nat64_counters *stats,
175     void *logdata)
176 {
177 	struct nhop_object *nh4 = NULL;
178 	struct nhop_object *nh6 = NULL;
179 	struct sockaddr_in6 dst6;
180 	struct sockaddr_in dst4;
181 	struct sockaddr *dst;
182 	struct ip6_hdr *ip6;
183 	struct ip *ip4;
184 	struct ifnet *ifp;
185 	int error;
186 
187 	ip4 = mtod(m, struct ip *);
188 	error = 0;
189 	switch (ip4->ip_v) {
190 	case IPVERSION:
191 		dst4.sin_addr = ip4->ip_dst;
192 		nh4 = nat64_find_route4(&dst4, m);
193 		if (nh4 == NULL) {
194 			NAT64STAT_INC(stats, noroute4);
195 			error = EHOSTUNREACH;
196 		} else {
197 			ifp = nh4->nh_ifp;
198 			dst = (struct sockaddr *)&dst4;
199 		}
200 		break;
201 	case (IPV6_VERSION >> 4):
202 		ip6 = mtod(m, struct ip6_hdr *);
203 		dst6.sin6_addr = ip6->ip6_dst;
204 		nh6 = nat64_find_route6(&dst6, m);
205 		if (nh6 == NULL) {
206 			NAT64STAT_INC(stats, noroute6);
207 			error = EHOSTUNREACH;
208 		} else {
209 			ifp = nh6->nh_ifp;
210 			dst = (struct sockaddr *)&dst6;
211 		}
212 		break;
213 	default:
214 		m_freem(m);
215 		NAT64STAT_INC(stats, dropped);
216 		DPRINTF(DP_DROPS, "dropped due to unknown IP version");
217 		return (EAFNOSUPPORT);
218 	}
219 	if (error != 0) {
220 		m_freem(m);
221 		return (EHOSTUNREACH);
222 	}
223 	if (logdata != NULL)
224 		nat64_log(logdata, m, dst->sa_family);
225 	error = (*ifp->if_output)(ifp, m, dst, NULL);
226 	if (error != 0)
227 		NAT64STAT_INC(stats, oerrors);
228 	return (error);
229 }
230 
231 static int
232 nat64_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
233     struct nat64_counters *stats, void *logdata)
234 {
235 	struct ip *ip4;
236 	int ret, af;
237 
238 	ip4 = mtod(m, struct ip *);
239 	switch (ip4->ip_v) {
240 	case IPVERSION:
241 		af = AF_INET;
242 		ret = NETISR_IP;
243 		break;
244 	case (IPV6_VERSION >> 4):
245 		af = AF_INET6;
246 		ret = NETISR_IPV6;
247 		break;
248 	default:
249 		m_freem(m);
250 		NAT64STAT_INC(stats, dropped);
251 		DPRINTF(DP_DROPS, "unknown IP version");
252 		return (EAFNOSUPPORT);
253 	}
254 	if (logdata != NULL)
255 		nat64_log(logdata, m, af);
256 	if (m->m_pkthdr.rcvif == NULL)
257 		m->m_pkthdr.rcvif = V_loif;
258 	ret = netisr_queue(ret, m);
259 	if (ret != 0)
260 		NAT64STAT_INC(stats, oerrors);
261 	return (ret);
262 }
263 
264 static int
265 nat64_output_one(struct mbuf *m, struct nat64_counters *stats, void *logdata)
266 {
267 
268 	return (nat64_output(NULL, m, NULL, stats, logdata));
269 }
270 
271 /*
272  * Check the given IPv6 prefix and length according to RFC6052:
273  *   The prefixes can only have one of the following lengths:
274  *   32, 40, 48, 56, 64, or 96 (The Well-Known Prefix is 96 bits long).
275  * Returns zero on success, otherwise EINVAL.
276  */
277 int
278 nat64_check_prefixlen(int length)
279 {
280 
281 	switch (length) {
282 	case 32:
283 	case 40:
284 	case 48:
285 	case 56:
286 	case 64:
287 	case 96:
288 		return (0);
289 	}
290 	return (EINVAL);
291 }
292 
293 int
294 nat64_check_prefix6(const struct in6_addr *prefix, int length)
295 {
296 
297 	if (nat64_check_prefixlen(length) != 0)
298 		return (EINVAL);
299 
300 	/* Well-known prefix has 96 prefix length */
301 	if (IN6_IS_ADDR_WKPFX(prefix) && length != 96)
302 		return (EINVAL);
303 
304 	/* Bits 64 to 71 must be set to zero */
305 	if (prefix->__u6_addr.__u6_addr8[8] != 0)
306 		return (EINVAL);
307 
308 	/* Some extra checks */
309 	if (IN6_IS_ADDR_MULTICAST(prefix) ||
310 	    IN6_IS_ADDR_UNSPECIFIED(prefix) ||
311 	    IN6_IS_ADDR_LOOPBACK(prefix))
312 		return (EINVAL);
313 	return (0);
314 }
315 
316 int
317 nat64_check_private_ip4(const struct nat64_config *cfg, in_addr_t ia)
318 {
319 
320 	if (cfg->flags & NAT64_ALLOW_PRIVATE)
321 		return (0);
322 
323 	/* WKPFX must not be used to represent non-global IPv4 addresses */
324 	if (cfg->flags & NAT64_WKPFX) {
325 		/* IN_PRIVATE */
326 		if ((ia & htonl(0xff000000)) == htonl(0x0a000000) ||
327 		    (ia & htonl(0xfff00000)) == htonl(0xac100000) ||
328 		    (ia & htonl(0xffff0000)) == htonl(0xc0a80000))
329 			return (1);
330 		/*
331 		 * RFC 5735:
332 		 *  192.0.0.0/24 - reserved for IETF protocol assignments
333 		 *  192.88.99.0/24 - for use as 6to4 relay anycast addresses
334 		 *  198.18.0.0/15 - for use in benchmark tests
335 		 *  192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 - for use
336 		 *   in documentation and example code
337 		 */
338 		if ((ia & htonl(0xffffff00)) == htonl(0xc0000000) ||
339 		    (ia & htonl(0xffffff00)) == htonl(0xc0586300) ||
340 		    (ia & htonl(0xfffffe00)) == htonl(0xc6120000) ||
341 		    (ia & htonl(0xffffff00)) == htonl(0xc0000200) ||
342 		    (ia & htonl(0xfffffe00)) == htonl(0xc6336400) ||
343 		    (ia & htonl(0xffffff00)) == htonl(0xcb007100))
344 			return (1);
345 	}
346 	return (0);
347 }
348 
349 /*
350  * Embed @ia IPv4 address into @ip6 IPv6 address.
351  * Place to embedding determined from prefix length @plen.
352  */
353 void
354 nat64_embed_ip4(struct in6_addr *ip6, int plen, in_addr_t ia)
355 {
356 
357 	switch (plen) {
358 	case 32:
359 	case 96:
360 		ip6->s6_addr32[plen / 32] = ia;
361 		break;
362 	case 40:
363 	case 48:
364 	case 56:
365 		/*
366 		 * Preserve prefix bits.
367 		 * Since suffix bits should be zero and reserved for future
368 		 * use, we just overwrite the whole word, where they are.
369 		 */
370 		ip6->s6_addr32[1] &= 0xffffffff << (32 - plen % 32);
371 #if BYTE_ORDER == BIG_ENDIAN
372 		ip6->s6_addr32[1] |= ia >> (plen % 32);
373 		ip6->s6_addr32[2] = ia << (24 - plen % 32);
374 #elif BYTE_ORDER == LITTLE_ENDIAN
375 		ip6->s6_addr32[1] |= ia << (plen % 32);
376 		ip6->s6_addr32[2] = ia >> (24 - plen % 32);
377 #endif
378 		break;
379 	case 64:
380 #if BYTE_ORDER == BIG_ENDIAN
381 		ip6->s6_addr32[2] = ia >> 8;
382 		ip6->s6_addr32[3] = ia << 24;
383 #elif BYTE_ORDER == LITTLE_ENDIAN
384 		ip6->s6_addr32[2] = ia << 8;
385 		ip6->s6_addr32[3] = ia >> 24;
386 #endif
387 		break;
388 	default:
389 		panic("Wrong plen: %d", plen);
390 	};
391 	/*
392 	 * Bits 64 to 71 of the address are reserved for compatibility
393 	 * with the host identifier format defined in the IPv6 addressing
394 	 * architecture [RFC4291]. These bits MUST be set to zero.
395 	 */
396 	ip6->s6_addr8[8] = 0;
397 }
398 
399 in_addr_t
400 nat64_extract_ip4(const struct in6_addr *ip6, int plen)
401 {
402 	in_addr_t ia;
403 
404 	/*
405 	 * According to RFC 6052 p2.2:
406 	 * IPv4-embedded IPv6 addresses are composed of a variable-length
407 	 * prefix, the embedded IPv4 address, and a variable length suffix.
408 	 * The suffix bits are reserved for future extensions and SHOULD
409 	 * be set to zero.
410 	 */
411 	switch (plen) {
412 	case 32:
413 		if (ip6->s6_addr32[3] != 0 || ip6->s6_addr32[2] != 0)
414 			goto badip6;
415 		break;
416 	case 40:
417 		if (ip6->s6_addr32[3] != 0 ||
418 		    (ip6->s6_addr32[2] & htonl(0xff00ffff)) != 0)
419 			goto badip6;
420 		break;
421 	case 48:
422 		if (ip6->s6_addr32[3] != 0 ||
423 		    (ip6->s6_addr32[2] & htonl(0xff0000ff)) != 0)
424 			goto badip6;
425 		break;
426 	case 56:
427 		if (ip6->s6_addr32[3] != 0 || ip6->s6_addr8[8] != 0)
428 			goto badip6;
429 		break;
430 	case 64:
431 		if (ip6->s6_addr8[8] != 0 ||
432 		    (ip6->s6_addr32[3] & htonl(0x00ffffff)) != 0)
433 			goto badip6;
434 	};
435 	switch (plen) {
436 	case 32:
437 	case 96:
438 		ia = ip6->s6_addr32[plen / 32];
439 		break;
440 	case 40:
441 	case 48:
442 	case 56:
443 #if BYTE_ORDER == BIG_ENDIAN
444 		ia = (ip6->s6_addr32[1] << (plen % 32)) |
445 		    (ip6->s6_addr32[2] >> (24 - plen % 32));
446 #elif BYTE_ORDER == LITTLE_ENDIAN
447 		ia = (ip6->s6_addr32[1] >> (plen % 32)) |
448 		    (ip6->s6_addr32[2] << (24 - plen % 32));
449 #endif
450 		break;
451 	case 64:
452 #if BYTE_ORDER == BIG_ENDIAN
453 		ia = (ip6->s6_addr32[2] << 8) | (ip6->s6_addr32[3] >> 24);
454 #elif BYTE_ORDER == LITTLE_ENDIAN
455 		ia = (ip6->s6_addr32[2] >> 8) | (ip6->s6_addr32[3] << 24);
456 #endif
457 		break;
458 	default:
459 		return (0);
460 	};
461 	if (nat64_check_ip4(ia) == 0)
462 		return (ia);
463 
464 	DPRINTF(DP_GENERIC | DP_DROPS,
465 	    "invalid destination address: %08x", ia);
466 	return (0);
467 badip6:
468 	DPRINTF(DP_GENERIC | DP_DROPS, "invalid IPv4-embedded IPv6 address");
469 	return (0);
470 }
471 
472 /*
473  * According to RFC 1624 the equation for incremental checksum update is:
474  *	HC' = ~(~HC + ~m + m')	--	[Eqn. 3]
475  *	HC' = HC - ~m - m'	--	[Eqn. 4]
476  * So, when we are replacing IPv4 addresses to IPv6, we
477  * can assume, that new bytes previously were zeros, and vise versa -
478  * when we replacing IPv6 addresses to IPv4, now unused bytes become
479  * zeros. The payload length in pseudo header has bigger size, but one
480  * half of it should be zero. Using the equation 4 we get:
481  *	HC' = HC - (~m0 + m0')	-- m0 is first changed word
482  *	HC' = (HC - (~m0 + m0')) - (~m1 + m1')	-- m1 is second changed word
483  *	HC' = HC - ~m0 - m0' - ~m1 - m1' - ... =
484  *	  = HC - sum(~m[i] + m'[i])
485  *
486  * The function result should be used as follows:
487  *	IPv6 to IPv4:	HC' = cksum_add(HC, result)
488  *	IPv4 to IPv6:	HC' = cksum_add(HC, ~result)
489  */
490 static uint16_t
491 nat64_cksum_convert(struct ip6_hdr *ip6, struct ip *ip)
492 {
493 	uint32_t sum;
494 	uint16_t *p;
495 
496 	sum = ~ip->ip_src.s_addr >> 16;
497 	sum += ~ip->ip_src.s_addr & 0xffff;
498 	sum += ~ip->ip_dst.s_addr >> 16;
499 	sum += ~ip->ip_dst.s_addr & 0xffff;
500 
501 	for (p = (uint16_t *)&ip6->ip6_src;
502 	    p < (uint16_t *)(&ip6->ip6_src + 2); p++)
503 		sum += *p;
504 
505 	while (sum >> 16)
506 		sum = (sum & 0xffff) + (sum >> 16);
507 	return (sum);
508 }
509 
510 static void
511 nat64_init_ip4hdr(const struct ip6_hdr *ip6, const struct ip6_frag *frag,
512     uint16_t plen, uint8_t proto, struct ip *ip)
513 {
514 
515 	/* assume addresses are already initialized */
516 	ip->ip_v = IPVERSION;
517 	ip->ip_hl = sizeof(*ip) >> 2;
518 	ip->ip_tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
519 	ip->ip_len = htons(sizeof(*ip) + plen);
520 	ip->ip_ttl = ip6->ip6_hlim;
521 	if (*V_nat64ip6stealth == 0)
522 		ip->ip_ttl -= IPV6_HLIMDEC;
523 	ip->ip_sum = 0;
524 	ip->ip_p = (proto == IPPROTO_ICMPV6) ? IPPROTO_ICMP: proto;
525 	ip_fillid(ip);
526 	if (frag != NULL) {
527 		ip->ip_off = htons(ntohs(frag->ip6f_offlg) >> 3);
528 		if (frag->ip6f_offlg & IP6F_MORE_FRAG)
529 			ip->ip_off |= htons(IP_MF);
530 	} else {
531 		ip->ip_off = htons(IP_DF);
532 	}
533 	ip->ip_sum = in_cksum_hdr(ip);
534 }
535 
536 #define	FRAGSZ(mtu) ((mtu) - sizeof(struct ip6_hdr) - sizeof(struct ip6_frag))
537 static NAT64NOINLINE int
538 nat64_fragment6(struct nat64_counters *stats, struct ip6_hdr *ip6,
539     struct mbufq *mq, struct mbuf *m, uint32_t mtu, uint16_t ip_id,
540     uint16_t ip_off)
541 {
542 	struct ip6_frag ip6f;
543 	struct mbuf *n;
544 	uint16_t hlen, len, offset;
545 	int plen;
546 
547 	plen = ntohs(ip6->ip6_plen);
548 	hlen = sizeof(struct ip6_hdr);
549 
550 	/* Fragmentation isn't needed */
551 	if (ip_off == 0 && plen <= mtu - hlen) {
552 		M_PREPEND(m, hlen, M_NOWAIT);
553 		if (m == NULL) {
554 			NAT64STAT_INC(stats, nomem);
555 			return (ENOMEM);
556 		}
557 		bcopy(ip6, mtod(m, void *), hlen);
558 		if (mbufq_enqueue(mq, m) != 0) {
559 			m_freem(m);
560 			NAT64STAT_INC(stats, dropped);
561 			DPRINTF(DP_DROPS, "dropped due to mbufq overflow");
562 			return (ENOBUFS);
563 		}
564 		return (0);
565 	}
566 
567 	hlen += sizeof(struct ip6_frag);
568 	ip6f.ip6f_reserved = 0;
569 	ip6f.ip6f_nxt = ip6->ip6_nxt;
570 	ip6->ip6_nxt = IPPROTO_FRAGMENT;
571 	if (ip_off != 0) {
572 		/*
573 		 * We have got an IPv4 fragment.
574 		 * Use offset value and ip_id from original fragment.
575 		 */
576 		ip6f.ip6f_ident = htonl(ntohs(ip_id));
577 		offset = (ntohs(ip_off) & IP_OFFMASK) << 3;
578 		NAT64STAT_INC(stats, ifrags);
579 	} else {
580 		/* The packet size exceeds interface MTU */
581 		ip6f.ip6f_ident = htonl(ip6_randomid());
582 		offset = 0; /* First fragment*/
583 	}
584 	while (plen > 0 && m != NULL) {
585 		n = NULL;
586 		len = FRAGSZ(mtu) & ~7;
587 		if (len > plen)
588 			len = plen;
589 		ip6->ip6_plen = htons(len + sizeof(ip6f));
590 		ip6f.ip6f_offlg = ntohs(offset);
591 		if (len < plen || (ip_off & htons(IP_MF)) != 0)
592 			ip6f.ip6f_offlg |= IP6F_MORE_FRAG;
593 		offset += len;
594 		plen -= len;
595 		if (plen > 0) {
596 			n = m_split(m, len, M_NOWAIT);
597 			if (n == NULL)
598 				goto fail;
599 		}
600 		M_PREPEND(m, hlen, M_NOWAIT);
601 		if (m == NULL)
602 			goto fail;
603 		bcopy(ip6, mtod(m, void *), sizeof(struct ip6_hdr));
604 		bcopy(&ip6f, mtodo(m, sizeof(struct ip6_hdr)),
605 		    sizeof(struct ip6_frag));
606 		if (mbufq_enqueue(mq, m) != 0)
607 			goto fail;
608 		m = n;
609 	}
610 	NAT64STAT_ADD(stats, ofrags, mbufq_len(mq));
611 	return (0);
612 fail:
613 	if (m != NULL)
614 		m_freem(m);
615 	if (n != NULL)
616 		m_freem(n);
617 	mbufq_drain(mq);
618 	NAT64STAT_INC(stats, nomem);
619 	return (ENOMEM);
620 }
621 
622 static struct nhop_object *
623 nat64_find_route6(struct sockaddr_in6 *dst, struct mbuf *m)
624 {
625 	struct nhop_object *nh;
626 	NET_EPOCH_ASSERT();
627 	nh = fib6_lookup(M_GETFIB(m), &dst->sin6_addr, 0, 0, 0);
628 	if (nh == NULL)
629 		return NULL;
630 	if (nh->nh_flags & (NHF_BLACKHOLE | NHF_REJECT))
631 		return NULL;
632 	/*
633 	 * XXX: we need to use destination address with embedded scope
634 	 * zone id, because LLTABLE uses such form of addresses for lookup.
635 	 */
636 	dst->sin6_family = AF_INET6;
637 	dst->sin6_len = sizeof(*dst);
638 	dst->sin6_addr = ifatoia6(nh->nh_ifa)->ia_addr.sin6_addr;
639 	if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
640 		dst->sin6_addr.s6_addr16[1] =
641 		    htons(nh->nh_ifp->if_index & 0xffff);
642 	dst->sin6_port = 0;
643 	dst->sin6_scope_id = 0;
644 	dst->sin6_flowinfo = 0;
645 
646 	return nh;
647 }
648 
649 #define	NAT64_ICMP6_PLEN	64
650 static NAT64NOINLINE void
651 nat64_icmp6_reflect(struct mbuf *m, uint8_t type, uint8_t code, uint32_t mtu,
652     struct nat64_counters *stats, void *logdata)
653 {
654 	struct icmp6_hdr *icmp6;
655 	struct ip6_hdr *ip6, *oip6;
656 	struct mbuf *n;
657 	int len, plen, proto;
658 
659 	len = 0;
660 	proto = nat64_getlasthdr(m, &len);
661 	if (proto < 0) {
662 		DPRINTF(DP_DROPS, "mbuf isn't contigious");
663 		goto freeit;
664 	}
665 	/*
666 	 * Do not send ICMPv6 in reply to ICMPv6 errors.
667 	 */
668 	if (proto == IPPROTO_ICMPV6) {
669 		if (m->m_len < len + sizeof(*icmp6)) {
670 			DPRINTF(DP_DROPS, "mbuf isn't contigious");
671 			goto freeit;
672 		}
673 		icmp6 = mtodo(m, len);
674 		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST ||
675 		    icmp6->icmp6_type == ND_REDIRECT) {
676 			DPRINTF(DP_DROPS, "do not send ICMPv6 in reply to "
677 			    "ICMPv6 errors");
678 			goto freeit;
679 		}
680 		/*
681 		 * If there are extra headers between IPv6 and ICMPv6,
682 		 * strip off them.
683 		 */
684 		if (len > sizeof(struct ip6_hdr)) {
685 			/*
686 			 * NOTE: ipfw_chk already did m_pullup() and it is
687 			 * expected that data is contigious from the start
688 			 * of IPv6 header up to the end of ICMPv6 header.
689 			 */
690 			bcopy(mtod(m, caddr_t),
691 			    mtodo(m, len - sizeof(struct ip6_hdr)),
692 			    sizeof(struct ip6_hdr));
693 			m_adj(m, len - sizeof(struct ip6_hdr));
694 		}
695 	}
696 	/*
697 	if (icmp6_ratelimit(&ip6->ip6_src, type, code))
698 		goto freeit;
699 		*/
700 	ip6 = mtod(m, struct ip6_hdr *);
701 	switch (type) {
702 	case ICMP6_DST_UNREACH:
703 	case ICMP6_PACKET_TOO_BIG:
704 	case ICMP6_TIME_EXCEEDED:
705 	case ICMP6_PARAM_PROB:
706 		break;
707 	default:
708 		goto freeit;
709 	}
710 	/* Calculate length of ICMPv6 payload */
711 	len = (m->m_pkthdr.len > NAT64_ICMP6_PLEN) ? NAT64_ICMP6_PLEN:
712 	    m->m_pkthdr.len;
713 
714 	/* Create new ICMPv6 datagram */
715 	plen = len + sizeof(struct icmp6_hdr);
716 	n = m_get2(sizeof(struct ip6_hdr) + plen + max_hdr, M_NOWAIT,
717 	    MT_HEADER, M_PKTHDR);
718 	if (n == NULL) {
719 		NAT64STAT_INC(stats, nomem);
720 		m_freem(m);
721 		return;
722 	}
723 	/*
724 	 * Move pkthdr from original mbuf. We should have initialized some
725 	 * fields, because we can reinject this mbuf to netisr and it will
726 	 * go trough input path (it requires at least rcvif should be set).
727 	 * Also do M_ALIGN() to reduce chances of need to allocate new mbuf
728 	 * in the chain, when we will do M_PREPEND() or make some type of
729 	 * tunneling.
730 	 */
731 	m_move_pkthdr(n, m);
732 	M_ALIGN(n, sizeof(struct ip6_hdr) + plen + max_hdr);
733 
734 	n->m_len = n->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
735 	oip6 = mtod(n, struct ip6_hdr *);
736 	/*
737 	 * Make IPv6 source address selection for reflected datagram.
738 	 * nat64_check_ip6() doesn't allow scoped addresses, therefore
739 	 * we use zero scopeid.
740 	 */
741 	if (in6_selectsrc_addr(M_GETFIB(n), &ip6->ip6_src, 0,
742 	    n->m_pkthdr.rcvif, &oip6->ip6_src, NULL) != 0) {
743 		/*
744 		 * Failed to find proper source address, drop the packet.
745 		 */
746 		m_freem(n);
747 		goto freeit;
748 	}
749 	oip6->ip6_dst = ip6->ip6_src;
750 	oip6->ip6_nxt = IPPROTO_ICMPV6;
751 	oip6->ip6_flow = 0;
752 	oip6->ip6_vfc |= IPV6_VERSION;
753 	oip6->ip6_hlim = V_ip6_defhlim;
754 	oip6->ip6_plen = htons(plen);
755 
756 	icmp6 = mtodo(n, sizeof(struct ip6_hdr));
757 	icmp6->icmp6_cksum = 0;
758 	icmp6->icmp6_type = type;
759 	icmp6->icmp6_code = code;
760 	icmp6->icmp6_mtu = htonl(mtu);
761 
762 	m_copydata(m, 0, len, mtodo(n, sizeof(struct ip6_hdr) +
763 	    sizeof(struct icmp6_hdr)));
764 	icmp6->icmp6_cksum = in6_cksum(n, IPPROTO_ICMPV6,
765 	    sizeof(struct ip6_hdr), plen);
766 	m_freem(m);
767 	V_nat64out->output_one(n, stats, logdata);
768 	return;
769 freeit:
770 	NAT64STAT_INC(stats, dropped);
771 	m_freem(m);
772 }
773 
774 static struct nhop_object *
775 nat64_find_route4(struct sockaddr_in *dst, struct mbuf *m)
776 {
777 	struct nhop_object *nh;
778 
779 	NET_EPOCH_ASSERT();
780 	nh = fib4_lookup(M_GETFIB(m), dst->sin_addr, 0, 0, 0);
781 	if (nh == NULL)
782 		return NULL;
783 	if (nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST | NHF_REJECT))
784 		return NULL;
785 
786 	dst->sin_family = AF_INET;
787 	dst->sin_len = sizeof(*dst);
788 	dst->sin_addr = IA_SIN(nh->nh_ifa)->sin_addr;
789 	dst->sin_port = 0;
790 	return nh;
791 }
792 
793 #define	NAT64_ICMP_PLEN	64
794 static NAT64NOINLINE void
795 nat64_icmp_reflect(struct mbuf *m, uint8_t type,
796     uint8_t code, uint16_t mtu, struct nat64_counters *stats, void *logdata)
797 {
798 	struct icmp *icmp;
799 	struct ip *ip, *oip;
800 	struct mbuf *n;
801 	int len, plen;
802 
803 	ip = mtod(m, struct ip *);
804 	/* Do not send ICMP error if packet is not the first fragment */
805 	if (ip->ip_off & ~ntohs(IP_MF|IP_DF)) {
806 		DPRINTF(DP_DROPS, "not first fragment");
807 		goto freeit;
808 	}
809 	/* Do not send ICMP in reply to ICMP errors */
810 	if (ip->ip_p == IPPROTO_ICMP) {
811 		if (m->m_len < (ip->ip_hl << 2)) {
812 			DPRINTF(DP_DROPS, "mbuf isn't contigious");
813 			goto freeit;
814 		}
815 		icmp = mtodo(m, ip->ip_hl << 2);
816 		if (!ICMP_INFOTYPE(icmp->icmp_type)) {
817 			DPRINTF(DP_DROPS, "do not send ICMP in reply to "
818 			    "ICMP errors");
819 			goto freeit;
820 		}
821 	}
822 	switch (type) {
823 	case ICMP_UNREACH:
824 	case ICMP_TIMXCEED:
825 	case ICMP_PARAMPROB:
826 		break;
827 	default:
828 		goto freeit;
829 	}
830 	/* Calculate length of ICMP payload */
831 	len = (m->m_pkthdr.len > NAT64_ICMP_PLEN) ? (ip->ip_hl << 2) + 8:
832 	    m->m_pkthdr.len;
833 
834 	/* Create new ICMPv4 datagram */
835 	plen = len + sizeof(struct icmphdr) + sizeof(uint32_t);
836 	n = m_get2(sizeof(struct ip) + plen + max_hdr, M_NOWAIT,
837 	    MT_HEADER, M_PKTHDR);
838 	if (n == NULL) {
839 		NAT64STAT_INC(stats, nomem);
840 		m_freem(m);
841 		return;
842 	}
843 	m_move_pkthdr(n, m);
844 	M_ALIGN(n, sizeof(struct ip) + plen + max_hdr);
845 
846 	n->m_len = n->m_pkthdr.len = sizeof(struct ip) + plen;
847 	oip = mtod(n, struct ip *);
848 	oip->ip_v = IPVERSION;
849 	oip->ip_hl = sizeof(struct ip) >> 2;
850 	oip->ip_tos = 0;
851 	oip->ip_len = htons(n->m_pkthdr.len);
852 	oip->ip_ttl = V_ip_defttl;
853 	oip->ip_p = IPPROTO_ICMP;
854 	ip_fillid(oip);
855 	oip->ip_off = htons(IP_DF);
856 	oip->ip_src = ip->ip_dst;
857 	oip->ip_dst = ip->ip_src;
858 	oip->ip_sum = 0;
859 	oip->ip_sum = in_cksum_hdr(oip);
860 
861 	icmp = mtodo(n, sizeof(struct ip));
862 	icmp->icmp_type = type;
863 	icmp->icmp_code = code;
864 	icmp->icmp_cksum = 0;
865 	icmp->icmp_pmvoid = 0;
866 	icmp->icmp_nextmtu = htons(mtu);
867 	m_copydata(m, 0, len, mtodo(n, sizeof(struct ip) +
868 	    sizeof(struct icmphdr) + sizeof(uint32_t)));
869 	icmp->icmp_cksum = in_cksum_skip(n, sizeof(struct ip) + plen,
870 	    sizeof(struct ip));
871 	m_freem(m);
872 	V_nat64out->output_one(n, stats, logdata);
873 	return;
874 freeit:
875 	NAT64STAT_INC(stats, dropped);
876 	m_freem(m);
877 }
878 
879 /* Translate ICMP echo request/reply into ICMPv6 */
880 static void
881 nat64_icmp_handle_echo(struct ip6_hdr *ip6, struct icmp6_hdr *icmp6,
882     uint16_t id, uint8_t type)
883 {
884 	uint16_t old;
885 
886 	old = *(uint16_t *)icmp6;	/* save type+code in one word */
887 	icmp6->icmp6_type = type;
888 	/* Reflect ICMPv6 -> ICMPv4 type translation in the cksum */
889 	icmp6->icmp6_cksum = cksum_adjust(icmp6->icmp6_cksum,
890 	    old, *(uint16_t *)icmp6);
891 	if (id != 0) {
892 		old = icmp6->icmp6_id;
893 		icmp6->icmp6_id = id;
894 		/* Reflect ICMP id translation in the cksum */
895 		icmp6->icmp6_cksum = cksum_adjust(icmp6->icmp6_cksum,
896 		    old, id);
897 	}
898 	/* Reflect IPv6 pseudo header in the cksum */
899 	icmp6->icmp6_cksum = ~in6_cksum_pseudo(ip6, ntohs(ip6->ip6_plen),
900 	    IPPROTO_ICMPV6, ~icmp6->icmp6_cksum);
901 }
902 
903 static NAT64NOINLINE struct mbuf *
904 nat64_icmp_translate(struct mbuf *m, struct ip6_hdr *ip6, uint16_t icmpid,
905     int offset, struct nat64_config *cfg)
906 {
907 	struct ip ip;
908 	struct icmp *icmp;
909 	struct tcphdr *tcp;
910 	struct udphdr *udp;
911 	struct ip6_hdr *eip6;
912 	struct mbuf *n;
913 	uint32_t mtu;
914 	int len, hlen, plen;
915 	uint8_t type, code;
916 
917 	if (m->m_len < offset + ICMP_MINLEN)
918 		m = m_pullup(m, offset + ICMP_MINLEN);
919 	if (m == NULL) {
920 		NAT64STAT_INC(&cfg->stats, nomem);
921 		return (m);
922 	}
923 	mtu = 0;
924 	icmp = mtodo(m, offset);
925 	/* RFC 7915 p4.2 */
926 	switch (icmp->icmp_type) {
927 	case ICMP_ECHOREPLY:
928 		type = ICMP6_ECHO_REPLY;
929 		code = 0;
930 		break;
931 	case ICMP_UNREACH:
932 		type = ICMP6_DST_UNREACH;
933 		switch (icmp->icmp_code) {
934 		case ICMP_UNREACH_NET:
935 		case ICMP_UNREACH_HOST:
936 		case ICMP_UNREACH_SRCFAIL:
937 		case ICMP_UNREACH_NET_UNKNOWN:
938 		case ICMP_UNREACH_HOST_UNKNOWN:
939 		case ICMP_UNREACH_TOSNET:
940 		case ICMP_UNREACH_TOSHOST:
941 			code = ICMP6_DST_UNREACH_NOROUTE;
942 			break;
943 		case ICMP_UNREACH_PROTOCOL:
944 			type = ICMP6_PARAM_PROB;
945 			code = ICMP6_PARAMPROB_NEXTHEADER;
946 			break;
947 		case ICMP_UNREACH_PORT:
948 			code = ICMP6_DST_UNREACH_NOPORT;
949 			break;
950 		case ICMP_UNREACH_NEEDFRAG:
951 			type = ICMP6_PACKET_TOO_BIG;
952 			code = 0;
953 			/* XXX: needs an additional look */
954 			mtu = max(IPV6_MMTU, ntohs(icmp->icmp_nextmtu) + 20);
955 			break;
956 		case ICMP_UNREACH_NET_PROHIB:
957 		case ICMP_UNREACH_HOST_PROHIB:
958 		case ICMP_UNREACH_FILTER_PROHIB:
959 		case ICMP_UNREACH_PRECEDENCE_CUTOFF:
960 			code = ICMP6_DST_UNREACH_ADMIN;
961 			break;
962 		default:
963 			DPRINTF(DP_DROPS, "Unsupported ICMP type %d, code %d",
964 			    icmp->icmp_type, icmp->icmp_code);
965 			goto freeit;
966 		}
967 		break;
968 	case ICMP_TIMXCEED:
969 		type = ICMP6_TIME_EXCEEDED;
970 		code = icmp->icmp_code;
971 		break;
972 	case ICMP_ECHO:
973 		type = ICMP6_ECHO_REQUEST;
974 		code = 0;
975 		break;
976 	case ICMP_PARAMPROB:
977 		type = ICMP6_PARAM_PROB;
978 		switch (icmp->icmp_code) {
979 		case ICMP_PARAMPROB_ERRATPTR:
980 		case ICMP_PARAMPROB_LENGTH:
981 			code = ICMP6_PARAMPROB_HEADER;
982 			switch (icmp->icmp_pptr) {
983 			case 0: /* Version/IHL */
984 			case 1: /* Type Of Service */
985 				mtu = icmp->icmp_pptr;
986 				break;
987 			case 2: /* Total Length */
988 			case 3: mtu = 4; /* Payload Length */
989 				break;
990 			case 8: /* Time to Live */
991 				mtu = 7; /* Hop Limit */
992 				break;
993 			case 9: /* Protocol */
994 				mtu = 6; /* Next Header */
995 				break;
996 			case 12: /* Source address */
997 			case 13:
998 			case 14:
999 			case 15:
1000 				mtu = 8;
1001 				break;
1002 			case 16: /* Destination address */
1003 			case 17:
1004 			case 18:
1005 			case 19:
1006 				mtu = 24;
1007 				break;
1008 			default: /* Silently drop */
1009 				DPRINTF(DP_DROPS, "Unsupported ICMP type %d,"
1010 				    " code %d, pptr %d", icmp->icmp_type,
1011 				    icmp->icmp_code, icmp->icmp_pptr);
1012 				goto freeit;
1013 			}
1014 			break;
1015 		default:
1016 			DPRINTF(DP_DROPS, "Unsupported ICMP type %d,"
1017 			    " code %d, pptr %d", icmp->icmp_type,
1018 			    icmp->icmp_code, icmp->icmp_pptr);
1019 			goto freeit;
1020 		}
1021 		break;
1022 	default:
1023 		DPRINTF(DP_DROPS, "Unsupported ICMP type %d, code %d",
1024 		    icmp->icmp_type, icmp->icmp_code);
1025 		goto freeit;
1026 	}
1027 	/*
1028 	 * For echo request/reply we can use original payload,
1029 	 * but we need adjust icmp_cksum, because ICMPv6 cksum covers
1030 	 * IPv6 pseudo header and ICMPv6 types differs from ICMPv4.
1031 	 */
1032 	if (type == ICMP6_ECHO_REQUEST || type == ICMP6_ECHO_REPLY) {
1033 		nat64_icmp_handle_echo(ip6, ICMP6(icmp), icmpid, type);
1034 		return (m);
1035 	}
1036 	/*
1037 	 * For other types of ICMP messages we need to translate inner
1038 	 * IPv4 header to IPv6 header.
1039 	 * Assume ICMP src is the same as payload dst
1040 	 * E.g. we have ( GWsrc1 , NATIP1 ) in outer header
1041 	 * and          ( NATIP1, Hostdst1 ) in ICMP copy header.
1042 	 * In that case, we already have map for NATIP1 and GWsrc1.
1043 	 * The only thing we need is to copy IPv6 map prefix to
1044 	 * Hostdst1.
1045 	 */
1046 	hlen = offset + ICMP_MINLEN;
1047 	if (m->m_pkthdr.len < hlen + sizeof(struct ip) + ICMP_MINLEN) {
1048 		DPRINTF(DP_DROPS, "Message is too short %d",
1049 		    m->m_pkthdr.len);
1050 		goto freeit;
1051 	}
1052 	m_copydata(m, hlen, sizeof(struct ip), (char *)&ip);
1053 	if (ip.ip_v != IPVERSION) {
1054 		DPRINTF(DP_DROPS, "Wrong IP version %d", ip.ip_v);
1055 		goto freeit;
1056 	}
1057 	hlen += ip.ip_hl << 2; /* Skip inner IP header */
1058 	if (nat64_check_ip4(ip.ip_src.s_addr) != 0 ||
1059 	    nat64_check_ip4(ip.ip_dst.s_addr) != 0 ||
1060 	    nat64_check_private_ip4(cfg, ip.ip_src.s_addr) != 0 ||
1061 	    nat64_check_private_ip4(cfg, ip.ip_dst.s_addr) != 0) {
1062 		DPRINTF(DP_DROPS, "IP addresses checks failed %04x -> %04x",
1063 		    ntohl(ip.ip_src.s_addr), ntohl(ip.ip_dst.s_addr));
1064 		goto freeit;
1065 	}
1066 	if (m->m_pkthdr.len < hlen + ICMP_MINLEN) {
1067 		DPRINTF(DP_DROPS, "Message is too short %d",
1068 		    m->m_pkthdr.len);
1069 		goto freeit;
1070 	}
1071 #if 0
1072 	/*
1073 	 * Check that inner source matches the outer destination.
1074 	 * XXX: We need some method to convert IPv4 into IPv6 address here,
1075 	 *	and compare IPv6 addresses.
1076 	 */
1077 	if (ip.ip_src.s_addr != nat64_get_ip4(&ip6->ip6_dst)) {
1078 		DPRINTF(DP_GENERIC, "Inner source doesn't match destination ",
1079 		    "%04x vs %04x", ip.ip_src.s_addr,
1080 		    nat64_get_ip4(&ip6->ip6_dst));
1081 		goto freeit;
1082 	}
1083 #endif
1084 	/*
1085 	 * Create new mbuf for ICMPv6 datagram.
1086 	 * NOTE: len is data length just after inner IP header.
1087 	 */
1088 	len = m->m_pkthdr.len - hlen;
1089 	if (sizeof(struct ip6_hdr) +
1090 	    sizeof(struct icmp6_hdr) + len > NAT64_ICMP6_PLEN)
1091 		len = NAT64_ICMP6_PLEN - sizeof(struct icmp6_hdr) -
1092 		    sizeof(struct ip6_hdr);
1093 	plen = sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr) + len;
1094 	n = m_get2(offset + plen + max_hdr, M_NOWAIT, MT_HEADER, M_PKTHDR);
1095 	if (n == NULL) {
1096 		NAT64STAT_INC(&cfg->stats, nomem);
1097 		m_freem(m);
1098 		return (NULL);
1099 	}
1100 	m_move_pkthdr(n, m);
1101 	M_ALIGN(n, offset + plen + max_hdr);
1102 	n->m_len = n->m_pkthdr.len = offset + plen;
1103 	/* Adjust ip6_plen in outer header */
1104 	ip6->ip6_plen = htons(plen);
1105 	/* Construct new inner IPv6 header */
1106 	eip6 = mtodo(n, offset + sizeof(struct icmp6_hdr));
1107 	eip6->ip6_src = ip6->ip6_dst;
1108 
1109 	/* Use the same prefix that we have in outer header */
1110 	eip6->ip6_dst = ip6->ip6_src;
1111 	MPASS(cfg->flags & NAT64_PLATPFX);
1112 	nat64_embed_ip4(&eip6->ip6_dst, cfg->plat_plen, ip.ip_dst.s_addr);
1113 
1114 	eip6->ip6_flow = htonl(ip.ip_tos << 20);
1115 	eip6->ip6_vfc |= IPV6_VERSION;
1116 	eip6->ip6_hlim = ip.ip_ttl;
1117 	eip6->ip6_plen = htons(ntohs(ip.ip_len) - (ip.ip_hl << 2));
1118 	eip6->ip6_nxt = (ip.ip_p == IPPROTO_ICMP) ? IPPROTO_ICMPV6: ip.ip_p;
1119 	m_copydata(m, hlen, len, (char *)(eip6 + 1));
1120 	/*
1121 	 * We need to translate source port in the inner ULP header,
1122 	 * and adjust ULP checksum.
1123 	 */
1124 	switch (ip.ip_p) {
1125 	case IPPROTO_TCP:
1126 		if (len < offsetof(struct tcphdr, th_sum))
1127 			break;
1128 		tcp = TCP(eip6 + 1);
1129 		if (icmpid != 0) {
1130 			tcp->th_sum = cksum_adjust(tcp->th_sum,
1131 			    tcp->th_sport, icmpid);
1132 			tcp->th_sport = icmpid;
1133 		}
1134 		tcp->th_sum = cksum_add(tcp->th_sum,
1135 		    ~nat64_cksum_convert(eip6, &ip));
1136 		break;
1137 	case IPPROTO_UDP:
1138 		if (len < offsetof(struct udphdr, uh_sum))
1139 			break;
1140 		udp = UDP(eip6 + 1);
1141 		if (icmpid != 0) {
1142 			udp->uh_sum = cksum_adjust(udp->uh_sum,
1143 			    udp->uh_sport, icmpid);
1144 			udp->uh_sport = icmpid;
1145 		}
1146 		udp->uh_sum = cksum_add(udp->uh_sum,
1147 		    ~nat64_cksum_convert(eip6, &ip));
1148 		break;
1149 	case IPPROTO_ICMP:
1150 		/*
1151 		 * Check if this is an ICMP error message for echo request
1152 		 * that we sent. I.e. ULP in the data containing invoking
1153 		 * packet is IPPROTO_ICMP and its type is ICMP_ECHO.
1154 		 */
1155 		icmp = (struct icmp *)(eip6 + 1);
1156 		if (icmp->icmp_type != ICMP_ECHO) {
1157 			m_freem(n);
1158 			goto freeit;
1159 		}
1160 		/*
1161 		 * For our client this original datagram should looks
1162 		 * like it was ICMPv6 datagram with type ICMP6_ECHO_REQUEST.
1163 		 * Thus we need adjust icmp_cksum and convert type from
1164 		 * ICMP_ECHO to ICMP6_ECHO_REQUEST.
1165 		 */
1166 		nat64_icmp_handle_echo(eip6, ICMP6(icmp), icmpid,
1167 		    ICMP6_ECHO_REQUEST);
1168 	}
1169 	m_freem(m);
1170 	/* Convert ICMPv4 into ICMPv6 header */
1171 	icmp = mtodo(n, offset);
1172 	ICMP6(icmp)->icmp6_type = type;
1173 	ICMP6(icmp)->icmp6_code = code;
1174 	ICMP6(icmp)->icmp6_mtu = htonl(mtu);
1175 	ICMP6(icmp)->icmp6_cksum = 0;
1176 	ICMP6(icmp)->icmp6_cksum = cksum_add(
1177 	    ~in6_cksum_pseudo(ip6, plen, IPPROTO_ICMPV6, 0),
1178 	    in_cksum_skip(n, n->m_pkthdr.len, offset));
1179 	return (n);
1180 freeit:
1181 	m_freem(m);
1182 	NAT64STAT_INC(&cfg->stats, dropped);
1183 	return (NULL);
1184 }
1185 
1186 int
1187 nat64_getlasthdr(struct mbuf *m, int *offset)
1188 {
1189 	struct ip6_hdr *ip6;
1190 	struct ip6_hbh *hbh;
1191 	int proto, hlen;
1192 
1193 	if (offset != NULL)
1194 		hlen = *offset;
1195 	else
1196 		hlen = 0;
1197 
1198 	if (m->m_len < hlen + sizeof(*ip6))
1199 		return (-1);
1200 
1201 	ip6 = mtodo(m, hlen);
1202 	hlen += sizeof(*ip6);
1203 	proto = ip6->ip6_nxt;
1204 	/* Skip extension headers */
1205 	while (proto == IPPROTO_HOPOPTS || proto == IPPROTO_ROUTING ||
1206 	    proto == IPPROTO_DSTOPTS) {
1207 		hbh = mtodo(m, hlen);
1208 		/*
1209 		 * We expect mbuf has contigious data up to
1210 		 * upper level header.
1211 		 */
1212 		if (m->m_len < hlen)
1213 			return (-1);
1214 		/*
1215 		 * We doesn't support Jumbo payload option,
1216 		 * so return error.
1217 		 */
1218 		if (proto == IPPROTO_HOPOPTS && ip6->ip6_plen == 0)
1219 			return (-1);
1220 		proto = hbh->ip6h_nxt;
1221 		hlen += (hbh->ip6h_len + 1) << 3;
1222 	}
1223 	if (offset != NULL)
1224 		*offset = hlen;
1225 	return (proto);
1226 }
1227 
1228 int
1229 nat64_do_handle_ip4(struct mbuf *m, struct in6_addr *saddr,
1230     struct in6_addr *daddr, uint16_t lport, struct nat64_config *cfg,
1231     void *logdata)
1232 {
1233 	struct nhop_object *nh;
1234 	struct ip6_hdr ip6;
1235 	struct sockaddr_in6 dst;
1236 	struct ip *ip;
1237 	struct mbufq mq;
1238 	uint16_t ip_id, ip_off;
1239 	uint16_t *csum;
1240 	int plen, hlen;
1241 	uint8_t proto;
1242 
1243 	ip = mtod(m, struct ip*);
1244 
1245 	if (*V_nat64ipstealth == 0 && ip->ip_ttl <= IPTTLDEC) {
1246 		nat64_icmp_reflect(m, ICMP_TIMXCEED,
1247 		    ICMP_TIMXCEED_INTRANS, 0, &cfg->stats, logdata);
1248 		return (NAT64RETURN);
1249 	}
1250 
1251 	ip6.ip6_dst = *daddr;
1252 	ip6.ip6_src = *saddr;
1253 
1254 	hlen = ip->ip_hl << 2;
1255 	plen = ntohs(ip->ip_len) - hlen;
1256 	proto = ip->ip_p;
1257 
1258 	/* Save ip_id and ip_off, both are in network byte order */
1259 	ip_id = ip->ip_id;
1260 	ip_off = ip->ip_off & htons(IP_OFFMASK | IP_MF);
1261 
1262 	/* Fragment length must be multiple of 8 octets */
1263 	if ((ip->ip_off & htons(IP_MF)) != 0 && (plen & 0x7) != 0) {
1264 		nat64_icmp_reflect(m, ICMP_PARAMPROB,
1265 		    ICMP_PARAMPROB_LENGTH, 0, &cfg->stats, logdata);
1266 		return (NAT64RETURN);
1267 	}
1268 	/* Fragmented ICMP is unsupported */
1269 	if (proto == IPPROTO_ICMP && ip_off != 0) {
1270 		DPRINTF(DP_DROPS, "dropped due to fragmented ICMP");
1271 		NAT64STAT_INC(&cfg->stats, dropped);
1272 		return (NAT64MFREE);
1273 	}
1274 
1275 	dst.sin6_addr = ip6.ip6_dst;
1276 	nh = nat64_find_route6(&dst, m);
1277 	if (nh == NULL) {
1278 		NAT64STAT_INC(&cfg->stats, noroute6);
1279 		nat64_icmp_reflect(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0,
1280 		    &cfg->stats, logdata);
1281 		return (NAT64RETURN);
1282 	}
1283 	if (nh->nh_mtu < plen + sizeof(ip6) &&
1284 	    (ip->ip_off & htons(IP_DF)) != 0) {
1285 		nat64_icmp_reflect(m, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG,
1286 		    FRAGSZ(nh->nh_mtu) + sizeof(struct ip), &cfg->stats, logdata);
1287 		return (NAT64RETURN);
1288 	}
1289 
1290 	ip6.ip6_flow = htonl(ip->ip_tos << 20);
1291 	ip6.ip6_vfc |= IPV6_VERSION;
1292 	ip6.ip6_hlim = ip->ip_ttl;
1293 	if (*V_nat64ipstealth == 0)
1294 		ip6.ip6_hlim -= IPTTLDEC;
1295 	ip6.ip6_plen = htons(plen);
1296 	ip6.ip6_nxt = (proto == IPPROTO_ICMP) ? IPPROTO_ICMPV6: proto;
1297 	/* Convert checksums. */
1298 	switch (proto) {
1299 	case IPPROTO_TCP:
1300 		csum = &TCP(mtodo(m, hlen))->th_sum;
1301 		if (lport != 0) {
1302 			struct tcphdr *tcp = TCP(mtodo(m, hlen));
1303 			*csum = cksum_adjust(*csum, tcp->th_dport, lport);
1304 			tcp->th_dport = lport;
1305 		}
1306 		*csum = cksum_add(*csum, ~nat64_cksum_convert(&ip6, ip));
1307 		break;
1308 	case IPPROTO_UDP:
1309 		csum = &UDP(mtodo(m, hlen))->uh_sum;
1310 		if (lport != 0) {
1311 			struct udphdr *udp = UDP(mtodo(m, hlen));
1312 			*csum = cksum_adjust(*csum, udp->uh_dport, lport);
1313 			udp->uh_dport = lport;
1314 		}
1315 		*csum = cksum_add(*csum, ~nat64_cksum_convert(&ip6, ip));
1316 		break;
1317 	case IPPROTO_ICMP:
1318 		m = nat64_icmp_translate(m, &ip6, lport, hlen, cfg);
1319 		if (m == NULL)	/* stats already accounted */
1320 			return (NAT64RETURN);
1321 	}
1322 
1323 	m_adj(m, hlen);
1324 	mbufq_init(&mq, 255);
1325 	nat64_fragment6(&cfg->stats, &ip6, &mq, m, nh->nh_mtu, ip_id, ip_off);
1326 	while ((m = mbufq_dequeue(&mq)) != NULL) {
1327 		if (V_nat64out->output(nh->nh_ifp, m, (struct sockaddr *)&dst,
1328 		    &cfg->stats, logdata) != 0)
1329 			break;
1330 		NAT64STAT_INC(&cfg->stats, opcnt46);
1331 	}
1332 	mbufq_drain(&mq);
1333 	return (NAT64RETURN);
1334 }
1335 
1336 int
1337 nat64_handle_icmp6(struct mbuf *m, int hlen, uint32_t aaddr, uint16_t aport,
1338     struct nat64_config *cfg, void *logdata)
1339 {
1340 	struct ip ip;
1341 	struct icmp6_hdr *icmp6;
1342 	struct ip6_frag *ip6f;
1343 	struct ip6_hdr *ip6, *ip6i;
1344 	uint32_t mtu;
1345 	int plen, proto;
1346 	uint8_t type, code;
1347 
1348 	if (hlen == 0) {
1349 		ip6 = mtod(m, struct ip6_hdr *);
1350 		if (nat64_check_ip6(&ip6->ip6_src) != 0 ||
1351 		    nat64_check_ip6(&ip6->ip6_dst) != 0)
1352 			return (NAT64SKIP);
1353 
1354 		proto = nat64_getlasthdr(m, &hlen);
1355 		if (proto != IPPROTO_ICMPV6) {
1356 			DPRINTF(DP_DROPS,
1357 			    "dropped due to mbuf isn't contigious");
1358 			NAT64STAT_INC(&cfg->stats, dropped);
1359 			return (NAT64MFREE);
1360 		}
1361 	}
1362 
1363 	/*
1364 	 * Translate ICMPv6 type and code to ICMPv4 (RFC7915).
1365 	 * NOTE: ICMPv6 echo handled by nat64_do_handle_ip6().
1366 	 */
1367 	icmp6 = mtodo(m, hlen);
1368 	mtu = 0;
1369 	switch (icmp6->icmp6_type) {
1370 	case ICMP6_DST_UNREACH:
1371 		type = ICMP_UNREACH;
1372 		switch (icmp6->icmp6_code) {
1373 		case ICMP6_DST_UNREACH_NOROUTE:
1374 		case ICMP6_DST_UNREACH_BEYONDSCOPE:
1375 		case ICMP6_DST_UNREACH_ADDR:
1376 			code = ICMP_UNREACH_HOST;
1377 			break;
1378 		case ICMP6_DST_UNREACH_ADMIN:
1379 			code = ICMP_UNREACH_HOST_PROHIB;
1380 			break;
1381 		case ICMP6_DST_UNREACH_NOPORT:
1382 			code = ICMP_UNREACH_PORT;
1383 			break;
1384 		default:
1385 			DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1386 			    " code %d", icmp6->icmp6_type,
1387 			    icmp6->icmp6_code);
1388 			NAT64STAT_INC(&cfg->stats, dropped);
1389 			return (NAT64MFREE);
1390 		}
1391 		break;
1392 	case ICMP6_PACKET_TOO_BIG:
1393 		type = ICMP_UNREACH;
1394 		code = ICMP_UNREACH_NEEDFRAG;
1395 		mtu = ntohl(icmp6->icmp6_mtu);
1396 		if (mtu < IPV6_MMTU) {
1397 			DPRINTF(DP_DROPS, "Wrong MTU %d in ICMPv6 type %d,"
1398 			    " code %d", mtu, icmp6->icmp6_type,
1399 			    icmp6->icmp6_code);
1400 			NAT64STAT_INC(&cfg->stats, dropped);
1401 			return (NAT64MFREE);
1402 		}
1403 		/*
1404 		 * Adjust MTU to reflect difference between
1405 		 * IPv6 an IPv4 headers.
1406 		 */
1407 		mtu -= sizeof(struct ip6_hdr) - sizeof(struct ip);
1408 		break;
1409 	case ICMP6_TIME_EXCEEDED:
1410 		type = ICMP_TIMXCEED;
1411 		code = icmp6->icmp6_code;
1412 		break;
1413 	case ICMP6_PARAM_PROB:
1414 		switch (icmp6->icmp6_code) {
1415 		case ICMP6_PARAMPROB_HEADER:
1416 			type = ICMP_PARAMPROB;
1417 			code = ICMP_PARAMPROB_ERRATPTR;
1418 			mtu = ntohl(icmp6->icmp6_pptr);
1419 			switch (mtu) {
1420 			case 0: /* Version/Traffic Class */
1421 			case 1: /* Traffic Class/Flow Label */
1422 				break;
1423 			case 4: /* Payload Length */
1424 			case 5:
1425 				mtu = 2;
1426 				break;
1427 			case 6: /* Next Header */
1428 				mtu = 9;
1429 				break;
1430 			case 7: /* Hop Limit */
1431 				mtu = 8;
1432 				break;
1433 			default:
1434 				if (mtu >= 8 && mtu <= 23) {
1435 					mtu = 12; /* Source address */
1436 					break;
1437 				}
1438 				if (mtu >= 24 && mtu <= 39) {
1439 					mtu = 16; /* Destination address */
1440 					break;
1441 				}
1442 				DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1443 				    " code %d, pptr %d", icmp6->icmp6_type,
1444 				    icmp6->icmp6_code, mtu);
1445 				NAT64STAT_INC(&cfg->stats, dropped);
1446 				return (NAT64MFREE);
1447 			}
1448 		case ICMP6_PARAMPROB_NEXTHEADER:
1449 			type = ICMP_UNREACH;
1450 			code = ICMP_UNREACH_PROTOCOL;
1451 			break;
1452 		default:
1453 			DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d,"
1454 			    " code %d, pptr %d", icmp6->icmp6_type,
1455 			    icmp6->icmp6_code, ntohl(icmp6->icmp6_pptr));
1456 			NAT64STAT_INC(&cfg->stats, dropped);
1457 			return (NAT64MFREE);
1458 		}
1459 		break;
1460 	default:
1461 		DPRINTF(DP_DROPS, "Unsupported ICMPv6 type %d, code %d",
1462 		    icmp6->icmp6_type, icmp6->icmp6_code);
1463 		NAT64STAT_INC(&cfg->stats, dropped);
1464 		return (NAT64MFREE);
1465 	}
1466 
1467 	hlen += sizeof(struct icmp6_hdr);
1468 	if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
1469 		NAT64STAT_INC(&cfg->stats, dropped);
1470 		DPRINTF(DP_DROPS, "Message is too short %d",
1471 		    m->m_pkthdr.len);
1472 		return (NAT64MFREE);
1473 	}
1474 	/*
1475 	 * We need at least ICMP_MINLEN bytes of original datagram payload
1476 	 * to generate ICMP message. It is nice that ICMP_MINLEN is equal
1477 	 * to sizeof(struct ip6_frag). So, if embedded datagram had a fragment
1478 	 * header we will not have to do m_pullup() again.
1479 	 *
1480 	 * What we have here:
1481 	 * Outer header: (IPv6iGW, v4mapPRefix+v4exthost)
1482 	 * Inner header: (v4mapPRefix+v4host, IPv6iHost) [sport, dport]
1483 	 * We need to translate it to:
1484 	 *
1485 	 * Outer header: (alias_host, v4exthost)
1486 	 * Inner header: (v4exthost, alias_host) [sport, alias_port]
1487 	 *
1488 	 * Assume caller function has checked if v4mapPRefix+v4host
1489 	 * matches configured prefix.
1490 	 * The only two things we should be provided with are mapping between
1491 	 * IPv6iHost <> alias_host and between dport and alias_port.
1492 	 */
1493 	if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
1494 		m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
1495 	if (m == NULL) {
1496 		NAT64STAT_INC(&cfg->stats, nomem);
1497 		return (NAT64RETURN);
1498 	}
1499 	ip6 = mtod(m, struct ip6_hdr *);
1500 	ip6i = mtodo(m, hlen);
1501 	ip6f = NULL;
1502 	proto = ip6i->ip6_nxt;
1503 	plen = ntohs(ip6i->ip6_plen);
1504 	hlen += sizeof(struct ip6_hdr);
1505 	if (proto == IPPROTO_FRAGMENT) {
1506 		if (m->m_pkthdr.len < hlen + sizeof(struct ip6_frag) +
1507 		    ICMP_MINLEN)
1508 			goto fail;
1509 		ip6f = mtodo(m, hlen);
1510 		proto = ip6f->ip6f_nxt;
1511 		plen -= sizeof(struct ip6_frag);
1512 		hlen += sizeof(struct ip6_frag);
1513 		/* Ajust MTU to reflect frag header size */
1514 		if (type == ICMP_UNREACH && code == ICMP_UNREACH_NEEDFRAG)
1515 			mtu -= sizeof(struct ip6_frag);
1516 	}
1517 	if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
1518 		DPRINTF(DP_DROPS, "Unsupported proto %d in the inner header",
1519 		    proto);
1520 		goto fail;
1521 	}
1522 	if (nat64_check_ip6(&ip6i->ip6_src) != 0 ||
1523 	    nat64_check_ip6(&ip6i->ip6_dst) != 0) {
1524 		DPRINTF(DP_DROPS, "Inner addresses do not passes the check");
1525 		goto fail;
1526 	}
1527 	/* Check if outer dst is the same as inner src */
1528 	if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6i->ip6_src)) {
1529 		DPRINTF(DP_DROPS, "Inner src doesn't match outer dst");
1530 		goto fail;
1531 	}
1532 
1533 	/* Now we need to make a fake IPv4 packet to generate ICMP message */
1534 	ip.ip_dst.s_addr = aaddr;
1535 	ip.ip_src.s_addr = nat64_extract_ip4(&ip6i->ip6_src, cfg->plat_plen);
1536 	if (ip.ip_src.s_addr == 0)
1537 		goto fail;
1538 	/* XXX: Make fake ulp header */
1539 	if (V_nat64out == &nat64_direct) /* init_ip4hdr will decrement it */
1540 		ip6i->ip6_hlim += IPV6_HLIMDEC;
1541 	nat64_init_ip4hdr(ip6i, ip6f, plen, proto, &ip);
1542 	m_adj(m, hlen - sizeof(struct ip));
1543 	bcopy(&ip, mtod(m, void *), sizeof(ip));
1544 	nat64_icmp_reflect(m, type, code, (uint16_t)mtu, &cfg->stats,
1545 	    logdata);
1546 	return (NAT64RETURN);
1547 fail:
1548 	/*
1549 	 * We must call m_freem() because mbuf pointer could be
1550 	 * changed with m_pullup().
1551 	 */
1552 	m_freem(m);
1553 	NAT64STAT_INC(&cfg->stats, dropped);
1554 	return (NAT64RETURN);
1555 }
1556 
1557 int
1558 nat64_do_handle_ip6(struct mbuf *m, uint32_t aaddr, uint16_t aport,
1559     struct nat64_config *cfg, void *logdata)
1560 {
1561 	struct ip ip;
1562 	struct nhop_object *nh;
1563 	struct sockaddr_in dst;
1564 	struct ip6_frag *frag;
1565 	struct ip6_hdr *ip6;
1566 	struct icmp6_hdr *icmp6;
1567 	uint16_t *csum;
1568 	int plen, hlen, proto;
1569 
1570 	/*
1571 	 * XXX: we expect ipfw_chk() did m_pullup() up to upper level
1572 	 * protocol's headers. Also we skip some checks, that ip6_input(),
1573 	 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
1574 	 */
1575 	ip6 = mtod(m, struct ip6_hdr *);
1576 	if (nat64_check_ip6(&ip6->ip6_src) != 0 ||
1577 	    nat64_check_ip6(&ip6->ip6_dst) != 0) {
1578 		return (NAT64SKIP);
1579 	}
1580 
1581 	/* Starting from this point we must not return zero */
1582 	ip.ip_src.s_addr = aaddr;
1583 	if (nat64_check_ip4(ip.ip_src.s_addr) != 0) {
1584 		DPRINTF(DP_GENERIC | DP_DROPS, "invalid source address: %08x",
1585 		    ip.ip_src.s_addr);
1586 		NAT64STAT_INC(&cfg->stats, dropped);
1587 		return (NAT64MFREE);
1588 	}
1589 
1590 	ip.ip_dst.s_addr = nat64_extract_ip4(&ip6->ip6_dst, cfg->plat_plen);
1591 	if (ip.ip_dst.s_addr == 0) {
1592 		NAT64STAT_INC(&cfg->stats, dropped);
1593 		return (NAT64MFREE);
1594 	}
1595 
1596 	if (*V_nat64ip6stealth == 0 && ip6->ip6_hlim <= IPV6_HLIMDEC) {
1597 		nat64_icmp6_reflect(m, ICMP6_TIME_EXCEEDED,
1598 		    ICMP6_TIME_EXCEED_TRANSIT, 0, &cfg->stats, logdata);
1599 		return (NAT64RETURN);
1600 	}
1601 
1602 	hlen = 0;
1603 	plen = ntohs(ip6->ip6_plen);
1604 	proto = nat64_getlasthdr(m, &hlen);
1605 	if (proto < 0) {
1606 		DPRINTF(DP_DROPS, "dropped due to mbuf isn't contigious");
1607 		NAT64STAT_INC(&cfg->stats, dropped);
1608 		return (NAT64MFREE);
1609 	}
1610 	frag = NULL;
1611 	if (proto == IPPROTO_FRAGMENT) {
1612 		/* ipfw_chk should m_pullup up to frag header */
1613 		if (m->m_len < hlen + sizeof(*frag)) {
1614 			DPRINTF(DP_DROPS,
1615 			    "dropped due to mbuf isn't contigious");
1616 			NAT64STAT_INC(&cfg->stats, dropped);
1617 			return (NAT64MFREE);
1618 		}
1619 		frag = mtodo(m, hlen);
1620 		proto = frag->ip6f_nxt;
1621 		hlen += sizeof(*frag);
1622 		/* Fragmented ICMPv6 is unsupported */
1623 		if (proto == IPPROTO_ICMPV6) {
1624 			DPRINTF(DP_DROPS, "dropped due to fragmented ICMPv6");
1625 			NAT64STAT_INC(&cfg->stats, dropped);
1626 			return (NAT64MFREE);
1627 		}
1628 		/* Fragment length must be multiple of 8 octets */
1629 		if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0 &&
1630 		    ((plen + sizeof(struct ip6_hdr) - hlen) & 0x7) != 0) {
1631 			nat64_icmp6_reflect(m, ICMP6_PARAM_PROB,
1632 			    ICMP6_PARAMPROB_HEADER,
1633 			    offsetof(struct ip6_hdr, ip6_plen), &cfg->stats,
1634 			    logdata);
1635 			return (NAT64RETURN);
1636 		}
1637 	}
1638 	plen -= hlen - sizeof(struct ip6_hdr);
1639 	if (plen < 0 || m->m_pkthdr.len < plen + hlen) {
1640 		DPRINTF(DP_DROPS, "plen %d, pkthdr.len %d, hlen %d",
1641 		    plen, m->m_pkthdr.len, hlen);
1642 		NAT64STAT_INC(&cfg->stats, dropped);
1643 		return (NAT64MFREE);
1644 	}
1645 
1646 	icmp6 = NULL;	/* Make gcc happy */
1647 	if (proto == IPPROTO_ICMPV6) {
1648 		icmp6 = mtodo(m, hlen);
1649 		if (icmp6->icmp6_type != ICMP6_ECHO_REQUEST &&
1650 		    icmp6->icmp6_type != ICMP6_ECHO_REPLY)
1651 			return (nat64_handle_icmp6(m, hlen, aaddr, aport,
1652 			    cfg, logdata));
1653 	}
1654 	dst.sin_addr.s_addr = ip.ip_dst.s_addr;
1655 	nh = nat64_find_route4(&dst, m);
1656 	if (nh == NULL) {
1657 		NAT64STAT_INC(&cfg->stats, noroute4);
1658 		nat64_icmp6_reflect(m, ICMP6_DST_UNREACH,
1659 		    ICMP6_DST_UNREACH_NOROUTE, 0, &cfg->stats, logdata);
1660 		return (NAT64RETURN);
1661 	}
1662 	if (nh->nh_mtu < plen + sizeof(ip)) {
1663 		nat64_icmp6_reflect(m, ICMP6_PACKET_TOO_BIG, 0, nh->nh_mtu,
1664 		    &cfg->stats, logdata);
1665 		return (NAT64RETURN);
1666 	}
1667 	nat64_init_ip4hdr(ip6, frag, plen, proto, &ip);
1668 	/* Convert checksums. */
1669 	switch (proto) {
1670 	case IPPROTO_TCP:
1671 		csum = &TCP(mtodo(m, hlen))->th_sum;
1672 		if (aport != 0) {
1673 			struct tcphdr *tcp = TCP(mtodo(m, hlen));
1674 			*csum = cksum_adjust(*csum, tcp->th_sport, aport);
1675 			tcp->th_sport = aport;
1676 		}
1677 		*csum = cksum_add(*csum, nat64_cksum_convert(ip6, &ip));
1678 		break;
1679 	case IPPROTO_UDP:
1680 		csum = &UDP(mtodo(m, hlen))->uh_sum;
1681 		if (aport != 0) {
1682 			struct udphdr *udp = UDP(mtodo(m, hlen));
1683 			*csum = cksum_adjust(*csum, udp->uh_sport, aport);
1684 			udp->uh_sport = aport;
1685 		}
1686 		*csum = cksum_add(*csum, nat64_cksum_convert(ip6, &ip));
1687 		break;
1688 	case IPPROTO_ICMPV6:
1689 		/* Checksum in ICMPv6 covers pseudo header */
1690 		csum = &icmp6->icmp6_cksum;
1691 		*csum = cksum_add(*csum, in6_cksum_pseudo(ip6, plen,
1692 		    IPPROTO_ICMPV6, 0));
1693 		/* Convert ICMPv6 types to ICMP */
1694 		proto = *(uint16_t *)icmp6; /* save old word for cksum_adjust */
1695 		if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST)
1696 			icmp6->icmp6_type = ICMP_ECHO;
1697 		else /* ICMP6_ECHO_REPLY */
1698 			icmp6->icmp6_type = ICMP_ECHOREPLY;
1699 		*csum = cksum_adjust(*csum, (uint16_t)proto,
1700 		    *(uint16_t *)icmp6);
1701 		if (aport != 0) {
1702 			uint16_t old_id = icmp6->icmp6_id;
1703 			icmp6->icmp6_id = aport;
1704 			*csum = cksum_adjust(*csum, old_id, aport);
1705 		}
1706 		break;
1707 	};
1708 
1709 	m_adj(m, hlen - sizeof(ip));
1710 	bcopy(&ip, mtod(m, void *), sizeof(ip));
1711 	if (V_nat64out->output(nh->nh_ifp, m, (struct sockaddr *)&dst,
1712 	    &cfg->stats, logdata) == 0)
1713 		NAT64STAT_INC(&cfg->stats, opcnt64);
1714 	return (NAT64RETURN);
1715 }
1716 
1717