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