ippool.c revision 353074
1/*	$FreeBSD: stable/11/contrib/ipfilter/tools/ippool.c 353074 2019-10-04 01:38:56Z cy $	*/
2
3/*
4 * Copyright (C) 2012 by Darren Reed.
5 *
6 * See the IPFILTER.LICENCE file for details on licencing.
7 */
8#include <sys/types.h>
9#include <sys/time.h>
10#include <sys/param.h>
11#include <sys/socket.h>
12# include <sys/cdefs.h>
13#include <sys/ioctl.h>
14
15#include <net/if.h>
16#include <netinet/in.h>
17
18#include <arpa/inet.h>
19
20#include <stdio.h>
21#include <fcntl.h>
22#include <stdlib.h>
23#include <string.h>
24#include <netdb.h>
25#include <ctype.h>
26#include <unistd.h>
27# include <nlist.h>
28
29#include "ipf.h"
30#include "netinet/ipl.h"
31#include "netinet/ip_lookup.h"
32#include "netinet/ip_pool.h"
33#include "netinet/ip_htable.h"
34#include "kmem.h"
35
36
37extern	int	ippool_yyparse __P((void));
38extern	int	ippool_yydebug;
39extern	FILE	*ippool_yyin;
40extern	char	*optarg;
41extern	int	lineNum;
42
43void	usage __P((char *));
44int	main __P((int, char **));
45int	poolcommand __P((int, int, char *[]));
46int	poolnodecommand __P((int, int, char *[]));
47int	loadpoolfile __P((int, char *[], char *));
48int	poollist __P((int, char *[]));
49void	poollist_dead __P((int, char *, int, char *, char *));
50void	poollist_live __P((int, char *, int, int));
51int	poolflush __P((int, char *[]));
52int	poolstats __P((int, char *[]));
53int	gettype __P((char *, u_int *));
54int	getrole __P((char *));
55int	setnodeaddr __P((int, int, void *ptr, char *arg));
56void	showpools_live __P((int, int, ipf_pool_stat_t *, char *));
57void	showhashs_live __P((int, int, iphtstat_t *, char *));
58void	showdstls_live __P((int, int, ipf_dstl_stat_t *, char *));
59
60int	opts = 0;
61int	fd = -1;
62int	use_inet6 = 0;
63wordtab_t *pool_fields = NULL;
64int	nohdrfields = 0;
65
66
67void
68usage(prog)
69	char *prog;
70{
71	fprintf(stderr, "Usage:\t%s\n", prog);
72	fprintf(stderr, "\t-a [-dnv] [-m <name>] [-o <role>] [-t type] [-T ttl] -i <ipaddr>[/netmask]\n");
73	fprintf(stderr, "\t-A [-dnv] [-m <name>] [-o <role>] [-S <seed>] [-t <type>]\n");
74	fprintf(stderr, "\t-f <file> [-dnuv]\n");
75	fprintf(stderr, "\t-F [-dv] [-o <role>] [-t <type>]\n");
76	fprintf(stderr, "\t-l [-dv] [-m <name>] [-t <type>] [-O <fields>]\n");
77	fprintf(stderr, "\t-r [-dnv] [-m <name>] [-o <role>] [-t type] -i <ipaddr>[/netmask]\n");
78	fprintf(stderr, "\t-R [-dnv] [-m <name>] [-o <role>] [-t <type>]\n");
79	fprintf(stderr, "\t-s [-dtv] [-M <core>] [-N <namelist>]\n");
80	exit(1);
81}
82
83
84int
85main(argc, argv)
86	int argc;
87	char *argv[];
88{
89	int err = 1;
90
91	if (argc < 2)
92		usage(argv[0]);
93
94	assigndefined(getenv("IPPOOL_PREDEFINED"));
95
96	switch (getopt(argc, argv, "aAf:FlnrRsv"))
97	{
98	case 'a' :
99		err = poolnodecommand(0, argc, argv);
100		break;
101	case 'A' :
102		err = poolcommand(0, argc, argv);
103		break;
104	case 'f' :
105		err = loadpoolfile(argc, argv, optarg);
106		break;
107	case 'F' :
108		err = poolflush(argc, argv);
109		break;
110	case 'l' :
111		err = poollist(argc, argv);
112		break;
113	case 'n' :
114		opts |= OPT_DONOTHING|OPT_DONTOPEN;
115		break;
116	case 'r' :
117		err = poolnodecommand(1, argc, argv);
118		break;
119	case 'R' :
120		err = poolcommand(1, argc, argv);
121		break;
122	case 's' :
123		err = poolstats(argc, argv);
124		break;
125	case 'v' :
126		opts |= OPT_VERBOSE;
127		break;
128	default :
129		exit(1);
130	}
131
132	if (err != 0)
133		exit(1);
134	return 0;
135}
136
137
138int
139poolnodecommand(remove, argc, argv)
140	int remove, argc;
141	char *argv[];
142{
143	int err = 0, c, ipset, role, type = IPLT_POOL, ttl = 0;
144	char *poolname = NULL;
145	ip_pool_node_t pnode;
146	iphtent_t hnode;
147	void *ptr = &pnode;
148
149	ipset = 0;
150	role = IPL_LOGIPF;
151	bzero((char *)&pnode, sizeof(pnode));
152	bzero((char *)&hnode, sizeof(hnode));
153
154	while ((c = getopt(argc, argv, "di:m:no:Rt:T:v")) != -1)
155		switch (c)
156		{
157		case 'd' :
158			opts |= OPT_DEBUG;
159			ippool_yydebug++;
160			break;
161		case 'i' :
162			if (setnodeaddr(type, role, ptr, optarg) == 0)
163				ipset = 1;
164			break;
165		case 'm' :
166			poolname = optarg;
167			break;
168		case 'n' :
169			opts |= OPT_DONOTHING|OPT_DONTOPEN;
170			break;
171		case 'o' :
172			if (ipset == 1) {
173				fprintf(stderr,
174					"cannot set role after ip address\n");
175				return -1;
176			}
177			role = getrole(optarg);
178			if (role == IPL_LOGNONE)
179				return -1;
180			break;
181		case 'R' :
182			opts |= OPT_NORESOLVE;
183			break;
184		case 't' :
185			if (ipset == 1) {
186				fprintf(stderr,
187					"cannot set type after ip address\n");
188				return -1;
189			}
190			type = gettype(optarg, NULL);
191			switch (type) {
192			case IPLT_NONE :
193				fprintf(stderr, "unknown type '%s'\n", optarg);
194				return -1;
195			case IPLT_HASH :
196				ptr = &hnode;
197				break;
198			case IPLT_POOL :
199			default :
200				break;
201			}
202			break;
203		case 'T' :
204			ttl = atoi(optarg);
205			if (ttl < 0) {
206				fprintf(stderr, "cannot set negative ttl\n");
207				return -1;
208			}
209			break;
210		case 'v' :
211			opts |= OPT_VERBOSE;
212			break;
213		}
214
215	if (argv[optind] != NULL && ipset == 0) {
216		if (setnodeaddr(type, role, ptr, argv[optind]) == 0)
217			ipset = 1;
218	}
219
220	if (opts & OPT_DEBUG)
221		fprintf(stderr, "poolnodecommand: opts = %#x\n", opts);
222
223	if (ipset == 0) {
224		fprintf(stderr, "no IP address given with -i\n");
225		return -1;
226	}
227
228	if (poolname == NULL) {
229		fprintf(stderr, "poolname not given with add/remove node\n");
230		return -1;
231	}
232
233	switch (type) {
234	case IPLT_POOL :
235		if (remove == 0)
236			err = load_poolnode(role, poolname, &pnode, ttl, ioctl);
237		else
238			err = remove_poolnode(role, poolname, &pnode, ioctl);
239		break;
240	case IPLT_HASH :
241		if (remove == 0)
242			err = load_hashnode(role, poolname, &hnode, ttl, ioctl);
243		else
244			err = remove_hashnode(role, poolname, &hnode, ioctl);
245		break;
246	default :
247		break;
248	}
249	return err;
250}
251
252
253int
254poolcommand(remove, argc, argv)
255	int remove, argc;
256	char *argv[];
257{
258	int type, role, c, err;
259	char *poolname;
260	iphtable_t iph;
261	ip_pool_t pool;
262
263	err = 1;
264	role = 0;
265	type = 0;
266	poolname = NULL;
267	role = IPL_LOGIPF;
268	bzero((char *)&iph, sizeof(iph));
269	bzero((char *)&pool, sizeof(pool));
270
271	while ((c = getopt(argc, argv, "dm:no:RSv")) != -1)
272		switch (c)
273		{
274		case 'd' :
275			opts |= OPT_DEBUG;
276			ippool_yydebug++;
277			break;
278		case 'm' :
279			poolname = optarg;
280			break;
281		case 'n' :
282			opts |= OPT_DONOTHING|OPT_DONTOPEN;
283			break;
284		case 'o' :
285			role = getrole(optarg);
286			if (role == IPL_LOGNONE) {
287				fprintf(stderr, "unknown role '%s'\n", optarg);
288				return -1;
289			}
290			break;
291		case 'R' :
292			opts |= OPT_NORESOLVE;
293			break;
294		case 'S' :
295			iph.iph_seed = atoi(optarg);
296			break;
297		case 'v' :
298			opts |= OPT_VERBOSE;
299			break;
300		}
301
302	if (opts & OPT_DEBUG)
303		fprintf(stderr, "poolcommand: opts = %#x\n", opts);
304
305	if (poolname == NULL) {
306		fprintf(stderr, "poolname not given with add/remove pool\n");
307		return -1;
308	}
309
310	type = gettype(argv[optind], &iph.iph_type);
311	if (type == IPLT_NONE) {
312		fprintf(stderr, "unknown type '%s'\n", argv[optind]);
313		return -1;
314	}
315
316	if (type == IPLT_HASH) {
317		strncpy(iph.iph_name, poolname, sizeof(iph.iph_name));
318		iph.iph_name[sizeof(iph.iph_name) - 1] = '\0';
319		iph.iph_unit = role;
320	} else if (type == IPLT_POOL) {
321		strncpy(pool.ipo_name, poolname, sizeof(pool.ipo_name));
322		pool.ipo_name[sizeof(pool.ipo_name) - 1] = '\0';
323		pool.ipo_unit = role;
324	}
325
326	if (remove == 0) {
327		switch (type)
328		{
329		case IPLT_HASH :
330			err = load_hash(&iph, NULL, ioctl);
331			break;
332		case IPLT_POOL :
333			err = load_pool(&pool, ioctl);
334			break;
335		}
336	} else {
337		switch (type)
338		{
339		case IPLT_HASH :
340			err = remove_hash(&iph, ioctl);
341			break;
342		case IPLT_POOL :
343			err = remove_pool(&pool, ioctl);
344			break;
345		}
346	}
347	return err;
348}
349
350
351int
352loadpoolfile(argc, argv, infile)
353	int argc;
354	char *argv[], *infile;
355{
356	int c;
357
358	infile = optarg;
359
360	while ((c = getopt(argc, argv, "dnRuv")) != -1)
361		switch (c)
362		{
363		case 'd' :
364			opts |= OPT_DEBUG;
365			ippool_yydebug++;
366			break;
367		case 'n' :
368			opts |= OPT_DONOTHING|OPT_DONTOPEN;
369			break;
370		case 'R' :
371			opts |= OPT_NORESOLVE;
372			break;
373		case 'u' :
374			opts |= OPT_REMOVE;
375			break;
376		case 'v' :
377			opts |= OPT_VERBOSE;
378			break;
379		}
380
381	if (opts & OPT_DEBUG)
382		fprintf(stderr, "loadpoolfile: opts = %#x\n", opts);
383
384	if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN)) && (fd == -1)) {
385		fd = open(IPLOOKUP_NAME, O_RDWR);
386		if (fd == -1) {
387			perror("open(IPLOOKUP_NAME)");
388			exit(1);
389		}
390	}
391
392	if (ippool_parsefile(fd, infile, ioctl) != 0)
393		return -1;
394	return 0;
395}
396
397
398int
399poolstats(argc, argv)
400	int argc;
401	char *argv[];
402{
403	int c, type, role, live_kernel;
404	ipf_pool_stat_t plstat;
405	ipf_dstl_stat_t dlstat;
406	char *kernel, *core;
407	iphtstat_t htstat;
408	iplookupop_t op;
409
410	core = NULL;
411	kernel = NULL;
412	live_kernel = 1;
413	type = IPLT_ALL;
414	role = IPL_LOGALL;
415
416	bzero((char *)&op, sizeof(op));
417
418	while ((c = getopt(argc, argv, "dM:N:o:t:v")) != -1)
419		switch (c)
420		{
421		case 'd' :
422			opts |= OPT_DEBUG;
423			break;
424		case 'M' :
425			live_kernel = 0;
426			core = optarg;
427			break;
428		case 'N' :
429			live_kernel = 0;
430			kernel = optarg;
431			break;
432		case 'o' :
433			role = getrole(optarg);
434			if (role == IPL_LOGNONE) {
435				fprintf(stderr, "unknown role '%s'\n", optarg);
436				return -1;
437			}
438			break;
439		case 't' :
440			type = gettype(optarg, NULL);
441			if (type != IPLT_POOL) {
442				fprintf(stderr,
443					"-s not supported for this type yet\n");
444				return -1;
445			}
446			break;
447		case 'v' :
448			opts |= OPT_VERBOSE;
449			break;
450		}
451
452	if (opts & OPT_DEBUG)
453		fprintf(stderr, "poolstats: opts = %#x\n", opts);
454
455	if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN)) && (fd == -1)) {
456		fd = open(IPLOOKUP_NAME, O_RDWR);
457		if (fd == -1) {
458			perror("open(IPLOOKUP_NAME)");
459			exit(1);
460		}
461	}
462
463	if (type == IPLT_ALL || type == IPLT_POOL) {
464		op.iplo_type = IPLT_POOL;
465		op.iplo_struct = &plstat;
466		op.iplo_size = sizeof(plstat);
467		if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN))) {
468			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
469			if (c == -1) {
470				ipferror(fd, "ioctl(S0IOCLOOKUPSTAT)");
471				return -1;
472			}
473			printf("%lu\taddress pools\n", plstat.ipls_pools);
474			printf("%lu\taddress pool nodes\n", plstat.ipls_nodes);
475		}
476	}
477
478	if (type == IPLT_ALL || type == IPLT_HASH) {
479		op.iplo_type = IPLT_HASH;
480		op.iplo_struct = &htstat;
481		op.iplo_size = sizeof(htstat);
482		if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN))) {
483			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
484			if (c == -1) {
485				ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
486				return -1;
487			}
488			printf("%lu\thash tables\n", htstat.iphs_numtables);
489			printf("%lu\thash table nodes\n", htstat.iphs_numnodes);
490			printf("%lu\thash table no memory \n",
491				htstat.iphs_nomem);
492		}
493	}
494
495	if (type == IPLT_ALL || type == IPLT_DSTLIST) {
496		op.iplo_type = IPLT_DSTLIST;
497		op.iplo_struct = &dlstat;
498		op.iplo_size = sizeof(dlstat);
499		if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN))) {
500			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
501			if (c == -1) {
502				ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
503				return -1;
504			}
505			printf("%u\tdestination lists\n",
506			       dlstat.ipls_numlists);
507			printf("%u\tdestination list nodes\n",
508			       dlstat.ipls_numnodes);
509			printf("%lu\tdestination list no memory\n",
510			       dlstat.ipls_nomem);
511			printf("%u\tdestination list zombies\n",
512			       dlstat.ipls_numdereflists);
513			printf("%u\tdesetination list node zombies\n",
514			       dlstat.ipls_numderefnodes);
515		}
516	}
517	return 0;
518}
519
520
521int
522poolflush(argc, argv)
523	int argc;
524	char *argv[];
525{
526	int c, role, type, arg;
527	iplookupflush_t flush;
528
529	arg = IPLT_ALL;
530	type = IPLT_ALL;
531	role = IPL_LOGALL;
532
533	while ((c = getopt(argc, argv, "do:t:v")) != -1)
534		switch (c)
535		{
536		case 'd' :
537			opts |= OPT_DEBUG;
538			break;
539		case 'o' :
540			role = getrole(optarg);
541			if (role == IPL_LOGNONE) {
542				fprintf(stderr, "unknown role '%s'\n", optarg);
543				return -1;
544			}
545			break;
546		case 't' :
547			type = gettype(optarg, NULL);
548			if (type == IPLT_NONE) {
549				fprintf(stderr, "unknown type '%s'\n", optarg);
550				return -1;
551			}
552			break;
553		case 'v' :
554			opts |= OPT_VERBOSE;
555			break;
556		}
557
558	if (opts & OPT_DEBUG)
559		fprintf(stderr, "poolflush: opts = %#x\n", opts);
560
561	if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN)) && (fd == -1)) {
562		fd = open(IPLOOKUP_NAME, O_RDWR);
563		if (fd == -1) {
564			perror("open(IPLOOKUP_NAME)");
565			exit(1);
566		}
567	}
568
569	bzero((char *)&flush, sizeof(flush));
570	flush.iplf_type = type;
571	flush.iplf_unit = role;
572	flush.iplf_arg = arg;
573
574	if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN))) {
575		if (ioctl(fd, SIOCLOOKUPFLUSH, &flush) == -1) {
576			ipferror(fd, "ioctl(SIOCLOOKUPFLUSH)");
577			exit(1);
578		}
579
580	}
581	printf("%u object%s flushed\n", flush.iplf_count,
582	       (flush.iplf_count == 1) ? "" : "s");
583
584	return 0;
585}
586
587
588int
589getrole(rolename)
590	char *rolename;
591{
592	int role;
593
594	if (!strcasecmp(rolename, "ipf")) {
595		role = IPL_LOGIPF;
596#if 0
597	} else if (!strcasecmp(rolename, "nat")) {
598		role = IPL_LOGNAT;
599	} else if (!strcasecmp(rolename, "state")) {
600		role = IPL_LOGSTATE;
601	} else if (!strcasecmp(rolename, "auth")) {
602		role = IPL_LOGAUTH;
603	} else if (!strcasecmp(rolename, "sync")) {
604		role = IPL_LOGSYNC;
605	} else if (!strcasecmp(rolename, "scan")) {
606		role = IPL_LOGSCAN;
607	} else if (!strcasecmp(rolename, "pool")) {
608		role = IPL_LOGLOOKUP;
609	} else if (!strcasecmp(rolename, "count")) {
610		role = IPL_LOGCOUNT;
611#endif
612	} else {
613		role = IPL_LOGNONE;
614	}
615
616	return role;
617}
618
619
620int
621gettype(typename, minor)
622	char *typename;
623	u_int *minor;
624{
625	int type;
626
627	if (!strcasecmp(typename, "tree") || !strcasecmp(typename, "pool")) {
628		type = IPLT_POOL;
629	} else if (!strcasecmp(typename, "hash")) {
630		type = IPLT_HASH;
631		if (minor != NULL)
632			*minor = IPHASH_LOOKUP;
633	} else if (!strcasecmp(typename, "group-map")) {
634		type = IPLT_HASH;
635		if (minor != NULL)
636			*minor = IPHASH_GROUPMAP;
637	} else {
638		type = IPLT_NONE;
639	}
640	return type;
641}
642
643
644int
645poollist(argc, argv)
646	int argc;
647	char *argv[];
648{
649	char *kernel, *core, *poolname;
650	int c, role, type, live_kernel;
651	iplookupop_t op;
652
653	core = NULL;
654	kernel = NULL;
655	live_kernel = 1;
656	type = IPLT_ALL;
657	poolname = NULL;
658	role = IPL_LOGALL;
659
660	while ((c = getopt(argc, argv, "dm:M:N:o:t:v")) != -1)
661		switch (c)
662		{
663		case 'd' :
664			opts |= OPT_DEBUG;
665			break;
666		case 'm' :
667			poolname = optarg;
668			break;
669		case 'M' :
670			live_kernel = 0;
671			core = optarg;
672			break;
673		case 'N' :
674			live_kernel = 0;
675			kernel = optarg;
676			break;
677		case 'o' :
678			role = getrole(optarg);
679			if (role == IPL_LOGNONE) {
680				fprintf(stderr, "unknown role '%s'\n", optarg);
681				return -1;
682			}
683			break;
684		case 'O' :
685			pool_fields = parsefields(poolfields, optarg);
686			break;
687		case 't' :
688			type = gettype(optarg, NULL);
689			if (type == IPLT_NONE) {
690				fprintf(stderr, "unknown type '%s'\n", optarg);
691				return -1;
692			}
693			break;
694		case 'v' :
695			opts |= OPT_VERBOSE;
696			break;
697		}
698
699	if (opts & OPT_DEBUG)
700		fprintf(stderr, "poollist: opts = %#x\n", opts);
701
702	if (!(opts & (OPT_DONOTHING|OPT_DONTOPEN)) && (fd == -1)) {
703		fd = open(IPLOOKUP_NAME, O_RDWR);
704		if (fd == -1) {
705			perror("open(IPLOOKUP_NAME)");
706			exit(1);
707		}
708	}
709
710	bzero((char *)&op, sizeof(op));
711	if (poolname != NULL) {
712		strncpy(op.iplo_name, poolname, sizeof(op.iplo_name));
713		op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
714	}
715	op.iplo_unit = role;
716
717	if (live_kernel)
718		poollist_live(role, poolname, type, fd);
719	else
720		poollist_dead(role, poolname, type, kernel, core);
721	return 0;
722}
723
724
725void
726poollist_dead(role, poolname, type, kernel, core)
727	int role, type;
728	char *poolname, *kernel, *core;
729{
730	iphtable_t *hptr;
731	ip_pool_t *ptr;
732
733	if (openkmem(kernel, core) == -1)
734		exit(-1);
735
736	if (type == IPLT_ALL || type == IPLT_POOL) {
737		ip_pool_t *pools[IPL_LOGSIZE];
738		struct nlist names[2] = { { "ip_pool_list" } , { "" } };
739
740		if (nlist(kernel, names) != 1)
741			return;
742
743		bzero(&pools, sizeof(pools));
744		if (kmemcpy((char *)&pools, names[0].n_value, sizeof(pools)))
745			return;
746
747		if (role != IPL_LOGALL) {
748			ptr = pools[role];
749			while (ptr != NULL) {
750				ptr = printpool(ptr, kmemcpywrap, poolname,
751						opts, pool_fields);
752			}
753		} else {
754			for (role = 0; role <= IPL_LOGMAX; role++) {
755				ptr = pools[role];
756				while (ptr != NULL) {
757					ptr = printpool(ptr, kmemcpywrap,
758							poolname, opts,
759							pool_fields);
760				}
761			}
762			role = IPL_LOGALL;
763		}
764	}
765	if (type == IPLT_ALL || type == IPLT_HASH) {
766		iphtable_t *tables[IPL_LOGSIZE];
767		struct nlist names[2] = { { "ipf_htables" } , { "" } };
768
769		if (nlist(kernel, names) != 1)
770			return;
771
772		bzero(&tables, sizeof(tables));
773		if (kmemcpy((char *)&tables, names[0].n_value, sizeof(tables)))
774			return;
775
776		if (role != IPL_LOGALL) {
777			hptr = tables[role];
778			while (hptr != NULL) {
779				hptr = printhash(hptr, kmemcpywrap,
780						 poolname, opts, pool_fields);
781			}
782		} else {
783			for (role = 0; role <= IPL_LOGMAX; role++) {
784				hptr = tables[role];
785				while (hptr != NULL) {
786					hptr = printhash(hptr, kmemcpywrap,
787							 poolname, opts,
788							 pool_fields);
789				}
790			}
791		}
792	}
793}
794
795
796void
797poollist_live(role, poolname, type, fd)
798	int role, type, fd;
799	char *poolname;
800{
801	ipf_pool_stat_t plstat;
802	iplookupop_t op;
803	int c;
804
805	if (type == IPLT_ALL || type == IPLT_POOL) {
806		op.iplo_type = IPLT_POOL;
807		op.iplo_size = sizeof(plstat);
808		op.iplo_struct = &plstat;
809		op.iplo_name[0] = '\0';
810		op.iplo_arg = 0;
811
812		if (role != IPL_LOGALL) {
813			op.iplo_unit = role;
814
815			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
816			if (c == -1) {
817				ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
818				return;
819			}
820
821			showpools_live(fd, role, &plstat, poolname);
822		} else {
823			for (role = -1; role <= IPL_LOGMAX; role++) {
824				op.iplo_unit = role;
825
826				c = ioctl(fd, SIOCLOOKUPSTAT, &op);
827				if (c == -1) {
828					ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
829					return;
830				}
831
832				showpools_live(fd, role, &plstat, poolname);
833			}
834
835			role = IPL_LOGALL;
836		}
837	}
838
839	if (type == IPLT_ALL || type == IPLT_HASH) {
840		iphtstat_t htstat;
841
842		op.iplo_type = IPLT_HASH;
843		op.iplo_size = sizeof(htstat);
844		op.iplo_struct = &htstat;
845		op.iplo_name[0] = '\0';
846		op.iplo_arg = 0;
847
848		if (role != IPL_LOGALL) {
849			op.iplo_unit = role;
850
851			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
852			if (c == -1) {
853				ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
854				return;
855			}
856			showhashs_live(fd, role, &htstat, poolname);
857		} else {
858			for (role = 0; role <= IPL_LOGMAX; role++) {
859
860				op.iplo_unit = role;
861				c = ioctl(fd, SIOCLOOKUPSTAT, &op);
862				if (c == -1) {
863					ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
864					return;
865				}
866
867				showhashs_live(fd, role, &htstat, poolname);
868			}
869			role = IPL_LOGALL;
870		}
871	}
872
873	if (type == IPLT_ALL || type == IPLT_DSTLIST) {
874		ipf_dstl_stat_t dlstat;
875
876		op.iplo_type = IPLT_DSTLIST;
877		op.iplo_size = sizeof(dlstat);
878		op.iplo_struct = &dlstat;
879		op.iplo_name[0] = '\0';
880		op.iplo_arg = 0;
881
882		if (role != IPL_LOGALL) {
883			op.iplo_unit = role;
884
885			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
886			if (c == -1) {
887				ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
888				return;
889			}
890			showdstls_live(fd, role, &dlstat, poolname);
891		} else {
892			for (role = 0; role <= IPL_LOGMAX; role++) {
893
894				op.iplo_unit = role;
895				c = ioctl(fd, SIOCLOOKUPSTAT, &op);
896				if (c == -1) {
897					ipferror(fd, "ioctl(SIOCLOOKUPSTAT)");
898					return;
899				}
900
901				showdstls_live(fd, role, &dlstat, poolname);
902			}
903			role = IPL_LOGALL;
904		}
905	}
906}
907
908
909void
910showpools_live(fd, role, plstp, poolname)
911	int fd, role;
912	ipf_pool_stat_t *plstp;
913	char *poolname;
914{
915	ipflookupiter_t iter;
916	ip_pool_t pool;
917	ipfobj_t obj;
918
919	obj.ipfo_rev = IPFILTER_VERSION;
920	obj.ipfo_type = IPFOBJ_LOOKUPITER;
921	obj.ipfo_size = sizeof(iter);
922	obj.ipfo_ptr = &iter;
923
924	iter.ili_type = IPLT_POOL;
925	iter.ili_otype = IPFLOOKUPITER_LIST;
926	iter.ili_ival = IPFGENITER_LOOKUP;
927	iter.ili_nitems = 1;
928	iter.ili_data = &pool;
929	iter.ili_unit = role;
930	*iter.ili_name = '\0';
931
932	bzero((char *)&pool, sizeof(pool));
933
934	while (plstp->ipls_list[role + 1] != NULL) {
935		if (ioctl(fd, SIOCLOOKUPITER, &obj)) {
936			ipferror(fd, "ioctl(SIOCLOOKUPITER)");
937			break;
938		}
939		if (((pool.ipo_flags & IPOOL_DELETE) == 0) ||
940		    ((opts & OPT_DEBUG) != 0))
941			printpool_live(&pool, fd, poolname, opts, pool_fields);
942
943		plstp->ipls_list[role + 1] = pool.ipo_next;
944	}
945}
946
947
948void
949showhashs_live(fd, role, htstp, poolname)
950	int fd, role;
951	iphtstat_t *htstp;
952	char *poolname;
953{
954	ipflookupiter_t iter;
955	iphtable_t table;
956	ipfobj_t obj;
957
958	obj.ipfo_rev = IPFILTER_VERSION;
959	obj.ipfo_type = IPFOBJ_LOOKUPITER;
960	obj.ipfo_size = sizeof(iter);
961	obj.ipfo_ptr = &iter;
962
963	iter.ili_type = IPLT_HASH;
964	iter.ili_otype = IPFLOOKUPITER_LIST;
965	iter.ili_ival = IPFGENITER_LOOKUP;
966	iter.ili_nitems = 1;
967	iter.ili_data = &table;
968	iter.ili_unit = role;
969	*iter.ili_name = '\0';
970
971	while (htstp->iphs_tables != NULL) {
972		if (ioctl(fd, SIOCLOOKUPITER, &obj)) {
973			ipferror(fd, "ioctl(SIOCLOOKUPITER)");
974			break;
975		}
976
977		printhash_live(&table, fd, poolname, opts, pool_fields);
978
979		htstp->iphs_tables = table.iph_next;
980	}
981}
982
983
984void
985showdstls_live(fd, role, dlstp, poolname)
986	int fd, role;
987	ipf_dstl_stat_t *dlstp;
988	char *poolname;
989{
990	ipflookupiter_t iter;
991	ippool_dst_t table;
992	ipfobj_t obj;
993
994	obj.ipfo_rev = IPFILTER_VERSION;
995	obj.ipfo_type = IPFOBJ_LOOKUPITER;
996	obj.ipfo_size = sizeof(iter);
997	obj.ipfo_ptr = &iter;
998
999	iter.ili_type = IPLT_DSTLIST;
1000	iter.ili_otype = IPFLOOKUPITER_LIST;
1001	iter.ili_ival = IPFGENITER_LOOKUP;
1002	iter.ili_nitems = 1;
1003	iter.ili_data = &table;
1004	iter.ili_unit = role;
1005	*iter.ili_name = '\0';
1006
1007	while (dlstp->ipls_list[role] != NULL) {
1008		if (ioctl(fd, SIOCLOOKUPITER, &obj)) {
1009			ipferror(fd, "ioctl(SIOCLOOKUPITER)");
1010			break;
1011		}
1012
1013		printdstl_live(&table, fd, poolname, opts, pool_fields);
1014
1015		dlstp->ipls_list[role] = table.ipld_next;
1016	}
1017}
1018
1019
1020int
1021setnodeaddr(int type, int role, void *ptr, char *arg)
1022{
1023	struct in_addr mask;
1024	char *s;
1025
1026	s = strchr(arg, '/');
1027	if (s == NULL)
1028		mask.s_addr = 0xffffffff;
1029	else if (strchr(s, '.') == NULL) {
1030		if (ntomask(AF_INET, atoi(s + 1), &mask.s_addr) != 0)
1031			return -1;
1032	} else {
1033		mask.s_addr = inet_addr(s + 1);
1034	}
1035	if (s != NULL)
1036		*s = '\0';
1037
1038	if (type == IPLT_POOL) {
1039		ip_pool_node_t *node = ptr;
1040
1041#ifdef USE_INET6
1042		if (node->ipn_addr.adf_family == AF_INET)
1043#endif
1044			node->ipn_addr.adf_len = offsetof(addrfamily_t,
1045							  adf_addr) +
1046						 sizeof(struct in_addr);
1047#ifdef USE_INET6
1048		else
1049			node->ipn_addr.adf_len = offsetof(addrfamily_t,
1050							  adf_addr) +
1051						 sizeof(struct in6_addr);
1052#endif
1053		node->ipn_addr.adf_addr.in4.s_addr = inet_addr(arg);
1054		node->ipn_mask.adf_len = node->ipn_addr.adf_len;
1055		node->ipn_mask.adf_addr.in4.s_addr = mask.s_addr;
1056	} else if (type == IPLT_HASH) {
1057		iphtent_t *node = ptr;
1058
1059		node->ipe_addr.in4.s_addr = inet_addr(arg);
1060		node->ipe_mask.in4.s_addr = mask.s_addr;
1061        	node->ipe_family = AF_INET;
1062        	node->ipe_unit = role;
1063	}
1064
1065	return 0;
1066}
1067