xref: /illumos-gate/usr/src/cmd/ipf/tools/ipfstat.c (revision ef1266ef)
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  *
6786c7074Sjojemann  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
77c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
894bdecd9SRob Gulewich  *
994bdecd9SRob Gulewich  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
107c478bd9Sstevel@tonic-gate  */
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>
23ab25eeb5Syz #ifdef linux
24ab25eeb5Syz # include <linux/a.out.h>
25ab25eeb5Syz #else
26ab25eeb5Syz # include <nlist.h>
27ab25eeb5Syz #endif
287c478bd9Sstevel@tonic-gate #include <ctype.h>
29ab25eeb5Syz #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
30ab25eeb5Syz # include <stddef.h>
31ab25eeb5Syz #endif
327c478bd9Sstevel@tonic-gate #include "ipf.h"
33ab25eeb5Syz #include "netinet/ipl.h"
347c478bd9Sstevel@tonic-gate #if defined(STATETOP)
357c478bd9Sstevel@tonic-gate # if defined(_BSDI_VERSION)
36ab25eeb5Syz #  undef STATETOP
377c478bd9Sstevel@tonic-gate # endif
387c478bd9Sstevel@tonic-gate # if defined(__FreeBSD__) && \
397c478bd9Sstevel@tonic-gate      (!defined(__FreeBSD_version) || (__FreeBSD_version < 430000))
407c478bd9Sstevel@tonic-gate #  undef STATETOP
417c478bd9Sstevel@tonic-gate # endif
427c478bd9Sstevel@tonic-gate # if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105000000)
437c478bd9Sstevel@tonic-gate #  undef STATETOP
447c478bd9Sstevel@tonic-gate # endif
457c478bd9Sstevel@tonic-gate # if defined(sun)
467c478bd9Sstevel@tonic-gate #  if defined(__svr4__) || defined(__SVR4)
477c478bd9Sstevel@tonic-gate #   include <sys/select.h>
487c478bd9Sstevel@tonic-gate #  else
497c478bd9Sstevel@tonic-gate #   undef STATETOP	/* NOT supported on SunOS4 */
507c478bd9Sstevel@tonic-gate #  endif
517c478bd9Sstevel@tonic-gate # endif
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate #if defined(STATETOP) && !defined(linux)
547c478bd9Sstevel@tonic-gate # include <netinet/ip_var.h>
557c478bd9Sstevel@tonic-gate # include <netinet/tcp_fsm.h>
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate #ifdef STATETOP
58ab25eeb5Syz # include <ctype.h>
59ab25eeb5Syz # include <signal.h>
60af5f29ddSToomas Soome # if defined(SOLARIS) || defined(__NetBSD__) || defined(_BSDI_VERSION) || \
617c478bd9Sstevel@tonic-gate      defined(__sgi)
627c478bd9Sstevel@tonic-gate #  ifdef ERR
637c478bd9Sstevel@tonic-gate #   undef ERR
647c478bd9Sstevel@tonic-gate #  endif
65ab25eeb5Syz #  undef ISASCII
66ab25eeb5Syz #  undef ISPRINT
677c478bd9Sstevel@tonic-gate #  include <curses.h>
687c478bd9Sstevel@tonic-gate # else /* SOLARIS */
697c478bd9Sstevel@tonic-gate #  include <ncurses.h>
707c478bd9Sstevel@tonic-gate # endif /* SOLARIS */
717c478bd9Sstevel@tonic-gate #endif /* STATETOP */
727c478bd9Sstevel@tonic-gate #include "kmem.h"
737c478bd9Sstevel@tonic-gate #if defined(__NetBSD__) || (__OpenBSD__)
747c478bd9Sstevel@tonic-gate # include <paths.h>
757c478bd9Sstevel@tonic-gate #endif
7694bdecd9SRob Gulewich #include "ipfzone.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #if !defined(lint)
797c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)fils.c	1.21 4/20/96 (C) 1993-2000 Darren Reed";
80ab25eeb5Syz static const char rcsid[] = "@(#)$Id: ipfstat.c,v 1.44.2.12 2005/06/12 07:18:46 darrenr Exp $";
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #ifdef __hpux
847c478bd9Sstevel@tonic-gate # define	nlist	nlist64
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate extern	char	*optarg;
887c478bd9Sstevel@tonic-gate extern	int	optind;
89ab25eeb5Syz extern	int	opterr;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	PRINTF	(void)printf
927c478bd9Sstevel@tonic-gate #define	FPRINTF	(void)fprintf
937c478bd9Sstevel@tonic-gate static	char	*filters[4] = { "ipfilter(in)", "ipfilter(out)",
947c478bd9Sstevel@tonic-gate 				"ipacct(in)", "ipacct(out)" };
957c478bd9Sstevel@tonic-gate static	int	state_logging = -1;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int	opts = 0;
987c478bd9Sstevel@tonic-gate int	use_inet6 = 0;
997c478bd9Sstevel@tonic-gate int	live_kernel = 1;
1007c478bd9Sstevel@tonic-gate int	state_fd = -1;
1017c478bd9Sstevel@tonic-gate int	ipf_fd = -1;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #ifdef STATETOP
1047c478bd9Sstevel@tonic-gate #define	STSTRSIZE 	80
1057c478bd9Sstevel@tonic-gate #define	STGROWSIZE	16
1067c478bd9Sstevel@tonic-gate #define	HOSTNMLEN	40
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	STSORT_PR	0
1097c478bd9Sstevel@tonic-gate #define	STSORT_PKTS	1
1107c478bd9Sstevel@tonic-gate #define	STSORT_BYTES	2
1117c478bd9Sstevel@tonic-gate #define	STSORT_TTL	3
1127c478bd9Sstevel@tonic-gate #define	STSORT_SRCIP	4
113ab25eeb5Syz #define	STSORT_SRCPT	5
114ab25eeb5Syz #define	STSORT_DSTIP	6
115ab25eeb5Syz #define	STSORT_DSTPT	7
116ab25eeb5Syz #define	STSORT_MAX	STSORT_DSTPT
1177c478bd9Sstevel@tonic-gate #define	STSORT_DEFAULT	STSORT_BYTES
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate typedef struct statetop {
1217c478bd9Sstevel@tonic-gate 	i6addr_t	st_src;
1227c478bd9Sstevel@tonic-gate 	i6addr_t	st_dst;
1237c478bd9Sstevel@tonic-gate 	u_short		st_sport;
1247c478bd9Sstevel@tonic-gate 	u_short 	st_dport;
1257c478bd9Sstevel@tonic-gate 	u_char		st_p;
126ab25eeb5Syz 	u_char		st_v;
1277c478bd9Sstevel@tonic-gate 	u_char		st_state[2];
1287c478bd9Sstevel@tonic-gate 	U_QUAD_T	st_pkts;
1297c478bd9Sstevel@tonic-gate 	U_QUAD_T	st_bytes;
1307c478bd9Sstevel@tonic-gate 	u_long		st_age;
1317c478bd9Sstevel@tonic-gate } statetop_t;
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
134ab25eeb5Syz int	main __P((int, char *[]));
135ab25eeb5Syz 
1367c478bd9Sstevel@tonic-gate static	void	showstats __P((friostat_t *, u_32_t));
13743412a42SDarren Reed static	void	showfrstates __P((ipfrstat_t *, u_long));
1387c478bd9Sstevel@tonic-gate static	void	showlist __P((friostat_t *));
1397c478bd9Sstevel@tonic-gate static	void	showipstates __P((ips_stat_t *));
1407c478bd9Sstevel@tonic-gate static	void	showauthstates __P((fr_authstat_t *));
1417c478bd9Sstevel@tonic-gate static	void	showgroups __P((friostat_t *));
142ab25eeb5Syz static	void	usage __P((char *));
143f4b3ec61Sdh static	void	printlivelist __P((int, int, frentry_t *, char *, char *));
144f4b3ec61Sdh static	void	printdeadlist __P((int, int, frentry_t *, char *, char *));
1457c478bd9Sstevel@tonic-gate static	void	printlist __P((frentry_t *, char *));
146ab25eeb5Syz static	void	parse_ipportstr __P((const char *, i6addr_t *, int *));
1477c478bd9Sstevel@tonic-gate static	void	ipfstate_live __P((char *, friostat_t **, ips_stat_t **,
1487c478bd9Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1497c478bd9Sstevel@tonic-gate static	void	ipfstate_dead __P((char *, friostat_t **, ips_stat_t **,
1507c478bd9Sstevel@tonic-gate 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
1517c478bd9Sstevel@tonic-gate #ifdef STATETOP
152ab25eeb5Syz static	void	topipstates __P((i6addr_t, i6addr_t, int, int, int,
153ab25eeb5Syz 				 int, int, int));
154ab25eeb5Syz static	void	sig_break __P((int));
155ab25eeb5Syz static	void	sig_resize __P((int));
156ab25eeb5Syz static	char	*getip __P((int, i6addr_t *));
1577c478bd9Sstevel@tonic-gate static	char	*ttl_to_string __P((long));
1587c478bd9Sstevel@tonic-gate static	int	sort_p __P((const void *, const void *));
1597c478bd9Sstevel@tonic-gate static	int	sort_pkts __P((const void *, const void *));
1607c478bd9Sstevel@tonic-gate static	int	sort_bytes __P((const void *, const void *));
1617c478bd9Sstevel@tonic-gate static	int	sort_ttl __P((const void *, const void *));
1627c478bd9Sstevel@tonic-gate static	int	sort_srcip __P((const void *, const void *));
163ab25eeb5Syz static	int	sort_srcpt __P((const void *, const void *));
1647c478bd9Sstevel@tonic-gate static	int	sort_dstip __P((const void *, const void *));
165ab25eeb5Syz static	int	sort_dstpt __P((const void *, const void *));
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
169ab25eeb5Syz static void usage(name)
1707c478bd9Sstevel@tonic-gate char *name;
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate #ifdef  USE_INET6
173ab25eeb5Syz 	fprintf(stderr, "Usage: %s [-6aAdfghIilnoRsv]\n", name);
1747c478bd9Sstevel@tonic-gate #else
175ab25eeb5Syz 	fprintf(stderr, "Usage: %s [-aAdfghIilnoRsv]\n", name);
1767c478bd9Sstevel@tonic-gate #endif
177ab25eeb5Syz 	fprintf(stderr, "       %s [-M corefile] [-N symbol-list]\n", name);
178ab25eeb5Syz #ifdef	USE_INET6
179ab25eeb5Syz 	fprintf(stderr, "       %s -t [-6C] ", name);
180ab25eeb5Syz #else
181ab25eeb5Syz 	fprintf(stderr, "       %s -t [-C] ", name);
182ab25eeb5Syz #endif
18394bdecd9SRob Gulewich 	fprintf(stderr, "[-G|-z zonename] ");
184ab25eeb5Syz 	fprintf(stderr, "[-D destination address] [-P protocol] [-S source address] [-T refresh time]\n");
1857c478bd9Sstevel@tonic-gate 	exit(1);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate int main(argc,argv)
1907c478bd9Sstevel@tonic-gate int argc;
1917c478bd9Sstevel@tonic-gate char *argv[];
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	fr_authstat_t	frauthst;
1947c478bd9Sstevel@tonic-gate 	fr_authstat_t	*frauthstp = &frauthst;
1957c478bd9Sstevel@tonic-gate 	friostat_t fio;
1967c478bd9Sstevel@tonic-gate 	friostat_t *fiop = &fio;
1977c478bd9Sstevel@tonic-gate 	ips_stat_t ipsst;
1987c478bd9Sstevel@tonic-gate 	ips_stat_t *ipsstp = &ipsst;
1997c478bd9Sstevel@tonic-gate 	ipfrstat_t ifrst;
2007c478bd9Sstevel@tonic-gate 	ipfrstat_t *ifrstp = &ifrst;
2017c478bd9Sstevel@tonic-gate 	char	*device = IPL_NAME, *memf = NULL;
202ab25eeb5Syz 	char	*options, *kern = NULL;
2037c478bd9Sstevel@tonic-gate 	int	c, myoptind;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	int protocol = -1;		/* -1 = wild card for any protocol */
2067c478bd9Sstevel@tonic-gate 	int refreshtime = 1; 		/* default update time */
2077c478bd9Sstevel@tonic-gate 	int sport = -1;			/* -1 = wild card for any source port */
2087c478bd9Sstevel@tonic-gate 	int dport = -1;			/* -1 = wild card for any dest port */
2097c478bd9Sstevel@tonic-gate 	int topclosed = 0;		/* do not show closed tcp sessions */
210ab25eeb5Syz 	i6addr_t saddr, daddr;
2117c478bd9Sstevel@tonic-gate 	u_32_t frf;
2127c478bd9Sstevel@tonic-gate 
213ab25eeb5Syz #ifdef	USE_INET6
21494bdecd9SRob Gulewich 	options = "6aACdfgG:hIilnostvD:M:N:P:RS:T:z:";
215ab25eeb5Syz #else
21694bdecd9SRob Gulewich 	options = "aACdfgG:hIilnostvD:M:N:P:RS:T:z:";
217ab25eeb5Syz #endif
218ab25eeb5Syz 
219ab25eeb5Syz 	saddr.in4.s_addr = INADDR_ANY; 	/* default any v4 source addr */
220ab25eeb5Syz 	daddr.in4.s_addr = INADDR_ANY; 	/* default any v4 dest addr */
221ab25eeb5Syz #ifdef	USE_INET6
222ab25eeb5Syz 	saddr.in6 = in6addr_any;	/* default any v6 source addr */
223ab25eeb5Syz 	daddr.in6 = in6addr_any;	/* default any v6 dest addr */
224ab25eeb5Syz #endif
225ab25eeb5Syz 
226ab25eeb5Syz 	/* Don't warn about invalid flags when we run getopt for the 1st time */
227ab25eeb5Syz 	opterr = 0;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
23094bdecd9SRob Gulewich 	 * Parse these four arguments now lest there be any buffer overflows
2317c478bd9Sstevel@tonic-gate 	 * in the parsing of the rest.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	myoptind = optind;
234ab25eeb5Syz 	while ((c = getopt(argc, argv, options)) != -1) {
2357c478bd9Sstevel@tonic-gate 		switch (c)
2367c478bd9Sstevel@tonic-gate 		{
23794bdecd9SRob Gulewich 		case 'G' :
23894bdecd9SRob Gulewich 			setzonename_global(optarg);
23994bdecd9SRob Gulewich 			break;
2407c478bd9Sstevel@tonic-gate 		case 'M' :
2417c478bd9Sstevel@tonic-gate 			memf = optarg;
2427c478bd9Sstevel@tonic-gate 			live_kernel = 0;
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 		case 'N' :
2457c478bd9Sstevel@tonic-gate 			kern = optarg;
2467c478bd9Sstevel@tonic-gate 			live_kernel = 0;
2477c478bd9Sstevel@tonic-gate 			break;
24894bdecd9SRob Gulewich 		case 'z' :
24994bdecd9SRob Gulewich 			setzonename(optarg);
25094bdecd9SRob Gulewich 			break;
2517c478bd9Sstevel@tonic-gate 		}
252ab25eeb5Syz 	}
2537c478bd9Sstevel@tonic-gate 	optind = myoptind;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (live_kernel == 1) {
2567c478bd9Sstevel@tonic-gate 		if ((state_fd = open(IPSTATE_NAME, O_RDONLY)) == -1) {
257ab25eeb5Syz 			perror("open(IPSTATE_NAME)");
2587c478bd9Sstevel@tonic-gate 			exit(-1);
2597c478bd9Sstevel@tonic-gate 		}
26094bdecd9SRob Gulewich 
26194bdecd9SRob Gulewich 		if (setzone(state_fd) != 0) {
26294bdecd9SRob Gulewich 			close(state_fd);
26394bdecd9SRob Gulewich 			exit(-1);
26494bdecd9SRob Gulewich 		}
26594bdecd9SRob Gulewich 
2667c478bd9Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
267ab25eeb5Syz 			fprintf(stderr, "open(%s)", device);
268ab25eeb5Syz 			perror("");
2697c478bd9Sstevel@tonic-gate 			exit(-1);
2707c478bd9Sstevel@tonic-gate 		}
27194bdecd9SRob Gulewich 
27294bdecd9SRob Gulewich 		if (setzone(ipf_fd) != 0) {
27394bdecd9SRob Gulewich 			close(ipf_fd);
27494bdecd9SRob Gulewich 			exit(-1);
27594bdecd9SRob Gulewich 		}
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
278ab25eeb5Syz 	if (kern != NULL || memf != NULL) {
2797c478bd9Sstevel@tonic-gate 		(void)setgid(getgid());
2807c478bd9Sstevel@tonic-gate 		(void)setreuid(getuid(), getuid());
281f4b3ec61Sdh 		if (openkmem(kern, memf) == -1)
282f4b3ec61Sdh 			exit(-1);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
285ab25eeb5Syz 	if (live_kernel == 1)
286ab25eeb5Syz 		(void) checkrev(device);
2877c478bd9Sstevel@tonic-gate 	(void)setgid(getgid());
2887c478bd9Sstevel@tonic-gate 	(void)setreuid(getuid(), getuid());
2897c478bd9Sstevel@tonic-gate 
290ab25eeb5Syz 	opterr = 1;
291ab25eeb5Syz 
292ab25eeb5Syz 	while ((c = getopt(argc, argv, options)) != -1)
2937c478bd9Sstevel@tonic-gate 	{
2947c478bd9Sstevel@tonic-gate 		switch (c)
2957c478bd9Sstevel@tonic-gate 		{
2967c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
2977c478bd9Sstevel@tonic-gate 		case '6' :
2987c478bd9Sstevel@tonic-gate 			use_inet6 = 1;
2997c478bd9Sstevel@tonic-gate 			break;
3007c478bd9Sstevel@tonic-gate #endif
3017c478bd9Sstevel@tonic-gate 		case 'a' :
3027c478bd9Sstevel@tonic-gate 			opts |= OPT_ACCNT|OPT_SHOWLIST;
3037c478bd9Sstevel@tonic-gate 			break;
3047c478bd9Sstevel@tonic-gate 		case 'A' :
3057c478bd9Sstevel@tonic-gate 			opts |= OPT_AUTHSTATS;
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 		case 'C' :
3087c478bd9Sstevel@tonic-gate 			topclosed = 1;
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 		case 'd' :
3117c478bd9Sstevel@tonic-gate 			opts |= OPT_DEBUG;
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 		case 'D' :
3147c478bd9Sstevel@tonic-gate 			parse_ipportstr(optarg, &daddr, &dport);
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 		case 'f' :
3177c478bd9Sstevel@tonic-gate 			opts |= OPT_FRSTATES;
3187c478bd9Sstevel@tonic-gate 			break;
3197c478bd9Sstevel@tonic-gate 		case 'g' :
3207c478bd9Sstevel@tonic-gate 			opts |= OPT_GROUPS;
3217c478bd9Sstevel@tonic-gate 			break;
32294bdecd9SRob Gulewich 		case 'G' :
32394bdecd9SRob Gulewich 			/* Already handled by getzoneopt() above */
32494bdecd9SRob Gulewich 			break;
3257c478bd9Sstevel@tonic-gate 		case 'h' :
3267c478bd9Sstevel@tonic-gate 			opts |= OPT_HITS;
3277c478bd9Sstevel@tonic-gate 			break;
3287c478bd9Sstevel@tonic-gate 		case 'i' :
3297c478bd9Sstevel@tonic-gate 			opts |= OPT_INQUE|OPT_SHOWLIST;
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 		case 'I' :
3327c478bd9Sstevel@tonic-gate 			opts |= OPT_INACTIVE;
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		case 'l' :
3357c478bd9Sstevel@tonic-gate 			opts |= OPT_SHOWLIST;
3367c478bd9Sstevel@tonic-gate 			break;
3377c478bd9Sstevel@tonic-gate 		case 'M' :
3387c478bd9Sstevel@tonic-gate 			break;
3397c478bd9Sstevel@tonic-gate 		case 'N' :
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		case 'n' :
3427c478bd9Sstevel@tonic-gate 			opts |= OPT_SHOWLINENO;
3437c478bd9Sstevel@tonic-gate 			break;
3447c478bd9Sstevel@tonic-gate 		case 'o' :
3457c478bd9Sstevel@tonic-gate 			opts |= OPT_OUTQUE|OPT_SHOWLIST;
3467c478bd9Sstevel@tonic-gate 			break;
3477c478bd9Sstevel@tonic-gate 		case 'P' :
348ab25eeb5Syz 			protocol = getproto(optarg);
349ab25eeb5Syz 			if (protocol == -1) {
350ab25eeb5Syz 				fprintf(stderr, "%s: Invalid protocol: %s\n",
3517c478bd9Sstevel@tonic-gate 					argv[0], optarg);
3527c478bd9Sstevel@tonic-gate 				exit(-2);
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 			break;
355ab25eeb5Syz 		case 'R' :
356ab25eeb5Syz 			opts |= OPT_NORESOLVE;
357ab25eeb5Syz 			break;
3587c478bd9Sstevel@tonic-gate 		case 's' :
3597c478bd9Sstevel@tonic-gate 			opts |= OPT_IPSTATES;
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		case 'S' :
3627c478bd9Sstevel@tonic-gate 			parse_ipportstr(optarg, &saddr, &sport);
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case 't' :
3657c478bd9Sstevel@tonic-gate #ifdef STATETOP
3667c478bd9Sstevel@tonic-gate 			opts |= OPT_STATETOP;
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate #else
3697c478bd9Sstevel@tonic-gate 			fprintf(stderr,
370ab25eeb5Syz 				"%s: state top facility not compiled in\n",
3717c478bd9Sstevel@tonic-gate 				argv[0]);
3727c478bd9Sstevel@tonic-gate 			exit(-2);
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 		case 'T' :
3757c478bd9Sstevel@tonic-gate 			if (!sscanf(optarg, "%d", &refreshtime) ||
3767c478bd9Sstevel@tonic-gate 				    (refreshtime <= 0)) {
3777c478bd9Sstevel@tonic-gate 				fprintf(stderr,
378ab25eeb5Syz 					"%s: Invalid refreshtime < 1 : %s\n",
3797c478bd9Sstevel@tonic-gate 					argv[0], optarg);
3807c478bd9Sstevel@tonic-gate 				exit(-2);
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			break;
3837c478bd9Sstevel@tonic-gate 		case 'v' :
3847c478bd9Sstevel@tonic-gate 			opts |= OPT_VERBOSE;
3857c478bd9Sstevel@tonic-gate 			opts |= OPT_UNDEF;
3867c478bd9Sstevel@tonic-gate 			break;
38794bdecd9SRob Gulewich 		case 'z' :
38894bdecd9SRob Gulewich 			/* Already handled by getzoneopt() above */
38994bdecd9SRob Gulewich 			break;
3907c478bd9Sstevel@tonic-gate 		default :
391ab25eeb5Syz 			usage(argv[0]);
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (live_kernel == 1) {
3977c478bd9Sstevel@tonic-gate 		bzero((char *)&fio, sizeof(fio));
3987c478bd9Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
3997c478bd9Sstevel@tonic-gate 		bzero((char *)&ifrst, sizeof(ifrst));
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		ipfstate_live(device, &fiop, &ipsstp, &ifrstp,
4027c478bd9Sstevel@tonic-gate 			      &frauthstp, &frf);
4037c478bd9Sstevel@tonic-gate 	} else
4047c478bd9Sstevel@tonic-gate 		ipfstate_dead(kern, &fiop, &ipsstp, &ifrstp, &frauthstp, &frf);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (opts & OPT_IPSTATES) {
4077c478bd9Sstevel@tonic-gate 		showipstates(ipsstp);
4087c478bd9Sstevel@tonic-gate 	} else if (opts & OPT_SHOWLIST) {
4097c478bd9Sstevel@tonic-gate 		showlist(fiop);
4107c478bd9Sstevel@tonic-gate 		if ((opts & OPT_OUTQUE) && (opts & OPT_INQUE)){
4117c478bd9Sstevel@tonic-gate 			opts &= ~OPT_OUTQUE;
4127c478bd9Sstevel@tonic-gate 			showlist(fiop);
4137c478bd9Sstevel@tonic-gate 		}
414ab25eeb5Syz 	} else if (opts & OPT_FRSTATES)
41543412a42SDarren Reed 		showfrstates(ifrstp, fiop->f_ticks);
4167c478bd9Sstevel@tonic-gate #ifdef STATETOP
417ab25eeb5Syz 	else if (opts & OPT_STATETOP)
418ab25eeb5Syz 		topipstates(saddr, daddr, sport, dport, protocol,
419ab25eeb5Syz 			    use_inet6 ? 6 : 4, refreshtime, topclosed);
4207c478bd9Sstevel@tonic-gate #endif
421ab25eeb5Syz 	else if (opts & OPT_AUTHSTATS)
422ab25eeb5Syz 		showauthstates(frauthstp);
423ab25eeb5Syz 	else if (opts & OPT_GROUPS)
424ab25eeb5Syz 		showgroups(fiop);
425ab25eeb5Syz 	else
426ab25eeb5Syz 		showstats(fiop, frf);
427ab25eeb5Syz 
4287c478bd9Sstevel@tonic-gate 	return 0;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Fill in the stats structures from the live kernel, using a combination
4347c478bd9Sstevel@tonic-gate  * of ioctl's and copying directly from kernel memory.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static void ipfstate_live(device, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
4377c478bd9Sstevel@tonic-gate char *device;
4387c478bd9Sstevel@tonic-gate friostat_t **fiopp;
4397c478bd9Sstevel@tonic-gate ips_stat_t **ipsstpp;
4407c478bd9Sstevel@tonic-gate ipfrstat_t **ifrstpp;
4417c478bd9Sstevel@tonic-gate fr_authstat_t **frauthstpp;
4427c478bd9Sstevel@tonic-gate u_32_t *frfp;
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (checkrev(device) == -1) {
4477c478bd9Sstevel@tonic-gate 		fprintf(stderr, "User/kernel version check failed\n");
4487c478bd9Sstevel@tonic-gate 		exit(1);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) == 0) {
4527c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4537c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4547c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(friostat_t);
4557c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*fiopp;
4567c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_IPFSTAT;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFS, &ipfo) == -1) {
4597c478bd9Sstevel@tonic-gate 			perror("ioctl(ipf:SIOCGETFS)");
4607c478bd9Sstevel@tonic-gate 			exit(-1);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGETFF, frfp) == -1)
4647c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGETFF)");
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if ((opts & OPT_IPSTATES) != 0) {
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4707c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4717c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ips_stat_t);
4727c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ipsstpp;
4737c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_STATESTAT;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
4767c478bd9Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETFS)");
4777c478bd9Sstevel@tonic-gate 			exit(-1);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		if (ioctl(state_fd, SIOCGETLG, &state_logging) == -1) {
4807c478bd9Sstevel@tonic-gate 			perror("ioctl(state:SIOCGETLG)");
4817c478bd9Sstevel@tonic-gate 			exit(-1);
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if ((opts & OPT_FRSTATES) != 0) {
4867c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
4877c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
4887c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(ipfrstat_t);
4897c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*ifrstpp;
4907c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_FRAGSTAT;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (ioctl(ipf_fd, SIOCGFRST, &ipfo) == -1) {
4937c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCGFRST)");
4947c478bd9Sstevel@tonic-gate 			exit(-1);
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
4997c478bd9Sstevel@tonic-gate 		PRINTF("opts %#x name %s\n", opts, device);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if ((opts & OPT_AUTHSTATS) != 0) {
5027c478bd9Sstevel@tonic-gate 		if (ipf_fd >= 0) {
5037c478bd9Sstevel@tonic-gate 			close(ipf_fd);
5047c478bd9Sstevel@tonic-gate 			ipf_fd = -1;
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 		device = IPAUTH_NAME;
5077c478bd9Sstevel@tonic-gate 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
5087c478bd9Sstevel@tonic-gate 			perror("open");
5097c478bd9Sstevel@tonic-gate 			exit(-1);
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 
51294bdecd9SRob Gulewich 		if (setzone(ipf_fd) != 0) {
51394bdecd9SRob Gulewich 			close(ipf_fd);
51494bdecd9SRob Gulewich 			exit(-1);
51594bdecd9SRob Gulewich 		}
51694bdecd9SRob Gulewich 
5177c478bd9Sstevel@tonic-gate 		bzero((caddr_t)&ipfo, sizeof(ipfo));
5187c478bd9Sstevel@tonic-gate 		ipfo.ipfo_rev = IPFILTER_VERSION;
5197c478bd9Sstevel@tonic-gate 		ipfo.ipfo_size = sizeof(fr_authstat_t);
5207c478bd9Sstevel@tonic-gate 		ipfo.ipfo_ptr = (void *)*frauthstpp;
5217c478bd9Sstevel@tonic-gate 		ipfo.ipfo_type = IPFOBJ_AUTHSTAT;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	    	if (ioctl(ipf_fd, SIOCATHST, &ipfo) == -1) {
5247c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCATHST)");
5257c478bd9Sstevel@tonic-gate 			exit(-1);
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * Build up the stats structures from data held in the "core" memory.
5337c478bd9Sstevel@tonic-gate  * This is mainly useful when looking at data in crash dumps and ioctl's
5347c478bd9Sstevel@tonic-gate  * just won't work any more.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate static void ipfstate_dead(kernel, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
5377c478bd9Sstevel@tonic-gate char *kernel;
5387c478bd9Sstevel@tonic-gate friostat_t **fiopp;
5397c478bd9Sstevel@tonic-gate ips_stat_t **ipsstpp;
5407c478bd9Sstevel@tonic-gate ipfrstat_t **ifrstpp;
5417c478bd9Sstevel@tonic-gate fr_authstat_t **frauthstpp;
5427c478bd9Sstevel@tonic-gate u_32_t *frfp;
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	static fr_authstat_t frauthst, *frauthstp;
5457c478bd9Sstevel@tonic-gate 	static ips_stat_t ipsst, *ipsstp;
5467c478bd9Sstevel@tonic-gate 	static ipfrstat_t ifrst, *ifrstp;
5477c478bd9Sstevel@tonic-gate 	static friostat_t fio, *fiop;
5487c478bd9Sstevel@tonic-gate 	int temp;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	void *rules[2][2];
5517c478bd9Sstevel@tonic-gate 	struct nlist deadlist[43] = {
5527c478bd9Sstevel@tonic-gate 		{ "fr_authstats" },		/* 0 */
5537c478bd9Sstevel@tonic-gate 		{ "fae_list" },
5547c478bd9Sstevel@tonic-gate 		{ "ipauth" },
5557c478bd9Sstevel@tonic-gate 		{ "fr_authlist" },
5567c478bd9Sstevel@tonic-gate 		{ "fr_authstart" },
5577c478bd9Sstevel@tonic-gate 		{ "fr_authend" },		/* 5 */
5587c478bd9Sstevel@tonic-gate 		{ "fr_authnext" },
5597c478bd9Sstevel@tonic-gate 		{ "fr_auth" },
5607c478bd9Sstevel@tonic-gate 		{ "fr_authused" },
5617c478bd9Sstevel@tonic-gate 		{ "fr_authsize" },
5627c478bd9Sstevel@tonic-gate 		{ "fr_defaultauthage" },	/* 10 */
5637c478bd9Sstevel@tonic-gate 		{ "fr_authpkts" },
5647c478bd9Sstevel@tonic-gate 		{ "fr_auth_lock" },
5657c478bd9Sstevel@tonic-gate 		{ "frstats" },
5667c478bd9Sstevel@tonic-gate 		{ "ips_stats" },
5677c478bd9Sstevel@tonic-gate 		{ "ips_num" },			/* 15 */
5687c478bd9Sstevel@tonic-gate 		{ "ips_wild" },
5697c478bd9Sstevel@tonic-gate 		{ "ips_list" },
5707c478bd9Sstevel@tonic-gate 		{ "ips_table" },
5717c478bd9Sstevel@tonic-gate 		{ "fr_statemax" },
5727c478bd9Sstevel@tonic-gate 		{ "fr_statesize" },		/* 20 */
5737c478bd9Sstevel@tonic-gate 		{ "fr_state_doflush" },
5747c478bd9Sstevel@tonic-gate 		{ "fr_state_lock" },
5757c478bd9Sstevel@tonic-gate 		{ "ipfr_heads" },
5767c478bd9Sstevel@tonic-gate 		{ "ipfr_nattab" },
5777c478bd9Sstevel@tonic-gate 		{ "ipfr_stats" },		/* 25 */
5787c478bd9Sstevel@tonic-gate 		{ "ipfr_inuse" },
5797c478bd9Sstevel@tonic-gate 		{ "fr_ipfrttl" },
5807c478bd9Sstevel@tonic-gate 		{ "fr_frag_lock" },
5817c478bd9Sstevel@tonic-gate 		{ "ipfr_timer_id" },
5827c478bd9Sstevel@tonic-gate 		{ "fr_nat_lock" },		/* 30 */
5837c478bd9Sstevel@tonic-gate 		{ "ipfilter" },
5847c478bd9Sstevel@tonic-gate 		{ "ipfilter6" },
5857c478bd9Sstevel@tonic-gate 		{ "ipacct" },
5867c478bd9Sstevel@tonic-gate 		{ "ipacct6" },
5877c478bd9Sstevel@tonic-gate 		{ "ipl_frouteok" },		/* 35 */
5887c478bd9Sstevel@tonic-gate 		{ "fr_running" },
5897c478bd9Sstevel@tonic-gate 		{ "ipfgroups" },
5907c478bd9Sstevel@tonic-gate 		{ "fr_active" },
5917c478bd9Sstevel@tonic-gate 		{ "fr_pass" },
5927c478bd9Sstevel@tonic-gate 		{ "fr_flags" },			/* 40 */
5937c478bd9Sstevel@tonic-gate 		{ "ipstate_logging" },
5947c478bd9Sstevel@tonic-gate 		{ NULL }
5957c478bd9Sstevel@tonic-gate 	};
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	frauthstp = &frauthst;
5997c478bd9Sstevel@tonic-gate 	ipsstp = &ipsst;
6007c478bd9Sstevel@tonic-gate 	ifrstp = &ifrst;
6017c478bd9Sstevel@tonic-gate 	fiop = &fio;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	*frfp = 0;
6047c478bd9Sstevel@tonic-gate 	*fiopp = fiop;
6057c478bd9Sstevel@tonic-gate 	*ipsstpp = ipsstp;
6067c478bd9Sstevel@tonic-gate 	*ifrstpp = ifrstp;
6077c478bd9Sstevel@tonic-gate 	*frauthstpp = frauthstp;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	bzero((char *)fiop, sizeof(*fiop));
6107c478bd9Sstevel@tonic-gate 	bzero((char *)ipsstp, sizeof(*ipsstp));
6117c478bd9Sstevel@tonic-gate 	bzero((char *)ifrstp, sizeof(*ifrstp));
6127c478bd9Sstevel@tonic-gate 	bzero((char *)frauthstp, sizeof(*frauthstp));
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (nlist(kernel, deadlist) == -1) {
6157c478bd9Sstevel@tonic-gate 		fprintf(stderr, "nlist error\n");
6167c478bd9Sstevel@tonic-gate 		return;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/*
6207c478bd9Sstevel@tonic-gate 	 * This is for SIOCGETFF.
6217c478bd9Sstevel@tonic-gate 	 */
6227c478bd9Sstevel@tonic-gate 	kmemcpy((char *)frfp, (u_long)deadlist[40].n_value, sizeof(*frfp));
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/*
6257c478bd9Sstevel@tonic-gate 	 * f_locks is a combination of the lock variable from each part of
6267c478bd9Sstevel@tonic-gate 	 * ipfilter (state, auth, nat, fragments).
6277c478bd9Sstevel@tonic-gate 	 */
6287c478bd9Sstevel@tonic-gate 	kmemcpy((char *)fiop, (u_long)deadlist[13].n_value, sizeof(*fiop));
6297c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[22].n_value,
6307c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[0]));
6317c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[30].n_value,
6327c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[1]));
6337c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[2], (u_long)deadlist[28].n_value,
6347c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[2]));
6357c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_locks[3], (u_long)deadlist[12].n_value,
6367c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_locks[3]));
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Get pointers to each list of rules (active, inactive, in, out)
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[31].n_value, sizeof(rules));
6427c478bd9Sstevel@tonic-gate 	fiop->f_fin[0] = rules[0][0];
6437c478bd9Sstevel@tonic-gate 	fiop->f_fin[1] = rules[0][1];
6447c478bd9Sstevel@tonic-gate 	fiop->f_fout[0] = rules[1][0];
6457c478bd9Sstevel@tonic-gate 	fiop->f_fout[1] = rules[1][1];
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * Same for IPv6, except make them null if support for it is not
6497c478bd9Sstevel@tonic-gate 	 * being compiled in.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
6527c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[32].n_value, sizeof(rules));
6537c478bd9Sstevel@tonic-gate 	fiop->f_fin6[0] = rules[0][0];
6547c478bd9Sstevel@tonic-gate 	fiop->f_fin6[1] = rules[0][1];
6557c478bd9Sstevel@tonic-gate 	fiop->f_fout6[0] = rules[1][0];
6567c478bd9Sstevel@tonic-gate 	fiop->f_fout6[1] = rules[1][1];
6577c478bd9Sstevel@tonic-gate #else
6587c478bd9Sstevel@tonic-gate 	fiop->f_fin6[0] = NULL;
6597c478bd9Sstevel@tonic-gate 	fiop->f_fin6[1] = NULL;
6607c478bd9Sstevel@tonic-gate 	fiop->f_fout6[0] = NULL;
6617c478bd9Sstevel@tonic-gate 	fiop->f_fout6[1] = NULL;
6627c478bd9Sstevel@tonic-gate #endif
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * Now get accounting rules pointers.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[33].n_value, sizeof(rules));
6687c478bd9Sstevel@tonic-gate 	fiop->f_acctin[0] = rules[0][0];
6697c478bd9Sstevel@tonic-gate 	fiop->f_acctin[1] = rules[0][1];
6707c478bd9Sstevel@tonic-gate 	fiop->f_acctout[0] = rules[1][0];
6717c478bd9Sstevel@tonic-gate 	fiop->f_acctout[1] = rules[1][1];
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
6747c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&rules, (u_long)deadlist[34].n_value, sizeof(rules));
6757c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[0] = rules[0][0];
6767c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[1] = rules[0][1];
6777c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[0] = rules[1][0];
6787c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[1] = rules[1][1];
6797c478bd9Sstevel@tonic-gate #else
6807c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[0] = NULL;
6817c478bd9Sstevel@tonic-gate 	fiop->f_acctin6[1] = NULL;
6827c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[0] = NULL;
6837c478bd9Sstevel@tonic-gate 	fiop->f_acctout6[1] = NULL;
6847c478bd9Sstevel@tonic-gate #endif
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * A collection of "global" variables used inside the kernel which
6887c478bd9Sstevel@tonic-gate 	 * are all collected in friostat_t via ioctl.
6897c478bd9Sstevel@tonic-gate 	 */
6907c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_froute, (u_long)deadlist[35].n_value,
6917c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_froute));
6927c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_running, (u_long)deadlist[36].n_value,
6937c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_running));
6947c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_groups, (u_long)deadlist[37].n_value,
6957c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_groups));
6967c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_active, (u_long)deadlist[38].n_value,
6977c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_active));
6987c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&fiop->f_defpass, (u_long)deadlist[39].n_value,
6997c478bd9Sstevel@tonic-gate 		sizeof(fiop->f_defpass));
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	/*
7027c478bd9Sstevel@tonic-gate 	 * Build up the state information stats structure.
7037c478bd9Sstevel@tonic-gate 	 */
7047c478bd9Sstevel@tonic-gate 	kmemcpy((char *)ipsstp, (u_long)deadlist[14].n_value, sizeof(*ipsstp));
7057c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&temp, (u_long)deadlist[15].n_value, sizeof(temp));
7067c478bd9Sstevel@tonic-gate 	ipsstp->iss_active = temp;
7077c478bd9Sstevel@tonic-gate 	ipsstp->iss_table = (void *)deadlist[18].n_value;
7087c478bd9Sstevel@tonic-gate 	ipsstp->iss_list = (void *)deadlist[17].n_value;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Build up the authentiation information stats structure.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	kmemcpy((char *)frauthstp, (u_long)deadlist[0].n_value,
7147c478bd9Sstevel@tonic-gate 		sizeof(*frauthstp));
7157c478bd9Sstevel@tonic-gate 	frauthstp->fas_faelist = (void *)deadlist[1].n_value;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * Build up the fragment information stats structure.
7197c478bd9Sstevel@tonic-gate 	 */
7207c478bd9Sstevel@tonic-gate 	kmemcpy((char *)ifrstp, (u_long)deadlist[25].n_value,
7217c478bd9Sstevel@tonic-gate 		sizeof(*ifrstp));
7227c478bd9Sstevel@tonic-gate 	ifrstp->ifs_table = (void *)deadlist[23].n_value;
7237c478bd9Sstevel@tonic-gate 	ifrstp->ifs_nattab = (void *)deadlist[24].n_value;
7247c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&ifrstp->ifs_inuse, (u_long)deadlist[26].n_value,
7257c478bd9Sstevel@tonic-gate 		sizeof(ifrstp->ifs_inuse));
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	/*
7287c478bd9Sstevel@tonic-gate 	 * Get logging on/off switches
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate 	kmemcpy((char *)&state_logging, (u_long)deadlist[41].n_value,
7317c478bd9Sstevel@tonic-gate 		sizeof(state_logging));
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate  * Display the kernel stats for packets blocked and passed and other
7377c478bd9Sstevel@tonic-gate  * associated running totals which are kept.
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate static	void	showstats(fp, frf)
7407c478bd9Sstevel@tonic-gate struct	friostat	*fp;
7417c478bd9Sstevel@tonic-gate u_32_t frf;
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	PRINTF("bad packets:\t\tin %lu\tout %lu\n",
7457c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
7467c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
7477c478bd9Sstevel@tonic-gate 	PRINTF(" IPv6 packets:\t\tin %lu out %lu\n",
7487c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ipv6, fp->f_st[1].fr_ipv6);
7497c478bd9Sstevel@tonic-gate #endif
7507c478bd9Sstevel@tonic-gate 	PRINTF(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
7517c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
7527c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_nom);
753ab25eeb5Syz 	PRINTF(" counted %lu short %lu\n",
7547c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_acct, fp->f_st[0].fr_short);
7557c478bd9Sstevel@tonic-gate 	PRINTF("output packets:\t\tblocked %lu passed %lu nomatch %lu",
7567c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
7577c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_nom);
758ab25eeb5Syz 	PRINTF(" counted %lu short %lu\n",
7597c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_acct, fp->f_st[1].fr_short);
7607c478bd9Sstevel@tonic-gate 	PRINTF(" input packets logged:\tblocked %lu passed %lu\n",
7617c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
7627c478bd9Sstevel@tonic-gate 	PRINTF("output packets logged:\tblocked %lu passed %lu\n",
7637c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
7647c478bd9Sstevel@tonic-gate 	PRINTF(" packets logged:\tinput %lu output %lu\n",
7657c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pkl, fp->f_st[1].fr_pkl);
7667c478bd9Sstevel@tonic-gate 	PRINTF(" log failures:\t\tinput %lu output %lu\n",
7677c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_skip, fp->f_st[1].fr_skip);
768ab25eeb5Syz 	PRINTF("fragment state(in):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
769ab25eeb5Syz 			fp->f_st[0].fr_nfr, fp->f_st[0].fr_bnfr,
770ab25eeb5Syz 			fp->f_st[0].fr_cfr);
771ab25eeb5Syz 	PRINTF("fragment state(out):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
772ab25eeb5Syz 			fp->f_st[1].fr_nfr, fp->f_st[1].fr_bnfr,
773ab25eeb5Syz 			fp->f_st[0].fr_cfr);
7747c478bd9Sstevel@tonic-gate 	PRINTF("packet state(in):\tkept %lu\tlost %lu\n",
7757c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ads, fp->f_st[0].fr_bads);
7767c478bd9Sstevel@tonic-gate 	PRINTF("packet state(out):\tkept %lu\tlost %lu\n",
7777c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_ads, fp->f_st[1].fr_bads);
7787c478bd9Sstevel@tonic-gate 	PRINTF("ICMP replies:\t%lu\tTCP RSTs sent:\t%lu\n",
7797c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_ret, fp->f_st[1].fr_ret);
7807c478bd9Sstevel@tonic-gate 	PRINTF("Invalid source(in):\t%lu\n", fp->f_st[0].fr_badsrc);
7817c478bd9Sstevel@tonic-gate 	PRINTF("Result cache hits(in):\t%lu\t(out):\t%lu\n",
7827c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_chit, fp->f_st[1].fr_chit);
7837c478bd9Sstevel@tonic-gate 	PRINTF("IN Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7847c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pull[0], fp->f_st[0].fr_pull[1]);
7857c478bd9Sstevel@tonic-gate 	PRINTF("OUT Pullups succeeded:\t%lu\tfailed:\t%lu\n",
7867c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_pull[0], fp->f_st[1].fr_pull[1]);
7877c478bd9Sstevel@tonic-gate 	PRINTF("Fastroute successes:\t%lu\tfailures:\t%lu\n",
7887c478bd9Sstevel@tonic-gate 			fp->f_froute[0], fp->f_froute[1]);
7897c478bd9Sstevel@tonic-gate 	PRINTF("TCP cksum fails(in):\t%lu\t(out):\t%lu\n",
7907c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_tcpbad, fp->f_st[1].fr_tcpbad);
7917c478bd9Sstevel@tonic-gate 	PRINTF("IPF Ticks:\t%lu\n", fp->f_ticks);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	PRINTF("Packet log flags set: (%#x)\n", frf);
7947c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGPASS)
7957c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets passed through filter\n");
7967c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGBLOCK)
7977c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets blocked by filter\n");
7987c478bd9Sstevel@tonic-gate 	if (frf & FF_LOGNOMATCH)
7997c478bd9Sstevel@tonic-gate 		PRINTF("\tpackets not matched by filter\n");
8007c478bd9Sstevel@tonic-gate 	if (!frf)
8017c478bd9Sstevel@tonic-gate 		PRINTF("\tnone\n");
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * Print out a list of rules from the kernel, starting at the one passed.
8077c478bd9Sstevel@tonic-gate  */
808f4b3ec61Sdh static void printlivelist(out, set, fp, group, comment)
809f4b3ec61Sdh int out, set;
8107c478bd9Sstevel@tonic-gate frentry_t *fp;
811f4b3ec61Sdh char *group, *comment;
8127c478bd9Sstevel@tonic-gate {
813f4b3ec61Sdh 	frgroup_t *grtop, *grtail, *g;
8147c478bd9Sstevel@tonic-gate 	struct	frentry	fb, *fg;
815786c7074Sjojemann 	int n;
816f4b3ec61Sdh 	ipfruleiter_t rule;
817f4b3ec61Sdh 	ipfobj_t obj;
818*ef1266efSToomas Soome 	u_long array[1000];
819f4b3ec61Sdh 
820f4b3ec61Sdh 	fb.fr_next = fp;
821f4b3ec61Sdh 	n = 0;
822f4b3ec61Sdh 
823f4b3ec61Sdh 	grtop = NULL;
824f4b3ec61Sdh 	grtail = NULL;
825f4b3ec61Sdh 	rule.iri_ver = use_inet6? AF_INET6 : AF_INET;
826f4b3ec61Sdh 	rule.iri_inout = out;
827f4b3ec61Sdh 	rule.iri_active = set;
82890b0a856Sjojemann 	rule.iri_nrules = 1;
829f4b3ec61Sdh 	rule.iri_rule = &fb;
830f4b3ec61Sdh 	if (group != NULL)
831f4b3ec61Sdh 		strncpy(rule.iri_group, group, FR_GROUPLEN);
832f4b3ec61Sdh 	else
833f4b3ec61Sdh 		rule.iri_group[0] = '\0';
834f4b3ec61Sdh 
835f4b3ec61Sdh 	bzero((char *)&obj, sizeof(obj));
836f4b3ec61Sdh 	obj.ipfo_rev = IPFILTER_VERSION;
837f4b3ec61Sdh 	obj.ipfo_type = IPFOBJ_IPFITER;
838f4b3ec61Sdh 	obj.ipfo_size = sizeof(rule);
839f4b3ec61Sdh 	obj.ipfo_ptr = &rule;
840f4b3ec61Sdh 
841f4b3ec61Sdh 	do {
842f4b3ec61Sdh 		memset(array, 0xff, sizeof(array));
843f4b3ec61Sdh 		fp = (frentry_t *)array;
844f4b3ec61Sdh 		rule.iri_rule = fp;
845f4b3ec61Sdh 		if (ioctl(ipf_fd, SIOCIPFITER, &obj) == -1) {
846f4b3ec61Sdh 			perror("ioctl(SIOCIPFITER)");
8477c478bd9Sstevel@tonic-gate 			return;
8487c478bd9Sstevel@tonic-gate 		}
849f4b3ec61Sdh 		if (fp->fr_data != NULL)
850f4b3ec61Sdh 			fp->fr_data = (char *)fp + sizeof(*fp);
851f4b3ec61Sdh 
852f4b3ec61Sdh 		n++;
853f4b3ec61Sdh 
8547c478bd9Sstevel@tonic-gate 		if (opts & (OPT_HITS|OPT_VERBOSE))
8557c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
8567c478bd9Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_hits);
8577c478bd9Sstevel@tonic-gate #else
8587c478bd9Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_hits);
8597c478bd9Sstevel@tonic-gate #endif
8607c478bd9Sstevel@tonic-gate 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
8617c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
8627c478bd9Sstevel@tonic-gate 			PRINTF("%qu ", (unsigned long long) fp->fr_bytes);
8637c478bd9Sstevel@tonic-gate #else
8647c478bd9Sstevel@tonic-gate 			PRINTF("%lu ", fp->fr_bytes);
8657c478bd9Sstevel@tonic-gate #endif
8667c478bd9Sstevel@tonic-gate 		if (opts & OPT_SHOWLINENO)
8677c478bd9Sstevel@tonic-gate 			PRINTF("@%d ", n);
868f4b3ec61Sdh 
869f4b3ec61Sdh 		printfr(fp, ioctl);
870f4b3ec61Sdh 		if (opts & OPT_DEBUG) {
871f4b3ec61Sdh 			binprint(fp, sizeof(*fp));
872f4b3ec61Sdh 			if (fp->fr_data != NULL && fp->fr_dsize > 0)
873f4b3ec61Sdh 				binprint(fp->fr_data, fp->fr_dsize);
874f4b3ec61Sdh 		}
875f4b3ec61Sdh 
876f4b3ec61Sdh 		if (fp->fr_grhead[0] != '\0') {
877f4b3ec61Sdh 			g = calloc(1, sizeof(*g));
878f4b3ec61Sdh 
879f4b3ec61Sdh 			if (g != NULL) {
880f4b3ec61Sdh 				strncpy(g->fg_name, fp->fr_grhead,
881f4b3ec61Sdh 					FR_GROUPLEN);
882f4b3ec61Sdh 				if (grtop == NULL) {
883f4b3ec61Sdh 					grtop = g;
884f4b3ec61Sdh 					grtail = g;
885f4b3ec61Sdh 				} else {
886f4b3ec61Sdh 					grtail->fg_next = g;
887f4b3ec61Sdh 					grtail = g;
888f4b3ec61Sdh 				}
889f4b3ec61Sdh 			}
890f4b3ec61Sdh 		}
891f4b3ec61Sdh 	} while (fp->fr_next != NULL);
892f4b3ec61Sdh 
893f4b3ec61Sdh 	while ((g = grtop) != NULL) {
894f4b3ec61Sdh 		printlivelist(out, set, NULL, g->fg_name, comment);
895f4b3ec61Sdh 		grtop = g->fg_next;
896f4b3ec61Sdh 		free(g);
897f4b3ec61Sdh 	}
898f4b3ec61Sdh }
899f4b3ec61Sdh 
900f4b3ec61Sdh 
901f4b3ec61Sdh static void printdeadlist(out, set, fp, group, comment)
902f4b3ec61Sdh int out, set;
903f4b3ec61Sdh frentry_t *fp;
904f4b3ec61Sdh char *group, *comment;
905f4b3ec61Sdh {
906f4b3ec61Sdh 	frgroup_t *grtop, *grtail, *g;
907f4b3ec61Sdh 	struct	frentry	fb, *fg;
908f4b3ec61Sdh 	char	*data;
909f4b3ec61Sdh 	u_32_t	type;
910f4b3ec61Sdh 	int	n;
911f4b3ec61Sdh 
912f4b3ec61Sdh 	fb.fr_next = fp;
913f4b3ec61Sdh 	n = 0;
914f4b3ec61Sdh 	grtop = NULL;
915f4b3ec61Sdh 	grtail = NULL;
916f4b3ec61Sdh 
917f4b3ec61Sdh 	do {
918f4b3ec61Sdh 		fp = fb.fr_next;
919f4b3ec61Sdh 		if (kmemcpy((char *)&fb, (u_long)fb.fr_next,
920f4b3ec61Sdh 			    sizeof(fb)) == -1) {
921f4b3ec61Sdh 			perror("kmemcpy");
922f4b3ec61Sdh 			return;
923f4b3ec61Sdh 		}
924f4b3ec61Sdh 
9257c478bd9Sstevel@tonic-gate 		data = NULL;
926f4b3ec61Sdh 		type = fb.fr_type & ~FR_T_BUILTIN;
9277c478bd9Sstevel@tonic-gate 		if (type == FR_T_IPF || type == FR_T_BPFOPC) {
928f4b3ec61Sdh 			if (fb.fr_dsize) {
929f4b3ec61Sdh 				data = malloc(fb.fr_dsize);
9307c478bd9Sstevel@tonic-gate 
931f4b3ec61Sdh 				if (kmemcpy(data, (u_long)fb.fr_data,
932f4b3ec61Sdh 					    fb.fr_dsize) == -1) {
9337c478bd9Sstevel@tonic-gate 					perror("kmemcpy");
9347c478bd9Sstevel@tonic-gate 					return;
9357c478bd9Sstevel@tonic-gate 				}
936f4b3ec61Sdh 				fb.fr_data = data;
9377c478bd9Sstevel@tonic-gate 			}
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 
940f4b3ec61Sdh 		n++;
941f4b3ec61Sdh 
942f4b3ec61Sdh 		if (opts & (OPT_HITS|OPT_VERBOSE))
943f4b3ec61Sdh #ifdef	USE_QUAD_T
944f4b3ec61Sdh 			PRINTF("%qu ", (unsigned long long) fb.fr_hits);
945f4b3ec61Sdh #else
946f4b3ec61Sdh 			PRINTF("%lu ", fb.fr_hits);
947f4b3ec61Sdh #endif
948f4b3ec61Sdh 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
949f4b3ec61Sdh #ifdef	USE_QUAD_T
950f4b3ec61Sdh 			PRINTF("%qu ", (unsigned long long) fb.fr_bytes);
951f4b3ec61Sdh #else
952f4b3ec61Sdh 			PRINTF("%lu ", fb.fr_bytes);
953f4b3ec61Sdh #endif
954f4b3ec61Sdh 		if (opts & OPT_SHOWLINENO)
955f4b3ec61Sdh 			PRINTF("@%d ", n);
956f4b3ec61Sdh 
9577c478bd9Sstevel@tonic-gate 		printfr(fp, ioctl);
958ab25eeb5Syz 		if (opts & OPT_DEBUG) {
9597c478bd9Sstevel@tonic-gate 			binprint(fp, sizeof(*fp));
960f4b3ec61Sdh 			if (fb.fr_data != NULL && fb.fr_dsize > 0)
961f4b3ec61Sdh 				binprint(fb.fr_data, fb.fr_dsize);
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		if (data != NULL)
9647c478bd9Sstevel@tonic-gate 			free(data);
965f4b3ec61Sdh 		if (fb.fr_grhead[0] != '\0') {
966f4b3ec61Sdh 			g = calloc(1, sizeof(*g));
967f4b3ec61Sdh 
968f4b3ec61Sdh 			if (g != NULL) {
969f4b3ec61Sdh 				strncpy(g->fg_name, fb.fr_grhead,
970f4b3ec61Sdh 					FR_GROUPLEN);
971f4b3ec61Sdh 				if (grtop == NULL) {
972f4b3ec61Sdh 					grtop = g;
973f4b3ec61Sdh 					grtail = g;
974f4b3ec61Sdh 				} else {
975f4b3ec61Sdh 					grtail->fg_next = g;
976f4b3ec61Sdh 					grtail = g;
977f4b3ec61Sdh 				}
978f4b3ec61Sdh 			}
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 		if (type == FR_T_CALLFUNC) {
981f4b3ec61Sdh 			printdeadlist(out, set, fb.fr_data, group,
982f4b3ec61Sdh 				      "# callfunc: ");
9837c478bd9Sstevel@tonic-gate 		}
984f4b3ec61Sdh 	} while (fb.fr_next != NULL);
985f4b3ec61Sdh 
986f4b3ec61Sdh 	while ((g = grtop) != NULL) {
987f4b3ec61Sdh 		printdeadlist(out, set, NULL, g->fg_name, comment);
988f4b3ec61Sdh 		grtop = g->fg_next;
989f4b3ec61Sdh 		free(g);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
993f4b3ec61Sdh 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * print out all of the asked for rule sets, using the stats struct as
9967c478bd9Sstevel@tonic-gate  * the base from which to get the pointers.
9977c478bd9Sstevel@tonic-gate  */
9987c478bd9Sstevel@tonic-gate static	void	showlist(fiop)
9997c478bd9Sstevel@tonic-gate struct	friostat	*fiop;
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	struct	frentry	*fp = NULL;
10027c478bd9Sstevel@tonic-gate 	int	i, set;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	set = fiop->f_active;
10057c478bd9Sstevel@tonic-gate 	if (opts & OPT_INACTIVE)
10067c478bd9Sstevel@tonic-gate 		set = 1 - set;
10077c478bd9Sstevel@tonic-gate 	if (opts & OPT_ACCNT) {
10087c478bd9Sstevel@tonic-gate #ifdef USE_INET6
10097c478bd9Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
10107c478bd9Sstevel@tonic-gate 			i = F_ACOUT;
10117c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout6[set];
10127c478bd9Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
10137c478bd9Sstevel@tonic-gate 			i = F_ACIN;
10147c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin6[set];
10157c478bd9Sstevel@tonic-gate 		} else
10167c478bd9Sstevel@tonic-gate #endif
10177c478bd9Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
10187c478bd9Sstevel@tonic-gate 			i = F_ACOUT;
10197c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctout[set];
10207c478bd9Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
10217c478bd9Sstevel@tonic-gate 			i = F_ACIN;
10227c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_acctin[set];
10237c478bd9Sstevel@tonic-gate 		} else {
10247c478bd9Sstevel@tonic-gate 			FPRINTF(stderr, "No -i or -o given with -a\n");
10257c478bd9Sstevel@tonic-gate 			return;
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 	} else {
10287c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
10297c478bd9Sstevel@tonic-gate 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
10307c478bd9Sstevel@tonic-gate 			i = F_OUT;
10317c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout6[set];
10327c478bd9Sstevel@tonic-gate 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
10337c478bd9Sstevel@tonic-gate 			i = F_IN;
10347c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin6[set];
10357c478bd9Sstevel@tonic-gate 		} else
10367c478bd9Sstevel@tonic-gate #endif
10377c478bd9Sstevel@tonic-gate 		if (opts & OPT_OUTQUE) {
10387c478bd9Sstevel@tonic-gate 			i = F_OUT;
10397c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fout[set];
10407c478bd9Sstevel@tonic-gate 		} else if (opts & OPT_INQUE) {
10417c478bd9Sstevel@tonic-gate 			i = F_IN;
10427c478bd9Sstevel@tonic-gate 			fp = (struct frentry *)fiop->f_fin[set];
10437c478bd9Sstevel@tonic-gate 		} else
10447c478bd9Sstevel@tonic-gate 			return;
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
10477c478bd9Sstevel@tonic-gate 		FPRINTF(stderr, "showlist:opts %#x i %d\n", opts, i);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if (opts & OPT_VERBOSE)
10507c478bd9Sstevel@tonic-gate 		PRINTF("fp %p set %d\n", fp, set);
10517c478bd9Sstevel@tonic-gate 	if (!fp) {
10527c478bd9Sstevel@tonic-gate 		FPRINTF(stderr, "empty list for %s%s\n",
10537c478bd9Sstevel@tonic-gate 			(opts & OPT_INACTIVE) ? "inactive " : "", filters[i]);
10547c478bd9Sstevel@tonic-gate 		return;
10557c478bd9Sstevel@tonic-gate 	}
1056f4b3ec61Sdh 	if (live_kernel == 1)
1057f4b3ec61Sdh 		printlivelist(i, set, fp, NULL, NULL);
1058f4b3ec61Sdh 	else
1059f4b3ec61Sdh 		printdeadlist(i, set, fp, NULL, NULL);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate  * Display ipfilter stateful filtering information
10657c478bd9Sstevel@tonic-gate  */
10667c478bd9Sstevel@tonic-gate static void showipstates(ipsp)
10677c478bd9Sstevel@tonic-gate ips_stat_t *ipsp;
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	u_long minlen, maxlen, totallen, *buckets;
10707c478bd9Sstevel@tonic-gate 	int i, sz;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	sz = sizeof(*buckets) * ipsp->iss_statesize;
10737c478bd9Sstevel@tonic-gate 	buckets = (u_long *)malloc(sz);
10745e985db5Sschuster 	if (buckets == NULL) {
10755e985db5Sschuster 		perror("malloc");
10765e985db5Sschuster 		exit(1);
10775e985db5Sschuster 	}
10787c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)buckets, (u_long)ipsp->iss_bucketlen, sz)) {
10797c478bd9Sstevel@tonic-gate 		free(buckets);
10807c478bd9Sstevel@tonic-gate 		return;
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	 * If a list of states hasn't been asked for, only print out stats
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	if (!(opts & OPT_SHOWLIST)) {
10877c478bd9Sstevel@tonic-gate 		PRINTF("IP states added:\n\t%lu TCP\n\t%lu UDP\n\t%lu ICMP\n",
10887c478bd9Sstevel@tonic-gate 			ipsp->iss_tcp, ipsp->iss_udp, ipsp->iss_icmp);
108940cdc2e8SAlexandr Nedvedicky 		PRINTF("\t%lu hits\n\t%lu misses\n",
109040cdc2e8SAlexandr Nedvedicky 			ipsp->iss_hits, ipsp->iss_miss);
109140cdc2e8SAlexandr Nedvedicky 		PRINTF("\t%lu maximum\n\t%lu no memory\n", ipsp->iss_max,
109240cdc2e8SAlexandr Nedvedicky 			ipsp->iss_nomem);
10935b48165cSJohn Ojemann 		PRINTF("\t%lu active\n\t%lu expired\n",
10945b48165cSJohn Ojemann 			ipsp->iss_active, ipsp->iss_expire);
10955b48165cSJohn Ojemann 		PRINTF("\t%lu closed\n\t%u orphans\n",
10965b48165cSJohn Ojemann 			ipsp->iss_fin, ipsp->iss_orphans);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 		PRINTF("State logging %sabled\n",
10997c478bd9Sstevel@tonic-gate 			state_logging ? "en" : "dis");
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		PRINTF("\nState table bucket statistics:\n");
110240cdc2e8SAlexandr Nedvedicky 		PRINTF("\t%lu in use\n\t%lu max bucket\n", ipsp->iss_inuse,
110340cdc2e8SAlexandr Nedvedicky 			ipsp->iss_bucketfull);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 		minlen = ipsp->iss_max;
11067c478bd9Sstevel@tonic-gate 		totallen = 0;
11077c478bd9Sstevel@tonic-gate 		maxlen = 0;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 		for (i = 0; i < ipsp->iss_statesize; i++) {
11107c478bd9Sstevel@tonic-gate 			if (buckets[i] > maxlen)
11117c478bd9Sstevel@tonic-gate 				maxlen = buckets[i];
11127c478bd9Sstevel@tonic-gate 			if (buckets[i] < minlen)
11137c478bd9Sstevel@tonic-gate 					minlen = buckets[i];
11147c478bd9Sstevel@tonic-gate 			totallen += buckets[i];
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 		PRINTF("\t%2.2f%% bucket usage\n\t%lu minimal length\n",
11187c478bd9Sstevel@tonic-gate 			((float)ipsp->iss_inuse / ipsp->iss_statesize) * 100.0,
11197c478bd9Sstevel@tonic-gate 			minlen);
11207c478bd9Sstevel@tonic-gate 		PRINTF("\t%lu maximal length\n\t%.3f average length\n",
11217c478bd9Sstevel@tonic-gate 			maxlen,
11227c478bd9Sstevel@tonic-gate 			ipsp->iss_inuse ? (float) totallen/ ipsp->iss_inuse :
11237c478bd9Sstevel@tonic-gate 					  0.0);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate #define ENTRIES_PER_LINE 5
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE) {
11287c478bd9Sstevel@tonic-gate 			PRINTF("\nCurrent bucket sizes :\n");
11297c478bd9Sstevel@tonic-gate 			for (i = 0; i < ipsp->iss_statesize; i++) {
1130ab25eeb5Syz 				if ((i % ENTRIES_PER_LINE) == 0)
11317c478bd9Sstevel@tonic-gate 					PRINTF("\t");
11327c478bd9Sstevel@tonic-gate 				PRINTF("%4d -> %4lu", i, buckets[i]);
11337c478bd9Sstevel@tonic-gate 				if ((i % ENTRIES_PER_LINE) ==
1134ab25eeb5Syz 				    (ENTRIES_PER_LINE - 1))
11357c478bd9Sstevel@tonic-gate 					PRINTF("\n");
1136ab25eeb5Syz 				else
11377c478bd9Sstevel@tonic-gate 					PRINTF("  ");
11387c478bd9Sstevel@tonic-gate 			}
11397c478bd9Sstevel@tonic-gate 			PRINTF("\n");
11407c478bd9Sstevel@tonic-gate 		}
11417c478bd9Sstevel@tonic-gate 		PRINTF("\n");
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		free(buckets);
11447c478bd9Sstevel@tonic-gate 		return;
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/*
11487c478bd9Sstevel@tonic-gate 	 * Print out all the state information currently held in the kernel.
11497c478bd9Sstevel@tonic-gate 	 */
11507c478bd9Sstevel@tonic-gate 	while (ipsp->iss_list != NULL) {
1151ab25eeb5Syz 		ipsp->iss_list = printstate(ipsp->iss_list, opts,
1152ab25eeb5Syz 					    ipsp->iss_ticks);
11537c478bd9Sstevel@tonic-gate 	}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	free(buckets);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate #ifdef STATETOP
1160ab25eeb5Syz static int handle_resize = 0, handle_break = 0;
1161ab25eeb5Syz 
1162ab25eeb5Syz static void topipstates(saddr, daddr, sport, dport, protocol, ver,
11637c478bd9Sstevel@tonic-gate 		        refreshtime, topclosed)
1164ab25eeb5Syz i6addr_t saddr;
1165ab25eeb5Syz i6addr_t daddr;
11667c478bd9Sstevel@tonic-gate int sport;
11677c478bd9Sstevel@tonic-gate int dport;
11687c478bd9Sstevel@tonic-gate int protocol;
1169ab25eeb5Syz int ver;
11707c478bd9Sstevel@tonic-gate int refreshtime;
11717c478bd9Sstevel@tonic-gate int topclosed;
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate 	char str1[STSTRSIZE], str2[STSTRSIZE], str3[STSTRSIZE], str4[STSTRSIZE];
11747c478bd9Sstevel@tonic-gate 	int maxtsentries = 0, reverse = 0, sorting = STSORT_DEFAULT;
1175ab25eeb5Syz 	int i, j, winy, tsentry, maxx, maxy, redraw = 0, ret = 0;
1176ab25eeb5Syz 	int len, srclen, dstlen, forward = 1, c = 0;
11777c478bd9Sstevel@tonic-gate 	ips_stat_t ipsst, *ipsstp = &ipsst;
11787c478bd9Sstevel@tonic-gate 	statetop_t *tstable = NULL, *tp;
1179ab25eeb5Syz 	const char *errstr = "";
11807c478bd9Sstevel@tonic-gate 	ipstate_t ips;
11817c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
1182ab25eeb5Syz 	struct timeval selecttimeout;
11837c478bd9Sstevel@tonic-gate 	char hostnm[HOSTNMLEN];
11847c478bd9Sstevel@tonic-gate 	struct protoent *proto;
11857c478bd9Sstevel@tonic-gate 	fd_set readfd;
11867c478bd9Sstevel@tonic-gate 	time_t t;
11877c478bd9Sstevel@tonic-gate 
1188ab25eeb5Syz 	/* install signal handlers */
1189ab25eeb5Syz 	signal(SIGINT, sig_break);
1190ab25eeb5Syz 	signal(SIGQUIT, sig_break);
1191ab25eeb5Syz 	signal(SIGTERM, sig_break);
1192ab25eeb5Syz 	signal(SIGWINCH, sig_resize);
1193ab25eeb5Syz 
11947c478bd9Sstevel@tonic-gate 	/* init ncurses stuff */
11957c478bd9Sstevel@tonic-gate   	initscr();
11967c478bd9Sstevel@tonic-gate   	cbreak();
11977c478bd9Sstevel@tonic-gate   	noecho();
1198ab25eeb5Syz 	curs_set(0);
1199ab25eeb5Syz 	timeout(0);
1200ab25eeb5Syz 	getmaxyx(stdscr, maxy, maxx);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* init hostname */
12037c478bd9Sstevel@tonic-gate 	gethostname(hostnm, sizeof(hostnm) - 1);
12047c478bd9Sstevel@tonic-gate 	hostnm[sizeof(hostnm) - 1] = '\0';
1205ab25eeb5Syz 
12067c478bd9Sstevel@tonic-gate 	/* init ipfobj_t stuff */
12077c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&ipfo, sizeof(ipfo));
12087c478bd9Sstevel@tonic-gate 	ipfo.ipfo_rev = IPFILTER_VERSION;
12097c478bd9Sstevel@tonic-gate 	ipfo.ipfo_size = sizeof(*ipsstp);
12107c478bd9Sstevel@tonic-gate 	ipfo.ipfo_ptr = (void *)ipsstp;
12117c478bd9Sstevel@tonic-gate 	ipfo.ipfo_type = IPFOBJ_STATESTAT;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/* repeat until user aborts */
12147c478bd9Sstevel@tonic-gate 	while ( 1 ) {
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		/* get state table */
12177c478bd9Sstevel@tonic-gate 		bzero((char *)&ipsst, sizeof(ipsst));
12187c478bd9Sstevel@tonic-gate 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
1219ab25eeb5Syz 			errstr = "ioctl(SIOCGETFS)";
1220ab25eeb5Syz 			ret = -1;
1221ab25eeb5Syz 			goto out;
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		/* clear the history */
12257c478bd9Sstevel@tonic-gate 		tsentry = -1;
12267c478bd9Sstevel@tonic-gate 
1227ab25eeb5Syz 		/* reset max str len */
1228ab25eeb5Syz 		srclen = dstlen = 0;
1229ab25eeb5Syz 
12307c478bd9Sstevel@tonic-gate 		/* read the state table and store in tstable */
1231ab25eeb5Syz 		for (; ipsstp->iss_list; ipsstp->iss_list = ips.is_next) {
1232ab25eeb5Syz 
12337c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ips, (u_long)ipsstp->iss_list,
12347c478bd9Sstevel@tonic-gate 				    sizeof(ips)))
12357c478bd9Sstevel@tonic-gate 				break;
12367c478bd9Sstevel@tonic-gate 
1237ab25eeb5Syz 			if (ips.is_v != ver)
1238ab25eeb5Syz 				continue;
1239ab25eeb5Syz 
1240ab25eeb5Syz 			/* check v4 src/dest addresses */
1241ab25eeb5Syz 			if (ips.is_v == 4) {
1242ab25eeb5Syz 				if ((saddr.in4.s_addr != INADDR_ANY &&
1243ab25eeb5Syz 				     saddr.in4.s_addr != ips.is_saddr) ||
1244ab25eeb5Syz 				    (daddr.in4.s_addr != INADDR_ANY &&
1245ab25eeb5Syz 				     daddr.in4.s_addr != ips.is_daddr))
1246ab25eeb5Syz 					continue;
1247ab25eeb5Syz 			}
1248ab25eeb5Syz #ifdef	USE_INET6
1249ab25eeb5Syz 			/* check v6 src/dest addresses */
1250ab25eeb5Syz 			if (ips.is_v == 6) {
1251ab25eeb5Syz 				if ((IP6_NEQ(&saddr, &in6addr_any) &&
1252ab25eeb5Syz 				     IP6_NEQ(&saddr, &ips.is_src)) ||
1253ab25eeb5Syz 				    (IP6_NEQ(&daddr, &in6addr_any) &&
1254ab25eeb5Syz 				     IP6_NEQ(&daddr, &ips.is_dst)))
1255ab25eeb5Syz 					continue;
1256ab25eeb5Syz 			}
1257ab25eeb5Syz #endif
1258ab25eeb5Syz 			/* check protocol */
1259ab25eeb5Syz 			if (protocol > 0 && protocol != ips.is_p)
1260ab25eeb5Syz 				continue;
1261ab25eeb5Syz 
1262ab25eeb5Syz 			/* check ports if protocol is TCP or UDP */
1263ab25eeb5Syz 			if (((ips.is_p == IPPROTO_TCP) ||
1264ab25eeb5Syz 			     (ips.is_p == IPPROTO_UDP)) &&
1265ab25eeb5Syz 			   (((sport > 0) && (htons(sport) != ips.is_sport)) ||
1266ab25eeb5Syz 			    ((dport > 0) && (htons(dport) != ips.is_dport))))
1267ab25eeb5Syz 				continue;
1268ab25eeb5Syz 
1269ab25eeb5Syz 			/* show closed TCP sessions ? */
1270ab25eeb5Syz 			if ((topclosed == 0) && (ips.is_p == IPPROTO_TCP) &&
1271ab25eeb5Syz 			    (ips.is_state[0] >= IPF_TCPS_LAST_ACK) &&
1272ab25eeb5Syz 			    (ips.is_state[1] >= IPF_TCPS_LAST_ACK))
1273ab25eeb5Syz 				continue;
1274ab25eeb5Syz 
1275ab25eeb5Syz 			/*
1276ab25eeb5Syz 			 * if necessary make room for this state
1277ab25eeb5Syz 			 * entry
1278ab25eeb5Syz 			 */
1279ab25eeb5Syz 			tsentry++;
1280ab25eeb5Syz 			if (!maxtsentries || tsentry == maxtsentries) {
1281ab25eeb5Syz 				maxtsentries += STGROWSIZE;
1282ab25eeb5Syz 				tstable = realloc(tstable,
1283ab25eeb5Syz 				    maxtsentries * sizeof(statetop_t));
1284ab25eeb5Syz 				if (tstable == NULL) {
1285ab25eeb5Syz 					perror("realloc");
1286ab25eeb5Syz 					exit(-1);
12877c478bd9Sstevel@tonic-gate 				}
1288ab25eeb5Syz 			}
12897c478bd9Sstevel@tonic-gate 
1290ab25eeb5Syz 			/* get max src/dest address string length */
1291ab25eeb5Syz 			len = strlen(getip(ips.is_v, &ips.is_src));
1292ab25eeb5Syz 			if (srclen < len)
1293ab25eeb5Syz 				srclen = len;
1294ab25eeb5Syz 			len = strlen(getip(ips.is_v, &ips.is_dst));
1295ab25eeb5Syz 			if (dstlen < len)
1296ab25eeb5Syz 				dstlen = len;
1297ab25eeb5Syz 
1298ab25eeb5Syz 			/* fill structure */
1299ab25eeb5Syz 			tp = tstable + tsentry;
1300ab25eeb5Syz 			tp->st_src = ips.is_src;
1301ab25eeb5Syz 			tp->st_dst = ips.is_dst;
1302ab25eeb5Syz 			tp->st_p = ips.is_p;
1303ab25eeb5Syz 			tp->st_v = ips.is_v;
1304ab25eeb5Syz 			tp->st_state[0] = ips.is_state[0];
1305ab25eeb5Syz 			tp->st_state[1] = ips.is_state[1];
1306ab25eeb5Syz 			if (forward) {
1307ab25eeb5Syz 				tp->st_pkts = ips.is_pkts[0]+ips.is_pkts[1];
1308ab25eeb5Syz 				tp->st_bytes = ips.is_bytes[0]+ips.is_bytes[1];
1309ab25eeb5Syz 			} else {
1310ab25eeb5Syz 				tp->st_pkts = ips.is_pkts[2]+ips.is_pkts[3];
1311ab25eeb5Syz 				tp->st_bytes = ips.is_bytes[2]+ips.is_bytes[3];
1312ab25eeb5Syz 			}
1313ab25eeb5Syz 			tp->st_age = ips.is_die - ipsstp->iss_ticks;
1314ab25eeb5Syz 			if ((ips.is_p == IPPROTO_TCP) ||
1315ab25eeb5Syz 			    (ips.is_p == IPPROTO_UDP)) {
1316ab25eeb5Syz 				tp->st_sport = ips.is_sport;
1317ab25eeb5Syz 				tp->st_dport = ips.is_dport;
13187c478bd9Sstevel@tonic-gate 			}
13197c478bd9Sstevel@tonic-gate 		}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 		/* sort the array */
1323ab25eeb5Syz 		if (tsentry != -1) {
13247c478bd9Sstevel@tonic-gate 			switch (sorting)
13257c478bd9Sstevel@tonic-gate 			{
13267c478bd9Sstevel@tonic-gate 			case STSORT_PR:
13277c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13287c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_p);
13297c478bd9Sstevel@tonic-gate 				break;
13307c478bd9Sstevel@tonic-gate 			case STSORT_PKTS:
13317c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13327c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_pkts);
13337c478bd9Sstevel@tonic-gate 				break;
13347c478bd9Sstevel@tonic-gate 			case STSORT_BYTES:
13357c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13367c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_bytes);
13377c478bd9Sstevel@tonic-gate 				break;
13387c478bd9Sstevel@tonic-gate 			case STSORT_TTL:
13397c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13407c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_ttl);
13417c478bd9Sstevel@tonic-gate 				break;
13427c478bd9Sstevel@tonic-gate 			case STSORT_SRCIP:
13437c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13447c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_srcip);
13457c478bd9Sstevel@tonic-gate 				break;
1346ab25eeb5Syz 			case STSORT_SRCPT:
1347ab25eeb5Syz 				qsort(tstable, tsentry +1,
1348ab25eeb5Syz 					sizeof(statetop_t), sort_srcpt);
1349ab25eeb5Syz 				break;
13507c478bd9Sstevel@tonic-gate 			case STSORT_DSTIP:
13517c478bd9Sstevel@tonic-gate 				qsort(tstable, tsentry + 1,
13527c478bd9Sstevel@tonic-gate 				      sizeof(statetop_t), sort_dstip);
13537c478bd9Sstevel@tonic-gate 				break;
1354ab25eeb5Syz 			case STSORT_DSTPT:
1355ab25eeb5Syz 				qsort(tstable, tsentry + 1,
1356ab25eeb5Syz 				      sizeof(statetop_t), sort_dstpt);
1357ab25eeb5Syz 				break;
13587c478bd9Sstevel@tonic-gate 			default:
13597c478bd9Sstevel@tonic-gate 				break;
13607c478bd9Sstevel@tonic-gate 			}
1361ab25eeb5Syz 		}
1362ab25eeb5Syz 
1363ab25eeb5Syz 		/* handle window resizes */
1364ab25eeb5Syz 		if (handle_resize) {
1365ab25eeb5Syz 			endwin();
1366ab25eeb5Syz 			initscr();
1367ab25eeb5Syz 			cbreak();
1368ab25eeb5Syz 			noecho();
1369ab25eeb5Syz 			curs_set(0);
1370ab25eeb5Syz 			timeout(0);
1371ab25eeb5Syz 			getmaxyx(stdscr, maxy, maxx);
1372ab25eeb5Syz 			redraw = 1;
1373ab25eeb5Syz 			handle_resize = 0;
1374ab25eeb5Syz                 }
1375ab25eeb5Syz 
1376ab25eeb5Syz 		/* stop program? */
1377ab25eeb5Syz 		if (handle_break)
1378ab25eeb5Syz 			break;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 		/* print title */
13817c478bd9Sstevel@tonic-gate 		erase();
13827c478bd9Sstevel@tonic-gate 		attron(A_BOLD);
1383ab25eeb5Syz 		winy = 0;
1384ab25eeb5Syz 		move(winy,0);
13857c478bd9Sstevel@tonic-gate 		sprintf(str1, "%s - %s - state top", hostnm, IPL_VERSION);
13867c478bd9Sstevel@tonic-gate 		for (j = 0 ; j < (maxx - 8 - strlen(str1)) / 2; j++)
13877c478bd9Sstevel@tonic-gate 			printw(" ");
13887c478bd9Sstevel@tonic-gate 		printw("%s", str1);
13897c478bd9Sstevel@tonic-gate 		attroff(A_BOLD);
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		/* just for fun add a clock */
1392ab25eeb5Syz 		move(winy, maxx - 8);
13937c478bd9Sstevel@tonic-gate 		t = time(NULL);
13947c478bd9Sstevel@tonic-gate 		strftime(str1, 80, "%T", localtime(&t));
13957c478bd9Sstevel@tonic-gate 		printw("%s\n", str1);
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 		/*
1398ab25eeb5Syz 		 * print the display filters, this is placed in the loop,
13997c478bd9Sstevel@tonic-gate 		 * because someday I might add code for changing these
14007c478bd9Sstevel@tonic-gate 		 * while the programming is running :-)
14017c478bd9Sstevel@tonic-gate 		 */
14027c478bd9Sstevel@tonic-gate 		if (sport >= 0)
1403ab25eeb5Syz 			sprintf(str1, "%s,%d", getip(ver, &saddr), sport);
14047c478bd9Sstevel@tonic-gate 		else
1405ab25eeb5Syz 			sprintf(str1, "%s", getip(ver, &saddr));
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 		if (dport >= 0)
1408ab25eeb5Syz 			sprintf(str2, "%s,%d", getip(ver, &daddr), dport);
14097c478bd9Sstevel@tonic-gate 		else
1410ab25eeb5Syz 			sprintf(str2, "%s", getip(ver, &daddr));
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 		if (protocol < 0)
14137c478bd9Sstevel@tonic-gate 			strcpy(str3, "any");
14147c478bd9Sstevel@tonic-gate 		else if ((proto = getprotobynumber(protocol)) != NULL)
1415ab25eeb5Syz 			sprintf(str3, "%s", proto->p_name);
14167c478bd9Sstevel@tonic-gate 		else
14177c478bd9Sstevel@tonic-gate 			sprintf(str3, "%d", protocol);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 		switch (sorting)
14207c478bd9Sstevel@tonic-gate 		{
14217c478bd9Sstevel@tonic-gate 		case STSORT_PR:
14227c478bd9Sstevel@tonic-gate 			sprintf(str4, "proto");
14237c478bd9Sstevel@tonic-gate 			break;
14247c478bd9Sstevel@tonic-gate 		case STSORT_PKTS:
14257c478bd9Sstevel@tonic-gate 			sprintf(str4, "# pkts");
14267c478bd9Sstevel@tonic-gate 			break;
14277c478bd9Sstevel@tonic-gate 		case STSORT_BYTES:
14287c478bd9Sstevel@tonic-gate 			sprintf(str4, "# bytes");
14297c478bd9Sstevel@tonic-gate 			break;
14307c478bd9Sstevel@tonic-gate 		case STSORT_TTL:
14317c478bd9Sstevel@tonic-gate 			sprintf(str4, "ttl");
14327c478bd9Sstevel@tonic-gate 			break;
14337c478bd9Sstevel@tonic-gate 		case STSORT_SRCIP:
1434ab25eeb5Syz 			sprintf(str4, "src ip");
1435ab25eeb5Syz 			break;
1436ab25eeb5Syz 		case STSORT_SRCPT:
1437ab25eeb5Syz 			sprintf(str4, "src port");
14387c478bd9Sstevel@tonic-gate 			break;
14397c478bd9Sstevel@tonic-gate 		case STSORT_DSTIP:
1440ab25eeb5Syz 			sprintf(str4, "dest ip");
1441ab25eeb5Syz 			break;
1442ab25eeb5Syz 		case STSORT_DSTPT:
1443ab25eeb5Syz 			sprintf(str4, "dest port");
14447c478bd9Sstevel@tonic-gate 			break;
14457c478bd9Sstevel@tonic-gate 		default:
14467c478bd9Sstevel@tonic-gate 			sprintf(str4, "unknown");
14477c478bd9Sstevel@tonic-gate 			break;
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		if (reverse)
14517c478bd9Sstevel@tonic-gate 			strcat(str4, " (reverse)");
14527c478bd9Sstevel@tonic-gate 
1453ab25eeb5Syz 		winy += 2;
1454ab25eeb5Syz 		move(winy,0);
1455ab25eeb5Syz 		printw("Src: %s, Dest: %s, Proto: %s, Sorted by: %s\n\n",
14567c478bd9Sstevel@tonic-gate 		       str1, str2, str3, str4);
14577c478bd9Sstevel@tonic-gate 
1458ab25eeb5Syz 		/*
1459ab25eeb5Syz 		 * For an IPv4 IP address we need at most 15 characters,
1460ab25eeb5Syz 		 * 4 tuples of 3 digits, separated by 3 dots. Enforce this
1461ab25eeb5Syz 		 * length, so the colums do not change positions based
1462ab25eeb5Syz 		 * on the size of the IP address. This length makes the
1463ab25eeb5Syz 		 * output fit in a 80 column terminal.
1464ab25eeb5Syz 		 * We are lacking a good solution for IPv6 addresses (that
1465ab25eeb5Syz 		 * can be longer that 15 characters), so we do not enforce
1466ab25eeb5Syz 		 * a maximum on the IP field size.
1467ab25eeb5Syz 		 */
1468ab25eeb5Syz 		if (srclen < 15)
1469ab25eeb5Syz 			srclen = 15;
1470ab25eeb5Syz 		if (dstlen < 15)
1471ab25eeb5Syz 			dstlen = 15;
1472ab25eeb5Syz 
14737c478bd9Sstevel@tonic-gate 		/* print column description */
1474ab25eeb5Syz 		winy += 2;
1475ab25eeb5Syz 		move(winy,0);
14767c478bd9Sstevel@tonic-gate 		attron(A_BOLD);
1477ab25eeb5Syz 		printw("%-*s %-*s %3s %4s %7s %9s %9s\n",
1478ab25eeb5Syz 		       srclen + 6, "Source IP", dstlen + 6, "Destination IP",
1479ab25eeb5Syz 		       "ST", "PR", "#pkts", "#bytes", "ttl");
14807c478bd9Sstevel@tonic-gate 		attroff(A_BOLD);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 		/* print all the entries */
14837c478bd9Sstevel@tonic-gate 		tp = tstable;
14847c478bd9Sstevel@tonic-gate 		if (reverse)
14857c478bd9Sstevel@tonic-gate 			tp += tsentry;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 		if (tsentry > maxy - 6)
14887c478bd9Sstevel@tonic-gate 			tsentry = maxy - 6;
14897c478bd9Sstevel@tonic-gate 		for (i = 0; i <= tsentry; i++) {
14907c478bd9Sstevel@tonic-gate 			/* print src/dest and port */
14917c478bd9Sstevel@tonic-gate 			if ((tp->st_p == IPPROTO_TCP) ||
14927c478bd9Sstevel@tonic-gate 			    (tp->st_p == IPPROTO_UDP)) {
14937c478bd9Sstevel@tonic-gate 				sprintf(str1, "%s,%hu",
1494ab25eeb5Syz 					getip(tp->st_v, &tp->st_src),
14957c478bd9Sstevel@tonic-gate 					ntohs(tp->st_sport));
14967c478bd9Sstevel@tonic-gate 				sprintf(str2, "%s,%hu",
1497ab25eeb5Syz 					getip(tp->st_v, &tp->st_dst),
14987c478bd9Sstevel@tonic-gate 					ntohs(tp->st_dport));
14997c478bd9Sstevel@tonic-gate 			} else {
1500ab25eeb5Syz 				sprintf(str1, "%s", getip(tp->st_v,
1501ab25eeb5Syz 				    &tp->st_src));
1502ab25eeb5Syz 				sprintf(str2, "%s", getip(tp->st_v,
1503ab25eeb5Syz 				    &tp->st_dst));
15047c478bd9Sstevel@tonic-gate 			}
1505ab25eeb5Syz 			winy++;
1506ab25eeb5Syz 			move(winy, 0);
1507ab25eeb5Syz 			printw("%-*s %-*s", srclen + 6, str1, dstlen + 6, str2);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 			/* print state */
15107c478bd9Sstevel@tonic-gate 			sprintf(str1, "%X/%X", tp->st_state[0],
15117c478bd9Sstevel@tonic-gate 				tp->st_state[1]);
15127c478bd9Sstevel@tonic-gate 			printw(" %3s", str1);
15137c478bd9Sstevel@tonic-gate 
1514ab25eeb5Syz 			/* print protocol */
15157c478bd9Sstevel@tonic-gate 			proto = getprotobynumber(tp->st_p);
15167c478bd9Sstevel@tonic-gate 			if (proto) {
15177c478bd9Sstevel@tonic-gate 				strncpy(str1, proto->p_name, 4);
15187c478bd9Sstevel@tonic-gate 				str1[4] = '\0';
15197c478bd9Sstevel@tonic-gate 			} else {
15207c478bd9Sstevel@tonic-gate 				sprintf(str1, "%d", tp->st_p);
15217c478bd9Sstevel@tonic-gate 			}
1522ab25eeb5Syz 			/* just print icmp for IPv6-ICMP */
1523ab25eeb5Syz 			if (tp->st_p == IPPROTO_ICMPV6)
1524ab25eeb5Syz 				strcpy(str1, "icmp");
15257c478bd9Sstevel@tonic-gate 			printw(" %4s", str1);
1526ab25eeb5Syz 
1527ab25eeb5Syz 			/* print #pkt/#bytes */
15287c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
15297c478bd9Sstevel@tonic-gate 			printw(" %7qu %9qu", (unsigned long long) tp->st_pkts,
15307c478bd9Sstevel@tonic-gate 				(unsigned long long) tp->st_bytes);
15317c478bd9Sstevel@tonic-gate #else
15327c478bd9Sstevel@tonic-gate 			printw(" %7lu %9lu", tp->st_pkts, tp->st_bytes);
15337c478bd9Sstevel@tonic-gate #endif
15347c478bd9Sstevel@tonic-gate 			printw(" %9s", ttl_to_string(tp->st_age));
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 			if (reverse)
15377c478bd9Sstevel@tonic-gate 				tp--;
15387c478bd9Sstevel@tonic-gate 			else
15397c478bd9Sstevel@tonic-gate 				tp++;
15407c478bd9Sstevel@tonic-gate 		}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 		/* screen data structure is filled, now update the screen */
15437c478bd9Sstevel@tonic-gate 		if (redraw)
15447c478bd9Sstevel@tonic-gate 			clearok(stdscr,1);
15457c478bd9Sstevel@tonic-gate 
1546ab25eeb5Syz 		if (refresh() == ERR)
1547ab25eeb5Syz 			break;
15487c478bd9Sstevel@tonic-gate 		if (redraw) {
15497c478bd9Sstevel@tonic-gate 			clearok(stdscr,0);
15507c478bd9Sstevel@tonic-gate 			redraw = 0;
15517c478bd9Sstevel@tonic-gate 		}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		/* wait for key press or a 1 second time out period */
15547c478bd9Sstevel@tonic-gate 		selecttimeout.tv_sec = refreshtime;
15557c478bd9Sstevel@tonic-gate 		selecttimeout.tv_usec = 0;
15567c478bd9Sstevel@tonic-gate 		FD_ZERO(&readfd);
15577c478bd9Sstevel@tonic-gate 		FD_SET(0, &readfd);
15587c478bd9Sstevel@tonic-gate 		select(1, &readfd, NULL, NULL, &selecttimeout);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		/* if key pressed, read all waiting keys */
15617c478bd9Sstevel@tonic-gate 		if (FD_ISSET(0, &readfd)) {
15627c478bd9Sstevel@tonic-gate 			c = wgetch(stdscr);
15637c478bd9Sstevel@tonic-gate 			if (c == ERR)
15647c478bd9Sstevel@tonic-gate 				continue;
15657c478bd9Sstevel@tonic-gate 
1566ab25eeb5Syz 			if (ISALPHA(c) && ISUPPER(c))
1567ab25eeb5Syz 				c = TOLOWER(c);
1568ab25eeb5Syz 			if (c == 'l') {
15697c478bd9Sstevel@tonic-gate 				redraw = 1;
1570ab25eeb5Syz 			} else if (c == 'q') {
15715e985db5Sschuster 				break;
1572ab25eeb5Syz 			} else if (c == 'r') {
15737c478bd9Sstevel@tonic-gate 				reverse = !reverse;
1574ab25eeb5Syz 			} else if (c == 'b') {
1575ab25eeb5Syz 				forward = 0;
1576ab25eeb5Syz 			} else if (c == 'f') {
1577ab25eeb5Syz 				forward = 1;
1578ab25eeb5Syz 			} else if (c == 's') {
1579ab25eeb5Syz 				if (++sorting > STSORT_MAX)
15807c478bd9Sstevel@tonic-gate 					sorting = 0;
15817c478bd9Sstevel@tonic-gate 			}
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 	} /* while */
15847c478bd9Sstevel@tonic-gate 
1585ab25eeb5Syz out:
15867c478bd9Sstevel@tonic-gate 	printw("\n");
1587c0e86175San 	refresh();
15887c478bd9Sstevel@tonic-gate 	endwin();
1589ab25eeb5Syz 	free(tstable);
1590ab25eeb5Syz 	if (ret != 0)
1591ab25eeb5Syz 		perror(errstr);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate #endif
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate  * Show fragment cache information that's held in the kernel.
15987c478bd9Sstevel@tonic-gate  */
159943412a42SDarren Reed static void showfrstates(ifsp, ticks)
16007c478bd9Sstevel@tonic-gate ipfrstat_t *ifsp;
160143412a42SDarren Reed u_long ticks;
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	struct ipfr *ipfrtab[IPFT_SIZE], ifr;
16047c478bd9Sstevel@tonic-gate 	int i;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	/*
16077c478bd9Sstevel@tonic-gate 	 * print out the numeric statistics
16087c478bd9Sstevel@tonic-gate 	 */
16097c478bd9Sstevel@tonic-gate 	PRINTF("IP fragment states:\n\t%lu new\n\t%lu expired\n\t%lu hits\n",
16107c478bd9Sstevel@tonic-gate 		ifsp->ifs_new, ifsp->ifs_expire, ifsp->ifs_hits);
1611ab25eeb5Syz 	PRINTF("\t%lu retrans\n\t%lu too short\n",
1612ab25eeb5Syz 		ifsp->ifs_retrans0, ifsp->ifs_short);
16137c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu no memory\n\t%lu already exist\n",
16147c478bd9Sstevel@tonic-gate 		ifsp->ifs_nomem, ifsp->ifs_exists);
16157c478bd9Sstevel@tonic-gate 	PRINTF("\t%lu inuse\n", ifsp->ifs_inuse);
16167c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_table, sizeof(ipfrtab)))
16177c478bd9Sstevel@tonic-gate 		return;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/*
16207c478bd9Sstevel@tonic-gate 	 * Print out the contents (if any) of the fragment cache table.
16217c478bd9Sstevel@tonic-gate 	 */
1622ab25eeb5Syz 	PRINTF("\n");
16237c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
1624ab25eeb5Syz 		while (ipfrtab[i] != NULL) {
16257c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
16267c478bd9Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
16277c478bd9Sstevel@tonic-gate 				break;
162843412a42SDarren Reed 			ifr.ipfr_ttl -= ticks;
1629ab25eeb5Syz 			printfraginfo("", &ifr);
16307c478bd9Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
16317c478bd9Sstevel@tonic-gate 		}
1632ab25eeb5Syz 	/*
1633ab25eeb5Syz 	 * Print out the contents (if any) of the NAT fragment cache table.
1634ab25eeb5Syz 	 */
16357c478bd9Sstevel@tonic-gate 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_nattab,sizeof(ipfrtab)))
16367c478bd9Sstevel@tonic-gate 		return;
16377c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPFT_SIZE; i++)
1638ab25eeb5Syz 		while (ipfrtab[i] != NULL) {
16397c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
16407c478bd9Sstevel@tonic-gate 				    sizeof(ifr)) == -1)
16417c478bd9Sstevel@tonic-gate 				break;
164243412a42SDarren Reed 			ifr.ipfr_ttl -= ticks;
1643ab25eeb5Syz 			printfraginfo("NAT: ", &ifr);
16447c478bd9Sstevel@tonic-gate 			ipfrtab[i] = ifr.ipfr_next;
16457c478bd9Sstevel@tonic-gate 		}
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate /*
16507c478bd9Sstevel@tonic-gate  * Show stats on how auth within IPFilter has been used
16517c478bd9Sstevel@tonic-gate  */
16527c478bd9Sstevel@tonic-gate static void showauthstates(asp)
16537c478bd9Sstevel@tonic-gate fr_authstat_t *asp;
16547c478bd9Sstevel@tonic-gate {
16557c478bd9Sstevel@tonic-gate 	frauthent_t *frap, fra;
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate #ifdef	USE_QUAD_T
16587c478bd9Sstevel@tonic-gate 	printf("Authorisation hits: %qu\tmisses %qu\n",
16597c478bd9Sstevel@tonic-gate 		(unsigned long long) asp->fas_hits,
16607c478bd9Sstevel@tonic-gate 		(unsigned long long) asp->fas_miss);
16617c478bd9Sstevel@tonic-gate #else
16627c478bd9Sstevel@tonic-gate 	printf("Authorisation hits: %ld\tmisses %ld\n", asp->fas_hits,
16637c478bd9Sstevel@tonic-gate 		asp->fas_miss);
16647c478bd9Sstevel@tonic-gate #endif
16657c478bd9Sstevel@tonic-gate 	printf("nospace %ld\nadded %ld\nsendfail %ld\nsendok %ld\n",
16667c478bd9Sstevel@tonic-gate 		asp->fas_nospace, asp->fas_added, asp->fas_sendfail,
16677c478bd9Sstevel@tonic-gate 		asp->fas_sendok);
16687c478bd9Sstevel@tonic-gate 	printf("queok %ld\nquefail %ld\nexpire %ld\n",
16697c478bd9Sstevel@tonic-gate 		asp->fas_queok, asp->fas_quefail, asp->fas_expire);
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	frap = asp->fas_faelist;
16727c478bd9Sstevel@tonic-gate 	while (frap) {
16737c478bd9Sstevel@tonic-gate 		if (kmemcpy((char *)&fra, (u_long)frap, sizeof(fra)) == -1)
16747c478bd9Sstevel@tonic-gate 			break;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		printf("age %ld\t", fra.fae_age);
16777c478bd9Sstevel@tonic-gate 		printfr(&fra.fae_fr, ioctl);
16787c478bd9Sstevel@tonic-gate 		frap = fra.fae_next;
16797c478bd9Sstevel@tonic-gate 	}
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate /*
16847c478bd9Sstevel@tonic-gate  * Display groups used for each of filter rules, accounting rules and
16857c478bd9Sstevel@tonic-gate  * authentication, separately.
16867c478bd9Sstevel@tonic-gate  */
16877c478bd9Sstevel@tonic-gate static void showgroups(fiop)
16887c478bd9Sstevel@tonic-gate struct friostat	*fiop;
16897c478bd9Sstevel@tonic-gate {
16907c478bd9Sstevel@tonic-gate 	static char *gnames[3] = { "Filter", "Accounting", "Authentication" };
1691ab25eeb5Syz 	static int gnums[3] = { IPL_LOGIPF, IPL_LOGCOUNT, IPL_LOGAUTH };
16927c478bd9Sstevel@tonic-gate 	frgroup_t *fp, grp;
16937c478bd9Sstevel@tonic-gate 	int on, off, i;
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	on = fiop->f_active;
16967c478bd9Sstevel@tonic-gate 	off = 1 - on;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
16997c478bd9Sstevel@tonic-gate 		printf("%s groups (active):\n", gnames[i]);
1700ab25eeb5Syz 		for (fp = fiop->f_groups[gnums[i]][on]; fp != NULL;
1701ab25eeb5Syz 		     fp = grp.fg_next)
17027c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
17037c478bd9Sstevel@tonic-gate 				break;
17047c478bd9Sstevel@tonic-gate 			else
17057c478bd9Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
17067c478bd9Sstevel@tonic-gate 		printf("%s groups (inactive):\n", gnames[i]);
1707ab25eeb5Syz 		for (fp = fiop->f_groups[gnums[i]][off]; fp != NULL;
1708ab25eeb5Syz 		     fp = grp.fg_next)
17097c478bd9Sstevel@tonic-gate 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
17107c478bd9Sstevel@tonic-gate 				break;
17117c478bd9Sstevel@tonic-gate 			else
17127c478bd9Sstevel@tonic-gate 				printf("%s\n", grp.fg_name);
17137c478bd9Sstevel@tonic-gate 	}
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate static void parse_ipportstr(argument, ip, port)
17177c478bd9Sstevel@tonic-gate const char *argument;
1718ab25eeb5Syz i6addr_t *ip;
17197c478bd9Sstevel@tonic-gate int *port;
17207c478bd9Sstevel@tonic-gate {
17217c478bd9Sstevel@tonic-gate 	char *s, *comma;
1722ab25eeb5Syz 	int ok = 0;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	/* make working copy of argument, Theoretically you must be able
17257c478bd9Sstevel@tonic-gate 	 * to write to optarg, but that seems very ugly to me....
17267c478bd9Sstevel@tonic-gate 	 */
17277c478bd9Sstevel@tonic-gate 	s = strdup(argument);
17287c478bd9Sstevel@tonic-gate 	if (s == NULL)
17297c478bd9Sstevel@tonic-gate 		return;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	/* get port */
17327c478bd9Sstevel@tonic-gate 	if ((comma = strchr(s, ',')) != NULL) {
17337c478bd9Sstevel@tonic-gate 		if (!strcasecmp(comma + 1, "any")) {
17347c478bd9Sstevel@tonic-gate 			*port = -1;
17357c478bd9Sstevel@tonic-gate 		} else if (!sscanf(comma + 1, "%d", port) ||
17367c478bd9Sstevel@tonic-gate 			   (*port < 0) || (*port > 65535)) {
17377c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Invalid port specfication in %s\n",
17387c478bd9Sstevel@tonic-gate 				argument);
17397c478bd9Sstevel@tonic-gate 			free(s);
17407c478bd9Sstevel@tonic-gate 			exit(-2);
17417c478bd9Sstevel@tonic-gate 		}
17427c478bd9Sstevel@tonic-gate 		*comma = '\0';
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/* get ip address */
17477c478bd9Sstevel@tonic-gate 	if (!strcasecmp(s, "any")) {
1748ab25eeb5Syz 		ip->in4.s_addr = INADDR_ANY;
1749ab25eeb5Syz #ifdef	USE_INET6
1750ab25eeb5Syz 		ip->in6 = in6addr_any;
1751ab25eeb5Syz 	} else if (use_inet6 && inet_pton(AF_INET6, s, &ip->in6)) {
1752ab25eeb5Syz 		ok = 1;
1753ab25eeb5Syz #endif
1754ab25eeb5Syz 	} else if (inet_aton(s, &ip->in4))
1755ab25eeb5Syz 		ok = 1;
1756ab25eeb5Syz 
1757ab25eeb5Syz 	if (ok == 0) {
17587c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Invalid IP address: %s\n", s);
17597c478bd9Sstevel@tonic-gate 		free(s);
17607c478bd9Sstevel@tonic-gate 		exit(-2);
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/* free allocated memory */
17647c478bd9Sstevel@tonic-gate 	free(s);
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate #ifdef STATETOP
1769ab25eeb5Syz static void sig_resize(s)
1770ab25eeb5Syz int s;
1771ab25eeb5Syz {
1772ab25eeb5Syz 	handle_resize = 1;
1773ab25eeb5Syz }
1774ab25eeb5Syz 
1775ab25eeb5Syz static void sig_break(s)
1776ab25eeb5Syz int s;
1777ab25eeb5Syz {
1778ab25eeb5Syz 	handle_break = 1;
1779ab25eeb5Syz }
1780ab25eeb5Syz 
1781ab25eeb5Syz static char *getip(v, addr)
1782ab25eeb5Syz int v;
1783ab25eeb5Syz i6addr_t *addr;
1784ab25eeb5Syz {
1785ab25eeb5Syz #ifdef  USE_INET6
1786ab25eeb5Syz 	static char hostbuf[MAXHOSTNAMELEN+1];
1787ab25eeb5Syz #endif
1788ab25eeb5Syz 
1789ab25eeb5Syz 	if (v == 4)
1790ab25eeb5Syz 		return inet_ntoa(addr->in4);
1791ab25eeb5Syz 
1792ab25eeb5Syz #ifdef  USE_INET6
1793ab25eeb5Syz 	(void) inet_ntop(AF_INET6, &addr->in6, hostbuf, sizeof(hostbuf) - 1);
1794ab25eeb5Syz 	hostbuf[MAXHOSTNAMELEN] = '\0';
1795ab25eeb5Syz 	return hostbuf;
1796ab25eeb5Syz #else
1797ab25eeb5Syz 	return "IPv6";
1798ab25eeb5Syz #endif
1799ab25eeb5Syz }
1800ab25eeb5Syz 
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate static char *ttl_to_string(ttl)
18037c478bd9Sstevel@tonic-gate long int ttl;
18047c478bd9Sstevel@tonic-gate {
1805ab25eeb5Syz 	static char ttlbuf[STSTRSIZE];
18067c478bd9Sstevel@tonic-gate 	int hours, minutes, seconds;
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	/* ttl is in half seconds */
18097c478bd9Sstevel@tonic-gate 	ttl /= 2;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	hours = ttl / 3600;
18127c478bd9Sstevel@tonic-gate 	ttl = ttl % 3600;
18137c478bd9Sstevel@tonic-gate 	minutes = ttl / 60;
18147c478bd9Sstevel@tonic-gate 	seconds = ttl % 60;
18157c478bd9Sstevel@tonic-gate 
1816ab25eeb5Syz 	if (hours > 0)
18177c478bd9Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d:%02d", hours, minutes, seconds);
18187c478bd9Sstevel@tonic-gate 	else
18197c478bd9Sstevel@tonic-gate 		sprintf(ttlbuf, "%2d:%02d", minutes, seconds);
18207c478bd9Sstevel@tonic-gate 	return ttlbuf;
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate static int sort_pkts(a, b)
18257c478bd9Sstevel@tonic-gate const void *a;
18267c478bd9Sstevel@tonic-gate const void *b;
18277c478bd9Sstevel@tonic-gate {
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
18307c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	if (ap->st_pkts == bp->st_pkts)
18337c478bd9Sstevel@tonic-gate 		return 0;
18347c478bd9Sstevel@tonic-gate 	else if (ap->st_pkts < bp->st_pkts)
18357c478bd9Sstevel@tonic-gate 		return 1;
18367c478bd9Sstevel@tonic-gate 	return -1;
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate static int sort_bytes(a, b)
18417c478bd9Sstevel@tonic-gate const void *a;
18427c478bd9Sstevel@tonic-gate const void *b;
18437c478bd9Sstevel@tonic-gate {
18447c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
18457c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if (ap->st_bytes == bp->st_bytes)
18487c478bd9Sstevel@tonic-gate 		return 0;
18497c478bd9Sstevel@tonic-gate 	else if (ap->st_bytes < bp->st_bytes)
18507c478bd9Sstevel@tonic-gate 		return 1;
18517c478bd9Sstevel@tonic-gate 	return -1;
18527c478bd9Sstevel@tonic-gate }
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate static int sort_p(a, b)
18567c478bd9Sstevel@tonic-gate const void *a;
18577c478bd9Sstevel@tonic-gate const void *b;
18587c478bd9Sstevel@tonic-gate {
18597c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
18607c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	if (ap->st_p == bp->st_p)
18637c478bd9Sstevel@tonic-gate 		return 0;
18647c478bd9Sstevel@tonic-gate 	else if (ap->st_p < bp->st_p)
18657c478bd9Sstevel@tonic-gate 		return 1;
18667c478bd9Sstevel@tonic-gate 	return -1;
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static int sort_ttl(a, b)
18717c478bd9Sstevel@tonic-gate const void *a;
18727c478bd9Sstevel@tonic-gate const void *b;
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
18757c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if (ap->st_age == bp->st_age)
18787c478bd9Sstevel@tonic-gate 		return 0;
18797c478bd9Sstevel@tonic-gate 	else if (ap->st_age < bp->st_age)
18807c478bd9Sstevel@tonic-gate 		return 1;
18817c478bd9Sstevel@tonic-gate 	return -1;
18827c478bd9Sstevel@tonic-gate }
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate static int sort_srcip(a, b)
18857c478bd9Sstevel@tonic-gate const void *a;
18867c478bd9Sstevel@tonic-gate const void *b;
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
18897c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
18907c478bd9Sstevel@tonic-gate 
1891ab25eeb5Syz #ifdef USE_INET6
1892ab25eeb5Syz 	if (use_inet6) {
1893ab25eeb5Syz 		if (IP6_EQ(&ap->st_src, &bp->st_src))
1894ab25eeb5Syz 			return 0;
1895ab25eeb5Syz 		else if (IP6_GT(&ap->st_src, &bp->st_src))
1896ab25eeb5Syz 			return 1;
1897ab25eeb5Syz 	} else
1898ab25eeb5Syz #endif
1899ab25eeb5Syz 	{
1900ab25eeb5Syz 		if (ntohl(ap->st_src.in4.s_addr) ==
1901ab25eeb5Syz 		    ntohl(bp->st_src.in4.s_addr))
1902ab25eeb5Syz 			return 0;
1903ab25eeb5Syz 		else if (ntohl(ap->st_src.in4.s_addr) >
1904ab25eeb5Syz 		         ntohl(bp->st_src.in4.s_addr))
1905ab25eeb5Syz 			return 1;
1906ab25eeb5Syz 	}
1907ab25eeb5Syz 	return -1;
1908ab25eeb5Syz }
1909ab25eeb5Syz 
1910ab25eeb5Syz static int sort_srcpt(a, b)
1911ab25eeb5Syz const void *a;
1912ab25eeb5Syz const void *b;
1913ab25eeb5Syz {
1914ab25eeb5Syz 	register const statetop_t *ap = a;
1915ab25eeb5Syz 	register const statetop_t *bp = b;
1916ab25eeb5Syz 
1917ab25eeb5Syz 	if (htons(ap->st_sport) == htons(bp->st_sport))
19187c478bd9Sstevel@tonic-gate 		return 0;
1919ab25eeb5Syz 	else if (htons(ap->st_sport) > htons(bp->st_sport))
19207c478bd9Sstevel@tonic-gate 		return 1;
19217c478bd9Sstevel@tonic-gate 	return -1;
19227c478bd9Sstevel@tonic-gate }
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate static int sort_dstip(a, b)
19257c478bd9Sstevel@tonic-gate const void *a;
19267c478bd9Sstevel@tonic-gate const void *b;
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate 	register const statetop_t *ap = a;
19297c478bd9Sstevel@tonic-gate 	register const statetop_t *bp = b;
19307c478bd9Sstevel@tonic-gate 
1931ab25eeb5Syz #ifdef USE_INET6
1932ab25eeb5Syz 	if (use_inet6) {
1933ab25eeb5Syz 		if (IP6_EQ(&ap->st_dst, &bp->st_dst))
1934ab25eeb5Syz 			return 0;
1935ab25eeb5Syz 		else if (IP6_GT(&ap->st_dst, &bp->st_dst))
1936ab25eeb5Syz 			return 1;
1937ab25eeb5Syz 	} else
1938ab25eeb5Syz #endif
1939ab25eeb5Syz 	{
1940ab25eeb5Syz 		if (ntohl(ap->st_dst.in4.s_addr) ==
1941ab25eeb5Syz 		    ntohl(bp->st_dst.in4.s_addr))
1942ab25eeb5Syz 			return 0;
1943ab25eeb5Syz 		else if (ntohl(ap->st_dst.in4.s_addr) >
1944ab25eeb5Syz 		         ntohl(bp->st_dst.in4.s_addr))
1945ab25eeb5Syz 			return 1;
1946ab25eeb5Syz 	}
1947ab25eeb5Syz 	return -1;
1948ab25eeb5Syz }
1949ab25eeb5Syz 
1950ab25eeb5Syz static int sort_dstpt(a, b)
1951ab25eeb5Syz const void *a;
1952ab25eeb5Syz const void *b;
1953ab25eeb5Syz {
1954ab25eeb5Syz 	register const statetop_t *ap = a;
1955ab25eeb5Syz 	register const statetop_t *bp = b;
1956ab25eeb5Syz 
1957ab25eeb5Syz 	if (htons(ap->st_dport) == htons(bp->st_dport))
19587c478bd9Sstevel@tonic-gate 		return 0;
1959ab25eeb5Syz 	else if (htons(ap->st_dport) > htons(bp->st_dport))
19607c478bd9Sstevel@tonic-gate 		return 1;
19617c478bd9Sstevel@tonic-gate 	return -1;
19627c478bd9Sstevel@tonic-gate }
1963ab25eeb5Syz 
19647c478bd9Sstevel@tonic-gate #endif
1965