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