xref: /freebsd/sys/netinet6/ip_fw_nat64.h (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015-2019 Yandex LLC
5  * Copyright (c) 2015 Alexander V. Chernikov <melifaro@FreeBSD.org>
6  * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef	_NETINET6_IP_FW_NAT64_H_
33 #define	_NETINET6_IP_FW_NAT64_H_
34 
35 struct ipfw_nat64stl_stats {
36 	uint64_t	opcnt64;	/* 6to4 of packets translated */
37 	uint64_t	opcnt46;	/* 4to6 of packets translated */
38 	uint64_t	ofrags;		/* number of fragments generated */
39 	uint64_t	ifrags;		/* number of fragments received */
40 	uint64_t	oerrors;	/* number of output errors */
41 	uint64_t	noroute4;
42 	uint64_t	noroute6;
43 	uint64_t	noproto;	/* Protocol not supported */
44 	uint64_t	nomem;		/* mbuf allocation failed */
45 	uint64_t	dropped;	/* dropped due to some errors */
46 };
47 
48 struct ipfw_nat64clat_stats {
49 	uint64_t	opcnt64;	/* 6to4 of packets translated */
50 	uint64_t	opcnt46;	/* 4to6 of packets translated */
51 	uint64_t	ofrags;		/* number of fragments generated */
52 	uint64_t	ifrags;		/* number of fragments received */
53 	uint64_t	oerrors;	/* number of output errors */
54 	uint64_t	noroute4;
55 	uint64_t	noroute6;
56 	uint64_t	noproto;	/* Protocol not supported */
57 	uint64_t	nomem;		/* mbuf allocation failed */
58 	uint64_t	dropped;	/* dropped due to some errors */
59 };
60 
61 struct ipfw_nat64lsn_stats {
62 	uint64_t	opcnt64;	/* 6to4 of packets translated */
63 	uint64_t	opcnt46;	/* 4to6 of packets translated */
64 	uint64_t	ofrags;		/* number of fragments generated */
65 	uint64_t	ifrags;		/* number of fragments received */
66 	uint64_t	oerrors;	/* number of output errors */
67 	uint64_t	noroute4;
68 	uint64_t	noroute6;
69 	uint64_t	noproto;	/* Protocol not supported */
70 	uint64_t	nomem;		/* mbuf allocation failed */
71 	uint64_t	dropped;	/* dropped due to some errors */
72 
73 	uint64_t	nomatch4;	/* No addr/port match */
74 	uint64_t	jcalls;		/* Number of job handler calls */
75 	uint64_t	jrequests;	/* Number of job requests */
76 	uint64_t	jhostsreq;	/* Number of job host requests */
77 	uint64_t	jportreq;	/* Number of portgroup requests */
78 	uint64_t	jhostfails;	/* Number of failed host allocs */
79 	uint64_t	jportfails;	/* Number of failed portgroup allocs */
80 	uint64_t	jreinjected;	/* Number of packets reinjected to q */
81 	uint64_t	jmaxlen;	/* Max queue length reached */
82 	uint64_t	jnomem;		/* No memory to alloc queue item */
83 
84 	uint64_t	screated;	/* Number of states created */
85 	uint64_t	sdeleted;	/* Number of states deleted */
86 	uint64_t	spgcreated;	/* Number of portgroups created */
87 	uint64_t	spgdeleted;	/* Number of portgroups deleted */
88 	uint64_t	hostcount;	/* Number of hosts  */
89 	uint64_t	tcpchunks;	/* Number of TCP chunks */
90 	uint64_t	udpchunks;	/* Number of UDP chunks */
91 	uint64_t	icmpchunks;	/* Number of ICMP chunks */
92 
93 	uint64_t	_reserved[4];
94 };
95 
96 #define	NAT64_LOG		0x0001	/* Enable logging via BPF */
97 #define	NAT64_ALLOW_PRIVATE	0x0002	/* Allow private IPv4 address
98 					 * translation
99 					 */
100 typedef struct _ipfw_nat64stl_cfg {
101 	char		name[64];	/* NAT name			*/
102 	ipfw_obj_ntlv	ntlv6;		/* object name tlv		*/
103 	ipfw_obj_ntlv	ntlv4;		/* object name tlv		*/
104 	struct in6_addr	prefix6;	/* NAT64 prefix */
105 	uint8_t		plen6;		/* Prefix length */
106 	uint8_t		set;		/* Named instance set [0..31] */
107 	uint8_t		spare[2];
108 	uint32_t	flags;
109 } ipfw_nat64stl_cfg;
110 
111 typedef struct _ipfw_nat64clat_cfg {
112 	char		name[64];	/* NAT name			*/
113 	struct in6_addr	plat_prefix;	/* NAT64 (PLAT) prefix */
114 	struct in6_addr	clat_prefix;	/* Client (CLAT) prefix */
115 	uint8_t		plat_plen;	/* PLAT Prefix length */
116 	uint8_t		clat_plen;	/* CLAT Prefix length */
117 	uint8_t		set;		/* Named instance set [0..31] */
118 	uint8_t		spare;
119 	uint32_t	flags;
120 } ipfw_nat64clat_cfg;
121 
122 /*
123  * NAT64LSN default configuration values
124  */
125 #define	NAT64LSN_MAX_PORTS	2048	/* Unused */
126 #define	NAT64LSN_JMAXLEN	2048	/* Max outstanding requests. */
127 #define	NAT64LSN_TCP_SYN_AGE	10	/* State's TTL after SYN received. */
128 #define	NAT64LSN_TCP_EST_AGE	(2 * 3600) /* TTL for established connection */
129 #define	NAT64LSN_TCP_FIN_AGE	180	/* State's TTL after FIN/RST received */
130 #define	NAT64LSN_UDP_AGE	120	/* TTL for UDP states */
131 #define	NAT64LSN_ICMP_AGE	60	/* TTL for ICMP states */
132 #define	NAT64LSN_HOST_AGE	3600	/* TTL for stale host entry */
133 #define	NAT64LSN_PG_AGE		900	/* TTL for stale ports groups */
134 
135 typedef struct _ipfw_nat64lsn_cfg {
136 	char		name[64];	/* NAT name			*/
137 	uint32_t	flags;
138 
139 	uint32_t	max_ports;      /* Unused */
140 	uint32_t	agg_prefix_len; /* Unused */
141 	uint32_t	agg_prefix_max; /* Unused */
142 
143 	struct in_addr	prefix4;
144 	uint16_t	plen4;		/* Prefix length */
145 	uint16_t	plen6;		/* Prefix length */
146 	struct in6_addr	prefix6;	/* NAT64 prefix */
147 	uint32_t	jmaxlen;	/* Max jobqueue length */
148 
149 	uint16_t	min_port;	/* Unused */
150 	uint16_t	max_port;	/* Unused */
151 
152 	uint16_t	nh_delete_delay;/* Stale host delete delay */
153 	uint16_t	pg_delete_delay;/* Stale portgroup delete delay */
154 	uint16_t	st_syn_ttl;	/* TCP syn expire */
155 	uint16_t	st_close_ttl;	/* TCP fin expire */
156 	uint16_t	st_estab_ttl;	/* TCP established expire */
157 	uint16_t	st_udp_ttl;	/* UDP expire */
158 	uint16_t	st_icmp_ttl;	/* ICMP expire */
159 	uint8_t		set;		/* Named instance set [0..31] */
160 	uint8_t		states_chunks;	/* Number of states chunks per PG */
161 } ipfw_nat64lsn_cfg;
162 
163 typedef struct _ipfw_nat64lsn_state {
164 	struct in_addr	daddr;		/* Remote IPv4 address */
165 	uint16_t	dport;		/* Remote destination port */
166 	uint16_t	aport;		/* Local alias port */
167 	uint16_t	sport;		/* Source port */
168 	uint8_t		flags;		/* State flags */
169 	uint8_t		spare[3];
170 	uint16_t	idle;		/* Last used time */
171 } ipfw_nat64lsn_state;
172 
173 typedef struct _ipfw_nat64lsn_stg {
174 	uint64_t	next_idx;	/* next state index */
175 	struct in_addr	alias4;		/* IPv4 alias address */
176 	uint8_t		proto;		/* protocol */
177 	uint8_t		flags;
178 	uint16_t	spare;
179 	struct in6_addr	host6;		/* Bound IPv6 host */
180 	uint32_t	count;		/* Number of states */
181 	uint32_t	spare2;
182 } ipfw_nat64lsn_stg;
183 
184 typedef struct _ipfw_nat64lsn_state_v1 {
185 	struct in6_addr	host6;		/* Bound IPv6 host */
186 	struct in_addr	daddr;		/* Remote IPv4 address */
187 	uint16_t	dport;		/* Remote destination port */
188 	uint16_t	aport;		/* Local alias port */
189 	uint16_t	sport;		/* Source port */
190 	uint16_t	spare;
191 	uint16_t	idle;		/* Last used time */
192 	uint8_t		flags;		/* State flags */
193 	uint8_t		proto;		/* protocol */
194 } ipfw_nat64lsn_state_v1;
195 
196 typedef struct _ipfw_nat64lsn_stg_v1 {
197 	union nat64lsn_pgidx {
198 		uint64_t	index;
199 		struct {
200 			uint8_t		chunk;	/* states chunk */
201 			uint8_t		proto;	/* protocol */
202 			uint16_t	port;	/* base port */
203 			in_addr_t	addr;	/* alias address */
204 		};
205 	} next;				/* next state index */
206 	struct in_addr	alias4;		/* IPv4 alias address */
207 	uint32_t	count;		/* Number of states */
208 } ipfw_nat64lsn_stg_v1;
209 
210 #endif /* _NETINET6_IP_FW_NAT64_H_ */
211