1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2001-07-28 11:59:33 +00:00
|
|
|
* Copyright (C) 1993-2001 by Darren Reed.
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 11:59:33 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
1997-05-25 15:45:04 +00:00
|
|
|
#ifdef __FreeBSD__
|
2001-07-28 11:59:33 +00:00
|
|
|
# ifndef __FreeBSD_cc_version
|
|
|
|
# include <osreldate.h>
|
|
|
|
# else
|
|
|
|
# if __FreeBSD_cc_version < 430000
|
|
|
|
# include <osreldate.h>
|
|
|
|
# endif
|
|
|
|
# endif
|
1997-05-25 15:45:04 +00:00
|
|
|
#endif
|
2003-02-15 06:27:40 +00:00
|
|
|
#if defined(__sgi) && (IRIX > 602)
|
2002-03-19 11:45:20 +00:00
|
|
|
# include <sys/ptimers.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#if !defined(__SVR4) && !defined(__GNUC__)
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
1997-05-25 15:45:04 +00:00
|
|
|
#include <sys/time.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <net/if.h>
|
1997-05-25 15:45:04 +00:00
|
|
|
#if __FreeBSD_version >= 300000
|
|
|
|
# include <net/if_var.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <resolv.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "ip_compat.h"
|
|
|
|
#include "ip_fil.h"
|
1999-11-08 20:51:23 +00:00
|
|
|
#include "ip_nat.h"
|
|
|
|
#include "ip_state.h"
|
1997-02-09 22:50:16 +00:00
|
|
|
#include "ipf.h"
|
1999-11-08 20:51:23 +00:00
|
|
|
#include "ipl.h"
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if !defined(lint)
|
2000-05-24 02:14:22 +00:00
|
|
|
static const char sccsid[] = "@(#)ipf.c 1.23 6/5/96 (C) 1993-2000 Darren Reed";
|
2004-06-21 22:47:51 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id: ipf.c,v 2.10.2.23 2003/06/27 14:39:13 darrenr Exp $";
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if SOLARIS
|
1997-04-03 10:22:02 +00:00
|
|
|
static void blockunknown __P((void));
|
|
|
|
#endif
|
|
|
|
#if !defined(__SVR4) && defined(__GNUC__)
|
|
|
|
extern char *index __P((const char *, int));
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern char *optarg;
|
2004-06-21 22:47:51 +00:00
|
|
|
extern int optind;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1999-11-08 20:51:23 +00:00
|
|
|
void frsync __P((void));
|
1997-04-03 10:22:02 +00:00
|
|
|
void zerostats __P((void));
|
|
|
|
int main __P((int, char *[]));
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
int opts = 0;
|
2000-05-24 02:14:22 +00:00
|
|
|
int use_inet6 = 0;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
static int fd = -1;
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
static void procfile __P((char *, char *)), flushfilter __P((char *));
|
2004-06-21 22:47:51 +00:00
|
|
|
static int set_state __P((u_int));
|
|
|
|
static void showstats __P((friostat_t *));
|
1997-04-03 10:22:02 +00:00
|
|
|
static void packetlogon __P((char *)), swapactive __P((void));
|
1997-11-16 04:52:19 +00:00
|
|
|
static int opendevice __P((char *));
|
|
|
|
static void closedevice __P((void));
|
2000-10-26 12:45:54 +00:00
|
|
|
static char *getline __P((char *, size_t, FILE *, int *));
|
1997-11-16 04:52:19 +00:00
|
|
|
static char *ipfname = IPL_NAME;
|
2004-06-21 22:47:51 +00:00
|
|
|
static void usage __P((char *));
|
2001-07-28 11:59:33 +00:00
|
|
|
static int showversion __P((void));
|
2004-06-21 22:47:51 +00:00
|
|
|
static int get_flags __P((int *));
|
1999-11-08 20:51:23 +00:00
|
|
|
|
|
|
|
|
2001-07-28 11:59:33 +00:00
|
|
|
#if SOLARIS
|
|
|
|
# define OPTS "6AdDEf:F:Il:noPrsUvVyzZ"
|
|
|
|
#else
|
|
|
|
# define OPTS "6AdDEf:F:Il:noPrsvVyzZ"
|
|
|
|
#endif
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
static void usage(name)
|
|
|
|
char *name;
|
1999-11-08 20:51:23 +00:00
|
|
|
{
|
2004-06-21 22:47:51 +00:00
|
|
|
fprintf(stderr, "usage: %s [-%s] %s %s %s\n", name, OPTS,
|
1999-11-08 20:51:23 +00:00
|
|
|
"[-l block|pass|nomatch]", "[-F i|o|a|s|S]", "[-f filename]");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
int main(argc,argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
1997-11-16 04:52:19 +00:00
|
|
|
int c;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (argc < 2)
|
|
|
|
usage(argv[0]);
|
|
|
|
|
2001-07-28 11:59:33 +00:00
|
|
|
while ((c = getopt(argc, argv, OPTS)) != -1) {
|
1997-02-09 22:50:16 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
2000-05-24 02:14:22 +00:00
|
|
|
case '6' :
|
|
|
|
use_inet6 = 1;
|
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
case 'A' :
|
|
|
|
opts &= ~OPT_INACTIVE;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'E' :
|
2004-06-21 22:47:51 +00:00
|
|
|
if (set_state((u_int)1))
|
|
|
|
exit(1);
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'D' :
|
2004-06-21 22:47:51 +00:00
|
|
|
if (set_state((u_int)0))
|
|
|
|
exit(1);
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'd' :
|
|
|
|
opts |= OPT_DEBUG;
|
|
|
|
break;
|
|
|
|
case 'f' :
|
|
|
|
procfile(argv[0], optarg);
|
|
|
|
break;
|
|
|
|
case 'F' :
|
|
|
|
flushfilter(optarg);
|
|
|
|
break;
|
|
|
|
case 'I' :
|
|
|
|
opts |= OPT_INACTIVE;
|
|
|
|
break;
|
|
|
|
case 'l' :
|
|
|
|
packetlogon(optarg);
|
|
|
|
break;
|
|
|
|
case 'n' :
|
|
|
|
opts |= OPT_DONOTHING;
|
|
|
|
break;
|
|
|
|
case 'o' :
|
|
|
|
break;
|
1997-11-16 04:52:19 +00:00
|
|
|
case 'P' :
|
|
|
|
ipfname = IPL_AUTH;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'r' :
|
|
|
|
opts |= OPT_REMOVE;
|
|
|
|
break;
|
|
|
|
case 's' :
|
|
|
|
swapactive();
|
|
|
|
break;
|
|
|
|
#if SOLARIS
|
|
|
|
case 'U' :
|
|
|
|
blockunknown();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'v' :
|
2001-07-28 11:59:33 +00:00
|
|
|
opts += OPT_VERBOSE;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
1999-11-08 20:51:23 +00:00
|
|
|
case 'V' :
|
2001-07-28 11:59:33 +00:00
|
|
|
if (showversion())
|
|
|
|
exit(1);
|
1999-11-08 20:51:23 +00:00
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'y' :
|
|
|
|
frsync();
|
|
|
|
break;
|
|
|
|
case 'z' :
|
|
|
|
opts |= OPT_ZERORULEST;
|
|
|
|
break;
|
|
|
|
case 'Z' :
|
|
|
|
zerostats();
|
|
|
|
break;
|
2004-06-21 22:47:51 +00:00
|
|
|
case '?' :
|
2001-07-28 11:59:33 +00:00
|
|
|
default :
|
2004-06-21 22:47:51 +00:00
|
|
|
usage(argv[0]);
|
2001-07-28 11:59:33 +00:00
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (optind < 2)
|
|
|
|
usage(argv[0]);
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (fd != -1)
|
|
|
|
(void) close(fd);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
static int opendevice(ipfdev)
|
|
|
|
char *ipfdev;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
if (opts & OPT_DONOTHING)
|
2004-06-21 22:47:51 +00:00
|
|
|
return 0;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (!ipfdev)
|
|
|
|
ipfdev = ipfname;
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
/*
|
|
|
|
* shouldn't we really be testing for fd < 0 here and below?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fd != -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((fd = open(ipfdev, O_RDWR)) == -1) {
|
|
|
|
if ((fd = open(ipfdev, O_RDONLY)) == -1) {
|
|
|
|
perror("open device");
|
|
|
|
if (errno == ENODEV)
|
|
|
|
fprintf(stderr, "IPFilter enabled?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
static void closedevice()
|
|
|
|
{
|
2004-06-21 22:47:51 +00:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
1997-11-16 04:52:19 +00:00
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
/*
|
|
|
|
* Return codes:
|
|
|
|
* 0 Success
|
|
|
|
* !0 Failure (and an error message has already been printed)
|
|
|
|
*/
|
|
|
|
static int get_flags(i)
|
|
|
|
int *i;
|
1999-11-08 20:51:23 +00:00
|
|
|
{
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opts & OPT_DONOTHING)
|
1999-11-08 20:51:23 +00:00
|
|
|
return 0;
|
2004-06-21 22:47:51 +00:00
|
|
|
|
|
|
|
if (opendevice(ipfname) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCGETFF, i) == -1) {
|
|
|
|
perror("SIOCGETFF");
|
|
|
|
return -1;
|
1999-11-08 20:51:23 +00:00
|
|
|
}
|
2004-06-21 22:47:51 +00:00
|
|
|
return 0;
|
1999-11-08 20:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
static int set_state(enable)
|
1997-02-09 22:50:16 +00:00
|
|
|
u_int enable;
|
|
|
|
{
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opts & OPT_DONOTHING)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (opendevice(ipfname))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCFRENB, &enable) == -1) {
|
|
|
|
if (errno == EBUSY)
|
|
|
|
/* Not really an error */
|
|
|
|
fprintf(stderr,
|
|
|
|
"IP Filter: already initialized\n");
|
|
|
|
else {
|
|
|
|
perror("SIOCFRENB");
|
|
|
|
return -1;
|
2000-05-24 02:14:22 +00:00
|
|
|
}
|
2004-06-21 22:47:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void procfile(name, file)
|
|
|
|
char *name, *file;
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[513], *s;
|
|
|
|
struct frentry *fr;
|
1999-11-08 20:51:23 +00:00
|
|
|
u_int add, del;
|
|
|
|
int linenum = 0;
|
2004-06-21 22:47:51 +00:00
|
|
|
int parsestatus;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1)
|
|
|
|
exit(1);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
if (opts & OPT_INACTIVE) {
|
|
|
|
add = SIOCADIFR;
|
|
|
|
del = SIOCRMIFR;
|
1999-11-08 20:51:23 +00:00
|
|
|
} else {
|
|
|
|
add = SIOCADAFR;
|
|
|
|
del = SIOCRMAFR;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
if (opts & OPT_DEBUG)
|
|
|
|
printf("add %x del %x\n", add, del);
|
|
|
|
|
|
|
|
initparse();
|
|
|
|
|
|
|
|
if (!strcmp(file, "-"))
|
|
|
|
fp = stdin;
|
|
|
|
else if (!(fp = fopen(file, "r"))) {
|
|
|
|
fprintf(stderr, "%s: fopen(%s) failed: %s\n", name, file,
|
|
|
|
STRERROR(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-10-26 12:45:54 +00:00
|
|
|
while (getline(line, sizeof(line), fp, &linenum)) {
|
1997-02-09 22:50:16 +00:00
|
|
|
/*
|
1998-06-20 18:29:38 +00:00
|
|
|
* treat CR as EOL. LF is converted to NUL by getline().
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
|
|
|
if ((s = index(line, '\r')))
|
|
|
|
*s = '\0';
|
|
|
|
/*
|
|
|
|
* # is comment marker, everything after is a ignored
|
|
|
|
*/
|
|
|
|
if ((s = index(line, '#')))
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
if (!*line)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opts & OPT_VERBOSE)
|
1998-06-20 18:29:38 +00:00
|
|
|
(void)fprintf(stderr, "[%s]\n", line);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
parsestatus = 1;
|
|
|
|
fr = parse(line, linenum, &parsestatus);
|
1997-02-09 22:50:16 +00:00
|
|
|
(void)fflush(stdout);
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (parsestatus != 0) {
|
|
|
|
fprintf(stderr, "%s: %s: %s error (%d), quitting\n",
|
|
|
|
name, file,
|
|
|
|
((parsestatus < 0)? "parse": "internal"),
|
|
|
|
parsestatus);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (fr) {
|
|
|
|
if (opts & OPT_ZERORULEST)
|
|
|
|
add = SIOCZRLST;
|
|
|
|
else if (opts & OPT_INACTIVE)
|
1997-04-03 10:22:02 +00:00
|
|
|
add = (u_int)fr->fr_hits ? SIOCINIFR :
|
|
|
|
SIOCADIFR;
|
1997-02-09 22:50:16 +00:00
|
|
|
else
|
1997-04-03 10:22:02 +00:00
|
|
|
add = (u_int)fr->fr_hits ? SIOCINAFR :
|
|
|
|
SIOCADAFR;
|
1997-02-09 22:50:16 +00:00
|
|
|
if (fr->fr_hits)
|
|
|
|
fr->fr_hits--;
|
|
|
|
if (fr && (opts & OPT_VERBOSE))
|
|
|
|
printfr(fr);
|
|
|
|
if (fr && (opts & OPT_OUTQUE))
|
|
|
|
fr->fr_flags |= FR_OUTQUE;
|
|
|
|
|
|
|
|
if (opts & OPT_DEBUG)
|
|
|
|
binprint(fr);
|
|
|
|
|
|
|
|
if ((opts & OPT_ZERORULEST) &&
|
|
|
|
!(opts & OPT_DONOTHING)) {
|
2000-07-19 13:52:47 +00:00
|
|
|
if (ioctl(fd, add, &fr) == -1) {
|
|
|
|
fprintf(stderr, "%d:", linenum);
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("ioctl(SIOCZRLST)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
2000-07-19 13:52:47 +00:00
|
|
|
} else {
|
1997-04-03 10:22:02 +00:00
|
|
|
#ifdef USE_QUAD_T
|
|
|
|
printf("hits %qd bytes %qd ",
|
2000-05-24 02:14:22 +00:00
|
|
|
(long long)fr->fr_hits,
|
|
|
|
(long long)fr->fr_bytes);
|
1997-04-03 10:22:02 +00:00
|
|
|
#else
|
1997-02-09 22:50:16 +00:00
|
|
|
printf("hits %ld bytes %ld ",
|
|
|
|
fr->fr_hits, fr->fr_bytes);
|
2000-05-24 02:14:22 +00:00
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
printfr(fr);
|
|
|
|
}
|
|
|
|
} else if ((opts & OPT_REMOVE) &&
|
|
|
|
!(opts & OPT_DONOTHING)) {
|
2000-07-19 13:52:47 +00:00
|
|
|
if (ioctl(fd, del, &fr) == -1) {
|
|
|
|
fprintf(stderr, "%d:", linenum);
|
2000-05-24 02:14:22 +00:00
|
|
|
perror("ioctl(delete rule)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
2000-07-19 13:52:47 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
} else if (!(opts & OPT_DONOTHING)) {
|
2000-07-19 13:52:47 +00:00
|
|
|
if (ioctl(fd, add, &fr) == -1) {
|
|
|
|
fprintf(stderr, "%d:", linenum);
|
2000-05-24 02:14:22 +00:00
|
|
|
perror("ioctl(add/insert rule)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
2000-07-19 13:52:47 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-06-20 18:29:38 +00:00
|
|
|
if (ferror(fp) || !feof(fp)) {
|
|
|
|
fprintf(stderr, "%s: %s: file error or line too long\n",
|
|
|
|
name, file);
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
(void)fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-06-20 18:29:38 +00:00
|
|
|
* Similar to fgets(3) but can handle '\\' and NL is converted to NUL.
|
2004-06-21 22:47:51 +00:00
|
|
|
* Returns NULL if error occurred, EOF encounterd or input line is too long.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
2000-10-26 12:45:54 +00:00
|
|
|
static char *getline(str, size, file, linenum)
|
1997-02-09 22:50:16 +00:00
|
|
|
register char *str;
|
|
|
|
size_t size;
|
|
|
|
FILE *file;
|
2000-10-26 12:45:54 +00:00
|
|
|
int *linenum;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
1998-06-20 18:29:38 +00:00
|
|
|
char *p;
|
|
|
|
int s, len;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
do {
|
2000-07-19 13:52:47 +00:00
|
|
|
for (p = str, s = size;; p += (len - 1), s -= (len - 1)) {
|
1998-06-20 18:29:38 +00:00
|
|
|
/*
|
2004-06-21 22:47:51 +00:00
|
|
|
* if an error occurred, EOF was encounterd, or there
|
1998-06-20 18:29:38 +00:00
|
|
|
* was no room to put NUL, return NULL.
|
|
|
|
*/
|
|
|
|
if (fgets(p, s, file) == NULL)
|
|
|
|
return (NULL);
|
1997-11-16 04:52:19 +00:00
|
|
|
len = strlen(p);
|
2000-07-19 13:52:47 +00:00
|
|
|
if (p[len - 1] != '\n') {
|
|
|
|
p[len] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2000-10-26 12:45:54 +00:00
|
|
|
(*linenum)++;
|
1997-11-16 04:52:19 +00:00
|
|
|
p[len - 1] = '\0';
|
2000-07-19 13:52:47 +00:00
|
|
|
if (len < 2 || p[len - 2] != '\\')
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
2000-07-19 13:52:47 +00:00
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Convert '\\' to a space so words don't
|
|
|
|
* run together
|
|
|
|
*/
|
|
|
|
p[len - 2] = ' ';
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
2000-07-19 13:52:47 +00:00
|
|
|
} while (*str == '\0');
|
1998-06-20 18:29:38 +00:00
|
|
|
return (str);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void packetlogon(opt)
|
|
|
|
char *opt;
|
|
|
|
{
|
2002-08-28 13:42:48 +00:00
|
|
|
int flag;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (get_flags(&flag))
|
|
|
|
exit(1);
|
|
|
|
|
2000-05-24 02:14:22 +00:00
|
|
|
if (flag != 0) {
|
1999-11-08 20:51:23 +00:00
|
|
|
if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE)
|
|
|
|
printf("log flag is currently %#x\n", flag);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flag &= ~(FF_LOGPASS|FF_LOGNOMATCH|FF_LOGBLOCK);
|
|
|
|
|
|
|
|
if (index(opt, 'p')) {
|
|
|
|
flag |= FF_LOGPASS;
|
|
|
|
if (opts & OPT_VERBOSE)
|
|
|
|
printf("set log flag: pass\n");
|
|
|
|
}
|
|
|
|
if (index(opt, 'm') && (*opt == 'n' || *opt == 'N')) {
|
|
|
|
flag |= FF_LOGNOMATCH;
|
|
|
|
if (opts & OPT_VERBOSE)
|
|
|
|
printf("set log flag: nomatch\n");
|
|
|
|
}
|
|
|
|
if (index(opt, 'b') || index(opt, 'd')) {
|
|
|
|
flag |= FF_LOGBLOCK;
|
|
|
|
if (opts & OPT_VERBOSE)
|
|
|
|
printf("set log flag: block\n");
|
|
|
|
}
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
|
|
|
if (ioctl(fd, SIOCSETFF, &flag) != 0) {
|
|
|
|
perror("ioctl(SIOCSETFF)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
|
2004-06-21 22:47:51 +00:00
|
|
|
/*
|
|
|
|
* Even though the ioctls above succeeded, it
|
|
|
|
* is possible that a calling script/program
|
|
|
|
* relies on the following verbose mode string.
|
|
|
|
* Thus, we still take an error exit if get_flags
|
|
|
|
* fails here.
|
|
|
|
*/
|
|
|
|
if (get_flags(&flag))
|
|
|
|
exit(1);
|
1997-02-09 22:50:16 +00:00
|
|
|
printf("log flag is now %#x\n", flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void flushfilter(arg)
|
|
|
|
char *arg;
|
|
|
|
{
|
|
|
|
int fl = 0, rem;
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (!arg || !*arg) {
|
|
|
|
fprintf(stderr, "-F: no filter specified\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (!strcmp(arg, "s") || !strcmp(arg, "S")) {
|
|
|
|
if (*arg == 'S')
|
|
|
|
fl = 0;
|
|
|
|
else
|
|
|
|
fl = 1;
|
|
|
|
rem = fl;
|
|
|
|
|
|
|
|
closedevice();
|
2004-06-21 22:47:51 +00:00
|
|
|
|
|
|
|
if (opendevice(IPL_STATE) == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
2003-02-15 06:27:40 +00:00
|
|
|
if (use_inet6) {
|
2004-06-21 22:47:51 +00:00
|
|
|
if (ioctl(fd, SIOCIPFL6, &fl) == -1) {
|
2003-02-15 06:27:40 +00:00
|
|
|
perror("ioctl(SIOCIPFL6)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-02-15 06:27:40 +00:00
|
|
|
} else {
|
2004-06-21 22:47:51 +00:00
|
|
|
if (ioctl(fd, SIOCIPFFL, &fl) == -1) {
|
2003-02-15 06:27:40 +00:00
|
|
|
perror("ioctl(SIOCIPFFL)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-02-15 06:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
1997-11-16 04:52:19 +00:00
|
|
|
if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
|
|
|
|
printf("remove flags %s (%d)\n", arg, rem);
|
|
|
|
printf("removed %d filter rules\n", fl);
|
|
|
|
}
|
|
|
|
closedevice();
|
|
|
|
return;
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
if (strchr(arg, 'i') || strchr(arg, 'I'))
|
|
|
|
fl = FR_INQUE;
|
|
|
|
if (strchr(arg, 'o') || strchr(arg, 'O'))
|
|
|
|
fl = FR_OUTQUE;
|
|
|
|
if (strchr(arg, 'a') || strchr(arg, 'A'))
|
|
|
|
fl = FR_OUTQUE|FR_INQUE;
|
|
|
|
fl |= (opts & FR_INACTIVE);
|
|
|
|
rem = fl;
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
2003-02-15 06:27:40 +00:00
|
|
|
if (use_inet6) {
|
2004-06-21 22:47:51 +00:00
|
|
|
if (ioctl(fd, SIOCIPFL6, &fl) == -1) {
|
2003-02-15 06:27:40 +00:00
|
|
|
perror("ioctl(SIOCIPFL6)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-02-15 06:27:40 +00:00
|
|
|
} else {
|
2004-06-21 22:47:51 +00:00
|
|
|
if (ioctl(fd, SIOCIPFFL, &fl) == -1) {
|
2003-02-15 06:27:40 +00:00
|
|
|
perror("ioctl(SIOCIPFFL)");
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-02-15 06:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
|
|
|
|
printf("remove flags %s%s (%d)\n", (rem & FR_INQUE) ? "I" : "",
|
|
|
|
(rem & FR_OUTQUE) ? "O" : "", rem);
|
|
|
|
printf("removed %d filter rules\n", fl);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void swapactive()
|
|
|
|
{
|
|
|
|
int in = 2;
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
|
|
|
if (ioctl(fd, SIOCSWAPA, &in) == -1) {
|
|
|
|
perror("ioctl(SIOCSWAPA)");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Set %d now inactive\n", in);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-08 20:51:23 +00:00
|
|
|
void frsync()
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
1998-06-20 18:29:38 +00:00
|
|
|
int frsyn = 0;
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
|
|
|
if (ioctl(fd, SIOCFRSYN, &frsyn) == -1) {
|
|
|
|
perror("SIOCFRSYN");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("filter sync'd\n");
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void zerostats()
|
|
|
|
{
|
|
|
|
friostat_t fio;
|
2000-05-24 02:14:22 +00:00
|
|
|
friostat_t *fiop = &fio;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
2000-05-24 02:14:22 +00:00
|
|
|
if (ioctl(fd, SIOCFRZST, &fiop) == -1) {
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("ioctl(SIOCFRZST)");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2000-05-24 02:14:22 +00:00
|
|
|
showstats(fiop);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-06-21 22:47:51 +00:00
|
|
|
* Read the kernel stats for packets blocked and passed
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
|
|
|
static void showstats(fp)
|
|
|
|
friostat_t *fp;
|
|
|
|
{
|
|
|
|
#if SOLARIS
|
|
|
|
printf("dropped packets:\tin %lu\tout %lu\n",
|
|
|
|
fp->f_st[0].fr_drop, fp->f_st[1].fr_drop);
|
|
|
|
printf("non-ip packets:\t\tin %lu\tout %lu\n",
|
|
|
|
fp->f_st[0].fr_notip, fp->f_st[1].fr_notip);
|
|
|
|
printf(" bad packets:\t\tin %lu\tout %lu\n",
|
|
|
|
fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
|
|
|
|
#endif
|
|
|
|
printf(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
|
|
|
|
fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
|
|
|
|
fp->f_st[0].fr_nom);
|
|
|
|
printf(" counted %lu\n", fp->f_st[0].fr_acct);
|
|
|
|
printf("output packets:\t\tblocked %lu passed %lu nomatch %lu",
|
|
|
|
fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
|
|
|
|
fp->f_st[1].fr_nom);
|
|
|
|
printf(" counted %lu\n", fp->f_st[0].fr_acct);
|
|
|
|
printf(" input packets logged:\tblocked %lu passed %lu\n",
|
|
|
|
fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
|
|
|
|
printf("output packets logged:\tblocked %lu passed %lu\n",
|
|
|
|
fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
|
|
|
|
printf(" packets logged:\tinput %lu-%lu output %lu-%lu\n",
|
|
|
|
fp->f_st[0].fr_pkl, fp->f_st[0].fr_skip,
|
|
|
|
fp->f_st[1].fr_pkl, fp->f_st[1].fr_skip);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if SOLARIS
|
1997-04-03 10:22:02 +00:00
|
|
|
static void blockunknown()
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2004-06-21 22:47:51 +00:00
|
|
|
int flag;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (opendevice(ipfname) == -1)
|
2004-06-21 22:47:51 +00:00
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (get_flags(&flag))
|
|
|
|
exit(1);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1999-11-08 20:51:23 +00:00
|
|
|
if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE)
|
1997-02-09 22:50:16 +00:00
|
|
|
printf("log flag is currently %#x\n", flag);
|
|
|
|
|
|
|
|
flag ^= FF_BLOCKNONIP;
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
if (opendevice(ipfname) == -1)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
if (!(opts & OPT_DONOTHING)) {
|
|
|
|
if (ioctl(fd, SIOCSETFF, &flag))
|
|
|
|
perror("ioctl(SIOCSETFF)");
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
|
|
|
|
if (ioctl(fd, SIOCGETFF, &flag))
|
|
|
|
perror("ioctl(SIOCGETFF)");
|
|
|
|
|
|
|
|
printf("log flag is now %#x\n", flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-11-08 20:51:23 +00:00
|
|
|
|
|
|
|
|
2004-06-21 22:47:51 +00:00
|
|
|
/*
|
|
|
|
* nonzero return value means caller should exit with error
|
|
|
|
*/
|
2001-07-28 11:59:33 +00:00
|
|
|
static int showversion()
|
1999-11-08 20:51:23 +00:00
|
|
|
{
|
|
|
|
struct friostat fio;
|
2000-05-24 02:14:22 +00:00
|
|
|
struct friostat *fiop=&fio;
|
2004-06-21 22:47:51 +00:00
|
|
|
int flags, vfd;
|
1999-11-08 20:51:23 +00:00
|
|
|
char *s;
|
|
|
|
|
2000-05-24 02:14:22 +00:00
|
|
|
printf("ipf: %s (%d)\n", IPL_VERSION, (int)sizeof(frentry_t));
|
1999-11-08 20:51:23 +00:00
|
|
|
|
2000-08-13 04:58:02 +00:00
|
|
|
if ((vfd = open(ipfname, O_RDONLY)) == -1) {
|
|
|
|
perror("open device");
|
2001-07-28 11:59:33 +00:00
|
|
|
return 1;
|
2000-08-13 04:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(vfd, SIOCGETFS, &fiop)) {
|
2000-10-26 12:45:54 +00:00
|
|
|
perror("ioctl(SIOCGETFS)");
|
2000-08-13 04:58:02 +00:00
|
|
|
close(vfd);
|
2001-07-28 11:59:33 +00:00
|
|
|
return 1;
|
1999-11-08 20:51:23 +00:00
|
|
|
}
|
2000-08-13 04:58:02 +00:00
|
|
|
close(vfd);
|
1999-11-08 20:51:23 +00:00
|
|
|
|
|
|
|
printf("Kernel: %-*.*s\n", (int)sizeof(fio.f_version),
|
|
|
|
(int)sizeof(fio.f_version), fio.f_version);
|
|
|
|
printf("Running: %s\n", fio.f_running ? "yes" : "no");
|
2004-06-21 22:47:51 +00:00
|
|
|
|
|
|
|
if (get_flags(&flags)) {
|
|
|
|
return 1;
|
|
|
|
}
|
1999-11-08 20:51:23 +00:00
|
|
|
printf("Log Flags: %#x = ", flags);
|
|
|
|
s = "";
|
|
|
|
if (flags & FF_LOGPASS) {
|
|
|
|
printf("pass");
|
|
|
|
s = ", ";
|
|
|
|
}
|
|
|
|
if (flags & FF_LOGBLOCK) {
|
|
|
|
printf("%sblock", s);
|
|
|
|
s = ", ";
|
|
|
|
}
|
|
|
|
if (flags & FF_LOGNOMATCH) {
|
|
|
|
printf("%snomatch", s);
|
|
|
|
s = ", ";
|
|
|
|
}
|
|
|
|
if (flags & FF_BLOCKNONIP) {
|
|
|
|
printf("%snonip", s);
|
|
|
|
s = ", ";
|
|
|
|
}
|
|
|
|
if (!*s)
|
|
|
|
printf("none set");
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
printf("Default: ");
|
|
|
|
if (fio.f_defpass & FR_PASS)
|
|
|
|
s = "pass";
|
|
|
|
else if (fio.f_defpass & FR_BLOCK)
|
|
|
|
s = "block";
|
|
|
|
else
|
|
|
|
s = "nomatch -> block";
|
|
|
|
printf("%s all, Logging: %savailable\n", s, fio.f_logging ? "" : "un");
|
|
|
|
printf("Active list: %d\n", fio.f_active);
|
2001-07-28 11:59:33 +00:00
|
|
|
|
|
|
|
return 0;
|
1999-11-08 20:51:23 +00:00
|
|
|
}
|