xref: /freebsd/sbin/ipfw/tables.c (revision 78bc019d220e05abb5b12f678f9b4a847019bbcc)
1 /*
2  * Copyright (c) 2014 Yandex LLC
3  * Copyright (c) 2014 Alexander V. Chernikov
4  *
5  * Redistribution and use in source forms, with and without modification,
6  * are permitted provided that this entire comment appears intact.
7  *
8  * Redistribution in binary form may occur without any restrictions.
9  * Obviously, it would be nice if you gave credit where credit is due
10  * but requiring it would be too onerous.
11  *
12  * This software is provided ``AS IS'' without any warranties of any kind.
13  *
14  * in-kernel ipfw tables support.
15  *
16  * $FreeBSD$
17  */
18 
19 
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/socket.h>
23 #include <sys/sysctl.h>
24 
25 #include <ctype.h>
26 #include <err.h>
27 #include <errno.h>
28 #include <netdb.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sysexits.h>
33 
34 #include <net/ethernet.h>
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <netinet/ip_fw.h>
38 #include <arpa/inet.h>
39 #include <netdb.h>
40 
41 #include "ipfw2.h"
42 
43 static void table_modify_record(ipfw_obj_header *oh, int ac, char *av[],
44     int add, int quiet, int update, int atomic);
45 static int table_flush(ipfw_obj_header *oh);
46 static int table_destroy(ipfw_obj_header *oh);
47 static int table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i);
48 static int table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i);
49 static int table_do_swap(ipfw_obj_header *oh, char *second);
50 static void table_create(ipfw_obj_header *oh, int ac, char *av[]);
51 static void table_modify(ipfw_obj_header *oh, int ac, char *av[]);
52 static void table_lookup(ipfw_obj_header *oh, int ac, char *av[]);
53 static void table_lock(ipfw_obj_header *oh, int lock);
54 static int table_swap(ipfw_obj_header *oh, char *second);
55 static int table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i);
56 static int table_show_info(ipfw_xtable_info *i, void *arg);
57 
58 static int table_destroy_one(ipfw_xtable_info *i, void *arg);
59 static int table_flush_one(ipfw_xtable_info *i, void *arg);
60 static int table_show_one(ipfw_xtable_info *i, void *arg);
61 static int table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh);
62 static void table_show_list(ipfw_obj_header *oh, int need_header);
63 static void table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent);
64 
65 static void tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
66     char *key, int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi);
67 static void tentry_fill_value(ipfw_obj_header *oh, ipfw_obj_tentry *tent,
68     char *arg, uint8_t type, uint32_t vmask);
69 static void table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
70     uint32_t vmask, int print_ip);
71 
72 typedef int (table_cb_t)(ipfw_xtable_info *i, void *arg);
73 static int tables_foreach(table_cb_t *f, void *arg, int sort);
74 
75 #ifndef s6_addr32
76 #define s6_addr32 __u6_addr.__u6_addr32
77 #endif
78 
79 static struct _s_x tabletypes[] = {
80       { "addr",		IPFW_TABLE_ADDR },
81       { "mac",		IPFW_TABLE_MAC },
82       { "iface",	IPFW_TABLE_INTERFACE },
83       { "number",	IPFW_TABLE_NUMBER },
84       { "flow",		IPFW_TABLE_FLOW },
85       { NULL, 0 }
86 };
87 
88 /* Default algorithms for various table types */
89 static struct _s_x tablealgos[] = {
90       { "addr:radix",	IPFW_TABLE_ADDR },
91       { "flow:hash",	IPFW_TABLE_FLOW },
92       { "iface:array",	IPFW_TABLE_INTERFACE },
93       { "number:array",	IPFW_TABLE_NUMBER },
94       { NULL, 0 }
95 };
96 
97 static struct _s_x tablevaltypes[] = {
98       { "skipto",	IPFW_VTYPE_SKIPTO },
99       { "pipe",		IPFW_VTYPE_PIPE },
100       { "fib",		IPFW_VTYPE_FIB },
101       { "nat",		IPFW_VTYPE_NAT },
102       { "dscp",		IPFW_VTYPE_DSCP },
103       { "tag",		IPFW_VTYPE_TAG },
104       { "divert",	IPFW_VTYPE_DIVERT },
105       { "netgraph",	IPFW_VTYPE_NETGRAPH },
106       { "limit",	IPFW_VTYPE_LIMIT },
107       { "ipv4",		IPFW_VTYPE_NH4 },
108       { "ipv6",		IPFW_VTYPE_NH6 },
109       { NULL, 0 }
110 };
111 
112 static struct _s_x tablecmds[] = {
113       { "add",		TOK_ADD },
114       { "delete",	TOK_DEL },
115       { "create",	TOK_CREATE },
116       { "destroy",	TOK_DESTROY },
117       { "flush",	TOK_FLUSH },
118       { "modify",	TOK_MODIFY },
119       { "swap",		TOK_SWAP },
120       { "info",		TOK_INFO },
121       { "detail",	TOK_DETAIL },
122       { "list",		TOK_LIST },
123       { "lookup",	TOK_LOOKUP },
124       { "atomic",	TOK_ATOMIC },
125       { "lock",		TOK_LOCK },
126       { "unlock",	TOK_UNLOCK },
127       { NULL, 0 }
128 };
129 
130 static int
131 lookup_host (char *host, struct in_addr *ipaddr)
132 {
133 	struct hostent *he;
134 
135 	if (!inet_aton(host, ipaddr)) {
136 		if ((he = gethostbyname(host)) == NULL)
137 			return(-1);
138 		*ipaddr = *(struct in_addr *)he->h_addr_list[0];
139 	}
140 	return(0);
141 }
142 
143 /*
144  * This one handles all table-related commands
145  * 	ipfw table NAME create ...
146  * 	ipfw table NAME modify ...
147  * 	ipfw table {NAME | all} destroy
148  * 	ipfw table NAME swap NAME
149  * 	ipfw table NAME lock
150  * 	ipfw table NAME unlock
151  * 	ipfw table NAME add addr[/masklen] [value]
152  * 	ipfw table NAME add [addr[/masklen] value] [addr[/masklen] value] ..
153  * 	ipfw table NAME delete addr[/masklen] [addr[/masklen]] ..
154  * 	ipfw table NAME lookup addr
155  * 	ipfw table {NAME | all} flush
156  * 	ipfw table {NAME | all} list
157  * 	ipfw table {NAME | all} info
158  * 	ipfw table {NAME | all} detail
159  */
160 void
161 ipfw_table_handler(int ac, char *av[])
162 {
163 	int do_add, is_all;
164 	int atomic, error, tcmd;
165 	ipfw_xtable_info i;
166 	ipfw_obj_header oh;
167 	char *tablename;
168 	uint8_t set;
169 	void *arg;
170 
171 	memset(&oh, 0, sizeof(oh));
172 	is_all = 0;
173 	if (g_co.use_set != 0)
174 		set = g_co.use_set - 1;
175 	else
176 		set = 0;
177 
178 	ac--; av++;
179 	NEED1("table needs name");
180 	tablename = *av;
181 
182 	if (table_check_name(tablename) == 0) {
183 		table_fill_ntlv(&oh.ntlv, *av, set, 1);
184 		oh.idx = 1;
185 	} else {
186 		if (strcmp(tablename, "all") == 0)
187 			is_all = 1;
188 		else
189 			errx(EX_USAGE, "table name %s is invalid", tablename);
190 	}
191 	ac--; av++;
192 	NEED1("table needs command");
193 
194 	tcmd = get_token(tablecmds, *av, "table command");
195 	/* Check if atomic operation was requested */
196 	atomic = 0;
197 	if (tcmd == TOK_ATOMIC) {
198 		ac--; av++;
199 		NEED1("atomic needs command");
200 		tcmd = get_token(tablecmds, *av, "table command");
201 		switch (tcmd) {
202 		case TOK_ADD:
203 			break;
204 		default:
205 			errx(EX_USAGE, "atomic is not compatible with %s", *av);
206 		}
207 		atomic = 1;
208 	}
209 
210 	switch (tcmd) {
211 	case TOK_LIST:
212 	case TOK_INFO:
213 	case TOK_DETAIL:
214 	case TOK_FLUSH:
215 	case TOK_DESTROY:
216 		break;
217 	default:
218 		if (is_all != 0)
219 			errx(EX_USAGE, "table name required");
220 	}
221 
222 	switch (tcmd) {
223 	case TOK_ADD:
224 	case TOK_DEL:
225 		do_add = **av == 'a';
226 		ac--; av++;
227 		table_modify_record(&oh, ac, av, do_add, g_co.do_quiet,
228 		    g_co.do_quiet, atomic);
229 		break;
230 	case TOK_CREATE:
231 		ac--; av++;
232 		table_create(&oh, ac, av);
233 		break;
234 	case TOK_MODIFY:
235 		ac--; av++;
236 		table_modify(&oh, ac, av);
237 		break;
238 	case TOK_DESTROY:
239 		if (is_all == 0) {
240 			if (table_destroy(&oh) == 0)
241 				break;
242 			if (errno != ESRCH)
243 				err(EX_OSERR, "failed to destroy table %s",
244 				    tablename);
245 			/* ESRCH isn't fatal, warn if not quiet mode */
246 			if (g_co.do_quiet == 0)
247 				warn("failed to destroy table %s", tablename);
248 		} else {
249 			error = tables_foreach(table_destroy_one, &oh, 1);
250 			if (error != 0)
251 				err(EX_OSERR,
252 				    "failed to destroy tables list");
253 		}
254 		break;
255 	case TOK_FLUSH:
256 		if (is_all == 0) {
257 			if ((error = table_flush(&oh)) == 0)
258 				break;
259 			if (errno != ESRCH)
260 				err(EX_OSERR, "failed to flush table %s info",
261 				    tablename);
262 			/* ESRCH isn't fatal, warn if not quiet mode */
263 			if (g_co.do_quiet == 0)
264 				warn("failed to flush table %s info",
265 				    tablename);
266 		} else {
267 			error = tables_foreach(table_flush_one, &oh, 1);
268 			if (error != 0)
269 				err(EX_OSERR, "failed to flush tables list");
270 			/* XXX: we ignore errors here */
271 		}
272 		break;
273 	case TOK_SWAP:
274 		ac--; av++;
275 		NEED1("second table name required");
276 		table_swap(&oh, *av);
277 		break;
278 	case TOK_LOCK:
279 	case TOK_UNLOCK:
280 		table_lock(&oh, (tcmd == TOK_LOCK));
281 		break;
282 	case TOK_DETAIL:
283 	case TOK_INFO:
284 		arg = (tcmd == TOK_DETAIL) ? (void *)1 : NULL;
285 		if (is_all == 0) {
286 			if ((error = table_get_info(&oh, &i)) != 0)
287 				err(EX_OSERR, "failed to request table info");
288 			table_show_info(&i, arg);
289 		} else {
290 			error = tables_foreach(table_show_info, arg, 1);
291 			if (error != 0)
292 				err(EX_OSERR, "failed to request tables list");
293 		}
294 		break;
295 	case TOK_LIST:
296 		arg = is_all ? (void*)1 : NULL;
297 		if (is_all == 0) {
298 			if ((error = table_get_info(&oh, &i)) != 0)
299 				err(EX_OSERR, "failed to request table info");
300 			table_show_one(&i, arg);
301 		} else {
302 			error = tables_foreach(table_show_one, arg, 1);
303 			if (error != 0)
304 				err(EX_OSERR, "failed to request tables list");
305 		}
306 		break;
307 	case TOK_LOOKUP:
308 		ac--; av++;
309 		table_lookup(&oh, ac, av);
310 		break;
311 	}
312 }
313 
314 void
315 table_fill_ntlv(ipfw_obj_ntlv *ntlv, const char *name, uint8_t set,
316     uint16_t uidx)
317 {
318 
319 	ntlv->head.type = IPFW_TLV_TBL_NAME;
320 	ntlv->head.length = sizeof(ipfw_obj_ntlv);
321 	ntlv->idx = uidx;
322 	ntlv->set = set;
323 	strlcpy(ntlv->name, name, sizeof(ntlv->name));
324 }
325 
326 static void
327 table_fill_objheader(ipfw_obj_header *oh, ipfw_xtable_info *i)
328 {
329 
330 	oh->idx = 1;
331 	table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
332 }
333 
334 static struct _s_x tablenewcmds[] = {
335       { "type",		TOK_TYPE },
336       { "valtype",	TOK_VALTYPE },
337       { "algo",		TOK_ALGO },
338       { "limit",	TOK_LIMIT },
339       { "locked",	TOK_LOCK },
340       { "missing",	TOK_MISSING },
341       { "or-flush",	TOK_ORFLUSH },
342       { NULL, 0 }
343 };
344 
345 static struct _s_x flowtypecmds[] = {
346       { "src-ip",	IPFW_TFFLAG_SRCIP },
347       { "proto",	IPFW_TFFLAG_PROTO },
348       { "src-port",	IPFW_TFFLAG_SRCPORT },
349       { "dst-ip",	IPFW_TFFLAG_DSTIP },
350       { "dst-port",	IPFW_TFFLAG_DSTPORT },
351       { NULL, 0 }
352 };
353 
354 static int
355 table_parse_type(uint8_t ttype, char *p, uint8_t *tflags)
356 {
357 	uint32_t fset, fclear;
358 	char *e;
359 
360 	/* Parse type options */
361 	switch(ttype) {
362 	case IPFW_TABLE_FLOW:
363 		fset = fclear = 0;
364 		if (fill_flags(flowtypecmds, p, &e, &fset, &fclear) != 0)
365 			errx(EX_USAGE,
366 			    "unable to parse flow option %s", e);
367 		*tflags = fset;
368 		break;
369 	default:
370 		return (EX_USAGE);
371 	}
372 
373 	return (0);
374 }
375 
376 static void
377 table_print_type(char *tbuf, size_t size, uint8_t type, uint8_t tflags)
378 {
379 	const char *tname;
380 	int l;
381 
382 	if ((tname = match_value(tabletypes, type)) == NULL)
383 		tname = "unknown";
384 
385 	l = snprintf(tbuf, size, "%s", tname);
386 	tbuf += l;
387 	size -= l;
388 
389 	switch(type) {
390 	case IPFW_TABLE_FLOW:
391 		if (tflags != 0) {
392 			*tbuf++ = ':';
393 			l--;
394 			print_flags_buffer(tbuf, size, flowtypecmds, tflags);
395 		}
396 		break;
397 	}
398 }
399 
400 /*
401  * Creates new table
402  *
403  * ipfw table NAME create [ type { addr | iface | number | flow } ]
404  *     [ algo algoname ] [missing] [or-flush]
405  */
406 static void
407 table_create(ipfw_obj_header *oh, int ac, char *av[])
408 {
409 	ipfw_xtable_info xi, xie;
410 	int error, missing, orflush, tcmd, val;
411 	uint32_t fset, fclear;
412 	char *e, *p;
413 	char tbuf[128];
414 
415 	missing = orflush = 0;
416 	memset(&xi, 0, sizeof(xi));
417 	while (ac > 0) {
418 		tcmd = get_token(tablenewcmds, *av, "option");
419 		ac--; av++;
420 
421 		switch (tcmd) {
422 		case TOK_LIMIT:
423 			NEED1("limit value required");
424 			xi.limit = strtol(*av, NULL, 10);
425 			ac--; av++;
426 			break;
427 		case TOK_TYPE:
428 			NEED1("table type required");
429 			/* Type may have suboptions after ':' */
430 			if ((p = strchr(*av, ':')) != NULL)
431 				*p++ = '\0';
432 			val = match_token(tabletypes, *av);
433 			if (val == -1) {
434 				concat_tokens(tbuf, sizeof(tbuf), tabletypes,
435 				    ", ");
436 				errx(EX_USAGE,
437 				    "Unknown tabletype: %s. Supported: %s",
438 				    *av, tbuf);
439 			}
440 			xi.type = val;
441 			if (p != NULL) {
442 				error = table_parse_type(val, p, &xi.tflags);
443 				if (error != 0)
444 					errx(EX_USAGE,
445 					    "Unsupported suboptions: %s", p);
446 			}
447 			ac--; av++;
448 			break;
449 		case TOK_VALTYPE:
450 			NEED1("table value type required");
451 			fset = fclear = 0;
452 			val = fill_flags(tablevaltypes, *av, &e, &fset, &fclear);
453 			if (val != -1) {
454 				xi.vmask = fset;
455 				ac--; av++;
456 				break;
457 			}
458 			concat_tokens(tbuf, sizeof(tbuf), tablevaltypes, ", ");
459 			errx(EX_USAGE, "Unknown value type: %s. Supported: %s",
460 			    e, tbuf);
461 			break;
462 		case TOK_ALGO:
463 			NEED1("table algorithm name required");
464 			if (strlen(*av) > sizeof(xi.algoname))
465 				errx(EX_USAGE, "algorithm name too long");
466 			strlcpy(xi.algoname, *av, sizeof(xi.algoname));
467 			ac--; av++;
468 			break;
469 		case TOK_LOCK:
470 			xi.flags |= IPFW_TGFLAGS_LOCKED;
471 			break;
472 		case TOK_ORFLUSH:
473 			orflush = 1;
474 			/* FALLTHROUGH */
475 		case TOK_MISSING:
476 			missing = 1;
477 			break;
478 		}
479 	}
480 
481 	/* Set some defaults to preserve compatibility. */
482 	if (xi.algoname[0] == '\0') {
483 		const char *algo;
484 
485 		if (xi.type == 0)
486 			xi.type = IPFW_TABLE_ADDR;
487 		algo = match_value(tablealgos, xi.type);
488 		if (algo != NULL)
489 			strlcpy(xi.algoname, algo, sizeof(xi.algoname));
490 	}
491 	if (xi.vmask == 0)
492 		xi.vmask = IPFW_VTYPE_LEGACY;
493 
494 	error = table_do_create(oh, &xi);
495 
496 	if (error == 0)
497 		return;
498 
499 	if (errno != EEXIST || missing == 0)
500 		err(EX_OSERR, "Table creation failed");
501 
502 	/* Check that existing table is the same we are trying to create */
503 	if (table_get_info(oh, &xie) != 0)
504 		err(EX_OSERR, "Existing table check failed");
505 
506 	if (xi.limit != xie.limit || xi.type != xie.type ||
507 	    xi.tflags != xie.tflags || xi.vmask != xie.vmask || (
508 	    xi.algoname[0] != '\0' && strcmp(xi.algoname,
509 	    xie.algoname) != 0) || xi.flags != xie.flags)
510 		errx(EX_DATAERR, "The existing table is not compatible "
511 		    "with one you are creating.");
512 
513 	/* Flush existing table if instructed to do so */
514 	if (orflush != 0 && table_flush(oh) != 0)
515 		err(EX_OSERR, "Table flush on creation failed");
516 }
517 
518 /*
519  * Creates new table
520  *
521  * Request: [ ipfw_obj_header ipfw_xtable_info ]
522  *
523  * Returns 0 on success.
524  */
525 static int
526 table_do_create(ipfw_obj_header *oh, ipfw_xtable_info *i)
527 {
528 	char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
529 	int error;
530 
531 	memcpy(tbuf, oh, sizeof(*oh));
532 	memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
533 	oh = (ipfw_obj_header *)tbuf;
534 
535 	error = do_set3(IP_FW_TABLE_XCREATE, &oh->opheader, sizeof(tbuf));
536 
537 	return (error);
538 }
539 
540 /*
541  * Modifies existing table
542  *
543  * ipfw table NAME modify [ limit number ]
544  */
545 static void
546 table_modify(ipfw_obj_header *oh, int ac, char *av[])
547 {
548 	ipfw_xtable_info xi;
549 	int tcmd;
550 
551 	memset(&xi, 0, sizeof(xi));
552 
553 	while (ac > 0) {
554 		tcmd = get_token(tablenewcmds, *av, "option");
555 		ac--; av++;
556 
557 		switch (tcmd) {
558 		case TOK_LIMIT:
559 			NEED1("limit value required");
560 			xi.limit = strtol(*av, NULL, 10);
561 			xi.mflags |= IPFW_TMFLAGS_LIMIT;
562 			ac--; av++;
563 			break;
564 		default:
565 			errx(EX_USAGE, "cmd is not supported for modification");
566 		}
567 	}
568 
569 	if (table_do_modify(oh, &xi) != 0)
570 		err(EX_OSERR, "Table modification failed");
571 }
572 
573 /*
574  * Modifies existing table.
575  *
576  * Request: [ ipfw_obj_header ipfw_xtable_info ]
577  *
578  * Returns 0 on success.
579  */
580 static int
581 table_do_modify(ipfw_obj_header *oh, ipfw_xtable_info *i)
582 {
583 	char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
584 	int error;
585 
586 	memcpy(tbuf, oh, sizeof(*oh));
587 	memcpy(tbuf + sizeof(*oh), i, sizeof(*i));
588 	oh = (ipfw_obj_header *)tbuf;
589 
590 	error = do_set3(IP_FW_TABLE_XMODIFY, &oh->opheader, sizeof(tbuf));
591 
592 	return (error);
593 }
594 
595 /*
596  * Locks or unlocks given table
597  */
598 static void
599 table_lock(ipfw_obj_header *oh, int lock)
600 {
601 	ipfw_xtable_info xi;
602 
603 	memset(&xi, 0, sizeof(xi));
604 
605 	xi.mflags |= IPFW_TMFLAGS_LOCK;
606 	xi.flags |= (lock != 0) ? IPFW_TGFLAGS_LOCKED : 0;
607 
608 	if (table_do_modify(oh, &xi) != 0)
609 		err(EX_OSERR, "Table %s failed", lock != 0 ? "lock" : "unlock");
610 }
611 
612 /*
613  * Destroys given table specified by @oh->ntlv.
614  * Returns 0 on success.
615  */
616 static int
617 table_destroy(ipfw_obj_header *oh)
618 {
619 
620 	if (do_set3(IP_FW_TABLE_XDESTROY, &oh->opheader, sizeof(*oh)) != 0)
621 		return (-1);
622 
623 	return (0);
624 }
625 
626 static int
627 table_destroy_one(ipfw_xtable_info *i, void *arg)
628 {
629 	ipfw_obj_header *oh;
630 
631 	oh = (ipfw_obj_header *)arg;
632 	table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
633 	if (table_destroy(oh) != 0) {
634 		if (g_co.do_quiet == 0)
635 			warn("failed to destroy table(%s) in set %u",
636 			    i->tablename, i->set);
637 		return (-1);
638 	}
639 	return (0);
640 }
641 
642 /*
643  * Flushes given table specified by @oh->ntlv.
644  * Returns 0 on success.
645  */
646 static int
647 table_flush(ipfw_obj_header *oh)
648 {
649 
650 	if (do_set3(IP_FW_TABLE_XFLUSH, &oh->opheader, sizeof(*oh)) != 0)
651 		return (-1);
652 
653 	return (0);
654 }
655 
656 static int
657 table_do_swap(ipfw_obj_header *oh, char *second)
658 {
659 	char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ntlv)];
660 	int error;
661 
662 	memset(tbuf, 0, sizeof(tbuf));
663 	memcpy(tbuf, oh, sizeof(*oh));
664 	oh = (ipfw_obj_header *)tbuf;
665 	table_fill_ntlv((ipfw_obj_ntlv *)(oh + 1), second, oh->ntlv.set, 1);
666 
667 	error = do_set3(IP_FW_TABLE_XSWAP, &oh->opheader, sizeof(tbuf));
668 
669 	return (error);
670 }
671 
672 /*
673  * Swaps given table with @second one.
674  */
675 static int
676 table_swap(ipfw_obj_header *oh, char *second)
677 {
678 
679 	if (table_check_name(second) != 0)
680 		errx(EX_USAGE, "table name %s is invalid", second);
681 
682 	if (table_do_swap(oh, second) == 0)
683 		return (0);
684 
685 	switch (errno) {
686 	case EINVAL:
687 		errx(EX_USAGE, "Unable to swap table: check types");
688 	case EFBIG:
689 		errx(EX_USAGE, "Unable to swap table: check limits");
690 	}
691 
692 	return (0);
693 }
694 
695 
696 /*
697  * Retrieves table in given table specified by @oh->ntlv.
698  * it inside @i.
699  * Returns 0 on success.
700  */
701 static int
702 table_get_info(ipfw_obj_header *oh, ipfw_xtable_info *i)
703 {
704 	char tbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_xtable_info)];
705 	size_t sz;
706 
707 	sz = sizeof(tbuf);
708 	memset(tbuf, 0, sizeof(tbuf));
709 	memcpy(tbuf, oh, sizeof(*oh));
710 	oh = (ipfw_obj_header *)tbuf;
711 
712 	if (do_get3(IP_FW_TABLE_XINFO, &oh->opheader, &sz) != 0)
713 		return (errno);
714 
715 	if (sz < sizeof(tbuf))
716 		return (EINVAL);
717 
718 	*i = *(ipfw_xtable_info *)(oh + 1);
719 
720 	return (0);
721 }
722 
723 static struct _s_x tablealgoclass[] = {
724       { "hash",		IPFW_TACLASS_HASH },
725       { "array",	IPFW_TACLASS_ARRAY },
726       { "radix",	IPFW_TACLASS_RADIX },
727       { NULL, 0 }
728 };
729 
730 struct ta_cldata {
731 	uint8_t		taclass;
732 	uint8_t		spare4;
733 	uint16_t	itemsize;
734 	uint16_t	itemsize6;
735 	uint32_t	size;
736 	uint32_t	count;
737 };
738 
739 /*
740  * Print global/per-AF table @i algorithm info.
741  */
742 static void
743 table_show_tainfo(ipfw_xtable_info *i __unused, struct ta_cldata *d,
744     const char *af, const char *taclass)
745 {
746 
747 	switch (d->taclass) {
748 	case IPFW_TACLASS_HASH:
749 	case IPFW_TACLASS_ARRAY:
750 		printf(" %salgorithm %s info\n", af, taclass);
751 		if (d->itemsize == d->itemsize6)
752 			printf("  size: %u items: %u itemsize: %u\n",
753 			    d->size, d->count, d->itemsize);
754 		else
755 			printf("  size: %u items: %u "
756 			    "itemsize4: %u itemsize6: %u\n",
757 			    d->size, d->count,
758 			    d->itemsize, d->itemsize6);
759 		break;
760 	case IPFW_TACLASS_RADIX:
761 		printf(" %salgorithm %s info\n", af, taclass);
762 		if (d->itemsize == d->itemsize6)
763 			printf("  items: %u itemsize: %u\n",
764 			    d->count, d->itemsize);
765 		else
766 			printf("  items: %u "
767 			    "itemsize4: %u itemsize6: %u\n",
768 			    d->count, d->itemsize, d->itemsize6);
769 		break;
770 	default:
771 		printf(" algo class: %s\n", taclass);
772 	}
773 }
774 
775 static void
776 table_print_valheader(char *buf, size_t bufsize, uint32_t vmask)
777 {
778 
779 	if (vmask == IPFW_VTYPE_LEGACY) {
780 		snprintf(buf, bufsize, "legacy");
781 		return;
782 	}
783 
784 	memset(buf, 0, bufsize);
785 	print_flags_buffer(buf, bufsize, tablevaltypes, vmask);
786 }
787 
788 /*
789  * Prints table info struct @i in human-readable form.
790  */
791 static int
792 table_show_info(ipfw_xtable_info *i, void *arg)
793 {
794 	const char *vtype;
795 	ipfw_ta_tinfo *tainfo;
796 	int afdata, afitem;
797 	struct ta_cldata d;
798 	char ttype[64], tvtype[64];
799 
800 	table_print_type(ttype, sizeof(ttype), i->type, i->tflags);
801 	table_print_valheader(tvtype, sizeof(tvtype), i->vmask);
802 
803 	printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
804 	if ((i->flags & IPFW_TGFLAGS_LOCKED) != 0)
805 		printf(" kindex: %d, type: %s, locked\n", i->kidx, ttype);
806 	else
807 		printf(" kindex: %d, type: %s\n", i->kidx, ttype);
808 	printf(" references: %u, valtype: %s\n", i->refcnt, tvtype);
809 	printf(" algorithm: %s\n", i->algoname);
810 	printf(" items: %u, size: %u\n", i->count, i->size);
811 	if (i->limit > 0)
812 		printf(" limit: %u\n", i->limit);
813 
814 	/* Print algo-specific info if requested & set  */
815 	if (arg == NULL)
816 		return (0);
817 
818 	if ((i->ta_info.flags & IPFW_TATFLAGS_DATA) == 0)
819 		return (0);
820 	tainfo = &i->ta_info;
821 
822 	afdata = 0;
823 	afitem = 0;
824 	if (tainfo->flags & IPFW_TATFLAGS_AFDATA)
825 		afdata = 1;
826 	if (tainfo->flags & IPFW_TATFLAGS_AFITEM)
827 		afitem = 1;
828 
829 	memset(&d, 0, sizeof(d));
830 	d.taclass = tainfo->taclass4;
831 	d.size = tainfo->size4;
832 	d.count = tainfo->count4;
833 	d.itemsize = tainfo->itemsize4;
834 	if (afdata == 0 && afitem != 0)
835 		d.itemsize6 = tainfo->itemsize6;
836 	else
837 		d.itemsize6 = d.itemsize;
838 	if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
839 		vtype = "unknown";
840 
841 	if (afdata == 0) {
842 		table_show_tainfo(i, &d, "", vtype);
843 	} else {
844 		table_show_tainfo(i, &d, "IPv4 ", vtype);
845 		memset(&d, 0, sizeof(d));
846 		d.taclass = tainfo->taclass6;
847 		if ((vtype = match_value(tablealgoclass, d.taclass)) == NULL)
848 			vtype = "unknown";
849 		d.size = tainfo->size6;
850 		d.count = tainfo->count6;
851 		d.itemsize = tainfo->itemsize6;
852 		d.itemsize6 = d.itemsize;
853 		table_show_tainfo(i, &d, "IPv6 ", vtype);
854 	}
855 
856 	return (0);
857 }
858 
859 
860 /*
861  * Function wrappers which can be used either
862  * as is or as foreach function parameter.
863  */
864 
865 static int
866 table_show_one(ipfw_xtable_info *i, void *arg)
867 {
868 	ipfw_obj_header *oh = NULL;
869 	int error;
870 	int is_all;
871 
872 	is_all = arg == NULL ? 0 : 1;
873 
874 	if ((error = table_do_get_list(i, &oh)) != 0) {
875 		err(EX_OSERR, "Error requesting table %s list", i->tablename);
876 		return (error);
877 	}
878 
879 	table_show_list(oh, is_all);
880 
881 	free(oh);
882 	return (0);
883 }
884 
885 static int
886 table_flush_one(ipfw_xtable_info *i, void *arg)
887 {
888 	ipfw_obj_header *oh;
889 
890 	oh = (ipfw_obj_header *)arg;
891 
892 	table_fill_ntlv(&oh->ntlv, i->tablename, i->set, 1);
893 
894 	return (table_flush(oh));
895 }
896 
897 static int
898 table_do_modify_record(int cmd, ipfw_obj_header *oh,
899     ipfw_obj_tentry *tent, int count, int atomic)
900 {
901 	ipfw_obj_ctlv *ctlv;
902 	ipfw_obj_tentry *tent_base;
903 	caddr_t pbuf;
904 	char xbuf[sizeof(*oh) + sizeof(ipfw_obj_ctlv) + sizeof(*tent)];
905 	int error, i;
906 	size_t sz;
907 
908 	sz = sizeof(*ctlv) + sizeof(*tent) * count;
909 	if (count == 1) {
910 		memset(xbuf, 0, sizeof(xbuf));
911 		pbuf = xbuf;
912 	} else {
913 		if ((pbuf = calloc(1, sizeof(*oh) + sz)) == NULL)
914 			return (ENOMEM);
915 	}
916 
917 	memcpy(pbuf, oh, sizeof(*oh));
918 	oh = (ipfw_obj_header *)pbuf;
919 	oh->opheader.version = 1;
920 
921 	ctlv = (ipfw_obj_ctlv *)(oh + 1);
922 	ctlv->count = count;
923 	ctlv->head.length = sz;
924 	if (atomic != 0)
925 		ctlv->flags |= IPFW_CTF_ATOMIC;
926 
927 	tent_base = tent;
928 	memcpy(ctlv + 1, tent, sizeof(*tent) * count);
929 	tent = (ipfw_obj_tentry *)(ctlv + 1);
930 	for (i = 0; i < count; i++, tent++) {
931 		tent->head.length = sizeof(ipfw_obj_tentry);
932 		tent->idx = oh->idx;
933 	}
934 
935 	sz += sizeof(*oh);
936 	error = do_get3(cmd, &oh->opheader, &sz);
937 	if (error != 0)
938 		error = errno;
939 	tent = (ipfw_obj_tentry *)(ctlv + 1);
940 	/* Copy result back to provided buffer */
941 	memcpy(tent_base, ctlv + 1, sizeof(*tent) * count);
942 
943 	if (pbuf != xbuf)
944 		free(pbuf);
945 
946 	return (error);
947 }
948 
949 static void
950 table_modify_record(ipfw_obj_header *oh, int ac, char *av[], int add,
951     int quiet, int update, int atomic)
952 {
953 	ipfw_obj_tentry *ptent, tent, *tent_buf;
954 	ipfw_xtable_info xi;
955 	const char *etxt, *px, *texterr;
956 	uint8_t type;
957 	uint32_t vmask;
958 	int cmd, count, error, i, ignored;
959 
960 	if (ac == 0)
961 		errx(EX_USAGE, "address required");
962 
963 	if (add != 0) {
964 		cmd = IP_FW_TABLE_XADD;
965 		texterr = "Adding record failed";
966 	} else {
967 		cmd = IP_FW_TABLE_XDEL;
968 		texterr = "Deleting record failed";
969 	}
970 
971 	/*
972 	 * Calculate number of entries:
973 	 * Assume [key val] x N for add
974 	 * and
975 	 * key x N for delete
976 	 */
977 	count = (add != 0) ? ac / 2 + 1 : ac;
978 
979 	if (count <= 1) {
980 		/* Adding single entry with/without value */
981 		memset(&tent, 0, sizeof(tent));
982 		tent_buf = &tent;
983 	} else {
984 
985 		if ((tent_buf = calloc(count, sizeof(tent))) == NULL)
986 			errx(EX_OSERR,
987 			    "Unable to allocate memory for all entries");
988 	}
989 	ptent = tent_buf;
990 
991 	memset(&xi, 0, sizeof(xi));
992 	count = 0;
993 	while (ac > 0) {
994 		tentry_fill_key(oh, ptent, *av, add, &type, &vmask, &xi);
995 
996 		/*
997 		 * Compatibility layer: auto-create table if not exists.
998 		 */
999 		if (xi.tablename[0] == '\0') {
1000 			xi.type = type;
1001 			xi.vmask = vmask;
1002 			strlcpy(xi.tablename, oh->ntlv.name,
1003 			    sizeof(xi.tablename));
1004 			if (quiet == 0)
1005 				warnx("DEPRECATED: inserting data into "
1006 				    "non-existent table %s. (auto-created)",
1007 				    xi.tablename);
1008 			table_do_create(oh, &xi);
1009 		}
1010 
1011 		oh->ntlv.type = type;
1012 		ac--; av++;
1013 
1014 		if (add != 0 && ac > 0) {
1015 			tentry_fill_value(oh, ptent, *av, type, vmask);
1016 			ac--; av++;
1017 		}
1018 
1019 		if (update != 0)
1020 			ptent->head.flags |= IPFW_TF_UPDATE;
1021 
1022 		count++;
1023 		ptent++;
1024 	}
1025 
1026 	error = table_do_modify_record(cmd, oh, tent_buf, count, atomic);
1027 
1028 	/*
1029 	 * Compatibility stuff: do not yell on duplicate keys or
1030 	 * failed deletions.
1031 	 */
1032 	if (error == 0 || (error == EEXIST && add != 0) ||
1033 	    (error == ENOENT && add == 0)) {
1034 		if (quiet != 0) {
1035 			if (tent_buf != &tent)
1036 				free(tent_buf);
1037 			return;
1038 		}
1039 	}
1040 
1041 	/* Report results back */
1042 	ptent = tent_buf;
1043 	for (i = 0; i < count; ptent++, i++) {
1044 		ignored = 0;
1045 		switch (ptent->result) {
1046 		case IPFW_TR_ADDED:
1047 			px = "added";
1048 			break;
1049 		case IPFW_TR_DELETED:
1050 			px = "deleted";
1051 			break;
1052 		case IPFW_TR_UPDATED:
1053 			px = "updated";
1054 			break;
1055 		case IPFW_TR_LIMIT:
1056 			px = "limit";
1057 			ignored = 1;
1058 			break;
1059 		case IPFW_TR_ERROR:
1060 			px = "error";
1061 			ignored = 1;
1062 			break;
1063 		case IPFW_TR_NOTFOUND:
1064 			px = "notfound";
1065 			ignored = 1;
1066 			break;
1067 		case IPFW_TR_EXISTS:
1068 			px = "exists";
1069 			ignored = 1;
1070 			break;
1071 		case IPFW_TR_IGNORED:
1072 			px = "ignored";
1073 			ignored = 1;
1074 			break;
1075 		default:
1076 			px = "unknown";
1077 			ignored = 1;
1078 		}
1079 
1080 		if (error != 0 && atomic != 0 && ignored == 0)
1081 			printf("%s(reverted): ", px);
1082 		else
1083 			printf("%s: ", px);
1084 
1085 		table_show_entry(&xi, ptent);
1086 	}
1087 
1088 	if (tent_buf != &tent)
1089 		free(tent_buf);
1090 
1091 	if (error == 0)
1092 		return;
1093 	/* Get real OS error */
1094 	error = errno;
1095 
1096 	/* Try to provide more human-readable error */
1097 	switch (error) {
1098 	case EEXIST:
1099 		etxt = "record already exists";
1100 		break;
1101 	case EFBIG:
1102 		etxt = "limit hit";
1103 		break;
1104 	case ESRCH:
1105 		etxt = "table not found";
1106 		break;
1107 	case ENOENT:
1108 		etxt = "record not found";
1109 		break;
1110 	case EACCES:
1111 		etxt = "table is locked";
1112 		break;
1113 	default:
1114 		etxt = strerror(error);
1115 	}
1116 
1117 	errx(EX_OSERR, "%s: %s", texterr, etxt);
1118 }
1119 
1120 static int
1121 table_do_lookup(ipfw_obj_header *oh, char *key, ipfw_xtable_info *xi,
1122     ipfw_obj_tentry *xtent)
1123 {
1124 	char xbuf[sizeof(ipfw_obj_header) + sizeof(ipfw_obj_tentry)];
1125 	ipfw_obj_tentry *tent;
1126 	uint8_t type;
1127 	uint32_t vmask;
1128 	size_t sz;
1129 
1130 	memcpy(xbuf, oh, sizeof(*oh));
1131 	oh = (ipfw_obj_header *)xbuf;
1132 	tent = (ipfw_obj_tentry *)(oh + 1);
1133 
1134 	memset(tent, 0, sizeof(*tent));
1135 	tent->head.length = sizeof(*tent);
1136 	tent->idx = 1;
1137 
1138 	tentry_fill_key(oh, tent, key, 0, &type, &vmask, xi);
1139 	oh->ntlv.type = type;
1140 
1141 	sz = sizeof(xbuf);
1142 	if (do_get3(IP_FW_TABLE_XFIND, &oh->opheader, &sz) != 0)
1143 		return (errno);
1144 
1145 	if (sz < sizeof(xbuf))
1146 		return (EINVAL);
1147 
1148 	*xtent = *tent;
1149 
1150 	return (0);
1151 }
1152 
1153 static void
1154 table_lookup(ipfw_obj_header *oh, int ac, char *av[])
1155 {
1156 	ipfw_obj_tentry xtent;
1157 	ipfw_xtable_info xi;
1158 	char key[64];
1159 	int error;
1160 
1161 	if (ac == 0)
1162 		errx(EX_USAGE, "address required");
1163 
1164 	strlcpy(key, *av, sizeof(key));
1165 
1166 	memset(&xi, 0, sizeof(xi));
1167 	error = table_do_lookup(oh, key, &xi, &xtent);
1168 
1169 	switch (error) {
1170 	case 0:
1171 		break;
1172 	case ESRCH:
1173 		errx(EX_UNAVAILABLE, "Table %s not found", oh->ntlv.name);
1174 	case ENOENT:
1175 		errx(EX_UNAVAILABLE, "Entry %s not found", *av);
1176 	case ENOTSUP:
1177 		errx(EX_UNAVAILABLE, "Table %s algo does not support "
1178 		    "\"lookup\" method", oh->ntlv.name);
1179 	default:
1180 		err(EX_OSERR, "getsockopt(IP_FW_TABLE_XFIND)");
1181 	}
1182 
1183 	table_show_entry(&xi, &xtent);
1184 }
1185 
1186 static void
1187 tentry_fill_key_type(char *arg, ipfw_obj_tentry *tentry, uint8_t type,
1188     uint8_t tflags)
1189 {
1190 	char *p, *pp;
1191 	int mask, af;
1192 	struct in6_addr *paddr, tmp;
1193 	struct ether_addr *mac;
1194 	struct tflow_entry *tfe;
1195 	uint32_t key, *pkey;
1196 	uint16_t port;
1197 	struct protoent *pent;
1198 	struct servent *sent;
1199 	int masklen;
1200 
1201 	mask = masklen = 0;
1202 	af = 0;
1203 	paddr = (struct in6_addr *)&tentry->k;
1204 
1205 	switch (type) {
1206 	case IPFW_TABLE_ADDR:
1207 		/* Remove / if exists */
1208 		if ((p = strchr(arg, '/')) != NULL) {
1209 			*p = '\0';
1210 			mask = atoi(p + 1);
1211 		}
1212 
1213 		if (inet_pton(AF_INET, arg, paddr) == 1) {
1214 			if (p != NULL && mask > 32)
1215 				errx(EX_DATAERR, "bad IPv4 mask width: %s",
1216 				    p + 1);
1217 
1218 			masklen = p ? mask : 32;
1219 			af = AF_INET;
1220 		} else if (inet_pton(AF_INET6, arg, paddr) == 1) {
1221 			if (IN6_IS_ADDR_V4COMPAT(paddr))
1222 				errx(EX_DATAERR,
1223 				    "Use IPv4 instead of v4-compatible");
1224 			if (p != NULL && mask > 128)
1225 				errx(EX_DATAERR, "bad IPv6 mask width: %s",
1226 				    p + 1);
1227 
1228 			masklen = p ? mask : 128;
1229 			af = AF_INET6;
1230 		} else {
1231 			/* Assume FQDN */
1232 			if (lookup_host(arg, (struct in_addr *)paddr) != 0)
1233 				errx(EX_NOHOST, "hostname ``%s'' unknown", arg);
1234 
1235 			masklen = 32;
1236 			type = IPFW_TABLE_ADDR;
1237 			af = AF_INET;
1238 		}
1239 		break;
1240 	case IPFW_TABLE_MAC:
1241 		/* Remove / if exists */
1242 		if ((p = strchr(arg, '/')) != NULL) {
1243 			*p = '\0';
1244 			mask = atoi(p + 1);
1245 		}
1246 
1247 		if (p != NULL && mask > 8 * ETHER_ADDR_LEN)
1248 			errx(EX_DATAERR, "bad MAC mask width: %s",
1249 			    p + 1);
1250 
1251 		if ((mac = ether_aton(arg)) == NULL)
1252 			errx(EX_DATAERR, "Incorrect MAC address");
1253 
1254 		memcpy(tentry->k.mac, mac->octet, ETHER_ADDR_LEN);
1255 		masklen = p ? mask : 8 * ETHER_ADDR_LEN;
1256 		af = AF_LINK;
1257 		break;
1258 	case IPFW_TABLE_INTERFACE:
1259 		/* Assume interface name. Copy significant data only */
1260 		mask = MIN(strlen(arg), IF_NAMESIZE - 1);
1261 		memcpy(paddr, arg, mask);
1262 		/* Set mask to exact match */
1263 		masklen = 8 * IF_NAMESIZE;
1264 		break;
1265 	case IPFW_TABLE_NUMBER:
1266 		/* Port or any other key */
1267 		key = strtol(arg, &p, 10);
1268 		if (*p != '\0')
1269 			errx(EX_DATAERR, "Invalid number: %s", arg);
1270 
1271 		pkey = (uint32_t *)paddr;
1272 		*pkey = key;
1273 		masklen = 32;
1274 		break;
1275 	case IPFW_TABLE_FLOW:
1276 		/* Assume [src-ip][,proto][,src-port][,dst-ip][,dst-port] */
1277 		tfe = &tentry->k.flow;
1278 		af = 0;
1279 
1280 		/* Handle <ipv4|ipv6> */
1281 		if ((tflags & IPFW_TFFLAG_SRCIP) != 0) {
1282 			if ((p = strchr(arg, ',')) != NULL)
1283 				*p++ = '\0';
1284 			/* Determine family using temporary storage */
1285 			if (inet_pton(AF_INET, arg, &tmp) == 1) {
1286 				if (af != 0 && af != AF_INET)
1287 					errx(EX_DATAERR,
1288 					    "Inconsistent address family\n");
1289 				af = AF_INET;
1290 				memcpy(&tfe->a.a4.sip, &tmp, 4);
1291 			} else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1292 				if (af != 0 && af != AF_INET6)
1293 					errx(EX_DATAERR,
1294 					    "Inconsistent address family\n");
1295 				af = AF_INET6;
1296 				memcpy(&tfe->a.a6.sip6, &tmp, 16);
1297 			}
1298 
1299 			arg = p;
1300 		}
1301 
1302 		/* Handle <proto-num|proto-name> */
1303 		if ((tflags & IPFW_TFFLAG_PROTO) != 0) {
1304 			if (arg == NULL)
1305 				errx(EX_DATAERR, "invalid key: proto missing");
1306 			if ((p = strchr(arg, ',')) != NULL)
1307 				*p++ = '\0';
1308 
1309 			key = strtol(arg, &pp, 10);
1310 			if (*pp != '\0') {
1311 				if ((pent = getprotobyname(arg)) == NULL)
1312 					errx(EX_DATAERR, "Unknown proto: %s",
1313 					    arg);
1314 				else
1315 					key = pent->p_proto;
1316 			}
1317 
1318 			if (key > 255)
1319 				errx(EX_DATAERR, "Bad protocol number: %u",key);
1320 
1321 			tfe->proto = key;
1322 
1323 			arg = p;
1324 		}
1325 
1326 		/* Handle <port-num|service-name> */
1327 		if ((tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1328 			if (arg == NULL)
1329 				errx(EX_DATAERR, "invalid key: src port missing");
1330 			if ((p = strchr(arg, ',')) != NULL)
1331 				*p++ = '\0';
1332 
1333 			port = htons(strtol(arg, &pp, 10));
1334 			if (*pp != '\0') {
1335 				if ((sent = getservbyname(arg, NULL)) == NULL)
1336 					errx(EX_DATAERR, "Unknown service: %s",
1337 					    arg);
1338 				port = sent->s_port;
1339 			}
1340 			tfe->sport = port;
1341 			arg = p;
1342 		}
1343 
1344 		/* Handle <ipv4|ipv6>*/
1345 		if ((tflags & IPFW_TFFLAG_DSTIP) != 0) {
1346 			if (arg == NULL)
1347 				errx(EX_DATAERR, "invalid key: dst ip missing");
1348 			if ((p = strchr(arg, ',')) != NULL)
1349 				*p++ = '\0';
1350 			/* Determine family using temporary storage */
1351 			if (inet_pton(AF_INET, arg, &tmp) == 1) {
1352 				if (af != 0 && af != AF_INET)
1353 					errx(EX_DATAERR,
1354 					    "Inconsistent address family");
1355 				af = AF_INET;
1356 				memcpy(&tfe->a.a4.dip, &tmp, 4);
1357 			} else if (inet_pton(AF_INET6, arg, &tmp) == 1) {
1358 				if (af != 0 && af != AF_INET6)
1359 					errx(EX_DATAERR,
1360 					    "Inconsistent address family");
1361 				af = AF_INET6;
1362 				memcpy(&tfe->a.a6.dip6, &tmp, 16);
1363 			}
1364 
1365 			arg = p;
1366 		}
1367 
1368 		/* Handle <port-num|service-name> */
1369 		if ((tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1370 			if (arg == NULL)
1371 				errx(EX_DATAERR, "invalid key: dst port missing");
1372 			if ((p = strchr(arg, ',')) != NULL)
1373 				*p++ = '\0';
1374 
1375 			port = htons(strtol(arg, &pp, 10));
1376 			if (*pp != '\0') {
1377 				if ((sent = getservbyname(arg, NULL)) == NULL)
1378 					errx(EX_DATAERR, "Unknown service: %s",
1379 					    arg);
1380 				port = sent->s_port;
1381 			}
1382 			tfe->dport = port;
1383 			arg = p;
1384 		}
1385 
1386 		tfe->af = af;
1387 
1388 		break;
1389 
1390 	default:
1391 		errx(EX_DATAERR, "Unsupported table type: %d", type);
1392 	}
1393 
1394 	tentry->subtype = af;
1395 	tentry->masklen = masklen;
1396 }
1397 
1398 /*
1399  * Tries to guess table key type.
1400  * This procedure is used in legacy table auto-create
1401  * code AND in `ipfw -n` ruleset checking.
1402  *
1403  * Imported from old table_fill_xentry() parse code.
1404  */
1405 static int
1406 guess_key_type(char *key, uint8_t *ptype)
1407 {
1408 	char *p;
1409 	struct in6_addr addr;
1410 	uint32_t kv;
1411 
1412 	if (ishexnumber(*key) != 0 || *key == ':') {
1413 		/* Remove / if exists */
1414 		if ((p = strchr(key, '/')) != NULL)
1415 			*p = '\0';
1416 
1417 		if ((inet_pton(AF_INET, key, &addr) == 1) ||
1418 		    (inet_pton(AF_INET6, key, &addr) == 1)) {
1419 			*ptype = IPFW_TABLE_CIDR;
1420 			if (p != NULL)
1421 				*p = '/';
1422 			return (0);
1423 		} else {
1424 			/* Port or any other key */
1425 			/* Skip non-base 10 entries like 'fa1' */
1426 			kv = strtol(key, &p, 10);
1427 			if (*p == '\0') {
1428 				*ptype = IPFW_TABLE_NUMBER;
1429 				return (0);
1430 			} else if ((p != key) && (*p == '.')) {
1431 				/*
1432 				 * Warn on IPv4 address strings
1433 				 * which are "valid" for inet_aton() but not
1434 				 * in inet_pton().
1435 				 *
1436 				 * Typical examples: '10.5' or '10.0.0.05'
1437 				 */
1438 				return (1);
1439 			}
1440 		}
1441 	}
1442 
1443 	if (strchr(key, '.') == NULL) {
1444 		*ptype = IPFW_TABLE_INTERFACE;
1445 		return (0);
1446 	}
1447 
1448 	if (lookup_host(key, (struct in_addr *)&addr) != 0)
1449 		return (1);
1450 
1451 	*ptype = IPFW_TABLE_CIDR;
1452 	return (0);
1453 }
1454 
1455 static void
1456 tentry_fill_key(ipfw_obj_header *oh, ipfw_obj_tentry *tent, char *key,
1457     int add, uint8_t *ptype, uint32_t *pvmask, ipfw_xtable_info *xi)
1458 {
1459 	uint8_t type, tflags;
1460 	uint32_t vmask;
1461 	int error;
1462 
1463 	type = 0;
1464 	tflags = 0;
1465 	vmask = 0;
1466 
1467 	if (xi->tablename[0] == '\0')
1468 		error = table_get_info(oh, xi);
1469 	else
1470 		error = 0;
1471 
1472 	if (error == 0) {
1473 		if (g_co.test_only == 0) {
1474 			/* Table found */
1475 			type = xi->type;
1476 			tflags = xi->tflags;
1477 			vmask = xi->vmask;
1478 		} else {
1479 			/*
1480 			 * We're running `ipfw -n`
1481 			 * Compatibility layer: try to guess key type
1482 			 * before failing.
1483 			 */
1484 			if (guess_key_type(key, &type) != 0) {
1485 				/* Inknown key */
1486 				errx(EX_USAGE, "Cannot guess "
1487 				    "key '%s' type", key);
1488 			}
1489 			vmask = IPFW_VTYPE_LEGACY;
1490 		}
1491 	} else {
1492 		if (error != ESRCH)
1493 			errx(EX_OSERR, "Error requesting table %s info",
1494 			    oh->ntlv.name);
1495 		if (add == 0)
1496 			errx(EX_DATAERR, "Table %s does not exist",
1497 			    oh->ntlv.name);
1498 		/*
1499 		 * Table does not exist
1500 		 * Compatibility layer: try to guess key type before failing.
1501 		 */
1502 		if (guess_key_type(key, &type) != 0) {
1503 			/* Inknown key */
1504 			errx(EX_USAGE, "Table %s does not exist, cannot guess "
1505 			    "key '%s' type", oh->ntlv.name, key);
1506 		}
1507 
1508 		vmask = IPFW_VTYPE_LEGACY;
1509 	}
1510 
1511 	tentry_fill_key_type(key, tent, type, tflags);
1512 
1513 	*ptype = type;
1514 	*pvmask = vmask;
1515 }
1516 
1517 static void
1518 set_legacy_value(uint32_t val, ipfw_table_value *v)
1519 {
1520 	v->tag = val;
1521 	v->pipe = val;
1522 	v->divert = val;
1523 	v->skipto = val;
1524 	v->netgraph = val;
1525 	v->fib = val;
1526 	v->nat = val;
1527 	v->nh4 = val;
1528 	v->dscp = (uint8_t)val;
1529 	v->limit = val;
1530 }
1531 
1532 static void
1533 tentry_fill_value(ipfw_obj_header *oh __unused, ipfw_obj_tentry *tent,
1534     char *arg, uint8_t type __unused, uint32_t vmask)
1535 {
1536 	struct addrinfo hints, *res;
1537 	struct in_addr ipaddr;
1538 	const char *etype;
1539 	char *comma, *e, *n, *p;
1540 	uint32_t a4, flag, val;
1541 	ipfw_table_value *v;
1542 	uint32_t i;
1543 	int dval;
1544 
1545 	v = &tent->v.value;
1546 
1547 	/* Compat layer: keep old behavior for legacy value types */
1548 	if (vmask == IPFW_VTYPE_LEGACY) {
1549 		/* Try to interpret as number first */
1550 		val = strtoul(arg, &p, 0);
1551 		if (*p == '\0') {
1552 			set_legacy_value(val, v);
1553 			return;
1554 		}
1555 		if (inet_pton(AF_INET, arg, &val) == 1) {
1556 			set_legacy_value(ntohl(val), v);
1557 			return;
1558 		}
1559 		/* Try hostname */
1560 		if (lookup_host(arg, &ipaddr) == 0) {
1561 			set_legacy_value(ntohl(ipaddr.s_addr), v);
1562 			return;
1563 		}
1564 		errx(EX_OSERR, "Unable to parse value %s", arg);
1565 	}
1566 
1567 	/*
1568 	 * Shorthands: handle single value if vmask consists
1569 	 * of numbers only. e.g.:
1570 	 * vmask = "fib,skipto" -> treat input "1" as "1,1"
1571 	 */
1572 
1573 	n = arg;
1574 	etype = NULL;
1575 	for (i = 1; i < (1u << 31); i *= 2) {
1576 		if ((flag = (vmask & i)) == 0)
1577 			continue;
1578 		vmask &= ~flag;
1579 
1580 		if ((comma = strchr(n, ',')) != NULL)
1581 			*comma = '\0';
1582 
1583 		switch (flag) {
1584 		case IPFW_VTYPE_TAG:
1585 			v->tag = strtol(n, &e, 10);
1586 			if (*e != '\0')
1587 				etype = "tag";
1588 			break;
1589 		case IPFW_VTYPE_PIPE:
1590 			v->pipe = strtol(n, &e, 10);
1591 			if (*e != '\0')
1592 				etype = "pipe";
1593 			break;
1594 		case IPFW_VTYPE_DIVERT:
1595 			v->divert = strtol(n, &e, 10);
1596 			if (*e != '\0')
1597 				etype = "divert";
1598 			break;
1599 		case IPFW_VTYPE_SKIPTO:
1600 			v->skipto = strtol(n, &e, 10);
1601 			if (*e != '\0')
1602 				etype = "skipto";
1603 			break;
1604 		case IPFW_VTYPE_NETGRAPH:
1605 			v->netgraph = strtol(n, &e, 10);
1606 			if (*e != '\0')
1607 				etype = "netgraph";
1608 			break;
1609 		case IPFW_VTYPE_FIB:
1610 			v->fib = strtol(n, &e, 10);
1611 			if (*e != '\0')
1612 				etype = "fib";
1613 			break;
1614 		case IPFW_VTYPE_NAT:
1615 			v->nat = strtol(n, &e, 10);
1616 			if (*e != '\0')
1617 				etype = "nat";
1618 			break;
1619 		case IPFW_VTYPE_LIMIT:
1620 			v->limit = strtol(n, &e, 10);
1621 			if (*e != '\0')
1622 				etype = "limit";
1623 			break;
1624 		case IPFW_VTYPE_NH4:
1625 			if (strchr(n, '.') != NULL &&
1626 			    inet_pton(AF_INET, n, &a4) == 1) {
1627 				v->nh4 = ntohl(a4);
1628 				break;
1629 			}
1630 			if (lookup_host(n, &ipaddr) == 0) {
1631 				v->nh4 = ntohl(ipaddr.s_addr);
1632 				break;
1633 			}
1634 			etype = "ipv4";
1635 			break;
1636 		case IPFW_VTYPE_DSCP:
1637 			if (isalpha(*n)) {
1638 				if ((dval = match_token(f_ipdscp, n)) != -1) {
1639 					v->dscp = dval;
1640 					break;
1641 				} else
1642 					etype = "DSCP code";
1643 			} else {
1644 				v->dscp = strtol(n, &e, 10);
1645 				if (v->dscp > 63 || *e != '\0')
1646 					etype = "DSCP value";
1647 			}
1648 			break;
1649 		case IPFW_VTYPE_NH6:
1650 			if (strchr(n, ':') != NULL) {
1651 				memset(&hints, 0, sizeof(hints));
1652 				hints.ai_family = AF_INET6;
1653 				hints.ai_flags = AI_NUMERICHOST;
1654 				if (getaddrinfo(n, NULL, &hints, &res) == 0) {
1655 					v->nh6 = ((struct sockaddr_in6 *)
1656 					    res->ai_addr)->sin6_addr;
1657 					v->zoneid = ((struct sockaddr_in6 *)
1658 					    res->ai_addr)->sin6_scope_id;
1659 					freeaddrinfo(res);
1660 					break;
1661 				}
1662 			}
1663 			etype = "ipv6";
1664 			break;
1665 		}
1666 
1667 		if (etype != NULL)
1668 			errx(EX_USAGE, "Unable to parse %s as %s", n, etype);
1669 
1670 		if (comma != NULL)
1671 			*comma++ = ',';
1672 
1673 		if ((n = comma) != NULL)
1674 			continue;
1675 
1676 		/* End of input. */
1677 		if (vmask != 0)
1678 			errx(EX_USAGE, "Not enough fields inside value");
1679 	}
1680 }
1681 
1682 /*
1683  * Compare table names.
1684  * Honor number comparison.
1685  */
1686 static int
1687 tablename_cmp(const void *a, const void *b)
1688 {
1689 	const ipfw_xtable_info *ia, *ib;
1690 
1691 	ia = (const ipfw_xtable_info *)a;
1692 	ib = (const ipfw_xtable_info *)b;
1693 
1694 	return (stringnum_cmp(ia->tablename, ib->tablename));
1695 }
1696 
1697 /*
1698  * Retrieves table list from kernel,
1699  * optionally sorts it and calls requested function for each table.
1700  * Returns 0 on success.
1701  */
1702 static int
1703 tables_foreach(table_cb_t *f, void *arg, int sort)
1704 {
1705 	ipfw_obj_lheader *olh;
1706 	ipfw_xtable_info *info;
1707 	size_t sz;
1708 	uint32_t i;
1709 	int error;
1710 
1711 	/* Start with reasonable default */
1712 	sz = sizeof(*olh) + 16 * sizeof(ipfw_xtable_info);
1713 
1714 	for (;;) {
1715 		if ((olh = calloc(1, sz)) == NULL)
1716 			return (ENOMEM);
1717 
1718 		olh->size = sz;
1719 		if (do_get3(IP_FW_TABLES_XLIST, &olh->opheader, &sz) != 0) {
1720 			sz = olh->size;
1721 			free(olh);
1722 			if (errno != ENOMEM)
1723 				return (errno);
1724 			continue;
1725 		}
1726 
1727 		if (sort != 0)
1728 			qsort(olh + 1, olh->count, olh->objsize,
1729 			    tablename_cmp);
1730 
1731 		info = (ipfw_xtable_info *)(olh + 1);
1732 		for (i = 0; i < olh->count; i++) {
1733 			if (g_co.use_set == 0 || info->set == g_co.use_set - 1)
1734 				error = f(info, arg);
1735 			info = (ipfw_xtable_info *)((caddr_t)info +
1736 			    olh->objsize);
1737 		}
1738 		free(olh);
1739 		break;
1740 	}
1741 	return (0);
1742 }
1743 
1744 
1745 /*
1746  * Retrieves all entries for given table @i in
1747  * eXtended format. Allocate buffer large enough
1748  * to store result. Called needs to free it later.
1749  *
1750  * Returns 0 on success.
1751  */
1752 static int
1753 table_do_get_list(ipfw_xtable_info *i, ipfw_obj_header **poh)
1754 {
1755 	ipfw_obj_header *oh;
1756 	size_t sz;
1757 	int c;
1758 
1759 	sz = 0;
1760 	oh = NULL;
1761 	for (c = 0; c < 8; c++) {
1762 		if (sz < i->size)
1763 			sz = i->size + 44;
1764 		if (oh != NULL)
1765 			free(oh);
1766 		if ((oh = calloc(1, sz)) == NULL)
1767 			continue;
1768 		table_fill_objheader(oh, i);
1769 		oh->opheader.version = 1; /* Current version */
1770 		if (do_get3(IP_FW_TABLE_XLIST, &oh->opheader, &sz) == 0) {
1771 			*poh = oh;
1772 			return (0);
1773 		}
1774 
1775 		if (errno != ENOMEM)
1776 			break;
1777 	}
1778 	free(oh);
1779 
1780 	return (errno);
1781 }
1782 
1783 /*
1784  * Shows all entries from @oh in human-readable format
1785  */
1786 static void
1787 table_show_list(ipfw_obj_header *oh, int need_header)
1788 {
1789 	ipfw_obj_tentry *tent;
1790 	uint32_t count;
1791 	ipfw_xtable_info *i;
1792 
1793 	i = (ipfw_xtable_info *)(oh + 1);
1794 	tent = (ipfw_obj_tentry *)(i + 1);
1795 
1796 	if (need_header)
1797 		printf("--- table(%s), set(%u) ---\n", i->tablename, i->set);
1798 
1799 	count = i->count;
1800 	while (count > 0) {
1801 		table_show_entry(i, tent);
1802 		tent = (ipfw_obj_tentry *)((caddr_t)tent + tent->head.length);
1803 		count--;
1804 	}
1805 }
1806 
1807 static void
1808 table_show_value(char *buf, size_t bufsize, ipfw_table_value *v,
1809     uint32_t vmask, int print_ip)
1810 {
1811 	char abuf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1812 	struct sockaddr_in6 sa6;
1813 	uint32_t flag, i, l;
1814 	size_t sz;
1815 	struct in_addr a4;
1816 
1817 	sz = bufsize;
1818 
1819 	/*
1820 	 * Some shorthands for printing values:
1821 	 * legacy assumes all values are equal, so keep the first one.
1822 	 */
1823 	if (vmask == IPFW_VTYPE_LEGACY) {
1824 		if (print_ip != 0) {
1825 			flag = htonl(v->tag);
1826 			inet_ntop(AF_INET, &flag, buf, sz);
1827 		} else
1828 			snprintf(buf, sz, "%u", v->tag);
1829 		return;
1830 	}
1831 
1832 	for (i = 1; i < (1u << 31); i *= 2) {
1833 		if ((flag = (vmask & i)) == 0)
1834 			continue;
1835 		l = 0;
1836 
1837 		switch (flag) {
1838 		case IPFW_VTYPE_TAG:
1839 			l = snprintf(buf, sz, "%u,", v->tag);
1840 			break;
1841 		case IPFW_VTYPE_PIPE:
1842 			l = snprintf(buf, sz, "%u,", v->pipe);
1843 			break;
1844 		case IPFW_VTYPE_DIVERT:
1845 			l = snprintf(buf, sz, "%d,", v->divert);
1846 			break;
1847 		case IPFW_VTYPE_SKIPTO:
1848 			l = snprintf(buf, sz, "%d,", v->skipto);
1849 			break;
1850 		case IPFW_VTYPE_NETGRAPH:
1851 			l = snprintf(buf, sz, "%u,", v->netgraph);
1852 			break;
1853 		case IPFW_VTYPE_FIB:
1854 			l = snprintf(buf, sz, "%u,", v->fib);
1855 			break;
1856 		case IPFW_VTYPE_NAT:
1857 			l = snprintf(buf, sz, "%u,", v->nat);
1858 			break;
1859 		case IPFW_VTYPE_LIMIT:
1860 			l = snprintf(buf, sz, "%u,", v->limit);
1861 			break;
1862 		case IPFW_VTYPE_NH4:
1863 			a4.s_addr = htonl(v->nh4);
1864 			inet_ntop(AF_INET, &a4, abuf, sizeof(abuf));
1865 			l = snprintf(buf, sz, "%s,", abuf);
1866 			break;
1867 		case IPFW_VTYPE_DSCP:
1868 			l = snprintf(buf, sz, "%d,", v->dscp);
1869 			break;
1870 		case IPFW_VTYPE_NH6:
1871 			sa6.sin6_family = AF_INET6;
1872 			sa6.sin6_len = sizeof(sa6);
1873 			sa6.sin6_addr = v->nh6;
1874 			sa6.sin6_port = 0;
1875 			sa6.sin6_scope_id = v->zoneid;
1876 			if (getnameinfo((const struct sockaddr *)&sa6,
1877 			    sa6.sin6_len, abuf, sizeof(abuf), NULL, 0,
1878 			    NI_NUMERICHOST) == 0)
1879 				l = snprintf(buf, sz, "%s,", abuf);
1880 			break;
1881 		}
1882 
1883 		buf += l;
1884 		sz -= l;
1885 	}
1886 
1887 	if (sz != bufsize)
1888 		*(buf - 1) = '\0';
1889 }
1890 
1891 static void
1892 table_show_entry(ipfw_xtable_info *i, ipfw_obj_tentry *tent)
1893 {
1894 	char tbuf[128], pval[128];
1895 	const char *comma;
1896 	const u_char *mac;
1897 	void *paddr;
1898 	struct tflow_entry *tfe;
1899 
1900 	table_show_value(pval, sizeof(pval), &tent->v.value, i->vmask,
1901 	    g_co.do_value_as_ip);
1902 
1903 	switch (i->type) {
1904 	case IPFW_TABLE_ADDR:
1905 		/* IPv4 or IPv6 prefixes */
1906 		inet_ntop(tent->subtype, &tent->k, tbuf, sizeof(tbuf));
1907 		printf("%s/%u %s\n", tbuf, tent->masklen, pval);
1908 		break;
1909 	case IPFW_TABLE_MAC:
1910 		/* MAC prefixes */
1911 		mac = tent->k.mac;
1912 		printf("%02x:%02x:%02x:%02x:%02x:%02x/%u %s\n",
1913 		    mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
1914 		    tent->masklen, pval);
1915 		break;
1916 	case IPFW_TABLE_INTERFACE:
1917 		/* Interface names */
1918 		printf("%s %s\n", tent->k.iface, pval);
1919 		break;
1920 	case IPFW_TABLE_NUMBER:
1921 		/* numbers */
1922 		printf("%u %s\n", tent->k.key, pval);
1923 		break;
1924 	case IPFW_TABLE_FLOW:
1925 		/* flows */
1926 		tfe = &tent->k.flow;
1927 		comma = "";
1928 
1929 		if ((i->tflags & IPFW_TFFLAG_SRCIP) != 0) {
1930 			if (tfe->af == AF_INET)
1931 				paddr = &tfe->a.a4.sip;
1932 			else
1933 				paddr = &tfe->a.a6.sip6;
1934 
1935 			inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1936 			printf("%s%s", comma, tbuf);
1937 			comma = ",";
1938 		}
1939 
1940 		if ((i->tflags & IPFW_TFFLAG_PROTO) != 0) {
1941 			printf("%s%d", comma, tfe->proto);
1942 			comma = ",";
1943 		}
1944 
1945 		if ((i->tflags & IPFW_TFFLAG_SRCPORT) != 0) {
1946 			printf("%s%d", comma, ntohs(tfe->sport));
1947 			comma = ",";
1948 		}
1949 		if ((i->tflags & IPFW_TFFLAG_DSTIP) != 0) {
1950 			if (tfe->af == AF_INET)
1951 				paddr = &tfe->a.a4.dip;
1952 			else
1953 				paddr = &tfe->a.a6.dip6;
1954 
1955 			inet_ntop(tfe->af, paddr, tbuf, sizeof(tbuf));
1956 			printf("%s%s", comma, tbuf);
1957 			comma = ",";
1958 		}
1959 
1960 		if ((i->tflags & IPFW_TFFLAG_DSTPORT) != 0) {
1961 			printf("%s%d", comma, ntohs(tfe->dport));
1962 			comma = ",";
1963 		}
1964 
1965 		printf(" %s\n", pval);
1966 	}
1967 }
1968 
1969 static int
1970 table_do_get_stdlist(uint16_t opcode, ipfw_obj_lheader **polh)
1971 {
1972 	ipfw_obj_lheader req, *olh;
1973 	size_t sz;
1974 
1975 	memset(&req, 0, sizeof(req));
1976 	sz = sizeof(req);
1977 
1978 	if (do_get3(opcode, &req.opheader, &sz) != 0)
1979 		if (errno != ENOMEM)
1980 			return (errno);
1981 
1982 	sz = req.size;
1983 	if ((olh = calloc(1, sz)) == NULL)
1984 		return (ENOMEM);
1985 
1986 	olh->size = sz;
1987 	if (do_get3(opcode, &olh->opheader, &sz) != 0) {
1988 		free(olh);
1989 		return (errno);
1990 	}
1991 
1992 	*polh = olh;
1993 	return (0);
1994 }
1995 
1996 static int
1997 table_do_get_algolist(ipfw_obj_lheader **polh)
1998 {
1999 
2000 	return (table_do_get_stdlist(IP_FW_TABLES_ALIST, polh));
2001 }
2002 
2003 static int
2004 table_do_get_vlist(ipfw_obj_lheader **polh)
2005 {
2006 
2007 	return (table_do_get_stdlist(IP_FW_TABLE_VLIST, polh));
2008 }
2009 
2010 void
2011 ipfw_list_ta(int ac __unused, char *av[] __unused)
2012 {
2013 	ipfw_obj_lheader *olh;
2014 	ipfw_ta_info *info;
2015 	const char *atype;
2016 	uint32_t i;
2017 	int error;
2018 
2019 	error = table_do_get_algolist(&olh);
2020 	if (error != 0)
2021 		err(EX_OSERR, "Unable to request algorithm list");
2022 
2023 	info = (ipfw_ta_info *)(olh + 1);
2024 	for (i = 0; i < olh->count; i++) {
2025 		if ((atype = match_value(tabletypes, info->type)) == NULL)
2026 			atype = "unknown";
2027 		printf("--- %s ---\n", info->algoname);
2028 		printf(" type: %s\n refcount: %u\n", atype, info->refcnt);
2029 
2030 		info = (ipfw_ta_info *)((caddr_t)info + olh->objsize);
2031 	}
2032 
2033 	free(olh);
2034 }
2035 
2036 
2037 /* Copy of current kernel table_value structure */
2038 struct _table_value {
2039 	uint32_t	tag;		/* O_TAG/O_TAGGED */
2040 	uint32_t	pipe;		/* O_PIPE/O_QUEUE */
2041 	uint16_t	divert;		/* O_DIVERT/O_TEE */
2042 	uint16_t	skipto;		/* skipto, CALLRET */
2043 	uint32_t	netgraph;	/* O_NETGRAPH/O_NGTEE */
2044 	uint32_t	fib;		/* O_SETFIB */
2045 	uint32_t	nat;		/* O_NAT */
2046 	uint32_t	nh4;
2047 	uint8_t		dscp;
2048 	uint8_t		spare0;
2049 	uint16_t	spare1;
2050 	/* -- 32 bytes -- */
2051 	struct in6_addr	nh6;
2052 	uint32_t	limit;		/* O_LIMIT */
2053 	uint32_t	zoneid;
2054 	uint64_t	refcnt;		/* Number of references */
2055 };
2056 
2057 static int
2058 compare_values(const void *_a, const void *_b)
2059 {
2060 	const struct _table_value *a, *b;
2061 
2062 	a = (const struct _table_value *)_a;
2063 	b = (const struct _table_value *)_b;
2064 
2065 	if (a->spare1 < b->spare1)
2066 		return (-1);
2067 	else if (a->spare1 > b->spare1)
2068 		return (1);
2069 
2070 	return (0);
2071 }
2072 
2073 void
2074 ipfw_list_values(int ac __unused, char *av[] __unused)
2075 {
2076 	char buf[128];
2077 	ipfw_obj_lheader *olh;
2078 	struct _table_value *v;
2079 	uint32_t i, vmask;
2080 	int error;
2081 
2082 	error = table_do_get_vlist(&olh);
2083 	if (error != 0)
2084 		err(EX_OSERR, "Unable to request value list");
2085 
2086 	vmask = 0x7FFFFFFF; /* Similar to IPFW_VTYPE_LEGACY */
2087 
2088 	table_print_valheader(buf, sizeof(buf), vmask);
2089 	printf("HEADER: %s\n", buf);
2090 	v = (struct _table_value *)(olh + 1);
2091 	qsort(v, olh->count, olh->objsize, compare_values);
2092 	for (i = 0; i < olh->count; i++) {
2093 		table_show_value(buf, sizeof(buf), (ipfw_table_value *)v,
2094 		    vmask, 0);
2095 		printf("[%u] refs=%lu %s\n", v->spare1, (u_long)v->refcnt, buf);
2096 		v = (struct _table_value *)((caddr_t)v + olh->objsize);
2097 	}
2098 
2099 	free(olh);
2100 }
2101 
2102 int
2103 table_check_name(const char *tablename)
2104 {
2105 
2106 	if (ipfw_check_object_name(tablename) != 0)
2107 		return (EINVAL);
2108 	/* Restrict some 'special' names */
2109 	if (strcmp(tablename, "all") == 0)
2110 		return (EINVAL);
2111 	return (0);
2112 }
2113 
2114