xref: /titanic_53/usr/src/cmd/ipf/tools/ipf.c (revision ea8244dc4688c6c3f1381849c50ec65d054a37a1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2001 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
57c478bd9Sstevel@tonic-gate  *
6*ea8244dcSJohn Ojemann  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
77c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #ifdef	__FreeBSD__
117c478bd9Sstevel@tonic-gate # ifndef __FreeBSD_cc_version
127c478bd9Sstevel@tonic-gate #  include <osreldate.h>
137c478bd9Sstevel@tonic-gate # else
147c478bd9Sstevel@tonic-gate #  if __FreeBSD_cc_version < 430000
157c478bd9Sstevel@tonic-gate #   include <osreldate.h>
167c478bd9Sstevel@tonic-gate #  endif
177c478bd9Sstevel@tonic-gate # endif
187c478bd9Sstevel@tonic-gate #endif
197c478bd9Sstevel@tonic-gate #include "ipf.h"
207c478bd9Sstevel@tonic-gate #include <fcntl.h>
217c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
227c478bd9Sstevel@tonic-gate #include "netinet/ipl.h"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #if !defined(lint)
257c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)ipf.c	1.23 6/5/96 (C) 1993-2000 Darren Reed";
26ab25eeb5Syz155240 static const char rcsid[] = "@(#)$Id: ipf.c,v 1.35.2.3 2004/12/15 18:27:17 darrenr Exp $";
277c478bd9Sstevel@tonic-gate #endif
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if !defined(__SVR4) && defined(__GNUC__)
307c478bd9Sstevel@tonic-gate extern	char	*index __P((const char *, int));
317c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate extern	char	*optarg;
347c478bd9Sstevel@tonic-gate extern	int	optind;
357c478bd9Sstevel@tonic-gate extern	frentry_t *frtop;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 
38ab25eeb5Syz155240 void	ipf_frsync __P((void));
397c478bd9Sstevel@tonic-gate void	zerostats __P((void));
407c478bd9Sstevel@tonic-gate int	main __P((int, char *[]));
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int	opts = 0;
437c478bd9Sstevel@tonic-gate int	outputc = 0;
447c478bd9Sstevel@tonic-gate int	use_inet6 = 0;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static	void	procfile __P((char *, char *)), flushfilter __P((char *));
477c478bd9Sstevel@tonic-gate static	void	set_state __P((u_int)), showstats __P((friostat_t *));
487c478bd9Sstevel@tonic-gate static	void	packetlogon __P((char *)), swapactive __P((void));
497c478bd9Sstevel@tonic-gate static	int	opendevice __P((char *, int));
507c478bd9Sstevel@tonic-gate static	void	closedevice __P((void));
517c478bd9Sstevel@tonic-gate static	char	*ipfname = IPL_NAME;
527c478bd9Sstevel@tonic-gate static	void	usage __P((void));
537c478bd9Sstevel@tonic-gate static	int	showversion __P((void));
547c478bd9Sstevel@tonic-gate static	int	get_flags __P((void));
557c478bd9Sstevel@tonic-gate static	void	ipf_interceptadd __P((int, ioctlfunc_t, void *));
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static	int	fd = -1;
587c478bd9Sstevel@tonic-gate static	ioctlfunc_t	iocfunctions[IPL_LOGSIZE] = { ioctl, ioctl, ioctl,
597c478bd9Sstevel@tonic-gate 						      ioctl, ioctl, ioctl,
607c478bd9Sstevel@tonic-gate 						      ioctl, ioctl };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static void usage()
647c478bd9Sstevel@tonic-gate {
65ab25eeb5Syz155240 	fprintf(stderr, "usage: ipf [-6AdDEInoPrRsvVyzZ] %s %s %s\n",
66ab25eeb5Syz155240 		"[-l block|pass|nomatch|state|nat]", "[-cc] [-F i|o|a|s|S|u]",
67ab25eeb5Syz155240 		"[-f filename] [-T <tuneopts>]");
687c478bd9Sstevel@tonic-gate 	exit(1);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int main(argc,argv)
737c478bd9Sstevel@tonic-gate int argc;
747c478bd9Sstevel@tonic-gate char *argv[];
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	int c;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (argc < 2)
797c478bd9Sstevel@tonic-gate 		usage();
807c478bd9Sstevel@tonic-gate 
81ab25eeb5Syz155240 	while ((c = getopt(argc, argv, "6Ac:dDEf:F:Il:noPrRsT:vVyzZ")) != -1) {
827c478bd9Sstevel@tonic-gate 		switch (c)
837c478bd9Sstevel@tonic-gate 		{
847c478bd9Sstevel@tonic-gate 		case '?' :
857c478bd9Sstevel@tonic-gate 			usage();
867c478bd9Sstevel@tonic-gate 			break;
877c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
887c478bd9Sstevel@tonic-gate 		case '6' :
897c478bd9Sstevel@tonic-gate 			use_inet6 = 1;
907c478bd9Sstevel@tonic-gate 			break;
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 		case 'A' :
937c478bd9Sstevel@tonic-gate 			opts &= ~OPT_INACTIVE;
947c478bd9Sstevel@tonic-gate 			break;
95ab25eeb5Syz155240 		case 'c' :
96ab25eeb5Syz155240 			if (strcmp(optarg, "c") == 0)
977c478bd9Sstevel@tonic-gate 				outputc = 1;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case 'E' :
1007c478bd9Sstevel@tonic-gate 			set_state((u_int)1);
1017c478bd9Sstevel@tonic-gate 			break;
1027c478bd9Sstevel@tonic-gate 		case 'D' :
1037c478bd9Sstevel@tonic-gate 			set_state((u_int)0);
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case 'd' :
1067c478bd9Sstevel@tonic-gate 			opts ^= OPT_DEBUG;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		case 'f' :
1097c478bd9Sstevel@tonic-gate 			procfile(argv[0], optarg);
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case 'F' :
1127c478bd9Sstevel@tonic-gate 			flushfilter(optarg);
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		case 'I' :
1157c478bd9Sstevel@tonic-gate 			opts ^= OPT_INACTIVE;
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case 'l' :
1187c478bd9Sstevel@tonic-gate 			packetlogon(optarg);
1197c478bd9Sstevel@tonic-gate 			break;
1207c478bd9Sstevel@tonic-gate 		case 'n' :
1217c478bd9Sstevel@tonic-gate 			opts ^= OPT_DONOTHING;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case 'o' :
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case 'P' :
1267c478bd9Sstevel@tonic-gate 			ipfname = IPAUTH_NAME;
1277c478bd9Sstevel@tonic-gate 			break;
128ab25eeb5Syz155240 		case 'R' :
129ab25eeb5Syz155240 			opts ^= OPT_NORESOLVE;
130ab25eeb5Syz155240 			break;
1317c478bd9Sstevel@tonic-gate 		case 'r' :
1327c478bd9Sstevel@tonic-gate 			opts ^= OPT_REMOVE;
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case 's' :
1357c478bd9Sstevel@tonic-gate 			swapactive();
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		case 'T' :
138ab25eeb5Syz155240 			if (opendevice(ipfname, 1) >= 0)
139ab25eeb5Syz155240 				ipf_dotuning(fd, optarg, ioctl);
1407c478bd9Sstevel@tonic-gate 			break;
1417c478bd9Sstevel@tonic-gate 		case 'v' :
1427c478bd9Sstevel@tonic-gate 			opts += OPT_VERBOSE;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		case 'V' :
1457c478bd9Sstevel@tonic-gate 			if (showversion())
1467c478bd9Sstevel@tonic-gate 				exit(1);
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'y' :
149ab25eeb5Syz155240 			ipf_frsync();
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'z' :
1527c478bd9Sstevel@tonic-gate 			opts ^= OPT_ZERORULEST;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'Z' :
1557c478bd9Sstevel@tonic-gate 			zerostats();
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (optind < 2)
1617c478bd9Sstevel@tonic-gate 		usage();
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (fd != -1)
1647c478bd9Sstevel@tonic-gate 		(void) close(fd);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return(0);
1677c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static int opendevice(ipfdev, check)
1727c478bd9Sstevel@tonic-gate char *ipfdev;
1737c478bd9Sstevel@tonic-gate int check;
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	if (opts & OPT_DONOTHING)
1767c478bd9Sstevel@tonic-gate 		return -2;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (check && checkrev(ipfname) == -1) {
1797c478bd9Sstevel@tonic-gate 		fprintf(stderr, "User/kernel version check failed\n");
1807c478bd9Sstevel@tonic-gate 		return -2;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (!ipfdev)
1847c478bd9Sstevel@tonic-gate 		ipfdev = ipfname;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (fd == -1)
1877c478bd9Sstevel@tonic-gate 		if ((fd = open(ipfdev, O_RDWR)) == -1)
1887c478bd9Sstevel@tonic-gate 			if ((fd = open(ipfdev, O_RDONLY)) == -1)
1897c478bd9Sstevel@tonic-gate 				perror("open device");
1907c478bd9Sstevel@tonic-gate 	return fd;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate static void closedevice()
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	close(fd);
1977c478bd9Sstevel@tonic-gate 	fd = -1;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static	int	get_flags()
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	int i;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if ((opendevice(ipfname, 1) != -2) &&
2067c478bd9Sstevel@tonic-gate 	    (ioctl(fd, SIOCGETFF, &i) == -1)) {
2077c478bd9Sstevel@tonic-gate 		perror("SIOCGETFF");
2087c478bd9Sstevel@tonic-gate 		return 0;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	return i;
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static	void	set_state(enable)
2157c478bd9Sstevel@tonic-gate u_int	enable;
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	if (opendevice(ipfname, 0) != -2)
2187c478bd9Sstevel@tonic-gate 		if (ioctl(fd, SIOCFRENB, &enable) == -1) {
2197c478bd9Sstevel@tonic-gate 			if (errno == EBUSY)
2207c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2217c478bd9Sstevel@tonic-gate 					"IP FIlter: already initialized\n");
2227c478bd9Sstevel@tonic-gate 			else
2237c478bd9Sstevel@tonic-gate 				perror("SIOCFRENB");
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	return;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static	void	procfile(name, file)
2307c478bd9Sstevel@tonic-gate char	*name, *file;
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	(void) opendevice(ipfname, 1);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	initparse();
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	ipf_parsefile(fd, ipf_interceptadd, iocfunctions, file);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (outputc) {
2397c478bd9Sstevel@tonic-gate 		printC(0);
2407c478bd9Sstevel@tonic-gate 		printC(1);
2417c478bd9Sstevel@tonic-gate 		emit(-1, -1, NULL, NULL);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static void ipf_interceptadd(fd, ioctlfunc, ptr)
2477c478bd9Sstevel@tonic-gate int fd;
2487c478bd9Sstevel@tonic-gate ioctlfunc_t ioctlfunc;
2497c478bd9Sstevel@tonic-gate void *ptr;
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	if (outputc)
2527c478bd9Sstevel@tonic-gate 		printc(ptr);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	ipf_addrule(fd, ioctlfunc, ptr);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static void packetlogon(opt)
2597c478bd9Sstevel@tonic-gate char	*opt;
2607c478bd9Sstevel@tonic-gate {
261ab25eeb5Syz155240 	int	flag, xfd, logopt, change = 0;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	flag = get_flags();
2647c478bd9Sstevel@tonic-gate 	if (flag != 0) {
2657c478bd9Sstevel@tonic-gate 		if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE)
2667c478bd9Sstevel@tonic-gate 			printf("log flag is currently %#x\n", flag);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	flag &= ~(FF_LOGPASS|FF_LOGNOMATCH|FF_LOGBLOCK);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (strstr(opt, "pass")) {
2727c478bd9Sstevel@tonic-gate 		flag |= FF_LOGPASS;
2737c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE)
2747c478bd9Sstevel@tonic-gate 			printf("set log flag: pass\n");
275ab25eeb5Syz155240 		change = 1;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	if (strstr(opt, "nomatch")) {
2787c478bd9Sstevel@tonic-gate 		flag |= FF_LOGNOMATCH;
2797c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE)
2807c478bd9Sstevel@tonic-gate 			printf("set log flag: nomatch\n");
281ab25eeb5Syz155240 		change = 1;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	if (strstr(opt, "block") || index(opt, 'd')) {
2847c478bd9Sstevel@tonic-gate 		flag |= FF_LOGBLOCK;
2857c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE)
2867c478bd9Sstevel@tonic-gate 			printf("set log flag: block\n");
287ab25eeb5Syz155240 		change = 1;
288ab25eeb5Syz155240 	}
289ab25eeb5Syz155240 	if (strstr(opt, "none")) {
290ab25eeb5Syz155240 		if (opts & OPT_VERBOSE)
291ab25eeb5Syz155240 			printf("disable all log flags\n");
292ab25eeb5Syz155240 		change = 1;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
295ab25eeb5Syz155240 	if (change == 1) {
296ab25eeb5Syz155240 		if (opendevice(ipfname, 1) != -2 &&
297ab25eeb5Syz155240 		    (ioctl(fd, SIOCSETFF, &flag) != 0))
2987c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCSETFF)");
299ab25eeb5Syz155240 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
3027c478bd9Sstevel@tonic-gate 		flag = get_flags();
303ab25eeb5Syz155240 		printf("log flags are now %#x\n", flag);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (strstr(opt, "state")) {
3077c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE)
3087c478bd9Sstevel@tonic-gate 			printf("set state log flag\n");
3097c478bd9Sstevel@tonic-gate 		xfd = open(IPSTATE_NAME, O_RDWR);
3107c478bd9Sstevel@tonic-gate 		if (xfd >= 0) {
3117c478bd9Sstevel@tonic-gate 			logopt = 0;
3127c478bd9Sstevel@tonic-gate 			if (ioctl(xfd, SIOCGETLG, &logopt))
3137c478bd9Sstevel@tonic-gate 				perror("ioctl(SIOCGETLG)");
3147c478bd9Sstevel@tonic-gate 			else {
3157c478bd9Sstevel@tonic-gate 				logopt = 1 - logopt;
3167c478bd9Sstevel@tonic-gate 				if (ioctl(xfd, SIOCSETLG, &logopt))
3177c478bd9Sstevel@tonic-gate 					perror("ioctl(SIOCSETLG)");
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 			close(xfd);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (strstr(opt, "nat")) {
3247c478bd9Sstevel@tonic-gate 		if (opts & OPT_VERBOSE)
3257c478bd9Sstevel@tonic-gate 			printf("set nat log flag\n");
3267c478bd9Sstevel@tonic-gate 		xfd = open(IPNAT_NAME, O_RDWR);
3277c478bd9Sstevel@tonic-gate 		if (xfd >= 0) {
3287c478bd9Sstevel@tonic-gate 			logopt = 0;
3297c478bd9Sstevel@tonic-gate 			if (ioctl(xfd, SIOCGETLG, &logopt))
3307c478bd9Sstevel@tonic-gate 				perror("ioctl(SIOCGETLG)");
3317c478bd9Sstevel@tonic-gate 			else {
3327c478bd9Sstevel@tonic-gate 				logopt = 1 - logopt;
3337c478bd9Sstevel@tonic-gate 				if (ioctl(xfd, SIOCSETLG, &logopt))
3347c478bd9Sstevel@tonic-gate 					perror("ioctl(SIOCSETLG)");
3357c478bd9Sstevel@tonic-gate 			}
3367c478bd9Sstevel@tonic-gate 			close(xfd);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static	void	flushfilter(arg)
3437c478bd9Sstevel@tonic-gate char	*arg;
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	int	fl = 0, rem;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (!arg || !*arg)
3487c478bd9Sstevel@tonic-gate 		return;
3497c478bd9Sstevel@tonic-gate 	if (!strcmp(arg, "s") || !strcmp(arg, "S")) {
3507c478bd9Sstevel@tonic-gate 		if (*arg == 'S')
351*ea8244dcSJohn Ojemann 			fl = FLUSH_TABLE_ALL;
3527c478bd9Sstevel@tonic-gate 		else
353*ea8244dcSJohn Ojemann 			fl = FLUSH_TABLE_CLOSING;
3547c478bd9Sstevel@tonic-gate 		rem = fl;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		closedevice();
357ab25eeb5Syz155240 		if (opendevice(IPSTATE_NAME, 1) == -2)
358ab25eeb5Syz155240 			exit(1);
3597663b816Sml37995 
360ab25eeb5Syz155240 		if (!(opts & OPT_DONOTHING)) {
3617663b816Sml37995 			if (use_inet6) {
362ab25eeb5Syz155240 				if (ioctl(fd, SIOCIPFL6, &fl) == -1) {
363ab25eeb5Syz155240 					perror("ioctl(SIOCIPFL6)");
364ab25eeb5Syz155240 					exit(1);
365ab25eeb5Syz155240 				}
3667663b816Sml37995 			} else {
367ab25eeb5Syz155240 				if (ioctl(fd, SIOCIPFFL, &fl) == -1) {
368ab25eeb5Syz155240 					perror("ioctl(SIOCIPFFL)");
369ab25eeb5Syz155240 					exit(1);
3707663b816Sml37995 				}
3717663b816Sml37995 			}
372ab25eeb5Syz155240 		}
3737c478bd9Sstevel@tonic-gate 		if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
3747c478bd9Sstevel@tonic-gate 			printf("remove flags %s (%d)\n", arg, rem);
3757c478bd9Sstevel@tonic-gate 			printf("removed %d filter rules\n", fl);
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 		closedevice();
3787c478bd9Sstevel@tonic-gate 		return;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #ifdef	SIOCIPFFA
3827c478bd9Sstevel@tonic-gate 	if (!strcmp(arg, "u")) {
3837c478bd9Sstevel@tonic-gate 		closedevice();
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * Flush auth rules and packets
3867c478bd9Sstevel@tonic-gate 		 */
3877c478bd9Sstevel@tonic-gate 		if (opendevice(IPL_AUTH, 1) == -1)
3887c478bd9Sstevel@tonic-gate 			perror("open(IPL_AUTH)");
3897c478bd9Sstevel@tonic-gate 		else {
3907c478bd9Sstevel@tonic-gate 			if (ioctl(fd, SIOCIPFFA, &fl) == -1)
3917c478bd9Sstevel@tonic-gate 				perror("ioctl(SIOCIPFFA)");
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 		closedevice();
3947c478bd9Sstevel@tonic-gate 		return;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate #endif
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (strchr(arg, 'i') || strchr(arg, 'I'))
3997c478bd9Sstevel@tonic-gate 		fl = FR_INQUE;
4007c478bd9Sstevel@tonic-gate 	if (strchr(arg, 'o') || strchr(arg, 'O'))
4017c478bd9Sstevel@tonic-gate 		fl = FR_OUTQUE;
4027c478bd9Sstevel@tonic-gate 	if (strchr(arg, 'a') || strchr(arg, 'A'))
4037c478bd9Sstevel@tonic-gate 		fl = FR_OUTQUE|FR_INQUE;
4047c478bd9Sstevel@tonic-gate 	if (opts & OPT_INACTIVE)
4057c478bd9Sstevel@tonic-gate 		fl |= FR_INACTIVE;
4067c478bd9Sstevel@tonic-gate 	rem = fl;
4077c478bd9Sstevel@tonic-gate 
408ab25eeb5Syz155240 	if (opendevice(ipfname, 1) == -2)
409ab25eeb5Syz155240 		exit(1);
410ab25eeb5Syz155240 
411ab25eeb5Syz155240 	if (!(opts & OPT_DONOTHING)) {
4127663b816Sml37995 		if (use_inet6) {
413ab25eeb5Syz155240 			if (ioctl(fd, SIOCIPFL6, &fl) == -1) {
414ab25eeb5Syz155240 				perror("ioctl(SIOCIPFL6)");
415ab25eeb5Syz155240 				exit(1);
416ab25eeb5Syz155240 			}
4177663b816Sml37995 		} else {
418ab25eeb5Syz155240 			if (ioctl(fd, SIOCIPFFL, &fl) == -1) {
419ab25eeb5Syz155240 				perror("ioctl(SIOCIPFFL)");
420ab25eeb5Syz155240 				exit(1);
421ab25eeb5Syz155240 			}
4227663b816Sml37995 		}
4237663b816Sml37995 	}
4247663b816Sml37995 
4257c478bd9Sstevel@tonic-gate 	if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
4267c478bd9Sstevel@tonic-gate 		printf("remove flags %s%s (%d)\n", (rem & FR_INQUE) ? "I" : "",
4277c478bd9Sstevel@tonic-gate 			(rem & FR_OUTQUE) ? "O" : "", rem);
4287c478bd9Sstevel@tonic-gate 		printf("removed %d filter rules\n", fl);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 	return;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static void swapactive()
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	int in = 2;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (opendevice(ipfname, 1) != -2 && ioctl(fd, SIOCSWAPA, &in) == -1)
4397c478bd9Sstevel@tonic-gate 		perror("ioctl(SIOCSWAPA)");
4407c478bd9Sstevel@tonic-gate 	else
4417c478bd9Sstevel@tonic-gate 		printf("Set %d now inactive\n", in);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 
445ab25eeb5Syz155240 void ipf_frsync()
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	int frsyn = 0;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (opendevice(ipfname, 1) != -2 && ioctl(fd, SIOCFRSYN, &frsyn) == -1)
4507c478bd9Sstevel@tonic-gate 		perror("SIOCFRSYN");
4517c478bd9Sstevel@tonic-gate 	else
4527c478bd9Sstevel@tonic-gate 		printf("filter sync'd\n");
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate void zerostats()
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	friostat_t	fio;
4597c478bd9Sstevel@tonic-gate 	friostat_t	*fiop = &fio;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (opendevice(ipfname, 1) != -2) {
4627c478bd9Sstevel@tonic-gate 		if (ioctl(fd, SIOCFRZST, &fiop) == -1) {
4637c478bd9Sstevel@tonic-gate 			perror("ioctl(SIOCFRZST)");
4647c478bd9Sstevel@tonic-gate 			exit(-1);
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		showstats(fiop);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate  * read the kernel stats for packets blocked and passed
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate static void showstats(fp)
4767c478bd9Sstevel@tonic-gate friostat_t	*fp;
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	printf("bad packets:\t\tin %lu\tout %lu\n",
4797c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
4807c478bd9Sstevel@tonic-gate 	printf(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
4817c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
4827c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_nom);
4837c478bd9Sstevel@tonic-gate 	printf(" counted %lu\n", fp->f_st[0].fr_acct);
4847c478bd9Sstevel@tonic-gate 	printf("output packets:\t\tblocked %lu passed %lu nomatch %lu",
4857c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
4867c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_nom);
4877c478bd9Sstevel@tonic-gate 	printf(" counted %lu\n", fp->f_st[0].fr_acct);
4887c478bd9Sstevel@tonic-gate 	printf(" input packets logged:\tblocked %lu passed %lu\n",
4897c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
4907c478bd9Sstevel@tonic-gate 	printf("output packets logged:\tblocked %lu passed %lu\n",
4917c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
4927c478bd9Sstevel@tonic-gate 	printf(" packets logged:\tinput %lu-%lu output %lu-%lu\n",
4937c478bd9Sstevel@tonic-gate 			fp->f_st[0].fr_pkl, fp->f_st[0].fr_skip,
4947c478bd9Sstevel@tonic-gate 			fp->f_st[1].fr_pkl, fp->f_st[1].fr_skip);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static int showversion()
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	struct friostat fio;
5017c478bd9Sstevel@tonic-gate 	ipfobj_t ipfo;
5027c478bd9Sstevel@tonic-gate 	u_32_t flags;
5037c478bd9Sstevel@tonic-gate 	char *s;
5047c478bd9Sstevel@tonic-gate 	int vfd;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&ipfo, sizeof(ipfo));
5077c478bd9Sstevel@tonic-gate 	ipfo.ipfo_rev = IPFILTER_VERSION;
5087c478bd9Sstevel@tonic-gate 	ipfo.ipfo_size = sizeof(fio);
5097c478bd9Sstevel@tonic-gate 	ipfo.ipfo_ptr = (void *)&fio;
5107c478bd9Sstevel@tonic-gate 	ipfo.ipfo_type = IPFOBJ_IPFSTAT;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	printf("ipf: %s (%d)\n", IPL_VERSION, (int)sizeof(frentry_t));
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if ((vfd = open(ipfname, O_RDONLY)) == -1) {
5157c478bd9Sstevel@tonic-gate 		perror("open device");
5167c478bd9Sstevel@tonic-gate 		return 1;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (ioctl(vfd, SIOCGETFS, &ipfo)) {
5207c478bd9Sstevel@tonic-gate 		perror("ioctl(SIOCGETFS)");
5217c478bd9Sstevel@tonic-gate 		close(vfd);
5227c478bd9Sstevel@tonic-gate 		return 1;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 	close(vfd);
5257c478bd9Sstevel@tonic-gate 	flags = get_flags();
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	printf("Kernel: %-*.*s\n", (int)sizeof(fio.f_version),
5287c478bd9Sstevel@tonic-gate 		(int)sizeof(fio.f_version), fio.f_version);
5297c478bd9Sstevel@tonic-gate 	printf("Running: %s\n", (fio.f_running > 0) ? "yes" : "no");
5307c478bd9Sstevel@tonic-gate 	printf("Log Flags: %#x = ", flags);
5317c478bd9Sstevel@tonic-gate 	s = "";
5327c478bd9Sstevel@tonic-gate 	if (flags & FF_LOGPASS) {
5337c478bd9Sstevel@tonic-gate 		printf("pass");
5347c478bd9Sstevel@tonic-gate 		s = ", ";
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 	if (flags & FF_LOGBLOCK) {
5377c478bd9Sstevel@tonic-gate 		printf("%sblock", s);
5387c478bd9Sstevel@tonic-gate 		s = ", ";
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	if (flags & FF_LOGNOMATCH) {
5417c478bd9Sstevel@tonic-gate 		printf("%snomatch", s);
5427c478bd9Sstevel@tonic-gate 		s = ", ";
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	if (flags & FF_BLOCKNONIP) {
5457c478bd9Sstevel@tonic-gate 		printf("%snonip", s);
5467c478bd9Sstevel@tonic-gate 		s = ", ";
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 	if (!*s)
5497c478bd9Sstevel@tonic-gate 		printf("none set");
5507c478bd9Sstevel@tonic-gate 	putchar('\n');
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	printf("Default: ");
5537c478bd9Sstevel@tonic-gate 	if (FR_ISPASS(fio.f_defpass))
5547c478bd9Sstevel@tonic-gate 		s = "pass";
5557c478bd9Sstevel@tonic-gate 	else if (FR_ISBLOCK(fio.f_defpass))
5567c478bd9Sstevel@tonic-gate 		s = "block";
5577c478bd9Sstevel@tonic-gate 	else
5587c478bd9Sstevel@tonic-gate 		s = "nomatch -> block";
5597c478bd9Sstevel@tonic-gate 	printf("%s all, Logging: %savailable\n", s, fio.f_logging ? "" : "un");
5607c478bd9Sstevel@tonic-gate 	printf("Active list: %d\n", fio.f_active);
561ab25eeb5Syz155240 	printf("Feature mask: %#x\n", fio.f_features);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return 0;
5647c478bd9Sstevel@tonic-gate }
565