xref: /titanic_52/usr/src/cmd/ipf/tools/ipfstat.c (revision 6aed92a90298c01b144335994db0608c6ab7029f)
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  *
67c478bd9Sstevel@tonic-gate  * Copyright 2005 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>
237c478bd9Sstevel@tonic-gate #include <nlist.h>
247c478bd9Sstevel@tonic-gate #include <ctype.h>
257c478bd9Sstevel@tonic-gate #include <stddef.h>
267c478bd9Sstevel@tonic-gate #include "ipf.h"
277c478bd9Sstevel@tonic-gate #include "ipl.h"
287c478bd9Sstevel@tonic-gate #if defined(STATETOP)
297c478bd9Sstevel@tonic-gate # if defined(_BSDI_VERSION)
307c478bd9Sstevel@tonic-gate #  undef STATETOP)
317c478bd9Sstevel@tonic-gate # endif
327c478bd9Sstevel@tonic-gate # if defined(__FreeBSD__) && \
337c478bd9Sstevel@tonic-gate      (!defined(__FreeBSD_version) || (__FreeBSD_version < 430000))
347c478bd9Sstevel@tonic-gate #  undef STATETOP
357c478bd9Sstevel@tonic-gate # endif
367c478bd9Sstevel@tonic-gate # if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105000000)
377c478bd9Sstevel@tonic-gate #  undef STATETOP
387c478bd9Sstevel@tonic-gate # endif
397c478bd9Sstevel@tonic-gate # if defined(sun)
407c478bd9Sstevel@tonic-gate #  if defined(__svr4__) || defined(__SVR4)
417c478bd9Sstevel@tonic-gate #   include <sys/select.h>
427c478bd9Sstevel@tonic-gate #  else
437c478bd9Sstevel@tonic-gate #   undef STATETOP	/* NOT supported on SunOS4 */
447c478bd9Sstevel@tonic-gate #  endif
457c478bd9Sstevel@tonic-gate # endif
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate #if defined(STATETOP) && !defined(linux)
487c478bd9Sstevel@tonic-gate # include <netinet/ip_var.h>
497c478bd9Sstevel@tonic-gate # include <netinet/tcp_fsm.h>
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate #ifdef STATETOP
527c478bd9Sstevel@tonic-gate #if SOLARIS2 >= 10
537c478bd9Sstevel@tonic-gate # include "ipl.h"
547c478bd9Sstevel@tonic-gate #else
557c478bd9Sstevel@tonic-gate # include "netinet/ipl.h"
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate # if SOLARIS || defined(__NetBSD__) || defined(_BSDI_VERSION) || \
587c478bd9Sstevel@tonic-gate      defined(__sgi)
597c478bd9Sstevel@tonic-gate #  ifdef ERR
607c478bd9Sstevel@tonic-gate #   undef ERR
617c478bd9Sstevel@tonic-gate #  endif
627c478bd9Sstevel@tonic-gate #  include <curses.h>
637c478bd9Sstevel@tonic-gate # else /* SOLARIS */
647c478bd9Sstevel@tonic-gate #  include <ncurses.h>
657c478bd9Sstevel@tonic-gate # endif /* SOLARIS */
667c478bd9Sstevel@tonic-gate #endif /* STATETOP */
677c478bd9Sstevel@tonic-gate #include "kmem.h"
687c478bd9Sstevel@tonic-gate #if defined(__NetBSD__) || (__OpenBSD__)
697c478bd9Sstevel@tonic-gate # include <paths.h>
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #if !defined(lint)
737c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)fils.c	1.21 4/20/96 (C) 1993-2000 Darren Reed";
747c478bd9Sstevel@tonic-gate static const char rcsid[] = "@(#)$Id: ipfstat.c,v 1.28 2003/07/01 01:03:04 darrenr Exp $";
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #ifdef __hpux
787c478bd9Sstevel@tonic-gate # define	nlist	nlist64
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate extern	char	*optarg;
827c478bd9Sstevel@tonic-gate extern	int	optind;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	PRINTF	(void)printf
857c478bd9Sstevel@tonic-gate #define	FPRINTF	(void)fprintf
867c478bd9Sstevel@tonic-gate #define	F_IN	0
877c478bd9Sstevel@tonic-gate #define	F_OUT	1
887c478bd9Sstevel@tonic-gate #define	F_ACIN	2
897c478bd9Sstevel@tonic-gate #define	F_ACOUT	3
907c478bd9Sstevel@tonic-gate static	char	*filters[4] = { "ipfilter(in)", "ipfilter(out)",
917c478bd9Sstevel@tonic-gate 				"ipacct(in)", "ipacct(out)" };
927c478bd9Sstevel@tonic-gate static	int	state_logging = -1;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int	opts = 0;
957c478bd9Sstevel@tonic-gate int	use_inet6 = 0;
967c478bd9Sstevel@tonic-gate int	live_kernel = 1;
977c478bd9Sstevel@tonic-gate int	state_fd = -1;
987c478bd9Sstevel@tonic-gate int	ipf_fd = -1;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #ifdef STATETOP
1017c478bd9Sstevel@tonic-gate #define	STSTRSIZE 	80
1027c478bd9Sstevel@tonic-gate #define	STGROWSIZE	16
1037c478bd9Sstevel@tonic-gate #define	HOSTNMLEN	40
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	STSORT_PR	0
1067c478bd9Sstevel@tonic-gate #define	STSORT_PKTS	1
1077c478bd9Sstevel@tonic-gate #define	STSORT_BYTES	2
1087c478bd9Sstevel@tonic-gate #define	STSORT_TTL	3
1097c478bd9Sstevel@tonic-gate #define	STSORT_SRCIP	4
1107c478bd9Sstevel@tonic-gate #define	STSORT_DSTIP	5
1117c478bd9Sstevel@tonic-gate #define	STSORT_MAX	STSORT_DSTIP
1127c478bd9Sstevel@tonic-gate #define	STSORT_DEFAULT	STSORT_BYTES
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate typedef struct statetop {
1167c478bd9Sstevel@tonic-gate 	i6addr_t	st_src;
1177c478bd9Sstevel@tonic-gate 	i6addr_t	st_dst;
1187c478bd9Sstevel@tonic-gate 	u_short		st_sport;
1197c478bd9Sstevel@tonic-gate 	u_short 	st_dport;
1207c478bd9Sstevel@tonic-gate 	u_char		st_p;
1217c478bd9Sstevel@tonic-gate 	u_char		st_state[2];
1227c478bd9Sstevel@tonic-gate 	U_QUAD_T	st_pkts;
1237c478bd9Sstevel@tonic-gate 	U_QUAD_T	st_bytes;
1247c478bd9Sstevel@tonic-gate 	u_long		st_age;
1257c478bd9Sstevel@tonic-gate } statetop_t;
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate extern	int	main __P((int, char *[]));
1297c478bd9Sstevel@tonic-gate static	void	showstats __P((friostat_t *, u_32_t));
1307c478bd9Sstevel@tonic-gate static	void	showfrstates __P((ipfrstat_t *));
1317c478bd9Sstevel@tonic-gate static	void	showlist __P((friostat_t *));
1327c478bd9Sstevel@tonic-gate static	void	showipstates __P((ips_stat_t *));
1337c478bd9Sstevel@tonic-gate static	void	showauthstates __P((fr_authstat_t *));
1347c478bd9Sstevel@tonic-gate static	void	showgroups __P((friostat_t *));
1357c478bd9Sstevel@tonic-gate static	void	Usage __P((char *));
1367c478bd9Sstevel@tonic-gate static	void	printlist __P((frentry_t *, char *));
1377c478bd9Sstevel@tonic-gate static	void	parse_ipportstr __P((const char *, struct in_addr *, int *));
1387c478bd9Sstevel@tonic-gate static	void	ipfstate_live __P((char *, friostat_t **, ips_stat_t **,
1397c478bd9Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1407c478bd9Sstevel@tonic-gate static	void	ipfstate_dead __P((char *, friostat_t **, ips_stat_t **,
1417c478bd9Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1427c478bd9Sstevel@tonic-gate #ifdef STATETOP
1437c478bd9Sstevel@tonic-gate static	void	topipstates __P((struct in_addr, struct in_addr, int, int, int, int, int));
1447c478bd9Sstevel@tonic-gate static	char	*ttl_to_string __P((long));
1457c478bd9Sstevel@tonic-gate static	int	sort_p __P((const void *, const void *));
1467c478bd9Sstevel@tonic-gate static	int	sort_pkts __P((const void *, const void *));
1477c478bd9Sstevel@tonic-gate static	int	sort_bytes __P((const void *, const void *));
1487c478bd9Sstevel@tonic-gate static	int	sort_ttl __P((const void *, const void *));
1497c478bd9Sstevel@tonic-gate static	int	sort_srcip __P((const void *, const void *));
1507c478bd9Sstevel@tonic-gate static	int	sort_dstip __P((const void *, const void *));
1517c478bd9Sstevel@tonic-gate #endif
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static void Usage(name)
1557c478bd9Sstevel@tonic-gate char *name;
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate #ifdef  USE_INET6
1587c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Usage: %s [-6aAfhIinosv] [-d <device>]\n", name);
1597c478bd9Sstevel@tonic-gate #else
1607c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Usage: %s [-aAfhIinosv] [-d <device>]\n", name);
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\t\t[-M corefile] [-N symbol-list]\n");
1637c478bd9Sstevel@tonic-gate 	fprintf(stderr, "       %s -t [-S source address] [-D destination address] [-P protocol] [-T refreshtime] [-C] [-d <device>]\n", name);
1647c478bd9Sstevel@tonic-gate 	exit(1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int main(argc,argv)
1697c478bd9Sstevel@tonic-gate int argc;
1707c478bd9Sstevel@tonic-gate char *argv[];
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	fr_authstat_t	frauthst;
1737c478bd9Sstevel@tonic-gate 	fr_authstat_t	*frauthstp = &frauthst;
1747c478bd9Sstevel@tonic-gate 	friostat_t fio;
1757c478bd9Sstevel@tonic-gate 	friostat_t *fiop = &fio;
1767c478bd9Sstevel@tonic-gate 	ips_stat_t ipsst;
1777c478bd9Sstevel@tonic-gate 	ips_stat_t *ipsstp = &ipsst;
1787c478bd9Sstevel@tonic-gate 	ipfrstat_t ifrst;
1797c478bd9Sstevel@tonic-gate 	ipfrstat_t *ifrstp = &ifrst;
1807c478bd9Sstevel@tonic-gate 	char	*device = IPL_NAME, *memf = NULL;
1817c478bd9Sstevel@tonic-gate 	char	*kern = NULL;
1827c478bd9Sstevel@tonic-gate 	int	c, myoptind;
1837c478bd9Sstevel@tonic-gate 	struct protoent *proto;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	int protocol = -1;		/* -1 = wild card for any protocol */
1867c478bd9Sstevel@tonic-gate 	int refreshtime = 1; 		/* default update time */
1877c478bd9Sstevel@tonic-gate 	int sport = -1;			/* -1 = wild card for any source port */
1887c478bd9Sstevel@tonic-gate 	int dport = -1;			/* -1 = wild card for any dest port */
1897c478bd9Sstevel@tonic-gate 	int topclosed = 0;		/* do not show closed tcp sessions */
1907c478bd9Sstevel@tonic-gate 	struct in_addr saddr, daddr;
1917c478bd9Sstevel@tonic-gate 	u_32_t frf;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	saddr.s_addr = INADDR_ANY; 	/* default any source addr */
1947c478bd9Sstevel@tonic-gate 	daddr.s_addr = INADDR_ANY; 	/* default any dest addr */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Parse these two arguments now lest there be any buffer overflows
1987c478bd9Sstevel@tonic-gate 	 * in the parsing of the rest.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	myoptind = optind;
2017c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "6aACdfghIilnostvD:M:N:P:S:T:")) != -1)
2027c478bd9Sstevel@tonic-gate 		switch (c)
2037c478bd9Sstevel@tonic-gate 		{
2047c478bd9Sstevel@tonic-gate 		case 'M' :
2057c478bd9Sstevel@tonic-gate 			memf = optarg;
2067c478bd9Sstevel@tonic-gate 			live_kernel = 0;
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 		case 'N' :
2097c478bd9Sstevel@tonic-gate 			kern = optarg;
2107c478bd9Sstevel@tonic-gate 			live_kernel = 0;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	optind = myoptind;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (live_kernel == 1) {
2167c478bd9Sstevel@tonic-gate 		if ((state_fd = open(IPSTATE_NAME, O_RDONLY)) == -1) {
2177c478bd9Sstevel@tonic-gate 			perror("open");
2187c478bd9Sstevel@tonic-gate 			exit(-1);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
2217c478bd9Sstevel@tonic-gate 			perror("open");
2227c478bd9Sstevel@tonic-gate 			exit(-1);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (kern != NULL || memf != NULL)
2277c478bd9Sstevel@tonic-gate 	{
2287c478bd9Sstevel@tonic-gate 		(void)setgid(getgid());
2297c478bd9Sstevel@tonic-gate 		(void)setreuid(getuid(), getuid());
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (openkmem(kern, memf) == -1)
2337c478bd9Sstevel@tonic-gate 		exit(-1);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	(void)setgid(getgid());
2367c478bd9Sstevel@tonic-gate 	(void)setreuid(getuid(), getuid());
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "6aACdfghIilnostvD:M:N:P:S:T:")) != -1)
2397c478bd9Sstevel@tonic-gate 	{
2407c478bd9Sstevel@tonic-gate 		switch (c)
2417c478bd9Sstevel@tonic-gate 		{
2427c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
2437c478bd9Sstevel@tonic-gate 		case '6' :
2447c478bd9Sstevel@tonic-gate 			use_inet6 = 1;
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate 		case 'a' :
2487c478bd9Sstevel@tonic-gate 			opts |= OPT_ACCNT|OPT_SHOWLIST;
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		case 'A' :
2517c478bd9Sstevel@tonic-gate 			opts |= OPT_AUTHSTATS;
2527c478bd9Sstevel@tonic-gate 			break;
2537c478bd9Sstevel@tonic-gate 		case 'C' :
2547c478bd9Sstevel@tonic-gate 			topclosed = 1;
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 		case 'd' :
2577c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
2587c478bd9Sstevel@tonic-gate 			break;
2597c478bd9Sstevel@tonic-gate 		case 'D' :
2607c478bd9Sstevel@tonic-gate 			parse_ipportstr(optarg, &daddr, &dport);
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 		case 'f' :
2637c478bd9Sstevel@tonic-gate 			opts |= OPT_FRSTATES;
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 		case 'g' :
2667c478bd9Sstevel@tonic-gate 			opts |= OPT_GROUPS;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		case 'h' :
2697c478bd9Sstevel@tonic-gate 			opts |= OPT_HITS;
2707c478bd9Sstevel@tonic-gate 			break;
2717c478bd9Sstevel@tonic-gate 		case 'i' :
2727c478bd9Sstevel@tonic-gate 			opts |= OPT_INQUE|OPT_SHOWLIST;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 		case 'I' :
2757c478bd9Sstevel@tonic-gate 			opts |= OPT_INACTIVE;
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 		case 'l' :
2787c478bd9Sstevel@tonic-gate 			opts |= OPT_SHOWLIST;
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 		case 'M' :
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 		case 'N' :
2837c478bd9Sstevel@tonic-gate 			break;
2847c478bd9Sstevel@tonic-gate 		case 'n' :
2857c478bd9Sstevel@tonic-gate 			opts |= OPT_SHOWLINENO;
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case 'o' :
2887c478bd9Sstevel@tonic-gate 			opts |= OPT_OUTQUE|OPT_SHOWLIST;
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		case 'P' :
2917c478bd9Sstevel@tonic-gate 			if ((proto = getprotobyname(optarg)) != NULL) {
2927c478bd9Sstevel@tonic-gate 				protocol = proto->p_proto;
2937c478bd9Sstevel@tonic-gate 			} else if (!sscanf(optarg, "%d", &protocol) ||
2947c478bd9Sstevel@tonic-gate 					   (protocol < 0)) {
2957c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s : Invalid protocol: %s\n",
2967c478bd9Sstevel@tonic-gate 					argv[0], optarg);
2977c478bd9Sstevel@tonic-gate 				exit(-2);
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate 		case 's' :
3017c478bd9Sstevel@tonic-gate 			opts |= OPT_IPSTATES;
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 		case 'S' :
3047c478bd9Sstevel@tonic-gate 			parse_ipportstr(optarg, &saddr, &sport);
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 		case 't' :
3077c478bd9Sstevel@tonic-gate #ifdef STATETOP
3087c478bd9Sstevel@tonic-gate 			opts |= OPT_STATETOP;
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate #else
3117c478bd9Sstevel@tonic-gate 			fprintf(stderr,
3127c478bd9Sstevel@tonic-gate 				"%s : state top facility not compiled in\n",
3137c478bd9Sstevel@tonic-gate 				argv[0]);
3147c478bd9Sstevel@tonic-gate 			exit(-2);
3157c478bd9Sstevel@tonic-gate #endif
3167c478bd9Sstevel@tonic-gate 		case 'T' :
3177c478bd9Sstevel@tonic-gate 			if (!sscanf(optarg, "%d", &refreshtime) ||
3187c478bd9Sstevel@tonic-gate 				    (refreshtime <= 0)) {
3197c478bd9Sstevel@tonic-gate 				fprintf(stderr,
3207c478bd9Sstevel@tonic-gate 					"%s : Invalid refreshtime < 1 : %s\n",
3217c478bd9Sstevel@tonic-gate 					argv[0], optarg);
3227c478bd9Sstevel@tonic-gate 				exit(-2);
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 		case 'v' :
3267c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
3277c478bd9Sstevel@tonic-gate 			opts |= OPT_UNDEF;
3287c478bd9Sstevel@tonic-gate 			break;
3297c478bd9Sstevel@tonic-gate 		default :
3307c478bd9Sstevel@tonic-gate 			Usage(argv[0]);
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (live_kernel == 1) {
3367c478bd9Sstevel@tonic-gate 		bzero((char *)&fio, sizeof(fio));
3377c478bd9Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
3387c478bd9Sstevel@tonic-gate 		bzero((char *)&ifrst, sizeof(ifrst));
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		ipfstate_live(device, &fiop, &ipsstp, &ifrstp,
3417c478bd9Sstevel@tonic-gate 			      &frauthstp, &frf);
3427c478bd9Sstevel@tonic-gate 	} else
3437c478bd9Sstevel@tonic-gate 		ipfstate_dead(kern, &fiop, &ipsstp, &ifrstp, &frauthstp, &frf);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (opts & OPT_IPSTATES) {
3467c478bd9Sstevel@tonic-gate 		showipstates(ipsstp);
3477c478bd9Sstevel@tonic-gate 	} else if (opts & OPT_SHOWLIST) {
3487c478bd9Sstevel@tonic-gate 		showlist(fiop);
3497c478bd9Sstevel@tonic-gate 		if ((opts & OPT_OUTQUE) && (opts & OPT_INQUE)){
3507c478bd9Sstevel@tonic-gate 			opts &= ~OPT_OUTQUE;
3517c478bd9Sstevel@tonic-gate 			showlist(fiop);
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 	} else {
3547c478bd9Sstevel@tonic-gate 		if (opts & OPT_FRSTATES)
3557c478bd9Sstevel@tonic-gate 			showfrstates(ifrstp);
3567c478bd9Sstevel@tonic-gate #ifdef STATETOP
3577c478bd9Sstevel@tonic-gate 		else if (opts & OPT_STATETOP)
3587c478bd9Sstevel@tonic-gate 			topipstates(saddr, daddr, sport, dport,
3597c478bd9Sstevel@tonic-gate 				    protocol, refreshtime, topclosed);
3607c478bd9Sstevel@tonic-gate #endif
3617c478bd9Sstevel@tonic-gate 		else if (opts & OPT_AUTHSTATS)
3627c478bd9Sstevel@tonic-gate 			showauthstates(frauthstp);
3637c478bd9Sstevel@tonic-gate 		else if (opts & OPT_GROUPS)
3647c478bd9Sstevel@tonic-gate 			showgroups(fiop);
3657c478bd9Sstevel@tonic-gate 		else
3667c478bd9Sstevel@tonic-gate 			showstats(fiop, frf);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	return 0;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * Fill in the stats structures from the live kernel, using a combination
3747c478bd9Sstevel@tonic-gate  * of ioctl's and copying directly from kernel memory.
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate static void ipfstate_live(device, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
3777c478bd9Sstevel@tonic-gate char *device;
3787c478bd9Sstevel@tonic-gate friostat_t **fiopp;
3797c478bd9Sstevel@tonic-gate ips_stat_t **ipsstpp;
3807c478bd9Sstevel@tonic-gate ipfrstat_t **ifrstpp;
3817c478bd9Sstevel@tonic-gate fr_authstat_t **frauthstpp;
3827c478bd9Sstevel@tonic-gate u_32_t *frfp;
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (checkrev(device) == -1) {
3877c478bd9Sstevel@tonic-gate 		fprintf(stderr, "User/kernel version check failed\n");
3887c478bd9Sstevel@tonic-gate 		exit(1);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) == 0) {
3927c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
3937c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
3947c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(friostat_t);
3957c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*fiopp;
3967c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_IPFSTAT;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFS, &ipfo) == -1) {
3997c478bd9Sstevel@tonic-gate 			perror("ioctl(ipf:SIOCGETFS)");
4007c478bd9Sstevel@tonic-gate 			exit(-1);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFF, frfp) == -1)
4047c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGETFF)");
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if ((opts & OPT_IPSTATES) != 0) {
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4107c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4117c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ips_stat_t);
4127c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ipsstpp;
4137c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_STATESTAT;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
4167c478bd9Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETFS)");
4177c478bd9Sstevel@tonic-gate 			exit(-1);
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 		if (ioctl(state_fd, SIOCGETLG, &state_logging) == -1) {
4207c478bd9Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETLG)");
4217c478bd9Sstevel@tonic-gate 			exit(-1);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if ((opts & OPT_FRSTATES) != 0) {
4267c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4277c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4287c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ipfrstat_t);
4297c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ifrstpp;
4307c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_FRAGSTAT;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGFRST, &ipfo) == -1) {
4337c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGFRST)");
4347c478bd9Sstevel@tonic-gate 			exit(-1);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
4397c478bd9Sstevel@tonic-gate 		PRINTF("opts %#x name %s\n", opts, device);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) != 0) {
4427c478bd9Sstevel@tonic-gate 		if (ipf_fd >= 0) {
4437c478bd9Sstevel@tonic-gate 			close(ipf_fd);
4447c478bd9Sstevel@tonic-gate 			ipf_fd = -1;
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		device = IPAUTH_NAME;
4477c478bd9Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
4487c478bd9Sstevel@tonic-gate 			perror("open");
4497c478bd9Sstevel@tonic-gate 			exit(-1);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4537c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4547c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(fr_authstat_t);
4557c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*frauthstpp;
4567c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_AUTHSTAT;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	    	if (ioctl(ipf_fd, SIOCATHST, &ipfo) == -1) {
4597c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCATHST)");
4607c478bd9Sstevel@tonic-gate 			exit(-1);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * Build up the stats structures from data held in the "core" memory.
4687c478bd9Sstevel@tonic-gate  * This is mainly useful when looking at data in crash dumps and ioctl's
4697c478bd9Sstevel@tonic-gate  * just won't work any more.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate static void ipfstate_dead(kernel, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
4727c478bd9Sstevel@tonic-gate char *kernel;
4737c478bd9Sstevel@tonic-gate friostat_t **fiopp;
4747c478bd9Sstevel@tonic-gate ips_stat_t **ipsstpp;
4757c478bd9Sstevel@tonic-gate ipfrstat_t **ifrstpp;
4767c478bd9Sstevel@tonic-gate fr_authstat_t **frauthstpp;
4777c478bd9Sstevel@tonic-gate u_32_t *frfp;
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	static fr_authstat_t frauthst, *frauthstp;
4807c478bd9Sstevel@tonic-gate 	static ips_stat_t ipsst, *ipsstp;
4817c478bd9Sstevel@tonic-gate 	static ipfrstat_t ifrst, *ifrstp;
4827c478bd9Sstevel@tonic-gate 	static friostat_t fio, *fiop;
4837c478bd9Sstevel@tonic-gate 	int temp;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	void *rules[2][2];
4867c478bd9Sstevel@tonic-gate 	struct nlist deadlist[43] = {
4877c478bd9Sstevel@tonic-gate 		{ "fr_authstats" },		/* 0 */
4887c478bd9Sstevel@tonic-gate 		{ "fae_list" },
4897c478bd9Sstevel@tonic-gate 		{ "ipauth" },
4907c478bd9Sstevel@tonic-gate 		{ "fr_authlist" },
4917c478bd9Sstevel@tonic-gate 		{ "fr_authstart" },
4927c478bd9Sstevel@tonic-gate 		{ "fr_authend" },		/* 5 */
4937c478bd9Sstevel@tonic-gate 		{ "fr_authnext" },
4947c478bd9Sstevel@tonic-gate 		{ "fr_auth" },
4957c478bd9Sstevel@tonic-gate 		{ "fr_authused" },
4967c478bd9Sstevel@tonic-gate 		{ "fr_authsize" },
4977c478bd9Sstevel@tonic-gate 		{ "fr_defaultauthage" },	/* 10 */
4987c478bd9Sstevel@tonic-gate 		{ "fr_authpkts" },
4997c478bd9Sstevel@tonic-gate 		{ "fr_auth_lock" },
5007c478bd9Sstevel@tonic-gate 		{ "frstats" },
5017c478bd9Sstevel@tonic-gate 		{ "ips_stats" },
5027c478bd9Sstevel@tonic-gate 		{ "ips_num" },			/* 15 */
5037c478bd9Sstevel@tonic-gate 		{ "ips_wild" },
5047c478bd9Sstevel@tonic-gate 		{ "ips_list" },
5057c478bd9Sstevel@tonic-gate 		{ "ips_table" },
5067c478bd9Sstevel@tonic-gate 		{ "fr_statemax" },
5077c478bd9Sstevel@tonic-gate 		{ "fr_statesize" },		/* 20 */
5087c478bd9Sstevel@tonic-gate 		{ "fr_state_doflush" },
5097c478bd9Sstevel@tonic-gate 		{ "fr_state_lock" },
5107c478bd9Sstevel@tonic-gate 		{ "ipfr_heads" },
5117c478bd9Sstevel@tonic-gate 		{ "ipfr_nattab" },
5127c478bd9Sstevel@tonic-gate 		{ "ipfr_stats" },		/* 25 */
5137c478bd9Sstevel@tonic-gate 		{ "ipfr_inuse" },
5147c478bd9Sstevel@tonic-gate 		{ "fr_ipfrttl" },
5157c478bd9Sstevel@tonic-gate 		{ "fr_frag_lock" },
5167c478bd9Sstevel@tonic-gate 		{ "ipfr_timer_id" },
5177c478bd9Sstevel@tonic-gate 		{ "fr_nat_lock" },		/* 30 */
5187c478bd9Sstevel@tonic-gate 		{ "ipfilter" },
5197c478bd9Sstevel@tonic-gate 		{ "ipfilter6" },
5207c478bd9Sstevel@tonic-gate 		{ "ipacct" },
5217c478bd9Sstevel@tonic-gate 		{ "ipacct6" },
5227c478bd9Sstevel@tonic-gate 		{ "ipl_frouteok" },		/* 35 */
5237c478bd9Sstevel@tonic-gate 		{ "fr_running" },
5247c478bd9Sstevel@tonic-gate 		{ "ipfgroups" },
5257c478bd9Sstevel@tonic-gate 		{ "fr_active" },
5267c478bd9Sstevel@tonic-gate 		{ "fr_pass" },
5277c478bd9Sstevel@tonic-gate 		{ "fr_flags" },			/* 40 */
5287c478bd9Sstevel@tonic-gate 		{ "ipstate_logging" },
5297c478bd9Sstevel@tonic-gate 		{ NULL }
5307c478bd9Sstevel@tonic-gate 	};
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	frauthstp = &frauthst;
5347c478bd9Sstevel@tonic-gate 	ipsstp = &ipsst;
5357c478bd9Sstevel@tonic-gate 	ifrstp = &ifrst;
5367c478bd9Sstevel@tonic-gate 	fiop = &fio;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	*frfp = 0;
5397c478bd9Sstevel@tonic-gate 	*fiopp = fiop;
5407c478bd9Sstevel@tonic-gate 	*ipsstpp = ipsstp;
5417c478bd9Sstevel@tonic-gate 	*ifrstpp = ifrstp;
5427c478bd9Sstevel@tonic-gate 	*frauthstpp = frauthstp;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	bzero((char *)fiop, sizeof(*fiop));
5457c478bd9Sstevel@tonic-gate 	bzero((char *)ipsstp, sizeof(*ipsstp));
5467c478bd9Sstevel@tonic-gate 	bzero((char *)ifrstp, sizeof(*ifrstp));
5477c478bd9Sstevel@tonic-gate 	bzero((char *)frauthstp, sizeof(*frauthstp));
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (nlist(kernel, deadlist) == -1) {
5507c478bd9Sstevel@tonic-gate 		fprintf(stderr, "nlist error\n");
5517c478bd9Sstevel@tonic-gate 		return;
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * This is for SIOCGETFF.
5567c478bd9Sstevel@tonic-gate 	 */
5577c478bd9Sstevel@tonic-gate 	kmemcpy((char *)frfp, (u_long)deadlist[40].n_value, sizeof(*frfp));
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	/*
5607c478bd9Sstevel@tonic-gate 	 * f_locks is a combination of the lock variable from each part of
5617c478bd9Sstevel@tonic-gate 	 * ipfilter (state, auth, nat, fragments).
5627c478bd9Sstevel@tonic-gate 	 */
5637c478bd9Sstevel@tonic-gate 	kmemcpy((char *)fiop, (u_long)deadlist[13].n_value, sizeof(*fiop));
5647c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[22].n_value,
5657c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[0]));
5667c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[30].n_value,
5677c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[1]));
5687c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[2], (u_long)deadlist[28].n_value,
5697c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[2]));
5707c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[3], (u_long)deadlist[12].n_value,
5717c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[3]));
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * Get pointers to each list of rules (active, inactive, in, out)
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[31].n_value, sizeof(rules));
5777c478bd9Sstevel@tonic-gate 	fiop->f_fin[0] = rules[0][0];
5787c478bd9Sstevel@tonic-gate 	fiop->f_fin[1] = rules[0][1];
5797c478bd9Sstevel@tonic-gate 	fiop->f_fout[0] = rules[1][0];
5807c478bd9Sstevel@tonic-gate 	fiop->f_fout[1] = rules[1][1];
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * Same for IPv6, except make them null if support for it is not
5847c478bd9Sstevel@tonic-gate 	 * being compiled in.
5857c478bd9Sstevel@tonic-gate 	 */
5867c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
5877c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[32].n_value, sizeof(rules));
5887c478bd9Sstevel@tonic-gate 	fiop->f_fin6[0] = rules[0][0];
5897c478bd9Sstevel@tonic-gate 	fiop->f_fin6[1] = rules[0][1];
5907c478bd9Sstevel@tonic-gate 	fiop->f_fout6[0] = rules[1][0];
5917c478bd9Sstevel@tonic-gate 	fiop->f_fout6[1] = rules[1][1];
5927c478bd9Sstevel@tonic-gate #else
5937c478bd9Sstevel@tonic-gate 	fiop->f_fin6[0] = NULL;
5947c478bd9Sstevel@tonic-gate 	fiop->f_fin6[1] = NULL;
5957c478bd9Sstevel@tonic-gate 	fiop->f_fout6[0] = NULL;
5967c478bd9Sstevel@tonic-gate 	fiop->f_fout6[1] = NULL;
5977c478bd9Sstevel@tonic-gate #endif
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	/*
6007c478bd9Sstevel@tonic-gate 	 * Now get accounting rules pointers.
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[33].n_value, sizeof(rules));
6037c478bd9Sstevel@tonic-gate 	fiop->f_acctin[0] = rules[0][0];
6047c478bd9Sstevel@tonic-gate 	fiop->f_acctin[1] = rules[0][1];
6057c478bd9Sstevel@tonic-gate 	fiop->f_acctout[0] = rules[1][0];
6067c478bd9Sstevel@tonic-gate 	fiop->f_acctout[1] = rules[1][1];
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
6097c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[34].n_value, sizeof(rules));
6107c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[0] = rules[0][0];
6117c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[1] = rules[0][1];
6127c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[0] = rules[1][0];
6137c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[1] = rules[1][1];
6147c478bd9Sstevel@tonic-gate #else
6157c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[0] = NULL;
6167c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[1] = NULL;
6177c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[0] = NULL;
6187c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[1] = NULL;
6197c478bd9Sstevel@tonic-gate #endif
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * A collection of "global" variables used inside the kernel which
6237c478bd9Sstevel@tonic-gate 	 * are all collected in friostat_t via ioctl.
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_froute, (u_long)deadlist[35].n_value,
6267c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_froute));
6277c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_running, (u_long)deadlist[36].n_value,
6287c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_running));
6297c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_groups, (u_long)deadlist[37].n_value,
6307c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_groups));
6317c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_active, (u_long)deadlist[38].n_value,
6327c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_active));
6337c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_defpass, (u_long)deadlist[39].n_value,
6347c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_defpass));
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/*
6377c478bd9Sstevel@tonic-gate 	 * Build up the state information stats structure.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	kmemcpy((char *)ipsstp, (u_long)deadlist[14].n_value, sizeof(*ipsstp));
6407c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&temp, (u_long)deadlist[15].n_value, sizeof(temp));
6417c478bd9Sstevel@tonic-gate 	ipsstp->iss_active = temp;
6427c478bd9Sstevel@tonic-gate 	ipsstp->iss_table = (void *)deadlist[18].n_value;
6437c478bd9Sstevel@tonic-gate 	ipsstp->iss_list = (void *)deadlist[17].n_value;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/*
6467c478bd9Sstevel@tonic-gate 	 * Build up the authentiation information stats structure.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	kmemcpy((char *)frauthstp, (u_long)deadlist[0].n_value,
6497c478bd9Sstevel@tonic-gate 		sizeof(*frauthstp));
6507c478bd9Sstevel@tonic-gate 	frauthstp->fas_faelist = (void *)deadlist[1].n_value;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * Build up the fragment information stats structure.
6547c478bd9Sstevel@tonic-gate 	 */
6557c478bd9Sstevel@tonic-gate 	kmemcpy((char *)ifrstp, (u_long)deadlist[25].n_value,
6567c478bd9Sstevel@tonic-gate 		sizeof(*ifrstp));
6577c478bd9Sstevel@tonic-gate 	ifrstp->ifs_table = (void *)deadlist[23].n_value;
6587c478bd9Sstevel@tonic-gate 	ifrstp->ifs_nattab = (void *)deadlist[24].n_value;
6597c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&ifrstp->ifs_inuse, (u_long)deadlist[26].n_value,
6607c478bd9Sstevel@tonic-gate 		sizeof(ifrstp->ifs_inuse));
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/*
6637c478bd9Sstevel@tonic-gate 	 * Get logging on/off switches
6647c478bd9Sstevel@tonic-gate 	 */
6657c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&state_logging, (u_long)deadlist[41].n_value,
6667c478bd9Sstevel@tonic-gate 		sizeof(state_logging));
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate  * Display the kernel stats for packets blocked and passed and other
6727c478bd9Sstevel@tonic-gate  * associated running totals which are kept.
6737c478bd9Sstevel@tonic-gate  */
6747c478bd9Sstevel@tonic-gate static	void	showstats(fp, frf)
6757c478bd9Sstevel@tonic-gate struct	friostat	*fp;
6767c478bd9Sstevel@tonic-gate u_32_t frf;
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	PRINTF("bad packets:\t\tin %lu\tout %lu\n",
6807c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
6817c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
6827c478bd9Sstevel@tonic-gate 	PRINTF(" IPv6 packets:\t\tin %lu out %lu\n",
6837c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ipv6, fp->f_st[1].fr_ipv6);
6847c478bd9Sstevel@tonic-gate #endif
6857c478bd9Sstevel@tonic-gate 	PRINTF(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
6867c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
6877c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_nom);
6887c478bd9Sstevel@tonic-gate 	PRINTF(" counted %lu short %lu\n",
6897c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_acct, fp->f_st[0].fr_short);
6907c478bd9Sstevel@tonic-gate 	PRINTF("output packets:\t\tblocked %lu passed %lu nomatch %lu",
6917c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
6927c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_nom);
6937c478bd9Sstevel@tonic-gate 	PRINTF(" counted %lu short %lu\n",
6947c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_acct, fp->f_st[1].fr_short);
6957c478bd9Sstevel@tonic-gate 	PRINTF(" input packets logged:\tblocked %lu passed %lu\n",
6967c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
6977c478bd9Sstevel@tonic-gate 	PRINTF("output packets logged:\tblocked %lu passed %lu\n",
6987c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
6997c478bd9Sstevel@tonic-gate 	PRINTF(" packets logged:\tinput %lu output %lu\n",
7007c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pkl, fp->f_st[1].fr_pkl);
7017c478bd9Sstevel@tonic-gate 	PRINTF(" log failures:\t\tinput %lu output %lu\n",
7027c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_skip, fp->f_st[1].fr_skip);
7037c478bd9Sstevel@tonic-gate 	PRINTF("fragment state(in):\tkept %lu\tlost %lu\n",
7047c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_nfr, fp->f_st[0].fr_bnfr);
7057c478bd9Sstevel@tonic-gate 	PRINTF("fragment state(out):\tkept %lu\tlost %lu\n",
7067c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_nfr, fp->f_st[1].fr_bnfr);
7077c478bd9Sstevel@tonic-gate 	PRINTF("packet state(in):\tkept %lu\tlost %lu\n",
7087c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ads, fp->f_st[0].fr_bads);
7097c478bd9Sstevel@tonic-gate 	PRINTF("packet state(out):\tkept %lu\tlost %lu\n",
7107c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_ads, fp->f_st[1].fr_bads);
7117c478bd9Sstevel@tonic-gate 	PRINTF("ICMP replies:\t%lu\tTCP RSTs sent:\t%lu\n",
7127c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ret, fp->f_st[1].fr_ret);
7137c478bd9Sstevel@tonic-gate 	PRINTF("Invalid source(in):\t%lu\n", fp->f_st[0].fr_badsrc);
7147c478bd9Sstevel@tonic-gate 	PRINTF("Result cache hits(in):\t%lu\t(out):\t%lu\n",
7157c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_chit, fp->f_st[1].fr_chit);
7167c478bd9Sstevel@tonic-gate 	PRINTF("IN Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7177c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pull[0], fp->f_st[0].fr_pull[1]);
7187c478bd9Sstevel@tonic-gate 	PRINTF("OUT Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7197c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_pull[0], fp->f_st[1].fr_pull[1]);
7207c478bd9Sstevel@tonic-gate 	PRINTF("Fastroute successes:\t%lu\tfailures:\t%lu\n",
7217c478bd9Sstevel@tonic-gate 			fp->f_froute[0], fp->f_froute[1]);
7227c478bd9Sstevel@tonic-gate 	PRINTF("TCP cksum fails(in):\t%lu\t(out):\t%lu\n",
7237c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_tcpbad, fp->f_st[1].fr_tcpbad);
7247c478bd9Sstevel@tonic-gate 	PRINTF("IPF Ticks:\t%lu\n", fp->f_ticks);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	PRINTF("Packet log flags set: (%#x)\n", frf);
7277c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGPASS)
7287c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets passed through filter\n");
7297c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGBLOCK)
7307c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets blocked by filter\n");
7317c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGNOMATCH)
7327c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets not matched by filter\n");
7337c478bd9Sstevel@tonic-gate 	if (!frf)
7347c478bd9Sstevel@tonic-gate 		PRINTF("\tnone\n");
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate  * Print out a list of rules from the kernel, starting at the one passed.
7407c478bd9Sstevel@tonic-gate  */
7417c478bd9Sstevel@tonic-gate static void printlist(fp, comment)
7427c478bd9Sstevel@tonic-gate frentry_t *fp;
7437c478bd9Sstevel@tonic-gate char *comment;
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	struct	frentry	fb, *fg;
7467c478bd9Sstevel@tonic-gate 	char	*data;
7477c478bd9Sstevel@tonic-gate 	u_32_t	type;
7487c478bd9Sstevel@tonic-gate 	int	n;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	for (n = 1; fp; n++) {
7517c478bd9Sstevel@tonic-gate 		if (kmemcpy((char *)&fb, (u_long)fp, sizeof(fb)) == -1) {
7527c478bd9Sstevel@tonic-gate 			perror("kmemcpy");
7537c478bd9Sstevel@tonic-gate 			return;
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		fp = &fb;
7567c478bd9Sstevel@tonic-gate 		if (opts & (OPT_HITS|OPT_VERBOSE))
7577c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
7587c478bd9Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_hits);
7597c478bd9Sstevel@tonic-gate #else
7607c478bd9Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_hits);
7617c478bd9Sstevel@tonic-gate #endif
7627c478bd9Sstevel@tonic-gate 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
7637c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
7647c478bd9Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_bytes);
7657c478bd9Sstevel@tonic-gate #else
7667c478bd9Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_bytes);
7677c478bd9Sstevel@tonic-gate #endif
7687c478bd9Sstevel@tonic-gate 		if (opts & OPT_SHOWLINENO)
7697c478bd9Sstevel@tonic-gate 			PRINTF("@%d ", n);
7707c478bd9Sstevel@tonic-gate 		data = NULL;
7717c478bd9Sstevel@tonic-gate 		type = fp->fr_type & ~FR_T_BUILTIN;
7727c478bd9Sstevel@tonic-gate 		if (type == FR_T_IPF || type == FR_T_BPFOPC) {
7737c478bd9Sstevel@tonic-gate 			if (fp->fr_dsize) {
7747c478bd9Sstevel@tonic-gate 				data = malloc(fp->fr_dsize);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 				if (kmemcpy(data, (u_long)fp->fr_data,
7777c478bd9Sstevel@tonic-gate 					    fp->fr_dsize) == -1) {
7787c478bd9Sstevel@tonic-gate 					perror("kmemcpy");
7797c478bd9Sstevel@tonic-gate 					return;
7807c478bd9Sstevel@tonic-gate 				}
7817c478bd9Sstevel@tonic-gate 				fp->fr_data = data;
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		printfr(fp, ioctl);
7867c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE) {
7877c478bd9Sstevel@tonic-gate 			binprint(fp, sizeof(*fp));
7887c478bd9Sstevel@tonic-gate 			if (fp->fr_data != NULL && fp->fr_dsize > 0)
7897c478bd9Sstevel@tonic-gate 				binprint(fp->fr_data, fp->fr_dsize);
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 		if (data != NULL)
7927c478bd9Sstevel@tonic-gate 			free(data);
7937c478bd9Sstevel@tonic-gate 		if (fp->fr_grp != NULL) {
7947c478bd9Sstevel@tonic-gate 			if (!kmemcpy((char *)&fg, (u_long)fp->fr_grp,
7957c478bd9Sstevel@tonic-gate 				     sizeof(fg)))
7967c478bd9Sstevel@tonic-gate 				printlist(fg, comment);
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 		if (type == FR_T_CALLFUNC) {
7997c478bd9Sstevel@tonic-gate 			printlist(fp->fr_data, "# callfunc: ");
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 		fp = fp->fr_next;
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * print out all of the asked for rule sets, using the stats struct as
8077c478bd9Sstevel@tonic-gate  * the base from which to get the pointers.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate static	void	showlist(fiop)
8107c478bd9Sstevel@tonic-gate struct	friostat	*fiop;
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	struct	frentry	*fp = NULL;
8137c478bd9Sstevel@tonic-gate 	int	i, set;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	set = fiop->f_active;
8167c478bd9Sstevel@tonic-gate 	if (opts & OPT_INACTIVE)
8177c478bd9Sstevel@tonic-gate 		set = 1 - set;
8187c478bd9Sstevel@tonic-gate 	if (opts & OPT_ACCNT) {
8197c478bd9Sstevel@tonic-gate #ifdef USE_INET6
8207c478bd9Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
8217c478bd9Sstevel@tonic-gate 			i = F_ACOUT;
8227c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout6[set];
8237c478bd9Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
8247c478bd9Sstevel@tonic-gate 			i = F_ACIN;
8257c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin6[set];
8267c478bd9Sstevel@tonic-gate 		} else
8277c478bd9Sstevel@tonic-gate #endif
8287c478bd9Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
8297c478bd9Sstevel@tonic-gate 			i = F_ACOUT;
8307c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout[set];
8317c478bd9Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
8327c478bd9Sstevel@tonic-gate 			i = F_ACIN;
8337c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin[set];
8347c478bd9Sstevel@tonic-gate 		} else {
8357c478bd9Sstevel@tonic-gate 			FPRINTF(stderr, "No -i or -o given with -a\n");
8367c478bd9Sstevel@tonic-gate 			return;
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 	} else {
8397c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
8407c478bd9Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
8417c478bd9Sstevel@tonic-gate 			i = F_OUT;
8427c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout6[set];
8437c478bd9Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
8447c478bd9Sstevel@tonic-gate 			i = F_IN;
8457c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin6[set];
8467c478bd9Sstevel@tonic-gate 		} else
8477c478bd9Sstevel@tonic-gate #endif
8487c478bd9Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
8497c478bd9Sstevel@tonic-gate 			i = F_OUT;
8507c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout[set];
8517c478bd9Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
8527c478bd9Sstevel@tonic-gate 			i = F_IN;
8537c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin[set];
8547c478bd9Sstevel@tonic-gate 		} else
8557c478bd9Sstevel@tonic-gate 			return;
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
8587c478bd9Sstevel@tonic-gate 		FPRINTF(stderr, "showlist:opts %#x i %d\n", opts, i);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
8617c478bd9Sstevel@tonic-gate 		PRINTF("fp %p set %d\n", fp, set);
8627c478bd9Sstevel@tonic-gate 	if (!fp) {
8637c478bd9Sstevel@tonic-gate 		FPRINTF(stderr, "empty list for %s%s\n",
8647c478bd9Sstevel@tonic-gate 			(opts & OPT_INACTIVE) ? "inactive " : "", filters[i]);
8657c478bd9Sstevel@tonic-gate 		return;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	printlist(fp, NULL);
8687c478bd9Sstevel@tonic-gate }
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate /*
8727c478bd9Sstevel@tonic-gate  * Display ipfilter stateful filtering information
8737c478bd9Sstevel@tonic-gate  */
8747c478bd9Sstevel@tonic-gate static void showipstates(ipsp)
8757c478bd9Sstevel@tonic-gate ips_stat_t *ipsp;
8767c478bd9Sstevel@tonic-gate {
8777c478bd9Sstevel@tonic-gate 	u_long minlen, maxlen, totallen, *buckets;
8787c478bd9Sstevel@tonic-gate 	int i, sz;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	sz = sizeof(*buckets) * ipsp->iss_statesize;
8817c478bd9Sstevel@tonic-gate 	buckets = (u_long *)malloc(sz);
8827c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)buckets, (u_long)ipsp->iss_bucketlen, sz)) {
8837c478bd9Sstevel@tonic-gate 		free(buckets);
8847c478bd9Sstevel@tonic-gate 		return;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	/*
8887c478bd9Sstevel@tonic-gate 	 * If a list of states hasn't been asked for, only print out stats
8897c478bd9Sstevel@tonic-gate 	 */
8907c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_SHOWLIST)) {
8917c478bd9Sstevel@tonic-gate 		PRINTF("IP states added:\n\t%lu TCP\n\t%lu UDP\n\t%lu ICMP\n",
8927c478bd9Sstevel@tonic-gate 			ipsp->iss_tcp, ipsp->iss_udp, ipsp->iss_icmp);
8937c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu hits\n\t%lu misses\n", ipsp->iss_hits,
8947c478bd9Sstevel@tonic-gate 			ipsp->iss_miss);
8957c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu maximum\n\t%lu no memory\n\t%lu max bucket\n",
8967c478bd9Sstevel@tonic-gate 			ipsp->iss_max, ipsp->iss_nomem, ipsp->iss_bucketfull);
8977c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu active\n\t%lu expired\n\t%lu closed\n",
8987c478bd9Sstevel@tonic-gate 			ipsp->iss_active, ipsp->iss_expire, ipsp->iss_fin);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 		PRINTF("State logging %sabled\n",
9017c478bd9Sstevel@tonic-gate 			state_logging ? "en" : "dis");
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 		PRINTF("\nState table bucket statistics:\n");
9047c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu in use\t\n", ipsp->iss_inuse);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		minlen = ipsp->iss_max;
9077c478bd9Sstevel@tonic-gate 		totallen = 0;
9087c478bd9Sstevel@tonic-gate 		maxlen = 0;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		for (i = 0; i < ipsp->iss_statesize; i++) {
9117c478bd9Sstevel@tonic-gate 			if (buckets[i] > maxlen)
9127c478bd9Sstevel@tonic-gate 				maxlen = buckets[i];
9137c478bd9Sstevel@tonic-gate 			if (buckets[i] < minlen)
9147c478bd9Sstevel@tonic-gate 					minlen = buckets[i];
9157c478bd9Sstevel@tonic-gate 			totallen += buckets[i];
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		PRINTF("\t%2.2f%% bucket usage\n\t%lu minimal length\n",
9197c478bd9Sstevel@tonic-gate 			((float)ipsp->iss_inuse / ipsp->iss_statesize) * 100.0,
9207c478bd9Sstevel@tonic-gate 			minlen);
9217c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu maximal length\n\t%.3f average length\n",
9227c478bd9Sstevel@tonic-gate 			maxlen,
9237c478bd9Sstevel@tonic-gate 			ipsp->iss_inuse ? (float) totallen/ ipsp->iss_inuse :
9247c478bd9Sstevel@tonic-gate 					  0.0);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate #define ENTRIES_PER_LINE 5
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE) {
9297c478bd9Sstevel@tonic-gate 			PRINTF("\nCurrent bucket sizes :\n");
9307c478bd9Sstevel@tonic-gate 			for (i = 0; i < ipsp->iss_statesize; i++) {
9317c478bd9Sstevel@tonic-gate 				if ((i % ENTRIES_PER_LINE) == 0)
9327c478bd9Sstevel@tonic-gate 					PRINTF("\t");
9337c478bd9Sstevel@tonic-gate 				PRINTF("%4d -> %4lu", i, buckets[i]);
9347c478bd9Sstevel@tonic-gate 				if ((i % ENTRIES_PER_LINE) ==
9357c478bd9Sstevel@tonic-gate 				    (ENTRIES_PER_LINE - 1))
9367c478bd9Sstevel@tonic-gate 					PRINTF("\n");
9377c478bd9Sstevel@tonic-gate 				else
9387c478bd9Sstevel@tonic-gate 					PRINTF("  ");
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 			PRINTF("\n");
9417c478bd9Sstevel@tonic-gate 		}
9427c478bd9Sstevel@tonic-gate 		PRINTF("\n");
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		free(buckets);
9457c478bd9Sstevel@tonic-gate 		return;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	/*
9497c478bd9Sstevel@tonic-gate 	 * Print out all the state information currently held in the kernel.
9507c478bd9Sstevel@tonic-gate 	 */
9517c478bd9Sstevel@tonic-gate 	while (ipsp->iss_list != NULL) {
9527c478bd9Sstevel@tonic-gate 		ipsp->iss_list = printstate(ipsp->iss_list, opts);
9537c478bd9Sstevel@tonic-gate 	}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	free(buckets);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate #ifdef STATETOP
9607c478bd9Sstevel@tonic-gate static void topipstates(saddr, daddr, sport, dport, protocol,
9617c478bd9Sstevel@tonic-gate 		        refreshtime, topclosed)
9627c478bd9Sstevel@tonic-gate struct in_addr saddr;
9637c478bd9Sstevel@tonic-gate struct in_addr daddr;
9647c478bd9Sstevel@tonic-gate int sport;
9657c478bd9Sstevel@tonic-gate int dport;
9667c478bd9Sstevel@tonic-gate int protocol;
9677c478bd9Sstevel@tonic-gate int refreshtime;
9687c478bd9Sstevel@tonic-gate int topclosed;
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	char str1[STSTRSIZE], str2[STSTRSIZE], str3[STSTRSIZE], str4[STSTRSIZE];
9717c478bd9Sstevel@tonic-gate 	int maxtsentries = 0, reverse = 0, sorting = STSORT_DEFAULT;
9727c478bd9Sstevel@tonic-gate 	int i, j, winx, tsentry, maxx, maxy, redraw = 0;
9737c478bd9Sstevel@tonic-gate 	ips_stat_t ipsst, *ipsstp = &ipsst;
9747c478bd9Sstevel@tonic-gate 	statetop_t *tstable = NULL, *tp;
9757c478bd9Sstevel@tonic-gate 	ipstate_t ips;
9767c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
9777c478bd9Sstevel@tonic-gate 	struct timeval selecttimeout;
9787c478bd9Sstevel@tonic-gate 	char hostnm[HOSTNMLEN];
9797c478bd9Sstevel@tonic-gate 	struct protoent *proto;
9807c478bd9Sstevel@tonic-gate 	fd_set readfd;
9817c478bd9Sstevel@tonic-gate 	int c = 0;
9827c478bd9Sstevel@tonic-gate 	time_t t;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/* init ncurses stuff */
9857c478bd9Sstevel@tonic-gate   	initscr();
9867c478bd9Sstevel@tonic-gate   	cbreak();
9877c478bd9Sstevel@tonic-gate   	noecho();
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	/* init hostname */
9907c478bd9Sstevel@tonic-gate 	gethostname(hostnm, sizeof(hostnm) - 1);
9917c478bd9Sstevel@tonic-gate 	hostnm[sizeof(hostnm) - 1] = '\0';
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/* init ipfobj_t stuff */
9947c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&ipfo, sizeof(ipfo));
9957c478bd9Sstevel@tonic-gate 	ipfo.ipfo_rev = IPFILTER_VERSION;
9967c478bd9Sstevel@tonic-gate 	ipfo.ipfo_size = sizeof(*ipsstp);
9977c478bd9Sstevel@tonic-gate 	ipfo.ipfo_ptr = (void *)ipsstp;
9987c478bd9Sstevel@tonic-gate 	ipfo.ipfo_type = IPFOBJ_STATESTAT;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	/* repeat until user aborts */
10017c478bd9Sstevel@tonic-gate 	while ( 1 ) {
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		/* get state table */
10047c478bd9Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
10057c478bd9Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
10067c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGETFS)");
10077c478bd9Sstevel@tonic-gate 			exit(-1);
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		/* clear the history */
10117c478bd9Sstevel@tonic-gate 		tsentry = -1;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		/* read the state table and store in tstable */
10147c478bd9Sstevel@tonic-gate 		while (ipsstp->iss_list) {
10157c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ips, (u_long)ipsstp->iss_list,
10167c478bd9Sstevel@tonic-gate 				    sizeof(ips)))
10177c478bd9Sstevel@tonic-gate 				break;
10187c478bd9Sstevel@tonic-gate 			ipsstp->iss_list = ips.is_next;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 			if (((saddr.s_addr == INADDR_ANY) ||
10217c478bd9Sstevel@tonic-gate 			     (saddr.s_addr == ips.is_saddr)) &&
10227c478bd9Sstevel@tonic-gate 			    ((daddr.s_addr == INADDR_ANY) ||
10237c478bd9Sstevel@tonic-gate 			     (daddr.s_addr == ips.is_daddr)) &&
10247c478bd9Sstevel@tonic-gate 			    ((protocol < 0) || (protocol == ips.is_p)) &&
10257c478bd9Sstevel@tonic-gate 			    (((ips.is_p != IPPROTO_TCP) &&
10267c478bd9Sstevel@tonic-gate 			     (ips.is_p != IPPROTO_UDP)) ||
10277c478bd9Sstevel@tonic-gate 			     (((sport < 0) ||
10287c478bd9Sstevel@tonic-gate 			       (htons(sport) == ips.is_sport)) &&
10297c478bd9Sstevel@tonic-gate 			      ((dport < 0) ||
10307c478bd9Sstevel@tonic-gate 			       (htons(dport) == ips.is_dport)))) &&
10317c478bd9Sstevel@tonic-gate 			     (topclosed || (ips.is_p != IPPROTO_TCP) ||
10327c478bd9Sstevel@tonic-gate 			     (ips.is_state[0] < TCPS_LAST_ACK) ||
10337c478bd9Sstevel@tonic-gate 			     (ips.is_state[1] < TCPS_LAST_ACK))) {
10347c478bd9Sstevel@tonic-gate 				/*
10357c478bd9Sstevel@tonic-gate 				 * if necessary make room for this state
10367c478bd9Sstevel@tonic-gate 				 * entry
10377c478bd9Sstevel@tonic-gate 				 */
10387c478bd9Sstevel@tonic-gate 				tsentry++;
10397c478bd9Sstevel@tonic-gate 				if (!maxtsentries ||
10407c478bd9Sstevel@tonic-gate 				    (tsentry == maxtsentries)) {
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 					maxtsentries += STGROWSIZE;
10437c478bd9Sstevel@tonic-gate 					tstable = realloc(tstable, maxtsentries * sizeof(statetop_t));
10447c478bd9Sstevel@tonic-gate 					if (!tstable) {
10457c478bd9Sstevel@tonic-gate 						perror("malloc");
10467c478bd9Sstevel@tonic-gate 						exit(-1);
10477c478bd9Sstevel@tonic-gate 					}
10487c478bd9Sstevel@tonic-gate 				}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 				/* fill structure */
10517c478bd9Sstevel@tonic-gate 				tp = tstable + tsentry;
10527c478bd9Sstevel@tonic-gate 				tp->st_src = ips.is_src;
10537c478bd9Sstevel@tonic-gate 				tp->st_dst = ips.is_dst;
10547c478bd9Sstevel@tonic-gate 				tp->st_p = ips.is_p;
10557c478bd9Sstevel@tonic-gate 				tp->st_state[0] = ips.is_state[0];
10567c478bd9Sstevel@tonic-gate 				tp->st_state[1] = ips.is_state[1];
10577c478bd9Sstevel@tonic-gate 				tp->st_pkts = ips.is_pkts[0] + ips.is_pkts[1];
10587c478bd9Sstevel@tonic-gate 				tp->st_bytes = ips.is_bytes[0] +
10597c478bd9Sstevel@tonic-gate 					       ips.is_bytes[1];
1060*6aed92a9Syx160601 				tp->st_age = ips.is_die - ipsstp->iss_ticks;
10617c478bd9Sstevel@tonic-gate 				if ((ips.is_p == IPPROTO_TCP) ||
10627c478bd9Sstevel@tonic-gate 				    (ips.is_p == IPPROTO_UDP)) {
10637c478bd9Sstevel@tonic-gate 					tp->st_sport = ips.is_sport;
10647c478bd9Sstevel@tonic-gate 					tp->st_dport = ips.is_dport;
10657c478bd9Sstevel@tonic-gate 				}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 			}
10687c478bd9Sstevel@tonic-gate 		}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 		/* sort the array */
10727c478bd9Sstevel@tonic-gate 		if (tsentry != -1)
10737c478bd9Sstevel@tonic-gate 			switch (sorting)
10747c478bd9Sstevel@tonic-gate 			{
10757c478bd9Sstevel@tonic-gate 			case STSORT_PR:
10767c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
10777c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_p);
10787c478bd9Sstevel@tonic-gate 				break;
10797c478bd9Sstevel@tonic-gate 			case STSORT_PKTS:
10807c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
10817c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_pkts);
10827c478bd9Sstevel@tonic-gate 				break;
10837c478bd9Sstevel@tonic-gate 			case STSORT_BYTES:
10847c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
10857c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_bytes);
10867c478bd9Sstevel@tonic-gate 				break;
10877c478bd9Sstevel@tonic-gate 			case STSORT_TTL:
10887c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
10897c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_ttl);
10907c478bd9Sstevel@tonic-gate 				break;
10917c478bd9Sstevel@tonic-gate 			case STSORT_SRCIP:
10927c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
10937c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_srcip);
10947c478bd9Sstevel@tonic-gate 				break;
10957c478bd9Sstevel@tonic-gate 			case STSORT_DSTIP:
10967c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
10977c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_dstip);
10987c478bd9Sstevel@tonic-gate 				break;
10997c478bd9Sstevel@tonic-gate 			default:
11007c478bd9Sstevel@tonic-gate 				break;
11017c478bd9Sstevel@tonic-gate 			}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 		/* print title */
11047c478bd9Sstevel@tonic-gate 		erase();
11057c478bd9Sstevel@tonic-gate 		getmaxyx(stdscr, maxy, maxx);
11067c478bd9Sstevel@tonic-gate 		attron(A_BOLD);
11077c478bd9Sstevel@tonic-gate 		winx = 0;
11087c478bd9Sstevel@tonic-gate 		move(winx,0);
11097c478bd9Sstevel@tonic-gate 		sprintf(str1, "%s - %s - state top", hostnm, IPL_VERSION);
11107c478bd9Sstevel@tonic-gate 		for (j = 0 ; j < (maxx - 8 - strlen(str1)) / 2; j++)
11117c478bd9Sstevel@tonic-gate 			printw(" ");
11127c478bd9Sstevel@tonic-gate 		printw("%s", str1);
11137c478bd9Sstevel@tonic-gate 		attroff(A_BOLD);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		/* just for fun add a clock */
11167c478bd9Sstevel@tonic-gate 		move(winx, maxx - 8);
11177c478bd9Sstevel@tonic-gate 		t = time(NULL);
11187c478bd9Sstevel@tonic-gate 		strftime(str1, 80, "%T", localtime(&t));
11197c478bd9Sstevel@tonic-gate 		printw("%s\n", str1);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		/*
11227c478bd9Sstevel@tonic-gate 		 * print the display filters, this is placed in the loop,
11237c478bd9Sstevel@tonic-gate 		 * because someday I might add code for changing these
11247c478bd9Sstevel@tonic-gate 		 * while the programming is running :-)
11257c478bd9Sstevel@tonic-gate 		 */
11267c478bd9Sstevel@tonic-gate 		if (sport >= 0)
11277c478bd9Sstevel@tonic-gate 			sprintf(str1, "%s,%d", inet_ntoa(saddr), sport);
11287c478bd9Sstevel@tonic-gate 		else
11297c478bd9Sstevel@tonic-gate 			sprintf(str1, "%s", inet_ntoa(saddr));
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 		if (dport >= 0)
11327c478bd9Sstevel@tonic-gate 			sprintf(str2, "%s,%d", inet_ntoa(daddr), dport);
11337c478bd9Sstevel@tonic-gate 		else
11347c478bd9Sstevel@tonic-gate 			sprintf(str2, "%s", inet_ntoa(daddr));
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 		if (protocol < 0)
11377c478bd9Sstevel@tonic-gate 			strcpy(str3, "any");
11387c478bd9Sstevel@tonic-gate 		else if ((proto = getprotobynumber(protocol)) != NULL)
11397c478bd9Sstevel@tonic-gate 			sprintf(str3, "%s", proto->p_name);
11407c478bd9Sstevel@tonic-gate 		else
11417c478bd9Sstevel@tonic-gate 			sprintf(str3, "%d", protocol);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		switch (sorting)
11447c478bd9Sstevel@tonic-gate 		{
11457c478bd9Sstevel@tonic-gate 		case STSORT_PR:
11467c478bd9Sstevel@tonic-gate 			sprintf(str4, "proto");
11477c478bd9Sstevel@tonic-gate 			break;
11487c478bd9Sstevel@tonic-gate 		case STSORT_PKTS:
11497c478bd9Sstevel@tonic-gate 			sprintf(str4, "# pkts");
11507c478bd9Sstevel@tonic-gate 			break;
11517c478bd9Sstevel@tonic-gate 		case STSORT_BYTES:
11527c478bd9Sstevel@tonic-gate 			sprintf(str4, "# bytes");
11537c478bd9Sstevel@tonic-gate 			break;
11547c478bd9Sstevel@tonic-gate 		case STSORT_TTL:
11557c478bd9Sstevel@tonic-gate 			sprintf(str4, "ttl");
11567c478bd9Sstevel@tonic-gate 			break;
11577c478bd9Sstevel@tonic-gate 		case STSORT_SRCIP:
11587c478bd9Sstevel@tonic-gate 			sprintf(str4, "srcip");
11597c478bd9Sstevel@tonic-gate 			break;
11607c478bd9Sstevel@tonic-gate 		case STSORT_DSTIP:
11617c478bd9Sstevel@tonic-gate 			sprintf(str4, "dstip");
11627c478bd9Sstevel@tonic-gate 			break;
11637c478bd9Sstevel@tonic-gate 		default:
11647c478bd9Sstevel@tonic-gate 			sprintf(str4, "unknown");
11657c478bd9Sstevel@tonic-gate 			break;
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		if (reverse)
11697c478bd9Sstevel@tonic-gate 			strcat(str4, " (reverse)");
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 		winx += 2;
11727c478bd9Sstevel@tonic-gate 		move(winx,0);
11737c478bd9Sstevel@tonic-gate 		printw("Src = %s  Dest = %s  Proto = %s  Sorted by = %s\n\n",
11747c478bd9Sstevel@tonic-gate 		       str1, str2, str3, str4);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		/* print column description */
11777c478bd9Sstevel@tonic-gate 		winx += 2;
11787c478bd9Sstevel@tonic-gate 		move(winx,0);
11797c478bd9Sstevel@tonic-gate 		attron(A_BOLD);
11807c478bd9Sstevel@tonic-gate 		printw("%-21s %-21s %3s %4s %7s %9s %9s\n", "Source IP",
11817c478bd9Sstevel@tonic-gate 		       "Destination IP", "ST", "PR", "#pkts", "#bytes", "ttl");
11827c478bd9Sstevel@tonic-gate 		attroff(A_BOLD);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		/* print all the entries */
11857c478bd9Sstevel@tonic-gate 		tp = tstable;
11867c478bd9Sstevel@tonic-gate 		if (reverse)
11877c478bd9Sstevel@tonic-gate 			tp += tsentry;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 		if (tsentry > maxy - 6)
11907c478bd9Sstevel@tonic-gate 			tsentry = maxy - 6;
11917c478bd9Sstevel@tonic-gate 		for (i = 0; i <= tsentry; i++) {
11927c478bd9Sstevel@tonic-gate 			/* print src/dest and port */
11937c478bd9Sstevel@tonic-gate 			if ((tp->st_p == IPPROTO_TCP) ||
11947c478bd9Sstevel@tonic-gate 			    (tp->st_p == IPPROTO_UDP)) {
11957c478bd9Sstevel@tonic-gate 				sprintf(str1, "%s,%hu",
11967c478bd9Sstevel@tonic-gate 					inet_ntoa(tp->st_src.in4),
11977c478bd9Sstevel@tonic-gate 					ntohs(tp->st_sport));
11987c478bd9Sstevel@tonic-gate 				sprintf(str2, "%s,%hu",
11997c478bd9Sstevel@tonic-gate 					inet_ntoa(tp->st_dst.in4),
12007c478bd9Sstevel@tonic-gate 					ntohs(tp->st_dport));
12017c478bd9Sstevel@tonic-gate 			} else {
12027c478bd9Sstevel@tonic-gate 				sprintf(str1, "%s", inet_ntoa(tp->st_src.in4));
12037c478bd9Sstevel@tonic-gate 				sprintf(str2, "%s", inet_ntoa(tp->st_dst.in4));
12047c478bd9Sstevel@tonic-gate 			}
12057c478bd9Sstevel@tonic-gate 			winx++;
12067c478bd9Sstevel@tonic-gate 			move(winx, 0);
12077c478bd9Sstevel@tonic-gate 			printw("%-21s %-21s", str1, str2);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			/* print state */
12107c478bd9Sstevel@tonic-gate 			sprintf(str1, "%X/%X", tp->st_state[0],
12117c478bd9Sstevel@tonic-gate 				tp->st_state[1]);
12127c478bd9Sstevel@tonic-gate 			printw(" %3s", str1);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 			/* print proto */
12157c478bd9Sstevel@tonic-gate 			proto = getprotobynumber(tp->st_p);
12167c478bd9Sstevel@tonic-gate 			if (proto) {
12177c478bd9Sstevel@tonic-gate 				strncpy(str1, proto->p_name, 4);
12187c478bd9Sstevel@tonic-gate 				str1[4] = '\0';
12197c478bd9Sstevel@tonic-gate 			} else {
12207c478bd9Sstevel@tonic-gate 				sprintf(str1, "%d", tp->st_p);
12217c478bd9Sstevel@tonic-gate 			}
12227c478bd9Sstevel@tonic-gate 			printw(" %4s", str1);
12237c478bd9Sstevel@tonic-gate 				/* print #pkt/#bytes */
12247c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
12257c478bd9Sstevel@tonic-gate 			printw(" %7qu %9qu", (unsigned long long) tp->st_pkts,
12267c478bd9Sstevel@tonic-gate 				(unsigned long long) tp->st_bytes);
12277c478bd9Sstevel@tonic-gate #else
12287c478bd9Sstevel@tonic-gate 			printw(" %7lu %9lu", tp->st_pkts, tp->st_bytes);
12297c478bd9Sstevel@tonic-gate #endif
12307c478bd9Sstevel@tonic-gate 			printw(" %9s", ttl_to_string(tp->st_age));
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			if (reverse)
12337c478bd9Sstevel@tonic-gate 				tp--;
12347c478bd9Sstevel@tonic-gate 			else
12357c478bd9Sstevel@tonic-gate 				tp++;
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		/* screen data structure is filled, now update the screen */
12397c478bd9Sstevel@tonic-gate 		if (redraw)
12407c478bd9Sstevel@tonic-gate 			clearok(stdscr,1);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		refresh();
12437c478bd9Sstevel@tonic-gate 		if (redraw) {
12447c478bd9Sstevel@tonic-gate 			clearok(stdscr,0);
12457c478bd9Sstevel@tonic-gate 			redraw = 0;
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		/* wait for key press or a 1 second time out period */
12497c478bd9Sstevel@tonic-gate 		selecttimeout.tv_sec = refreshtime;
12507c478bd9Sstevel@tonic-gate 		selecttimeout.tv_usec = 0;
12517c478bd9Sstevel@tonic-gate 		FD_ZERO(&readfd);
12527c478bd9Sstevel@tonic-gate 		FD_SET(0, &readfd);
12537c478bd9Sstevel@tonic-gate 		select(1, &readfd, NULL, NULL, &selecttimeout);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 		/* if key pressed, read all waiting keys */
12567c478bd9Sstevel@tonic-gate 		if (FD_ISSET(0, &readfd)) {
12577c478bd9Sstevel@tonic-gate 			c = wgetch(stdscr);
12587c478bd9Sstevel@tonic-gate 			if (c == ERR)
12597c478bd9Sstevel@tonic-gate 				continue;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 			if (tolower(c) == 'l') {
12627c478bd9Sstevel@tonic-gate 				redraw = 1;
12637c478bd9Sstevel@tonic-gate 			} else if (tolower(c) == 'q') {
12647c478bd9Sstevel@tonic-gate 				nocbreak();
12657c478bd9Sstevel@tonic-gate 				endwin();
12667c478bd9Sstevel@tonic-gate 				exit(0);
12677c478bd9Sstevel@tonic-gate 			} else if (tolower(c) == 'r') {
12687c478bd9Sstevel@tonic-gate 				reverse = !reverse;
12697c478bd9Sstevel@tonic-gate 			} else if (tolower(c) == 's') {
12707c478bd9Sstevel@tonic-gate 				sorting++;
12717c478bd9Sstevel@tonic-gate 				if (sorting > STSORT_MAX)
12727c478bd9Sstevel@tonic-gate 					sorting = 0;
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 		}
12757c478bd9Sstevel@tonic-gate 	} /* while */
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	printw("\n");
12787c478bd9Sstevel@tonic-gate 	nocbreak();
12797c478bd9Sstevel@tonic-gate 	endwin();
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	free(tstable);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate #endif
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate /*
12877c478bd9Sstevel@tonic-gate  * Show fragment cache information that's held in the kernel.
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate static void showfrstates(ifsp)
12907c478bd9Sstevel@tonic-gate ipfrstat_t *ifsp;
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	struct ipfr *ipfrtab[IPFT_SIZE], ifr;
12937c478bd9Sstevel@tonic-gate 	frentry_t fr;
12947c478bd9Sstevel@tonic-gate 	int i;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	/*
12977c478bd9Sstevel@tonic-gate 	 * print out the numeric statistics
12987c478bd9Sstevel@tonic-gate 	 */
12997c478bd9Sstevel@tonic-gate 	PRINTF("IP fragment states:\n\t%lu new\n\t%lu expired\n\t%lu hits\n",
13007c478bd9Sstevel@tonic-gate 		ifsp->ifs_new, ifsp->ifs_expire, ifsp->ifs_hits);
13017c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu retrans\n\t%lu too short\n", ifsp->ifs_retrans0, ifsp->ifs_short);
13027c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu no memory\n\t%lu already exist\n",
13037c478bd9Sstevel@tonic-gate 		ifsp->ifs_nomem, ifsp->ifs_exists);
13047c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu inuse\n", ifsp->ifs_inuse);
13057c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_table, sizeof(ipfrtab)))
13067c478bd9Sstevel@tonic-gate 		return;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * Print out the contents (if any) of the fragment cache table.
13107c478bd9Sstevel@tonic-gate 	 */
13117c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
13127c478bd9Sstevel@tonic-gate 		while (ipfrtab[i]) {
13137c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
13147c478bd9Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
13157c478bd9Sstevel@tonic-gate 				break;
13167c478bd9Sstevel@tonic-gate 			PRINTF("%s -> ", hostname(4, &ifr.ipfr_src));
13177c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&fr, (u_long)ifr.ipfr_rule,
13187c478bd9Sstevel@tonic-gate 				    sizeof(fr)) == -1)
13197c478bd9Sstevel@tonic-gate 				break;
13207c478bd9Sstevel@tonic-gate 			PRINTF("%s %d %d %d %#02x = %#x\n",
13217c478bd9Sstevel@tonic-gate 				hostname(4, &ifr.ipfr_dst), ifr.ipfr_id,
13227c478bd9Sstevel@tonic-gate 				ifr.ipfr_ttl, ifr.ipfr_p, ifr.ipfr_tos,
13237c478bd9Sstevel@tonic-gate 				fr.fr_flags);
13247c478bd9Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_nattab,sizeof(ipfrtab)))
13277c478bd9Sstevel@tonic-gate 		return;
13287c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
13297c478bd9Sstevel@tonic-gate 		while (ipfrtab[i]) {
13307c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
13317c478bd9Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
13327c478bd9Sstevel@tonic-gate 				break;
13337c478bd9Sstevel@tonic-gate 			PRINTF("NAT: %s -> ", hostname(4, &ifr.ipfr_src));
13347c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&fr, (u_long)ifr.ipfr_rule,
13357c478bd9Sstevel@tonic-gate 				    sizeof(fr)) == -1)
13367c478bd9Sstevel@tonic-gate 				break;
13377c478bd9Sstevel@tonic-gate 			PRINTF("%s %d %d %d %#02x = %#x\n",
13387c478bd9Sstevel@tonic-gate 				hostname(4, &ifr.ipfr_dst), ifr.ipfr_id,
13397c478bd9Sstevel@tonic-gate 				ifr.ipfr_ttl, ifr.ipfr_p, ifr.ipfr_tos,
13407c478bd9Sstevel@tonic-gate 				fr.fr_flags);
13417c478bd9Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /*
13477c478bd9Sstevel@tonic-gate  * Show stats on how auth within IPFilter has been used
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate static void showauthstates(asp)
13507c478bd9Sstevel@tonic-gate fr_authstat_t *asp;
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	frauthent_t *frap, fra;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
13557c478bd9Sstevel@tonic-gate 	printf("Authorisation hits: %qu\tmisses %qu\n",
13567c478bd9Sstevel@tonic-gate 		(unsigned long long) asp->fas_hits,
13577c478bd9Sstevel@tonic-gate 		(unsigned long long) asp->fas_miss);
13587c478bd9Sstevel@tonic-gate #else
13597c478bd9Sstevel@tonic-gate 	printf("Authorisation hits: %ld\tmisses %ld\n", asp->fas_hits,
13607c478bd9Sstevel@tonic-gate 		asp->fas_miss);
13617c478bd9Sstevel@tonic-gate #endif
13627c478bd9Sstevel@tonic-gate 	printf("nospace %ld\nadded %ld\nsendfail %ld\nsendok %ld\n",
13637c478bd9Sstevel@tonic-gate 		asp->fas_nospace, asp->fas_added, asp->fas_sendfail,
13647c478bd9Sstevel@tonic-gate 		asp->fas_sendok);
13657c478bd9Sstevel@tonic-gate 	printf("queok %ld\nquefail %ld\nexpire %ld\n",
13667c478bd9Sstevel@tonic-gate 		asp->fas_queok, asp->fas_quefail, asp->fas_expire);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	frap = asp->fas_faelist;
13697c478bd9Sstevel@tonic-gate 	while (frap) {
13707c478bd9Sstevel@tonic-gate 		if (kmemcpy((char *)&fra, (u_long)frap, sizeof(fra)) == -1)
13717c478bd9Sstevel@tonic-gate 			break;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 		printf("age %ld\t", fra.fae_age);
13747c478bd9Sstevel@tonic-gate 		printfr(&fra.fae_fr, ioctl);
13757c478bd9Sstevel@tonic-gate 		frap = fra.fae_next;
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate /*
13817c478bd9Sstevel@tonic-gate  * Display groups used for each of filter rules, accounting rules and
13827c478bd9Sstevel@tonic-gate  * authentication, separately.
13837c478bd9Sstevel@tonic-gate  */
13847c478bd9Sstevel@tonic-gate static void showgroups(fiop)
13857c478bd9Sstevel@tonic-gate struct friostat	*fiop;
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	static char *gnames[3] = { "Filter", "Accounting", "Authentication" };
13887c478bd9Sstevel@tonic-gate 	frgroup_t *fp, grp;
13897c478bd9Sstevel@tonic-gate 	int on, off, i;
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	on = fiop->f_active;
13927c478bd9Sstevel@tonic-gate 	off = 1 - on;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
13957c478bd9Sstevel@tonic-gate 		printf("%s groups (active):\n", gnames[i]);
13967c478bd9Sstevel@tonic-gate 		for (fp = fiop->f_groups[i][on]; fp; fp = grp.fg_next)
13977c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
13987c478bd9Sstevel@tonic-gate 				break;
13997c478bd9Sstevel@tonic-gate 			else
14007c478bd9Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
14017c478bd9Sstevel@tonic-gate 		printf("%s groups (inactive):\n", gnames[i]);
14027c478bd9Sstevel@tonic-gate 		for (fp = fiop->f_groups[i][off]; fp; fp = grp.fg_next)
14037c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
14047c478bd9Sstevel@tonic-gate 				break;
14057c478bd9Sstevel@tonic-gate 			else
14067c478bd9Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate static void parse_ipportstr(argument, ip, port)
14117c478bd9Sstevel@tonic-gate const char *argument;
14127c478bd9Sstevel@tonic-gate struct in_addr *ip;
14137c478bd9Sstevel@tonic-gate int *port;
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	char *s, *comma;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	/* make working copy of argument, Theoretically you must be able
14197c478bd9Sstevel@tonic-gate 	 * to write to optarg, but that seems very ugly to me....
14207c478bd9Sstevel@tonic-gate 	 */
14217c478bd9Sstevel@tonic-gate 	s = strdup(argument);
14227c478bd9Sstevel@tonic-gate 	if (s == NULL)
14237c478bd9Sstevel@tonic-gate 		return;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	/* get port */
14267c478bd9Sstevel@tonic-gate 	if ((comma = strchr(s, ',')) != NULL) {
14277c478bd9Sstevel@tonic-gate 		if (!strcasecmp(comma + 1, "any")) {
14287c478bd9Sstevel@tonic-gate 			*port = -1;
14297c478bd9Sstevel@tonic-gate 		} else if (!sscanf(comma + 1, "%d", port) ||
14307c478bd9Sstevel@tonic-gate 			   (*port < 0) || (*port > 65535)) {
14317c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Invalid port specfication in %s\n",
14327c478bd9Sstevel@tonic-gate 				argument);
14337c478bd9Sstevel@tonic-gate 			free(s);
14347c478bd9Sstevel@tonic-gate 			exit(-2);
14357c478bd9Sstevel@tonic-gate 		}
14367c478bd9Sstevel@tonic-gate 		*comma = '\0';
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/* get ip address */
14417c478bd9Sstevel@tonic-gate 	if (!strcasecmp(s, "any")) {
14427c478bd9Sstevel@tonic-gate 		ip->s_addr = INADDR_ANY;
14437c478bd9Sstevel@tonic-gate 	} else	if (!inet_aton(s, ip)) {
14447c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Invalid IP address: %s\n", s);
14457c478bd9Sstevel@tonic-gate 		free(s);
14467c478bd9Sstevel@tonic-gate 		exit(-2);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	/* free allocated memory */
14507c478bd9Sstevel@tonic-gate 	free(s);
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate #ifdef STATETOP
14557c478bd9Sstevel@tonic-gate static char ttlbuf[STSTRSIZE];
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate static char *ttl_to_string(ttl)
14587c478bd9Sstevel@tonic-gate long int ttl;
14597c478bd9Sstevel@tonic-gate {
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	int hours, minutes, seconds;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	/* ttl is in half seconds */
14647c478bd9Sstevel@tonic-gate 	ttl /= 2;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	hours = ttl / 3600;
14677c478bd9Sstevel@tonic-gate 	ttl = ttl % 3600;
14687c478bd9Sstevel@tonic-gate 	minutes = ttl / 60;
14697c478bd9Sstevel@tonic-gate 	seconds = ttl % 60;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if (hours > 0 )
14727c478bd9Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d:%02d", hours, minutes, seconds);
14737c478bd9Sstevel@tonic-gate 	else
14747c478bd9Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d", minutes, seconds);
14757c478bd9Sstevel@tonic-gate 	return ttlbuf;
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate static int sort_pkts(a, b)
14807c478bd9Sstevel@tonic-gate const void *a;
14817c478bd9Sstevel@tonic-gate const void *b;
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
14857c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	if (ap->st_pkts == bp->st_pkts)
14887c478bd9Sstevel@tonic-gate 		return 0;
14897c478bd9Sstevel@tonic-gate 	else if (ap->st_pkts < bp->st_pkts)
14907c478bd9Sstevel@tonic-gate 		return 1;
14917c478bd9Sstevel@tonic-gate 	return -1;
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate static int sort_bytes(a, b)
14967c478bd9Sstevel@tonic-gate const void *a;
14977c478bd9Sstevel@tonic-gate const void *b;
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
15007c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	if (ap->st_bytes == bp->st_bytes)
15037c478bd9Sstevel@tonic-gate 		return 0;
15047c478bd9Sstevel@tonic-gate 	else if (ap->st_bytes < bp->st_bytes)
15057c478bd9Sstevel@tonic-gate 		return 1;
15067c478bd9Sstevel@tonic-gate 	return -1;
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate static int sort_p(a, b)
15117c478bd9Sstevel@tonic-gate const void *a;
15127c478bd9Sstevel@tonic-gate const void *b;
15137c478bd9Sstevel@tonic-gate {
15147c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
15157c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if (ap->st_p == bp->st_p)
15187c478bd9Sstevel@tonic-gate 		return 0;
15197c478bd9Sstevel@tonic-gate 	else if (ap->st_p < bp->st_p)
15207c478bd9Sstevel@tonic-gate 		return 1;
15217c478bd9Sstevel@tonic-gate 	return -1;
15227c478bd9Sstevel@tonic-gate }
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate static int sort_ttl(a, b)
15267c478bd9Sstevel@tonic-gate const void *a;
15277c478bd9Sstevel@tonic-gate const void *b;
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
15307c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	if (ap->st_age == bp->st_age)
15337c478bd9Sstevel@tonic-gate 		return 0;
15347c478bd9Sstevel@tonic-gate 	else if (ap->st_age < bp->st_age)
15357c478bd9Sstevel@tonic-gate 		return 1;
15367c478bd9Sstevel@tonic-gate 	return -1;
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate static int sort_srcip(a, b)
15407c478bd9Sstevel@tonic-gate const void *a;
15417c478bd9Sstevel@tonic-gate const void *b;
15427c478bd9Sstevel@tonic-gate {
15437c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
15447c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	if (ntohl(ap->st_src.in4.s_addr) == ntohl(bp->st_src.in4.s_addr))
15477c478bd9Sstevel@tonic-gate 		return 0;
15487c478bd9Sstevel@tonic-gate 	else if (ntohl(ap->st_src.in4.s_addr) > ntohl(bp->st_src.in4.s_addr))
15497c478bd9Sstevel@tonic-gate 		return 1;
15507c478bd9Sstevel@tonic-gate 	return -1;
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate static int sort_dstip(a, b)
15547c478bd9Sstevel@tonic-gate const void *a;
15557c478bd9Sstevel@tonic-gate const void *b;
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
15587c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	if (ntohl(ap->st_dst.in4.s_addr) == ntohl(bp->st_dst.in4.s_addr))
15617c478bd9Sstevel@tonic-gate 		return 0;
15627c478bd9Sstevel@tonic-gate 	else if (ntohl(ap->st_dst.in4.s_addr) > ntohl(bp->st_dst.in4.s_addr))
15637c478bd9Sstevel@tonic-gate 		return 1;
15647c478bd9Sstevel@tonic-gate 	return -1;
15657c478bd9Sstevel@tonic-gate }
15667c478bd9Sstevel@tonic-gate #endif
1567