1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2002-03-19 11:45:20 +00:00
|
|
|
* Copyright (C) 1993-2002 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
|
2002-03-19 11:45:20 +00:00
|
|
|
#ifdef __sgi
|
|
|
|
# include <sys/ptimers.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#if !defined(__SVR4) && !defined(__svr4__) && !defined(__sgi)
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <strings.h>
|
|
|
|
#else
|
1997-11-16 04:52:19 +00:00
|
|
|
#if !defined(__sgi)
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <sys/byteorder.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
#endif
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <sys/file.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/param.h>
|
1997-05-25 15:45:04 +00:00
|
|
|
#include <sys/time.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
#ifndef linux
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/ip_var.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#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 <netdb.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <resolv.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include "ip_compat.h"
|
1997-11-16 04:52:19 +00:00
|
|
|
#include <netinet/tcpip.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "ip_fil.h"
|
1999-11-08 20:51:23 +00:00
|
|
|
#include "ip_nat.h"
|
|
|
|
#include "ip_state.h"
|
2002-03-19 11:45:20 +00:00
|
|
|
#include "ip_frag.h"
|
1997-02-09 22:50:16 +00:00
|
|
|
#include "ipf.h"
|
|
|
|
#include "ipt.h"
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if !defined(lint)
|
2000-05-24 02:14:22 +00:00
|
|
|
static const char sccsid[] = "@(#)ipt.c 1.19 6/3/96 (C) 1993-2000 Darren Reed";
|
2002-03-19 11:45:20 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id: ipt.c,v 2.6.2.19 2002/03/11 03:30:51 darrenr Exp $";
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern char *optarg;
|
1997-04-03 10:22:02 +00:00
|
|
|
extern struct frentry *ipfilter[2][2];
|
|
|
|
extern struct ipread snoop, etherf, tcpd, pcap, iptext, iphex;
|
2000-05-24 02:14:22 +00:00
|
|
|
extern struct ifnet *get_unit __P((char *, int));
|
1997-04-03 10:22:02 +00:00
|
|
|
extern void init_ifp __P((void));
|
1999-11-08 20:51:23 +00:00
|
|
|
extern ipnat_t *natparse __P((char *, int));
|
2000-02-09 20:46:45 +00:00
|
|
|
extern int fr_running;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
int opts = 0;
|
2000-05-24 02:14:22 +00:00
|
|
|
int use_inet6 = 0;
|
1997-04-03 10:22:02 +00:00
|
|
|
int main __P((int, char *[]));
|
2002-03-19 11:45:20 +00:00
|
|
|
int loadrules __P((char *));
|
|
|
|
int kmemcpy __P((char *, long, int));
|
|
|
|
void dumpnat __P((void));
|
|
|
|
void dumpstate __P((void));
|
|
|
|
char *getifname __P((void *));
|
|
|
|
void drain_log __P((char *));
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
int main(argc,argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
char *datain, *iface, *ifname, *packet, *logout;
|
|
|
|
int fd, i, dir, c, loaded, dump, hlen;
|
1997-04-03 10:22:02 +00:00
|
|
|
struct ifnet *ifp;
|
2002-03-19 11:45:20 +00:00
|
|
|
struct ipread *r;
|
|
|
|
u_long buf[2048];
|
1997-11-16 04:52:19 +00:00
|
|
|
ip_t *ip;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
dir = 0;
|
|
|
|
dump = 0;
|
|
|
|
loaded = 0;
|
|
|
|
r = &iptext;
|
|
|
|
iface = NULL;
|
|
|
|
logout = NULL;
|
|
|
|
ifname = "anon0";
|
|
|
|
datain = NULL;
|
|
|
|
|
|
|
|
nat_init();
|
|
|
|
fr_stateinit();
|
|
|
|
initparse();
|
|
|
|
ipflog_init();
|
|
|
|
fr_running = 1;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "6bdDEHi:I:l:NoPr:STvxX")) != -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
2000-05-24 02:14:22 +00:00
|
|
|
#ifdef USE_INET6
|
|
|
|
case '6' :
|
|
|
|
use_inet6 = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'b' :
|
|
|
|
opts |= OPT_BRIEF;
|
|
|
|
break;
|
|
|
|
case 'd' :
|
|
|
|
opts |= OPT_DEBUG;
|
|
|
|
break;
|
2002-03-19 11:45:20 +00:00
|
|
|
case 'D' :
|
|
|
|
dump = 1;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'i' :
|
|
|
|
datain = optarg;
|
|
|
|
break;
|
|
|
|
case 'I' :
|
2002-03-19 11:45:20 +00:00
|
|
|
ifname = optarg;
|
|
|
|
break;
|
|
|
|
case 'l' :
|
|
|
|
logout = optarg;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
1997-04-03 10:22:02 +00:00
|
|
|
case 'o' :
|
|
|
|
opts |= OPT_SAVEOUT;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'r' :
|
2002-03-19 11:45:20 +00:00
|
|
|
if (loadrules(optarg) == -1)
|
|
|
|
return -1;
|
|
|
|
loaded = 1;
|
1997-02-09 22:50:16 +00:00
|
|
|
break;
|
|
|
|
case 'v' :
|
|
|
|
opts |= OPT_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'E' :
|
|
|
|
r = ðerf;
|
|
|
|
break;
|
|
|
|
case 'H' :
|
|
|
|
r = &iphex;
|
|
|
|
break;
|
1999-11-08 20:51:23 +00:00
|
|
|
case 'N' :
|
|
|
|
opts |= OPT_NAT;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'P' :
|
|
|
|
r = &pcap;
|
|
|
|
break;
|
|
|
|
case 'S' :
|
|
|
|
r = &snoop;
|
|
|
|
break;
|
|
|
|
case 'T' :
|
|
|
|
r = &tcpd;
|
|
|
|
break;
|
2002-03-19 11:45:20 +00:00
|
|
|
case 'x' :
|
|
|
|
opts |= OPT_HEX;
|
|
|
|
break;
|
1997-02-09 22:50:16 +00:00
|
|
|
case 'X' :
|
|
|
|
r = &iptext;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
if (loaded == 0) {
|
|
|
|
(void)fprintf(stderr,"no rules loaded\n");
|
1997-02-09 22:50:16 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
if (opts & OPT_SAVEOUT)
|
|
|
|
init_ifp();
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (datain)
|
|
|
|
fd = (*r->r_open)(datain);
|
|
|
|
else
|
|
|
|
fd = (*r->r_open)("-");
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
exit(-1);
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
ip = (ip_t *)buf;
|
1997-04-03 10:22:02 +00:00
|
|
|
while ((i = (*r->r_readip)((char *)buf, sizeof(buf),
|
|
|
|
&iface, &dir)) > 0) {
|
2002-03-19 11:45:20 +00:00
|
|
|
if (iface == NULL || *iface == '\0')
|
|
|
|
iface = ifname;
|
|
|
|
ifp = get_unit(iface, ip->ip_v);
|
|
|
|
hlen = 0;
|
|
|
|
if (!use_inet6) {
|
|
|
|
ip->ip_off = ntohs(ip->ip_off);
|
|
|
|
ip->ip_len = ntohs(ip->ip_len);
|
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
}
|
|
|
|
#ifdef USE_INET6
|
|
|
|
else
|
|
|
|
hlen = sizeof(ip6_t);
|
|
|
|
#endif
|
|
|
|
packet = (char *)buf;
|
|
|
|
/* ipfr_slowtimer(); */
|
|
|
|
i = fr_check(ip, hlen, ifp, dir, (mb_t **)&packet);
|
1999-11-08 20:51:23 +00:00
|
|
|
if ((opts & OPT_NAT) == 0)
|
|
|
|
switch (i)
|
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
case -5 :
|
|
|
|
(void)printf("block return-icmp-as-dest");
|
|
|
|
break;
|
|
|
|
case -4 :
|
|
|
|
(void)printf("block return-icmp");
|
|
|
|
break;
|
|
|
|
case -3 :
|
|
|
|
(void)printf("block return-rst");
|
|
|
|
break;
|
1999-11-08 20:51:23 +00:00
|
|
|
case -2 :
|
|
|
|
(void)printf("auth");
|
|
|
|
break;
|
|
|
|
case -1 :
|
|
|
|
(void)printf("block");
|
|
|
|
break;
|
|
|
|
case 0 :
|
|
|
|
(void)printf("pass");
|
|
|
|
break;
|
|
|
|
case 1 :
|
|
|
|
(void)printf("nomatch");
|
|
|
|
break;
|
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
if (!use_inet6) {
|
|
|
|
ip->ip_off = htons(ip->ip_off);
|
|
|
|
ip->ip_len = htons(ip->ip_len);
|
|
|
|
}
|
1999-11-08 20:51:23 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (!(opts & OPT_BRIEF)) {
|
|
|
|
putchar(' ');
|
1997-11-16 04:52:19 +00:00
|
|
|
printpacket((ip_t *)buf);
|
1997-02-09 22:50:16 +00:00
|
|
|
printf("--------------");
|
1999-11-08 20:51:23 +00:00
|
|
|
} else if ((opts & (OPT_BRIEF|OPT_NAT)) == (OPT_NAT|OPT_BRIEF))
|
|
|
|
printpacket((ip_t *)buf);
|
1997-11-16 04:52:19 +00:00
|
|
|
#ifndef linux
|
2002-03-19 11:45:20 +00:00
|
|
|
if (dir && (ifp != NULL) && ip->ip_v && (packet != NULL))
|
|
|
|
# if defined(__sgi) && (IRIX < 605)
|
|
|
|
(*ifp->if_output)(ifp, (void *)packet, NULL);
|
1997-11-16 04:52:19 +00:00
|
|
|
# else
|
2002-03-19 11:45:20 +00:00
|
|
|
(*ifp->if_output)(ifp, (void *)packet, NULL, 0);
|
1997-11-16 04:52:19 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
1999-11-08 20:51:23 +00:00
|
|
|
if ((opts & (OPT_BRIEF|OPT_NAT)) != (OPT_NAT|OPT_BRIEF))
|
|
|
|
putchar('\n');
|
1997-02-09 22:50:16 +00:00
|
|
|
dir = 0;
|
2002-03-19 11:45:20 +00:00
|
|
|
if (iface != ifname) {
|
|
|
|
free(iface);
|
|
|
|
iface = ifname;
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
(*r->r_close)();
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
if (logout != NULL) {
|
|
|
|
drain_log(logout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dump == 1) {
|
|
|
|
dumpnat();
|
|
|
|
dumpstate();
|
|
|
|
}
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load in either NAT or ipf rules from a file, which is treated as stdin
|
|
|
|
* if the name is "-". NOTE, stdin can only be used once as the file is
|
|
|
|
* closed after use.
|
|
|
|
*/
|
|
|
|
int loadrules(file)
|
|
|
|
char *file;
|
|
|
|
{
|
|
|
|
char line[513], *s;
|
|
|
|
int linenum, i;
|
|
|
|
void *fr;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (!strcmp(file, "-"))
|
|
|
|
fp = stdin;
|
|
|
|
else if (!(fp = fopen(file, "r"))) {
|
|
|
|
(void)fprintf(stderr, "couldn't open %s\n", file);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(opts & OPT_BRIEF))
|
|
|
|
(void)printf("opening rule file \"%s\"\n", file);
|
|
|
|
|
|
|
|
linenum = 0;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line) - 1, fp)) {
|
|
|
|
linenum++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* treat both CR and LF as EOL
|
|
|
|
*/
|
|
|
|
if ((s = index(line, '\n')))
|
|
|
|
*s = '\0';
|
|
|
|
if ((s = index(line, '\r')))
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* # is comment marker, everything after is a ignored
|
|
|
|
*/
|
|
|
|
if ((s = index(line, '#')))
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
if (!*line)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* fake an `ioctl' call :) */
|
|
|
|
|
|
|
|
if ((opts & OPT_NAT) != 0) {
|
|
|
|
if (!(fr = natparse(line, linenum)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
i = IPL_EXTERN(ioctl)(IPL_LOGNAT, SIOCADNAT,
|
|
|
|
(caddr_t)&fr, FWRITE|FREAD);
|
|
|
|
if (opts & OPT_DEBUG)
|
|
|
|
fprintf(stderr, "iplioctl(ADNAT,%p,1) = %d\n",
|
|
|
|
fr, i);
|
|
|
|
} else {
|
|
|
|
if (!(fr = parse(line, linenum)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
i = IPL_EXTERN(ioctl)(0, SIOCADAFR, (caddr_t)&fr,
|
|
|
|
FWRITE|FREAD);
|
|
|
|
if (opts & OPT_DEBUG)
|
|
|
|
fprintf(stderr, "iplioctl(ADAFR,%p,1) = %d\n",
|
|
|
|
fr, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int kmemcpy(addr, offset, size)
|
|
|
|
char *addr;
|
|
|
|
long offset;
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
bcopy((char *)offset, addr, size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display the built up NAT table rules and mapping entries.
|
|
|
|
*/
|
|
|
|
void dumpnat()
|
|
|
|
{
|
|
|
|
ipnat_t *ipn;
|
|
|
|
nat_t *nat;
|
|
|
|
|
|
|
|
printf("List of active MAP/Redirect filters:\n");
|
|
|
|
for (ipn = nat_list; ipn != NULL; ipn = ipn->in_next)
|
|
|
|
printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
|
|
|
|
printf("\nList of active sessions:\n");
|
|
|
|
for (nat = nat_instances; nat; nat = nat->nat_next)
|
|
|
|
printactivenat(nat, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display the built up state table rules and mapping entries.
|
|
|
|
*/
|
|
|
|
void dumpstate()
|
|
|
|
{
|
|
|
|
ipstate_t *ips;
|
|
|
|
|
|
|
|
printf("List of active state sessions:\n");
|
|
|
|
for (ips = ips_list; ips != NULL; )
|
|
|
|
ips = printstate(ips, opts & (OPT_DEBUG|OPT_VERBOSE));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pointer to an interface in the kernel, return a pointer to a
|
|
|
|
* string which is the interface name.
|
|
|
|
*/
|
|
|
|
char *getifname(ptr)
|
|
|
|
void *ptr;
|
|
|
|
{
|
|
|
|
#if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
|
|
|
|
defined(__OpenBSD__)
|
|
|
|
#else
|
|
|
|
char buf[32], *s;
|
|
|
|
int len;
|
|
|
|
#endif
|
|
|
|
struct ifnet netif;
|
|
|
|
|
|
|
|
if (ptr == (void *)-1)
|
|
|
|
return "!";
|
|
|
|
if (ptr == NULL)
|
|
|
|
return "-";
|
|
|
|
|
|
|
|
if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
|
|
|
|
return "X";
|
|
|
|
#if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
|
|
|
|
defined(__OpenBSD__)
|
|
|
|
return strdup(netif.if_xname);
|
|
|
|
#else
|
|
|
|
if (kmemcpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
|
|
|
|
return "X";
|
|
|
|
if (netif.if_unit < 10)
|
|
|
|
len = 2;
|
|
|
|
else if (netif.if_unit < 1000)
|
|
|
|
len = 3;
|
|
|
|
else if (netif.if_unit < 10000)
|
|
|
|
len = 4;
|
|
|
|
else
|
|
|
|
len = 5;
|
|
|
|
buf[sizeof(buf) - len] = '\0';
|
|
|
|
for (s = buf; *s && !isdigit(*s); s++)
|
|
|
|
;
|
|
|
|
if (isdigit(*s))
|
|
|
|
*s = '\0';
|
|
|
|
sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
|
|
|
|
return strdup(buf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void drain_log(filename)
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
char buffer[IPLLOGSIZE];
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
|
|
|
size_t resid;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
|
|
|
|
if (fd == -1) {
|
|
|
|
perror("drain_log:open");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
bzero((char *)&iov, sizeof(iov));
|
|
|
|
iov.iov_base = buffer;
|
|
|
|
iov.iov_len = sizeof(buffer);
|
|
|
|
|
|
|
|
bzero((char *)&uio, sizeof(uio));
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_resid = iov.iov_len;
|
|
|
|
resid = uio.uio_resid;
|
|
|
|
|
|
|
|
if (ipflog_read(0, &uio) == 0) {
|
|
|
|
/*
|
|
|
|
* If nothing was read then break out.
|
|
|
|
*/
|
|
|
|
if (uio.uio_resid == resid)
|
|
|
|
break;
|
|
|
|
write(fd, buffer, resid - uio.uio_resid);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|