1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Filter command Interface
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1999-05-08 11:07:56 +00:00
|
|
|
* $Id: filter.c,v 1.27 1999/01/28 01:56:31 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO: Shoud send ICMP error message when we discard packets.
|
|
|
|
*/
|
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
1995-09-17 16:14:49 +00:00
|
|
|
#include <netdb.h>
|
1998-03-16 22:54:35 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
1998-06-15 19:06:25 +00:00
|
|
|
#include "defs.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "command.h"
|
1997-06-09 03:27:43 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
1998-01-29 23:11:44 +00:00
|
|
|
#include "iplist.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "throughput.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1998-01-30 19:46:07 +00:00
|
|
|
#include "hdlc.h"
|
1998-01-31 02:48:30 +00:00
|
|
|
#include "fsm.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "lcp.h"
|
|
|
|
#include "ccp.h"
|
|
|
|
#include "link.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
1997-06-28 01:34:03 +00:00
|
|
|
#include "ipcp.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "filter.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "descriptor.h"
|
|
|
|
#include "prompt.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "mp.h"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
static int filter_Nam2Proto(int, char const *const *);
|
|
|
|
static int filter_Nam2Op(const char *);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-06-27 12:03:50 +00:00
|
|
|
static const u_int32_t netmasks[33] = {
|
1997-08-25 00:29:32 +00:00
|
|
|
0x00000000,
|
|
|
|
0x80000000, 0xC0000000, 0xE0000000, 0xF0000000,
|
|
|
|
0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000,
|
|
|
|
0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000,
|
|
|
|
0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000,
|
|
|
|
0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000,
|
|
|
|
0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00,
|
|
|
|
0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0,
|
|
|
|
0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF,
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
struct in_addr
|
|
|
|
bits2mask(int bits)
|
|
|
|
{
|
|
|
|
struct in_addr result;
|
|
|
|
|
|
|
|
result.s_addr = htonl(netmasks[bits]);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
int
|
1999-01-28 01:56:34 +00:00
|
|
|
ParseAddr(struct ipcp *ipcp, const char *data,
|
1998-03-13 21:07:46 +00:00
|
|
|
struct in_addr *paddr, struct in_addr *pmask, int *pwidth)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1997-11-22 03:37:54 +00:00
|
|
|
int bits, len;
|
|
|
|
char *wp;
|
|
|
|
const char *cp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-12-13 02:37:33 +00:00
|
|
|
if (pmask)
|
1998-01-29 23:11:44 +00:00
|
|
|
pmask->s_addr = INADDR_BROADCAST; /* Assume 255.255.255.255 as default */
|
1997-12-13 02:37:33 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
cp = pmask || pwidth ? strchr(data, '/') : NULL;
|
|
|
|
len = cp ? cp - data : strlen(data);
|
1997-11-22 03:37:54 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
if (ipcp && strncasecmp(data, "HISADDR", len) == 0)
|
1998-03-13 21:07:46 +00:00
|
|
|
*paddr = ipcp->peer_ip;
|
1999-01-28 01:56:34 +00:00
|
|
|
else if (ipcp && strncasecmp(data, "MYADDR", len) == 0)
|
1998-03-13 21:07:46 +00:00
|
|
|
*paddr = ipcp->my_ip;
|
1997-11-22 03:37:54 +00:00
|
|
|
else if (len > 15)
|
1999-01-28 01:56:34 +00:00
|
|
|
log_Printf(LogWARN, "ParseAddr: %s: Bad address\n", data);
|
1997-11-22 03:37:54 +00:00
|
|
|
else {
|
|
|
|
char s[16];
|
1999-01-28 01:56:34 +00:00
|
|
|
strncpy(s, data, len);
|
1997-11-22 03:37:54 +00:00
|
|
|
s[len] = '\0';
|
|
|
|
if (inet_aton(s, paddr) == 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseAddr: %s: Bad address\n", s);
|
1997-11-22 03:37:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1997-10-23 10:09:35 +00:00
|
|
|
}
|
1997-11-22 03:37:54 +00:00
|
|
|
if (cp && *++cp) {
|
1995-01-31 06:29:58 +00:00
|
|
|
bits = strtol(cp, &wp, 0);
|
|
|
|
if (cp == wp || bits < 0 || bits > 32) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseAddr: bad mask width.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-04-16 18:30:54 +00:00
|
|
|
} else if (paddr->s_addr == INADDR_ANY)
|
|
|
|
/* An IP of 0.0.0.0 without a width is anything */
|
|
|
|
bits = 0;
|
|
|
|
else
|
|
|
|
/* If a valid IP is given without a width, assume 32 bits */
|
1995-01-31 06:29:58 +00:00
|
|
|
bits = 32;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1997-12-13 02:37:33 +00:00
|
|
|
if (pwidth)
|
|
|
|
*pwidth = bits;
|
|
|
|
|
1998-10-22 02:32:50 +00:00
|
|
|
if (pmask) {
|
|
|
|
if (paddr->s_addr == INADDR_ANY)
|
|
|
|
pmask->s_addr = INADDR_ANY;
|
|
|
|
else
|
1999-01-28 01:56:34 +00:00
|
|
|
*pmask = bits2mask(bits);
|
1998-10-22 02:32:50 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1995-09-17 16:14:49 +00:00
|
|
|
static int
|
1997-11-22 03:37:54 +00:00
|
|
|
ParsePort(const char *service, int proto)
|
1995-09-17 16:14:49 +00:00
|
|
|
{
|
1997-11-22 03:37:54 +00:00
|
|
|
const char *protocol_name;
|
|
|
|
char *cp;
|
1995-09-17 16:14:49 +00:00
|
|
|
struct servent *servent;
|
|
|
|
int port;
|
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case P_UDP:
|
|
|
|
protocol_name = "udp";
|
|
|
|
break;
|
|
|
|
case P_TCP:
|
|
|
|
protocol_name = "tcp";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
protocol_name = 0;
|
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
servent = getservbyname(service, protocol_name);
|
1995-09-17 16:14:49 +00:00
|
|
|
if (servent != 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (ntohs(servent->s_port));
|
1995-09-17 16:14:49 +00:00
|
|
|
|
|
|
|
port = strtol(service, &cp, 0);
|
|
|
|
if (cp == service) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParsePort: %s is not a port name or number.\n",
|
1997-08-25 00:29:32 +00:00
|
|
|
service);
|
|
|
|
return (0);
|
1995-09-17 16:14:49 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (port);
|
1995-09-17 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* ICMP Syntax: src eq icmp_message_type
|
|
|
|
*/
|
|
|
|
static int
|
1998-03-16 22:52:54 +00:00
|
|
|
ParseIcmp(int argc, char const *const *argv, struct filterent *tgt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int type;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
|
|
|
/* permit/deny all ICMP types */
|
1998-03-16 22:52:54 +00:00
|
|
|
tgt->opt.srcop = OP_NONE;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
1998-04-03 19:25:07 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case 3:
|
1997-10-26 01:04:02 +00:00
|
|
|
if (!strcmp(*argv, "src") && !strcmp(argv[1], "eq")) {
|
1995-01-31 06:29:58 +00:00
|
|
|
type = strtol(argv[2], &cp, 0);
|
|
|
|
if (cp == argv[2]) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseIcmp: type is expected.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-03-16 22:52:54 +00:00
|
|
|
tgt->opt.srcop = OP_EQ;
|
|
|
|
tgt->opt.srcport = type;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
1998-04-03 19:25:07 +00:00
|
|
|
|
|
|
|
default:
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseIcmp: bad icmp syntax.\n");
|
1998-04-03 19:25:07 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UDP Syntax: [src op port] [dst op port]
|
|
|
|
*/
|
|
|
|
static int
|
1998-03-16 22:52:54 +00:00
|
|
|
ParseUdpOrTcp(int argc, char const *const *argv, int proto,
|
|
|
|
struct filterent *tgt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-03-16 22:52:54 +00:00
|
|
|
tgt->opt.srcop = tgt->opt.dstop = OP_NONE;
|
1998-04-16 18:30:54 +00:00
|
|
|
tgt->opt.estab = tgt->opt.syn = tgt->opt.finrst = 0;
|
1997-10-23 10:09:35 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
if (argc >= 3 && !strcmp(*argv, "src")) {
|
1998-04-03 19:25:07 +00:00
|
|
|
tgt->opt.srcop = filter_Nam2Op(argv[1]);
|
1998-03-16 22:52:54 +00:00
|
|
|
if (tgt->opt.srcop == OP_NONE) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseUdpOrTcp: bad operation\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-03-16 22:52:54 +00:00
|
|
|
tgt->opt.srcport = ParsePort(argv[2], proto);
|
|
|
|
if (tgt->opt.srcport == 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
|
|
|
argc -= 3;
|
|
|
|
argv += 3;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-04-16 18:30:54 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
if (argc >= 3 && !strcmp(argv[0], "dst")) {
|
1998-04-03 19:25:07 +00:00
|
|
|
tgt->opt.dstop = filter_Nam2Op(argv[1]);
|
1998-03-16 22:52:54 +00:00
|
|
|
if (tgt->opt.dstop == OP_NONE) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseUdpOrTcp: bad operation\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-03-16 22:52:54 +00:00
|
|
|
tgt->opt.dstport = ParsePort(argv[2], proto);
|
|
|
|
if (tgt->opt.dstport == 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
|
|
|
argc -= 3;
|
|
|
|
argv += 3;
|
1995-09-17 16:14:49 +00:00
|
|
|
}
|
1998-04-16 18:30:54 +00:00
|
|
|
|
1998-04-25 10:49:52 +00:00
|
|
|
if (proto == P_TCP) {
|
1998-04-16 18:30:54 +00:00
|
|
|
for (; argc > 0; argc--, argv++)
|
|
|
|
if (!strcmp(*argv, "estab"))
|
|
|
|
tgt->opt.estab = 1;
|
|
|
|
else if (!strcmp(*argv, "syn"))
|
|
|
|
tgt->opt.syn = 1;
|
|
|
|
else if (!strcmp(*argv, "finrst"))
|
|
|
|
tgt->opt.finrst = 1;
|
|
|
|
else
|
|
|
|
break;
|
1995-09-17 16:14:49 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-04-16 18:30:54 +00:00
|
|
|
if (argc > 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "ParseUdpOrTcp: bad src/dst port syntax: %s\n", *argv);
|
1998-04-16 18:30:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static int
|
1998-03-13 21:07:46 +00:00
|
|
|
Parse(struct ipcp *ipcp, int argc, char const *const *argv,
|
1998-03-16 22:52:54 +00:00
|
|
|
struct filterent *ofp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int action, proto;
|
|
|
|
int val;
|
|
|
|
char *wp;
|
1998-03-16 22:52:54 +00:00
|
|
|
struct filterent filterdata;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
val = strtol(*argv, &wp, 0);
|
|
|
|
if (*argv == wp || val > MAXFILTERS) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Parse: invalid filter number.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (val < 0) {
|
|
|
|
for (val = 0; val < MAXFILTERS; val++) {
|
|
|
|
ofp->action = A_NONE;
|
|
|
|
ofp++;
|
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Parse: filter cleared.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
ofp += val;
|
|
|
|
|
|
|
|
if (--argc == 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Parse: missing action.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
argv++;
|
|
|
|
|
|
|
|
proto = P_NONE;
|
1997-12-24 09:29:17 +00:00
|
|
|
memset(&filterdata, '\0', sizeof filterdata);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
if (!strcmp(*argv, "permit")) {
|
1995-01-31 06:29:58 +00:00
|
|
|
action = A_PERMIT;
|
1997-10-26 01:04:02 +00:00
|
|
|
} else if (!strcmp(*argv, "deny")) {
|
1995-01-31 06:29:58 +00:00
|
|
|
action = A_DENY;
|
1997-10-26 01:04:02 +00:00
|
|
|
} else if (!strcmp(*argv, "clear")) {
|
1995-01-31 06:29:58 +00:00
|
|
|
ofp->action = A_NONE;
|
1997-08-25 00:29:32 +00:00
|
|
|
return (1);
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Parse: bad action: %s\n", *argv);
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-03-16 22:52:54 +00:00
|
|
|
filterdata.action = action;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
if (argc && filterdata.action == A_DENY) {
|
1997-10-26 01:04:02 +00:00
|
|
|
if (!strcmp(*argv, "host")) {
|
1998-03-16 22:52:54 +00:00
|
|
|
filterdata.action |= A_UHOST;
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1997-10-26 01:04:02 +00:00
|
|
|
} else if (!strcmp(*argv, "port")) {
|
1998-03-16 22:52:54 +00:00
|
|
|
filterdata.action |= A_UPORT;
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1999-01-28 01:56:34 +00:00
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
proto = filter_Nam2Proto(argc, argv);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (proto == P_NONE) {
|
1999-01-28 01:56:34 +00:00
|
|
|
if (!argc)
|
|
|
|
log_Printf(LogWARN, "Parse: address/mask is expected.\n");
|
|
|
|
else if (ParseAddr(ipcp, *argv, &filterdata.saddr, &filterdata.smask,
|
|
|
|
&filterdata.swidth)) {
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1998-04-03 19:25:07 +00:00
|
|
|
proto = filter_Nam2Proto(argc, argv);
|
1999-01-28 01:56:34 +00:00
|
|
|
if (!argc)
|
|
|
|
log_Printf(LogWARN, "Parse: address/mask is expected.\n");
|
|
|
|
else if (proto == P_NONE) {
|
|
|
|
if (ParseAddr(ipcp, *argv, &filterdata.daddr, &filterdata.dmask,
|
1998-03-16 22:52:54 +00:00
|
|
|
&filterdata.dwidth)) {
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-04-03 19:25:07 +00:00
|
|
|
proto = filter_Nam2Proto(argc, argv);
|
1999-01-28 01:56:34 +00:00
|
|
|
if (argc && proto != P_NONE) {
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-07-27 23:26:23 +00:00
|
|
|
} else {
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Parse: Address/protocol expected.\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
1997-08-25 00:29:32 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val = 1;
|
1998-03-16 22:52:54 +00:00
|
|
|
filterdata.proto = proto;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (proto) {
|
|
|
|
case P_TCP:
|
1998-03-16 22:52:54 +00:00
|
|
|
val = ParseUdpOrTcp(argc, argv, P_TCP, &filterdata);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case P_UDP:
|
1998-03-16 22:52:54 +00:00
|
|
|
val = ParseUdpOrTcp(argc, argv, P_UDP, &filterdata);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case P_ICMP:
|
1998-03-16 22:52:54 +00:00
|
|
|
val = ParseIcmp(argc, argv, &filterdata);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Parse: Src: %s\n", inet_ntoa(filterdata.saddr));
|
|
|
|
log_Printf(LogDEBUG, "Parse: Src mask: %s\n", inet_ntoa(filterdata.smask));
|
|
|
|
log_Printf(LogDEBUG, "Parse: Dst: %s\n", inet_ntoa(filterdata.daddr));
|
|
|
|
log_Printf(LogDEBUG, "Parse: Dst mask: %s\n", inet_ntoa(filterdata.dmask));
|
|
|
|
log_Printf(LogDEBUG, "Parse: Proto = %d\n", proto);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Parse: src: %s (%d)\n",
|
1998-04-03 19:25:07 +00:00
|
|
|
filter_Op2Nam(filterdata.opt.srcop), filterdata.opt.srcport);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Parse: dst: %s (%d)\n",
|
1998-04-03 19:25:07 +00:00
|
|
|
filter_Op2Nam(filterdata.opt.dstop), filterdata.opt.dstport);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "Parse: estab: %u\n", filterdata.opt.estab);
|
|
|
|
log_Printf(LogDEBUG, "Parse: syn: %u\n", filterdata.opt.syn);
|
|
|
|
log_Printf(LogDEBUG, "Parse: finrst: %u\n", filterdata.opt.finrst);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (val)
|
1998-03-16 22:52:54 +00:00
|
|
|
*ofp = filterdata;
|
1997-08-25 00:29:32 +00:00
|
|
|
return (val);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
filter_Set(struct cmdargs const *arg)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-03-16 22:52:54 +00:00
|
|
|
struct filter *filter;
|
|
|
|
|
1998-04-14 23:17:24 +00:00
|
|
|
if (arg->argc < arg->argn+2)
|
1998-03-16 22:52:54 +00:00
|
|
|
return -1;
|
|
|
|
|
1998-04-14 23:17:24 +00:00
|
|
|
if (!strcmp(arg->argv[arg->argn], "in"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.in;
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (!strcmp(arg->argv[arg->argn], "out"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.out;
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (!strcmp(arg->argv[arg->argn], "dial"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.dial;
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (!strcmp(arg->argv[arg->argn], "alive"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.alive;
|
1998-04-16 18:30:54 +00:00
|
|
|
else {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "filter_Set: %s: Invalid filter name.\n",
|
1998-04-16 18:30:54 +00:00
|
|
|
arg->argv[arg->argn]);
|
1998-03-16 22:52:54 +00:00
|
|
|
return -1;
|
1997-06-09 03:27:43 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-04-14 23:17:24 +00:00
|
|
|
Parse(&arg->bundle->ncp.ipcp, arg->argc - arg->argn - 1,
|
|
|
|
arg->argv + arg->argn + 1, filter->rule);
|
1998-03-16 22:52:54 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
const char *
|
|
|
|
filter_Action2Nam(int act)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1998-04-03 19:25:07 +00:00
|
|
|
static const char *actname[] = { "none ", "permit ", "deny " };
|
|
|
|
return actname[act & (A_PERMIT|A_DENY)];
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
static void
|
1998-04-03 19:26:02 +00:00
|
|
|
doShowFilter(struct filterent *fp, struct prompt *prompt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < MAXFILTERS; n++, fp++) {
|
|
|
|
if (fp->action != A_NONE) {
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, " %2d %s", n, filter_Action2Nam(fp->action));
|
1997-11-12 18:47:30 +00:00
|
|
|
if (fp->action & A_UHOST)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "host ");
|
1997-11-12 18:47:30 +00:00
|
|
|
else if (fp->action & A_UPORT)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "port ");
|
1997-11-12 18:47:30 +00:00
|
|
|
else
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, " ");
|
|
|
|
prompt_Printf(prompt, "%s/%d ", inet_ntoa(fp->saddr), fp->swidth);
|
|
|
|
prompt_Printf(prompt, "%s/%d ", inet_ntoa(fp->daddr), fp->dwidth);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (fp->proto) {
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "%s", filter_Proto2Nam(fp->proto));
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (fp->opt.srcop)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, " src %s %d", filter_Op2Nam(fp->opt.srcop),
|
1997-08-25 00:29:32 +00:00
|
|
|
fp->opt.srcport);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (fp->opt.dstop)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, " dst %s %d", filter_Op2Nam(fp->opt.dstop),
|
1997-08-25 00:29:32 +00:00
|
|
|
fp->opt.dstport);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (fp->opt.estab)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, " estab");
|
1998-04-16 18:30:54 +00:00
|
|
|
if (fp->opt.syn)
|
|
|
|
prompt_Printf(prompt, " syn");
|
|
|
|
if (fp->opt.finrst)
|
|
|
|
prompt_Printf(prompt, " finrst");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(prompt, "\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
filter_Show(struct cmdargs const *arg)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-04-14 23:17:24 +00:00
|
|
|
if (arg->argc > arg->argn+1)
|
1998-03-16 22:52:54 +00:00
|
|
|
return -1;
|
|
|
|
|
1998-04-14 23:17:24 +00:00
|
|
|
if (arg->argc == arg->argn+1) {
|
1998-03-16 22:52:54 +00:00
|
|
|
struct filter *filter;
|
|
|
|
|
1998-04-14 23:17:24 +00:00
|
|
|
if (!strcmp(arg->argv[arg->argn], "in"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.in;
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (!strcmp(arg->argv[arg->argn], "out"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.out;
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (!strcmp(arg->argv[arg->argn], "dial"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.dial;
|
1998-04-14 23:17:24 +00:00
|
|
|
else if (!strcmp(arg->argv[arg->argn], "alive"))
|
1998-03-16 22:52:54 +00:00
|
|
|
filter = &arg->bundle->filter.alive;
|
|
|
|
else
|
|
|
|
return -1;
|
1998-04-03 19:26:02 +00:00
|
|
|
doShowFilter(filter->rule, arg->prompt);
|
1998-03-16 22:52:54 +00:00
|
|
|
} else {
|
|
|
|
struct filter *filter[4];
|
|
|
|
int f;
|
|
|
|
|
|
|
|
filter[0] = &arg->bundle->filter.in;
|
|
|
|
filter[1] = &arg->bundle->filter.out;
|
|
|
|
filter[2] = &arg->bundle->filter.dial;
|
|
|
|
filter[3] = &arg->bundle->filter.alive;
|
|
|
|
for (f = 0; f < 4; f++) {
|
|
|
|
if (f)
|
1998-04-03 19:26:02 +00:00
|
|
|
prompt_Printf(arg->prompt, "\n");
|
|
|
|
prompt_Printf(arg->prompt, "%s:\n", filter[f]->name);
|
|
|
|
doShowFilter(filter[f]->rule, arg->prompt);
|
1998-03-16 22:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1997-06-09 03:27:43 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
static const char *protoname[] = { "none", "tcp", "udp", "icmp" };
|
|
|
|
|
|
|
|
const char *
|
|
|
|
filter_Proto2Nam(int proto)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-04-03 19:25:07 +00:00
|
|
|
if (proto >= sizeof protoname / sizeof protoname[0])
|
|
|
|
return "unknown";
|
|
|
|
return protoname[proto];
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
static int
|
|
|
|
filter_Nam2Proto(int argc, char const *const *argv)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-04-03 19:25:07 +00:00
|
|
|
int proto;
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
proto = 0;
|
|
|
|
else
|
|
|
|
for (proto = sizeof protoname / sizeof protoname[0] - 1; proto; proto--)
|
|
|
|
if (!strcasecmp(*argv, protoname[proto]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
return proto;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
static const char *opname[] = {"none", "eq", "gt", "unknown", "lt"};
|
|
|
|
|
|
|
|
const char *
|
|
|
|
filter_Op2Nam(int op)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1998-04-03 19:25:07 +00:00
|
|
|
if (op >= sizeof opname / sizeof opname[0])
|
|
|
|
return "unknown";
|
|
|
|
return opname[op];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filter_Nam2Op(const char *cp)
|
|
|
|
{
|
|
|
|
int op;
|
|
|
|
|
|
|
|
for (op = sizeof opname / sizeof opname[0] - 1; op; op--)
|
|
|
|
if (!strcasecmp(cp, opname[op]))
|
|
|
|
break;
|
|
|
|
|
|
|
|
return op;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|