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