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