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