2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 # ifndef __FreeBSD_cc_version
8 # include <osreldate.h>
10 # if __FreeBSD_cc_version < 430000
11 # include <osreldate.h>
15 #if defined(__sgi) && (IRIX > 602)
16 # include <sys/ptimers.h>
23 #if !defined(__SVR4) && !defined(__GNUC__)
26 #include <sys/types.h>
27 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
37 #if __FreeBSD_version >= 300000
38 # include <net/if_var.h>
40 #include <netinet/ip.h>
42 #include <arpa/nameser.h>
44 #include "ip_compat.h"
52 static const char sccsid[] = "@(#)ipf.c 1.23 6/5/96 (C) 1993-2000 Darren Reed";
53 static const char rcsid[] = "@(#)$Id: ipf.c,v 2.10.2.19 2002/12/06 11:41:13 darrenr Exp $";
57 static void blockunknown __P((void));
59 #if !defined(__SVR4) && defined(__GNUC__)
60 extern char *index __P((const char *, int));
65 void frsync __P((void));
66 void zerostats __P((void));
67 int main __P((int, char *[]));
74 static void procfile __P((char *, char *)), flushfilter __P((char *));
75 static void set_state __P((u_int)), showstats __P((friostat_t *));
76 static void packetlogon __P((char *)), swapactive __P((void));
77 static int opendevice __P((char *));
78 static void closedevice __P((void));
79 static char *getline __P((char *, size_t, FILE *, int *));
80 static char *ipfname = IPL_NAME;
81 static void usage __P((void));
82 static int showversion __P((void));
83 static int get_flags __P((void));
87 # define OPTS "6AdDEf:F:Il:noPrsUvVyzZ"
89 # define OPTS "6AdDEf:F:Il:noPrsvVyzZ"
94 fprintf(stderr, "usage: ipf [-%s] %s %s %s\n", OPTS,
95 "[-l block|pass|nomatch]", "[-F i|o|a|s|S]", "[-f filename]");
106 while ((c = getopt(argc, argv, OPTS)) != -1) {
113 opts &= ~OPT_INACTIVE;
125 procfile(argv[0], optarg);
131 opts |= OPT_INACTIVE;
137 opts |= OPT_DONOTHING;
166 opts |= OPT_ZERORULEST;
185 static int opendevice(ipfdev)
188 if (opts & OPT_DONOTHING)
194 if (!(opts & OPT_DONOTHING) && fd == -1)
195 if ((fd = open(ipfdev, O_RDWR)) == -1)
196 if ((fd = open(ipfdev, O_RDONLY)) == -1) {
197 perror("open device");
199 fprintf(stderr, "IPFilter enabled?\n");
205 static void closedevice()
212 static int get_flags()
216 if ((opendevice(ipfname) != -2) && (ioctl(fd, SIOCGETFF, &i) == -1)) {
224 static void set_state(enable)
227 if (opendevice(ipfname) != -2)
228 if (ioctl(fd, SIOCFRENB, &enable) == -1) {
231 "IP Filter: already initialized\n");
238 static void procfile(name, file)
247 (void) opendevice(ipfname);
249 if (opts & OPT_INACTIVE) {
256 if (opts & OPT_DEBUG)
257 printf("add %x del %x\n", add, del);
261 if (!strcmp(file, "-"))
263 else if (!(fp = fopen(file, "r"))) {
264 fprintf(stderr, "%s: fopen(%s) failed: %s\n", name, file,
269 while (getline(line, sizeof(line), fp, &linenum)) {
271 * treat CR as EOL. LF is converted to NUL by getline().
273 if ((s = index(line, '\r')))
276 * # is comment marker, everything after is a ignored
278 if ((s = index(line, '#')))
284 if (opts & OPT_VERBOSE)
285 (void)fprintf(stderr, "[%s]\n", line);
287 fr = parse(line, linenum);
288 (void)fflush(stdout);
291 if (opts & OPT_ZERORULEST)
293 else if (opts & OPT_INACTIVE)
294 add = (u_int)fr->fr_hits ? SIOCINIFR :
297 add = (u_int)fr->fr_hits ? SIOCINAFR :
301 if (fr && (opts & OPT_VERBOSE))
303 if (fr && (opts & OPT_OUTQUE))
304 fr->fr_flags |= FR_OUTQUE;
306 if (opts & OPT_DEBUG)
309 if ((opts & OPT_ZERORULEST) &&
310 !(opts & OPT_DONOTHING)) {
311 if (ioctl(fd, add, &fr) == -1) {
312 fprintf(stderr, "%d:", linenum);
313 perror("ioctl(SIOCZRLST)");
316 printf("hits %qd bytes %qd ",
317 (long long)fr->fr_hits,
318 (long long)fr->fr_bytes);
320 printf("hits %ld bytes %ld ",
321 fr->fr_hits, fr->fr_bytes);
325 } else if ((opts & OPT_REMOVE) &&
326 !(opts & OPT_DONOTHING)) {
327 if (ioctl(fd, del, &fr) == -1) {
328 fprintf(stderr, "%d:", linenum);
329 perror("ioctl(delete rule)");
331 } else if (!(opts & OPT_DONOTHING)) {
332 if (ioctl(fd, add, &fr) == -1) {
333 fprintf(stderr, "%d:", linenum);
334 perror("ioctl(add/insert rule)");
339 if (ferror(fp) || !feof(fp)) {
340 fprintf(stderr, "%s: %s: file error or line too long\n",
348 * Similar to fgets(3) but can handle '\\' and NL is converted to NUL.
349 * Returns NULL if error occured, EOF encounterd or input line is too long.
351 static char *getline(str, size, file, linenum)
361 for (p = str, s = size;; p += (len - 1), s -= (len - 1)) {
363 * if an error occured, EOF was encounterd, or there
364 * was no room to put NUL, return NULL.
366 if (fgets(p, s, file) == NULL)
369 if (p[len - 1] != '\n') {
375 if (len < 2 || p[len - 2] != '\\')
379 * Convert '\\' to a space so words don't
384 } while (*str == '\0');
389 static void packetlogon(opt)
396 if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE)
397 printf("log flag is currently %#x\n", flag);
400 flag &= ~(FF_LOGPASS|FF_LOGNOMATCH|FF_LOGBLOCK);
402 if (index(opt, 'p')) {
404 if (opts & OPT_VERBOSE)
405 printf("set log flag: pass\n");
407 if (index(opt, 'm') && (*opt == 'n' || *opt == 'N')) {
408 flag |= FF_LOGNOMATCH;
409 if (opts & OPT_VERBOSE)
410 printf("set log flag: nomatch\n");
412 if (index(opt, 'b') || index(opt, 'd')) {
414 if (opts & OPT_VERBOSE)
415 printf("set log flag: block\n");
418 if (opendevice(ipfname) != -2 && (ioctl(fd, SIOCSETFF, &flag) != 0))
419 perror("ioctl(SIOCSETFF)");
421 if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
423 printf("log flag is now %#x\n", flag);
428 static void flushfilter(arg)
435 if (!strcmp(arg, "s") || !strcmp(arg, "S")) {
443 if (opendevice(IPL_STATE) != -2) {
445 if (ioctl(fd, SIOCIPFL6, &fl) == -1)
446 perror("ioctl(SIOCIPFL6)");
448 if (ioctl(fd, SIOCIPFFL, &fl) == -1)
449 perror("ioctl(SIOCIPFFL)");
452 if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
453 printf("remove flags %s (%d)\n", arg, rem);
454 printf("removed %d filter rules\n", fl);
459 if (strchr(arg, 'i') || strchr(arg, 'I'))
461 if (strchr(arg, 'o') || strchr(arg, 'O'))
463 if (strchr(arg, 'a') || strchr(arg, 'A'))
464 fl = FR_OUTQUE|FR_INQUE;
465 fl |= (opts & FR_INACTIVE);
468 if (opendevice(ipfname) != -2) {
470 if (ioctl(fd, SIOCIPFL6, &fl) == -1)
471 perror("ioctl(SIOCIPFL6)");
473 if (ioctl(fd, SIOCIPFFL, &fl) == -1)
474 perror("ioctl(SIOCIPFFL)");
477 if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
478 printf("remove flags %s%s (%d)\n", (rem & FR_INQUE) ? "I" : "",
479 (rem & FR_OUTQUE) ? "O" : "", rem);
480 printf("removed %d filter rules\n", fl);
486 static void swapactive()
490 if (opendevice(ipfname) != -2 && ioctl(fd, SIOCSWAPA, &in) == -1)
491 perror("ioctl(SIOCSWAPA)");
493 printf("Set %d now inactive\n", in);
501 if (opendevice(ipfname) != -2 && ioctl(fd, SIOCFRSYN, &frsyn) == -1)
504 printf("filter sync'd\n");
511 friostat_t *fiop = &fio;
513 if (opendevice(ipfname) != -2) {
514 if (ioctl(fd, SIOCFRZST, &fiop) == -1) {
515 perror("ioctl(SIOCFRZST)");
525 * read the kernel stats for packets blocked and passed
527 static void showstats(fp)
531 printf("dropped packets:\tin %lu\tout %lu\n",
532 fp->f_st[0].fr_drop, fp->f_st[1].fr_drop);
533 printf("non-ip packets:\t\tin %lu\tout %lu\n",
534 fp->f_st[0].fr_notip, fp->f_st[1].fr_notip);
535 printf(" bad packets:\t\tin %lu\tout %lu\n",
536 fp->f_st[0].fr_bad, fp->f_st[1].fr_bad);
538 printf(" input packets:\t\tblocked %lu passed %lu nomatch %lu",
539 fp->f_st[0].fr_block, fp->f_st[0].fr_pass,
541 printf(" counted %lu\n", fp->f_st[0].fr_acct);
542 printf("output packets:\t\tblocked %lu passed %lu nomatch %lu",
543 fp->f_st[1].fr_block, fp->f_st[1].fr_pass,
545 printf(" counted %lu\n", fp->f_st[0].fr_acct);
546 printf(" input packets logged:\tblocked %lu passed %lu\n",
547 fp->f_st[0].fr_bpkl, fp->f_st[0].fr_ppkl);
548 printf("output packets logged:\tblocked %lu passed %lu\n",
549 fp->f_st[1].fr_bpkl, fp->f_st[1].fr_ppkl);
550 printf(" packets logged:\tinput %lu-%lu output %lu-%lu\n",
551 fp->f_st[0].fr_pkl, fp->f_st[0].fr_skip,
552 fp->f_st[1].fr_pkl, fp->f_st[1].fr_skip);
557 static void blockunknown()
561 if (opendevice(ipfname) == -1)
565 if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE)
566 printf("log flag is currently %#x\n", flag);
568 flag ^= FF_BLOCKNONIP;
570 if (opendevice(ipfname) != -2 && ioctl(fd, SIOCSETFF, &flag))
571 perror("ioctl(SIOCSETFF)");
573 if ((opts & (OPT_DONOTHING|OPT_VERBOSE)) == OPT_VERBOSE) {
574 if (ioctl(fd, SIOCGETFF, &flag))
575 perror("ioctl(SIOCGETFF)");
577 printf("log flag is now %#x\n", flag);
583 static int showversion()
586 struct friostat *fiop=&fio;
591 printf("ipf: %s (%d)\n", IPL_VERSION, (int)sizeof(frentry_t));
593 if ((vfd = open(ipfname, O_RDONLY)) == -1) {
594 perror("open device");
598 if (ioctl(vfd, SIOCGETFS, &fiop)) {
599 perror("ioctl(SIOCGETFS)");
606 printf("Kernel: %-*.*s\n", (int)sizeof(fio.f_version),
607 (int)sizeof(fio.f_version), fio.f_version);
608 printf("Running: %s\n", fio.f_running ? "yes" : "no");
609 printf("Log Flags: %#x = ", flags);
611 if (flags & FF_LOGPASS) {
615 if (flags & FF_LOGBLOCK) {
616 printf("%sblock", s);
619 if (flags & FF_LOGNOMATCH) {
620 printf("%snomatch", s);
623 if (flags & FF_BLOCKNONIP) {
624 printf("%snonip", s);
632 if (fio.f_defpass & FR_PASS)
634 else if (fio.f_defpass & FR_BLOCK)
637 s = "nomatch -> block";
638 printf("%s all, Logging: %savailable\n", s, fio.f_logging ? "" : "un");
639 printf("Active list: %d\n", fio.f_active);