xref: /freebsd/sbin/ipfw/nat64lsn.c (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 2015-2016 Yandex LLC
3  * Copyright (c) 2015-2016 Alexander V. Chernikov <melifaro@FreeBSD.org>
4  * Copyright (c) 2015-2016 Andrey V. Elsukov <ae@FreeBSD.org>
5  * All rights reserved.
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 static void nat64lsn_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name,
54     uint8_t set);
55 typedef int (nat64lsn_cb_t)(ipfw_nat64lsn_cfg *cfg, const char *name,
56     uint8_t set);
57 static int nat64lsn_foreach(nat64lsn_cb_t *f, const char *name, uint8_t set,
58     int sort);
59 
60 static void nat64lsn_create(const char *name, uint8_t set, int ac, char **av);
61 static void nat64lsn_config(const char *name, uint8_t set, int ac, char **av);
62 static void nat64lsn_destroy(const char *name, uint8_t set);
63 static void nat64lsn_stats(const char *name, uint8_t set);
64 static void nat64lsn_reset_stats(const char *name, uint8_t set);
65 static int nat64lsn_show_cb(ipfw_nat64lsn_cfg *cfg, const char *name,
66     uint8_t set);
67 static int nat64lsn_destroy_cb(ipfw_nat64lsn_cfg *cfg, const char *name,
68     uint8_t set);
69 static int nat64lsn_states_cb(ipfw_nat64lsn_cfg *cfg, const char *name,
70     uint8_t set);
71 
72 static struct _s_x nat64cmds[] = {
73       { "create",	TOK_CREATE },
74       { "config",	TOK_CONFIG },
75       { "destroy",	TOK_DESTROY },
76       { "list",		TOK_LIST },
77       { "show",		TOK_LIST },
78       { "stats",	TOK_STATS },
79       { NULL, 0 }
80 };
81 
82 static uint64_t
83 nat64lsn_print_states(void *buf)
84 {
85 	char s[INET6_ADDRSTRLEN], a[INET_ADDRSTRLEN], f[INET_ADDRSTRLEN];
86 	char sflags[4], *sf, *proto;
87 	ipfw_obj_header *oh;
88 	ipfw_obj_data *od;
89 	ipfw_nat64lsn_stg *stg;
90 	ipfw_nat64lsn_state *ste;
91 	uint64_t next_idx;
92 	int i, sz;
93 
94 	oh = (ipfw_obj_header *)buf;
95 	od = (ipfw_obj_data *)(oh + 1);
96 	stg = (ipfw_nat64lsn_stg *)(od + 1);
97 	sz = od->head.length - sizeof(*od);
98 	next_idx = 0;
99 	while (sz > 0 && next_idx != 0xFF) {
100 		next_idx = stg->next_idx;
101 		sz -= sizeof(*stg);
102 		if (stg->count == 0) {
103 			stg++;
104 			continue;
105 		}
106 		switch (stg->proto) {
107 		case IPPROTO_TCP:
108 			proto = "TCP";
109 			break;
110 		case IPPROTO_UDP:
111 			proto = "UDP";
112 			break;
113 		case IPPROTO_ICMPV6:
114 			proto = "ICMPv6";
115 			break;
116 		}
117 		inet_ntop(AF_INET6, &stg->host6, s, sizeof(s));
118 		inet_ntop(AF_INET, &stg->alias4, a, sizeof(a));
119 		ste = (ipfw_nat64lsn_state *)(stg + 1);
120 		for (i = 0; i < stg->count && sz > 0; i++) {
121 			sf = sflags;
122 			inet_ntop(AF_INET, &ste->daddr, f, sizeof(f));
123 			if (stg->proto == IPPROTO_TCP) {
124 				if (ste->flags & 0x02)
125 					*sf++ = 'S';
126 				if (ste->flags & 0x04)
127 					*sf++ = 'E';
128 				if (ste->flags & 0x01)
129 					*sf++ = 'F';
130 			}
131 			*sf = '\0';
132 			switch (stg->proto) {
133 			case IPPROTO_TCP:
134 			case IPPROTO_UDP:
135 				printf("%s:%d\t%s:%d\t%s\t%s\t%d\t%s:%d\n",
136 				    s, ste->sport, a, ste->aport, proto,
137 				    sflags, ste->idle, f, ste->dport);
138 				break;
139 			case IPPROTO_ICMPV6:
140 				printf("%s\t%s\t%s\t\t%d\t%s\n",
141 				    s, a, proto, ste->idle, f);
142 				break;
143 			default:
144 				printf("%s\t%s\t%d\t\t%d\t%s\n",
145 				    s, a, stg->proto, ste->idle, f);
146 			}
147 			ste++;
148 			sz -= sizeof(*ste);
149 		}
150 		stg = (ipfw_nat64lsn_stg *)ste;
151 	}
152 	return (next_idx);
153 }
154 
155 static int
156 nat64lsn_states_cb(ipfw_nat64lsn_cfg *cfg, const char *name, uint8_t set)
157 {
158 	ipfw_obj_header *oh;
159 	ipfw_obj_data *od;
160 	void *buf;
161 	uint64_t next_idx;
162 	size_t sz;
163 
164 	if (name != NULL && strcmp(cfg->name, name) != 0)
165 		return (ESRCH);
166 
167 	if (set != 0 && cfg->set != set)
168 		return (ESRCH);
169 
170 	next_idx = 0;
171 	sz = 4096;
172 	if ((buf = calloc(1, sz)) == NULL)
173 		err(EX_OSERR, NULL);
174 	do {
175 		oh = (ipfw_obj_header *)buf;
176 		od = (ipfw_obj_data *)(oh + 1);
177 		nat64lsn_fill_ntlv(&oh->ntlv, cfg->name, set);
178 		od->head.type = IPFW_TLV_OBJDATA;
179 		od->head.length = sizeof(*od) + sizeof(next_idx);
180 		*((uint64_t *)(od + 1)) = next_idx;
181 		if (do_get3(IP_FW_NAT64LSN_LIST_STATES, &oh->opheader, &sz))
182 			err(EX_OSERR, "Error reading nat64lsn states");
183 		next_idx = nat64lsn_print_states(buf);
184 		sz = 4096;
185 		memset(buf, 0, sz);
186 	} while (next_idx != 0xFF);
187 
188 	free(buf);
189 	return (0);
190 }
191 
192 static struct _s_x nat64statscmds[] = {
193       { "reset",	TOK_RESET },
194       { NULL, 0 }
195 };
196 
197 static void
198 ipfw_nat64lsn_stats_handler(const char *name, uint8_t set, int ac, char *av[])
199 {
200 	int tcmd;
201 
202 	if (ac == 0) {
203 		nat64lsn_stats(name, set);
204 		return;
205 	}
206 	NEED1("nat64lsn stats needs command");
207 	tcmd = get_token(nat64statscmds, *av, "nat64lsn stats command");
208 	switch (tcmd) {
209 	case TOK_RESET:
210 		nat64lsn_reset_stats(name, set);
211 	}
212 }
213 
214 static struct _s_x nat64listcmds[] = {
215       { "states",	TOK_STATES },
216       { "config",	TOK_CONFIG },
217       { NULL, 0 }
218 };
219 
220 static void
221 ipfw_nat64lsn_list_handler(const char *name, uint8_t set, int ac, char *av[])
222 {
223 	int tcmd;
224 
225 	if (ac == 0) {
226 		nat64lsn_foreach(nat64lsn_show_cb, name, set, 1);
227 		return;
228 	}
229 	NEED1("nat64lsn list needs command");
230 	tcmd = get_token(nat64listcmds, *av, "nat64lsn list command");
231 	switch (tcmd) {
232 	case TOK_STATES:
233 		nat64lsn_foreach(nat64lsn_states_cb, name, set, 1);
234 		break;
235 	case TOK_CONFIG:
236 		nat64lsn_foreach(nat64lsn_show_cb, name, set, 1);
237 	}
238 }
239 
240 /*
241  * This one handles all nat64lsn-related commands
242  *	ipfw [set N] nat64lsn NAME {create | config} ...
243  *	ipfw [set N] nat64lsn NAME stats
244  *	ipfw [set N] nat64lsn {NAME | all} destroy
245  *	ipfw [set N] nat64lsn {NAME | all} {list | show} [config | states]
246  */
247 #define	nat64lsn_check_name	table_check_name
248 void
249 ipfw_nat64lsn_handler(int ac, char *av[])
250 {
251 	const char *name;
252 	int tcmd;
253 	uint8_t set;
254 
255 	if (co.use_set != 0)
256 		set = co.use_set - 1;
257 	else
258 		set = 0;
259 	ac--; av++;
260 
261 	NEED1("nat64lsn needs instance name");
262 	name = *av;
263 	if (nat64lsn_check_name(name) != 0) {
264 		if (strcmp(name, "all") == 0)
265 			name = NULL;
266 		else
267 			errx(EX_USAGE, "nat64lsn instance name %s is invalid",
268 			    name);
269 	}
270 	ac--; av++;
271 	NEED1("nat64lsn needs command");
272 
273 	tcmd = get_token(nat64cmds, *av, "nat64lsn command");
274 	if (name == NULL && tcmd != TOK_DESTROY && tcmd != TOK_LIST)
275 		errx(EX_USAGE, "nat64lsn instance name required");
276 	switch (tcmd) {
277 	case TOK_CREATE:
278 		ac--; av++;
279 		nat64lsn_create(name, set, ac, av);
280 		break;
281 	case TOK_CONFIG:
282 		ac--; av++;
283 		nat64lsn_config(name, set, ac, av);
284 		break;
285 	case TOK_LIST:
286 		ac--; av++;
287 		ipfw_nat64lsn_list_handler(name, set, ac, av);
288 		break;
289 	case TOK_DESTROY:
290 		if (name == NULL)
291 			nat64lsn_foreach(nat64lsn_destroy_cb, NULL, set, 0);
292 		else
293 			nat64lsn_destroy(name, set);
294 		break;
295 	case TOK_STATS:
296 		ac--; av++;
297 		ipfw_nat64lsn_stats_handler(name, set, ac, av);
298 	}
299 }
300 
301 static void
302 nat64lsn_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set)
303 {
304 
305 	ntlv->head.type = IPFW_TLV_EACTION_NAME(1); /* it doesn't matter */
306 	ntlv->head.length = sizeof(ipfw_obj_ntlv);
307 	ntlv->idx = 1;
308 	ntlv->set = set;
309 	strlcpy(ntlv->name, name, sizeof(ntlv->name));
310 }
311 
312 static void
313 nat64lsn_apply_mask(int af, void *prefix, uint16_t plen)
314 {
315 	struct in6_addr mask6, *p6;
316 	struct in_addr mask4, *p4;
317 
318 	if (af == AF_INET) {
319 		p4 = (struct in_addr *)prefix;
320 		mask4.s_addr = htonl(~((1 << (32 - plen)) - 1));
321 		p4->s_addr &= mask4.s_addr;
322 	} else if (af == AF_INET6) {
323 		p6 = (struct in6_addr *)prefix;
324 		n2mask(&mask6, plen);
325 		APPLY_MASK(p6, &mask6);
326 	}
327 }
328 
329 static void
330 nat64lsn_parse_prefix(const char *arg, int af, void *prefix, uint16_t *plen)
331 {
332 	char *p, *l;
333 
334 	p = strdup(arg);
335 	if (p == NULL)
336 		err(EX_OSERR, NULL);
337 	if ((l = strchr(p, '/')) != NULL)
338 		*l++ = '\0';
339 	if (l == NULL)
340 		errx(EX_USAGE, "Prefix length required");
341 	if (inet_pton(af, p, prefix) != 1)
342 		errx(EX_USAGE, "Bad prefix: %s", p);
343 	*plen = (uint16_t)strtol(l, &l, 10);
344 	if (*l != '\0' || *plen == 0 || (af == AF_INET && *plen > 32) ||
345 	    (af == AF_INET6 && *plen > 96))
346 		errx(EX_USAGE, "Bad prefix length: %s", arg);
347 	nat64lsn_apply_mask(af, prefix, *plen);
348 	free(p);
349 }
350 
351 static uint32_t
352 nat64lsn_parse_int(const char *arg, const char *desc)
353 {
354 	char *p;
355 	uint32_t val;
356 
357 	val = (uint32_t)strtol(arg, &p, 10);
358 	if (*p != '\0')
359 		errx(EX_USAGE, "Invalid %s value: %s\n", desc, arg);
360 	return (val);
361 }
362 
363 static struct _s_x nat64newcmds[] = {
364       { "prefix6",	TOK_PREFIX6 },
365       { "agg_len",	TOK_AGG_LEN }, /* not yet */
366       { "agg_count",	TOK_AGG_COUNT }, /* not yet */
367       { "port_range",	TOK_PORT_RANGE }, /* not yet */
368       { "jmaxlen",	TOK_JMAXLEN },
369       { "prefix4",	TOK_PREFIX4 },
370       { "max_ports",	TOK_MAX_PORTS },
371       { "host_del_age",	TOK_HOST_DEL_AGE },
372       { "pg_del_age",	TOK_PG_DEL_AGE },
373       { "tcp_syn_age",	TOK_TCP_SYN_AGE },
374       { "tcp_close_age",TOK_TCP_CLOSE_AGE },
375       { "tcp_est_age",	TOK_TCP_EST_AGE },
376       { "udp_age",	TOK_UDP_AGE },
377       { "icmp_age",	TOK_ICMP_AGE },
378       { "log",		TOK_LOG },
379       { "-log",		TOK_LOGOFF },
380       { NULL, 0 }
381 };
382 
383 /*
384  * Creates new nat64lsn instance
385  * ipfw nat64lsn <NAME> create
386  *     [ max_ports <N> ]
387  * Request: [ ipfw_obj_lheader ipfw_nat64lsn_cfg ]
388  */
389 #define	NAT64LSN_HAS_PREFIX4	0x01
390 #define	NAT64LSN_HAS_PREFIX6	0x02
391 static void
392 nat64lsn_create(const char *name, uint8_t set, int ac, char **av)
393 {
394 	char buf[sizeof(ipfw_obj_lheader) + sizeof(ipfw_nat64lsn_cfg)];
395 	ipfw_nat64lsn_cfg *cfg;
396 	ipfw_obj_lheader *olh;
397 	int tcmd, flags;
398 	char *opt;
399 
400 	memset(&buf, 0, sizeof(buf));
401 	olh = (ipfw_obj_lheader *)buf;
402 	cfg = (ipfw_nat64lsn_cfg *)(olh + 1);
403 
404 	/* Some reasonable defaults */
405 	inet_pton(AF_INET6, "64:ff9b::", &cfg->prefix6);
406 	cfg->plen6 = 96;
407 	cfg->set = set;
408 	cfg->max_ports = NAT64LSN_MAX_PORTS;
409 	cfg->jmaxlen = NAT64LSN_JMAXLEN;
410 	cfg->nh_delete_delay = NAT64LSN_HOST_AGE;
411 	cfg->pg_delete_delay = NAT64LSN_PG_AGE;
412 	cfg->st_syn_ttl = NAT64LSN_TCP_SYN_AGE;
413 	cfg->st_estab_ttl = NAT64LSN_TCP_EST_AGE;
414 	cfg->st_close_ttl = NAT64LSN_TCP_FIN_AGE;
415 	cfg->st_udp_ttl = NAT64LSN_UDP_AGE;
416 	cfg->st_icmp_ttl = NAT64LSN_ICMP_AGE;
417 	flags = NAT64LSN_HAS_PREFIX6;
418 	while (ac > 0) {
419 		tcmd = get_token(nat64newcmds, *av, "option");
420 		opt = *av;
421 		ac--; av++;
422 
423 		switch (tcmd) {
424 		case TOK_PREFIX4:
425 			NEED1("IPv4 prefix required");
426 			nat64lsn_parse_prefix(*av, AF_INET, &cfg->prefix4,
427 			    &cfg->plen4);
428 			flags |= NAT64LSN_HAS_PREFIX4;
429 			ac--; av++;
430 			break;
431 #if 0
432 		case TOK_PREFIX6:
433 			NEED1("IPv6 prefix required");
434 			nat64lsn_parse_prefix(*av, AF_INET6, &cfg->prefix6,
435 			    &cfg->plen6);
436 			ac--; av++;
437 			break;
438 		case TOK_AGG_LEN:
439 			NEED1("Aggregation prefix len required");
440 			cfg->agg_prefix_len = nat64lsn_parse_int(*av, opt);
441 			ac--; av++;
442 			break;
443 		case TOK_AGG_COUNT:
444 			NEED1("Max per-prefix count required");
445 			cfg->agg_prefix_max = nat64lsn_parse_int(*av, opt);
446 			ac--; av++;
447 			break;
448 		case TOK_PORT_RANGE:
449 			NEED1("port range x[:y] required");
450 			if ((p = strchr(*av, ':')) == NULL)
451 				cfg->min_port = (uint16_t)nat64lsn_parse_int(
452 				    *av, opt);
453 			else {
454 				*p++ = '\0';
455 				cfg->min_port = (uint16_t)nat64lsn_parse_int(
456 				    *av, opt);
457 				cfg->max_port = (uint16_t)nat64lsn_parse_int(
458 				    p, opt);
459 			}
460 			ac--; av++;
461 			break;
462 		case TOK_JMAXLEN:
463 			NEED1("job queue length required");
464 			cfg->jmaxlen = nat64lsn_parse_int(*av, opt);
465 			ac--; av++;
466 			break;
467 #endif
468 		case TOK_MAX_PORTS:
469 			NEED1("Max per-user ports required");
470 			cfg->max_ports = nat64lsn_parse_int(*av, opt);
471 			ac--; av++;
472 			break;
473 		case TOK_HOST_DEL_AGE:
474 			NEED1("host delete delay required");
475 			cfg->nh_delete_delay = (uint16_t)nat64lsn_parse_int(
476 			    *av, opt);
477 			ac--; av++;
478 			break;
479 		case TOK_PG_DEL_AGE:
480 			NEED1("portgroup delete delay required");
481 			cfg->pg_delete_delay = (uint16_t)nat64lsn_parse_int(
482 			    *av, opt);
483 			ac--; av++;
484 			break;
485 		case TOK_TCP_SYN_AGE:
486 			NEED1("tcp syn age required");
487 			cfg->st_syn_ttl = (uint16_t)nat64lsn_parse_int(
488 			    *av, opt);
489 			ac--; av++;
490 			break;
491 		case TOK_TCP_CLOSE_AGE:
492 			NEED1("tcp close age required");
493 			cfg->st_close_ttl = (uint16_t)nat64lsn_parse_int(
494 			    *av, opt);
495 			ac--; av++;
496 			break;
497 		case TOK_TCP_EST_AGE:
498 			NEED1("tcp est age required");
499 			cfg->st_estab_ttl = (uint16_t)nat64lsn_parse_int(
500 			    *av, opt);
501 			ac--; av++;
502 			break;
503 		case TOK_UDP_AGE:
504 			NEED1("udp age required");
505 			cfg->st_udp_ttl = (uint16_t)nat64lsn_parse_int(
506 			    *av, opt);
507 			ac--; av++;
508 			break;
509 		case TOK_ICMP_AGE:
510 			NEED1("icmp age required");
511 			cfg->st_icmp_ttl = (uint16_t)nat64lsn_parse_int(
512 			    *av, opt);
513 			ac--; av++;
514 			break;
515 		case TOK_LOG:
516 			cfg->flags |= NAT64_LOG;
517 			break;
518 		case TOK_LOGOFF:
519 			cfg->flags &= ~NAT64_LOG;
520 			break;
521 		}
522 	}
523 
524 	/* Check validness */
525 	if ((flags & NAT64LSN_HAS_PREFIX4) != NAT64LSN_HAS_PREFIX4)
526 		errx(EX_USAGE, "prefix4 required");
527 
528 	olh->count = 1;
529 	olh->objsize = sizeof(*cfg);
530 	olh->size = sizeof(buf);
531 	strlcpy(cfg->name, name, sizeof(cfg->name));
532 	if (do_set3(IP_FW_NAT64LSN_CREATE, &olh->opheader, sizeof(buf)) != 0)
533 		err(EX_OSERR, "nat64lsn instance creation failed");
534 }
535 
536 /*
537  * Configures existing nat64lsn instance
538  * ipfw nat64lsn <NAME> config <options>
539  * Request: [ ipfw_obj_header ipfw_nat64lsn_cfg ]
540  */
541 static void
542 nat64lsn_config(const char *name, uint8_t set, int ac, char **av)
543 {
544 	char buf[sizeof(ipfw_obj_header) + sizeof(ipfw_nat64lsn_cfg)];
545 	ipfw_nat64lsn_cfg *cfg;
546 	ipfw_obj_header *oh;
547 	size_t sz;
548 	char *opt;
549 	int tcmd;
550 
551 	if (ac == 0)
552 		errx(EX_USAGE, "config options required");
553 	memset(&buf, 0, sizeof(buf));
554 	oh = (ipfw_obj_header *)buf;
555 	cfg = (ipfw_nat64lsn_cfg *)(oh + 1);
556 	sz = sizeof(buf);
557 
558 	nat64lsn_fill_ntlv(&oh->ntlv, name, set);
559 	if (do_get3(IP_FW_NAT64LSN_CONFIG, &oh->opheader, &sz) != 0)
560 		err(EX_OSERR, "failed to get config for instance %s", name);
561 
562 	while (ac > 0) {
563 		tcmd = get_token(nat64newcmds, *av, "option");
564 		opt = *av;
565 		ac--; av++;
566 
567 		switch (tcmd) {
568 		case TOK_MAX_PORTS:
569 			NEED1("Max per-user ports required");
570 			cfg->max_ports = nat64lsn_parse_int(*av, opt);
571 			ac--; av++;
572 			break;
573 		case TOK_JMAXLEN:
574 			NEED1("job queue length required");
575 			cfg->jmaxlen = nat64lsn_parse_int(*av, opt);
576 			ac--; av++;
577 			break;
578 		case TOK_HOST_DEL_AGE:
579 			NEED1("host delete delay required");
580 			cfg->nh_delete_delay = (uint16_t)nat64lsn_parse_int(
581 			    *av, opt);
582 			ac--; av++;
583 			break;
584 		case TOK_PG_DEL_AGE:
585 			NEED1("portgroup delete delay required");
586 			cfg->pg_delete_delay = (uint16_t)nat64lsn_parse_int(
587 			    *av, opt);
588 			ac--; av++;
589 			break;
590 		case TOK_TCP_SYN_AGE:
591 			NEED1("tcp syn age required");
592 			cfg->st_syn_ttl = (uint16_t)nat64lsn_parse_int(
593 			    *av, opt);
594 			ac--; av++;
595 			break;
596 		case TOK_TCP_CLOSE_AGE:
597 			NEED1("tcp close age required");
598 			cfg->st_close_ttl = (uint16_t)nat64lsn_parse_int(
599 			    *av, opt);
600 			ac--; av++;
601 			break;
602 		case TOK_TCP_EST_AGE:
603 			NEED1("tcp est age required");
604 			cfg->st_estab_ttl = (uint16_t)nat64lsn_parse_int(
605 			    *av, opt);
606 			ac--; av++;
607 			break;
608 		case TOK_UDP_AGE:
609 			NEED1("udp age required");
610 			cfg->st_udp_ttl = (uint16_t)nat64lsn_parse_int(
611 			    *av, opt);
612 			ac--; av++;
613 			break;
614 		case TOK_ICMP_AGE:
615 			NEED1("icmp age required");
616 			cfg->st_icmp_ttl = (uint16_t)nat64lsn_parse_int(
617 			    *av, opt);
618 			ac--; av++;
619 			break;
620 		case TOK_LOG:
621 			cfg->flags |= NAT64_LOG;
622 			break;
623 		case TOK_LOGOFF:
624 			cfg->flags &= ~NAT64_LOG;
625 			break;
626 		default:
627 			errx(EX_USAGE, "Can't change %s option", opt);
628 		}
629 	}
630 
631 	if (do_set3(IP_FW_NAT64LSN_CONFIG, &oh->opheader, sizeof(buf)) != 0)
632 		err(EX_OSERR, "nat64lsn instance configuration failed");
633 }
634 
635 /*
636  * Reset nat64lsn instance statistics specified by @oh->ntlv.
637  * Request: [ ipfw_obj_header ]
638  */
639 static void
640 nat64lsn_reset_stats(const char *name, uint8_t set)
641 {
642 	ipfw_obj_header oh;
643 
644 	memset(&oh, 0, sizeof(oh));
645 	nat64lsn_fill_ntlv(&oh.ntlv, name, set);
646 	if (do_set3(IP_FW_NAT64LSN_RESET_STATS, &oh.opheader, sizeof(oh)) != 0)
647 		err(EX_OSERR, "failed to reset stats for instance %s", name);
648 }
649 
650 /*
651  * Destroys nat64lsn instance specified by @oh->ntlv.
652  * Request: [ ipfw_obj_header ]
653  */
654 static void
655 nat64lsn_destroy(const char *name, uint8_t set)
656 {
657 	ipfw_obj_header oh;
658 
659 	memset(&oh, 0, sizeof(oh));
660 	nat64lsn_fill_ntlv(&oh.ntlv, name, set);
661 	if (do_set3(IP_FW_NAT64LSN_DESTROY, &oh.opheader, sizeof(oh)) != 0)
662 		err(EX_OSERR, "failed to destroy nat instance %s", name);
663 }
664 
665 /*
666  * Get nat64lsn instance statistics.
667  * Request: [ ipfw_obj_header ]
668  * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ] ]
669  */
670 static int
671 nat64lsn_get_stats(const char *name, uint8_t set,
672     struct ipfw_nat64lsn_stats *stats)
673 {
674 	ipfw_obj_header *oh;
675 	ipfw_obj_ctlv *oc;
676 	size_t sz;
677 
678 	sz = sizeof(*oh) + sizeof(*oc) + sizeof(*stats);
679 	oh = calloc(1, sz);
680 	nat64lsn_fill_ntlv(&oh->ntlv, name, set);
681 	if (do_get3(IP_FW_NAT64LSN_STATS, &oh->opheader, &sz) == 0) {
682 		oc = (ipfw_obj_ctlv *)(oh + 1);
683 		memcpy(stats, oc + 1, sizeof(*stats));
684 		free(oh);
685 		return (0);
686 	}
687 	free(oh);
688 	return (-1);
689 }
690 
691 static void
692 nat64lsn_stats(const char *name, uint8_t set)
693 {
694 	struct ipfw_nat64lsn_stats stats;
695 
696 	if (nat64lsn_get_stats(name, set, &stats) != 0)
697 		err(EX_OSERR, "Error retrieving stats");
698 
699 	if (co.use_set != 0 || set != 0)
700 		printf("set %u ", set);
701 	printf("nat64lsn %s\n", name);
702 	printf("\t%ju packets translated from IPv6 to IPv4\n",
703 	    (uintmax_t)stats.opcnt64);
704 	printf("\t%ju packets translated from IPv4 to IPv6\n",
705 	    (uintmax_t)stats.opcnt46);
706 	printf("\t%ju IPv6 fragments created\n",
707 	    (uintmax_t)stats.ofrags);
708 	printf("\t%ju IPv4 fragments received\n",
709 	    (uintmax_t)stats.ifrags);
710 	printf("\t%ju output packets dropped due to no bufs, etc.\n",
711 	    (uintmax_t)stats.oerrors);
712 	printf("\t%ju output packets discarded due to no IPv4 route\n",
713 	    (uintmax_t)stats.noroute4);
714 	printf("\t%ju output packets discarded due to no IPv6 route\n",
715 	    (uintmax_t)stats.noroute6);
716 	printf("\t%ju packets discarded due to unsupported protocol\n",
717 	    (uintmax_t)stats.noproto);
718 	printf("\t%ju packets discarded due to memory allocation problems\n",
719 	    (uintmax_t)stats.nomem);
720 	printf("\t%ju packets discarded due to some errors\n",
721 	    (uintmax_t)stats.dropped);
722 	printf("\t%ju packets not matched with IPv4 prefix\n",
723 	    (uintmax_t)stats.nomatch4);
724 
725 	printf("\t%ju mbufs queued for post processing\n",
726 	    (uintmax_t)stats.jreinjected);
727 	printf("\t%ju times the job queue was processed\n",
728 	    (uintmax_t)stats.jcalls);
729 	printf("\t%ju job requests queued\n",
730 	    (uintmax_t)stats.jrequests);
731 	printf("\t%ju job requests queue limit reached\n",
732 	    (uintmax_t)stats.jmaxlen);
733 	printf("\t%ju job requests failed due to memory allocation problems\n",
734 	    (uintmax_t)stats.jnomem);
735 
736 	printf("\t%ju hosts allocated\n", (uintmax_t)stats.hostcount);
737 	printf("\t%ju hosts requested\n", (uintmax_t)stats.jhostsreq);
738 	printf("\t%ju host requests failed\n", (uintmax_t)stats.jhostfails);
739 
740 	printf("\t%ju portgroups requested\n", (uintmax_t)stats.jportreq);
741 	printf("\t%ju portgroups allocated\n", (uintmax_t)stats.spgcreated);
742 	printf("\t%ju portgroups deleted\n", (uintmax_t)stats.spgdeleted);
743 	printf("\t%ju portgroup requests failed\n",
744 	    (uintmax_t)stats.jportfails);
745 	printf("\t%ju portgroups allocated for TCP\n",
746 	    (uintmax_t)stats.tcpchunks);
747 	printf("\t%ju portgroups allocated for UDP\n",
748 	    (uintmax_t)stats.udpchunks);
749 	printf("\t%ju portgroups allocated for ICMP\n",
750 	    (uintmax_t)stats.icmpchunks);
751 
752 	printf("\t%ju states created\n", (uintmax_t)stats.screated);
753 	printf("\t%ju states deleted\n", (uintmax_t)stats.sdeleted);
754 }
755 
756 static int
757 nat64lsn_show_cb(ipfw_nat64lsn_cfg *cfg, const char *name, uint8_t set)
758 {
759 	char abuf[INET6_ADDRSTRLEN];
760 
761 	if (name != NULL && strcmp(cfg->name, name) != 0)
762 		return (ESRCH);
763 
764 	if (co.use_set != 0 && cfg->set != set)
765 		return (ESRCH);
766 
767 	if (co.use_set != 0 || cfg->set != 0)
768 		printf("set %u ", cfg->set);
769 	inet_ntop(AF_INET, &cfg->prefix4, abuf, sizeof(abuf));
770 	printf("nat64lsn %s prefix4 %s/%u ", cfg->name, abuf, cfg->plen4);
771 #if 0
772 	inet_ntop(AF_INET6, &cfg->prefix6, abuf, sizeof(abuf));
773 	printf("prefix6 %s/%u", abuf, cfg->plen6);
774 	printf("agg_len %u agg_count %u ", cfg->agg_prefix_len,
775 	    cfg->agg_prefix_max);
776 	if (cfg->min_port != NAT64LSN_PORT_MIN ||
777 	    cfg->max_port != NAT64LSN_PORT_MAX)
778 		printf(" port_range %u:%u", cfg->min_port, cfg->max_port);
779 	if (cfg->jmaxlen != NAT64LSN_JMAXLEN)
780 		printf(" jmaxlen %u ", cfg->jmaxlen);
781 #endif
782 	if (cfg->max_ports != NAT64LSN_MAX_PORTS)
783 		printf(" max_ports %u", cfg->max_ports);
784 	if (cfg->nh_delete_delay != NAT64LSN_HOST_AGE)
785 		printf(" host_del_age %u", cfg->nh_delete_delay);
786 	if (cfg->pg_delete_delay != NAT64LSN_PG_AGE)
787 		printf(" pg_del_age %u ", cfg->pg_delete_delay);
788 	if (cfg->st_syn_ttl != NAT64LSN_TCP_SYN_AGE)
789 		printf(" tcp_syn_age %u", cfg->st_syn_ttl);
790 	if (cfg->st_close_ttl != NAT64LSN_TCP_FIN_AGE)
791 		printf(" tcp_close_age %u", cfg->st_close_ttl);
792 	if (cfg->st_estab_ttl != NAT64LSN_TCP_EST_AGE)
793 		printf(" tcp_est_age %u", cfg->st_estab_ttl);
794 	if (cfg->st_udp_ttl != NAT64LSN_UDP_AGE)
795 		printf(" udp_age %u", cfg->st_udp_ttl);
796 	if (cfg->st_icmp_ttl != NAT64LSN_ICMP_AGE)
797 		printf(" icmp_age %u", cfg->st_icmp_ttl);
798 	if (cfg->flags & NAT64_LOG)
799 		printf(" log");
800 	printf("\n");
801 	return (0);
802 }
803 
804 static int
805 nat64lsn_destroy_cb(ipfw_nat64lsn_cfg *cfg, const char *name, uint8_t set)
806 {
807 
808 	if (co.use_set != 0 && cfg->set != set)
809 		return (ESRCH);
810 
811 	nat64lsn_destroy(cfg->name, cfg->set);
812 	return (0);
813 }
814 
815 
816 /*
817  * Compare nat64lsn instances names.
818  * Honor number comparison.
819  */
820 static int
821 nat64name_cmp(const void *a, const void *b)
822 {
823 	ipfw_nat64lsn_cfg *ca, *cb;
824 
825 	ca = (ipfw_nat64lsn_cfg *)a;
826 	cb = (ipfw_nat64lsn_cfg *)b;
827 
828 	if (ca->set > cb->set)
829 		return (1);
830 	else if (ca->set < cb->set)
831 		return (-1);
832 	return (stringnum_cmp(ca->name, cb->name));
833 }
834 
835 /*
836  * Retrieves nat64lsn instance list from kernel,
837  * optionally sorts it and calls requested function for each instance.
838  *
839  * Request: [ ipfw_obj_lheader ]
840  * Reply: [ ipfw_obj_lheader ipfw_nat64lsn_cfg x N ]
841  */
842 static int
843 nat64lsn_foreach(nat64lsn_cb_t *f, const char *name, uint8_t set,  int sort)
844 {
845 	ipfw_obj_lheader *olh;
846 	ipfw_nat64lsn_cfg *cfg;
847 	size_t sz;
848 	int i, error;
849 
850 	/* Start with reasonable default */
851 	sz = sizeof(*olh) + 16 * sizeof(ipfw_nat64lsn_cfg);
852 
853 	for (;;) {
854 		if ((olh = calloc(1, sz)) == NULL)
855 			return (ENOMEM);
856 
857 		olh->size = sz;
858 		if (do_get3(IP_FW_NAT64LSN_LIST, &olh->opheader, &sz) != 0) {
859 			sz = olh->size;
860 			free(olh);
861 			if (errno != ENOMEM)
862 				return (errno);
863 			continue;
864 		}
865 
866 		if (sort != 0)
867 			qsort(olh + 1, olh->count, olh->objsize,
868 			    nat64name_cmp);
869 
870 		cfg = (ipfw_nat64lsn_cfg *)(olh + 1);
871 		for (i = 0; i < olh->count; i++) {
872 			error = f(cfg, name, set); /* Ignore errors for now */
873 			cfg = (ipfw_nat64lsn_cfg *)((caddr_t)cfg +
874 			    olh->objsize);
875 		}
876 		free(olh);
877 		break;
878 	}
879 	return (0);
880 }
881 
882