1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP IP Protocol 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-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
* o Return ICMP message for filterd packet
|
|
|
|
* and optionaly record it into log.
|
|
|
|
*/
|
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>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/tcp.h>
|
1996-01-11 17:48:59 +00:00
|
|
|
#include <arpa/inet.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>
|
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"
|
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
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1997-11-16 22:15:11 +00:00
|
|
|
#include "tun.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "ip.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-12-27 11:54:57 +00:00
|
|
|
static const char * const TcpFlags[] = {
|
|
|
|
"FIN", "SYN", "RST", "PSH", "ACK", "URG"
|
|
|
|
};
|
1995-01-31 06:29:58 +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:
|
1997-08-25 00:29:32 +00:00
|
|
|
return (pport == rport);
|
1995-01-31 06:29:58 +00:00
|
|
|
case OP_GT:
|
1997-08-25 00:29:32 +00:00
|
|
|
return (pport > rport);
|
1995-01-31 06:29:58 +00:00
|
|
|
case OP_LT:
|
1997-08-25 00:29:32 +00:00
|
|
|
return (pport < rport);
|
1995-01-31 06:29:58 +00:00
|
|
|
default:
|
1997-08-25 00:29:32 +00:00
|
|
|
return (0);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-08 11:07:56 +00:00
|
|
|
* Check a packet against a defined filter
|
1999-07-27 23:44:00 +00:00
|
|
|
* Returns 0 to accept the packet, non-zero to drop the packet
|
|
|
|
*
|
|
|
|
* 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.
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
static int
|
1999-07-27 23:44:00 +00:00
|
|
|
FilterCheck(const struct ip *pip, const struct filter *filter)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-07-27 23:44:00 +00:00
|
|
|
int gotinfo; /* true if IP payload decoded */
|
|
|
|
int cproto; /* P_* protocol type if (gotinfo) */
|
|
|
|
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 */
|
|
|
|
const struct filterent *fp = filter->rule;
|
1998-04-03 19:25:07 +00:00
|
|
|
char dbuff[100];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-07-27 23:44:00 +00:00
|
|
|
if (fp->f_action == A_NONE)
|
|
|
|
return (0); /* No rule is given. Permit this packet */
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
len = ntohs(pip->ip_off) & IP_OFFMASK; /* fragment offset */
|
|
|
|
if (len > 0) { /* Not first fragment within datagram */
|
|
|
|
if (len < (24 >> 3)) /* don't allow fragment to over-write header */
|
|
|
|
return (1);
|
|
|
|
/* permit fragments on in and out filter */
|
1999-09-16 18:47:59 +00:00
|
|
|
return (!filter->fragok);
|
1999-07-27 23:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cproto = gotinfo = estab = syn = finrst = didname = 0;
|
|
|
|
sport = dport = 0;
|
|
|
|
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;
|
|
|
|
if (!((pip->ip_src.s_addr ^ fp->f_src.ipaddr.s_addr) &
|
|
|
|
fp->f_src.mask.s_addr) &&
|
|
|
|
!((pip->ip_dst.s_addr ^ fp->f_dst.ipaddr.s_addr) &
|
|
|
|
fp->f_dst.mask.s_addr)) {
|
|
|
|
if (fp->f_proto != P_NONE) {
|
|
|
|
if (!gotinfo) {
|
|
|
|
const char *ptop = (const char *) pip + (pip->ip_hl << 2);
|
|
|
|
const struct tcphdr *th;
|
|
|
|
const struct udphdr *uh;
|
|
|
|
const struct icmp *ih;
|
|
|
|
int datalen; /* IP datagram length */
|
|
|
|
|
|
|
|
datalen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
|
|
|
|
switch (pip->ip_p) {
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
cproto = P_ICMP;
|
|
|
|
if (datalen < 8) /* ICMP must be at least 8 octets */
|
|
|
|
return (1);
|
|
|
|
ih = (const struct icmp *) ptop;
|
|
|
|
sport = ih->icmp_type;
|
|
|
|
estab = syn = finrst = -1;
|
|
|
|
if (log_IsKept(LogDEBUG))
|
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
|
|
|
|
break;
|
|
|
|
case IPPROTO_IGMP:
|
|
|
|
cproto = P_IGMP;
|
|
|
|
if (datalen < 8) /* IGMP uses 8-octet messages */
|
|
|
|
return (1);
|
|
|
|
estab = syn = finrst = -1;
|
|
|
|
sport = ntohs(0);
|
|
|
|
break;
|
1999-09-30 07:23:13 +00:00
|
|
|
#ifdef IPPROTO_GRE
|
|
|
|
case IPPROTO_GRE:
|
|
|
|
cproto = P_GRE;
|
|
|
|
if (datalen < 2) /* GRE uses 2-octet+ messages */
|
|
|
|
return (1);
|
|
|
|
estab = syn = finrst = -1;
|
|
|
|
sport = ntohs(0);
|
|
|
|
break;
|
|
|
|
#endif
|
1999-08-02 15:29:19 +00:00
|
|
|
#ifdef IPPROTO_OSPFIGP
|
1999-08-02 11:53:16 +00:00
|
|
|
case IPPROTO_OSPFIGP:
|
|
|
|
cproto = P_OSPF;
|
|
|
|
if (datalen < 8) /* IGMP uses 8-octet messages */
|
|
|
|
return (1);
|
|
|
|
estab = syn = finrst = -1;
|
|
|
|
sport = ntohs(0);
|
|
|
|
break;
|
1999-08-02 15:29:19 +00:00
|
|
|
#endif
|
1999-07-27 23:44:00 +00:00
|
|
|
case IPPROTO_UDP:
|
|
|
|
case IPPROTO_IPIP:
|
|
|
|
cproto = P_UDP;
|
|
|
|
if (datalen < 8) /* UDP header is 8 octets */
|
|
|
|
return (1);
|
|
|
|
uh = (const struct udphdr *) ptop;
|
|
|
|
sport = ntohs(uh->uh_sport);
|
|
|
|
dport = ntohs(uh->uh_dport);
|
|
|
|
estab = syn = finrst = -1;
|
|
|
|
if (log_IsKept(LogDEBUG))
|
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
|
|
|
|
sport, dport);
|
|
|
|
break;
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
cproto = P_TCP;
|
|
|
|
th = (const struct tcphdr *) ptop;
|
|
|
|
/* TCP headers are variable length. The following code
|
|
|
|
* ensures that the TCP header length isn't de-referenced if
|
|
|
|
* the datagram is too short
|
|
|
|
*/
|
|
|
|
if (datalen < 20 || datalen < (th->th_off << 2))
|
|
|
|
return (1);
|
|
|
|
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';
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (1); /* We'll block unknown type of packet */
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
|
|
|
|
filter_Proto2Nam(cproto), dbuff);
|
|
|
|
}
|
|
|
|
gotinfo = 1;
|
|
|
|
}
|
|
|
|
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, "
|
|
|
|
"check against proto %s%s, action = %s\n",
|
|
|
|
n, filter_Proto2Nam(fp->f_proto),
|
|
|
|
dbuff, filter_Action2Nam(fp->f_action));
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 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)
|
|
|
|
fp = &filter->rule[n = fp->f_action];
|
|
|
|
else
|
|
|
|
return (fp->f_action != A_PERMIT);
|
|
|
|
} else {
|
|
|
|
n++;
|
1995-01-31 06:29:58 +00:00
|
|
|
fp++;
|
|
|
|
}
|
|
|
|
}
|
1999-07-27 23:44:00 +00:00
|
|
|
return (1); /* No rule is mached. Deny this packet */
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-03-16 22:52:54 +00:00
|
|
|
#ifdef notdef
|
1995-01-31 06:29:58 +00:00
|
|
|
static void
|
1999-05-09 20:02:29 +00:00
|
|
|
IcmpError(struct ip *pip, int code)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
|
|
|
if (pip->ip_p != IPPROTO_ICMP) {
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = m_get(m_len, MB_IPIN);
|
|
|
|
memcpy(MBUF_CTOP(bp), ptr, m_len);
|
1998-05-01 19:26:12 +00:00
|
|
|
vj_SendFrame(bp);
|
1999-12-20 20:29:47 +00:00
|
|
|
ipcp_AddOutOctets(m_len);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1998-03-16 22:52:54 +00:00
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For debugging aid.
|
|
|
|
*/
|
|
|
|
int
|
1998-03-16 22:52:54 +00:00
|
|
|
PacketCheck(struct bundle *bundle, char *cp, int nb, struct filter *filter)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct ip *pip;
|
|
|
|
struct tcphdr *th;
|
|
|
|
struct udphdr *uh;
|
|
|
|
struct icmp *icmph;
|
|
|
|
char *ptop;
|
|
|
|
int mask, len, n;
|
1999-09-04 00:00:21 +00:00
|
|
|
int pri = 0;
|
1997-06-16 21:20:00 +00:00
|
|
|
int logit, loglen;
|
1998-06-15 19:06:58 +00:00
|
|
|
char logbuf[200];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
logit = log_IsKept(LogTCPIP) && filter->logok;
|
1997-06-16 21:20:00 +00:00
|
|
|
loglen = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
pip = (struct ip *) cp;
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
1998-03-16 22:52:54 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, "%s ", filter->name);
|
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
|
|
|
ptop = (cp + (pip->ip_hl << 2));
|
|
|
|
|
|
|
|
switch (pip->ip_p) {
|
|
|
|
case IPPROTO_ICMP:
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
1997-08-25 00:29:32 +00:00
|
|
|
icmph = (struct icmp *) ptop;
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"ICMP: %s:%d ---> ", inet_ntoa(pip->ip_src), icmph->icmp_type);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s:%d", inet_ntoa(pip->ip_dst), icmph->icmp_type);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case IPPROTO_UDP:
|
1999-09-07 07:51:11 +00:00
|
|
|
uh = (struct udphdr *) ptop;
|
|
|
|
if (pip->ip_tos == IPTOS_LOWDELAY)
|
|
|
|
pri++;
|
|
|
|
|
|
|
|
if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
|
|
|
|
ipcp_IsUrgentUdpPort(&bundle->ncp.ipcp, ntohs(uh->uh_sport),
|
|
|
|
ntohs(uh->uh_dport)))
|
|
|
|
pri++;
|
|
|
|
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"UDP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
|
|
|
|
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,
|
|
|
|
"GRE: %s ---> ", inet_ntoa(pip->ip_src));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s", inet_ntoa(pip->ip_dst));
|
|
|
|
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,
|
|
|
|
"OSPF: %s ---> ", inet_ntoa(pip->ip_src));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s", inet_ntoa(pip->ip_dst));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
1999-08-02 15:29:19 +00:00
|
|
|
#endif
|
1999-09-07 07:51:11 +00:00
|
|
|
|
1998-06-14 01:21:24 +00:00
|
|
|
case IPPROTO_IPIP:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
uh = (struct udphdr *) ptop;
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"IPIP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
1998-06-14 01:21:24 +00:00
|
|
|
case IPPROTO_IGMP:
|
|
|
|
if (logit && loglen < sizeof logbuf) {
|
|
|
|
uh = (struct udphdr *) ptop;
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"IGMP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(uh->uh_sport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s:%d", inet_ntoa(pip->ip_dst), ntohs(uh->uh_dport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
|
|
|
break;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
case IPPROTO_TCP:
|
1997-08-25 00:29:32 +00:00
|
|
|
th = (struct tcphdr *) ptop;
|
1995-01-31 06:29:58 +00:00
|
|
|
if (pip->ip_tos == IPTOS_LOWDELAY)
|
1999-09-04 00:00:21 +00:00
|
|
|
pri++;
|
1999-09-07 07:51:11 +00:00
|
|
|
|
|
|
|
if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0 &&
|
|
|
|
ipcp_IsUrgentTcpPort(&bundle->ncp.ipcp, 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) {
|
1995-01-31 06:29:58 +00:00
|
|
|
len = ntohs(pip->ip_len) - (pip->ip_hl << 2) - (th->th_off << 2);
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"TCP: %s:%d ---> ", inet_ntoa(pip->ip_src), ntohs(th->th_sport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
"%s:%d", inet_ntoa(pip->ip_dst), ntohs(th->th_dport));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
1995-01-31 06:29:58 +00:00
|
|
|
n = 0;
|
|
|
|
for (mask = TH_FIN; mask != 0x40; mask <<= 1) {
|
1997-06-16 21:20:00 +00:00
|
|
|
if (th->th_flags & mask) {
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen, " %s", TcpFlags[n]);
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
n++;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
1998-06-27 23:48:54 +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) {
|
1997-08-25 00:29:32 +00:00
|
|
|
u_short *sp;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
ptop += 20;
|
1997-08-25 00:29:32 +00:00
|
|
|
sp = (u_short *) ptop;
|
1997-06-16 21:20:00 +00:00
|
|
|
if (ntohs(sp[0]) == 0x0204) {
|
1997-08-25 00:29:32 +00:00
|
|
|
snprintf(logbuf + loglen, sizeof logbuf - loglen,
|
|
|
|
" MSS = %d", ntohs(sp[1]));
|
|
|
|
loglen += strlen(logbuf + loglen);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1997-06-16 21:20:00 +00:00
|
|
|
|
1999-07-27 23:44:00 +00:00
|
|
|
if (FilterCheck(pip, filter)) {
|
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);
|
1998-03-16 22:52:54 +00:00
|
|
|
#ifdef notdef
|
1997-08-25 00:29:32 +00:00
|
|
|
if (direction == 0)
|
|
|
|
IcmpError(pip, pri);
|
1998-03-16 22:52:54 +00:00
|
|
|
#endif
|
1997-08-25 00:29:32 +00:00
|
|
|
return (-1);
|
1995-01-31 06:29:58 +00:00
|
|
|
} else {
|
1998-03-16 22:52:54 +00:00
|
|
|
/* Check Keep Alive filter */
|
1998-04-25 10:49:52 +00:00
|
|
|
if (logit) {
|
1999-07-27 23:44:00 +00:00
|
|
|
if (FilterCheck(pip, &bundle->filter.alive))
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTCPIP, "%s - NO KEEPALIVE\n", logbuf);
|
1998-03-24 18:47:32 +00:00
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTCPIP, "%s\n", logbuf);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (pri);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
struct mbuf *
|
|
|
|
ip_Input(struct bundle *bundle, struct link *l, struct mbuf *bp)
|
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;
|
1999-05-08 11:07:56 +00:00
|
|
|
struct ip *pip;
|
2000-01-23 01:48:19 +00:00
|
|
|
char *data;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
if (bundle->ncp.ipcp.fsm.state != ST_OPENED) {
|
|
|
|
log_Printf(LogWARN, "ip_Input: IPCP not open - packet dropped\n");
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
m_settype(bp, MB_IPIN);
|
|
|
|
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);
|
1999-05-14 09:35:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
mbuf_Read(bp, tun.data, nb);
|
1997-05-23 04:54:03 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0)
|
|
|
|
return NULL;
|
1998-03-24 18:47:32 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
pip = (struct ip *)tun.data;
|
1999-07-27 23:44:00 +00:00
|
|
|
if (!FilterCheck(pip, &bundle->filter.alive))
|
1999-05-08 11:07:56 +00:00
|
|
|
bundle_StartIdleTimer(bundle);
|
1998-03-24 18:47:32 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
|
1998-03-24 18:47:32 +00:00
|
|
|
|
2000-01-23 01:48:19 +00:00
|
|
|
if (bundle->dev.header) {
|
|
|
|
tun.family = htonl(AF_INET);
|
|
|
|
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
|
|
|
|
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-08-26 17:39:37 +00:00
|
|
|
ip_Enqueue(struct ipcp *ipcp, int pri, char *ptr, int count)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1999-09-04 00:00:21 +00:00
|
|
|
if (pri < 0 || pri >= IPCP_QUEUES(ipcp))
|
1998-08-26 17:39:37 +00:00
|
|
|
log_Printf(LogERROR, "Can't store in ip queue %d\n", pri);
|
|
|
|
else {
|
1999-05-08 11:07:56 +00:00
|
|
|
/*
|
|
|
|
* We allocate an extra 6 bytes, four at the front and two at the end.
|
|
|
|
* This is an optimisation so that we need to do less work in
|
1999-12-20 20:29:47 +00:00
|
|
|
* m_prepend() in acf_LayerPush() and proto_LayerPush() and
|
1999-05-08 11:07:56 +00:00
|
|
|
* appending in hdlc_LayerPush().
|
|
|
|
*/
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = m_get(count + 6, MB_IPOUT);
|
|
|
|
bp->m_offset += 4;
|
|
|
|
bp->m_len -= 6;
|
1998-08-26 17:39:37 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), ptr, count);
|
1999-12-20 20:29:47 +00:00
|
|
|
m_enqueue(ipcp->Queue + pri, bp);
|
1998-08-26 17:39:37 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1998-08-25 17:48:43 +00:00
|
|
|
void
|
1998-08-26 17:39:37 +00:00
|
|
|
ip_DeleteQueue(struct ipcp *ipcp)
|
1998-08-25 17:48:43 +00:00
|
|
|
{
|
|
|
|
struct mqueue *queue;
|
|
|
|
|
1999-09-04 00:00:21 +00:00
|
|
|
for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++)
|
1998-08-25 17:48:43 +00:00
|
|
|
while (queue->top)
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(m_dequeue(queue));
|
1998-08-25 17:48:43 +00:00
|
|
|
}
|
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
size_t
|
1998-08-26 17:39:37 +00:00
|
|
|
ip_QueueLen(struct ipcp *ipcp)
|
1995-03-11 15:18:55 +00:00
|
|
|
{
|
|
|
|
struct mqueue *queue;
|
1999-12-20 20:29:47 +00:00
|
|
|
size_t result;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1999-12-20 20:29:47 +00:00
|
|
|
result = 0;
|
1999-09-04 00:00:21 +00:00
|
|
|
for (queue = ipcp->Queue; queue < ipcp->Queue + IPCP_QUEUES(ipcp); queue++)
|
1999-12-20 20:29:47 +00:00
|
|
|
result += queue->len;
|
1998-02-23 00:38:44 +00:00
|
|
|
|
|
|
|
return result;
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:21:56 +00:00
|
|
|
int
|
1999-05-08 11:07:56 +00:00
|
|
|
ip_PushPacket(struct link *l, struct bundle *bundle)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-08-26 17:39:37 +00:00
|
|
|
struct ipcp *ipcp = &bundle->ncp.ipcp;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct mqueue *queue;
|
|
|
|
struct mbuf *bp;
|
1999-05-08 11:07:56 +00:00
|
|
|
struct ip *pip;
|
1999-12-20 20:29:47 +00:00
|
|
|
int m_len;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-08-26 17:39:37 +00:00
|
|
|
if (ipcp->fsm.state != ST_OPENED)
|
1998-04-03 19:21:56 +00:00
|
|
|
return 0;
|
1998-03-24 18:47:32 +00:00
|
|
|
|
1999-09-04 00:00:21 +00:00
|
|
|
queue = ipcp->Queue + IPCP_QUEUES(ipcp) - 1;
|
|
|
|
do {
|
1995-01-31 06:29:58 +00:00
|
|
|
if (queue->top) {
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = m_pullup(m_dequeue(queue));
|
|
|
|
m_len = m_length(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
pip = (struct ip *)MBUF_CTOP(bp);
|
1999-07-27 23:44:00 +00:00
|
|
|
if (!FilterCheck(pip, &bundle->filter.alive))
|
1999-05-08 11:07:56 +00:00
|
|
|
bundle_StartIdleTimer(bundle);
|
1999-09-04 00:00:21 +00:00
|
|
|
link_PushPacket(l, bp, bundle, 0, PROTO_IP);
|
1999-12-20 20:29:47 +00:00
|
|
|
ipcp_AddOutOctets(ipcp, m_len);
|
1999-05-08 11:07:56 +00:00
|
|
|
return 1;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-09-04 00:00:21 +00:00
|
|
|
} while (queue-- != ipcp->Queue);
|
1998-03-24 18:47:32 +00:00
|
|
|
|
1998-04-03 19:21:56 +00:00
|
|
|
return 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|