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
|
|
|
*
|
1997-03-05 12:08:44 +00:00
|
|
|
* $Id: ipfw.c,v 1.40 1997/02/22 14:32:36 peter 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>
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <limits.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.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>
|
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
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
int lineno = -1;
|
1996-02-24 13:39:46 +00:00
|
|
|
char progname[BUFSIZ]; /* Program name for errors */
|
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
|
|
|
|
|
|
|
int
|
|
|
|
mask_bits(m_ad)
|
|
|
|
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
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1994-10-28 15:06:53 +00:00
|
|
|
void
|
1996-02-24 00:20:56 +00:00
|
|
|
show_ipfw(chain)
|
|
|
|
struct ip_fw *chain;
|
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;
|
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)
|
|
|
|
printf("%10lu %10lu ",chain->fw_pcnt,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;
|
|
|
|
case IP_FW_F_DIVERT:
|
|
|
|
printf("divert %u", chain->fw_divert_port);
|
|
|
|
break;
|
|
|
|
case IP_FW_F_COUNT:
|
|
|
|
printf("count");
|
|
|
|
break;
|
|
|
|
case IP_FW_F_DENY:
|
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPRPL)
|
|
|
|
printf("reject");
|
|
|
|
else
|
|
|
|
printf("deny");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "impossible");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
1996-08-13 19:43:24 +00:00
|
|
|
if (chain->fw_prot != IPPROTO_IP) {
|
1996-08-05 02:38:51 +00:00
|
|
|
comma = " ";
|
|
|
|
for (i=0;i<chain->fw_nsp; i++ ) {
|
1996-08-13 19:43:24 +00:00
|
|
|
print_port(chain->fw_prot, chain->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
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = " ";
|
1994-12-13 15:56:51 +00:00
|
|
|
for (i=0;i<chain->fw_ndp;i++) {
|
1996-08-13 19:43:24 +00:00
|
|
|
print_port(chain->fw_prot, chain->fw_pts[chain->fw_nsp+i],
|
|
|
|
comma);
|
1996-04-02 11:43:28 +00:00
|
|
|
if (i==0 && (chain->fw_flg & IP_FW_F_DRNG))
|
1996-02-24 00:20:56 +00:00
|
|
|
comma = "-";
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-10-28 15:06:53 +00:00
|
|
|
comma = ",";
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if ((chain->fw_flg & IP_FW_F_IN) && (chain->fw_flg & IP_FW_F_OUT))
|
|
|
|
;
|
|
|
|
else if (chain->fw_flg & IP_FW_F_IN)
|
1996-06-29 01:28:19 +00:00
|
|
|
printf(" in");
|
1996-02-24 00:20:56 +00:00
|
|
|
else if (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
|
|
|
|
|
|
|
if (chain->fw_flg&IP_FW_F_IFNAME && chain->fw_via_name[0]) {
|
|
|
|
char ifnb[FW_IFNLEN+1];
|
1995-02-24 14:32:45 +00:00
|
|
|
printf(" via ");
|
1996-02-24 00:20:56 +00:00
|
|
|
strncpy(ifnb,chain->fw_via_name,FW_IFNLEN);
|
|
|
|
ifnb[FW_IFNLEN]='\0';
|
1996-06-09 23:46:22 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_IFUWILD)
|
|
|
|
printf("%s*",ifnb);
|
|
|
|
else
|
|
|
|
printf("%s%d",ifnb,chain->fw_via_unit);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (chain->fw_via_ip.s_addr) {
|
1994-12-12 17:19:33 +00:00
|
|
|
printf(" via ");
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(inet_ntoa(chain->fw_via_ip));
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
for (type_index = 0; type_index < 256; ++type_index)
|
|
|
|
if (chain->fw_icmptypes[type_index / (sizeof(unsigned) * 8)] &
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
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];
|
|
|
|
int l,i;
|
|
|
|
|
|
|
|
memset(rules,0,sizeof rules);
|
|
|
|
l = sizeof rules;
|
|
|
|
i = getsockopt(s, IPPROTO_IP, IP_FW_GET, rules, &l);
|
|
|
|
if (i < 0)
|
|
|
|
err(2,"getsockopt(IP_FW_GET)");
|
|
|
|
for (r=rules; l >= sizeof rules[0]; r++, l-=sizeof rules[0])
|
|
|
|
show_ipfw(r);
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
show_usage(str)
|
|
|
|
char *str;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1995-03-03 12:28:34 +00:00
|
|
|
if (str)
|
|
|
|
fprintf(stderr,"%s: ERROR - %s\n",progname,str);
|
1996-02-24 00:20:56 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Usage:\n"
|
|
|
|
"\t%s [options]\n"
|
|
|
|
"\t\tflush\n"
|
|
|
|
"\t\tadd [number] rule\n"
|
|
|
|
"\t\tdelete number\n"
|
|
|
|
"\t\tlist [number]\n"
|
1997-02-10 15:36:54 +00:00
|
|
|
"\t\tshow [number]\n"
|
1996-02-24 00:20:56 +00:00
|
|
|
"\t\tzero [number]\n"
|
|
|
|
"\trule:\taction proto src dst extras...\n"
|
1996-07-10 19:44:30 +00:00
|
|
|
"\t\taction: {allow|deny|reject|count|divert port} [log]\n"
|
1996-08-13 19:43:24 +00:00
|
|
|
"\t\tproto: {ip|tcp|udp|icmp|<number>}}\n"
|
1996-06-09 23:46:22 +00:00
|
|
|
"\t\tsrc: from {any|ip[{/bits|:mask}]} [{port|port-port},[port],...]\n"
|
|
|
|
"\t\tdst: to {any|ip[{/bits|:mask}]} [{port|port-port},[port],...]\n"
|
1996-02-24 00:20:56 +00:00
|
|
|
"\textras:\n"
|
|
|
|
"\t\tfragment\n"
|
|
|
|
"\t\t{in|out|inout}\n"
|
|
|
|
"\t\tvia {ifname|ip}\n"
|
1996-05-11 20:31:55 +00:00
|
|
|
"\t\t{established|setup}\n"
|
1996-06-09 23:46:22 +00:00
|
|
|
"\t\ttcpflags [!]{syn|fin|rst|ack|psh|urg},...\n"
|
1996-05-11 20:31:55 +00:00
|
|
|
"\t\tipoptions [!]{ssrr|lsrr|rr|ts},...\n"
|
1996-06-09 23:46:22 +00:00
|
|
|
"\t\ticmptypes {type},...\n"
|
1996-08-05 02:38:51 +00:00
|
|
|
"\t\tproto {ipproto},...\n"
|
1996-02-24 00:20:56 +00:00
|
|
|
, progname
|
|
|
|
);
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
|
|
|
|
fprintf(stderr,"See man %s(8) for proper usage.\n",progname);
|
|
|
|
exit (1);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
int
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
if (lookup_host(*av,ipno) != 0)
|
1996-02-24 00:20:56 +00:00
|
|
|
show_usage("ip number\n");
|
1996-06-23 20:47:51 +00:00
|
|
|
switch (md) {
|
|
|
|
case ':':
|
|
|
|
if (!inet_aton(p,mask))
|
|
|
|
show_usage("ip number\n");
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (atoi(p) == 0) {
|
|
|
|
mask->s_addr = 0;
|
|
|
|
} else {
|
|
|
|
mask->s_addr = htonl(0xffffffff << (32 - atoi(p)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mask->s_addr = htonl(0xffffffff);
|
|
|
|
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
|
|
|
|
1996-05-11 20:31:55 +00:00
|
|
|
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)
|
1996-08-13 19:43:24 +00:00
|
|
|
errx(1, "too many ports (max is %d)", IP_FW_MAX_PORTS);
|
1996-05-11 20:31:55 +00:00
|
|
|
ptr[off+*cnt] = port;
|
|
|
|
(*cnt)++;
|
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
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
|
|
|
|
1996-05-11 20:31:55 +00:00
|
|
|
s = strchr(arg,'-');
|
1996-02-24 00:20:56 +00:00
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
1996-05-11 20:31:55 +00:00
|
|
|
if (strchr(arg, ','))
|
|
|
|
errx(1, "port range must be first in list");
|
1996-08-13 19:43:24 +00:00
|
|
|
add_port(cnt, ptr, off, *arg ? atoi(arg) : 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';
|
1996-08-13 19:43:24 +00:00
|
|
|
add_port(cnt, ptr, off, *arg ? atoi(arg) : 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';
|
1996-08-13 19:43:24 +00:00
|
|
|
add_port(cnt, ptr, off, atoi(arg));
|
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
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
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]))
|
|
|
|
show_usage("invalid tcp flag\n");
|
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
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
fill_ipopt(set, reset, vp)
|
|
|
|
u_char *set, *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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
void
|
|
|
|
fill_icmptypes(types, vp, fw_flg)
|
|
|
|
u_long *types;
|
|
|
|
char **vp;
|
|
|
|
u_short *fw_flg;
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
|
|
|
|
if (icmptype > 255)
|
|
|
|
show_usage("ICMP types are between 0 and 255 inclusive");
|
|
|
|
|
|
|
|
types[icmptype / (sizeof(unsigned) * 8)] |=
|
|
|
|
1 << (icmptype % (sizeof(unsigned) * 8));
|
|
|
|
*fw_flg |= IP_FW_F_ICMPBIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
if (ac && isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_FW_DEL, &rule, sizeof rule);
|
|
|
|
if (i)
|
1996-05-11 20:31:55 +00:00
|
|
|
err(1,"setsockopt(IP_FW_DEL)");
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
1995-02-24 14:32:45 +00:00
|
|
|
|
1996-10-17 01:05:03 +00:00
|
|
|
int
|
|
|
|
verify_interface(rule)
|
|
|
|
struct ip_fw *rule;
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
rule->fw_via_name,
|
|
|
|
rule->fw_flg & IP_FW_F_IFUWILD ? 0 : rule->fw_via_unit);
|
|
|
|
|
|
|
|
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
|
|
|
|
return(-1); /* interface isn't recognized by the kernel */
|
|
|
|
|
|
|
|
return(0); /* interface exists */
|
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
void
|
|
|
|
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;
|
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 */
|
1996-07-10 19:44:30 +00:00
|
|
|
if (ac && (!strncmp(*av,"accept",strlen(*av))
|
|
|
|
|| !strncmp(*av,"pass",strlen(*av))
|
|
|
|
|| !strncmp(*av,"allow",strlen(*av))
|
|
|
|
|| !strncmp(*av,"permit",strlen(*av)))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_ACCEPT; av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"count",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_COUNT; av++; ac--;
|
1996-07-10 19:44:30 +00:00
|
|
|
} else if (ac && !strncmp(*av,"divert",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_DIVERT; av++; ac--;
|
|
|
|
if (!ac)
|
|
|
|
show_usage("missing divert port");
|
|
|
|
rule.fw_divert_port = strtoul(*av, NULL, 0); av++; ac--;
|
|
|
|
if (rule.fw_divert_port == 0)
|
|
|
|
show_usage("illegal divert port");
|
|
|
|
} else if (ac && (!strncmp(*av,"deny",strlen(*av)))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_DENY; av++; ac--;
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (ac && !strncmp(*av,"reject",strlen(*av))) {
|
1996-07-10 19:44:30 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DENY|IP_FW_F_ICMPRPL; av++; ac--;
|
1996-02-24 00:20:56 +00:00
|
|
|
} else {
|
1996-07-10 19:44:30 +00:00
|
|
|
show_usage("missing/unrecognized action\n");
|
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 */
|
1996-08-13 19:43:24 +00:00
|
|
|
if (ac) {
|
|
|
|
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\n");
|
|
|
|
}
|
|
|
|
} else
|
1996-02-24 00:20:56 +00:00
|
|
|
show_usage("missing protocol\n");
|
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--; }
|
|
|
|
else show_usage("missing ``from''\n");
|
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--;
|
|
|
|
}
|
|
|
|
if (!ac) show_usage("Missing arguments\n");
|
|
|
|
|
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
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (ac && isdigit(**av)) {
|
1996-08-05 02:38:51 +00:00
|
|
|
if (fill_port(&rule.fw_nsp, &rule.fw_pts, 0, *av, 0))
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_SRNG;
|
|
|
|
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--; }
|
|
|
|
else show_usage("missing ``to''\n");
|
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--;
|
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
if (!ac) show_usage("Missing arguments\n");
|
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
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
if (ac && isdigit(**av)) {
|
1996-08-05 02:38:51 +00:00
|
|
|
if (fill_port(&rule.fw_ndp, &rule.fw_pts, rule.fw_nsp, *av, 0))
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DRNG;
|
|
|
|
av++; ac--;
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
|
|
|
|
1996-08-13 19:43:24 +00:00
|
|
|
if ((rule.fw_prot != IPPROTO_TCP) &&
|
|
|
|
(rule.fw_prot != IPPROTO_UDP) &&
|
|
|
|
(rule.fw_nsp || rule.fw_ndp)) {
|
1996-06-29 01:28:19 +00:00
|
|
|
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) {
|
1996-06-29 01:28:19 +00:00
|
|
|
if (ac && !strncmp(*av,"via",strlen(*av))) {
|
|
|
|
if (rule.fw_via_ip.s_addr || (rule.fw_flg & IP_FW_F_IFNAME)) {
|
|
|
|
show_usage("multiple 'via' options specified");
|
|
|
|
}
|
|
|
|
|
|
|
|
av++; ac--;
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!ac) {
|
|
|
|
show_usage("'via' option specified with no interface.");
|
|
|
|
}
|
1996-06-29 01:28:19 +00:00
|
|
|
if (!isdigit(**av)) {
|
|
|
|
char *q;
|
|
|
|
|
1997-02-10 15:36:54 +00:00
|
|
|
strncpy(rule.fw_via_name, *av, sizeof(rule.fw_via_name));
|
1997-03-05 12:08:44 +00:00
|
|
|
rule.fw_via_name[sizeof(rule.fw_via_name) - 1] = '\0';
|
1996-06-29 01:28:19 +00:00
|
|
|
for (q = rule.fw_via_name; *q && !isdigit(*q) && *q != '*'; q++)
|
|
|
|
continue;
|
|
|
|
if (*q == '*')
|
1996-08-05 02:38:51 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_IFUWILD;
|
1996-06-29 01:28:19 +00:00
|
|
|
else
|
|
|
|
rule.fw_via_unit = atoi(q);
|
|
|
|
*q = '\0';
|
|
|
|
rule.fw_flg |= IP_FW_F_IFNAME;
|
1996-10-17 01:05:03 +00:00
|
|
|
if (verify_interface(&rule) != 0)
|
|
|
|
fprintf(stderr, "Warning: interface does not exist\n");
|
1996-06-29 01:28:19 +00:00
|
|
|
} else if (inet_aton(*av,&rule.fw_via_ip) == INADDR_NONE) {
|
|
|
|
show_usage("bad IP# after via\n");
|
|
|
|
}
|
|
|
|
av++; ac--;
|
|
|
|
continue;
|
|
|
|
}
|
1996-05-11 20:31:55 +00:00
|
|
|
if (!strncmp(*av,"fragment",strlen(*av))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_FRAG; av++; ac--; continue;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
if (!strncmp(*av,"in",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_IN; av++; ac--; continue;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
if (!strncmp(*av,"out",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_OUT; av++; ac--; continue;
|
1995-03-03 12:28:34 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
if (ac > 1 && !strncmp(*av,"ipoptions",strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (ac > 1 && !strncmp(*av,"tcpflags",strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
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) {
|
1996-06-09 23:46:22 +00:00
|
|
|
if (ac > 1 && !strncmp(*av,"icmptypes",strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
fill_icmptypes(rule.fw_icmptypes, av, &rule.fw_flg);
|
|
|
|
av++; ac--; continue;
|
|
|
|
}
|
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
printf("%d %s\n",ac,*av);
|
|
|
|
show_usage("Unknown argument\n");
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
|
|
|
show_ipfw(&rule);
|
1996-02-24 00:20:56 +00:00
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_FW_ADD, &rule, sizeof rule);
|
|
|
|
if (i)
|
1996-05-11 20:31:55 +00:00
|
|
|
err(1,"setsockopt(IP_FW_ADD)");
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
void
|
|
|
|
zero (ac, av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
|
|
|
{
|
|
|
|
av++; ac--;
|
|
|
|
|
|
|
|
if (!ac) {
|
|
|
|
/* clear all entries */
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_ZERO,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
|
|
|
printf("Accounting cleared.\n");
|
1996-06-09 23:46:22 +00:00
|
|
|
} else {
|
|
|
|
/* clear a specific entry */
|
|
|
|
struct ip_fw rule;
|
|
|
|
|
|
|
|
memset(&rule, 0, sizeof rule);
|
|
|
|
|
|
|
|
/* Rule number */
|
|
|
|
if (isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
|
|
|
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_FW_ZERO, &rule, sizeof rule))
|
|
|
|
err(1, "setsockopt(Zero)");
|
|
|
|
printf("Entry %d cleared\n", rule.fw_number);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
show_usage("expected number");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
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
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
char ch;
|
|
|
|
extern int optind;
|
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);
|
|
|
|
|
1997-02-10 15:36:54 +00:00
|
|
|
while ((ch = getopt(ac, av ,"afqtN")) != EOF)
|
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:
|
|
|
|
show_usage("Unrecognised switch");
|
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 {
|
|
|
|
fflush(stdout);
|
|
|
|
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 ) {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_FLUSH,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
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
|
1996-02-24 13:39:46 +00:00
|
|
|
char buf[BUFSIZ];
|
1995-10-23 03:58:06 +00:00
|
|
|
char *args[MAX_ARGS];
|
|
|
|
char linename[10];
|
1996-02-24 00:20:56 +00:00
|
|
|
int i;
|
1995-10-23 03:58:06 +00:00
|
|
|
FILE *f;
|
|
|
|
|
1997-02-10 15:36:54 +00:00
|
|
|
strncpy(progname,*av, sizeof(progname));
|
1997-03-05 12:08:44 +00:00
|
|
|
progname[sizeof(progname) - 1] = '\0';
|
1996-06-18 01:46:34 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
s = socket( AF_INET, SOCK_RAW, IPPROTO_RAW );
|
|
|
|
if ( s < 0 ) {
|
|
|
|
fprintf(stderr,"%s: Can't open raw socket.\n"
|
1996-06-09 23:46:22 +00:00
|
|
|
"Must be root to use this program.\n",progname);
|
1996-02-24 00:20:56 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
setbuf(stdout,0);
|
|
|
|
|
1995-10-23 03:58:06 +00:00
|
|
|
if (av[1] && !access(av[1], R_OK)) {
|
1996-02-24 00:20:56 +00:00
|
|
|
lineno = 0;
|
1995-10-23 03:58:06 +00:00
|
|
|
f = fopen(av[1], "r");
|
1996-02-24 13:39:46 +00:00
|
|
|
while (fgets(buf, BUFSIZ, f)) {
|
1995-10-23 03:58:06 +00:00
|
|
|
if (buf[strlen(buf)-1]=='\n')
|
|
|
|
buf[strlen(buf)-1] = 0;
|
|
|
|
|
|
|
|
lineno++;
|
|
|
|
sprintf(linename, "Line %d", lineno);
|
|
|
|
args[0] = linename;
|
|
|
|
|
|
|
|
args[1] = buf;
|
|
|
|
while(*args[1] == ' ')
|
|
|
|
args[1]++;
|
|
|
|
i = 2;
|
1996-02-24 00:20:56 +00:00
|
|
|
while((args[i] = strchr(args[i-1],' '))) {
|
1995-10-23 03:58:06 +00:00
|
|
|
*(args[i]++) = 0;
|
|
|
|
while(*args[i] == ' ')
|
|
|
|
args[i]++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (*args[i-1] == 0)
|
|
|
|
i--;
|
|
|
|
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);
|
|
|
|
} else
|
|
|
|
ipfw_main(ac,av);
|
1996-02-24 00:20:56 +00:00
|
|
|
return 0;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|