1d8caf56eSAndrey V. Elsukov /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3002cae78SAndrey V. Elsukov *
4002cae78SAndrey V. Elsukov * Copyright (c) 2015-2019 Yandex LLC
5002cae78SAndrey 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/types.h>
30d8caf56eSAndrey V. Elsukov #include <sys/socket.h>
31d8caf56eSAndrey V. Elsukov
32d8caf56eSAndrey V. Elsukov #include "ipfw2.h"
33d8caf56eSAndrey V. Elsukov
34d8caf56eSAndrey V. Elsukov #include <ctype.h>
35d8caf56eSAndrey V. Elsukov #include <err.h>
36d8caf56eSAndrey V. Elsukov #include <errno.h>
37c5e85276SAndrey V. Elsukov #include <inttypes.h>
38d8caf56eSAndrey V. Elsukov #include <netdb.h>
39d8caf56eSAndrey V. Elsukov #include <stdio.h>
40d8caf56eSAndrey V. Elsukov #include <stdlib.h>
41d8caf56eSAndrey V. Elsukov #include <string.h>
42d8caf56eSAndrey V. Elsukov #include <sysexits.h>
43d8caf56eSAndrey V. Elsukov
44d8caf56eSAndrey V. Elsukov #include <net/if.h>
45d8caf56eSAndrey V. Elsukov #include <netinet/in.h>
46d8caf56eSAndrey V. Elsukov #include <netinet/ip_fw.h>
47d8caf56eSAndrey V. Elsukov #include <netinet6/ip_fw_nat64.h>
48d8caf56eSAndrey V. Elsukov #include <arpa/inet.h>
49d8caf56eSAndrey V. Elsukov
50d8caf56eSAndrey V. Elsukov typedef int (nat64stl_cb_t)(ipfw_nat64stl_cfg *i, const char *name,
51d8caf56eSAndrey V. Elsukov uint8_t set);
52d8caf56eSAndrey V. Elsukov static int nat64stl_foreach(nat64stl_cb_t *f, const char *name, uint8_t set,
53d8caf56eSAndrey V. Elsukov int sort);
54d8caf56eSAndrey V. Elsukov
55d8caf56eSAndrey V. Elsukov static void nat64stl_create(const char *name, uint8_t set, int ac, char **av);
56d8caf56eSAndrey V. Elsukov static void nat64stl_config(const char *name, uint8_t set, int ac, char **av);
57d8caf56eSAndrey V. Elsukov static void nat64stl_destroy(const char *name, uint8_t set);
58d8caf56eSAndrey V. Elsukov static void nat64stl_stats(const char *name, uint8_t set);
59d8caf56eSAndrey V. Elsukov static void nat64stl_reset_stats(const char *name, uint8_t set);
60d8caf56eSAndrey V. Elsukov static int nat64stl_show_cb(ipfw_nat64stl_cfg *cfg, const char *name,
61d8caf56eSAndrey V. Elsukov uint8_t set);
62d8caf56eSAndrey V. Elsukov static int nat64stl_destroy_cb(ipfw_nat64stl_cfg *cfg, const char *name,
63d8caf56eSAndrey V. Elsukov uint8_t set);
64d8caf56eSAndrey V. Elsukov
65d8caf56eSAndrey V. Elsukov static struct _s_x nat64cmds[] = {
66d8caf56eSAndrey V. Elsukov { "create", TOK_CREATE },
67d8caf56eSAndrey V. Elsukov { "config", TOK_CONFIG },
68d8caf56eSAndrey V. Elsukov { "destroy", TOK_DESTROY },
69d8caf56eSAndrey V. Elsukov { "list", TOK_LIST },
70d8caf56eSAndrey V. Elsukov { "show", TOK_LIST },
71d8caf56eSAndrey V. Elsukov { "stats", TOK_STATS },
72d8caf56eSAndrey V. Elsukov { NULL, 0 }
73d8caf56eSAndrey V. Elsukov };
74d8caf56eSAndrey V. Elsukov
75d8caf56eSAndrey V. Elsukov #define IPV6_ADDR_INT32_WKPFX htonl(0x64ff9b)
76d8caf56eSAndrey V. Elsukov #define IN6_IS_ADDR_WKPFX(a) \
77d8caf56eSAndrey V. Elsukov ((a)->__u6_addr.__u6_addr32[0] == IPV6_ADDR_INT32_WKPFX && \
78d8caf56eSAndrey V. Elsukov (a)->__u6_addr.__u6_addr32[1] == 0 && \
79d8caf56eSAndrey V. Elsukov (a)->__u6_addr.__u6_addr32[2] == 0)
80782360deSAndrey V. Elsukov int
ipfw_check_nat64prefix(const struct in6_addr * prefix,int length)81782360deSAndrey V. Elsukov ipfw_check_nat64prefix(const struct in6_addr *prefix, int length)
82d8caf56eSAndrey V. Elsukov {
83d8caf56eSAndrey V. Elsukov
84d8caf56eSAndrey V. Elsukov switch (length) {
85d8caf56eSAndrey V. Elsukov case 32:
86d8caf56eSAndrey V. Elsukov case 40:
87d8caf56eSAndrey V. Elsukov case 48:
88d8caf56eSAndrey V. Elsukov case 56:
89d8caf56eSAndrey V. Elsukov case 64:
90d8caf56eSAndrey V. Elsukov /* Well-known prefix has 96 prefix length */
91d8caf56eSAndrey V. Elsukov if (IN6_IS_ADDR_WKPFX(prefix))
92782360deSAndrey V. Elsukov return (EINVAL);
93d8caf56eSAndrey V. Elsukov /* FALLTHROUGH */
94d8caf56eSAndrey V. Elsukov case 96:
95d8caf56eSAndrey V. Elsukov /* Bits 64 to 71 must be set to zero */
96d8caf56eSAndrey V. Elsukov if (prefix->__u6_addr.__u6_addr8[8] != 0)
97782360deSAndrey V. Elsukov return (EINVAL);
98d8caf56eSAndrey V. Elsukov /* XXX: looks incorrect */
99d8caf56eSAndrey V. Elsukov if (IN6_IS_ADDR_MULTICAST(prefix) ||
100d8caf56eSAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(prefix) ||
101d8caf56eSAndrey V. Elsukov IN6_IS_ADDR_LOOPBACK(prefix))
102782360deSAndrey V. Elsukov return (EINVAL);
103d8caf56eSAndrey V. Elsukov return (0);
104d8caf56eSAndrey V. Elsukov }
105782360deSAndrey V. Elsukov return (EINVAL);
106d8caf56eSAndrey V. Elsukov }
107d8caf56eSAndrey V. Elsukov
108d8caf56eSAndrey V. Elsukov static struct _s_x nat64statscmds[] = {
109d8caf56eSAndrey V. Elsukov { "reset", TOK_RESET },
110d8caf56eSAndrey V. Elsukov { NULL, 0 }
111d8caf56eSAndrey V. Elsukov };
112d8caf56eSAndrey V. Elsukov
113d8caf56eSAndrey V. Elsukov /*
114d8caf56eSAndrey V. Elsukov * This one handles all nat64stl-related commands
115d8caf56eSAndrey V. Elsukov * ipfw [set N] nat64stl NAME {create | config} ...
116d8caf56eSAndrey V. Elsukov * ipfw [set N] nat64stl NAME stats [reset]
117d8caf56eSAndrey V. Elsukov * ipfw [set N] nat64stl {NAME | all} destroy
118d8caf56eSAndrey V. Elsukov * ipfw [set N] nat64stl {NAME | all} {list | show}
119d8caf56eSAndrey V. Elsukov */
120d8caf56eSAndrey V. Elsukov #define nat64stl_check_name table_check_name
121d8caf56eSAndrey V. Elsukov void
ipfw_nat64stl_handler(int ac,char * av[])122d8caf56eSAndrey V. Elsukov ipfw_nat64stl_handler(int ac, char *av[])
123d8caf56eSAndrey V. Elsukov {
124d8caf56eSAndrey V. Elsukov const char *name;
125d8caf56eSAndrey V. Elsukov int tcmd;
126d8caf56eSAndrey V. Elsukov uint8_t set;
127d8caf56eSAndrey V. Elsukov
12856707beeSMark Johnston if (g_co.use_set != 0)
12956707beeSMark Johnston set = g_co.use_set - 1;
130d8caf56eSAndrey V. Elsukov else
131d8caf56eSAndrey V. Elsukov set = 0;
132d8caf56eSAndrey V. Elsukov ac--; av++;
133d8caf56eSAndrey V. Elsukov
134d8caf56eSAndrey V. Elsukov NEED1("nat64stl needs instance name");
135d8caf56eSAndrey V. Elsukov name = *av;
136d8caf56eSAndrey V. Elsukov if (nat64stl_check_name(name) != 0) {
137d8caf56eSAndrey V. Elsukov if (strcmp(name, "all") == 0)
138d8caf56eSAndrey V. Elsukov name = NULL;
139d8caf56eSAndrey V. Elsukov else
140d8caf56eSAndrey V. Elsukov errx(EX_USAGE, "nat64stl instance name %s is invalid",
141d8caf56eSAndrey V. Elsukov name);
142d8caf56eSAndrey V. Elsukov }
143d8caf56eSAndrey V. Elsukov ac--; av++;
144d8caf56eSAndrey V. Elsukov NEED1("nat64stl needs command");
145d8caf56eSAndrey V. Elsukov
146d8caf56eSAndrey V. Elsukov tcmd = get_token(nat64cmds, *av, "nat64stl command");
147d8caf56eSAndrey V. Elsukov if (name == NULL && tcmd != TOK_DESTROY && tcmd != TOK_LIST)
148d8caf56eSAndrey V. Elsukov errx(EX_USAGE, "nat64stl instance name required");
149d8caf56eSAndrey V. Elsukov switch (tcmd) {
150d8caf56eSAndrey V. Elsukov case TOK_CREATE:
151d8caf56eSAndrey V. Elsukov ac--; av++;
152d8caf56eSAndrey V. Elsukov nat64stl_create(name, set, ac, av);
153d8caf56eSAndrey V. Elsukov break;
154d8caf56eSAndrey V. Elsukov case TOK_CONFIG:
155d8caf56eSAndrey V. Elsukov ac--; av++;
156d8caf56eSAndrey V. Elsukov nat64stl_config(name, set, ac, av);
157d8caf56eSAndrey V. Elsukov break;
158d8caf56eSAndrey V. Elsukov case TOK_LIST:
159d8caf56eSAndrey V. Elsukov nat64stl_foreach(nat64stl_show_cb, name, set, 1);
160d8caf56eSAndrey V. Elsukov break;
161d8caf56eSAndrey V. Elsukov case TOK_DESTROY:
162d8caf56eSAndrey V. Elsukov if (name == NULL)
163d8caf56eSAndrey V. Elsukov nat64stl_foreach(nat64stl_destroy_cb, NULL, set, 0);
164d8caf56eSAndrey V. Elsukov else
165d8caf56eSAndrey V. Elsukov nat64stl_destroy(name, set);
166d8caf56eSAndrey V. Elsukov break;
167d8caf56eSAndrey V. Elsukov case TOK_STATS:
168d8caf56eSAndrey V. Elsukov ac--; av++;
169d8caf56eSAndrey V. Elsukov if (ac == 0) {
170d8caf56eSAndrey V. Elsukov nat64stl_stats(name, set);
171d8caf56eSAndrey V. Elsukov break;
172d8caf56eSAndrey V. Elsukov }
173d8caf56eSAndrey V. Elsukov tcmd = get_token(nat64statscmds, *av, "stats command");
174d8caf56eSAndrey V. Elsukov if (tcmd == TOK_RESET)
175d8caf56eSAndrey V. Elsukov nat64stl_reset_stats(name, set);
176d8caf56eSAndrey V. Elsukov }
177d8caf56eSAndrey V. Elsukov }
178d8caf56eSAndrey V. Elsukov
179d8caf56eSAndrey V. Elsukov
180d8caf56eSAndrey V. Elsukov static void
nat64stl_fill_ntlv(ipfw_obj_ntlv * ntlv,const char * name,uint8_t set)181d8caf56eSAndrey V. Elsukov nat64stl_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set)
182d8caf56eSAndrey V. Elsukov {
183d8caf56eSAndrey V. Elsukov
184d8caf56eSAndrey V. Elsukov ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
185d8caf56eSAndrey V. Elsukov ntlv->head.length = sizeof(ipfw_obj_ntlv);
186d8caf56eSAndrey V. Elsukov ntlv->idx = 1;
187d8caf56eSAndrey V. Elsukov ntlv->set = set;
188d8caf56eSAndrey V. Elsukov strlcpy(ntlv->name, name, sizeof(ntlv->name));
189d8caf56eSAndrey V. Elsukov }
190d8caf56eSAndrey V. Elsukov
191d8caf56eSAndrey V. Elsukov static struct _s_x nat64newcmds[] = {
192d8caf56eSAndrey V. Elsukov { "table4", TOK_TABLE4 },
193d8caf56eSAndrey V. Elsukov { "table6", TOK_TABLE6 },
194d8caf56eSAndrey V. Elsukov { "prefix6", TOK_PREFIX6 },
195d8caf56eSAndrey V. Elsukov { "log", TOK_LOG },
196d8caf56eSAndrey V. Elsukov { "-log", TOK_LOGOFF },
197b11efc1eSAndrey V. Elsukov { "allow_private", TOK_PRIVATE },
198b11efc1eSAndrey V. Elsukov { "-allow_private", TOK_PRIVATEOFF },
199d8caf56eSAndrey V. Elsukov { NULL, 0 }
200d8caf56eSAndrey V. Elsukov };
201d8caf56eSAndrey V. Elsukov
202d8caf56eSAndrey V. Elsukov /*
203d8caf56eSAndrey V. Elsukov * Creates new nat64stl instance
204d8caf56eSAndrey V. Elsukov * ipfw nat64stl <NAME> create table4 <name> table6 <name> [ prefix6 <prefix>]
205d8caf56eSAndrey V. Elsukov * Request: [ ipfw_obj_lheader ipfw_nat64stl_cfg ]
206d8caf56eSAndrey V. Elsukov */
207d8caf56eSAndrey V. Elsukov #define NAT64STL_HAS_TABLE4 0x01
208d8caf56eSAndrey V. Elsukov #define NAT64STL_HAS_TABLE6 0x02
209d8caf56eSAndrey V. Elsukov #define NAT64STL_HAS_PREFIX6 0x04
210d8caf56eSAndrey V. Elsukov static void
nat64stl_create(const char * name,uint8_t set,int ac,char * av[])211d8caf56eSAndrey V. Elsukov nat64stl_create(const char *name, uint8_t set, int ac, char *av[])
212d8caf56eSAndrey V. Elsukov {
213d8caf56eSAndrey V. Elsukov char buf[sizeof(ipfw_obj_lheader) + sizeof(ipfw_nat64stl_cfg)];
214d8caf56eSAndrey V. Elsukov ipfw_nat64stl_cfg *cfg;
215d8caf56eSAndrey V. Elsukov ipfw_obj_lheader *olh;
216d8caf56eSAndrey V. Elsukov int tcmd, flags;
217d8caf56eSAndrey V. Elsukov char *p;
218d8caf56eSAndrey V. Elsukov
219d8caf56eSAndrey V. Elsukov memset(buf, 0, sizeof(buf));
220d8caf56eSAndrey V. Elsukov olh = (ipfw_obj_lheader *)buf;
221d8caf56eSAndrey V. Elsukov cfg = (ipfw_nat64stl_cfg *)(olh + 1);
222d8caf56eSAndrey V. Elsukov
223d8caf56eSAndrey V. Elsukov /* Some reasonable defaults */
224d8caf56eSAndrey V. Elsukov inet_pton(AF_INET6, "64:ff9b::", &cfg->prefix6);
225d8caf56eSAndrey V. Elsukov cfg->plen6 = 96;
226d8caf56eSAndrey V. Elsukov cfg->set = set;
227d8caf56eSAndrey V. Elsukov flags = NAT64STL_HAS_PREFIX6;
228d8caf56eSAndrey V. Elsukov while (ac > 0) {
229d8caf56eSAndrey V. Elsukov tcmd = get_token(nat64newcmds, *av, "option");
230d8caf56eSAndrey V. Elsukov ac--; av++;
231d8caf56eSAndrey V. Elsukov
232d8caf56eSAndrey V. Elsukov switch (tcmd) {
233d8caf56eSAndrey V. Elsukov case TOK_TABLE4:
234d8caf56eSAndrey V. Elsukov NEED1("table name required");
235d8caf56eSAndrey V. Elsukov table_fill_ntlv(&cfg->ntlv4, *av, set, 4);
236d8caf56eSAndrey V. Elsukov flags |= NAT64STL_HAS_TABLE4;
237d8caf56eSAndrey V. Elsukov ac--; av++;
238d8caf56eSAndrey V. Elsukov break;
239d8caf56eSAndrey V. Elsukov case TOK_TABLE6:
240d8caf56eSAndrey V. Elsukov NEED1("table name required");
241d8caf56eSAndrey V. Elsukov table_fill_ntlv(&cfg->ntlv6, *av, set, 6);
242d8caf56eSAndrey V. Elsukov flags |= NAT64STL_HAS_TABLE6;
243d8caf56eSAndrey V. Elsukov ac--; av++;
244d8caf56eSAndrey V. Elsukov break;
245d8caf56eSAndrey V. Elsukov case TOK_PREFIX6:
246d8caf56eSAndrey V. Elsukov NEED1("IPv6 prefix6 required");
247d8caf56eSAndrey V. Elsukov if ((p = strchr(*av, '/')) != NULL)
248d8caf56eSAndrey V. Elsukov *p++ = '\0';
2492403d477SAndrey V. Elsukov else
2502403d477SAndrey V. Elsukov errx(EX_USAGE,
2512403d477SAndrey V. Elsukov "Prefix length required: %s", *av);
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
nat64stl_config(const char * name,uint8_t set,int ac,char ** av)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
nat64stl_destroy(const char * name,uint8_t set)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
nat64stl_get_stats(const char * name,uint8_t set,struct ipfw_nat64stl_stats * stats)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
nat64stl_stats(const char * name,uint8_t set)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
40856707beeSMark Johnston if (g_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
nat64stl_reset_stats(const char * name,uint8_t set)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
nat64stl_show_cb(ipfw_nat64stl_cfg * cfg,const char * name,uint8_t set)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
45756707beeSMark Johnston if (g_co.use_set != 0 && cfg->set != set)
458d8caf56eSAndrey V. Elsukov return (ESRCH);
459d8caf56eSAndrey V. Elsukov
46056707beeSMark Johnston if (g_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
nat64stl_destroy_cb(ipfw_nat64stl_cfg * cfg,const char * name __unused,uint8_t set)47656707beeSMark Johnston nat64stl_destroy_cb(ipfw_nat64stl_cfg *cfg, const char *name __unused,
47756707beeSMark Johnston uint8_t set)
478d8caf56eSAndrey V. Elsukov {
479d8caf56eSAndrey V. Elsukov
48056707beeSMark Johnston if (g_co.use_set != 0 && cfg->set != set)
481d8caf56eSAndrey V. Elsukov return (ESRCH);
482d8caf56eSAndrey V. Elsukov
483d8caf56eSAndrey V. Elsukov nat64stl_destroy(cfg->name, cfg->set);
484d8caf56eSAndrey V. Elsukov return (0);
485d8caf56eSAndrey V. Elsukov }
486d8caf56eSAndrey V. Elsukov
487d8caf56eSAndrey V. Elsukov
488d8caf56eSAndrey V. Elsukov /*
489d8caf56eSAndrey V. Elsukov * Compare nat64stl instances names.
490d8caf56eSAndrey V. Elsukov * Honor number comparison.
491d8caf56eSAndrey V. Elsukov */
492d8caf56eSAndrey V. Elsukov static int
nat64name_cmp(const void * a,const void * b)493d8caf56eSAndrey V. Elsukov nat64name_cmp(const void *a, const void *b)
494d8caf56eSAndrey V. Elsukov {
49556707beeSMark Johnston const ipfw_nat64stl_cfg *ca, *cb;
496d8caf56eSAndrey V. Elsukov
49756707beeSMark Johnston ca = (const ipfw_nat64stl_cfg *)a;
49856707beeSMark Johnston cb = (const ipfw_nat64stl_cfg *)b;
499d8caf56eSAndrey V. Elsukov
500d8caf56eSAndrey V. Elsukov if (ca->set > cb->set)
501d8caf56eSAndrey V. Elsukov return (1);
502d8caf56eSAndrey V. Elsukov else if (ca->set < cb->set)
503d8caf56eSAndrey V. Elsukov return (-1);
504d8caf56eSAndrey V. Elsukov return (stringnum_cmp(ca->name, cb->name));
505d8caf56eSAndrey V. Elsukov }
506d8caf56eSAndrey V. Elsukov
507d8caf56eSAndrey V. Elsukov /*
508d8caf56eSAndrey V. Elsukov * Retrieves nat64stl instance list from kernel,
509d8caf56eSAndrey V. Elsukov * optionally sorts it and calls requested function for each instance.
510d8caf56eSAndrey V. Elsukov *
511d8caf56eSAndrey V. Elsukov * Request: [ ipfw_obj_lheader ]
512d8caf56eSAndrey V. Elsukov * Reply: [ ipfw_obj_lheader ipfw_nat64stl_cfg x N ]
513d8caf56eSAndrey V. Elsukov */
514d8caf56eSAndrey V. Elsukov static int
nat64stl_foreach(nat64stl_cb_t * f,const char * name,uint8_t set,int sort)515d8caf56eSAndrey V. Elsukov nat64stl_foreach(nat64stl_cb_t *f, const char *name, uint8_t set, int sort)
516d8caf56eSAndrey V. Elsukov {
517d8caf56eSAndrey V. Elsukov ipfw_obj_lheader *olh;
518d8caf56eSAndrey V. Elsukov ipfw_nat64stl_cfg *cfg;
519d8caf56eSAndrey V. Elsukov size_t sz;
52056707beeSMark Johnston uint32_t i;
521d8caf56eSAndrey V. Elsukov
522d8caf56eSAndrey V. Elsukov /* Start with reasonable default */
523d8caf56eSAndrey V. Elsukov sz = sizeof(*olh) + 16 * sizeof(*cfg);
524d8caf56eSAndrey V. Elsukov for (;;) {
525d8caf56eSAndrey V. Elsukov if ((olh = calloc(1, sz)) == NULL)
526d8caf56eSAndrey V. Elsukov return (ENOMEM);
527d8caf56eSAndrey V. Elsukov
528d8caf56eSAndrey V. Elsukov olh->size = sz;
529d8caf56eSAndrey V. Elsukov if (do_get3(IP_FW_NAT64STL_LIST, &olh->opheader, &sz) != 0) {
530d8caf56eSAndrey V. Elsukov sz = olh->size;
531d8caf56eSAndrey V. Elsukov free(olh);
532d8caf56eSAndrey V. Elsukov if (errno != ENOMEM)
533d8caf56eSAndrey V. Elsukov return (errno);
534d8caf56eSAndrey V. Elsukov continue;
535d8caf56eSAndrey V. Elsukov }
536d8caf56eSAndrey V. Elsukov
537d8caf56eSAndrey V. Elsukov if (sort != 0)
538d8caf56eSAndrey V. Elsukov qsort(olh + 1, olh->count, olh->objsize,
539d8caf56eSAndrey V. Elsukov nat64name_cmp);
540d8caf56eSAndrey V. Elsukov
541d8caf56eSAndrey V. Elsukov cfg = (ipfw_nat64stl_cfg *)(olh + 1);
542d8caf56eSAndrey V. Elsukov for (i = 0; i < olh->count; i++) {
543*f5e73306SJohn Baldwin (void)f(cfg, name, set); /* Ignore errors for now */
544d8caf56eSAndrey V. Elsukov cfg = (ipfw_nat64stl_cfg *)((caddr_t)cfg +
545d8caf56eSAndrey V. Elsukov olh->objsize);
546d8caf56eSAndrey V. Elsukov }
547d8caf56eSAndrey V. Elsukov free(olh);
548d8caf56eSAndrey V. Elsukov break;
549d8caf56eSAndrey V. Elsukov }
550d8caf56eSAndrey V. Elsukov return (0);
551d8caf56eSAndrey V. Elsukov }
552d8caf56eSAndrey V. Elsukov
553