xref: /illumos-gate/usr/src/cmd/ipf/tools/ippool.c (revision d7c57852)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6f4b3ec61Sdh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
77c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
894bdecd9SRob Gulewich  *
994bdecd9SRob Gulewich  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
10*d7c57852SGary Mills  * Copyright 2017 Gary Mills
117c478bd9Sstevel@tonic-gate  */
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <sys/types.h>
147c478bd9Sstevel@tonic-gate #include <sys/time.h>
157c478bd9Sstevel@tonic-gate #include <sys/param.h>
167c478bd9Sstevel@tonic-gate #include <sys/socket.h>
177c478bd9Sstevel@tonic-gate #if defined(BSD) && (BSD >= 199306)
187c478bd9Sstevel@tonic-gate # include <sys/cdefs.h>
197c478bd9Sstevel@tonic-gate #endif
207c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <net/if.h>
237c478bd9Sstevel@tonic-gate #if __FreeBSD_version >= 300000
247c478bd9Sstevel@tonic-gate # include <net/if_var.h>
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <netdb.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
37f4b3ec61Sdh #include <nlist.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "ipf.h"
40f4b3ec61Sdh #include "netinet/ipl.h"
417c478bd9Sstevel@tonic-gate #include "netinet/ip_lookup.h"
427c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h"
437c478bd9Sstevel@tonic-gate #include "netinet/ip_htable.h"
447c478bd9Sstevel@tonic-gate #include "kmem.h"
4594bdecd9SRob Gulewich #include "ipfzone.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern	int	ippool_yyparse __P((void));
487c478bd9Sstevel@tonic-gate extern	int	ippool_yydebug;
497c478bd9Sstevel@tonic-gate extern	FILE	*ippool_yyin;
507c478bd9Sstevel@tonic-gate extern	char	*optarg;
517c478bd9Sstevel@tonic-gate extern	int	lineNum;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate void	showpools __P((ip_pool_stat_t *));
547c478bd9Sstevel@tonic-gate void	usage __P((char *));
557c478bd9Sstevel@tonic-gate int	main __P((int, char **));
567c478bd9Sstevel@tonic-gate int	poolcommand __P((int, int, char *[]));
577c478bd9Sstevel@tonic-gate int	poolnodecommand __P((int, int, char *[]));
587c478bd9Sstevel@tonic-gate int	loadpoolfile __P((int, char *[], char *));
597c478bd9Sstevel@tonic-gate int	poollist __P((int, char *[]));
607c478bd9Sstevel@tonic-gate int	poolflush __P((int, char *[]));
617c478bd9Sstevel@tonic-gate int	poolstats __P((int, char *[]));
627c478bd9Sstevel@tonic-gate int	gettype __P((char *, u_int *));
637c478bd9Sstevel@tonic-gate int	getrole __P((char *));
64f4b3ec61Sdh void	poollist_dead __P((int, char *, int, char *, char *));
65f4b3ec61Sdh void	showpools_live(int, int, ip_pool_stat_t *, char *, int);
66f4b3ec61Sdh void	showhashs_live(int, int, iphtstat_t *, char *, int);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int	opts = 0;
697c478bd9Sstevel@tonic-gate int	fd = -1;
707c478bd9Sstevel@tonic-gate int	use_inet6 = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
usage(prog)737c478bd9Sstevel@tonic-gate void usage(prog)
747c478bd9Sstevel@tonic-gate char *prog;
757c478bd9Sstevel@tonic-gate {
7694bdecd9SRob Gulewich 	const char *zoneopt = "[-G|-z zonename] ";
777c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Usage:\t%s\n", prog);
7894bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-a [-dnv] %s[-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n",
7994bdecd9SRob Gulewich 	    zoneopt);
8094bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-A [-dnv] %s[-m <name>] [-o <role>] [-S <seed>] [-t <type>]\n",
8194bdecd9SRob Gulewich 	    zoneopt);
8294bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-f <file> %s[-dnuv]\n", zoneopt);
8394bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-F [-dv] %s[-o <role>] [-t <type>]\n", zoneopt);
8494bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-l [-dv] %s[-m <name>] [-t <type>]\n", zoneopt);
8594bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-r [-dnv] %s[-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n",
8694bdecd9SRob Gulewich 	    zoneopt);
8794bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-R [-dnv] %s[-m <name>] [-o <role>] [-t <type>]\n",
8894bdecd9SRob Gulewich 	    zoneopt);
8994bdecd9SRob Gulewich 	fprintf(stderr, "\t\t\t-s [-dtv] %s[-M <core>] [-N <namelist>]\n",
9094bdecd9SRob Gulewich 	    zoneopt);
917c478bd9Sstevel@tonic-gate 	exit(1);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
main(argc,argv)957c478bd9Sstevel@tonic-gate int main(argc, argv)
967c478bd9Sstevel@tonic-gate int argc;
977c478bd9Sstevel@tonic-gate char *argv[];
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	int err;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (argc < 2)
1027c478bd9Sstevel@tonic-gate 		usage(argv[0]);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	switch (getopt(argc, argv, "aAf:FlrRs"))
1057c478bd9Sstevel@tonic-gate 	{
1067c478bd9Sstevel@tonic-gate 	case 'a' :
1077c478bd9Sstevel@tonic-gate 		err = poolnodecommand(0, argc, argv);
1087c478bd9Sstevel@tonic-gate 		break;
1097c478bd9Sstevel@tonic-gate 	case 'A' :
1107c478bd9Sstevel@tonic-gate 		err = poolcommand(0, argc, argv);
1117c478bd9Sstevel@tonic-gate 		break;
1127c478bd9Sstevel@tonic-gate 	case 'f' :
1137c478bd9Sstevel@tonic-gate 		err = loadpoolfile(argc, argv, optarg);
1147c478bd9Sstevel@tonic-gate 		break;
1157c478bd9Sstevel@tonic-gate 	case 'F' :
1167c478bd9Sstevel@tonic-gate 		err = poolflush(argc, argv);
1177c478bd9Sstevel@tonic-gate 		break;
1187c478bd9Sstevel@tonic-gate 	case 'l' :
1197c478bd9Sstevel@tonic-gate 		err = poollist(argc, argv);
1207c478bd9Sstevel@tonic-gate 		break;
1217c478bd9Sstevel@tonic-gate 	case 'r' :
1227c478bd9Sstevel@tonic-gate 		err = poolnodecommand(1, argc, argv);
1237c478bd9Sstevel@tonic-gate 		break;
1247c478bd9Sstevel@tonic-gate 	case 'R' :
1257c478bd9Sstevel@tonic-gate 		err = poolcommand(1, argc, argv);
1267c478bd9Sstevel@tonic-gate 		break;
1277c478bd9Sstevel@tonic-gate 	case 's' :
1287c478bd9Sstevel@tonic-gate 		err = poolstats(argc, argv);
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 	default :
1317c478bd9Sstevel@tonic-gate 		exit(1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return err;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 
poolnodecommand(remove,argc,argv)1387c478bd9Sstevel@tonic-gate int poolnodecommand(remove, argc, argv)
1397c478bd9Sstevel@tonic-gate int remove, argc;
1407c478bd9Sstevel@tonic-gate char *argv[];
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	char *poolname = NULL, *s;
1437c478bd9Sstevel@tonic-gate 	int err, c, ipset, role;
1447c478bd9Sstevel@tonic-gate 	ip_pool_node_t node;
1457c478bd9Sstevel@tonic-gate 	struct in_addr mask;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	ipset = 0;
1487c478bd9Sstevel@tonic-gate 	role = IPL_LOGIPF;
1497c478bd9Sstevel@tonic-gate 	bzero((char *)&node, sizeof(node));
1507c478bd9Sstevel@tonic-gate 
15194bdecd9SRob Gulewich 	while ((c = getopt(argc, argv, "di:G:m:no:Rvz:")) != -1)
1527c478bd9Sstevel@tonic-gate 		switch (c)
1537c478bd9Sstevel@tonic-gate 		{
1547c478bd9Sstevel@tonic-gate 		case 'd' :
1557c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
1567c478bd9Sstevel@tonic-gate 			ippool_yydebug++;
1577c478bd9Sstevel@tonic-gate 			break;
15894bdecd9SRob Gulewich 		case 'G' :
15994bdecd9SRob Gulewich 			setzonename_global(optarg);
16094bdecd9SRob Gulewich 			break;
1617c478bd9Sstevel@tonic-gate 		case 'i' :
1627c478bd9Sstevel@tonic-gate 			s = strchr(optarg, '/');
1637c478bd9Sstevel@tonic-gate 			if (s == NULL)
1647c478bd9Sstevel@tonic-gate 				mask.s_addr = 0xffffffff;
1657c478bd9Sstevel@tonic-gate 			else if (strchr(s, '.') == NULL) {
1667c478bd9Sstevel@tonic-gate 				if (ntomask(4, atoi(s + 1), &mask.s_addr) != 0)
1677c478bd9Sstevel@tonic-gate 					return -1;
1687c478bd9Sstevel@tonic-gate 			} else {
1697c478bd9Sstevel@tonic-gate 				mask.s_addr = inet_addr(s + 1);
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			if (s != NULL)
1727c478bd9Sstevel@tonic-gate 				*s = '\0';
1737c478bd9Sstevel@tonic-gate 			ipset = 1;
174ab25eeb5Syz 			node.ipn_addr.adf_len = sizeof(node.ipn_addr);
1757c478bd9Sstevel@tonic-gate 			node.ipn_addr.adf_addr.in4.s_addr = inet_addr(optarg);
176ab25eeb5Syz 			node.ipn_mask.adf_len = sizeof(node.ipn_mask);
1777c478bd9Sstevel@tonic-gate 			node.ipn_mask.adf_addr.in4.s_addr = mask.s_addr;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 		case 'm' :
1807c478bd9Sstevel@tonic-gate 			poolname = optarg;
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 		case 'n' :
1837c478bd9Sstevel@tonic-gate 			opts |= OPT_DONOTHING;
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		case 'o' :
1867c478bd9Sstevel@tonic-gate 			role = getrole(optarg);
1877c478bd9Sstevel@tonic-gate 			if (role == IPL_LOGNONE)
1887c478bd9Sstevel@tonic-gate 				return -1;
1897c478bd9Sstevel@tonic-gate 			break;
190ab25eeb5Syz 		case 'R' :
191ab25eeb5Syz 			opts |= OPT_NORESOLVE;
192ab25eeb5Syz 			break;
1937c478bd9Sstevel@tonic-gate 		case 'v' :
1947c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
1957c478bd9Sstevel@tonic-gate 			break;
19694bdecd9SRob Gulewich 		case 'z' :
19794bdecd9SRob Gulewich 			setzonename(optarg);
19894bdecd9SRob Gulewich 			break;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 
201ab25eeb5Syz 	if (opts & OPT_DEBUG)
202ab25eeb5Syz 		fprintf(stderr, "poolnodecommand: opts = %#x\n", opts);
203ab25eeb5Syz 
2047c478bd9Sstevel@tonic-gate 	if (ipset == 0)
2057c478bd9Sstevel@tonic-gate 		return -1;
2067c478bd9Sstevel@tonic-gate 	if (poolname == NULL) {
2077c478bd9Sstevel@tonic-gate 		fprintf(stderr, "poolname not given with add/remove node\n");
2087c478bd9Sstevel@tonic-gate 		return -1;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (remove == 0)
2127c478bd9Sstevel@tonic-gate 		err = load_poolnode(0, poolname, &node, ioctl);
2137c478bd9Sstevel@tonic-gate 	else
2147c478bd9Sstevel@tonic-gate 		err = remove_poolnode(0, poolname, &node, ioctl);
2157c478bd9Sstevel@tonic-gate 	return err;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 
poolcommand(remove,argc,argv)2197c478bd9Sstevel@tonic-gate int poolcommand(remove, argc, argv)
2207c478bd9Sstevel@tonic-gate int remove, argc;
2217c478bd9Sstevel@tonic-gate char *argv[];
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	int type, role, c, err;
2247c478bd9Sstevel@tonic-gate 	char *poolname;
2257c478bd9Sstevel@tonic-gate 	iphtable_t iph;
2267c478bd9Sstevel@tonic-gate 	ip_pool_t pool;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	err = 1;
2297c478bd9Sstevel@tonic-gate 	role = 0;
2307c478bd9Sstevel@tonic-gate 	type = 0;
2317c478bd9Sstevel@tonic-gate 	poolname = NULL;
2327c478bd9Sstevel@tonic-gate 	role = IPL_LOGIPF;
2337c478bd9Sstevel@tonic-gate 	bzero((char *)&iph, sizeof(iph));
2347c478bd9Sstevel@tonic-gate 	bzero((char *)&pool, sizeof(pool));
2357c478bd9Sstevel@tonic-gate 
23694bdecd9SRob Gulewich 	while ((c = getopt(argc, argv, "dG:m:no:RS:t:vz:")) != -1)
2377c478bd9Sstevel@tonic-gate 		switch (c)
2387c478bd9Sstevel@tonic-gate 		{
2397c478bd9Sstevel@tonic-gate 		case 'd' :
2407c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
2417c478bd9Sstevel@tonic-gate 			ippool_yydebug++;
2427c478bd9Sstevel@tonic-gate 			break;
24394bdecd9SRob Gulewich 		case 'G' :
24494bdecd9SRob Gulewich 			setzonename_global(optarg);
24594bdecd9SRob Gulewich 			break;
2467c478bd9Sstevel@tonic-gate 		case 'm' :
2477c478bd9Sstevel@tonic-gate 			poolname = optarg;
2487c478bd9Sstevel@tonic-gate 			break;
2497c478bd9Sstevel@tonic-gate 		case 'n' :
2507c478bd9Sstevel@tonic-gate 			opts |= OPT_DONOTHING;
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 		case 'o' :
2537c478bd9Sstevel@tonic-gate 			role = getrole(optarg);
2547c478bd9Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
2557c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
2567c478bd9Sstevel@tonic-gate 				return -1;
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 			break;
259ab25eeb5Syz 		case 'R' :
260ab25eeb5Syz 			opts |= OPT_NORESOLVE;
261ab25eeb5Syz 			break;
2627c478bd9Sstevel@tonic-gate 		case 'S' :
2637c478bd9Sstevel@tonic-gate 			iph.iph_seed = atoi(optarg);
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 		case 't' :
2667c478bd9Sstevel@tonic-gate 			type = gettype(optarg, &iph.iph_type);
2677c478bd9Sstevel@tonic-gate 			if (type == IPLT_NONE) {
2687c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown type '%s'\n", optarg);
2697c478bd9Sstevel@tonic-gate 				return -1;
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 		case 'v' :
2737c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
2747c478bd9Sstevel@tonic-gate 			break;
27594bdecd9SRob Gulewich 		case 'z' :
27694bdecd9SRob Gulewich 			setzonename(optarg);
27794bdecd9SRob Gulewich 			break;
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 
280ab25eeb5Syz 	if (opts & OPT_DEBUG)
281ab25eeb5Syz 		fprintf(stderr, "poolcommand: opts = %#x\n", opts);
282ab25eeb5Syz 
2837c478bd9Sstevel@tonic-gate 	if (poolname == NULL) {
2847c478bd9Sstevel@tonic-gate 		fprintf(stderr, "poolname not given with add/remove pool\n");
2857c478bd9Sstevel@tonic-gate 		return -1;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (type == IPLT_HASH) {
2897c478bd9Sstevel@tonic-gate 		strncpy(iph.iph_name, poolname, sizeof(iph.iph_name));
2907c478bd9Sstevel@tonic-gate 		iph.iph_name[sizeof(iph.iph_name) - 1] = '\0';
2917c478bd9Sstevel@tonic-gate 		iph.iph_unit = role;
2927c478bd9Sstevel@tonic-gate 	} else if (type == IPLT_POOL) {
2937c478bd9Sstevel@tonic-gate 		strncpy(pool.ipo_name, poolname, sizeof(pool.ipo_name));
2947c478bd9Sstevel@tonic-gate 		pool.ipo_name[sizeof(pool.ipo_name) - 1] = '\0';
2957c478bd9Sstevel@tonic-gate 		pool.ipo_unit = role;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (remove == 0) {
2997c478bd9Sstevel@tonic-gate 		switch (type)
3007c478bd9Sstevel@tonic-gate 		{
3017c478bd9Sstevel@tonic-gate 		case IPLT_HASH :
3027c478bd9Sstevel@tonic-gate 			err = load_hash(&iph, NULL, ioctl);
3037c478bd9Sstevel@tonic-gate 			break;
3047c478bd9Sstevel@tonic-gate 		case IPLT_POOL :
3057c478bd9Sstevel@tonic-gate 			err = load_pool(&pool, ioctl);
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 	} else {
3097c478bd9Sstevel@tonic-gate 		switch (type)
3107c478bd9Sstevel@tonic-gate 		{
3117c478bd9Sstevel@tonic-gate 		case IPLT_HASH :
3127c478bd9Sstevel@tonic-gate 			err = remove_hash(&iph, ioctl);
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 		case IPLT_POOL :
3157c478bd9Sstevel@tonic-gate 			err = remove_pool(&pool, ioctl);
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 	return err;
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
loadpoolfile(argc,argv,infile)3237c478bd9Sstevel@tonic-gate int loadpoolfile(argc, argv, infile)
3247c478bd9Sstevel@tonic-gate int argc;
3257c478bd9Sstevel@tonic-gate char *argv[], *infile;
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	int c;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	infile = optarg;
3307c478bd9Sstevel@tonic-gate 
33194bdecd9SRob Gulewich 	while ((c = getopt(argc, argv, "dG:nRuvz:")) != -1)
3327c478bd9Sstevel@tonic-gate 		switch (c)
3337c478bd9Sstevel@tonic-gate 		{
3347c478bd9Sstevel@tonic-gate 		case 'd' :
3357c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
3367c478bd9Sstevel@tonic-gate 			ippool_yydebug++;
3377c478bd9Sstevel@tonic-gate 			break;
33894bdecd9SRob Gulewich 		case 'G' :
33994bdecd9SRob Gulewich 			setzonename_global(optarg);
34094bdecd9SRob Gulewich 			break;
3417c478bd9Sstevel@tonic-gate 		case 'n' :
3427c478bd9Sstevel@tonic-gate 			opts |= OPT_DONOTHING;
3437c478bd9Sstevel@tonic-gate 			break;
344ab25eeb5Syz 		case 'R' :
345ab25eeb5Syz 			opts |= OPT_NORESOLVE;
346ab25eeb5Syz 			break;
347ab25eeb5Syz 		case 'u' :
3487c478bd9Sstevel@tonic-gate 			opts |= OPT_REMOVE;
3497c478bd9Sstevel@tonic-gate 			break;
3507c478bd9Sstevel@tonic-gate 		case 'v' :
3517c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
3527c478bd9Sstevel@tonic-gate 			break;
35394bdecd9SRob Gulewich 		case 'z' :
35494bdecd9SRob Gulewich 			setzonename(optarg);
35594bdecd9SRob Gulewich 			break;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 
358ab25eeb5Syz 	if (opts & OPT_DEBUG)
359ab25eeb5Syz 		fprintf(stderr, "loadpoolfile: opts = %#x\n", opts);
360ab25eeb5Syz 
3617c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
3627c478bd9Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
3637c478bd9Sstevel@tonic-gate 		if (fd == -1) {
3647c478bd9Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
3657c478bd9Sstevel@tonic-gate 			exit(1);
3667c478bd9Sstevel@tonic-gate 		}
36794bdecd9SRob Gulewich 
36894bdecd9SRob Gulewich 		if (setzone(fd) != 0) {
36994bdecd9SRob Gulewich 			close(fd);
37094bdecd9SRob Gulewich 			exit(1);
37194bdecd9SRob Gulewich 		}
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (ippool_parsefile(fd, infile, ioctl) != 0)
3757c478bd9Sstevel@tonic-gate 		return -1;
3767c478bd9Sstevel@tonic-gate 	return 0;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 
poollist(argc,argv)3807c478bd9Sstevel@tonic-gate int poollist(argc, argv)
3817c478bd9Sstevel@tonic-gate int argc;
3827c478bd9Sstevel@tonic-gate char *argv[];
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	char *kernel, *core, *poolname;
3857c478bd9Sstevel@tonic-gate 	int c, role, type, live_kernel;
386*d7c57852SGary Mills 	ip_pool_stat_t  plstat;
387*d7c57852SGary Mills 	iphtstat_t  htstat;
3887663b816Sml 	iphtable_t *hptr;
3897c478bd9Sstevel@tonic-gate 	iplookupop_t op;
3907c478bd9Sstevel@tonic-gate 	ip_pool_t *ptr;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	core = NULL;
3937c478bd9Sstevel@tonic-gate 	kernel = NULL;
3947c478bd9Sstevel@tonic-gate 	live_kernel = 1;
3957c478bd9Sstevel@tonic-gate 	type = IPLT_ALL;
3967c478bd9Sstevel@tonic-gate 	poolname = NULL;
3977c478bd9Sstevel@tonic-gate 	role = IPL_LOGALL;
3987c478bd9Sstevel@tonic-gate 
39994bdecd9SRob Gulewich 	while ((c = getopt(argc, argv, "dG:m:M:N:o:Rt:vz:")) != -1)
4007c478bd9Sstevel@tonic-gate 		switch (c)
4017c478bd9Sstevel@tonic-gate 		{
4027c478bd9Sstevel@tonic-gate 		case 'd' :
4037c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
4047c478bd9Sstevel@tonic-gate 			break;
40594bdecd9SRob Gulewich 		case 'G' :
40694bdecd9SRob Gulewich 			setzonename_global(optarg);
40794bdecd9SRob Gulewich 			break;
4087c478bd9Sstevel@tonic-gate 		case 'm' :
4097c478bd9Sstevel@tonic-gate 			poolname = optarg;
4107c478bd9Sstevel@tonic-gate 			break;
4117c478bd9Sstevel@tonic-gate 		case 'M' :
4127c478bd9Sstevel@tonic-gate 			live_kernel = 0;
4137c478bd9Sstevel@tonic-gate 			core = optarg;
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 		case 'N' :
4167c478bd9Sstevel@tonic-gate 			live_kernel = 0;
4177c478bd9Sstevel@tonic-gate 			kernel = optarg;
4187c478bd9Sstevel@tonic-gate 			break;
4197c478bd9Sstevel@tonic-gate 		case 'o' :
4207c478bd9Sstevel@tonic-gate 			role = getrole(optarg);
4217c478bd9Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
4227c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
4237c478bd9Sstevel@tonic-gate 				return -1;
4247c478bd9Sstevel@tonic-gate 			}
4257c478bd9Sstevel@tonic-gate 			break;
426ab25eeb5Syz 		case 'R' :
427ab25eeb5Syz 			opts |= OPT_NORESOLVE;
428ab25eeb5Syz 			break;
4297c478bd9Sstevel@tonic-gate 		case 't' :
4307c478bd9Sstevel@tonic-gate 			type = gettype(optarg, NULL);
4317c478bd9Sstevel@tonic-gate 			if (type == IPLT_NONE) {
4327c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown type '%s'\n", optarg);
4337c478bd9Sstevel@tonic-gate 				return -1;
4347c478bd9Sstevel@tonic-gate 			}
4357c478bd9Sstevel@tonic-gate 			break;
4367c478bd9Sstevel@tonic-gate 		case 'v' :
4377c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
4387c478bd9Sstevel@tonic-gate 			break;
43994bdecd9SRob Gulewich 		case 'z' :
44094bdecd9SRob Gulewich 			setzonename(optarg);
44194bdecd9SRob Gulewich 			break;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 
4447663b816Sml 	if (opts & OPT_DEBUG)
4457663b816Sml 		fprintf(stderr, "poollist: opts = %#x\n", opts);
4467663b816Sml 
4477c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
4487c478bd9Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
4497c478bd9Sstevel@tonic-gate 		if (fd == -1) {
4507c478bd9Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
4517c478bd9Sstevel@tonic-gate 			exit(1);
4527c478bd9Sstevel@tonic-gate 		}
45394bdecd9SRob Gulewich 
45494bdecd9SRob Gulewich 		if (setzone(fd) != 0) {
45594bdecd9SRob Gulewich 			close(fd);
45694bdecd9SRob Gulewich 			exit(1);
45794bdecd9SRob Gulewich 		}
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	bzero((char *)&op, sizeof(op));
4617c478bd9Sstevel@tonic-gate 	if (poolname != NULL) {
4627c478bd9Sstevel@tonic-gate 		strncpy(op.iplo_name, poolname, sizeof(op.iplo_name));
4637c478bd9Sstevel@tonic-gate 		op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	op.iplo_unit = role;
4667c478bd9Sstevel@tonic-gate 
467f4b3ec61Sdh 	if (live_kernel == 0) {
468f4b3ec61Sdh 		poollist_dead(role, poolname, type, kernel, core);
469f4b3ec61Sdh 		return (0);
470f4b3ec61Sdh 	}
4717c478bd9Sstevel@tonic-gate 
4727663b816Sml 	if (type == IPLT_ALL || type == IPLT_POOL) {
4737663b816Sml 		op.iplo_type = IPLT_POOL;
4747663b816Sml 		op.iplo_size = sizeof(plstat);
4757663b816Sml 		op.iplo_struct = &plstat;
476f4b3ec61Sdh 		op.iplo_name[0] = '\0';
477f4b3ec61Sdh 		op.iplo_arg = 0;
4787663b816Sml 
4797663b816Sml 		if (role != IPL_LOGALL) {
480f4b3ec61Sdh 			op.iplo_unit = role;
481f4b3ec61Sdh 
482f4b3ec61Sdh 			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
483f4b3ec61Sdh 			if (c == -1) {
484f4b3ec61Sdh 				perror("ioctl(SIOCLOOKUPSTAT)");
485f4b3ec61Sdh 				return -1;
4867c478bd9Sstevel@tonic-gate 			}
487f4b3ec61Sdh 
488f4b3ec61Sdh 			showpools_live(fd, role, &plstat, poolname, opts);
4897663b816Sml 		} else {
4907663b816Sml 			for (role = 0; role <= IPL_LOGMAX; role++) {
491f4b3ec61Sdh 				op.iplo_unit = role;
492f4b3ec61Sdh 
493f4b3ec61Sdh 				c = ioctl(fd, SIOCLOOKUPSTAT, &op);
494f4b3ec61Sdh 				if (c == -1) {
495f4b3ec61Sdh 					perror("ioctl(SIOCLOOKUPSTAT)");
496f4b3ec61Sdh 					return -1;
4977663b816Sml 				}
498f4b3ec61Sdh 
499f4b3ec61Sdh 				showpools_live(fd, role, &plstat, poolname, opts);
5007663b816Sml 			}
501f4b3ec61Sdh 
5027663b816Sml 			role = IPL_LOGALL;
5037663b816Sml 		}
5047663b816Sml 	}
5057663b816Sml 	if (type == IPLT_ALL || type == IPLT_HASH) {
5067663b816Sml 		op.iplo_type = IPLT_HASH;
5077663b816Sml 		op.iplo_size = sizeof(htstat);
5087663b816Sml 		op.iplo_struct = &htstat;
509f4b3ec61Sdh 		op.iplo_name[0] = '\0';
510f4b3ec61Sdh 		op.iplo_arg = 0;
5117663b816Sml 
5127663b816Sml 		if (role != IPL_LOGALL) {
513f4b3ec61Sdh 			op.iplo_unit = role;
514f4b3ec61Sdh 
515f4b3ec61Sdh 			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
516f4b3ec61Sdh 			if (c == -1) {
517f4b3ec61Sdh 				perror("ioctl(SIOCLOOKUPSTAT)");
518f4b3ec61Sdh 				return -1;
5197663b816Sml 			}
520f4b3ec61Sdh 			showhashs_live(fd, role, &htstat, poolname, opts);
5217663b816Sml 		} else {
5227663b816Sml 			for (role = 0; role <= IPL_LOGMAX; role++) {
5237663b816Sml 
5247663b816Sml 				op.iplo_unit = role;
5257663b816Sml 				c = ioctl(fd, SIOCLOOKUPSTAT, &op);
5267663b816Sml 				if (c == -1) {
5277663b816Sml 					perror("ioctl(SIOCLOOKUPSTAT)");
5287663b816Sml 					return -1;
5297663b816Sml 				}
530f4b3ec61Sdh 
531f4b3ec61Sdh 				showhashs_live(fd, role, &htstat, poolname, opts);
5327663b816Sml 			}
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	return 0;
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
poollist_dead(role,poolname,type,kernel,core)538f4b3ec61Sdh void poollist_dead(role, poolname, type, kernel, core)
539f4b3ec61Sdh int role, type;
540f4b3ec61Sdh char *poolname, *kernel, *core;
541f4b3ec61Sdh {
542f4b3ec61Sdh 	iphtable_t *hptr;
543f4b3ec61Sdh 	ip_pool_t *ptr;
544f4b3ec61Sdh 
545f4b3ec61Sdh 	if (openkmem(kernel, core) == -1)
546f4b3ec61Sdh 		exit(-1);
547f4b3ec61Sdh 
548f4b3ec61Sdh 	if (type == IPLT_ALL || type == IPLT_POOL) {
549f4b3ec61Sdh 		ip_pool_t *pools[IPL_LOGSIZE];
550f4b3ec61Sdh 		struct nlist names[2] = { { "ip_pool_list" } , { "" } };
551f4b3ec61Sdh 
552f4b3ec61Sdh 		if (nlist(kernel, names) != 1)
553f4b3ec61Sdh 			return;
554f4b3ec61Sdh 
555f4b3ec61Sdh 		bzero(&pools, sizeof(pools));
556f4b3ec61Sdh 		if (kmemcpy((char *)&pools, names[0].n_value, sizeof(pools)))
557f4b3ec61Sdh 			return;
558f4b3ec61Sdh 
559f4b3ec61Sdh 		if (role != IPL_LOGALL) {
560f4b3ec61Sdh 			ptr = pools[role];
561f4b3ec61Sdh 			while (ptr != NULL) {
562f4b3ec61Sdh 				ptr = printpool(ptr, kmemcpywrap,
563f4b3ec61Sdh 						poolname, opts);
564f4b3ec61Sdh 			}
565f4b3ec61Sdh 		} else {
566f4b3ec61Sdh 			for (role = 0; role <= IPL_LOGMAX; role++) {
567f4b3ec61Sdh 				ptr = pools[role];
568f4b3ec61Sdh 				while (ptr != NULL) {
569f4b3ec61Sdh 					ptr = printpool(ptr, kmemcpywrap,
570f4b3ec61Sdh 							poolname, opts);
571f4b3ec61Sdh 				}
572f4b3ec61Sdh 			}
573f4b3ec61Sdh 			role = IPL_LOGALL;
574f4b3ec61Sdh 		}
575f4b3ec61Sdh 	}
576f4b3ec61Sdh 	if (type == IPLT_ALL || type == IPLT_HASH) {
577f4b3ec61Sdh 		iphtable_t *tables[IPL_LOGSIZE];
578f4b3ec61Sdh 		struct nlist names[2] = { { "ipf_htables" } , { "" } };
579f4b3ec61Sdh 
580f4b3ec61Sdh 		if (nlist(kernel, names) != 1)
581f4b3ec61Sdh 			return;
582f4b3ec61Sdh 
583f4b3ec61Sdh 		bzero(&tables, sizeof(tables));
584f4b3ec61Sdh 		if (kmemcpy((char *)&tables, names[0].n_value, sizeof(tables)))
585f4b3ec61Sdh 			return;
586f4b3ec61Sdh 
587f4b3ec61Sdh 		if (role != IPL_LOGALL) {
588f4b3ec61Sdh 			hptr = tables[role];
589f4b3ec61Sdh 			while (hptr != NULL) {
590f4b3ec61Sdh 				hptr = printhash(hptr, kmemcpywrap,
591f4b3ec61Sdh 						 poolname, opts);
592f4b3ec61Sdh 			}
593f4b3ec61Sdh 		} else {
594f4b3ec61Sdh 			for (role = 0; role <= IPL_LOGMAX; role++) {
595f4b3ec61Sdh 				hptr = tables[role];
596f4b3ec61Sdh 				while (hptr != NULL) {
597f4b3ec61Sdh 					hptr = printhash(hptr, kmemcpywrap,
598f4b3ec61Sdh 							 poolname, opts);
599f4b3ec61Sdh 				}
600f4b3ec61Sdh 			}
601f4b3ec61Sdh 		}
602f4b3ec61Sdh 	}
603f4b3ec61Sdh }
604f4b3ec61Sdh 
605f4b3ec61Sdh 
606f4b3ec61Sdh void
showpools_live(fd,role,plstp,poolname,opts)607f4b3ec61Sdh showpools_live(fd, role, plstp, poolname, opts)
608f4b3ec61Sdh int fd, role;
609f4b3ec61Sdh ip_pool_stat_t *plstp;
610f4b3ec61Sdh char *poolname;
611f4b3ec61Sdh int opts;
612f4b3ec61Sdh {
613f4b3ec61Sdh 	ipflookupiter_t iter;
614f4b3ec61Sdh 	ip_pool_t pool;
615f4b3ec61Sdh 	ipfobj_t obj;
616f4b3ec61Sdh 
617f4b3ec61Sdh 	obj.ipfo_rev = IPFILTER_VERSION;
618f4b3ec61Sdh 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
619f4b3ec61Sdh 	obj.ipfo_size = sizeof(iter);
620f4b3ec61Sdh 	obj.ipfo_ptr = &iter;
621f4b3ec61Sdh 
622f4b3ec61Sdh 	iter.ili_type = IPLT_POOL;
623f4b3ec61Sdh 	iter.ili_otype = IPFLOOKUPITER_LIST;
624f4b3ec61Sdh 	iter.ili_ival = IPFGENITER_LOOKUP;
625f4b3ec61Sdh 	iter.ili_data = &pool;
626f4b3ec61Sdh 	iter.ili_unit = role;
627f4b3ec61Sdh 	*iter.ili_name = '\0';
628f4b3ec61Sdh 
629f4b3ec61Sdh 	while (plstp->ipls_list[role] != NULL) {
630f4b3ec61Sdh 		if (ioctl(fd, SIOCLOOKUPITER, &obj)) {
631f4b3ec61Sdh 			perror("ioctl(SIOCLOOKUPITER)");
632f4b3ec61Sdh 			break;
633f4b3ec61Sdh 		}
634f4b3ec61Sdh 		(void) printpool_live(&pool, fd, poolname, opts);
635f4b3ec61Sdh 
636f4b3ec61Sdh 		plstp->ipls_list[role] = pool.ipo_next;
637f4b3ec61Sdh 	}
638f4b3ec61Sdh }
6397c478bd9Sstevel@tonic-gate 
poolstats(argc,argv)6407c478bd9Sstevel@tonic-gate int poolstats(argc, argv)
6417c478bd9Sstevel@tonic-gate int argc;
6427c478bd9Sstevel@tonic-gate char *argv[];
6437c478bd9Sstevel@tonic-gate {
644*d7c57852SGary Mills 	int c, type, role;
6457c478bd9Sstevel@tonic-gate 	ip_pool_stat_t plstat;
6467663b816Sml 	iphtstat_t htstat;
6477c478bd9Sstevel@tonic-gate 	iplookupop_t op;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	type = IPLT_ALL;
6507c478bd9Sstevel@tonic-gate 	role = IPL_LOGALL;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	bzero((char *)&op, sizeof(op));
6537c478bd9Sstevel@tonic-gate 
65494bdecd9SRob Gulewich 	while ((c = getopt(argc, argv, "dG:M:N:o:t:vz:")) != -1)
6557c478bd9Sstevel@tonic-gate 		switch (c)
6567c478bd9Sstevel@tonic-gate 		{
6577c478bd9Sstevel@tonic-gate 		case 'd' :
6587c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
6597c478bd9Sstevel@tonic-gate 			break;
66094bdecd9SRob Gulewich 		case 'G' :
66194bdecd9SRob Gulewich 			setzonename_global(optarg);
66294bdecd9SRob Gulewich 			break;
6637c478bd9Sstevel@tonic-gate 		case 'M' :
6647c478bd9Sstevel@tonic-gate 			break;
6657c478bd9Sstevel@tonic-gate 		case 'N' :
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 		case 'o' :
6687c478bd9Sstevel@tonic-gate 			role = getrole(optarg);
6697c478bd9Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
6707c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
6717c478bd9Sstevel@tonic-gate 				return -1;
6727c478bd9Sstevel@tonic-gate 			}
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 		case 't' :
6757c478bd9Sstevel@tonic-gate 			type = gettype(optarg, NULL);
6767c478bd9Sstevel@tonic-gate 			if (type != IPLT_POOL) {
6777c478bd9Sstevel@tonic-gate 				fprintf(stderr,
6787c478bd9Sstevel@tonic-gate 					"-s not supported for this type yet\n");
6797c478bd9Sstevel@tonic-gate 				return -1;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 			break;
6827c478bd9Sstevel@tonic-gate 		case 'v' :
6837c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
6847c478bd9Sstevel@tonic-gate 			break;
68594bdecd9SRob Gulewich 		case 'z' :
68694bdecd9SRob Gulewich 			setzonename(optarg);
68794bdecd9SRob Gulewich 			break;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 
6907663b816Sml 	if (opts & OPT_DEBUG)
6917663b816Sml 		fprintf(stderr, "poolstats: opts = %#x\n", opts);
6927663b816Sml 
6937c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
6947c478bd9Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
6957c478bd9Sstevel@tonic-gate 		if (fd == -1) {
6967c478bd9Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
6977c478bd9Sstevel@tonic-gate 			exit(1);
6987c478bd9Sstevel@tonic-gate 		}
69994bdecd9SRob Gulewich 
70094bdecd9SRob Gulewich 		if (setzone(fd) != 0) {
70194bdecd9SRob Gulewich 			close(fd);
70294bdecd9SRob Gulewich 			exit(1);
70394bdecd9SRob Gulewich 		}
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067663b816Sml 	if (type == IPLT_ALL || type == IPLT_POOL) {
7077663b816Sml 		op.iplo_type = IPLT_POOL;
7087663b816Sml 		op.iplo_struct = &plstat;
7097663b816Sml 		op.iplo_size = sizeof(plstat);
7107663b816Sml 		if (!(opts & OPT_DONOTHING)) {
7117663b816Sml 			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
7127663b816Sml 			if (c == -1) {
7137663b816Sml 				perror("ioctl(SIOCLOOKUPSTAT)");
7147663b816Sml 				return -1;
7157663b816Sml 			}
7167663b816Sml 			printf("Pools:\t%lu\n", plstat.ipls_pools);
7177663b816Sml 			printf("Nodes:\t%lu\n", plstat.ipls_nodes);
7187663b816Sml 		}
7197663b816Sml 	}
7207663b816Sml 
7217663b816Sml 	if (type == IPLT_ALL || type == IPLT_HASH) {
7227663b816Sml 		op.iplo_type = IPLT_HASH;
7237663b816Sml 		op.iplo_struct = &htstat;
7247663b816Sml 		op.iplo_size = sizeof(htstat);
7257663b816Sml 		if (!(opts & OPT_DONOTHING)) {
7267663b816Sml 			c = ioctl(fd, SIOCLOOKUPSTAT, &op);
7277663b816Sml 			if (c == -1) {
7287663b816Sml 				perror("ioctl(SIOCLOOKUPSTAT)");
7297663b816Sml 				return -1;
7307663b816Sml 			}
7317663b816Sml 			printf("Hash Tables:\t%lu\n", htstat.iphs_numtables);
7327663b816Sml 			printf("Nodes:\t%lu\n", htstat.iphs_numnodes);
7337663b816Sml 			printf("Out of Memory:\t%lu\n", htstat.iphs_nomem);
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 	return 0;
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 
poolflush(argc,argv)7407c478bd9Sstevel@tonic-gate int poolflush(argc, argv)
7417c478bd9Sstevel@tonic-gate int argc;
7427c478bd9Sstevel@tonic-gate char *argv[];
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	int c, role, type, arg;
7457c478bd9Sstevel@tonic-gate 	iplookupflush_t flush;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	arg = IPLT_ALL;
7487c478bd9Sstevel@tonic-gate 	type = IPLT_ALL;
7497c478bd9Sstevel@tonic-gate 	role = IPL_LOGALL;
7507c478bd9Sstevel@tonic-gate 
75194bdecd9SRob Gulewich 	while ((c = getopt(argc, argv, "do:t:vz:")) != -1)
7527c478bd9Sstevel@tonic-gate 		switch (c)
7537c478bd9Sstevel@tonic-gate 		{
7547c478bd9Sstevel@tonic-gate 		case 'd' :
7557c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
7567c478bd9Sstevel@tonic-gate 			break;
7577c478bd9Sstevel@tonic-gate 		case 'o' :
7587c478bd9Sstevel@tonic-gate 			role = getrole(optarg);
7597c478bd9Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
7607c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
7617c478bd9Sstevel@tonic-gate 				return -1;
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate 			break;
7647c478bd9Sstevel@tonic-gate 		case 't' :
7657c478bd9Sstevel@tonic-gate 			type = gettype(optarg, NULL);
7667c478bd9Sstevel@tonic-gate 			if (type == IPLT_NONE) {
7677c478bd9Sstevel@tonic-gate 				fprintf(stderr, "unknown type '%s'\n", optarg);
7687c478bd9Sstevel@tonic-gate 				return -1;
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 			break;
7717c478bd9Sstevel@tonic-gate 		case 'v' :
7727c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
7737c478bd9Sstevel@tonic-gate 			break;
77494bdecd9SRob Gulewich 		case 'z' :
77594bdecd9SRob Gulewich 			setzonename(optarg);
77694bdecd9SRob Gulewich 			break;
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 
779ab25eeb5Syz 	if (opts & OPT_DEBUG)
780ab25eeb5Syz 		fprintf(stderr, "poolflush: opts = %#x\n", opts);
781ab25eeb5Syz 
7827c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
7837c478bd9Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
7847c478bd9Sstevel@tonic-gate 		if (fd == -1) {
7857c478bd9Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
7867c478bd9Sstevel@tonic-gate 			exit(1);
7877c478bd9Sstevel@tonic-gate 		}
78894bdecd9SRob Gulewich 
78994bdecd9SRob Gulewich 		if (setzone(fd) != 0) {
79094bdecd9SRob Gulewich 			close(fd);
79194bdecd9SRob Gulewich 			exit(1);
79294bdecd9SRob Gulewich 		}
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	bzero((char *)&flush, sizeof(flush));
7967c478bd9Sstevel@tonic-gate 	flush.iplf_type = type;
7977c478bd9Sstevel@tonic-gate 	flush.iplf_unit = role;
7987c478bd9Sstevel@tonic-gate 	flush.iplf_arg = arg;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING)) {
8017c478bd9Sstevel@tonic-gate 		if (ioctl(fd, SIOCLOOKUPFLUSH, &flush) == -1) {
8027c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCLOOKUPFLUSH)");
8037c478bd9Sstevel@tonic-gate 			exit(1);
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 	printf("%u object%s flushed\n", flush.iplf_count,
8087c478bd9Sstevel@tonic-gate 	       (flush.iplf_count == 1) ? "" : "s");
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	return 0;
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 
getrole(rolename)8147c478bd9Sstevel@tonic-gate int getrole(rolename)
8157c478bd9Sstevel@tonic-gate char *rolename;
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	int role;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if (!strcasecmp(rolename, "ipf")) {
8207c478bd9Sstevel@tonic-gate 		role = IPL_LOGIPF;
8217c478bd9Sstevel@tonic-gate #if 0
8227c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "nat")) {
8237c478bd9Sstevel@tonic-gate 		role = IPL_LOGNAT;
8247c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "state")) {
8257c478bd9Sstevel@tonic-gate 		role = IPL_LOGSTATE;
8267c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "auth")) {
8277c478bd9Sstevel@tonic-gate 		role = IPL_LOGAUTH;
8287c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "sync")) {
8297c478bd9Sstevel@tonic-gate 		role = IPL_LOGSYNC;
8307c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "scan")) {
8317c478bd9Sstevel@tonic-gate 		role = IPL_LOGSCAN;
8327c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "pool")) {
8337c478bd9Sstevel@tonic-gate 		role = IPL_LOGLOOKUP;
8347c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "count")) {
8357c478bd9Sstevel@tonic-gate 		role = IPL_LOGCOUNT;
8367c478bd9Sstevel@tonic-gate #endif
8377c478bd9Sstevel@tonic-gate 	} else {
8387c478bd9Sstevel@tonic-gate 		role = IPL_LOGNONE;
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	return role;
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 
gettype(typename,minor)8457c478bd9Sstevel@tonic-gate int gettype(typename, minor)
8467c478bd9Sstevel@tonic-gate char *typename;
8477c478bd9Sstevel@tonic-gate u_int *minor;
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	int type;
8507c478bd9Sstevel@tonic-gate 
851ab25eeb5Syz 	if (!strcasecmp(optarg, "tree")) {
8527c478bd9Sstevel@tonic-gate 		type = IPLT_POOL;
8537c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(optarg, "hash")) {
8547c478bd9Sstevel@tonic-gate 		type = IPLT_HASH;
8557c478bd9Sstevel@tonic-gate 		if (minor != NULL)
8567c478bd9Sstevel@tonic-gate 			*minor = IPHASH_LOOKUP;
8577c478bd9Sstevel@tonic-gate 	} else if (!strcasecmp(optarg, "group-map")) {
8587c478bd9Sstevel@tonic-gate 		type = IPLT_HASH;
8597c478bd9Sstevel@tonic-gate 		if (minor != NULL)
8607c478bd9Sstevel@tonic-gate 			*minor = IPHASH_GROUPMAP;
8617c478bd9Sstevel@tonic-gate 	} else {
8627c478bd9Sstevel@tonic-gate 		type = IPLT_NONE;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 	return type;
8657c478bd9Sstevel@tonic-gate }
866f4b3ec61Sdh 
showhashs_live(fd,role,htstp,poolname,opts)867f4b3ec61Sdh void showhashs_live(fd, role, htstp, poolname, opts)
868f4b3ec61Sdh int fd, role;
869f4b3ec61Sdh iphtstat_t *htstp;
870f4b3ec61Sdh char *poolname;
871f4b3ec61Sdh int opts;
872f4b3ec61Sdh {
873f4b3ec61Sdh 	ipflookupiter_t iter;
874f4b3ec61Sdh 	iphtable_t table;
875f4b3ec61Sdh 	ipfobj_t obj;
876f4b3ec61Sdh 
877f4b3ec61Sdh 	obj.ipfo_rev = IPFILTER_VERSION;
878f4b3ec61Sdh 	obj.ipfo_type = IPFOBJ_LOOKUPITER;
879f4b3ec61Sdh 	obj.ipfo_size = sizeof(iter);
880f4b3ec61Sdh 	obj.ipfo_ptr = &iter;
881f4b3ec61Sdh 
882f4b3ec61Sdh 	iter.ili_type = IPLT_HASH;
883f4b3ec61Sdh 	iter.ili_otype = IPFLOOKUPITER_LIST;
884f4b3ec61Sdh 	iter.ili_ival = IPFGENITER_LOOKUP;
885f4b3ec61Sdh 	iter.ili_data = &table;
886f4b3ec61Sdh 	iter.ili_unit = role;
887f4b3ec61Sdh 	*iter.ili_name = '\0';
888f4b3ec61Sdh 
889f4b3ec61Sdh 	while (htstp->iphs_tables != NULL) {
890f4b3ec61Sdh 		if (ioctl(fd, SIOCLOOKUPITER, &obj)) {
891f4b3ec61Sdh 			perror("ioctl(SIOCLOOKUPITER)");
892f4b3ec61Sdh 			break;
893f4b3ec61Sdh 		}
894f4b3ec61Sdh 
895f4b3ec61Sdh 		printhash_live(&table, fd, poolname, opts);
896f4b3ec61Sdh 
897f4b3ec61Sdh 		htstp->iphs_tables = table.iph_next;
898f4b3ec61Sdh 	}
899f4b3ec61Sdh }
900