xref: /onnv-gate/usr/src/cmd/ipf/tools/ipfstat.c (revision 8463:a5df9cefde5c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 1993-2001, 2003 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
66518Sjojemann  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
70Sstevel@tonic-gate  * Use is subject to license terms.
80Sstevel@tonic-gate  */
90Sstevel@tonic-gate 
100Sstevel@tonic-gate #ifdef __FreeBSD__
110Sstevel@tonic-gate # ifndef __FreeBSD_cc_version
120Sstevel@tonic-gate #  include <osreldate.h>
130Sstevel@tonic-gate # else
140Sstevel@tonic-gate #  if __FreeBSD_cc_version < 430000
150Sstevel@tonic-gate #   include <osreldate.h>
160Sstevel@tonic-gate #  endif
170Sstevel@tonic-gate # endif
180Sstevel@tonic-gate #endif
190Sstevel@tonic-gate #include <sys/ioctl.h>
200Sstevel@tonic-gate #include <fcntl.h>
212393Syz155240 #ifdef linux
222393Syz155240 # include <linux/a.out.h>
232393Syz155240 #else
242393Syz155240 # include <nlist.h>
252393Syz155240 #endif
260Sstevel@tonic-gate #include <ctype.h>
272393Syz155240 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
282393Syz155240 # include <stddef.h>
292393Syz155240 #endif
300Sstevel@tonic-gate #include "ipf.h"
312393Syz155240 #include "netinet/ipl.h"
320Sstevel@tonic-gate #if defined(STATETOP)
330Sstevel@tonic-gate # if defined(_BSDI_VERSION)
342393Syz155240 #  undef STATETOP
350Sstevel@tonic-gate # endif
360Sstevel@tonic-gate # if defined(__FreeBSD__) && \
370Sstevel@tonic-gate      (!defined(__FreeBSD_version) || (__FreeBSD_version < 430000))
380Sstevel@tonic-gate #  undef STATETOP
390Sstevel@tonic-gate # endif
400Sstevel@tonic-gate # if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105000000)
410Sstevel@tonic-gate #  undef STATETOP
420Sstevel@tonic-gate # endif
430Sstevel@tonic-gate # if defined(sun)
440Sstevel@tonic-gate #  if defined(__svr4__) || defined(__SVR4)
450Sstevel@tonic-gate #   include <sys/select.h>
460Sstevel@tonic-gate #  else
470Sstevel@tonic-gate #   undef STATETOP	/* NOT supported on SunOS4 */
480Sstevel@tonic-gate #  endif
490Sstevel@tonic-gate # endif
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate #if defined(STATETOP) && !defined(linux)
520Sstevel@tonic-gate # include <netinet/ip_var.h>
530Sstevel@tonic-gate # include <netinet/tcp_fsm.h>
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate #ifdef STATETOP
562393Syz155240 # include <ctype.h>
572393Syz155240 # include <signal.h>
580Sstevel@tonic-gate # if SOLARIS || defined(__NetBSD__) || defined(_BSDI_VERSION) || \
590Sstevel@tonic-gate      defined(__sgi)
600Sstevel@tonic-gate #  ifdef ERR
610Sstevel@tonic-gate #   undef ERR
620Sstevel@tonic-gate #  endif
632393Syz155240 #  undef ISASCII
642393Syz155240 #  undef ISPRINT
650Sstevel@tonic-gate #  include <curses.h>
660Sstevel@tonic-gate # else /* SOLARIS */
670Sstevel@tonic-gate #  include <ncurses.h>
680Sstevel@tonic-gate # endif /* SOLARIS */
690Sstevel@tonic-gate #endif /* STATETOP */
700Sstevel@tonic-gate #include "kmem.h"
710Sstevel@tonic-gate #if defined(__NetBSD__) || (__OpenBSD__)
720Sstevel@tonic-gate # include <paths.h>
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #if !defined(lint)
760Sstevel@tonic-gate static const char sccsid[] = "@(#)fils.c	1.21 4/20/96 (C) 1993-2000 Darren Reed";
772393Syz155240 static const char rcsid[] = "@(#)$Id: ipfstat.c,v 1.44.2.12 2005/06/12 07:18:46 darrenr Exp $";
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #ifdef __hpux
810Sstevel@tonic-gate # define	nlist	nlist64
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate 
840Sstevel@tonic-gate extern	char	*optarg;
850Sstevel@tonic-gate extern	int	optind;
862393Syz155240 extern	int	opterr;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	PRINTF	(void)printf
890Sstevel@tonic-gate #define	FPRINTF	(void)fprintf
900Sstevel@tonic-gate static	char	*filters[4] = { "ipfilter(in)", "ipfilter(out)",
910Sstevel@tonic-gate 				"ipacct(in)", "ipacct(out)" };
920Sstevel@tonic-gate static	int	state_logging = -1;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate int	opts = 0;
950Sstevel@tonic-gate int	use_inet6 = 0;
960Sstevel@tonic-gate int	live_kernel = 1;
970Sstevel@tonic-gate int	state_fd = -1;
980Sstevel@tonic-gate int	ipf_fd = -1;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #ifdef STATETOP
1010Sstevel@tonic-gate #define	STSTRSIZE 	80
1020Sstevel@tonic-gate #define	STGROWSIZE	16
1030Sstevel@tonic-gate #define	HOSTNMLEN	40
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #define	STSORT_PR	0
1060Sstevel@tonic-gate #define	STSORT_PKTS	1
1070Sstevel@tonic-gate #define	STSORT_BYTES	2
1080Sstevel@tonic-gate #define	STSORT_TTL	3
1090Sstevel@tonic-gate #define	STSORT_SRCIP	4
1102393Syz155240 #define	STSORT_SRCPT	5
1112393Syz155240 #define	STSORT_DSTIP	6
1122393Syz155240 #define	STSORT_DSTPT	7
1132393Syz155240 #define	STSORT_MAX	STSORT_DSTPT
1140Sstevel@tonic-gate #define	STSORT_DEFAULT	STSORT_BYTES
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate typedef struct statetop {
1180Sstevel@tonic-gate 	i6addr_t	st_src;
1190Sstevel@tonic-gate 	i6addr_t	st_dst;
1200Sstevel@tonic-gate 	u_short		st_sport;
1210Sstevel@tonic-gate 	u_short 	st_dport;
1220Sstevel@tonic-gate 	u_char		st_p;
1232393Syz155240 	u_char		st_v;
1240Sstevel@tonic-gate 	u_char		st_state[2];
1250Sstevel@tonic-gate 	U_QUAD_T	st_pkts;
1260Sstevel@tonic-gate 	U_QUAD_T	st_bytes;
1270Sstevel@tonic-gate 	u_long		st_age;
1280Sstevel@tonic-gate } statetop_t;
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate 
1312393Syz155240 int	main __P((int, char *[]));
1322393Syz155240 
1330Sstevel@tonic-gate static	void	showstats __P((friostat_t *, u_32_t));
134*8463SDarren.Reed@Sun.COM static	void	showfrstates __P((ipfrstat_t *, u_long));
1350Sstevel@tonic-gate static	void	showlist __P((friostat_t *));
1360Sstevel@tonic-gate static	void	showipstates __P((ips_stat_t *));
1370Sstevel@tonic-gate static	void	showauthstates __P((fr_authstat_t *));
1380Sstevel@tonic-gate static	void	showgroups __P((friostat_t *));
1392393Syz155240 static	void	usage __P((char *));
1403448Sdh155122 static	void	printlivelist __P((int, int, frentry_t *, char *, char *));
1413448Sdh155122 static	void	printdeadlist __P((int, int, frentry_t *, char *, char *));
1420Sstevel@tonic-gate static	void	printlist __P((frentry_t *, char *));
1432393Syz155240 static	void	parse_ipportstr __P((const char *, i6addr_t *, int *));
1440Sstevel@tonic-gate static	void	ipfstate_live __P((char *, friostat_t **, ips_stat_t **,
1450Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1460Sstevel@tonic-gate static	void	ipfstate_dead __P((char *, friostat_t **, ips_stat_t **,
1470Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1480Sstevel@tonic-gate #ifdef STATETOP
1492393Syz155240 static	void	topipstates __P((i6addr_t, i6addr_t, int, int, int,
1502393Syz155240 				 int, int, int));
1512393Syz155240 static	void	sig_break __P((int));
1522393Syz155240 static	void	sig_resize __P((int));
1532393Syz155240 static	char	*getip __P((int, i6addr_t *));
1540Sstevel@tonic-gate static	char	*ttl_to_string __P((long));
1550Sstevel@tonic-gate static	int	sort_p __P((const void *, const void *));
1560Sstevel@tonic-gate static	int	sort_pkts __P((const void *, const void *));
1570Sstevel@tonic-gate static	int	sort_bytes __P((const void *, const void *));
1580Sstevel@tonic-gate static	int	sort_ttl __P((const void *, const void *));
1590Sstevel@tonic-gate static	int	sort_srcip __P((const void *, const void *));
1602393Syz155240 static	int	sort_srcpt __P((const void *, const void *));
1610Sstevel@tonic-gate static	int	sort_dstip __P((const void *, const void *));
1622393Syz155240 static	int	sort_dstpt __P((const void *, const void *));
1630Sstevel@tonic-gate #endif
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 
usage(name)1662393Syz155240 static void usage(name)
1670Sstevel@tonic-gate char *name;
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate #ifdef  USE_INET6
1702393Syz155240 	fprintf(stderr, "Usage: %s [-6aAdfghIilnoRsv]\n", name);
1710Sstevel@tonic-gate #else
1722393Syz155240 	fprintf(stderr, "Usage: %s [-aAdfghIilnoRsv]\n", name);
1730Sstevel@tonic-gate #endif
1742393Syz155240 	fprintf(stderr, "       %s [-M corefile] [-N symbol-list]\n", name);
1752393Syz155240 #ifdef	USE_INET6
1762393Syz155240 	fprintf(stderr, "       %s -t [-6C] ", name);
1772393Syz155240 #else
1782393Syz155240 	fprintf(stderr, "       %s -t [-C] ", name);
1792393Syz155240 #endif
1802393Syz155240 	fprintf(stderr, "[-D destination address] [-P protocol] [-S source address] [-T refresh time]\n");
1810Sstevel@tonic-gate 	exit(1);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 
main(argc,argv)1850Sstevel@tonic-gate int main(argc,argv)
1860Sstevel@tonic-gate int argc;
1870Sstevel@tonic-gate char *argv[];
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	fr_authstat_t	frauthst;
1900Sstevel@tonic-gate 	fr_authstat_t	*frauthstp = &frauthst;
1910Sstevel@tonic-gate 	friostat_t fio;
1920Sstevel@tonic-gate 	friostat_t *fiop = &fio;
1930Sstevel@tonic-gate 	ips_stat_t ipsst;
1940Sstevel@tonic-gate 	ips_stat_t *ipsstp = &ipsst;
1950Sstevel@tonic-gate 	ipfrstat_t ifrst;
1960Sstevel@tonic-gate 	ipfrstat_t *ifrstp = &ifrst;
1970Sstevel@tonic-gate 	char	*device = IPL_NAME, *memf = NULL;
1982393Syz155240 	char	*options, *kern = NULL;
1990Sstevel@tonic-gate 	int	c, myoptind;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	int protocol = -1;		/* -1 = wild card for any protocol */
2020Sstevel@tonic-gate 	int refreshtime = 1; 		/* default update time */
2030Sstevel@tonic-gate 	int sport = -1;			/* -1 = wild card for any source port */
2040Sstevel@tonic-gate 	int dport = -1;			/* -1 = wild card for any dest port */
2050Sstevel@tonic-gate 	int topclosed = 0;		/* do not show closed tcp sessions */
2062393Syz155240 	i6addr_t saddr, daddr;
2070Sstevel@tonic-gate 	u_32_t frf;
2080Sstevel@tonic-gate 
2092393Syz155240 #ifdef	USE_INET6
2102393Syz155240 	options = "6aACdfghIilnostvD:M:N:P:RS:T:";
2112393Syz155240 #else
2122393Syz155240 	options = "aACdfghIilnostvD:M:N:P:RS:T:";
2132393Syz155240 #endif
2142393Syz155240 
2152393Syz155240 	saddr.in4.s_addr = INADDR_ANY; 	/* default any v4 source addr */
2162393Syz155240 	daddr.in4.s_addr = INADDR_ANY; 	/* default any v4 dest addr */
2172393Syz155240 #ifdef	USE_INET6
2182393Syz155240 	saddr.in6 = in6addr_any;	/* default any v6 source addr */
2192393Syz155240 	daddr.in6 = in6addr_any;	/* default any v6 dest addr */
2202393Syz155240 #endif
2212393Syz155240 
2222393Syz155240 	/* Don't warn about invalid flags when we run getopt for the 1st time */
2232393Syz155240 	opterr = 0;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * Parse these two arguments now lest there be any buffer overflows
2270Sstevel@tonic-gate 	 * in the parsing of the rest.
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	myoptind = optind;
2302393Syz155240 	while ((c = getopt(argc, argv, options)) != -1) {
2310Sstevel@tonic-gate 		switch (c)
2320Sstevel@tonic-gate 		{
2330Sstevel@tonic-gate 		case 'M' :
2340Sstevel@tonic-gate 			memf = optarg;
2350Sstevel@tonic-gate 			live_kernel = 0;
2360Sstevel@tonic-gate 			break;
2370Sstevel@tonic-gate 		case 'N' :
2380Sstevel@tonic-gate 			kern = optarg;
2390Sstevel@tonic-gate 			live_kernel = 0;
2400Sstevel@tonic-gate 			break;
2410Sstevel@tonic-gate 		}
2422393Syz155240 	}
2430Sstevel@tonic-gate 	optind = myoptind;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (live_kernel == 1) {
2460Sstevel@tonic-gate 		if ((state_fd = open(IPSTATE_NAME, O_RDONLY)) == -1) {
2472393Syz155240 			perror("open(IPSTATE_NAME)");
2480Sstevel@tonic-gate 			exit(-1);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
2512393Syz155240 			fprintf(stderr, "open(%s)", device);
2522393Syz155240 			perror("");
2530Sstevel@tonic-gate 			exit(-1);
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2572393Syz155240 	if (kern != NULL || memf != NULL) {
2580Sstevel@tonic-gate 		(void)setgid(getgid());
2590Sstevel@tonic-gate 		(void)setreuid(getuid(), getuid());
2603448Sdh155122 		if (openkmem(kern, memf) == -1)
2613448Sdh155122 			exit(-1);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2642393Syz155240 	if (live_kernel == 1)
2652393Syz155240 		(void) checkrev(device);
2660Sstevel@tonic-gate 	(void)setgid(getgid());
2670Sstevel@tonic-gate 	(void)setreuid(getuid(), getuid());
2680Sstevel@tonic-gate 
2692393Syz155240 	opterr = 1;
2702393Syz155240 
2712393Syz155240 	while ((c = getopt(argc, argv, options)) != -1)
2720Sstevel@tonic-gate 	{
2730Sstevel@tonic-gate 		switch (c)
2740Sstevel@tonic-gate 		{
2750Sstevel@tonic-gate #ifdef	USE_INET6
2760Sstevel@tonic-gate 		case '6' :
2770Sstevel@tonic-gate 			use_inet6 = 1;
2780Sstevel@tonic-gate 			break;
2790Sstevel@tonic-gate #endif
2800Sstevel@tonic-gate 		case 'a' :
2810Sstevel@tonic-gate 			opts |= OPT_ACCNT|OPT_SHOWLIST;
2820Sstevel@tonic-gate 			break;
2830Sstevel@tonic-gate 		case 'A' :
2840Sstevel@tonic-gate 			opts |= OPT_AUTHSTATS;
2850Sstevel@tonic-gate 			break;
2860Sstevel@tonic-gate 		case 'C' :
2870Sstevel@tonic-gate 			topclosed = 1;
2880Sstevel@tonic-gate 			break;
2890Sstevel@tonic-gate 		case 'd' :
2900Sstevel@tonic-gate 			opts |= OPT_DEBUG;
2910Sstevel@tonic-gate 			break;
2920Sstevel@tonic-gate 		case 'D' :
2930Sstevel@tonic-gate 			parse_ipportstr(optarg, &daddr, &dport);
2940Sstevel@tonic-gate 			break;
2950Sstevel@tonic-gate 		case 'f' :
2960Sstevel@tonic-gate 			opts |= OPT_FRSTATES;
2970Sstevel@tonic-gate 			break;
2980Sstevel@tonic-gate 		case 'g' :
2990Sstevel@tonic-gate 			opts |= OPT_GROUPS;
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 		case 'h' :
3020Sstevel@tonic-gate 			opts |= OPT_HITS;
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 		case 'i' :
3050Sstevel@tonic-gate 			opts |= OPT_INQUE|OPT_SHOWLIST;
3060Sstevel@tonic-gate 			break;
3070Sstevel@tonic-gate 		case 'I' :
3080Sstevel@tonic-gate 			opts |= OPT_INACTIVE;
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 		case 'l' :
3110Sstevel@tonic-gate 			opts |= OPT_SHOWLIST;
3120Sstevel@tonic-gate 			break;
3130Sstevel@tonic-gate 		case 'M' :
3140Sstevel@tonic-gate 			break;
3150Sstevel@tonic-gate 		case 'N' :
3160Sstevel@tonic-gate 			break;
3170Sstevel@tonic-gate 		case 'n' :
3180Sstevel@tonic-gate 			opts |= OPT_SHOWLINENO;
3190Sstevel@tonic-gate 			break;
3200Sstevel@tonic-gate 		case 'o' :
3210Sstevel@tonic-gate 			opts |= OPT_OUTQUE|OPT_SHOWLIST;
3220Sstevel@tonic-gate 			break;
3230Sstevel@tonic-gate 		case 'P' :
3242393Syz155240 			protocol = getproto(optarg);
3252393Syz155240 			if (protocol == -1) {
3262393Syz155240 				fprintf(stderr, "%s: Invalid protocol: %s\n",
3270Sstevel@tonic-gate 					argv[0], optarg);
3280Sstevel@tonic-gate 				exit(-2);
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 			break;
3312393Syz155240 		case 'R' :
3322393Syz155240 			opts |= OPT_NORESOLVE;
3332393Syz155240 			break;
3340Sstevel@tonic-gate 		case 's' :
3350Sstevel@tonic-gate 			opts |= OPT_IPSTATES;
3360Sstevel@tonic-gate 			break;
3370Sstevel@tonic-gate 		case 'S' :
3380Sstevel@tonic-gate 			parse_ipportstr(optarg, &saddr, &sport);
3390Sstevel@tonic-gate 			break;
3400Sstevel@tonic-gate 		case 't' :
3410Sstevel@tonic-gate #ifdef STATETOP
3420Sstevel@tonic-gate 			opts |= OPT_STATETOP;
3430Sstevel@tonic-gate 			break;
3440Sstevel@tonic-gate #else
3450Sstevel@tonic-gate 			fprintf(stderr,
3462393Syz155240 				"%s: state top facility not compiled in\n",
3470Sstevel@tonic-gate 				argv[0]);
3480Sstevel@tonic-gate 			exit(-2);
3490Sstevel@tonic-gate #endif
3500Sstevel@tonic-gate 		case 'T' :
3510Sstevel@tonic-gate 			if (!sscanf(optarg, "%d", &refreshtime) ||
3520Sstevel@tonic-gate 				    (refreshtime <= 0)) {
3530Sstevel@tonic-gate 				fprintf(stderr,
3542393Syz155240 					"%s: Invalid refreshtime < 1 : %s\n",
3550Sstevel@tonic-gate 					argv[0], optarg);
3560Sstevel@tonic-gate 				exit(-2);
3570Sstevel@tonic-gate 			}
3580Sstevel@tonic-gate 			break;
3590Sstevel@tonic-gate 		case 'v' :
3600Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
3610Sstevel@tonic-gate 			opts |= OPT_UNDEF;
3620Sstevel@tonic-gate 			break;
3630Sstevel@tonic-gate 		default :
3642393Syz155240 			usage(argv[0]);
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if (live_kernel == 1) {
3700Sstevel@tonic-gate 		bzero((char *)&fio, sizeof(fio));
3710Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
3720Sstevel@tonic-gate 		bzero((char *)&ifrst, sizeof(ifrst));
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		ipfstate_live(device, &fiop, &ipsstp, &ifrstp,
3750Sstevel@tonic-gate 			      &frauthstp, &frf);
3760Sstevel@tonic-gate 	} else
3770Sstevel@tonic-gate 		ipfstate_dead(kern, &fiop, &ipsstp, &ifrstp, &frauthstp, &frf);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (opts & OPT_IPSTATES) {
3800Sstevel@tonic-gate 		showipstates(ipsstp);
3810Sstevel@tonic-gate 	} else if (opts & OPT_SHOWLIST) {
3820Sstevel@tonic-gate 		showlist(fiop);
3830Sstevel@tonic-gate 		if ((opts & OPT_OUTQUE) && (opts & OPT_INQUE)){
3840Sstevel@tonic-gate 			opts &= ~OPT_OUTQUE;
3850Sstevel@tonic-gate 			showlist(fiop);
3860Sstevel@tonic-gate 		}
3872393Syz155240 	} else if (opts & OPT_FRSTATES)
388*8463SDarren.Reed@Sun.COM 		showfrstates(ifrstp, fiop->f_ticks);
3890Sstevel@tonic-gate #ifdef STATETOP
3902393Syz155240 	else if (opts & OPT_STATETOP)
3912393Syz155240 		topipstates(saddr, daddr, sport, dport, protocol,
3922393Syz155240 			    use_inet6 ? 6 : 4, refreshtime, topclosed);
3930Sstevel@tonic-gate #endif
3942393Syz155240 	else if (opts & OPT_AUTHSTATS)
3952393Syz155240 		showauthstates(frauthstp);
3962393Syz155240 	else if (opts & OPT_GROUPS)
3972393Syz155240 		showgroups(fiop);
3982393Syz155240 	else
3992393Syz155240 		showstats(fiop, frf);
4002393Syz155240 
4010Sstevel@tonic-gate 	return 0;
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate /*
4060Sstevel@tonic-gate  * Fill in the stats structures from the live kernel, using a combination
4070Sstevel@tonic-gate  * of ioctl's and copying directly from kernel memory.
4080Sstevel@tonic-gate  */
ipfstate_live(device,fiopp,ipsstpp,ifrstpp,frauthstpp,frfp)4090Sstevel@tonic-gate static void ipfstate_live(device, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
4100Sstevel@tonic-gate char *device;
4110Sstevel@tonic-gate friostat_t **fiopp;
4120Sstevel@tonic-gate ips_stat_t **ipsstpp;
4130Sstevel@tonic-gate ipfrstat_t **ifrstpp;
4140Sstevel@tonic-gate fr_authstat_t **frauthstpp;
4150Sstevel@tonic-gate u_32_t *frfp;
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	ipfobj_t ipfo;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	if (checkrev(device) == -1) {
4200Sstevel@tonic-gate 		fprintf(stderr, "User/kernel version check failed\n");
4210Sstevel@tonic-gate 		exit(1);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) == 0) {
4250Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4260Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4270Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(friostat_t);
4280Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*fiopp;
4290Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_IPFSTAT;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFS, &ipfo) == -1) {
4320Sstevel@tonic-gate 			perror("ioctl(ipf:SIOCGETFS)");
4330Sstevel@tonic-gate 			exit(-1);
4340Sstevel@tonic-gate 		}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFF, frfp) == -1)
4370Sstevel@tonic-gate 			perror("ioctl(SIOCGETFF)");
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if ((opts & OPT_IPSTATES) != 0) {
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4430Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4440Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ips_stat_t);
4450Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ipsstpp;
4460Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_STATESTAT;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
4490Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETFS)");
4500Sstevel@tonic-gate 			exit(-1);
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 		if (ioctl(state_fd, SIOCGETLG, &state_logging) == -1) {
4530Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETLG)");
4540Sstevel@tonic-gate 			exit(-1);
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if ((opts & OPT_FRSTATES) != 0) {
4590Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4600Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4610Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ipfrstat_t);
4620Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ifrstpp;
4630Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_FRAGSTAT;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGFRST, &ipfo) == -1) {
4660Sstevel@tonic-gate 			perror("ioctl(SIOCGFRST)");
4670Sstevel@tonic-gate 			exit(-1);
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
4720Sstevel@tonic-gate 		PRINTF("opts %#x name %s\n", opts, device);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) != 0) {
4750Sstevel@tonic-gate 		if (ipf_fd >= 0) {
4760Sstevel@tonic-gate 			close(ipf_fd);
4770Sstevel@tonic-gate 			ipf_fd = -1;
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		device = IPAUTH_NAME;
4800Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
4810Sstevel@tonic-gate 			perror("open");
4820Sstevel@tonic-gate 			exit(-1);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4860Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4870Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(fr_authstat_t);
4880Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*frauthstpp;
4890Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_AUTHSTAT;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	    	if (ioctl(ipf_fd, SIOCATHST, &ipfo) == -1) {
4920Sstevel@tonic-gate 			perror("ioctl(SIOCATHST)");
4930Sstevel@tonic-gate 			exit(-1);
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate  * Build up the stats structures from data held in the "core" memory.
5010Sstevel@tonic-gate  * This is mainly useful when looking at data in crash dumps and ioctl's
5020Sstevel@tonic-gate  * just won't work any more.
5030Sstevel@tonic-gate  */
ipfstate_dead(kernel,fiopp,ipsstpp,ifrstpp,frauthstpp,frfp)5040Sstevel@tonic-gate static void ipfstate_dead(kernel, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
5050Sstevel@tonic-gate char *kernel;
5060Sstevel@tonic-gate friostat_t **fiopp;
5070Sstevel@tonic-gate ips_stat_t **ipsstpp;
5080Sstevel@tonic-gate ipfrstat_t **ifrstpp;
5090Sstevel@tonic-gate fr_authstat_t **frauthstpp;
5100Sstevel@tonic-gate u_32_t *frfp;
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	static fr_authstat_t frauthst, *frauthstp;
5130Sstevel@tonic-gate 	static ips_stat_t ipsst, *ipsstp;
5140Sstevel@tonic-gate 	static ipfrstat_t ifrst, *ifrstp;
5150Sstevel@tonic-gate 	static friostat_t fio, *fiop;
5160Sstevel@tonic-gate 	int temp;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	void *rules[2][2];
5190Sstevel@tonic-gate 	struct nlist deadlist[43] = {
5200Sstevel@tonic-gate 		{ "fr_authstats" },		/* 0 */
5210Sstevel@tonic-gate 		{ "fae_list" },
5220Sstevel@tonic-gate 		{ "ipauth" },
5230Sstevel@tonic-gate 		{ "fr_authlist" },
5240Sstevel@tonic-gate 		{ "fr_authstart" },
5250Sstevel@tonic-gate 		{ "fr_authend" },		/* 5 */
5260Sstevel@tonic-gate 		{ "fr_authnext" },
5270Sstevel@tonic-gate 		{ "fr_auth" },
5280Sstevel@tonic-gate 		{ "fr_authused" },
5290Sstevel@tonic-gate 		{ "fr_authsize" },
5300Sstevel@tonic-gate 		{ "fr_defaultauthage" },	/* 10 */
5310Sstevel@tonic-gate 		{ "fr_authpkts" },
5320Sstevel@tonic-gate 		{ "fr_auth_lock" },
5330Sstevel@tonic-gate 		{ "frstats" },
5340Sstevel@tonic-gate 		{ "ips_stats" },
5350Sstevel@tonic-gate 		{ "ips_num" },			/* 15 */
5360Sstevel@tonic-gate 		{ "ips_wild" },
5370Sstevel@tonic-gate 		{ "ips_list" },
5380Sstevel@tonic-gate 		{ "ips_table" },
5390Sstevel@tonic-gate 		{ "fr_statemax" },
5400Sstevel@tonic-gate 		{ "fr_statesize" },		/* 20 */
5410Sstevel@tonic-gate 		{ "fr_state_doflush" },
5420Sstevel@tonic-gate 		{ "fr_state_lock" },
5430Sstevel@tonic-gate 		{ "ipfr_heads" },
5440Sstevel@tonic-gate 		{ "ipfr_nattab" },
5450Sstevel@tonic-gate 		{ "ipfr_stats" },		/* 25 */
5460Sstevel@tonic-gate 		{ "ipfr_inuse" },
5470Sstevel@tonic-gate 		{ "fr_ipfrttl" },
5480Sstevel@tonic-gate 		{ "fr_frag_lock" },
5490Sstevel@tonic-gate 		{ "ipfr_timer_id" },
5500Sstevel@tonic-gate 		{ "fr_nat_lock" },		/* 30 */
5510Sstevel@tonic-gate 		{ "ipfilter" },
5520Sstevel@tonic-gate 		{ "ipfilter6" },
5530Sstevel@tonic-gate 		{ "ipacct" },
5540Sstevel@tonic-gate 		{ "ipacct6" },
5550Sstevel@tonic-gate 		{ "ipl_frouteok" },		/* 35 */
5560Sstevel@tonic-gate 		{ "fr_running" },
5570Sstevel@tonic-gate 		{ "ipfgroups" },
5580Sstevel@tonic-gate 		{ "fr_active" },
5590Sstevel@tonic-gate 		{ "fr_pass" },
5600Sstevel@tonic-gate 		{ "fr_flags" },			/* 40 */
5610Sstevel@tonic-gate 		{ "ipstate_logging" },
5620Sstevel@tonic-gate 		{ NULL }
5630Sstevel@tonic-gate 	};
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	frauthstp = &frauthst;
5670Sstevel@tonic-gate 	ipsstp = &ipsst;
5680Sstevel@tonic-gate 	ifrstp = &ifrst;
5690Sstevel@tonic-gate 	fiop = &fio;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	*frfp = 0;
5720Sstevel@tonic-gate 	*fiopp = fiop;
5730Sstevel@tonic-gate 	*ipsstpp = ipsstp;
5740Sstevel@tonic-gate 	*ifrstpp = ifrstp;
5750Sstevel@tonic-gate 	*frauthstpp = frauthstp;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	bzero((char *)fiop, sizeof(*fiop));
5780Sstevel@tonic-gate 	bzero((char *)ipsstp, sizeof(*ipsstp));
5790Sstevel@tonic-gate 	bzero((char *)ifrstp, sizeof(*ifrstp));
5800Sstevel@tonic-gate 	bzero((char *)frauthstp, sizeof(*frauthstp));
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	if (nlist(kernel, deadlist) == -1) {
5830Sstevel@tonic-gate 		fprintf(stderr, "nlist error\n");
5840Sstevel@tonic-gate 		return;
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * This is for SIOCGETFF.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	kmemcpy((char *)frfp, (u_long)deadlist[40].n_value, sizeof(*frfp));
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * f_locks is a combination of the lock variable from each part of
5940Sstevel@tonic-gate 	 * ipfilter (state, auth, nat, fragments).
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	kmemcpy((char *)fiop, (u_long)deadlist[13].n_value, sizeof(*fiop));
5970Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[22].n_value,
5980Sstevel@tonic-gate 		sizeof(fiop->f_locks[0]));
5990Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[30].n_value,
6000Sstevel@tonic-gate 		sizeof(fiop->f_locks[1]));
6010Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[2], (u_long)deadlist[28].n_value,
6020Sstevel@tonic-gate 		sizeof(fiop->f_locks[2]));
6030Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[3], (u_long)deadlist[12].n_value,
6040Sstevel@tonic-gate 		sizeof(fiop->f_locks[3]));
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * Get pointers to each list of rules (active, inactive, in, out)
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[31].n_value, sizeof(rules));
6100Sstevel@tonic-gate 	fiop->f_fin[0] = rules[0][0];
6110Sstevel@tonic-gate 	fiop->f_fin[1] = rules[0][1];
6120Sstevel@tonic-gate 	fiop->f_fout[0] = rules[1][0];
6130Sstevel@tonic-gate 	fiop->f_fout[1] = rules[1][1];
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/*
6160Sstevel@tonic-gate 	 * Same for IPv6, except make them null if support for it is not
6170Sstevel@tonic-gate 	 * being compiled in.
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate #ifdef	USE_INET6
6200Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[32].n_value, sizeof(rules));
6210Sstevel@tonic-gate 	fiop->f_fin6[0] = rules[0][0];
6220Sstevel@tonic-gate 	fiop->f_fin6[1] = rules[0][1];
6230Sstevel@tonic-gate 	fiop->f_fout6[0] = rules[1][0];
6240Sstevel@tonic-gate 	fiop->f_fout6[1] = rules[1][1];
6250Sstevel@tonic-gate #else
6260Sstevel@tonic-gate 	fiop->f_fin6[0] = NULL;
6270Sstevel@tonic-gate 	fiop->f_fin6[1] = NULL;
6280Sstevel@tonic-gate 	fiop->f_fout6[0] = NULL;
6290Sstevel@tonic-gate 	fiop->f_fout6[1] = NULL;
6300Sstevel@tonic-gate #endif
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/*
6330Sstevel@tonic-gate 	 * Now get accounting rules pointers.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[33].n_value, sizeof(rules));
6360Sstevel@tonic-gate 	fiop->f_acctin[0] = rules[0][0];
6370Sstevel@tonic-gate 	fiop->f_acctin[1] = rules[0][1];
6380Sstevel@tonic-gate 	fiop->f_acctout[0] = rules[1][0];
6390Sstevel@tonic-gate 	fiop->f_acctout[1] = rules[1][1];
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate #ifdef	USE_INET6
6420Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[34].n_value, sizeof(rules));
6430Sstevel@tonic-gate 	fiop->f_acctin6[0] = rules[0][0];
6440Sstevel@tonic-gate 	fiop->f_acctin6[1] = rules[0][1];
6450Sstevel@tonic-gate 	fiop->f_acctout6[0] = rules[1][0];
6460Sstevel@tonic-gate 	fiop->f_acctout6[1] = rules[1][1];
6470Sstevel@tonic-gate #else
6480Sstevel@tonic-gate 	fiop->f_acctin6[0] = NULL;
6490Sstevel@tonic-gate 	fiop->f_acctin6[1] = NULL;
6500Sstevel@tonic-gate 	fiop->f_acctout6[0] = NULL;
6510Sstevel@tonic-gate 	fiop->f_acctout6[1] = NULL;
6520Sstevel@tonic-gate #endif
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * A collection of "global" variables used inside the kernel which
6560Sstevel@tonic-gate 	 * are all collected in friostat_t via ioctl.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_froute, (u_long)deadlist[35].n_value,
6590Sstevel@tonic-gate 		sizeof(fiop->f_froute));
6600Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_running, (u_long)deadlist[36].n_value,
6610Sstevel@tonic-gate 		sizeof(fiop->f_running));
6620Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_groups, (u_long)deadlist[37].n_value,
6630Sstevel@tonic-gate 		sizeof(fiop->f_groups));
6640Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_active, (u_long)deadlist[38].n_value,
6650Sstevel@tonic-gate 		sizeof(fiop->f_active));
6660Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_defpass, (u_long)deadlist[39].n_value,
6670Sstevel@tonic-gate 		sizeof(fiop->f_defpass));
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Build up the state information stats structure.
6710Sstevel@tonic-gate 	 */
6720Sstevel@tonic-gate 	kmemcpy((char *)ipsstp, (u_long)deadlist[14].n_value, sizeof(*ipsstp));
6730Sstevel@tonic-gate 	kmemcpy((char *)&temp, (u_long)deadlist[15].n_value, sizeof(temp));
6740Sstevel@tonic-gate 	ipsstp->iss_active = temp;
6750Sstevel@tonic-gate 	ipsstp->iss_table = (void *)deadlist[18].n_value;
6760Sstevel@tonic-gate 	ipsstp->iss_list = (void *)deadlist[17].n_value;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * Build up the authentiation information stats structure.
6800Sstevel@tonic-gate 	 */
6810Sstevel@tonic-gate 	kmemcpy((char *)frauthstp, (u_long)deadlist[0].n_value,
6820Sstevel@tonic-gate 		sizeof(*frauthstp));
6830Sstevel@tonic-gate 	frauthstp->fas_faelist = (void *)deadlist[1].n_value;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * Build up the fragment information stats structure.
6870Sstevel@tonic-gate 	 */
6880Sstevel@tonic-gate 	kmemcpy((char *)ifrstp, (u_long)deadlist[25].n_value,
6890Sstevel@tonic-gate 		sizeof(*ifrstp));
6900Sstevel@tonic-gate 	ifrstp->ifs_table = (void *)deadlist[23].n_value;
6910Sstevel@tonic-gate 	ifrstp->ifs_nattab = (void *)deadlist[24].n_value;
6920Sstevel@tonic-gate 	kmemcpy((char *)&ifrstp->ifs_inuse, (u_long)deadlist[26].n_value,
6930Sstevel@tonic-gate 		sizeof(ifrstp->ifs_inuse));
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/*
6960Sstevel@tonic-gate 	 * Get logging on/off switches
6970Sstevel@tonic-gate 	 */
6980Sstevel@tonic-gate 	kmemcpy((char *)&state_logging, (u_long)deadlist[41].n_value,
6990Sstevel@tonic-gate 		sizeof(state_logging));
7000Sstevel@tonic-gate }
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * Display the kernel stats for packets blocked and passed and other
7050Sstevel@tonic-gate  * associated running totals which are kept.
7060Sstevel@tonic-gate  */
showstats(fp,frf)7070Sstevel@tonic-gate static	void	showstats(fp, frf)
7080Sstevel@tonic-gate struct	friostat	*fp;
7090Sstevel@tonic-gate u_32_t frf;
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	PRINTF("bad packets:\t\tin %lu\tout %lu\n",
7130Sstevel@tonic-gate 			fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
7140Sstevel@tonic-gate #ifdef	USE_INET6
7150Sstevel@tonic-gate 	PRINTF(" IPv6 packets:\t\tin %lu out %lu\n",
7160Sstevel@tonic-gate 			fp->f_st[0].fr_ipv6, fp->f_st[1].fr_ipv6);
7170Sstevel@tonic-gate #endif
7180Sstevel@tonic-gate 	PRINTF(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
7190Sstevel@tonic-gate 			fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
7200Sstevel@tonic-gate 			fp->f_st[0].fr_nom);
7212393Syz155240 	PRINTF(" counted %lu short %lu\n",
7220Sstevel@tonic-gate 			fp->f_st[0].fr_acct, fp->f_st[0].fr_short);
7230Sstevel@tonic-gate 	PRINTF("output packets:\t\tblocked %lu passed %lu nomatch %lu",
7240Sstevel@tonic-gate 			fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
7250Sstevel@tonic-gate 			fp->f_st[1].fr_nom);
7262393Syz155240 	PRINTF(" counted %lu short %lu\n",
7270Sstevel@tonic-gate 			fp->f_st[1].fr_acct, fp->f_st[1].fr_short);
7280Sstevel@tonic-gate 	PRINTF(" input packets logged:\tblocked %lu passed %lu\n",
7290Sstevel@tonic-gate 			fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
7300Sstevel@tonic-gate 	PRINTF("output packets logged:\tblocked %lu passed %lu\n",
7310Sstevel@tonic-gate 			fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
7320Sstevel@tonic-gate 	PRINTF(" packets logged:\tinput %lu output %lu\n",
7330Sstevel@tonic-gate 			fp->f_st[0].fr_pkl, fp->f_st[1].fr_pkl);
7340Sstevel@tonic-gate 	PRINTF(" log failures:\t\tinput %lu output %lu\n",
7350Sstevel@tonic-gate 			fp->f_st[0].fr_skip, fp->f_st[1].fr_skip);
7362393Syz155240 	PRINTF("fragment state(in):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
7372393Syz155240 			fp->f_st[0].fr_nfr, fp->f_st[0].fr_bnfr,
7382393Syz155240 			fp->f_st[0].fr_cfr);
7392393Syz155240 	PRINTF("fragment state(out):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
7402393Syz155240 			fp->f_st[1].fr_nfr, fp->f_st[1].fr_bnfr,
7412393Syz155240 			fp->f_st[0].fr_cfr);
7420Sstevel@tonic-gate 	PRINTF("packet state(in):\tkept %lu\tlost %lu\n",
7430Sstevel@tonic-gate 			fp->f_st[0].fr_ads, fp->f_st[0].fr_bads);
7440Sstevel@tonic-gate 	PRINTF("packet state(out):\tkept %lu\tlost %lu\n",
7450Sstevel@tonic-gate 			fp->f_st[1].fr_ads, fp->f_st[1].fr_bads);
7460Sstevel@tonic-gate 	PRINTF("ICMP replies:\t%lu\tTCP RSTs sent:\t%lu\n",
7470Sstevel@tonic-gate 			fp->f_st[0].fr_ret, fp->f_st[1].fr_ret);
7480Sstevel@tonic-gate 	PRINTF("Invalid source(in):\t%lu\n", fp->f_st[0].fr_badsrc);
7490Sstevel@tonic-gate 	PRINTF("Result cache hits(in):\t%lu\t(out):\t%lu\n",
7500Sstevel@tonic-gate 			fp->f_st[0].fr_chit, fp->f_st[1].fr_chit);
7510Sstevel@tonic-gate 	PRINTF("IN Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7520Sstevel@tonic-gate 			fp->f_st[0].fr_pull[0], fp->f_st[0].fr_pull[1]);
7530Sstevel@tonic-gate 	PRINTF("OUT Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7540Sstevel@tonic-gate 			fp->f_st[1].fr_pull[0], fp->f_st[1].fr_pull[1]);
7550Sstevel@tonic-gate 	PRINTF("Fastroute successes:\t%lu\tfailures:\t%lu\n",
7560Sstevel@tonic-gate 			fp->f_froute[0], fp->f_froute[1]);
7570Sstevel@tonic-gate 	PRINTF("TCP cksum fails(in):\t%lu\t(out):\t%lu\n",
7580Sstevel@tonic-gate 			fp->f_st[0].fr_tcpbad, fp->f_st[1].fr_tcpbad);
7590Sstevel@tonic-gate 	PRINTF("IPF Ticks:\t%lu\n", fp->f_ticks);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	PRINTF("Packet log flags set: (%#x)\n", frf);
7620Sstevel@tonic-gate 	if (frf & FF_LOGPASS)
7630Sstevel@tonic-gate 		PRINTF("\tpackets passed through filter\n");
7640Sstevel@tonic-gate 	if (frf & FF_LOGBLOCK)
7650Sstevel@tonic-gate 		PRINTF("\tpackets blocked by filter\n");
7660Sstevel@tonic-gate 	if (frf & FF_LOGNOMATCH)
7670Sstevel@tonic-gate 		PRINTF("\tpackets not matched by filter\n");
7680Sstevel@tonic-gate 	if (!frf)
7690Sstevel@tonic-gate 		PRINTF("\tnone\n");
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * Print out a list of rules from the kernel, starting at the one passed.
7750Sstevel@tonic-gate  */
printlivelist(out,set,fp,group,comment)7763448Sdh155122 static void printlivelist(out, set, fp, group, comment)
7773448Sdh155122 int out, set;
7780Sstevel@tonic-gate frentry_t *fp;
7793448Sdh155122 char *group, *comment;
7800Sstevel@tonic-gate {
7813448Sdh155122 	frgroup_t *grtop, *grtail, *g;
7820Sstevel@tonic-gate 	struct	frentry	fb, *fg;
7836518Sjojemann 	int n;
7843448Sdh155122 	ipfruleiter_t rule;
7853448Sdh155122 	ipfobj_t obj;
7863448Sdh155122 
7873448Sdh155122 	fb.fr_next = fp;
7883448Sdh155122 	n = 0;
7890Sstevel@tonic-gate 
7903448Sdh155122 	grtop = NULL;
7913448Sdh155122 	grtail = NULL;
7923448Sdh155122 	rule.iri_ver = use_inet6? AF_INET6 : AF_INET;
7933448Sdh155122 	rule.iri_inout = out;
7943448Sdh155122 	rule.iri_active = set;
7955417Sjojemann 	rule.iri_nrules = 1;
7963448Sdh155122 	rule.iri_rule = &fb;
7973448Sdh155122 	if (group != NULL)
7983448Sdh155122 		strncpy(rule.iri_group, group, FR_GROUPLEN);
7993448Sdh155122 	else
8003448Sdh155122 		rule.iri_group[0] = '\0';
8013448Sdh155122 
8023448Sdh155122 	bzero((char *)&obj, sizeof(obj));
8033448Sdh155122 	obj.ipfo_rev = IPFILTER_VERSION;
8043448Sdh155122 	obj.ipfo_type = IPFOBJ_IPFITER;
8053448Sdh155122 	obj.ipfo_size = sizeof(rule);
8063448Sdh155122 	obj.ipfo_ptr = &rule;
8073448Sdh155122 
8083448Sdh155122 	do {
8093448Sdh155122 		u_long array[1000];
8103448Sdh155122 
8113448Sdh155122 		memset(array, 0xff, sizeof(array));
8123448Sdh155122 		fp = (frentry_t *)array;
8133448Sdh155122 		rule.iri_rule = fp;
8143448Sdh155122 		if (ioctl(ipf_fd, SIOCIPFITER, &obj) == -1) {
8153448Sdh155122 			perror("ioctl(SIOCIPFITER)");
8160Sstevel@tonic-gate 			return;
8170Sstevel@tonic-gate 		}
8183448Sdh155122 		if (fp->fr_data != NULL)
8193448Sdh155122 			fp->fr_data = (char *)fp + sizeof(*fp);
8203448Sdh155122 
8213448Sdh155122 		n++;
8223448Sdh155122 
8230Sstevel@tonic-gate 		if (opts & (OPT_HITS|OPT_VERBOSE))
8240Sstevel@tonic-gate #ifdef	USE_QUAD_T
8250Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_hits);
8260Sstevel@tonic-gate #else
8270Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_hits);
8280Sstevel@tonic-gate #endif
8290Sstevel@tonic-gate 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
8300Sstevel@tonic-gate #ifdef	USE_QUAD_T
8310Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_bytes);
8320Sstevel@tonic-gate #else
8330Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_bytes);
8340Sstevel@tonic-gate #endif
8350Sstevel@tonic-gate 		if (opts & OPT_SHOWLINENO)
8360Sstevel@tonic-gate 			PRINTF("@%d ", n);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		printfr(fp, ioctl);
8392393Syz155240 		if (opts & OPT_DEBUG) {
8400Sstevel@tonic-gate 			binprint(fp, sizeof(*fp));
8410Sstevel@tonic-gate 			if (fp->fr_data != NULL && fp->fr_dsize > 0)
8420Sstevel@tonic-gate 				binprint(fp->fr_data, fp->fr_dsize);
8430Sstevel@tonic-gate 		}
8443448Sdh155122 
8453448Sdh155122 		if (fp->fr_grhead[0] != '\0') {
8463448Sdh155122 			g = calloc(1, sizeof(*g));
8473448Sdh155122 
8483448Sdh155122 			if (g != NULL) {
8493448Sdh155122 				strncpy(g->fg_name, fp->fr_grhead,
8503448Sdh155122 					FR_GROUPLEN);
8513448Sdh155122 				if (grtop == NULL) {
8523448Sdh155122 					grtop = g;
8533448Sdh155122 					grtail = g;
8543448Sdh155122 				} else {
8553448Sdh155122 					grtail->fg_next = g;
8563448Sdh155122 					grtail = g;
8573448Sdh155122 				}
8583448Sdh155122 			}
8593448Sdh155122 		}
8603448Sdh155122 	} while (fp->fr_next != NULL);
8613448Sdh155122 
8623448Sdh155122 	while ((g = grtop) != NULL) {
8633448Sdh155122 		printlivelist(out, set, NULL, g->fg_name, comment);
8643448Sdh155122 		grtop = g->fg_next;
8653448Sdh155122 		free(g);
8663448Sdh155122 	}
8673448Sdh155122 }
8683448Sdh155122 
8693448Sdh155122 
printdeadlist(out,set,fp,group,comment)8703448Sdh155122 static void printdeadlist(out, set, fp, group, comment)
8713448Sdh155122 int out, set;
8723448Sdh155122 frentry_t *fp;
8733448Sdh155122 char *group, *comment;
8743448Sdh155122 {
8753448Sdh155122 	frgroup_t *grtop, *grtail, *g;
8763448Sdh155122 	struct	frentry	fb, *fg;
8773448Sdh155122 	char	*data;
8783448Sdh155122 	u_32_t	type;
8793448Sdh155122 	int	n;
8803448Sdh155122 
8813448Sdh155122 	fb.fr_next = fp;
8823448Sdh155122 	n = 0;
8833448Sdh155122 	grtop = NULL;
8843448Sdh155122 	grtail = NULL;
8853448Sdh155122 
8863448Sdh155122 	do {
8873448Sdh155122 		fp = fb.fr_next;
8883448Sdh155122 		if (kmemcpy((char *)&fb, (u_long)fb.fr_next,
8893448Sdh155122 			    sizeof(fb)) == -1) {
8903448Sdh155122 			perror("kmemcpy");
8913448Sdh155122 			return;
8923448Sdh155122 		}
8933448Sdh155122 
8943448Sdh155122 		data = NULL;
8953448Sdh155122 		type = fb.fr_type & ~FR_T_BUILTIN;
8963448Sdh155122 		if (type == FR_T_IPF || type == FR_T_BPFOPC) {
8973448Sdh155122 			if (fb.fr_dsize) {
8983448Sdh155122 				data = malloc(fb.fr_dsize);
8993448Sdh155122 
9003448Sdh155122 				if (kmemcpy(data, (u_long)fb.fr_data,
9013448Sdh155122 					    fb.fr_dsize) == -1) {
9023448Sdh155122 					perror("kmemcpy");
9033448Sdh155122 					return;
9043448Sdh155122 				}
9053448Sdh155122 				fb.fr_data = data;
9063448Sdh155122 			}
9073448Sdh155122 		}
9083448Sdh155122 
9093448Sdh155122 		n++;
9103448Sdh155122 
9113448Sdh155122 		if (opts & (OPT_HITS|OPT_VERBOSE))
9123448Sdh155122 #ifdef	USE_QUAD_T
9133448Sdh155122 			PRINTF("%qu ", (unsigned long long) fb.fr_hits);
9143448Sdh155122 #else
9153448Sdh155122 			PRINTF("%lu ", fb.fr_hits);
9163448Sdh155122 #endif
9173448Sdh155122 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
9183448Sdh155122 #ifdef	USE_QUAD_T
9193448Sdh155122 			PRINTF("%qu ", (unsigned long long) fb.fr_bytes);
9203448Sdh155122 #else
9213448Sdh155122 			PRINTF("%lu ", fb.fr_bytes);
9223448Sdh155122 #endif
9233448Sdh155122 		if (opts & OPT_SHOWLINENO)
9243448Sdh155122 			PRINTF("@%d ", n);
9253448Sdh155122 
9263448Sdh155122 		printfr(fp, ioctl);
9273448Sdh155122 		if (opts & OPT_DEBUG) {
9283448Sdh155122 			binprint(fp, sizeof(*fp));
9293448Sdh155122 			if (fb.fr_data != NULL && fb.fr_dsize > 0)
9303448Sdh155122 				binprint(fb.fr_data, fb.fr_dsize);
9313448Sdh155122 		}
9320Sstevel@tonic-gate 		if (data != NULL)
9330Sstevel@tonic-gate 			free(data);
9343448Sdh155122 		if (fb.fr_grhead[0] != '\0') {
9353448Sdh155122 			g = calloc(1, sizeof(*g));
9363448Sdh155122 
9373448Sdh155122 			if (g != NULL) {
9383448Sdh155122 				strncpy(g->fg_name, fb.fr_grhead,
9393448Sdh155122 					FR_GROUPLEN);
9403448Sdh155122 				if (grtop == NULL) {
9413448Sdh155122 					grtop = g;
9423448Sdh155122 					grtail = g;
9433448Sdh155122 				} else {
9443448Sdh155122 					grtail->fg_next = g;
9453448Sdh155122 					grtail = g;
9463448Sdh155122 				}
9473448Sdh155122 			}
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 		if (type == FR_T_CALLFUNC) {
9503448Sdh155122 			printdeadlist(out, set, fb.fr_data, group,
9513448Sdh155122 				      "# callfunc: ");
9520Sstevel@tonic-gate 		}
9533448Sdh155122 	} while (fb.fr_next != NULL);
9543448Sdh155122 
9553448Sdh155122 	while ((g = grtop) != NULL) {
9563448Sdh155122 		printdeadlist(out, set, NULL, g->fg_name, comment);
9573448Sdh155122 		grtop = g->fg_next;
9583448Sdh155122 		free(g);
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate 
9623448Sdh155122 
9630Sstevel@tonic-gate /*
9640Sstevel@tonic-gate  * print out all of the asked for rule sets, using the stats struct as
9650Sstevel@tonic-gate  * the base from which to get the pointers.
9660Sstevel@tonic-gate  */
showlist(fiop)9670Sstevel@tonic-gate static	void	showlist(fiop)
9680Sstevel@tonic-gate struct	friostat	*fiop;
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	struct	frentry	*fp = NULL;
9710Sstevel@tonic-gate 	int	i, set;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	set = fiop->f_active;
9740Sstevel@tonic-gate 	if (opts & OPT_INACTIVE)
9750Sstevel@tonic-gate 		set = 1 - set;
9760Sstevel@tonic-gate 	if (opts & OPT_ACCNT) {
9770Sstevel@tonic-gate #ifdef USE_INET6
9780Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
9790Sstevel@tonic-gate 			i = F_ACOUT;
9800Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout6[set];
9810Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
9820Sstevel@tonic-gate 			i = F_ACIN;
9830Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin6[set];
9840Sstevel@tonic-gate 		} else
9850Sstevel@tonic-gate #endif
9860Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
9870Sstevel@tonic-gate 			i = F_ACOUT;
9880Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout[set];
9890Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
9900Sstevel@tonic-gate 			i = F_ACIN;
9910Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin[set];
9920Sstevel@tonic-gate 		} else {
9930Sstevel@tonic-gate 			FPRINTF(stderr, "No -i or -o given with -a\n");
9940Sstevel@tonic-gate 			return;
9950Sstevel@tonic-gate 		}
9960Sstevel@tonic-gate 	} else {
9970Sstevel@tonic-gate #ifdef	USE_INET6
9980Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
9990Sstevel@tonic-gate 			i = F_OUT;
10000Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout6[set];
10010Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
10020Sstevel@tonic-gate 			i = F_IN;
10030Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin6[set];
10040Sstevel@tonic-gate 		} else
10050Sstevel@tonic-gate #endif
10060Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
10070Sstevel@tonic-gate 			i = F_OUT;
10080Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout[set];
10090Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
10100Sstevel@tonic-gate 			i = F_IN;
10110Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin[set];
10120Sstevel@tonic-gate 		} else
10130Sstevel@tonic-gate 			return;
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
10160Sstevel@tonic-gate 		FPRINTF(stderr, "showlist:opts %#x i %d\n", opts, i);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
10190Sstevel@tonic-gate 		PRINTF("fp %p set %d\n", fp, set);
10200Sstevel@tonic-gate 	if (!fp) {
10210Sstevel@tonic-gate 		FPRINTF(stderr, "empty list for %s%s\n",
10220Sstevel@tonic-gate 			(opts & OPT_INACTIVE) ? "inactive " : "", filters[i]);
10230Sstevel@tonic-gate 		return;
10240Sstevel@tonic-gate 	}
10253448Sdh155122 	if (live_kernel == 1)
10263448Sdh155122 		printlivelist(i, set, fp, NULL, NULL);
10273448Sdh155122 	else
10283448Sdh155122 		printdeadlist(i, set, fp, NULL, NULL);
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate /*
10330Sstevel@tonic-gate  * Display ipfilter stateful filtering information
10340Sstevel@tonic-gate  */
showipstates(ipsp)10350Sstevel@tonic-gate static void showipstates(ipsp)
10360Sstevel@tonic-gate ips_stat_t *ipsp;
10370Sstevel@tonic-gate {
10380Sstevel@tonic-gate 	u_long minlen, maxlen, totallen, *buckets;
10390Sstevel@tonic-gate 	int i, sz;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	sz = sizeof(*buckets) * ipsp->iss_statesize;
10420Sstevel@tonic-gate 	buckets = (u_long *)malloc(sz);
10431448Sschuster 	if (buckets == NULL) {
10441448Sschuster 		perror("malloc");
10451448Sschuster 		exit(1);
10461448Sschuster 	}
10470Sstevel@tonic-gate 	if (kmemcpy((char *)buckets, (u_long)ipsp->iss_bucketlen, sz)) {
10480Sstevel@tonic-gate 		free(buckets);
10490Sstevel@tonic-gate 		return;
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	/*
10530Sstevel@tonic-gate 	 * If a list of states hasn't been asked for, only print out stats
10540Sstevel@tonic-gate 	 */
10550Sstevel@tonic-gate 	if (!(opts & OPT_SHOWLIST)) {
10560Sstevel@tonic-gate 		PRINTF("IP states added:\n\t%lu TCP\n\t%lu UDP\n\t%lu ICMP\n",
10570Sstevel@tonic-gate 			ipsp->iss_tcp, ipsp->iss_udp, ipsp->iss_icmp);
10587704SAlexandr.Nedvedicky@Sun.COM 		PRINTF("\t%lu hits\n\t%lu misses\n",
10597704SAlexandr.Nedvedicky@Sun.COM 			ipsp->iss_hits, ipsp->iss_miss);
10607704SAlexandr.Nedvedicky@Sun.COM 		PRINTF("\t%lu maximum\n\t%lu no memory\n", ipsp->iss_max,
10617704SAlexandr.Nedvedicky@Sun.COM 			ipsp->iss_nomem);
10627432SJohn.Ojemann@Sun.COM 		PRINTF("\t%lu active\n\t%lu expired\n",
10637432SJohn.Ojemann@Sun.COM 			ipsp->iss_active, ipsp->iss_expire);
10647432SJohn.Ojemann@Sun.COM 		PRINTF("\t%lu closed\n\t%u orphans\n",
10657432SJohn.Ojemann@Sun.COM 			ipsp->iss_fin, ipsp->iss_orphans);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		PRINTF("State logging %sabled\n",
10680Sstevel@tonic-gate 			state_logging ? "en" : "dis");
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 		PRINTF("\nState table bucket statistics:\n");
10717704SAlexandr.Nedvedicky@Sun.COM 		PRINTF("\t%lu in use\n\t%lu max bucket\n", ipsp->iss_inuse,
10727704SAlexandr.Nedvedicky@Sun.COM 			ipsp->iss_bucketfull);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		minlen = ipsp->iss_max;
10750Sstevel@tonic-gate 		totallen = 0;
10760Sstevel@tonic-gate 		maxlen = 0;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 		for (i = 0; i < ipsp->iss_statesize; i++) {
10790Sstevel@tonic-gate 			if (buckets[i] > maxlen)
10800Sstevel@tonic-gate 				maxlen = buckets[i];
10810Sstevel@tonic-gate 			if (buckets[i] < minlen)
10820Sstevel@tonic-gate 					minlen = buckets[i];
10830Sstevel@tonic-gate 			totallen += buckets[i];
10840Sstevel@tonic-gate 		}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		PRINTF("\t%2.2f%% bucket usage\n\t%lu minimal length\n",
10870Sstevel@tonic-gate 			((float)ipsp->iss_inuse / ipsp->iss_statesize) * 100.0,
10880Sstevel@tonic-gate 			minlen);
10890Sstevel@tonic-gate 		PRINTF("\t%lu maximal length\n\t%.3f average length\n",
10900Sstevel@tonic-gate 			maxlen,
10910Sstevel@tonic-gate 			ipsp->iss_inuse ? (float) totallen/ ipsp->iss_inuse :
10920Sstevel@tonic-gate 					  0.0);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate #define ENTRIES_PER_LINE 5
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		if (opts & OPT_VERBOSE) {
10970Sstevel@tonic-gate 			PRINTF("\nCurrent bucket sizes :\n");
10980Sstevel@tonic-gate 			for (i = 0; i < ipsp->iss_statesize; i++) {
10992393Syz155240 				if ((i % ENTRIES_PER_LINE) == 0)
11000Sstevel@tonic-gate 					PRINTF("\t");
11010Sstevel@tonic-gate 				PRINTF("%4d -> %4lu", i, buckets[i]);
11020Sstevel@tonic-gate 				if ((i % ENTRIES_PER_LINE) ==
11032393Syz155240 				    (ENTRIES_PER_LINE - 1))
11040Sstevel@tonic-gate 					PRINTF("\n");
11052393Syz155240 				else
11060Sstevel@tonic-gate 					PRINTF("  ");
11070Sstevel@tonic-gate 			}
11080Sstevel@tonic-gate 			PRINTF("\n");
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 		PRINTF("\n");
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		free(buckets);
11130Sstevel@tonic-gate 		return;
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/*
11170Sstevel@tonic-gate 	 * Print out all the state information currently held in the kernel.
11180Sstevel@tonic-gate 	 */
11190Sstevel@tonic-gate 	while (ipsp->iss_list != NULL) {
11202393Syz155240 		ipsp->iss_list = printstate(ipsp->iss_list, opts,
11212393Syz155240 					    ipsp->iss_ticks);
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	free(buckets);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate #ifdef STATETOP
11292393Syz155240 static int handle_resize = 0, handle_break = 0;
11302393Syz155240 
topipstates(saddr,daddr,sport,dport,protocol,ver,refreshtime,topclosed)11312393Syz155240 static void topipstates(saddr, daddr, sport, dport, protocol, ver,
11320Sstevel@tonic-gate 		        refreshtime, topclosed)
11332393Syz155240 i6addr_t saddr;
11342393Syz155240 i6addr_t daddr;
11350Sstevel@tonic-gate int sport;
11360Sstevel@tonic-gate int dport;
11370Sstevel@tonic-gate int protocol;
11382393Syz155240 int ver;
11390Sstevel@tonic-gate int refreshtime;
11400Sstevel@tonic-gate int topclosed;
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	char str1[STSTRSIZE], str2[STSTRSIZE], str3[STSTRSIZE], str4[STSTRSIZE];
11430Sstevel@tonic-gate 	int maxtsentries = 0, reverse = 0, sorting = STSORT_DEFAULT;
11442393Syz155240 	int i, j, winy, tsentry, maxx, maxy, redraw = 0, ret = 0;
11452393Syz155240 	int len, srclen, dstlen, forward = 1, c = 0;
11460Sstevel@tonic-gate 	ips_stat_t ipsst, *ipsstp = &ipsst;
11470Sstevel@tonic-gate 	statetop_t *tstable = NULL, *tp;
11482393Syz155240 	const char *errstr = "";
11490Sstevel@tonic-gate 	ipstate_t ips;
11500Sstevel@tonic-gate 	ipfobj_t ipfo;
11512393Syz155240 	struct timeval selecttimeout;
11520Sstevel@tonic-gate 	char hostnm[HOSTNMLEN];
11530Sstevel@tonic-gate 	struct protoent *proto;
11540Sstevel@tonic-gate 	fd_set readfd;
11550Sstevel@tonic-gate 	time_t t;
11560Sstevel@tonic-gate 
11572393Syz155240 	/* install signal handlers */
11582393Syz155240 	signal(SIGINT, sig_break);
11592393Syz155240 	signal(SIGQUIT, sig_break);
11602393Syz155240 	signal(SIGTERM, sig_break);
11612393Syz155240 	signal(SIGWINCH, sig_resize);
11622393Syz155240 
11630Sstevel@tonic-gate 	/* init ncurses stuff */
11640Sstevel@tonic-gate   	initscr();
11650Sstevel@tonic-gate   	cbreak();
11660Sstevel@tonic-gate   	noecho();
11672393Syz155240 	curs_set(0);
11682393Syz155240 	timeout(0);
11692393Syz155240 	getmaxyx(stdscr, maxy, maxx);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/* init hostname */
11720Sstevel@tonic-gate 	gethostname(hostnm, sizeof(hostnm) - 1);
11730Sstevel@tonic-gate 	hostnm[sizeof(hostnm) - 1] = '\0';
11742393Syz155240 
11750Sstevel@tonic-gate 	/* init ipfobj_t stuff */
11760Sstevel@tonic-gate 	bzero((caddr_t)&ipfo, sizeof(ipfo));
11770Sstevel@tonic-gate 	ipfo.ipfo_rev = IPFILTER_VERSION;
11780Sstevel@tonic-gate 	ipfo.ipfo_size = sizeof(*ipsstp);
11790Sstevel@tonic-gate 	ipfo.ipfo_ptr = (void *)ipsstp;
11800Sstevel@tonic-gate 	ipfo.ipfo_type = IPFOBJ_STATESTAT;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	/* repeat until user aborts */
11830Sstevel@tonic-gate 	while ( 1 ) {
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		/* get state table */
11860Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
11870Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
11882393Syz155240 			errstr = "ioctl(SIOCGETFS)";
11892393Syz155240 			ret = -1;
11902393Syz155240 			goto out;
11910Sstevel@tonic-gate 		}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		/* clear the history */
11940Sstevel@tonic-gate 		tsentry = -1;
11950Sstevel@tonic-gate 
11962393Syz155240 		/* reset max str len */
11972393Syz155240 		srclen = dstlen = 0;
11982393Syz155240 
11990Sstevel@tonic-gate 		/* read the state table and store in tstable */
12002393Syz155240 		for (; ipsstp->iss_list; ipsstp->iss_list = ips.is_next) {
12012393Syz155240 
12020Sstevel@tonic-gate 			if (kmemcpy((char *)&ips, (u_long)ipsstp->iss_list,
12030Sstevel@tonic-gate 				    sizeof(ips)))
12040Sstevel@tonic-gate 				break;
12052393Syz155240 
12062393Syz155240 			if (ips.is_v != ver)
12072393Syz155240 				continue;
12080Sstevel@tonic-gate 
12092393Syz155240 			/* check v4 src/dest addresses */
12102393Syz155240 			if (ips.is_v == 4) {
12112393Syz155240 				if ((saddr.in4.s_addr != INADDR_ANY &&
12122393Syz155240 				     saddr.in4.s_addr != ips.is_saddr) ||
12132393Syz155240 				    (daddr.in4.s_addr != INADDR_ANY &&
12142393Syz155240 				     daddr.in4.s_addr != ips.is_daddr))
12152393Syz155240 					continue;
12162393Syz155240 			}
12172393Syz155240 #ifdef	USE_INET6
12182393Syz155240 			/* check v6 src/dest addresses */
12192393Syz155240 			if (ips.is_v == 6) {
12202393Syz155240 				if ((IP6_NEQ(&saddr, &in6addr_any) &&
12212393Syz155240 				     IP6_NEQ(&saddr, &ips.is_src)) ||
12222393Syz155240 				    (IP6_NEQ(&daddr, &in6addr_any) &&
12232393Syz155240 				     IP6_NEQ(&daddr, &ips.is_dst)))
12242393Syz155240 					continue;
12252393Syz155240 			}
12262393Syz155240 #endif
12272393Syz155240 			/* check protocol */
12282393Syz155240 			if (protocol > 0 && protocol != ips.is_p)
12292393Syz155240 				continue;
12302393Syz155240 
12312393Syz155240 			/* check ports if protocol is TCP or UDP */
12322393Syz155240 			if (((ips.is_p == IPPROTO_TCP) ||
12332393Syz155240 			     (ips.is_p == IPPROTO_UDP)) &&
12342393Syz155240 			   (((sport > 0) && (htons(sport) != ips.is_sport)) ||
12352393Syz155240 			    ((dport > 0) && (htons(dport) != ips.is_dport))))
12362393Syz155240 				continue;
12372393Syz155240 
12382393Syz155240 			/* show closed TCP sessions ? */
12392393Syz155240 			if ((topclosed == 0) && (ips.is_p == IPPROTO_TCP) &&
12402393Syz155240 			    (ips.is_state[0] >= IPF_TCPS_LAST_ACK) &&
12412393Syz155240 			    (ips.is_state[1] >= IPF_TCPS_LAST_ACK))
12422393Syz155240 				continue;
12430Sstevel@tonic-gate 
12442393Syz155240 			/*
12452393Syz155240 			 * if necessary make room for this state
12462393Syz155240 			 * entry
12472393Syz155240 			 */
12482393Syz155240 			tsentry++;
12492393Syz155240 			if (!maxtsentries || tsentry == maxtsentries) {
12502393Syz155240 				maxtsentries += STGROWSIZE;
12512393Syz155240 				tstable = realloc(tstable,
12522393Syz155240 				    maxtsentries * sizeof(statetop_t));
12532393Syz155240 				if (tstable == NULL) {
12542393Syz155240 					perror("realloc");
12552393Syz155240 					exit(-1);
12560Sstevel@tonic-gate 				}
12572393Syz155240 			}
12580Sstevel@tonic-gate 
12592393Syz155240 			/* get max src/dest address string length */
12602393Syz155240 			len = strlen(getip(ips.is_v, &ips.is_src));
12612393Syz155240 			if (srclen < len)
12622393Syz155240 				srclen = len;
12632393Syz155240 			len = strlen(getip(ips.is_v, &ips.is_dst));
12642393Syz155240 			if (dstlen < len)
12652393Syz155240 				dstlen = len;
12660Sstevel@tonic-gate 
12672393Syz155240 			/* fill structure */
12682393Syz155240 			tp = tstable + tsentry;
12692393Syz155240 			tp->st_src = ips.is_src;
12702393Syz155240 			tp->st_dst = ips.is_dst;
12712393Syz155240 			tp->st_p = ips.is_p;
12722393Syz155240 			tp->st_v = ips.is_v;
12732393Syz155240 			tp->st_state[0] = ips.is_state[0];
12742393Syz155240 			tp->st_state[1] = ips.is_state[1];
12752393Syz155240 			if (forward) {
12762393Syz155240 				tp->st_pkts = ips.is_pkts[0]+ips.is_pkts[1];
12772393Syz155240 				tp->st_bytes = ips.is_bytes[0]+ips.is_bytes[1];
12782393Syz155240 			} else {
12792393Syz155240 				tp->st_pkts = ips.is_pkts[2]+ips.is_pkts[3];
12802393Syz155240 				tp->st_bytes = ips.is_bytes[2]+ips.is_bytes[3];
12812393Syz155240 			}
12822393Syz155240 			tp->st_age = ips.is_die - ipsstp->iss_ticks;
12832393Syz155240 			if ((ips.is_p == IPPROTO_TCP) ||
12842393Syz155240 			    (ips.is_p == IPPROTO_UDP)) {
12852393Syz155240 				tp->st_sport = ips.is_sport;
12862393Syz155240 				tp->st_dport = ips.is_dport;
12870Sstevel@tonic-gate 			}
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 		/* sort the array */
12922393Syz155240 		if (tsentry != -1) {
12930Sstevel@tonic-gate 			switch (sorting)
12940Sstevel@tonic-gate 			{
12950Sstevel@tonic-gate 			case STSORT_PR:
12960Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
12970Sstevel@tonic-gate 				      sizeof(statetop_t), sort_p);
12980Sstevel@tonic-gate 				break;
12990Sstevel@tonic-gate 			case STSORT_PKTS:
13000Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13010Sstevel@tonic-gate 				      sizeof(statetop_t), sort_pkts);
13020Sstevel@tonic-gate 				break;
13030Sstevel@tonic-gate 			case STSORT_BYTES:
13040Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13050Sstevel@tonic-gate 				      sizeof(statetop_t), sort_bytes);
13060Sstevel@tonic-gate 				break;
13070Sstevel@tonic-gate 			case STSORT_TTL:
13080Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13090Sstevel@tonic-gate 				      sizeof(statetop_t), sort_ttl);
13100Sstevel@tonic-gate 				break;
13110Sstevel@tonic-gate 			case STSORT_SRCIP:
13120Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13130Sstevel@tonic-gate 				      sizeof(statetop_t), sort_srcip);
13140Sstevel@tonic-gate 				break;
13152393Syz155240 			case STSORT_SRCPT:
13162393Syz155240 				qsort(tstable, tsentry +1,
13172393Syz155240 					sizeof(statetop_t), sort_srcpt);
13182393Syz155240 				break;
13190Sstevel@tonic-gate 			case STSORT_DSTIP:
13200Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13210Sstevel@tonic-gate 				      sizeof(statetop_t), sort_dstip);
13220Sstevel@tonic-gate 				break;
13232393Syz155240 			case STSORT_DSTPT:
13242393Syz155240 				qsort(tstable, tsentry + 1,
13252393Syz155240 				      sizeof(statetop_t), sort_dstpt);
13262393Syz155240 				break;
13270Sstevel@tonic-gate 			default:
13280Sstevel@tonic-gate 				break;
13290Sstevel@tonic-gate 			}
13302393Syz155240 		}
13312393Syz155240 
13322393Syz155240 		/* handle window resizes */
13332393Syz155240 		if (handle_resize) {
13342393Syz155240 			endwin();
13352393Syz155240 			initscr();
13362393Syz155240 			cbreak();
13372393Syz155240 			noecho();
13382393Syz155240 			curs_set(0);
13392393Syz155240 			timeout(0);
13402393Syz155240 			getmaxyx(stdscr, maxy, maxx);
13412393Syz155240 			redraw = 1;
13422393Syz155240 			handle_resize = 0;
13432393Syz155240                 }
13442393Syz155240 
13452393Syz155240 		/* stop program? */
13462393Syz155240 		if (handle_break)
13472393Syz155240 			break;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		/* print title */
13500Sstevel@tonic-gate 		erase();
13510Sstevel@tonic-gate 		attron(A_BOLD);
13522393Syz155240 		winy = 0;
13532393Syz155240 		move(winy,0);
13540Sstevel@tonic-gate 		sprintf(str1, "%s - %s - state top", hostnm, IPL_VERSION);
13550Sstevel@tonic-gate 		for (j = 0 ; j < (maxx - 8 - strlen(str1)) / 2; j++)
13560Sstevel@tonic-gate 			printw(" ");
13570Sstevel@tonic-gate 		printw("%s", str1);
13580Sstevel@tonic-gate 		attroff(A_BOLD);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 		/* just for fun add a clock */
13612393Syz155240 		move(winy, maxx - 8);
13620Sstevel@tonic-gate 		t = time(NULL);
13630Sstevel@tonic-gate 		strftime(str1, 80, "%T", localtime(&t));
13640Sstevel@tonic-gate 		printw("%s\n", str1);
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 		/*
13672393Syz155240 		 * print the display filters, this is placed in the loop,
13680Sstevel@tonic-gate 		 * because someday I might add code for changing these
13690Sstevel@tonic-gate 		 * while the programming is running :-)
13700Sstevel@tonic-gate 		 */
13710Sstevel@tonic-gate 		if (sport >= 0)
13722393Syz155240 			sprintf(str1, "%s,%d", getip(ver, &saddr), sport);
13730Sstevel@tonic-gate 		else
13742393Syz155240 			sprintf(str1, "%s", getip(ver, &saddr));
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 		if (dport >= 0)
13772393Syz155240 			sprintf(str2, "%s,%d", getip(ver, &daddr), dport);
13780Sstevel@tonic-gate 		else
13792393Syz155240 			sprintf(str2, "%s", getip(ver, &daddr));
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 		if (protocol < 0)
13820Sstevel@tonic-gate 			strcpy(str3, "any");
13830Sstevel@tonic-gate 		else if ((proto = getprotobynumber(protocol)) != NULL)
13842393Syz155240 			sprintf(str3, "%s", proto->p_name);
13850Sstevel@tonic-gate 		else
13860Sstevel@tonic-gate 			sprintf(str3, "%d", protocol);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		switch (sorting)
13890Sstevel@tonic-gate 		{
13900Sstevel@tonic-gate 		case STSORT_PR:
13910Sstevel@tonic-gate 			sprintf(str4, "proto");
13920Sstevel@tonic-gate 			break;
13930Sstevel@tonic-gate 		case STSORT_PKTS:
13940Sstevel@tonic-gate 			sprintf(str4, "# pkts");
13950Sstevel@tonic-gate 			break;
13960Sstevel@tonic-gate 		case STSORT_BYTES:
13970Sstevel@tonic-gate 			sprintf(str4, "# bytes");
13980Sstevel@tonic-gate 			break;
13990Sstevel@tonic-gate 		case STSORT_TTL:
14000Sstevel@tonic-gate 			sprintf(str4, "ttl");
14010Sstevel@tonic-gate 			break;
14020Sstevel@tonic-gate 		case STSORT_SRCIP:
14032393Syz155240 			sprintf(str4, "src ip");
14042393Syz155240 			break;
14052393Syz155240 		case STSORT_SRCPT:
14062393Syz155240 			sprintf(str4, "src port");
14070Sstevel@tonic-gate 			break;
14080Sstevel@tonic-gate 		case STSORT_DSTIP:
14092393Syz155240 			sprintf(str4, "dest ip");
14102393Syz155240 			break;
14112393Syz155240 		case STSORT_DSTPT:
14122393Syz155240 			sprintf(str4, "dest port");
14130Sstevel@tonic-gate 			break;
14140Sstevel@tonic-gate 		default:
14150Sstevel@tonic-gate 			sprintf(str4, "unknown");
14160Sstevel@tonic-gate 			break;
14170Sstevel@tonic-gate 		}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 		if (reverse)
14200Sstevel@tonic-gate 			strcat(str4, " (reverse)");
14210Sstevel@tonic-gate 
14222393Syz155240 		winy += 2;
14232393Syz155240 		move(winy,0);
14242393Syz155240 		printw("Src: %s, Dest: %s, Proto: %s, Sorted by: %s\n\n",
14250Sstevel@tonic-gate 		       str1, str2, str3, str4);
14260Sstevel@tonic-gate 
14272393Syz155240 		/*
14282393Syz155240 		 * For an IPv4 IP address we need at most 15 characters,
14292393Syz155240 		 * 4 tuples of 3 digits, separated by 3 dots. Enforce this
14302393Syz155240 		 * length, so the colums do not change positions based
14312393Syz155240 		 * on the size of the IP address. This length makes the
14322393Syz155240 		 * output fit in a 80 column terminal.
14332393Syz155240 		 * We are lacking a good solution for IPv6 addresses (that
14342393Syz155240 		 * can be longer that 15 characters), so we do not enforce
14352393Syz155240 		 * a maximum on the IP field size.
14362393Syz155240 		 */
14372393Syz155240 		if (srclen < 15)
14382393Syz155240 			srclen = 15;
14392393Syz155240 		if (dstlen < 15)
14402393Syz155240 			dstlen = 15;
14412393Syz155240 
14420Sstevel@tonic-gate 		/* print column description */
14432393Syz155240 		winy += 2;
14442393Syz155240 		move(winy,0);
14450Sstevel@tonic-gate 		attron(A_BOLD);
14462393Syz155240 		printw("%-*s %-*s %3s %4s %7s %9s %9s\n",
14472393Syz155240 		       srclen + 6, "Source IP", dstlen + 6, "Destination IP",
14482393Syz155240 		       "ST", "PR", "#pkts", "#bytes", "ttl");
14490Sstevel@tonic-gate 		attroff(A_BOLD);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 		/* print all the entries */
14520Sstevel@tonic-gate 		tp = tstable;
14530Sstevel@tonic-gate 		if (reverse)
14540Sstevel@tonic-gate 			tp += tsentry;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 		if (tsentry > maxy - 6)
14570Sstevel@tonic-gate 			tsentry = maxy - 6;
14580Sstevel@tonic-gate 		for (i = 0; i <= tsentry; i++) {
14590Sstevel@tonic-gate 			/* print src/dest and port */
14600Sstevel@tonic-gate 			if ((tp->st_p == IPPROTO_TCP) ||
14610Sstevel@tonic-gate 			    (tp->st_p == IPPROTO_UDP)) {
14620Sstevel@tonic-gate 				sprintf(str1, "%s,%hu",
14632393Syz155240 					getip(tp->st_v, &tp->st_src),
14640Sstevel@tonic-gate 					ntohs(tp->st_sport));
14650Sstevel@tonic-gate 				sprintf(str2, "%s,%hu",
14662393Syz155240 					getip(tp->st_v, &tp->st_dst),
14670Sstevel@tonic-gate 					ntohs(tp->st_dport));
14680Sstevel@tonic-gate 			} else {
14692393Syz155240 				sprintf(str1, "%s", getip(tp->st_v,
14702393Syz155240 				    &tp->st_src));
14712393Syz155240 				sprintf(str2, "%s", getip(tp->st_v,
14722393Syz155240 				    &tp->st_dst));
14730Sstevel@tonic-gate 			}
14742393Syz155240 			winy++;
14752393Syz155240 			move(winy, 0);
14762393Syz155240 			printw("%-*s %-*s", srclen + 6, str1, dstlen + 6, str2);
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 			/* print state */
14790Sstevel@tonic-gate 			sprintf(str1, "%X/%X", tp->st_state[0],
14800Sstevel@tonic-gate 				tp->st_state[1]);
14810Sstevel@tonic-gate 			printw(" %3s", str1);
14820Sstevel@tonic-gate 
14832393Syz155240 			/* print protocol */
14840Sstevel@tonic-gate 			proto = getprotobynumber(tp->st_p);
14850Sstevel@tonic-gate 			if (proto) {
14860Sstevel@tonic-gate 				strncpy(str1, proto->p_name, 4);
14870Sstevel@tonic-gate 				str1[4] = '\0';
14880Sstevel@tonic-gate 			} else {
14890Sstevel@tonic-gate 				sprintf(str1, "%d", tp->st_p);
14900Sstevel@tonic-gate 			}
14912393Syz155240 			/* just print icmp for IPv6-ICMP */
14922393Syz155240 			if (tp->st_p == IPPROTO_ICMPV6)
14932393Syz155240 				strcpy(str1, "icmp");
14940Sstevel@tonic-gate 			printw(" %4s", str1);
14952393Syz155240 
14962393Syz155240 			/* print #pkt/#bytes */
14970Sstevel@tonic-gate #ifdef	USE_QUAD_T
14980Sstevel@tonic-gate 			printw(" %7qu %9qu", (unsigned long long) tp->st_pkts,
14990Sstevel@tonic-gate 				(unsigned long long) tp->st_bytes);
15000Sstevel@tonic-gate #else
15010Sstevel@tonic-gate 			printw(" %7lu %9lu", tp->st_pkts, tp->st_bytes);
15020Sstevel@tonic-gate #endif
15030Sstevel@tonic-gate 			printw(" %9s", ttl_to_string(tp->st_age));
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 			if (reverse)
15060Sstevel@tonic-gate 				tp--;
15070Sstevel@tonic-gate 			else
15080Sstevel@tonic-gate 				tp++;
15090Sstevel@tonic-gate 		}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		/* screen data structure is filled, now update the screen */
15120Sstevel@tonic-gate 		if (redraw)
15130Sstevel@tonic-gate 			clearok(stdscr,1);
15140Sstevel@tonic-gate 
15152393Syz155240 		if (refresh() == ERR)
15162393Syz155240 			break;
15170Sstevel@tonic-gate 		if (redraw) {
15180Sstevel@tonic-gate 			clearok(stdscr,0);
15190Sstevel@tonic-gate 			redraw = 0;
15200Sstevel@tonic-gate 		}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 		/* wait for key press or a 1 second time out period */
15230Sstevel@tonic-gate 		selecttimeout.tv_sec = refreshtime;
15240Sstevel@tonic-gate 		selecttimeout.tv_usec = 0;
15250Sstevel@tonic-gate 		FD_ZERO(&readfd);
15260Sstevel@tonic-gate 		FD_SET(0, &readfd);
15270Sstevel@tonic-gate 		select(1, &readfd, NULL, NULL, &selecttimeout);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		/* if key pressed, read all waiting keys */
15300Sstevel@tonic-gate 		if (FD_ISSET(0, &readfd)) {
15310Sstevel@tonic-gate 			c = wgetch(stdscr);
15320Sstevel@tonic-gate 			if (c == ERR)
15330Sstevel@tonic-gate 				continue;
15340Sstevel@tonic-gate 
15352393Syz155240 			if (ISALPHA(c) && ISUPPER(c))
15362393Syz155240 				c = TOLOWER(c);
15372393Syz155240 			if (c == 'l') {
15380Sstevel@tonic-gate 				redraw = 1;
15392393Syz155240 			} else if (c == 'q') {
15401448Sschuster 				break;
15412393Syz155240 			} else if (c == 'r') {
15420Sstevel@tonic-gate 				reverse = !reverse;
15432393Syz155240 			} else if (c == 'b') {
15442393Syz155240 				forward = 0;
15452393Syz155240 			} else if (c == 'f') {
15462393Syz155240 				forward = 1;
15472393Syz155240 			} else if (c == 's') {
15482393Syz155240 				if (++sorting > STSORT_MAX)
15490Sstevel@tonic-gate 					sorting = 0;
15500Sstevel@tonic-gate 			}
15510Sstevel@tonic-gate 		}
15520Sstevel@tonic-gate 	} /* while */
15530Sstevel@tonic-gate 
15542393Syz155240 out:
15550Sstevel@tonic-gate 	printw("\n");
15564985San207044 	refresh();
15570Sstevel@tonic-gate 	endwin();
15582393Syz155240 	free(tstable);
15592393Syz155240 	if (ret != 0)
15602393Syz155240 		perror(errstr);
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate #endif
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate /*
15660Sstevel@tonic-gate  * Show fragment cache information that's held in the kernel.
15670Sstevel@tonic-gate  */
showfrstates(ifsp,ticks)1568*8463SDarren.Reed@Sun.COM static void showfrstates(ifsp, ticks)
15690Sstevel@tonic-gate ipfrstat_t *ifsp;
1570*8463SDarren.Reed@Sun.COM u_long ticks;
15710Sstevel@tonic-gate {
15720Sstevel@tonic-gate 	struct ipfr *ipfrtab[IPFT_SIZE], ifr;
15730Sstevel@tonic-gate 	int i;
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * print out the numeric statistics
15770Sstevel@tonic-gate 	 */
15780Sstevel@tonic-gate 	PRINTF("IP fragment states:\n\t%lu new\n\t%lu expired\n\t%lu hits\n",
15790Sstevel@tonic-gate 		ifsp->ifs_new, ifsp->ifs_expire, ifsp->ifs_hits);
15802393Syz155240 	PRINTF("\t%lu retrans\n\t%lu too short\n",
15812393Syz155240 		ifsp->ifs_retrans0, ifsp->ifs_short);
15820Sstevel@tonic-gate 	PRINTF("\t%lu no memory\n\t%lu already exist\n",
15830Sstevel@tonic-gate 		ifsp->ifs_nomem, ifsp->ifs_exists);
15840Sstevel@tonic-gate 	PRINTF("\t%lu inuse\n", ifsp->ifs_inuse);
15850Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_table, sizeof(ipfrtab)))
15860Sstevel@tonic-gate 		return;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	/*
15890Sstevel@tonic-gate 	 * Print out the contents (if any) of the fragment cache table.
15900Sstevel@tonic-gate 	 */
15912393Syz155240 	PRINTF("\n");
15920Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
15932393Syz155240 		while (ipfrtab[i] != NULL) {
15940Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
15950Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
15960Sstevel@tonic-gate 				break;
1597*8463SDarren.Reed@Sun.COM 			ifr.ipfr_ttl -= ticks;
15982393Syz155240 			printfraginfo("", &ifr);
15990Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
16000Sstevel@tonic-gate 		}
16012393Syz155240 	/*
16022393Syz155240 	 * Print out the contents (if any) of the NAT fragment cache table.
16032393Syz155240 	 */
16040Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_nattab,sizeof(ipfrtab)))
16050Sstevel@tonic-gate 		return;
16060Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
16072393Syz155240 		while (ipfrtab[i] != NULL) {
16080Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
16090Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
16100Sstevel@tonic-gate 				break;
1611*8463SDarren.Reed@Sun.COM 			ifr.ipfr_ttl -= ticks;
16122393Syz155240 			printfraginfo("NAT: ", &ifr);
16130Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
16140Sstevel@tonic-gate 		}
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate /*
16190Sstevel@tonic-gate  * Show stats on how auth within IPFilter has been used
16200Sstevel@tonic-gate  */
showauthstates(asp)16210Sstevel@tonic-gate static void showauthstates(asp)
16220Sstevel@tonic-gate fr_authstat_t *asp;
16230Sstevel@tonic-gate {
16240Sstevel@tonic-gate 	frauthent_t *frap, fra;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate #ifdef	USE_QUAD_T
16270Sstevel@tonic-gate 	printf("Authorisation hits: %qu\tmisses %qu\n",
16280Sstevel@tonic-gate 		(unsigned long long) asp->fas_hits,
16290Sstevel@tonic-gate 		(unsigned long long) asp->fas_miss);
16300Sstevel@tonic-gate #else
16310Sstevel@tonic-gate 	printf("Authorisation hits: %ld\tmisses %ld\n", asp->fas_hits,
16320Sstevel@tonic-gate 		asp->fas_miss);
16330Sstevel@tonic-gate #endif
16340Sstevel@tonic-gate 	printf("nospace %ld\nadded %ld\nsendfail %ld\nsendok %ld\n",
16350Sstevel@tonic-gate 		asp->fas_nospace, asp->fas_added, asp->fas_sendfail,
16360Sstevel@tonic-gate 		asp->fas_sendok);
16370Sstevel@tonic-gate 	printf("queok %ld\nquefail %ld\nexpire %ld\n",
16380Sstevel@tonic-gate 		asp->fas_queok, asp->fas_quefail, asp->fas_expire);
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	frap = asp->fas_faelist;
16410Sstevel@tonic-gate 	while (frap) {
16420Sstevel@tonic-gate 		if (kmemcpy((char *)&fra, (u_long)frap, sizeof(fra)) == -1)
16430Sstevel@tonic-gate 			break;
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 		printf("age %ld\t", fra.fae_age);
16460Sstevel@tonic-gate 		printfr(&fra.fae_fr, ioctl);
16470Sstevel@tonic-gate 		frap = fra.fae_next;
16480Sstevel@tonic-gate 	}
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate /*
16530Sstevel@tonic-gate  * Display groups used for each of filter rules, accounting rules and
16540Sstevel@tonic-gate  * authentication, separately.
16550Sstevel@tonic-gate  */
showgroups(fiop)16560Sstevel@tonic-gate static void showgroups(fiop)
16570Sstevel@tonic-gate struct friostat	*fiop;
16580Sstevel@tonic-gate {
16590Sstevel@tonic-gate 	static char *gnames[3] = { "Filter", "Accounting", "Authentication" };
16602393Syz155240 	static int gnums[3] = { IPL_LOGIPF, IPL_LOGCOUNT, IPL_LOGAUTH };
16610Sstevel@tonic-gate 	frgroup_t *fp, grp;
16620Sstevel@tonic-gate 	int on, off, i;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	on = fiop->f_active;
16650Sstevel@tonic-gate 	off = 1 - on;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
16680Sstevel@tonic-gate 		printf("%s groups (active):\n", gnames[i]);
16692393Syz155240 		for (fp = fiop->f_groups[gnums[i]][on]; fp != NULL;
16702393Syz155240 		     fp = grp.fg_next)
16710Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
16720Sstevel@tonic-gate 				break;
16730Sstevel@tonic-gate 			else
16740Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
16750Sstevel@tonic-gate 		printf("%s groups (inactive):\n", gnames[i]);
16762393Syz155240 		for (fp = fiop->f_groups[gnums[i]][off]; fp != NULL;
16772393Syz155240 		     fp = grp.fg_next)
16780Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
16790Sstevel@tonic-gate 				break;
16800Sstevel@tonic-gate 			else
16810Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
16820Sstevel@tonic-gate 	}
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
parse_ipportstr(argument,ip,port)16850Sstevel@tonic-gate static void parse_ipportstr(argument, ip, port)
16860Sstevel@tonic-gate const char *argument;
16872393Syz155240 i6addr_t *ip;
16880Sstevel@tonic-gate int *port;
16890Sstevel@tonic-gate {
16900Sstevel@tonic-gate 	char *s, *comma;
16912393Syz155240 	int ok = 0;
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	/* make working copy of argument, Theoretically you must be able
16940Sstevel@tonic-gate 	 * to write to optarg, but that seems very ugly to me....
16950Sstevel@tonic-gate 	 */
16960Sstevel@tonic-gate 	s = strdup(argument);
16970Sstevel@tonic-gate 	if (s == NULL)
16980Sstevel@tonic-gate 		return;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	/* get port */
17010Sstevel@tonic-gate 	if ((comma = strchr(s, ',')) != NULL) {
17020Sstevel@tonic-gate 		if (!strcasecmp(comma + 1, "any")) {
17030Sstevel@tonic-gate 			*port = -1;
17040Sstevel@tonic-gate 		} else if (!sscanf(comma + 1, "%d", port) ||
17050Sstevel@tonic-gate 			   (*port < 0) || (*port > 65535)) {
17060Sstevel@tonic-gate 			fprintf(stderr, "Invalid port specfication in %s\n",
17070Sstevel@tonic-gate 				argument);
17080Sstevel@tonic-gate 			free(s);
17090Sstevel@tonic-gate 			exit(-2);
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 		*comma = '\0';
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/* get ip address */
17160Sstevel@tonic-gate 	if (!strcasecmp(s, "any")) {
17172393Syz155240 		ip->in4.s_addr = INADDR_ANY;
17182393Syz155240 #ifdef	USE_INET6
17192393Syz155240 		ip->in6 = in6addr_any;
17202393Syz155240 	} else if (use_inet6 && inet_pton(AF_INET6, s, &ip->in6)) {
17212393Syz155240 		ok = 1;
17222393Syz155240 #endif
17232393Syz155240 	} else if (inet_aton(s, &ip->in4))
17242393Syz155240 		ok = 1;
17252393Syz155240 
17262393Syz155240 	if (ok == 0) {
17270Sstevel@tonic-gate 		fprintf(stderr, "Invalid IP address: %s\n", s);
17280Sstevel@tonic-gate 		free(s);
17290Sstevel@tonic-gate 		exit(-2);
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	/* free allocated memory */
17330Sstevel@tonic-gate 	free(s);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate #ifdef STATETOP
sig_resize(s)17382393Syz155240 static void sig_resize(s)
17392393Syz155240 int s;
17402393Syz155240 {
17412393Syz155240 	handle_resize = 1;
17422393Syz155240 }
17432393Syz155240 
sig_break(s)17442393Syz155240 static void sig_break(s)
17452393Syz155240 int s;
17462393Syz155240 {
17472393Syz155240 	handle_break = 1;
17482393Syz155240 }
17492393Syz155240 
getip(v,addr)17502393Syz155240 static char *getip(v, addr)
17512393Syz155240 int v;
17522393Syz155240 i6addr_t *addr;
17532393Syz155240 {
17542393Syz155240 #ifdef  USE_INET6
17552393Syz155240 	static char hostbuf[MAXHOSTNAMELEN+1];
17562393Syz155240 #endif
17572393Syz155240 
17582393Syz155240 	if (v == 4)
17592393Syz155240 		return inet_ntoa(addr->in4);
17602393Syz155240 
17612393Syz155240 #ifdef  USE_INET6
17622393Syz155240 	(void) inet_ntop(AF_INET6, &addr->in6, hostbuf, sizeof(hostbuf) - 1);
17632393Syz155240 	hostbuf[MAXHOSTNAMELEN] = '\0';
17642393Syz155240 	return hostbuf;
17652393Syz155240 #else
17662393Syz155240 	return "IPv6";
17672393Syz155240 #endif
17682393Syz155240 }
17692393Syz155240 
17700Sstevel@tonic-gate 
ttl_to_string(ttl)17710Sstevel@tonic-gate static char *ttl_to_string(ttl)
17720Sstevel@tonic-gate long int ttl;
17730Sstevel@tonic-gate {
17742393Syz155240 	static char ttlbuf[STSTRSIZE];
17750Sstevel@tonic-gate 	int hours, minutes, seconds;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/* ttl is in half seconds */
17780Sstevel@tonic-gate 	ttl /= 2;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	hours = ttl / 3600;
17810Sstevel@tonic-gate 	ttl = ttl % 3600;
17820Sstevel@tonic-gate 	minutes = ttl / 60;
17830Sstevel@tonic-gate 	seconds = ttl % 60;
17840Sstevel@tonic-gate 
17852393Syz155240 	if (hours > 0)
17860Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d:%02d", hours, minutes, seconds);
17870Sstevel@tonic-gate 	else
17880Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d", minutes, seconds);
17890Sstevel@tonic-gate 	return ttlbuf;
17900Sstevel@tonic-gate }
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 
sort_pkts(a,b)17930Sstevel@tonic-gate static int sort_pkts(a, b)
17940Sstevel@tonic-gate const void *a;
17950Sstevel@tonic-gate const void *b;
17960Sstevel@tonic-gate {
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	register const statetop_t *ap = a;
17990Sstevel@tonic-gate 	register const statetop_t *bp = b;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	if (ap->st_pkts == bp->st_pkts)
18020Sstevel@tonic-gate 		return 0;
18030Sstevel@tonic-gate 	else if (ap->st_pkts < bp->st_pkts)
18040Sstevel@tonic-gate 		return 1;
18050Sstevel@tonic-gate 	return -1;
18060Sstevel@tonic-gate }
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 
sort_bytes(a,b)18090Sstevel@tonic-gate static int sort_bytes(a, b)
18100Sstevel@tonic-gate const void *a;
18110Sstevel@tonic-gate const void *b;
18120Sstevel@tonic-gate {
18130Sstevel@tonic-gate 	register const statetop_t *ap = a;
18140Sstevel@tonic-gate 	register const statetop_t *bp = b;
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	if (ap->st_bytes == bp->st_bytes)
18170Sstevel@tonic-gate 		return 0;
18180Sstevel@tonic-gate 	else if (ap->st_bytes < bp->st_bytes)
18190Sstevel@tonic-gate 		return 1;
18200Sstevel@tonic-gate 	return -1;
18210Sstevel@tonic-gate }
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 
sort_p(a,b)18240Sstevel@tonic-gate static int sort_p(a, b)
18250Sstevel@tonic-gate const void *a;
18260Sstevel@tonic-gate const void *b;
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	register const statetop_t *ap = a;
18290Sstevel@tonic-gate 	register const statetop_t *bp = b;
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	if (ap->st_p == bp->st_p)
18320Sstevel@tonic-gate 		return 0;
18330Sstevel@tonic-gate 	else if (ap->st_p < bp->st_p)
18340Sstevel@tonic-gate 		return 1;
18350Sstevel@tonic-gate 	return -1;
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 
sort_ttl(a,b)18390Sstevel@tonic-gate static int sort_ttl(a, b)
18400Sstevel@tonic-gate const void *a;
18410Sstevel@tonic-gate const void *b;
18420Sstevel@tonic-gate {
18430Sstevel@tonic-gate 	register const statetop_t *ap = a;
18440Sstevel@tonic-gate 	register const statetop_t *bp = b;
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	if (ap->st_age == bp->st_age)
18470Sstevel@tonic-gate 		return 0;
18480Sstevel@tonic-gate 	else if (ap->st_age < bp->st_age)
18490Sstevel@tonic-gate 		return 1;
18500Sstevel@tonic-gate 	return -1;
18510Sstevel@tonic-gate }
18520Sstevel@tonic-gate 
sort_srcip(a,b)18530Sstevel@tonic-gate static int sort_srcip(a, b)
18540Sstevel@tonic-gate const void *a;
18550Sstevel@tonic-gate const void *b;
18560Sstevel@tonic-gate {
18570Sstevel@tonic-gate 	register const statetop_t *ap = a;
18580Sstevel@tonic-gate 	register const statetop_t *bp = b;
18590Sstevel@tonic-gate 
18602393Syz155240 #ifdef USE_INET6
18612393Syz155240 	if (use_inet6) {
18622393Syz155240 		if (IP6_EQ(&ap->st_src, &bp->st_src))
18632393Syz155240 			return 0;
18642393Syz155240 		else if (IP6_GT(&ap->st_src, &bp->st_src))
18652393Syz155240 			return 1;
18662393Syz155240 	} else
18672393Syz155240 #endif
18682393Syz155240 	{
18692393Syz155240 		if (ntohl(ap->st_src.in4.s_addr) ==
18702393Syz155240 		    ntohl(bp->st_src.in4.s_addr))
18712393Syz155240 			return 0;
18722393Syz155240 		else if (ntohl(ap->st_src.in4.s_addr) >
18732393Syz155240 		         ntohl(bp->st_src.in4.s_addr))
18742393Syz155240 			return 1;
18752393Syz155240 	}
18762393Syz155240 	return -1;
18772393Syz155240 }
18782393Syz155240 
sort_srcpt(a,b)18792393Syz155240 static int sort_srcpt(a, b)
18802393Syz155240 const void *a;
18812393Syz155240 const void *b;
18822393Syz155240 {
18832393Syz155240 	register const statetop_t *ap = a;
18842393Syz155240 	register const statetop_t *bp = b;
18852393Syz155240 
18862393Syz155240 	if (htons(ap->st_sport) == htons(bp->st_sport))
18870Sstevel@tonic-gate 		return 0;
18882393Syz155240 	else if (htons(ap->st_sport) > htons(bp->st_sport))
18890Sstevel@tonic-gate 		return 1;
18900Sstevel@tonic-gate 	return -1;
18910Sstevel@tonic-gate }
18920Sstevel@tonic-gate 
sort_dstip(a,b)18930Sstevel@tonic-gate static int sort_dstip(a, b)
18940Sstevel@tonic-gate const void *a;
18950Sstevel@tonic-gate const void *b;
18960Sstevel@tonic-gate {
18970Sstevel@tonic-gate 	register const statetop_t *ap = a;
18980Sstevel@tonic-gate 	register const statetop_t *bp = b;
18990Sstevel@tonic-gate 
19002393Syz155240 #ifdef USE_INET6
19012393Syz155240 	if (use_inet6) {
19022393Syz155240 		if (IP6_EQ(&ap->st_dst, &bp->st_dst))
19032393Syz155240 			return 0;
19042393Syz155240 		else if (IP6_GT(&ap->st_dst, &bp->st_dst))
19052393Syz155240 			return 1;
19062393Syz155240 	} else
19072393Syz155240 #endif
19082393Syz155240 	{
19092393Syz155240 		if (ntohl(ap->st_dst.in4.s_addr) ==
19102393Syz155240 		    ntohl(bp->st_dst.in4.s_addr))
19112393Syz155240 			return 0;
19122393Syz155240 		else if (ntohl(ap->st_dst.in4.s_addr) >
19132393Syz155240 		         ntohl(bp->st_dst.in4.s_addr))
19142393Syz155240 			return 1;
19152393Syz155240 	}
19162393Syz155240 	return -1;
19172393Syz155240 }
19182393Syz155240 
sort_dstpt(a,b)19192393Syz155240 static int sort_dstpt(a, b)
19202393Syz155240 const void *a;
19212393Syz155240 const void *b;
19222393Syz155240 {
19232393Syz155240 	register const statetop_t *ap = a;
19242393Syz155240 	register const statetop_t *bp = b;
19252393Syz155240 
19262393Syz155240 	if (htons(ap->st_dport) == htons(bp->st_dport))
19270Sstevel@tonic-gate 		return 0;
19282393Syz155240 	else if (htons(ap->st_dport) > htons(bp->st_dport))
19290Sstevel@tonic-gate 		return 1;
19300Sstevel@tonic-gate 	return -1;
19310Sstevel@tonic-gate }
19322393Syz155240 
19330Sstevel@tonic-gate #endif
1934