xref: /freebsd/sbin/ipfw/nat64stl.c (revision 002cae78daca2488aa423f13036a2c9dca5b134b)
1d8caf56eSAndrey V. Elsukov /*-
2*002cae78SAndrey V. Elsukov  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*002cae78SAndrey V. Elsukov  *
4*002cae78SAndrey V. Elsukov  * Copyright (c) 2015-2019 Yandex LLC
5*002cae78SAndrey V. Elsukov  * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
6d8caf56eSAndrey V. Elsukov  *
7d8caf56eSAndrey V. Elsukov  * Redistribution and use in source and binary forms, with or without
8d8caf56eSAndrey V. Elsukov  * modification, are permitted provided that the following conditions
9d8caf56eSAndrey V. Elsukov  * are met:
10d8caf56eSAndrey V. Elsukov  *
11d8caf56eSAndrey V. Elsukov  * 1. Redistributions of source code must retain the above copyright
12d8caf56eSAndrey V. Elsukov  *    notice, this list of conditions and the following disclaimer.
13d8caf56eSAndrey V. Elsukov  * 2. Redistributions in binary form must reproduce the above copyright
14d8caf56eSAndrey V. Elsukov  *    notice, this list of conditions and the following disclaimer in the
15d8caf56eSAndrey V. Elsukov  *    documentation and/or other materials provided with the distribution.
16d8caf56eSAndrey V. Elsukov  *
17d8caf56eSAndrey V. Elsukov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18d8caf56eSAndrey V. Elsukov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19d8caf56eSAndrey V. Elsukov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20d8caf56eSAndrey V. Elsukov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21d8caf56eSAndrey V. Elsukov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22d8caf56eSAndrey V. Elsukov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23d8caf56eSAndrey V. Elsukov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d8caf56eSAndrey V. Elsukov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d8caf56eSAndrey V. Elsukov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26d8caf56eSAndrey V. Elsukov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d8caf56eSAndrey V. Elsukov  */
28d8caf56eSAndrey V. Elsukov 
29d8caf56eSAndrey V. Elsukov #include <sys/cdefs.h>
30d8caf56eSAndrey V. Elsukov __FBSDID("$FreeBSD$");
31d8caf56eSAndrey V. Elsukov 
32d8caf56eSAndrey V. Elsukov #include <sys/types.h>
33d8caf56eSAndrey V. Elsukov #include <sys/socket.h>
34d8caf56eSAndrey V. Elsukov 
35d8caf56eSAndrey V. Elsukov #include "ipfw2.h"
36d8caf56eSAndrey V. Elsukov 
37d8caf56eSAndrey V. Elsukov #include <ctype.h>
38d8caf56eSAndrey V. Elsukov #include <err.h>
39d8caf56eSAndrey V. Elsukov #include <errno.h>
40c5e85276SAndrey V. Elsukov #include <inttypes.h>
41d8caf56eSAndrey V. Elsukov #include <netdb.h>
42d8caf56eSAndrey V. Elsukov #include <stdio.h>
43d8caf56eSAndrey V. Elsukov #include <stdlib.h>
44d8caf56eSAndrey V. Elsukov #include <string.h>
45d8caf56eSAndrey V. Elsukov #include <sysexits.h>
46d8caf56eSAndrey V. Elsukov 
47d8caf56eSAndrey V. Elsukov #include <net/if.h>
48d8caf56eSAndrey V. Elsukov #include <netinet/in.h>
49d8caf56eSAndrey V. Elsukov #include <netinet/ip_fw.h>
50d8caf56eSAndrey V. Elsukov #include <netinet6/ip_fw_nat64.h>
51d8caf56eSAndrey V. Elsukov #include <arpa/inet.h>
52d8caf56eSAndrey V. Elsukov 
53d8caf56eSAndrey V. Elsukov typedef int (nat64stl_cb_t)(ipfw_nat64stl_cfg *i, const char *name,
54d8caf56eSAndrey V. Elsukov     uint8_t set);
55d8caf56eSAndrey V. Elsukov static int nat64stl_foreach(nat64stl_cb_t *f, const char *name, uint8_t set,
56d8caf56eSAndrey V. Elsukov     int sort);
57d8caf56eSAndrey V. Elsukov 
58d8caf56eSAndrey V. Elsukov static void nat64stl_create(const char *name, uint8_t set, int ac, char **av);
59d8caf56eSAndrey V. Elsukov static void nat64stl_config(const char *name, uint8_t set, int ac, char **av);
60d8caf56eSAndrey V. Elsukov static void nat64stl_destroy(const char *name, uint8_t set);
61d8caf56eSAndrey V. Elsukov static void nat64stl_stats(const char *name, uint8_t set);
62d8caf56eSAndrey V. Elsukov static void nat64stl_reset_stats(const char *name, uint8_t set);
63d8caf56eSAndrey V. Elsukov static int nat64stl_show_cb(ipfw_nat64stl_cfg *cfg, const char *name,
64d8caf56eSAndrey V. Elsukov     uint8_t set);
65d8caf56eSAndrey V. Elsukov static int nat64stl_destroy_cb(ipfw_nat64stl_cfg *cfg, const char *name,
66d8caf56eSAndrey V. Elsukov     uint8_t set);
67d8caf56eSAndrey V. Elsukov 
68d8caf56eSAndrey V. Elsukov static struct _s_x nat64cmds[] = {
69d8caf56eSAndrey V. Elsukov       { "create",	TOK_CREATE },
70d8caf56eSAndrey V. Elsukov       { "config",	TOK_CONFIG },
71d8caf56eSAndrey V. Elsukov       { "destroy",	TOK_DESTROY },
72d8caf56eSAndrey V. Elsukov       { "list",		TOK_LIST },
73d8caf56eSAndrey V. Elsukov       { "show",		TOK_LIST },
74d8caf56eSAndrey V. Elsukov       { "stats",	TOK_STATS },
75d8caf56eSAndrey V. Elsukov       { NULL, 0 }
76d8caf56eSAndrey V. Elsukov };
77d8caf56eSAndrey V. Elsukov 
78d8caf56eSAndrey V. Elsukov #define	IPV6_ADDR_INT32_WKPFX	htonl(0x64ff9b)
79d8caf56eSAndrey V. Elsukov #define	IN6_IS_ADDR_WKPFX(a)					\
80d8caf56eSAndrey V. Elsukov     ((a)->__u6_addr.__u6_addr32[0] == IPV6_ADDR_INT32_WKPFX &&	\
81d8caf56eSAndrey V. Elsukov 	(a)->__u6_addr.__u6_addr32[1] == 0 &&			\
82d8caf56eSAndrey V. Elsukov 	(a)->__u6_addr.__u6_addr32[2] == 0)
83782360deSAndrey V. Elsukov int
84782360deSAndrey V. Elsukov ipfw_check_nat64prefix(const struct in6_addr *prefix, int length)
85d8caf56eSAndrey V. Elsukov {
86d8caf56eSAndrey V. Elsukov 
87d8caf56eSAndrey V. Elsukov 	switch (length) {
88d8caf56eSAndrey V. Elsukov 	case 32:
89d8caf56eSAndrey V. Elsukov 	case 40:
90d8caf56eSAndrey V. Elsukov 	case 48:
91d8caf56eSAndrey V. Elsukov 	case 56:
92d8caf56eSAndrey V. Elsukov 	case 64:
93d8caf56eSAndrey V. Elsukov 		/* Well-known prefix has 96 prefix length */
94d8caf56eSAndrey V. Elsukov 		if (IN6_IS_ADDR_WKPFX(prefix))
95782360deSAndrey V. Elsukov 			return (EINVAL);
96d8caf56eSAndrey V. Elsukov 		/* FALLTHROUGH */
97d8caf56eSAndrey V. Elsukov 	case 96:
98d8caf56eSAndrey V. Elsukov 		/* Bits 64 to 71 must be set to zero */
99d8caf56eSAndrey V. Elsukov 		if (prefix->__u6_addr.__u6_addr8[8] != 0)
100782360deSAndrey V. Elsukov 			return (EINVAL);
101d8caf56eSAndrey V. Elsukov 		/* XXX: looks incorrect */
102d8caf56eSAndrey V. Elsukov 		if (IN6_IS_ADDR_MULTICAST(prefix) ||
103d8caf56eSAndrey V. Elsukov 		    IN6_IS_ADDR_UNSPECIFIED(prefix) ||
104d8caf56eSAndrey V. Elsukov 		    IN6_IS_ADDR_LOOPBACK(prefix))
105782360deSAndrey V. Elsukov 			return (EINVAL);
106d8caf56eSAndrey V. Elsukov 		return (0);
107d8caf56eSAndrey V. Elsukov 	}
108782360deSAndrey V. Elsukov 	return (EINVAL);
109d8caf56eSAndrey V. Elsukov }
110d8caf56eSAndrey V. Elsukov 
111d8caf56eSAndrey V. Elsukov static struct _s_x nat64statscmds[] = {
112d8caf56eSAndrey V. Elsukov       { "reset",	TOK_RESET },
113d8caf56eSAndrey V. Elsukov       { NULL, 0 }
114d8caf56eSAndrey V. Elsukov };
115d8caf56eSAndrey V. Elsukov 
116d8caf56eSAndrey V. Elsukov /*
117d8caf56eSAndrey V. Elsukov  * This one handles all nat64stl-related commands
118d8caf56eSAndrey V. Elsukov  *	ipfw [set N] nat64stl NAME {create | config} ...
119d8caf56eSAndrey V. Elsukov  *	ipfw [set N] nat64stl NAME stats [reset]
120d8caf56eSAndrey V. Elsukov  *	ipfw [set N] nat64stl {NAME | all} destroy
121d8caf56eSAndrey V. Elsukov  *	ipfw [set N] nat64stl {NAME | all} {list | show}
122d8caf56eSAndrey V. Elsukov  */
123d8caf56eSAndrey V. Elsukov #define	nat64stl_check_name	table_check_name
124d8caf56eSAndrey V. Elsukov void
125d8caf56eSAndrey V. Elsukov ipfw_nat64stl_handler(int ac, char *av[])
126d8caf56eSAndrey V. Elsukov {
127d8caf56eSAndrey V. Elsukov 	const char *name;
128d8caf56eSAndrey V. Elsukov 	int tcmd;
129d8caf56eSAndrey V. Elsukov 	uint8_t set;
130d8caf56eSAndrey V. Elsukov 
131d8caf56eSAndrey V. Elsukov 	if (co.use_set != 0)
132d8caf56eSAndrey V. Elsukov 		set = co.use_set - 1;
133d8caf56eSAndrey V. Elsukov 	else
134d8caf56eSAndrey V. Elsukov 		set = 0;
135d8caf56eSAndrey V. Elsukov 	ac--; av++;
136d8caf56eSAndrey V. Elsukov 
137d8caf56eSAndrey V. Elsukov 	NEED1("nat64stl needs instance name");
138d8caf56eSAndrey V. Elsukov 	name = *av;
139d8caf56eSAndrey V. Elsukov 	if (nat64stl_check_name(name) != 0) {
140d8caf56eSAndrey V. Elsukov 		if (strcmp(name, "all") == 0)
141d8caf56eSAndrey V. Elsukov 			name = NULL;
142d8caf56eSAndrey V. Elsukov 		else
143d8caf56eSAndrey V. Elsukov 			errx(EX_USAGE, "nat64stl instance name %s is invalid",
144d8caf56eSAndrey V. Elsukov 			    name);
145d8caf56eSAndrey V. Elsukov 	}
146d8caf56eSAndrey V. Elsukov 	ac--; av++;
147d8caf56eSAndrey V. Elsukov 	NEED1("nat64stl needs command");
148d8caf56eSAndrey V. Elsukov 
149d8caf56eSAndrey V. Elsukov 	tcmd = get_token(nat64cmds, *av, "nat64stl command");
150d8caf56eSAndrey V. Elsukov 	if (name == NULL && tcmd != TOK_DESTROY && tcmd != TOK_LIST)
151d8caf56eSAndrey V. Elsukov 		errx(EX_USAGE, "nat64stl instance name required");
152d8caf56eSAndrey V. Elsukov 	switch (tcmd) {
153d8caf56eSAndrey V. Elsukov 	case TOK_CREATE:
154d8caf56eSAndrey V. Elsukov 		ac--; av++;
155d8caf56eSAndrey V. Elsukov 		nat64stl_create(name, set, ac, av);
156d8caf56eSAndrey V. Elsukov 		break;
157d8caf56eSAndrey V. Elsukov 	case TOK_CONFIG:
158d8caf56eSAndrey V. Elsukov 		ac--; av++;
159d8caf56eSAndrey V. Elsukov 		nat64stl_config(name, set, ac, av);
160d8caf56eSAndrey V. Elsukov 		break;
161d8caf56eSAndrey V. Elsukov 	case TOK_LIST:
162d8caf56eSAndrey V. Elsukov 		nat64stl_foreach(nat64stl_show_cb, name, set, 1);
163d8caf56eSAndrey V. Elsukov 		break;
164d8caf56eSAndrey V. Elsukov 	case TOK_DESTROY:
165d8caf56eSAndrey V. Elsukov 		if (name == NULL)
166d8caf56eSAndrey V. Elsukov 			nat64stl_foreach(nat64stl_destroy_cb, NULL, set, 0);
167d8caf56eSAndrey V. Elsukov 		else
168d8caf56eSAndrey V. Elsukov 			nat64stl_destroy(name, set);
169d8caf56eSAndrey V. Elsukov 		break;
170d8caf56eSAndrey V. Elsukov 	case TOK_STATS:
171d8caf56eSAndrey V. Elsukov 		ac--; av++;
172d8caf56eSAndrey V. Elsukov 		if (ac == 0) {
173d8caf56eSAndrey V. Elsukov 			nat64stl_stats(name, set);
174d8caf56eSAndrey V. Elsukov 			break;
175d8caf56eSAndrey V. Elsukov 		}
176d8caf56eSAndrey V. Elsukov 		tcmd = get_token(nat64statscmds, *av, "stats command");
177d8caf56eSAndrey V. Elsukov 		if (tcmd == TOK_RESET)
178d8caf56eSAndrey V. Elsukov 			nat64stl_reset_stats(name, set);
179d8caf56eSAndrey V. Elsukov 	}
180d8caf56eSAndrey V. Elsukov }
181d8caf56eSAndrey V. Elsukov 
182d8caf56eSAndrey V. Elsukov 
183d8caf56eSAndrey V. Elsukov static void
184d8caf56eSAndrey V. Elsukov nat64stl_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set)
185d8caf56eSAndrey V. Elsukov {
186d8caf56eSAndrey V. Elsukov 
187d8caf56eSAndrey V. Elsukov 	ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
188d8caf56eSAndrey V. Elsukov 	ntlv->head.length = sizeof(ipfw_obj_ntlv);
189d8caf56eSAndrey V. Elsukov 	ntlv->idx = 1;
190d8caf56eSAndrey V. Elsukov 	ntlv->set = set;
191d8caf56eSAndrey V. Elsukov 	strlcpy(ntlv->name, name, sizeof(ntlv->name));
192d8caf56eSAndrey V. Elsukov }
193d8caf56eSAndrey V. Elsukov 
194d8caf56eSAndrey V. Elsukov static struct _s_x nat64newcmds[] = {
195d8caf56eSAndrey V. Elsukov       { "table4",	TOK_TABLE4 },
196d8caf56eSAndrey V. Elsukov       { "table6",	TOK_TABLE6 },
197d8caf56eSAndrey V. Elsukov       { "prefix6",	TOK_PREFIX6 },
198d8caf56eSAndrey V. Elsukov       { "log",		TOK_LOG },
199d8caf56eSAndrey V. Elsukov       { "-log",		TOK_LOGOFF },
200b11efc1eSAndrey V. Elsukov       { "allow_private", TOK_PRIVATE },
201b11efc1eSAndrey V. Elsukov       { "-allow_private", TOK_PRIVATEOFF },
202d8caf56eSAndrey V. Elsukov       { NULL, 0 }
203d8caf56eSAndrey V. Elsukov };
204d8caf56eSAndrey V. Elsukov 
205d8caf56eSAndrey V. Elsukov /*
206d8caf56eSAndrey V. Elsukov  * Creates new nat64stl instance
207d8caf56eSAndrey V. Elsukov  * ipfw nat64stl <NAME> create table4 <name> table6 <name> [ prefix6 <prefix>]
208d8caf56eSAndrey V. Elsukov  * Request: [ ipfw_obj_lheader ipfw_nat64stl_cfg ]
209d8caf56eSAndrey V. Elsukov  */
210d8caf56eSAndrey V. Elsukov #define	NAT64STL_HAS_TABLE4	0x01
211d8caf56eSAndrey V. Elsukov #define	NAT64STL_HAS_TABLE6	0x02
212d8caf56eSAndrey V. Elsukov #define	NAT64STL_HAS_PREFIX6	0x04
213d8caf56eSAndrey V. Elsukov static void
214d8caf56eSAndrey V. Elsukov nat64stl_create(const char *name, uint8_t set, int ac, char *av[])
215d8caf56eSAndrey V. Elsukov {
216d8caf56eSAndrey V. Elsukov 	char buf[sizeof(ipfw_obj_lheader) + sizeof(ipfw_nat64stl_cfg)];
217d8caf56eSAndrey V. Elsukov 	ipfw_nat64stl_cfg *cfg;
218d8caf56eSAndrey V. Elsukov 	ipfw_obj_lheader *olh;
219d8caf56eSAndrey V. Elsukov 	int tcmd, flags;
220d8caf56eSAndrey V. Elsukov 	char *p;
221d8caf56eSAndrey V. Elsukov 
222d8caf56eSAndrey V. Elsukov 	memset(buf, 0, sizeof(buf));
223d8caf56eSAndrey V. Elsukov 	olh = (ipfw_obj_lheader *)buf;
224d8caf56eSAndrey V. Elsukov 	cfg = (ipfw_nat64stl_cfg *)(olh + 1);
225d8caf56eSAndrey V. Elsukov 
226d8caf56eSAndrey V. Elsukov 	/* Some reasonable defaults */
227d8caf56eSAndrey V. Elsukov 	inet_pton(AF_INET6, "64:ff9b::", &cfg->prefix6);
228d8caf56eSAndrey V. Elsukov 	cfg->plen6 = 96;
229d8caf56eSAndrey V. Elsukov 	cfg->set = set;
230d8caf56eSAndrey V. Elsukov 	flags = NAT64STL_HAS_PREFIX6;
231d8caf56eSAndrey V. Elsukov 	while (ac > 0) {
232d8caf56eSAndrey V. Elsukov 		tcmd = get_token(nat64newcmds, *av, "option");
233d8caf56eSAndrey V. Elsukov 		ac--; av++;
234d8caf56eSAndrey V. Elsukov 
235d8caf56eSAndrey V. Elsukov 		switch (tcmd) {
236d8caf56eSAndrey V. Elsukov 		case TOK_TABLE4:
237d8caf56eSAndrey V. Elsukov 			NEED1("table name required");
238d8caf56eSAndrey V. Elsukov 			table_fill_ntlv(&cfg->ntlv4, *av, set, 4);
239d8caf56eSAndrey V. Elsukov 			flags |= NAT64STL_HAS_TABLE4;
240d8caf56eSAndrey V. Elsukov 			ac--; av++;
241d8caf56eSAndrey V. Elsukov 			break;
242d8caf56eSAndrey V. Elsukov 		case TOK_TABLE6:
243d8caf56eSAndrey V. Elsukov 			NEED1("table name required");
244d8caf56eSAndrey V. Elsukov 			table_fill_ntlv(&cfg->ntlv6, *av, set, 6);
245d8caf56eSAndrey V. Elsukov 			flags |= NAT64STL_HAS_TABLE6;
246d8caf56eSAndrey V. Elsukov 			ac--; av++;
247d8caf56eSAndrey V. Elsukov 			break;
248d8caf56eSAndrey V. Elsukov 		case TOK_PREFIX6:
249d8caf56eSAndrey V. Elsukov 			NEED1("IPv6 prefix6 required");
250d8caf56eSAndrey V. Elsukov 			if ((p = strchr(*av, '/')) != NULL)
251d8caf56eSAndrey V. Elsukov 				*p++ = '\0';
252d8caf56eSAndrey V. Elsukov 			if (inet_pton(AF_INET6, *av, &cfg->prefix6) != 1)
253d8caf56eSAndrey V. Elsukov 				errx(EX_USAGE,
254d8caf56eSAndrey V. Elsukov 				    "Bad prefix: %s", *av);
255d8caf56eSAndrey V. Elsukov 			cfg->plen6 = strtol(p, NULL, 10);
256782360deSAndrey V. Elsukov 			if (ipfw_check_nat64prefix(&cfg->prefix6,
257d8caf56eSAndrey V. Elsukov 			    cfg->plen6) != 0)
258d8caf56eSAndrey V. Elsukov 				errx(EX_USAGE,
259d8caf56eSAndrey V. Elsukov 				    "Bad prefix length: %s", p);
260d8caf56eSAndrey V. Elsukov 			flags |= NAT64STL_HAS_PREFIX6;
261d8caf56eSAndrey V. Elsukov 			ac--; av++;
262d8caf56eSAndrey V. Elsukov 			break;
263d8caf56eSAndrey V. Elsukov 		case TOK_LOG:
264d8caf56eSAndrey V. Elsukov 			cfg->flags |= NAT64_LOG;
265d8caf56eSAndrey V. Elsukov 			break;
266d8caf56eSAndrey V. Elsukov 		case TOK_LOGOFF:
267d8caf56eSAndrey V. Elsukov 			cfg->flags &= ~NAT64_LOG;
268d8caf56eSAndrey V. Elsukov 			break;
269b11efc1eSAndrey V. Elsukov 		case TOK_PRIVATE:
270b11efc1eSAndrey V. Elsukov 			cfg->flags |= NAT64_ALLOW_PRIVATE;
271b11efc1eSAndrey V. Elsukov 			break;
272b11efc1eSAndrey V. Elsukov 		case TOK_PRIVATEOFF:
273b11efc1eSAndrey V. Elsukov 			cfg->flags &= ~NAT64_ALLOW_PRIVATE;
274b11efc1eSAndrey V. Elsukov 			break;
275d8caf56eSAndrey V. Elsukov 		}
276d8caf56eSAndrey V. Elsukov 	}
277d8caf56eSAndrey V. Elsukov 
278d8caf56eSAndrey V. Elsukov 	/* Check validness */
279d8caf56eSAndrey V. Elsukov 	if ((flags & NAT64STL_HAS_TABLE4) != NAT64STL_HAS_TABLE4)
280d8caf56eSAndrey V. Elsukov 		errx(EX_USAGE, "table4 required");
281d8caf56eSAndrey V. Elsukov 	if ((flags & NAT64STL_HAS_TABLE6) != NAT64STL_HAS_TABLE6)
282d8caf56eSAndrey V. Elsukov 		errx(EX_USAGE, "table6 required");
283d8caf56eSAndrey V. Elsukov 	if ((flags & NAT64STL_HAS_PREFIX6) != NAT64STL_HAS_PREFIX6)
284d8caf56eSAndrey V. Elsukov 		errx(EX_USAGE, "prefix6 required");
285d8caf56eSAndrey V. Elsukov 
286d8caf56eSAndrey V. Elsukov 	olh->count = 1;
287d8caf56eSAndrey V. Elsukov 	olh->objsize = sizeof(*cfg);
288d8caf56eSAndrey V. Elsukov 	olh->size = sizeof(buf);
289d8caf56eSAndrey V. Elsukov 	strlcpy(cfg->name, name, sizeof(cfg->name));
290d8caf56eSAndrey V. Elsukov 	if (do_set3(IP_FW_NAT64STL_CREATE, &olh->opheader, sizeof(buf)) != 0)
291d8caf56eSAndrey V. Elsukov 		err(EX_OSERR, "nat64stl instance creation failed");
292d8caf56eSAndrey V. Elsukov }
293d8caf56eSAndrey V. Elsukov 
294d8caf56eSAndrey V. Elsukov /*
295d8caf56eSAndrey V. Elsukov  * Configures existing nat64stl instance
296d8caf56eSAndrey V. Elsukov  * ipfw nat64stl <NAME> config <options>
297d8caf56eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ipfw_nat64stl_cfg ]
298d8caf56eSAndrey V. Elsukov  */
299d8caf56eSAndrey V. Elsukov static void
300d8caf56eSAndrey V. Elsukov nat64stl_config(const char *name, uint8_t set, int ac, char **av)
301d8caf56eSAndrey V. Elsukov {
302d8caf56eSAndrey V. Elsukov 	char buf[sizeof(ipfw_obj_header) + sizeof(ipfw_nat64stl_cfg)];
303d8caf56eSAndrey V. Elsukov 	ipfw_nat64stl_cfg *cfg;
304d8caf56eSAndrey V. Elsukov 	ipfw_obj_header *oh;
305d8caf56eSAndrey V. Elsukov 	char *opt;
306d8caf56eSAndrey V. Elsukov 	size_t sz;
307d8caf56eSAndrey V. Elsukov 	int tcmd;
308d8caf56eSAndrey V. Elsukov 
309d8caf56eSAndrey V. Elsukov 	if (ac == 0)
310d8caf56eSAndrey V. Elsukov 		errx(EX_USAGE, "config options required");
311d8caf56eSAndrey V. Elsukov 	memset(&buf, 0, sizeof(buf));
312d8caf56eSAndrey V. Elsukov 	oh = (ipfw_obj_header *)buf;
313d8caf56eSAndrey V. Elsukov 	cfg = (ipfw_nat64stl_cfg *)(oh + 1);
314d8caf56eSAndrey V. Elsukov 	sz = sizeof(buf);
315d8caf56eSAndrey V. Elsukov 
316d8caf56eSAndrey V. Elsukov 	nat64stl_fill_ntlv(&oh->ntlv, name, set);
317d8caf56eSAndrey V. Elsukov 	if (do_get3(IP_FW_NAT64STL_CONFIG, &oh->opheader, &sz) != 0)
318d8caf56eSAndrey V. Elsukov 		err(EX_OSERR, "failed to get config for instance %s", name);
319d8caf56eSAndrey V. Elsukov 
320d8caf56eSAndrey V. Elsukov 	while (ac > 0) {
321d8caf56eSAndrey V. Elsukov 		tcmd = get_token(nat64newcmds, *av, "option");
322d8caf56eSAndrey V. Elsukov 		opt = *av;
323d8caf56eSAndrey V. Elsukov 		ac--; av++;
324d8caf56eSAndrey V. Elsukov 
325d8caf56eSAndrey V. Elsukov 		switch (tcmd) {
326d8caf56eSAndrey V. Elsukov #if 0
327d8caf56eSAndrey V. Elsukov 		case TOK_TABLE4:
328d8caf56eSAndrey V. Elsukov 			NEED1("table name required");
329d8caf56eSAndrey V. Elsukov 			table_fill_ntlv(&cfg->ntlv4, *av, set, 4);
330d8caf56eSAndrey V. Elsukov 			ac--; av++;
331d8caf56eSAndrey V. Elsukov 			break;
332d8caf56eSAndrey V. Elsukov 		case TOK_TABLE6:
333d8caf56eSAndrey V. Elsukov 			NEED1("table name required");
334d8caf56eSAndrey V. Elsukov 			table_fill_ntlv(&cfg->ntlv6, *av, set, 6);
335d8caf56eSAndrey V. Elsukov 			ac--; av++;
336d8caf56eSAndrey V. Elsukov 			break;
337d8caf56eSAndrey V. Elsukov #endif
338d8caf56eSAndrey V. Elsukov 		case TOK_LOG:
339d8caf56eSAndrey V. Elsukov 			cfg->flags |= NAT64_LOG;
340d8caf56eSAndrey V. Elsukov 			break;
341d8caf56eSAndrey V. Elsukov 		case TOK_LOGOFF:
342d8caf56eSAndrey V. Elsukov 			cfg->flags &= ~NAT64_LOG;
343d8caf56eSAndrey V. Elsukov 			break;
344b11efc1eSAndrey V. Elsukov 		case TOK_PRIVATE:
345b11efc1eSAndrey V. Elsukov 			cfg->flags |= NAT64_ALLOW_PRIVATE;
346b11efc1eSAndrey V. Elsukov 			break;
347b11efc1eSAndrey V. Elsukov 		case TOK_PRIVATEOFF:
348b11efc1eSAndrey V. Elsukov 			cfg->flags &= ~NAT64_ALLOW_PRIVATE;
349b11efc1eSAndrey V. Elsukov 			break;
350d8caf56eSAndrey V. Elsukov 		default:
351d8caf56eSAndrey V. Elsukov 			errx(EX_USAGE, "Can't change %s option", opt);
352d8caf56eSAndrey V. Elsukov 		}
353d8caf56eSAndrey V. Elsukov 	}
354d8caf56eSAndrey V. Elsukov 
355d8caf56eSAndrey V. Elsukov 	if (do_set3(IP_FW_NAT64STL_CONFIG, &oh->opheader, sizeof(buf)) != 0)
356d8caf56eSAndrey V. Elsukov 		err(EX_OSERR, "nat64stl instance configuration failed");
357d8caf56eSAndrey V. Elsukov }
358d8caf56eSAndrey V. Elsukov 
359d8caf56eSAndrey V. Elsukov /*
360d8caf56eSAndrey V. Elsukov  * Destroys nat64stl instance.
361d8caf56eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ]
362d8caf56eSAndrey V. Elsukov  */
363d8caf56eSAndrey V. Elsukov static void
364d8caf56eSAndrey V. Elsukov nat64stl_destroy(const char *name, uint8_t set)
365d8caf56eSAndrey V. Elsukov {
366d8caf56eSAndrey V. Elsukov 	ipfw_obj_header oh;
367d8caf56eSAndrey V. Elsukov 
368d8caf56eSAndrey V. Elsukov 	memset(&oh, 0, sizeof(oh));
369d8caf56eSAndrey V. Elsukov 	nat64stl_fill_ntlv(&oh.ntlv, name, set);
370d8caf56eSAndrey V. Elsukov 	if (do_set3(IP_FW_NAT64STL_DESTROY, &oh.opheader, sizeof(oh)) != 0)
371d8caf56eSAndrey V. Elsukov 		err(EX_OSERR, "failed to destroy nat instance %s", name);
372d8caf56eSAndrey V. Elsukov }
373d8caf56eSAndrey V. Elsukov 
374d8caf56eSAndrey V. Elsukov /*
375d8caf56eSAndrey V. Elsukov  * Get nat64stl instance statistics.
376d8caf56eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ]
377d8caf56eSAndrey V. Elsukov  * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ] ]
378d8caf56eSAndrey V. Elsukov  */
379d8caf56eSAndrey V. Elsukov static int
380d8caf56eSAndrey V. Elsukov nat64stl_get_stats(const char *name, uint8_t set,
381d8caf56eSAndrey V. Elsukov     struct ipfw_nat64stl_stats *stats)
382d8caf56eSAndrey V. Elsukov {
383d8caf56eSAndrey V. Elsukov 	ipfw_obj_header *oh;
384d8caf56eSAndrey V. Elsukov 	ipfw_obj_ctlv *oc;
385d8caf56eSAndrey V. Elsukov 	size_t sz;
386d8caf56eSAndrey V. Elsukov 
387d8caf56eSAndrey V. Elsukov 	sz = sizeof(*oh) + sizeof(*oc) + sizeof(*stats);
388d8caf56eSAndrey V. Elsukov 	oh = calloc(1, sz);
389d8caf56eSAndrey V. Elsukov 	nat64stl_fill_ntlv(&oh->ntlv, name, set);
390d8caf56eSAndrey V. Elsukov 	if (do_get3(IP_FW_NAT64STL_STATS, &oh->opheader, &sz) == 0) {
391d8caf56eSAndrey V. Elsukov 		oc = (ipfw_obj_ctlv *)(oh + 1);
392d8caf56eSAndrey V. Elsukov 		memcpy(stats, oc + 1, sizeof(*stats));
393d8caf56eSAndrey V. Elsukov 		free(oh);
394d8caf56eSAndrey V. Elsukov 		return (0);
395d8caf56eSAndrey V. Elsukov 	}
396d8caf56eSAndrey V. Elsukov 	free(oh);
397d8caf56eSAndrey V. Elsukov 	return (-1);
398d8caf56eSAndrey V. Elsukov }
399d8caf56eSAndrey V. Elsukov 
400d8caf56eSAndrey V. Elsukov static void
401d8caf56eSAndrey V. Elsukov nat64stl_stats(const char *name, uint8_t set)
402d8caf56eSAndrey V. Elsukov {
403d8caf56eSAndrey V. Elsukov 	struct ipfw_nat64stl_stats stats;
404d8caf56eSAndrey V. Elsukov 
405d8caf56eSAndrey V. Elsukov 	if (nat64stl_get_stats(name, set, &stats) != 0)
406d8caf56eSAndrey V. Elsukov 		err(EX_OSERR, "Error retrieving stats");
407d8caf56eSAndrey V. Elsukov 
408c5e85276SAndrey V. Elsukov 	if (co.use_set != 0 || set != 0)
409c5e85276SAndrey V. Elsukov 		printf("set %u ", set);
410c5e85276SAndrey V. Elsukov 	printf("nat64stl %s\n", name);
411c5e85276SAndrey V. Elsukov 
412c5e85276SAndrey V. Elsukov 	printf("\t%ju packets translated from IPv6 to IPv4\n",
413c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.opcnt64);
414c5e85276SAndrey V. Elsukov 	printf("\t%ju packets translated from IPv4 to IPv6\n",
415c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.opcnt46);
416c5e85276SAndrey V. Elsukov 	printf("\t%ju IPv6 fragments created\n",
417c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.ofrags);
418c5e85276SAndrey V. Elsukov 	printf("\t%ju IPv4 fragments received\n",
419c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.ifrags);
420c5e85276SAndrey V. Elsukov 	printf("\t%ju output packets dropped due to no bufs, etc.\n",
421c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.oerrors);
422c5e85276SAndrey V. Elsukov 	printf("\t%ju output packets discarded due to no IPv4 route\n",
423c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.noroute4);
424c5e85276SAndrey V. Elsukov 	printf("\t%ju output packets discarded due to no IPv6 route\n",
425c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.noroute6);
426c5e85276SAndrey V. Elsukov 	printf("\t%ju packets discarded due to unsupported protocol\n",
427c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.noproto);
428c5e85276SAndrey V. Elsukov 	printf("\t%ju packets discarded due to memory allocation problems\n",
429c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.nomem);
430c5e85276SAndrey V. Elsukov 	printf("\t%ju packets discarded due to some errors\n",
431c5e85276SAndrey V. Elsukov 	    (uintmax_t)stats.dropped);
432d8caf56eSAndrey V. Elsukov }
433d8caf56eSAndrey V. Elsukov 
434d8caf56eSAndrey V. Elsukov /*
435d8caf56eSAndrey V. Elsukov  * Reset nat64stl instance statistics specified by @oh->ntlv.
436d8caf56eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ]
437d8caf56eSAndrey V. Elsukov  */
438d8caf56eSAndrey V. Elsukov static void
439d8caf56eSAndrey V. Elsukov nat64stl_reset_stats(const char *name, uint8_t set)
440d8caf56eSAndrey V. Elsukov {
441d8caf56eSAndrey V. Elsukov 	ipfw_obj_header oh;
442d8caf56eSAndrey V. Elsukov 
443d8caf56eSAndrey V. Elsukov 	memset(&oh, 0, sizeof(oh));
444d8caf56eSAndrey V. Elsukov 	nat64stl_fill_ntlv(&oh.ntlv, name, set);
445d8caf56eSAndrey V. Elsukov 	if (do_set3(IP_FW_NAT64STL_RESET_STATS, &oh.opheader, sizeof(oh)) != 0)
446d8caf56eSAndrey V. Elsukov 		err(EX_OSERR, "failed to reset stats for instance %s", name);
447d8caf56eSAndrey V. Elsukov }
448d8caf56eSAndrey V. Elsukov 
449d8caf56eSAndrey V. Elsukov static int
450d8caf56eSAndrey V. Elsukov nat64stl_show_cb(ipfw_nat64stl_cfg *cfg, const char *name, uint8_t set)
451d8caf56eSAndrey V. Elsukov {
452782360deSAndrey V. Elsukov 	char abuf[INET6_ADDRSTRLEN];
453d8caf56eSAndrey V. Elsukov 
454d8caf56eSAndrey V. Elsukov 	if (name != NULL && strcmp(cfg->name, name) != 0)
455d8caf56eSAndrey V. Elsukov 		return (ESRCH);
456d8caf56eSAndrey V. Elsukov 
457d8caf56eSAndrey V. Elsukov 	if (co.use_set != 0 && cfg->set != set)
458d8caf56eSAndrey V. Elsukov 		return (ESRCH);
459d8caf56eSAndrey V. Elsukov 
460d8caf56eSAndrey V. Elsukov 	if (co.use_set != 0 || cfg->set != 0)
461d8caf56eSAndrey V. Elsukov 		printf("set %u ", cfg->set);
462782360deSAndrey V. Elsukov 
463d8caf56eSAndrey V. Elsukov 	printf("nat64stl %s table4 %s table6 %s",
464d8caf56eSAndrey V. Elsukov 	    cfg->name, cfg->ntlv4.name, cfg->ntlv6.name);
465782360deSAndrey V. Elsukov 	inet_ntop(AF_INET6, &cfg->prefix6, abuf, sizeof(abuf));
466782360deSAndrey V. Elsukov 	printf(" prefix6 %s/%u", abuf, cfg->plen6);
467d8caf56eSAndrey V. Elsukov 	if (cfg->flags & NAT64_LOG)
468d8caf56eSAndrey V. Elsukov 		printf(" log");
469b11efc1eSAndrey V. Elsukov 	if (cfg->flags & NAT64_ALLOW_PRIVATE)
470b11efc1eSAndrey V. Elsukov 		printf(" allow_private");
471d8caf56eSAndrey V. Elsukov 	printf("\n");
472d8caf56eSAndrey V. Elsukov 	return (0);
473d8caf56eSAndrey V. Elsukov }
474d8caf56eSAndrey V. Elsukov 
475d8caf56eSAndrey V. Elsukov static int
476d8caf56eSAndrey V. Elsukov nat64stl_destroy_cb(ipfw_nat64stl_cfg *cfg, const char *name, uint8_t set)
477d8caf56eSAndrey V. Elsukov {
478d8caf56eSAndrey V. Elsukov 
479d8caf56eSAndrey V. Elsukov 	if (co.use_set != 0 && cfg->set != set)
480d8caf56eSAndrey V. Elsukov 		return (ESRCH);
481d8caf56eSAndrey V. Elsukov 
482d8caf56eSAndrey V. Elsukov 	nat64stl_destroy(cfg->name, cfg->set);
483d8caf56eSAndrey V. Elsukov 	return (0);
484d8caf56eSAndrey V. Elsukov }
485d8caf56eSAndrey V. Elsukov 
486d8caf56eSAndrey V. Elsukov 
487d8caf56eSAndrey V. Elsukov /*
488d8caf56eSAndrey V. Elsukov  * Compare nat64stl instances names.
489d8caf56eSAndrey V. Elsukov  * Honor number comparison.
490d8caf56eSAndrey V. Elsukov  */
491d8caf56eSAndrey V. Elsukov static int
492d8caf56eSAndrey V. Elsukov nat64name_cmp(const void *a, const void *b)
493d8caf56eSAndrey V. Elsukov {
494d8caf56eSAndrey V. Elsukov 	ipfw_nat64stl_cfg *ca, *cb;
495d8caf56eSAndrey V. Elsukov 
496d8caf56eSAndrey V. Elsukov 	ca = (ipfw_nat64stl_cfg *)a;
497d8caf56eSAndrey V. Elsukov 	cb = (ipfw_nat64stl_cfg *)b;
498d8caf56eSAndrey V. Elsukov 
499d8caf56eSAndrey V. Elsukov 	if (ca->set > cb->set)
500d8caf56eSAndrey V. Elsukov 		return (1);
501d8caf56eSAndrey V. Elsukov 	else if (ca->set < cb->set)
502d8caf56eSAndrey V. Elsukov 		return (-1);
503d8caf56eSAndrey V. Elsukov 	return (stringnum_cmp(ca->name, cb->name));
504d8caf56eSAndrey V. Elsukov }
505d8caf56eSAndrey V. Elsukov 
506d8caf56eSAndrey V. Elsukov /*
507d8caf56eSAndrey V. Elsukov  * Retrieves nat64stl instance list from kernel,
508d8caf56eSAndrey V. Elsukov  * optionally sorts it and calls requested function for each instance.
509d8caf56eSAndrey V. Elsukov  *
510d8caf56eSAndrey V. Elsukov  * Request: [ ipfw_obj_lheader ]
511d8caf56eSAndrey V. Elsukov  * Reply: [ ipfw_obj_lheader ipfw_nat64stl_cfg x N ]
512d8caf56eSAndrey V. Elsukov  */
513d8caf56eSAndrey V. Elsukov static int
514d8caf56eSAndrey V. Elsukov nat64stl_foreach(nat64stl_cb_t *f, const char *name, uint8_t set, int sort)
515d8caf56eSAndrey V. Elsukov {
516d8caf56eSAndrey V. Elsukov 	ipfw_obj_lheader *olh;
517d8caf56eSAndrey V. Elsukov 	ipfw_nat64stl_cfg *cfg;
518d8caf56eSAndrey V. Elsukov 	size_t sz;
519d8caf56eSAndrey V. Elsukov 	int i, error;
520d8caf56eSAndrey V. Elsukov 
521d8caf56eSAndrey V. Elsukov 	/* Start with reasonable default */
522d8caf56eSAndrey V. Elsukov 	sz = sizeof(*olh) + 16 * sizeof(*cfg);
523d8caf56eSAndrey V. Elsukov 	for (;;) {
524d8caf56eSAndrey V. Elsukov 		if ((olh = calloc(1, sz)) == NULL)
525d8caf56eSAndrey V. Elsukov 			return (ENOMEM);
526d8caf56eSAndrey V. Elsukov 
527d8caf56eSAndrey V. Elsukov 		olh->size = sz;
528d8caf56eSAndrey V. Elsukov 		if (do_get3(IP_FW_NAT64STL_LIST, &olh->opheader, &sz) != 0) {
529d8caf56eSAndrey V. Elsukov 			sz = olh->size;
530d8caf56eSAndrey V. Elsukov 			free(olh);
531d8caf56eSAndrey V. Elsukov 			if (errno != ENOMEM)
532d8caf56eSAndrey V. Elsukov 				return (errno);
533d8caf56eSAndrey V. Elsukov 			continue;
534d8caf56eSAndrey V. Elsukov 		}
535d8caf56eSAndrey V. Elsukov 
536d8caf56eSAndrey V. Elsukov 		if (sort != 0)
537d8caf56eSAndrey V. Elsukov 			qsort(olh + 1, olh->count, olh->objsize,
538d8caf56eSAndrey V. Elsukov 			    nat64name_cmp);
539d8caf56eSAndrey V. Elsukov 
540d8caf56eSAndrey V. Elsukov 		cfg = (ipfw_nat64stl_cfg *)(olh + 1);
541d8caf56eSAndrey V. Elsukov 		for (i = 0; i < olh->count; i++) {
542d8caf56eSAndrey V. Elsukov 			error = f(cfg, name, set); /* Ignore errors for now */
543d8caf56eSAndrey V. Elsukov 			cfg = (ipfw_nat64stl_cfg *)((caddr_t)cfg +
544d8caf56eSAndrey V. Elsukov 			    olh->objsize);
545d8caf56eSAndrey V. Elsukov 		}
546d8caf56eSAndrey V. Elsukov 		free(olh);
547d8caf56eSAndrey V. Elsukov 		break;
548d8caf56eSAndrey V. Elsukov 	}
549d8caf56eSAndrey V. Elsukov 	return (0);
550d8caf56eSAndrey V. Elsukov }
551d8caf56eSAndrey V. Elsukov 
552