2 * Copyright (c) 2015-2016 Yandex LLC
3 * Copyright (c) 2015-2016 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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netinet/ip_fw.h>
49 #include <netinet6/ip_fw_nat64.h>
50 #include <arpa/inet.h>
52 typedef int (nat64stl_cb_t)(ipfw_nat64stl_cfg *i, const char *name,
54 static int nat64stl_foreach(nat64stl_cb_t *f, const char *name, uint8_t set,
57 static void nat64stl_create(const char *name, uint8_t set, int ac, char **av);
58 static void nat64stl_config(const char *name, uint8_t set, int ac, char **av);
59 static void nat64stl_destroy(const char *name, uint8_t set);
60 static void nat64stl_stats(const char *name, uint8_t set);
61 static void nat64stl_reset_stats(const char *name, uint8_t set);
62 static int nat64stl_show_cb(ipfw_nat64stl_cfg *cfg, const char *name,
64 static int nat64stl_destroy_cb(ipfw_nat64stl_cfg *cfg, const char *name,
67 static struct _s_x nat64cmds[] = {
68 { "create", TOK_CREATE },
69 { "config", TOK_CONFIG },
70 { "destroy", TOK_DESTROY },
73 { "stats", TOK_STATS },
77 #define IPV6_ADDR_INT32_WKPFX htonl(0x64ff9b)
78 #define IN6_IS_ADDR_WKPFX(a) \
79 ((a)->__u6_addr.__u6_addr32[0] == IPV6_ADDR_INT32_WKPFX && \
80 (a)->__u6_addr.__u6_addr32[1] == 0 && \
81 (a)->__u6_addr.__u6_addr32[2] == 0)
83 ipfw_check_nat64prefix(const struct in6_addr *prefix, int length)
92 /* Well-known prefix has 96 prefix length */
93 if (IN6_IS_ADDR_WKPFX(prefix))
97 /* Bits 64 to 71 must be set to zero */
98 if (prefix->__u6_addr.__u6_addr8[8] != 0)
100 /* XXX: looks incorrect */
101 if (IN6_IS_ADDR_MULTICAST(prefix) ||
102 IN6_IS_ADDR_UNSPECIFIED(prefix) ||
103 IN6_IS_ADDR_LOOPBACK(prefix))
110 static struct _s_x nat64statscmds[] = {
111 { "reset", TOK_RESET },
116 * This one handles all nat64stl-related commands
117 * ipfw [set N] nat64stl NAME {create | config} ...
118 * ipfw [set N] nat64stl NAME stats [reset]
119 * ipfw [set N] nat64stl {NAME | all} destroy
120 * ipfw [set N] nat64stl {NAME | all} {list | show}
122 #define nat64stl_check_name table_check_name
124 ipfw_nat64stl_handler(int ac, char *av[])
131 set = co.use_set - 1;
136 NEED1("nat64stl needs instance name");
138 if (nat64stl_check_name(name) != 0) {
139 if (strcmp(name, "all") == 0)
142 errx(EX_USAGE, "nat64stl instance name %s is invalid",
146 NEED1("nat64stl needs command");
148 tcmd = get_token(nat64cmds, *av, "nat64stl command");
149 if (name == NULL && tcmd != TOK_DESTROY && tcmd != TOK_LIST)
150 errx(EX_USAGE, "nat64stl instance name required");
154 nat64stl_create(name, set, ac, av);
158 nat64stl_config(name, set, ac, av);
161 nat64stl_foreach(nat64stl_show_cb, name, set, 1);
165 nat64stl_foreach(nat64stl_destroy_cb, NULL, set, 0);
167 nat64stl_destroy(name, set);
172 nat64stl_stats(name, set);
175 tcmd = get_token(nat64statscmds, *av, "stats command");
176 if (tcmd == TOK_RESET)
177 nat64stl_reset_stats(name, set);
183 nat64stl_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set)
186 ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
187 ntlv->head.length = sizeof(ipfw_obj_ntlv);
190 strlcpy(ntlv->name, name, sizeof(ntlv->name));
193 static struct _s_x nat64newcmds[] = {
194 { "table4", TOK_TABLE4 },
195 { "table6", TOK_TABLE6 },
196 { "prefix6", TOK_PREFIX6 },
198 { "-log", TOK_LOGOFF },
199 { "allow_private", TOK_PRIVATE },
200 { "-allow_private", TOK_PRIVATEOFF },
205 * Creates new nat64stl instance
206 * ipfw nat64stl <NAME> create table4 <name> table6 <name> [ prefix6 <prefix>]
207 * Request: [ ipfw_obj_lheader ipfw_nat64stl_cfg ]
209 #define NAT64STL_HAS_TABLE4 0x01
210 #define NAT64STL_HAS_TABLE6 0x02
211 #define NAT64STL_HAS_PREFIX6 0x04
213 nat64stl_create(const char *name, uint8_t set, int ac, char *av[])
215 char buf[sizeof(ipfw_obj_lheader) + sizeof(ipfw_nat64stl_cfg)];
216 ipfw_nat64stl_cfg *cfg;
217 ipfw_obj_lheader *olh;
221 memset(buf, 0, sizeof(buf));
222 olh = (ipfw_obj_lheader *)buf;
223 cfg = (ipfw_nat64stl_cfg *)(olh + 1);
225 /* Some reasonable defaults */
226 inet_pton(AF_INET6, "64:ff9b::", &cfg->prefix6);
229 flags = NAT64STL_HAS_PREFIX6;
231 tcmd = get_token(nat64newcmds, *av, "option");
236 NEED1("table name required");
237 table_fill_ntlv(&cfg->ntlv4, *av, set, 4);
238 flags |= NAT64STL_HAS_TABLE4;
242 NEED1("table name required");
243 table_fill_ntlv(&cfg->ntlv6, *av, set, 6);
244 flags |= NAT64STL_HAS_TABLE6;
248 NEED1("IPv6 prefix6 required");
249 if ((p = strchr(*av, '/')) != NULL)
251 if (inet_pton(AF_INET6, *av, &cfg->prefix6) != 1)
253 "Bad prefix: %s", *av);
254 cfg->plen6 = strtol(p, NULL, 10);
255 if (ipfw_check_nat64prefix(&cfg->prefix6,
258 "Bad prefix length: %s", p);
259 flags |= NAT64STL_HAS_PREFIX6;
263 cfg->flags |= NAT64_LOG;
266 cfg->flags &= ~NAT64_LOG;
269 cfg->flags |= NAT64_ALLOW_PRIVATE;
272 cfg->flags &= ~NAT64_ALLOW_PRIVATE;
277 /* Check validness */
278 if ((flags & NAT64STL_HAS_TABLE4) != NAT64STL_HAS_TABLE4)
279 errx(EX_USAGE, "table4 required");
280 if ((flags & NAT64STL_HAS_TABLE6) != NAT64STL_HAS_TABLE6)
281 errx(EX_USAGE, "table6 required");
282 if ((flags & NAT64STL_HAS_PREFIX6) != NAT64STL_HAS_PREFIX6)
283 errx(EX_USAGE, "prefix6 required");
286 olh->objsize = sizeof(*cfg);
287 olh->size = sizeof(buf);
288 strlcpy(cfg->name, name, sizeof(cfg->name));
289 if (do_set3(IP_FW_NAT64STL_CREATE, &olh->opheader, sizeof(buf)) != 0)
290 err(EX_OSERR, "nat64stl instance creation failed");
294 * Configures existing nat64stl instance
295 * ipfw nat64stl <NAME> config <options>
296 * Request: [ ipfw_obj_header ipfw_nat64stl_cfg ]
299 nat64stl_config(const char *name, uint8_t set, int ac, char **av)
301 char buf[sizeof(ipfw_obj_header) + sizeof(ipfw_nat64stl_cfg)];
302 ipfw_nat64stl_cfg *cfg;
309 errx(EX_USAGE, "config options required");
310 memset(&buf, 0, sizeof(buf));
311 oh = (ipfw_obj_header *)buf;
312 cfg = (ipfw_nat64stl_cfg *)(oh + 1);
315 nat64stl_fill_ntlv(&oh->ntlv, name, set);
316 if (do_get3(IP_FW_NAT64STL_CONFIG, &oh->opheader, &sz) != 0)
317 err(EX_OSERR, "failed to get config for instance %s", name);
320 tcmd = get_token(nat64newcmds, *av, "option");
327 NEED1("table name required");
328 table_fill_ntlv(&cfg->ntlv4, *av, set, 4);
332 NEED1("table name required");
333 table_fill_ntlv(&cfg->ntlv6, *av, set, 6);
338 cfg->flags |= NAT64_LOG;
341 cfg->flags &= ~NAT64_LOG;
344 cfg->flags |= NAT64_ALLOW_PRIVATE;
347 cfg->flags &= ~NAT64_ALLOW_PRIVATE;
350 errx(EX_USAGE, "Can't change %s option", opt);
354 if (do_set3(IP_FW_NAT64STL_CONFIG, &oh->opheader, sizeof(buf)) != 0)
355 err(EX_OSERR, "nat64stl instance configuration failed");
359 * Destroys nat64stl instance.
360 * Request: [ ipfw_obj_header ]
363 nat64stl_destroy(const char *name, uint8_t set)
367 memset(&oh, 0, sizeof(oh));
368 nat64stl_fill_ntlv(&oh.ntlv, name, set);
369 if (do_set3(IP_FW_NAT64STL_DESTROY, &oh.opheader, sizeof(oh)) != 0)
370 err(EX_OSERR, "failed to destroy nat instance %s", name);
374 * Get nat64stl instance statistics.
375 * Request: [ ipfw_obj_header ]
376 * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ] ]
379 nat64stl_get_stats(const char *name, uint8_t set,
380 struct ipfw_nat64stl_stats *stats)
386 sz = sizeof(*oh) + sizeof(*oc) + sizeof(*stats);
388 nat64stl_fill_ntlv(&oh->ntlv, name, set);
389 if (do_get3(IP_FW_NAT64STL_STATS, &oh->opheader, &sz) == 0) {
390 oc = (ipfw_obj_ctlv *)(oh + 1);
391 memcpy(stats, oc + 1, sizeof(*stats));
400 nat64stl_stats(const char *name, uint8_t set)
402 struct ipfw_nat64stl_stats stats;
404 if (nat64stl_get_stats(name, set, &stats) != 0)
405 err(EX_OSERR, "Error retrieving stats");
407 if (co.use_set != 0 || set != 0)
408 printf("set %u ", set);
409 printf("nat64stl %s\n", name);
411 printf("\t%ju packets translated from IPv6 to IPv4\n",
412 (uintmax_t)stats.opcnt64);
413 printf("\t%ju packets translated from IPv4 to IPv6\n",
414 (uintmax_t)stats.opcnt46);
415 printf("\t%ju IPv6 fragments created\n",
416 (uintmax_t)stats.ofrags);
417 printf("\t%ju IPv4 fragments received\n",
418 (uintmax_t)stats.ifrags);
419 printf("\t%ju output packets dropped due to no bufs, etc.\n",
420 (uintmax_t)stats.oerrors);
421 printf("\t%ju output packets discarded due to no IPv4 route\n",
422 (uintmax_t)stats.noroute4);
423 printf("\t%ju output packets discarded due to no IPv6 route\n",
424 (uintmax_t)stats.noroute6);
425 printf("\t%ju packets discarded due to unsupported protocol\n",
426 (uintmax_t)stats.noproto);
427 printf("\t%ju packets discarded due to memory allocation problems\n",
428 (uintmax_t)stats.nomem);
429 printf("\t%ju packets discarded due to some errors\n",
430 (uintmax_t)stats.dropped);
434 * Reset nat64stl instance statistics specified by @oh->ntlv.
435 * Request: [ ipfw_obj_header ]
438 nat64stl_reset_stats(const char *name, uint8_t set)
442 memset(&oh, 0, sizeof(oh));
443 nat64stl_fill_ntlv(&oh.ntlv, name, set);
444 if (do_set3(IP_FW_NAT64STL_RESET_STATS, &oh.opheader, sizeof(oh)) != 0)
445 err(EX_OSERR, "failed to reset stats for instance %s", name);
449 nat64stl_show_cb(ipfw_nat64stl_cfg *cfg, const char *name, uint8_t set)
451 char abuf[INET6_ADDRSTRLEN];
453 if (name != NULL && strcmp(cfg->name, name) != 0)
456 if (co.use_set != 0 && cfg->set != set)
459 if (co.use_set != 0 || cfg->set != 0)
460 printf("set %u ", cfg->set);
462 printf("nat64stl %s table4 %s table6 %s",
463 cfg->name, cfg->ntlv4.name, cfg->ntlv6.name);
464 inet_ntop(AF_INET6, &cfg->prefix6, abuf, sizeof(abuf));
465 printf(" prefix6 %s/%u", abuf, cfg->plen6);
466 if (cfg->flags & NAT64_LOG)
468 if (cfg->flags & NAT64_ALLOW_PRIVATE)
469 printf(" allow_private");
475 nat64stl_destroy_cb(ipfw_nat64stl_cfg *cfg, const char *name, uint8_t set)
478 if (co.use_set != 0 && cfg->set != set)
481 nat64stl_destroy(cfg->name, cfg->set);
487 * Compare nat64stl instances names.
488 * Honor number comparison.
491 nat64name_cmp(const void *a, const void *b)
493 ipfw_nat64stl_cfg *ca, *cb;
495 ca = (ipfw_nat64stl_cfg *)a;
496 cb = (ipfw_nat64stl_cfg *)b;
498 if (ca->set > cb->set)
500 else if (ca->set < cb->set)
502 return (stringnum_cmp(ca->name, cb->name));
506 * Retrieves nat64stl instance list from kernel,
507 * optionally sorts it and calls requested function for each instance.
509 * Request: [ ipfw_obj_lheader ]
510 * Reply: [ ipfw_obj_lheader ipfw_nat64stl_cfg x N ]
513 nat64stl_foreach(nat64stl_cb_t *f, const char *name, uint8_t set, int sort)
515 ipfw_obj_lheader *olh;
516 ipfw_nat64stl_cfg *cfg;
520 /* Start with reasonable default */
521 sz = sizeof(*olh) + 16 * sizeof(*cfg);
523 if ((olh = calloc(1, sz)) == NULL)
527 if (do_get3(IP_FW_NAT64STL_LIST, &olh->opheader, &sz) != 0) {
536 qsort(olh + 1, olh->count, olh->objsize,
539 cfg = (ipfw_nat64stl_cfg *)(olh + 1);
540 for (i = 0; i < olh->count; i++) {
541 error = f(cfg, name, set); /* Ignore errors for now */
542 cfg = (ipfw_nat64stl_cfg *)((caddr_t)cfg +