2 * Copyright (c) 2015-2018 Yandex LLC
3 * Copyright (c) 2015-2018 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
30 #ifndef _IP_FW_NAT64_TRANSLATE_H_
31 #define _IP_FW_NAT64_TRANSLATE_H_
34 uint64_t opcnt64; /* 6to4 of packets translated */
35 uint64_t opcnt46; /* 4to6 of packets translated */
36 uint64_t ofrags; /* number of fragments generated */
37 uint64_t ifrags; /* number of fragments received */
38 uint64_t oerrors; /* number of output errors */
41 uint64_t nomatch4; /* No addr/port match */
42 uint64_t noproto; /* Protocol not supported */
43 uint64_t nomem; /* mbufs allocation failed */
44 uint64_t dropped; /* number of packets silently
45 * dropped due to some errors/
49 uint64_t jrequests; /* number of jobs requests queued */
50 uint64_t jcalls; /* number of jobs handler calls */
51 uint64_t jhostsreq; /* number of hosts requests */
65 #define IPFW_NAT64_VERSION 1
66 #define NAT64STATS (sizeof(struct nat64_stats) / sizeof(uint64_t))
67 struct nat64_counters {
68 counter_u64_t cnt[NAT64STATS];
70 #define NAT64STAT_ADD(s, f, v) \
71 counter_u64_add((s)->cnt[ \
72 offsetof(struct nat64_stats, f) / sizeof(uint64_t)], (v))
73 #define NAT64STAT_INC(s, f) NAT64STAT_ADD(s, f, 1)
74 #define NAT64STAT_FETCH(s, f) \
75 counter_u64_fetch((s)->cnt[ \
76 offsetof(struct nat64_stats, f) / sizeof(uint64_t)])
78 #define L3HDR(_ip, _t) ((_t)((uint32_t *)(_ip) + (_ip)->ip_hl))
79 #define TCP(p) ((struct tcphdr *)(p))
80 #define UDP(p) ((struct udphdr *)(p))
81 #define ICMP(p) ((struct icmphdr *)(p))
82 #define ICMP6(p) ((struct icmp6_hdr *)(p))
90 #define NAT64_WKPFX 0x00010000 /* prefix6 is WKPFX */
91 struct in6_addr prefix6;
94 struct nat64_counters stats;
98 nat64_check_ip6(struct in6_addr *addr)
101 /* XXX: We should really check /8 */
102 if (addr->s6_addr16[0] == 0 || /* 0000::/8 Reserved by IETF */
103 IN6_IS_ADDR_MULTICAST(addr) || IN6_IS_ADDR_LINKLOCAL(addr))
109 nat64_check_ip4(in_addr_t ia)
113 if ((ia & htonl(0xff000000)) == htonl(0x7f000000))
116 if ((ia & htonl(0xffff0000)) == htonl(0xa9fe0000))
118 /* IN_MULTICAST & IN_EXPERIMENTAL */
119 if ((ia & htonl(0xe0000000)) == htonl(0xe0000000))
124 /* Well-known prefix 64:ff9b::/96 */
125 #define IPV6_ADDR_INT32_WKPFX htonl(0x64ff9b)
126 #define IN6_IS_ADDR_WKPFX(a) \
127 ((a)->s6_addr32[0] == IPV6_ADDR_INT32_WKPFX && \
128 (a)->s6_addr32[1] == 0 && (a)->s6_addr32[2] == 0)
130 int nat64_check_private_ip4(const struct nat64_config *cfg, in_addr_t ia);
131 int nat64_check_prefix6(const struct in6_addr *prefix, int length);
132 int nat64_getlasthdr(struct mbuf *m, int *offset);
133 int nat64_do_handle_ip4(struct mbuf *m, struct in6_addr *saddr,
134 struct in6_addr *daddr, uint16_t lport, struct nat64_config *cfg,
136 int nat64_do_handle_ip6(struct mbuf *m, uint32_t aaddr, uint16_t aport,
137 struct nat64_config *cfg, void *logdata);
138 int nat64_handle_icmp6(struct mbuf *m, int hlen, uint32_t aaddr,
139 uint16_t aport, struct nat64_config *cfg, void *logdata);
140 void nat64_embed_ip4(const struct nat64_config *cfg, in_addr_t ia,
141 struct in6_addr *ip6);
142 in_addr_t nat64_extract_ip4(const struct nat64_config *cfg,
143 const struct in6_addr *ip6);
145 void nat64_set_output_method(int);
146 int nat64_get_output_method(void);