xref: /titanic_41/usr/src/cmd/ipf/tools/ipfstat.c (revision 2278144afd2005b3eabcfd9bc412fe5ceb78749d)
1 /*
2  * Copyright (C) 1993-2001, 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
7  * Use is subject to license terms.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #ifdef __FreeBSD__
13 # ifndef __FreeBSD_cc_version
14 #  include <osreldate.h>
15 # else
16 #  if __FreeBSD_cc_version < 430000
17 #   include <osreldate.h>
18 #  endif
19 # endif
20 #endif
21 #include <sys/ioctl.h>
22 #include <fcntl.h>
23 #ifdef linux
24 # include <linux/a.out.h>
25 #else
26 # include <nlist.h>
27 #endif
28 #include <ctype.h>
29 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
30 # include <stddef.h>
31 #endif
32 #include "ipf.h"
33 #include "netinet/ipl.h"
34 #if defined(STATETOP)
35 # if defined(_BSDI_VERSION)
36 #  undef STATETOP
37 # endif
38 # if defined(__FreeBSD__) && \
39      (!defined(__FreeBSD_version) || (__FreeBSD_version < 430000))
40 #  undef STATETOP
41 # endif
42 # if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 105000000)
43 #  undef STATETOP
44 # endif
45 # if defined(sun)
46 #  if defined(__svr4__) || defined(__SVR4)
47 #   include <sys/select.h>
48 #  else
49 #   undef STATETOP	/* NOT supported on SunOS4 */
50 #  endif
51 # endif
52 #endif
53 #if defined(STATETOP) && !defined(linux)
54 # include <netinet/ip_var.h>
55 # include <netinet/tcp_fsm.h>
56 #endif
57 #ifdef STATETOP
58 # include <ctype.h>
59 # include <signal.h>
60 # if SOLARIS || defined(__NetBSD__) || defined(_BSDI_VERSION) || \
61      defined(__sgi)
62 #  ifdef ERR
63 #   undef ERR
64 #  endif
65 #  undef ISASCII
66 #  undef ISPRINT
67 #  include <curses.h>
68 # else /* SOLARIS */
69 #  include <ncurses.h>
70 # endif /* SOLARIS */
71 #endif /* STATETOP */
72 #include "kmem.h"
73 #if defined(__NetBSD__) || (__OpenBSD__)
74 # include <paths.h>
75 #endif
76 
77 #if !defined(lint)
78 static const char sccsid[] = "@(#)fils.c	1.21 4/20/96 (C) 1993-2000 Darren Reed";
79 static const char rcsid[] = "@(#)$Id: ipfstat.c,v 1.44.2.12 2005/06/12 07:18:46 darrenr Exp $";
80 #endif
81 
82 #ifdef __hpux
83 # define	nlist	nlist64
84 #endif
85 
86 extern	char	*optarg;
87 extern	int	optind;
88 extern	int	opterr;
89 
90 #define	PRINTF	(void)printf
91 #define	FPRINTF	(void)fprintf
92 #define	F_IN	0
93 #define	F_OUT	1
94 #define	F_ACIN	2
95 #define	F_ACOUT	3
96 static	char	*filters[4] = { "ipfilter(in)", "ipfilter(out)",
97 				"ipacct(in)", "ipacct(out)" };
98 static	int	state_logging = -1;
99 
100 int	opts = 0;
101 int	use_inet6 = 0;
102 int	live_kernel = 1;
103 int	state_fd = -1;
104 int	ipf_fd = -1;
105 
106 #ifdef STATETOP
107 #define	STSTRSIZE 	80
108 #define	STGROWSIZE	16
109 #define	HOSTNMLEN	40
110 
111 #define	STSORT_PR	0
112 #define	STSORT_PKTS	1
113 #define	STSORT_BYTES	2
114 #define	STSORT_TTL	3
115 #define	STSORT_SRCIP	4
116 #define	STSORT_SRCPT	5
117 #define	STSORT_DSTIP	6
118 #define	STSORT_DSTPT	7
119 #define	STSORT_MAX	STSORT_DSTPT
120 #define	STSORT_DEFAULT	STSORT_BYTES
121 
122 
123 typedef struct statetop {
124 	i6addr_t	st_src;
125 	i6addr_t	st_dst;
126 	u_short		st_sport;
127 	u_short 	st_dport;
128 	u_char		st_p;
129 	u_char		st_v;
130 	u_char		st_state[2];
131 	U_QUAD_T	st_pkts;
132 	U_QUAD_T	st_bytes;
133 	u_long		st_age;
134 } statetop_t;
135 #endif
136 
137 int	main __P((int, char *[]));
138 
139 static	void	showstats __P((friostat_t *, u_32_t));
140 static	void	showfrstates __P((ipfrstat_t *));
141 static	void	showlist __P((friostat_t *));
142 static	void	showipstates __P((ips_stat_t *));
143 static	void	showauthstates __P((fr_authstat_t *));
144 static	void	showgroups __P((friostat_t *));
145 static	void	usage __P((char *));
146 static	void	printlist __P((frentry_t *, char *));
147 static	void	parse_ipportstr __P((const char *, i6addr_t *, int *));
148 static	void	ipfstate_live __P((char *, friostat_t **, ips_stat_t **,
149 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
150 static	void	ipfstate_dead __P((char *, friostat_t **, ips_stat_t **,
151 				   ipfrstat_t **, fr_authstat_t **, u_32_t *));
152 #ifdef STATETOP
153 static	void	topipstates __P((i6addr_t, i6addr_t, int, int, int,
154 				 int, int, int));
155 static	void	sig_break __P((int));
156 static	void	sig_resize __P((int));
157 static	char	*getip __P((int, i6addr_t *));
158 static	char	*ttl_to_string __P((long));
159 static	int	sort_p __P((const void *, const void *));
160 static	int	sort_pkts __P((const void *, const void *));
161 static	int	sort_bytes __P((const void *, const void *));
162 static	int	sort_ttl __P((const void *, const void *));
163 static	int	sort_srcip __P((const void *, const void *));
164 static	int	sort_srcpt __P((const void *, const void *));
165 static	int	sort_dstip __P((const void *, const void *));
166 static	int	sort_dstpt __P((const void *, const void *));
167 #endif
168 
169 
170 static void usage(name)
171 char *name;
172 {
173 #ifdef  USE_INET6
174 	fprintf(stderr, "Usage: %s [-6aAdfghIilnoRsv]\n", name);
175 #else
176 	fprintf(stderr, "Usage: %s [-aAdfghIilnoRsv]\n", name);
177 #endif
178 	fprintf(stderr, "       %s [-M corefile] [-N symbol-list]\n", name);
179 #ifdef	USE_INET6
180 	fprintf(stderr, "       %s -t [-6C] ", name);
181 #else
182 	fprintf(stderr, "       %s -t [-C] ", name);
183 #endif
184 	fprintf(stderr, "[-D destination address] [-P protocol] [-S source address] [-T refresh time]\n");
185 	exit(1);
186 }
187 
188 
189 int main(argc,argv)
190 int argc;
191 char *argv[];
192 {
193 	fr_authstat_t	frauthst;
194 	fr_authstat_t	*frauthstp = &frauthst;
195 	friostat_t fio;
196 	friostat_t *fiop = &fio;
197 	ips_stat_t ipsst;
198 	ips_stat_t *ipsstp = &ipsst;
199 	ipfrstat_t ifrst;
200 	ipfrstat_t *ifrstp = &ifrst;
201 	char	*device = IPL_NAME, *memf = NULL;
202 	char	*options, *kern = NULL;
203 	int	c, myoptind;
204 
205 	int protocol = -1;		/* -1 = wild card for any protocol */
206 	int refreshtime = 1; 		/* default update time */
207 	int sport = -1;			/* -1 = wild card for any source port */
208 	int dport = -1;			/* -1 = wild card for any dest port */
209 	int topclosed = 0;		/* do not show closed tcp sessions */
210 	i6addr_t saddr, daddr;
211 	u_32_t frf;
212 
213 #ifdef	USE_INET6
214 	options = "6aACdfghIilnostvD:M:N:P:RS:T:";
215 #else
216 	options = "aACdfghIilnostvD:M:N:P:RS:T:";
217 #endif
218 
219 	saddr.in4.s_addr = INADDR_ANY; 	/* default any v4 source addr */
220 	daddr.in4.s_addr = INADDR_ANY; 	/* default any v4 dest addr */
221 #ifdef	USE_INET6
222 	saddr.in6 = in6addr_any;	/* default any v6 source addr */
223 	daddr.in6 = in6addr_any;	/* default any v6 dest addr */
224 #endif
225 
226 	/* Don't warn about invalid flags when we run getopt for the 1st time */
227 	opterr = 0;
228 
229 	/*
230 	 * Parse these two arguments now lest there be any buffer overflows
231 	 * in the parsing of the rest.
232 	 */
233 	myoptind = optind;
234 	while ((c = getopt(argc, argv, options)) != -1) {
235 		switch (c)
236 		{
237 		case 'M' :
238 			memf = optarg;
239 			live_kernel = 0;
240 			break;
241 		case 'N' :
242 			kern = optarg;
243 			live_kernel = 0;
244 			break;
245 		}
246 	}
247 	optind = myoptind;
248 
249 	if (live_kernel == 1) {
250 		if ((state_fd = open(IPSTATE_NAME, O_RDONLY)) == -1) {
251 			perror("open(IPSTATE_NAME)");
252 			exit(-1);
253 		}
254 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
255 			fprintf(stderr, "open(%s)", device);
256 			perror("");
257 			exit(-1);
258 		}
259 	}
260 
261 	if (kern != NULL || memf != NULL) {
262 		(void)setgid(getgid());
263 		(void)setreuid(getuid(), getuid());
264 	}
265 
266 	if (live_kernel == 1)
267 		(void) checkrev(device);
268 	if (openkmem(kern, memf) == -1)
269 		exit(-1);
270 
271 	(void)setgid(getgid());
272 	(void)setreuid(getuid(), getuid());
273 
274 	opterr = 1;
275 
276 	while ((c = getopt(argc, argv, options)) != -1)
277 	{
278 		switch (c)
279 		{
280 #ifdef	USE_INET6
281 		case '6' :
282 			use_inet6 = 1;
283 			break;
284 #endif
285 		case 'a' :
286 			opts |= OPT_ACCNT|OPT_SHOWLIST;
287 			break;
288 		case 'A' :
289 			opts |= OPT_AUTHSTATS;
290 			break;
291 		case 'C' :
292 			topclosed = 1;
293 			break;
294 		case 'd' :
295 			opts |= OPT_DEBUG;
296 			break;
297 		case 'D' :
298 			parse_ipportstr(optarg, &daddr, &dport);
299 			break;
300 		case 'f' :
301 			opts |= OPT_FRSTATES;
302 			break;
303 		case 'g' :
304 			opts |= OPT_GROUPS;
305 			break;
306 		case 'h' :
307 			opts |= OPT_HITS;
308 			break;
309 		case 'i' :
310 			opts |= OPT_INQUE|OPT_SHOWLIST;
311 			break;
312 		case 'I' :
313 			opts |= OPT_INACTIVE;
314 			break;
315 		case 'l' :
316 			opts |= OPT_SHOWLIST;
317 			break;
318 		case 'M' :
319 			break;
320 		case 'N' :
321 			break;
322 		case 'n' :
323 			opts |= OPT_SHOWLINENO;
324 			break;
325 		case 'o' :
326 			opts |= OPT_OUTQUE|OPT_SHOWLIST;
327 			break;
328 		case 'P' :
329 			protocol = getproto(optarg);
330 			if (protocol == -1) {
331 				fprintf(stderr, "%s: Invalid protocol: %s\n",
332 					argv[0], optarg);
333 				exit(-2);
334 			}
335 			break;
336 		case 'R' :
337 			opts |= OPT_NORESOLVE;
338 			break;
339 		case 's' :
340 			opts |= OPT_IPSTATES;
341 			break;
342 		case 'S' :
343 			parse_ipportstr(optarg, &saddr, &sport);
344 			break;
345 		case 't' :
346 #ifdef STATETOP
347 			opts |= OPT_STATETOP;
348 			break;
349 #else
350 			fprintf(stderr,
351 				"%s: state top facility not compiled in\n",
352 				argv[0]);
353 			exit(-2);
354 #endif
355 		case 'T' :
356 			if (!sscanf(optarg, "%d", &refreshtime) ||
357 				    (refreshtime <= 0)) {
358 				fprintf(stderr,
359 					"%s: Invalid refreshtime < 1 : %s\n",
360 					argv[0], optarg);
361 				exit(-2);
362 			}
363 			break;
364 		case 'v' :
365 			opts |= OPT_VERBOSE;
366 			opts |= OPT_UNDEF;
367 			break;
368 		default :
369 			usage(argv[0]);
370 			break;
371 		}
372 	}
373 
374 	if (live_kernel == 1) {
375 		bzero((char *)&fio, sizeof(fio));
376 		bzero((char *)&ipsst, sizeof(ipsst));
377 		bzero((char *)&ifrst, sizeof(ifrst));
378 
379 		ipfstate_live(device, &fiop, &ipsstp, &ifrstp,
380 			      &frauthstp, &frf);
381 	} else
382 		ipfstate_dead(kern, &fiop, &ipsstp, &ifrstp, &frauthstp, &frf);
383 
384 	if (opts & OPT_IPSTATES) {
385 		showipstates(ipsstp);
386 	} else if (opts & OPT_SHOWLIST) {
387 		showlist(fiop);
388 		if ((opts & OPT_OUTQUE) && (opts & OPT_INQUE)){
389 			opts &= ~OPT_OUTQUE;
390 			showlist(fiop);
391 		}
392 	} else if (opts & OPT_FRSTATES)
393 		showfrstates(ifrstp);
394 #ifdef STATETOP
395 	else if (opts & OPT_STATETOP)
396 		topipstates(saddr, daddr, sport, dport, protocol,
397 			    use_inet6 ? 6 : 4, refreshtime, topclosed);
398 #endif
399 	else if (opts & OPT_AUTHSTATS)
400 		showauthstates(frauthstp);
401 	else if (opts & OPT_GROUPS)
402 		showgroups(fiop);
403 	else
404 		showstats(fiop, frf);
405 
406 	return 0;
407 }
408 
409 
410 /*
411  * Fill in the stats structures from the live kernel, using a combination
412  * of ioctl's and copying directly from kernel memory.
413  */
414 static void ipfstate_live(device, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
415 char *device;
416 friostat_t **fiopp;
417 ips_stat_t **ipsstpp;
418 ipfrstat_t **ifrstpp;
419 fr_authstat_t **frauthstpp;
420 u_32_t *frfp;
421 {
422 	ipfobj_t ipfo;
423 
424 	if (checkrev(device) == -1) {
425 		fprintf(stderr, "User/kernel version check failed\n");
426 		exit(1);
427 	}
428 
429 	if ((opts & OPT_AUTHSTATS) == 0) {
430 		bzero((caddr_t)&ipfo, sizeof(ipfo));
431 		ipfo.ipfo_rev = IPFILTER_VERSION;
432 		ipfo.ipfo_size = sizeof(friostat_t);
433 		ipfo.ipfo_ptr = (void *)*fiopp;
434 		ipfo.ipfo_type = IPFOBJ_IPFSTAT;
435 
436 		if (ioctl(ipf_fd, SIOCGETFS, &ipfo) == -1) {
437 			perror("ioctl(ipf:SIOCGETFS)");
438 			exit(-1);
439 		}
440 
441 		if (ioctl(ipf_fd, SIOCGETFF, frfp) == -1)
442 			perror("ioctl(SIOCGETFF)");
443 	}
444 
445 	if ((opts & OPT_IPSTATES) != 0) {
446 
447 		bzero((caddr_t)&ipfo, sizeof(ipfo));
448 		ipfo.ipfo_rev = IPFILTER_VERSION;
449 		ipfo.ipfo_size = sizeof(ips_stat_t);
450 		ipfo.ipfo_ptr = (void *)*ipsstpp;
451 		ipfo.ipfo_type = IPFOBJ_STATESTAT;
452 
453 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
454 			perror("ioctl(state:SIOCGETFS)");
455 			exit(-1);
456 		}
457 		if (ioctl(state_fd, SIOCGETLG, &state_logging) == -1) {
458 			perror("ioctl(state:SIOCGETLG)");
459 			exit(-1);
460 		}
461 	}
462 
463 	if ((opts & OPT_FRSTATES) != 0) {
464 		bzero((caddr_t)&ipfo, sizeof(ipfo));
465 		ipfo.ipfo_rev = IPFILTER_VERSION;
466 		ipfo.ipfo_size = sizeof(ipfrstat_t);
467 		ipfo.ipfo_ptr = (void *)*ifrstpp;
468 		ipfo.ipfo_type = IPFOBJ_FRAGSTAT;
469 
470 		if (ioctl(ipf_fd, SIOCGFRST, &ipfo) == -1) {
471 			perror("ioctl(SIOCGFRST)");
472 			exit(-1);
473 		}
474 	}
475 
476 	if (opts & OPT_VERBOSE)
477 		PRINTF("opts %#x name %s\n", opts, device);
478 
479 	if ((opts & OPT_AUTHSTATS) != 0) {
480 		if (ipf_fd >= 0) {
481 			close(ipf_fd);
482 			ipf_fd = -1;
483 		}
484 		device = IPAUTH_NAME;
485 		if ((ipf_fd = open(device, O_RDONLY)) == -1) {
486 			perror("open");
487 			exit(-1);
488 		}
489 
490 		bzero((caddr_t)&ipfo, sizeof(ipfo));
491 		ipfo.ipfo_rev = IPFILTER_VERSION;
492 		ipfo.ipfo_size = sizeof(fr_authstat_t);
493 		ipfo.ipfo_ptr = (void *)*frauthstpp;
494 		ipfo.ipfo_type = IPFOBJ_AUTHSTAT;
495 
496 	    	if (ioctl(ipf_fd, SIOCATHST, &ipfo) == -1) {
497 			perror("ioctl(SIOCATHST)");
498 			exit(-1);
499 		}
500 	}
501 }
502 
503 
504 /*
505  * Build up the stats structures from data held in the "core" memory.
506  * This is mainly useful when looking at data in crash dumps and ioctl's
507  * just won't work any more.
508  */
509 static void ipfstate_dead(kernel, fiopp, ipsstpp, ifrstpp, frauthstpp, frfp)
510 char *kernel;
511 friostat_t **fiopp;
512 ips_stat_t **ipsstpp;
513 ipfrstat_t **ifrstpp;
514 fr_authstat_t **frauthstpp;
515 u_32_t *frfp;
516 {
517 	static fr_authstat_t frauthst, *frauthstp;
518 	static ips_stat_t ipsst, *ipsstp;
519 	static ipfrstat_t ifrst, *ifrstp;
520 	static friostat_t fio, *fiop;
521 	int temp;
522 
523 	void *rules[2][2];
524 	struct nlist deadlist[43] = {
525 		{ "fr_authstats" },		/* 0 */
526 		{ "fae_list" },
527 		{ "ipauth" },
528 		{ "fr_authlist" },
529 		{ "fr_authstart" },
530 		{ "fr_authend" },		/* 5 */
531 		{ "fr_authnext" },
532 		{ "fr_auth" },
533 		{ "fr_authused" },
534 		{ "fr_authsize" },
535 		{ "fr_defaultauthage" },	/* 10 */
536 		{ "fr_authpkts" },
537 		{ "fr_auth_lock" },
538 		{ "frstats" },
539 		{ "ips_stats" },
540 		{ "ips_num" },			/* 15 */
541 		{ "ips_wild" },
542 		{ "ips_list" },
543 		{ "ips_table" },
544 		{ "fr_statemax" },
545 		{ "fr_statesize" },		/* 20 */
546 		{ "fr_state_doflush" },
547 		{ "fr_state_lock" },
548 		{ "ipfr_heads" },
549 		{ "ipfr_nattab" },
550 		{ "ipfr_stats" },		/* 25 */
551 		{ "ipfr_inuse" },
552 		{ "fr_ipfrttl" },
553 		{ "fr_frag_lock" },
554 		{ "ipfr_timer_id" },
555 		{ "fr_nat_lock" },		/* 30 */
556 		{ "ipfilter" },
557 		{ "ipfilter6" },
558 		{ "ipacct" },
559 		{ "ipacct6" },
560 		{ "ipl_frouteok" },		/* 35 */
561 		{ "fr_running" },
562 		{ "ipfgroups" },
563 		{ "fr_active" },
564 		{ "fr_pass" },
565 		{ "fr_flags" },			/* 40 */
566 		{ "ipstate_logging" },
567 		{ NULL }
568 	};
569 
570 
571 	frauthstp = &frauthst;
572 	ipsstp = &ipsst;
573 	ifrstp = &ifrst;
574 	fiop = &fio;
575 
576 	*frfp = 0;
577 	*fiopp = fiop;
578 	*ipsstpp = ipsstp;
579 	*ifrstpp = ifrstp;
580 	*frauthstpp = frauthstp;
581 
582 	bzero((char *)fiop, sizeof(*fiop));
583 	bzero((char *)ipsstp, sizeof(*ipsstp));
584 	bzero((char *)ifrstp, sizeof(*ifrstp));
585 	bzero((char *)frauthstp, sizeof(*frauthstp));
586 
587 	if (nlist(kernel, deadlist) == -1) {
588 		fprintf(stderr, "nlist error\n");
589 		return;
590 	}
591 
592 	/*
593 	 * This is for SIOCGETFF.
594 	 */
595 	kmemcpy((char *)frfp, (u_long)deadlist[40].n_value, sizeof(*frfp));
596 
597 	/*
598 	 * f_locks is a combination of the lock variable from each part of
599 	 * ipfilter (state, auth, nat, fragments).
600 	 */
601 	kmemcpy((char *)fiop, (u_long)deadlist[13].n_value, sizeof(*fiop));
602 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[22].n_value,
603 		sizeof(fiop->f_locks[0]));
604 	kmemcpy((char *)&fiop->f_locks[0], (u_long)deadlist[30].n_value,
605 		sizeof(fiop->f_locks[1]));
606 	kmemcpy((char *)&fiop->f_locks[2], (u_long)deadlist[28].n_value,
607 		sizeof(fiop->f_locks[2]));
608 	kmemcpy((char *)&fiop->f_locks[3], (u_long)deadlist[12].n_value,
609 		sizeof(fiop->f_locks[3]));
610 
611 	/*
612 	 * Get pointers to each list of rules (active, inactive, in, out)
613 	 */
614 	kmemcpy((char *)&rules, (u_long)deadlist[31].n_value, sizeof(rules));
615 	fiop->f_fin[0] = rules[0][0];
616 	fiop->f_fin[1] = rules[0][1];
617 	fiop->f_fout[0] = rules[1][0];
618 	fiop->f_fout[1] = rules[1][1];
619 
620 	/*
621 	 * Same for IPv6, except make them null if support for it is not
622 	 * being compiled in.
623 	 */
624 #ifdef	USE_INET6
625 	kmemcpy((char *)&rules, (u_long)deadlist[32].n_value, sizeof(rules));
626 	fiop->f_fin6[0] = rules[0][0];
627 	fiop->f_fin6[1] = rules[0][1];
628 	fiop->f_fout6[0] = rules[1][0];
629 	fiop->f_fout6[1] = rules[1][1];
630 #else
631 	fiop->f_fin6[0] = NULL;
632 	fiop->f_fin6[1] = NULL;
633 	fiop->f_fout6[0] = NULL;
634 	fiop->f_fout6[1] = NULL;
635 #endif
636 
637 	/*
638 	 * Now get accounting rules pointers.
639 	 */
640 	kmemcpy((char *)&rules, (u_long)deadlist[33].n_value, sizeof(rules));
641 	fiop->f_acctin[0] = rules[0][0];
642 	fiop->f_acctin[1] = rules[0][1];
643 	fiop->f_acctout[0] = rules[1][0];
644 	fiop->f_acctout[1] = rules[1][1];
645 
646 #ifdef	USE_INET6
647 	kmemcpy((char *)&rules, (u_long)deadlist[34].n_value, sizeof(rules));
648 	fiop->f_acctin6[0] = rules[0][0];
649 	fiop->f_acctin6[1] = rules[0][1];
650 	fiop->f_acctout6[0] = rules[1][0];
651 	fiop->f_acctout6[1] = rules[1][1];
652 #else
653 	fiop->f_acctin6[0] = NULL;
654 	fiop->f_acctin6[1] = NULL;
655 	fiop->f_acctout6[0] = NULL;
656 	fiop->f_acctout6[1] = NULL;
657 #endif
658 
659 	/*
660 	 * A collection of "global" variables used inside the kernel which
661 	 * are all collected in friostat_t via ioctl.
662 	 */
663 	kmemcpy((char *)&fiop->f_froute, (u_long)deadlist[35].n_value,
664 		sizeof(fiop->f_froute));
665 	kmemcpy((char *)&fiop->f_running, (u_long)deadlist[36].n_value,
666 		sizeof(fiop->f_running));
667 	kmemcpy((char *)&fiop->f_groups, (u_long)deadlist[37].n_value,
668 		sizeof(fiop->f_groups));
669 	kmemcpy((char *)&fiop->f_active, (u_long)deadlist[38].n_value,
670 		sizeof(fiop->f_active));
671 	kmemcpy((char *)&fiop->f_defpass, (u_long)deadlist[39].n_value,
672 		sizeof(fiop->f_defpass));
673 
674 	/*
675 	 * Build up the state information stats structure.
676 	 */
677 	kmemcpy((char *)ipsstp, (u_long)deadlist[14].n_value, sizeof(*ipsstp));
678 	kmemcpy((char *)&temp, (u_long)deadlist[15].n_value, sizeof(temp));
679 	ipsstp->iss_active = temp;
680 	ipsstp->iss_table = (void *)deadlist[18].n_value;
681 	ipsstp->iss_list = (void *)deadlist[17].n_value;
682 
683 	/*
684 	 * Build up the authentiation information stats structure.
685 	 */
686 	kmemcpy((char *)frauthstp, (u_long)deadlist[0].n_value,
687 		sizeof(*frauthstp));
688 	frauthstp->fas_faelist = (void *)deadlist[1].n_value;
689 
690 	/*
691 	 * Build up the fragment information stats structure.
692 	 */
693 	kmemcpy((char *)ifrstp, (u_long)deadlist[25].n_value,
694 		sizeof(*ifrstp));
695 	ifrstp->ifs_table = (void *)deadlist[23].n_value;
696 	ifrstp->ifs_nattab = (void *)deadlist[24].n_value;
697 	kmemcpy((char *)&ifrstp->ifs_inuse, (u_long)deadlist[26].n_value,
698 		sizeof(ifrstp->ifs_inuse));
699 
700 	/*
701 	 * Get logging on/off switches
702 	 */
703 	kmemcpy((char *)&state_logging, (u_long)deadlist[41].n_value,
704 		sizeof(state_logging));
705 }
706 
707 
708 /*
709  * Display the kernel stats for packets blocked and passed and other
710  * associated running totals which are kept.
711  */
712 static	void	showstats(fp, frf)
713 struct	friostat	*fp;
714 u_32_t frf;
715 {
716 
717 	PRINTF("bad packets:\t\tin %lu\tout %lu\n",
718 			fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
719 #ifdef	USE_INET6
720 	PRINTF(" IPv6 packets:\t\tin %lu out %lu\n",
721 			fp->f_st[0].fr_ipv6, fp->f_st[1].fr_ipv6);
722 #endif
723 	PRINTF(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
724 			fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
725 			fp->f_st[0].fr_nom);
726 	PRINTF(" counted %lu short %lu\n",
727 			fp->f_st[0].fr_acct, fp->f_st[0].fr_short);
728 	PRINTF("output packets:\t\tblocked %lu passed %lu nomatch %lu",
729 			fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
730 			fp->f_st[1].fr_nom);
731 	PRINTF(" counted %lu short %lu\n",
732 			fp->f_st[1].fr_acct, fp->f_st[1].fr_short);
733 	PRINTF(" input packets logged:\tblocked %lu passed %lu\n",
734 			fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
735 	PRINTF("output packets logged:\tblocked %lu passed %lu\n",
736 			fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
737 	PRINTF(" packets logged:\tinput %lu output %lu\n",
738 			fp->f_st[0].fr_pkl, fp->f_st[1].fr_pkl);
739 	PRINTF(" log failures:\t\tinput %lu output %lu\n",
740 			fp->f_st[0].fr_skip, fp->f_st[1].fr_skip);
741 	PRINTF("fragment state(in):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
742 			fp->f_st[0].fr_nfr, fp->f_st[0].fr_bnfr,
743 			fp->f_st[0].fr_cfr);
744 	PRINTF("fragment state(out):\tkept %lu\tlost %lu\tnot fragmented %lu\n",
745 			fp->f_st[1].fr_nfr, fp->f_st[1].fr_bnfr,
746 			fp->f_st[0].fr_cfr);
747 	PRINTF("packet state(in):\tkept %lu\tlost %lu\n",
748 			fp->f_st[0].fr_ads, fp->f_st[0].fr_bads);
749 	PRINTF("packet state(out):\tkept %lu\tlost %lu\n",
750 			fp->f_st[1].fr_ads, fp->f_st[1].fr_bads);
751 	PRINTF("ICMP replies:\t%lu\tTCP RSTs sent:\t%lu\n",
752 			fp->f_st[0].fr_ret, fp->f_st[1].fr_ret);
753 	PRINTF("Invalid source(in):\t%lu\n", fp->f_st[0].fr_badsrc);
754 	PRINTF("Result cache hits(in):\t%lu\t(out):\t%lu\n",
755 			fp->f_st[0].fr_chit, fp->f_st[1].fr_chit);
756 	PRINTF("IN Pullups succeeded:\t%lu\tfailed:\t%lu\n",
757 			fp->f_st[0].fr_pull[0], fp->f_st[0].fr_pull[1]);
758 	PRINTF("OUT Pullups succeeded:\t%lu\tfailed:\t%lu\n",
759 			fp->f_st[1].fr_pull[0], fp->f_st[1].fr_pull[1]);
760 	PRINTF("Fastroute successes:\t%lu\tfailures:\t%lu\n",
761 			fp->f_froute[0], fp->f_froute[1]);
762 	PRINTF("TCP cksum fails(in):\t%lu\t(out):\t%lu\n",
763 			fp->f_st[0].fr_tcpbad, fp->f_st[1].fr_tcpbad);
764 	PRINTF("IPF Ticks:\t%lu\n", fp->f_ticks);
765 
766 	PRINTF("Packet log flags set: (%#x)\n", frf);
767 	if (frf & FF_LOGPASS)
768 		PRINTF("\tpackets passed through filter\n");
769 	if (frf & FF_LOGBLOCK)
770 		PRINTF("\tpackets blocked by filter\n");
771 	if (frf & FF_LOGNOMATCH)
772 		PRINTF("\tpackets not matched by filter\n");
773 	if (!frf)
774 		PRINTF("\tnone\n");
775 }
776 
777 
778 /*
779  * Print out a list of rules from the kernel, starting at the one passed.
780  */
781 static void printlist(fp, comment)
782 frentry_t *fp;
783 char *comment;
784 {
785 	struct	frentry	fb, *fg;
786 	char	*data;
787 	u_32_t	type;
788 	int	n;
789 
790 	for (n = 1; fp; n++) {
791 		if (kmemcpy((char *)&fb, (u_long)fp, sizeof(fb)) == -1) {
792 			perror("kmemcpy");
793 			return;
794 		}
795 		fp = &fb;
796 		if (opts & (OPT_HITS|OPT_VERBOSE))
797 #ifdef	USE_QUAD_T
798 			PRINTF("%qu ", (unsigned long long) fp->fr_hits);
799 #else
800 			PRINTF("%lu ", fp->fr_hits);
801 #endif
802 		if (opts & (OPT_ACCNT|OPT_VERBOSE))
803 #ifdef	USE_QUAD_T
804 			PRINTF("%qu ", (unsigned long long) fp->fr_bytes);
805 #else
806 			PRINTF("%lu ", fp->fr_bytes);
807 #endif
808 		if (opts & OPT_SHOWLINENO)
809 			PRINTF("@%d ", n);
810 		data = NULL;
811 		type = fp->fr_type & ~FR_T_BUILTIN;
812 		if (type == FR_T_IPF || type == FR_T_BPFOPC) {
813 			if (fp->fr_dsize) {
814 				data = malloc(fp->fr_dsize);
815 				if (data == NULL) {
816 					perror("malloc");
817 					exit(1);
818 				}
819 
820 				if (kmemcpy(data, (u_long)fp->fr_data,
821 					    fp->fr_dsize) == -1) {
822 					perror("kmemcpy");
823 					return;
824 				}
825 				fp->fr_data = data;
826 			}
827 		}
828 
829 		printfr(fp, ioctl);
830 		if (opts & OPT_DEBUG) {
831 			binprint(fp, sizeof(*fp));
832 			if (fp->fr_data != NULL && fp->fr_dsize > 0)
833 				binprint(fp->fr_data, fp->fr_dsize);
834 		}
835 		if (data != NULL)
836 			free(data);
837 		if (fp->fr_grp != NULL) {
838 			if (!kmemcpy((char *)&fg, (u_long)fp->fr_grp,
839 				     sizeof(fg)))
840 				printlist(fg, comment);
841 		}
842 		if (type == FR_T_CALLFUNC) {
843 			printlist(fp->fr_data, "# callfunc: ");
844 		}
845 		fp = fp->fr_next;
846 	}
847 }
848 
849 /*
850  * print out all of the asked for rule sets, using the stats struct as
851  * the base from which to get the pointers.
852  */
853 static	void	showlist(fiop)
854 struct	friostat	*fiop;
855 {
856 	struct	frentry	*fp = NULL;
857 	int	i, set;
858 
859 	set = fiop->f_active;
860 	if (opts & OPT_INACTIVE)
861 		set = 1 - set;
862 	if (opts & OPT_ACCNT) {
863 #ifdef USE_INET6
864 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
865 			i = F_ACOUT;
866 			fp = (struct frentry *)fiop->f_acctout6[set];
867 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
868 			i = F_ACIN;
869 			fp = (struct frentry *)fiop->f_acctin6[set];
870 		} else
871 #endif
872 		if (opts & OPT_OUTQUE) {
873 			i = F_ACOUT;
874 			fp = (struct frentry *)fiop->f_acctout[set];
875 		} else if (opts & OPT_INQUE) {
876 			i = F_ACIN;
877 			fp = (struct frentry *)fiop->f_acctin[set];
878 		} else {
879 			FPRINTF(stderr, "No -i or -o given with -a\n");
880 			return;
881 		}
882 	} else {
883 #ifdef	USE_INET6
884 		if ((use_inet6) && (opts & OPT_OUTQUE)) {
885 			i = F_OUT;
886 			fp = (struct frentry *)fiop->f_fout6[set];
887 		} else if ((use_inet6) && (opts & OPT_INQUE)) {
888 			i = F_IN;
889 			fp = (struct frentry *)fiop->f_fin6[set];
890 		} else
891 #endif
892 		if (opts & OPT_OUTQUE) {
893 			i = F_OUT;
894 			fp = (struct frentry *)fiop->f_fout[set];
895 		} else if (opts & OPT_INQUE) {
896 			i = F_IN;
897 			fp = (struct frentry *)fiop->f_fin[set];
898 		} else
899 			return;
900 	}
901 	if (opts & OPT_VERBOSE)
902 		FPRINTF(stderr, "showlist:opts %#x i %d\n", opts, i);
903 
904 	if (opts & OPT_VERBOSE)
905 		PRINTF("fp %p set %d\n", fp, set);
906 	if (!fp) {
907 		FPRINTF(stderr, "empty list for %s%s\n",
908 			(opts & OPT_INACTIVE) ? "inactive " : "", filters[i]);
909 		return;
910 	}
911 	printlist(fp, NULL);
912 }
913 
914 
915 /*
916  * Display ipfilter stateful filtering information
917  */
918 static void showipstates(ipsp)
919 ips_stat_t *ipsp;
920 {
921 	u_long minlen, maxlen, totallen, *buckets;
922 	int i, sz;
923 
924 	sz = sizeof(*buckets) * ipsp->iss_statesize;
925 	buckets = (u_long *)malloc(sz);
926 	if (buckets == NULL) {
927 		perror("malloc");
928 		exit(1);
929 	}
930 	if (kmemcpy((char *)buckets, (u_long)ipsp->iss_bucketlen, sz)) {
931 		free(buckets);
932 		return;
933 	}
934 
935 	/*
936 	 * If a list of states hasn't been asked for, only print out stats
937 	 */
938 	if (!(opts & OPT_SHOWLIST)) {
939 		PRINTF("IP states added:\n\t%lu TCP\n\t%lu UDP\n\t%lu ICMP\n",
940 			ipsp->iss_tcp, ipsp->iss_udp, ipsp->iss_icmp);
941 		PRINTF("\t%lu hits\n\t%lu misses\n", ipsp->iss_hits,
942 			ipsp->iss_miss);
943 		PRINTF("\t%lu maximum\n\t%lu no memory\n\t%lu max bucket\n",
944 			ipsp->iss_max, ipsp->iss_nomem, ipsp->iss_bucketfull);
945 		PRINTF("\t%lu maximum\n\t%lu no memory\n\t%lu bkts in use\n",
946 			ipsp->iss_max, ipsp->iss_nomem, ipsp->iss_inuse);
947 		PRINTF("\t%lu active\n\t%lu expired\n\t%lu closed\n",
948 			ipsp->iss_active, ipsp->iss_expire, ipsp->iss_fin);
949 
950 		PRINTF("State logging %sabled\n",
951 			state_logging ? "en" : "dis");
952 
953 		PRINTF("\nState table bucket statistics:\n");
954 		PRINTF("\t%lu in use\t\n", ipsp->iss_inuse);
955 
956 		minlen = ipsp->iss_max;
957 		totallen = 0;
958 		maxlen = 0;
959 
960 		for (i = 0; i < ipsp->iss_statesize; i++) {
961 			if (buckets[i] > maxlen)
962 				maxlen = buckets[i];
963 			if (buckets[i] < minlen)
964 					minlen = buckets[i];
965 			totallen += buckets[i];
966 		}
967 
968 		PRINTF("\t%2.2f%% bucket usage\n\t%lu minimal length\n",
969 			((float)ipsp->iss_inuse / ipsp->iss_statesize) * 100.0,
970 			minlen);
971 		PRINTF("\t%lu maximal length\n\t%.3f average length\n",
972 			maxlen,
973 			ipsp->iss_inuse ? (float) totallen/ ipsp->iss_inuse :
974 					  0.0);
975 
976 #define ENTRIES_PER_LINE 5
977 
978 		if (opts & OPT_VERBOSE) {
979 			PRINTF("\nCurrent bucket sizes :\n");
980 			for (i = 0; i < ipsp->iss_statesize; i++) {
981 				if ((i % ENTRIES_PER_LINE) == 0)
982 					PRINTF("\t");
983 				PRINTF("%4d -> %4lu", i, buckets[i]);
984 				if ((i % ENTRIES_PER_LINE) ==
985 				    (ENTRIES_PER_LINE - 1))
986 					PRINTF("\n");
987 				else
988 					PRINTF("  ");
989 			}
990 			PRINTF("\n");
991 		}
992 		PRINTF("\n");
993 
994 		free(buckets);
995 		return;
996 	}
997 
998 	/*
999 	 * Print out all the state information currently held in the kernel.
1000 	 */
1001 	while (ipsp->iss_list != NULL) {
1002 		ipsp->iss_list = printstate(ipsp->iss_list, opts,
1003 					    ipsp->iss_ticks);
1004 	}
1005 
1006 	free(buckets);
1007 }
1008 
1009 
1010 #ifdef STATETOP
1011 static int handle_resize = 0, handle_break = 0;
1012 
1013 static void topipstates(saddr, daddr, sport, dport, protocol, ver,
1014 		        refreshtime, topclosed)
1015 i6addr_t saddr;
1016 i6addr_t daddr;
1017 int sport;
1018 int dport;
1019 int protocol;
1020 int ver;
1021 int refreshtime;
1022 int topclosed;
1023 {
1024 	char str1[STSTRSIZE], str2[STSTRSIZE], str3[STSTRSIZE], str4[STSTRSIZE];
1025 	int maxtsentries = 0, reverse = 0, sorting = STSORT_DEFAULT;
1026 	int i, j, winy, tsentry, maxx, maxy, redraw = 0, ret = 0;
1027 	int len, srclen, dstlen, forward = 1, c = 0;
1028 	ips_stat_t ipsst, *ipsstp = &ipsst;
1029 	statetop_t *tstable = NULL, *tp;
1030 	const char *errstr = "";
1031 	ipstate_t ips;
1032 	ipfobj_t ipfo;
1033 	struct timeval selecttimeout;
1034 	char hostnm[HOSTNMLEN];
1035 	struct protoent *proto;
1036 	fd_set readfd;
1037 	time_t t;
1038 
1039 	/* install signal handlers */
1040 	signal(SIGINT, sig_break);
1041 	signal(SIGQUIT, sig_break);
1042 	signal(SIGTERM, sig_break);
1043 	signal(SIGWINCH, sig_resize);
1044 
1045 	/* init ncurses stuff */
1046   	initscr();
1047   	cbreak();
1048   	noecho();
1049 	curs_set(0);
1050 	timeout(0);
1051 	getmaxyx(stdscr, maxy, maxx);
1052 
1053 	/* init hostname */
1054 	gethostname(hostnm, sizeof(hostnm) - 1);
1055 	hostnm[sizeof(hostnm) - 1] = '\0';
1056 
1057 	/* init ipfobj_t stuff */
1058 	bzero((caddr_t)&ipfo, sizeof(ipfo));
1059 	ipfo.ipfo_rev = IPFILTER_VERSION;
1060 	ipfo.ipfo_size = sizeof(*ipsstp);
1061 	ipfo.ipfo_ptr = (void *)ipsstp;
1062 	ipfo.ipfo_type = IPFOBJ_STATESTAT;
1063 
1064 	/* repeat until user aborts */
1065 	while ( 1 ) {
1066 
1067 		/* get state table */
1068 		bzero((char *)&ipsst, sizeof(ipsst));
1069 		if ((ioctl(state_fd, SIOCGETFS, &ipfo) == -1)) {
1070 			errstr = "ioctl(SIOCGETFS)";
1071 			ret = -1;
1072 			goto out;
1073 		}
1074 
1075 		/* clear the history */
1076 		tsentry = -1;
1077 
1078 		/* reset max str len */
1079 		srclen = dstlen = 0;
1080 
1081 		/* read the state table and store in tstable */
1082 		for (; ipsstp->iss_list; ipsstp->iss_list = ips.is_next) {
1083 
1084 			if (kmemcpy((char *)&ips, (u_long)ipsstp->iss_list,
1085 				    sizeof(ips)))
1086 				break;
1087 
1088 			if (ips.is_v != ver)
1089 				continue;
1090 
1091 			/* check v4 src/dest addresses */
1092 			if (ips.is_v == 4) {
1093 				if ((saddr.in4.s_addr != INADDR_ANY &&
1094 				     saddr.in4.s_addr != ips.is_saddr) ||
1095 				    (daddr.in4.s_addr != INADDR_ANY &&
1096 				     daddr.in4.s_addr != ips.is_daddr))
1097 					continue;
1098 			}
1099 #ifdef	USE_INET6
1100 			/* check v6 src/dest addresses */
1101 			if (ips.is_v == 6) {
1102 				if ((IP6_NEQ(&saddr, &in6addr_any) &&
1103 				     IP6_NEQ(&saddr, &ips.is_src)) ||
1104 				    (IP6_NEQ(&daddr, &in6addr_any) &&
1105 				     IP6_NEQ(&daddr, &ips.is_dst)))
1106 					continue;
1107 			}
1108 #endif
1109 			/* check protocol */
1110 			if (protocol > 0 && protocol != ips.is_p)
1111 				continue;
1112 
1113 			/* check ports if protocol is TCP or UDP */
1114 			if (((ips.is_p == IPPROTO_TCP) ||
1115 			     (ips.is_p == IPPROTO_UDP)) &&
1116 			   (((sport > 0) && (htons(sport) != ips.is_sport)) ||
1117 			    ((dport > 0) && (htons(dport) != ips.is_dport))))
1118 				continue;
1119 
1120 			/* show closed TCP sessions ? */
1121 			if ((topclosed == 0) && (ips.is_p == IPPROTO_TCP) &&
1122 			    (ips.is_state[0] >= IPF_TCPS_LAST_ACK) &&
1123 			    (ips.is_state[1] >= IPF_TCPS_LAST_ACK))
1124 				continue;
1125 
1126 			/*
1127 			 * if necessary make room for this state
1128 			 * entry
1129 			 */
1130 			tsentry++;
1131 			if (!maxtsentries || tsentry == maxtsentries) {
1132 				maxtsentries += STGROWSIZE;
1133 				tstable = realloc(tstable,
1134 				    maxtsentries * sizeof(statetop_t));
1135 				if (tstable == NULL) {
1136 					perror("realloc");
1137 					exit(-1);
1138 				}
1139 			}
1140 
1141 			/* get max src/dest address string length */
1142 			len = strlen(getip(ips.is_v, &ips.is_src));
1143 			if (srclen < len)
1144 				srclen = len;
1145 			len = strlen(getip(ips.is_v, &ips.is_dst));
1146 			if (dstlen < len)
1147 				dstlen = len;
1148 
1149 			/* fill structure */
1150 			tp = tstable + tsentry;
1151 			tp->st_src = ips.is_src;
1152 			tp->st_dst = ips.is_dst;
1153 			tp->st_p = ips.is_p;
1154 			tp->st_v = ips.is_v;
1155 			tp->st_state[0] = ips.is_state[0];
1156 			tp->st_state[1] = ips.is_state[1];
1157 			if (forward) {
1158 				tp->st_pkts = ips.is_pkts[0]+ips.is_pkts[1];
1159 				tp->st_bytes = ips.is_bytes[0]+ips.is_bytes[1];
1160 			} else {
1161 				tp->st_pkts = ips.is_pkts[2]+ips.is_pkts[3];
1162 				tp->st_bytes = ips.is_bytes[2]+ips.is_bytes[3];
1163 			}
1164 			tp->st_age = ips.is_die - ipsstp->iss_ticks;
1165 			if ((ips.is_p == IPPROTO_TCP) ||
1166 			    (ips.is_p == IPPROTO_UDP)) {
1167 				tp->st_sport = ips.is_sport;
1168 				tp->st_dport = ips.is_dport;
1169 			}
1170 		}
1171 
1172 
1173 		/* sort the array */
1174 		if (tsentry != -1) {
1175 			switch (sorting)
1176 			{
1177 			case STSORT_PR:
1178 				qsort(tstable, tsentry + 1,
1179 				      sizeof(statetop_t), sort_p);
1180 				break;
1181 			case STSORT_PKTS:
1182 				qsort(tstable, tsentry + 1,
1183 				      sizeof(statetop_t), sort_pkts);
1184 				break;
1185 			case STSORT_BYTES:
1186 				qsort(tstable, tsentry + 1,
1187 				      sizeof(statetop_t), sort_bytes);
1188 				break;
1189 			case STSORT_TTL:
1190 				qsort(tstable, tsentry + 1,
1191 				      sizeof(statetop_t), sort_ttl);
1192 				break;
1193 			case STSORT_SRCIP:
1194 				qsort(tstable, tsentry + 1,
1195 				      sizeof(statetop_t), sort_srcip);
1196 				break;
1197 			case STSORT_SRCPT:
1198 				qsort(tstable, tsentry +1,
1199 					sizeof(statetop_t), sort_srcpt);
1200 				break;
1201 			case STSORT_DSTIP:
1202 				qsort(tstable, tsentry + 1,
1203 				      sizeof(statetop_t), sort_dstip);
1204 				break;
1205 			case STSORT_DSTPT:
1206 				qsort(tstable, tsentry + 1,
1207 				      sizeof(statetop_t), sort_dstpt);
1208 				break;
1209 			default:
1210 				break;
1211 			}
1212 		}
1213 
1214 		/* handle window resizes */
1215 		if (handle_resize) {
1216 			endwin();
1217 			initscr();
1218 			cbreak();
1219 			noecho();
1220 			curs_set(0);
1221 			timeout(0);
1222 			getmaxyx(stdscr, maxy, maxx);
1223 			redraw = 1;
1224 			handle_resize = 0;
1225                 }
1226 
1227 		/* stop program? */
1228 		if (handle_break)
1229 			break;
1230 
1231 		/* print title */
1232 		erase();
1233 		attron(A_BOLD);
1234 		winy = 0;
1235 		move(winy,0);
1236 		sprintf(str1, "%s - %s - state top", hostnm, IPL_VERSION);
1237 		for (j = 0 ; j < (maxx - 8 - strlen(str1)) / 2; j++)
1238 			printw(" ");
1239 		printw("%s", str1);
1240 		attroff(A_BOLD);
1241 
1242 		/* just for fun add a clock */
1243 		move(winy, maxx - 8);
1244 		t = time(NULL);
1245 		strftime(str1, 80, "%T", localtime(&t));
1246 		printw("%s\n", str1);
1247 
1248 		/*
1249 		 * print the display filters, this is placed in the loop,
1250 		 * because someday I might add code for changing these
1251 		 * while the programming is running :-)
1252 		 */
1253 		if (sport >= 0)
1254 			sprintf(str1, "%s,%d", getip(ver, &saddr), sport);
1255 		else
1256 			sprintf(str1, "%s", getip(ver, &saddr));
1257 
1258 		if (dport >= 0)
1259 			sprintf(str2, "%s,%d", getip(ver, &daddr), dport);
1260 		else
1261 			sprintf(str2, "%s", getip(ver, &daddr));
1262 
1263 		if (protocol < 0)
1264 			strcpy(str3, "any");
1265 		else if ((proto = getprotobynumber(protocol)) != NULL)
1266 			sprintf(str3, "%s", proto->p_name);
1267 		else
1268 			sprintf(str3, "%d", protocol);
1269 
1270 		switch (sorting)
1271 		{
1272 		case STSORT_PR:
1273 			sprintf(str4, "proto");
1274 			break;
1275 		case STSORT_PKTS:
1276 			sprintf(str4, "# pkts");
1277 			break;
1278 		case STSORT_BYTES:
1279 			sprintf(str4, "# bytes");
1280 			break;
1281 		case STSORT_TTL:
1282 			sprintf(str4, "ttl");
1283 			break;
1284 		case STSORT_SRCIP:
1285 			sprintf(str4, "src ip");
1286 			break;
1287 		case STSORT_SRCPT:
1288 			sprintf(str4, "src port");
1289 			break;
1290 		case STSORT_DSTIP:
1291 			sprintf(str4, "dest ip");
1292 			break;
1293 		case STSORT_DSTPT:
1294 			sprintf(str4, "dest port");
1295 			break;
1296 		default:
1297 			sprintf(str4, "unknown");
1298 			break;
1299 		}
1300 
1301 		if (reverse)
1302 			strcat(str4, " (reverse)");
1303 
1304 		winy += 2;
1305 		move(winy,0);
1306 		printw("Src: %s, Dest: %s, Proto: %s, Sorted by: %s\n\n",
1307 		       str1, str2, str3, str4);
1308 
1309 		/*
1310 		 * For an IPv4 IP address we need at most 15 characters,
1311 		 * 4 tuples of 3 digits, separated by 3 dots. Enforce this
1312 		 * length, so the colums do not change positions based
1313 		 * on the size of the IP address. This length makes the
1314 		 * output fit in a 80 column terminal.
1315 		 * We are lacking a good solution for IPv6 addresses (that
1316 		 * can be longer that 15 characters), so we do not enforce
1317 		 * a maximum on the IP field size.
1318 		 */
1319 		if (srclen < 15)
1320 			srclen = 15;
1321 		if (dstlen < 15)
1322 			dstlen = 15;
1323 
1324 		/* print column description */
1325 		winy += 2;
1326 		move(winy,0);
1327 		attron(A_BOLD);
1328 		printw("%-*s %-*s %3s %4s %7s %9s %9s\n",
1329 		       srclen + 6, "Source IP", dstlen + 6, "Destination IP",
1330 		       "ST", "PR", "#pkts", "#bytes", "ttl");
1331 		attroff(A_BOLD);
1332 
1333 		/* print all the entries */
1334 		tp = tstable;
1335 		if (reverse)
1336 			tp += tsentry;
1337 
1338 		if (tsentry > maxy - 6)
1339 			tsentry = maxy - 6;
1340 		for (i = 0; i <= tsentry; i++) {
1341 			/* print src/dest and port */
1342 			if ((tp->st_p == IPPROTO_TCP) ||
1343 			    (tp->st_p == IPPROTO_UDP)) {
1344 				sprintf(str1, "%s,%hu",
1345 					getip(tp->st_v, &tp->st_src),
1346 					ntohs(tp->st_sport));
1347 				sprintf(str2, "%s,%hu",
1348 					getip(tp->st_v, &tp->st_dst),
1349 					ntohs(tp->st_dport));
1350 			} else {
1351 				sprintf(str1, "%s", getip(tp->st_v,
1352 				    &tp->st_src));
1353 				sprintf(str2, "%s", getip(tp->st_v,
1354 				    &tp->st_dst));
1355 			}
1356 			winy++;
1357 			move(winy, 0);
1358 			printw("%-*s %-*s", srclen + 6, str1, dstlen + 6, str2);
1359 
1360 			/* print state */
1361 			sprintf(str1, "%X/%X", tp->st_state[0],
1362 				tp->st_state[1]);
1363 			printw(" %3s", str1);
1364 
1365 			/* print protocol */
1366 			proto = getprotobynumber(tp->st_p);
1367 			if (proto) {
1368 				strncpy(str1, proto->p_name, 4);
1369 				str1[4] = '\0';
1370 			} else {
1371 				sprintf(str1, "%d", tp->st_p);
1372 			}
1373 			/* just print icmp for IPv6-ICMP */
1374 			if (tp->st_p == IPPROTO_ICMPV6)
1375 				strcpy(str1, "icmp");
1376 			printw(" %4s", str1);
1377 
1378 			/* print #pkt/#bytes */
1379 #ifdef	USE_QUAD_T
1380 			printw(" %7qu %9qu", (unsigned long long) tp->st_pkts,
1381 				(unsigned long long) tp->st_bytes);
1382 #else
1383 			printw(" %7lu %9lu", tp->st_pkts, tp->st_bytes);
1384 #endif
1385 			printw(" %9s", ttl_to_string(tp->st_age));
1386 
1387 			if (reverse)
1388 				tp--;
1389 			else
1390 				tp++;
1391 		}
1392 
1393 		/* screen data structure is filled, now update the screen */
1394 		if (redraw)
1395 			clearok(stdscr,1);
1396 
1397 		if (refresh() == ERR)
1398 			break;
1399 		if (redraw) {
1400 			clearok(stdscr,0);
1401 			redraw = 0;
1402 		}
1403 
1404 		/* wait for key press or a 1 second time out period */
1405 		selecttimeout.tv_sec = refreshtime;
1406 		selecttimeout.tv_usec = 0;
1407 		FD_ZERO(&readfd);
1408 		FD_SET(0, &readfd);
1409 		select(1, &readfd, NULL, NULL, &selecttimeout);
1410 
1411 		/* if key pressed, read all waiting keys */
1412 		if (FD_ISSET(0, &readfd)) {
1413 			c = wgetch(stdscr);
1414 			if (c == ERR)
1415 				continue;
1416 
1417 			if (ISALPHA(c) && ISUPPER(c))
1418 				c = TOLOWER(c);
1419 			if (c == 'l') {
1420 				redraw = 1;
1421 			} else if (c == 'q') {
1422 				break;
1423 			} else if (c == 'r') {
1424 				reverse = !reverse;
1425 			} else if (c == 'b') {
1426 				forward = 0;
1427 			} else if (c == 'f') {
1428 				forward = 1;
1429 			} else if (c == 's') {
1430 				if (++sorting > STSORT_MAX)
1431 					sorting = 0;
1432 			}
1433 		}
1434 	} /* while */
1435 
1436 out:
1437 	printw("\n");
1438 	curs_set(1);
1439 	nocbreak();
1440 	endwin();
1441 
1442 	free(tstable);
1443 	if (ret != 0)
1444 		perror(errstr);
1445 }
1446 #endif
1447 
1448 
1449 /*
1450  * Show fragment cache information that's held in the kernel.
1451  */
1452 static void showfrstates(ifsp)
1453 ipfrstat_t *ifsp;
1454 {
1455 	struct ipfr *ipfrtab[IPFT_SIZE], ifr;
1456 	int i;
1457 
1458 	/*
1459 	 * print out the numeric statistics
1460 	 */
1461 	PRINTF("IP fragment states:\n\t%lu new\n\t%lu expired\n\t%lu hits\n",
1462 		ifsp->ifs_new, ifsp->ifs_expire, ifsp->ifs_hits);
1463 	PRINTF("\t%lu retrans\n\t%lu too short\n",
1464 		ifsp->ifs_retrans0, ifsp->ifs_short);
1465 	PRINTF("\t%lu no memory\n\t%lu already exist\n",
1466 		ifsp->ifs_nomem, ifsp->ifs_exists);
1467 	PRINTF("\t%lu inuse\n", ifsp->ifs_inuse);
1468 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_table, sizeof(ipfrtab)))
1469 		return;
1470 
1471 	/*
1472 	 * Print out the contents (if any) of the fragment cache table.
1473 	 */
1474 	PRINTF("\n");
1475 	for (i = 0; i < IPFT_SIZE; i++)
1476 		while (ipfrtab[i] != NULL) {
1477 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
1478 				    sizeof(ifr)) == -1)
1479 				break;
1480 			printfraginfo("", &ifr);
1481 			ipfrtab[i] = ifr.ipfr_next;
1482 		}
1483 	/*
1484 	 * Print out the contents (if any) of the NAT fragment cache table.
1485 	 */
1486 	if (kmemcpy((char *)ipfrtab, (u_long)ifsp->ifs_nattab,sizeof(ipfrtab)))
1487 		return;
1488 	for (i = 0; i < IPFT_SIZE; i++)
1489 		while (ipfrtab[i] != NULL) {
1490 			if (kmemcpy((char *)&ifr, (u_long)ipfrtab[i],
1491 				    sizeof(ifr)) == -1)
1492 				break;
1493 			printfraginfo("NAT: ", &ifr);
1494 			ipfrtab[i] = ifr.ipfr_next;
1495 		}
1496 }
1497 
1498 
1499 /*
1500  * Show stats on how auth within IPFilter has been used
1501  */
1502 static void showauthstates(asp)
1503 fr_authstat_t *asp;
1504 {
1505 	frauthent_t *frap, fra;
1506 
1507 #ifdef	USE_QUAD_T
1508 	printf("Authorisation hits: %qu\tmisses %qu\n",
1509 		(unsigned long long) asp->fas_hits,
1510 		(unsigned long long) asp->fas_miss);
1511 #else
1512 	printf("Authorisation hits: %ld\tmisses %ld\n", asp->fas_hits,
1513 		asp->fas_miss);
1514 #endif
1515 	printf("nospace %ld\nadded %ld\nsendfail %ld\nsendok %ld\n",
1516 		asp->fas_nospace, asp->fas_added, asp->fas_sendfail,
1517 		asp->fas_sendok);
1518 	printf("queok %ld\nquefail %ld\nexpire %ld\n",
1519 		asp->fas_queok, asp->fas_quefail, asp->fas_expire);
1520 
1521 	frap = asp->fas_faelist;
1522 	while (frap) {
1523 		if (kmemcpy((char *)&fra, (u_long)frap, sizeof(fra)) == -1)
1524 			break;
1525 
1526 		printf("age %ld\t", fra.fae_age);
1527 		printfr(&fra.fae_fr, ioctl);
1528 		frap = fra.fae_next;
1529 	}
1530 }
1531 
1532 
1533 /*
1534  * Display groups used for each of filter rules, accounting rules and
1535  * authentication, separately.
1536  */
1537 static void showgroups(fiop)
1538 struct friostat	*fiop;
1539 {
1540 	static char *gnames[3] = { "Filter", "Accounting", "Authentication" };
1541 	static int gnums[3] = { IPL_LOGIPF, IPL_LOGCOUNT, IPL_LOGAUTH };
1542 	frgroup_t *fp, grp;
1543 	int on, off, i;
1544 
1545 	on = fiop->f_active;
1546 	off = 1 - on;
1547 
1548 	for (i = 0; i < 3; i++) {
1549 		printf("%s groups (active):\n", gnames[i]);
1550 		for (fp = fiop->f_groups[gnums[i]][on]; fp != NULL;
1551 		     fp = grp.fg_next)
1552 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
1553 				break;
1554 			else
1555 				printf("%s\n", grp.fg_name);
1556 		printf("%s groups (inactive):\n", gnames[i]);
1557 		for (fp = fiop->f_groups[gnums[i]][off]; fp != NULL;
1558 		     fp = grp.fg_next)
1559 			if (kmemcpy((char *)&grp, (u_long)fp, sizeof(grp)))
1560 				break;
1561 			else
1562 				printf("%s\n", grp.fg_name);
1563 	}
1564 }
1565 
1566 static void parse_ipportstr(argument, ip, port)
1567 const char *argument;
1568 i6addr_t *ip;
1569 int *port;
1570 {
1571 	char *s, *comma;
1572 	int ok = 0;
1573 
1574 	/* make working copy of argument, Theoretically you must be able
1575 	 * to write to optarg, but that seems very ugly to me....
1576 	 */
1577 	s = strdup(argument);
1578 	if (s == NULL)
1579 		return;
1580 
1581 	/* get port */
1582 	if ((comma = strchr(s, ',')) != NULL) {
1583 		if (!strcasecmp(comma + 1, "any")) {
1584 			*port = -1;
1585 		} else if (!sscanf(comma + 1, "%d", port) ||
1586 			   (*port < 0) || (*port > 65535)) {
1587 			fprintf(stderr, "Invalid port specfication in %s\n",
1588 				argument);
1589 			free(s);
1590 			exit(-2);
1591 		}
1592 		*comma = '\0';
1593 	}
1594 
1595 
1596 	/* get ip address */
1597 	if (!strcasecmp(s, "any")) {
1598 		ip->in4.s_addr = INADDR_ANY;
1599 #ifdef	USE_INET6
1600 		ip->in6 = in6addr_any;
1601 	} else if (use_inet6 && inet_pton(AF_INET6, s, &ip->in6)) {
1602 		ok = 1;
1603 #endif
1604 	} else if (inet_aton(s, &ip->in4))
1605 		ok = 1;
1606 
1607 	if (ok == 0) {
1608 		fprintf(stderr, "Invalid IP address: %s\n", s);
1609 		free(s);
1610 		exit(-2);
1611 	}
1612 
1613 	/* free allocated memory */
1614 	free(s);
1615 }
1616 
1617 
1618 #ifdef STATETOP
1619 static void sig_resize(s)
1620 int s;
1621 {
1622 	handle_resize = 1;
1623 }
1624 
1625 static void sig_break(s)
1626 int s;
1627 {
1628 	handle_break = 1;
1629 }
1630 
1631 static char *getip(v, addr)
1632 int v;
1633 i6addr_t *addr;
1634 {
1635 #ifdef  USE_INET6
1636 	static char hostbuf[MAXHOSTNAMELEN+1];
1637 #endif
1638 
1639 	if (v == 4)
1640 		return inet_ntoa(addr->in4);
1641 
1642 #ifdef  USE_INET6
1643 	(void) inet_ntop(AF_INET6, &addr->in6, hostbuf, sizeof(hostbuf) - 1);
1644 	hostbuf[MAXHOSTNAMELEN] = '\0';
1645 	return hostbuf;
1646 #else
1647 	return "IPv6";
1648 #endif
1649 }
1650 
1651 
1652 static char *ttl_to_string(ttl)
1653 long int ttl;
1654 {
1655 	static char ttlbuf[STSTRSIZE];
1656 	int hours, minutes, seconds;
1657 
1658 	/* ttl is in half seconds */
1659 	ttl /= 2;
1660 
1661 	hours = ttl / 3600;
1662 	ttl = ttl % 3600;
1663 	minutes = ttl / 60;
1664 	seconds = ttl % 60;
1665 
1666 	if (hours > 0)
1667 		sprintf(ttlbuf, "%2d:%02d:%02d", hours, minutes, seconds);
1668 	else
1669 		sprintf(ttlbuf, "%2d:%02d", minutes, seconds);
1670 	return ttlbuf;
1671 }
1672 
1673 
1674 static int sort_pkts(a, b)
1675 const void *a;
1676 const void *b;
1677 {
1678 
1679 	register const statetop_t *ap = a;
1680 	register const statetop_t *bp = b;
1681 
1682 	if (ap->st_pkts == bp->st_pkts)
1683 		return 0;
1684 	else if (ap->st_pkts < bp->st_pkts)
1685 		return 1;
1686 	return -1;
1687 }
1688 
1689 
1690 static int sort_bytes(a, b)
1691 const void *a;
1692 const void *b;
1693 {
1694 	register const statetop_t *ap = a;
1695 	register const statetop_t *bp = b;
1696 
1697 	if (ap->st_bytes == bp->st_bytes)
1698 		return 0;
1699 	else if (ap->st_bytes < bp->st_bytes)
1700 		return 1;
1701 	return -1;
1702 }
1703 
1704 
1705 static int sort_p(a, b)
1706 const void *a;
1707 const void *b;
1708 {
1709 	register const statetop_t *ap = a;
1710 	register const statetop_t *bp = b;
1711 
1712 	if (ap->st_p == bp->st_p)
1713 		return 0;
1714 	else if (ap->st_p < bp->st_p)
1715 		return 1;
1716 	return -1;
1717 }
1718 
1719 
1720 static int sort_ttl(a, b)
1721 const void *a;
1722 const void *b;
1723 {
1724 	register const statetop_t *ap = a;
1725 	register const statetop_t *bp = b;
1726 
1727 	if (ap->st_age == bp->st_age)
1728 		return 0;
1729 	else if (ap->st_age < bp->st_age)
1730 		return 1;
1731 	return -1;
1732 }
1733 
1734 static int sort_srcip(a, b)
1735 const void *a;
1736 const void *b;
1737 {
1738 	register const statetop_t *ap = a;
1739 	register const statetop_t *bp = b;
1740 
1741 #ifdef USE_INET6
1742 	if (use_inet6) {
1743 		if (IP6_EQ(&ap->st_src, &bp->st_src))
1744 			return 0;
1745 		else if (IP6_GT(&ap->st_src, &bp->st_src))
1746 			return 1;
1747 	} else
1748 #endif
1749 	{
1750 		if (ntohl(ap->st_src.in4.s_addr) ==
1751 		    ntohl(bp->st_src.in4.s_addr))
1752 			return 0;
1753 		else if (ntohl(ap->st_src.in4.s_addr) >
1754 		         ntohl(bp->st_src.in4.s_addr))
1755 			return 1;
1756 	}
1757 	return -1;
1758 }
1759 
1760 static int sort_srcpt(a, b)
1761 const void *a;
1762 const void *b;
1763 {
1764 	register const statetop_t *ap = a;
1765 	register const statetop_t *bp = b;
1766 
1767 	if (htons(ap->st_sport) == htons(bp->st_sport))
1768 		return 0;
1769 	else if (htons(ap->st_sport) > htons(bp->st_sport))
1770 		return 1;
1771 	return -1;
1772 }
1773 
1774 static int sort_dstip(a, b)
1775 const void *a;
1776 const void *b;
1777 {
1778 	register const statetop_t *ap = a;
1779 	register const statetop_t *bp = b;
1780 
1781 #ifdef USE_INET6
1782 	if (use_inet6) {
1783 		if (IP6_EQ(&ap->st_dst, &bp->st_dst))
1784 			return 0;
1785 		else if (IP6_GT(&ap->st_dst, &bp->st_dst))
1786 			return 1;
1787 	} else
1788 #endif
1789 	{
1790 		if (ntohl(ap->st_dst.in4.s_addr) ==
1791 		    ntohl(bp->st_dst.in4.s_addr))
1792 			return 0;
1793 		else if (ntohl(ap->st_dst.in4.s_addr) >
1794 		         ntohl(bp->st_dst.in4.s_addr))
1795 			return 1;
1796 	}
1797 	return -1;
1798 }
1799 
1800 static int sort_dstpt(a, b)
1801 const void *a;
1802 const void *b;
1803 {
1804 	register const statetop_t *ap = a;
1805 	register const statetop_t *bp = b;
1806 
1807 	if (htons(ap->st_dport) == htons(bp->st_dport))
1808 		return 0;
1809 	else if (htons(ap->st_dport) > htons(bp->st_dport))
1810 		return 1;
1811 	return -1;
1812 }
1813 
1814 #endif
1815