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
|
|
|
*
|
1998-07-11 19:05:24 +00:00
|
|
|
* $Id: ip.c,v 1.48 1998/06/27 23:48:45 brian Exp $
|
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.
|
|
|
|
*/
|
1998-04-07 00:54:26 +00:00
|
|
|
#include <sys/types.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-11-22 03:37:54 +00:00
|
|
|
#ifndef NOALIAS
|
1997-05-23 04:54:03 +00:00
|
|
|
#include <alias.h>
|
1997-11-22 03:37:54 +00:00
|
|
|
#endif
|
1997-10-04 00:14:39 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#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"
|
1998-03-13 21:07:46 +00:00
|
|
|
#include "bundle.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
#include "vjcomp.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
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
static const u_short interactive_ports[32] = {
|
1997-08-25 00:29:32 +00:00
|
|
|
544, 513, 514, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 21, 22, 23, 0, 0, 0, 0, 0, 0, 0, 543,
|
1995-01-31 06:29:58 +00:00
|
|
|
};
|
|
|
|
|
1996-01-30 11:08:50 +00:00
|
|
|
#define INTERACTIVE(p) (interactive_ports[(p) & 0x1F] == (p))
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
static const char *TcpFlags[] = { "FIN", "SYN", "RST", "PSH", "ACK", "URG" };
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
static 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) {
|
1997-08-25 00:29:32 +00:00
|
|
|
case OP_EQ:
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check a packet against with defined filters
|
|
|
|
*/
|
|
|
|
static int
|
1998-03-16 22:52:54 +00:00
|
|
|
FilterCheck(struct ip *pip, struct filter *filter)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-04-16 18:30:54 +00:00
|
|
|
int gotinfo, cproto, estab, syn, finrst, n, len, didname;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct tcphdr *th;
|
|
|
|
struct udphdr *uh;
|
|
|
|
struct icmp *ih;
|
|
|
|
char *ptop;
|
|
|
|
u_short sport, dport;
|
1998-03-16 22:52:54 +00:00
|
|
|
struct filterent *fp = filter->rule;
|
1998-04-03 19:25:07 +00:00
|
|
|
char dbuff[100];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
if (fp->action) {
|
1998-04-16 18:30:54 +00:00
|
|
|
cproto = gotinfo = estab = syn = finrst = didname = 0;
|
1995-01-31 06:29:58 +00:00
|
|
|
sport = dport = 0;
|
|
|
|
for (n = 0; n < MAXFILTERS; n++) {
|
|
|
|
if (fp->action) {
|
1997-08-25 00:29:32 +00:00
|
|
|
/* permit fragments on in and out filter */
|
1998-03-16 22:52:54 +00:00
|
|
|
if (filter->fragok && (ntohs(pip->ip_off) & IP_OFFMASK) != 0)
|
1997-08-25 00:29:32 +00:00
|
|
|
return (A_PERMIT);
|
1998-03-16 22:52:54 +00:00
|
|
|
|
1998-04-03 19:25:07 +00:00
|
|
|
if (!didname)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, "%s filter:\n", filter->name);
|
1998-04-03 19:25:07 +00:00
|
|
|
didname = 1;
|
|
|
|
|
1997-11-12 21:04:21 +00:00
|
|
|
if ((pip->ip_src.s_addr & fp->smask.s_addr) ==
|
|
|
|
(fp->saddr.s_addr & fp->smask.s_addr) &&
|
|
|
|
(pip->ip_dst.s_addr & fp->dmask.s_addr) ==
|
|
|
|
(fp->daddr.s_addr & fp->dmask.s_addr)) {
|
1995-01-31 06:29:58 +00:00
|
|
|
if (fp->proto) {
|
|
|
|
if (!gotinfo) {
|
1997-08-25 00:29:32 +00:00
|
|
|
ptop = (char *) pip + (pip->ip_hl << 2);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (pip->ip_p) {
|
|
|
|
case IPPROTO_ICMP:
|
1997-08-25 00:29:32 +00:00
|
|
|
cproto = P_ICMP;
|
|
|
|
ih = (struct icmp *) ptop;
|
|
|
|
sport = ih->icmp_type;
|
1998-04-16 18:30:54 +00:00
|
|
|
estab = syn = finrst = -1;
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogDEBUG))
|
1998-04-03 19:25:07 +00:00
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d", sport);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_UDP:
|
1998-06-14 01:21:24 +00:00
|
|
|
case IPPROTO_IGMP:
|
|
|
|
case IPPROTO_IPIP:
|
1997-08-25 00:29:32 +00:00
|
|
|
cproto = P_UDP;
|
|
|
|
uh = (struct udphdr *) ptop;
|
|
|
|
sport = ntohs(uh->uh_sport);
|
|
|
|
dport = ntohs(uh->uh_dport);
|
1998-04-16 18:30:54 +00:00
|
|
|
estab = syn = finrst = -1;
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogDEBUG))
|
1998-04-03 19:25:07 +00:00
|
|
|
snprintf(dbuff, sizeof dbuff, "sport = %d, dport = %d",
|
|
|
|
sport, dport);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
case IPPROTO_TCP:
|
1997-08-25 00:29:32 +00:00
|
|
|
cproto = P_TCP;
|
|
|
|
th = (struct tcphdr *) ptop;
|
|
|
|
sport = ntohs(th->th_sport);
|
|
|
|
dport = ntohs(th->th_dport);
|
1995-01-31 06:29:58 +00:00
|
|
|
estab = (th->th_flags & TH_ACK);
|
1998-04-16 18:30:54 +00:00
|
|
|
syn = (th->th_flags & TH_SYN);
|
|
|
|
finrst = (th->th_flags & (TH_FIN|TH_RST));
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogDEBUG) && !estab)
|
1998-04-03 19:25:07 +00:00
|
|
|
snprintf(dbuff, sizeof dbuff,
|
|
|
|
"flags = %02x, sport = %d, dport = %d",
|
|
|
|
th->th_flags, sport, dport);
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
1998-04-03 19:25:07 +00:00
|
|
|
return (A_DENY); /* We'll block unknown type of packet */
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogDEBUG)) {
|
1998-04-03 19:25:07 +00:00
|
|
|
if (estab != -1) {
|
|
|
|
len = strlen(dbuff);
|
1998-04-16 18:30:54 +00:00
|
|
|
snprintf(dbuff + len, sizeof dbuff - len,
|
|
|
|
", estab = %d, syn = %d, finrst = %d",
|
|
|
|
estab, syn, finrst);
|
1998-04-03 19:25:07 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, " Filter: proto = %s, %s\n",
|
1998-04-03 19:25:07 +00:00
|
|
|
filter_Proto2Nam(cproto), dbuff);
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
gotinfo = 1;
|
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogDEBUG)) {
|
1998-04-03 19:25:07 +00:00
|
|
|
if (fp->opt.srcop != OP_NONE) {
|
|
|
|
snprintf(dbuff, sizeof dbuff, ", src %s %d",
|
|
|
|
filter_Op2Nam(fp->opt.srcop), fp->opt.srcport);
|
|
|
|
len = strlen(dbuff);
|
|
|
|
} else
|
|
|
|
len = 0;
|
|
|
|
if (fp->opt.dstop != OP_NONE) {
|
|
|
|
snprintf(dbuff + len, sizeof dbuff - len,
|
|
|
|
", dst %s %d", filter_Op2Nam(fp->opt.dstop),
|
|
|
|
fp->opt.dstport);
|
|
|
|
} else if (!len)
|
|
|
|
*dbuff = '\0';
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, " rule = %d: Address match, "
|
1998-04-03 19:25:07 +00:00
|
|
|
"check against proto %s%s, action = %s\n",
|
|
|
|
n, filter_Proto2Nam(fp->proto),
|
|
|
|
dbuff, filter_Action2Nam(fp->action));
|
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (cproto == fp->proto) {
|
|
|
|
if ((fp->opt.srcop == OP_NONE ||
|
1998-04-16 18:30:54 +00:00
|
|
|
PortMatch(fp->opt.srcop, sport, fp->opt.srcport)) &&
|
1995-01-31 06:29:58 +00:00
|
|
|
(fp->opt.dstop == OP_NONE ||
|
1998-04-16 18:30:54 +00:00
|
|
|
PortMatch(fp->opt.dstop, dport, fp->opt.dstport)) &&
|
|
|
|
(fp->opt.estab == 0 || estab) &&
|
|
|
|
(fp->opt.syn == 0 || syn) &&
|
|
|
|
(fp->opt.finrst == 0 || finrst)) {
|
1997-08-25 00:29:32 +00:00
|
|
|
return (fp->action);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Address is mached. Make a decision. */
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, " rule = %d: Address match, action = %s\n", n,
|
1998-04-03 19:25:07 +00:00
|
|
|
filter_Action2Nam(fp->action));
|
1997-08-25 00:29:32 +00:00
|
|
|
return (fp->action);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-04-03 19:25:07 +00:00
|
|
|
} else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogDEBUG, " rule = %d: Address mismatch\n", n);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
fp++;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (A_DENY); /* No rule is mached. Deny this packet */
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
return (A_PERMIT); /* No rule is given. Permit 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
|
1997-08-25 00:29:32 +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) {
|
1998-05-01 19:26:12 +00:00
|
|
|
bp = mbuf_Alloc(cnt, MB_IPIN);
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), ptr, cnt);
|
1998-05-01 19:26:12 +00:00
|
|
|
vj_SendFrame(bp);
|
1998-03-13 21:07:46 +00:00
|
|
|
ipcp_AddOutOctets(cnt);
|
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;
|
|
|
|
int pri = PRI_NORMAL;
|
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;
|
|
|
|
case IPPROTO_UDP:
|
1997-06-16 21:20:00 +00:00
|
|
|
if (logit && loglen < sizeof logbuf) {
|
1997-08-25 00:29:32 +00:00
|
|
|
uh = (struct udphdr *) ptop;
|
|
|
|
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;
|
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;
|
|
|
|
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;
|
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)
|
|
|
|
pri = PRI_FAST;
|
1996-01-30 11:08:50 +00:00
|
|
|
else if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0) {
|
1995-01-31 06:29:58 +00:00
|
|
|
if (INTERACTIVE(ntohs(th->th_sport)) || INTERACTIVE(ntohs(th->th_dport)))
|
1997-08-25 00:29:32 +00:00
|
|
|
pri = PRI_FAST;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
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
|
|
|
|
1998-03-16 22:52:54 +00:00
|
|
|
if ((FilterCheck(pip, filter) & A_DENY)) {
|
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) {
|
1998-03-24 18:47:32 +00:00
|
|
|
if (FilterCheck(pip, &bundle->filter.alive) & A_DENY)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
ip_Input(struct bundle *bundle, struct mbuf * bp)
|
1998-02-02 19:32:16 +00:00
|
|
|
{
|
1995-01-31 06:29:58 +00:00
|
|
|
u_char *cp;
|
|
|
|
struct mbuf *wp;
|
|
|
|
int nb, nw;
|
1997-11-22 03:37:54 +00:00
|
|
|
struct tun_data tun;
|
1998-03-24 18:47:32 +00:00
|
|
|
struct ip *pip = (struct ip *)tun.data;
|
1998-06-27 23:48:54 +00:00
|
|
|
#ifndef NOALIAS
|
1998-06-14 01:21:24 +00:00
|
|
|
struct ip *piip = (struct ip *)((char *)pip + (pip->ip_hl << 2));
|
1998-06-27 23:48:54 +00:00
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1997-11-16 22:15:11 +00:00
|
|
|
tun_fill_header(tun, AF_INET);
|
|
|
|
cp = tun.data;
|
1995-01-31 06:29:58 +00:00
|
|
|
nb = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
for (wp = bp; wp; wp = wp->next) { /* Copy to contiguous region */
|
1998-01-04 20:26:37 +00:00
|
|
|
if (sizeof tun.data - (cp - tun.data) < wp->cnt) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogWARN, "ip_Input: Packet too large (%d) - dropped\n",
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Length(bp));
|
|
|
|
mbuf_Free(bp);
|
1998-01-04 20:26:37 +00:00
|
|
|
return;
|
|
|
|
}
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(cp, MBUF_CTOP(wp), wp->cnt);
|
1995-01-31 06:29:58 +00:00
|
|
|
cp += wp->cnt;
|
|
|
|
nb += wp->cnt;
|
|
|
|
}
|
|
|
|
|
1997-11-22 03:37:54 +00:00
|
|
|
#ifndef NOALIAS
|
1998-06-27 14:17:28 +00:00
|
|
|
if (bundle->AliasEnabled && pip->ip_p != IPPROTO_IGMP &&
|
1998-06-14 01:21:24 +00:00
|
|
|
(pip->ip_p != IPPROTO_IPIP || !IN_CLASSD(ntohl(piip->ip_dst.s_addr)))) {
|
1997-11-22 03:37:54 +00:00
|
|
|
struct tun_data *frag;
|
1997-05-23 04:54:03 +00:00
|
|
|
int iresult;
|
|
|
|
char *fptr;
|
1996-12-12 14:39:47 +00:00
|
|
|
|
1998-06-27 14:17:28 +00:00
|
|
|
iresult = PacketAliasIn(tun.data, sizeof tun.data);
|
1997-11-16 22:15:11 +00:00
|
|
|
nb = ntohs(((struct ip *) tun.data)->ip_len);
|
1997-05-23 04:54:03 +00:00
|
|
|
|
|
|
|
if (nb > MAX_MRU) {
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogWARN, "ip_Input: Problem with IP header length\n");
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1997-05-23 04:54:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (iresult == PKT_ALIAS_OK
|
1997-08-25 00:29:32 +00:00
|
|
|
|| iresult == PKT_ALIAS_FOUND_HEADER_FRAGMENT) {
|
1998-03-16 22:52:54 +00:00
|
|
|
if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1997-08-25 00:29:32 +00:00
|
|
|
return;
|
1997-05-23 04:54:03 +00:00
|
|
|
}
|
1998-03-24 18:47:32 +00:00
|
|
|
|
|
|
|
if (!(FilterCheck(pip, &bundle->filter.alive) & A_DENY))
|
|
|
|
bundle_StartIdleTimer(bundle);
|
|
|
|
|
1998-03-13 21:07:46 +00:00
|
|
|
ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
|
1997-05-23 04:54:03 +00:00
|
|
|
|
1997-11-16 22:15:11 +00:00
|
|
|
nb = ntohs(((struct ip *) tun.data)->ip_len);
|
1997-12-24 09:29:17 +00:00
|
|
|
nb += sizeof tun - sizeof tun.data;
|
1998-05-06 23:50:25 +00:00
|
|
|
nw = write(bundle->dev.fd, &tun, nb);
|
1998-04-25 09:16:23 +00:00
|
|
|
if (nw != nb) {
|
1997-10-04 00:14:39 +00:00
|
|
|
if (nw == -1)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: wrote %d, got %s\n", nb,
|
1997-10-04 00:14:39 +00:00
|
|
|
strerror(errno));
|
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: wrote %d, got %d\n", nb, nw);
|
1998-04-25 09:16:23 +00:00
|
|
|
}
|
1997-05-23 04:54:03 +00:00
|
|
|
|
|
|
|
if (iresult == PKT_ALIAS_FOUND_HEADER_FRAGMENT) {
|
1998-06-27 14:17:28 +00:00
|
|
|
while ((fptr = PacketAliasGetFragment(tun.data)) != NULL) {
|
|
|
|
PacketAliasFragmentIn(tun.data, fptr);
|
1997-08-25 00:29:32 +00:00
|
|
|
nb = ntohs(((struct ip *) fptr)->ip_len);
|
1997-12-24 09:29:17 +00:00
|
|
|
frag = (struct tun_data *)
|
|
|
|
((char *)fptr - sizeof tun + sizeof tun.data);
|
|
|
|
nb += sizeof tun - sizeof tun.data;
|
1998-05-06 23:50:25 +00:00
|
|
|
nw = write(bundle->dev.fd, frag, nb);
|
1998-04-25 09:16:23 +00:00
|
|
|
if (nw != nb) {
|
1997-10-04 00:14:39 +00:00
|
|
|
if (nw == -1)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: wrote %d, got %s\n", nb,
|
1997-10-04 00:14:39 +00:00
|
|
|
strerror(errno));
|
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: wrote %d, got %d\n", nb, nw);
|
1998-04-25 09:16:23 +00:00
|
|
|
}
|
1997-11-16 22:15:11 +00:00
|
|
|
free(frag);
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-05-23 04:54:03 +00:00
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
} else if (iresult == PKT_ALIAS_UNRESOLVED_FRAGMENT) {
|
1997-11-16 22:15:11 +00:00
|
|
|
nb = ntohs(((struct ip *) tun.data)->ip_len);
|
1997-12-24 09:29:17 +00:00
|
|
|
nb += sizeof tun - sizeof tun.data;
|
1997-11-16 22:15:11 +00:00
|
|
|
frag = (struct tun_data *)malloc(nb);
|
|
|
|
if (frag == NULL)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogALERT, "ip_Input: Cannot allocate memory for fragment\n");
|
1997-05-23 04:54:03 +00:00
|
|
|
else {
|
1997-11-16 22:15:11 +00:00
|
|
|
tun_fill_header(*frag, AF_INET);
|
1997-12-24 09:29:17 +00:00
|
|
|
memcpy(frag->data, tun.data, nb - sizeof tun + sizeof tun.data);
|
1998-06-27 14:17:28 +00:00
|
|
|
PacketAliasSaveFragment(frag->data);
|
1997-05-23 04:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
1997-11-22 03:37:54 +00:00
|
|
|
} else
|
|
|
|
#endif /* #ifndef NOALIAS */
|
|
|
|
{ /* no aliasing */
|
1998-03-16 22:52:54 +00:00
|
|
|
if (PacketCheck(bundle, tun.data, nb, &bundle->filter.in) < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1997-05-23 04:54:03 +00:00
|
|
|
return;
|
|
|
|
}
|
1998-03-24 18:47:32 +00:00
|
|
|
|
|
|
|
if (!(FilterCheck(pip, &bundle->filter.alive) & A_DENY))
|
|
|
|
bundle_StartIdleTimer(bundle);
|
|
|
|
|
1998-03-13 21:07:46 +00:00
|
|
|
ipcp_AddInOctets(&bundle->ncp.ipcp, nb);
|
1998-03-24 18:47:32 +00:00
|
|
|
|
1997-12-24 09:29:17 +00:00
|
|
|
nb += sizeof tun - sizeof tun.data;
|
1998-05-06 23:50:25 +00:00
|
|
|
nw = write(bundle->dev.fd, &tun, nb);
|
1998-03-13 01:36:10 +00:00
|
|
|
if (nw != nb) {
|
1997-10-04 00:14:39 +00:00
|
|
|
if (nw == -1)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: wrote %d, got %s\n", nb, strerror(errno));
|
1997-10-04 00:14:39 +00:00
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "ip_Input: wrote %d, got %d\n", nb, nw);
|
1998-03-13 01:36:10 +00:00
|
|
|
}
|
1997-05-23 04:54:03 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
static struct mqueue IpOutputQueues[PRI_FAST + 1];
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
ip_Enqueue(int pri, char *ptr, int count)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
bp = mbuf_Alloc(count, MB_IPQ);
|
1997-10-26 01:04:02 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), ptr, count);
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Enqueue(&IpOutputQueues[pri], bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1995-05-30 03:57:47 +00:00
|
|
|
int
|
1998-02-23 00:38:44 +00:00
|
|
|
ip_QueueLen()
|
1995-03-11 15:18:55 +00:00
|
|
|
{
|
|
|
|
struct mqueue *queue;
|
1998-02-23 00:38:44 +00:00
|
|
|
int result = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1998-02-23 00:38:44 +00:00
|
|
|
for (queue = &IpOutputQueues[PRI_MAX]; queue >= IpOutputQueues; queue--)
|
|
|
|
result += queue->qlen;
|
|
|
|
|
|
|
|
return result;
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:21:56 +00:00
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
ip_FlushPacket(struct link *l, struct bundle *bundle)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mqueue *queue;
|
|
|
|
struct mbuf *bp;
|
1997-05-10 01:22:19 +00:00
|
|
|
int cnt;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-03-13 21:07:46 +00:00
|
|
|
if (bundle->ncp.ipcp.fsm.state != ST_OPENED)
|
1998-04-03 19:21:56 +00:00
|
|
|
return 0;
|
1998-03-24 18:47:32 +00:00
|
|
|
|
|
|
|
for (queue = &IpOutputQueues[PRI_FAST]; queue >= IpOutputQueues; queue--)
|
1995-01-31 06:29:58 +00:00
|
|
|
if (queue->top) {
|
1998-05-01 19:26:12 +00:00
|
|
|
bp = mbuf_Dequeue(queue);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (bp) {
|
1998-03-24 18:47:32 +00:00
|
|
|
struct ip *pip = (struct ip *)MBUF_CTOP(bp);
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
cnt = mbuf_Length(bp);
|
1998-03-24 18:47:32 +00:00
|
|
|
if (!(FilterCheck(pip, &bundle->filter.alive) & A_DENY))
|
|
|
|
bundle_StartIdleTimer(bundle);
|
1998-07-11 19:05:24 +00:00
|
|
|
vj_SendFrame(l, bp, bundle);
|
1998-03-13 21:07:46 +00:00
|
|
|
ipcp_AddOutOctets(&bundle->ncp.ipcp, cnt);
|
1998-04-03 19:21:56 +00:00
|
|
|
return 1;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
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
|
|
|
}
|