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