xref: /onnv-gate/usr/src/cmd/ipf/tools/ippool.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (C) 2003 by Darren Reed.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
7*0Sstevel@tonic-gate  * Use is subject to license terms.
8*0Sstevel@tonic-gate  */
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include <sys/types.h>
13*0Sstevel@tonic-gate #include <sys/time.h>
14*0Sstevel@tonic-gate #include <sys/param.h>
15*0Sstevel@tonic-gate #include <sys/socket.h>
16*0Sstevel@tonic-gate #if defined(BSD) && (BSD >= 199306)
17*0Sstevel@tonic-gate # include <sys/cdefs.h>
18*0Sstevel@tonic-gate #endif
19*0Sstevel@tonic-gate #include <sys/ioctl.h>
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include <net/if.h>
22*0Sstevel@tonic-gate #if __FreeBSD_version >= 300000
23*0Sstevel@tonic-gate # include <net/if_var.h>
24*0Sstevel@tonic-gate #endif
25*0Sstevel@tonic-gate #include <netinet/in.h>
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #include <arpa/inet.h>
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <fcntl.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <netdb.h>
34*0Sstevel@tonic-gate #include <ctype.h>
35*0Sstevel@tonic-gate #include <unistd.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "ipf.h"
38*0Sstevel@tonic-gate #if SOLARIS2 >= 10
39*0Sstevel@tonic-gate #include "ip_lookup.h"
40*0Sstevel@tonic-gate #include "ip_pool.h"
41*0Sstevel@tonic-gate #include "ip_htable.h"
42*0Sstevel@tonic-gate #else
43*0Sstevel@tonic-gate #include "netinet/ip_lookup.h"
44*0Sstevel@tonic-gate #include "netinet/ip_pool.h"
45*0Sstevel@tonic-gate #include "netinet/ip_htable.h"
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate #include "kmem.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate extern	int	ippool_yyparse __P((void));
51*0Sstevel@tonic-gate extern	int	ippool_yydebug;
52*0Sstevel@tonic-gate extern	FILE	*ippool_yyin;
53*0Sstevel@tonic-gate extern	char	*optarg;
54*0Sstevel@tonic-gate extern	int	lineNum;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate void	showpools __P((ip_pool_stat_t *));
57*0Sstevel@tonic-gate void	usage __P((char *));
58*0Sstevel@tonic-gate int	main __P((int, char **));
59*0Sstevel@tonic-gate int	poolcommand __P((int, int, char *[]));
60*0Sstevel@tonic-gate int	poolnodecommand __P((int, int, char *[]));
61*0Sstevel@tonic-gate int	loadpoolfile __P((int, char *[], char *));
62*0Sstevel@tonic-gate int	poollist __P((int, char *[]));
63*0Sstevel@tonic-gate int	poolflush __P((int, char *[]));
64*0Sstevel@tonic-gate int	poolstats __P((int, char *[]));
65*0Sstevel@tonic-gate int	gettype __P((char *, u_int *));
66*0Sstevel@tonic-gate int	getrole __P((char *));
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate int	opts = 0;
69*0Sstevel@tonic-gate int	fd = -1;
70*0Sstevel@tonic-gate int	use_inet6 = 0;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate void usage(prog)
74*0Sstevel@tonic-gate char *prog;
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	fprintf(stderr, "Usage:\t%s\n", prog);
77*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-a [-dnv] [-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n");
78*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-A [-dnv] [-m <name>] [-o <role>] [-S <seed>] [-t <type>]\n");
79*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-f <file> [-dnuv]\n");
80*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-F [-dv] [-o <role>] [-t <type>]\n");
81*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-l [-dv] [-m <name>] [-t <type>]\n");
82*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-r [-dnv] [-m <name>] [-o <role>] -i <ipaddr>[/netmask]\n");
83*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-R [-dnv] [-m <name>] [-o <role>] [-t <type>]\n");
84*0Sstevel@tonic-gate 	fprintf(stderr, "\t\t\t-s [-dtv] [-M <core>] [-N <namelist>]\n");
85*0Sstevel@tonic-gate 	exit(1);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate int main(argc, argv)
90*0Sstevel@tonic-gate int argc;
91*0Sstevel@tonic-gate char *argv[];
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	int err;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	if (argc < 2)
96*0Sstevel@tonic-gate 		usage(argv[0]);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	switch (getopt(argc, argv, "aAf:FlrRs"))
99*0Sstevel@tonic-gate 	{
100*0Sstevel@tonic-gate 	case 'a' :
101*0Sstevel@tonic-gate 		err = poolnodecommand(0, argc, argv);
102*0Sstevel@tonic-gate 		break;
103*0Sstevel@tonic-gate 	case 'A' :
104*0Sstevel@tonic-gate 		err = poolcommand(0, argc, argv);
105*0Sstevel@tonic-gate 		break;
106*0Sstevel@tonic-gate 	case 'f' :
107*0Sstevel@tonic-gate 		err = loadpoolfile(argc, argv, optarg);
108*0Sstevel@tonic-gate 		break;
109*0Sstevel@tonic-gate 	case 'F' :
110*0Sstevel@tonic-gate 		err = poolflush(argc, argv);
111*0Sstevel@tonic-gate 		break;
112*0Sstevel@tonic-gate 	case 'l' :
113*0Sstevel@tonic-gate 		err = poollist(argc, argv);
114*0Sstevel@tonic-gate 		break;
115*0Sstevel@tonic-gate 	case 'r' :
116*0Sstevel@tonic-gate 		err = poolnodecommand(1, argc, argv);
117*0Sstevel@tonic-gate 		break;
118*0Sstevel@tonic-gate 	case 'R' :
119*0Sstevel@tonic-gate 		err = poolcommand(1, argc, argv);
120*0Sstevel@tonic-gate 		break;
121*0Sstevel@tonic-gate 	case 's' :
122*0Sstevel@tonic-gate 		err = poolstats(argc, argv);
123*0Sstevel@tonic-gate 		break;
124*0Sstevel@tonic-gate 	default :
125*0Sstevel@tonic-gate 		exit(1);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	return err;
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate int poolnodecommand(remove, argc, argv)
133*0Sstevel@tonic-gate int remove, argc;
134*0Sstevel@tonic-gate char *argv[];
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	char *poolname = NULL, *s;
137*0Sstevel@tonic-gate 	int err, c, ipset, role;
138*0Sstevel@tonic-gate 	ip_pool_node_t node;
139*0Sstevel@tonic-gate 	struct in_addr mask;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	ipset = 0;
142*0Sstevel@tonic-gate 	role = IPL_LOGIPF;
143*0Sstevel@tonic-gate 	bzero((char *)&node, sizeof(node));
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "di:m:no:v")) != -1)
146*0Sstevel@tonic-gate 		switch (c)
147*0Sstevel@tonic-gate 		{
148*0Sstevel@tonic-gate 		case 'd' :
149*0Sstevel@tonic-gate 			opts |= OPT_DEBUG;
150*0Sstevel@tonic-gate 			ippool_yydebug++;
151*0Sstevel@tonic-gate 			break;
152*0Sstevel@tonic-gate 		case 'i' :
153*0Sstevel@tonic-gate 			s = strchr(optarg, '/');
154*0Sstevel@tonic-gate 			if (s == NULL)
155*0Sstevel@tonic-gate 				mask.s_addr = 0xffffffff;
156*0Sstevel@tonic-gate 			else if (strchr(s, '.') == NULL) {
157*0Sstevel@tonic-gate 				if (ntomask(4, atoi(s + 1), &mask.s_addr) != 0)
158*0Sstevel@tonic-gate 					return -1;
159*0Sstevel@tonic-gate 			} else {
160*0Sstevel@tonic-gate 				mask.s_addr = inet_addr(s + 1);
161*0Sstevel@tonic-gate 			}
162*0Sstevel@tonic-gate 			if (s != NULL)
163*0Sstevel@tonic-gate 				*s = '\0';
164*0Sstevel@tonic-gate 			ipset = 1;
165*0Sstevel@tonic-gate 			node.ipn_addr.adf_addr.in4.s_addr = inet_addr(optarg);
166*0Sstevel@tonic-gate 			node.ipn_mask.adf_addr.in4.s_addr = mask.s_addr;
167*0Sstevel@tonic-gate 			break;
168*0Sstevel@tonic-gate 		case 'm' :
169*0Sstevel@tonic-gate 			poolname = optarg;
170*0Sstevel@tonic-gate 			break;
171*0Sstevel@tonic-gate 		case 'n' :
172*0Sstevel@tonic-gate 			opts |= OPT_DONOTHING;
173*0Sstevel@tonic-gate 			break;
174*0Sstevel@tonic-gate 		case 'o' :
175*0Sstevel@tonic-gate 			role = getrole(optarg);
176*0Sstevel@tonic-gate 			if (role == IPL_LOGNONE)
177*0Sstevel@tonic-gate 				return -1;
178*0Sstevel@tonic-gate 			break;
179*0Sstevel@tonic-gate 		case 'v' :
180*0Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
181*0Sstevel@tonic-gate 			break;
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (ipset == 0)
185*0Sstevel@tonic-gate 		return -1;
186*0Sstevel@tonic-gate 	if (poolname == NULL) {
187*0Sstevel@tonic-gate 		fprintf(stderr, "poolname not given with add/remove node\n");
188*0Sstevel@tonic-gate 		return -1;
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (remove == 0)
192*0Sstevel@tonic-gate 		err = load_poolnode(0, poolname, &node, ioctl);
193*0Sstevel@tonic-gate 	else
194*0Sstevel@tonic-gate 		err = remove_poolnode(0, poolname, &node, ioctl);
195*0Sstevel@tonic-gate 	return err;
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate int poolcommand(remove, argc, argv)
200*0Sstevel@tonic-gate int remove, argc;
201*0Sstevel@tonic-gate char *argv[];
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	int type, role, c, err;
204*0Sstevel@tonic-gate 	char *poolname;
205*0Sstevel@tonic-gate 	iphtable_t iph;
206*0Sstevel@tonic-gate 	ip_pool_t pool;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	err = 1;
209*0Sstevel@tonic-gate 	role = 0;
210*0Sstevel@tonic-gate 	type = 0;
211*0Sstevel@tonic-gate 	poolname = NULL;
212*0Sstevel@tonic-gate 	role = IPL_LOGIPF;
213*0Sstevel@tonic-gate 	bzero((char *)&iph, sizeof(iph));
214*0Sstevel@tonic-gate 	bzero((char *)&pool, sizeof(pool));
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dm:no:S:t:v")) != -1)
217*0Sstevel@tonic-gate 		switch (c)
218*0Sstevel@tonic-gate 		{
219*0Sstevel@tonic-gate 		case 'd' :
220*0Sstevel@tonic-gate 			opts |= OPT_DEBUG;
221*0Sstevel@tonic-gate 			ippool_yydebug++;
222*0Sstevel@tonic-gate 			break;
223*0Sstevel@tonic-gate 		case 'm' :
224*0Sstevel@tonic-gate 			poolname = optarg;
225*0Sstevel@tonic-gate 			break;
226*0Sstevel@tonic-gate 		case 'n' :
227*0Sstevel@tonic-gate 			opts |= OPT_DONOTHING;
228*0Sstevel@tonic-gate 			break;
229*0Sstevel@tonic-gate 		case 'o' :
230*0Sstevel@tonic-gate 			role = getrole(optarg);
231*0Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
232*0Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
233*0Sstevel@tonic-gate 				return -1;
234*0Sstevel@tonic-gate 			}
235*0Sstevel@tonic-gate 			break;
236*0Sstevel@tonic-gate 		case 'S' :
237*0Sstevel@tonic-gate 			iph.iph_seed = atoi(optarg);
238*0Sstevel@tonic-gate 			break;
239*0Sstevel@tonic-gate 		case 't' :
240*0Sstevel@tonic-gate 			type = gettype(optarg, &iph.iph_type);
241*0Sstevel@tonic-gate 			if (type == IPLT_NONE) {
242*0Sstevel@tonic-gate 				fprintf(stderr, "unknown type '%s'\n", optarg);
243*0Sstevel@tonic-gate 				return -1;
244*0Sstevel@tonic-gate 			}
245*0Sstevel@tonic-gate 			break;
246*0Sstevel@tonic-gate 		case 'v' :
247*0Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
248*0Sstevel@tonic-gate 			break;
249*0Sstevel@tonic-gate 		}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (poolname == NULL) {
252*0Sstevel@tonic-gate 		fprintf(stderr, "poolname not given with add/remove pool\n");
253*0Sstevel@tonic-gate 		return -1;
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	if (type == IPLT_HASH) {
257*0Sstevel@tonic-gate 		strncpy(iph.iph_name, poolname, sizeof(iph.iph_name));
258*0Sstevel@tonic-gate 		iph.iph_name[sizeof(iph.iph_name) - 1] = '\0';
259*0Sstevel@tonic-gate 		iph.iph_unit = role;
260*0Sstevel@tonic-gate 	} else if (type == IPLT_POOL) {
261*0Sstevel@tonic-gate 		strncpy(pool.ipo_name, poolname, sizeof(pool.ipo_name));
262*0Sstevel@tonic-gate 		pool.ipo_name[sizeof(pool.ipo_name) - 1] = '\0';
263*0Sstevel@tonic-gate 		pool.ipo_unit = role;
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	if (remove == 0) {
267*0Sstevel@tonic-gate 		switch (type)
268*0Sstevel@tonic-gate 		{
269*0Sstevel@tonic-gate 		case IPLT_HASH :
270*0Sstevel@tonic-gate 			err = load_hash(&iph, NULL, ioctl);
271*0Sstevel@tonic-gate 			break;
272*0Sstevel@tonic-gate 		case IPLT_POOL :
273*0Sstevel@tonic-gate 			err = load_pool(&pool, ioctl);
274*0Sstevel@tonic-gate 			break;
275*0Sstevel@tonic-gate 		}
276*0Sstevel@tonic-gate 	} else {
277*0Sstevel@tonic-gate 		switch (type)
278*0Sstevel@tonic-gate 		{
279*0Sstevel@tonic-gate 		case IPLT_HASH :
280*0Sstevel@tonic-gate 			err = remove_hash(&iph, ioctl);
281*0Sstevel@tonic-gate 			break;
282*0Sstevel@tonic-gate 		case IPLT_POOL :
283*0Sstevel@tonic-gate 			err = remove_pool(&pool, ioctl);
284*0Sstevel@tonic-gate 			break;
285*0Sstevel@tonic-gate 		}
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate 	return err;
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate int loadpoolfile(argc, argv, infile)
292*0Sstevel@tonic-gate int argc;
293*0Sstevel@tonic-gate char *argv[], *infile;
294*0Sstevel@tonic-gate {
295*0Sstevel@tonic-gate 	int c;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	infile = optarg;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dnrv")) != -1)
300*0Sstevel@tonic-gate 		switch (c)
301*0Sstevel@tonic-gate 		{
302*0Sstevel@tonic-gate 		case 'd' :
303*0Sstevel@tonic-gate 			opts |= OPT_DEBUG;
304*0Sstevel@tonic-gate 			ippool_yydebug++;
305*0Sstevel@tonic-gate 			break;
306*0Sstevel@tonic-gate 		case 'n' :
307*0Sstevel@tonic-gate 			opts |= OPT_DONOTHING;
308*0Sstevel@tonic-gate 			break;
309*0Sstevel@tonic-gate 		case 'r' :
310*0Sstevel@tonic-gate 			opts |= OPT_REMOVE;
311*0Sstevel@tonic-gate 			break;
312*0Sstevel@tonic-gate 		case 'v' :
313*0Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
314*0Sstevel@tonic-gate 			break;
315*0Sstevel@tonic-gate 		}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
318*0Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
319*0Sstevel@tonic-gate 		if (fd == -1) {
320*0Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
321*0Sstevel@tonic-gate 			exit(1);
322*0Sstevel@tonic-gate 		}
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	if (ippool_parsefile(fd, infile, ioctl) != 0)
326*0Sstevel@tonic-gate 		return -1;
327*0Sstevel@tonic-gate 	return 0;
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate int poollist(argc, argv)
332*0Sstevel@tonic-gate int argc;
333*0Sstevel@tonic-gate char *argv[];
334*0Sstevel@tonic-gate {
335*0Sstevel@tonic-gate 	char *kernel, *core, *poolname;
336*0Sstevel@tonic-gate 	int c, role, type, live_kernel;
337*0Sstevel@tonic-gate 	ip_pool_stat_t *plstp, plstat;
338*0Sstevel@tonic-gate 	iplookupop_t op;
339*0Sstevel@tonic-gate 	ip_pool_t *ptr;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	core = NULL;
342*0Sstevel@tonic-gate 	kernel = NULL;
343*0Sstevel@tonic-gate 	live_kernel = 1;
344*0Sstevel@tonic-gate 	type = IPLT_ALL;
345*0Sstevel@tonic-gate 	poolname = NULL;
346*0Sstevel@tonic-gate 	role = IPL_LOGALL;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dm:M:N:o:t:v")) != -1)
349*0Sstevel@tonic-gate 		switch (c)
350*0Sstevel@tonic-gate 		{
351*0Sstevel@tonic-gate 		case 'd' :
352*0Sstevel@tonic-gate 			opts |= OPT_DEBUG;
353*0Sstevel@tonic-gate 			break;
354*0Sstevel@tonic-gate 		case 'm' :
355*0Sstevel@tonic-gate 			poolname = optarg;
356*0Sstevel@tonic-gate 			break;
357*0Sstevel@tonic-gate 		case 'M' :
358*0Sstevel@tonic-gate 			live_kernel = 0;
359*0Sstevel@tonic-gate 			core = optarg;
360*0Sstevel@tonic-gate 			break;
361*0Sstevel@tonic-gate 		case 'N' :
362*0Sstevel@tonic-gate 			live_kernel = 0;
363*0Sstevel@tonic-gate 			kernel = optarg;
364*0Sstevel@tonic-gate 			break;
365*0Sstevel@tonic-gate 		case 'o' :
366*0Sstevel@tonic-gate 			role = getrole(optarg);
367*0Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
368*0Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
369*0Sstevel@tonic-gate 				return -1;
370*0Sstevel@tonic-gate 			}
371*0Sstevel@tonic-gate 			break;
372*0Sstevel@tonic-gate 		case 't' :
373*0Sstevel@tonic-gate 			type = gettype(optarg, NULL);
374*0Sstevel@tonic-gate 			if (type == IPLT_NONE) {
375*0Sstevel@tonic-gate 				fprintf(stderr, "unknown type '%s'\n", optarg);
376*0Sstevel@tonic-gate 				return -1;
377*0Sstevel@tonic-gate 			}
378*0Sstevel@tonic-gate 			break;
379*0Sstevel@tonic-gate 		case 'v' :
380*0Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
381*0Sstevel@tonic-gate 			break;
382*0Sstevel@tonic-gate 		}
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
385*0Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
386*0Sstevel@tonic-gate 		if (fd == -1) {
387*0Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
388*0Sstevel@tonic-gate 			exit(1);
389*0Sstevel@tonic-gate 		}
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	bzero((char *)&op, sizeof(op));
393*0Sstevel@tonic-gate 	if (poolname != NULL) {
394*0Sstevel@tonic-gate 		strncpy(op.iplo_name, poolname, sizeof(op.iplo_name));
395*0Sstevel@tonic-gate 		op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 	op.iplo_type = type;
398*0Sstevel@tonic-gate 	op.iplo_unit = role;
399*0Sstevel@tonic-gate 	op.iplo_size = sizeof(plstat);
400*0Sstevel@tonic-gate 	op.iplo_struct = &plstat;
401*0Sstevel@tonic-gate 	plstp = &plstat;
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	c = ioctl(fd, SIOCLOOKUPSTAT, &op);
404*0Sstevel@tonic-gate 	if (c == -1) {
405*0Sstevel@tonic-gate 		perror("ioctl(SIOCLOOKUPSTAT)");
406*0Sstevel@tonic-gate 		return -1;
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	if (openkmem(kernel, core) == -1)
410*0Sstevel@tonic-gate 		exit(-1);
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	if (role != IPL_LOGALL) {
413*0Sstevel@tonic-gate 		ptr = plstp->ipls_list[role];
414*0Sstevel@tonic-gate 		while (ptr != NULL) {
415*0Sstevel@tonic-gate 			ptr = printpool(ptr, kmemcpywrap, opts);
416*0Sstevel@tonic-gate 		}
417*0Sstevel@tonic-gate 	} else {
418*0Sstevel@tonic-gate 		for (role = 0; role <= IPL_LOGMAX; role++) {
419*0Sstevel@tonic-gate 			ptr = plstp->ipls_list[role];
420*0Sstevel@tonic-gate 			while (ptr != NULL) {
421*0Sstevel@tonic-gate 				ptr = printpool(ptr, kmemcpywrap, opts);
422*0Sstevel@tonic-gate 			}
423*0Sstevel@tonic-gate 		}
424*0Sstevel@tonic-gate 	}
425*0Sstevel@tonic-gate 	return 0;
426*0Sstevel@tonic-gate }
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate int poolstats(argc, argv)
430*0Sstevel@tonic-gate int argc;
431*0Sstevel@tonic-gate char *argv[];
432*0Sstevel@tonic-gate {
433*0Sstevel@tonic-gate 	int c, type, role, live_kernel;
434*0Sstevel@tonic-gate 	ip_pool_stat_t plstat;
435*0Sstevel@tonic-gate 	char *kernel, *core;
436*0Sstevel@tonic-gate 	iplookupop_t op;
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	core = NULL;
439*0Sstevel@tonic-gate 	kernel = NULL;
440*0Sstevel@tonic-gate 	live_kernel = 1;
441*0Sstevel@tonic-gate 	type = IPLT_ALL;
442*0Sstevel@tonic-gate 	role = IPL_LOGALL;
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	bzero((char *)&op, sizeof(op));
445*0Sstevel@tonic-gate 	op.iplo_struct = &plstat;
446*0Sstevel@tonic-gate 	op.iplo_size = sizeof(plstat);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "dM:N:o:t:v")) != -1)
449*0Sstevel@tonic-gate 		switch (c)
450*0Sstevel@tonic-gate 		{
451*0Sstevel@tonic-gate 		case 'd' :
452*0Sstevel@tonic-gate 			opts |= OPT_DEBUG;
453*0Sstevel@tonic-gate 			break;
454*0Sstevel@tonic-gate 		case 'M' :
455*0Sstevel@tonic-gate 			live_kernel = 0;
456*0Sstevel@tonic-gate 			core = optarg;
457*0Sstevel@tonic-gate 			break;
458*0Sstevel@tonic-gate 		case 'N' :
459*0Sstevel@tonic-gate 			live_kernel = 0;
460*0Sstevel@tonic-gate 			kernel = optarg;
461*0Sstevel@tonic-gate 			break;
462*0Sstevel@tonic-gate 		case 'o' :
463*0Sstevel@tonic-gate 			role = getrole(optarg);
464*0Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
465*0Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
466*0Sstevel@tonic-gate 				return -1;
467*0Sstevel@tonic-gate 			}
468*0Sstevel@tonic-gate 			break;
469*0Sstevel@tonic-gate 		case 't' :
470*0Sstevel@tonic-gate 			type = gettype(optarg, NULL);
471*0Sstevel@tonic-gate 			if (type != IPLT_POOL) {
472*0Sstevel@tonic-gate 				fprintf(stderr,
473*0Sstevel@tonic-gate 					"-s not supported for this type yet\n");
474*0Sstevel@tonic-gate 				return -1;
475*0Sstevel@tonic-gate 			}
476*0Sstevel@tonic-gate 			break;
477*0Sstevel@tonic-gate 		case 'v' :
478*0Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
479*0Sstevel@tonic-gate 			break;
480*0Sstevel@tonic-gate 		}
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
483*0Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
484*0Sstevel@tonic-gate 		if (fd == -1) {
485*0Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
486*0Sstevel@tonic-gate 			exit(1);
487*0Sstevel@tonic-gate 		}
488*0Sstevel@tonic-gate 	}
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING)) {
491*0Sstevel@tonic-gate 		c = ioctl(fd, SIOCLOOKUPSTAT, &op);
492*0Sstevel@tonic-gate 		if (c == -1) {
493*0Sstevel@tonic-gate 			perror("ioctl(SIOCLOOKUPSTAT)");
494*0Sstevel@tonic-gate 			return -1;
495*0Sstevel@tonic-gate 		}
496*0Sstevel@tonic-gate 		printf("Pools:\t%lu\n", plstat.ipls_pools);
497*0Sstevel@tonic-gate 		printf("Hash Tables:\t%lu\n", plstat.ipls_tables);
498*0Sstevel@tonic-gate 		printf("Nodes:\t%lu\n", plstat.ipls_nodes);
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 	return 0;
501*0Sstevel@tonic-gate }
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate int poolflush(argc, argv)
505*0Sstevel@tonic-gate int argc;
506*0Sstevel@tonic-gate char *argv[];
507*0Sstevel@tonic-gate {
508*0Sstevel@tonic-gate 	int c, role, type, arg;
509*0Sstevel@tonic-gate 	iplookupflush_t flush;
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	arg = IPLT_ALL;
512*0Sstevel@tonic-gate 	type = IPLT_ALL;
513*0Sstevel@tonic-gate 	role = IPL_LOGALL;
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "do:t:v")) != -1)
516*0Sstevel@tonic-gate 		switch (c)
517*0Sstevel@tonic-gate 		{
518*0Sstevel@tonic-gate 		case 'd' :
519*0Sstevel@tonic-gate 			opts |= OPT_DEBUG;
520*0Sstevel@tonic-gate 			break;
521*0Sstevel@tonic-gate 		case 'o' :
522*0Sstevel@tonic-gate 			role = getrole(optarg);
523*0Sstevel@tonic-gate 			if (role == IPL_LOGNONE) {
524*0Sstevel@tonic-gate 				fprintf(stderr, "unknown role '%s'\n", optarg);
525*0Sstevel@tonic-gate 				return -1;
526*0Sstevel@tonic-gate 			}
527*0Sstevel@tonic-gate 			break;
528*0Sstevel@tonic-gate 		case 't' :
529*0Sstevel@tonic-gate 			type = gettype(optarg, NULL);
530*0Sstevel@tonic-gate 			if (type == IPLT_NONE) {
531*0Sstevel@tonic-gate 				fprintf(stderr, "unknown type '%s'\n", optarg);
532*0Sstevel@tonic-gate 				return -1;
533*0Sstevel@tonic-gate 			}
534*0Sstevel@tonic-gate 			break;
535*0Sstevel@tonic-gate 		case 'v' :
536*0Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
537*0Sstevel@tonic-gate 			break;
538*0Sstevel@tonic-gate 		}
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING) && (fd == -1)) {
541*0Sstevel@tonic-gate 		fd = open(IPLOOKUP_NAME, O_RDWR);
542*0Sstevel@tonic-gate 		if (fd == -1) {
543*0Sstevel@tonic-gate 			perror("open(IPLOOKUP_NAME)");
544*0Sstevel@tonic-gate 			exit(1);
545*0Sstevel@tonic-gate 		}
546*0Sstevel@tonic-gate 	}
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	bzero((char *)&flush, sizeof(flush));
549*0Sstevel@tonic-gate 	flush.iplf_type = type;
550*0Sstevel@tonic-gate 	flush.iplf_unit = role;
551*0Sstevel@tonic-gate 	flush.iplf_arg = arg;
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 	if (!(opts & OPT_DONOTHING)) {
554*0Sstevel@tonic-gate 		if (ioctl(fd, SIOCLOOKUPFLUSH, &flush) == -1) {
555*0Sstevel@tonic-gate 			perror("ioctl(SIOCLOOKUPFLUSH)");
556*0Sstevel@tonic-gate 			exit(1);
557*0Sstevel@tonic-gate 		}
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	}
560*0Sstevel@tonic-gate 	printf("%u object%s flushed\n", flush.iplf_count,
561*0Sstevel@tonic-gate 	       (flush.iplf_count == 1) ? "" : "s");
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	return 0;
564*0Sstevel@tonic-gate }
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate int getrole(rolename)
568*0Sstevel@tonic-gate char *rolename;
569*0Sstevel@tonic-gate {
570*0Sstevel@tonic-gate 	int role;
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	if (!strcasecmp(rolename, "ipf")) {
573*0Sstevel@tonic-gate 		role = IPL_LOGIPF;
574*0Sstevel@tonic-gate #if 0
575*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "nat")) {
576*0Sstevel@tonic-gate 		role = IPL_LOGNAT;
577*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "state")) {
578*0Sstevel@tonic-gate 		role = IPL_LOGSTATE;
579*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "auth")) {
580*0Sstevel@tonic-gate 		role = IPL_LOGAUTH;
581*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "sync")) {
582*0Sstevel@tonic-gate 		role = IPL_LOGSYNC;
583*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "scan")) {
584*0Sstevel@tonic-gate 		role = IPL_LOGSCAN;
585*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "pool")) {
586*0Sstevel@tonic-gate 		role = IPL_LOGLOOKUP;
587*0Sstevel@tonic-gate 	} else if (!strcasecmp(rolename, "count")) {
588*0Sstevel@tonic-gate 		role = IPL_LOGCOUNT;
589*0Sstevel@tonic-gate #endif
590*0Sstevel@tonic-gate 	} else {
591*0Sstevel@tonic-gate 		role = IPL_LOGNONE;
592*0Sstevel@tonic-gate 	}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 	return role;
595*0Sstevel@tonic-gate }
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate int gettype(typename, minor)
599*0Sstevel@tonic-gate char *typename;
600*0Sstevel@tonic-gate u_int *minor;
601*0Sstevel@tonic-gate {
602*0Sstevel@tonic-gate 	int type;
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	if (!strcasecmp(optarg, "pool")) {
605*0Sstevel@tonic-gate 		type = IPLT_POOL;
606*0Sstevel@tonic-gate 	} else if (!strcasecmp(optarg, "hash")) {
607*0Sstevel@tonic-gate 		type = IPLT_HASH;
608*0Sstevel@tonic-gate 		if (minor != NULL)
609*0Sstevel@tonic-gate 			*minor = IPHASH_LOOKUP;
610*0Sstevel@tonic-gate 	} else if (!strcasecmp(optarg, "group-map")) {
611*0Sstevel@tonic-gate 		type = IPLT_HASH;
612*0Sstevel@tonic-gate 		if (minor != NULL)
613*0Sstevel@tonic-gate 			*minor = IPHASH_GROUPMAP;
614*0Sstevel@tonic-gate 	} else {
615*0Sstevel@tonic-gate 		type = IPLT_NONE;
616*0Sstevel@tonic-gate 	}
617*0Sstevel@tonic-gate 	return type;
618*0Sstevel@tonic-gate }
619