2 * Copyright (c) 2016 Yandex LLC
3 * Copyright (c) 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/param.h>
32 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <netinet/ip_fw.h>
48 #include <netinet6/ip_fw_nptv6.h>
49 #include <arpa/inet.h>
52 typedef int (nptv6_cb_t)(ipfw_nptv6_cfg *i, const char *name, uint8_t set);
53 static int nptv6_foreach(nptv6_cb_t *f, const char *name, uint8_t set,
56 static void nptv6_create(const char *name, uint8_t set, int ac, char **av);
57 static void nptv6_destroy(const char *name, uint8_t set);
58 static void nptv6_stats(const char *name, uint8_t set);
59 static void nptv6_reset_stats(const char *name, uint8_t set);
60 static int nptv6_show_cb(ipfw_nptv6_cfg *cfg, const char *name, uint8_t set);
61 static int nptv6_destroy_cb(ipfw_nptv6_cfg *cfg, const char *name, uint8_t set);
63 static struct _s_x nptv6cmds[] = {
64 { "create", TOK_CREATE },
65 { "destroy", TOK_DESTROY },
68 { "stats", TOK_STATS },
72 static struct _s_x nptv6statscmds[] = {
73 { "reset", TOK_RESET },
78 * This one handles all NPTv6-related commands
79 * ipfw [set N] nptv6 NAME {create | config} ...
80 * ipfw [set N] nptv6 NAME stats [reset]
81 * ipfw [set N] nptv6 {NAME | all} destroy
82 * ipfw [set N] nptv6 {NAME | all} {list | show}
84 #define nptv6_check_name table_check_name
86 ipfw_nptv6_handler(int ac, char *av[])
98 NEED1("nptv6 needs instance name");
100 if (nptv6_check_name(name) != 0) {
101 if (strcmp(name, "all") == 0) {
104 errx(EX_USAGE, "nptv6 instance name %s is invalid",
108 NEED1("nptv6 needs command");
110 tcmd = get_token(nptv6cmds, *av, "nptv6 command");
111 if (name == NULL && tcmd != TOK_DESTROY && tcmd != TOK_LIST)
112 errx(EX_USAGE, "nptv6 instance name required");
116 nptv6_create(name, set, ac, av);
119 nptv6_foreach(nptv6_show_cb, name, set, 1);
123 nptv6_foreach(nptv6_destroy_cb, NULL, set, 0);
125 nptv6_destroy(name, set);
130 nptv6_stats(name, set);
133 tcmd = get_token(nptv6statscmds, *av, "stats command");
134 if (tcmd == TOK_RESET)
135 nptv6_reset_stats(name, set);
141 nptv6_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set)
144 ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
145 ntlv->head.length = sizeof(ipfw_obj_ntlv);
148 strlcpy(ntlv->name, name, sizeof(ntlv->name));
151 static struct _s_x nptv6newcmds[] = {
152 { "int_prefix", TOK_INTPREFIX },
153 { "ext_prefix", TOK_EXTPREFIX },
154 { "prefixlen", TOK_PREFIXLEN },
155 { "ext_if", TOK_EXTIF },
161 nptv6_parse_prefix(const char *arg, struct in6_addr *prefix, int *len)
168 if ((l = strchr(p, '/')) != NULL)
170 if (inet_pton(AF_INET6, p, prefix) != 1)
171 errx(EX_USAGE, "Bad prefix: %s", p);
173 *len = (int)strtol(l, &l, 10);
174 if (*l != '\0' || *len <= 0 || *len > 64)
175 errx(EX_USAGE, "Bad prefix length: %s", arg);
181 * Creates new nptv6 instance
182 * ipfw nptv6 <NAME> create int_prefix <prefix> ext_prefix <prefix>
183 * Request: [ ipfw_obj_lheader ipfw_nptv6_cfg ]
185 #define NPTV6_HAS_INTPREFIX 0x01
186 #define NPTV6_HAS_EXTPREFIX 0x02
187 #define NPTV6_HAS_PREFIXLEN 0x04
189 nptv6_create(const char *name, uint8_t set, int ac, char *av[])
191 char buf[sizeof(ipfw_obj_lheader) + sizeof(ipfw_nptv6_cfg)];
192 struct in6_addr mask;
194 ipfw_obj_lheader *olh;
195 int tcmd, flags, plen;
199 memset(buf, 0, sizeof(buf));
200 olh = (ipfw_obj_lheader *)buf;
201 cfg = (ipfw_nptv6_cfg *)(olh + 1);
205 tcmd = get_token(nptv6newcmds, *av, "option");
210 NEED1("IPv6 prefix required");
211 nptv6_parse_prefix(*av, &cfg->internal, &plen);
212 flags |= NPTV6_HAS_INTPREFIX;
218 if (flags & NPTV6_HAS_EXTPREFIX)
220 "Only one ext_prefix or ext_if allowed");
221 NEED1("IPv6 prefix required");
222 nptv6_parse_prefix(*av, &cfg->external, &plen);
223 flags |= NPTV6_HAS_EXTPREFIX;
229 if (flags & NPTV6_HAS_EXTPREFIX)
231 "Only one ext_prefix or ext_if allowed");
232 NEED1("Interface name required");
233 if (strlen(*av) >= sizeof(cfg->if_name))
234 errx(EX_USAGE, "Invalid interface name");
235 flags |= NPTV6_HAS_EXTPREFIX;
236 cfg->flags |= NPTV6_DYNAMIC_PREFIX;
237 strncpy(cfg->if_name, *av, sizeof(cfg->if_name));
241 NEED1("IPv6 prefix length required");
242 plen = strtol(*av, &p, 10);
244 if (*p != '\0' || plen < 8 || plen > 64)
245 errx(EX_USAGE, "wrong prefix length: %s", *av);
246 /* RFC 6296 Sec. 3.1 */
247 if (cfg->plen > 0 && cfg->plen != plen) {
248 warnx("Prefix length mismatch (%d vs %d). "
249 "It was extended up to %d",
250 cfg->plen, plen, MAX(plen, cfg->plen));
251 plen = MAX(plen, cfg->plen);
254 flags |= NPTV6_HAS_PREFIXLEN;
260 /* Check validness */
261 if ((flags & NPTV6_HAS_INTPREFIX) != NPTV6_HAS_INTPREFIX)
262 errx(EX_USAGE, "int_prefix required");
263 if ((flags & NPTV6_HAS_EXTPREFIX) != NPTV6_HAS_EXTPREFIX)
264 errx(EX_USAGE, "ext_prefix or ext_if required");
265 if ((flags & NPTV6_HAS_PREFIXLEN) != NPTV6_HAS_PREFIXLEN)
266 errx(EX_USAGE, "prefixlen required");
268 n2mask(&mask, cfg->plen);
269 APPLY_MASK(&cfg->internal, &mask);
270 if ((cfg->flags & NPTV6_DYNAMIC_PREFIX) == 0)
271 APPLY_MASK(&cfg->external, &mask);
274 olh->objsize = sizeof(*cfg);
275 olh->size = sizeof(buf);
276 strlcpy(cfg->name, name, sizeof(cfg->name));
277 if (do_set3(IP_FW_NPTV6_CREATE, &olh->opheader, sizeof(buf)) != 0)
278 err(EX_OSERR, "nptv6 instance creation failed");
282 * Destroys NPTv6 instance.
283 * Request: [ ipfw_obj_header ]
286 nptv6_destroy(const char *name, uint8_t set)
290 memset(&oh, 0, sizeof(oh));
291 nptv6_fill_ntlv(&oh.ntlv, name, set);
292 if (do_set3(IP_FW_NPTV6_DESTROY, &oh.opheader, sizeof(oh)) != 0)
293 err(EX_OSERR, "failed to destroy nat instance %s", name);
297 * Get NPTv6 instance statistics.
298 * Request: [ ipfw_obj_header ]
299 * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ] ]
302 nptv6_get_stats(const char *name, uint8_t set, struct ipfw_nptv6_stats *stats)
308 sz = sizeof(*oh) + sizeof(*oc) + sizeof(*stats);
310 nptv6_fill_ntlv(&oh->ntlv, name, set);
311 if (do_get3(IP_FW_NPTV6_STATS, &oh->opheader, &sz) == 0) {
312 oc = (ipfw_obj_ctlv *)(oh + 1);
313 memcpy(stats, oc + 1, sizeof(*stats));
322 nptv6_stats(const char *name, uint8_t set)
324 struct ipfw_nptv6_stats stats;
326 if (nptv6_get_stats(name, set, &stats) != 0)
327 err(EX_OSERR, "Error retrieving stats");
329 if (co.use_set != 0 || set != 0)
330 printf("set %u ", set);
331 printf("nptv6 %s\n", name);
332 printf("\t%ju packets translated (internal to external)\n",
333 (uintmax_t)stats.in2ex);
334 printf("\t%ju packets translated (external to internal)\n",
335 (uintmax_t)stats.ex2in);
336 printf("\t%ju packets dropped due to some error\n",
337 (uintmax_t)stats.dropped);
341 * Reset NPTv6 instance statistics specified by @oh->ntlv.
342 * Request: [ ipfw_obj_header ]
345 nptv6_reset_stats(const char *name, uint8_t set)
349 memset(&oh, 0, sizeof(oh));
350 nptv6_fill_ntlv(&oh.ntlv, name, set);
351 if (do_set3(IP_FW_NPTV6_RESET_STATS, &oh.opheader, sizeof(oh)) != 0)
352 err(EX_OSERR, "failed to reset stats for instance %s", name);
356 nptv6_show_cb(ipfw_nptv6_cfg *cfg, const char *name, uint8_t set)
358 char abuf[INET6_ADDRSTRLEN];
360 if (name != NULL && strcmp(cfg->name, name) != 0)
363 if (co.use_set != 0 && cfg->set != set)
366 if (co.use_set != 0 || cfg->set != 0)
367 printf("set %u ", cfg->set);
368 inet_ntop(AF_INET6, &cfg->internal, abuf, sizeof(abuf));
369 printf("nptv6 %s int_prefix %s ", cfg->name, abuf);
370 if (cfg->flags & NPTV6_DYNAMIC_PREFIX)
371 printf("ext_if %s ", cfg->if_name);
373 inet_ntop(AF_INET6, &cfg->external, abuf, sizeof(abuf));
374 printf("ext_prefix %s ", abuf);
376 printf("prefixlen %u\n", cfg->plen);
381 nptv6_destroy_cb(ipfw_nptv6_cfg *cfg, const char *name, uint8_t set)
384 if (co.use_set != 0 && cfg->set != set)
387 nptv6_destroy(cfg->name, cfg->set);
393 * Compare NPTv6 instances names.
394 * Honor number comparison.
397 nptv6name_cmp(const void *a, const void *b)
399 ipfw_nptv6_cfg *ca, *cb;
401 ca = (ipfw_nptv6_cfg *)a;
402 cb = (ipfw_nptv6_cfg *)b;
404 if (ca->set > cb->set)
406 else if (ca->set < cb->set)
408 return (stringnum_cmp(ca->name, cb->name));
412 * Retrieves NPTv6 instance list from kernel,
413 * Request: [ ipfw_obj_lheader ]
414 * Reply: [ ipfw_obj_lheader ipfw_nptv6_cfg x N ]
417 nptv6_foreach(nptv6_cb_t *f, const char *name, uint8_t set, int sort)
419 ipfw_obj_lheader *olh;
424 /* Start with reasonable default */
425 sz = sizeof(*olh) + 16 * sizeof(*cfg);
427 if ((olh = calloc(1, sz)) == NULL)
431 if (do_get3(IP_FW_NPTV6_LIST, &olh->opheader, &sz) != 0) {
440 qsort(olh + 1, olh->count, olh->objsize, nptv6name_cmp);
442 cfg = (ipfw_nptv6_cfg *)(olh + 1);
443 for (i = 0; i < olh->count; i++) {
444 error = f(cfg, name, set);
445 cfg = (ipfw_nptv6_cfg *)((caddr_t)cfg + olh->objsize);