1994-10-28 15:06:53 +00:00
|
|
|
/*
|
1996-02-24 00:20:56 +00:00
|
|
|
* Copyright (c) 1996 Poul-Henning Kamp
|
1994-10-28 15:06:53 +00:00
|
|
|
* Copyright (c) 1994 Ugen J.S.Antsilevich
|
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
|
|
|
*
|
1996-04-02 11:43:28 +00:00
|
|
|
* $Id: ipfw.c,v 1.21 1996/02/24 13:39:46 phk Exp $
|
1995-08-22 00:38:02 +00:00
|
|
|
*
|
1994-10-28 15:06:53 +00:00
|
|
|
*/
|
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
#include <stdio.h>
|
1996-02-24 00:20:56 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1995-10-23 03:58:06 +00:00
|
|
|
#include <unistd.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1994-11-16 10:18:18 +00:00
|
|
|
#include <netdb.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <limits.h>
|
1996-02-23 15:52:28 +00:00
|
|
|
#include <sys/queue.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <sys/socket.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-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
|
|
|
|
|
|
|
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;
|
|
|
|
int i,mb;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
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-02-23 15:52:28 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ACCEPT)
|
1996-02-24 00:20:56 +00:00
|
|
|
printf("accept");
|
1996-02-23 15:52:28 +00:00
|
|
|
else if (chain->fw_flg & IP_FW_F_ICMPRPL)
|
1996-02-24 00:20:56 +00:00
|
|
|
printf("reject");
|
|
|
|
else if (chain->fw_flg & IP_FW_F_COUNT)
|
|
|
|
printf("count");
|
|
|
|
else
|
|
|
|
printf("deny");
|
|
|
|
if (chain->fw_flg & IP_FW_F_PRN)
|
|
|
|
printf(" log");
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1994-12-13 15:56:51 +00:00
|
|
|
switch (chain->fw_flg & IP_FW_F_KIND) {
|
1994-11-16 10:18:18 +00:00
|
|
|
case IP_FW_F_ICMP:
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(" icmp ");
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case IP_FW_F_TCP:
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(" tcp ");
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case IP_FW_F_UDP:
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(" udp ");
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case IP_FW_F_ALL:
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(" all ");
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("from ");
|
|
|
|
|
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
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = " ";
|
1994-12-13 15:56:51 +00:00
|
|
|
for (i=0;i<chain->fw_nsp; i++ ) {
|
|
|
|
printf("%s%d",comma,chain->fw_pts[i]);
|
1995-05-30 06:12:45 +00:00
|
|
|
if (i==0 && (chain->fw_flg & IP_FW_F_SRNG))
|
1996-02-24 00:20:56 +00:00
|
|
|
comma = "-";
|
1995-05-30 06:12:45 +00:00
|
|
|
else
|
1994-11-16 10:18:18 +00:00
|
|
|
comma = ",";
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1994-11-16 10:18:18 +00:00
|
|
|
printf(" to ");
|
|
|
|
|
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++) {
|
|
|
|
printf("%s%d",comma,chain->fw_pts[chain->fw_nsp+i]);
|
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)
|
|
|
|
printf(" in ");
|
|
|
|
else if (chain->fw_flg & IP_FW_F_OUT)
|
|
|
|
printf(" out ");
|
|
|
|
|
|
|
|
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';
|
|
|
|
printf("%s%d",ifnb,chain->fw_via_unit);
|
|
|
|
} 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)
|
|
|
|
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-02-24 00:20:56 +00:00
|
|
|
if (chain->fw_tcpf == IP_FW_TCPF_SYN &&
|
|
|
|
chain->fw_tcpnf == IP_FW_TCPF_ACK)
|
1996-02-24 13:39:46 +00:00
|
|
|
printf(" setup");
|
|
|
|
else if (chain->fw_tcpnf == IP_FW_TCPF_SYN && !chain->fw_tcpf)
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(" established");
|
|
|
|
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
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("\n");
|
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)");
|
|
|
|
printf("FireWall chain entries: %d %d\n",l,i);
|
|
|
|
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);
|
|
|
|
else
|
|
|
|
fprintf(stderr,"%s: ERROR - bad arguments\n",progname);
|
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"
|
|
|
|
"\t\tzero [number]\n"
|
|
|
|
"\trule:\taction proto src dst extras...\n"
|
1996-04-02 11:43:28 +00:00
|
|
|
"\t\taction: {allow|deny|reject|count} [log]\n"
|
1996-02-24 00:20:56 +00:00
|
|
|
"\t\tproto: {ip|tcp|udp|icmp}}\n"
|
|
|
|
"\t\tsrc: {any|ip[{/bits|:mask}]} [{port|port-port},...]\n"
|
|
|
|
"\t\tdst: {any|ip[{/bits|:mask}]} [{port|port-port},...]\n"
|
|
|
|
"\textras:\n"
|
|
|
|
"\t\tfragment\n"
|
|
|
|
"\t\t{in|out|inout}\n"
|
|
|
|
"\t\tvia {ifname|ip}\n"
|
|
|
|
"\t\ttcpflags [!]{syn|fin|rst|ack|psh},...\n"
|
|
|
|
, 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-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';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inet_aton(*av,ipno))
|
|
|
|
show_usage("ip number\n");
|
|
|
|
if (md == ':' && !inet_aton(p,mask))
|
|
|
|
show_usage("ip number\n");
|
|
|
|
else if (md == '/')
|
|
|
|
mask->s_addr = htonl(0xffffffff << (32 - atoi(p)));
|
|
|
|
else
|
|
|
|
mask->s_addr = htonl(0xffffffff);
|
|
|
|
av++;
|
|
|
|
ac--;
|
|
|
|
}
|
|
|
|
*acp = ac;
|
|
|
|
*avp = 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
|
|
|
int
|
|
|
|
fill_port(cnt, ptr, off, av)
|
|
|
|
u_short *cnt, *ptr, off;
|
|
|
|
char **av;
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
char *s, sc = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
s = strchr(*av,'-');
|
|
|
|
if (s) {
|
|
|
|
sc = *s;
|
|
|
|
*s++ = '\0';
|
|
|
|
ptr[off+*cnt] = atoi(*av);
|
|
|
|
(*cnt)++;
|
|
|
|
*av = s;
|
|
|
|
s = strchr(*av,',');
|
|
|
|
if (s) {
|
|
|
|
sc = *s;
|
|
|
|
*s++ = '\0';
|
|
|
|
} else
|
|
|
|
sc = '\0';
|
|
|
|
ptr[off+*cnt] = atoi(*av);
|
|
|
|
(*cnt)++;
|
|
|
|
if (sc && sc != ',') show_usage("Expected comma\n");
|
|
|
|
*av = s;
|
|
|
|
sc = 0;
|
|
|
|
i = 1;
|
|
|
|
}
|
1996-04-02 11:43:28 +00:00
|
|
|
while (*av != NULL) {
|
1996-02-24 00:20:56 +00:00
|
|
|
s = strchr(*av,',');
|
|
|
|
if (s) {
|
|
|
|
sc = *s;
|
|
|
|
*s++ = '\0';
|
|
|
|
} else
|
|
|
|
sc = '\0';
|
|
|
|
ptr[off+*cnt] = atoi(*av);
|
|
|
|
(*cnt)++;
|
|
|
|
if (!sc)
|
|
|
|
break;
|
|
|
|
if (sc != ',') show_usage("Expected comma\n");
|
|
|
|
*av = s;
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
return i;
|
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) {
|
|
|
|
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';
|
|
|
|
if (!strncmp(p,"syn",strlen(p))) *d |= IP_FW_TCPF_SYN;
|
|
|
|
if (!strncmp(p,"fin",strlen(p))) *d |= IP_FW_TCPF_FIN;
|
|
|
|
if (!strncmp(p,"ack",strlen(p))) *d |= IP_FW_TCPF_ACK;
|
|
|
|
if (!strncmp(p,"psh",strlen(p))) *d |= IP_FW_TCPF_PSH;
|
|
|
|
if (!strncmp(p,"rst",strlen(p))) *d |= IP_FW_TCPF_RST;
|
|
|
|
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-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)
|
|
|
|
err(1,"setsockopt(Add)");
|
|
|
|
}
|
1995-02-24 14:32:45 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
memset(&rule, 0, sizeof rule);
|
|
|
|
|
|
|
|
av++; ac--;
|
|
|
|
|
|
|
|
/* Rule number */
|
|
|
|
if (ac && isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Action */
|
|
|
|
if (ac && !strncmp(*av,"accept",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_ACCEPT; av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"pass",strlen(*av))) {
|
|
|
|
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--;
|
|
|
|
} else if (ac && !strncmp(*av,"deny",strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"reject",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_ICMPRPL; av++; ac--;
|
|
|
|
} else {
|
|
|
|
show_usage("missing 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 */
|
|
|
|
if (ac && !strncmp(*av,"ip",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_ALL; av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"all",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_ALL; av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"tcp",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_TCP; av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"udp",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_UDP; av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av,"icmp",strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_ICMP; av++; ac--;
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
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)) {
|
|
|
|
if (fill_port(&rule.fw_nsp, &rule.fw_pts, 0, av))
|
|
|
|
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
|
|
|
|
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)) {
|
|
|
|
if (fill_port(&rule.fw_ndp, &rule.fw_pts, rule.fw_nsp, av))
|
|
|
|
rule.fw_flg |= IP_FW_F_DRNG;
|
|
|
|
av++; ac--;
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 13:39:46 +00:00
|
|
|
if (ac && !strncmp(*av,"via",strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
if (!isdigit(**av)) {
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
strcpy(rule.fw_via_name, *av);
|
|
|
|
for (q = rule.fw_via_name; *q && !isdigit(*q); q++)
|
|
|
|
continue;
|
|
|
|
rule.fw_via_unit = atoi(q);
|
|
|
|
*q = '\0';
|
|
|
|
rule.fw_flg |= IP_FW_F_IFNAME;
|
|
|
|
} else if (inet_aton(*av,&rule.fw_via_ip) == INADDR_NONE) {
|
|
|
|
show_usage("bad IP# after via\n");
|
|
|
|
}
|
|
|
|
av++; ac--;
|
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
while (ac) {
|
|
|
|
if (!strncmp(*av,"frag",strlen(*av))) {
|
|
|
|
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-02-24 13:39:46 +00:00
|
|
|
if ((rule.fw_flg & IP_FW_F_KIND) == IP_FW_F_TCP) {
|
|
|
|
if (!strncmp(*av,"established",strlen(*av))) {
|
|
|
|
rule.fw_tcpnf |= IP_FW_TCPF_SYN;
|
|
|
|
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-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
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
show_ipfw(&rule);
|
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_FW_ADD, &rule, sizeof rule);
|
|
|
|
if (i)
|
|
|
|
err(1,"setsockopt(Delete)");
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
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-02-24 13:39:46 +00:00
|
|
|
while ((ch = getopt(ac, av ,"aN")) != 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-02-24 13:39:46 +00:00
|
|
|
case 'N':
|
1996-02-24 00:20:56 +00:00
|
|
|
do_resolv=1;
|
1994-11-16 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
1995-03-03 12:28:34 +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) {
|
1995-03-03 12:28:34 +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
|
|
|
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))) {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_FLUSH,NULL,0)<0) {
|
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
1994-11-16 10:18:18 +00:00
|
|
|
exit(1);
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
|
|
|
printf("Flushed all rules.\n");
|
|
|
|
} else if (!strncmp(*av, "zero", strlen(*av))) {
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_FW_ZERO,NULL,0)<0) {
|
1994-11-16 10:18:18 +00:00
|
|
|
fprintf(stderr,"%s: setsockopt failed.\n",progname);
|
|
|
|
exit(1);
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
|
|
|
printf("Accounting cleared.\n");
|
|
|
|
} else if (!strncmp(*av, "print", strlen(*av))) {
|
|
|
|
list(--ac,++av);
|
|
|
|
} else if (!strncmp(*av, "list", strlen(*av))) {
|
|
|
|
list(--ac,++av);
|
|
|
|
} else {
|
|
|
|
show_usage(NULL);
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +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;
|
|
|
|
|
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"
|
|
|
|
"Must be root to use this programm. \n",progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
setbuf(stdout,0);
|
|
|
|
|
|
|
|
strcpy(progname,*av);
|
|
|
|
|
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
|
|
|
}
|