xref: /titanic_52/usr/src/cmd/ipf/tools/ipfstat.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001, 2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6*ab25eeb5Syz155240  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
77c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #ifdef __FreeBSD__
137c478bd9Sstevel@tonic-gate # ifndef __FreeBSD_cc_version
147c478bd9Sstevel@tonic-gate #  include <osreldate.h>
157c478bd9Sstevel@tonic-gate # else
167c478bd9Sstevel@tonic-gate #  if __FreeBSD_cc_version < 430000
177c478bd9Sstevel@tonic-gate #   include <osreldate.h>
187c478bd9Sstevel@tonic-gate #  endif
197c478bd9Sstevel@tonic-gate # endif
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
227c478bd9Sstevel@tonic-gate #include <fcntl.h>
23*ab25eeb5Syz155240 #ifdef linux
24*ab25eeb5Syz155240 # include <linux/a.out.h>
25*ab25eeb5Syz155240 #else
267c478bd9Sstevel@tonic-gate # include <nlist.h>
27*ab25eeb5Syz155240 #endif
287c478bd9Sstevel@tonic-gate #include <ctype.h>
29*ab25eeb5Syz155240 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
307c478bd9Sstevel@tonic-gate # include <stddef.h>
31*ab25eeb5Syz155240 #endif
327c478bd9Sstevel@tonic-gate #include "ipf.h"
33*ab25eeb5Syz155240 #include "netinet/ipl.h"
347c478bd9Sstevel@tonic-gate #if defined(STATETOP)
357c478bd9Sstevel@tonic-gate # if defined(_BSDI_VERSION)
36*ab25eeb5Syz155240 #  undef STATETOP
377c478bd9Sstevel@tonic-gate # endif
387c478bd9Sstevel@tonic-gate # if defined(__FreeBSD__) && \
397c478bd9Sstevel@tonic-gate      (!defined(__FreeBSD_version) || (__FreeBSD_version < 430000))
407c478bd9Sstevel@tonic-gate #  undef STATETOP
417c478bd9Sstevel@tonic-gate # endif
427c478bd9Sstevel@tonic-gate # if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105000000)
437c478bd9Sstevel@tonic-gate #  undef STATETOP
447c478bd9Sstevel@tonic-gate # endif
457c478bd9Sstevel@tonic-gate # if defined(sun)
467c478bd9Sstevel@tonic-gate #  if defined(__svr4__) || defined(__SVR4)
477c478bd9Sstevel@tonic-gate #   include <sys/select.h>
487c478bd9Sstevel@tonic-gate #  else
497c478bd9Sstevel@tonic-gate #   undef STATETOP	/* NOT supported on SunOS4 */
507c478bd9Sstevel@tonic-gate #  endif
517c478bd9Sstevel@tonic-gate # endif
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate #if defined(STATETOP) && !defined(linux)
547c478bd9Sstevel@tonic-gate # include <netinet/ip_var.h>
557c478bd9Sstevel@tonic-gate # include <netinet/tcp_fsm.h>
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate #ifdef STATETOP
58*ab25eeb5Syz155240 # include <ctype.h>
59*ab25eeb5Syz155240 # include <signal.h>
607c478bd9Sstevel@tonic-gate # if SOLARIS || defined(__NetBSD__) || defined(_BSDI_VERSION) || \
617c478bd9Sstevel@tonic-gate      defined(__sgi)
627c478bd9Sstevel@tonic-gate #  ifdef ERR
637c478bd9Sstevel@tonic-gate #   undef ERR
647c478bd9Sstevel@tonic-gate #  endif
65*ab25eeb5Syz155240 #  undef ISASCII
66*ab25eeb5Syz155240 #  undef ISPRINT
677c478bd9Sstevel@tonic-gate #  include <curses.h>
687c478bd9Sstevel@tonic-gate # else /* SOLARIS */
697c478bd9Sstevel@tonic-gate #  include <ncurses.h>
707c478bd9Sstevel@tonic-gate # endif /* SOLARIS */
717c478bd9Sstevel@tonic-gate #endif /* STATETOP */
727c478bd9Sstevel@tonic-gate #include "kmem.h"
737c478bd9Sstevel@tonic-gate #if defined(__NetBSD__) || (__OpenBSD__)
747c478bd9Sstevel@tonic-gate # include <paths.h>
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #if !defined(lint)
787c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)fils.c	1.21 4/20/96 (C) 1993-2000 Darren Reed";
79*ab25eeb5Syz155240 static const char rcsid[] = "@(#)$Id: ipfstat.c,v 1.44.2.12 2005/06/12 07:18:46 darrenr Exp $";
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifdef __hpux
837c478bd9Sstevel@tonic-gate # define	nlist	nlist64
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate extern	char	*optarg;
877c478bd9Sstevel@tonic-gate extern	int	optind;
88*ab25eeb5Syz155240 extern	int	opterr;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	PRINTF	(void)printf
917c478bd9Sstevel@tonic-gate #define	FPRINTF	(void)fprintf
927c478bd9Sstevel@tonic-gate #define	F_IN	0
937c478bd9Sstevel@tonic-gate #define	F_OUT	1
947c478bd9Sstevel@tonic-gate #define	F_ACIN	2
957c478bd9Sstevel@tonic-gate #define	F_ACOUT	3
967c478bd9Sstevel@tonic-gate static	char	*filters[4] = { "ipfilter(in)", "ipfilter(out)",
977c478bd9Sstevel@tonic-gate 				"ipacct(in)", "ipacct(out)" };
987c478bd9Sstevel@tonic-gate static	int	state_logging = -1;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate int	opts = 0;
1017c478bd9Sstevel@tonic-gate int	use_inet6 = 0;
1027c478bd9Sstevel@tonic-gate int	live_kernel = 1;
1037c478bd9Sstevel@tonic-gate int	state_fd = -1;
1047c478bd9Sstevel@tonic-gate int	ipf_fd = -1;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #ifdef STATETOP
1077c478bd9Sstevel@tonic-gate #define	STSTRSIZE 	80
1087c478bd9Sstevel@tonic-gate #define	STGROWSIZE	16
1097c478bd9Sstevel@tonic-gate #define	HOSTNMLEN	40
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	STSORT_PR	0
1127c478bd9Sstevel@tonic-gate #define	STSORT_PKTS	1
1137c478bd9Sstevel@tonic-gate #define	STSORT_BYTES	2
1147c478bd9Sstevel@tonic-gate #define	STSORT_TTL	3
1157c478bd9Sstevel@tonic-gate #define	STSORT_SRCIP	4
116*ab25eeb5Syz155240 #define	STSORT_SRCPT	5
117*ab25eeb5Syz155240 #define	STSORT_DSTIP	6
118*ab25eeb5Syz155240 #define	STSORT_DSTPT	7
119*ab25eeb5Syz155240 #define	STSORT_MAX	STSORT_DSTPT
1207c478bd9Sstevel@tonic-gate #define	STSORT_DEFAULT	STSORT_BYTES
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate typedef struct statetop {
1247c478bd9Sstevel@tonic-gate 	i6addr_t	st_src;
1257c478bd9Sstevel@tonic-gate 	i6addr_t	st_dst;
1267c478bd9Sstevel@tonic-gate 	u_short		st_sport;
1277c478bd9Sstevel@tonic-gate 	u_short 	st_dport;
1287c478bd9Sstevel@tonic-gate 	u_char		st_p;
129*ab25eeb5Syz155240 	u_char		st_v;
1307c478bd9Sstevel@tonic-gate 	u_char		st_state[2];
1317c478bd9Sstevel@tonic-gate 	U_QUAD_T	st_pkts;
1327c478bd9Sstevel@tonic-gate 	U_QUAD_T	st_bytes;
1337c478bd9Sstevel@tonic-gate 	u_long		st_age;
1347c478bd9Sstevel@tonic-gate } statetop_t;
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate 
137*ab25eeb5Syz155240 int	main __P((int, char *[]));
138*ab25eeb5Syz155240 
1397c478bd9Sstevel@tonic-gate static	void	showstats __P((friostat_t *, u_32_t));
1407c478bd9Sstevel@tonic-gate static	void	showfrstates __P((ipfrstat_t *));
1417c478bd9Sstevel@tonic-gate static	void	showlist __P((friostat_t *));
1427c478bd9Sstevel@tonic-gate static	void	showipstates __P((ips_stat_t *));
1437c478bd9Sstevel@tonic-gate static	void	showauthstates __P((fr_authstat_t *));
1447c478bd9Sstevel@tonic-gate static	void	showgroups __P((friostat_t *));
145*ab25eeb5Syz155240 static	void	usage __P((char *));
1467c478bd9Sstevel@tonic-gate static	void	printlist __P((frentry_t *, char *));
147*ab25eeb5Syz155240 static	void	parse_ipportstr __P((const char *, i6addr_t *, int *));
1487c478bd9Sstevel@tonic-gate static	void	ipfstate_live __P((char *, friostat_t **, ips_stat_t **,
1497c478bd9Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1507c478bd9Sstevel@tonic-gate static	void	ipfstate_dead __P((char *, friostat_t **, ips_stat_t **,
1517c478bd9Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1527c478bd9Sstevel@tonic-gate #ifdef STATETOP
153*ab25eeb5Syz155240 static	void	topipstates __P((i6addr_t, i6addr_t, int, int, int,
154*ab25eeb5Syz155240 				 int, int, int));
155*ab25eeb5Syz155240 static	void	sig_break __P((int));
156*ab25eeb5Syz155240 static	void	sig_resize __P((int));
157*ab25eeb5Syz155240 static	char	*getip __P((int, i6addr_t *));
1587c478bd9Sstevel@tonic-gate static	char	*ttl_to_string __P((long));
1597c478bd9Sstevel@tonic-gate static	int	sort_p __P((const void *, const void *));
1607c478bd9Sstevel@tonic-gate static	int	sort_pkts __P((const void *, const void *));
1617c478bd9Sstevel@tonic-gate static	int	sort_bytes __P((const void *, const void *));
1627c478bd9Sstevel@tonic-gate static	int	sort_ttl __P((const void *, const void *));
1637c478bd9Sstevel@tonic-gate static	int	sort_srcip __P((const void *, const void *));
164*ab25eeb5Syz155240 static	int	sort_srcpt __P((const void *, const void *));
1657c478bd9Sstevel@tonic-gate static	int	sort_dstip __P((const void *, const void *));
166*ab25eeb5Syz155240 static	int	sort_dstpt __P((const void *, const void *));
1677c478bd9Sstevel@tonic-gate #endif
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
170*ab25eeb5Syz155240 static void usage(name)
1717c478bd9Sstevel@tonic-gate char *name;
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate #ifdef  USE_INET6
174*ab25eeb5Syz155240 	fprintf(stderr, "Usage: %s [-6aAdfghIilnoRsv]\n", name);
1757c478bd9Sstevel@tonic-gate #else
176*ab25eeb5Syz155240 	fprintf(stderr, "Usage: %s [-aAdfghIilnoRsv]\n", name);
1777c478bd9Sstevel@tonic-gate #endif
178*ab25eeb5Syz155240 	fprintf(stderr, "       %s [-M corefile] [-N symbol-list]\n", name);
179*ab25eeb5Syz155240 #ifdef	USE_INET6
180*ab25eeb5Syz155240 	fprintf(stderr, "       %s -t [-6C] ", name);
181*ab25eeb5Syz155240 #else
182*ab25eeb5Syz155240 	fprintf(stderr, "       %s -t [-C] ", name);
183*ab25eeb5Syz155240 #endif
184*ab25eeb5Syz155240 	fprintf(stderr, "[-D destination address] [-P protocol] [-S source address] [-T refresh time]\n");
1857c478bd9Sstevel@tonic-gate 	exit(1);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate int main(argc,argv)
1907c478bd9Sstevel@tonic-gate int argc;
1917c478bd9Sstevel@tonic-gate char *argv[];
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	fr_authstat_t	frauthst;
1947c478bd9Sstevel@tonic-gate 	fr_authstat_t	*frauthstp = &frauthst;
1957c478bd9Sstevel@tonic-gate 	friostat_t fio;
1967c478bd9Sstevel@tonic-gate 	friostat_t *fiop = &fio;
1977c478bd9Sstevel@tonic-gate 	ips_stat_t ipsst;
1987c478bd9Sstevel@tonic-gate 	ips_stat_t *ipsstp = &ipsst;
1997c478bd9Sstevel@tonic-gate 	ipfrstat_t ifrst;
2007c478bd9Sstevel@tonic-gate 	ipfrstat_t *ifrstp = &ifrst;
2017c478bd9Sstevel@tonic-gate 	char	*device = IPL_NAME, *memf = NULL;
202*ab25eeb5Syz155240 	char	*options, *kern = NULL;
2037c478bd9Sstevel@tonic-gate 	int	c, myoptind;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	int protocol = -1;		/* -1 = wild card for any protocol */
2067c478bd9Sstevel@tonic-gate 	int refreshtime = 1; 		/* default update time */
2077c478bd9Sstevel@tonic-gate 	int sport = -1;			/* -1 = wild card for any source port */
2087c478bd9Sstevel@tonic-gate 	int dport = -1;			/* -1 = wild card for any dest port */
2097c478bd9Sstevel@tonic-gate 	int topclosed = 0;		/* do not show closed tcp sessions */
210*ab25eeb5Syz155240 	i6addr_t saddr, daddr;
2117c478bd9Sstevel@tonic-gate 	u_32_t frf;
2127c478bd9Sstevel@tonic-gate 
213*ab25eeb5Syz155240 #ifdef	USE_INET6
214*ab25eeb5Syz155240 	options = "6aACdfghIilnostvD:M:N:P:RS:T:";
215*ab25eeb5Syz155240 #else
216*ab25eeb5Syz155240 	options = "aACdfghIilnostvD:M:N:P:RS:T:";
217*ab25eeb5Syz155240 #endif
218*ab25eeb5Syz155240 
219*ab25eeb5Syz155240 	saddr.in4.s_addr = INADDR_ANY; 	/* default any v4 source addr */
220*ab25eeb5Syz155240 	daddr.in4.s_addr = INADDR_ANY; 	/* default any v4 dest addr */
221*ab25eeb5Syz155240 #ifdef	USE_INET6
222*ab25eeb5Syz155240 	saddr.in6 = in6addr_any;	/* default any v6 source addr */
223*ab25eeb5Syz155240 	daddr.in6 = in6addr_any;	/* default any v6 dest addr */
224*ab25eeb5Syz155240 #endif
225*ab25eeb5Syz155240 
226*ab25eeb5Syz155240 	/* Don't warn about invalid flags when we run getopt for the 1st time */
227*ab25eeb5Syz155240 	opterr = 0;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Parse these two arguments now lest there be any buffer overflows
2317c478bd9Sstevel@tonic-gate 	 * in the parsing of the rest.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	myoptind = optind;
234*ab25eeb5Syz155240 	while ((c = getopt(argc, argv, options)) != -1) {
2357c478bd9Sstevel@tonic-gate 		switch (c)
2367c478bd9Sstevel@tonic-gate 		{
2377c478bd9Sstevel@tonic-gate 		case 'M' :
2387c478bd9Sstevel@tonic-gate 			memf = optarg;
2397c478bd9Sstevel@tonic-gate 			live_kernel = 0;
2407c478bd9Sstevel@tonic-gate 			break;
2417c478bd9Sstevel@tonic-gate 		case 'N' :
2427c478bd9Sstevel@tonic-gate 			kern = optarg;
2437c478bd9Sstevel@tonic-gate 			live_kernel = 0;
2447c478bd9Sstevel@tonic-gate 			break;
2457c478bd9Sstevel@tonic-gate 		}
246*ab25eeb5Syz155240 	}
2477c478bd9Sstevel@tonic-gate 	optind = myoptind;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (live_kernel == 1) {
2507c478bd9Sstevel@tonic-gate 		if ((state_fd = open(IPSTATE_NAME, O_RDONLY)) == -1) {
251*ab25eeb5Syz155240 			perror("open(IPSTATE_NAME)");
2527c478bd9Sstevel@tonic-gate 			exit(-1);
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
255*ab25eeb5Syz155240 			fprintf(stderr, "open(%s)", device);
256*ab25eeb5Syz155240 			perror("");
2577c478bd9Sstevel@tonic-gate 			exit(-1);
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
261*ab25eeb5Syz155240 	if (kern != NULL || memf != NULL) {
2627c478bd9Sstevel@tonic-gate 		(void)setgid(getgid());
2637c478bd9Sstevel@tonic-gate 		(void)setreuid(getuid(), getuid());
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
266*ab25eeb5Syz155240 	if (live_kernel == 1)
267*ab25eeb5Syz155240 		(void) checkrev(device);
2687c478bd9Sstevel@tonic-gate 	if (openkmem(kern, memf) == -1)
2697c478bd9Sstevel@tonic-gate 		exit(-1);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	(void)setgid(getgid());
2727c478bd9Sstevel@tonic-gate 	(void)setreuid(getuid(), getuid());
2737c478bd9Sstevel@tonic-gate 
274*ab25eeb5Syz155240 	opterr = 1;
275*ab25eeb5Syz155240 
276*ab25eeb5Syz155240 	while ((c = getopt(argc, argv, options)) != -1)
2777c478bd9Sstevel@tonic-gate 	{
2787c478bd9Sstevel@tonic-gate 		switch (c)
2797c478bd9Sstevel@tonic-gate 		{
2807c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
2817c478bd9Sstevel@tonic-gate 		case '6' :
2827c478bd9Sstevel@tonic-gate 			use_inet6 = 1;
2837c478bd9Sstevel@tonic-gate 			break;
2847c478bd9Sstevel@tonic-gate #endif
2857c478bd9Sstevel@tonic-gate 		case 'a' :
2867c478bd9Sstevel@tonic-gate 			opts |= OPT_ACCNT|OPT_SHOWLIST;
2877c478bd9Sstevel@tonic-gate 			break;
2887c478bd9Sstevel@tonic-gate 		case 'A' :
2897c478bd9Sstevel@tonic-gate 			opts |= OPT_AUTHSTATS;
2907c478bd9Sstevel@tonic-gate 			break;
2917c478bd9Sstevel@tonic-gate 		case 'C' :
2927c478bd9Sstevel@tonic-gate 			topclosed = 1;
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 		case 'd' :
2957c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 		case 'D' :
2987c478bd9Sstevel@tonic-gate 			parse_ipportstr(optarg, &daddr, &dport);
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 		case 'f' :
3017c478bd9Sstevel@tonic-gate 			opts |= OPT_FRSTATES;
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 		case 'g' :
3047c478bd9Sstevel@tonic-gate 			opts |= OPT_GROUPS;
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 		case 'h' :
3077c478bd9Sstevel@tonic-gate 			opts |= OPT_HITS;
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 		case 'i' :
3107c478bd9Sstevel@tonic-gate 			opts |= OPT_INQUE|OPT_SHOWLIST;
3117c478bd9Sstevel@tonic-gate 			break;
3127c478bd9Sstevel@tonic-gate 		case 'I' :
3137c478bd9Sstevel@tonic-gate 			opts |= OPT_INACTIVE;
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 		case 'l' :
3167c478bd9Sstevel@tonic-gate 			opts |= OPT_SHOWLIST;
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		case 'M' :
3197c478bd9Sstevel@tonic-gate 			break;
3207c478bd9Sstevel@tonic-gate 		case 'N' :
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 		case 'n' :
3237c478bd9Sstevel@tonic-gate 			opts |= OPT_SHOWLINENO;
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 		case 'o' :
3267c478bd9Sstevel@tonic-gate 			opts |= OPT_OUTQUE|OPT_SHOWLIST;
3277c478bd9Sstevel@tonic-gate 			break;
3287c478bd9Sstevel@tonic-gate 		case 'P' :
329*ab25eeb5Syz155240 			protocol = getproto(optarg);
330*ab25eeb5Syz155240 			if (protocol == -1) {
3317c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s: Invalid protocol: %s\n",
3327c478bd9Sstevel@tonic-gate 					argv[0], optarg);
3337c478bd9Sstevel@tonic-gate 				exit(-2);
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 			break;
336*ab25eeb5Syz155240 		case 'R' :
337*ab25eeb5Syz155240 			opts |= OPT_NORESOLVE;
338*ab25eeb5Syz155240 			break;
3397c478bd9Sstevel@tonic-gate 		case 's' :
3407c478bd9Sstevel@tonic-gate 			opts |= OPT_IPSTATES;
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		case 'S' :
3437c478bd9Sstevel@tonic-gate 			parse_ipportstr(optarg, &saddr, &sport);
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 		case 't' :
3467c478bd9Sstevel@tonic-gate #ifdef STATETOP
3477c478bd9Sstevel@tonic-gate 			opts |= OPT_STATETOP;
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate #else
3507c478bd9Sstevel@tonic-gate 			fprintf(stderr,
3517c478bd9Sstevel@tonic-gate 				"%s: state top facility not compiled in\n",
3527c478bd9Sstevel@tonic-gate 				argv[0]);
3537c478bd9Sstevel@tonic-gate 			exit(-2);
3547c478bd9Sstevel@tonic-gate #endif
3557c478bd9Sstevel@tonic-gate 		case 'T' :
3567c478bd9Sstevel@tonic-gate 			if (!sscanf(optarg, "%d", &refreshtime) ||
3577c478bd9Sstevel@tonic-gate 				    (refreshtime <= 0)) {
3587c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3597c478bd9Sstevel@tonic-gate 					"%s: Invalid refreshtime < 1 : %s\n",
3607c478bd9Sstevel@tonic-gate 					argv[0], optarg);
3617c478bd9Sstevel@tonic-gate 				exit(-2);
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case 'v' :
3657c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
3667c478bd9Sstevel@tonic-gate 			opts |= OPT_UNDEF;
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 		default :
369*ab25eeb5Syz155240 			usage(argv[0]);
3707c478bd9Sstevel@tonic-gate 			break;
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (live_kernel == 1) {
3757c478bd9Sstevel@tonic-gate 		bzero((char *)&fio, sizeof(fio));
3767c478bd9Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
3777c478bd9Sstevel@tonic-gate 		bzero((char *)&ifrst, sizeof(ifrst));
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		ipfstate_live(device, &fiop, &ipsstp, &ifrstp,
3807c478bd9Sstevel@tonic-gate 			      &frauthstp, &frf);
3817c478bd9Sstevel@tonic-gate 	} else
3827c478bd9Sstevel@tonic-gate 		ipfstate_dead(kern, &fiop, &ipsstp, &ifrstp, &frauthstp, &frf);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (opts & OPT_IPSTATES) {
3857c478bd9Sstevel@tonic-gate 		showipstates(ipsstp);
3867c478bd9Sstevel@tonic-gate 	} else if (opts & OPT_SHOWLIST) {
3877c478bd9Sstevel@tonic-gate 		showlist(fiop);
3887c478bd9Sstevel@tonic-gate 		if ((opts & OPT_OUTQUE) && (opts & OPT_INQUE)){
3897c478bd9Sstevel@tonic-gate 			opts &= ~OPT_OUTQUE;
3907c478bd9Sstevel@tonic-gate 			showlist(fiop);
3917c478bd9Sstevel@tonic-gate 		}
392*ab25eeb5Syz155240 	} else if (opts & OPT_FRSTATES)
3937c478bd9Sstevel@tonic-gate 		showfrstates(ifrstp);
3947c478bd9Sstevel@tonic-gate #ifdef STATETOP
3957c478bd9Sstevel@tonic-gate 	else if (opts & OPT_STATETOP)
396*ab25eeb5Syz155240 		topipstates(saddr, daddr, sport, dport, protocol,
397*ab25eeb5Syz155240 			    use_inet6 ? 6 : 4, refreshtime, topclosed);
3987c478bd9Sstevel@tonic-gate #endif
3997c478bd9Sstevel@tonic-gate 	else if (opts & OPT_AUTHSTATS)
4007c478bd9Sstevel@tonic-gate 		showauthstates(frauthstp);
4017c478bd9Sstevel@tonic-gate 	else if (opts & OPT_GROUPS)
4027c478bd9Sstevel@tonic-gate 		showgroups(fiop);
4037c478bd9Sstevel@tonic-gate 	else
4047c478bd9Sstevel@tonic-gate 		showstats(fiop, frf);
405*ab25eeb5Syz155240 
4067c478bd9Sstevel@tonic-gate 	return 0;
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * Fill in the stats structures from the live kernel, using a combination
4127c478bd9Sstevel@tonic-gate  * of ioctl's and copying directly from kernel memory.
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate static void ipfstate_live(device, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
4157c478bd9Sstevel@tonic-gate char *device;
4167c478bd9Sstevel@tonic-gate friostat_t **fiopp;
4177c478bd9Sstevel@tonic-gate ips_stat_t **ipsstpp;
4187c478bd9Sstevel@tonic-gate ipfrstat_t **ifrstpp;
4197c478bd9Sstevel@tonic-gate fr_authstat_t **frauthstpp;
4207c478bd9Sstevel@tonic-gate u_32_t *frfp;
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if (checkrev(device) == -1) {
4257c478bd9Sstevel@tonic-gate 		fprintf(stderr, "User/kernel version check failed\n");
4267c478bd9Sstevel@tonic-gate 		exit(1);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) == 0) {
4307c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4317c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4327c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(friostat_t);
4337c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*fiopp;
4347c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_IPFSTAT;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFS, &ipfo) == -1) {
4377c478bd9Sstevel@tonic-gate 			perror("ioctl(ipf:SIOCGETFS)");
4387c478bd9Sstevel@tonic-gate 			exit(-1);
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFF, frfp) == -1)
4427c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGETFF)");
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if ((opts & OPT_IPSTATES) != 0) {
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4487c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4497c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ips_stat_t);
4507c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ipsstpp;
4517c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_STATESTAT;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
4547c478bd9Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETFS)");
4557c478bd9Sstevel@tonic-gate 			exit(-1);
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 		if (ioctl(state_fd, SIOCGETLG, &state_logging) == -1) {
4587c478bd9Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETLG)");
4597c478bd9Sstevel@tonic-gate 			exit(-1);
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if ((opts & OPT_FRSTATES) != 0) {
4647c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4657c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4667c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ipfrstat_t);
4677c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ifrstpp;
4687c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_FRAGSTAT;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGFRST, &ipfo) == -1) {
4717c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGFRST)");
4727c478bd9Sstevel@tonic-gate 			exit(-1);
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
4777c478bd9Sstevel@tonic-gate 		PRINTF("opts %#x name %s\n", opts, device);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) != 0) {
4807c478bd9Sstevel@tonic-gate 		if (ipf_fd >= 0) {
4817c478bd9Sstevel@tonic-gate 			close(ipf_fd);
4827c478bd9Sstevel@tonic-gate 			ipf_fd = -1;
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		device = IPAUTH_NAME;
4857c478bd9Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
4867c478bd9Sstevel@tonic-gate 			perror("open");
4877c478bd9Sstevel@tonic-gate 			exit(-1);
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4917c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4927c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(fr_authstat_t);
4937c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*frauthstpp;
4947c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_AUTHSTAT;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	    	if (ioctl(ipf_fd, SIOCATHST, &ipfo) == -1) {
4977c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCATHST)");
4987c478bd9Sstevel@tonic-gate 			exit(-1);
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * Build up the stats structures from data held in the "core" memory.
5067c478bd9Sstevel@tonic-gate  * This is mainly useful when looking at data in crash dumps and ioctl's
5077c478bd9Sstevel@tonic-gate  * just won't work any more.
5087c478bd9Sstevel@tonic-gate  */
5097c478bd9Sstevel@tonic-gate static void ipfstate_dead(kernel, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
5107c478bd9Sstevel@tonic-gate char *kernel;
5117c478bd9Sstevel@tonic-gate friostat_t **fiopp;
5127c478bd9Sstevel@tonic-gate ips_stat_t **ipsstpp;
5137c478bd9Sstevel@tonic-gate ipfrstat_t **ifrstpp;
5147c478bd9Sstevel@tonic-gate fr_authstat_t **frauthstpp;
5157c478bd9Sstevel@tonic-gate u_32_t *frfp;
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	static fr_authstat_t frauthst, *frauthstp;
5187c478bd9Sstevel@tonic-gate 	static ips_stat_t ipsst, *ipsstp;
5197c478bd9Sstevel@tonic-gate 	static ipfrstat_t ifrst, *ifrstp;
5207c478bd9Sstevel@tonic-gate 	static friostat_t fio, *fiop;
5217c478bd9Sstevel@tonic-gate 	int temp;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	void *rules[2][2];
5247c478bd9Sstevel@tonic-gate 	struct nlist deadlist[43] = {
5257c478bd9Sstevel@tonic-gate 		{ "fr_authstats" },		/* 0 */
5267c478bd9Sstevel@tonic-gate 		{ "fae_list" },
5277c478bd9Sstevel@tonic-gate 		{ "ipauth" },
5287c478bd9Sstevel@tonic-gate 		{ "fr_authlist" },
5297c478bd9Sstevel@tonic-gate 		{ "fr_authstart" },
5307c478bd9Sstevel@tonic-gate 		{ "fr_authend" },		/* 5 */
5317c478bd9Sstevel@tonic-gate 		{ "fr_authnext" },
5327c478bd9Sstevel@tonic-gate 		{ "fr_auth" },
5337c478bd9Sstevel@tonic-gate 		{ "fr_authused" },
5347c478bd9Sstevel@tonic-gate 		{ "fr_authsize" },
5357c478bd9Sstevel@tonic-gate 		{ "fr_defaultauthage" },	/* 10 */
5367c478bd9Sstevel@tonic-gate 		{ "fr_authpkts" },
5377c478bd9Sstevel@tonic-gate 		{ "fr_auth_lock" },
5387c478bd9Sstevel@tonic-gate 		{ "frstats" },
5397c478bd9Sstevel@tonic-gate 		{ "ips_stats" },
5407c478bd9Sstevel@tonic-gate 		{ "ips_num" },			/* 15 */
5417c478bd9Sstevel@tonic-gate 		{ "ips_wild" },
5427c478bd9Sstevel@tonic-gate 		{ "ips_list" },
5437c478bd9Sstevel@tonic-gate 		{ "ips_table" },
5447c478bd9Sstevel@tonic-gate 		{ "fr_statemax" },
5457c478bd9Sstevel@tonic-gate 		{ "fr_statesize" },		/* 20 */
5467c478bd9Sstevel@tonic-gate 		{ "fr_state_doflush" },
5477c478bd9Sstevel@tonic-gate 		{ "fr_state_lock" },
5487c478bd9Sstevel@tonic-gate 		{ "ipfr_heads" },
5497c478bd9Sstevel@tonic-gate 		{ "ipfr_nattab" },
5507c478bd9Sstevel@tonic-gate 		{ "ipfr_stats" },		/* 25 */
5517c478bd9Sstevel@tonic-gate 		{ "ipfr_inuse" },
5527c478bd9Sstevel@tonic-gate 		{ "fr_ipfrttl" },
5537c478bd9Sstevel@tonic-gate 		{ "fr_frag_lock" },
5547c478bd9Sstevel@tonic-gate 		{ "ipfr_timer_id" },
5557c478bd9Sstevel@tonic-gate 		{ "fr_nat_lock" },		/* 30 */
5567c478bd9Sstevel@tonic-gate 		{ "ipfilter" },
5577c478bd9Sstevel@tonic-gate 		{ "ipfilter6" },
5587c478bd9Sstevel@tonic-gate 		{ "ipacct" },
5597c478bd9Sstevel@tonic-gate 		{ "ipacct6" },
5607c478bd9Sstevel@tonic-gate 		{ "ipl_frouteok" },		/* 35 */
5617c478bd9Sstevel@tonic-gate 		{ "fr_running" },
5627c478bd9Sstevel@tonic-gate 		{ "ipfgroups" },
5637c478bd9Sstevel@tonic-gate 		{ "fr_active" },
5647c478bd9Sstevel@tonic-gate 		{ "fr_pass" },
5657c478bd9Sstevel@tonic-gate 		{ "fr_flags" },			/* 40 */
5667c478bd9Sstevel@tonic-gate 		{ "ipstate_logging" },
5677c478bd9Sstevel@tonic-gate 		{ NULL }
5687c478bd9Sstevel@tonic-gate 	};
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	frauthstp = &frauthst;
5727c478bd9Sstevel@tonic-gate 	ipsstp = &ipsst;
5737c478bd9Sstevel@tonic-gate 	ifrstp = &ifrst;
5747c478bd9Sstevel@tonic-gate 	fiop = &fio;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	*frfp = 0;
5777c478bd9Sstevel@tonic-gate 	*fiopp = fiop;
5787c478bd9Sstevel@tonic-gate 	*ipsstpp = ipsstp;
5797c478bd9Sstevel@tonic-gate 	*ifrstpp = ifrstp;
5807c478bd9Sstevel@tonic-gate 	*frauthstpp = frauthstp;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	bzero((char *)fiop, sizeof(*fiop));
5837c478bd9Sstevel@tonic-gate 	bzero((char *)ipsstp, sizeof(*ipsstp));
5847c478bd9Sstevel@tonic-gate 	bzero((char *)ifrstp, sizeof(*ifrstp));
5857c478bd9Sstevel@tonic-gate 	bzero((char *)frauthstp, sizeof(*frauthstp));
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (nlist(kernel, deadlist) == -1) {
5887c478bd9Sstevel@tonic-gate 		fprintf(stderr, "nlist error\n");
5897c478bd9Sstevel@tonic-gate 		return;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * This is for SIOCGETFF.
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	kmemcpy((char *)frfp, (u_long)deadlist[40].n_value, sizeof(*frfp));
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * f_locks is a combination of the lock variable from each part of
5997c478bd9Sstevel@tonic-gate 	 * ipfilter (state, auth, nat, fragments).
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	kmemcpy((char *)fiop, (u_long)deadlist[13].n_value, sizeof(*fiop));
6027c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[22].n_value,
6037c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[0]));
6047c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[30].n_value,
6057c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[1]));
6067c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[2], (u_long)deadlist[28].n_value,
6077c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[2]));
6087c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[3], (u_long)deadlist[12].n_value,
6097c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[3]));
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/*
6127c478bd9Sstevel@tonic-gate 	 * Get pointers to each list of rules (active, inactive, in, out)
6137c478bd9Sstevel@tonic-gate 	 */
6147c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[31].n_value, sizeof(rules));
6157c478bd9Sstevel@tonic-gate 	fiop->f_fin[0] = rules[0][0];
6167c478bd9Sstevel@tonic-gate 	fiop->f_fin[1] = rules[0][1];
6177c478bd9Sstevel@tonic-gate 	fiop->f_fout[0] = rules[1][0];
6187c478bd9Sstevel@tonic-gate 	fiop->f_fout[1] = rules[1][1];
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * Same for IPv6, except make them null if support for it is not
6227c478bd9Sstevel@tonic-gate 	 * being compiled in.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
6257c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[32].n_value, sizeof(rules));
6267c478bd9Sstevel@tonic-gate 	fiop->f_fin6[0] = rules[0][0];
6277c478bd9Sstevel@tonic-gate 	fiop->f_fin6[1] = rules[0][1];
6287c478bd9Sstevel@tonic-gate 	fiop->f_fout6[0] = rules[1][0];
6297c478bd9Sstevel@tonic-gate 	fiop->f_fout6[1] = rules[1][1];
6307c478bd9Sstevel@tonic-gate #else
6317c478bd9Sstevel@tonic-gate 	fiop->f_fin6[0] = NULL;
6327c478bd9Sstevel@tonic-gate 	fiop->f_fin6[1] = NULL;
6337c478bd9Sstevel@tonic-gate 	fiop->f_fout6[0] = NULL;
6347c478bd9Sstevel@tonic-gate 	fiop->f_fout6[1] = NULL;
6357c478bd9Sstevel@tonic-gate #endif
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * Now get accounting rules pointers.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[33].n_value, sizeof(rules));
6417c478bd9Sstevel@tonic-gate 	fiop->f_acctin[0] = rules[0][0];
6427c478bd9Sstevel@tonic-gate 	fiop->f_acctin[1] = rules[0][1];
6437c478bd9Sstevel@tonic-gate 	fiop->f_acctout[0] = rules[1][0];
6447c478bd9Sstevel@tonic-gate 	fiop->f_acctout[1] = rules[1][1];
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
6477c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[34].n_value, sizeof(rules));
6487c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[0] = rules[0][0];
6497c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[1] = rules[0][1];
6507c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[0] = rules[1][0];
6517c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[1] = rules[1][1];
6527c478bd9Sstevel@tonic-gate #else
6537c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[0] = NULL;
6547c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[1] = NULL;
6557c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[0] = NULL;
6567c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[1] = NULL;
6577c478bd9Sstevel@tonic-gate #endif
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * A collection of "global" variables used inside the kernel which
6617c478bd9Sstevel@tonic-gate 	 * are all collected in friostat_t via ioctl.
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_froute, (u_long)deadlist[35].n_value,
6647c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_froute));
6657c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_running, (u_long)deadlist[36].n_value,
6667c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_running));
6677c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_groups, (u_long)deadlist[37].n_value,
6687c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_groups));
6697c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_active, (u_long)deadlist[38].n_value,
6707c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_active));
6717c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_defpass, (u_long)deadlist[39].n_value,
6727c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_defpass));
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * Build up the state information stats structure.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate 	kmemcpy((char *)ipsstp, (u_long)deadlist[14].n_value, sizeof(*ipsstp));
6787c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&temp, (u_long)deadlist[15].n_value, sizeof(temp));
6797c478bd9Sstevel@tonic-gate 	ipsstp->iss_active = temp;
6807c478bd9Sstevel@tonic-gate 	ipsstp->iss_table = (void *)deadlist[18].n_value;
6817c478bd9Sstevel@tonic-gate 	ipsstp->iss_list = (void *)deadlist[17].n_value;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/*
6847c478bd9Sstevel@tonic-gate 	 * Build up the authentiation information stats structure.
6857c478bd9Sstevel@tonic-gate 	 */
6867c478bd9Sstevel@tonic-gate 	kmemcpy((char *)frauthstp, (u_long)deadlist[0].n_value,
6877c478bd9Sstevel@tonic-gate 		sizeof(*frauthstp));
6887c478bd9Sstevel@tonic-gate 	frauthstp->fas_faelist = (void *)deadlist[1].n_value;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Build up the fragment information stats structure.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 	kmemcpy((char *)ifrstp, (u_long)deadlist[25].n_value,
6947c478bd9Sstevel@tonic-gate 		sizeof(*ifrstp));
6957c478bd9Sstevel@tonic-gate 	ifrstp->ifs_table = (void *)deadlist[23].n_value;
6967c478bd9Sstevel@tonic-gate 	ifrstp->ifs_nattab = (void *)deadlist[24].n_value;
6977c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&ifrstp->ifs_inuse, (u_long)deadlist[26].n_value,
6987c478bd9Sstevel@tonic-gate 		sizeof(ifrstp->ifs_inuse));
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Get logging on/off switches
7027c478bd9Sstevel@tonic-gate 	 */
7037c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&state_logging, (u_long)deadlist[41].n_value,
7047c478bd9Sstevel@tonic-gate 		sizeof(state_logging));
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate  * Display the kernel stats for packets blocked and passed and other
7107c478bd9Sstevel@tonic-gate  * associated running totals which are kept.
7117c478bd9Sstevel@tonic-gate  */
7127c478bd9Sstevel@tonic-gate static	void	showstats(fp, frf)
7137c478bd9Sstevel@tonic-gate struct	friostat	*fp;
7147c478bd9Sstevel@tonic-gate u_32_t frf;
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	PRINTF("bad packets:\t\tin %lu\tout %lu\n",
7187c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
7197c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
7207c478bd9Sstevel@tonic-gate 	PRINTF(" IPv6 packets:\t\tin %lu out %lu\n",
7217c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ipv6, fp->f_st[1].fr_ipv6);
7227c478bd9Sstevel@tonic-gate #endif
7237c478bd9Sstevel@tonic-gate 	PRINTF(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
7247c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
7257c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_nom);
7267c478bd9Sstevel@tonic-gate 	PRINTF(" counted %lu short %lu\n",
7277c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_acct, fp->f_st[0].fr_short);
7287c478bd9Sstevel@tonic-gate 	PRINTF("output packets:\t\tblocked %lu passed %lu nomatch %lu",
7297c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
7307c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_nom);
7317c478bd9Sstevel@tonic-gate 	PRINTF(" counted %lu short %lu\n",
7327c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_acct, fp->f_st[1].fr_short);
7337c478bd9Sstevel@tonic-gate 	PRINTF(" input packets logged:\tblocked %lu passed %lu\n",
7347c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
7357c478bd9Sstevel@tonic-gate 	PRINTF("output packets logged:\tblocked %lu passed %lu\n",
7367c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
7377c478bd9Sstevel@tonic-gate 	PRINTF(" packets logged:\tinput %lu output %lu\n",
7387c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pkl, fp->f_st[1].fr_pkl);
7397c478bd9Sstevel@tonic-gate 	PRINTF(" log failures:\t\tinput %lu output %lu\n",
7407c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_skip, fp->f_st[1].fr_skip);
741*ab25eeb5Syz155240 	PRINTF("fragment state(in):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
742*ab25eeb5Syz155240 			fp->f_st[0].fr_nfr, fp->f_st[0].fr_bnfr,
743*ab25eeb5Syz155240 			fp->f_st[0].fr_cfr);
744*ab25eeb5Syz155240 	PRINTF("fragment state(out):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
745*ab25eeb5Syz155240 			fp->f_st[1].fr_nfr, fp->f_st[1].fr_bnfr,
746*ab25eeb5Syz155240 			fp->f_st[0].fr_cfr);
7477c478bd9Sstevel@tonic-gate 	PRINTF("packet state(in):\tkept %lu\tlost %lu\n",
7487c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ads, fp->f_st[0].fr_bads);
7497c478bd9Sstevel@tonic-gate 	PRINTF("packet state(out):\tkept %lu\tlost %lu\n",
7507c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_ads, fp->f_st[1].fr_bads);
7517c478bd9Sstevel@tonic-gate 	PRINTF("ICMP replies:\t%lu\tTCP RSTs sent:\t%lu\n",
7527c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ret, fp->f_st[1].fr_ret);
7537c478bd9Sstevel@tonic-gate 	PRINTF("Invalid source(in):\t%lu\n", fp->f_st[0].fr_badsrc);
7547c478bd9Sstevel@tonic-gate 	PRINTF("Result cache hits(in):\t%lu\t(out):\t%lu\n",
7557c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_chit, fp->f_st[1].fr_chit);
7567c478bd9Sstevel@tonic-gate 	PRINTF("IN Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7577c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pull[0], fp->f_st[0].fr_pull[1]);
7587c478bd9Sstevel@tonic-gate 	PRINTF("OUT Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7597c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_pull[0], fp->f_st[1].fr_pull[1]);
7607c478bd9Sstevel@tonic-gate 	PRINTF("Fastroute successes:\t%lu\tfailures:\t%lu\n",
7617c478bd9Sstevel@tonic-gate 			fp->f_froute[0], fp->f_froute[1]);
7627c478bd9Sstevel@tonic-gate 	PRINTF("TCP cksum fails(in):\t%lu\t(out):\t%lu\n",
7637c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_tcpbad, fp->f_st[1].fr_tcpbad);
7647c478bd9Sstevel@tonic-gate 	PRINTF("IPF Ticks:\t%lu\n", fp->f_ticks);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	PRINTF("Packet log flags set: (%#x)\n", frf);
7677c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGPASS)
7687c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets passed through filter\n");
7697c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGBLOCK)
7707c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets blocked by filter\n");
7717c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGNOMATCH)
7727c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets not matched by filter\n");
7737c478bd9Sstevel@tonic-gate 	if (!frf)
7747c478bd9Sstevel@tonic-gate 		PRINTF("\tnone\n");
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate  * Print out a list of rules from the kernel, starting at the one passed.
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate static void printlist(fp, comment)
7827c478bd9Sstevel@tonic-gate frentry_t *fp;
7837c478bd9Sstevel@tonic-gate char *comment;
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	struct	frentry	fb, *fg;
7867c478bd9Sstevel@tonic-gate 	char	*data;
7877c478bd9Sstevel@tonic-gate 	u_32_t	type;
7887c478bd9Sstevel@tonic-gate 	int	n;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	for (n = 1; fp; n++) {
7917c478bd9Sstevel@tonic-gate 		if (kmemcpy((char *)&fb, (u_long)fp, sizeof(fb)) == -1) {
7927c478bd9Sstevel@tonic-gate 			perror("kmemcpy");
7937c478bd9Sstevel@tonic-gate 			return;
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		fp = &fb;
7967c478bd9Sstevel@tonic-gate 		if (opts & (OPT_HITS|OPT_VERBOSE))
7977c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
7987c478bd9Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_hits);
7997c478bd9Sstevel@tonic-gate #else
8007c478bd9Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_hits);
8017c478bd9Sstevel@tonic-gate #endif
8027c478bd9Sstevel@tonic-gate 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
8037c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
8047c478bd9Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_bytes);
8057c478bd9Sstevel@tonic-gate #else
8067c478bd9Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_bytes);
8077c478bd9Sstevel@tonic-gate #endif
8087c478bd9Sstevel@tonic-gate 		if (opts & OPT_SHOWLINENO)
8097c478bd9Sstevel@tonic-gate 			PRINTF("@%d ", n);
8107c478bd9Sstevel@tonic-gate 		data = NULL;
8117c478bd9Sstevel@tonic-gate 		type = fp->fr_type & ~FR_T_BUILTIN;
8127c478bd9Sstevel@tonic-gate 		if (type == FR_T_IPF || type == FR_T_BPFOPC) {
8137c478bd9Sstevel@tonic-gate 			if (fp->fr_dsize) {
8147c478bd9Sstevel@tonic-gate 				data = malloc(fp->fr_dsize);
8155e985db5Sschuster 				if (data == NULL) {
8165e985db5Sschuster 					perror("malloc");
8175e985db5Sschuster 					exit(1);
8185e985db5Sschuster 				}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 				if (kmemcpy(data, (u_long)fp->fr_data,
8217c478bd9Sstevel@tonic-gate 					    fp->fr_dsize) == -1) {
8227c478bd9Sstevel@tonic-gate 					perror("kmemcpy");
8237c478bd9Sstevel@tonic-gate 					return;
8247c478bd9Sstevel@tonic-gate 				}
8257c478bd9Sstevel@tonic-gate 				fp->fr_data = data;
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		printfr(fp, ioctl);
830*ab25eeb5Syz155240 		if (opts & OPT_DEBUG) {
8317c478bd9Sstevel@tonic-gate 			binprint(fp, sizeof(*fp));
8327c478bd9Sstevel@tonic-gate 			if (fp->fr_data != NULL && fp->fr_dsize > 0)
8337c478bd9Sstevel@tonic-gate 				binprint(fp->fr_data, fp->fr_dsize);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 		if (data != NULL)
8367c478bd9Sstevel@tonic-gate 			free(data);
8377c478bd9Sstevel@tonic-gate 		if (fp->fr_grp != NULL) {
8387c478bd9Sstevel@tonic-gate 			if (!kmemcpy((char *)&fg, (u_long)fp->fr_grp,
8397c478bd9Sstevel@tonic-gate 				     sizeof(fg)))
8407c478bd9Sstevel@tonic-gate 				printlist(fg, comment);
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 		if (type == FR_T_CALLFUNC) {
8437c478bd9Sstevel@tonic-gate 			printlist(fp->fr_data, "# callfunc: ");
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 		fp = fp->fr_next;
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * print out all of the asked for rule sets, using the stats struct as
8517c478bd9Sstevel@tonic-gate  * the base from which to get the pointers.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate static	void	showlist(fiop)
8547c478bd9Sstevel@tonic-gate struct	friostat	*fiop;
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	struct	frentry	*fp = NULL;
8577c478bd9Sstevel@tonic-gate 	int	i, set;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	set = fiop->f_active;
8607c478bd9Sstevel@tonic-gate 	if (opts & OPT_INACTIVE)
8617c478bd9Sstevel@tonic-gate 		set = 1 - set;
8627c478bd9Sstevel@tonic-gate 	if (opts & OPT_ACCNT) {
8637c478bd9Sstevel@tonic-gate #ifdef USE_INET6
8647c478bd9Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
8657c478bd9Sstevel@tonic-gate 			i = F_ACOUT;
8667c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout6[set];
8677c478bd9Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
8687c478bd9Sstevel@tonic-gate 			i = F_ACIN;
8697c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin6[set];
8707c478bd9Sstevel@tonic-gate 		} else
8717c478bd9Sstevel@tonic-gate #endif
8727c478bd9Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
8737c478bd9Sstevel@tonic-gate 			i = F_ACOUT;
8747c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout[set];
8757c478bd9Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
8767c478bd9Sstevel@tonic-gate 			i = F_ACIN;
8777c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin[set];
8787c478bd9Sstevel@tonic-gate 		} else {
8797c478bd9Sstevel@tonic-gate 			FPRINTF(stderr, "No -i or -o given with -a\n");
8807c478bd9Sstevel@tonic-gate 			return;
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 	} else {
8837c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
8847c478bd9Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
8857c478bd9Sstevel@tonic-gate 			i = F_OUT;
8867c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout6[set];
8877c478bd9Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
8887c478bd9Sstevel@tonic-gate 			i = F_IN;
8897c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin6[set];
8907c478bd9Sstevel@tonic-gate 		} else
8917c478bd9Sstevel@tonic-gate #endif
8927c478bd9Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
8937c478bd9Sstevel@tonic-gate 			i = F_OUT;
8947c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout[set];
8957c478bd9Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
8967c478bd9Sstevel@tonic-gate 			i = F_IN;
8977c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin[set];
8987c478bd9Sstevel@tonic-gate 		} else
8997c478bd9Sstevel@tonic-gate 			return;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
9027c478bd9Sstevel@tonic-gate 		FPRINTF(stderr, "showlist:opts %#x i %d\n", opts, i);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
9057c478bd9Sstevel@tonic-gate 		PRINTF("fp %p set %d\n", fp, set);
9067c478bd9Sstevel@tonic-gate 	if (!fp) {
9077c478bd9Sstevel@tonic-gate 		FPRINTF(stderr, "empty list for %s%s\n",
9087c478bd9Sstevel@tonic-gate 			(opts & OPT_INACTIVE) ? "inactive " : "", filters[i]);
9097c478bd9Sstevel@tonic-gate 		return;
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	printlist(fp, NULL);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate /*
9167c478bd9Sstevel@tonic-gate  * Display ipfilter stateful filtering information
9177c478bd9Sstevel@tonic-gate  */
9187c478bd9Sstevel@tonic-gate static void showipstates(ipsp)
9197c478bd9Sstevel@tonic-gate ips_stat_t *ipsp;
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	u_long minlen, maxlen, totallen, *buckets;
9227c478bd9Sstevel@tonic-gate 	int i, sz;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	sz = sizeof(*buckets) * ipsp->iss_statesize;
9257c478bd9Sstevel@tonic-gate 	buckets = (u_long *)malloc(sz);
9265e985db5Sschuster 	if (buckets == NULL) {
9275e985db5Sschuster 		perror("malloc");
9285e985db5Sschuster 		exit(1);
9295e985db5Sschuster 	}
9307c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)buckets, (u_long)ipsp->iss_bucketlen, sz)) {
9317c478bd9Sstevel@tonic-gate 		free(buckets);
9327c478bd9Sstevel@tonic-gate 		return;
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * If a list of states hasn't been asked for, only print out stats
9377c478bd9Sstevel@tonic-gate 	 */
9387c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_SHOWLIST)) {
9397c478bd9Sstevel@tonic-gate 		PRINTF("IP states added:\n\t%lu TCP\n\t%lu UDP\n\t%lu ICMP\n",
9407c478bd9Sstevel@tonic-gate 			ipsp->iss_tcp, ipsp->iss_udp, ipsp->iss_icmp);
9417c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu hits\n\t%lu misses\n", ipsp->iss_hits,
9427c478bd9Sstevel@tonic-gate 			ipsp->iss_miss);
9437c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu maximum\n\t%lu no memory\n\t%lu max bucket\n",
9447c478bd9Sstevel@tonic-gate 			ipsp->iss_max, ipsp->iss_nomem, ipsp->iss_bucketfull);
945*ab25eeb5Syz155240 		PRINTF("\t%lu maximum\n\t%lu no memory\n\t%lu bkts in use\n",
946*ab25eeb5Syz155240 			ipsp->iss_max, ipsp->iss_nomem, ipsp->iss_inuse);
9477c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu active\n\t%lu expired\n\t%lu closed\n",
9487c478bd9Sstevel@tonic-gate 			ipsp->iss_active, ipsp->iss_expire, ipsp->iss_fin);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		PRINTF("State logging %sabled\n",
9517c478bd9Sstevel@tonic-gate 			state_logging ? "en" : "dis");
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		PRINTF("\nState table bucket statistics:\n");
9547c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu in use\t\n", ipsp->iss_inuse);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		minlen = ipsp->iss_max;
9577c478bd9Sstevel@tonic-gate 		totallen = 0;
9587c478bd9Sstevel@tonic-gate 		maxlen = 0;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		for (i = 0; i < ipsp->iss_statesize; i++) {
9617c478bd9Sstevel@tonic-gate 			if (buckets[i] > maxlen)
9627c478bd9Sstevel@tonic-gate 				maxlen = buckets[i];
9637c478bd9Sstevel@tonic-gate 			if (buckets[i] < minlen)
9647c478bd9Sstevel@tonic-gate 					minlen = buckets[i];
9657c478bd9Sstevel@tonic-gate 			totallen += buckets[i];
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		PRINTF("\t%2.2f%% bucket usage\n\t%lu minimal length\n",
9697c478bd9Sstevel@tonic-gate 			((float)ipsp->iss_inuse / ipsp->iss_statesize) * 100.0,
9707c478bd9Sstevel@tonic-gate 			minlen);
9717c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu maximal length\n\t%.3f average length\n",
9727c478bd9Sstevel@tonic-gate 			maxlen,
9737c478bd9Sstevel@tonic-gate 			ipsp->iss_inuse ? (float) totallen/ ipsp->iss_inuse :
9747c478bd9Sstevel@tonic-gate 					  0.0);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate #define ENTRIES_PER_LINE 5
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE) {
9797c478bd9Sstevel@tonic-gate 			PRINTF("\nCurrent bucket sizes :\n");
9807c478bd9Sstevel@tonic-gate 			for (i = 0; i < ipsp->iss_statesize; i++) {
9817c478bd9Sstevel@tonic-gate 				if ((i % ENTRIES_PER_LINE) == 0)
9827c478bd9Sstevel@tonic-gate 					PRINTF("\t");
9837c478bd9Sstevel@tonic-gate 				PRINTF("%4d -> %4lu", i, buckets[i]);
9847c478bd9Sstevel@tonic-gate 				if ((i % ENTRIES_PER_LINE) ==
9857c478bd9Sstevel@tonic-gate 				    (ENTRIES_PER_LINE - 1))
9867c478bd9Sstevel@tonic-gate 					PRINTF("\n");
9877c478bd9Sstevel@tonic-gate 				else
9887c478bd9Sstevel@tonic-gate 					PRINTF("  ");
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 			PRINTF("\n");
9917c478bd9Sstevel@tonic-gate 		}
9927c478bd9Sstevel@tonic-gate 		PRINTF("\n");
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 		free(buckets);
9957c478bd9Sstevel@tonic-gate 		return;
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	/*
9997c478bd9Sstevel@tonic-gate 	 * Print out all the state information currently held in the kernel.
10007c478bd9Sstevel@tonic-gate 	 */
10017c478bd9Sstevel@tonic-gate 	while (ipsp->iss_list != NULL) {
1002*ab25eeb5Syz155240 		ipsp->iss_list = printstate(ipsp->iss_list, opts,
1003*ab25eeb5Syz155240 					    ipsp->iss_ticks);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	free(buckets);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate #ifdef STATETOP
1011*ab25eeb5Syz155240 static int handle_resize = 0, handle_break = 0;
1012*ab25eeb5Syz155240 
1013*ab25eeb5Syz155240 static void topipstates(saddr, daddr, sport, dport, protocol, ver,
10147c478bd9Sstevel@tonic-gate 		        refreshtime, topclosed)
1015*ab25eeb5Syz155240 i6addr_t saddr;
1016*ab25eeb5Syz155240 i6addr_t daddr;
10177c478bd9Sstevel@tonic-gate int sport;
10187c478bd9Sstevel@tonic-gate int dport;
10197c478bd9Sstevel@tonic-gate int protocol;
1020*ab25eeb5Syz155240 int ver;
10217c478bd9Sstevel@tonic-gate int refreshtime;
10227c478bd9Sstevel@tonic-gate int topclosed;
10237c478bd9Sstevel@tonic-gate {
10247c478bd9Sstevel@tonic-gate 	char str1[STSTRSIZE], str2[STSTRSIZE], str3[STSTRSIZE], str4[STSTRSIZE];
10257c478bd9Sstevel@tonic-gate 	int maxtsentries = 0, reverse = 0, sorting = STSORT_DEFAULT;
1026*ab25eeb5Syz155240 	int i, j, winy, tsentry, maxx, maxy, redraw = 0, ret = 0;
1027*ab25eeb5Syz155240 	int len, srclen, dstlen, forward = 1, c = 0;
10287c478bd9Sstevel@tonic-gate 	ips_stat_t ipsst, *ipsstp = &ipsst;
10297c478bd9Sstevel@tonic-gate 	statetop_t *tstable = NULL, *tp;
1030*ab25eeb5Syz155240 	const char *errstr = "";
10317c478bd9Sstevel@tonic-gate 	ipstate_t ips;
10327c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
10337c478bd9Sstevel@tonic-gate 	struct timeval selecttimeout;
10347c478bd9Sstevel@tonic-gate 	char hostnm[HOSTNMLEN];
10357c478bd9Sstevel@tonic-gate 	struct protoent *proto;
10367c478bd9Sstevel@tonic-gate 	fd_set readfd;
10377c478bd9Sstevel@tonic-gate 	time_t t;
10387c478bd9Sstevel@tonic-gate 
1039*ab25eeb5Syz155240 	/* install signal handlers */
1040*ab25eeb5Syz155240 	signal(SIGINT, sig_break);
1041*ab25eeb5Syz155240 	signal(SIGQUIT, sig_break);
1042*ab25eeb5Syz155240 	signal(SIGTERM, sig_break);
1043*ab25eeb5Syz155240 	signal(SIGWINCH, sig_resize);
1044*ab25eeb5Syz155240 
10457c478bd9Sstevel@tonic-gate 	/* init ncurses stuff */
10467c478bd9Sstevel@tonic-gate   	initscr();
10477c478bd9Sstevel@tonic-gate   	cbreak();
10487c478bd9Sstevel@tonic-gate   	noecho();
1049*ab25eeb5Syz155240 	curs_set(0);
1050*ab25eeb5Syz155240 	timeout(0);
1051*ab25eeb5Syz155240 	getmaxyx(stdscr, maxy, maxx);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	/* init hostname */
10547c478bd9Sstevel@tonic-gate 	gethostname(hostnm, sizeof(hostnm) - 1);
10557c478bd9Sstevel@tonic-gate 	hostnm[sizeof(hostnm) - 1] = '\0';
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	/* init ipfobj_t stuff */
10587c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&ipfo, sizeof(ipfo));
10597c478bd9Sstevel@tonic-gate 	ipfo.ipfo_rev = IPFILTER_VERSION;
10607c478bd9Sstevel@tonic-gate 	ipfo.ipfo_size = sizeof(*ipsstp);
10617c478bd9Sstevel@tonic-gate 	ipfo.ipfo_ptr = (void *)ipsstp;
10627c478bd9Sstevel@tonic-gate 	ipfo.ipfo_type = IPFOBJ_STATESTAT;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/* repeat until user aborts */
10657c478bd9Sstevel@tonic-gate 	while ( 1 ) {
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 		/* get state table */
10687c478bd9Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
10697c478bd9Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
1070*ab25eeb5Syz155240 			errstr = "ioctl(SIOCGETFS)";
1071*ab25eeb5Syz155240 			ret = -1;
1072*ab25eeb5Syz155240 			goto out;
10737c478bd9Sstevel@tonic-gate 		}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		/* clear the history */
10767c478bd9Sstevel@tonic-gate 		tsentry = -1;
10777c478bd9Sstevel@tonic-gate 
1078*ab25eeb5Syz155240 		/* reset max str len */
1079*ab25eeb5Syz155240 		srclen = dstlen = 0;
1080*ab25eeb5Syz155240 
10817c478bd9Sstevel@tonic-gate 		/* read the state table and store in tstable */
1082*ab25eeb5Syz155240 		for (; ipsstp->iss_list; ipsstp->iss_list = ips.is_next) {
1083*ab25eeb5Syz155240 
10847c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ips, (u_long)ipsstp->iss_list,
10857c478bd9Sstevel@tonic-gate 				    sizeof(ips)))
10867c478bd9Sstevel@tonic-gate 				break;
10877c478bd9Sstevel@tonic-gate 
1088*ab25eeb5Syz155240 			if (ips.is_v != ver)
1089*ab25eeb5Syz155240 				continue;
1090*ab25eeb5Syz155240 
1091*ab25eeb5Syz155240 			/* check v4 src/dest addresses */
1092*ab25eeb5Syz155240 			if (ips.is_v == 4) {
1093*ab25eeb5Syz155240 				if ((saddr.in4.s_addr != INADDR_ANY &&
1094*ab25eeb5Syz155240 				     saddr.in4.s_addr != ips.is_saddr) ||
1095*ab25eeb5Syz155240 				    (daddr.in4.s_addr != INADDR_ANY &&
1096*ab25eeb5Syz155240 				     daddr.in4.s_addr != ips.is_daddr))
1097*ab25eeb5Syz155240 					continue;
1098*ab25eeb5Syz155240 			}
1099*ab25eeb5Syz155240 #ifdef	USE_INET6
1100*ab25eeb5Syz155240 			/* check v6 src/dest addresses */
1101*ab25eeb5Syz155240 			if (ips.is_v == 6) {
1102*ab25eeb5Syz155240 				if ((IP6_NEQ(&saddr, &in6addr_any) &&
1103*ab25eeb5Syz155240 				     IP6_NEQ(&saddr, &ips.is_src)) ||
1104*ab25eeb5Syz155240 				    (IP6_NEQ(&daddr, &in6addr_any) &&
1105*ab25eeb5Syz155240 				     IP6_NEQ(&daddr, &ips.is_dst)))
1106*ab25eeb5Syz155240 					continue;
1107*ab25eeb5Syz155240 			}
1108*ab25eeb5Syz155240 #endif
1109*ab25eeb5Syz155240 			/* check protocol */
1110*ab25eeb5Syz155240 			if (protocol > 0 && protocol != ips.is_p)
1111*ab25eeb5Syz155240 				continue;
1112*ab25eeb5Syz155240 
1113*ab25eeb5Syz155240 			/* check ports if protocol is TCP or UDP */
1114*ab25eeb5Syz155240 			if (((ips.is_p == IPPROTO_TCP) ||
1115*ab25eeb5Syz155240 			     (ips.is_p == IPPROTO_UDP)) &&
1116*ab25eeb5Syz155240 			   (((sport > 0) && (htons(sport) != ips.is_sport)) ||
1117*ab25eeb5Syz155240 			    ((dport > 0) && (htons(dport) != ips.is_dport))))
1118*ab25eeb5Syz155240 				continue;
1119*ab25eeb5Syz155240 
1120*ab25eeb5Syz155240 			/* show closed TCP sessions ? */
1121*ab25eeb5Syz155240 			if ((topclosed == 0) && (ips.is_p == IPPROTO_TCP) &&
1122*ab25eeb5Syz155240 			    (ips.is_state[0] >= IPF_TCPS_LAST_ACK) &&
1123*ab25eeb5Syz155240 			    (ips.is_state[1] >= IPF_TCPS_LAST_ACK))
1124*ab25eeb5Syz155240 				continue;
1125*ab25eeb5Syz155240 
11267c478bd9Sstevel@tonic-gate 			/*
11277c478bd9Sstevel@tonic-gate 			 * if necessary make room for this state
11287c478bd9Sstevel@tonic-gate 			 * entry
11297c478bd9Sstevel@tonic-gate 			 */
11307c478bd9Sstevel@tonic-gate 			tsentry++;
1131*ab25eeb5Syz155240 			if (!maxtsentries || tsentry == maxtsentries) {
11327c478bd9Sstevel@tonic-gate 				maxtsentries += STGROWSIZE;
1133*ab25eeb5Syz155240 				tstable = realloc(tstable,
1134*ab25eeb5Syz155240 				    maxtsentries * sizeof(statetop_t));
1135*ab25eeb5Syz155240 				if (tstable == NULL) {
1136*ab25eeb5Syz155240 					perror("realloc");
1137*ab25eeb5Syz155240 					exit(-1);
11387c478bd9Sstevel@tonic-gate 				}
11397c478bd9Sstevel@tonic-gate 			}
11407c478bd9Sstevel@tonic-gate 
1141*ab25eeb5Syz155240 			/* get max src/dest address string length */
1142*ab25eeb5Syz155240 			len = strlen(getip(ips.is_v, &ips.is_src));
1143*ab25eeb5Syz155240 			if (srclen < len)
1144*ab25eeb5Syz155240 				srclen = len;
1145*ab25eeb5Syz155240 			len = strlen(getip(ips.is_v, &ips.is_dst));
1146*ab25eeb5Syz155240 			if (dstlen < len)
1147*ab25eeb5Syz155240 				dstlen = len;
1148*ab25eeb5Syz155240 
11497c478bd9Sstevel@tonic-gate 			/* fill structure */
11507c478bd9Sstevel@tonic-gate 			tp = tstable + tsentry;
11517c478bd9Sstevel@tonic-gate 			tp->st_src = ips.is_src;
11527c478bd9Sstevel@tonic-gate 			tp->st_dst = ips.is_dst;
11537c478bd9Sstevel@tonic-gate 			tp->st_p = ips.is_p;
1154*ab25eeb5Syz155240 			tp->st_v = ips.is_v;
11557c478bd9Sstevel@tonic-gate 			tp->st_state[0] = ips.is_state[0];
11567c478bd9Sstevel@tonic-gate 			tp->st_state[1] = ips.is_state[1];
1157*ab25eeb5Syz155240 			if (forward) {
11587c478bd9Sstevel@tonic-gate 				tp->st_pkts = ips.is_pkts[0]+ips.is_pkts[1];
1159*ab25eeb5Syz155240 				tp->st_bytes = ips.is_bytes[0]+ips.is_bytes[1];
1160*ab25eeb5Syz155240 			} else {
1161*ab25eeb5Syz155240 				tp->st_pkts = ips.is_pkts[2]+ips.is_pkts[3];
1162*ab25eeb5Syz155240 				tp->st_bytes = ips.is_bytes[2]+ips.is_bytes[3];
1163*ab25eeb5Syz155240 			}
11646aed92a9Syx160601 			tp->st_age = ips.is_die - ipsstp->iss_ticks;
11657c478bd9Sstevel@tonic-gate 			if ((ips.is_p == IPPROTO_TCP) ||
11667c478bd9Sstevel@tonic-gate 			    (ips.is_p == IPPROTO_UDP)) {
11677c478bd9Sstevel@tonic-gate 				tp->st_sport = ips.is_sport;
11687c478bd9Sstevel@tonic-gate 				tp->st_dport = ips.is_dport;
11697c478bd9Sstevel@tonic-gate 			}
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		/* sort the array */
1174*ab25eeb5Syz155240 		if (tsentry != -1) {
11757c478bd9Sstevel@tonic-gate 			switch (sorting)
11767c478bd9Sstevel@tonic-gate 			{
11777c478bd9Sstevel@tonic-gate 			case STSORT_PR:
11787c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
11797c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_p);
11807c478bd9Sstevel@tonic-gate 				break;
11817c478bd9Sstevel@tonic-gate 			case STSORT_PKTS:
11827c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
11837c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_pkts);
11847c478bd9Sstevel@tonic-gate 				break;
11857c478bd9Sstevel@tonic-gate 			case STSORT_BYTES:
11867c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
11877c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_bytes);
11887c478bd9Sstevel@tonic-gate 				break;
11897c478bd9Sstevel@tonic-gate 			case STSORT_TTL:
11907c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
11917c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_ttl);
11927c478bd9Sstevel@tonic-gate 				break;
11937c478bd9Sstevel@tonic-gate 			case STSORT_SRCIP:
11947c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
11957c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_srcip);
11967c478bd9Sstevel@tonic-gate 				break;
1197*ab25eeb5Syz155240 			case STSORT_SRCPT:
1198*ab25eeb5Syz155240 				qsort(tstable, tsentry +1,
1199*ab25eeb5Syz155240 					sizeof(statetop_t), sort_srcpt);
1200*ab25eeb5Syz155240 				break;
12017c478bd9Sstevel@tonic-gate 			case STSORT_DSTIP:
12027c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
12037c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_dstip);
12047c478bd9Sstevel@tonic-gate 				break;
1205*ab25eeb5Syz155240 			case STSORT_DSTPT:
1206*ab25eeb5Syz155240 				qsort(tstable, tsentry + 1,
1207*ab25eeb5Syz155240 				      sizeof(statetop_t), sort_dstpt);
1208*ab25eeb5Syz155240 				break;
12097c478bd9Sstevel@tonic-gate 			default:
12107c478bd9Sstevel@tonic-gate 				break;
12117c478bd9Sstevel@tonic-gate 			}
1212*ab25eeb5Syz155240 		}
1213*ab25eeb5Syz155240 
1214*ab25eeb5Syz155240 		/* handle window resizes */
1215*ab25eeb5Syz155240 		if (handle_resize) {
1216*ab25eeb5Syz155240 			endwin();
1217*ab25eeb5Syz155240 			initscr();
1218*ab25eeb5Syz155240 			cbreak();
1219*ab25eeb5Syz155240 			noecho();
1220*ab25eeb5Syz155240 			curs_set(0);
1221*ab25eeb5Syz155240 			timeout(0);
1222*ab25eeb5Syz155240 			getmaxyx(stdscr, maxy, maxx);
1223*ab25eeb5Syz155240 			redraw = 1;
1224*ab25eeb5Syz155240 			handle_resize = 0;
1225*ab25eeb5Syz155240                 }
1226*ab25eeb5Syz155240 
1227*ab25eeb5Syz155240 		/* stop program? */
1228*ab25eeb5Syz155240 		if (handle_break)
1229*ab25eeb5Syz155240 			break;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		/* print title */
12327c478bd9Sstevel@tonic-gate 		erase();
12337c478bd9Sstevel@tonic-gate 		attron(A_BOLD);
1234*ab25eeb5Syz155240 		winy = 0;
1235*ab25eeb5Syz155240 		move(winy,0);
12367c478bd9Sstevel@tonic-gate 		sprintf(str1, "%s - %s - state top", hostnm, IPL_VERSION);
12377c478bd9Sstevel@tonic-gate 		for (j = 0 ; j < (maxx - 8 - strlen(str1)) / 2; j++)
12387c478bd9Sstevel@tonic-gate 			printw(" ");
12397c478bd9Sstevel@tonic-gate 		printw("%s", str1);
12407c478bd9Sstevel@tonic-gate 		attroff(A_BOLD);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		/* just for fun add a clock */
1243*ab25eeb5Syz155240 		move(winy, maxx - 8);
12447c478bd9Sstevel@tonic-gate 		t = time(NULL);
12457c478bd9Sstevel@tonic-gate 		strftime(str1, 80, "%T", localtime(&t));
12467c478bd9Sstevel@tonic-gate 		printw("%s\n", str1);
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		/*
12497c478bd9Sstevel@tonic-gate 		 * print the display filters, this is placed in the loop,
12507c478bd9Sstevel@tonic-gate 		 * because someday I might add code for changing these
12517c478bd9Sstevel@tonic-gate 		 * while the programming is running :-)
12527c478bd9Sstevel@tonic-gate 		 */
12537c478bd9Sstevel@tonic-gate 		if (sport >= 0)
1254*ab25eeb5Syz155240 			sprintf(str1, "%s,%d", getip(ver, &saddr), sport);
12557c478bd9Sstevel@tonic-gate 		else
1256*ab25eeb5Syz155240 			sprintf(str1, "%s", getip(ver, &saddr));
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 		if (dport >= 0)
1259*ab25eeb5Syz155240 			sprintf(str2, "%s,%d", getip(ver, &daddr), dport);
12607c478bd9Sstevel@tonic-gate 		else
1261*ab25eeb5Syz155240 			sprintf(str2, "%s", getip(ver, &daddr));
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		if (protocol < 0)
12647c478bd9Sstevel@tonic-gate 			strcpy(str3, "any");
12657c478bd9Sstevel@tonic-gate 		else if ((proto = getprotobynumber(protocol)) != NULL)
12667c478bd9Sstevel@tonic-gate 			sprintf(str3, "%s", proto->p_name);
12677c478bd9Sstevel@tonic-gate 		else
12687c478bd9Sstevel@tonic-gate 			sprintf(str3, "%d", protocol);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 		switch (sorting)
12717c478bd9Sstevel@tonic-gate 		{
12727c478bd9Sstevel@tonic-gate 		case STSORT_PR:
12737c478bd9Sstevel@tonic-gate 			sprintf(str4, "proto");
12747c478bd9Sstevel@tonic-gate 			break;
12757c478bd9Sstevel@tonic-gate 		case STSORT_PKTS:
12767c478bd9Sstevel@tonic-gate 			sprintf(str4, "# pkts");
12777c478bd9Sstevel@tonic-gate 			break;
12787c478bd9Sstevel@tonic-gate 		case STSORT_BYTES:
12797c478bd9Sstevel@tonic-gate 			sprintf(str4, "# bytes");
12807c478bd9Sstevel@tonic-gate 			break;
12817c478bd9Sstevel@tonic-gate 		case STSORT_TTL:
12827c478bd9Sstevel@tonic-gate 			sprintf(str4, "ttl");
12837c478bd9Sstevel@tonic-gate 			break;
12847c478bd9Sstevel@tonic-gate 		case STSORT_SRCIP:
12857c478bd9Sstevel@tonic-gate 			sprintf(str4, "src ip");
12867c478bd9Sstevel@tonic-gate 			break;
1287*ab25eeb5Syz155240 		case STSORT_SRCPT:
1288*ab25eeb5Syz155240 			sprintf(str4, "src port");
1289*ab25eeb5Syz155240 			break;
12907c478bd9Sstevel@tonic-gate 		case STSORT_DSTIP:
1291*ab25eeb5Syz155240 			sprintf(str4, "dest ip");
1292*ab25eeb5Syz155240 			break;
1293*ab25eeb5Syz155240 		case STSORT_DSTPT:
1294*ab25eeb5Syz155240 			sprintf(str4, "dest port");
12957c478bd9Sstevel@tonic-gate 			break;
12967c478bd9Sstevel@tonic-gate 		default:
12977c478bd9Sstevel@tonic-gate 			sprintf(str4, "unknown");
12987c478bd9Sstevel@tonic-gate 			break;
12997c478bd9Sstevel@tonic-gate 		}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 		if (reverse)
13027c478bd9Sstevel@tonic-gate 			strcat(str4, " (reverse)");
13037c478bd9Sstevel@tonic-gate 
1304*ab25eeb5Syz155240 		winy += 2;
1305*ab25eeb5Syz155240 		move(winy,0);
1306*ab25eeb5Syz155240 		printw("Src: %s, Dest: %s, Proto: %s, Sorted by: %s\n\n",
13077c478bd9Sstevel@tonic-gate 		       str1, str2, str3, str4);
13087c478bd9Sstevel@tonic-gate 
1309*ab25eeb5Syz155240 		/*
1310*ab25eeb5Syz155240 		 * For an IPv4 IP address we need at most 15 characters,
1311*ab25eeb5Syz155240 		 * 4 tuples of 3 digits, separated by 3 dots. Enforce this
1312*ab25eeb5Syz155240 		 * length, so the colums do not change positions based
1313*ab25eeb5Syz155240 		 * on the size of the IP address. This length makes the
1314*ab25eeb5Syz155240 		 * output fit in a 80 column terminal.
1315*ab25eeb5Syz155240 		 * We are lacking a good solution for IPv6 addresses (that
1316*ab25eeb5Syz155240 		 * can be longer that 15 characters), so we do not enforce
1317*ab25eeb5Syz155240 		 * a maximum on the IP field size.
1318*ab25eeb5Syz155240 		 */
1319*ab25eeb5Syz155240 		if (srclen < 15)
1320*ab25eeb5Syz155240 			srclen = 15;
1321*ab25eeb5Syz155240 		if (dstlen < 15)
1322*ab25eeb5Syz155240 			dstlen = 15;
1323*ab25eeb5Syz155240 
13247c478bd9Sstevel@tonic-gate 		/* print column description */
1325*ab25eeb5Syz155240 		winy += 2;
1326*ab25eeb5Syz155240 		move(winy,0);
13277c478bd9Sstevel@tonic-gate 		attron(A_BOLD);
1328*ab25eeb5Syz155240 		printw("%-*s %-*s %3s %4s %7s %9s %9s\n",
1329*ab25eeb5Syz155240 		       srclen + 6, "Source IP", dstlen + 6, "Destination IP",
1330*ab25eeb5Syz155240 		       "ST", "PR", "#pkts", "#bytes", "ttl");
13317c478bd9Sstevel@tonic-gate 		attroff(A_BOLD);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		/* print all the entries */
13347c478bd9Sstevel@tonic-gate 		tp = tstable;
13357c478bd9Sstevel@tonic-gate 		if (reverse)
13367c478bd9Sstevel@tonic-gate 			tp += tsentry;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 		if (tsentry > maxy - 6)
13397c478bd9Sstevel@tonic-gate 			tsentry = maxy - 6;
13407c478bd9Sstevel@tonic-gate 		for (i = 0; i <= tsentry; i++) {
13417c478bd9Sstevel@tonic-gate 			/* print src/dest and port */
13427c478bd9Sstevel@tonic-gate 			if ((tp->st_p == IPPROTO_TCP) ||
13437c478bd9Sstevel@tonic-gate 			    (tp->st_p == IPPROTO_UDP)) {
13447c478bd9Sstevel@tonic-gate 				sprintf(str1, "%s,%hu",
1345*ab25eeb5Syz155240 					getip(tp->st_v, &tp->st_src),
13467c478bd9Sstevel@tonic-gate 					ntohs(tp->st_sport));
13477c478bd9Sstevel@tonic-gate 				sprintf(str2, "%s,%hu",
1348*ab25eeb5Syz155240 					getip(tp->st_v, &tp->st_dst),
13497c478bd9Sstevel@tonic-gate 					ntohs(tp->st_dport));
13507c478bd9Sstevel@tonic-gate 			} else {
1351*ab25eeb5Syz155240 				sprintf(str1, "%s", getip(tp->st_v,
1352*ab25eeb5Syz155240 				    &tp->st_src));
1353*ab25eeb5Syz155240 				sprintf(str2, "%s", getip(tp->st_v,
1354*ab25eeb5Syz155240 				    &tp->st_dst));
13557c478bd9Sstevel@tonic-gate 			}
1356*ab25eeb5Syz155240 			winy++;
1357*ab25eeb5Syz155240 			move(winy, 0);
1358*ab25eeb5Syz155240 			printw("%-*s %-*s", srclen + 6, str1, dstlen + 6, str2);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 			/* print state */
13617c478bd9Sstevel@tonic-gate 			sprintf(str1, "%X/%X", tp->st_state[0],
13627c478bd9Sstevel@tonic-gate 				tp->st_state[1]);
13637c478bd9Sstevel@tonic-gate 			printw(" %3s", str1);
13647c478bd9Sstevel@tonic-gate 
1365*ab25eeb5Syz155240 			/* print protocol */
13667c478bd9Sstevel@tonic-gate 			proto = getprotobynumber(tp->st_p);
13677c478bd9Sstevel@tonic-gate 			if (proto) {
13687c478bd9Sstevel@tonic-gate 				strncpy(str1, proto->p_name, 4);
13697c478bd9Sstevel@tonic-gate 				str1[4] = '\0';
13707c478bd9Sstevel@tonic-gate 			} else {
13717c478bd9Sstevel@tonic-gate 				sprintf(str1, "%d", tp->st_p);
13727c478bd9Sstevel@tonic-gate 			}
1373*ab25eeb5Syz155240 			/* just print icmp for IPv6-ICMP */
1374*ab25eeb5Syz155240 			if (tp->st_p == IPPROTO_ICMPV6)
1375*ab25eeb5Syz155240 				strcpy(str1, "icmp");
13767c478bd9Sstevel@tonic-gate 			printw(" %4s", str1);
1377*ab25eeb5Syz155240 
13787c478bd9Sstevel@tonic-gate 			/* print #pkt/#bytes */
13797c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
13807c478bd9Sstevel@tonic-gate 			printw(" %7qu %9qu", (unsigned long long) tp->st_pkts,
13817c478bd9Sstevel@tonic-gate 				(unsigned long long) tp->st_bytes);
13827c478bd9Sstevel@tonic-gate #else
13837c478bd9Sstevel@tonic-gate 			printw(" %7lu %9lu", tp->st_pkts, tp->st_bytes);
13847c478bd9Sstevel@tonic-gate #endif
13857c478bd9Sstevel@tonic-gate 			printw(" %9s", ttl_to_string(tp->st_age));
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 			if (reverse)
13887c478bd9Sstevel@tonic-gate 				tp--;
13897c478bd9Sstevel@tonic-gate 			else
13907c478bd9Sstevel@tonic-gate 				tp++;
13917c478bd9Sstevel@tonic-gate 		}
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		/* screen data structure is filled, now update the screen */
13947c478bd9Sstevel@tonic-gate 		if (redraw)
13957c478bd9Sstevel@tonic-gate 			clearok(stdscr,1);
13967c478bd9Sstevel@tonic-gate 
1397*ab25eeb5Syz155240 		if (refresh() == ERR)
1398*ab25eeb5Syz155240 			break;
13997c478bd9Sstevel@tonic-gate 		if (redraw) {
14007c478bd9Sstevel@tonic-gate 			clearok(stdscr,0);
14017c478bd9Sstevel@tonic-gate 			redraw = 0;
14027c478bd9Sstevel@tonic-gate 		}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 		/* wait for key press or a 1 second time out period */
14057c478bd9Sstevel@tonic-gate 		selecttimeout.tv_sec = refreshtime;
14067c478bd9Sstevel@tonic-gate 		selecttimeout.tv_usec = 0;
14077c478bd9Sstevel@tonic-gate 		FD_ZERO(&readfd);
14087c478bd9Sstevel@tonic-gate 		FD_SET(0, &readfd);
14097c478bd9Sstevel@tonic-gate 		select(1, &readfd, NULL, NULL, &selecttimeout);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 		/* if key pressed, read all waiting keys */
14127c478bd9Sstevel@tonic-gate 		if (FD_ISSET(0, &readfd)) {
14137c478bd9Sstevel@tonic-gate 			c = wgetch(stdscr);
14147c478bd9Sstevel@tonic-gate 			if (c == ERR)
14157c478bd9Sstevel@tonic-gate 				continue;
14167c478bd9Sstevel@tonic-gate 
1417*ab25eeb5Syz155240 			if (ISALPHA(c) && ISUPPER(c))
1418*ab25eeb5Syz155240 				c = TOLOWER(c);
1419*ab25eeb5Syz155240 			if (c == 'l') {
14207c478bd9Sstevel@tonic-gate 				redraw = 1;
1421*ab25eeb5Syz155240 			} else if (c == 'q') {
14225e985db5Sschuster 				break;
1423*ab25eeb5Syz155240 			} else if (c == 'r') {
14247c478bd9Sstevel@tonic-gate 				reverse = !reverse;
1425*ab25eeb5Syz155240 			} else if (c == 'b') {
1426*ab25eeb5Syz155240 				forward = 0;
1427*ab25eeb5Syz155240 			} else if (c == 'f') {
1428*ab25eeb5Syz155240 				forward = 1;
1429*ab25eeb5Syz155240 			} else if (c == 's') {
1430*ab25eeb5Syz155240 				if (++sorting > STSORT_MAX)
14317c478bd9Sstevel@tonic-gate 					sorting = 0;
14327c478bd9Sstevel@tonic-gate 			}
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 	} /* while */
14357c478bd9Sstevel@tonic-gate 
1436*ab25eeb5Syz155240 out:
14377c478bd9Sstevel@tonic-gate 	printw("\n");
1438*ab25eeb5Syz155240 	curs_set(1);
14397c478bd9Sstevel@tonic-gate 	nocbreak();
14407c478bd9Sstevel@tonic-gate 	endwin();
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	free(tstable);
1443*ab25eeb5Syz155240 	if (ret != 0)
1444*ab25eeb5Syz155240 		perror(errstr);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate #endif
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate /*
14507c478bd9Sstevel@tonic-gate  * Show fragment cache information that's held in the kernel.
14517c478bd9Sstevel@tonic-gate  */
14527c478bd9Sstevel@tonic-gate static void showfrstates(ifsp)
14537c478bd9Sstevel@tonic-gate ipfrstat_t *ifsp;
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	struct ipfr *ipfrtab[IPFT_SIZE], ifr;
14567c478bd9Sstevel@tonic-gate 	int i;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	/*
14597c478bd9Sstevel@tonic-gate 	 * print out the numeric statistics
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 	PRINTF("IP fragment states:\n\t%lu new\n\t%lu expired\n\t%lu hits\n",
14627c478bd9Sstevel@tonic-gate 		ifsp->ifs_new, ifsp->ifs_expire, ifsp->ifs_hits);
1463*ab25eeb5Syz155240 	PRINTF("\t%lu retrans\n\t%lu too short\n",
1464*ab25eeb5Syz155240 		ifsp->ifs_retrans0, ifsp->ifs_short);
14657c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu no memory\n\t%lu already exist\n",
14667c478bd9Sstevel@tonic-gate 		ifsp->ifs_nomem, ifsp->ifs_exists);
14677c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu inuse\n", ifsp->ifs_inuse);
14687c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_table, sizeof(ipfrtab)))
14697c478bd9Sstevel@tonic-gate 		return;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	/*
14727c478bd9Sstevel@tonic-gate 	 * Print out the contents (if any) of the fragment cache table.
14737c478bd9Sstevel@tonic-gate 	 */
1474*ab25eeb5Syz155240 	PRINTF("\n");
14757c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
1476*ab25eeb5Syz155240 		while (ipfrtab[i] != NULL) {
14777c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
14787c478bd9Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
14797c478bd9Sstevel@tonic-gate 				break;
1480*ab25eeb5Syz155240 			printfraginfo("", &ifr);
14817c478bd9Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
14827c478bd9Sstevel@tonic-gate 		}
1483*ab25eeb5Syz155240 	/*
1484*ab25eeb5Syz155240 	 * Print out the contents (if any) of the NAT fragment cache table.
1485*ab25eeb5Syz155240 	 */
14867c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_nattab,sizeof(ipfrtab)))
14877c478bd9Sstevel@tonic-gate 		return;
14887c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
1489*ab25eeb5Syz155240 		while (ipfrtab[i] != NULL) {
14907c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
14917c478bd9Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
14927c478bd9Sstevel@tonic-gate 				break;
1493*ab25eeb5Syz155240 			printfraginfo("NAT: ", &ifr);
14947c478bd9Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate /*
15007c478bd9Sstevel@tonic-gate  * Show stats on how auth within IPFilter has been used
15017c478bd9Sstevel@tonic-gate  */
15027c478bd9Sstevel@tonic-gate static void showauthstates(asp)
15037c478bd9Sstevel@tonic-gate fr_authstat_t *asp;
15047c478bd9Sstevel@tonic-gate {
15057c478bd9Sstevel@tonic-gate 	frauthent_t *frap, fra;
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
15087c478bd9Sstevel@tonic-gate 	printf("Authorisation hits: %qu\tmisses %qu\n",
15097c478bd9Sstevel@tonic-gate 		(unsigned long long) asp->fas_hits,
15107c478bd9Sstevel@tonic-gate 		(unsigned long long) asp->fas_miss);
15117c478bd9Sstevel@tonic-gate #else
15127c478bd9Sstevel@tonic-gate 	printf("Authorisation hits: %ld\tmisses %ld\n", asp->fas_hits,
15137c478bd9Sstevel@tonic-gate 		asp->fas_miss);
15147c478bd9Sstevel@tonic-gate #endif
15157c478bd9Sstevel@tonic-gate 	printf("nospace %ld\nadded %ld\nsendfail %ld\nsendok %ld\n",
15167c478bd9Sstevel@tonic-gate 		asp->fas_nospace, asp->fas_added, asp->fas_sendfail,
15177c478bd9Sstevel@tonic-gate 		asp->fas_sendok);
15187c478bd9Sstevel@tonic-gate 	printf("queok %ld\nquefail %ld\nexpire %ld\n",
15197c478bd9Sstevel@tonic-gate 		asp->fas_queok, asp->fas_quefail, asp->fas_expire);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	frap = asp->fas_faelist;
15227c478bd9Sstevel@tonic-gate 	while (frap) {
15237c478bd9Sstevel@tonic-gate 		if (kmemcpy((char *)&fra, (u_long)frap, sizeof(fra)) == -1)
15247c478bd9Sstevel@tonic-gate 			break;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 		printf("age %ld\t", fra.fae_age);
15277c478bd9Sstevel@tonic-gate 		printfr(&fra.fae_fr, ioctl);
15287c478bd9Sstevel@tonic-gate 		frap = fra.fae_next;
15297c478bd9Sstevel@tonic-gate 	}
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate /*
15347c478bd9Sstevel@tonic-gate  * Display groups used for each of filter rules, accounting rules and
15357c478bd9Sstevel@tonic-gate  * authentication, separately.
15367c478bd9Sstevel@tonic-gate  */
15377c478bd9Sstevel@tonic-gate static void showgroups(fiop)
15387c478bd9Sstevel@tonic-gate struct friostat	*fiop;
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate 	static char *gnames[3] = { "Filter", "Accounting", "Authentication" };
1541*ab25eeb5Syz155240 	static int gnums[3] = { IPL_LOGIPF, IPL_LOGCOUNT, IPL_LOGAUTH };
15427c478bd9Sstevel@tonic-gate 	frgroup_t *fp, grp;
15437c478bd9Sstevel@tonic-gate 	int on, off, i;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	on = fiop->f_active;
15467c478bd9Sstevel@tonic-gate 	off = 1 - on;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
15497c478bd9Sstevel@tonic-gate 		printf("%s groups (active):\n", gnames[i]);
1550*ab25eeb5Syz155240 		for (fp = fiop->f_groups[gnums[i]][on]; fp != NULL;
1551*ab25eeb5Syz155240 		     fp = grp.fg_next)
15527c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
15537c478bd9Sstevel@tonic-gate 				break;
15547c478bd9Sstevel@tonic-gate 			else
15557c478bd9Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
15567c478bd9Sstevel@tonic-gate 		printf("%s groups (inactive):\n", gnames[i]);
1557*ab25eeb5Syz155240 		for (fp = fiop->f_groups[gnums[i]][off]; fp != NULL;
1558*ab25eeb5Syz155240 		     fp = grp.fg_next)
15597c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
15607c478bd9Sstevel@tonic-gate 				break;
15617c478bd9Sstevel@tonic-gate 			else
15627c478bd9Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
15637c478bd9Sstevel@tonic-gate 	}
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate static void parse_ipportstr(argument, ip, port)
15677c478bd9Sstevel@tonic-gate const char *argument;
1568*ab25eeb5Syz155240 i6addr_t *ip;
15697c478bd9Sstevel@tonic-gate int *port;
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	char *s, *comma;
1572*ab25eeb5Syz155240 	int ok = 0;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	/* make working copy of argument, Theoretically you must be able
15757c478bd9Sstevel@tonic-gate 	 * to write to optarg, but that seems very ugly to me....
15767c478bd9Sstevel@tonic-gate 	 */
15777c478bd9Sstevel@tonic-gate 	s = strdup(argument);
15787c478bd9Sstevel@tonic-gate 	if (s == NULL)
15797c478bd9Sstevel@tonic-gate 		return;
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	/* get port */
15827c478bd9Sstevel@tonic-gate 	if ((comma = strchr(s, ',')) != NULL) {
15837c478bd9Sstevel@tonic-gate 		if (!strcasecmp(comma + 1, "any")) {
15847c478bd9Sstevel@tonic-gate 			*port = -1;
15857c478bd9Sstevel@tonic-gate 		} else if (!sscanf(comma + 1, "%d", port) ||
15867c478bd9Sstevel@tonic-gate 			   (*port < 0) || (*port > 65535)) {
15877c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Invalid port specfication in %s\n",
15887c478bd9Sstevel@tonic-gate 				argument);
15897c478bd9Sstevel@tonic-gate 			free(s);
15907c478bd9Sstevel@tonic-gate 			exit(-2);
15917c478bd9Sstevel@tonic-gate 		}
15927c478bd9Sstevel@tonic-gate 		*comma = '\0';
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/* get ip address */
15977c478bd9Sstevel@tonic-gate 	if (!strcasecmp(s, "any")) {
1598*ab25eeb5Syz155240 		ip->in4.s_addr = INADDR_ANY;
1599*ab25eeb5Syz155240 #ifdef	USE_INET6
1600*ab25eeb5Syz155240 		ip->in6 = in6addr_any;
1601*ab25eeb5Syz155240 	} else if (use_inet6 && inet_pton(AF_INET6, s, &ip->in6)) {
1602*ab25eeb5Syz155240 		ok = 1;
1603*ab25eeb5Syz155240 #endif
1604*ab25eeb5Syz155240 	} else if (inet_aton(s, &ip->in4))
1605*ab25eeb5Syz155240 		ok = 1;
1606*ab25eeb5Syz155240 
1607*ab25eeb5Syz155240 	if (ok == 0) {
16087c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Invalid IP address: %s\n", s);
16097c478bd9Sstevel@tonic-gate 		free(s);
16107c478bd9Sstevel@tonic-gate 		exit(-2);
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	/* free allocated memory */
16147c478bd9Sstevel@tonic-gate 	free(s);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate #ifdef STATETOP
1619*ab25eeb5Syz155240 static void sig_resize(s)
1620*ab25eeb5Syz155240 int s;
1621*ab25eeb5Syz155240 {
1622*ab25eeb5Syz155240 	handle_resize = 1;
1623*ab25eeb5Syz155240 }
1624*ab25eeb5Syz155240 
1625*ab25eeb5Syz155240 static void sig_break(s)
1626*ab25eeb5Syz155240 int s;
1627*ab25eeb5Syz155240 {
1628*ab25eeb5Syz155240 	handle_break = 1;
1629*ab25eeb5Syz155240 }
1630*ab25eeb5Syz155240 
1631*ab25eeb5Syz155240 static char *getip(v, addr)
1632*ab25eeb5Syz155240 int v;
1633*ab25eeb5Syz155240 i6addr_t *addr;
1634*ab25eeb5Syz155240 {
1635*ab25eeb5Syz155240 #ifdef  USE_INET6
1636*ab25eeb5Syz155240 	static char hostbuf[MAXHOSTNAMELEN+1];
1637*ab25eeb5Syz155240 #endif
1638*ab25eeb5Syz155240 
1639*ab25eeb5Syz155240 	if (v == 4)
1640*ab25eeb5Syz155240 		return inet_ntoa(addr->in4);
1641*ab25eeb5Syz155240 
1642*ab25eeb5Syz155240 #ifdef  USE_INET6
1643*ab25eeb5Syz155240 	(void) inet_ntop(AF_INET6, &addr->in6, hostbuf, sizeof(hostbuf) - 1);
1644*ab25eeb5Syz155240 	hostbuf[MAXHOSTNAMELEN] = '\0';
1645*ab25eeb5Syz155240 	return hostbuf;
1646*ab25eeb5Syz155240 #else
1647*ab25eeb5Syz155240 	return "IPv6";
1648*ab25eeb5Syz155240 #endif
1649*ab25eeb5Syz155240 }
1650*ab25eeb5Syz155240 
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate static char *ttl_to_string(ttl)
16537c478bd9Sstevel@tonic-gate long int ttl;
16547c478bd9Sstevel@tonic-gate {
1655*ab25eeb5Syz155240 	static char ttlbuf[STSTRSIZE];
16567c478bd9Sstevel@tonic-gate 	int hours, minutes, seconds;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	/* ttl is in half seconds */
16597c478bd9Sstevel@tonic-gate 	ttl /= 2;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	hours = ttl / 3600;
16627c478bd9Sstevel@tonic-gate 	ttl = ttl % 3600;
16637c478bd9Sstevel@tonic-gate 	minutes = ttl / 60;
16647c478bd9Sstevel@tonic-gate 	seconds = ttl % 60;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (hours > 0)
16677c478bd9Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d:%02d", hours, minutes, seconds);
16687c478bd9Sstevel@tonic-gate 	else
16697c478bd9Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d", minutes, seconds);
16707c478bd9Sstevel@tonic-gate 	return ttlbuf;
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate static int sort_pkts(a, b)
16757c478bd9Sstevel@tonic-gate const void *a;
16767c478bd9Sstevel@tonic-gate const void *b;
16777c478bd9Sstevel@tonic-gate {
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
16807c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	if (ap->st_pkts == bp->st_pkts)
16837c478bd9Sstevel@tonic-gate 		return 0;
16847c478bd9Sstevel@tonic-gate 	else if (ap->st_pkts < bp->st_pkts)
16857c478bd9Sstevel@tonic-gate 		return 1;
16867c478bd9Sstevel@tonic-gate 	return -1;
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate static int sort_bytes(a, b)
16917c478bd9Sstevel@tonic-gate const void *a;
16927c478bd9Sstevel@tonic-gate const void *b;
16937c478bd9Sstevel@tonic-gate {
16947c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
16957c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if (ap->st_bytes == bp->st_bytes)
16987c478bd9Sstevel@tonic-gate 		return 0;
16997c478bd9Sstevel@tonic-gate 	else if (ap->st_bytes < bp->st_bytes)
17007c478bd9Sstevel@tonic-gate 		return 1;
17017c478bd9Sstevel@tonic-gate 	return -1;
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate static int sort_p(a, b)
17067c478bd9Sstevel@tonic-gate const void *a;
17077c478bd9Sstevel@tonic-gate const void *b;
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
17107c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (ap->st_p == bp->st_p)
17137c478bd9Sstevel@tonic-gate 		return 0;
17147c478bd9Sstevel@tonic-gate 	else if (ap->st_p < bp->st_p)
17157c478bd9Sstevel@tonic-gate 		return 1;
17167c478bd9Sstevel@tonic-gate 	return -1;
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate static int sort_ttl(a, b)
17217c478bd9Sstevel@tonic-gate const void *a;
17227c478bd9Sstevel@tonic-gate const void *b;
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
17257c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	if (ap->st_age == bp->st_age)
17287c478bd9Sstevel@tonic-gate 		return 0;
17297c478bd9Sstevel@tonic-gate 	else if (ap->st_age < bp->st_age)
17307c478bd9Sstevel@tonic-gate 		return 1;
17317c478bd9Sstevel@tonic-gate 	return -1;
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate static int sort_srcip(a, b)
17357c478bd9Sstevel@tonic-gate const void *a;
17367c478bd9Sstevel@tonic-gate const void *b;
17377c478bd9Sstevel@tonic-gate {
17387c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
17397c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
17407c478bd9Sstevel@tonic-gate 
1741*ab25eeb5Syz155240 #ifdef USE_INET6
1742*ab25eeb5Syz155240 	if (use_inet6) {
1743*ab25eeb5Syz155240 		if (IP6_EQ(&ap->st_src, &bp->st_src))
17447c478bd9Sstevel@tonic-gate 			return 0;
1745*ab25eeb5Syz155240 		else if (IP6_GT(&ap->st_src, &bp->st_src))
1746*ab25eeb5Syz155240 			return 1;
1747*ab25eeb5Syz155240 	} else
1748*ab25eeb5Syz155240 #endif
1749*ab25eeb5Syz155240 	{
1750*ab25eeb5Syz155240 		if (ntohl(ap->st_src.in4.s_addr) ==
1751*ab25eeb5Syz155240 		    ntohl(bp->st_src.in4.s_addr))
1752*ab25eeb5Syz155240 			return 0;
1753*ab25eeb5Syz155240 		else if (ntohl(ap->st_src.in4.s_addr) >
1754*ab25eeb5Syz155240 		         ntohl(bp->st_src.in4.s_addr))
1755*ab25eeb5Syz155240 			return 1;
1756*ab25eeb5Syz155240 	}
1757*ab25eeb5Syz155240 	return -1;
1758*ab25eeb5Syz155240 }
1759*ab25eeb5Syz155240 
1760*ab25eeb5Syz155240 static int sort_srcpt(a, b)
1761*ab25eeb5Syz155240 const void *a;
1762*ab25eeb5Syz155240 const void *b;
1763*ab25eeb5Syz155240 {
1764*ab25eeb5Syz155240 	register const statetop_t *ap = a;
1765*ab25eeb5Syz155240 	register const statetop_t *bp = b;
1766*ab25eeb5Syz155240 
1767*ab25eeb5Syz155240 	if (htons(ap->st_sport) == htons(bp->st_sport))
1768*ab25eeb5Syz155240 		return 0;
1769*ab25eeb5Syz155240 	else if (htons(ap->st_sport) > htons(bp->st_sport))
17707c478bd9Sstevel@tonic-gate 		return 1;
17717c478bd9Sstevel@tonic-gate 	return -1;
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate static int sort_dstip(a, b)
17757c478bd9Sstevel@tonic-gate const void *a;
17767c478bd9Sstevel@tonic-gate const void *b;
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
17797c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
17807c478bd9Sstevel@tonic-gate 
1781*ab25eeb5Syz155240 #ifdef USE_INET6
1782*ab25eeb5Syz155240 	if (use_inet6) {
1783*ab25eeb5Syz155240 		if (IP6_EQ(&ap->st_dst, &bp->st_dst))
17847c478bd9Sstevel@tonic-gate 			return 0;
1785*ab25eeb5Syz155240 		else if (IP6_GT(&ap->st_dst, &bp->st_dst))
1786*ab25eeb5Syz155240 			return 1;
1787*ab25eeb5Syz155240 	} else
1788*ab25eeb5Syz155240 #endif
1789*ab25eeb5Syz155240 	{
1790*ab25eeb5Syz155240 		if (ntohl(ap->st_dst.in4.s_addr) ==
1791*ab25eeb5Syz155240 		    ntohl(bp->st_dst.in4.s_addr))
1792*ab25eeb5Syz155240 			return 0;
1793*ab25eeb5Syz155240 		else if (ntohl(ap->st_dst.in4.s_addr) >
1794*ab25eeb5Syz155240 		         ntohl(bp->st_dst.in4.s_addr))
1795*ab25eeb5Syz155240 			return 1;
1796*ab25eeb5Syz155240 	}
1797*ab25eeb5Syz155240 	return -1;
1798*ab25eeb5Syz155240 }
1799*ab25eeb5Syz155240 
1800*ab25eeb5Syz155240 static int sort_dstpt(a, b)
1801*ab25eeb5Syz155240 const void *a;
1802*ab25eeb5Syz155240 const void *b;
1803*ab25eeb5Syz155240 {
1804*ab25eeb5Syz155240 	register const statetop_t *ap = a;
1805*ab25eeb5Syz155240 	register const statetop_t *bp = b;
1806*ab25eeb5Syz155240 
1807*ab25eeb5Syz155240 	if (htons(ap->st_dport) == htons(bp->st_dport))
1808*ab25eeb5Syz155240 		return 0;
1809*ab25eeb5Syz155240 	else if (htons(ap->st_dport) > htons(bp->st_dport))
18107c478bd9Sstevel@tonic-gate 		return 1;
18117c478bd9Sstevel@tonic-gate 	return -1;
18127c478bd9Sstevel@tonic-gate }
1813*ab25eeb5Syz155240 
18147c478bd9Sstevel@tonic-gate #endif
1815