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
|
|
|
*
|
1994-10-28 15:06:53 +00:00
|
|
|
*/
|
|
|
|
|
1999-05-29 08:12:38 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1999-05-29 08:12:38 +00:00
|
|
|
#endif /* not lint */
|
1999-06-11 09:43:53 +00:00
|
|
|
|
1999-05-29 08:12:38 +00:00
|
|
|
|
2000-05-01 20:19:44 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mbuf.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
1999-08-01 16:57:24 +00:00
|
|
|
#include <sys/sysctl.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#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>
|
1999-06-19 18:43:33 +00:00
|
|
|
#include <grp.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <limits.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
#include <netdb.h>
|
1999-06-19 18:43:33 +00:00
|
|
|
#include <pwd.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>
|
|
|
|
#include <unistd.h>
|
1999-05-29 08:12:38 +00:00
|
|
|
#include <sysexits.h>
|
1996-12-10 17:11:53 +00:00
|
|
|
|
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.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
1996-02-24 13:39:46 +00:00
|
|
|
#include <netinet/ip_fw.h>
|
1998-12-14 18:43:03 +00:00
|
|
|
#include <net/route.h> /* def. of struct route */
|
|
|
|
#include <netinet/ip_dummynet.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
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
int s, /* main RAW socket */
|
|
|
|
do_resolv, /* Would try to resolve all */
|
|
|
|
do_acct, /* Show packet/byte count */
|
|
|
|
do_time, /* Show time stamps */
|
|
|
|
do_quiet, /* Be quiet in add and flush */
|
|
|
|
do_force, /* Don't ask for confirmation */
|
|
|
|
do_pipe, /* this cmd refers to a pipe */
|
|
|
|
do_sort, /* field to sort results (0 = no) */
|
|
|
|
do_dynamic, /* display dynamic rules */
|
|
|
|
do_expired, /* display expired dynamic rules */
|
2000-10-11 13:02:30 +00:00
|
|
|
verbose;
|
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" },
|
2001-06-04 23:56:26 +00:00
|
|
|
{ ICMP_UNREACH_PRECEDENCE_CUTOFF, "precedence-cutoff" },
|
1997-06-02 05:02:37 +00:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
2001-07-22 06:40:11 +00:00
|
|
|
static void show_usage(void);
|
1998-01-07 02:23:04 +00:00
|
|
|
|
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
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
int h_fnd = 0, h_num = 0, i;
|
1996-02-24 00:20:56 +00:00
|
|
|
u_long mask;
|
1994-11-16 10:18:18 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
mask = ntohl(m_ad.s_addr);
|
|
|
|
for (i = 0 ; i < sizeof(u_long)*CHAR_BIT; i++) {
|
1994-11-16 10:18:18 +00:00
|
|
|
if (mask & 1L) {
|
2001-06-04 23:56:26 +00:00
|
|
|
h_fnd = 1;
|
1994-11-16 10:18:18 +00:00
|
|
|
h_num++;
|
|
|
|
} else {
|
|
|
|
if (h_fnd)
|
|
|
|
return -1;
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
mask = mask >> 1;
|
1995-05-30 06:12:45 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
return h_num;
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
print_port(u_char prot, u_short port, const char comma)
|
1996-06-09 23:46:22 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
struct servent *se = NULL;
|
1996-08-13 19:43:24 +00:00
|
|
|
struct protoent *pe;
|
1996-06-09 23:46:22 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (comma == ':') {
|
|
|
|
printf("%c0x%04x", comma, port);
|
|
|
|
return;
|
2000-01-08 11:19:19 +00:00
|
|
|
}
|
1996-06-09 23:46:22 +00:00
|
|
|
if (do_resolv) {
|
1996-08-13 19:43:24 +00:00
|
|
|
pe = getprotobynumber(prot);
|
2001-06-04 23:56:26 +00:00
|
|
|
se = getservbyport(htons(port), pe ? pe->p_name : NULL);
|
|
|
|
}
|
|
|
|
if (se)
|
|
|
|
printf("%c%s", comma, se->s_name);
|
|
|
|
else
|
|
|
|
printf("%c%d", comma, port);
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2001-06-04 23:56:26 +00:00
|
|
|
ifnb[FW_IFNLEN] = '\0';
|
1997-06-02 05:02:37 +00:00
|
|
|
if (un->fu_via_if.unit == -1)
|
|
|
|
printf(" %s %s*", key, ifnb);
|
2001-06-04 23:56:26 +00:00
|
|
|
else
|
1997-06-02 05:02:37 +00:00
|
|
|
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
|
2001-06-04 23:56:26 +00:00
|
|
|
show_ipfw(struct ip_fw *chain)
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
char comma;
|
1996-02-24 00:20:56 +00:00
|
|
|
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)
|
1999-05-29 08:12:38 +00:00
|
|
|
setservent(1/*stay open*/);
|
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
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (do_acct)
|
|
|
|
printf("%10qu %10qu ", chain->fw_pcnt, chain->fw_bcnt);
|
1994-11-16 10:18:18 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (do_time) {
|
|
|
|
if (chain->timestamp) {
|
1996-06-09 23:46:22 +00:00
|
|
|
char timestr[30];
|
|
|
|
|
|
|
|
strcpy(timestr, ctime((time_t *)&chain->timestamp));
|
|
|
|
*strchr(timestr, '\n') = '\0';
|
|
|
|
printf("%s ", timestr);
|
2001-06-04 23:56:26 +00:00
|
|
|
} else {
|
|
|
|
printf(" ");
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
2000-02-10 14:25:26 +00:00
|
|
|
if (chain->fw_flg == IP_FW_F_CHECK_S) {
|
|
|
|
printf("check-state\n");
|
2001-06-04 23:56:26 +00:00
|
|
|
goto done;
|
2000-02-10 14:25:26 +00:00
|
|
|
}
|
1996-06-09 23:46:22 +00:00
|
|
|
|
2000-06-18 02:48:19 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_RND_MATCH) {
|
2001-06-04 23:56:26 +00:00
|
|
|
double d = 1.0 * (int)(chain->pipe_ptr);
|
|
|
|
d = 1 - (d / 0x7fffffff);
|
2000-06-18 02:48:19 +00:00
|
|
|
printf("prob %f ", d);
|
|
|
|
}
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
switch (chain->fw_flg & IP_FW_F_COMMAND) {
|
1996-07-10 19:44:30 +00:00
|
|
|
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;
|
2001-06-04 23:56:26 +00:00
|
|
|
case IP_FW_F_PIPE:
|
|
|
|
printf("pipe %u", chain->fw_skipto_rule);
|
|
|
|
break;
|
|
|
|
case IP_FW_F_QUEUE:
|
|
|
|
printf("queue %u", chain->fw_skipto_rule);
|
|
|
|
break;
|
1997-06-02 05:02:37 +00:00
|
|
|
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
|
|
|
}
|
1999-08-11 15:36:13 +00:00
|
|
|
|
1999-08-01 16:57:24 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_PRN) {
|
1996-02-24 00:20:56 +00:00
|
|
|
printf(" log");
|
1999-08-01 16:57:24 +00:00
|
|
|
if (chain->fw_logamount)
|
|
|
|
printf(" logamount %d", chain->fw_logamount);
|
|
|
|
}
|
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
|
|
|
|
2001-02-13 14:12:37 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_SME) {
|
|
|
|
printf(" from me");
|
|
|
|
} else {
|
2001-06-04 23:56:26 +00:00
|
|
|
printf(" from %s",
|
|
|
|
chain->fw_flg & IP_FW_F_INVSRC ? "not " : "");
|
|
|
|
|
|
|
|
adrt = ntohl(chain->fw_smsk.s_addr);
|
|
|
|
if (adrt == ULONG_MAX && do_resolv) {
|
|
|
|
adrt = (chain->fw_src.s_addr);
|
|
|
|
he = gethostbyaddr((char *)&adrt,
|
|
|
|
sizeof(u_long), AF_INET);
|
|
|
|
if (he == NULL)
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_src));
|
2001-06-04 23:56:26 +00:00
|
|
|
else
|
|
|
|
printf("%s", he->h_name);
|
|
|
|
} else if (adrt != ULONG_MAX) {
|
|
|
|
mb = mask_bits(chain->fw_smsk);
|
1995-10-23 03:58:06 +00:00
|
|
|
if (mb == 0) {
|
|
|
|
printf("any");
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (mb > 0) {
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_src));
|
2001-06-04 23:56:26 +00:00
|
|
|
printf("/%d", mb);
|
1995-10-23 03:58:06 +00:00
|
|
|
} else {
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_src));
|
2001-06-04 23:56:26 +00:00
|
|
|
printf(":");
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_smsk));
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
} else {
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_src));
|
2001-02-13 14:12:37 +00:00
|
|
|
}
|
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) {
|
2001-06-04 23:56:26 +00:00
|
|
|
comma = ' ';
|
1997-06-02 05:02:37 +00:00
|
|
|
for (i = 0; i < nsp; i++) {
|
2001-06-04 23:56:26 +00:00
|
|
|
print_port(chain->fw_prot,
|
|
|
|
chain->fw_uar.fw_pts[i], comma);
|
|
|
|
if (i == 0 && (chain->fw_flg & IP_FW_F_SRNG))
|
|
|
|
comma = '-';
|
|
|
|
else if (i == 0 && (chain->fw_flg & IP_FW_F_SMSK))
|
|
|
|
comma = ':';
|
1996-08-05 02:38:51 +00:00
|
|
|
else
|
2001-06-04 23:56:26 +00:00
|
|
|
comma = ',';
|
1996-08-05 02:38:51 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
2001-02-13 14:12:37 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_DME) {
|
|
|
|
printf(" to me");
|
|
|
|
} else {
|
|
|
|
printf(" to %s", chain->fw_flg & IP_FW_F_INVDST ? "not " : "");
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
adrt = ntohl(chain->fw_dmsk.s_addr);
|
|
|
|
if (adrt == ULONG_MAX && do_resolv) {
|
|
|
|
adrt = (chain->fw_dst.s_addr);
|
|
|
|
he = gethostbyaddr((char *)&adrt,
|
|
|
|
sizeof(u_long), AF_INET);
|
|
|
|
if (he == NULL)
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_dst));
|
2001-06-04 23:56:26 +00:00
|
|
|
else
|
|
|
|
printf("%s", he->h_name);
|
|
|
|
} else if (adrt != ULONG_MAX) {
|
|
|
|
mb = mask_bits(chain->fw_dmsk);
|
|
|
|
if (mb == 0) {
|
|
|
|
printf("any");
|
|
|
|
} else if (mb > 0) {
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_dst));
|
2001-06-04 23:56:26 +00:00
|
|
|
printf("/%d", mb);
|
|
|
|
} else {
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_dst));
|
2001-06-04 23:56:26 +00:00
|
|
|
printf(":");
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_dmsk));
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-07-01 22:00:26 +00:00
|
|
|
printf("%s", inet_ntoa(chain->fw_dst));
|
2001-02-13 14:12:37 +00:00
|
|
|
}
|
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) {
|
2001-06-04 23:56:26 +00:00
|
|
|
comma = ' ';
|
1997-06-02 05:02:37 +00:00
|
|
|
for (i = 0; i < ndp; i++) {
|
2001-06-04 23:56:26 +00:00
|
|
|
print_port(chain->fw_prot,
|
|
|
|
chain->fw_uar.fw_pts[nsp+i], comma);
|
|
|
|
if (i == 0 && (chain->fw_flg & IP_FW_F_DRNG))
|
|
|
|
comma = '-';
|
|
|
|
else if (i == 0 && (chain->fw_flg & IP_FW_F_DMSK))
|
|
|
|
comma = ':';
|
1997-06-02 05:02:37 +00:00
|
|
|
else
|
2001-06-04 23:56:26 +00:00
|
|
|
comma = ',';
|
1997-06-02 05:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
|
1999-06-19 18:43:33 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_UID) {
|
|
|
|
struct passwd *pwd = getpwuid(chain->fw_uid);
|
|
|
|
|
|
|
|
if (pwd)
|
|
|
|
printf(" uid %s", pwd->pw_name);
|
|
|
|
else
|
|
|
|
printf(" uid %u", chain->fw_uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chain->fw_flg & IP_FW_F_GID) {
|
|
|
|
struct group *grp = getgrgid(chain->fw_gid);
|
|
|
|
|
|
|
|
if (grp)
|
|
|
|
printf(" gid %s", grp->gr_name);
|
|
|
|
else
|
|
|
|
printf(" gid %u", chain->fw_gid);
|
|
|
|
}
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_KEEP_S) {
|
|
|
|
if (chain->next_rule_ptr)
|
|
|
|
printf(" keep-state %d", (int)chain->next_rule_ptr);
|
|
|
|
else
|
|
|
|
printf(" keep-state");
|
|
|
|
}
|
|
|
|
/* Direction */
|
|
|
|
if (chain->fw_flg & IP_FW_BRIDGED)
|
|
|
|
printf(" bridged");
|
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(" 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
|
|
|
|
2000-10-02 03:03:31 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_IPOPT) {
|
2001-06-04 23:56:26 +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 ");
|
2001-06-04 23:56:26 +00:00
|
|
|
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-23 03:58:06 +00:00
|
|
|
|
2000-10-02 03:03:31 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_IPLEN)
|
|
|
|
printf(" iplen %u", chain->fw_iplen);
|
|
|
|
if (chain->fw_ipflg & IP_FW_IF_IPID)
|
2000-10-03 11:23:29 +00:00
|
|
|
printf(" ipid %#x", chain->fw_ipid);
|
2000-10-02 03:03:31 +00:00
|
|
|
|
|
|
|
if (chain->fw_ipflg & IP_FW_IF_IPTOS) {
|
2001-06-04 23:56:26 +00:00
|
|
|
int _opt_printed = 0;
|
2000-10-02 03:03:31 +00:00
|
|
|
|
|
|
|
printf(" iptos ");
|
2001-06-04 23:56:26 +00:00
|
|
|
if (chain->fw_iptos & IPTOS_LOWDELAY)
|
|
|
|
PRINTOPT("lowdelay");
|
|
|
|
if (chain->fw_ipntos & IPTOS_LOWDELAY)
|
|
|
|
PRINTOPT("!lowdelay");
|
|
|
|
if (chain->fw_iptos & IPTOS_THROUGHPUT)
|
|
|
|
PRINTOPT("throughput");
|
|
|
|
if (chain->fw_ipntos & IPTOS_THROUGHPUT)
|
|
|
|
PRINTOPT("!throughput");
|
|
|
|
if (chain->fw_iptos & IPTOS_RELIABILITY)
|
|
|
|
PRINTOPT("reliability");
|
|
|
|
if (chain->fw_ipntos & IPTOS_RELIABILITY)
|
|
|
|
PRINTOPT("!reliability");
|
|
|
|
if (chain->fw_iptos & IPTOS_MINCOST)
|
|
|
|
PRINTOPT("mincost");
|
|
|
|
if (chain->fw_ipntos & IPTOS_MINCOST)
|
|
|
|
PRINTOPT("!mincost");
|
|
|
|
if (chain->fw_iptos & IPTOS_CE)
|
|
|
|
PRINTOPT("congestion");
|
|
|
|
if (chain->fw_ipntos & IPTOS_CE)
|
|
|
|
PRINTOPT("!congestion");
|
|
|
|
}
|
2000-10-02 03:03:31 +00:00
|
|
|
|
|
|
|
if (chain->fw_ipflg & IP_FW_IF_IPTTL)
|
|
|
|
printf(" ipttl %u", chain->fw_ipttl);
|
|
|
|
|
|
|
|
if (chain->fw_ipflg & IP_FW_IF_IPVER)
|
|
|
|
printf(" ipversion %u", chain->fw_ipver);
|
|
|
|
|
o IPFW incorrectly handled filtering in the presence of previously
reserved and now allocated TCP flags in incoming packets. This patch
stops overloading those bits in the IP firewall rules, and moves
colliding flags to a seperate field, ipflg. The IPFW userland
management tool, ipfw(8), is updated to reflect this change. New TCP
flags related to ECN are now included in tcp.h for reference, although
we don't currently implement TCP+ECN.
o To use this fix without completely rebuilding, it is sufficient to copy
ip_fw.h and tcp.h into your appropriate include directory, then rebuild
the ipfw kernel module, and ipfw tool, and install both. Note that a
mismatch between module and userland tool will result in incorrect
installation of firewall rules that may have unexpected effects. This
is an MFC candidate, following shakedown. This bug does not appear
to affect ipfilter.
Reviewed by: security-officer, billf
Reported by: Aragon Gouveia <aragon@phat.za.net>
2001-01-09 03:10:30 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_TCPEST)
|
1996-04-03 13:49:10 +00:00
|
|
|
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");
|
2000-10-03 10:37:03 +00:00
|
|
|
else if (chain->fw_ipflg & IP_FW_IF_TCPFLG) {
|
2001-06-04 23:56:26 +00:00
|
|
|
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;}
|
|
|
|
|
2000-06-08 15:34:51 +00:00
|
|
|
printf(" tcpflags ");
|
2001-06-04 23:56:26 +00:00
|
|
|
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");
|
|
|
|
}
|
2000-10-02 03:03:31 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_TCPOPT) {
|
2001-06-04 23:56:26 +00:00
|
|
|
int _opt_printed = 0;
|
2000-06-08 15:34:51 +00:00
|
|
|
#define PRINTTOPT(x) {if (_opt_printed) printf(",");\
|
|
|
|
printf(x); _opt_printed = 1;}
|
|
|
|
|
|
|
|
printf(" tcpoptions ");
|
2001-06-04 23:56:26 +00:00
|
|
|
if (chain->fw_tcpopt & IP_FW_TCPOPT_MSS)
|
|
|
|
PRINTTOPT("mss");
|
|
|
|
if (chain->fw_tcpnopt & IP_FW_TCPOPT_MSS)
|
|
|
|
PRINTTOPT("!mss");
|
|
|
|
if (chain->fw_tcpopt & IP_FW_TCPOPT_WINDOW)
|
|
|
|
PRINTTOPT("window");
|
|
|
|
if (chain->fw_tcpnopt & IP_FW_TCPOPT_WINDOW)
|
|
|
|
PRINTTOPT("!window");
|
|
|
|
if (chain->fw_tcpopt & IP_FW_TCPOPT_SACK)
|
|
|
|
PRINTTOPT("sack");
|
|
|
|
if (chain->fw_tcpnopt & IP_FW_TCPOPT_SACK)
|
|
|
|
PRINTTOPT("!sack");
|
|
|
|
if (chain->fw_tcpopt & IP_FW_TCPOPT_TS)
|
|
|
|
PRINTTOPT("ts");
|
|
|
|
if (chain->fw_tcpnopt & IP_FW_TCPOPT_TS)
|
|
|
|
PRINTTOPT("!ts");
|
|
|
|
if (chain->fw_tcpopt & IP_FW_TCPOPT_CC)
|
|
|
|
PRINTTOPT("cc");
|
|
|
|
if (chain->fw_tcpnopt & IP_FW_TCPOPT_CC)
|
|
|
|
PRINTTOPT("!cc");
|
|
|
|
}
|
2000-06-08 15:34:51 +00:00
|
|
|
|
2000-10-02 03:03:31 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_TCPSEQ)
|
2001-04-05 07:37:55 +00:00
|
|
|
printf(" tcpseq %lu", (u_long)ntohl(chain->fw_tcpseq));
|
2000-10-02 03:03:31 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_TCPACK)
|
2001-04-05 07:37:55 +00:00
|
|
|
printf(" tcpack %lu", (u_long)ntohl(chain->fw_tcpack));
|
2000-10-02 03:03:31 +00:00
|
|
|
if (chain->fw_ipflg & IP_FW_IF_TCPWIN)
|
|
|
|
printf(" tcpwin %hu", ntohs(chain->fw_tcpwin));
|
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
if (chain->fw_flg & IP_FW_F_ICMPBIT) {
|
2001-06-04 23:56:26 +00:00
|
|
|
int i, first = 1;
|
|
|
|
unsigned j;
|
1996-06-09 23:46:22 +00:00
|
|
|
|
|
|
|
printf(" icmptype");
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
for (i = 0; i < IP_FW_ICMPTYPES_DIM; ++i)
|
|
|
|
for (j = 0; j < sizeof(unsigned) * 8; ++j)
|
|
|
|
if (chain->fw_uar.fw_icmptypes[i] & (1 << j)) {
|
|
|
|
printf("%c%d", first ? ' ' : ',', i);
|
|
|
|
first = 0;
|
|
|
|
}
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
1994-11-16 10:18:18 +00:00
|
|
|
printf("\n");
|
2000-02-10 14:25:26 +00:00
|
|
|
done:
|
1996-06-09 23:46:22 +00:00
|
|
|
if (do_resolv)
|
|
|
|
endservent();
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
static void
|
|
|
|
show_dyn_ipfw(struct ipfw_dyn_rule *d)
|
|
|
|
{
|
|
|
|
struct protoent *pe;
|
|
|
|
struct in_addr a;
|
|
|
|
|
|
|
|
if (!d->expire && !do_expired)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%05d %qu %qu (T %d, # %d) ty %d",
|
|
|
|
(int)(d->chain),
|
|
|
|
d->pcnt, d->bcnt,
|
|
|
|
d->expire,
|
|
|
|
d->bucket,
|
|
|
|
d->type);
|
|
|
|
|
|
|
|
if (do_resolv && (pe = getprotobynumber(d->id.proto)) != NULL)
|
|
|
|
printf(" %s,", pe->p_name);
|
|
|
|
else
|
|
|
|
printf(" %u,", d->id.proto);
|
|
|
|
|
|
|
|
a.s_addr = htonl(d->id.src_ip);
|
|
|
|
printf(" %s", inet_ntoa(a));
|
|
|
|
printf(" %d", d->id.src_port);
|
|
|
|
|
|
|
|
switch (d->type) {
|
|
|
|
default: /* bidir, no mask */
|
|
|
|
printf(" <->");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
a.s_addr = htonl(d->id.dst_ip);
|
|
|
|
printf(" %s", inet_ntoa(a));
|
|
|
|
printf(" %d", d->id.dst_port);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2000-02-10 14:25:26 +00:00
|
|
|
int
|
|
|
|
sort_q(const void *pa, const void *pb)
|
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
int rev = (do_sort < 0);
|
|
|
|
int field = rev ? -do_sort : do_sort;
|
|
|
|
long long res = 0;
|
|
|
|
const struct dn_flow_queue *a = pa;
|
|
|
|
const struct dn_flow_queue *b = pb;
|
|
|
|
|
|
|
|
switch (field) {
|
|
|
|
case 1: /* pkts */
|
|
|
|
res = a->len - b->len;
|
|
|
|
break;
|
|
|
|
case 2: /* bytes */
|
|
|
|
res = a->len_bytes - b->len_bytes;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* tot pkts */
|
|
|
|
res = a->tot_pkts - b->tot_pkts;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: /* tot bytes */
|
|
|
|
res = a->tot_bytes - b->tot_bytes;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (res < 0)
|
|
|
|
res = -1;
|
|
|
|
if (res > 0)
|
|
|
|
res = 1;
|
|
|
|
return (int)(rev ? res : -res);
|
2000-02-10 14:25:26 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 10:08:39 +00:00
|
|
|
static void
|
|
|
|
list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
|
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
int l;
|
|
|
|
|
|
|
|
printf(" mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
|
|
|
|
fs->flow_mask.proto,
|
|
|
|
fs->flow_mask.src_ip, fs->flow_mask.src_port,
|
|
|
|
fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
|
|
|
|
if (fs->rq_elements == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("BKT Prot ___Source IP/port____ "
|
|
|
|
"____Dest. IP/port____ Tot_pkt/bytes Pkt/Byte Drp\n");
|
|
|
|
if (do_sort != 0)
|
|
|
|
heapsort(q, fs->rq_elements, sizeof *q, sort_q);
|
|
|
|
for (l = 0; l < fs->rq_elements; l++) {
|
|
|
|
struct in_addr ina;
|
|
|
|
struct protoent *pe;
|
|
|
|
|
|
|
|
ina.s_addr = htonl(q[l].id.src_ip);
|
|
|
|
printf("%3d ", q[l].hash_slot);
|
|
|
|
pe = getprotobynumber(q[l].id.proto);
|
|
|
|
if (pe)
|
|
|
|
printf("%-4s ", pe->p_name);
|
|
|
|
else
|
|
|
|
printf("%4u ", q[l].id.proto);
|
|
|
|
printf("%15s/%-5d ",
|
|
|
|
inet_ntoa(ina), q[l].id.src_port);
|
|
|
|
ina.s_addr = htonl(q[l].id.dst_ip);
|
|
|
|
printf("%15s/%-5d ",
|
|
|
|
inet_ntoa(ina), q[l].id.dst_port);
|
|
|
|
printf("%4qu %8qu %2u %4u %3u\n",
|
|
|
|
q[l].tot_pkts, q[l].tot_bytes,
|
|
|
|
q[l].len, q[l].len_bytes, q[l].drops);
|
|
|
|
if (verbose)
|
|
|
|
printf(" S %20qd F %20qd\n",
|
|
|
|
q[l].S, q[l].F);
|
|
|
|
}
|
2000-06-08 10:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_flowset_parms(struct dn_flow_set *fs, char *prefix)
|
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
int l;
|
|
|
|
char qs[30];
|
|
|
|
char plr[30];
|
|
|
|
char red[90]; /* Display RED parameters */
|
|
|
|
|
|
|
|
l = fs->qsize;
|
|
|
|
if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
|
|
|
|
if (l >= 8192)
|
|
|
|
sprintf(qs, "%d KB", l / 1024);
|
|
|
|
else
|
|
|
|
sprintf(qs, "%d B", l);
|
|
|
|
} else
|
|
|
|
sprintf(qs, "%3d sl.", l);
|
|
|
|
if (fs->plr)
|
|
|
|
sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
|
|
|
|
else
|
|
|
|
plr[0] = '\0';
|
|
|
|
if (fs->flags_fs & DN_IS_RED) /* RED parameters */
|
|
|
|
sprintf(red,
|
|
|
|
"\n %cRED w_q %f min_th %d max_th %d max_p %f",
|
|
|
|
(fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
|
|
|
|
1.0*fs->w_q / (double)(1 << SCALE_RED),
|
|
|
|
SCALE_VAL(fs->min_th),
|
|
|
|
SCALE_VAL(fs->max_th),
|
|
|
|
1.0*fs->max_p / (double)(1 << SCALE_RED));
|
2000-06-08 10:08:39 +00:00
|
|
|
else
|
2001-06-04 23:56:26 +00:00
|
|
|
sprintf(red, "droptail");
|
|
|
|
|
|
|
|
printf("%s %s%s %d queues (%d buckets) %s\n",
|
|
|
|
prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
|
2000-06-08 10:08:39 +00:00
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
list(int ac, char *av[])
|
1994-10-28 15:06:53 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
struct ip_fw *rules, *r;
|
|
|
|
struct ipfw_dyn_rule *dynrules, *d;
|
1999-01-22 01:46:32 +00:00
|
|
|
struct dn_pipe *pipes;
|
|
|
|
void *data = NULL;
|
2001-06-04 23:56:26 +00:00
|
|
|
int n, nbytes, nstat, ndyn;
|
|
|
|
int exitval = EX_OK;
|
|
|
|
int lac;
|
|
|
|
char **lav;
|
|
|
|
u_long rnum;
|
|
|
|
char *endptr;
|
|
|
|
int seen = 0;
|
1996-02-24 13:39:46 +00:00
|
|
|
|
1999-01-22 01:46:32 +00:00
|
|
|
/* get rules or pipes from kernel, resizing array as necessary */
|
|
|
|
{
|
|
|
|
const int unit = do_pipe ? sizeof(*pipes) : sizeof(*rules);
|
|
|
|
const int ocmd = do_pipe ? IP_DUMMYNET_GET : IP_FW_GET;
|
2000-06-14 10:07:22 +00:00
|
|
|
int nalloc = unit;
|
2001-06-04 23:56:26 +00:00
|
|
|
nbytes = nalloc;
|
1999-01-22 01:46:32 +00:00
|
|
|
|
2000-06-14 10:07:22 +00:00
|
|
|
while (nbytes >= nalloc) {
|
1999-01-22 01:46:32 +00:00
|
|
|
nalloc = nalloc * 2 + 200;
|
2001-06-04 23:56:26 +00:00
|
|
|
nbytes = nalloc;
|
1999-01-22 01:46:32 +00:00
|
|
|
if ((data = realloc(data, nbytes)) == NULL)
|
|
|
|
err(EX_OSERR, "realloc");
|
|
|
|
if (getsockopt(s, IPPROTO_IP, ocmd, data, &nbytes) < 0)
|
|
|
|
err(EX_OSERR, "getsockopt(IP_%s_GET)",
|
|
|
|
do_pipe ? "DUMMYNET" : "FW");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* display requested pipes */
|
|
|
|
if (do_pipe) {
|
2001-06-04 23:56:26 +00:00
|
|
|
u_long rulenum;
|
|
|
|
void *next = data;
|
|
|
|
struct dn_pipe *p = (struct dn_pipe *) data;
|
|
|
|
struct dn_flow_set *fs;
|
|
|
|
struct dn_flow_queue *q;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if (ac > 0)
|
|
|
|
rulenum = strtoul(*av++, NULL, 10);
|
1999-01-22 01:46:32 +00:00
|
|
|
else
|
2001-06-04 23:56:26 +00:00
|
|
|
rulenum = 0;
|
|
|
|
for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
|
|
|
|
double b = p->bandwidth;
|
|
|
|
char buf[30];
|
|
|
|
char prefix[80];
|
|
|
|
|
|
|
|
if (p->next != (struct dn_pipe *)DN_IS_PIPE)
|
|
|
|
break;
|
|
|
|
l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
|
|
|
|
next = (void *)p + l;
|
|
|
|
nbytes -= l;
|
|
|
|
q = (struct dn_flow_queue *)(p+1);
|
|
|
|
|
|
|
|
if (rulenum != 0 && rulenum != p->pipe_nr)
|
|
|
|
continue;
|
|
|
|
if (p->if_name[0] != '\0')
|
|
|
|
sprintf(buf, "%s", p->if_name);
|
|
|
|
else if (b == 0)
|
|
|
|
sprintf(buf, "unlimited");
|
|
|
|
else if (b >= 1000000)
|
|
|
|
sprintf(buf, "%7.3f Mbit/s", b/1000000);
|
|
|
|
else if (b >= 1000)
|
|
|
|
sprintf(buf, "%7.3f Kbit/s", b/1000);
|
|
|
|
else
|
|
|
|
sprintf(buf, "%7.3f bit/s ", b);
|
|
|
|
|
|
|
|
sprintf(prefix, "%05d: %s %4d ms ",
|
|
|
|
p->pipe_nr, buf, p->delay);
|
|
|
|
print_flowset_parms(&(p->fs), prefix);
|
|
|
|
if (verbose)
|
|
|
|
printf(" V %20qd\n", p->V >> MY_M);
|
|
|
|
list_queues(&(p->fs), q);
|
|
|
|
}
|
|
|
|
fs = (struct dn_flow_set *) next;
|
|
|
|
for (; nbytes >= sizeof *fs; fs = (struct dn_flow_set *)next) {
|
|
|
|
char prefix[80];
|
|
|
|
|
|
|
|
if (fs->next != (struct dn_flow_set *)DN_IS_QUEUE)
|
|
|
|
break;
|
|
|
|
l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
|
|
|
|
next = (void *)fs + l;
|
|
|
|
nbytes -= l;
|
|
|
|
q = (struct dn_flow_queue *)(fs+1);
|
|
|
|
sprintf(prefix, "q%05d: weight %d pipe %d ",
|
|
|
|
fs->fs_nr, fs->weight, fs->parent_nr);
|
|
|
|
print_flowset_parms(fs, prefix);
|
|
|
|
list_queues(fs, q);
|
1999-01-22 01:46:32 +00:00
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
return;
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
|
|
|
|
rules = (struct ip_fw *)data;
|
|
|
|
for (nstat = 0; rules[nstat].fw_number < 65535; ++nstat)
|
|
|
|
/* nothing */ ;
|
|
|
|
nstat++; /* counting starts from 0 ... */
|
|
|
|
dynrules = (struct ipfw_dyn_rule *)&rules[nstat];
|
|
|
|
ndyn = (nbytes - (nstat * sizeof *rules)) / sizeof *dynrules;
|
|
|
|
|
|
|
|
|
|
|
|
/* if no rule numbers were specified, list all rules */
|
1999-01-22 01:46:32 +00:00
|
|
|
if (ac == 0) {
|
2001-06-04 23:56:26 +00:00
|
|
|
for (n = 0; n < nstat; n++)
|
|
|
|
show_ipfw(&rules[n]);
|
1999-01-22 01:46:32 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (do_dynamic && ndyn) {
|
|
|
|
printf("## Dynamic rules:\n");
|
|
|
|
for (n = 0, d = dynrules; n < ndyn; n++, d++)
|
|
|
|
show_dyn_ipfw(d);
|
1999-01-22 01:46:32 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
free(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* display specific rules requested on command line */
|
|
|
|
|
|
|
|
for (lac = ac, lav = av; lac != 0; lac--) {
|
|
|
|
/* convert command line rule # */
|
|
|
|
rnum = strtoul(*lav++, &endptr, 10);
|
|
|
|
if (*endptr) {
|
|
|
|
exitval = EX_USAGE;
|
|
|
|
warnx("invalid rule number: %s", *(lav - 1));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (n = seen = 0, r = rules; n < nstat; n++, r++) {
|
|
|
|
if (r->fw_number > rnum)
|
|
|
|
break;
|
|
|
|
if (r->fw_number == rnum) {
|
|
|
|
show_ipfw(r);
|
|
|
|
seen = 1;
|
1998-01-07 02:23:04 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
|
|
|
if (!seen) {
|
|
|
|
/* give precedence to other error(s) */
|
|
|
|
if (exitval == EX_OK)
|
|
|
|
exitval = EX_UNAVAILABLE;
|
|
|
|
warnx("rule %lu does not exist", rnum);
|
|
|
|
}
|
|
|
|
}
|
1999-01-22 01:46:32 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
printf("## Dynamic rules:\n");
|
|
|
|
if (do_dynamic && ndyn) {
|
|
|
|
for (lac = ac, lav = av; lac != 0; lac--) {
|
|
|
|
rnum = strtoul(*lav++, &endptr, 10);
|
|
|
|
if (*endptr)
|
|
|
|
/* already warned */
|
|
|
|
continue;
|
|
|
|
for (n = 0, d = dynrules; n < ndyn; n++, d++) {
|
|
|
|
if ((int)(d->chain) > rnum)
|
1999-01-22 01:46:32 +00:00
|
|
|
break;
|
2001-06-04 23:56:26 +00:00
|
|
|
if ((int)(d->chain) == rnum)
|
|
|
|
show_dyn_ipfw(d);
|
1998-01-07 02:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
2000-02-10 14:25:26 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
ac = 0;
|
|
|
|
|
1999-01-22 01:46:32 +00:00
|
|
|
free(data);
|
2001-06-04 23:56:26 +00:00
|
|
|
|
|
|
|
if (exitval != EX_OK)
|
|
|
|
exit(exitval);
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
2001-07-22 06:40:11 +00:00
|
|
|
show_usage(void)
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1997-06-13 06:27:12 +00:00
|
|
|
fprintf(stderr, "usage: ipfw [options]\n"
|
1999-05-24 10:01:22 +00:00
|
|
|
" [pipe] flush\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" add [number] rule\n"
|
1999-05-24 10:01:22 +00:00
|
|
|
" [pipe] delete number ...\n"
|
|
|
|
" [pipe] list [number ...]\n"
|
|
|
|
" [pipe] show [number ...]\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" zero [number ...]\n"
|
1999-08-01 16:57:24 +00:00
|
|
|
" resetlog [number ...]\n"
|
1999-06-02 05:59:48 +00:00
|
|
|
" pipe number config [pipeconfig]\n"
|
1999-08-11 15:36:13 +00:00
|
|
|
" rule: [prob <match_probability>] action proto src dst extras...\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" action:\n"
|
|
|
|
" {allow|permit|accept|pass|deny|drop|reject|unreach code|\n"
|
2001-06-04 23:56:26 +00:00
|
|
|
" reset|count|skipto num|divert port|tee port|fwd ip|\n"
|
|
|
|
" pipe num} [log [logamount count]]\n"
|
|
|
|
" proto: {ip|tcp|udp|icmp| < number>}\n"
|
|
|
|
" src: from [not] {me|any|ip[{/bits|:mask}]} [{port[-port]}, [port], ...]\n"
|
|
|
|
" dst: to [not] {me|any|ip[{/bits|:mask}]} [{port[-port]}, [port], ...]\n"
|
1997-06-02 05:02:37 +00:00
|
|
|
" extras:\n"
|
1999-06-19 18:43:33 +00:00
|
|
|
" uid {user id}\n"
|
|
|
|
" gid {group id}\n"
|
2001-06-04 23:56:26 +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"
|
2001-06-04 23:56:26 +00:00
|
|
|
" tcpflags [!]{syn|fin|rst|ack|psh|urg}, ...\n"
|
|
|
|
" ipoptions [!]{ssrr|lsrr|rr|ts}, ...\n"
|
2000-10-02 03:03:31 +00:00
|
|
|
" iplen {length}\n"
|
2000-10-03 11:23:29 +00:00
|
|
|
" ipid {identification number}\n"
|
2001-06-04 23:56:26 +00:00
|
|
|
" iptos [!]{lowdelay|throughput|reliability|mincost|congestion}, ...\n"
|
2000-10-02 03:03:31 +00:00
|
|
|
" ipttl {time to live}\n"
|
|
|
|
" ipversion {version number}\n"
|
2001-06-04 23:56:26 +00:00
|
|
|
" tcpoptions [!]{mss|window|sack|ts|cc}, ...\n"
|
2000-10-02 03:03:31 +00:00
|
|
|
" tcpseq {sequence number}\n"
|
|
|
|
" tcpack {acknowledgement number}\n"
|
|
|
|
" tcpwin {window size}\n"
|
2001-06-04 23:56:26 +00:00
|
|
|
" icmptypes {type[, type]}...\n"
|
2001-07-22 06:40:11 +00:00
|
|
|
" keep-state [method]\n"
|
1999-06-02 05:59:48 +00:00
|
|
|
" pipeconfig:\n"
|
1999-05-24 10:01:22 +00:00
|
|
|
" {bw|bandwidth} <number>{bit/s|Kbit/s|Mbit/s|Bytes/s|KBytes/s|MBytes/s}\n"
|
2000-06-08 10:08:39 +00:00
|
|
|
" {bw|bandwidth} interface_name\n"
|
1999-05-24 10:01:22 +00:00
|
|
|
" delay <milliseconds>\n"
|
|
|
|
" queue <size>{packets|Bytes|KBytes}\n"
|
|
|
|
" plr <fraction>\n"
|
2000-01-08 11:19:19 +00:00
|
|
|
" mask {all| [dst-ip|src-ip|dst-port|src-port|proto] <number>}\n"
|
|
|
|
" buckets <number>}\n"
|
2000-06-08 10:08:39 +00:00
|
|
|
" {red|gred} <fraction>/<number>/<number>/<fraction>\n"
|
|
|
|
" droptail\n"
|
1999-05-24 10:01:22 +00:00
|
|
|
);
|
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
|
2001-06-04 23:56:26 +00:00
|
|
|
lookup_host (char *host, struct in_addr *ipaddr)
|
1996-06-09 23:46:22 +00:00
|
|
|
{
|
1999-06-04 11:20:59 +00:00
|
|
|
struct hostent *he;
|
1996-06-09 23:46:22 +00:00
|
|
|
|
1999-06-04 11:20:59 +00:00
|
|
|
if (!inet_aton(host, ipaddr)) {
|
|
|
|
if ((he = gethostbyname(host)) == NULL)
|
|
|
|
return(-1);
|
|
|
|
*ipaddr = *(struct in_addr *)he->h_addr_list[0];
|
|
|
|
}
|
1996-06-09 23:46:22 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
fill_ip(struct in_addr *ipno, struct in_addr *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
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "any", strlen(*av))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
ipno->s_addr = mask->s_addr = 0; av++; ac--;
|
|
|
|
} else {
|
|
|
|
p = strchr(*av, '/');
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!p)
|
1996-02-24 00:20:56 +00:00
|
|
|
p = strchr(*av, ':');
|
|
|
|
if (p) {
|
|
|
|
md = *p;
|
2001-06-04 23:56:26 +00:00
|
|
|
*p++ = '\0';
|
1996-02-24 00:20:56 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
if (lookup_host(*av, ipno) != 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_NOHOST, "hostname ``%s'' unknown", *av);
|
1996-06-23 20:47:51 +00:00
|
|
|
switch (md) {
|
|
|
|
case ':':
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!inet_aton(p, mask))
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "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) {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "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;
|
|
|
|
}
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str);
|
1997-06-02 05:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
add_port(u_short *cnt, u_short *ptr, u_short off, u_short port)
|
1996-05-11 20:31:55 +00:00
|
|
|
{
|
|
|
|
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
|
2000-10-04 07:59:19 +00:00
|
|
|
lookup_port(const char *arg, int proto, int test, int nodash)
|
1997-06-23 22:32:13 +00:00
|
|
|
{
|
|
|
|
int val;
|
|
|
|
char *earg, buf[32];
|
|
|
|
struct servent *s;
|
1999-06-11 09:43:53 +00:00
|
|
|
char *p, *q;
|
1997-06-23 22:32:13 +00:00
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s", arg);
|
1999-06-11 09:43:53 +00:00
|
|
|
|
|
|
|
for (p = q = buf; *p; *q++ = *p++) {
|
|
|
|
if (*p == '\\') {
|
|
|
|
if (*(p+1))
|
|
|
|
p++;
|
|
|
|
} else {
|
|
|
|
if (*p == ',' || (nodash && *p == '-'))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
|
1997-06-23 22:32:13 +00:00
|
|
|
val = (int) strtoul(buf, &earg, 0);
|
|
|
|
if (!*buf || *earg) {
|
2000-10-04 07:59:19 +00:00
|
|
|
char *protocol = NULL;
|
|
|
|
|
|
|
|
if (proto != 0) {
|
|
|
|
struct protoent *pe = getprotobynumber(proto);
|
|
|
|
|
|
|
|
if (pe)
|
|
|
|
protocol = pe->p_name;
|
|
|
|
}
|
|
|
|
|
1997-06-23 22:32:13 +00:00
|
|
|
setservent(1);
|
2000-10-04 07:59:19 +00:00
|
|
|
if ((s = getservbyname(buf, protocol))) {
|
1997-06-23 22:32:13 +00:00
|
|
|
val = htons(s->s_port);
|
|
|
|
} else {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!test)
|
1999-06-11 09:43:53 +00:00
|
|
|
errx(EX_DATAERR, "unknown port ``%s''", buf);
|
1997-06-23 22:32:13 +00:00
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (val < 0 || val > 0xffff) {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!test)
|
|
|
|
errx(EX_DATAERR,
|
|
|
|
"port ``%s'' out of range", buf);
|
1997-06-23 22:32:13 +00:00
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(val);
|
|
|
|
}
|
|
|
|
|
2000-01-08 11:19:19 +00:00
|
|
|
/*
|
|
|
|
* return: 0 normally, 1 if first pair is a range,
|
|
|
|
* 2 if first pair is a port+mask
|
|
|
|
*/
|
1998-09-28 22:56:37 +00:00
|
|
|
static int
|
2000-10-04 07:59:19 +00:00
|
|
|
fill_port(u_short *cnt, u_short *ptr, u_short off, char *arg, int proto)
|
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
|
|
|
|
2000-01-08 11:19:19 +00:00
|
|
|
for (s = arg; *s && *s != ',' && *s != '-' && *s != ':'; s++) {
|
1999-06-11 09:43:53 +00:00
|
|
|
if (*s == '\\' && *(s+1))
|
|
|
|
s++;
|
|
|
|
}
|
2000-01-08 11:19:19 +00:00
|
|
|
if (*s == ':') {
|
|
|
|
*s++ = '\0';
|
|
|
|
if (strchr(arg, ','))
|
|
|
|
errx(EX_USAGE, "port/mask must be first in list");
|
2001-06-04 23:56:26 +00:00
|
|
|
add_port(cnt, ptr, off,
|
|
|
|
*arg ? lookup_port(arg, proto, 0, 0) : 0x0000);
|
2000-01-08 11:19:19 +00:00
|
|
|
arg = s;
|
2001-06-04 23:56:26 +00:00
|
|
|
s = strchr(arg, ',');
|
2000-01-08 11:19:19 +00:00
|
|
|
if (s)
|
|
|
|
*s++ = '\0';
|
2001-06-04 23:56:26 +00:00
|
|
|
add_port(cnt, ptr, off,
|
|
|
|
*arg ? lookup_port(arg, proto, 0, 0) : 0xffff);
|
2000-01-08 11:19:19 +00:00
|
|
|
arg = s;
|
|
|
|
initial_range = 2;
|
|
|
|
} else
|
1997-06-23 22:32:13 +00:00
|
|
|
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");
|
2001-06-04 23:56:26 +00:00
|
|
|
add_port(cnt, ptr, off,
|
|
|
|
*arg ? lookup_port(arg, proto, 0, 0) : 0x0000);
|
1996-05-11 20:31:55 +00:00
|
|
|
arg = s;
|
2001-06-04 23:56:26 +00:00
|
|
|
s = strchr(arg, ',');
|
1996-05-11 20:31:55 +00:00
|
|
|
if (s)
|
1996-02-24 00:20:56 +00:00
|
|
|
*s++ = '\0';
|
2001-06-04 23:56:26 +00:00
|
|
|
add_port(cnt, ptr, off,
|
|
|
|
*arg ? lookup_port(arg, proto, 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) {
|
2001-06-04 23:56:26 +00:00
|
|
|
s = strchr(arg, ',');
|
1996-05-11 20:31:55 +00:00
|
|
|
if (s)
|
1996-02-24 00:20:56 +00:00
|
|
|
*s++ = '\0';
|
2000-10-04 07:59:19 +00:00
|
|
|
add_port(cnt, ptr, off, lookup_port(arg, proto, 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
|
2000-06-08 15:34:51 +00:00
|
|
|
fill_tcpflag(u_char *set, u_char *reset, char **vp)
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
char *p = *vp, *q;
|
1996-02-24 00:20:56 +00:00
|
|
|
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, ',');
|
2001-06-04 23:56:26 +00:00
|
|
|
if (q)
|
1996-02-24 00:20:56 +00:00
|
|
|
*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]))
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "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
|
|
|
}
|
|
|
|
|
2000-06-08 15:34:51 +00:00
|
|
|
static void
|
|
|
|
fill_tcpopts(u_char *set, u_char *reset, char **vp)
|
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
char *p = *vp, *q;
|
2000-06-08 15:34:51 +00:00
|
|
|
u_char *d;
|
|
|
|
|
|
|
|
while (p && *p) {
|
|
|
|
struct tpcopts {
|
|
|
|
char * name;
|
|
|
|
u_char value;
|
|
|
|
} opts[] = {
|
|
|
|
{ "mss", IP_FW_TCPOPT_MSS },
|
2001-06-04 23:56:26 +00:00
|
|
|
{ "window", IP_FW_TCPOPT_WINDOW },
|
2000-06-08 15:34:51 +00:00
|
|
|
{ "sack", IP_FW_TCPOPT_SACK },
|
|
|
|
{ "ts", IP_FW_TCPOPT_TS },
|
|
|
|
{ "cc", IP_FW_TCPOPT_CC },
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (*p == '!') {
|
|
|
|
p++;
|
|
|
|
d = reset;
|
|
|
|
} else {
|
|
|
|
d = set;
|
|
|
|
}
|
|
|
|
q = strchr(p, ',');
|
2001-06-04 23:56:26 +00:00
|
|
|
if (q)
|
2000-06-08 15:34:51 +00:00
|
|
|
*q++ = '\0';
|
|
|
|
for (i = 0; i < sizeof(opts) / sizeof(opts[0]); ++i)
|
|
|
|
if (!strncmp(p, opts[i].name, strlen(p))) {
|
|
|
|
*d |= opts[i].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == sizeof(opts) / sizeof(opts[0]))
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "invalid tcp option ``%s''", p);
|
2000-06-08 15:34:51 +00:00
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
char *p = *vp, *q;
|
1996-02-24 00:20:56 +00:00
|
|
|
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, ',');
|
2001-06-04 23:56:26 +00:00
|
|
|
if (q)
|
1996-02-24 00:20:56 +00:00
|
|
|
*q++ = '\0';
|
2001-06-04 23:56:26 +00:00
|
|
|
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;
|
1996-02-24 00:20:56 +00:00
|
|
|
p = q;
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-02 03:03:31 +00:00
|
|
|
static void
|
|
|
|
fill_iptos(u_char *set, u_char *reset, char **vp)
|
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
char *p = *vp, *q;
|
2000-10-02 03:03:31 +00:00
|
|
|
u_char *d;
|
|
|
|
|
|
|
|
while (p && *p) {
|
|
|
|
if (*p == '!') {
|
|
|
|
p++;
|
|
|
|
d = reset;
|
|
|
|
} else {
|
|
|
|
d = set;
|
|
|
|
}
|
|
|
|
q = strchr(p, ',');
|
2001-06-04 23:56:26 +00:00
|
|
|
if (q)
|
2000-10-02 03:03:31 +00:00
|
|
|
*q++ = '\0';
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(p, "lowdelay", strlen(p)))
|
2000-10-02 03:03:31 +00:00
|
|
|
*d |= IPTOS_LOWDELAY;
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(p, "throughput", strlen(p)))
|
2000-10-02 03:03:31 +00:00
|
|
|
*d |= IPTOS_THROUGHPUT;
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(p, "reliability", strlen(p)))
|
2000-10-02 03:03:31 +00:00
|
|
|
*d |= IPTOS_RELIABILITY;
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(p, "mincost", strlen(p)))
|
2000-10-02 03:03:31 +00:00
|
|
|
*d |= IPTOS_MINCOST;
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(p, "congestion", strlen(p)))
|
2000-10-02 03:03:31 +00:00
|
|
|
*d |= IPTOS_CE;
|
|
|
|
#if 0 /* conflicting! */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(p, "ecntransport", strlen(p)))
|
2000-10-02 03:03:31 +00:00
|
|
|
*d |= IPTOS_ECT;
|
|
|
|
#endif
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
fill_icmptypes(unsigned *types, char **vp, u_int *fw_flg)
|
1996-06-09 23:46:22 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
unsigned long icmptype;
|
1996-06-09 23:46:22 +00:00
|
|
|
char *c = *vp;
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
while (*c) {
|
|
|
|
if (*c == ',')
|
1996-06-09 23:46:22 +00:00
|
|
|
++c;
|
|
|
|
|
|
|
|
icmptype = strtoul(c, &c, 0);
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (*c != ',' && *c != '\0')
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "invalid ICMP type");
|
1996-06-09 23:46:22 +00:00
|
|
|
|
1997-08-08 14:36:29 +00:00
|
|
|
if (icmptype >= IP_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "ICMP type out of range");
|
1996-06-09 23:46:22 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
types[icmptype / (sizeof(unsigned) * 8)] |=
|
1996-06-09 23:46:22 +00:00
|
|
|
1 << (icmptype % (sizeof(unsigned) * 8));
|
|
|
|
*fw_flg |= IP_FW_F_ICMPBIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
delete(int ac, char *av[])
|
1995-10-23 03:58:06 +00:00
|
|
|
{
|
1996-02-24 00:20:56 +00:00
|
|
|
struct ip_fw rule;
|
1998-12-14 18:43:03 +00:00
|
|
|
struct dn_pipe pipe;
|
1996-02-24 00:20:56 +00:00
|
|
|
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);
|
1998-12-14 18:43:03 +00:00
|
|
|
memset(&pipe, 0, sizeof pipe);
|
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)) {
|
2000-06-08 10:08:39 +00:00
|
|
|
i = atoi(*av); av++; ac--;
|
2001-06-04 23:56:26 +00:00
|
|
|
if (do_pipe) {
|
2000-06-08 10:08:39 +00:00
|
|
|
if (do_pipe == 1)
|
2001-06-04 23:56:26 +00:00
|
|
|
pipe.pipe_nr = i;
|
2000-06-08 10:08:39 +00:00
|
|
|
else
|
2001-06-04 23:56:26 +00:00
|
|
|
pipe.fs.fs_nr = i;
|
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_DUMMYNET_DEL,
|
|
|
|
&pipe, sizeof pipe);
|
|
|
|
if (i) {
|
|
|
|
exitval = 1;
|
|
|
|
warn("rule %u: setsockopt(%s)",
|
|
|
|
do_pipe == 1 ? pipe.pipe_nr: pipe.fs.fs_nr,
|
2000-06-08 10:08:39 +00:00
|
|
|
"IP_DUMMYNET_DEL");
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rule.fw_number = i;
|
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;
|
2001-06-04 23:56:26 +00:00
|
|
|
warn("rule %u: setsockopt(IP_FW_DEL)",
|
|
|
|
rule.fw_number);
|
1998-01-06 00:11:57 +00:00
|
|
|
}
|
1995-10-01 21:54:05 +00:00
|
|
|
}
|
1998-12-14 18:43:03 +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.
|
|
|
|
*/
|
2001-06-04 23:56:26 +00:00
|
|
|
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)
|
2001-06-04 23:56:26 +00:00
|
|
|
warnx("warning: interface ``%s'' does not exist",
|
|
|
|
ifr.ifr_name);
|
1997-06-02 05:02:37 +00:00
|
|
|
}
|
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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument for ``%s''", which);
|
1997-06-02 05:02:37 +00:00
|
|
|
|
|
|
|
/* 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;
|
2001-06-04 23:56:26 +00:00
|
|
|
strncpy(ifu->fu_via_if.name, arg,
|
|
|
|
sizeof(ifu->fu_via_if.name));
|
1997-06-02 05:02:37 +00:00
|
|
|
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)) {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "bad ip address ``%s''", arg);
|
1997-06-02 05:02:37 +00:00
|
|
|
} else
|
|
|
|
*byname = 0;
|
1996-10-17 01:05:03 +00:00
|
|
|
}
|
|
|
|
|
1998-12-14 18:43:03 +00:00
|
|
|
static void
|
|
|
|
config_pipe(int ac, char **av)
|
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
struct dn_pipe pipe;
|
|
|
|
int i;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
memset(&pipe, 0, sizeof pipe);
|
|
|
|
|
|
|
|
av++; ac--;
|
|
|
|
/* Pipe number */
|
|
|
|
if (ac && isdigit(**av)) {
|
|
|
|
i = atoi(*av); av++; ac--;
|
|
|
|
if (do_pipe == 1)
|
|
|
|
pipe.pipe_nr = i;
|
|
|
|
else
|
|
|
|
pipe.fs.fs_nr = i;
|
|
|
|
}
|
|
|
|
while (ac > 1) {
|
|
|
|
if (!strncmp(*av, "plr", strlen(*av))) {
|
|
|
|
|
|
|
|
double d = strtod(av[1], NULL);
|
|
|
|
if (d > 1)
|
|
|
|
d = 1;
|
|
|
|
else if (d < 0)
|
|
|
|
d = 0;
|
|
|
|
pipe.fs.plr = (int)(d*0x7fffffff);
|
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
|
|
|
} else if (!strncmp(*av, "queue", strlen(*av))) {
|
|
|
|
end = NULL;
|
|
|
|
pipe.fs.qsize = strtoul(av[1], &end, 0);
|
|
|
|
if (*end == 'K' || *end == 'k') {
|
|
|
|
pipe.fs.flags_fs |= DN_QSIZE_IS_BYTES;
|
|
|
|
pipe.fs.qsize *= 1024;
|
|
|
|
} else if (*end == 'B' || !strncmp(end, "by", 2)) {
|
|
|
|
pipe.fs.flags_fs |= DN_QSIZE_IS_BYTES;
|
2000-06-08 10:08:39 +00:00
|
|
|
}
|
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "buckets", strlen(*av))) {
|
|
|
|
pipe.fs.rq_size = strtoul(av[1], NULL, 0);
|
2000-06-08 10:08:39 +00:00
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "mask", strlen(*av))) {
|
|
|
|
/* per-flow queue, mask is dst_ip, dst_port,
|
|
|
|
* src_ip, src_port, proto measured in bits
|
|
|
|
*/
|
|
|
|
u_int32_t a;
|
|
|
|
void *par = NULL;
|
|
|
|
|
|
|
|
pipe.fs.flow_mask.dst_ip = 0;
|
|
|
|
pipe.fs.flow_mask.src_ip = 0;
|
|
|
|
pipe.fs.flow_mask.dst_port = 0;
|
|
|
|
pipe.fs.flow_mask.src_port = 0;
|
|
|
|
pipe.fs.flow_mask.proto = 0;
|
|
|
|
end = NULL;
|
|
|
|
av++; ac--;
|
|
|
|
if (ac >= 1 && !strncmp(*av, "all", strlen(*av))) {
|
|
|
|
/* special case -- all bits are significant */
|
|
|
|
pipe.fs.flow_mask.dst_ip = ~0;
|
|
|
|
pipe.fs.flow_mask.src_ip = ~0;
|
|
|
|
pipe.fs.flow_mask.dst_port = ~0;
|
|
|
|
pipe.fs.flow_mask.src_port = ~0;
|
|
|
|
pipe.fs.flow_mask.proto = ~0;
|
|
|
|
pipe.fs.flags_fs |= DN_HAVE_FLOW_MASK;
|
|
|
|
av++;
|
|
|
|
ac--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (ac >= 1) {
|
|
|
|
int len = strlen(*av);
|
|
|
|
|
|
|
|
if (!strncmp(*av, "dst-ip", len))
|
|
|
|
par = &pipe.fs.flow_mask.dst_ip;
|
|
|
|
else if (!strncmp(*av, "src-ip", len))
|
|
|
|
par = &pipe.fs.flow_mask.src_ip;
|
|
|
|
else if (!strncmp(*av, "dst-port", len))
|
|
|
|
par = &pipe.fs.flow_mask.dst_port;
|
|
|
|
else if (!strncmp(*av, "src-port", len))
|
|
|
|
par = &pipe.fs.flow_mask.src_port;
|
|
|
|
else if (!strncmp(*av, "proto", len))
|
|
|
|
par = &pipe.fs.flow_mask.proto;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (ac < 2)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "mask: %s value"
|
|
|
|
" missing", *av);
|
2001-06-04 23:56:26 +00:00
|
|
|
if (*av[1] == '/') {
|
|
|
|
a = strtoul(av[1]+1, &end, 0);
|
|
|
|
if (a == 32) /* special case... */
|
|
|
|
a = ~0;
|
|
|
|
else
|
|
|
|
a = (1 << a) - 1;
|
|
|
|
} else {
|
|
|
|
a = strtoul(av[1], &end, 0);
|
|
|
|
}
|
|
|
|
if (par == &pipe.fs.flow_mask.src_port
|
|
|
|
|| par == &pipe.fs.flow_mask.dst_port) {
|
|
|
|
if (a >= (1 << 16))
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "mask: %s"
|
|
|
|
" must be 16 bit, not"
|
|
|
|
" 0x%08x", *av, a);
|
2001-06-04 23:56:26 +00:00
|
|
|
*((u_int16_t *)par) = (u_int16_t)a;
|
|
|
|
} else if (par == &pipe.fs.flow_mask.proto) {
|
|
|
|
if (a >= (1 << 8))
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "mask: %s"
|
|
|
|
" must be"
|
|
|
|
" 8 bit, not 0x%08x",
|
|
|
|
*av, a);
|
2001-06-04 23:56:26 +00:00
|
|
|
*((u_int8_t *)par) = (u_int8_t)a;
|
|
|
|
} else
|
|
|
|
*((u_int32_t *)par) = a;
|
|
|
|
if (a != 0)
|
|
|
|
pipe.fs.flags_fs |= DN_HAVE_FLOW_MASK;
|
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
|
|
|
} /* end for */
|
|
|
|
} else if (!strncmp(*av, "red", strlen(*av))
|
|
|
|
|| !strncmp(*av, "gred", strlen(*av))) {
|
|
|
|
/* RED enabled */
|
|
|
|
pipe.fs.flags_fs |= DN_IS_RED;
|
|
|
|
if (*av[0] == 'g')
|
|
|
|
pipe.fs.flags_fs |= DN_IS_GENTLE_RED;
|
|
|
|
if ((end = strsep(&av[1], "/"))) {
|
|
|
|
double w_q = strtod(end, NULL);
|
|
|
|
if (w_q > 1 || w_q <= 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "w_q %f must be "
|
2001-06-04 23:56:26 +00:00
|
|
|
"0 < x <= 1", w_q);
|
|
|
|
pipe.fs.w_q = (int) (w_q * (1 << SCALE_RED));
|
|
|
|
}
|
|
|
|
if ((end = strsep(&av[1], "/"))) {
|
|
|
|
pipe.fs.min_th = strtoul(end, &end, 0);
|
|
|
|
if (*end == 'K' || *end == 'k')
|
|
|
|
pipe.fs.min_th *= 1024;
|
|
|
|
}
|
|
|
|
if ((end = strsep(&av[1], "/"))) {
|
|
|
|
pipe.fs.max_th = strtoul(end, &end, 0);
|
|
|
|
if (*end == 'K' || *end == 'k')
|
|
|
|
pipe.fs.max_th *= 1024;
|
|
|
|
}
|
|
|
|
if ((end = strsep(&av[1], "/"))) {
|
|
|
|
double max_p = strtod(end, NULL);
|
|
|
|
if (max_p > 1 || max_p <= 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "max_p %f must be "
|
2001-06-04 23:56:26 +00:00
|
|
|
"0 < x <= 1", max_p);
|
|
|
|
pipe.fs.max_p =
|
|
|
|
(int)(max_p * (1 << SCALE_RED));
|
|
|
|
}
|
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
|
|
|
} else if (!strncmp(*av, "droptail", strlen(*av))) {
|
|
|
|
/* DROPTAIL */
|
|
|
|
pipe.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
|
|
|
|
av += 1;
|
|
|
|
ac -= 1;
|
|
|
|
} else {
|
|
|
|
int len = strlen(*av);
|
|
|
|
if (do_pipe == 1) {
|
|
|
|
/* some commands are only good for pipes. */
|
|
|
|
if (!strncmp(*av, "bw", len)
|
|
|
|
|| !strncmp(*av, "bandwidth", len)) {
|
|
|
|
if (av[1][0] >= 'a'
|
|
|
|
&& av[1][0] <= 'z') {
|
|
|
|
int l = sizeof(pipe.if_name)-1;
|
|
|
|
/* interface name */
|
|
|
|
strncpy(pipe.if_name, av[1], l);
|
|
|
|
pipe.if_name[l] = '\0';
|
|
|
|
pipe.bandwidth = 0;
|
|
|
|
} else {
|
|
|
|
pipe.if_name[0] = '\0';
|
|
|
|
pipe.bandwidth =
|
|
|
|
strtoul(av[1], &end, 0);
|
|
|
|
if (*end == 'K'
|
|
|
|
|| *end == 'k') {
|
|
|
|
end++;
|
|
|
|
pipe.bandwidth *=
|
|
|
|
1000;
|
|
|
|
} else if (*end == 'M') {
|
|
|
|
end++;
|
|
|
|
pipe.bandwidth *=
|
|
|
|
1000000;
|
|
|
|
}
|
|
|
|
if (*end == 'B'
|
|
|
|
|| !strncmp(end, "by", 2))
|
|
|
|
pipe.bandwidth *= 8;
|
|
|
|
}
|
|
|
|
av += 2; ac -= 2;
|
|
|
|
} else if (!strncmp(*av, "delay", len)) {
|
|
|
|
pipe.delay = strtoul(av[1], NULL, 0);
|
|
|
|
av += 2; ac -= 2;
|
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "unrecognised pipe"
|
|
|
|
" option ``%s''", *av);
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
|
|
|
} else { /* this refers to a queue */
|
|
|
|
if (!strncmp(*av, "weight", len)) {
|
|
|
|
pipe.fs.weight =
|
|
|
|
strtoul(av[1], &end, 0);
|
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
|
|
|
} else if (!strncmp(*av, "pipe", len)) {
|
|
|
|
pipe.fs.parent_nr =
|
|
|
|
strtoul(av[1], &end, 0);
|
|
|
|
av += 2;
|
|
|
|
ac -= 2;
|
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "unrecognised option "
|
2001-06-04 23:56:26 +00:00
|
|
|
"``%s''", *av);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-06-08 10:08:39 +00:00
|
|
|
if (do_pipe == 1) {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.pipe_nr == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "pipe_nr %d must be > 0",
|
|
|
|
pipe.pipe_nr);
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.delay > 10000)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "delay %d must be < 10000",
|
|
|
|
pipe.delay);
|
2000-06-08 10:08:39 +00:00
|
|
|
} else { /* do_pipe == 2, queue */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.fs.parent_nr == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "pipe %d must be > 0",
|
|
|
|
pipe.fs.parent_nr);
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.fs.weight >100)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "weight %d must be <= 100",
|
2001-06-04 23:56:26 +00:00
|
|
|
pipe.fs.weight);
|
|
|
|
}
|
|
|
|
if (pipe.fs.flags_fs & DN_QSIZE_IS_BYTES) {
|
|
|
|
if (pipe.fs.qsize > 1024*1024)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "queue size %d, must be < 1MB",
|
2001-06-04 23:56:26 +00:00
|
|
|
pipe.fs.qsize);
|
2000-06-08 10:08:39 +00:00
|
|
|
} else {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.fs.qsize > 100)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "queue size %d, must be"
|
|
|
|
" 2 <= x <= 100", pipe.fs.qsize);
|
2000-06-08 10:08:39 +00:00
|
|
|
}
|
|
|
|
if (pipe.fs.flags_fs & DN_IS_RED) {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.fs.min_th >= pipe.fs.max_th)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "min_th %d must be < than max_th %d",
|
2001-06-04 23:56:26 +00:00
|
|
|
pipe.fs.min_th, pipe.fs.max_th);
|
|
|
|
if (pipe.fs.max_th == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "max_th must be > 0");
|
2001-06-04 23:56:26 +00:00
|
|
|
if (pipe.bandwidth) {
|
|
|
|
size_t len;
|
|
|
|
int lookup_depth, avg_pkt_size;
|
|
|
|
double s, idle, weight, w_q;
|
|
|
|
struct clockinfo clock;
|
|
|
|
int t;
|
|
|
|
|
|
|
|
len = sizeof(int);
|
|
|
|
if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
|
2000-06-08 10:08:39 +00:00
|
|
|
&lookup_depth, &len, NULL, 0) == -1)
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
errx(1, "sysctlbyname(\"%s\")",
|
|
|
|
"net.inet.ip.dummynet.red_lookup_depth");
|
|
|
|
if (lookup_depth == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
|
|
|
|
" must greater than zero");
|
2000-06-08 10:08:39 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
len = sizeof(int);
|
|
|
|
if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
|
2000-06-08 10:08:39 +00:00
|
|
|
&avg_pkt_size, &len, NULL, 0) == -1)
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
errx(1, "sysctlbyname(\"%s\")",
|
|
|
|
"net.inet.ip.dummynet.red_avg_pkt_size");
|
|
|
|
if (avg_pkt_size == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "net.inet.ip.dummynet.red_avg_pkt_size must"
|
2001-06-04 23:56:26 +00:00
|
|
|
"greater than zero");
|
|
|
|
|
|
|
|
len = sizeof(struct clockinfo);
|
|
|
|
if (sysctlbyname("kern.clockrate",
|
|
|
|
&clock, &len, NULL, 0) == -1)
|
|
|
|
errx(1, "sysctlbyname(\"%s\")",
|
|
|
|
"kern.clockrate");
|
|
|
|
|
|
|
|
/* ticks needed for sending a medium-sized packet */
|
|
|
|
s = clock.hz * avg_pkt_size * 8 / pipe.bandwidth;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* max idle time (in ticks) before avg queue size
|
|
|
|
* becomes 0.
|
|
|
|
* NOTA: (3/w_q) is approx the value x so that
|
|
|
|
* (1-w_q)^x < 10^-3.
|
|
|
|
*/
|
|
|
|
w_q = ((double)pipe.fs.w_q) / (1 << SCALE_RED);
|
|
|
|
idle = s * 3. / w_q;
|
|
|
|
pipe.fs.lookup_step = (int)idle / lookup_depth;
|
|
|
|
if (!pipe.fs.lookup_step)
|
|
|
|
pipe.fs.lookup_step = 1;
|
|
|
|
weight = 1 - w_q;
|
|
|
|
for (t = pipe.fs.lookup_step; t > 0; --t)
|
|
|
|
weight *= weight;
|
|
|
|
pipe.fs.lookup_weight =
|
|
|
|
(int)(weight * (1 << SCALE_RED));
|
|
|
|
}
|
2000-06-08 10:08:39 +00:00
|
|
|
}
|
1998-12-14 18:43:03 +00:00
|
|
|
#if 0
|
2001-06-04 23:56:26 +00:00
|
|
|
printf("configuring pipe %d bw %d delay %d size %d\n",
|
|
|
|
pipe.pipe_nr, pipe.bandwidth, pipe.delay, pipe.queue_size);
|
1998-12-14 18:43:03 +00:00
|
|
|
#endif
|
2001-06-04 23:56:26 +00:00
|
|
|
i = setsockopt(s, IPPROTO_IP, IP_DUMMYNET_CONFIGURE, &pipe,
|
|
|
|
sizeof pipe);
|
|
|
|
if (i)
|
|
|
|
err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
|
|
|
|
|
1998-12-14 18:43:03 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
add(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 */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac > 1 && !strncmp(*av, "prob", strlen(*av))) {
|
1999-08-11 15:36:13 +00:00
|
|
|
double d = strtod(av[1], NULL);
|
|
|
|
if (d <= 0 || d > 1)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "illegal match prob. %s", av[1]);
|
1999-08-11 15:36:13 +00:00
|
|
|
if (d != 1) { /* 1 means always match */
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_RND_MATCH;
|
1999-08-11 15:36:13 +00:00
|
|
|
/* we really store dont_match probability */
|
2001-06-04 23:56:26 +00:00
|
|
|
(long)rule.pipe_ptr = (long)((1 - d) * 0x7fffffff);
|
1999-08-11 15:36:13 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
av += 2; ac -= 2;
|
1999-08-11 15:36:13 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
if (ac == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing action");
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!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--;
|
2001-06-04 23:56:26 +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--;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "pipe", strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_PIPE; av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing pipe number");
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_divert_port = strtoul(*av, NULL, 0); av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "queue", strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_QUEUE; av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing queue number");
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_divert_port = strtoul(*av, NULL, 0); av++; ac--;
|
|
|
|
} 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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_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
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "illegal %s port", "divert");
|
1997-07-25 03:13:46 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "tee", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_TEE; av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_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
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "illegal %s port",
|
|
|
|
"tee divert");
|
1997-07-25 03:13:46 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "fwd", strlen(*av))
|
|
|
|
|| !strncmp(*av, "forward", strlen(*av))) {
|
1998-07-06 03:20:19 +00:00
|
|
|
struct in_addr dummyip;
|
|
|
|
char *pp;
|
|
|
|
rule.fw_flg |= IP_FW_F_FWD; av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing forwarding IP address");
|
1998-07-06 03:20:19 +00:00
|
|
|
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';
|
2000-10-04 07:59:19 +00:00
|
|
|
i = lookup_port(pp, 0, 1, 0);
|
1999-06-11 09:43:53 +00:00
|
|
|
if (i == -1)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "illegal forwarding"
|
|
|
|
" port ``%s''", pp);
|
1999-06-11 09:43:53 +00:00
|
|
|
else
|
|
|
|
rule.fw_fwd_ip.sin_port = (u_short)i;
|
1998-07-06 03:20:19 +00:00
|
|
|
}
|
|
|
|
fill_ip(&(rule.fw_fwd_ip.sin_addr), &dummyip, &ac, &av);
|
|
|
|
if (rule.fw_fwd_ip.sin_addr.s_addr == 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "illegal forwarding IP address");
|
1998-07-06 03:20:19 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "skipto", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_SKIPTO; av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing skipto rule number");
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_skipto_rule = strtoul(*av, NULL, 0); av++; ac--;
|
2001-06-04 23:56:26 +00:00
|
|
|
} 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--;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "reject", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_REJECT; av++; ac--;
|
|
|
|
rule.fw_reject_code = ICMP_UNREACH_HOST;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "reset", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_REJECT; av++; ac--;
|
|
|
|
rule.fw_reject_code = IP_FW_REJECT_RST; /* check TCP later */
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "unreach", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_REJECT; av++; ac--;
|
|
|
|
fill_reject_code(&rule.fw_reject_code, *av); av++; ac--;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "check-state", strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_F_CHECK_S; av++; ac--;
|
|
|
|
goto done;
|
1996-02-24 00:20:56 +00:00
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "invalid action ``%s''", *av);
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* [log] */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "log", strlen(*av))) {
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_PRN; av++; ac--;
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "logamount", strlen(*av))) {
|
1999-08-01 16:57:24 +00:00
|
|
|
if (!(rule.fw_flg & IP_FW_F_PRN))
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "``logamount'' not valid without"
|
|
|
|
" ``log''");
|
1999-08-01 16:57:24 +00:00
|
|
|
ac--; av++;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "``logamount'' requires argument");
|
1999-08-01 16:57:24 +00:00
|
|
|
rule.fw_logamount = atoi(*av);
|
2000-04-30 06:44:11 +00:00
|
|
|
if (rule.fw_logamount < 0)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "``logamount'' argument must be"
|
|
|
|
" positive");
|
2000-04-30 06:44:11 +00:00
|
|
|
if (rule.fw_logamount == 0)
|
|
|
|
rule.fw_logamount = -1;
|
1999-08-01 16:57:24 +00:00
|
|
|
ac--; av++;
|
|
|
|
}
|
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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing protocol");
|
1997-06-02 05:02:37 +00:00
|
|
|
if ((proto = atoi(*av)) > 0) {
|
|
|
|
rule.fw_prot = proto; av++; ac--;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "all", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_prot = IPPROTO_IP; av++; ac--;
|
|
|
|
} else if ((pe = getprotobyname(*av)) != NULL) {
|
|
|
|
rule.fw_prot = pe->p_proto; av++; ac--;
|
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "invalid protocol ``%s''", *av);
|
1997-06-02 05:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "``reset'' is only valid for tcp packets");
|
1994-10-28 15:06:53 +00:00
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
/* from */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "from", strlen(*av))) { av++; ac--; }
|
1997-06-02 05:02:37 +00:00
|
|
|
else
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing ``from''");
|
1994-10-28 15:06:53 +00:00
|
|
|
|
2001-06-04 23:56:26 +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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing arguments");
|
1997-01-16 21:04:29 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "me", strlen(*av))) {
|
2001-02-13 14:12:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_SME;
|
|
|
|
av++; ac--;
|
|
|
|
} else {
|
|
|
|
fill_ip(&rule.fw_src, &rule.fw_smsk, &ac, &av);
|
|
|
|
}
|
1994-10-28 15:06:53 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && (isdigit(**av)
|
|
|
|
|| lookup_port(*av, rule.fw_prot, 1, 1) >= 0)) {
|
1997-06-02 05:02:37 +00:00
|
|
|
u_short nports = 0;
|
2001-06-04 23:56:26 +00:00
|
|
|
int retval;
|
1997-06-02 05:02:37 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
retval = fill_port(&nports, rule.fw_uar.fw_pts,
|
|
|
|
0, *av, rule.fw_prot);
|
2000-01-08 11:19:19 +00:00
|
|
|
if (retval == 1)
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_SRNG;
|
2000-01-08 11:19:19 +00:00
|
|
|
else if (retval == 2)
|
|
|
|
rule.fw_flg |= IP_FW_F_SMSK;
|
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 */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "to", strlen(*av))) { av++; ac--; }
|
1997-06-02 05:02:37 +00:00
|
|
|
else
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing ``to''");
|
1995-10-01 21:54:05 +00:00
|
|
|
|
2001-06-04 23:56:26 +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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing arguments");
|
1995-10-01 21:54:05 +00:00
|
|
|
|
2001-02-13 14:12:37 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && !strncmp(*av, "me", strlen(*av))) {
|
2001-02-13 14:12:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DME;
|
|
|
|
av++; ac--;
|
|
|
|
} else {
|
|
|
|
fill_ip(&rule.fw_dst, &rule.fw_dmsk, &ac, &av);
|
|
|
|
}
|
1995-10-01 21:54:05 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac && (isdigit(**av)
|
|
|
|
|| lookup_port(*av, rule.fw_prot, 1, 1) >= 0)) {
|
1997-06-02 05:02:37 +00:00
|
|
|
u_short nports = 0;
|
2001-06-04 23:56:26 +00:00
|
|
|
int retval;
|
1997-06-02 05:02:37 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
retval = fill_port(&nports, rule.fw_uar.fw_pts,
|
|
|
|
IP_FW_GETNSRCP(&rule), *av, rule.fw_prot);
|
2000-01-08 11:19:19 +00:00
|
|
|
if (retval == 1)
|
1996-02-24 00:20:56 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_DRNG;
|
2000-01-08 11:19:19 +00:00
|
|
|
else if (retval == 2)
|
|
|
|
rule.fw_flg |= IP_FW_F_DMSK;
|
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))) {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "only TCP and UDP protocols are valid"
|
1997-06-02 05:02:37 +00:00
|
|
|
" with port specifications");
|
1996-02-24 13:39:46 +00:00
|
|
|
}
|
|
|
|
|
1996-02-24 00:20:56 +00:00
|
|
|
while (ac) {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(*av, "uid", strlen(*av))) {
|
1999-06-19 18:43:33 +00:00
|
|
|
struct passwd *pwd;
|
1999-09-03 18:18:46 +00:00
|
|
|
char *end;
|
|
|
|
uid_t uid;
|
1999-06-19 18:43:33 +00:00
|
|
|
|
|
|
|
rule.fw_flg |= IP_FW_F_UID;
|
|
|
|
ac--; av++;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "``uid'' requires argument");
|
1999-06-19 18:43:33 +00:00
|
|
|
|
1999-09-03 18:18:46 +00:00
|
|
|
uid = strtoul(*av, &end, 0);
|
|
|
|
if (*end == '\0')
|
|
|
|
pwd = getpwuid(uid);
|
|
|
|
else
|
|
|
|
pwd = getpwnam(*av);
|
|
|
|
if (pwd == NULL)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "uid \"%s\" is"
|
|
|
|
" nonexistent", *av);
|
1999-09-03 18:18:46 +00:00
|
|
|
rule.fw_uid = pwd->pw_uid;
|
1999-06-19 18:43:33 +00:00
|
|
|
ac--; av++;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "gid", strlen(*av))) {
|
1999-06-19 18:43:33 +00:00
|
|
|
struct group *grp;
|
1999-09-03 18:18:46 +00:00
|
|
|
char *end;
|
|
|
|
gid_t gid;
|
1999-06-19 18:43:33 +00:00
|
|
|
|
|
|
|
rule.fw_flg |= IP_FW_F_GID;
|
|
|
|
ac--; av++;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "``gid'' requires argument");
|
1999-06-19 18:43:33 +00:00
|
|
|
|
1999-09-03 18:18:46 +00:00
|
|
|
gid = strtoul(*av, &end, 0);
|
|
|
|
if (*end == '\0')
|
|
|
|
grp = getgrgid(gid);
|
|
|
|
else
|
|
|
|
grp = getgrnam(*av);
|
|
|
|
if (grp == NULL)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "gid \"%s\" is"
|
|
|
|
" nonexistent", *av);
|
1999-09-03 18:18:46 +00:00
|
|
|
rule.fw_gid = grp->gr_gid;
|
1999-06-19 18:43:33 +00:00
|
|
|
ac--; av++;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (!strncmp(*av, "in", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_IN;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "keep-state", strlen(*av))) {
|
|
|
|
u_long type;
|
|
|
|
rule.fw_flg |= IP_FW_F_KEEP_S;
|
|
|
|
|
|
|
|
av++; ac--;
|
|
|
|
if (ac > 0 && (type = atoi(*av)) != 0) {
|
|
|
|
(int)rule.next_rule_ptr = type;
|
|
|
|
av++; ac--;
|
|
|
|
}
|
|
|
|
} else if (!strncmp(*av, "bridged", strlen(*av))) {
|
|
|
|
rule.fw_flg |= IP_FW_BRIDGED;
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "out", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_OUT;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av, "xmit", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
union ip_fw_if ifu;
|
|
|
|
int byname;
|
|
|
|
|
|
|
|
if (saw_via) {
|
|
|
|
badviacombo:
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "``via'' is incompatible"
|
1997-06-02 05:02:37 +00:00
|
|
|
" with ``xmit'' and ``recv''");
|
1996-06-29 01:28:19 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
saw_xmrc = 1;
|
2001-06-04 23:56:26 +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;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av, "recv", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
union ip_fw_if ifu;
|
|
|
|
int byname;
|
|
|
|
|
|
|
|
if (saw_via)
|
|
|
|
goto badviacombo;
|
|
|
|
saw_xmrc = 1;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
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;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (ac && !strncmp(*av, "via", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
union ip_fw_if ifu;
|
|
|
|
int byname = 0;
|
|
|
|
|
|
|
|
if (saw_xmrc)
|
|
|
|
goto badviacombo;
|
|
|
|
saw_via = 1;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
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);
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "fragment", strlen(*av))) {
|
1997-06-02 05:02:37 +00:00
|
|
|
rule.fw_flg |= IP_FW_F_FRAG;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "ipoptions", strlen(*av))
|
|
|
|
|| !strncmp(*av, "ipopts", strlen(*av))) {
|
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
1997-06-02 05:02:37 +00:00
|
|
|
" for ``ipoptions''");
|
2000-10-02 03:03:31 +00:00
|
|
|
rule.fw_ipflg |= IP_FW_IF_IPOPT;
|
1996-02-24 00:20:56 +00:00
|
|
|
fill_ipopt(&rule.fw_ipopt, &rule.fw_ipnopt, av);
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "iplen", strlen(*av))) {
|
|
|
|
av++; ac--;
|
2000-10-02 03:03:31 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``iplen''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_IPLEN;
|
|
|
|
rule.fw_iplen = (u_short)strtoul(*av, NULL, 0);
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "ipid", strlen(*av))) {
|
2000-10-03 11:23:29 +00:00
|
|
|
unsigned long ipid;
|
|
|
|
char *c;
|
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
2000-10-02 03:03:31 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``ipid''");
|
2000-10-03 11:23:29 +00:00
|
|
|
ipid = strtoul(*av, &c, 0);
|
|
|
|
if (*c != '\0')
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "argument to ipid must"
|
|
|
|
" be numeric");
|
2000-10-03 11:23:29 +00:00
|
|
|
if (ipid > 65535)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "argument to ipid out"
|
|
|
|
" of range");
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_ipflg |= IP_FW_IF_IPID;
|
|
|
|
rule.fw_ipid = (u_short)ipid;
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "iptos", strlen(*av))) {
|
|
|
|
av++; ac--;
|
2000-10-02 03:03:31 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``iptos''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_IPTOS;
|
2000-10-02 03:03:31 +00:00
|
|
|
fill_iptos(&rule.fw_iptos, &rule.fw_ipntos, av);
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "ipttl", strlen(*av))) {
|
|
|
|
av++; ac--;
|
2000-10-02 03:03:31 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``ipttl''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_IPTTL;
|
|
|
|
rule.fw_ipttl = (u_short)strtoul(*av, NULL, 0);
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "ipversion", strlen(*av))
|
|
|
|
|| !strncmp(*av, "ipver", strlen(*av))) {
|
|
|
|
av++; ac--;
|
2000-10-02 03:03:31 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``ipversion''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_IPVER;
|
|
|
|
rule.fw_ipver = (u_short)strtoul(*av, NULL, 0);
|
|
|
|
av++; ac--;
|
|
|
|
} else if (rule.fw_prot == IPPROTO_TCP) {
|
|
|
|
if (!strncmp(*av, "established", strlen(*av))) {
|
o IPFW incorrectly handled filtering in the presence of previously
reserved and now allocated TCP flags in incoming packets. This patch
stops overloading those bits in the IP firewall rules, and moves
colliding flags to a seperate field, ipflg. The IPFW userland
management tool, ipfw(8), is updated to reflect this change. New TCP
flags related to ECN are now included in tcp.h for reference, although
we don't currently implement TCP+ECN.
o To use this fix without completely rebuilding, it is sufficient to copy
ip_fw.h and tcp.h into your appropriate include directory, then rebuild
the ipfw kernel module, and ipfw tool, and install both. Note that a
mismatch between module and userland tool will result in incorrect
installation of firewall rules that may have unexpected effects. This
is an MFC candidate, following shakedown. This bug does not appear
to affect ipfilter.
Reviewed by: security-officer, billf
Reported by: Aragon Gouveia <aragon@phat.za.net>
2001-01-09 03:10:30 +00:00
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPEST;
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "setup", strlen(*av))) {
|
1996-02-24 13:39:46 +00:00
|
|
|
rule.fw_tcpf |= IP_FW_TCPF_SYN;
|
|
|
|
rule.fw_tcpnf |= IP_FW_TCPF_ACK;
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPFLG;
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "tcpflags", strlen(*av))
|
|
|
|
|| !strncmp(*av, "tcpflgs", strlen(*av))) {
|
|
|
|
av++; ac--;
|
1997-06-02 05:02:37 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
1997-06-02 05:02:37 +00:00
|
|
|
" for ``tcpflags''");
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPFLG;
|
|
|
|
fill_tcpflag(&rule.fw_tcpf,
|
|
|
|
&rule.fw_tcpnf, av);
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "tcpoptions", strlen(*av))
|
|
|
|
|| !strncmp(*av, "tcpopts", strlen(*av))) {
|
|
|
|
av++; ac--;
|
2000-06-08 15:34:51 +00:00
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2000-07-17 03:02:15 +00:00
|
|
|
" for ``tcpoptions''");
|
2001-06-04 23:56:26 +00:00
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPOPT;
|
|
|
|
fill_tcpopts(&rule.fw_tcpopt,
|
|
|
|
&rule.fw_tcpnopt, av);
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "tcpseq", strlen(*av))) {
|
2000-10-02 03:03:31 +00:00
|
|
|
av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``tcpseq''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPSEQ;
|
|
|
|
rule.fw_tcpseq =
|
|
|
|
htonl(strtoul(*av, NULL, 0));
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "tcpack", strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``tcpack''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPACK;
|
|
|
|
rule.fw_tcpack =
|
|
|
|
htonl(strtoul(*av, NULL, 0));
|
|
|
|
av++; ac--;
|
|
|
|
} else if (!strncmp(*av, "tcpwin", strlen(*av))) {
|
|
|
|
av++; ac--;
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
2001-06-04 23:56:26 +00:00
|
|
|
" for ``tcpwin''");
|
|
|
|
rule.fw_ipflg |= IP_FW_IF_TCPWIN;
|
|
|
|
rule.fw_tcpwin =
|
|
|
|
htons((u_short)strtoul(*av, NULL, 0));
|
|
|
|
av++; ac--;
|
2001-07-10 05:44:51 +00:00
|
|
|
} else
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "unknown or out of order"
|
|
|
|
" argument ``%s''", *av);
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if (rule.fw_prot == IPPROTO_ICMP) {
|
|
|
|
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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "missing argument"
|
1997-06-02 05:02:37 +00:00
|
|
|
" 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);
|
2001-06-04 23:56:26 +00:00
|
|
|
av++; ac--;
|
2001-07-10 05:44:51 +00:00
|
|
|
} else
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "unknown or out of"
|
|
|
|
" order argument ``%s''", *av);
|
2001-07-10 05:44:51 +00:00
|
|
|
} else
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_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;
|
2001-06-04 23:56:26 +00:00
|
|
|
} else if ((rule.fw_flg & IP_FW_F_OIFACE)
|
|
|
|
&& (rule.fw_flg & IP_FW_F_IN)) {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "can't check xmit interface of incoming"
|
|
|
|
" packets");
|
2001-06-04 23:56:26 +00:00
|
|
|
}
|
1997-06-02 05:02:37 +00:00
|
|
|
|
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)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "can't mix 'frag' and tcpflags");
|
1998-02-12 00:57:06 +00:00
|
|
|
|
|
|
|
if (rule.fw_nports)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "can't mix 'frag' and port"
|
|
|
|
" specifications");
|
1998-02-12 00:57:06 +00:00
|
|
|
}
|
1999-08-01 16:57:24 +00:00
|
|
|
if (rule.fw_flg & IP_FW_F_PRN) {
|
|
|
|
if (!rule.fw_logamount) {
|
|
|
|
size_t len = sizeof(int);
|
|
|
|
|
|
|
|
if (sysctlbyname("net.inet.ip.fw.verbose_limit",
|
|
|
|
&rule.fw_logamount, &len, NULL, 0) == -1)
|
|
|
|
errx(1, "sysctlbyname(\"%s\")",
|
|
|
|
"net.inet.ip.fw.verbose_limit");
|
2000-04-30 06:44:11 +00:00
|
|
|
} else if (rule.fw_logamount == -1)
|
|
|
|
rule.fw_logamount = 0;
|
1999-08-01 16:57:24 +00:00
|
|
|
rule.fw_loghighest = rule.fw_logamount;
|
|
|
|
}
|
2000-02-10 14:25:26 +00:00
|
|
|
done:
|
2000-10-12 07:59:14 +00:00
|
|
|
i = sizeof(rule);
|
|
|
|
if (getsockopt(s, IPPROTO_IP, IP_FW_ADD, &rule, &i) == -1)
|
|
|
|
err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD");
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
2001-06-04 23:56:26 +00:00
|
|
|
show_ipfw(&rule);
|
1994-10-28 15:06:53 +00:00
|
|
|
}
|
|
|
|
|
1997-06-02 05:02:37 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
zero (int ac, char *av[])
|
1996-06-09 23:46:22 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
struct ip_fw rule;
|
|
|
|
int failed = EX_OK;
|
|
|
|
|
1996-06-09 23:46:22 +00:00
|
|
|
av++; ac--;
|
|
|
|
|
|
|
|
if (!ac) {
|
|
|
|
/* clear all entries */
|
2001-06-04 23:56:26 +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");
|
2001-06-04 23:56:26 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&rule, 0, sizeof rule);
|
|
|
|
while (ac) {
|
|
|
|
/* Rule number */
|
|
|
|
if (isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
|
|
|
if (setsockopt(s, IPPROTO_IP,
|
|
|
|
IP_FW_ZERO, &rule, sizeof rule)) {
|
|
|
|
warn("rule %u: setsockopt(IP_FW_ZERO)",
|
|
|
|
rule.fw_number);
|
|
|
|
failed = EX_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
else if (!do_quiet)
|
|
|
|
printf("Entry %d cleared\n",
|
|
|
|
rule.fw_number);
|
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "invalid rule number ``%s''", *av);
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
if (failed != EX_OK)
|
|
|
|
exit(failed);
|
1996-06-09 23:46:22 +00:00
|
|
|
}
|
|
|
|
|
1999-08-01 16:57:24 +00:00
|
|
|
static void
|
2001-06-04 23:56:26 +00:00
|
|
|
resetlog (int ac, char *av[])
|
1999-08-01 16:57:24 +00:00
|
|
|
{
|
2001-06-04 23:56:26 +00:00
|
|
|
struct ip_fw rule;
|
|
|
|
int failed = EX_OK;
|
|
|
|
|
1999-08-01 16:57:24 +00:00
|
|
|
av++; ac--;
|
|
|
|
|
|
|
|
if (!ac) {
|
|
|
|
/* clear all entries */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_FW_RESETLOG, NULL, 0) < 0)
|
|
|
|
err(EX_UNAVAILABLE, "setsockopt(IP_FW_RESETLOG)");
|
1999-08-01 16:57:24 +00:00
|
|
|
if (!do_quiet)
|
|
|
|
printf("Logging counts reset.\n");
|
2001-06-04 23:56:26 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&rule, 0, sizeof rule);
|
|
|
|
while (ac) {
|
|
|
|
/* Rule number */
|
|
|
|
if (isdigit(**av)) {
|
|
|
|
rule.fw_number = atoi(*av); av++; ac--;
|
|
|
|
if (setsockopt(s, IPPROTO_IP,
|
|
|
|
IP_FW_RESETLOG, &rule, sizeof rule)) {
|
|
|
|
warn("rule %u: setsockopt(IP_FW_RESETLOG)",
|
|
|
|
rule.fw_number);
|
|
|
|
failed = EX_UNAVAILABLE;
|
|
|
|
} else if (!do_quiet)
|
|
|
|
printf("Entry %d logging count reset\n",
|
|
|
|
rule.fw_number);
|
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_DATAERR, "invalid rule number ``%s''", *av);
|
1999-08-01 16:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
if (failed != EX_OK)
|
|
|
|
exit(failed);
|
1999-08-01 16:57:24 +00:00
|
|
|
}
|
|
|
|
|
1998-09-28 22:56:37 +00:00
|
|
|
static int
|
2001-06-04 23:56:26 +00:00
|
|
|
ipfw_main(int ac, char **av)
|
1994-11-16 10:18:18 +00:00
|
|
|
{
|
1994-10-28 15:06:53 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
int ch;
|
1994-10-28 15:06:53 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (ac == 1)
|
2001-07-22 06:40:11 +00:00
|
|
|
show_usage();
|
2000-10-11 13:02:30 +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;
|
2001-06-04 23:56:26 +00:00
|
|
|
while ((ch = getopt(ac, av, "s:adefNqtv")) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 's': /* sort */
|
|
|
|
do_sort = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
do_acct = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
do_dynamic = 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
do_expired = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
do_force = 1;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
do_resolv = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
do_quiet = 1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
do_time = 1;
|
|
|
|
break;
|
|
|
|
case 'v': /* verbose */
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
default:
|
2001-07-22 06:40:11 +00:00
|
|
|
show_usage();
|
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;
|
2001-07-22 06:40:11 +00:00
|
|
|
if (*(av += optind) == NULL)
|
|
|
|
errx(EX_USAGE, "bad arguments, for usage summary ``ipfw''");
|
1994-10-28 15:06:53 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
if (!strncmp(*av, "pipe", strlen(*av))) {
|
|
|
|
do_pipe = 1;
|
|
|
|
ac--;
|
|
|
|
av++;
|
|
|
|
} else if (!strncmp(*av, "queue", strlen(*av))) {
|
|
|
|
do_pipe = 2;
|
|
|
|
ac--;
|
|
|
|
av++;
|
|
|
|
}
|
|
|
|
if (!ac)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "pipe requires arguments");
|
2001-06-04 23:56:26 +00:00
|
|
|
|
|
|
|
/* allow argument swapping */
|
|
|
|
if (ac > 1 && *av[0] >= '0' && *av[0] <= '9') {
|
|
|
|
char *p = av[0];
|
|
|
|
av[0] = av[1];
|
|
|
|
av[1] = p;
|
1998-12-27 11:23:05 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
if (!strncmp(*av, "add", strlen(*av))) {
|
2001-06-04 23:56:26 +00:00
|
|
|
add(ac, av);
|
|
|
|
} else if (do_pipe && !strncmp(*av, "config", strlen(*av))) {
|
|
|
|
config_pipe(ac, av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "delete", strlen(*av))) {
|
2001-06-04 23:56:26 +00:00
|
|
|
delete(ac, av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "flush", strlen(*av))) {
|
1996-08-31 17:58:23 +00:00
|
|
|
int do_flush = 0;
|
|
|
|
|
2001-06-04 23:56:26 +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] ");
|
2001-06-04 23:56:26 +00:00
|
|
|
fflush(stdout);
|
1996-08-31 17:58:23 +00:00
|
|
|
do {
|
|
|
|
c = toupper(getc(stdin));
|
|
|
|
while (c != '\n' && getc(stdin) != '\n')
|
|
|
|
if (feof(stdin))
|
|
|
|
return (0);
|
|
|
|
} while (c != 'Y' && c != 'N');
|
|
|
|
printf("\n");
|
2001-06-04 23:56:26 +00:00
|
|
|
if (c == 'Y')
|
1996-08-31 17:58:23 +00:00
|
|
|
do_flush = 1;
|
|
|
|
}
|
2001-06-04 23:56:26 +00:00
|
|
|
if (do_flush) {
|
1999-05-24 10:01:22 +00:00
|
|
|
if (setsockopt(s, IPPROTO_IP,
|
2001-06-04 23:56:26 +00:00
|
|
|
do_pipe ? IP_DUMMYNET_FLUSH : IP_FW_FLUSH,
|
|
|
|
NULL, 0) < 0)
|
|
|
|
err(EX_UNAVAILABLE, "setsockopt(IP_%s_FLUSH)",
|
|
|
|
do_pipe ? "DUMMYNET" : "FW");
|
1997-02-10 15:36:54 +00:00
|
|
|
if (!do_quiet)
|
1999-06-02 05:59:48 +00:00
|
|
|
printf("Flushed all %s.\n",
|
|
|
|
do_pipe ? "pipes" : "rules");
|
1996-08-31 17:58:23 +00:00
|
|
|
}
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "zero", strlen(*av))) {
|
2001-06-04 23:56:26 +00:00
|
|
|
zero(ac, av);
|
1999-08-01 16:57:24 +00:00
|
|
|
} else if (!strncmp(*av, "resetlog", strlen(*av))) {
|
2001-06-04 23:56:26 +00:00
|
|
|
resetlog(ac, av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "print", strlen(*av))) {
|
2001-06-04 23:56:26 +00:00
|
|
|
list(--ac, ++av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else if (!strncmp(*av, "list", strlen(*av))) {
|
2001-06-04 23:56:26 +00:00
|
|
|
list(--ac, ++av);
|
1997-02-10 15:36:54 +00:00
|
|
|
} else if (!strncmp(*av, "show", strlen(*av))) {
|
|
|
|
do_acct++;
|
2001-06-04 23:56:26 +00:00
|
|
|
list(--ac, ++av);
|
1996-02-24 00:20:56 +00:00
|
|
|
} else {
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "bad arguments, for usage summary ``ipfw''");
|
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
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
int
|
|
|
|
main(int ac, char *av[])
|
1995-10-23 03:58:06 +00:00
|
|
|
{
|
|
|
|
#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];
|
2001-06-04 23:56:26 +00:00
|
|
|
int i, c, lineno, qflag, pflag, status;
|
1998-11-23 10:54:28 +00:00
|
|
|
FILE *f = NULL;
|
|
|
|
pid_t preproc = 0;
|
1995-10-23 03:58:06 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
|
|
|
if (s < 0)
|
1998-01-07 02:23:04 +00:00
|
|
|
err(EX_UNAVAILABLE, "socket");
|
1996-02-24 00:20:56 +00:00
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
setbuf(stdout, 0);
|
1996-02-24 00:20:56 +00:00
|
|
|
|
2000-02-10 14:25:26 +00:00
|
|
|
/*
|
2001-04-13 06:49:47 +00:00
|
|
|
* Only interpret the last command line argument as a file to
|
|
|
|
* be preprocessed if it is specified as an absolute pathname.
|
2000-02-10 14:25:26 +00:00
|
|
|
*/
|
|
|
|
|
2000-10-11 12:17:06 +00:00
|
|
|
if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0) {
|
1998-11-23 10:54:28 +00:00
|
|
|
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:
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "bad arguments, for usage"
|
|
|
|
" summary ``ipfw''");
|
1998-11-23 10:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
av += optind;
|
|
|
|
ac -= optind;
|
|
|
|
if (ac != 1)
|
2001-07-22 06:40:11 +00:00
|
|
|
errx(EX_USAGE, "extraneous filename arguments");
|
1998-11-23 10:54:28 +00:00
|
|
|
|
|
|
|
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 */
|
2001-06-04 23:56:26 +00:00
|
|
|
if (dup2(fileno(f), 0) == -1
|
|
|
|
|| dup2(pipedes[1], 1) == -1)
|
1998-11-23 10:54:28 +00:00
|
|
|
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';
|
2001-06-04 23:56:26 +00:00
|
|
|
i = 1;
|
|
|
|
if (qflag)
|
|
|
|
args[i++] = "-q";
|
1998-08-04 14:41:37 +00:00
|
|
|
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)
|
2001-06-04 23:56:26 +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
|
|
|
|
2001-06-04 23:56:26 +00:00
|
|
|
ipfw_main(i, args);
|
1995-10-23 03:58:06 +00:00
|
|
|
}
|
|
|
|
fclose(f);
|
1998-11-23 10:54:28 +00:00
|
|
|
if (pflag) {
|
2001-06-04 23:56:26 +00:00
|
|
|
if (waitpid(preproc, &status, 0) == -1)
|
|
|
|
errx(EX_OSERR, "waitpid()");
|
|
|
|
if (WIFEXITED(status) && 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));
|
1998-11-23 10:54:28 +00:00
|
|
|
}
|
2001-06-04 23:56:26 +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
|
|
|
}
|