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 * 67c478bd9Sstevel@tonic-gate * Copyright 2005 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 #if SOLARIS2 >= 10 257c478bd9Sstevel@tonic-gate #include "ipl.h" 267c478bd9Sstevel@tonic-gate #else 277c478bd9Sstevel@tonic-gate #include "netinet/ipl.h" 287c478bd9Sstevel@tonic-gate #endif 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #if !defined(lint) 317c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)ipf.c 1.23 6/5/96 (C) 1993-2000 Darren Reed"; 327c478bd9Sstevel@tonic-gate static const char rcsid[] = "@(#)$Id: ipf.c,v 1.24 2003/07/01 16:30:47 darrenr Exp $"; 337c478bd9Sstevel@tonic-gate #endif 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #if SOLARIS 367c478bd9Sstevel@tonic-gate static void blockunknown __P((void)); 377c478bd9Sstevel@tonic-gate #endif 387c478bd9Sstevel@tonic-gate #if !defined(__SVR4) && defined(__GNUC__) 397c478bd9Sstevel@tonic-gate extern char *index __P((const char *, int)); 407c478bd9Sstevel@tonic-gate #endif 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate extern char *optarg; 437c478bd9Sstevel@tonic-gate extern int optind; 447c478bd9Sstevel@tonic-gate extern frentry_t *frtop; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate void frsync __P((void)); 487c478bd9Sstevel@tonic-gate void zerostats __P((void)); 497c478bd9Sstevel@tonic-gate int main __P((int, char *[])); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate int opts = 0; 527c478bd9Sstevel@tonic-gate int outputc = 0; 537c478bd9Sstevel@tonic-gate int use_inet6 = 0; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate static void procfile __P((char *, char *)), flushfilter __P((char *)); 567c478bd9Sstevel@tonic-gate static void set_state __P((u_int)), showstats __P((friostat_t *)); 577c478bd9Sstevel@tonic-gate static void packetlogon __P((char *)), swapactive __P((void)); 587c478bd9Sstevel@tonic-gate static int opendevice __P((char *, int)); 597c478bd9Sstevel@tonic-gate static void closedevice __P((void)); 607c478bd9Sstevel@tonic-gate static char *ipfname = IPL_NAME; 617c478bd9Sstevel@tonic-gate static void usage __P((void)); 627c478bd9Sstevel@tonic-gate static int showversion __P((void)); 637c478bd9Sstevel@tonic-gate static int get_flags __P((void)); 647c478bd9Sstevel@tonic-gate static void ipf_interceptadd __P((int, ioctlfunc_t, void *)); 657c478bd9Sstevel@tonic-gate static void dotuning __P((char *)); 667c478bd9Sstevel@tonic-gate static void printtunable __P((ipftune_t *)); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static int fd = -1; 697c478bd9Sstevel@tonic-gate static ioctlfunc_t iocfunctions[IPL_LOGSIZE] = { ioctl, ioctl, ioctl, 707c478bd9Sstevel@tonic-gate ioctl, ioctl, ioctl, 717c478bd9Sstevel@tonic-gate ioctl, ioctl }; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate static void usage() 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: ipf [-" 777c478bd9Sstevel@tonic-gate #ifdef USE_INET6 787c478bd9Sstevel@tonic-gate "6" 797c478bd9Sstevel@tonic-gate #endif 807c478bd9Sstevel@tonic-gate "AdDEInoPrsUvVyzZ] %s %s %s %s\n", 817c478bd9Sstevel@tonic-gate "[-l block|pass|nomatch|state|nat]", "[-T optionlist]", 827c478bd9Sstevel@tonic-gate "[-F i|o|a|s|S|u]", "[-f filename]"); 837c478bd9Sstevel@tonic-gate exit(1); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate int main(argc,argv) 887c478bd9Sstevel@tonic-gate int argc; 897c478bd9Sstevel@tonic-gate char *argv[]; 907c478bd9Sstevel@tonic-gate { 917c478bd9Sstevel@tonic-gate int c; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate if (argc < 2) 947c478bd9Sstevel@tonic-gate usage(); 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "6ACdDEf:F:Il:noPrsT:UvVyzZ")) != -1) { 977c478bd9Sstevel@tonic-gate switch (c) 987c478bd9Sstevel@tonic-gate { 997c478bd9Sstevel@tonic-gate case '?' : 1007c478bd9Sstevel@tonic-gate usage(); 1017c478bd9Sstevel@tonic-gate break; 1027c478bd9Sstevel@tonic-gate #ifdef USE_INET6 1037c478bd9Sstevel@tonic-gate case '6' : 1047c478bd9Sstevel@tonic-gate use_inet6 = 1; 1057c478bd9Sstevel@tonic-gate break; 1067c478bd9Sstevel@tonic-gate #endif 1077c478bd9Sstevel@tonic-gate case 'A' : 1087c478bd9Sstevel@tonic-gate opts &= ~OPT_INACTIVE; 1097c478bd9Sstevel@tonic-gate break; 1107c478bd9Sstevel@tonic-gate #ifdef USE_OPTIONC 1117c478bd9Sstevel@tonic-gate case 'C' : 1127c478bd9Sstevel@tonic-gate outputc = 1; 1137c478bd9Sstevel@tonic-gate break; 1147c478bd9Sstevel@tonic-gate #endif 1157c478bd9Sstevel@tonic-gate case 'E' : 1167c478bd9Sstevel@tonic-gate set_state((u_int)1); 1177c478bd9Sstevel@tonic-gate break; 1187c478bd9Sstevel@tonic-gate case 'D' : 1197c478bd9Sstevel@tonic-gate set_state((u_int)0); 1207c478bd9Sstevel@tonic-gate break; 1217c478bd9Sstevel@tonic-gate case 'd' : 1227c478bd9Sstevel@tonic-gate opts ^= OPT_DEBUG; 1237c478bd9Sstevel@tonic-gate break; 1247c478bd9Sstevel@tonic-gate case 'f' : 1257c478bd9Sstevel@tonic-gate procfile(argv[0], optarg); 1267c478bd9Sstevel@tonic-gate break; 1277c478bd9Sstevel@tonic-gate case 'F' : 1287c478bd9Sstevel@tonic-gate flushfilter(optarg); 1297c478bd9Sstevel@tonic-gate break; 1307c478bd9Sstevel@tonic-gate case 'I' : 1317c478bd9Sstevel@tonic-gate opts ^= OPT_INACTIVE; 1327c478bd9Sstevel@tonic-gate break; 1337c478bd9Sstevel@tonic-gate case 'l' : 1347c478bd9Sstevel@tonic-gate packetlogon(optarg); 1357c478bd9Sstevel@tonic-gate break; 1367c478bd9Sstevel@tonic-gate case 'n' : 1377c478bd9Sstevel@tonic-gate opts ^= OPT_DONOTHING; 1387c478bd9Sstevel@tonic-gate break; 1397c478bd9Sstevel@tonic-gate case 'o' : 1407c478bd9Sstevel@tonic-gate break; 1417c478bd9Sstevel@tonic-gate case 'P' : 1427c478bd9Sstevel@tonic-gate ipfname = IPAUTH_NAME; 1437c478bd9Sstevel@tonic-gate break; 1447c478bd9Sstevel@tonic-gate case 'r' : 1457c478bd9Sstevel@tonic-gate opts ^= OPT_REMOVE; 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate case 's' : 1487c478bd9Sstevel@tonic-gate swapactive(); 1497c478bd9Sstevel@tonic-gate break; 1507c478bd9Sstevel@tonic-gate case 'T' : 1517c478bd9Sstevel@tonic-gate dotuning(optarg); 1527c478bd9Sstevel@tonic-gate break; 1537c478bd9Sstevel@tonic-gate #if SOLARIS 1547c478bd9Sstevel@tonic-gate case 'U' : 1557c478bd9Sstevel@tonic-gate blockunknown(); 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate #endif 1587c478bd9Sstevel@tonic-gate case 'v' : 1597c478bd9Sstevel@tonic-gate opts += OPT_VERBOSE; 1607c478bd9Sstevel@tonic-gate break; 1617c478bd9Sstevel@tonic-gate case 'V' : 1627c478bd9Sstevel@tonic-gate if (showversion()) 1637c478bd9Sstevel@tonic-gate exit(1); 1647c478bd9Sstevel@tonic-gate break; 1657c478bd9Sstevel@tonic-gate case 'y' : 1667c478bd9Sstevel@tonic-gate frsync(); 1677c478bd9Sstevel@tonic-gate break; 1687c478bd9Sstevel@tonic-gate case 'z' : 1697c478bd9Sstevel@tonic-gate opts ^= OPT_ZERORULEST; 1707c478bd9Sstevel@tonic-gate break; 1717c478bd9Sstevel@tonic-gate case 'Z' : 1727c478bd9Sstevel@tonic-gate zerostats(); 1737c478bd9Sstevel@tonic-gate break; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate if (optind < 2) 1787c478bd9Sstevel@tonic-gate usage(); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (fd != -1) 1817c478bd9Sstevel@tonic-gate (void) close(fd); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate return(0); 1847c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate static int opendevice(ipfdev, check) 1897c478bd9Sstevel@tonic-gate char *ipfdev; 1907c478bd9Sstevel@tonic-gate int check; 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate if (opts & OPT_DONOTHING) 1937c478bd9Sstevel@tonic-gate return -2; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (check && checkrev(ipfname) == -1) { 1967c478bd9Sstevel@tonic-gate fprintf(stderr, "User/kernel version check failed\n"); 1977c478bd9Sstevel@tonic-gate return -2; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate if (!ipfdev) 2017c478bd9Sstevel@tonic-gate ipfdev = ipfname; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate if (fd == -1) 2047c478bd9Sstevel@tonic-gate if ((fd = open(ipfdev, O_RDWR)) == -1) 2057c478bd9Sstevel@tonic-gate if ((fd = open(ipfdev, O_RDONLY)) == -1) 2067c478bd9Sstevel@tonic-gate perror("open device"); 2077c478bd9Sstevel@tonic-gate return fd; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate static void closedevice() 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate close(fd); 2147c478bd9Sstevel@tonic-gate fd = -1; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate static int get_flags() 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate int i; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if ((opendevice(ipfname, 1) != -2) && 2237c478bd9Sstevel@tonic-gate (ioctl(fd, SIOCGETFF, &i) == -1)) { 2247c478bd9Sstevel@tonic-gate perror("SIOCGETFF"); 2257c478bd9Sstevel@tonic-gate return 0; 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate return i; 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate static void set_state(enable) 2327c478bd9Sstevel@tonic-gate u_int enable; 2337c478bd9Sstevel@tonic-gate { 2347c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 0) != -2) 2357c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCFRENB, &enable) == -1) { 2367c478bd9Sstevel@tonic-gate if (errno == EBUSY) 2377c478bd9Sstevel@tonic-gate fprintf(stderr, 2387c478bd9Sstevel@tonic-gate "IP FIlter: already initialized\n"); 2397c478bd9Sstevel@tonic-gate else 2407c478bd9Sstevel@tonic-gate perror("SIOCFRENB"); 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate return; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate static void procfile(name, file) 2477c478bd9Sstevel@tonic-gate char *name, *file; 2487c478bd9Sstevel@tonic-gate { 2497c478bd9Sstevel@tonic-gate (void) opendevice(ipfname, 1); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate initparse(); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate ipf_parsefile(fd, ipf_interceptadd, iocfunctions, file); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate if (outputc) { 2567c478bd9Sstevel@tonic-gate printC(0); 2577c478bd9Sstevel@tonic-gate printC(1); 2587c478bd9Sstevel@tonic-gate emit(-1, -1, NULL, NULL); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static void ipf_interceptadd(fd, ioctlfunc, ptr) 2647c478bd9Sstevel@tonic-gate int fd; 2657c478bd9Sstevel@tonic-gate ioctlfunc_t ioctlfunc; 2667c478bd9Sstevel@tonic-gate void *ptr; 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate if (outputc) 2697c478bd9Sstevel@tonic-gate printc(ptr); 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate ipf_addrule(fd, ioctlfunc, ptr); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate static void packetlogon(opt) 2767c478bd9Sstevel@tonic-gate char *opt; 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate int flag, xfd, logopt; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate flag = get_flags(); 2817c478bd9Sstevel@tonic-gate if (flag != 0) { 2827c478bd9Sstevel@tonic-gate if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) 2837c478bd9Sstevel@tonic-gate printf("log flag is currently %#x\n", flag); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate flag &= ~(FF_LOGPASS|FF_LOGNOMATCH|FF_LOGBLOCK); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate if (strstr(opt, "pass")) { 2897c478bd9Sstevel@tonic-gate flag |= FF_LOGPASS; 2907c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 2917c478bd9Sstevel@tonic-gate printf("set log flag: pass\n"); 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate if (strstr(opt, "nomatch")) { 2947c478bd9Sstevel@tonic-gate flag |= FF_LOGNOMATCH; 2957c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 2967c478bd9Sstevel@tonic-gate printf("set log flag: nomatch\n"); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate if (strstr(opt, "block") || index(opt, 'd')) { 2997c478bd9Sstevel@tonic-gate flag |= FF_LOGBLOCK; 3007c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 3017c478bd9Sstevel@tonic-gate printf("set log flag: block\n"); 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) != -2 && (ioctl(fd, SIOCSETFF, &flag) != 0)) 3057c478bd9Sstevel@tonic-gate perror("ioctl(SIOCSETFF)"); 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) { 3087c478bd9Sstevel@tonic-gate flag = get_flags(); 3097c478bd9Sstevel@tonic-gate printf("log flag is now %#x\n", flag); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (strstr(opt, "state")) { 3137c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 3147c478bd9Sstevel@tonic-gate printf("set state log flag\n"); 3157c478bd9Sstevel@tonic-gate xfd = open(IPSTATE_NAME, O_RDWR); 3167c478bd9Sstevel@tonic-gate if (xfd >= 0) { 3177c478bd9Sstevel@tonic-gate logopt = 0; 3187c478bd9Sstevel@tonic-gate if (ioctl(xfd, SIOCGETLG, &logopt)) 3197c478bd9Sstevel@tonic-gate perror("ioctl(SIOCGETLG)"); 3207c478bd9Sstevel@tonic-gate else { 3217c478bd9Sstevel@tonic-gate logopt = 1 - logopt; 3227c478bd9Sstevel@tonic-gate if (ioctl(xfd, SIOCSETLG, &logopt)) 3237c478bd9Sstevel@tonic-gate perror("ioctl(SIOCSETLG)"); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate close(xfd); 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate if (strstr(opt, "nat")) { 3307c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 3317c478bd9Sstevel@tonic-gate printf("set nat log flag\n"); 3327c478bd9Sstevel@tonic-gate xfd = open(IPNAT_NAME, O_RDWR); 3337c478bd9Sstevel@tonic-gate if (xfd >= 0) { 3347c478bd9Sstevel@tonic-gate logopt = 0; 3357c478bd9Sstevel@tonic-gate if (ioctl(xfd, SIOCGETLG, &logopt)) 3367c478bd9Sstevel@tonic-gate perror("ioctl(SIOCGETLG)"); 3377c478bd9Sstevel@tonic-gate else { 3387c478bd9Sstevel@tonic-gate logopt = 1 - logopt; 3397c478bd9Sstevel@tonic-gate if (ioctl(xfd, SIOCSETLG, &logopt)) 3407c478bd9Sstevel@tonic-gate perror("ioctl(SIOCSETLG)"); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate close(xfd); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate static void flushfilter(arg) 3497c478bd9Sstevel@tonic-gate char *arg; 3507c478bd9Sstevel@tonic-gate { 3517c478bd9Sstevel@tonic-gate int fl = 0, rem; 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate if (!arg || !*arg) 3547c478bd9Sstevel@tonic-gate return; 3557c478bd9Sstevel@tonic-gate if (!strcmp(arg, "s") || !strcmp(arg, "S")) { 3567c478bd9Sstevel@tonic-gate if (*arg == 'S') 3577c478bd9Sstevel@tonic-gate fl = 0; 3587c478bd9Sstevel@tonic-gate else 3597c478bd9Sstevel@tonic-gate fl = 1; 3607c478bd9Sstevel@tonic-gate rem = fl; 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate closedevice(); 363*7663b816Sml37995 364*7663b816Sml37995 if (opendevice(IPSTATE_NAME, 1) != -2) { 365*7663b816Sml37995 if (use_inet6) { 366*7663b816Sml37995 #ifdef USE_INET6 367*7663b816Sml37995 if (ioctl(fd, SIOCIPFL6, &fl) == -1) 368*7663b816Sml37995 perror("SIOCIPFL6"); 369*7663b816Sml37995 #endif 370*7663b816Sml37995 } else { 371*7663b816Sml37995 if (ioctl(fd, SIOCIPFFL, &fl) == -1) 372*7663b816Sml37995 perror("SIOCIPFFL"); 373*7663b816Sml37995 } 374*7663b816Sml37995 } 375*7663b816Sml37995 3767c478bd9Sstevel@tonic-gate if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) { 3777c478bd9Sstevel@tonic-gate printf("remove flags %s (%d)\n", arg, rem); 3787c478bd9Sstevel@tonic-gate printf("removed %d filter rules\n", fl); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate closedevice(); 3817c478bd9Sstevel@tonic-gate return; 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate #ifdef SIOCIPFFA 3857c478bd9Sstevel@tonic-gate if (!strcmp(arg, "u")) { 3867c478bd9Sstevel@tonic-gate closedevice(); 3877c478bd9Sstevel@tonic-gate /* 3887c478bd9Sstevel@tonic-gate * Flush auth rules and packets 3897c478bd9Sstevel@tonic-gate */ 3907c478bd9Sstevel@tonic-gate if (opendevice(IPL_AUTH, 1) == -1) 3917c478bd9Sstevel@tonic-gate perror("open(IPL_AUTH)"); 3927c478bd9Sstevel@tonic-gate else { 3937c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCIPFFA, &fl) == -1) 3947c478bd9Sstevel@tonic-gate perror("ioctl(SIOCIPFFA)"); 3957c478bd9Sstevel@tonic-gate } 3967c478bd9Sstevel@tonic-gate closedevice(); 3977c478bd9Sstevel@tonic-gate return; 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate #endif 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate if (strchr(arg, 'i') || strchr(arg, 'I')) 4027c478bd9Sstevel@tonic-gate fl = FR_INQUE; 4037c478bd9Sstevel@tonic-gate if (strchr(arg, 'o') || strchr(arg, 'O')) 4047c478bd9Sstevel@tonic-gate fl = FR_OUTQUE; 4057c478bd9Sstevel@tonic-gate if (strchr(arg, 'a') || strchr(arg, 'A')) 4067c478bd9Sstevel@tonic-gate fl = FR_OUTQUE|FR_INQUE; 4077c478bd9Sstevel@tonic-gate if (opts & OPT_INACTIVE) 4087c478bd9Sstevel@tonic-gate fl |= FR_INACTIVE; 4097c478bd9Sstevel@tonic-gate rem = fl; 4107c478bd9Sstevel@tonic-gate 411*7663b816Sml37995 if (opendevice(ipfname, 1) != -2) { 412*7663b816Sml37995 if (use_inet6) { 413*7663b816Sml37995 #ifdef USE_INET6 414*7663b816Sml37995 if (ioctl(fd, SIOCIPFL6, &fl) == -1) 415*7663b816Sml37995 perror("SIOCIPFL6"); 416*7663b816Sml37995 #endif 417*7663b816Sml37995 } else { 418*7663b816Sml37995 if (ioctl(fd, SIOCIPFFL, &fl) == -1) 419*7663b816Sml37995 perror("SIOCIPFFL"); 420*7663b816Sml37995 } 421*7663b816Sml37995 } 422*7663b816Sml37995 4237c478bd9Sstevel@tonic-gate if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) { 4247c478bd9Sstevel@tonic-gate printf("remove flags %s%s (%d)\n", (rem & FR_INQUE) ? "I" : "", 4257c478bd9Sstevel@tonic-gate (rem & FR_OUTQUE) ? "O" : "", rem); 4267c478bd9Sstevel@tonic-gate printf("removed %d filter rules\n", fl); 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate return; 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate static void swapactive() 4337c478bd9Sstevel@tonic-gate { 4347c478bd9Sstevel@tonic-gate int in = 2; 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) != -2 && ioctl(fd, SIOCSWAPA, &in) == -1) 4377c478bd9Sstevel@tonic-gate perror("ioctl(SIOCSWAPA)"); 4387c478bd9Sstevel@tonic-gate else 4397c478bd9Sstevel@tonic-gate printf("Set %d now inactive\n", in); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate void frsync() 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate int frsyn = 0; 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) != -2 && ioctl(fd, SIOCFRSYN, &frsyn) == -1) 4487c478bd9Sstevel@tonic-gate perror("SIOCFRSYN"); 4497c478bd9Sstevel@tonic-gate else 4507c478bd9Sstevel@tonic-gate printf("filter sync'd\n"); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate 4547c478bd9Sstevel@tonic-gate void zerostats() 4557c478bd9Sstevel@tonic-gate { 4567c478bd9Sstevel@tonic-gate friostat_t fio; 4577c478bd9Sstevel@tonic-gate friostat_t *fiop = &fio; 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) != -2) { 4607c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCFRZST, &fiop) == -1) { 4617c478bd9Sstevel@tonic-gate perror("ioctl(SIOCFRZST)"); 4627c478bd9Sstevel@tonic-gate exit(-1); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate showstats(fiop); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * read the kernel stats for packets blocked and passed 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate static void showstats(fp) 4747c478bd9Sstevel@tonic-gate friostat_t *fp; 4757c478bd9Sstevel@tonic-gate { 4767c478bd9Sstevel@tonic-gate printf("bad packets:\t\tin %lu\tout %lu\n", 4777c478bd9Sstevel@tonic-gate fp->f_st[0].fr_bad, fp->f_st[1].fr_bad); 4787c478bd9Sstevel@tonic-gate printf(" input packets:\t\tblocked %lu passed %lu nomatch %lu", 4797c478bd9Sstevel@tonic-gate fp->f_st[0].fr_block, fp->f_st[0].fr_pass, 4807c478bd9Sstevel@tonic-gate fp->f_st[0].fr_nom); 4817c478bd9Sstevel@tonic-gate printf(" counted %lu\n", fp->f_st[0].fr_acct); 4827c478bd9Sstevel@tonic-gate printf("output packets:\t\tblocked %lu passed %lu nomatch %lu", 4837c478bd9Sstevel@tonic-gate fp->f_st[1].fr_block, fp->f_st[1].fr_pass, 4847c478bd9Sstevel@tonic-gate fp->f_st[1].fr_nom); 4857c478bd9Sstevel@tonic-gate printf(" counted %lu\n", fp->f_st[0].fr_acct); 4867c478bd9Sstevel@tonic-gate printf(" input packets logged:\tblocked %lu passed %lu\n", 4877c478bd9Sstevel@tonic-gate fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl); 4887c478bd9Sstevel@tonic-gate printf("output packets logged:\tblocked %lu passed %lu\n", 4897c478bd9Sstevel@tonic-gate fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl); 4907c478bd9Sstevel@tonic-gate printf(" packets logged:\tinput %lu-%lu output %lu-%lu\n", 4917c478bd9Sstevel@tonic-gate fp->f_st[0].fr_pkl, fp->f_st[0].fr_skip, 4927c478bd9Sstevel@tonic-gate fp->f_st[1].fr_pkl, fp->f_st[1].fr_skip); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate #if SOLARIS 4977c478bd9Sstevel@tonic-gate static void blockunknown() 4987c478bd9Sstevel@tonic-gate { 4997c478bd9Sstevel@tonic-gate u_32_t flag; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) == -1) 5027c478bd9Sstevel@tonic-gate return; 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate flag = get_flags(); 5057c478bd9Sstevel@tonic-gate if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) 5067c478bd9Sstevel@tonic-gate printf("log flag is currently %#x\n", flag); 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate flag ^= FF_BLOCKNONIP; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) != -2 && ioctl(fd, SIOCSETFF, &flag)) 5117c478bd9Sstevel@tonic-gate perror("ioctl(SIOCSETFF)"); 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) { 5147c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCGETFF, &flag)) 5157c478bd9Sstevel@tonic-gate perror("ioctl(SIOCGETFF)"); 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate printf("log flag is now %#x\n", flag); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate #endif 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate static int showversion() 5247c478bd9Sstevel@tonic-gate { 5257c478bd9Sstevel@tonic-gate struct friostat fio; 5267c478bd9Sstevel@tonic-gate ipfobj_t ipfo; 5277c478bd9Sstevel@tonic-gate u_32_t flags; 5287c478bd9Sstevel@tonic-gate char *s; 5297c478bd9Sstevel@tonic-gate int vfd; 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate bzero((caddr_t)&ipfo, sizeof(ipfo)); 5327c478bd9Sstevel@tonic-gate ipfo.ipfo_rev = IPFILTER_VERSION; 5337c478bd9Sstevel@tonic-gate ipfo.ipfo_size = sizeof(fio); 5347c478bd9Sstevel@tonic-gate ipfo.ipfo_ptr = (void *)&fio; 5357c478bd9Sstevel@tonic-gate ipfo.ipfo_type = IPFOBJ_IPFSTAT; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate printf("ipf: %s (%d)\n", IPL_VERSION, (int)sizeof(frentry_t)); 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate if ((vfd = open(ipfname, O_RDONLY)) == -1) { 5407c478bd9Sstevel@tonic-gate perror("open device"); 5417c478bd9Sstevel@tonic-gate return 1; 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate if (ioctl(vfd, SIOCGETFS, &ipfo)) { 5457c478bd9Sstevel@tonic-gate perror("ioctl(SIOCGETFS)"); 5467c478bd9Sstevel@tonic-gate close(vfd); 5477c478bd9Sstevel@tonic-gate return 1; 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate close(vfd); 5507c478bd9Sstevel@tonic-gate flags = get_flags(); 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate printf("Kernel: %-*.*s\n", (int)sizeof(fio.f_version), 5537c478bd9Sstevel@tonic-gate (int)sizeof(fio.f_version), fio.f_version); 5547c478bd9Sstevel@tonic-gate printf("Running: %s\n", (fio.f_running > 0) ? "yes" : "no"); 5557c478bd9Sstevel@tonic-gate printf("Log Flags: %#x = ", flags); 5567c478bd9Sstevel@tonic-gate s = ""; 5577c478bd9Sstevel@tonic-gate if (flags & FF_LOGPASS) { 5587c478bd9Sstevel@tonic-gate printf("pass"); 5597c478bd9Sstevel@tonic-gate s = ", "; 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate if (flags & FF_LOGBLOCK) { 5627c478bd9Sstevel@tonic-gate printf("%sblock", s); 5637c478bd9Sstevel@tonic-gate s = ", "; 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate if (flags & FF_LOGNOMATCH) { 5667c478bd9Sstevel@tonic-gate printf("%snomatch", s); 5677c478bd9Sstevel@tonic-gate s = ", "; 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate if (flags & FF_BLOCKNONIP) { 5707c478bd9Sstevel@tonic-gate printf("%snonip", s); 5717c478bd9Sstevel@tonic-gate s = ", "; 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate if (!*s) 5747c478bd9Sstevel@tonic-gate printf("none set"); 5757c478bd9Sstevel@tonic-gate putchar('\n'); 5767c478bd9Sstevel@tonic-gate 5777c478bd9Sstevel@tonic-gate printf("Default: "); 5787c478bd9Sstevel@tonic-gate if (FR_ISPASS(fio.f_defpass)) 5797c478bd9Sstevel@tonic-gate s = "pass"; 5807c478bd9Sstevel@tonic-gate else if (FR_ISBLOCK(fio.f_defpass)) 5817c478bd9Sstevel@tonic-gate s = "block"; 5827c478bd9Sstevel@tonic-gate else 5837c478bd9Sstevel@tonic-gate s = "nomatch -> block"; 5847c478bd9Sstevel@tonic-gate printf("%s all, Logging: %savailable\n", s, fio.f_logging ? "" : "un"); 5857c478bd9Sstevel@tonic-gate printf("Active list: %d\n", fio.f_active); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate return 0; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate static void dotuning(tuneargs) 5927c478bd9Sstevel@tonic-gate char *tuneargs; 5937c478bd9Sstevel@tonic-gate { 5947c478bd9Sstevel@tonic-gate ipfobj_t obj; 5957c478bd9Sstevel@tonic-gate ipftune_t tu; 5967c478bd9Sstevel@tonic-gate char *s, *t; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate if (opendevice(ipfname, 1) < 0) 5997c478bd9Sstevel@tonic-gate return; 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate bzero((char *)&tu, sizeof(tu)); 6027c478bd9Sstevel@tonic-gate obj.ipfo_rev = IPFILTER_VERSION; 6037c478bd9Sstevel@tonic-gate obj.ipfo_size = sizeof(tu);; 6047c478bd9Sstevel@tonic-gate obj.ipfo_ptr = (void *)&tu; 6057c478bd9Sstevel@tonic-gate obj.ipfo_type = IPFOBJ_TUNEABLE; 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate for (s = strtok(tuneargs, ","); s != NULL; s = strtok(NULL, ",")) { 6087c478bd9Sstevel@tonic-gate if (!strcmp(s, "list")) { 6097c478bd9Sstevel@tonic-gate while (1) { 6107c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCIPFGETNEXT, &obj) == -1) { 6117c478bd9Sstevel@tonic-gate perror("ioctl(SIOCIPFGETNEXT)"); 6127c478bd9Sstevel@tonic-gate break; 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate if (tu.ipft_cookie == NULL) 6157c478bd9Sstevel@tonic-gate break; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0'; 6187c478bd9Sstevel@tonic-gate printtunable(&tu); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate } else if ((t = strchr(s, '=')) != NULL) { 6217c478bd9Sstevel@tonic-gate *t++ = '\0'; 6227c478bd9Sstevel@tonic-gate strncpy(tu.ipft_name, s, sizeof(tu.ipft_name)); 6237c478bd9Sstevel@tonic-gate if (sscanf(t, "%lu", &tu.ipft_vlong) == 1) { 6247c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCIPFSET, &obj) == -1) { 6257c478bd9Sstevel@tonic-gate perror("ioctl(SIOCIPFSET)"); 6267c478bd9Sstevel@tonic-gate return; 6277c478bd9Sstevel@tonic-gate } 6287c478bd9Sstevel@tonic-gate } else { 6297c478bd9Sstevel@tonic-gate fprintf(stderr, "invalid value '%s'\n", s); 6307c478bd9Sstevel@tonic-gate return; 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate } else { 6337c478bd9Sstevel@tonic-gate strncpy(tu.ipft_name, s, sizeof(tu.ipft_name)); 6347c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCIPFGET, &obj) == -1) { 6357c478bd9Sstevel@tonic-gate perror("ioctl(SIOCIPFGET)"); 6367c478bd9Sstevel@tonic-gate return; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate if (tu.ipft_cookie == NULL) 6397c478bd9Sstevel@tonic-gate return; 6407c478bd9Sstevel@tonic-gate 6417c478bd9Sstevel@tonic-gate tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0'; 6427c478bd9Sstevel@tonic-gate printtunable(&tu); 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate static void printtunable(tup) 6497c478bd9Sstevel@tonic-gate ipftune_t *tup; 6507c478bd9Sstevel@tonic-gate { 6517c478bd9Sstevel@tonic-gate printf("%s\tmin %#lx\tmax %#lx\tcurrent ", 6527c478bd9Sstevel@tonic-gate tup->ipft_name, tup->ipft_min, tup->ipft_max); 6537c478bd9Sstevel@tonic-gate if (tup->ipft_sz == sizeof(u_long)) 6547c478bd9Sstevel@tonic-gate printf("%lu\n", tup->ipft_vlong); 6557c478bd9Sstevel@tonic-gate else if (tup->ipft_sz == sizeof(u_int)) 6567c478bd9Sstevel@tonic-gate printf("%u\n", tup->ipft_vint); 6577c478bd9Sstevel@tonic-gate else if (tup->ipft_sz == sizeof(u_short)) 6587c478bd9Sstevel@tonic-gate printf("%hu\n", tup->ipft_vshort); 6597c478bd9Sstevel@tonic-gate else if (tup->ipft_sz == sizeof(u_char)) 6607c478bd9Sstevel@tonic-gate printf("%u\n", (u_int)tup->ipft_vchar); 6617c478bd9Sstevel@tonic-gate else { 6627c478bd9Sstevel@tonic-gate printf("sz = %d\n", tup->ipft_sz); 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate } 665