1994-10-28 15:06:53 +00:00
|
|
|
/*
|
1996-08-13 19:43:24 +00:00
|
|
|
* Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
|
1994-10-28 15:06:53 +00:00
|
|
|
* Copyright (c) 1994 Ugen J.S.Antsilevich
|
1996-08-13 19:43:24 +00:00
|
|
|
*
|
1994-11-16 10:18:18 +00:00
|
|
|
* Idea and grammar partially left from:
|
|
|
|
* Copyright (c) 1993 Daniel Boulet
|
1994-10-28 15:06:53 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source forms, with and without modification,
|
|
|
|
* are permitted provided that this entire comment appears intact.
|
|
|
|
*
|
|
|
|
* Redistribution in binary form may occur without any restrictions.
|
|
|
|
* Obviously, it would be nice if you gave credit where credit is due
|
|
|
|
* but requiring it would be too onerous.
|
|
|
|
*
|
|
|
|
* This software is provided ``AS IS'' without any warranties of any kind.
|
|
|
|
*
|
1994-11-16 10:18:18 +00:00
|
|
|
* NEW command line interface for IP firewall facility
|
1995-08-22 00:38:02 +00:00
|
|
|
*
|
1998-12-07 05:54:37 +00:00
|
|
|
* $Id: ipfw.c,v 1.61 1998/11/23 10:54:28 joerg Exp $
|
1995-08-22 00:38:02 +00:00
|
|
|
*
|
1994-10-28 15:06:53 +00:00
|
|
|
*/
|
|
|
|
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <sys/time.h>
|
1998-11-23 10:54:28 +00:00
|
|
|
#include <sys/wait.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1998-11-23 10:54:28 +00:00
|
|
|
#include <errno.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <limits.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <netdb.h>
|
1998-11-23 10:54:28 +00:00
|
|
|
#include <signal.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-06-02 05:02:37 +00:00
|
|
|
#include <stdarg.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <string.h>
|
1998-11-23 10:54:28 +00:00
|
|
|
#include <sysexits.h>
|
1996-06-09 23:46:22 +00:00
|
|
|
#include <time.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1996-10-17 01:05:03 +00:00
|
|
|
#include <net/if.h>
|
1994-10-28 15:06:53 +00:00
|
|
|
#include <netinet/in.h>
|
1997-06-02 05:02:37 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <netinet/ip_fw.h>
|
1994-10-28 15:06:53 +00:00
|
|
|
#include <netinet/tcp.h>
|
1995-02-24 14:32:45 +00:00
|
|
|
#include <arpa/inet.h>
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
int lineno = -1;
|
1996-02-24 00:20:56 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
int s; /* main RAW socket */
|
1996-02-24 00:20:56 +00:00
|
|
|
int do_resolv=0; /* Would try to resolv all */
|
1994-12-12 17:19:33 +00:00
|
|
|
int do_acct=0; /* Show packet/byte count */
|
1996-06-09 23:46:22 +00:00
|
|
|
int do_time=0; /* Show time stamps */
|
1997-02-10 15:36:54 +00:00
|
|
|
int do_quiet=0; /* Be quiet in add and flush */
|
1996-08-31 17:58:23 +00:00
|
|
|
int do_force=0; /* Don't ask for confirmation */
|
1996-02-24 00:20:56 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
struct icmpcode {
|
|
|
|
int code;
|
|
|
|
char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct icmpcode icmpcodes[] = {
|
|
|
|
{ ICMP_UNREACH_NET, "net" },
|
|
|
|
{ ICMP_UNREACH_HOST, "host" },
|
|
|
|
{ ICMP_UNREACH_PROTOCOL, "protocol" },
|
|
|
|
{ ICMP_UNREACH_PORT, "port" },
|
|
|
|
{ ICMP_UNREACH_NEEDFRAG, "needfrag" },
|
|
|
|
{ ICMP_UNREACH_SRCFAIL, "srcfail" },
|
|
|
|
{ ICMP_UNREACH_NET_UNKNOWN, "net-unknown" },
|
|
|
|
{ ICMP_UNREACH_HOST_UNKNOWN, "host-unknown" },
|
|
|
|
{ ICMP_UNREACH_ISOLATED, "isolated" },
|
|
|
|
{ ICMP_UNREACH_NET_PROHIB, "net-prohib" },
|
|
|
|
{ ICMP_UNREACH_HOST_PROHIB, "host-prohib" },
|
|
|
|
{ ICMP_UNREACH_TOSNET, "tosnet" },
|
|
|
|
{ ICMP_UNREACH_TOSHOST, "toshost" },
|
|
|
|
{ ICMP_UNREACH_FILTER_PROHIB, "filter-prohib" },
|
|
|
|
{ ICMP_UNREACH_HOST_PRECEDENCE, "host-precedence" },
|
|
|
|
{ ICMP_UNREACH_PRECEDENCE_CUTOFF, "precedence-cutoff" },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
1998-01-07 02:23:04 +00:00
|
|
|
static void show_usage(const char *fmt, ...);
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static int
|
|
|
|
mask_bits(struct in_addr m_ad)
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
int h_fnd=0,h_num=0,i;
|
|
|
|
u_long mask;
|
1994-11-16 10:18:18 +00:00
|
|
|
|
|
|
|
mask=ntohl(m_ad.s_addr);
|
|
|
|
for (i=0;i<sizeof(u_long)*CHAR_BIT;i++) {
|
|
|
|
if (mask & 1L) {
|
|
|
|
h_fnd=1;
|
|
|
|
h_num++;
|
|
|
|
} else {
|
|
|
|
if (h_fnd)
|
|
|
|
return -1;
|
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
mask=mask>>1;
|
1995-05-30 06:12:45 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
return h_num;
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
1996-08-13 19:43:24 +00:00
|
|
|
print_port(prot, port, comma)
|
|
|
|
u_char prot;
|
|
|
|
u_short port;
|
1996-06-09 23:46:22 +00:00
|
|
|
const char *comma;
|
|
|
|
{
|
1996-08-13 19:43:24 +00:00
|
|
|
struct servent *se;
|
|
|
|
struct protoent *pe;
|
|
|
|
const char *protocol;
|
1996-06-09 23:46:22 +00:00
|
|
|
int printed = 0;
|
|
|
|
|
|
|
|
if (do_resolv) {
|
1996-08-13 19:43:24 +00:00
|
|
|
pe = getprotobynumber(prot);
|
|
|
|
if (pe)
|
|
|
|
protocol = pe->p_name;
|
|
|
|
else
|
|
|
|
protocol = NULL;
|
1996-08-05 02:38:51 +00:00
|
|
|
|
1996-08-13 19:43:24 +00:00
|
|
|
se = getservbyport(htons(port), protocol);
|
|
|
|
if (se) {
|
|
|
|
printf("%s%s", comma, se->s_name);
|
|
|
|
printed = 1;
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!printed)
|
|
|
|
printf("%s%d",comma,port);
|
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
|
|
|
print_iface(char *key, union ip_fw_if *un, int byname)
|
|
|
|
{
|
|
|
|
char ifnb[FW_IFNLEN+1];
|
|
|
|
|
|
|
|
if (byname) {
|
|
|
|
strncpy(ifnb, un->fu_via_if.name, FW_IFNLEN);
|
|
|
|
ifnb[FW_IFNLEN]='\0';
|
|
|
|
if (un->fu_via_if.unit == -1)
|
|
|
|
printf(" %s %s*", key, ifnb);
|
|
|
|
else
|
|
|
|
printf(" %s %s%d", key, ifnb, un->fu_via_if.unit);
|
|
|
|
} else if (un->fu_via_ip.s_addr != 0) {
|
|
|
|
printf(" %s %s", key, inet_ntoa(un->fu_via_ip));
|
|
|
|
} else
|
|
|
|
printf(" %s any", key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_reject_code(int code)
|
|
|
|
{
|
|
|
|
struct icmpcode *ic;
|
|
|
|
|
|
|
|
for (ic = icmpcodes; ic->str; ic++)
|
|
|
|
if (ic->code == code) {
|
|
|
|
printf("%s", ic->str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("%u", code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-01-08 03:03:54 +00:00
|
|
|
show_ipfw(struct ip_fw *chain, int pcwidth, int bcwidth)
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
char *comma;
|
|
|
|
u_long adrt;
|
|
|
|
struct hostent *he;
|
1996-08-13 19:43:24 +00:00
|
|
|
struct protoent *pe;
|
|
|
|
int i, mb;
|
1997-06-02 05:02:37 +00:00
|
|
|
int nsp = IP_FW_GETNSRCP(chain);
|
|
|
|
int ndp = IP_FW_GETNDSTP(chain);
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
if (do_resolv)
|
|
|
|
setservent(1/*stayopen*/);
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1996-02-23 15:52:28 +00:00
|
|
|
printf("%05u ", chain->fw_number);
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (do_acct)
|
1998-01-08 03:03:54 +00:00
|
|
|
printf("%*qu %*qu ",pcwidth,chain->fw_pcnt,bcwidth,chain->fw_bcnt);
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
if (do_time)
|
|
|
|
{
|
|
|
|
if (chain->timestamp)
|
|
|
|
{
|
|
|
|
char timestr[30];
|
|
|
|
|
|
|
|
strcpy(timestr, ctime((time_t *)&chain->timestamp));
|
|
|
|
*strchr(timestr, '\n') = '\0';
|
|
|
|
printf("%s ", timestr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
1996-07-10 19:44:30 +00:00
|
|
|
switch (chain->fw_flg & IP_FW_F_COMMAND)
|
|
|
|
{
|
|
|
|
case IP_FW_F_ACCEPT:
|
|
|
|
printf("allow");
|
|
|
|
break;
|
1997-06-02 05:02:37 +00:00
|
|
|
case IP_FW_F_DENY:
|
|
|
|
printf("deny");
|
1996-07-10 19:44:30 +00:00
|
|
|
break;
|
|
|
|
case IP_FW_F_COUNT:
|
|
|
|
printf("count");
|
|
|
|
break;
|
1997-06-02 05:02:37 +00:00
|
|
|
case IP_FW_F_DIVERT:
|
|
|
|
printf("divert %u", chain->fw_divert_port);
|
|
|
|
break;
|
|
|
|
case IP_FW_F_TEE:
|
|
|
|
printf("tee %u", chain->fw_divert_port);
|
|
|
|
break;
|
|
|
|
case IP_FW_F_SKIPTO:
|
|
|
|
printf("skipto %u", chain->fw_skipto_rule);
|
|
|
|
break;
|
|
|
|
case IP_FW_F_REJECT:
|
|
|
|
if (chain->fw_reject_code == IP_FW_REJECT_RST)
|
|
|
|
printf("reset");
|
|
|
|
else {
|
|
|
|
printf("unreach ");
|
|
|
|
print_reject_code(chain->fw_reject_code);
|
|
|
|
}
|
1996-07-10 19:44:30 +00:00
|
|
|
break;
|
1998-07-06 03:20:19 +00:00
|
|
|
case IP_FW_F_FWD:
|
|
|
|
printf("fwd %s", inet_ntoa(chain->fw_fwd_ip.sin_addr));
|
|
|
|
if(chain->fw_fwd_ip.sin_port)
|
|
|
|
printf(",%d", chain->fw_fwd_ip.sin_port);
|
|
|
|
break;
|
1996-07-10 19:44:30 +00:00
|
|
|
default:
|
1998-01-07 02:23:04 +00:00
|
|
|
errx(EX_OSERR, "impossible");
|
1996-07-10 19:44:30 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_PRN)
|
|
|
|
printf(" log");
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1996-08-13 19:43:24 +00:00
|
|
|
pe = getprotobynumber(chain->fw_prot);
|
|
|
|
if (pe)
|
|
|
|
printf(" %s", pe->p_name);
|
|
|
|
else
|
1997-06-02 05:02:37 +00:00
|
|
|
printf(" %u", chain->fw_prot);
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1997-01-16 21:04:29 +00:00
|
|
|
printf(" from %s", chain->fw_flg & IP_FW_F_INVSRC ? "not " : "");
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=ntohl(chain->fw_smsk.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
if (adrt==ULONG_MAX && do_resolv) {
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=(chain->fw_src.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
he=gethostbyaddr((char *)&adrt,sizeof(u_long),AF_INET);
|
|
|
|
if (he==NULL) {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_src));
|
1994-11-16 10:18:18 +00:00
|
|
|
} else
|
1994-10-28 15:06:53 +00:00
|
|
|
printf("%s",he->h_name);
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1995-10-23 03:58:06 +00:00
|
|
|
if (adrt!=ULONG_MAX) {
|
|
|
|
mb=mask_bits(chain->fw_smsk);
|
|
|
|
if (mb == 0) {
|
|
|
|
printf("any");
|
|
|
|
} else {
|
|
|
|
if (mb > 0) {
|
|
|
|
printf(inet_ntoa(chain->fw_src));
|
|
|
|
printf("/%d",mb);
|
|
|
|
} else {
|
|
|
|
printf(inet_ntoa(chain->fw_src));
|
|
|
|
printf(":");
|
|
|
|
printf(inet_ntoa(chain->fw_smsk));
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1995-10-23 03:58:06 +00:00
|
|
|
} else
|
|
|
|
printf(inet_ntoa(chain->fw_src));
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
if (chain->fw_prot == IPPROTO_TCP || chain->fw_prot == IPPROTO_UDP) {
|
1996-08-05 02:38:51 +00:00
|
|
|
comma = " ";
|
1997-06-02 05:02:37 +00:00
|
|
|
for (i = 0; i < nsp; i++) {
|
1998-01-08 03:03:54 +00:00
|
|
|
print_port(chain->fw_prot, chain->fw_uar.fw_pts[i], comma);
|
1996-08-05 02:38:51 +00:00
|
|
|
if (i==0 && (chain->fw_flg & IP_FW_F_SRNG))
|
|
|
|
comma = "-";
|
|
|
|
else
|
|
|
|
comma = ",";
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-01-16 21:04:29 +00:00
|
|
|
printf(" to %s", chain->fw_flg & IP_FW_F_INVDST ? "not " : "");
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=ntohl(chain->fw_dmsk.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
if (adrt==ULONG_MAX && do_resolv) {
|
1994-12-13 15:56:51 +00:00
|
|
|
adrt=(chain->fw_dst.s_addr);
|
1994-11-16 10:18:18 +00:00
|
|
|
he=gethostbyaddr((char *)&adrt,sizeof(u_long),AF_INET);
|
|
|
|
if (he==NULL) {
|
1994-12-13 15:56:51 +00:00
|
|
|
printf(inet_ntoa(chain->fw_dst));
|
1994-11-16 10:18:18 +00:00
|
|
|
} else
|
1994-10-28 15:06:53 +00:00
|
|
|
printf("%s",he->h_name);
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1995-10-23 03:58:06 +00:00
|
|
|
if (adrt!=ULONG_MAX) {
|
|
|
|
mb=mask_bits(chain->fw_dmsk);
|
|
|
|
if (mb == 0) {
|
|
|
|
printf("any");
|
|
|
|
} else {
|
|
|
|
if (mb > 0) {
|
|
|
|
printf(inet_ntoa(chain->fw_dst));
|
|
|
|
printf("/%d",mb);
|
|
|
|
} else {
|
|
|
|
printf(inet_ntoa(chain->fw_dst));
|
|
|
|
printf(":");
|
|
|
|
printf(inet_ntoa(chain->fw_dmsk));
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1995-10-23 03:58:06 +00:00
|
|
|
} else
|
|
|
|
printf(inet_ntoa(chain->fw_dst));
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
if (chain->fw_prot == IPPROTO_TCP || chain->fw_prot == IPPROTO_UDP) {
|
|
|
|
comma = " ";
|
|
|
|
for (i = 0; i < ndp; i++) {
|
1998-01-08 03:03:54 +00:00
|
|
|
print_port(chain->fw_prot, chain->fw_uar.fw_pts[nsp+i], comma);
|
1997-06-02 05:02:37 +00:00
|
|
|
if (i==0 && (chain->fw_flg & IP_FW_F_DRNG))
|
|
|
|
comma = "-";
|
|
|
|
else
|
|
|
|
comma = ",";
|
|
|
|
}
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
/* Direction */
|
|
|
|
if ((chain->fw_flg & IP_FW_F_IN) && !(chain->fw_flg & IP_FW_F_OUT))
|
1996-06-29 01:28:19 +00:00
|
|
|
printf(" in");
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!(chain->fw_flg & IP_FW_F_IN) && (chain->fw_flg & IP_FW_F_OUT))
|
1996-06-29 01:28:19 +00:00
|
|
|
printf(" out");
|
1996-02-24 00:20:56 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
/* Handle hack for "via" backwards compatibility */
|
|
|
|
if ((chain->fw_flg & IF_FW_F_VIAHACK) == IF_FW_F_VIAHACK) {
|
|
|
|
print_iface("via",
|
|
|
|
&chain->fw_in_if, chain->fw_flg & IP_FW_F_IIFNAME);
|
|
|
|
} else {
|
|
|
|
/* Receive interface specified */
|
|
|
|
if (chain->fw_flg & IP_FW_F_IIFACE)
|
|
|
|
print_iface("recv", &chain->fw_in_if,
|
|
|
|
chain->fw_flg & IP_FW_F_IIFNAME);
|
|
|
|
/* Transmit interface specified */
|
|
|
|
if (chain->fw_flg & IP_FW_F_OIFACE)
|
|
|
|
print_iface("xmit", &chain->fw_out_if,
|
|
|
|
chain->fw_flg & IP_FW_F_OIFNAME);
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (chain->fw_flg & IP_FW_F_FRAG)
|
1996-06-29 01:28:19 +00:00
|
|
|
printf(" frag");
|
1995-10-01 21:54:05 +00:00
|
|
|
|
1995-10-23 03:58:06 +00:00
|
|
|
if (chain->fw_ipopt || chain->fw_ipnopt) {
|
1996-02-24 00:20:56 +00:00
|
|
|
int _opt_printed = 0;
|
1995-10-01 21:54:05 +00:00
|
|
|
#define PRINTOPT(x) {if (_opt_printed) printf(",");\
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(x); _opt_printed = 1;}
|
|
|
|
|
|
|
|
printf(" ipopt ");
|
|
|
|
if (chain->fw_ipopt & IP_FW_IPOPT_SSRR) PRINTOPT("ssrr");
|
|
|
|
if (chain->fw_ipnopt & IP_FW_IPOPT_SSRR) PRINTOPT("!ssrr");
|
|
|
|
if (chain->fw_ipopt & IP_FW_IPOPT_LSRR) PRINTOPT("lsrr");
|
|
|
|
if (chain->fw_ipnopt & IP_FW_IPOPT_LSRR) PRINTOPT("!lsrr");
|
|
|
|
if (chain->fw_ipopt & IP_FW_IPOPT_RR) PRINTOPT("rr");
|
|
|
|
if (chain->fw_ipnopt & IP_FW_IPOPT_RR) PRINTOPT("!rr");
|
|
|
|
if (chain->fw_ipopt & IP_FW_IPOPT_TS) PRINTOPT("ts");
|
|
|
|
if (chain->fw_ipnopt & IP_FW_IPOPT_TS) PRINTOPT("!ts");
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
1995-10-23 03:58:06 +00:00
|
|
|
|
1996-04-03 13:49:10 +00:00
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_ESTAB)
|
|
|
|
printf(" established");
|
|
|
|
else if (chain->fw_tcpf == IP_FW_TCPF_SYN &&
|
1996-02-24 00:20:56 +00:00
|
|
|
chain->fw_tcpnf == IP_FW_TCPF_ACK)
|
1996-02-24 13:39:46 +00:00
|
|
|
printf(" setup");
|
1996-02-24 00:20:56 +00:00
|
|
|
else if (chain->fw_tcpf || chain->fw_tcpnf) {
|
|
|
|
int _flg_printed = 0;
|
1995-10-23 03:58:06 +00:00
|
|
|
#define PRINTFLG(x) {if (_flg_printed) printf(",");\
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(x); _flg_printed = 1;}
|
|
|
|
|
|
|
|
printf(" tcpflg ");
|
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_FIN) PRINTFLG("fin");
|
|
|
|
if (chain->fw_tcpnf & IP_FW_TCPF_FIN) PRINTFLG("!fin");
|
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_SYN) PRINTFLG("syn");
|
|
|
|
if (chain->fw_tcpnf & IP_FW_TCPF_SYN) PRINTFLG("!syn");
|
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_RST) PRINTFLG("rst");
|
|
|
|
if (chain->fw_tcpnf & IP_FW_TCPF_RST) PRINTFLG("!rst");
|
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_PSH) PRINTFLG("psh");
|
|
|
|
if (chain->fw_tcpnf & IP_FW_TCPF_PSH) PRINTFLG("!psh");
|
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_ACK) PRINTFLG("ack");
|
|
|
|
if (chain->fw_tcpnf & IP_FW_TCPF_ACK) PRINTFLG("!ack");
|
|
|
|
if (chain->fw_tcpf & IP_FW_TCPF_URG) PRINTFLG("urg");
|
|
|
|
if (chain->fw_tcpnf & IP_FW_TCPF_URG) PRINTFLG("!urg");
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
1996-06-09 23:46:22 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPBIT) {
|
|
|
|
int type_index;
|
|
|
|
int first = 1;
|
|
|
|
|
|
|
|
printf(" icmptype");
|
|
|
|
|
1997-08-08 14:36:29 +00:00
|
|
|
for (type_index = 0; type_index < IP_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8; ++type_index)
|
1998-01-08 03:03:54 +00:00
|
|
|
if (chain->fw_uar.fw_icmptypes[type_index / (sizeof(unsigned) * 8)] &
|
1996-06-09 23:46:22 +00:00
|
|
|
(1U << (type_index % (sizeof(unsigned) * 8)))) {
|
|
|
|
printf("%c%d", first == 1 ? ' ' : ',', type_index);
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("\n");
|
1996-06-09 23:46:22 +00:00
|
|
|
if (do_resolv)
|
|
|
|
endservent();
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
1996-02-24 00:20:56 +00:00
|
|
|
list(ac, av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1996-02-24 13:39:46 +00:00
|
|
|
struct ip_fw *r;
|
|
|
|
struct ip_fw rules[1024];
|
1998-01-08 03:03:54 +00:00
|
|
|
int l,i,bytes;
|
1998-01-07 02:23:04 +00:00
|
|
|
unsigned long rulenum;
|
1998-01-08 03:03:54 +00:00
|
|
|
int pcwidth = 0;
|
|
|
|
int bcwidth = 0;
|
1996-02-24 13:39:46 +00:00
|
|
|
|
1998-01-07 02:23:04 +00:00
|
|
|
/* extract rules from kernel */
|
1996-02-24 13:39:46 +00:00
|
|
|
memset(rules,0,sizeof rules);
|
1998-01-07 02:23:04 +00:00
|
|
|
bytes = sizeof rules;
|
|
|
|
i = getsockopt(s, IPPROTO_IP, IP_FW_GET, rules, &bytes);
|
1996-02-24 13:39:46 +00:00
|
|
|
if (i < 0)
|
|
|
|
err(2,"getsockopt(IP_FW_GET)");
|
1998-01-08 03:03:54 +00:00
|
|
|
if (do_acct)
|
|
|
|
/* find the maximum packet/byte counter widths */
|
|
|
|
for (r=rules, l = bytes; l >= sizeof rules[0];
|
|
|
|
r++, l-=sizeof rules[0]) {
|
|
|
|
char temp[32];
|
|
|
|
int width;
|
|
|
|
|
|
|
|
/* packet counter */
|
|
|
|
width = sprintf(temp, "%qu", r->fw_pcnt);
|
|
|
|
if (width > pcwidth)
|
|
|
|
pcwidth = width;
|
|
|
|
|
|
|
|
/* byte counter */
|
|
|
|
width = sprintf(temp, "%qu", r->fw_bcnt);
|
|
|
|
if (width > bcwidth)
|
|
|
|
bcwidth = width;
|
|
|
|
}
|
1998-01-07 02:23:04 +00:00
|
|
|
if (!ac) {
|
|
|
|
/* display all rules */
|
|
|
|
for (r = rules, l = bytes; l >= sizeof rules[0];
|
|
|
|
r++, l-=sizeof rules[0])
|
1998-01-08 03:03:54 +00:00
|
|
|
show_ipfw(r, pcwidth, bcwidth);
|
1998-01-07 02:23:04 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* display specific rules requested on command line */
|
|
|
|
int exitval = EX_OK;
|
|
|
|
|
|
|
|
while (ac--) {
|
|
|
|
char *endptr;
|
|
|
|
int seen;
|
|
|
|
|
|
|
|
/* convert command line rule # */
|
|
|
|
rulenum = strtoul(*av++, &endptr, 10);
|
|
|
|
if (*endptr) {
|
|
|
|
exitval = EX_USAGE;
|
1998-01-08 00:27:31 +00:00
|
|
|
warn("invalid rule number: %s", *(av - 1));
|
1998-01-07 02:23:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
seen = 0;
|
|
|
|
for (r = rules, l = bytes;
|
|
|
|
l >= sizeof rules[0] && r->fw_number <= rulenum;
|
|
|
|
r++, l-=sizeof rules[0])
|
|
|
|
if (rulenum == r->fw_number) {
|
1998-01-08 03:03:54 +00:00
|
|
|
show_ipfw(r, pcwidth, bcwidth);
|
1998-01-07 02:23:04 +00:00
|
|
|
seen = 1;
|
|
|
|
}
|
|
|
|
if (!seen) {
|
|
|
|
/* give precedence to other error(s) */
|
|
|
|
if (exitval == EX_OK)
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
warnx("rule %lu does not exist", rulenum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (exitval != EX_OK)
|
|
|
|
exit(exitval);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
|
|
|
show_usage(const char *fmt, ...)
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1997-06-02 05:02:37 +00:00
|
|
|
if (fmt) {
|
|
|
|
char buf[100];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
warnx("error: %s", buf);
|
1997-06-13 06:27:12 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "usage: ipfw [options]\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" flush\n"
|
|
|
|
" add [number] rule\n"
|
|
|
|
" delete number ...\n"
|
1998-01-07 02:23:04 +00:00
|
|
|
" list [number ...]\n"
|
|
|
|
" show [number ...]\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" zero [number ...]\n"
|
|
|
|
" rule: action proto src dst extras...\n"
|
|
|
|
" action:\n"
|
|
|
|
" {allow|permit|accept|pass|deny|drop|reject|unreach code|\n"
|
1998-07-06 03:20:19 +00:00
|
|
|
" reset|count|skipto num|divert port|tee port|fwd ip} [log]\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" proto: {ip|tcp|udp|icmp|<number>}\n"
|
|
|
|
" src: from [not] {any|ip[{/bits|:mask}]} [{port|port-port},[port],...]\n"
|
|
|
|
" dst: to [not] {any|ip[{/bits|:mask}]} [{port|port-port},[port],...]\n"
|
|
|
|
" extras:\n"
|
1998-02-12 00:57:06 +00:00
|
|
|
" fragment (may not be used with ports or tcpflags)\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" in\n"
|
|
|
|
" out\n"
|
|
|
|
" {xmit|recv|via} {iface|ip|any}\n"
|
|
|
|
" {established|setup}\n"
|
|
|
|
" tcpflags [!]{syn|fin|rst|ack|psh|urg},...\n"
|
|
|
|
" ipoptions [!]{ssrr|lsrr|rr|ts},...\n"
|
1997-06-13 06:27:12 +00:00
|
|
|
" icmptypes {type[,type]}...\n");
|
1997-06-02 05:02:37 +00:00
|
|
|
|
1998-01-07 02:23:04 +00:00
|
|
|
exit(EX_USAGE);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static int
|
1996-06-09 23:46:22 +00:00
|
|
|
lookup_host (host, ipaddr)
|
|
|
|
char *host;
|
|
|
|
struct in_addr *ipaddr;
|
|
|
|
{
|
|
|
|
struct hostent *he = gethostbyname(host);
|
|
|
|
|
|
|
|
if (!he)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
*ipaddr = *(struct in_addr *)he->h_addr_list[0];
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
1996-02-24 00:20:56 +00:00
|
|
|
fill_ip(ipno, mask, acp, avp)
|
|
|
|
struct in_addr *ipno, *mask;
|
|
|
|
int *acp;
|
|
|
|
char ***avp;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
int ac = *acp;
|
|
|
|
char **av = *avp;
|
|
|
|
char *p = 0, md = 0;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (ac && !strncmp(*av,"any",strlen(*av))) {
|
|
|
|
ipno->s_addr = mask->s_addr = 0; av++; ac--;
|
|
|
|
} else {
|
|
|
|
p = strchr(*av, '/');
|
|
|
|
if (!p)
|
|
|
|
p = strchr(*av, ':');
|
|
|
|
if (p) {
|
|
|
|
md = *p;
|
|
|
|
*p++ = '\0';
|
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
if (lookup_host(*av, ipno) != 0)
|
|
|
|
show_usage("hostname ``%s'' unknown", *av);
|
1996-06-23 20:47:51 +00:00
|
|
|
switch (md) {
|
|
|
|
case ':':
|
|
|
|
if (!inet_aton(p,mask))
|
1997-06-02 05:02:37 +00:00
|
|
|
show_usage("bad netmask ``%s''", p);
|
1996-06-23 20:47:51 +00:00
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (atoi(p) == 0) {
|
|
|
|
mask->s_addr = 0;
|
1997-06-02 05:02:37 +00:00
|
|
|
} else if (atoi(p) > 32) {
|
|
|
|
show_usage("bad width ``%s''", p);
|
1996-06-23 20:47:51 +00:00
|
|
|
} else {
|
1997-06-02 05:02:37 +00:00
|
|
|
mask->s_addr =
|
|
|
|
htonl(~0 << (32 - atoi(p)));
|
1996-06-23 20:47:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
1997-06-02 05:02:37 +00:00
|
|
|
mask->s_addr = htonl(~0);
|
1996-06-23 20:47:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ipno->s_addr &= mask->s_addr;
|
1996-02-24 00:20:56 +00:00
|
|
|
av++;
|
|
|
|
ac--;
|
|
|
|
}
|
|
|
|
*acp = ac;
|
|
|
|
*avp = av;
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
|
|
|
fill_reject_code(u_short *codep, char *str)
|
|
|
|
{
|
|
|
|
struct icmpcode *ic;
|
|
|
|
u_long val;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
val = strtoul(str, &s, 0);
|
|
|
|
if (s != str && *s == '\0' && val < 0x100) {
|
|
|
|
*codep = val;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (ic = icmpcodes; ic->str; ic++)
|
|
|
|
if (!strcasecmp(str, ic->str)) {
|
|
|
|
*codep = ic->code;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
show_usage("unknown ICMP unreachable code ``%s''", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1996-08-13 19:43:24 +00:00
|
|
|
add_port(cnt, ptr, off, port)
|
1996-05-11 20:31:55 +00:00
|
|
|
u_short *cnt, *ptr, off, port;
|
|
|
|
{
|
|
|
|
if (off + *cnt >= IP_FW_MAX_PORTS)
|
1998-01-07 02:23:04 +00:00
|
|
|
errx(EX_USAGE, "too many ports (max is %d)", IP_FW_MAX_PORTS);
|
1996-05-11 20:31:55 +00:00
|
|
|
ptr[off+*cnt] = port;
|
|
|
|
(*cnt)++;
|
|
|
|
}
|
|
|
|
|
1997-06-23 22:32:13 +00:00
|
|
|
static int
|
|
|
|
lookup_port(const char *arg, int test, int nodash)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
char *earg, buf[32];
|
|
|
|
struct servent *s;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s", arg);
|
|
|
|
buf[strcspn(arg, nodash ? "-," : ",")] = 0;
|
|
|
|
val = (int) strtoul(buf, &earg, 0);
|
|
|
|
if (!*buf || *earg) {
|
|
|
|
setservent(1);
|
|
|
|
if ((s = getservbyname(buf, NULL))) {
|
|
|
|
val = htons(s->s_port);
|
|
|
|
} else {
|
|
|
|
if (!test) {
|
1998-01-07 02:23:04 +00:00
|
|
|
errx(EX_DATAERR, "unknown port ``%s''", arg);
|
1997-06-23 22:32:13 +00:00
|
|
|
}
|
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (val < 0 || val > 0xffff) {
|
|
|
|
if (!test) {
|
1998-01-07 02:23:04 +00:00
|
|
|
errx(EX_DATAERR, "port ``%s'' out of range", arg);
|
1997-06-23 22:32:13 +00:00
|
|
|
}
|
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(val);
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static int
|
1996-08-13 19:43:24 +00:00
|
|
|
fill_port(cnt, ptr, off, arg)
|
1996-02-24 00:20:56 +00:00
|
|
|
u_short *cnt, *ptr, off;
|
1996-05-11 20:31:55 +00:00
|
|
|
char *arg;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1996-06-09 23:46:22 +00:00
|
|
|
char *s;
|
1996-05-11 20:31:55 +00:00
|
|
|
int initial_range = 0;
|
1996-02-24 00:20:56 +00:00
|
|
|
|
1997-06-23 22:32:13 +00:00
|
|
|
s = arg + strcspn(arg, "-,"); /* first port name can't have a dash */
|
|
|
|
if (*s == '-') {
|
1996-02-24 00:20:56 +00:00
|
|
|
*s++ = '\0';
|
1996-05-11 20:31:55 +00:00
|
|
|
if (strchr(arg, ','))
|
1998-01-07 02:23:04 +00:00
|
|
|
errx(EX_USAGE, "port range must be first in list");
|
1997-06-23 22:32:13 +00:00
|
|
|
add_port(cnt, ptr, off, *arg ? lookup_port(arg, 0, 0) : 0x0000);
|
1996-05-11 20:31:55 +00:00
|
|
|
arg = s;
|
|
|
|
s = strchr(arg,',');
|
|
|
|
if (s)
|
1996-02-24 00:20:56 +00:00
|
|
|
*s++ = '\0';
|
1997-06-23 22:32:13 +00:00
|
|
|
add_port(cnt, ptr, off, *arg ? lookup_port(arg, 0, 0) : 0xffff);
|
1996-05-11 20:31:55 +00:00
|
|
|
arg = s;
|
|
|
|
initial_range = 1;
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
1996-05-11 20:31:55 +00:00
|
|
|
while (arg != NULL) {
|
|
|
|
s = strchr(arg,',');
|
|
|
|
if (s)
|
1996-02-24 00:20:56 +00:00
|
|
|
*s++ = '\0';
|
1997-06-23 22:32:13 +00:00
|
|
|
add_port(cnt, ptr, off, lookup_port(arg, 0, 0));
|
1996-05-11 20:31:55 +00:00
|
|
|
arg = s;
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1996-05-11 20:31:55 +00:00
|
|
|
return initial_range;
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
1996-02-24 00:20:56 +00:00
|
|
|
fill_tcpflag(set, reset, vp)
|
|
|
|
u_char *set, *reset;
|
|
|
|
char **vp;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
char *p = *vp,*q;
|
|
|
|
u_char *d;
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
while (p && *p) {
|
1996-06-09 23:46:22 +00:00
|
|
|
struct tpcflags {
|
|
|
|
char * name;
|
|
|
|
u_char value;
|
|
|
|
} flags[] = {
|
|
|
|
{ "syn", IP_FW_TCPF_SYN },
|
|
|
|
{ "fin", IP_FW_TCPF_FIN },
|
|
|
|
{ "ack", IP_FW_TCPF_ACK },
|
|
|
|
{ "psh", IP_FW_TCPF_PSH },
|
|
|
|
{ "rst", IP_FW_TCPF_RST },
|
|
|
|
{ "urg", IP_FW_TCPF_URG }
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (*p == '!') {
|
|
|
|
p++;
|
|
|
|
d = reset;
|
1994-11-16 10:18:18 +00:00
|
|
|
} else {
|
1996-02-24 00:20:56 +00:00
|
|
|
d = set;
|
|
|
|
}
|
|
|
|
q = strchr(p, ',');
|
|
|
|
if (q)
|
|
|
|
*q++ = '\0';
|
1996-06-09 23:46:22 +00:00
|
|
|
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i)
|
|
|
|
if (!strncmp(p, flags[i].name, strlen(p))) {
|
|
|
|
*d |= flags[i].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == sizeof(flags) / sizeof(flags[0]))
|
1997-06-02 05:02:37 +00:00
|
|
|
show_usage("invalid tcp flag ``%s''", p);
|
1996-02-24 00:20:56 +00:00
|
|
|
p = q;
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
1995-02-24 14:32:45 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
|
|
|
fill_ipopt(u_char *set, u_char *reset, char **vp)
|
1995-10-01 21:54:05 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
char *p = *vp,*q;
|
|
|
|
u_char *d;
|
1995-10-01 21:54:05 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
while (p && *p) {
|
|
|
|
if (*p == '!') {
|
|
|
|
p++;
|
|
|
|
d = reset;
|
|
|
|
} else {
|
|
|
|
d = set;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
q = strchr(p, ',');
|
|
|
|
if (q)
|
|
|
|
*q++ = '\0';
|
|
|
|
if (!strncmp(p,"ssrr",strlen(p))) *d |= IP_FW_IPOPT_SSRR;
|
|
|
|
if (!strncmp(p,"lsrr",strlen(p))) *d |= IP_FW_IPOPT_LSRR;
|
|
|
|
if (!strncmp(p,"rr",strlen(p))) *d |= IP_FW_IPOPT_RR;
|
|
|
|
if (!strncmp(p,"ts",strlen(p))) *d |= IP_FW_IPOPT_TS;
|
|
|
|
p = q;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
1996-06-09 23:46:22 +00:00
|
|
|
fill_icmptypes(types, vp, fw_flg)
|
|
|
|
u_long *types;
|
|
|
|
char **vp;
|
1998-09-28 22:56:37 +00:00
|
|
|
u_int *fw_flg;
|
1996-06-09 23:46:22 +00:00
|
|
|
{
|
|
|
|
char *c = *vp;
|
|
|
|
|
|
|
|
while (*c)
|
|
|
|
{
|
|
|
|
unsigned long icmptype;
|
|
|
|
|
|
|
|
if ( *c == ',' )
|
|
|
|
++c;
|
|
|
|
|
|
|
|
icmptype = strtoul(c, &c, 0);
|
|
|
|
|
|
|
|
if ( *c != ',' && *c != '\0' )
|
|
|
|
show_usage("invalid ICMP type");
|
|
|
|
|
1997-08-08 14:36:29 +00:00
|
|
|
if (icmptype >= IP_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8)
|
|
|
|
show_usage("ICMP type out of range");
|
1996-06-09 23:46:22 +00:00
|
|
|
|
|
|
|
types[icmptype / (sizeof(unsigned) * 8)] |=
|
|
|
|
1 << (icmptype % (sizeof(unsigned) * 8));
|
|
|
|
*fw_flg |= IP_FW_F_ICMPBIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
1996-02-24 00:20:56 +00:00
|
|
|
delete(ac,av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
1995-10-23 03:58:06 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
struct ip_fw rule;
|
|
|
|
int i;
|
1998-01-07 02:23:04 +00:00
|
|
|
int exitval = EX_OK;
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
memset(&rule, 0, sizeof rule);
|
1995-10-23 03:58:06 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
av++; ac--;
|
1995-10-23 03:58:06 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* Rule number */
|
1997-06-02 05:02:37 +00:00
|
|
|
while (ac && isdigit(**av)) {
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_FW_DEL, &rule, sizeof rule);
|
1998-01-06 00:11:57 +00:00
|
|
|
if (i) {
|
1998-01-07 02:23:04 +00:00
|
|
|
exitval = EX_UNAVAILABLE;
|
1998-01-06 00:11:57 +00:00
|
|
|
warn("rule %u: setsockopt(%s)", rule.fw_number, "IP_FW_DEL");
|
|
|
|
}
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
1998-01-07 02:23:04 +00:00
|
|
|
if (exitval != EX_OK)
|
|
|
|
exit(exitval);
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
1995-02-24 14:32:45 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
|
|
|
verify_interface(union ip_fw_if *ifu)
|
1996-10-17 01:05:03 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a unit was specified, check for that exact interface.
|
|
|
|
* If a wildcard was specified, check for unit 0.
|
|
|
|
*/
|
|
|
|
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d",
|
1997-06-02 05:02:37 +00:00
|
|
|
ifu->fu_via_if.name,
|
|
|
|
ifu->fu_via_if.unit == -1 ? 0 : ifu->fu_via_if.unit);
|
1996-10-17 01:05:03 +00:00
|
|
|
|
|
|
|
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
|
1997-06-02 05:02:37 +00:00
|
|
|
warnx("warning: interface ``%s'' does not exist", ifr.ifr_name);
|
|
|
|
}
|
1996-10-17 01:05:03 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
|
|
|
fill_iface(char *which, union ip_fw_if *ifu, int *byname, int ac, char *arg)
|
|
|
|
{
|
|
|
|
if (!ac)
|
|
|
|
show_usage("missing argument for ``%s''", which);
|
|
|
|
|
|
|
|
/* Parse the interface or address */
|
|
|
|
if (!strcmp(arg, "any")) {
|
|
|
|
ifu->fu_via_ip.s_addr = 0;
|
|
|
|
*byname = 0;
|
|
|
|
} else if (!isdigit(*arg)) {
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
*byname = 1;
|
|
|
|
strncpy(ifu->fu_via_if.name, arg, sizeof(ifu->fu_via_if.name));
|
|
|
|
ifu->fu_via_if.name[sizeof(ifu->fu_via_if.name) - 1] = '\0';
|
|
|
|
for (q = ifu->fu_via_if.name;
|
|
|
|
*q && !isdigit(*q) && *q != '*'; q++)
|
|
|
|
continue;
|
|
|
|
ifu->fu_via_if.unit = (*q == '*') ? -1 : atoi(q);
|
|
|
|
*q = '\0';
|
|
|
|
verify_interface(ifu);
|
|
|
|
} else if (!inet_aton(arg, &ifu->fu_via_ip)) {
|
|
|
|
show_usage("bad ip address ``%s''", arg);
|
|
|
|
} else
|
|
|
|
*byname = 0;
|
1996-10-17 01:05:03 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
1996-02-24 00:20:56 +00:00
|
|
|
add(ac,av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
struct ip_fw rule;
|
|
|
|
int i;
|
1996-08-13 19:43:24 +00:00
|
|
|
u_char proto;
|
|
|
|
struct protoent *pe;
|
1997-06-02 05:02:37 +00:00
|
|
|
int saw_xmrc = 0, saw_via = 0;
|
1996-02-24 00:20:56 +00:00
|
|
|
|
|
|
|
memset(&rule, 0, sizeof rule);
|
|
|
|
|
|
|
|
av++; ac--;
|
|
|
|
|
|
|
|
/* Rule number */
|
|
|
|
if (ac && isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Action */
|
1997-06-02 05:02:37 +00:00
|
|
|
if (ac == 0)
|
|
|
|
show_usage("missing action");
|
|
|
|
if (!strncmp(*av,"accept",strlen(*av))
|
1996-07-10 19:44:30 +00:00
|
|
|
|| !strncmp(*av,"pass",strlen(*av))
|
|
|
|
|| !strncmp(*av,"allow",strlen(*av))
|
1997-06-02 05:02:37 +00:00
|
|
|
|| !strncmp(*av,"permit",strlen(*av))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_ACCEPT; av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
} else if (!strncmp(*av,"count",strlen(*av))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_COUNT; av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
} else if (!strncmp(*av,"divert",strlen(*av))) {
|
1996-07-10 19:44:30 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DIVERT; av++; ac--;
|
|
|
|
if (!ac)
|
1998-12-07 05:54:37 +00:00
|
|
|
show_usage("missing %s port", "divert");
|
1996-07-10 19:44:30 +00:00
|
|
|
rule.fw_divert_port = strtoul(*av, NULL, 0); av++; ac--;
|
1997-07-25 03:13:46 +00:00
|
|
|
if (rule.fw_divert_port == 0) {
|
|
|
|
struct servent *s;
|
|
|
|
setservent(1);
|
|
|
|
s = getservbyname(av[-1], "divert");
|
|
|
|
if (s != NULL)
|
|
|
|
rule.fw_divert_port = ntohs(s->s_port);
|
|
|
|
else
|
1998-12-07 05:54:37 +00:00
|
|
|
show_usage("illegal %s port", "divert");
|
1997-07-25 03:13:46 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
} else if (!strncmp(*av,"tee",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_TEE; av++; ac--;
|
|
|
|
if (!ac)
|
1998-12-07 05:54:37 +00:00
|
|
|
show_usage("missing %s port", "tee divert");
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_divert_port = strtoul(*av, NULL, 0); av++; ac--;
|
1997-07-25 03:13:46 +00:00
|
|
|
if (rule.fw_divert_port == 0) {
|
|
|
|
struct servent *s;
|
|
|
|
setservent(1);
|
|
|
|
s = getservbyname(av[-1], "divert");
|
|
|
|
if (s != NULL)
|
|
|
|
rule.fw_divert_port = ntohs(s->s_port);
|
|
|
|
else
|
1998-12-07 05:54:37 +00:00
|
|
|
show_usage("illegal %s port", "tee divert");
|
1997-07-25 03:13:46 +00:00
|
|
|
}
|
1998-12-07 05:54:37 +00:00
|
|
|
#ifndef IPFW_TEE_IS_FINALLY_IMPLEMENTED
|
|
|
|
err(EX_USAGE, "the ``tee'' action is not implemented");
|
|
|
|
#endif
|
1998-07-06 03:20:19 +00:00
|
|
|
} else if (!strncmp(*av,"fwd",strlen(*av)) ||
|
|
|
|
!strncmp(*av,"forward",strlen(*av))) {
|
|
|
|
struct in_addr dummyip;
|
|
|
|
char *pp;
|
|
|
|
rule.fw_flg |= IP_FW_F_FWD; av++; ac--;
|
|
|
|
if (!ac)
|
|
|
|
show_usage("missing forwarding IP address");
|
|
|
|
rule.fw_fwd_ip.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
rule.fw_fwd_ip.sin_family = AF_INET;
|
|
|
|
rule.fw_fwd_ip.sin_port = 0;
|
|
|
|
pp = strchr(*av, ':');
|
|
|
|
if(pp == NULL)
|
|
|
|
pp = strchr(*av, ',');
|
|
|
|
if(pp != NULL)
|
|
|
|
{
|
|
|
|
*(pp++) = '\0';
|
|
|
|
rule.fw_fwd_ip.sin_port = lookup_port(pp, 1, 1);
|
|
|
|
if(rule.fw_fwd_ip.sin_port == (unsigned int)-1)
|
|
|
|
show_usage("illegal forwarding port");
|
|
|
|
}
|
|
|
|
fill_ip(&(rule.fw_fwd_ip.sin_addr), &dummyip, &ac, &av);
|
|
|
|
if (rule.fw_fwd_ip.sin_addr.s_addr == 0)
|
|
|
|
show_usage("illegal forwarding IP address");
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
} else if (!strncmp(*av,"skipto",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_SKIPTO; av++; ac--;
|
|
|
|
if (!ac)
|
|
|
|
show_usage("missing skipto rule number");
|
|
|
|
rule.fw_skipto_rule = strtoul(*av, NULL, 0); av++; ac--;
|
|
|
|
} else if ((!strncmp(*av,"deny",strlen(*av))
|
|
|
|
|| !strncmp(*av,"drop",strlen(*av)))) {
|
1996-07-10 19:44:30 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DENY; av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
} else if (!strncmp(*av,"reject",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_REJECT; av++; ac--;
|
|
|
|
rule.fw_reject_code = ICMP_UNREACH_HOST;
|
|
|
|
} else if (!strncmp(*av,"reset",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_REJECT; av++; ac--;
|
|
|
|
rule.fw_reject_code = IP_FW_REJECT_RST; /* check TCP later */
|
|
|
|
} else if (!strncmp(*av,"unreach",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_REJECT; av++; ac--;
|
|
|
|
fill_reject_code(&rule.fw_reject_code, *av); av++; ac--;
|
1996-02-24 00:20:56 +00:00
|
|
|
} else {
|
1997-06-02 05:02:37 +00:00
|
|
|
show_usage("invalid action ``%s''", *av);
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* [log] */
|
|
|
|
if (ac && !strncmp(*av,"log",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_PRN; av++; ac--;
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* protocol */
|
1997-06-02 05:02:37 +00:00
|
|
|
if (ac == 0)
|
|
|
|
show_usage("missing protocol");
|
|
|
|
if ((proto = atoi(*av)) > 0) {
|
|
|
|
rule.fw_prot = proto; av++; ac--;
|
|
|
|
} else if (!strncmp(*av,"all",strlen(*av))) {
|
|
|
|
rule.fw_prot = IPPROTO_IP; av++; ac--;
|
|
|
|
} else if ((pe = getprotobyname(*av)) != NULL) {
|
|
|
|
rule.fw_prot = pe->p_proto; av++; ac--;
|
|
|
|
} else {
|
|
|
|
show_usage("invalid protocol ``%s''", *av);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rule.fw_prot != IPPROTO_TCP
|
|
|
|
&& (rule.fw_flg & IP_FW_F_COMMAND) == IP_FW_F_REJECT
|
|
|
|
&& rule.fw_reject_code == IP_FW_REJECT_RST)
|
|
|
|
show_usage("``reset'' is only valid for tcp packets");
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* from */
|
|
|
|
if (ac && !strncmp(*av,"from",strlen(*av))) { av++; ac--; }
|
1997-06-02 05:02:37 +00:00
|
|
|
else
|
|
|
|
show_usage("missing ``from''");
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-01-16 21:04:29 +00:00
|
|
|
if (ac && !strncmp(*av,"not",strlen(*av))) {
|
1997-01-17 07:01:21 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_INVSRC;
|
1997-01-16 21:04:29 +00:00
|
|
|
av++; ac--;
|
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
|
|
|
show_usage("missing arguments");
|
1997-01-16 21:04:29 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
fill_ip(&rule.fw_src, &rule.fw_smsk, &ac, &av);
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-06-23 22:32:13 +00:00
|
|
|
if (ac && (isdigit(**av) || lookup_port(*av, 1, 1) >= 0)) {
|
1997-06-02 05:02:37 +00:00
|
|
|
u_short nports = 0;
|
|
|
|
|
1998-01-08 03:03:54 +00:00
|
|
|
if (fill_port(&nports, rule.fw_uar.fw_pts, 0, *av))
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_SRNG;
|
1997-06-02 05:02:37 +00:00
|
|
|
IP_FW_SETNSRCP(&rule, nports);
|
1996-02-24 00:20:56 +00:00
|
|
|
av++; ac--;
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* to */
|
|
|
|
if (ac && !strncmp(*av,"to",strlen(*av))) { av++; ac--; }
|
1997-06-02 05:02:37 +00:00
|
|
|
else
|
|
|
|
show_usage("missing ``to''");
|
1995-10-01 21:54:05 +00:00
|
|
|
|
1997-01-16 21:04:29 +00:00
|
|
|
if (ac && !strncmp(*av,"not",strlen(*av))) {
|
1997-01-17 07:01:21 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_INVDST;
|
1997-01-16 21:04:29 +00:00
|
|
|
av++; ac--;
|
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
|
|
|
show_usage("missing arguments");
|
1995-10-01 21:54:05 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
fill_ip(&rule.fw_dst, &rule.fw_dmsk, &ac, &av);
|
1995-10-01 21:54:05 +00:00
|
|
|
|
1997-06-23 22:32:13 +00:00
|
|
|
if (ac && (isdigit(**av) || lookup_port(*av, 1, 1) >= 0)) {
|
1997-06-02 05:02:37 +00:00
|
|
|
u_short nports = 0;
|
|
|
|
|
|
|
|
if (fill_port(&nports,
|
1998-01-08 03:03:54 +00:00
|
|
|
rule.fw_uar.fw_pts, IP_FW_GETNSRCP(&rule), *av))
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DRNG;
|
1997-06-02 05:02:37 +00:00
|
|
|
IP_FW_SETNDSTP(&rule, nports);
|
1996-02-24 00:20:56 +00:00
|
|
|
av++; ac--;
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
if ((rule.fw_prot != IPPROTO_TCP) && (rule.fw_prot != IPPROTO_UDP)
|
|
|
|
&& (IP_FW_GETNSRCP(&rule) || IP_FW_GETNDSTP(&rule))) {
|
|
|
|
show_usage("only TCP and UDP protocols are valid"
|
|
|
|
" with port specifications");
|
1996-02-24 13:39:46 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
while (ac) {
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!strncmp(*av,"in",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_IN;
|
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
|
|
|
if (!strncmp(*av,"out",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_OUT;
|
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
|
|
|
if (ac && !strncmp(*av,"xmit",strlen(*av))) {
|
|
|
|
union ip_fw_if ifu;
|
|
|
|
int byname;
|
|
|
|
|
|
|
|
if (saw_via) {
|
|
|
|
badviacombo:
|
|
|
|
show_usage("``via'' is incompatible"
|
|
|
|
" with ``xmit'' and ``recv''");
|
1996-06-29 01:28:19 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
saw_xmrc = 1;
|
1996-06-29 01:28:19 +00:00
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
fill_iface("xmit", &ifu, &byname, ac, *av);
|
|
|
|
rule.fw_out_if = ifu;
|
|
|
|
rule.fw_flg |= IP_FW_F_OIFACE;
|
|
|
|
if (byname)
|
|
|
|
rule.fw_flg |= IP_FW_F_OIFNAME;
|
|
|
|
av++; ac--; continue;
|
1996-06-29 01:28:19 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (ac && !strncmp(*av,"recv",strlen(*av))) {
|
|
|
|
union ip_fw_if ifu;
|
|
|
|
int byname;
|
|
|
|
|
|
|
|
if (saw_via)
|
|
|
|
goto badviacombo;
|
|
|
|
saw_xmrc = 1;
|
|
|
|
av++; ac--;
|
|
|
|
fill_iface("recv", &ifu, &byname, ac, *av);
|
|
|
|
rule.fw_in_if = ifu;
|
|
|
|
rule.fw_flg |= IP_FW_F_IIFACE;
|
|
|
|
if (byname)
|
|
|
|
rule.fw_flg |= IP_FW_F_IIFNAME;
|
|
|
|
av++; ac--; continue;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (ac && !strncmp(*av,"via",strlen(*av))) {
|
|
|
|
union ip_fw_if ifu;
|
|
|
|
int byname = 0;
|
|
|
|
|
|
|
|
if (saw_xmrc)
|
|
|
|
goto badviacombo;
|
|
|
|
saw_via = 1;
|
|
|
|
av++; ac--;
|
|
|
|
fill_iface("via", &ifu, &byname, ac, *av);
|
|
|
|
rule.fw_out_if = rule.fw_in_if = ifu;
|
|
|
|
if (byname)
|
|
|
|
rule.fw_flg |=
|
|
|
|
(IP_FW_F_IIFNAME | IP_FW_F_OIFNAME);
|
|
|
|
av++; ac--; continue;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!strncmp(*av,"fragment",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_FRAG;
|
|
|
|
av++; ac--; continue;
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!strncmp(*av,"ipoptions",strlen(*av))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
|
|
|
show_usage("missing argument"
|
|
|
|
" for ``ipoptions''");
|
1996-02-24 00:20:56 +00:00
|
|
|
fill_ipopt(&rule.fw_ipopt, &rule.fw_ipnopt, av);
|
|
|
|
av++; ac--; continue;
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1996-08-13 19:43:24 +00:00
|
|
|
if (rule.fw_prot == IPPROTO_TCP) {
|
1996-02-24 13:39:46 +00:00
|
|
|
if (!strncmp(*av,"established",strlen(*av))) {
|
1996-04-03 13:49:10 +00:00
|
|
|
rule.fw_tcpf |= IP_FW_TCPF_ESTAB;
|
1996-02-24 13:39:46 +00:00
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
|
|
|
if (!strncmp(*av,"setup",strlen(*av))) {
|
|
|
|
rule.fw_tcpf |= IP_FW_TCPF_SYN;
|
|
|
|
rule.fw_tcpnf |= IP_FW_TCPF_ACK;
|
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!strncmp(*av,"tcpflags",strlen(*av))) {
|
1996-02-24 13:39:46 +00:00
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
|
|
|
show_usage("missing argument"
|
|
|
|
" for ``tcpflags''");
|
1996-02-24 13:39:46 +00:00
|
|
|
fill_tcpflag(&rule.fw_tcpf, &rule.fw_tcpnf, av);
|
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
|
|
|
}
|
1996-08-13 19:43:24 +00:00
|
|
|
if (rule.fw_prot == IPPROTO_ICMP) {
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!strncmp(*av,"icmptypes",strlen(*av))) {
|
1996-06-09 23:46:22 +00:00
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
|
|
|
show_usage("missing argument"
|
|
|
|
" for ``icmptypes''");
|
1998-01-08 03:03:54 +00:00
|
|
|
fill_icmptypes(rule.fw_uar.fw_icmptypes,
|
1997-06-02 05:02:37 +00:00
|
|
|
av, &rule.fw_flg);
|
1996-06-09 23:46:22 +00:00
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
show_usage("unknown argument ``%s''", *av);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
/* No direction specified -> do both directions */
|
|
|
|
if (!(rule.fw_flg & (IP_FW_F_OUT|IP_FW_F_IN)))
|
|
|
|
rule.fw_flg |= (IP_FW_F_OUT|IP_FW_F_IN);
|
|
|
|
|
|
|
|
/* Sanity check interface check, but handle "via" case separately */
|
|
|
|
if (saw_via) {
|
|
|
|
if (rule.fw_flg & IP_FW_F_IN)
|
|
|
|
rule.fw_flg |= IP_FW_F_IIFACE;
|
|
|
|
if (rule.fw_flg & IP_FW_F_OUT)
|
|
|
|
rule.fw_flg |= IP_FW_F_OIFACE;
|
|
|
|
} else if ((rule.fw_flg & IP_FW_F_OIFACE) && (rule.fw_flg & IP_FW_F_IN))
|
|
|
|
show_usage("can't check xmit interface of incoming packets");
|
|
|
|
|
1998-02-12 00:57:06 +00:00
|
|
|
/* frag may not be used in conjunction with ports or TCP flags */
|
|
|
|
if (rule.fw_flg & IP_FW_F_FRAG) {
|
|
|
|
if (rule.fw_tcpf || rule.fw_tcpnf)
|
1998-03-13 02:31:21 +00:00
|
|
|
show_usage("can't mix 'frag' and tcpflags");
|
1998-02-12 00:57:06 +00:00
|
|
|
|
|
|
|
if (rule.fw_nports)
|
1998-03-13 02:31:21 +00:00
|
|
|
show_usage("can't mix 'frag' and port specifications");
|
1998-02-12 00:57:06 +00:00
|
|
|
}
|
|
|
|
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
1998-01-08 03:03:54 +00:00
|
|
|
show_ipfw(&rule, 10, 10);
|
1996-02-24 00:20:56 +00:00
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_FW_ADD, &rule, sizeof rule);
|
|
|
|
if (i)
|
1998-01-07 02:23:04 +00:00
|
|
|
err(EX_UNAVAILABLE, "setsockopt(%s)", "IP_FW_ADD");
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
1996-06-09 23:46:22 +00:00
|
|
|
zero (ac, av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
|
|
|
{
|
|
|
|
av++; ac--;
|
|
|
|
|
|
|
|
if (!ac) {
|
|
|
|
/* clear all entries */
|
1997-06-02 05:02:37 +00:00
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_ZERO,NULL,0)<0)
|
1998-01-07 02:23:04 +00:00
|
|
|
err(EX_UNAVAILABLE, "setsockopt(%s)", "IP_FW_ZERO");
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
|
|
|
printf("Accounting cleared.\n");
|
1996-06-09 23:46:22 +00:00
|
|
|
} else {
|
|
|
|
struct ip_fw rule;
|
1998-01-07 02:23:04 +00:00
|
|
|
int failed = EX_OK;
|
1996-06-09 23:46:22 +00:00
|
|
|
|
|
|
|
memset(&rule, 0, sizeof rule);
|
1997-06-02 05:02:37 +00:00
|
|
|
while (ac) {
|
|
|
|
/* Rule number */
|
|
|
|
if (isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
|
|
|
if (setsockopt(s, IPPROTO_IP,
|
1998-01-06 00:11:57 +00:00
|
|
|
IP_FW_ZERO, &rule, sizeof rule)) {
|
|
|
|
warn("rule %u: setsockopt(%s)", rule.fw_number,
|
|
|
|
"IP_FW_ZERO");
|
1998-01-07 02:23:04 +00:00
|
|
|
failed = EX_UNAVAILABLE;
|
1998-01-06 00:11:57 +00:00
|
|
|
}
|
1998-05-15 12:38:07 +00:00
|
|
|
else if (!do_quiet)
|
1997-06-02 05:02:37 +00:00
|
|
|
printf("Entry %d cleared\n",
|
|
|
|
rule.fw_number);
|
|
|
|
} else
|
|
|
|
show_usage("invalid rule number ``%s''", *av);
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
1998-01-07 02:23:04 +00:00
|
|
|
if (failed != EX_OK)
|
|
|
|
exit(failed);
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static int
|
1995-10-23 03:58:06 +00:00
|
|
|
ipfw_main(ac,av)
|
1996-02-24 00:20:56 +00:00
|
|
|
int ac;
|
|
|
|
char **av;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-12-26 03:24:26 +00:00
|
|
|
int ch;
|
1998-11-23 10:54:28 +00:00
|
|
|
extern int optreset; /* XXX should be declared in <unistd.h> */
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if ( ac == 1 ) {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-08-31 17:58:23 +00:00
|
|
|
/* Set the force flag for non-interactive processes */
|
|
|
|
do_force = !isatty(STDIN_FILENO);
|
|
|
|
|
1998-11-23 10:54:28 +00:00
|
|
|
optind = optreset = 1;
|
1997-12-26 03:24:26 +00:00
|
|
|
while ((ch = getopt(ac, av, "afqtN")) != -1)
|
1994-11-16 10:18:18 +00:00
|
|
|
switch(ch) {
|
1994-12-12 17:19:33 +00:00
|
|
|
case 'a':
|
|
|
|
do_acct=1;
|
|
|
|
break;
|
1996-08-31 17:58:23 +00:00
|
|
|
case 'f':
|
|
|
|
do_force=1;
|
|
|
|
break;
|
1997-02-10 15:36:54 +00:00
|
|
|
case 'q':
|
|
|
|
do_quiet=1;
|
|
|
|
break;
|
1996-06-09 23:46:22 +00:00
|
|
|
case 't':
|
|
|
|
do_time=1;
|
|
|
|
break;
|
1996-02-24 13:39:46 +00:00
|
|
|
case 'N':
|
1996-02-24 00:20:56 +00:00
|
|
|
do_resolv=1;
|
1996-06-23 20:47:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
1997-06-13 06:27:12 +00:00
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
ac -= optind;
|
1994-11-16 10:18:18 +00:00
|
|
|
if (*(av+=optind)==NULL) {
|
1996-05-11 20:31:55 +00:00
|
|
|
show_usage("Bad arguments");
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (!strncmp(*av, "add", strlen(*av))) {
|
|
|
|
add(ac,av);
|
|
|
|
} else if (!strncmp(*av, "delete", strlen(*av))) {
|
|
|
|
delete(ac,av);
|
|
|
|
} else if (!strncmp(*av, "flush", strlen(*av))) {
|
1996-08-31 17:58:23 +00:00
|
|
|
int do_flush = 0;
|
|
|
|
|
1997-02-10 15:36:54 +00:00
|
|
|
if ( do_force || do_quiet )
|
1996-08-31 17:58:23 +00:00
|
|
|
do_flush = 1;
|
|
|
|
else {
|
|
|
|
int c;
|
|
|
|
|
|
|
|
/* Ask the user */
|
|
|
|
printf("Are you sure? [yn] ");
|
|
|
|
do {
|
1997-12-26 03:24:26 +00:00
|
|
|
fflush(stdout);
|
1996-08-31 17:58:23 +00:00
|
|
|
c = toupper(getc(stdin));
|
|
|
|
while (c != '\n' && getc(stdin) != '\n')
|
|
|
|
if (feof(stdin))
|
|
|
|
return (0);
|
|
|
|
} while (c != 'Y' && c != 'N');
|
|
|
|
printf("\n");
|
|
|
|
if (c == 'Y')
|
|
|
|
do_flush = 1;
|
|
|
|
}
|
|
|
|
if ( do_flush ) {
|
1997-06-02 05:02:37 +00:00
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_FLUSH,NULL,0) < 0)
|
1998-01-07 02:23:04 +00:00
|
|
|
err(EX_UNAVAILABLE, "setsockopt(%s)", "IP_FW_FLUSH");
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
|
|
|
printf("Flushed all rules.\n");
|
1996-08-31 17:58:23 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "zero", strlen(*av))) {
|
1996-06-09 23:46:22 +00:00
|
|
|
zero(ac,av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "print", strlen(*av))) {
|
|
|
|
list(--ac,++av);
|
|
|
|
} else if (!strncmp(*av, "list", strlen(*av))) {
|
|
|
|
list(--ac,++av);
|
1997-02-10 15:36:54 +00:00
|
|
|
} else if (!strncmp(*av, "show", strlen(*av))) {
|
|
|
|
do_acct++;
|
|
|
|
list(--ac,++av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else {
|
1996-05-11 20:31:55 +00:00
|
|
|
show_usage("Bad arguments");
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1996-06-23 20:47:51 +00:00
|
|
|
return 0;
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-23 15:52:28 +00:00
|
|
|
int
|
|
|
|
main(ac, av)
|
1995-10-23 03:58:06 +00:00
|
|
|
int ac;
|
|
|
|
char **av;
|
|
|
|
{
|
|
|
|
#define MAX_ARGS 32
|
1997-06-02 05:02:37 +00:00
|
|
|
#define WHITESP " \t\f\v\n\r"
|
1996-02-24 13:39:46 +00:00
|
|
|
char buf[BUFSIZ];
|
1998-11-23 10:54:28 +00:00
|
|
|
char *a, *p, *args[MAX_ARGS], *cmd = NULL;
|
1995-10-23 03:58:06 +00:00
|
|
|
char linename[10];
|
1998-11-23 10:54:28 +00:00
|
|
|
int i, c, qflag, pflag, status;
|
|
|
|
FILE *f = NULL;
|
|
|
|
pid_t preproc = 0;
|
1995-10-23 03:58:06 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
s = socket( AF_INET, SOCK_RAW, IPPROTO_RAW );
|
1997-06-02 05:02:37 +00:00
|
|
|
if ( s < 0 )
|
1998-01-07 02:23:04 +00:00
|
|
|
err(EX_UNAVAILABLE, "socket");
|
1996-02-24 00:20:56 +00:00
|
|
|
|
|
|
|
setbuf(stdout,0);
|
|
|
|
|
1998-11-23 10:54:28 +00:00
|
|
|
if (ac > 1 && access(av[ac - 1], R_OK) == 0) {
|
|
|
|
qflag = pflag = i = 0;
|
1996-02-24 00:20:56 +00:00
|
|
|
lineno = 0;
|
1995-10-23 03:58:06 +00:00
|
|
|
|
1998-11-23 10:54:28 +00:00
|
|
|
while ((c = getopt(ac, av, "D:U:p:q")) != -1)
|
|
|
|
switch(c) {
|
|
|
|
case 'D':
|
|
|
|
if (!pflag)
|
|
|
|
errx(EX_USAGE, "-D requires -p");
|
|
|
|
if (i > MAX_ARGS - 2)
|
|
|
|
errx(EX_USAGE,
|
|
|
|
"too many -D or -U options");
|
|
|
|
args[i++] = "-D";
|
|
|
|
args[i++] = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
if (!pflag)
|
|
|
|
errx(EX_USAGE, "-U requires -p");
|
|
|
|
if (i > MAX_ARGS - 2)
|
|
|
|
errx(EX_USAGE,
|
|
|
|
"too many -D or -U options");
|
|
|
|
args[i++] = "-U";
|
|
|
|
args[i++] = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
cmd = optarg;
|
|
|
|
args[0] = cmd;
|
|
|
|
i = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
qflag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
show_usage(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
av += optind;
|
|
|
|
ac -= optind;
|
|
|
|
if (ac != 1)
|
|
|
|
show_usage("extraneous filename arguments");
|
|
|
|
|
|
|
|
if ((f = fopen(av[0], "r")) == NULL)
|
|
|
|
err(EX_UNAVAILABLE, "fopen: %s", av[0]);
|
|
|
|
|
|
|
|
if (pflag) {
|
|
|
|
/* pipe through preprocessor (cpp or m4) */
|
|
|
|
int pipedes[2];
|
|
|
|
|
|
|
|
args[i] = 0;
|
|
|
|
|
|
|
|
if (pipe(pipedes) == -1)
|
|
|
|
err(EX_OSERR, "cannot create pipe");
|
|
|
|
|
|
|
|
switch((preproc = fork())) {
|
|
|
|
case -1:
|
|
|
|
err(EX_OSERR, "cannot fork");
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
/* child */
|
|
|
|
if (dup2(fileno(f), 0) == -1 ||
|
|
|
|
dup2(pipedes[1], 1) == -1)
|
|
|
|
err(EX_OSERR, "dup2()");
|
|
|
|
fclose(f);
|
|
|
|
close(pipedes[1]);
|
|
|
|
close(pipedes[0]);
|
|
|
|
execvp(cmd, args);
|
|
|
|
err(EX_OSERR, "execvp(%s) failed", cmd);
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* parent */
|
|
|
|
fclose(f);
|
|
|
|
close(pipedes[1]);
|
|
|
|
if ((f = fdopen(pipedes[0], "r")) == NULL) {
|
|
|
|
int savederrno = errno;
|
|
|
|
|
|
|
|
(void)kill(preproc, SIGTERM);
|
|
|
|
errno = savederrno;
|
|
|
|
err(EX_OSERR, "fdopen()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(buf, BUFSIZ, f)) {
|
1995-10-23 03:58:06 +00:00
|
|
|
lineno++;
|
|
|
|
sprintf(linename, "Line %d", lineno);
|
|
|
|
args[0] = linename;
|
|
|
|
|
1997-12-05 02:43:26 +00:00
|
|
|
if (*buf == '#')
|
|
|
|
continue;
|
1998-04-22 06:20:20 +00:00
|
|
|
if ((p = strchr(buf, '#')) != NULL)
|
|
|
|
*p = '\0';
|
1998-08-04 14:41:37 +00:00
|
|
|
i=1;
|
|
|
|
if (qflag) args[i++]="-q";
|
|
|
|
for (a = strtok(buf, WHITESP);
|
1997-06-02 05:02:37 +00:00
|
|
|
a && i < MAX_ARGS; a = strtok(NULL, WHITESP), i++)
|
|
|
|
args[i] = a;
|
1998-11-23 10:54:28 +00:00
|
|
|
if (i == (qflag? 2: 1))
|
1997-12-05 02:43:26 +00:00
|
|
|
continue;
|
1997-06-02 05:02:37 +00:00
|
|
|
if (i == MAX_ARGS)
|
1998-01-07 02:23:04 +00:00
|
|
|
errx(EX_USAGE, "%s: too many arguments", linename);
|
1995-10-23 03:58:06 +00:00
|
|
|
args[i] = NULL;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1995-10-23 03:58:06 +00:00
|
|
|
ipfw_main(i, args);
|
|
|
|
}
|
|
|
|
fclose(f);
|
1998-11-23 10:54:28 +00:00
|
|
|
if (pflag) {
|
|
|
|
if (waitpid(preproc, &status, 0) != -1) {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status) != EX_OK)
|
|
|
|
errx(EX_UNAVAILABLE,
|
|
|
|
"preprocessor exited with status %d",
|
|
|
|
WEXITSTATUS(status));
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
errx(EX_UNAVAILABLE,
|
|
|
|
"preprocessor exited with signal %d",
|
|
|
|
WTERMSIG(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-10-23 03:58:06 +00:00
|
|
|
} else
|
|
|
|
ipfw_main(ac,av);
|
1998-01-07 02:23:04 +00:00
|
|
|
return EX_OK;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|