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