2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015-2019 Yandex LLC
5 * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _IP_FW_NAT64_TRANSLATE_H_
32 #define _IP_FW_NAT64_TRANSLATE_H_
35 uint64_t opcnt64; /* 6to4 of packets translated */
36 uint64_t opcnt46; /* 4to6 of packets translated */
37 uint64_t ofrags; /* number of fragments generated */
38 uint64_t ifrags; /* number of fragments received */
39 uint64_t oerrors; /* number of output errors */
42 uint64_t nomatch4; /* No addr/port match */
43 uint64_t noproto; /* Protocol not supported */
44 uint64_t nomem; /* mbufs allocation failed */
45 uint64_t dropped; /* number of packets silently
46 * dropped due to some errors/
50 uint64_t jrequests; /* jobs requests queued */
51 uint64_t jcalls; /* jobs handler calls */
52 uint64_t jhostsreq; /* hosts requests */
53 uint64_t jportreq; /* PG allocation requests */
54 uint64_t jhostfails; /* hosts requests failed */
55 uint64_t jportfails; /* PG allocation failed */
66 #define IPFW_NAT64_VERSION 1
67 #define NAT64STATS (sizeof(struct nat64_stats) / sizeof(uint64_t))
68 struct nat64_counters {
69 counter_u64_t cnt[NAT64STATS];
71 #define NAT64STAT_ADD(s, f, v) \
72 counter_u64_add((s)->cnt[ \
73 offsetof(struct nat64_stats, f) / sizeof(uint64_t)], (v))
74 #define NAT64STAT_INC(s, f) NAT64STAT_ADD(s, f, 1)
75 #define NAT64STAT_FETCH(s, f) \
76 counter_u64_fetch((s)->cnt[ \
77 offsetof(struct nat64_stats, f) / sizeof(uint64_t)])
79 #define L3HDR(_ip, _t) ((_t)((uint32_t *)(_ip) + (_ip)->ip_hl))
80 #define TCP(p) ((struct tcphdr *)(p))
81 #define UDP(p) ((struct udphdr *)(p))
82 #define ICMP(p) ((struct icmphdr *)(p))
83 #define ICMP6(p) ((struct icmp6_hdr *)(p))
90 * According to RFC6877:
91 * PLAT is provider-side translator (XLAT) that translates N:1 global
92 * IPv6 addresses to global IPv4 addresses, and vice versa.
94 * CLAT is customer-side translator (XLAT) that algorithmically
95 * translates 1:1 private IPv4 addresses to global IPv6 addresses,
99 struct in6_addr clat_prefix;
100 struct in6_addr plat_prefix;
102 #define NAT64_WKPFX 0x00010000 /* prefix is well-known */
103 #define NAT64_CLATPFX 0x00020000 /* dst prefix is configured */
104 #define NAT64_PLATPFX 0x00040000 /* src prefix is configured */
108 struct nat64_counters stats;
112 nat64_check_ip6(struct in6_addr *addr)
115 /* XXX: We should really check /8 */
116 if (addr->s6_addr16[0] == 0 || /* 0000::/8 Reserved by IETF */
117 IN6_IS_ADDR_MULTICAST(addr) || IN6_IS_ADDR_LINKLOCAL(addr))
123 nat64_check_ip4(in_addr_t ia)
127 if ((ia & htonl(0xff000000)) == htonl(0x7f000000))
130 if ((ia & htonl(0xffff0000)) == htonl(0xa9fe0000))
132 /* IN_MULTICAST & IN_EXPERIMENTAL */
133 if ((ia & htonl(0xe0000000)) == htonl(0xe0000000))
138 /* Well-known prefix 64:ff9b::/96 */
139 #define IPV6_ADDR_INT32_WKPFX htonl(0x64ff9b)
140 #define IN6_IS_ADDR_WKPFX(a) \
141 ((a)->s6_addr32[0] == IPV6_ADDR_INT32_WKPFX && \
142 (a)->s6_addr32[1] == 0 && (a)->s6_addr32[2] == 0)
144 int nat64_check_private_ip4(const struct nat64_config *cfg, in_addr_t ia);
145 int nat64_check_prefixlen(int length);
146 int nat64_check_prefix6(const struct in6_addr *prefix, int length);
147 int nat64_getlasthdr(struct mbuf *m, int *offset);
148 int nat64_do_handle_ip4(struct mbuf *m, struct in6_addr *saddr,
149 struct in6_addr *daddr, uint16_t lport, struct nat64_config *cfg,
151 int nat64_do_handle_ip6(struct mbuf *m, uint32_t aaddr, uint16_t aport,
152 struct nat64_config *cfg, void *logdata);
153 int nat64_handle_icmp6(struct mbuf *m, int hlen, uint32_t aaddr,
154 uint16_t aport, struct nat64_config *cfg, void *logdata);
155 void nat64_embed_ip4(struct in6_addr *ip6, int plen, in_addr_t ia);
156 in_addr_t nat64_extract_ip4(const struct in6_addr *ip6, int plen);
158 void nat64_set_output_method(int);
159 int nat64_get_output_method(void);