2001-06-13 21:52:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
|
|
|
|
* Internet Initiative Japan, Inc (IIJ)
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
2001-06-13 21:52:19 +00:00
|
|
|
|
1999-01-28 01:56:34 +00:00
|
|
|
#include <sys/param.h>
|
1997-11-16 22:15:11 +00:00
|
|
|
#include <sys/socket.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <netinet/in.h>
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
#include <netinet/icmp6.h>
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/tcp.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-10-04 00:14:39 +00:00
|
|
|
#include <errno.h>
|
2001-08-14 16:05:52 +00:00
|
|
|
#include <netdb.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1999-05-08 11:07:56 +00:00
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
|
|
|
#include "proto.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "fsm.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "hdlc.h"
|
1998-01-29 23:11:44 +00:00
|
|
|
#include "throughput.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "iplist.h"
|
1998-03-16 22:54:35 +00:00
|
|
|
#include "slcompress.h"
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ncpaddr.h"
|
|
|
|
#include "ip.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "ipcp.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "filter.h"
|
1998-03-20 19:48:28 +00:00
|
|
|
#include "descriptor.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "lcp.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "ccp.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "mp.h"
|
1999-01-28 01:56:34 +00:00
|
|
|
#ifndef NORADIUS
|
|
|
|
#include "radius.h"
|
|
|
|
#endif
|
2001-08-14 16:05:52 +00:00
|
|
|
#include "ipv6cp.h"
|
|
|
|
#include "ncp.h"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1997-11-16 22:15:11 +00:00
|
|
|
#include "tun.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2000-03-14 01:46:49 +00:00
|
|
|
|
|
|
|
#define OPCODE_QUERY 0
|
|
|
|
#define OPCODE_IQUERY 1
|
|
|
|
#define OPCODE_STATUS 2
|
|
|
|
|
|
|
|
struct dns_header {
|
|
|
|
u_short id;
|
|
|
|
unsigned qr : 1;
|
|
|
|
unsigned opcode : 4;
|
|
|
|
unsigned aa : 1;
|
|
|
|
unsigned tc : 1;
|
|
|
|
unsigned rd : 1;
|
|
|
|
unsigned ra : 1;
|
|
|
|
unsigned z : 3;
|
|
|
|
unsigned rcode : 4;
|
|
|
|
u_short qdcount;
|
|
|
|
u_short ancount;
|
|
|
|
u_short nscount;
|
|
|
|
u_short arcount;
|
1999-12-27 11:54:57 +00:00
|
|
|
};
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2000-03-14 01:46:49 +00:00
|
|
|
static const char *
|
|
|
|
dns_Qclass2Txt(u_short qclass)
|
|
|
|
{
|
|
|
|
static char failure[6];
|
|
|
|
struct {
|
|
|
|
u_short id;
|
|
|
|
const char *txt;
|
|
|
|
} qtxt[] = {
|
|
|
|
/* rfc1035 */
|
|
|
|
{ 1, "IN" }, { 2, "CS" }, { 3, "CH" }, { 4, "HS" }, { 255, "*" }
|
|
|
|
};
|
|
|
|
int f;
|
|
|
|
|
|
|
|
for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
|
|
|
|
if (qtxt[f].id == qclass)
|
|
|
|
return qtxt[f].txt;
|
|
|
|
|
2000-03-14 01:46:54 +00:00
|
|
|
return HexStr(qclass, failure, sizeof failure);
|
2000-03-14 01:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
dns_Qtype2Txt(u_short qtype)
|
|
|
|
{
|
|
|
|
static char failure[6];
|
|
|
|
struct {
|
|
|
|
u_short id;
|
|
|
|
const char *txt;
|
|
|
|
} qtxt[] = {
|
|
|
|
/* rfc1035/rfc1700 */
|
|
|
|
{ 1, "A" }, { 2, "NS" }, { 3, "MD" }, { 4, "MF" }, { 5, "CNAME" },
|
|
|
|
{ 6, "SOA" }, { 7, "MB" }, { 8, "MG" }, { 9, "MR" }, { 10, "NULL" },
|
|
|
|
{ 11, "WKS" }, { 12, "PTR" }, { 13, "HINFO" }, { 14, "MINFO" },
|
|
|
|
{ 15, "MX" }, { 16, "TXT" }, { 17, "RP" }, { 18, "AFSDB" },
|
|
|
|
{ 19, "X25" }, { 20, "ISDN" }, { 21, "RT" }, { 22, "NSAP" },
|
|
|
|
{ 23, "NSAP-PTR" }, { 24, "SIG" }, { 25, "KEY" }, { 26, "PX" },
|
|
|
|
{ 27, "GPOS" }, { 28, "AAAA" }, { 252, "AXFR" }, { 253, "MAILB" },
|
|
|
|
{ 254, "MAILA" }, { 255, "*" }
|
|
|
|
};
|
|
|
|
int f;
|
|
|
|
|
|
|
|
for (f = 0; f < sizeof qtxt / sizeof *qtxt; f++)
|
|
|
|
if (qtxt[f].id == qtype)
|
|
|
|
return qtxt[f].txt;
|
|
|
|
|
2000-03-14 01:46:54 +00:00
|
|
|
return HexStr(qtype, failure, sizeof failure);
|
2000-03-14 01:46:49 +00:00
|
|
|
}
|
|
|
|
|
1999-07-27 23:44:00 +00:00
|
|
|
static __inline int
|
1997-08-25 00:29:32 +00:00
|
|
|
PortMatch(int op, u_short pport, u_short rport)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
switch (op) {
|
1999-07-27 23:44:00 +00:00
|
|
|
case OP_EQ:
|
2000-07-11 22:11:36 +00:00
|
|
|
return pport == rport;
|
1995-01-31 06:29:58 +00:00
|
|
|
case OP_GT:
|
2000-07-11 22:11:36 +00:00
|
|
|
return pport > rport;
|
1995-01-31 06:29:58 +00:00
|
|
|
case OP_LT:
|
2000-07-11 22:11:36 +00:00
|
|
|
return pport < rport;
|
1995-01-31 06:29:58 +00:00
|
|
|
default:
|
2000-07-11 22:11:36 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-08-14 16:05:52 +00:00
|
|
|
* Check a packet against the given filter
|
|
|
|
* Returns 0 to accept the packet, non-zero to drop the packet.
|
|
|
|
* If psecs is not NULL, populate it with the timeout associated
|
|
|
|
* with the filter rule matched.
|
1999-07-27 23:44:00 +00:00
|
|
|
*
|
2001-08-14 16:05:52 +00:00
|
|
|
* If filtering is enabled, the initial fragment of a datagram must
|
|
|
|
* contain the complete protocol header, and subsequent fragments
|
|
|
|
* must not attempt to over-write it.
|
|
|
|
*
|
|
|
|
* One (and only one) of pip or pip6 must be set.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
2001-08-14 16:05:52 +00:00
|
|
|
int
|
|
|
|
FilterCheck(const unsigned char *packet, u_int32_t family,
|
|
|
|
const struct filter *filter, unsigned *psecs)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-07-27 23:44:00 +00:00
|
|
|
int gotinfo; /* true if IP payload decoded */
|
2001-08-14 16:05:52 +00:00
|
|
|
int cproto; /* IPPROTO_* protocol number if (gotinfo) */
|
1999-07-27 23:44:00 +00:00
|
|
|
int estab, syn, finrst; /* TCP state flags if (gotinfo) */
|
|
|
|
u_short sport, dport; /* src, dest port from packet if (gotinfo) */
|
|
|
|
int n; /* filter rule to process */
|
|
|
|
int len; /* bytes used in dbuff */
|
|
|
|
int didname; /* true if filter header printed */
|
|
|
|
int match; /* true if condition matched */
|
2001-08-14 16:05:52 +00:00
|
|
|
int mindata; /* minimum data size or zero */
|
1999-07-27 23:44:00 +00:00
|
|
|
const struct filterent *fp = filter->rule;
|
2001-08-14 16:05:52 +00:00
|
|
|
char dbuff[100], dstip[16], prototxt[16];
|
|
|
|
struct protoent *pe;
|
|
|
|
struct ncpaddr srcaddr, dstaddr;
|
|
|
|
const char *payload; /* IP payload */
|
|
|
|
int datalen; /* IP datagram length */
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-07-27 23:44:00 +00:00
|
|
|
if (fp->f_action == A_NONE)
|
2000-07-11 22:11:36 +00:00
|
|
|
return 0; /* No rule is given. Permit this packet */
|
1999-07-27 23:44:00 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
if (family == AF_INET6) {
|
|
|
|
const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
|
|
|
|
|
|
|
|
ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
|
|
|
|
ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
|
2001-08-16 02:00:55 +00:00
|
|
|
datalen = ntohs(pip6->ip6_plen);
|
|
|
|
payload = packet + sizeof *pip6;
|
2001-08-14 16:05:52 +00:00
|
|
|
cproto = pip6->ip6_nxt;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Deny any packet fragment that tries to over-write the header.
|
|
|
|
* Since we no longer have the real header available, punt on the
|
|
|
|
* largest normal header - 20 bytes for TCP without options, rounded
|
|
|
|
* up to the next possible fragment boundary. Since the smallest
|
|
|
|
* `legal' MTU is 576, and the smallest recommended MTU is 296, any
|
|
|
|
* fragmentation within this range is dubious at best
|
|
|
|
*/
|
|
|
|
const struct ip *pip = (const struct ip *)packet;
|
|
|
|
|
2002-06-15 08:03:30 +00:00
|
|
|
len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
|
2001-08-14 16:05:52 +00:00
|
|
|
if (len > 0) { /* Not first fragment within datagram */
|
|
|
|
if (len < (24 >> 3)) { /* don't allow fragment to over-write header */
|
|
|
|
log_Printf(LogFILTER, " error: illegal header\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* permit fragments on in and out filter */
|
|
|
|
if (!filter->fragok) {
|
|
|
|
log_Printf(LogFILTER, " error: illegal fragmentation\n");
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
2000-08-28 23:24:52 +00:00
|
|
|
}
|
2002-06-15 08:03:30 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_setip4(&srcaddr, pip->ip_src);
|
|
|
|
ncpaddr_setip4(&dstaddr, pip->ip_dst);
|
|
|
|
datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
|
|
|
|
payload = packet + (pip->ip_hl << 2);
|
|
|
|
cproto = pip->ip_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pe = getprotobynumber(cproto)) == NULL)
|
|
|
|
snprintf(prototxt, sizeof prototxt, "%d", cproto);
|
|
|
|
else
|
|
|
|
snprintf(prototxt, sizeof prototxt, "%s", pe->p_name);
|
|
|
|
gotinfo = estab = syn = finrst = didname = 0;
|
1999-07-27 23:44:00 +00:00
|
|
|
sport = dport = 0;
|
2001-08-14 16:05:52 +00:00
|
|
|
|
1999-07-27 23:44:00 +00:00
|
|
|
for (n = 0; n < MAXFILTERS; ) {
|
|
|
|
if (fp->f_action == A_NONE) {
|
|
|
|
n++;
|
|
|
|
fp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!didname) {
|
|
|
|
log_Printf(LogDEBUG, "%s filter:\n", filter->name);
|
|
|
|
didname = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
match = 0;
|
2001-08-14 16:05:52 +00:00
|
|
|
|
|
|
|
if ((ncprange_family(&fp->f_src) == AF_UNSPEC ||
|
|
|
|
ncprange_contains(&fp->f_src, &srcaddr)) &&
|
|
|
|
(ncprange_family(&fp->f_dst) == AF_UNSPEC ||
|
|
|
|
ncprange_contains(&fp->f_dst, &dstaddr))) {
|
|
|
|
if (fp->f_proto != 0) {
|
2000-07-11 22:11:36 +00:00
|
|
|
if (!gotinfo) {
|
|
|
|
const struct tcphdr *th;
|
|
|
|
const struct udphdr *uh;
|
|
|
|
const struct icmp *ih;
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
const struct icmp6_hdr *ih6;
|
|
|
|
#endif
|
|
|
|
mindata = 0;
|
|
|
|
sport = dport = 0;
|
|
|
|
estab = syn = finrst = -1;
|
2000-07-11 22:11:36 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
switch (cproto) {
|
2000-07-11 22:11:36 +00:00
|
|
|
case IPPROTO_ICMP:
|
2001-08-14 16:05:52 +00:00
|
|
|
mindata = 8; /* ICMP must be at least 8 octets */
|
|
|
|
ih = (const struct icmp *)payload;
|
|
|
|
sport = ntohs(ih->icmp_type);
|
|
|
|
if (log_IsKept(LogDEBUG))
|
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
|
|
|
|
break;
|
2000-08-28 23:24:52 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
case IPPROTO_ICMPV6:
|
|
|
|
mindata = 8; /* ICMP must be at least 8 octets */
|
|
|
|
ih6 = (const struct icmp6_hdr *)payload;
|
|
|
|
sport = ntohs(ih6->icmp6_type);
|
2000-07-11 22:11:36 +00:00
|
|
|
if (log_IsKept(LogDEBUG))
|
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
|
|
|
|
break;
|
2001-08-14 16:05:52 +00:00
|
|
|
#endif
|
|
|
|
|
2000-07-11 22:11:36 +00:00
|
|
|
case IPPROTO_IGMP:
|
2001-08-14 16:05:52 +00:00
|
|
|
mindata = 8; /* IGMP uses 8-octet messages */
|
2000-07-11 22:11:36 +00:00
|
|
|
break;
|
2001-08-14 16:05:52 +00:00
|
|
|
|
1999-09-30 07:23:13 +00:00
|
|
|
#ifdef IPPROTO_GRE
|
|
|
|
case IPPROTO_GRE:
|
2001-08-14 16:05:52 +00:00
|
|
|
mindata = 2; /* GRE uses 2-octet+ messages */
|
1999-09-30 07:23:13 +00:00
|
|
|
break;
|
|
|
|
#endif
|
1999-08-02 15:29:19 +00:00
|
|
|
#ifdef IPPROTO_OSPFIGP
|
2000-07-11 22:11:36 +00:00
|
|
|
case IPPROTO_OSPFIGP:
|
2001-08-14 16:05:52 +00:00
|
|
|
mindata = 8; /* IGMP uses 8-octet messages */
|
2000-07-11 22:11:36 +00:00
|
|
|
break;
|
1999-08-02 15:29:19 +00:00
|
|
|
#endif
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
mindata = 20; /* RFC2893 Section 3.5: 5 * 32bit words */
|
2000-09-14 18:01:54 +00:00
|
|
|
break;
|
2001-08-14 16:05:52 +00:00
|
|
|
#endif
|
2000-08-28 23:24:52 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
mindata = 8; /* UDP header is 8 octets */
|
|
|
|
uh = (const struct udphdr *)payload;
|
2000-07-11 22:11:36 +00:00
|
|
|
sport = ntohs(uh->uh_sport);
|
|
|
|
dport = ntohs(uh->uh_dport);
|
|
|
|
if (log_IsKept(LogDEBUG))
|
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
|
|
|
|
sport, dport);
|
|
|
|
break;
|
2001-08-14 16:05:52 +00:00
|
|
|
|
2000-07-11 22:11:36 +00:00
|
|
|
case IPPROTO_TCP:
|
2001-08-14 16:05:52 +00:00
|
|
|
th = (const struct tcphdr *)payload;
|
|
|
|
/*
|
|
|
|
* TCP headers are variable length. The following code
|
2000-07-11 22:11:36 +00:00
|
|
|
* ensures that the TCP header length isn't de-referenced if
|
|
|
|
* the datagram is too short
|
|
|
|
*/
|
2000-08-28 23:24:52 +00:00
|
|
|
if (datalen < 20 || datalen < (th->th_off << 2)) {
|
|
|
|
log_Printf(LogFILTER, " error: TCP header incorrect\n");
|
2000-07-11 22:11:36 +00:00
|
|
|
return 1;
|
2000-08-28 23:24:52 +00:00
|
|
|
}
|
2000-07-11 22:11:36 +00:00
|
|
|
sport = ntohs(th->th_sport);
|
|
|
|
dport = ntohs(th->th_dport);
|
|
|
|
estab = (th->th_flags & TH_ACK);
|
|
|
|
syn = (th->th_flags & TH_SYN);
|
|
|
|
finrst = (th->th_flags & (TH_FIN|TH_RST));
|
|
|
|
if (log_IsKept(LogDEBUG)) {
|
|
|
|
if (!estab)
|
|
|
|
snprintf(dbuff, sizeof dbuff,
|
|
|
|
"flags = %02x, sport = %d, dport = %d",
|
|
|
|
th->th_flags, sport, dport);
|
|
|
|
else
|
|
|
|
*dbuff = '\0';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2001-08-14 16:05:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (datalen < mindata) {
|
|
|
|
log_Printf(LogFILTER, " error: proto %s must be at least"
|
|
|
|
" %d octets\n", prototxt, mindata);
|
|
|
|
return 1;
|
2000-07-11 22:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (log_IsKept(LogDEBUG)) {
|
|
|
|
if (estab != -1) {
|
|
|
|
len = strlen(dbuff);
|
|
|
|
snprintf(dbuff + len, sizeof dbuff - len,
|
|
|
|
", estab = %d, syn = %d, finrst = %d",
|
|
|
|
estab, syn, finrst);
|
|
|
|
}
|
2001-08-14 16:05:52 +00:00
|
|
|
log_Printf(LogDEBUG, " Filter: proto = %s, %s\n", prototxt, dbuff);
|
2000-07-11 22:11:36 +00:00
|
|
|
}
|
|
|
|
gotinfo = 1;
|
|
|
|
}
|
2001-08-14 16:05:52 +00:00
|
|
|
|
2000-07-11 22:11:36 +00:00
|
|
|
if (log_IsKept(LogDEBUG)) {
|
|
|
|
if (fp->f_srcop != OP_NONE) {
|
|
|
|
snprintf(dbuff, sizeof dbuff, ", src %s %d",
|
|
|
|
filter_Op2Nam(fp->f_srcop), fp->f_srcport);
|
|
|
|
len = strlen(dbuff);
|
|
|
|
} else
|
|
|
|
len = 0;
|
|
|
|
if (fp->f_dstop != OP_NONE) {
|
|
|
|
snprintf(dbuff + len, sizeof dbuff - len,
|
|
|
|
", dst %s %d", filter_Op2Nam(fp->f_dstop),
|
|
|
|
fp->f_dstport);
|
|
|
|
} else if (!len)
|
|
|
|
*dbuff = '\0';
|
|
|
|
|
|
|
|
log_Printf(LogDEBUG, " rule = %d: Address match, "
|
2001-08-14 16:05:52 +00:00
|
|
|
"check against proto %d%s, action = %s\n",
|
|
|
|
n, fp->f_proto, dbuff, filter_Action2Nam(fp->f_action));
|
2000-07-11 22:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cproto == fp->f_proto) {
|
|
|
|
if ((fp->f_srcop == OP_NONE ||
|
|
|
|
PortMatch(fp->f_srcop, sport, fp->f_srcport)) &&
|
|
|
|
(fp->f_dstop == OP_NONE ||
|
|
|
|
PortMatch(fp->f_dstop, dport, fp->f_dstport)) &&
|
|
|
|
(fp->f_estab == 0 || estab) &&
|
|
|
|
(fp->f_syn == 0 || syn) &&
|
|
|
|
(fp->f_finrst == 0 || finrst)) {
|
|
|
|
match = 1;
|
|
|
|
}
|
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
} else {
|
2000-07-11 22:11:36 +00:00
|
|
|
/* Address is matched and no protocol specified. Make a decision. */
|
|
|
|
log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
|
|
|
|
filter_Action2Nam(fp->f_action));
|
|
|
|
match = 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
} else
|
|
|
|
log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
|
|
|
|
|
|
|
|
if (match != fp->f_invert) {
|
|
|
|
/* Take specified action */
|
|
|
|
if (fp->f_action < A_NONE)
|
2000-07-11 22:11:36 +00:00
|
|
|
fp = &filter->rule[n = fp->f_action];
|
2000-08-28 23:24:52 +00:00
|
|
|
else {
|
2000-07-11 22:11:36 +00:00
|
|
|
if (fp->f_action == A_PERMIT) {
|
|
|
|
if (psecs != NULL)
|
|
|
|
*psecs = fp->timeout;
|
2000-08-28 23:24:52 +00:00
|
|
|
if (strcmp(filter->name, "DIAL") == 0) {
|
|
|
|
/* If dial filter then even print out accept packets */
|
|
|
|
if (log_IsKept(LogFILTER)) {
|
2001-08-14 16:05:52 +00:00
|
|
|
snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
|
2000-08-28 23:24:52 +00:00
|
|
|
log_Printf(LogFILTER, "%sbound rule = %d accept %s "
|
2001-08-14 16:05:52 +00:00
|
|
|
"src = %s:%d dst = %s:%d\n", filter->name, n, prototxt,
|
|
|
|
ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
|
2000-08-28 23:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-11 22:11:36 +00:00
|
|
|
return 0;
|
2000-08-28 23:24:52 +00:00
|
|
|
} else {
|
|
|
|
if (log_IsKept(LogFILTER)) {
|
2001-08-14 16:05:52 +00:00
|
|
|
snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
|
2002-06-15 08:03:30 +00:00
|
|
|
log_Printf(LogFILTER,
|
|
|
|
"%sbound rule = %d deny %s src = %s/%d dst = %s/%d\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
filter->name, n, prototxt,
|
|
|
|
ncpaddr_ntoa(&srcaddr), sport, dstip, dport);
|
2000-08-28 23:24:52 +00:00
|
|
|
}
|
2002-06-15 08:03:30 +00:00
|
|
|
return 1;
|
2001-08-14 16:05:52 +00:00
|
|
|
} /* Explict match. Deny this packet */
|
2000-08-28 23:24:52 +00:00
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
} else {
|
|
|
|
n++;
|
1995-01-31 06:29:58 +00:00
|
|
|
fp++;
|
|
|
|
}
|
|
|
|
}
|
2000-08-28 23:24:52 +00:00
|
|
|
|
|
|
|
if (log_IsKept(LogFILTER)) {
|
2001-08-14 16:05:52 +00:00
|
|
|
snprintf(dstip, sizeof dstip, "%s", ncpaddr_ntoa(&dstaddr));
|
2002-06-15 08:03:30 +00:00
|
|
|
log_Printf(LogFILTER,
|
|
|
|
"%sbound rule = implicit deny %s src = %s/%d dst = %s/%d\n",
|
2001-08-14 16:05:52 +00:00
|
|
|
filter->name, prototxt, ncpaddr_ntoa(&srcaddr), sport,
|
|
|
|
dstip, dport);
|
2000-08-28 23:24:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
return 1; /* No rule matched, deny this packet */
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
2000-03-14 01:46:49 +00:00
|
|
|
static void
|
|
|
|
ip_LogDNS(const struct udphdr *uh, const char *direction)
|
|
|
|
{
|
|
|
|
struct dns_header header;
|
|
|
|
const u_short *pktptr;
|
|
|
|
const u_char *ptr;
|
2001-05-30 15:52:24 +00:00
|
|
|
u_short *hptr, tmp;
|
2000-03-14 01:46:49 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
ptr = (const char *)uh + sizeof *uh;
|
|
|
|
len = ntohs(uh->uh_ulen) - sizeof *uh;
|
|
|
|
if (len < sizeof header + 5) /* rfc1024 */
|
|
|
|
return;
|
|
|
|
|
|
|
|
pktptr = (const u_short *)ptr;
|
|
|
|
hptr = (u_short *)&header;
|
|
|
|
ptr += sizeof header;
|
|
|
|
len -= sizeof header;
|
|
|
|
|
|
|
|
while (pktptr < (const u_short *)ptr) {
|
2000-07-11 22:11:36 +00:00
|
|
|
*hptr++ = ntohs(*pktptr); /* Careful of macro side-effects ! */
|
2000-03-14 01:46:49 +00:00
|
|
|
pktptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.opcode == OPCODE_QUERY && header.qr == 0) {
|
|
|
|
/* rfc1035 */
|
2001-03-09 20:31:02 +00:00
|
|
|
char namewithdot[MAXHOSTNAMELEN + 1], *n;
|
2000-03-14 01:46:49 +00:00
|
|
|
const char *qtype, *qclass;
|
|
|
|
const u_char *end;
|
|
|
|
|
2001-03-09 20:31:02 +00:00
|
|
|
n = namewithdot;
|
2000-03-14 01:46:49 +00:00
|
|
|
end = ptr + len - 4;
|
2001-03-09 20:31:02 +00:00
|
|
|
if (end - ptr >= sizeof namewithdot)
|
|
|
|
end = ptr + sizeof namewithdot - 1;
|
2000-03-14 01:46:49 +00:00
|
|
|
while (ptr < end) {
|
|
|
|
len = *ptr++;
|
|
|
|
if (len > end - ptr)
|
|
|
|
len = end - ptr;
|
2001-03-09 20:31:02 +00:00
|
|
|
if (n != namewithdot)
|
2000-03-14 01:46:49 +00:00
|
|
|
*n++ = '.';
|
|
|
|
memcpy(n, ptr, len);
|
|
|
|
ptr += len;
|
|
|
|
n += len;
|
|
|
|
}
|
|
|
|
*n = '\0';
|
|
|
|
|
2001-05-30 15:52:24 +00:00
|
|
|
if (log_IsKept(LogDNS)) {
|
|
|
|
memcpy(&tmp, end, sizeof tmp);
|
|
|
|
qtype = dns_Qtype2Txt(ntohs(tmp));
|
|
|
|
memcpy(&tmp, end + 2, sizeof tmp);
|
|
|
|
qclass = dns_Qclass2Txt(ntohs(tmp));
|
|
|
|
|
|
|
|
log_Printf(LogDNS, "%sbound query %s %s %s\n",
|
|
|
|
direction, qclass, qtype, namewithdot);
|
|
|
|
}
|
2000-03-14 01:46:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
2001-08-14 16:05:52 +00:00
|
|
|
* Check if the given packet matches the given filter.
|
|
|
|
* One of pip or pip6 must be set.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
int
|
2001-08-14 16:05:52 +00:00
|
|
|
PacketCheck(struct bundle *bundle, u_int32_t family,
|
|
|
|
const unsigned char *packet, int nb, struct filter *filter,
|
|
|
|
const char *prefix, unsigned *psecs)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2000-03-14 01:46:49 +00:00
|
|
|
static const char *const TcpFlags[] = {
|
|
|
|
"FIN", "SYN", "RST", "PSH", "ACK", "URG"
|
|
|
|
};
|
2001-08-14 16:05:52 +00:00
|
|
|
const struct tcphdr *th;
|
|
|
|
const struct udphdr *uh;
|
|
|
|
const struct icmp *icmph;
|
|
|
|
#ifndef NOINET6
|
|
|
|
const struct icmp6_hdr *icmp6h;
|
|
|
|
#endif
|
|
|
|
const unsigned char *payload;
|
|
|
|
struct ncpaddr srcaddr, dstaddr;
|
|
|
|
int cproto, mask, len, n, pri, logit, loglen, result;
|
1998-06-15 19:06:58 +00:00
|
|
|
char logbuf[200];
|
2001-08-14 16:05:52 +00:00
|
|
|
int datalen, frag;
|
|
|
|
u_char tos;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2000-03-29 09:31:52 +00:00
|
|
|
logit = (log_IsKept(LogTCPIP) || log_IsKept(LogDNS)) &&
|
|
|
|
(!filter || filter->logok);
|
1997-06-16 21:20:00 +00:00
|
|
|
loglen = 0;
|
2000-03-14 01:46:49 +00:00
|
|
|
pri = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
if (family == AF_INET6) {
|
|
|
|
const struct ip6_hdr *pip6 = (const struct ip6_hdr *)packet;
|
|
|
|
|
|
|
|
ncpaddr_setip6(&srcaddr, &pip6->ip6_src);
|
|
|
|
ncpaddr_setip6(&dstaddr, &pip6->ip6_dst);
|
|
|
|
datalen = ntohs(pip6->ip6_plen);
|
|
|
|
payload = packet + sizeof *pip6;
|
|
|
|
cproto = pip6->ip6_nxt;
|
2001-08-16 02:00:55 +00:00
|
|
|
tos = 0; /* XXX: pip6->ip6_vfc >> 4 ? */
|
2001-08-14 16:05:52 +00:00
|
|
|
frag = 0; /* XXX: ??? */
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
const struct ip *pip = (const struct ip *)packet;
|
|
|
|
|
|
|
|
ncpaddr_setip4(&srcaddr, pip->ip_src);
|
|
|
|
ncpaddr_setip4(&dstaddr, pip->ip_dst);
|
|
|
|
datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
|
|
|
|
payload = packet + (pip->ip_hl << 2);
|
|
|
|
cproto = pip->ip_p;
|
|
|
|
tos = pip->ip_tos;
|
|
|
|
frag = ntohs(pip->ip_off) & IP_OFFMASK;
|
|
|
|
}
|
|
|
|
|
2000-03-14 01:46:49 +00:00
|
|
|
uh = NULL;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
2000-07-07 14:22:08 +00:00
|
|
|
if (prefix)
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s", prefix);
|
|
|
|
else if (filter)
|
2000-03-29 09:31:52 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
|
|
|
|
else
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, " ");
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
1997-06-16 21:20:00 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
switch (cproto) {
|
1995-01-31 06:29:58 +00:00
|
|
|
case IPPROTO_ICMP:
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
2001-08-14 16:05:52 +00:00
|
|
|
len = datalen - sizeof *icmph;
|
|
|
|
icmph = (const struct icmp *)payload;
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmph->icmp_type);
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
case IPPROTO_ICMPV6:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
len = datalen - sizeof *icmp6h;
|
|
|
|
icmp6h = (const struct icmp6_hdr *)payload;
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"ICMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), icmp6h->icmp6_type);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s (%d/%d)", ncpaddr_ntoa(&dstaddr), len, nb);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case IPPROTO_UDP:
|
2001-08-14 16:05:52 +00:00
|
|
|
uh = (const struct udphdr *)payload;
|
|
|
|
if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
|
1999-09-07 07:51:11 +00:00
|
|
|
pri++;
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (!frag && ncp_IsUrgentUdpPort(&bundle->ncp, ntohs(uh->uh_sport),
|
|
|
|
ntohs(uh->uh_dport)))
|
1999-09-07 07:51:11 +00:00
|
|
|
pri++;
|
|
|
|
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
2001-08-14 16:05:52 +00:00
|
|
|
len = datalen - sizeof *uh;
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"UDP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(uh->uh_sport));
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s:%d (%d/%d)", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport),
|
2000-07-11 22:11:36 +00:00
|
|
|
len, nb);
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
2000-07-07 14:22:08 +00:00
|
|
|
|
|
|
|
if (Enabled(bundle, OPT_FILTERDECAP) &&
|
2001-08-14 16:05:52 +00:00
|
|
|
payload[sizeof *uh] == HDLC_ADDR &&
|
|
|
|
payload[sizeof *uh + 1] == HDLC_UI) {
|
2000-07-07 14:22:08 +00:00
|
|
|
u_short proto;
|
|
|
|
const char *type;
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
memcpy(&proto, payload + sizeof *uh + 2, sizeof proto);
|
2000-07-07 14:22:08 +00:00
|
|
|
type = NULL;
|
|
|
|
|
|
|
|
switch (ntohs(proto)) {
|
|
|
|
case PROTO_IP:
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
|
2001-08-14 16:05:52 +00:00
|
|
|
result = PacketCheck(bundle, AF_INET, payload + sizeof *uh + 4,
|
|
|
|
nb - (payload - packet) - sizeof *uh - 4, filter,
|
2000-07-11 22:11:36 +00:00
|
|
|
logbuf, psecs);
|
2000-07-07 14:22:08 +00:00
|
|
|
if (result != -2)
|
|
|
|
return result;
|
|
|
|
type = "IP";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROTO_VJUNCOMP: type = "compressed VJ"; break;
|
|
|
|
case PROTO_VJCOMP: type = "uncompressed VJ"; break;
|
|
|
|
case PROTO_MP: type = "Multi-link"; break;
|
|
|
|
case PROTO_ICOMPD: type = "Individual link CCP"; break;
|
|
|
|
case PROTO_COMPD: type = "CCP"; break;
|
|
|
|
case PROTO_IPCP: type = "IPCP"; break;
|
|
|
|
case PROTO_LCP: type = "LCP"; break;
|
|
|
|
case PROTO_PAP: type = "PAP"; break;
|
|
|
|
case PROTO_CBCP: type = "CBCP"; break;
|
|
|
|
case PROTO_LQR: type = "LQR"; break;
|
|
|
|
case PROTO_CHAP: type = "CHAP"; break;
|
|
|
|
}
|
|
|
|
if (type) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
" - %s data", type);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
1999-09-30 07:23:13 +00:00
|
|
|
#ifdef IPPROTO_GRE
|
|
|
|
case IPPROTO_GRE:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"GRE: %s ---> ", ncpaddr_ntoa(&srcaddr));
|
1999-09-30 07:23:13 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
|
1999-09-30 07:23:13 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1999-08-02 15:29:19 +00:00
|
|
|
#ifdef IPPROTO_OSPFIGP
|
1999-08-02 11:53:16 +00:00
|
|
|
case IPPROTO_OSPFIGP:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"OSPF: %s ---> ", ncpaddr_ntoa(&srcaddr));
|
1999-08-02 11:53:16 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
|
1999-08-02 11:53:16 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
1999-08-02 15:29:19 +00:00
|
|
|
#endif
|
1999-09-07 07:51:11 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
case IPPROTO_IPV6:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"IPv6: %s ---> ", ncpaddr_ntoa(&srcaddr));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s (%d/%d)", ncpaddr_ntoa(&dstaddr), datalen, nb);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Enabled(bundle, OPT_FILTERDECAP)) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
|
|
|
|
result = PacketCheck(bundle, AF_INET6, payload, nb - (payload - packet),
|
|
|
|
filter, logbuf, psecs);
|
|
|
|
if (result != -2)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1998-06-14 01:21:24 +00:00
|
|
|
case IPPROTO_IPIP:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"IPIP: %s ---> ", ncpaddr_ntoa(&srcaddr));
|
1998-06-14 01:21:24 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s", ncpaddr_ntoa(&dstaddr));
|
1998-06-14 01:21:24 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
2001-08-14 16:05:52 +00:00
|
|
|
}
|
2001-04-24 02:13:34 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (Enabled(bundle, OPT_FILTERDECAP) &&
|
|
|
|
((const struct ip *)payload)->ip_v == 4) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, " contains ");
|
|
|
|
result = PacketCheck(bundle, AF_INET, payload, nb - (payload - packet),
|
|
|
|
filter, logbuf, psecs);
|
|
|
|
if (result != -2)
|
|
|
|
return result;
|
1998-06-14 01:21:24 +00:00
|
|
|
}
|
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
2000-09-14 18:01:54 +00:00
|
|
|
case IPPROTO_ESP:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"ESP: %s ---> ", ncpaddr_ntoa(&srcaddr));
|
2000-09-14 18:01:54 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
2001-01-29 08:46:58 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_ntoa(&dstaddr), payload);
|
2000-09-14 18:01:54 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPPROTO_AH:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"AH: %s ---> ", ncpaddr_ntoa(&srcaddr));
|
2000-09-14 18:01:54 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
2001-01-29 08:46:58 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s, spi %p",
|
2001-08-14 16:05:52 +00:00
|
|
|
ncpaddr_ntoa(&dstaddr), payload + sizeof(u_int32_t));
|
2000-09-14 18:01:54 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-06-14 01:21:24 +00:00
|
|
|
case IPPROTO_IGMP:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
2001-08-14 16:05:52 +00:00
|
|
|
uh = (const struct udphdr *)payload;
|
1998-06-14 01:21:24 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"IGMP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr),
|
2000-07-11 22:11:36 +00:00
|
|
|
ntohs(uh->uh_sport));
|
1998-06-14 01:21:24 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(uh->uh_dport));
|
1998-06-14 01:21:24 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case IPPROTO_TCP:
|
2001-08-14 16:05:52 +00:00
|
|
|
th = (const struct tcphdr *)payload;
|
|
|
|
if (tos == IPTOS_LOWDELAY && bundle->ncp.cfg.urgent.tos)
|
1999-09-04 00:00:21 +00:00
|
|
|
pri++;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (!frag && ncp_IsUrgentTcpPort(&bundle->ncp, ntohs(th->th_sport),
|
|
|
|
ntohs(th->th_dport)))
|
1999-09-04 00:00:21 +00:00
|
|
|
pri++;
|
|
|
|
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
2001-08-14 16:05:52 +00:00
|
|
|
len = datalen - (th->th_off << 2);
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"TCP: %s:%d ---> ", ncpaddr_ntoa(&srcaddr), ntohs(th->th_sport));
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2001-08-14 16:05:52 +00:00
|
|
|
"%s:%d", ncpaddr_ntoa(&dstaddr), ntohs(th->th_dport));
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
1995-01-31 06:29:58 +00:00
|
|
|
n = 0;
|
|
|
|
for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
|
2000-07-11 22:11:36 +00:00
|
|
|
if (th->th_flags & mask) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
n++;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
2000-07-11 22:11:36 +00:00
|
|
|
" seq:%lx ack:%lx (%d/%d)",
|
|
|
|
(u_long)ntohl(th->th_seq), (u_long)ntohl(th->th_ack), len, nb);
|
1997-08-25 00:29:32 +00:00
|
|
|
loglen += strlen(logbuf + loglen);
|
1995-01-31 06:29:58 +00:00
|
|
|
if ((th->th_flags & TH_SYN) && nb > 40) {
|
2001-08-14 16:05:52 +00:00
|
|
|
const u_short *sp;
|
2000-07-11 22:11:36 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
sp = (const u_short *)(payload + 20);
|
2000-07-11 22:11:36 +00:00
|
|
|
if (ntohs(sp[0]) == 0x0204) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
" MSS = %d", ntohs(sp[1]));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-07-07 14:22:08 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (prefix)
|
|
|
|
return -2;
|
2001-08-14 16:05:52 +00:00
|
|
|
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"<%d>: %s ---> ", cproto, ncpaddr_ntoa(&srcaddr));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s (%d)", ncpaddr_ntoa(&dstaddr), nb);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-06-16 21:20:00 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (filter && FilterCheck(packet, family, filter, psecs)) {
|
1997-11-12 19:48:45 +00:00
|
|
|
if (logit)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTCPIP, "%s - BLOCKED\n", logbuf);
|
2000-03-14 01:46:49 +00:00
|
|
|
result = -1;
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1998-03-16 22:52:54 +00:00
|
|
|
/* Check Keep Alive filter */
|
2000-03-14 01:46:49 +00:00
|
|
|
if (logit && log_IsKept(LogTCPIP)) {
|
2000-07-11 22:11:36 +00:00
|
|
|
unsigned alivesecs;
|
|
|
|
|
|
|
|
alivesecs = 0;
|
2001-08-14 16:05:52 +00:00
|
|
|
if (filter &&
|
|
|
|
FilterCheck(packet, family, &bundle->filter.alive, &alivesecs))
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
|
2000-07-11 22:11:36 +00:00
|
|
|
else if (psecs != NULL) {
|
|
|
|
if(*psecs == 0)
|
|
|
|
*psecs = alivesecs;
|
|
|
|
if (*psecs) {
|
|
|
|
if (*psecs != alivesecs)
|
2002-06-15 08:03:30 +00:00
|
|
|
log_Printf(LogTCPIP, "%s - (timeout = %d / ALIVE = %d secs)\n",
|
2000-07-11 22:11:36 +00:00
|
|
|
logbuf, *psecs, alivesecs);
|
|
|
|
else
|
|
|
|
log_Printf(LogTCPIP, "%s - (timeout = %d secs)\n", logbuf, *psecs);
|
|
|
|
} else
|
|
|
|
log_Printf(LogTCPIP, "%s\n", logbuf);
|
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
2000-03-14 01:46:49 +00:00
|
|
|
result = pri;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
2000-03-14 01:46:49 +00:00
|
|
|
|
2000-03-29 09:31:52 +00:00
|
|
|
if (filter && uh && ntohs(uh->uh_dport) == 53 && log_IsKept(LogDNS))
|
2000-03-14 01:46:49 +00:00
|
|
|
ip_LogDNS(uh, filter->name);
|
|
|
|
|
|
|
|
return result;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
static int
|
|
|
|
ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp, u_int32_t af)
|
1998-02-02 19:32:16 +00:00
|
|
|
{
|
1995-01-31 06:29:58 +00:00
|
|
|
int nb, nw;
|
1997-11-22 03:37:54 +00:00
|
|
|
struct tun_data tun;
|
2000-01-23 01:48:19 +00:00
|
|
|
char *data;
|
2000-07-11 22:11:36 +00:00
|
|
|
unsigned secs, alivesecs;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
nb = m_length(bp);
|
1999-05-14 09:35:51 +00:00
|
|
|
if (nb > sizeof tun.data) {
|
|
|
|
log_Printf(LogWARN, "ip_Input: %s: Packet too large (got %d, max %d)\n",
|
|
|
|
l->name, nb, (int)(sizeof tun.data));
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(bp);
|
2001-08-14 16:05:52 +00:00
|
|
|
return 0;
|
1999-05-14 09:35:51 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
mbuf_Read(bp, tun.data, nb);
|
1997-05-23 04:54:03 +00:00
|
|
|
|
2000-07-11 22:11:36 +00:00
|
|
|
secs = 0;
|
2001-08-14 16:05:52 +00:00
|
|
|
if (PacketCheck(bundle, af, tun.data, nb, &bundle->filter.in,
|
|
|
|
NULL, &secs) < 0)
|
|
|
|
return 0;
|
1998-03-24 18:47:32 +00:00
|
|
|
|
2000-07-11 22:11:36 +00:00
|
|
|
alivesecs = 0;
|
2001-08-14 16:05:52 +00:00
|
|
|
if (!FilterCheck(tun.data, af, &bundle->filter.alive, &alivesecs)) {
|
2000-07-11 22:11:36 +00:00
|
|
|
if (secs == 0)
|
|
|
|
secs = alivesecs;
|
|
|
|
bundle_StartIdleTimer(bundle, secs);
|
|
|
|
}
|
1998-03-24 18:47:32 +00:00
|
|
|
|
2000-01-23 01:48:19 +00:00
|
|
|
if (bundle->dev.header) {
|
2001-08-14 16:05:52 +00:00
|
|
|
tun.header.family = htonl(af);
|
2000-01-23 01:48:19 +00:00
|
|
|
nb += sizeof tun - sizeof tun.data;
|
|
|
|
data = (char *)&tun;
|
|
|
|
} else
|
|
|
|
data = tun.data;
|
|
|
|
|
|
|
|
nw = write(bundle->dev.fd, data, nb);
|
1999-05-08 11:07:56 +00:00
|
|
|
if (nw != nb) {
|
|
|
|
if (nw == -1)
|
1999-05-14 09:35:51 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %s\n",
|
|
|
|
l->name, nb, strerror(errno));
|
1999-05-08 11:07:56 +00:00
|
|
|
else
|
1999-05-14 09:35:51 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: %s: wrote %d, got %d\n", l->name, nb, nw);
|
1997-05-23 04:54:03 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
return nb;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
struct mbuf *
|
|
|
|
ipv4_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2001-08-14 16:05:52 +00:00
|
|
|
int nb;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
|
|
|
|
log_Printf(LogWARN, "ipv4_Input: IPCP not open - packet dropped\n");
|
|
|
|
m_freem(bp);
|
|
|
|
return NULL;
|
1998-08-26 17:39:37 +00:00
|
|
|
}
|
1998-08-25 17:48:43 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
m_settype(bp, MB_IPIN);
|
1997-08-25 00:29:32 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
nb = ip_Input(bundle, l, bp, AF_INET);
|
|
|
|
ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
|
1998-02-23 00:38:44 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
return NULL;
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
#ifndef NOINET6
|
|
|
|
struct mbuf *
|
|
|
|
ipv6_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2001-08-14 16:05:52 +00:00
|
|
|
int nb;
|
1998-03-24 18:47:32 +00:00
|
|
|
|
2001-08-18 19:07:13 +00:00
|
|
|
if (bundle->ncp.ipv6cp.fsm.state != ST_OPENED) {
|
2001-08-14 16:05:52 +00:00
|
|
|
log_Printf(LogWARN, "ipv6_Input: IPV6CP not open - packet dropped\n");
|
|
|
|
m_freem(bp);
|
|
|
|
return NULL;
|
2001-06-18 15:00:22 +00:00
|
|
|
}
|
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
m_settype(bp, MB_IPV6IN);
|
|
|
|
|
|
|
|
nb = ip_Input(bundle, l, bp, AF_INET6);
|
|
|
|
ipv6cp_AddInOctets(&bundle->ncp.ipv6cp, nb);
|
1998-03-24 18:47:32 +00:00
|
|
|
|
2001-08-14 16:05:52 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
2001-08-14 16:05:52 +00:00
|
|
|
#endif
|