freebsd-dev/usr.sbin/ppp/filter.c

477 lines
10 KiB
C
Raw Normal View History

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
*
* $Id: filter.c,v 1.13 1997/08/25 00:29:11 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.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
1995-01-31 06:29:58 +00:00
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
1995-01-31 06:29:58 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "command.h"
#include "mbuf.h"
#include "log.h"
1995-01-31 06:29:58 +00:00
#include "filter.h"
#include "loadalias.h"
#include "vars.h"
#include "ipcp.h"
1995-01-31 06:29:58 +00:00
static struct filterent filterdata;
static u_long netmasks[33] = {
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
};
int
ParseAddr(int argc,
char **argv,
struct in_addr * paddr,
struct in_addr * pmask,
int *pwidth)
1995-01-31 06:29:58 +00:00
{
int bits;
char *cp, *wp;
if (argc < 1) {
LogPrintf(LogWARN, "ParseAddr: address/mask is expected.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
pmask->s_addr = 0xffffffff; /* Assume 255.255.255.255 as default */
1995-01-31 06:29:58 +00:00
cp = index(*argv, '/');
if (cp)
*cp++ = '\0';
if (strcasecmp(*argv, "HISADDR") == 0)
*paddr = IpcpInfo.his_ipaddr;
else if (strcasecmp(*argv, "MYADDR") == 0)
*paddr = IpcpInfo.want_ipaddr;
else
paddr->s_addr = inet_addr(*argv);
1995-01-31 06:29:58 +00:00
if (cp && *cp) {
bits = strtol(cp, &wp, 0);
if (cp == wp || bits < 0 || bits > 32) {
LogPrintf(LogWARN, "ParseAddr: bad mask width.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
} else {
/* if width is not given, assume whole 32 bits are meaningfull */
bits = 32;
}
1995-05-30 03:57:47 +00:00
1995-01-31 06:29:58 +00:00
*pwidth = bits;
pmask->s_addr = htonl(netmasks[bits]);
return (1);
1995-01-31 06:29:58 +00:00
}
static int
ParseProto(int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
int proto;
if (argc < 1)
return (P_NONE);
1995-01-31 06:29:58 +00:00
if (STREQ(*argv, "tcp"))
proto = P_TCP;
else if (STREQ(*argv, "udp"))
proto = P_UDP;
else if (STREQ(*argv, "icmp"))
proto = P_ICMP;
else
proto = P_NONE;
return (proto);
1995-01-31 06:29:58 +00:00
}
static int
ParsePort(char *service, int proto)
{
char *protocol_name, *cp;
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;
}
servent = getservbyname(service, protocol_name);
if (servent != 0)
return (ntohs(servent->s_port));
port = strtol(service, &cp, 0);
if (cp == service) {
LogPrintf(LogWARN, "ParsePort: %s is not a port name or number.\n",
service);
return (0);
}
return (port);
}
1995-01-31 06:29:58 +00:00
/*
* ICMP Syntax: src eq icmp_message_type
*/
static int
ParseIcmp(int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
int type;
char *cp;
switch (argc) {
case 0:
/* permit/deny all ICMP types */
filterdata.opt.srcop = OP_NONE;
break;
default:
LogPrintf(LogWARN, "ParseIcmp: bad icmp syntax.\n");
return (0);
1995-01-31 06:29:58 +00:00
case 3:
if (STREQ(*argv, "src") && STREQ(argv[1], "eq")) {
type = strtol(argv[2], &cp, 0);
if (cp == argv[2]) {
LogPrintf(LogWARN, "ParseIcmp: type is expected.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
filterdata.opt.srcop = OP_EQ;
filterdata.opt.srcport = type;
}
break;
}
return (1);
1995-01-31 06:29:58 +00:00
}
static int
ParseOp(char *cp)
1995-01-31 06:29:58 +00:00
{
int op = OP_NONE;
if (STREQ(cp, "eq"))
op = OP_EQ;
else if (STREQ(cp, "gt"))
op = OP_GT;
else if (STREQ(cp, "lt"))
op = OP_LT;
return (op);
1995-01-31 06:29:58 +00:00
}
/*
* UDP Syntax: [src op port] [dst op port]
*/
static int
ParseUdpOrTcp(int argc, char **argv, int proto)
1995-01-31 06:29:58 +00:00
{
if (argc == 0) {
/* permit/deny all tcp traffic */
filterdata.opt.srcop = filterdata.opt.dstop = A_NONE;
return (1);
1995-01-31 06:29:58 +00:00
}
if (argc < 3) {
LogPrintf(LogWARN, "ParseUdpOrTcp: bad udp/tcp syntax.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
if (argc >= 3 && STREQ(*argv, "src")) {
1995-01-31 06:29:58 +00:00
filterdata.opt.srcop = ParseOp(argv[1]);
if (filterdata.opt.srcop == OP_NONE) {
LogPrintf(LogWARN, "ParseUdpOrTcp: bad operation\n");
return (0);
1995-01-31 06:29:58 +00:00
}
filterdata.opt.srcport = ParsePort(argv[2], proto);
if (filterdata.opt.srcport == 0)
return (0);
argc -= 3;
argv += 3;
1995-01-31 06:29:58 +00:00
if (argc == 0)
return (1);
1995-01-31 06:29:58 +00:00
}
if (argc >= 3 && STREQ(argv[0], "dst")) {
filterdata.opt.dstop = ParseOp(argv[1]);
if (filterdata.opt.dstop == OP_NONE) {
LogPrintf(LogWARN, "ParseUdpOrTcp: bad operation\n");
return (0);
1995-01-31 06:29:58 +00:00
}
filterdata.opt.dstport = ParsePort(argv[2], proto);
if (filterdata.opt.dstport == 0)
return (0);
argc -= 3;
argv += 3;
if (argc == 0)
return (1);
}
if (argc == 1) {
if (STREQ(*argv, "estab")) {
filterdata.opt.estab = 1;
return (1);
1995-01-31 06:29:58 +00:00
}
LogPrintf(LogWARN, "ParseUdpOrTcp: estab is expected: %s\n", *argv);
return (0);
}
if (argc > 0)
LogPrintf(LogWARN, "ParseUdpOrTcp: bad src/dst port syntax: %s\n", *argv);
return (0);
1995-01-31 06:29:58 +00:00
}
char *opname[] = {"none", "eq", "gt", "lt"};
1995-01-31 06:29:58 +00:00
static int
Parse(int argc, char **argv, struct filterent * ofp)
1995-01-31 06:29:58 +00:00
{
int action, proto;
int val;
char *wp;
struct filterent *fp = &filterdata;
val = strtol(*argv, &wp, 0);
if (*argv == wp || val > MAXFILTERS) {
LogPrintf(LogWARN, "Parse: invalid filter number.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
if (val < 0) {
for (val = 0; val < MAXFILTERS; val++) {
ofp->action = A_NONE;
ofp++;
}
LogPrintf(LogWARN, "Parse: filter cleared.\n");
return (1);
1995-01-31 06:29:58 +00:00
}
ofp += val;
if (--argc == 0) {
LogPrintf(LogWARN, "Parse: missing action.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
argv++;
proto = P_NONE;
bzero(&filterdata, sizeof(filterdata));
if (STREQ(*argv, "permit")) {
action = A_PERMIT;
} else if (STREQ(*argv, "deny")) {
action = A_DENY;
} else if (STREQ(*argv, "clear")) {
ofp->action = A_NONE;
return (1);
1995-01-31 06:29:58 +00:00
} else {
LogPrintf(LogWARN, "Parse: bad action: %s\n", *argv);
return (0);
1995-01-31 06:29:58 +00:00
}
fp->action = action;
argc--;
argv++;
1995-01-31 06:29:58 +00:00
if (fp->action == A_DENY) {
1995-01-31 06:29:58 +00:00
if (STREQ(*argv, "host")) {
fp->action |= A_UHOST;
argc--;
argv++;
1995-01-31 06:29:58 +00:00
} else if (STREQ(*argv, "port")) {
fp->action |= A_UPORT;
argc--;
argv++;
1995-01-31 06:29:58 +00:00
}
}
proto = ParseProto(argc, argv);
1995-01-31 06:29:58 +00:00
if (proto == P_NONE) {
if (ParseAddr(argc, argv, &fp->saddr, &fp->smask, &fp->swidth)) {
argc--;
argv++;
1995-01-31 06:29:58 +00:00
proto = ParseProto(argc, argv);
if (proto == P_NONE) {
if (ParseAddr(argc, argv, &fp->daddr, &fp->dmask, &fp->dwidth)) {
argc--;
argv++;
1995-01-31 06:29:58 +00:00
}
proto = ParseProto(argc, argv);
if (proto) {
argc--;
argv++;
}
} else {
argc--;
argv++;
1995-01-31 06:29:58 +00:00
}
} else {
LogPrintf(LogWARN, "Parse: Address/protocol expected.\n");
return (0);
1995-01-31 06:29:58 +00:00
}
} else {
argc--;
argv++;
1995-01-31 06:29:58 +00:00
}
val = 1;
fp->proto = proto;
1995-01-31 06:29:58 +00:00
switch (proto) {
case P_TCP:
val = ParseUdpOrTcp(argc, argv, P_TCP);
1995-01-31 06:29:58 +00:00
break;
case P_UDP:
val = ParseUdpOrTcp(argc, argv, P_UDP);
1995-01-31 06:29:58 +00:00
break;
case P_ICMP:
val = ParseIcmp(argc, argv);
break;
}
LogPrintf(LogDEBUG, "Parse: Src: %s\n", inet_ntoa(fp->saddr));
LogPrintf(LogDEBUG, "Parse: Src mask: %s\n", inet_ntoa(fp->smask));
LogPrintf(LogDEBUG, "Parse: Dst: %s\n", inet_ntoa(fp->daddr));
LogPrintf(LogDEBUG, "Parse: Dst mask: %s\n", inet_ntoa(fp->dmask));
LogPrintf(LogDEBUG, "Parse: Proto = %d\n", proto);
1995-01-31 06:29:58 +00:00
LogPrintf(LogDEBUG, "Parse: src: %s (%d)\n", opname[fp->opt.srcop],
fp->opt.srcport);
LogPrintf(LogDEBUG, "Parse: dst: %s (%d)\n", opname[fp->opt.dstop],
fp->opt.dstport);
LogPrintf(LogDEBUG, "Parse: estab: %d\n", fp->opt.estab);
1995-01-31 06:29:58 +00:00
if (val)
*ofp = *fp;
return (val);
1995-01-31 06:29:58 +00:00
}
int
SetIfilter(struct cmdtab * list, int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
if (argc > 0) {
1995-01-31 06:29:58 +00:00
(void) Parse(argc, argv, ifilters);
return 0;
}
return -1;
1995-01-31 06:29:58 +00:00
}
int
SetOfilter(struct cmdtab * list, int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
if (argc > 0) {
1995-01-31 06:29:58 +00:00
(void) Parse(argc, argv, ofilters);
return 0;
}
return -1;
1995-01-31 06:29:58 +00:00
}
int
SetDfilter(struct cmdtab * list, int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
if (argc > 0) {
1995-01-31 06:29:58 +00:00
(void) Parse(argc, argv, dfilters);
return 0;
}
return -1;
1995-01-31 06:29:58 +00:00
}
int
SetAfilter(struct cmdtab * list, int argc, char **argv)
{
if (argc > 0) {
(void) Parse(argc, argv, afilters);
return 0;
}
return -1;
}
1995-01-31 06:29:58 +00:00
static char *protoname[] = {
"none", "tcp", "udp", "icmp",
};
static char *actname[] = {
"none ", "permit ", "deny ",
};
static void
ShowFilter(struct filterent * fp)
1995-01-31 06:29:58 +00:00
{
int n;
if (!VarTerm)
return;
1995-01-31 06:29:58 +00:00
for (n = 0; n < MAXFILTERS; n++, fp++) {
if (fp->action != A_NONE) {
fprintf(VarTerm, "%2d %s", n, actname[fp->action]);
fprintf(VarTerm, "%s/%d ", inet_ntoa(fp->saddr), fp->swidth);
fprintf(VarTerm, "%s/%d ", inet_ntoa(fp->daddr), fp->dwidth);
1995-01-31 06:29:58 +00:00
if (fp->proto) {
fprintf(VarTerm, "%s", protoname[fp->proto]);
1995-01-31 06:29:58 +00:00
if (fp->opt.srcop)
fprintf(VarTerm, " src %s %d", opname[fp->opt.srcop],
fp->opt.srcport);
1995-01-31 06:29:58 +00:00
if (fp->opt.dstop)
fprintf(VarTerm, " dst %s %d", opname[fp->opt.dstop],
fp->opt.dstport);
1995-01-31 06:29:58 +00:00
if (fp->opt.estab)
fprintf(VarTerm, " estab");
1995-01-31 06:29:58 +00:00
}
fprintf(VarTerm, "\n");
1995-01-31 06:29:58 +00:00
}
}
}
int
ShowIfilter(struct cmdtab * list, int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
ShowFilter(ifilters);
return 0;
1995-01-31 06:29:58 +00:00
}
int
ShowOfilter(struct cmdtab * list, int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
ShowFilter(ofilters);
return 0;
1995-01-31 06:29:58 +00:00
}
int
ShowDfilter(struct cmdtab * list, int argc, char **argv)
1995-01-31 06:29:58 +00:00
{
ShowFilter(dfilters);
return 0;
1995-01-31 06:29:58 +00:00
}
int
ShowAfilter(struct cmdtab * list, int argc, char **argv)
{
ShowFilter(afilters);
return 0;
}